21 #include <ndb_global.h>
25 void malloctest(
int loopcount,
int memsize,
int touch);
26 void freetest(
int loopcount,
int memsize);
27 void mmaptest(
int loopcount,
int memsize,
int touch);
28 void unmaptest(
int loopcount,
int memsize);
31 main(
int argc,
char ** argv)
37 printf(
"Usage: memtest X loopcount memsize(MB)\n");
38 printf(
"where X = \n");
39 printf(
"1 : malloc test \n");
40 printf(
"2 : mmap test \n");
41 printf(
"3 : malloc test + touch pages\n");
42 printf(
"4 : mmap test + touch pages\n");
43 printf(
"5 : malloc/free test \n");
44 printf(
"6 : mmap/munmap test \n");
45 printf(
"loopcount - number of loops\n");
46 printf(
"memsize - memory segment size to allocate in MB.\n");
51 loopcount = atoi(argv[2]);
52 memsize = atoi(argv[3]);
53 switch(atoi(argv[1])) {
54 case 1: malloctest(loopcount, memsize , 0 );
56 case 2: mmaptest(loopcount, memsize,0);
58 case 3: malloctest(loopcount, memsize,1);
60 case 4: mmaptest(loopcount, memsize,1);
62 case 5: freetest(loopcount, memsize);
64 case 6: unmaptest(loopcount, memsize);
71 long long getMilli() {
73 gettimeofday(&tick_time, 0);
76 ((
long long)tick_time.tv_sec) * ((
long long)1000) +
77 ((
long long)tick_time.tv_usec) / ((
long long)1000);
82 int res = gettimeofday(&tick_time, 0);
84 long long secs = tick_time.tv_sec;
85 long long micros = tick_time.tv_usec;
87 micros = secs*1000000+micros;
91 void malloctest(
int loopcount,
int memsize,
int touch) {
95 int size=memsize*1024*1024; ;
99 printf(
"Staring malloctest ");
101 printf(
"with touch\n");
107 for(i=0; i<loopcount; i++){
108 ptr=(
char *)malloc((
size_t)(
size));
110 printf(
"failed to malloc!\n");
114 for(j=0; j<
size; j=j+4096)
118 total=(int)(getMicro()-start);
120 mean=(float)((
float)total/(float)loopcount);
121 printf(
"Total time malloc %d bytes: %2.3f microsecs loopcount %d touch %d \n",
122 size, mean,loopcount, touch);
126 void mmaptest(
int loopcount,
int memsize,
int touch) {
131 int size=memsize*1024*1024; ;
134 printf(
"Staring mmaptest ");
136 printf(
"with touch \n");
141 for(i=0; i<loopcount; i++){
144 PROT_READ|PROT_WRITE,
145 MAP_PRIVATE|MAP_ANONYMOUS,
149 printf(
"failed to mmap!\n");
154 for(j=0; j<
size; j=j+4096)
158 total=(int)(getMicro()-start);
159 mean=(float)((
float)total/(float)loopcount);
160 printf(
"Total time mmap %d bytes: %2.3f microsecs \n",size, mean);
164 void unmaptest(loopcount, memsize)
170 int size=memsize*1024*1024; ;
173 printf(
"Staring munmap test (loopcount = 1 no matter what you prev. set)\n");
178 for(i=0; i<loopcount; i++){
181 PROT_READ|PROT_WRITE,
182 MAP_PRIVATE|MAP_ANONYMOUS,
186 printf(
"failed to mmap!\n");
191 for(j=0; j<
size; j=j+1)
194 if(munmap(ptr, size)<0) {
195 printf(
"failed to munmap!\n");
199 total=(int)(getMicro()-start);
212 mean=(float)((
float)total/(float)loopcount);
213 printf(
"Total time unmap %d bytes: %2.3f microsecs \n",size, mean);
216 void freetest(
int loopcount,
int memsize) {
220 int size=memsize*1024*1024; ;
225 printf(
"Staring free test (loopcount = 1 no matter what you prev. set)\n");
228 for(i=0; i<loopcount; i++){
229 ptr=(
char*)malloc((
size_t)(
size));
231 printf(
"failed to malloc!\n");
234 for(j=0; j<
size; j=j+4096)
238 total=(int)(getMicro()-start);
242 mean=(float)((
float)total/(float)loopcount);
243 printf(
"Total time free %d bytes: %2.3f microsecs loopcount %d \n",
244 size, mean,loopcount);