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