-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgasnet_diagnostic.c
1199 lines (1035 loc) · 40.1 KB
/
gasnet_diagnostic.c
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Source: bitbucket.org:berkeleylab/gasnet.git/gasnet_diagnostic.c $
* Description: GASNet internal diagnostics
* Copyright 2002, Dan Bonachea <bonachea@cs.berkeley.edu>
* Terms of use are as specified in license.txt
*/
#include <gasnet_internal.h>
/* filthy hack to allow simultaneous use of gasnet-internal and test.h facilities */
#undef malloc
#undef calloc
#undef realloc
#undef free
#undef strdup
#undef strndup
#undef assert
#define TEST_OMIT_CONFIGSTRINGS 1
#include <../tests/test.h>
#include <gasnet_handler.h>
/* this file should *only* contain symbols used for internal diagnostics,
so that we can avoid needlessly linking it into production executables
everything except the main entry point should be static, to prevent namespace pollution
*/
GASNETT_IDENT(gasneti_IdentString_diagnostics,
"$GASNetDiagnostics: (<link>) INCLUDES gasnet_diagnostic.o $");
#if GASNET_PAR
static void * thread_fn(void *arg);
#endif
static int num_threads = 1;
static int peer = -1;
static void * myseg = NULL;
static void * peerseg = NULL;
static void * peersegmid = NULL;
static int iters = 0;
static int iters0 = 0;
static int iters2 = 0;
#define gasneti_diag_hidx_base 128
#define gasnetc_diag_hidx_base 160
#define gasnete_diag_hidx_base 200
static int gasneti_diag_havehandlers = 0; /* true iff caller has registered our handler table */
static int id = 0;
#define TEST_HEADER(desc) \
PTHREAD_BARRIER(num_threads); \
if (!id) TEST_SECTION_BEGIN(); \
PTHREAD_BARRIER(num_threads); \
if (TEST_SECTION_ENABLED() && \
(MSG0("%c: %s %s...",TEST_SECTION_NAME(), \
(num_threads > 1?"parallel":"sequential"),desc),1))
#ifdef GASNETC_DIAGNOSTICS_SETUP
GASNETC_DIAGNOSTICS_SETUP /* can include helper source files, etc */
#endif
#ifdef GASNETE_DIAGNOSTICS_SETUP
GASNETE_DIAGNOSTICS_SETUP /* can include helper source files, etc */
#endif
#ifndef GASNETC_RUN_DIAGNOSTICS_SEQ
#define GASNETC_RUN_DIAGNOSTICS_SEQ(iters) (0)
#endif
#ifndef GASNETC_RUN_DIAGNOSTICS_PAR
#define GASNETC_RUN_DIAGNOSTICS_PAR(iters,threadid,numthreads) (0)
#endif
#ifndef GASNETE_RUN_DIAGNOSTICS_SEQ
#define GASNETE_RUN_DIAGNOSTICS_SEQ(iters) (0)
#endif
#ifndef GASNETE_RUN_DIAGNOSTICS_PAR
#define GASNETE_RUN_DIAGNOSTICS_PAR(iters,threadid,numthreads) (0)
#endif
/* ------------------------------------------------------------------------------------ */
/* misc sequential tests */
#if GASNET_DEBUG
extern gasneti_auxseg_request_t gasneti_auxseg_dummy(gasnet_seginfo_t *auxseg_info);
static void auxseg_test(void) {
BARRIER();
TEST_HEADER("auxseg test") {
gasneti_auxseg_dummy((void *)(uintptr_t)-1); /* call self-test */
}
}
#else
#define auxseg_test() TEST_HEADER("auxseg test - SKIPPED") do { } while(0)
#endif
static void mutex_test(int id);
static void rwlock_test(int id);
static void spinlock_test(int id);
static void cond_test(int id);
static void semaphore_test(int id);
static void lifo_test(int id);
static void atomic128_test(int id);
static void malloc_test(int id);
static void progressfns_test(int id);
static void op_test(int id);
/* ------------------------------------------------------------------------------------ */
/* run iters iterations of diagnostics and return zero on success
must be called collectively by exactly one thread on each node
in par mode, the test may internally spawn up to threadcnt threads
*/
extern int gasneti_run_diagnostics(int iter_cnt, int threadcnt, const char *testsections,
gasnet_seginfo_t const *seginfo) {
int i;
int partner = (gasnet_mynode() ^ 1);
if (partner == gasnet_nodes()) partner = gasnet_mynode();
test_errs = 0;
iters = iter_cnt;
iters2 = (iters <= INT_MAX/100) ? iters*100 : iters;
iters0 = MAX(1,iters/100);
peer = gasnet_mynode()^1;
if (peer == gasnet_nodes()) peer = gasnet_mynode();
assert_always(seginfo);
_test_seginfo = (gasnet_seginfo_t *)seginfo;
for (i=0; i < (int)gasnet_nodes(); i++) {
assert_always(_test_seginfo[i].size >= TEST_SEGSZ);
assert_always((((uintptr_t)_test_seginfo[i].addr) % PAGESZ) == 0);
}
myseg = TEST_MYSEG();
peerseg = TEST_SEG(peer);
peersegmid = (char *)peerseg + TEST_SEGSZ/2;
if (testsections) TEST_SECTION_PARSE(testsections);
TEST_GENERICS_WARNING();
auxseg_test();
BARRIER();
TEST_HEADER("malloc test") malloc_test(0);
BARRIER();
TEST_HEADER("gasneti_mutex_t test") mutex_test(0);
BARRIER();
TEST_HEADER("gasneti_cond_t test") cond_test(0);
BARRIER();
TEST_HEADER("gasneti_rwlock_t test") rwlock_test(0);
BARRIER();
spinlock_test(0);
BARRIER();
semaphore_test(0);
BARRIER();
atomic128_test(0);
BARRIER();
lifo_test(0);
BARRIER();
progressfns_test(0);
BARRIER();
op_test(0);
BARRIER();
TEST_HEADER("conduit tests") {
BARRIER();
test_errs += GASNETC_RUN_DIAGNOSTICS_SEQ(iters);
BARRIER();
test_errs += GASNETE_RUN_DIAGNOSTICS_SEQ(iters);
}
BARRIER();
#if GASNET_PAR
num_threads = threadcnt;
MSG0("spawning %i threads...", num_threads);
test_createandjoin_pthreads(num_threads, &thread_fn, NULL, 0);
#endif
BARRIER();
MSG0("GASNet internal diagnostics complete.");
return test_errs;
}
#undef MSG0
#undef ERR
#define MSG0 THREAD_MSG0(id)
#define ERR THREAD_ERR(id)
/* ------------------------------------------------------------------------------------ */
/* mixed parallel / sequential tests */
/* ------------------------------------------------------------------------------------ */
static void malloc_test(int id) {
int i, cnt = 0;
int maxobjs;
void **ptrs;
gasneti_heapstats_t stats_before, stats_after;
/* try to trigger any warm-up allocations potentially caused by barrier */
if (id == 0) {
/* each node takes a turn being a late arrival */
for (i=0; i < gasneti_nodes; i++) {
if (i == gasneti_mynode) {
uint64_t goal = gasnett_ticks_to_us(gasnett_ticks_now()) + 100000; /* 0.1s */
while (gasnett_ticks_to_us(gasnett_ticks_now()) < goal) {
gasnett_sched_yield();
}
}
BARRIER();
}
}
for (i=0; i < num_threads; i++) {
if (i == id) BARRIER(); /* each thread gets a chance */
PTHREAD_LOCALBARRIER(num_threads);
}
// bug 2788: try to isolate this test from conduit allocations that may take place during global barrier
sleep(1);
gasnet_AMPoll();
PTHREAD_LOCALBARRIER(num_threads);
if (!id) gasneti_getheapstats(&stats_before);
PTHREAD_LOCALBARRIER(num_threads);
gasneti_memcheck_all();
ptrs = gasneti_malloc_allowfail(8);
assert_always(ptrs);
gasneti_free(ptrs);
ptrs = gasneti_realloc(NULL,8);
assert_always(ptrs);
gasneti_free(ptrs);
gasneti_free(NULL);
PTHREAD_LOCALBARRIER(num_threads);
maxobjs = MIN(iters0,10000/num_threads);
ptrs = gasneti_calloc(maxobjs,sizeof(void*));
for (i = 0; i < maxobjs; i++) assert_always(ptrs[i] == NULL);
for (i = 0; i < iters/num_threads; i++) {
gasneti_memcheck_one();
if (cnt == maxobjs || (cnt > 0 && TEST_RAND_ONEIN(2))) {
size_t idx = TEST_RAND(0,cnt-1);
assert_always(ptrs[idx]);
gasneti_memcheck(ptrs[idx]);
if (TEST_RAND_ONEIN(2)) {
gasneti_free(ptrs[idx]);
cnt--;
ptrs[idx] = ptrs[cnt];
ptrs[cnt] = NULL;
} else {
ptrs[idx] = gasneti_realloc(ptrs[idx],TEST_RAND(1,16*1024));
}
} else {
void *p;
if (TEST_RAND_ONEIN(2)) {
p = gasneti_malloc(TEST_RAND(1,1024));
} else {
p = gasneti_calloc(1,TEST_RAND(1,1024));
}
if (TEST_RAND_ONEIN(4)) {
gasneti_leak(p);
}
gasneti_memcheck(p);
assert_always(p);
assert_always(ptrs[cnt] == NULL);
ptrs[cnt] = p;
cnt++;
}
}
gasneti_memcheck_all();
for (i = 0; i < cnt; i++) {
gasneti_free(ptrs[i]);
}
gasneti_free(ptrs);
gasneti_memcheck_all();
PTHREAD_LOCALBARRIER(num_threads);
for (i = 0; i < iters/num_threads; i++) {
int alignsz;
for (alignsz = 1; alignsz < 64*1024; alignsz *= 2) {
size_t sz = TEST_RAND(1,alignsz*2);
char * p = gasnett_malloc_aligned(alignsz,sz);
assert_always(p);
assert_always((((uintptr_t)p) & (alignsz-1)) == 0);
p[0] = 'x'; p[sz - 1] = 'y';
if (TEST_RAND_ONEIN(4)) {
gasneti_leak_aligned(p);
}
gasnett_free_aligned(p);
}
}
gasneti_memcheck_all();
PTHREAD_LOCALBARRIER(num_threads);
if (!id) {
gasneti_getheapstats(&stats_after);
#if GASNET_DEBUG
{
#if GASNETI_CONDUIT_THREADS
float tol = 0.1; /* allow for some heap change if a conduit thread is around */
#else
float tol = 0; /* we have all the threads, and nothing else should be allocating */
#endif
long delta_bytes = (long long)stats_before.live_bytes - (long long)stats_after.live_bytes;
long delta_objects = (long long)stats_before.live_objects - (long long)stats_after.live_objects;
if (labs(delta_bytes)/(double)stats_after.live_bytes > tol ||
labs(delta_objects)/(double)stats_after.live_objects > tol)
MSG("ERROR: unexpected heap size change:\n"
" stats_before.live_bytes=%llu stats_after.live_bytes=%llu\n"
" stats_before.live_objects=%llu stats_after.live_objects=%llu",
(unsigned long long)stats_before.live_bytes, (unsigned long long)stats_after.live_bytes,
(unsigned long long)stats_before.live_objects, (unsigned long long)stats_after.live_objects);
}
#endif
}
sleep(1);
PTHREAD_BARRIER(num_threads);
}
/* ------------------------------------------------------------------------------------ */
static void cond_test(int id) {
static gasneti_cond_t cond1 = GASNETI_COND_INITIALIZER;
static gasneti_cond_t cond2;
static gasneti_mutex_t lock1 = GASNETI_MUTEX_INITIALIZER;
static uint32_t done = 0;
int i;
PTHREAD_BARRIER(num_threads);
if (!id) {
gasneti_cond_init(&cond2);
gasneti_cond_destroy(&cond2);
gasneti_cond_init(&cond2);
gasneti_mutex_lock(&lock1);
gasneti_cond_signal(&cond1);
gasneti_cond_signal(&cond2);
gasneti_cond_broadcast(&cond1);
gasneti_cond_broadcast(&cond2);
gasneti_mutex_unlock(&lock1);
}
PTHREAD_BARRIER(num_threads);
if (!id) { /* awake thread */
for (i = 0; i < iters2; i++) {
gasneti_mutex_lock(&lock1);
if (i&1) {
gasneti_cond_signal(&cond1);
} else {
gasneti_cond_broadcast(&cond1);
}
gasneti_mutex_unlock(&lock1);
if (TEST_RAND_ONEIN(iters)) gasnett_sched_yield();
}
gasneti_mutex_lock(&lock1);
done = 1;
gasneti_cond_broadcast(&cond1);
gasneti_mutex_unlock(&lock1);
} else {
gasneti_mutex_lock(&lock1);
while (!done) {
gasneti_mutex_assertlocked(&lock1);
gasneti_cond_wait(&cond1, &lock1);
}
gasneti_mutex_unlock(&lock1);
gasneti_mutex_assertunlocked(&lock1);
}
PTHREAD_BARRIER(num_threads);
}
/* ------------------------------------------------------------------------------------ */
static void mutex_test(int id) {
static gasneti_mutex_t lock1 = GASNETI_MUTEX_INITIALIZER;
static gasneti_mutex_t lock2;
static unsigned int counter;
unsigned int count = iters2 / num_threads;
int i;
PTHREAD_BARRIER(num_threads);
if (!id) {
for (i=0; i<10; i++) {
gasneti_mutex_assertunlocked(&lock1);
gasneti_mutex_lock(&lock1);
gasneti_mutex_assertlocked(&lock1);
gasneti_mutex_unlock(&lock1);
gasneti_mutex_assertunlocked(&lock1);
assert_always(gasneti_mutex_trylock(&lock1) == GASNET_OK);
gasneti_mutex_assertlocked(&lock1);
gasneti_mutex_unlock(&lock1);
gasneti_mutex_init(&lock2);
gasneti_mutex_assertunlocked(&lock2);
gasneti_mutex_lock(&lock2);
gasneti_mutex_assertlocked(&lock2);
gasneti_mutex_unlock(&lock2);
gasneti_mutex_assertunlocked(&lock2);
gasneti_mutex_destroy(&lock2);
}
counter = 0;
}
PTHREAD_BARRIER(num_threads);
for (i=0;i<count;i++) {
if (i&1) {
gasneti_mutex_lock(&lock1);
} else {
int retval;
while ((retval=gasneti_mutex_trylock(&lock1)) != 0) {
assert_always(retval == EBUSY);
}
}
counter++;
gasneti_mutex_unlock(&lock1);
}
PTHREAD_BARRIER(num_threads);
if (counter != (num_threads * count))
ERR("failed mutex test: counter=%i expecting=%i", counter, (num_threads * count));
PTHREAD_BARRIER(num_threads);
}
/* ------------------------------------------------------------------------------------ */
static void rwlock_test(int id) {
static gasneti_rwlock_t lock1 = GASNETI_RWLOCK_INITIALIZER;
static gasneti_rwlock_t lock2;
#define NUMCHECK 256
static unsigned int check[NUMCHECK];
static unsigned int *numwrites;
unsigned int count = iters2 / num_threads;
int i;
int trywrite = 0;
PTHREAD_BARRIER(num_threads);
if (!id) { /* serial tests */
for (i=0; i<10; i++) {
gasneti_rwlock_assertunlocked(&lock1);
gasneti_rwlock_rdlock(&lock1);
gasneti_rwlock_assertlocked(&lock1);
gasneti_rwlock_assertrdlocked(&lock1);
gasneti_rwlock_unlock(&lock1);
gasneti_rwlock_assertunlocked(&lock1);
gasneti_rwlock_wrlock(&lock1);
gasneti_rwlock_assertlocked(&lock1);
gasneti_rwlock_assertwrlocked(&lock1);
gasneti_rwlock_unlock(&lock1);
gasneti_rwlock_assertunlocked(&lock1);
assert_always(gasneti_rwlock_tryrdlock(&lock1) == GASNET_OK);
gasneti_rwlock_assertlocked(&lock1);
gasneti_rwlock_assertrdlocked(&lock1);
gasneti_rwlock_unlock(&lock1);
assert_always(gasneti_rwlock_trywrlock(&lock1) == GASNET_OK);
gasneti_rwlock_assertlocked(&lock1);
gasneti_rwlock_assertwrlocked(&lock1);
gasneti_rwlock_unlock(&lock1);
gasneti_rwlock_init(&lock2);
gasneti_rwlock_assertunlocked(&lock2);
gasneti_rwlock_rdlock(&lock2);
gasneti_rwlock_assertlocked(&lock2);
gasneti_rwlock_assertrdlocked(&lock2);
gasneti_rwlock_unlock(&lock2);
gasneti_rwlock_wrlock(&lock2);
gasneti_rwlock_assertlocked(&lock2);
gasneti_rwlock_assertwrlocked(&lock2);
gasneti_rwlock_unlock(&lock2);
gasneti_rwlock_assertunlocked(&lock2);
gasneti_rwlock_destroy(&lock2);
}
numwrites = gasneti_calloc(num_threads, sizeof(*numwrites));
memset(check, 0, sizeof(check));
}
PTHREAD_BARRIER(num_threads);
for (i=0;i<count;i++) {
int j;
const int writer = ((id + i + 1) & 0xFF) == 1; /* early and infrequent writes */
if (writer) { /* write lock */
if (trywrite++ & 1) {
int retval;
while ((retval=gasneti_rwlock_trywrlock(&lock1)) != 0) {
assert_always(retval == EBUSY);
}
} else {
gasneti_rwlock_wrlock(&lock1);
}
gasneti_rwlock_assertwrlocked(&lock1);
/* perform writes */
for (j=NUMCHECK-1; j >= 0; j--) {
check[j]++;
}
numwrites[id]++;
}
if (!writer) { /* read lock */
if (i & 1) {
int retval;
while ((retval=gasneti_rwlock_tryrdlock(&lock1)) != 0) {
assert_always(retval == EBUSY);
}
} else {
gasneti_rwlock_rdlock(&lock1);
}
gasneti_rwlock_assertrdlocked(&lock1);
}
{ /* read-only check */
const unsigned int val = check[0]; /* try to detect a concurrent writer */
int k;
for (j=0; j < 10; j++) {
for (k=0; k < NUMCHECK; k++) {
unsigned int cv = check[k];
if_pf (cv != val)
ERR("failed rwlock test: check[%i]=%i expecting=%i", k, cv, val);
}
}
}
gasneti_rwlock_unlock(&lock1);
}
PTHREAD_BARRIER(num_threads);
if (!id) { /* final verification */
int sum = 0;
for (i=0; i < num_threads; i++) sum += numwrites[i];
assert_always(sum > 0);
for (i=0; i < NUMCHECK; i++) {
unsigned int cv = check[i];
if_pf (cv != sum)
ERR("failed rwlock test: check[%i]=%i expecting=%i", i, cv, sum);
}
gasneti_free(numwrites);
}
PTHREAD_BARRIER(num_threads);
}
/* ------------------------------------------------------------------------------------ */
#if GASNETI_HAVE_SPINLOCK
static void spinlock_test(int id) {
static gasneti_atomic_t lock1 = GASNETI_SPINLOCK_INITIALIZER;
static gasneti_atomic_t lock2;
static unsigned int counter;
unsigned int count = iters2 / num_threads;
int i;
PTHREAD_BARRIER(num_threads);
TEST_HEADER("spinlock test"); else return;
if (!id) {
gasneti_spinlock_lock(&lock1);
gasneti_spinlock_unlock(&lock1);
assert_always(gasneti_spinlock_trylock(&lock1) == GASNET_OK);
gasneti_spinlock_unlock(&lock1);
gasneti_spinlock_init(&lock2);
gasneti_spinlock_lock(&lock2);
gasneti_spinlock_unlock(&lock2);
gasneti_spinlock_destroy(&lock2);
gasneti_spinlock_init(&lock2);
counter = 0;
}
PTHREAD_BARRIER(num_threads);
for (i=0;i<count;i++) {
if (i&1) {
gasneti_spinlock_lock(&lock1);
} else {
int retval;
while ((retval=gasneti_spinlock_trylock(&lock1))) {
assert_always(retval == EBUSY);
}
}
counter++;
gasneti_spinlock_unlock(&lock1);
}
PTHREAD_BARRIER(num_threads);
if (counter != (num_threads * count))
ERR("failed spinlock test: counter=%i expecting=%i", counter, (num_threads * count));
PTHREAD_BARRIER(num_threads);
}
#else
static void spinlock_test(int id) {
TEST_HEADER("spinlock test - SKIPPED"); else return;
}
#endif
/* ------------------------------------------------------------------------------------ */
static void semaphore_test(int id) {
static gasneti_semaphore_t sema1 = GASNETI_SEMAPHORE_INITIALIZER(GASNETI_SEMAPHORE_MAX,0);
static gasneti_semaphore_t sema2;
static gasneti_atomic_t counter;
int count = iters2 / num_threads;
gasneti_atomic_val_t limit = MIN(1000000, num_threads * count);
int i;
PTHREAD_BARRIER(num_threads);
TEST_HEADER("semaphore test"); else return;
if (!id) {
if (!gasneti_semaphore_trydown(&sema1))
ERR("failed semaphore test: 'down' from GASNETI_SEMAPHORE_MAX failed");
gasneti_semaphore_up(&sema1);
if (gasneti_semaphore_read(&sema1) != GASNETI_SEMAPHORE_MAX)
ERR("failed semaphore test: 'up' to GASNETI_SEMAPHORE_MAX failed");
gasneti_semaphore_init(&sema2, limit, limit);
if (!gasneti_semaphore_trydown(&sema2))
ERR("failed semaphore test: trydown failed");
if (gasneti_semaphore_trydown_n(&sema2,4) != 4)
ERR("failed semaphore test: trydown_n failed");
if (gasneti_semaphore_trydown_partial(&sema2,5) != 5)
ERR("failed semaphore test: trydown_partial failed");
gasneti_semaphore_up_n(&sema2,10);
if (gasneti_semaphore_read(&sema2) != limit)
ERR("failed semaphore test: up/down test failed");
gasneti_semaphore_destroy(&sema2);
gasneti_semaphore_init(&sema2, limit, limit);
gasneti_atomic_set(&counter, 0, 0);
}
PTHREAD_BARRIER(num_threads);
for (i=0;i<count;i++) {
if (gasneti_semaphore_trydown(&sema1))
gasneti_semaphore_up(&sema1);
}
PTHREAD_BARRIER(num_threads);
while (gasneti_semaphore_trydown(&sema2))
gasneti_atomic_increment(&counter, 0);
if (gasneti_semaphore_trydown(&sema2))
ERR("failed semaphore test: trydown pounding test failed");
PTHREAD_BARRIER(num_threads);
if (gasneti_semaphore_read(&sema1) != GASNETI_SEMAPHORE_MAX)
ERR("failed semaphore test: trydown/up pounding test failed");
if (gasneti_atomic_read(&counter, 0) != limit)
ERR("failed semaphore test: trydown pounding test failed");
PTHREAD_BARRIER(num_threads);
if (!id) gasneti_semaphore_destroy(&sema2);
}
/* ------------------------------------------------------------------------------------ */
#if GASNETI_HAVE_ATOMIC128_T
static void atomic128_test(int id) {
char _var128[16 + GASNETI_HAVE_ATOMIC128_T - 1]; /* Space for var128 + alignment padding */
gasneti_atomic128_t *var128 = (gasneti_atomic128_t *)
(((uintptr_t)_var128 + GASNETI_HAVE_ATOMIC128_T - 1) & ~(GASNETI_HAVE_ATOMIC128_T - 1));
uint64_t readhi, readlo;
const uint64_t one64 = 1;
int i;
TEST_HEADER("128-bit atomic CAS test"); else return;
gasneti_atomic128_set(var128, 0, 2*iters, 0);
gasneti_atomic128_read(&readhi, &readlo, var128,0);
if ((readlo != (uint64_t)(2*iters)) || readhi)
ERR("gasneti_atomic128_set/gasneti_atomic128_read got wrong value (lo half)");
gasneti_atomic128_set(var128, 2*iters, 0, 0);
gasneti_atomic128_read(&readhi, &readlo, var128,0);
if ((readhi != (uint64_t)(2*iters)) || readlo)
ERR("gasneti_atomic128_set/gasneti_atomic128_read got wrong value (hi half)");
/* single bit-marching tests */
for (i=0;i<128;i++) {
const uint64_t tmplo = (i < 64) ? (one64<<i) : 0;
const uint64_t tmphi = (i >= 64) ? (one64<<(i-64)) : 0;
gasneti_atomic128_set(var128, tmphi, tmplo, 0);
gasneti_atomic128_read(&readhi, &readlo, var128, 0);
if ((readlo != tmplo) || (readhi != tmphi))
ERR("gasneti_atomic128_set/gasneti_atomic128_read got wrong value on bit %i", i);
if (gasneti_atomic128_compare_and_swap(var128, 0, 0, tmphi, tmplo, 0))
ERR("gasneti_atomic128_compare_and_swap succeeded at bit %i when it should have failed", i);
if (!gasneti_atomic128_compare_and_swap(var128, tmphi, tmplo, 0, 0, 0))
ERR("gasneti_atomic128_compare_and_swap failed at bit %i when it should have succeeded", i);
}
/* double bit-marching tests */
for (i=0;i<128;i++) {
int j;
for (j=0;j<i;j++) {
const uint64_t tmplo_i = (i < 64) ? (one64<<i) : 0;
const uint64_t tmphi_i = (i >= 64) ? (one64<<(i-64)) : 0;
const uint64_t tmplo_j = (j < 64) ? (one64<<j) : 0;
const uint64_t tmphi_j = (j >= 64) ? (one64<<(j-64)) : 0;
const uint64_t tmplo = tmplo_i | tmplo_j;
const uint64_t tmphi = tmphi_i | tmphi_j;
gasneti_atomic128_set(var128, tmphi, tmplo, 0);
if (gasneti_atomic128_compare_and_swap(var128, tmphi_i, tmplo_i, tmphi, tmplo, 0) ||
gasneti_atomic128_compare_and_swap(var128, tmphi_j, tmplo_j, tmphi, tmplo, 0))
ERR("gasneti_atomic128_compare_and_swap succeeded at bits %i and %i when it should have failed", i, j);
}
}
gasneti_atomic128_set(var128, iters, 0, 0);
for (i=0;i<iters;i++) {
if (gasneti_atomic128_compare_and_swap(var128, iters+i-1, i-1, iters+i-2, i-2, 0))
ERR("gasneti_atomic128_compare_and_swap succeeded at i=%i when it should have failed", i);
if (gasneti_atomic128_compare_and_swap(var128, iters+i+1, i+1, iters+i-2, i-2, 0))
ERR("gasneti_atomic128_compare_and_swap succeeded at i=%i when it should have failed", i);
gasneti_atomic128_read(&readhi, &readlo,var128,0);
if (((int)readhi != iters+i) || ((int)readlo != i))
ERR("gasneti_atomic128_compare_and_swap altered value when it should not have at i=%i", i);
if (!gasneti_atomic128_compare_and_swap(var128, iters+i, i, iters+i+1, i+1, 0))
ERR("gasneti_atomic128_compare_and_swap failed at i=%i when it should have succeeded", i);
gasneti_atomic128_read(&readhi, &readlo, var128,0);
if (((int)readhi != iters+i+1) || ((int)readlo != i+1))
ERR("gasneti_atomic128_compare_and_swap set wrong updated value at i=%i", i);
}
}
#else
static void atomic128_test(int id) {
TEST_HEADER("128-bit atomic test - SKIPPED"); else return;
}
#endif
/* ------------------------------------------------------------------------------------ */
static void lifo_test(int id) {
static gasneti_lifo_head_t lifo1 = GASNETI_LIFO_INITIALIZER;
static gasneti_lifo_head_t lifo2;
static gasneti_atomic_t counter;
int count = iters2 / num_threads;
int limit = MIN(1000000, num_threads * count);
int i;
PTHREAD_BARRIER(num_threads);
TEST_HEADER("lifo test"); else return;
{
void * tmp = test_malloc(sizeof(void *));
for (i = 0; i < count; ++i) {
gasneti_lifo_push(&lifo1, tmp);
tmp = gasneti_lifo_pop(&lifo1);
if (tmp == NULL)
ERR("failed lifo test: 1-each pop/push test failed at iteration %d", i);
}
PTHREAD_BARRIER(num_threads); /* See bug 2711 */
test_free(tmp);
}
#if 0 /* Redundant due to barrier inserted for bug2711 */
PTHREAD_BARRIER(num_threads);
#endif
if (!id) {
gasneti_lifo_init(&lifo2);
for (i = 0; i < limit; ++i)
gasneti_lifo_push(&lifo1, test_malloc(sizeof(void *)));
for (i = 0; i < limit; ++i) {
void * tmp = gasneti_lifo_pop(&lifo1);
if (tmp == NULL)
ERR("failed lifo test: push/pop test failed");
gasneti_lifo_push(&lifo2, tmp);
}
if (gasneti_lifo_pop(&lifo1) != NULL)
ERR("failed lifo test: push/pop test failed");
for (i = 0; i < limit; ++i) {
void * tmp = gasneti_lifo_pop(&lifo2);
if (tmp == NULL)
ERR("failed lifo test: push/pop test failed");
gasneti_lifo_push(&lifo1, tmp);
}
if (gasneti_lifo_pop(&lifo2) != NULL)
ERR("failed lifo test: push/pop test failed");
gasneti_lifo_destroy(&lifo2);
gasneti_lifo_init(&lifo2);
gasneti_atomic_set(&counter, 0, 0);
}
PTHREAD_BARRIER(num_threads);
for (i=0;i<count;i++) {
void * tmp = gasneti_lifo_pop(&lifo1);
if (tmp != NULL) {
gasneti_lifo_push(&lifo2, tmp);
gasneti_atomic_increment(&counter, 0);
}
}
PTHREAD_BARRIER(num_threads);
if ((gasneti_lifo_pop(&lifo1) != NULL) || (gasneti_atomic_read(&counter, 0) != limit))
ERR("failed lifo test: push/pop pounding test failed");
PTHREAD_BARRIER(num_threads);
{
void * head = NULL;
for (i=0;i<count;i++) {
void * tmp = gasneti_lifo_pop(&lifo2);
if (tmp != NULL) {
*(void **)tmp = head;
head = tmp;
gasneti_atomic_decrement(&counter, 0);
}
}
PTHREAD_BARRIER(num_threads); /* Barrier before free() for bug 2711 */
while (head != NULL) {
void * next = *(void **)head;
test_free(head);
head = next;
}
}
#if 0 /* Redundant due to barrier inserted for bug2711 */
PTHREAD_BARRIER(num_threads);
#endif
if ((gasneti_lifo_pop(&lifo2) != NULL) || (gasneti_atomic_read(&counter, 0) != 0))
ERR("failed lifo test: push/pop pounding test failed");
PTHREAD_BARRIER(num_threads);
if (!id) gasneti_lifo_destroy(&lifo2);
}
/* ------------------------------------------------------------------------------------ */
static int pf_cnt_boolean, pf_cnt_counted;
static gasnet_hsl_t pf_lock = GASNET_HSL_INITIALIZER;
static gasneti_weakatomic_t progressfn_req_sent = gasneti_weakatomic_init(0);
static gasneti_weakatomic_t progressfn_rep_rcvd = gasneti_weakatomic_init(0);
static void progressfn_reqh(gasnet_token_t token, void *buf, size_t nbytes) {
GASNET_Safe(gasnet_AMReplyMedium0(token, gasneti_diag_hidx_base + 1, buf, nbytes));
}
static void progressfn_reph(gasnet_token_t token, void *buf, size_t nbytes) {
gasneti_weakatomic_increment(&progressfn_rep_rcvd,0);
}
static void progressfn_tester(int *counter) {
static int active = 0; /* protocol provides mutual exclusion & recursion protection */
int iamactive = 0;
gasnet_hsl_lock(&pf_lock);
(*counter)++;
if (!active) { active = 1; iamactive = 1; }
gasnet_hsl_unlock(&pf_lock);
if (!iamactive) return;
/* do some work that should be legal inside a progress fn */
#if 0
if ((gasneti_weakatomic_read(&progressfn_req_sent,0) -
gasneti_weakatomic_read(&progressfn_rep_rcvd,0)) <= 128U)
#endif
{ static int tmp = 47;
int sz;
gasnet_put_nbi(peer, peersegmid, &tmp, sizeof(tmp));
gasnet_get_nbi(&tmp, peer, peersegmid, sizeof(tmp));
for (sz = 1; sz <= MIN(128*1024,TEST_SEGSZ/2); sz = (sz < 64?sz*2:sz*8)) {
gasnet_put_nbi_bulk(peer, peersegmid, myseg, sz);
gasnet_get_nbi_bulk(myseg, peer, peersegmid, sz);
}
sz = gasnet_try_syncnbi_all();
if (gasneti_diag_havehandlers) {
for (sz = 1; sz <= MIN(gasnet_AMMaxMedium(),MIN(64*1024,TEST_SEGSZ/2)); sz = (sz < 64?sz*2:sz*8)) {
gasneti_weakatomic_increment(&progressfn_req_sent,0);
gasnet_AMRequestMedium0(peer, gasneti_diag_hidx_base + 0, myseg, sz);
gasneti_weakatomic_increment(&progressfn_req_sent,0);
gasnet_AMRequestLong0(peer, gasneti_diag_hidx_base + 0, myseg, sz, peersegmid);
}
}
}
gasneti_local_mb();
active = 0;
}
static void progressfn_bool(void) { progressfn_tester(&pf_cnt_boolean); }
static void progressfn_counted(void) { progressfn_tester(&pf_cnt_counted); }
static void progressfns_test(int id) {
#if !GASNET_DEBUG
TEST_HEADER("progress functions test - SKIPPED"); else return;
return;
#else
int iter;
TEST_HEADER("progress functions test"); else return;
for (iter=0; iter < iters0; iter++) {
int i;
int cnt_c = 0, cnt_b = 0;
PTHREAD_BARRIER(num_threads);
gasneti_debug_progressfn_bool = progressfn_bool;
gasneti_debug_progressfn_counted = progressfn_counted;
pf_cnt_boolean = 0;
pf_cnt_counted = 0;
PTHREAD_BARRIER(num_threads);
if (!id) GASNETI_PROGRESSFNS_ENABLE(gasneti_pf_debug_boolean,BOOLEAN);
PTHREAD_BARRIER(num_threads);
GASNETI_PROGRESSFNS_ENABLE(gasneti_pf_debug_counted,COUNTED);
GASNETI_PROGRESSFNS_ENABLE(gasneti_pf_debug_counted,COUNTED);
GASNETI_PROGRESSFNS_DISABLE(gasneti_pf_debug_counted,COUNTED);
/* do some work that should cause progress fns to run */
for (i=0; i < 2; i++) {
int tmp;
gasnet_put(peer, peerseg, &tmp, sizeof(tmp));
gasnet_get(&tmp, peer, peerseg, sizeof(tmp));
gasnet_put_bulk(peer, peersegmid, myseg, 1024);
gasnet_get_bulk(myseg, peer, peersegmid, 1024);
gasnet_AMPoll();
}
PTHREAD_BARRIER(num_threads);
/* ensure they did run */
cnt_c = pf_cnt_counted; cnt_b = pf_cnt_boolean;
assert_always(cnt_c > 0); assert_always(cnt_b > 0);
/* disable progress fns and quiesce the system */
PTHREAD_BARRIER(num_threads);
if (!id) GASNETI_PROGRESSFNS_DISABLE(gasneti_pf_debug_boolean,BOOLEAN);
GASNETI_PROGRESSFNS_DISABLE(gasneti_pf_debug_counted,COUNTED);
PTHREAD_BARRIER(num_threads);
for (i=0; i < 1000; i++) { gasnet_AMPoll(); gasneti_sched_yield(); }
PTHREAD_BARRIER(num_threads);
cnt_c = pf_cnt_counted; cnt_b = pf_cnt_boolean;
PTHREAD_BARRIER(num_threads);
/* do some work that might cause progress fns to run */
for (i=0; i < 2; i++) {
int tmp;
gasnet_put(peer, peerseg, &tmp, sizeof(tmp));
gasnet_get(&tmp, peer, peerseg, sizeof(tmp));
gasnet_put_bulk(peer, peersegmid, myseg, 1024);
gasnet_get_bulk(myseg, peer, peersegmid, 1024);
gasnet_AMPoll();
}
PTHREAD_BARRIER(num_threads);
/* ensure they did not run */
assert_always(cnt_c == pf_cnt_counted); assert_always(cnt_b == pf_cnt_boolean);
}
GASNET_BLOCKUNTIL(gasneti_weakatomic_read(&progressfn_req_sent,0) ==
gasneti_weakatomic_read(&progressfn_rep_rcvd,0));
#endif
}
/* ------------------------------------------------------------------------------------ */
#if GASNETI_HAVE_EOP_INTERFACE
static void op_test(int id) {
int iter;
GASNET_BEGIN_FUNCTION();
PTHREAD_BARRIER(num_threads);
TEST_HEADER("internal op interface test"); else return;
for (iter=0; iter < iters0; iter++) {
static const void **share = NULL;
int peerid = ( id + 1 ) % num_threads;
PTHREAD_BARRIER(num_threads);
gasnet_wait_syncnbi_all();
PTHREAD_BARRIER(num_threads);
if (!id) share = test_malloc(sizeof(void *)*num_threads);
PTHREAD_BARRIER(num_threads);
{ gasneti_eop_t *eop;
gasnet_handle_t h;
eop = gasneti_eop_create(GASNETE_THREAD_GET_ALONE);
assert_always(eop);
h = gasneti_eop_to_handle(eop);
assert_always(gasnet_try_syncnb(h) == GASNET_ERR_NOT_READY);
share[id] = eop; /* hand-off eop to neighbor thread */
PTHREAD_BARRIER(num_threads);
gasneti_eop_markdone(share[peerid]); /* mark right neighbor's eop done */
PTHREAD_BARRIER(num_threads);
assert_always(gasnet_try_syncnb(h) == GASNET_OK);
}
PTHREAD_BARRIER(num_threads);
{ /* inc the get and put counts on my iop */
gasneti_iop_t *iop = gasneti_iop_register(1, 0 GASNETE_THREAD_GET);
assert_always(iop);
assert_always(gasnet_try_syncnbi_puts() == GASNET_ERR_NOT_READY);
assert_always(gasnet_try_syncnbi_gets() == GASNET_OK);
assert_always(gasnet_try_syncnbi_all() == GASNET_ERR_NOT_READY);
assert_always(iop == gasneti_iop_register(2, 1 GASNETE_THREAD_GET));
assert_always(gasnet_try_syncnbi_puts() == GASNET_ERR_NOT_READY);
assert_always(gasnet_try_syncnbi_gets() == GASNET_ERR_NOT_READY);
assert_always(gasnet_try_syncnbi_all() == GASNET_ERR_NOT_READY);
share[id] = iop; /* hand-off iop to neighbor thread */