summaryrefslogtreecommitdiff
path: root/storage/ndb/src/common/portlib/NdbPortLibTest.cpp
blob: 86623a865b1d27d6ccb5f02232ddb88ff93b5473 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* Copyright (C) 2003 MySQL AB

   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 */

/**
 *  NdbPortLibTest.cpp
 *  Test the functionality of portlib
 *  TODO - Add tests for NdbMem
 */

#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"

int TestHasFailed;
int verbose = 0;

static void fail(const char* test, const char* cause)
{
  TestHasFailed = 1;
  ndbout << test << " failed, " << cause << endl;
}

// test 1 variables and funcs

extern "C"  void* thread1func(void* arg)
{
  int arg1;
  int returnvalue = 8;
  arg1 = *(int*)arg;
  ndbout << "thread1: thread1func called with arg = " << arg1 << endl;

  //  delay(1000);
  if (arg1 != 7)
    fail("TEST1", "Wrong arg");

  return returnvalue;
}

// test 2 variables and funcs

NdbMutex* test2mutex;

extern "C" void* test2func(void* arg)
{

  int arg1;
  arg1 = *(int*)arg;
  ndbout << "thread" << arg1 << " started in test2func" << endl;

  if (NdbMutex_Lock(test2mutex) != 0)
    fail("TEST2", "Failed to lock mutex");

  ndbout << "thread" << arg1 << ", test2func " << endl;

  if (NdbMutex_Unlock(test2mutex) != 0)
    fail("TEST2", "Failed to unlock mutex");

  int returnvalue = arg1;
  return returnvalue;
}


// test 3 and 7 variables and funcs

NdbMutex* testmutex;
NdbCondition* testcond;
int testthreadsdone;

extern "C" void* testfunc(void* arg)
{
  int tmpVar;
  int threadno;
  int result;

  threadno = *(int*)arg;

  ndbout << "Thread" << threadno << " started in testfunc" << endl;
  do 
    {

      if ((threadno % 2) == 0)
	result = NdbSleep_SecSleep(1);
      else
	result = NdbSleep_MilliSleep(100);

      if (result != 0)
	fail("TEST3", "Wrong result from sleep function");

      if (NdbMutex_Lock(testmutex) != 0)
	fail("TEST3", "Wrong result from NdbMutex_Lock function");
  
      ndbout << "thread" << threadno << ", testfunc " << endl;
      testthreadsdone++;
      tmpVar = testthreadsdone;

      if (NdbCondition_Signal(testcond) != 0)
	fail("TEST3", "Wrong result from NdbCondition_Signal function");
  
      if (NdbMutex_Unlock(testmutex) != 0)
	fail("TEST3", "Wrong result from NdbMutex_Unlock function");

    }
  while(tmpVar<100);
  
  return 0;
}

extern "C" void* testTryLockfunc(void* arg)
{
  int tmpVar = 0;
  int threadno;
  int result;

  threadno = *(int*)arg;

  ndbout << "Thread" << threadno << " started" << endl;
  do 
    {

      if ((threadno % 2) == 0)
	result = NdbSleep_SecSleep(1);
      else
	result = NdbSleep_MilliSleep(100);

      if (result != 0)
	fail("TEST3", "Wrong result from sleep function");

      if (NdbMutex_Trylock(testmutex) == 0){
	 
	ndbout << "thread" << threadno << ", testTryLockfunc locked" << endl;
  	testthreadsdone++;
	tmpVar = testthreadsdone;
	
	if (NdbCondition_Signal(testcond) != 0)
	  fail("TEST3", "Wrong result from NdbCondition_Signal function");
  
	if (NdbMutex_Unlock(testmutex) != 0)
	  fail("TEST3", "Wrong result from NdbMutex_Unlock function");
      }

    }
  while(tmpVar<100);
  
  return 0;
}



void testMicros(int count);
Uint64 time_diff(Uint64 s1, Uint64 s2, Uint32 m1, Uint32 m2);

