Page MenuHomeFreeBSD
Authored By
dougm
May 31 2019, 7:38 AM
Size
1 KB
Referenced Files
None
Subscribers
None

mapper3.c

#include <sys/mman.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
enum {MAX_MAPS = 16384, PAGE_SIZE = 4096 };
int main(int argc, const char *argv[])
{
int i;
int nmappings = 0;
void *mappings[MAX_MAPS];
struct timeval beg, end;
struct timezone tz = {0, 0};
if (argc >= 2)
srandom(atoi(argv[1]));
for (i = 0; i < MAX_MAPS; ++i)
mappings[i] = MAP_FAILED;
gettimeofday(&beg, &tz);
for (i = 0; i < 2048 * MAX_MAPS; ++i) {
long r = random() % MAX_MAPS;
if (mappings[r] == MAP_FAILED) {
mappings[r] = mmap(0, (9 + r%7)*PAGE_SIZE, PROT_WRITE, MAP_ALIGNED(16) | MAP_ANON, -1, 0);
if (mappings[r] != MAP_FAILED)
++nmappings;
} else {
munmap(mappings[r], (9 + r%7)*PAGE_SIZE);
mappings[r] = MAP_FAILED;
--nmappings;
}
}
gettimeofday(&end, &tz);
end.tv_sec -= beg.tv_sec;
end.tv_usec -= beg.tv_usec;
if (end.tv_usec < 0) {
end.tv_usec += 1000000;
end.tv_sec--;
}
printf("nmappings = %d, time %ld.%ld; ok?\n", nmappings,
(long)end.tv_sec, (long)end.tv_usec);
return 0;
}

File Metadata

Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1933368
Default Alt Text
mapper3.c (1 KB)

Event Timeline