1
/* Copyright (C) 2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
18
#include <ndb_global.h>
22
void malloctest(int loopcount, int memsize, int touch);
23
void freetest(int loopcount, int memsize);
24
void mmaptest(int loopcount, int memsize, int touch);
25
void unmaptest(int loopcount, int memsize);
28
main(int argc, char ** argv)
34
printf("Usage: memtest X loopcount memsize(MB)\n");
35
printf("where X = \n");
36
printf("1 : malloc test \n");
37
printf("2 : mmap test \n");
38
printf("3 : malloc test + touch pages\n");
39
printf("4 : mmap test + touch pages\n");
40
printf("5 : malloc/free test \n");
41
printf("6 : mmap/munmap test \n");
42
printf("loopcount - number of loops\n");
43
printf("memsize - memory segment size to allocate in MB.\n");
48
loopcount = atoi(argv[2]);
49
memsize = atoi(argv[3]);
50
switch(atoi(argv[1])) {
51
case 1: malloctest(loopcount, memsize , 0 );
53
case 2: mmaptest(loopcount, memsize,0);
55
case 3: malloctest(loopcount, memsize,1);
57
case 4: mmaptest(loopcount, memsize,1);
59
case 5: freetest(loopcount, memsize);
61
case 6: unmaptest(loopcount, memsize);
68
long long getMilli() {
69
struct timeval tick_time;
70
gettimeofday(&tick_time, 0);
73
((long long)tick_time.tv_sec) * ((long long)1000) +
74
((long long)tick_time.tv_usec) / ((long long)1000);
78
struct timeval tick_time;
79
int res = gettimeofday(&tick_time, 0);
81
long long secs = tick_time.tv_sec;
82
long long micros = tick_time.tv_usec;
84
micros = secs*1000000+micros;
88
void malloctest(int loopcount, int memsize, int touch) {
92
int size=memsize*1024*1024; /*bytes*/;
96
printf("Staring malloctest ");
98
printf("with touch\n");
104
for(i=0; i<loopcount; i++){
105
ptr=(char *)malloc((size_t)(size));
107
printf("failed to malloc!\n");
111
for(j=0; j<size; j=j+4096)
115
total=(int)(getMicro()-start);
117
mean=(float)((float)total/(float)loopcount);
118
printf("Total time malloc %d bytes: %2.3f microsecs loopcount %d touch %d \n",
119
size, mean,loopcount, touch);
123
void mmaptest(int loopcount, int memsize, int touch) {
128
int size=memsize*1024*1024; /*bytes*/;
131
printf("Staring mmaptest ");
133
printf("with touch \n");
138
for(i=0; i<loopcount; i++){
141
PROT_READ|PROT_WRITE,
142
MAP_PRIVATE|MAP_ANONYMOUS,
146
printf("failed to mmap!\n");
151
for(j=0; j<size; j=j+4096)
155
total=(int)(getMicro()-start);
156
mean=(float)((float)total/(float)loopcount);
157
printf("Total time mmap %d bytes: %2.3f microsecs \n",size, mean);
161
void unmaptest(loopcount, memsize)
167
int size=memsize*1024*1024; /*bytes*/;
170
printf("Staring munmap test (loopcount = 1 no matter what you prev. set)\n");
175
for(i=0; i<loopcount; i++){
178
PROT_READ|PROT_WRITE,
179
MAP_PRIVATE|MAP_ANONYMOUS,
183
printf("failed to mmap!\n");
188
for(j=0; j<size; j=j+1)
191
if(munmap(ptr, size)<0) {
192
printf("failed to munmap!\n");
196
total=(int)(getMicro()-start);
198
for(j=8192; j<size; j=j+4096) {
203
for(j=0; j<4096; j=j+4096) {
209
mean=(float)((float)total/(float)loopcount);
210
printf("Total time unmap %d bytes: %2.3f microsecs \n",size, mean);
213
void freetest(int loopcount, int memsize) {
217
int size=memsize*1024*1024; /*bytes*/;
222
printf("Staring free test (loopcount = 1 no matter what you prev. set)\n");
225
for(i=0; i<loopcount; i++){
226
ptr=(char*)malloc((size_t)(size));
228
printf("failed to malloc!\n");
231
for(j=0; j<size; j=j+4096)
235
total=(int)(getMicro()-start);
239
mean=(float)((float)total/(float)loopcount);
240
printf("Total time free %d bytes: %2.3f microsecs loopcount %d \n",
241
size, mean,loopcount);