5 tst lib_mmap note{ standard mmap interface that works }end execute{
11 #include <sys/times.h>
13 #define MAPSIZE (64*1024)
14 #define BUFSIZE (8*1024)
17 #define Failed(file) (remove(file),1)
21 main(int argc, char** argv)
31 char file[1024], buf[MAPSIZE];
35 /* create data file in a local fs if possible */
37 if (access(f = "/tmp", 0) == 0 ||
38 access(f = "/usr/tmp", 0) == 0)
51 *t++ = '.'; *t++ = 'D'; *t = 0;
52 if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
55 for (i = 0; i < sizeof(buf); ++i)
56 buf[i] = '0' + (i%10);
57 for (i = 0; i < WRITE; ++i)
58 if (write(fd,buf,sizeof(buf)) != sizeof(buf))
62 /* see if can overwrite fixed map */
64 #define MAP_VARIABLE 0
66 if ((fd = open(file, O_RDWR)) < 0)
69 mm = mmap(NULL, sizeof(buf), (PROT_READ|PROT_WRITE),
70 (MAP_PRIVATE|MAP_VARIABLE), fd, 0);
71 if(mm == (caddr_t)0 || mm == (caddr_t)(-1))
73 mm = mmap(mm, sizeof(buf), (PROT_READ|PROT_WRITE),
74 (MAP_PRIVATE|MAP_FIXED), fd, 0);
75 okfixed = (mm == (caddr_t)0 || mm == (caddr_t)(-1)) ? 0 : 1;
76 munmap(mm, sizeof(buf));
80 if((fd = open(file, O_RDWR)) < 0)
83 for (i = 0; i < WRITE; ++i)
84 if (read(fd,buf,BUFSIZE) != BUFSIZE)
88 rdtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
91 if ((fd = open(file, O_RDWR)) < 0)
94 for(i = 0, mm = (caddr_t)0; i < WRITE; ++i)
96 { mm = (caddr_t)mmap(mm, MAPSIZE,
97 (PROT_READ|PROT_WRITE),
98 (MAP_PRIVATE | (mm ? MAP_FIXED : MAP_VARIABLE)),
104 mm = (caddr_t)mmap(NULL, MAPSIZE,
105 (PROT_READ|PROT_WRITE),
106 (MAP_PRIVATE|MAP_VARIABLE),
109 if(mm == (caddr_t)(-1) || mm == (caddr_t)0)
115 mmtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
117 return rdtm+60 < mmtm ? 1 : 0;
121 tst lib_mmap64 -D_LARGEFILE64_SOURCE note{ mmap64 interface and implementation work }end execute{
128 #include <sys/types.h>
129 #include <sys/mman.h>
130 #include <sys/stat.h>
140 char file[32] = {'/','t','m','p','/','m','m','X','X','X','X','X','X'};
142 /* hey, stubs are supposed to fail! */
143 if (stat64(".", &st) || !st.st_mode || !st.st_mtime)
145 if (!mktemp(file) || (fd = open64(file, O_CREAT|O_WRONLY, 0600)) < 0)
152 if (lseek64(fd, off, SEEK_SET) != off)
157 n = strlen(file) + 1;
158 if (write(fd, file, n) != n)
163 if (close(fd) < 0 || (fd = open64(file, O_RDWR)) < 0)
168 if (!(s = mmap64(NULL, (size_t)n, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, off)))
184 tst mmap_anon note{ use mmap MAP_ANON to get raw memory }end execute{
190 #include <sys/types.h>
191 #include <sys/mman.h>
192 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
193 #define MAP_ANON MAP_ANONYMOUS
198 addr = mmap(NULL,1024*1024,PROT_READ|PROT_WRITE,MAP_ANON|MAP_PRIVATE,-1,0);
199 return (addr && addr != (void*)(-1)) ? 0 : 1;
203 tst mmap_devzero note{ use mmap on /dev/zero to get raw memory }end execute{
209 #include <sys/types.h>
210 #include <sys/mman.h>
215 if((fd = open("/dev/zero", O_RDWR)) < 0)
217 addr = mmap(NULL,1024*1024,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
218 return (addr && addr != (void*)(-1)) ? 0 : 1;
222 tst note{ mmap is worth using }end output{
228 #include <sys/types.h>
229 #include <sys/mman.h>
230 #include <sys/stat.h>
231 #include <sys/times.h>
233 #define MAPSIZE (64*1024)
234 #define BUFSIZE (MAPSIZE/8)
238 #define Failed(file) (remove(file),1)
242 main(int argc, char** argv)
252 char file[1024], buf[MAPSIZE];
256 /* create data file */
257 f = argv[0]; t = file;
260 *t++ = '.'; *t++ = 'D'; *t = 0;
261 if ((fd = open(file, O_CREAT|O_TRUNC|O_WRONLY, 0666)) < 0)
264 for (i = 0; i < sizeof(buf); ++i)
265 buf[i] = '0' + (i%10);
266 for (i = 0; i < WRITE; ++i)
267 if (write(fd,buf,sizeof(buf)) != sizeof(buf))
273 for(run = 0; run < RUN; ++run)
274 { if((fd = open(file, O_RDWR)) < 0)
276 for (i = 0; i < WRITE; ++i)
277 { for(k = 0; k < MAPSIZE; k += BUFSIZE)
278 if (read(fd,buf,BUFSIZE) != BUFSIZE)
284 rdtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
288 for(run = 0; run < RUN; ++run)
289 { if ((fd = open(file, O_RDWR)) < 0)
291 for(i = 0, mm = (caddr_t)0; i < WRITE; ++i)
294 mm = (caddr_t)mmap(NULL, MAPSIZE,
295 (PROT_READ|PROT_WRITE),
296 MAP_PRIVATE, fd, i*MAPSIZE );
297 if(mm == (caddr_t)(-1) || mm == (caddr_t)0)
300 /* the memcpy is < BUFSIZE to simulate the
301 fact that functions like sfreserve/sfgetr do
302 not do buffer copying.
305 for(k = 0; k < MAPSIZE; k += BUFSIZE, t += BUFSIZE)
306 memcpy(buf,t,(3*BUFSIZE)/4);
311 mmtm = (etm.tms_utime-stm.tms_utime) + (etm.tms_stime-stm.tms_stime);
316 printf("#define _mmap_worthy 2 /* mmap is great */\n");
317 else if(4*mmtm <= 5*rdtm)
318 printf("#define _mmap_worthy 1 /* mmap is good */\n");
328 /* some systems get it wrong but escape concise detection */