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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
#include <ndb_global.h>
22
#include <NdbThread.h>
24
#include <NdbCondition.h>
45
void* mapSegment(void * arg);
46
void* unmapSegment(void * arg);
49
void* mapSegment(void * arg) {
51
ThreadData * threadArgs;
55
threadArgs = new ThreadData [1];
56
Uint32 size=5*1024*1024;
57
struct NdbThread* unmapthread_var;
60
int max=0, min =100000000, sum=0;
63
char * ptr =(char*) mmap(0,
66
MAP_PRIVATE|MAP_ANONYMOUS,
70
total=(int)(getMicro()-start);
72
ndbout << "T" << id << ": mmap took : " << total << " microsecs. "
74
ndbout_c(" mapped @ %p \n", ptr);
84
ndbout << "failed to mmap!" << endl;
89
threadArgs[0].mapAddr = (char *)ptr;
90
threadArgs[0].mapSize = size;
91
threadArgs[0].chunk = 4096;
92
threadArgs[0].idx = 0;
95
for(Uint32 j=0; j<size; j=j+4096)
98
unmapthread_var = NdbThread_Create(unmapSegment, // Function
99
(void**)&threadArgs[0],// Arg
101
(char*)"unmapthread", // Thread name
102
NDB_THREAD_PRIO_MEAN); // Thread prio
105
if(NdbThread_WaitFor(unmapthread_var, &status) != 0) {
106
ndbout << "test failed - exitting " << endl;
112
ndbout << "MAX: " << max << " MIN: " << min;
113
float mean = (float) ((float)sum/(float)run);
114
ndbout_c(" AVERAGE: %2.5f\n",mean);
119
void* unmapSegment(void * arg)
124
ThreadData * threadData = (ThreadData*) arg;
127
Uint32 mapSize = threadData->mapSize;
128
Uint32 chunk = threadData->chunk;
129
mapAddr = threadData->mapAddr;
133
freeAddr = mapAddr+mapSize-chunk;
134
NdbSleep_MilliSleep(100);
135
for(Uint32 i=0;i<mapSize; i = i+chunk) {
137
if(munmap(freeAddr, chunk) < 0){
138
ndbout << "munmap failed" << endl;
141
total=(int)(getMicro()-start);
142
freeAddr = freeAddr - chunk;
143
NdbSleep_MilliSleep(10);
144
ndbout << "unmap 4096 bytes : " << total << "microsecs" << endl;
151
static int segmentsize=1;
154
static struct getargs args[] = {
155
{ "trash", 't', arg_integer, &trash,
156
"trash the memory before (1 to trash 0 to not trash)", "trash"},
157
{ "segment", 's', arg_integer, &segmentsize,
158
"segment size (in MB)", "segment"},
162
static const int num_args = sizeof(args) / sizeof(args[0]);
164
NDB_MAIN(munmaptest) {
166
const char *progname = "munmaptest";
169
if(getarg(args, num_args, argc, argv, &optind)) {
170
arg_printusage(args, num_args, progname, "");
177
for(int i=0; i<100; i++) {
178
size=1+(int) (10.0*rand()/(RAND_MAX+1.0));
179
NdbSleep_MilliSleep(10);
182
PROT_READ|PROT_WRITE,
183
MAP_PRIVATE|MAP_ANONYMOUS,
186
for(int i=0;i<(size*1024*1024); i=i+4096) {
189
NdbSleep_MilliSleep(10);
199
struct NdbThread* mapthread_var;
203
ThreadData * threadArgs = new ThreadData[noThreads];
208
for(int i=0; i < noThreads; i++) {
209
threadArgs[i].mapSize = segmentsize*1024*1024;
210
threadArgs[i].idx = i;
211
mapthread_var = NdbThread_Create(mapSegment, // Function
212
(void**)&threadArgs[i],// Arg
214
(char*)"mapthread", // Thread name
215
NDB_THREAD_PRIO_MEAN); // Thread prio
220
if(NdbThread_WaitFor(mapthread_var, &status) != 0) {
221
ndbout << "test failed - exitting " << endl;
227
long long getMilli() {
228
struct timeval tick_time;
229
gettimeofday(&tick_time, 0);
232
((long long)tick_time.tv_sec) * ((long long)1000) +
233
((long long)tick_time.tv_usec) / ((long long)1000);
236
long long getMicro(){
237
struct timeval tick_time;
238
int res = gettimeofday(&tick_time, 0);
240
long long secs = tick_time.tv_sec;
241
long long micros = tick_time.tv_usec;
243
micros = secs*1000000+micros;