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