~vadim-tk/percona-server/percona-galera-5.1.57

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/portlib/memtest.c

  • Committer: root
  • Date: 2011-07-10 16:09:24 UTC
  • Revision ID: root@r815.office.percona.com-20110710160924-fyffqsbaclgu6vui
Initial port

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
 
 
18
#include <ndb_global.h>
 
19
 
 
20
long long getMilli();
 
21
long long getMicro();
 
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);
 
26
 
 
27
 
 
28
main(int argc, char ** argv)
 
29
{
 
30
 
 
31
  int loopcount;
 
32
  int memsize;
 
33
  if(argc < 4) {
 
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");
 
44
    exit(1);
 
45
  }
 
46
  
 
47
 
 
48
  loopcount = atoi(argv[2]);
 
49
  memsize = atoi(argv[3]);
 
50
  switch(atoi(argv[1])) {
 
51
  case 1: malloctest(loopcount, memsize , 0 );
 
52
    break;
 
53
  case 2: mmaptest(loopcount, memsize,0);
 
54
    break;
 
55
  case 3: malloctest(loopcount, memsize,1);
 
56
    break;
 
57
  case 4: mmaptest(loopcount, memsize,1);
 
58
    break;
 
59
  case 5: freetest(loopcount, memsize);
 
60
    break;
 
61
  case 6: unmaptest(loopcount, memsize);
 
62
    break;
 
63
  default:
 
64
    break;
 
65
  }
 
66
}
 
67
  
 
68
long long getMilli() {
 
69
  struct timeval tick_time;
 
70
  gettimeofday(&tick_time, 0);
 
71
 
 
72
  return 
 
73
    ((long long)tick_time.tv_sec)  * ((long long)1000) +
 
74
    ((long long)tick_time.tv_usec) / ((long long)1000);
 
75
}
 
76
 
 
77
long long getMicro(){
 
78
  struct timeval tick_time;
 
79
  int res = gettimeofday(&tick_time, 0);
 
80
 
 
81
  long long secs   = tick_time.tv_sec;
 
82
  long long micros = tick_time.tv_usec;
 
83
  
 
84
  micros = secs*1000000+micros;
 
85
  return micros;
 
86
}
 
87
 
 
88
void malloctest(int loopcount, int memsize, int touch) {
 
89
  long long start=0;
 
90
  int total=0;
 
91
  int i=0, j=0;
 
92
  int size=memsize*1024*1024; /*bytes*/;
 
93
  float mean;
 
94
  char * ptr =0;
 
95
  
 
96
  printf("Staring malloctest ");
 
97
  if(touch)
 
98
    printf("with touch\n");
 
99
  else
 
100
    printf("\n");
 
101
  
 
102
  start=getMicro();
 
103
  
 
104
  for(i=0; i<loopcount; i++){
 
105
    ptr=(char *)malloc((size_t)(size));
 
106
    if(ptr==0) {
 
107
      printf("failed to malloc!\n");
 
108
      return;
 
109
    }    
 
110
    if(touch) {
 
111
      for(j=0; j<size; j=j+4096)
 
112
        ptr[j]=1;
 
113
    }
 
114
  }
 
115
  total=(int)(getMicro()-start);
 
116
  
 
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);  
 
120
}
 
121
 
 
122
 
 
123
void mmaptest(int loopcount, int memsize, int touch) {
 
124
  long long start=0;
 
125
  int total=0;
 
126
  int i=0, j=0;
 
127
  char * ptr;
 
128
  int size=memsize*1024*1024; /*bytes*/;
 
129
  float mean;
 
130
 
 
131
  printf("Staring mmaptest ");
 
132
  if(touch)
 
133
    printf("with touch \n");
 
134
  else
 
135
    printf("\n");
 
136
 
 
137
  start=getMicro();  
 
138
  for(i=0; i<loopcount; i++){
 
139
    ptr = mmap(0, 
 
140
               size, 
 
141
               PROT_READ|PROT_WRITE, 
 
142
               MAP_PRIVATE|MAP_ANONYMOUS, 
 
143
               0,
 
144
               0);
 
145
    if(ptr<0) {
 
146
      printf("failed to mmap!\n");
 
147
      return;
 
148
    }
 
149
 
 
150
    if(touch) {
 
151
      for(j=0; j<size; j=j+4096)
 
152
        ptr[j]=1;
 
153
    }  
 
154
  }
 
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);  
 
158
}
 
159
 
 
160
 
 
161
void unmaptest(loopcount, memsize) 
 
162
{
 
163
  long long start=0;
 
164
  int total=0;
 
165
  int i=0, j=0;
 
166
  char * ptr;
 
167
  int size=memsize*1024*1024; /*bytes*/;
 
168
  float mean;
 
169
 
 
170
  printf("Staring munmap test (loopcount = 1 no matter what you prev. set)\n");
 
171
 
 
172
  loopcount = 1;
 
173
 
 
174
 
 
175
  for(i=0; i<loopcount; i++){
 
176
    ptr =(char*) mmap(0, 
 
177
                      size, 
 
178
                      PROT_READ|PROT_WRITE, 
 
179
                      MAP_PRIVATE|MAP_ANONYMOUS, 
 
180
                      0,
 
181
                      0);
 
182
    if(ptr<0) {
 
183
      printf("failed to mmap!\n");
 
184
      return;
 
185
    }
 
186
 
 
187
 
 
188
    for(j=0; j<size; j=j+1)
 
189
      ptr[j]='1';
 
190
    start=getMicro(); 
 
191
    if(munmap(ptr, size)<0) {
 
192
      printf("failed to munmap!\n");
 
193
      return;
 
194
    }
 
195
   
 
196
    total=(int)(getMicro()-start);
 
197
    /*
 
198
    for(j=8192; j<size; j=j+4096) {
 
199
 
 
200
        *(ptr+j)='1';
 
201
    }
 
202
    
 
203
    for(j=0; j<4096; j=j+4096) { 
 
204
      *(ptr+j)='1';
 
205
    }
 
206
    
 
207
    */
 
208
  }
 
209
  mean=(float)((float)total/(float)loopcount);
 
210
  printf("Total time unmap %d bytes: %2.3f microsecs  \n",size, mean);  
 
211
}
 
212
 
 
213
void freetest(int loopcount, int memsize) {
 
214
  long long start=0;
 
215
  int total=0;
 
216
  int i=0, j=0;
 
217
  int size=memsize*1024*1024; /*bytes*/;
 
218
  float mean;
 
219
  char * ptr =0;
 
220
 
 
221
  loopcount = 1;
 
222
  printf("Staring free test (loopcount = 1 no matter what you prev. set)\n");
 
223
 
 
224
  
 
225
  for(i=0; i<loopcount; i++){
 
226
    ptr=(char*)malloc((size_t)(size));
 
227
    if(ptr==0) {
 
228
      printf("failed to malloc!\n");
 
229
      return;
 
230
    }
 
231
    for(j=0; j<size; j=j+4096)
 
232
      ptr[j]='1';
 
233
    start=getMicro();     
 
234
    free(ptr);
 
235
    total=(int)(getMicro()-start);
 
236
  }
 
237
 
 
238
  
 
239
  mean=(float)((float)total/(float)loopcount);
 
240
  printf("Total time free %d bytes: %2.3f microsecs  loopcount %d \n",
 
241
         size, mean,loopcount);  
 
242
}