This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
forked from redhat-performance/libMicro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmicro.c
1563 lines (1281 loc) · 29.5 KB
/
libmicro.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* usr/src/OPENSOLARIS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* benchmarking routines
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include <pthread.h>
#include <dlfcn.h>
#include <errno.h>
#include <sys/resource.h>
#include <math.h>
#include <limits.h>
#ifdef __sun
#include <sys/elf.h>
#endif
#include "libmicro.h"
/*
* user visible globals
*/
int lm_argc = 0;
char ** lm_argv = NULL;
int lm_opt1;
int lm_optA;
int lm_optB;
int lm_optC = 100;
int lm_optD;
int lm_optE;
int lm_optH;
int lm_optI;
int lm_optL = 0;
int lm_optM = 0;
char *lm_optN;
int lm_optP;
int lm_optS;
int lm_optT;
int lm_optW;
int lm_def1 = 0;
int lm_defB = 0; /* use lm_nsecs_per_op */
int lm_defD = 10;
int lm_defH = 0;
char *lm_defN = NULL;
int lm_defP = 1;
int lm_defS = 0;
int lm_defT = 1;
/*
* default on fast platform, should be overridden by individual
* benchmarks if significantly wrong in either direction.
*/
int lm_nsecs_per_op = 5;
char *lm_procpath;
char lm_procname[STRSIZE];
char lm_usage[STRSIZE];
char lm_optstr[STRSIZE];
char lm_header[STRSIZE];
size_t lm_tsdsize = 0;
/*
* Globals we do not export to the user
*/
static barrier_t *lm_barrier;
static pid_t *pids = NULL;
static pthread_t *tids = NULL;
static int pindex = -1;
static void *tsdseg = NULL;
static size_t tsdsize = 0;
#ifdef USE_RDTSC
static long long lm_hz = 0;
#endif
/*
* Forward references
*/
static void worker_process();
static void usage();
static void print_stats(barrier_t *);
static void print_histo(barrier_t *);
static int remove_outliers(double *, int, stats_t *);
static long long nsecs_overhead;
static long long nsecs_resolution;
static long long get_nsecs_overhead();
static int crunch_stats(double *, int, stats_t *);
static void compute_stats(barrier_t *);
/*
* main routine; renamed in this file to allow linking with other
* files
*/
int
actual_main(int argc, char *argv[])
{
int i;
int opt;
extern char *optarg;
char *tmp;
char optstr[256];
barrier_t *b;
long long startnsecs;
#ifdef USE_RDTSC
if (getenv("LIBMICRO_HZ") == NULL) {
(void) printf("LIBMICRO_HZ needed but not set\n");
exit(1);
}
lm_hz = strtoll(getenv("LIBMICRO_HZ"), NULL, 10);
#endif
startnsecs = getnsecs();
lm_argc = argc;
lm_argv = argv;
/* before we do anything */
(void) benchmark_init();
nsecs_overhead = get_nsecs_overhead();
nsecs_resolution = get_nsecs_resolution();
/*
* Set defaults
*/
lm_opt1 = lm_def1;
lm_optB = lm_defB;
lm_optD = lm_defD;
lm_optH = lm_defH;
lm_optN = lm_defN;
lm_optP = lm_defP;
lm_optS = lm_defS;
lm_optT = lm_defT;
/*
* squirrel away the path to the current
* binary in a way that works on both
* Linux and Solaris
*/
if (*argv[0] == '/') {
lm_procpath = strdup(argv[0]);
*strrchr(lm_procpath, '/') = 0;
} else {
char path[1024];
(void) getcwd(path, 1024);
(void) strcat(path, "/");
(void) strcat(path, argv[0]);
*strrchr(path, '/') = 0;
lm_procpath = strdup(path);
}
/*
* name of binary
*/
if ((tmp = strrchr(argv[0], '/')) == NULL)
(void) strcpy(lm_procname, argv[0]);
else
(void) strcpy(lm_procname, tmp + 1);
if (lm_optN == NULL) {
lm_optN = lm_procname;
}
/*
* Parse command line arguments
*/
(void) sprintf(optstr, "1AB:C:D:EHI:LMN:P:RST:VW?%s", lm_optstr);
while ((opt = getopt(argc, argv, optstr)) != -1) {
switch (opt) {
case '1':
lm_opt1 = 1;
break;
case 'A':
lm_optA = 1;
break;
case 'B':
lm_optB = sizetoint(optarg);
break;
case 'C':
lm_optC = sizetoint(optarg);
break;
case 'D':
lm_optD = sizetoint(optarg);
break;
case 'E':
lm_optE = 1;
break;
case 'H':
lm_optH = 1;
break;
case 'I':
lm_optI = sizetoint(optarg);
break;
case 'L':
lm_optL = 1;
break;
case 'M':
lm_optM = 1;
break;
case 'N':
lm_optN = optarg;
break;
case 'P':
lm_optP = sizetoint(optarg);
break;
case 'S':
lm_optS = 1;
break;
case 'T':
lm_optT = sizetoint(optarg);
break;
case 'V':
(void) printf("%s\n", LIBMICRO_VERSION);
exit(0);
break;
case 'W':
lm_optW = 1;
lm_optS = 1;
break;
case '?':
usage();
exit(0);
break;
default:
if (benchmark_optswitch(opt, optarg) == -1) {
usage();
exit(0);
}
}
}
/* deal with implicit and overriding options */
if (lm_opt1 && lm_optP > 1) {
lm_optP = 1;
(void) printf("warning: -1 overrides -P\n");
}
if (lm_optE) {
(void) fprintf(stderr, "Running:%20s", lm_optN);
(void) fflush(stderr);
}
if (lm_optB == 0) {
/*
* neither benchmark or user has specified the number
* of cnts/sample, so use computed value
*/
if (lm_optI)
lm_nsecs_per_op = lm_optI;
lm_optB = nsecs_resolution * 100 / lm_nsecs_per_op;
if (lm_optB == 0)
lm_optB = 1;
}
/*
* now that the options are set
*/
if (benchmark_initrun() == -1) {
exit(1);
}
/* allocate dynamic data */
pids = (pid_t *)malloc(lm_optP * sizeof (pid_t));
if (pids == NULL) {
perror("malloc(pids)");
exit(1);
}
tids = (pthread_t *)malloc(lm_optT * sizeof (pthread_t));
if (tids == NULL) {
perror("malloc(tids)");
exit(1);
}
/* check that the case defines lm_tsdsize before proceeding */
if (lm_tsdsize == (size_t)-1) {
(void) fprintf(stderr, "error in benchmark_init: "
"lm_tsdsize not set\n");
exit(1);
}
/* round up tsdsize to nearest 128 to eliminate false sharing */
tsdsize = ((lm_tsdsize + 127) / 128) * 128;
/* allocate sufficient TSD for each thread in each process */
tsdseg = (void *)mmap(NULL, lm_optT * lm_optP * tsdsize + 8192,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0L);
if (tsdseg == NULL) {
perror("mmap(tsd)");
exit(1);
}
/* initialise worker synchronisation */
b = barrier_create(lm_optT * lm_optP, DATASIZE);
if (b == NULL) {
perror("barrier_create()");
exit(1);
}
lm_barrier = b;
b->ba_flag = 1;
/* need this here so that parent and children can call exit() */
(void) fflush(stdout);
(void) fflush(stderr);
/* when we started and when to stop */
b->ba_starttime = getnsecs();
b->ba_deadline = (long long) (b->ba_starttime + (lm_optD * 1000000LL));
/* do the work */
if (lm_opt1) {
/* single process, non-fork mode */
pindex = 0;
worker_process();
} else {
/* create worker processes */
for (i = 0; i < lm_optP; i++) {
pids[i] = fork();
switch (pids[i]) {
case 0:
pindex = i;
worker_process();
exit(0);
break;
case -1:
perror("fork");
exit(1);
break;
default:
continue;
}
}
/* wait for worker processes */
for (i = 0; i < lm_optP; i++) {
if (pids[i] > 0) {
(void) waitpid(pids[i], NULL, 0);
}
}
}
b->ba_endtime = getnsecs();
/* compute results */
compute_stats(b);
/* print arguments benchmark was invoked with ? */
if (lm_optL) {
int l;
(void) printf("# %s ", argv[0]);
for (l = 1; l < argc; l++) {
(void) printf("%s ", argv[l]);
}
(void) printf("\n");
}
/* print result header (unless suppressed) */
if (!lm_optH) {
(void) printf("%12s %3s %3s %12s %12s %8s %8s %s\n",
"", "prc", "thr",
"usecs/call",
"samples", "errors", "cnt/samp", lm_header);
}
/* print result */
(void) printf("%-12s %3d %3d %12.5f %12d %8lld %8d %s\n",
lm_optN, lm_optP, lm_optT,
(lm_optM?b->ba_corrected.st_mean:b->ba_corrected.st_median),
b->ba_batches, b->ba_errors, lm_optB,
benchmark_result());
if (lm_optS) {
print_stats(b);
}
/* just incase something goes awry */
(void) fflush(stdout);
(void) fflush(stderr);
/* cleanup by stages */
(void) benchmark_finirun();
(void) barrier_destroy(b);
(void) benchmark_fini();
if (lm_optE) {
(void) fprintf(stderr, " for %12.5f seconds\n",
(double)(getnsecs() - startnsecs) /
1.e9);
(void) fflush(stderr);
}
return (0);
}
void *
worker_thread(void *arg)
{
result_t r;
long long last_sleep = 0;
long long t;
r.re_errors = benchmark_initworker(arg);
while (lm_barrier->ba_flag) {
r.re_count = 0;
r.re_errors += benchmark_initbatch(arg);
/* sync to clock */
if (lm_optA && ((t = getnsecs()) - last_sleep) > 75000000LL) {
(void) poll(0, 0, 10);
last_sleep = t;
}
/* wait for it ... */
(void) barrier_queue(lm_barrier, NULL);
/* time the test */
r.re_t0 = getnsecs();
(void) benchmark(arg, &r);
r.re_t1 = getnsecs();
/* time to stop? */
if (r.re_t1 > lm_barrier->ba_deadline &&
(!lm_optC || lm_optC < lm_barrier->ba_batches)) {
lm_barrier->ba_flag = 0;
}
/* record results and sync */
(void) barrier_queue(lm_barrier, &r);
(void) benchmark_finibatch(arg);
r.re_errors = 0;
}
(void) benchmark_finiworker(arg);
return (0);
}
void
worker_process()
{
int i;
void *tsd;
for (i = 1; i < lm_optT; i++) {
tsd = gettsd(pindex, i);
if (pthread_create(&tids[i], NULL, worker_thread, tsd) != 0) {
perror("pthread_create");
exit(1);
}
}
tsd = gettsd(pindex, 0);
(void) worker_thread(tsd);
for (i = 1; i < lm_optT; i++) {
(void) pthread_join(tids[i], NULL);
}
}
void
usage()
{
(void) printf(
"usage: %s\n"
" [-1] (single process; overrides -P > 1)\n"
" [-A] (align with clock)\n"
" [-B batch-size (default %d)]\n"
" [-C minimum number of samples (default 0)]\n"
" [-D duration in msecs (default %ds)]\n"
" [-E (echo name to stderr)]\n"
" [-H] (suppress headers)\n"
" [-I] nsecs per op (used to compute batch size)"
" [-L] (print argument line)\n"
" [-M] (reports mean rather than median)\n"
" [-N test-name (default '%s')]\n"
" [-P processes (default %d)]\n"
" [-S] (print detailed stats)\n"
" [-T threads (default %d)]\n"
" [-V] (print the libMicro version and exit)\n"
" [-W] (flag possible benchmark problems)\n"
"%s\n",
lm_procname,
lm_defB, lm_defD, lm_procname, lm_defP, lm_defT,
lm_usage);
}
void
print_warnings(barrier_t *b)
{
int head = 0;
int increase;
if (b->ba_quant) {
if (!head++) {
(void) printf("#\n# WARNINGS\n");
}
increase = (int)(floor((nsecs_resolution * 100.0) /
((double)lm_optB * b->ba_corrected.st_median * 1000.0)) +
1.0);
(void) printf("# Quantization error likely;"
"increase batch size (-B option) %dX to avoid.\n",
increase);
}
/*
* XXX should warn on median != mean by a lot
*/
if (b->ba_errors) {
if (!head++) {
(void) printf("#\n# WARNINGS\n");
}
(void) printf("# Errors occured during benchmark.\n");
}
}
void
print_stats(barrier_t *b)
{
(void) printf("#\n");
(void) printf("# STATISTICS %12s %12s\n",
"usecs/call (raw)",
"usecs/call (outliers removed)");
if (b->ba_count == 0) {
(void) printf("zero samples\n");
return;
}
(void) printf("# min %12.5f %12.5f\n",
b->ba_raw.st_min,
b->ba_corrected.st_min);
(void) printf("# max %12.5f %12.5f\n",
b->ba_raw.st_max,
b->ba_corrected.st_max);
(void) printf("# mean %12.5f %12.5f\n",
b->ba_raw.st_mean,
b->ba_corrected.st_mean);
(void) printf("# median %12.5f %12.5f\n",
b->ba_raw.st_median,
b->ba_corrected.st_median);
(void) printf("# stddev %12.5f %12.5f\n",
b->ba_raw.st_stddev,
b->ba_corrected.st_stddev);
(void) printf("# standard error %12.5f %12.5f\n",
b->ba_raw.st_stderr,
b->ba_corrected.st_stderr);
(void) printf("# 99%% confidence level %12.5f %12.5f\n",
b->ba_raw.st_99confidence,
b->ba_corrected.st_99confidence);
(void) printf("# skew %12.5f %12.5f\n",
b->ba_raw.st_skew,
b->ba_corrected.st_skew);
(void) printf("# kurtosis %12.5f %12.5f\n",
b->ba_raw.st_kurtosis,
b->ba_corrected.st_kurtosis);
(void) printf("# time correlation %12.5f %12.5f\n",
b->ba_raw.st_timecorr,
b->ba_corrected.st_timecorr);
(void) printf("#\n");
(void) printf("# elasped time %12.5f\n", (b->ba_endtime -
b->ba_starttime) / 1.0e9);
(void) printf("# number of samples %12d\n", b->ba_batches);
(void) printf("# number of outliers %12d\n", b->ba_outliers);
(void) printf("# getnsecs overhead %12d\n", (int)nsecs_overhead);
(void) printf("#\n");
(void) printf("# DISTRIBUTION\n");
print_histo(b);
if (lm_optW) {
print_warnings(b);
}
}
void
update_stats(barrier_t *b, result_t *r)
{
double time;
double nsecs_per_call;
if (b->ba_waiters == 0) {
/* first thread only */
b->ba_t0 = r->re_t0;
b->ba_t1 = r->re_t1;
b->ba_count0 = 0;
b->ba_errors0 = 0;
} else {
/* all but first thread */
if (r->re_t0 < b->ba_t0) {
b->ba_t0 = r->re_t0;
}
if (r->re_t1 > b->ba_t1) {
b->ba_t1 = r->re_t1;
}
}
b->ba_count0 += r->re_count;
b->ba_errors0 += r->re_errors;
if (b->ba_waiters == b->ba_hwm - 1) {
/* last thread only */
time = (double)b->ba_t1 - (double)b->ba_t0 -
(double)nsecs_overhead;
if (time < 100 * nsecs_resolution)
b->ba_quant++;
/*
* normalize by procs * threads if not -U
*/
nsecs_per_call = time / (double)b->ba_count0 *
(double)(lm_optT * lm_optP);
b->ba_count += b->ba_count0;
b->ba_errors += b->ba_errors0;
b->ba_data[b->ba_batches % b->ba_datasize] =
nsecs_per_call;
b->ba_batches++;
}
}
#ifdef USE_SEMOP
barrier_t *
barrier_create(int hwm, int datasize)
{
struct sembuf s[1];
barrier_t *b;
/*LINTED*/
b = (barrier_t *)mmap(NULL,
sizeof (barrier_t) + (datasize - 1) * sizeof (double),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0L);
if (b == (barrier_t *)MAP_FAILED) {
return (NULL);
}
b->ba_datasize = datasize;
b->ba_flag = 0;
b->ba_hwm = hwm;
b->ba_semid = semget(IPC_PRIVATE, 3, 0600);
if (b->ba_semid == -1) {
(void) munmap((void *)b, sizeof (barrier_t));
return (NULL);
}
/* [hwm - 1, 0, 0] */
s[0].sem_num = 0;
s[0].sem_op = hwm - 1;
s[0].sem_flg = 0;
if (semop(b->ba_semid, s, 1) == -1) {
perror("semop(1)");
(void) semctl(b->ba_semid, 0, IPC_RMID);
(void) munmap((void *)b, sizeof (barrier_t));
return (NULL);
}
b->ba_waiters = 0;
b->ba_phase = 0;
b->ba_count = 0;
b->ba_errors = 0;
return (b);
}
int
barrier_destroy(barrier_t *b)
{
(void) semctl(b->ba_semid, 0, IPC_RMID);
(void) munmap((void *)b, sizeof (barrier_t));
return (0);
}
int
barrier_queue(barrier_t *b, result_t *r)
{
struct sembuf s[2];
/*
* {s0(-(hwm-1))}
* if ! nowait {s1(-(hwm-1))}
* (all other threads)
* update shared stats
* {s0(hwm-1), s1(1)}
* {s0(1), s2(-1)}
* else
* (last thread)
* update shared stats
* {s2(hwm-1)}
*/
s[0].sem_num = 0;
s[0].sem_op = -(b->ba_hwm - 1);
s[0].sem_flg = 0;
if (semop(b->ba_semid, s, 1) == -1) {
perror("semop(2)");
return (-1);
}
s[0].sem_num = 1;
s[0].sem_op = -(b->ba_hwm - 1);
s[0].sem_flg = IPC_NOWAIT;
if (semop(b->ba_semid, s, 1) == -1) {
if (errno != EAGAIN) {
perror("semop(3)");
return (-1);
}
/* all but the last thread */
if (r != NULL) {
update_stats(b, r);
}
b->ba_waiters++;
s[0].sem_num = 0;
s[0].sem_op = b->ba_hwm - 1;
s[0].sem_flg = 0;
s[1].sem_num = 1;
s[1].sem_op = 1;
s[1].sem_flg = 0;
if (semop(b->ba_semid, s, 2) == -1) {
perror("semop(4)");
return (-1);
}
s[0].sem_num = 0;
s[0].sem_op = 1;
s[0].sem_flg = 0;
s[1].sem_num = 2;
s[1].sem_op = -1;
s[1].sem_flg = 0;
if (semop(b->ba_semid, s, 2) == -1) {
perror("semop(5)");
return (-1);
}
} else {
/* the last thread */
if (r != NULL) {
update_stats(b, r);
}
b->ba_waiters = 0;
b->ba_phase++;
s[0].sem_num = 2;
s[0].sem_op = b->ba_hwm - 1;
s[0].sem_flg = 0;
if (semop(b->ba_semid, s, 1) == -1) {
perror("semop(6)");
return (-1);
}
}
return (0);
}
#else /* USE_SEMOP */
barrier_t *
barrier_create(int hwm, int datasize)
{
pthread_mutexattr_t attr;
pthread_condattr_t cattr;
barrier_t *b;
/*LINTED*/
b = (barrier_t *)mmap(NULL,
sizeof (barrier_t) + (datasize - 1) * sizeof (double),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANON, -1, 0L);
if (b == (barrier_t *)MAP_FAILED) {
return (NULL);
}
b->ba_datasize = datasize;
b->ba_hwm = hwm;
b->ba_flag = 0;
(void) pthread_mutexattr_init(&attr);
(void) pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
(void) pthread_condattr_init(&cattr);
(void) pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
(void) pthread_mutex_init(&b->ba_lock, &attr);
(void) pthread_cond_init(&b->ba_cv, &cattr);
b->ba_waiters = 0;
b->ba_phase = 0;
b->ba_count = 0;
b->ba_errors = 0;
return (b);
}
int
barrier_destroy(barrier_t *b)
{
(void) munmap((void *)b, sizeof (barrier_t));
return (0);
}
int
barrier_queue(barrier_t *b, result_t *r)
{
int phase;
(void) pthread_mutex_lock(&b->ba_lock);
if (r != NULL) {
update_stats(b, r);
}
phase = b->ba_phase;
b->ba_waiters++;
if (b->ba_hwm == b->ba_waiters) {
b->ba_waiters = 0;
b->ba_phase++;
(void) pthread_cond_broadcast(&b->ba_cv);
}
while (b->ba_phase == phase) {
(void) pthread_cond_wait(&b->ba_cv, &b->ba_lock);
}
(void) pthread_mutex_unlock(&b->ba_lock);
return (0);
}
#endif /* USE_SEMOP */
int
gettindex()
{
int i;
if (tids == NULL) {
return (-1);
}
for (i = 1; i < lm_optT; i++) {
if (pthread_self() == tids[i]) {
return (i);
}
}
return (0);
}
int
getpindex()
{
return (pindex);
}
void *
gettsd(int p, int t)
{
if ((p < 0) || (p >= lm_optP) || (t < 0) || (t >= lm_optT))
return (NULL);
return ((void *)((unsigned long)tsdseg +
(((p * lm_optT) + t) * tsdsize)));
}
#ifdef USE_GETHRTIME
long long
getnsecs()
{
return (gethrtime());
}
long long
getusecs()
{
return (gethrtime() / 1000);
}
#elif USE_RDTSC /* USE_GETHRTIME */
__inline__ long long
rdtsc(void)
{
unsigned long long x;
__asm__ volatile(".byte 0x0f, 0x31" : "=A" (x));
return (x);
}
long long
getusecs()
{
return (rdtsc() * 1000000 / lm_hz);
}
long long
getnsecs()
{
return (rdtsc() * 1000000000 / lm_hz);
}
#else /* USE_GETHRTIME */
long long
getusecs()
{
struct timeval tv;
(void) gettimeofday(&tv, NULL);
return ((long long)tv.tv_sec * 1000000LL + (long long) tv.tv_usec);
}
long long
getnsecs()
{
struct timeval tv;