NDB_COMMAND(PortLibTest, "portlibtest", "portlibtest", "Test the portable function layer", 4096){

  ndbout << "= TESTING ARGUMENT PASSING ============" << endl;
  ndbout << "ARGC: " << argc << endl;
  for(int i = 1; i < argc; i++){
    ndbout << " ARGV"<<i<<": " << (char*)argv[i] << endl;
  }
  ndbout << endl << endl;
  

  struct NdbThread* thread1var;
  void *status = 0;
  int arg = 7;

  TestHasFailed = 0;
  // create one thread and wait for it to return
  ndbout << "= TEST1 ===============================" << endl;

  thread1var = NdbThread_Create(thread1func, // Function 
				(void**)&arg,// Arg
				2048,        // Stacksize
				(char*)"thread1",  // Thread name
				NDB_THREAD_PRIO_MEAN); // Thread priority

  
  if(NdbThread_WaitFor(thread1var, &status) != 0)
    fail("TEST1", "NdbThread_WaitFor failed");
  // NOTE! thread return value is not yet used in Ndb and thus not tested(does not work)
  //ndbout << "thread1 returned, status = " << status << endl;
  //if (status != 8) 
  // fail("TEST1", "Wrong status");
  ndbout << "TEST1 completed" << endl;


  NdbThread_Destroy(&thread1var);

  // Create 10 threads that will wait for a mutex before printing it's message to screen
  ndbout << "= TEST2 ===============================" << endl;
#define T2_THREADS 10
  NdbThread* threads[T2_THREADS];
  int   args[T2_THREADS];
  void *status2 = 0;
  test2mutex = NdbMutex_Create();
  NdbMutex_Lock(test2mutex);

  for (int i = 0; i < T2_THREADS; i++)
    {
      args[i] = i;
    threads[i] = NdbThread_Create(test2func, // Function 
				  (void**)&args[i],// Arg
				  2048,        // Stacksize
				  (char*)"test2thread",  // Thread name
				  NDB_THREAD_PRIO_MEAN); // Thread priority
    if (threads[i] == NULL)
      fail("TEST2", "NdbThread_Create failed");
    }

  ndbout << "All threads created" << endl;

  NdbMutex_Unlock(test2mutex);

  for (int i = 0; i < T2_THREADS; i++)
  {
    if (NdbThread_WaitFor(threads[i], &status2))
      fail("TEST2", "NdbThread_WaitFor failed");      

    NdbThread_Destroy(&threads[i]);
    // Don't test return values
    //    ndbout << "thread" << i << " returned, status = " << status2 << endl;
    //    if (status2 != i)
    //      fail("TEST2", "Wrong status");
  }

  if (NdbMutex_Lock(test2mutex) != 0)
    fail("TEST2", "NdbMutex_Lock failed");
  if (NdbMutex_Unlock(test2mutex) != 0)
    fail("TEST2", "NdbMutex_Unlock failed");
  if (NdbMutex_Destroy(test2mutex) != 0)
    fail("TEST2", "NdbMutex_Destroy failed");
  ndbout << "TEST2 completed" << endl;

  ndbout << "= TEST3 ===============================" << endl;
  // Create 10 threads that will by synchronised by a condition
  // When they are awakened and have the mutex they will increment a global variable
#define T3_THREADS 10
  NdbThread* t3threads[T3_THREADS];
  int   t3args[T3_THREADS];
  void *status3 = 0;

  testmutex = NdbMutex_Create();
  testcond = NdbCondition_Create();
  testthreadsdone = 0;
  
  for (int i = 0; i < T3_THREADS; i++)
    {
      t3args[i] = i;
      t3threads[i] = NdbThread_Create(testfunc, // Function 
				      (void**)&t3args[i],// Arg
				      2048,        // Stacksize
				      (char*)"test3thread",  // Thread name
				      NDB_THREAD_PRIO_MEAN); // Thread priority
    }

  ndbout << "All threads created" << endl;

  if (NdbMutex_Lock(testmutex) != 0)
    fail("TEST3", "NdbMutex_Lock failed");
  
  while (testthreadsdone < T3_THREADS*10)
    {
      if(NdbCondition_Wait(testcond, testmutex) != 0)
	fail("TEST3", "NdbCondition_Wait failed");
      ndbout << "Condition signaled, there are " << testthreadsdone << " completed threads" << endl;
    }
  if (NdbMutex_Unlock(testmutex) != 0)
    fail("TEST3", "NdbMutex_Unlock failed");

  for (int i = 0; i < T3_THREADS; i++)
  {
    if (NdbThread_WaitFor(t3threads[i], &status3) != 0)
      fail("TEST3", "NdbThread_WaitFor failed");

    NdbThread_Destroy(&t3threads[i]);
    //ndbout << "thread" << i << " returned, status = " << status3 << endl;
    //if (status3 != i)
    //  fail("TEST3", "Wrong status");
  }

  NdbMutex_Destroy(testmutex);
  NdbCondition_Destroy(testcond);
  ndbout << "TEST3 completed" << endl;

  ndbout << "= TEST4 ===============================" << endl;
  // Check tick functions

  //#if 0

  int sleeptimes[] = {78, 12, 199, 567, 899};


  for (int i = 0; i < 5; i++)
  {
  ndbout << "*------------------------------- Measure" << i << endl;

  NDB_TICKS millisec_now; 
  NDB_TICKS millisec_now2;

  millisec_now = NdbTick_CurrentMillisecond();
  NdbSleep_MilliSleep(sleeptimes[i]);
  millisec_now2 = NdbTick_CurrentMillisecond();

  ndbout << "  Time before sleep = " << millisec_now << endl;
  ndbout << "  Time after sleep =  " << millisec_now2 << endl;
  ndbout << "  Tried to sleep "<<sleeptimes[i]<<" milliseconds." << endl;
  ndbout << "  Sleep time was " << millisec_now2 -millisec_now <<" milliseconds." << endl;

  }

  ndbout << "TEST4 completed" << endl;

  ndbout << "= TEST5 ===============================" << endl;
  // Check NdbOut

  ndbout << "Testing hex and dec functions of NdbOut" << endl;

  for (int i = 0; i<= 0xFF; i++)
    {
      ndbout << i << "=" <<hex << i << "="<<dec << i << ", ";
    }

  ndbout << endl<< "Testing that hex is reset to dec by endl" << endl;
  ndbout << hex << 67 << endl;
  ndbout << 67 << endl;
  
  ndbout << "TEST5 completed" << endl;


  ndbout << "= TEST6 ===============================" << endl;
  const char* theEnvHostNamePtr;
  char buf[255];
  char theHostHostName[256];
  theEnvHostNamePtr = NdbEnv_GetEnv("HOSTNAME", buf, 255);
  if(theEnvHostNamePtr == NULL)
    fail("TEST6", "Could not get HOSTNAME from env");
  else{
    ndbout << "HOSTNAME from GetEnv" <<  theEnvHostNamePtr << endl;
 
    NdbHost_GetHostName(theHostHostName);
  
    ndbout << "HOSTNAME from GetHostName" <<theHostHostName << endl;

    if (strcmp(theEnvHostNamePtr, theHostHostName) != 0)
      fail("TEST6", "NdbHost_GetHostName or NdbEnv_GetEnv failed");
  }

  ndbout << "= TEST7 ===============================" << endl;

  testmutex = NdbMutex_Create();
  testcond = NdbCondition_Create();
  testthreadsdone = 0;
  
  for (int i = 0; i < T3_THREADS; i++)
    {
      t3args[i] = i;
      t3threads[i] = NdbThread_Create(testfunc, // Function 
				      (void**)&t3args[i],// Arg
				      2048,        // Stacksize
				      (char*)"test7thread",  // Thread name
				      NDB_THREAD_PRIO_MEAN); // Thread priority
    }

  ndbout << "All threads created" << endl;

  if (NdbMutex_Lock(testmutex) != 0)
    fail("TEST7", "NdbMutex_Lock failed");

  while (testthreadsdone < T3_THREADS*10)
    {
      // just testing the functionality without timing out, therefor 20 sec.
      if(NdbCondition_WaitTimeout(testcond, testmutex, 20000) != 0)
	fail("TEST7", "NdbCondition_WaitTimeout failed");
      ndbout << "Condition signaled, there are " << testthreadsdone << " completed threads" << endl;
    }
  if (NdbMutex_Unlock(testmutex) != 0)
    fail("TEST7", "NdbMutex_Unlock failed");

  for (int i = 0; i < T3_THREADS; i++)
  {
    if (NdbThread_WaitFor(t3threads[i], &status3) != 0)
      fail("TEST7", "NdbThread_WaitFor failed");

    NdbThread_Destroy(&t3threads[i]);
  }

  NdbMutex_Destroy(testmutex);
  NdbCondition_Destroy(testcond);

  ndbout << "TEST7 completed" << endl;


  ndbout << "= TEST8 ===============================" << endl;
  ndbout << "         NdbCondition_WaitTimeout" << endl;
  testmutex = NdbMutex_Create();
  testcond = NdbCondition_Create();

  for (int i = 0; i < 5; i++)
  {
    ndbout << "*------------------------------- Measure" << i << endl;

  NDB_TICKS millisec_now; 
  NDB_TICKS millisec_now2;

  millisec_now = NdbTick_CurrentMillisecond();
  if (NdbCondition_WaitTimeout(testcond, testmutex, sleeptimes[i]) != 0)
    fail("TEST8", "NdbCondition_WaitTimeout failed");
  millisec_now2 = NdbTick_CurrentMillisecond();

  ndbout << "  Time before WaitTimeout = " << millisec_now << endl;
  ndbout << "  Time after WaitTimeout =  " << millisec_now2 << endl;
  ndbout << "  Tried to wait "<<sleeptimes[i]<<" milliseconds." << endl;
  ndbout << "  Wait time was " << millisec_now2 -millisec_now <<" milliseconds." << endl;

  }

  ndbout << "TEST8 completed" << endl;

 
  ndbout << "= TEST9 ===============================" << endl;
  ndbout << "         NdbTick_CurrentXXXXXsecond compare" << endl;

  for (int i = 0; i < 5; i++)
  {
    ndbout << "*------------------------------- Measure" << i << endl;

  NDB_TICKS millisec_now; 
  NDB_TICKS millisec_now2;
  Uint32 usec_now, usec_now2;
  Uint64 msec_now, msec_now2;


  millisec_now = NdbTick_CurrentMillisecond();
  NdbTick_CurrentMicrosecond( &msec_now, &usec_now);

  NdbSleep_MilliSleep(sleeptimes[i]);

  millisec_now2 = NdbTick_CurrentMillisecond();
  NdbTick_CurrentMicrosecond( &msec_now2, &usec_now2);

  Uint64 usecdiff = time_diff(msec_now,msec_now2,usec_now,usec_now2);
  NDB_TICKS msecdiff = millisec_now2 -millisec_now;

  ndbout << "     Slept "<<sleeptimes[i]<<" milliseconds." << endl;
  ndbout << "  Measured " << msecdiff <<" milliseconds with milli function ." << endl;
  ndbout << "  Measured " << usecdiff/1000 << "," << usecdiff%1000<<" milliseconds with micro function ." << endl;
  }

  ndbout << "TEST9 completed" << endl;


  const int iter = 20;
  ndbout << "Testing microsecond timer - " << iter << " iterations" << endl;
  testMicros(iter);
  ndbout << "Testing microsecond timer - COMPLETED" << endl;

  ndbout << "= TEST10 ===============================" << endl;

  testmutex = NdbMutex_Create();
  testcond = NdbCondition_Create();
  testthreadsdone = 0;
  
  for (int i = 0; i < T3_THREADS; i++)
    {
      t3args[i] = i;
      t3threads[i] = NdbThread_Create(testTryLockfunc, // Function 
				      (void**)&t3args[i],// Arg
				      2048,        // Stacksize
				      (char*)"test10thread",  // Thread name
				      NDB_THREAD_PRIO_MEAN); // Thread priority
    }

  ndbout << "All threads created" << endl;

  if (NdbMutex_Lock(testmutex) != 0)
    fail("TEST10", "NdbMutex_Lock failed");

  while (testthreadsdone < T3_THREADS*10)
    {
      if(NdbCondition_Wait(testcond, testmutex) != 0)
	fail("TEST10", "NdbCondition_WaitTimeout failed");
      ndbout << "Condition signaled, there are " << testthreadsdone << " completed threads" << endl;
    }
  if (NdbMutex_Unlock(testmutex) != 0)
    fail("TEST10", "NdbMutex_Unlock failed");

  for (int i = 0; i < T3_THREADS; i++)
  {
    if (NdbThread_WaitFor(t3threads[i], &status3) != 0)
      fail("TEST10", "NdbThread_WaitFor failed");

    NdbThread_Destroy(&t3threads[i]);
  }

  NdbMutex_Destroy(testmutex);
  NdbCondition_Destroy(testcond);

  ndbout << "TEST10 completed" << endl;


  // Check total status of test

  if (TestHasFailed == 1)
    ndbout << endl << "TEST FAILED!" << endl;
  else
    ndbout << endl << "TEST PASSED!" << endl;

  return TestHasFailed;

};

