22 #include <ndb_global.h>
25 #include <NdbThread.h>
27 #include <NdbCondition.h>
48 void* mapSegment(
void * arg);
49 void* unmapSegment(
void * arg);
52 void* mapSegment(
void * arg) {
59 Uint32
size=5*1024*1024;
63 int max=0, min =100000000, sum=0;
66 char * ptr =(
char*) mmap(0,
69 MAP_PRIVATE|MAP_ANONYMOUS,
73 total=(int)(getMicro()-start);
75 ndbout <<
"T" <<
id <<
": mmap took : " << total <<
" microsecs. "
77 ndbout_c(
" mapped @ %p \n", ptr);
87 ndbout <<
"failed to mmap!" << endl;
92 threadArgs[0].mapAddr = (
char *)ptr;
93 threadArgs[0].mapSize =
size;
94 threadArgs[0].chunk = 4096;
95 threadArgs[0].idx = 0;
98 for(Uint32 j=0; j<
size; j=j+4096)
101 unmapthread_var = NdbThread_Create(unmapSegment,
102 (
void**)&threadArgs[0],
104 (
char*)
"unmapthread",
105 NDB_THREAD_PRIO_MEAN);
108 if(NdbThread_WaitFor(unmapthread_var, &status) != 0) {
109 ndbout <<
"test failed - exitting " << endl;
115 ndbout <<
"MAX: " << max <<
" MIN: " << min;
116 float mean = (float) ((
float)sum/(float)run);
117 ndbout_c(
" AVERAGE: %2.5f\n",mean);
122 void* unmapSegment(
void * arg)
130 Uint32 mapSize = threadData->mapSize;
131 Uint32 chunk = threadData->chunk;
132 mapAddr = threadData->mapAddr;
136 freeAddr = mapAddr+mapSize-chunk;
137 NdbSleep_MilliSleep(100);
138 for(Uint32
i=0;
i<mapSize;
i =
i+chunk) {
140 if(munmap(freeAddr, chunk) < 0){
141 ndbout <<
"munmap failed" << endl;
144 total=(int)(getMicro()-start);
145 freeAddr = freeAddr - chunk;
146 NdbSleep_MilliSleep(10);
147 ndbout <<
"unmap 4096 bytes : " << total <<
"microsecs" << endl;
154 static int segmentsize=1;
157 static struct getargs args[] = {
158 {
"trash",
't', arg_integer, &trash,
159 "trash the memory before (1 to trash 0 to not trash)",
"trash"},
160 {
"segment",
's', arg_integer, &segmentsize,
161 "segment size (in MB)",
"segment"},
165 static const int num_args =
sizeof(args) /
sizeof(args[0]);
167 NDB_MAIN(munmaptest) {
169 const char *progname =
"munmaptest";
172 if(getarg(args, num_args, argc, argv, &optind)) {
173 arg_printusage(args, num_args, progname,
"");
180 for(
int i=0;
i<100;
i++) {
181 size=1+(int) (10.0*rand()/(RAND_MAX+1.0));
182 NdbSleep_MilliSleep(10);
185 PROT_READ|PROT_WRITE,
186 MAP_PRIVATE|MAP_ANONYMOUS,
189 for(
int i=0;
i<(size*1024*1024);
i=i+4096) {
192 NdbSleep_MilliSleep(10);
211 for(
int i=0;
i < noThreads;
i++) {
212 threadArgs[
i].mapSize = segmentsize*1024*1024;
213 threadArgs[
i].idx =
i;
214 mapthread_var = NdbThread_Create(mapSegment,
215 (
void**)&threadArgs[
i],
218 NDB_THREAD_PRIO_MEAN);
223 if(NdbThread_WaitFor(mapthread_var, &status) != 0) {
224 ndbout <<
"test failed - exitting " << endl;
230 long long getMilli() {
232 gettimeofday(&tick_time, 0);
235 ((
long long)tick_time.tv_sec) * ((
long long)1000) +
236 ((
long long)tick_time.tv_usec) / ((
long long)1000);
239 long long getMicro(){
241 int res = gettimeofday(&tick_time, 0);
243 long long secs = tick_time.tv_sec;
244 long long micros = tick_time.tv_usec;
246 micros = secs*1000000+micros;