SIONlib  1.7.7
Scalable I/O library for parallel access to task-local files
sion_cache.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** SIONLIB http://www.fz-juelich.de/jsc/sionlib **
3 *****************************************************************************
4 ** Copyright (c) 2008-2019 **
5 ** Forschungszentrum Juelich, Juelich Supercomputing Centre **
6 ** **
7 ** See the file COPYRIGHT in the package base directory for details **
8 ****************************************************************************/
9 #define _XOPEN_SOURCE 700
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <time.h>
15 #include <assert.h>
16 
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 
23 #include "sion.h"
24 #include "sion_debug.h"
25 #include "sion_error_handler.h"
26 #include "sion_internal.h"
27 #include "sion_cache.h"
28 
29 
30 #if defined(_SION_LINUX)
31 #include <sys/mman.h>
32 #elif defined(_SION_AIX)
33 #elif defined(_SION_BGP)
34 #endif
35 
36 
58  const char *t;
59  int rc = SION_SUCCESS;
60 
61  t = _sion_getenv("SION_CACHESIZE");
62  if(t) {
63  sion_filedesc->usecache=1;
64  sion_filedesc->cachesize=atoi(t);
65  sion_filedesc->flag1 |= _SION_FLAG1_USECACHE;
66  }
67  DPRINTFP((2, "_sion_cache_check_env", -1, "usecache=%d cachesize=%d flag1=%d\n", sion_filedesc->usecache, sion_filedesc->cachesize, sion_filedesc->flag1));
68 
69  return (rc);
70 }
71 
72 
79 int _sion_cache_init(_sion_filedesc *sion_filedesc) {
80  int rc = SION_SUCCESS;
81 
82  /* allocation */
83  if(sion_filedesc->usecache) {
84  if(sion_filedesc->mode == SION_FILEMODE_WRITE) {
85 #if defined(_SION_LINUX)
86  rc=_sion_cache_create_linux(sion_filedesc);
87 #elif defined(_SION_AIX)
88 #elif defined(_SION_BGP)
89 #endif
90  } else if(sion_filedesc->mode == SION_FILEMODE_READ) {
91 #if defined(_SION_LINUX)
92  rc=_sion_cache_load_linux(sion_filedesc);
93 #elif defined(_SION_AIX)
94 #elif defined(_SION_BGP)
95 #endif
96  } else {
97  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_cache_init: unknown SION mode\n");
98  }
99  }
100 
101  return (rc);
102 }
103 
104 
111 int _sion_cache_destroy(_sion_filedesc *sion_filedesc) {
112  int rc = SION_SUCCESS;
113 
114  /* deallocation */
115  if(sion_filedesc->usecache) {
116 #if defined(_SION_LINUX)
117  rc=_sion_cache_destroy_linux(sion_filedesc);
118 #elif defined(_SION_AIX)
119 #elif defined(_SION_BGP)
120 #endif
121  }
122 
123  return (rc);
124 }
125 
133  int rc = SION_SUCCESS;
134 
135  /* #NODIST# */
136 #ifdef _SION_CACHE
137 
138 #if defined(_SION_LINUX)
139  int shmfd;
140  char* shmptr;
141 
142  if(sion_filedesc->rank>0) sprintf(sion_filedesc->cachefn,"sion_%07d",sion_filedesc->rank);
143  else sprintf(sion_filedesc->cachefn,"sion_%07d",0);
144 
145  /* creating the shared memory object -- shm_open() */
146  shmfd = shm_open(sion_filedesc->cachefn, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG);
147  if (shmfd < 0) {
148  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_cache_create_linux: could not create segment\n");
149  }
150  DPRINTFP((2, "_sion_cache_create_linux", -1, "after shm_open %s shmfd=%d\n", sion_filedesc->cachefn, shmfd));
151 
152  /* adjusting mapped file size (make room for the whole segment to map) -- ftruncate() */
153  ftruncate(shmfd, sion_filedesc->cachesize);
154 
155  /* requesting the shared segment -- mmap() */
156  shmptr = (char *)mmap(NULL, sion_filedesc->cachesize, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
157  if (shmptr == NULL) {
158  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_cache_create_linux: could not mmap segment\n");
159  }
160  DPRINTFP((2, "_sion_cache_create_linux", -1, "after mmap %d bytes\n", sion_filedesc->cachesize));
161 
162  sion_filedesc->cacheid=shmfd;
163  sion_filedesc->cacheptr=shmptr;
164 #endif
165 
166 #endif
167  /* #NODIST# */
168  return(rc);
169 }
170 
178  int rc = SION_SUCCESS;
179  /* #NODIST# */
180 #ifdef _SION_CACHE
181 
182 #if defined(_SION_LINUX)
183  int shmfd;
184  char* shmptr;
185 
186  if(sion_filedesc->rank>0) sprintf(sion_filedesc->cachefn,"sion_%07d",sion_filedesc->rank);
187  else sprintf(sion_filedesc->cachefn,"sion_%07d",0);
188 
189  /* creating the shared memory object -- shm_open() */
190  shmfd = shm_open(sion_filedesc->cachefn, O_RDWR, S_IRWXU | S_IRWXG);
191  if (shmfd < 0) {
192  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_cache_losd_linux: could not load segment\n");
193  }
194  DPRINTFP((2, "_sion_cache_create_linux", -1, "after shm_open %s shmfd=%d\n", sion_filedesc->cachefn, shmfd));
195 
196  /* adjusting mapped file size (make room for the whole segment to map) -- ftruncate() */
197  ftruncate(shmfd, sion_filedesc->cachesize);
198 
199  /* requesting the shared segment -- mmap() */
200  shmptr = (char *)mmap(NULL, sion_filedesc->cachesize, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
201  if (shmptr == NULL) {
202  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_cache_create_linux: could not mmap segment\n");
203  }
204  DPRINTFP((2, "_sion_cache_create_linux", -1, "after mmap %d bytes\n", sion_filedesc->cachesize));
205 
206  sion_filedesc->cacheid=shmfd;
207  sion_filedesc->cacheptr=shmptr;
208 #endif
209 
210 #endif
211  /* #NODIST# */
212 
213  return(rc);
214 }
215 
216 
224  int rc = SION_SUCCESS;
225 
226  /* #NODIST# */
227 #ifdef _SION_CACHE
228 
229 #if defined(_SION_LINUX)
230  /* destroy shared memory object -- shm_unlink() */
231  if (shm_unlink(sion_filedesc->cachefn) != 0) {
232  _sion_errorprint(SION_NOT_SUCCESS,_SION_ERROR_RETURN,"_sion_cache_destroy_linux: could not destroy segment\n");
233  }
234 
235  sion_filedesc->cacheid=-1;
236  sion_filedesc->cacheptr=NULL;
237 #endif
238 
239 #endif
240  /* #NODIST# */
241 
242  return(rc);
243 }
int _sion_cache_destroy_linux(_sion_filedesc *sion_filedesc)
Deallocate cache for linux.
Definition: sion_cache.c:223
int _sion_cache_load_linux(_sion_filedesc *sion_filedesc)
Load an already allocated cache for linux.
Definition: sion_cache.c:177
int _sion_cache_init(_sion_filedesc *sion_filedesc)
Allocate and initalize the cache.
Definition: sion_cache.c:79
int _sion_cache_create_linux(_sion_filedesc *sion_filedesc)
Allocate cache for linux.
Definition: sion_cache.c:132
int _sion_cache_check_env(_sion_filedesc *sion_filedesc)
Check if environment variables are set to use cache.
Definition: sion_cache.c:57
int _sion_cache_destroy(_sion_filedesc *sion_filedesc)
Deallocate the cache.
Definition: sion_cache.c:111
#define _SION_FLAG1_USECACHE
Definition: sion_filedesc.h:24
#define SION_FILEMODE_WRITE
Definition: sion_filedesc.h:38
#define SION_FILEMODE_READ
Definition: sion_filedesc.h:37
char * _sion_getenv(const char *name)
Sion File Descriptor Structure.
Definition: sion_filedesc.h:79
char cachefn[SION_CACHE_FNLEN]