Uint64 time_diff(Uint64 s1, Uint64 s2, Uint32 m1, Uint32 m2){

  Uint64 diff = 0;
  diff += (s2 - s1) * 1000000;
  if(m2 >= m1)
    diff += (m2 - m1);
  else {
    diff += m2;
    diff -= m1;
  }

  //  if(0)
  // ndbout("(s1,m1) = (%d, %d) (s2,m2) = (%d, %d) -> diff = %d\n",
  //   (Uint32)s1,m1,(Uint32)s2,m2, (Uint32)diff);
  
  return diff;
};

void 
testMicros(int count){
  Uint32 avg = 0;
  Uint32 sum2 = 0;

  for(int i = 0; i<count; i++){
    Uint64 s1, s2;
    Uint32 m1, m2;
    if(NdbTick_CurrentMicrosecond(&s1, &m1) != 0){
      ndbout << "Failed to get current micro" << endl;
      TestHasFailed = 1; 
      return;
    }
    Uint32 r = (rand() % 1000) + 1;
    NdbSleep_MilliSleep(r);
    if(NdbTick_CurrentMicrosecond(&s2, &m2) != 0){
      ndbout << "Failed to get current micro" << endl;
      TestHasFailed = 1; 
      return;
    }
    Uint64 m = time_diff(s1,s2,m1,m2);
    if(verbose)
      ndbout << "Slept for " << r << " ms" 
	     << " - Measured  " << m << " us" << endl;
    
    if(m > (r*1000)){
      avg += (m - (r*1000));
      sum2 += (m - (r*1000)) * (m - (r*1000));
    } else {
      avg += ((r*1000) - m);
      sum2 += ((r*1000) - m) * ((r*1000) - m);
    }
#if 0
    m /= 1000;
    if(m > r && ((m - r) > 10)){
      ndbout << "Difference to big: " << (m - r) << " - Test failed" << endl;
      TestHasFailed = 1;
    }
    if(m < r && ((r - m) > 10)){
      ndbout << "Difference to big: " << (r - m) << " - Test failed" << endl;
      TestHasFailed = 1;
    }
#endif
  }

  Uint32 dev = (avg * avg - sum2) / count; dev /= count;
  avg /= count;

  Uint32 t = 0;
  while((t*t)<dev) t++;
  ndbout << "NOTE - measure are compared to NdbSleep_MilliSleep(...)" << endl;
  ndbout << "Average error = " << avg << " us" << endl;
  ndbout << "Stddev  error = " << t << " us" << endl;
}