|
| 1 | +/* ############################# MPC License ############################## */ |
| 2 | +/* # Wed Nov 19 15:19:19 CET 2008 # */ |
| 3 | +/* # Copyright or (C) or Copr. Commissariat a l'Energie Atomique # */ |
| 4 | +/* # # */ |
| 5 | +/* # IDDN.FR.001.230040.000.S.P.2007.000.10000 # */ |
| 6 | +/* # This file is part of the MPC Runtime. # */ |
| 7 | +/* # # */ |
| 8 | +/* # This software is governed by the CeCILL-C license under French law # */ |
| 9 | +/* # and abiding by the rules of distribution of free software. You can # */ |
| 10 | +/* # use, modify and/ or redistribute the software under the terms of # */ |
| 11 | +/* # the CeCILL-C license as circulated by CEA, CNRS and INRIA at the # */ |
| 12 | +/* # following URL http://www.cecill.info. # */ |
| 13 | +/* # # */ |
| 14 | +/* # The fact that you are presently reading this means that you have # */ |
| 15 | +/* # had knowledge of the CeCILL-C license and that you accept its # */ |
| 16 | +/* # terms. # */ |
| 17 | +/* # # */ |
| 18 | +/* # Authors: # */ |
| 19 | +/* # - Roussel Adrien [email protected] # */ |
| 20 | +/* # # */ |
| 21 | +/* ######################################################################## */ |
| 22 | + |
| 23 | +#include "sctk_alloc_pmem.h" |
| 24 | + |
| 25 | +SCTK_PUBLIC sctk_pmem_desc_t* sctk_pmem_create(const char* dir, size_t* size) |
| 26 | +{ |
| 27 | + |
| 28 | + sctk_pmem_desc_t *cur_desc = NULL; |
| 29 | + cur_desc = (sctk_pmem_desc_t*) malloc(sizeof(sctk_pmem_desc_t)); |
| 30 | + cur_desc->cur_size = 0x0; |
| 31 | + cur_desc->max_size = 0x0; |
| 32 | + cur_desc->ptr = NULL; |
| 33 | + cur_desc->start = NULL; |
| 34 | + |
| 35 | + SCTK_PMEM_FILE_TYPE type = TYPE_ERROR; |
| 36 | + long int alignment = sysconf(_SC_PAGE_SIZE); |
| 37 | + |
| 38 | + /* Print Banner */ |
| 39 | + fprintf(stdout, "System Page size: %ld\n", alignment); |
| 40 | + |
| 41 | + type = get_file_type(dir); |
| 42 | + |
| 43 | + switch(type) |
| 44 | + { |
| 45 | + case TYPE_REGULAR: |
| 46 | + fprintf(stdout, "File Type: Regular\n"); |
| 47 | + break; |
| 48 | + case TYPE_DEVDAX: |
| 49 | + fprintf(stdout, "File Type: Device\n"); |
| 50 | + break; |
| 51 | + case TYPE_ERROR: |
| 52 | + default: |
| 53 | + fprintf(stdout, "File Type: Unknown\n"); |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + |
| 57 | + *size = SCTK_ROUNDUP(*size, alignment); |
| 58 | + fprintf(stdout, "Requested size: %ld bytes\n", *size); |
| 59 | + |
| 60 | + if(type == TYPE_REGULAR) |
| 61 | + { |
| 62 | +#ifdef O_TMPFILE |
| 63 | + |
| 64 | + int fd = create_tmp_file(dir); |
| 65 | + |
| 66 | + /* Declare random access pattern for fd */ |
| 67 | + if(declare_random_access(fd, *size) != 0) |
| 68 | + { |
| 69 | + fprintf(stderr, "Error: fail of advise access pattern for pmem\n"); |
| 70 | + return NULL; |
| 71 | + } |
| 72 | + |
| 73 | + /* Allocate the file space for fd */ |
| 74 | + if(allocate_file_space(fd, *size) != 0) |
| 75 | + { |
| 76 | + fprintf(stderr, "Error: unable to ensure that pmem space has been allocated..\n"); |
| 77 | + return NULL; |
| 78 | + } |
| 79 | + |
| 80 | + void* base = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_SHARED_VALIDATE|MAP_SYNC, fd, 0); |
| 81 | + if(base == NULL) |
| 82 | + { |
| 83 | + fprintf(stderr, "Error: unable to map a file into %s\n", dir); |
| 84 | + return NULL; |
| 85 | + } |
| 86 | + |
| 87 | + close(fd); |
| 88 | + |
| 89 | + cur_desc->max_size = *size; |
| 90 | + cur_desc->ptr = base; |
| 91 | + cur_desc->start = base; |
| 92 | + |
| 93 | + return cur_desc; |
| 94 | + |
| 95 | +#else |
| 96 | + fprintf(stderr, "O_TMPFILE is not defined on the system...\n"); |
| 97 | + return NULL; |
| 98 | +#endif |
| 99 | + } |
| 100 | + else if(type == TYPE_DEVDAX) |
| 101 | + { |
| 102 | + fprintf(stderr, "Error: not supported now...\n"); |
| 103 | + free(cur_desc); |
| 104 | + return NULL; |
| 105 | + } |
| 106 | + |
| 107 | + free(cur_desc); |
| 108 | + return NULL; |
| 109 | +} |
| 110 | + |
| 111 | +SCTK_PUBLIC int sctk_pmem_destroy(sctk_pmem_desc_t* base, size_t size) |
| 112 | +{ |
| 113 | + int ret = munmap(base->start, size); |
| 114 | + base->ptr = NULL; |
| 115 | + free(base); |
| 116 | + return ret; |
| 117 | +} |
| 118 | + |
| 119 | +SCTK_INTERN SCTK_PMEM_FILE_TYPE get_file_type(const char* dir) |
| 120 | +{ |
| 121 | + fprintf(stdout, "Checking directory \"%s\"...\n", dir); |
| 122 | + |
| 123 | + /* Check the access to the file 'dev' */ |
| 124 | + if(access(dir, F_OK) != 0) |
| 125 | + { |
| 126 | + fprintf(stderr, "Can't access to \"%s\"\n", dir); |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + struct stat st; |
| 131 | + /* Get Information about the file pointed by 'dev' */ |
| 132 | + stat(dir, &st); |
| 133 | + |
| 134 | + /* Check if 'st' point to a character device (DEVDAX = char device) */ |
| 135 | + if(S_ISCHR(st.st_mode)) |
| 136 | + { |
| 137 | + /* Get the device ID */ |
| 138 | + dev_t device_id = st.st_rdev; |
| 139 | + |
| 140 | + /* Get major (class of the device) and minor */ |
| 141 | + unsigned int major = major(device_id); |
| 142 | + unsigned int minor = minor(device_id); |
| 143 | + |
| 144 | + char path[8192]; |
| 145 | + snprintf(path, 8192, "/sys/dev/char/%u:%u/subsystem", major, minor); |
| 146 | + |
| 147 | + fprintf(stdout, "Device Path: %s\n", path); |
| 148 | + |
| 149 | + char rpath[8192]; |
| 150 | + char* tpath = realpath(path, rpath); |
| 151 | + if(tpath == NULL) |
| 152 | + { |
| 153 | + fprintf(stderr, "Unable to resolve device path...\n"); |
| 154 | + return -1; |
| 155 | + } |
| 156 | + fprintf(stdout, "Real Path: %s\n", rpath); |
| 157 | + |
| 158 | + /*TODO: Is the real path containing "/dev/dax" string? |
| 159 | + * If yes: DEVDAX supported |
| 160 | + * If no: error |
| 161 | + * */ |
| 162 | + |
| 163 | + return TYPE_DEVDAX; |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + /* Check if dev refers to a directory */ |
| 168 | + if((st.st_mode & S_IFMT) != S_IFDIR) |
| 169 | + { |
| 170 | + fprintf(stderr, "Fatal Error: %s is not a directory\n", dir); |
| 171 | + return TYPE_ERROR; |
| 172 | + } |
| 173 | + |
| 174 | + return TYPE_REGULAR; |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +SCTK_INTERN int create_tmp_file(const char* dir) |
| 179 | +{ |
| 180 | + /* Create a new temporary file (read/write permissions) into dir directory */ |
| 181 | + //int fd = open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); |
| 182 | + int fd = open(dir, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); |
| 183 | + if(fd < 0) |
| 184 | + { |
| 185 | + fprintf(stderr, "Fatal Error: unable to open directory %s (Error code: %d)\n", dir, errno); |
| 186 | + return -1; |
| 187 | + } |
| 188 | + return fd; |
| 189 | +} |
| 190 | + |
| 191 | +/* Predeclaration of random access pattern for fd */ |
| 192 | +SCTK_INTERN int declare_random_access(int fd, const size_t size) |
| 193 | +{ |
| 194 | + return posix_fadvise(fd, 0, size, POSIX_FADV_RANDOM); |
| 195 | +} |
| 196 | + |
| 197 | +/* Try to allocate space into file fd to ensure that memory space is allocated */ |
| 198 | +SCTK_INTERN int allocate_file_space(int fd, const size_t size) |
| 199 | +{ |
| 200 | + return posix_fallocate(fd, 0, size); |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +SCTK_PUBLIC void* sctk_pmem_malloc(sctk_pmem_desc_t* desc, size_t size) |
| 205 | +{ |
| 206 | + void* ret = NULL; |
| 207 | + if(size + desc->cur_size <= desc->max_size) |
| 208 | + { |
| 209 | + desc->cur_size += size; |
| 210 | + ret = desc->ptr; |
| 211 | + desc->ptr += size; |
| 212 | + } |
| 213 | + return ret; |
| 214 | +} |
| 215 | + |
| 216 | +SCTK_PUBLIC void sctk_pmem_free(sctk_pmem_desc_t* desc, size_t size, void* ptr) |
| 217 | +{ |
| 218 | + memset(ptr, 0x0, size); |
| 219 | +} |
0 commit comments