1 /* Copyright (c) 2003-2005 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 
18 
19 #include <ndb_global.h>
20 
21 long long getMilli();
22 long long getMicro();
23 void malloctest(int loopcount, int memsize, int touch);
24 void freetest(int loopcount, int memsize);
25 void mmaptest(int loopcount, int memsize, int touch);
26 void unmaptest(int loopcount, int memsize);
27 
28 
main(int argc,char ** argv)29 main(int argc, char ** argv)
30 {
31 
32   int loopcount;
33   int memsize;
34   if(argc < 4) {
35     printf("Usage:  memtest X loopcount memsize(MB)\n");
36     printf("where X = \n");
37     printf("1 : malloc test \n");
38     printf("2 : mmap test \n");
39     printf("3 : malloc test + touch pages\n");
40     printf("4 : mmap test + touch pages\n");
41     printf("5 : malloc/free test \n");
42     printf("6 : mmap/munmap test \n");
43     printf("loopcount - number of loops\n");
44     printf("memsize - memory segment size to allocate in MB.\n");
45     exit(1);
46   }
47 
48 
49   loopcount = atoi(argv[2]);
50   memsize = atoi(argv[3]);
51   switch(atoi(argv[1])) {
52   case 1: malloctest(loopcount, memsize , 0 );
53     break;
54   case 2: mmaptest(loopcount, memsize,0);
55     break;
56   case 3: malloctest(loopcount, memsize,1);
57     break;
58   case 4: mmaptest(loopcount, memsize,1);
59     break;
60   case 5: freetest(loopcount, memsize);
61     break;
62   case 6: unmaptest(loopcount, memsize);
63     break;
64   default:
65     break;
66   }
67 }
68 
getMilli()69 long long getMilli() {
70   struct timeval tick_time;
71   gettimeofday(&tick_time, 0);
72 
73   return
74     ((long long)tick_time.tv_sec)  * ((long long)1000) +
75     ((long long)tick_time.tv_usec) / ((long long)1000);
76 }
77 
getMicro()78 long long getMicro(){
79   struct timeval tick_time;
80   int res = gettimeofday(&tick_time, 0);
81 
82   long long secs   = tick_time.tv_sec;
83   long long micros = tick_time.tv_usec;
84 
85   micros = secs*1000000+micros;
86   return micros;
87 }
88 
malloctest(int loopcount,int memsize,int touch)89 void malloctest(int loopcount, int memsize, int touch) {
90   long long start=0;
91   int total=0;
92   int i=0, j=0;
93   int size=memsize*1024*1024; /*bytes*/;
94   float mean;
95   char * ptr =0;
96 
97   printf("Staring malloctest ");
98   if(touch)
99     printf("with touch\n");
100   else
101     printf("\n");
102 
103   start=getMicro();
104 
105   for(i=0; i<loopcount; i++){
106     ptr=(char *)malloc((size_t)(size));
107     if(ptr==0) {
108       printf("failed to malloc!\n");
109       return;
110     }
111     if(touch) {
112       for(j=0; j<size; j=j+4096)
113 	ptr[j]=1;
114     }
115   }
116   total=(int)(getMicro()-start);
117 
118   mean=(float)((float)total/(float)loopcount);
119   printf("Total time malloc %d bytes: %2.3f microsecs  loopcount %d touch %d \n",
120 	 size, mean,loopcount, touch);
121 }
122 
123 
mmaptest(int loopcount,int memsize,int touch)124 void mmaptest(int loopcount, int memsize, int touch) {
125   long long start=0;
126   int total=0;
127   int i=0, j=0;
128   char * ptr;
129   int size=memsize*1024*1024; /*bytes*/;
130   float mean;
131 
132   printf("Staring mmaptest ");
133   if(touch)
134     printf("with touch \n");
135   else
136     printf("\n");
137 
138   start=getMicro();
139   for(i=0; i<loopcount; i++){
140     ptr = mmap(0,
141 	       size,
142 	       PROT_READ|PROT_WRITE,
143 	       MAP_PRIVATE|MAP_ANONYMOUS,
144 	       0,
145 	       0);
146     if(ptr<0) {
147       printf("failed to mmap!\n");
148       return;
149     }
150 
151     if(touch) {
152       for(j=0; j<size; j=j+4096)
153 	ptr[j]=1;
154     }
155   }
156   total=(int)(getMicro()-start);
157   mean=(float)((float)total/(float)loopcount);
158   printf("Total time mmap %d bytes: %2.3f microsecs  \n",size, mean);
159 }
160 
161 
unmaptest(loopcount,memsize)162 void unmaptest(loopcount, memsize)
163 {
164   long long start=0;
165   int total=0;
166   int i=0, j=0;
167   char * ptr;
168   int size=memsize*1024*1024; /*bytes*/;
169   float mean;
170 
171   printf("Staring munmap test (loopcount = 1 no matter what you prev. set)\n");
172 
173   loopcount = 1;
174 
175 
176   for(i=0; i<loopcount; i++){
177     ptr =(char*) mmap(0,
178 		      size,
179 		      PROT_READ|PROT_WRITE,
180 		      MAP_PRIVATE|MAP_ANONYMOUS,
181 		      0,
182 		      0);
183     if(ptr<0) {
184       printf("failed to mmap!\n");
185       return;
186     }
187 
188 
189     for(j=0; j<size; j=j+1)
190       ptr[j]='1';
191     start=getMicro();
192     if(munmap(ptr, size)<0) {
193       printf("failed to munmap!\n");
194       return;
195     }
196 
197     total=(int)(getMicro()-start);
198     /*
199     for(j=8192; j<size; j=j+4096) {
200 
201 	*(ptr+j)='1';
202     }
203 
204     for(j=0; j<4096; j=j+4096) {
205       *(ptr+j)='1';
206     }
207 
208     */
209   }
210   mean=(float)((float)total/(float)loopcount);
211   printf("Total time unmap %d bytes: %2.3f microsecs  \n",size, mean);
212 }
213 
freetest(int loopcount,int memsize)214 void freetest(int loopcount, int memsize) {
215   long long start=0;
216   int total=0;
217   int i=0, j=0;
218   int size=memsize*1024*1024; /*bytes*/;
219   float mean;
220   char * ptr =0;
221 
222   loopcount = 1;
223   printf("Staring free test (loopcount = 1 no matter what you prev. set)\n");
224 
225 
226   for(i=0; i<loopcount; i++){
227     ptr=(char*)malloc((size_t)(size));
228     if(ptr==0) {
229       printf("failed to malloc!\n");
230       return;
231     }
232     for(j=0; j<size; j=j+4096)
233       ptr[j]='1';
234     start=getMicro();
235     free(ptr);
236     total=(int)(getMicro()-start);
237   }
238 
239 
240   mean=(float)((float)total/(float)loopcount);
241   printf("Total time free %d bytes: %2.3f microsecs  loopcount %d \n",
242 	 size, mean,loopcount);
243 }
244