summaryrefslogtreecommitdiff
path: root/storage/ndb/src/common/portlib/munmaptest.cpp
blob: 7977dc886345262077eac3e710dd531ea75b6e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* Copyright (c) 2003-2005 MySQL AB
   Use is subject to license terms

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */




#include <ndb_global.h>

#include <NdbOut.hpp>
#include <NdbThread.h>
#include <NdbMutex.h>
#include <NdbCondition.h>
#include <NdbSleep.h>
#include <NdbTick.h>
#include <NdbEnv.h>
#include <NdbHost.h>
#include <NdbMain.h>
#include <getarg.h>

struct ThreadData
{
  char * mapAddr;
  Uint32 mapSize;
  Uint32 chunk;
  Uint32 idx;
  
};

long long getMilli();
long long getMicro();


void* mapSegment(void * arg);
void* unmapSegment(void * arg);


void* mapSegment(void * arg) {
  
  ThreadData * threadArgs;
  long long start=0;
  int total=0;
  int id = *(int *)arg;
  threadArgs = new ThreadData [1];
  Uint32 size=5*1024*1024;
  struct NdbThread* unmapthread_var;
  void *status = 0;  
  int run = 1;
  int max=0, min =100000000, sum=0;
  while(run < 1001) {
    start=getMicro(); 
    char * ptr =(char*) mmap(0, 
			     size, 
			     PROT_READ|PROT_WRITE, 
			     MAP_PRIVATE|MAP_ANONYMOUS, 
			     0,
			     0);

    total=(int)(getMicro()-start);
    
    ndbout << "T"  << id << ": mmap took : " << total << " microsecs. " 
	   << " Run: " << run ;
    ndbout_c(" mapped @ %p \n", ptr);
    
    if(total>max)
      max = total;
    if(total<min)
      min=total;
    
    sum+=total;
    
    if(ptr<0) {
      ndbout << "failed to mmap!" << endl;
      exit(1);
    }

    
    threadArgs[0].mapAddr = (char *)ptr;
    threadArgs[0].mapSize = size;
    threadArgs[0].chunk = 4096;
    threadArgs[0].idx = 0;
    
    
    for(Uint32 j=0; j<size; j=j+4096)
      ptr[j]='1';
    
    unmapthread_var = NdbThread_Create(unmapSegment, // Function 
				       (void**)&threadArgs[0],// Arg
				       32768,        // Stacksize
				       (char*)"unmapthread",  // Thread name
				       NDB_THREAD_PRIO_MEAN); // Thread prio
    
    
    if(NdbThread_WaitFor(unmapthread_var, &status) != 0) {
      ndbout << "test failed - exitting " << endl;
      exit(1);
    }
    run++;
  }
  
  ndbout << "MAX: " << max << " MIN: " << min;
  float mean = (float) ((float)sum/(float)run);
  ndbout_c(" AVERAGE: %2.5f\n",mean);
}



void* unmapSegment(void * arg)
{
  
  char * freeAddr;
  char * mapAddr;
  ThreadData * threadData = (ThreadData*) arg;
  int start=0;
  int total=0;
  Uint32 mapSize = threadData->mapSize;
  Uint32 chunk = threadData->chunk;
  mapAddr = threadData->mapAddr;
 
  
  
  freeAddr = mapAddr+mapSize-chunk;
  NdbSleep_MilliSleep(100);  
  for(Uint32 i=0;i<mapSize; i = i+chunk) {
    start=getMicro(); 
    if(munmap(freeAddr, chunk) < 0){
      ndbout << "munmap failed" << endl;
      exit(1);
    }
    total=(int)(getMicro()-start);
    freeAddr = freeAddr - chunk;
    NdbSleep_MilliSleep(10);
    ndbout << "unmap 4096 bytes : " << total << "microsecs" << endl;
  }
  return NULL;
}


static int trash;
static int segmentsize=1;


static struct getargs args[] = {
  { "trash", 't', arg_integer, &trash,
    "trash the memory before (1 to trash 0 to not trash)", "trash"},
  { "segment", 's', arg_integer, &segmentsize,
    "segment size (in MB)", "segment"},
};


static const int num_args = sizeof(args) / sizeof(args[0]);

NDB_MAIN(munmaptest) {
   
  const char *progname = "munmaptest"; 
  int optind = 0;

  if(getarg(args, num_args, argc, argv, &optind)) {
    arg_printusage(args, num_args, progname, "");
    exit(1);
  }
  
  int size;
  char * ptr;
  if(trash) {
    for(int i=0; i<100; i++) {
      size=1+(int) (10.0*rand()/(RAND_MAX+1.0));
      NdbSleep_MilliSleep(10);
      ptr =(char*) mmap(0, 
			size*1024*1024, 
			PROT_READ|PROT_WRITE, 
			MAP_PRIVATE|MAP_ANONYMOUS, 
			0,
			0);      
      for(int i=0;i<(size*1024*1024); i=i+4096) {
	*(ptr+i)='1';
      }
      NdbSleep_MilliSleep(10);
     
      munmap(ptr,size);

    }
    
    
  }

  int noThreads = 1;
  struct NdbThread*  mapthread_var;
  int id[noThreads];
  void *status=0;

  ThreadData * threadArgs = new ThreadData[noThreads];




  for(int i=0; i < noThreads; i++) {
    threadArgs[i].mapSize = segmentsize*1024*1024;
    threadArgs[i].idx = i;
    mapthread_var = NdbThread_Create(mapSegment, // Function
				     (void**)&threadArgs[i],// Arg
				     32768,        // Stacksize
				     (char*)"mapthread",  // Thread name
				     NDB_THREAD_PRIO_MEAN); // Thread prio
    
  }
  
  
  if(NdbThread_WaitFor(mapthread_var, &status) != 0) {
    ndbout << "test failed - exitting " << endl;
    exit(1);
  }

}

long long getMilli() {
  struct timeval tick_time;
  gettimeofday(&tick_time, 0);

  return 
    ((long long)tick_time.tv_sec)  * ((long long)1000) +
    ((long long)tick_time.tv_usec) / ((long long)1000);
}

long long getMicro(){
  struct timeval tick_time;
  int res = gettimeofday(&tick_time, 0);

  long long secs   = tick_time.tv_sec;
  long long micros = tick_time.tv_usec;
  
  micros = secs*1000000+micros;
  return micros;
}