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