forked from jmrosinski/GPTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gptl.c
3512 lines (3051 loc) · 102 KB
/
gptl.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
/*
** gptl.c
** Author: Jim Rosinski
**
** Main file contains most user-accessible GPTL functions
*/
#ifdef HAVE_MPI
#include <mpi.h>
#endif
#include <stdlib.h> /* malloc */
#include <sys/time.h> /* gettimeofday */
#include <sys/times.h> /* times */
#include <unistd.h> /* gettimeofday, syscall */
#include <stdio.h>
#include <string.h> /* memset, strcmp (via STRMATCH) */
#include <ctype.h> /* isdigit */
#include <sys/types.h> /* u_int8_t, u_int16_t */
#include <assert.h>
#ifdef HAVE_PAPI
#include <papi.h> /* PAPI_get_real_usec */
#endif
#ifdef HAVE_LIBRT
#include <time.h>
#endif
#ifdef _AIX
#include <sys/systemcfg.h>
#endif
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#endif
#include "private.h"
#include "gptl.h"
static Timer **timers = 0; /* linked list of timers */
static Timer **last = 0; /* last element in list */
static int *max_depth; /* maximum indentation level encountered */
static int *max_name_len; /* max length of timer name */
static volatile int nthreads = -1; /* num threads. Init to bad value */
/*
** For THREADED_PTHREADS case, default maxthreads to a big number.
** For THREADED_OMP, the value will be set to $OMP_NUM_THREADS, OR:
** For either case, the user can specify maxthreads with a GPTLsetoption call.
*/
#ifdef THREADED_PTHREADS
#define MAX_THREADS 64
static volatile int maxthreads = MAX_THREADS;
#else
static volatile int maxthreads = -1; /* max threads */
#endif
static int depthlimit = 99999; /* max depth for timers (99999 is effectively infinite) */
static volatile bool disabled = false; /* Timers disabled? */
static volatile bool initialized = false; /* GPTLinitialize has been called */
static volatile bool pr_has_been_called = false; /* GPTLpr_file has been called */
#ifdef HAVE_PAPI
Entry GPTLeventlist[MAX_AUX]; /* list of PAPI-based events to be counted */
int GPTLnevents = 0; /* number of PAPI events (init to 0) */
#endif
static bool dousepapi = false; /* saves a function call if stays false */
static bool verbose = false; /* output verbosity */
static bool percent = false; /* print wallclock also as percent of 1st timers[0] */
static bool dopr_preamble = true; /* whether to print preamble info */
static bool dopr_threadsort = true; /* whether to print sorted thread stats */
static bool dopr_multparent = true; /* whether to print multiple parent info */
static bool dopr_collision = true; /* whether to print hash collision info */
static bool dopr_memusage = false; /* whether to include memusage print when auto-profiling */
static time_t ref_gettimeofday = -1; /* ref start point for gettimeofday */
static time_t ref_clock_gettime = -1; /* ref start point for clock_gettime */
#ifdef _AIX
static time_t ref_read_real_time = -1; /* ref start point for read_real_time */
#endif
static long long ref_papitime = -1; /* ref start point for PAPI_get_real_usec */
#if ( defined THREADED_OMP )
#include <omp.h>
volatile int *GPTLthreadid_omp = 0; /* array of thread ids */
#elif ( defined THREADED_PTHREADS )
#include <pthread.h>
#define MUTEX_API
#ifdef MUTEX_API
static volatile pthread_mutex_t t_mutex;
#else
static volatile pthread_mutex_t t_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
volatile pthread_t *GPTLthreadid = 0; /* array of thread ids */
static int lock_mutex (void); /* lock a mutex for entry into a critical region */
static int unlock_mutex (void); /* unlock a mutex for exit from a critical region */
#else
/* Unthreaded case */
int GPTLthreadid = -1;
#endif
typedef struct {
const Option option; /* wall, cpu, etc. */
const char *str; /* descriptive string for printing */
bool enabled; /* flag */
} Settings;
/* Options, print strings, and default enable flags */
static Settings cpustats = {GPTLcpu, "Usr sys usr+sys ", false};
static Settings wallstats = {GPTLwall, "Wallclock max min ", true };
static Settings overheadstats = {GPTLoverhead, "self_OH parent_OH " , true };
static Hashentry **hashtable; /* table of entries */
static long ticks_per_sec; /* clock ticks per second */
static Timer ***callstack; /* call stack */
static Nofalse *stackidx; /* index into callstack: */
static Method method = GPTLfull_tree; /* default parent/child printing mechanism */
/* Local function prototypes */
static void print_titles (int, FILE *fp);
static void printstats (const Timer *, FILE *, int, int, bool, double, double);
static void add (Timer *, const Timer *);
static void print_multparentinfo (FILE *, Timer *);
static inline int get_cpustamp (long *, long *);
static int newchild (Timer *, Timer *);
static int get_max_depth (const Timer *, const int);
static int is_descendant (const Timer *, const Timer *);
static int is_onlist (const Timer *, const Timer *);
static char *methodstr (Method);
/* Prototypes from previously separate file threadutil.c */
static int threadinit (void); /* initialize threading environment */
static void threadfinalize (void); /* finalize threading environment */
static inline int get_thread_num (void); /* get 0-based thread number */
/* These are the (possibly) supported underlying wallclock timers */
static inline double utr_nanotime (void);
static inline double utr_mpiwtime (void);
static inline double utr_clock_gettime (void);
static inline double utr_papitime (void);
static inline double utr_read_real_time (void);
static inline double utr_gettimeofday (void);
static inline double utr_placebo (void);
static int init_nanotime (void);
static int init_mpiwtime (void);
static int init_clock_gettime (void);
static int init_papitime (void);
static int init_read_real_time (void);
static int init_gettimeofday (void);
static int init_placebo (void);
static inline unsigned int genhashidx (const char *);
static inline Timer *getentry_instr (const Hashentry *, void *, unsigned int *);
static inline Timer *getentry (const Hashentry *, const char *, unsigned int);
static void printself_andchildren (const Timer *, FILE *, int, int, double, double);
static inline int update_parent_info (Timer *, Timer **, int);
static inline int update_stats (Timer *, const double, const long, const long, const int);
static int update_ll_hash (Timer *, int, unsigned int);
static inline int update_ptr (Timer *, const int);
static int construct_tree (Timer *, Method);
static int get_max_depth (const Timer *, const int);
typedef struct {
const Funcoption option;
double (*func)(void);
int (*funcinit)(void);
const char *name;
} Funcentry;
static Funcentry funclist[] = {
{GPTLgettimeofday, utr_gettimeofday, init_gettimeofday, "gettimeofday"},
{GPTLnanotime, utr_nanotime, init_nanotime, "nanotime"},
{GPTLmpiwtime, utr_mpiwtime, init_mpiwtime, "MPI_Wtime"},
{GPTLclockgettime, utr_clock_gettime, init_clock_gettime, "clock_gettime"},
{GPTLpapitime, utr_papitime, init_papitime, "PAPI_get_real_usec"},
{GPTLread_real_time, utr_read_real_time, init_read_real_time,"read_real_time"}, /* AIX only */
{GPTLplacebo, utr_placebo, init_placebo, "placebo"} /* does nothing */
};
static const int nfuncentries = sizeof (funclist) / sizeof (Funcentry);
static double (*ptr2wtimefunc)() = 0; /* init to invalid */
static int funcidx = 0; /* default timer is gettimeofday */
#ifdef HAVE_NANOTIME
static float cpumhz = -1.; /* init to bad value */
static double cyc2sec = -1; /* init to bad value */
static inline long long nanotime (void); /* read counter (assembler) */
static float get_clockfreq (void); /* cycles/sec */
static char *clock_source = "UNKNOWN"; /* where clock found */
#endif
#define DEFAULT_TABLE_SIZE 1023
static int tablesize = DEFAULT_TABLE_SIZE; /* per-thread size of hash table (settable parameter) */
static int tablesizem1 = DEFAULT_TABLE_SIZE - 1;
#define MSGSIZ 256 /* max size of msg printed when dopr_memusage=true */
static int rssmax = 0; /* max rss of the process */
static bool imperfect_nest; /* e.g. start(A),start(B),stop(A) */
/* VERBOSE is a debugging ifdef local to the rest of this file */
#undef VERBOSE
/*
** GPTLsetoption: set option value to true or false.
**
** Input arguments:
** option: option to be set
** val: value to which option should be set (nonzero=true, zero=false)
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLsetoption (const int option, /* option */
const int val) /* value */
{
static const char *thisfunc = "GPTLsetoption";
if (initialized)
return GPTLerror ("%s: must be called BEFORE GPTLinitialize\n", thisfunc);
if (option == GPTLabort_on_error) {
GPTLset_abort_on_error ((bool) val);
if (verbose)
printf ("%s: boolean abort_on_error = %d\n", thisfunc, val);
return 0;
}
switch (option) {
case GPTLcpu:
#ifdef HAVE_TIMES
cpustats.enabled = (bool) val;
if (verbose)
printf ("%s: cpustats = %d\n", thisfunc, val);
#else
if (val)
return GPTLerror ("%s: times() not available\n", thisfunc);
#endif
return 0;
case GPTLwall:
wallstats.enabled = (bool) val;
if (verbose)
printf ("%s: boolean wallstats = %d\n", thisfunc, val);
return 0;
case GPTLoverhead:
overheadstats.enabled = (bool) val;
if (verbose)
printf ("%s: boolean overheadstats = %d\n", thisfunc, val);
return 0;
case GPTLdepthlimit:
depthlimit = val;
if (verbose)
printf ("%s: depthlimit = %d\n", thisfunc, val);
return 0;
case GPTLverbose:
verbose = (bool) val;
#ifdef HAVE_PAPI
(void) GPTL_PAPIsetoption (GPTLverbose, val);
#endif
if (verbose)
printf ("%s: boolean verbose = %d\n", thisfunc, val);
return 0;
case GPTLpercent:
percent = (bool) val;
if (verbose)
printf ("%s: boolean percent = %d\n", thisfunc, val);
return 0;
case GPTLdopr_preamble:
dopr_preamble = (bool) val;
if (verbose)
printf ("%s: boolean dopr_preamble = %d\n", thisfunc, val);
return 0;
case GPTLdopr_threadsort:
dopr_threadsort = (bool) val;
if (verbose)
printf ("%s: boolean dopr_threadsort = %d\n", thisfunc, val);
return 0;
case GPTLdopr_multparent:
dopr_multparent = (bool) val;
if (verbose)
printf ("%s: boolean dopr_multparent = %d\n", thisfunc, val);
return 0;
case GPTLdopr_collision:
dopr_collision = (bool) val;
if (verbose)
printf ("%s: boolean dopr_collision = %d\n", thisfunc, val);
return 0;
case GPTLdopr_memusage:
dopr_memusage = (bool) val;
if (verbose)
printf ("%s: boolean dopr_memusage = %d\n", thisfunc, val);
return 0;
case GPTLprint_method:
method = (Method) val;
if (verbose)
printf ("%s: print_method = %s\n", thisfunc, methodstr (method));
return 0;
case GPTLtablesize:
if (val < 1)
return GPTLerror ("%s: tablesize must be positive. %d is invalid\n", thisfunc, val);
tablesize = val;
tablesizem1 = val - 1;
if (verbose)
printf ("%s: tablesize = %d\n", thisfunc, tablesize);
return 0;
case GPTLsync_mpi:
#ifdef ENABLE_PMPI
if (GPTLpmpi_setoption (option, val) != 0)
fprintf (stderr, "%s: GPTLpmpi_setoption failure\n", thisfunc);
#endif
if (verbose)
printf ("%s: boolean sync_mpi = %d\n", thisfunc, val);
return 0;
case GPTLmaxthreads:
if (val < 1)
return GPTLerror ("%s: maxthreads must be positive. %d is invalid\n", thisfunc, val);
maxthreads = val;
return 0;
case GPTLmultiplex:
/* Allow GPTLmultiplex to fall through because it will be handled by GPTL_PAPIsetoption() */
default:
break;
}
#ifdef HAVE_PAPI
if (GPTL_PAPIsetoption (option, val) == 0) {
if (val)
dousepapi = true;
return 0;
}
#else
/* Make GPTLnarrowprint a placebo if PAPI not enabled */
if (option == GPTLnarrowprint)
return 0;
#endif
return GPTLerror ("%s: failure to enable option %d\n", thisfunc, option);
}
/*
** GPTLsetutr: set underlying timing routine.
**
** Input arguments:
** option: index which sets function
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLsetutr (const int option)
{
int i; /* index over number of underlying timer */
static const char *thisfunc = "GPTLsetutr";
if (initialized)
return GPTLerror ("%s: must be called BEFORE GPTLinitialize\n", thisfunc);
for (i = 0; i < nfuncentries; i++) {
if (option == (int) funclist[i].option) {
if (verbose)
printf ("%s: underlying wallclock timer = %s\n", thisfunc, funclist[i].name);
funcidx = i;
/*
** Return an error condition if the function is not available.
** OK for the user code to ignore: GPTLinitialize() will reset to gettimeofday
*/
if ((*funclist[i].funcinit)() < 0)
return GPTLerror ("%s: utr=%s not available or doesn't work\n", thisfunc, funclist[i].name);
else
return 0;
}
}
return GPTLerror ("%s: unknown option %d\n", thisfunc, option);
}
/*
** GPTLinitialize (): Initialization routine must be called from single-threaded
** region before any other timing routines may be called. The need for this
** routine could be eliminated if not targetting timing library for threaded
** capability.
**
** return value: 0 (success) or GPTLerror (failure)
*/
int GPTLinitialize (void)
{
int i; /* loop index */
int t; /* thread index */
double t1, t2; /* returned from underlying timer */
static const char *thisfunc = "GPTLinitialize";
if (initialized)
return GPTLerror ("%s: has already been called\n", thisfunc);
if (threadinit () < 0)
return GPTLerror ("%s: bad return from threadinit\n", thisfunc);
if ((ticks_per_sec = sysconf (_SC_CLK_TCK)) == -1)
return GPTLerror ("%s: failure from sysconf (_SC_CLK_TCK)\n", thisfunc);
/* Allocate space for global arrays */
callstack = (Timer ***) GPTLallocate (maxthreads * sizeof (Timer **), thisfunc);
stackidx = (Nofalse *) GPTLallocate (maxthreads * sizeof (Nofalse), thisfunc);
timers = (Timer **) GPTLallocate (maxthreads * sizeof (Timer *), thisfunc);
last = (Timer **) GPTLallocate (maxthreads * sizeof (Timer *), thisfunc);
max_depth = (int *) GPTLallocate (maxthreads * sizeof (int), thisfunc);
max_name_len = (int *) GPTLallocate (maxthreads * sizeof (int), thisfunc);
hashtable = (Hashentry **) GPTLallocate (maxthreads * sizeof (Hashentry *), thisfunc);
/* Initialize array values */
for (t = 0; t < maxthreads; t++) {
max_depth[t] = -1;
max_name_len[t] = 0;
callstack[t] = (Timer **) GPTLallocate (MAX_STACK * sizeof (Timer *), thisfunc);
hashtable[t] = (Hashentry *) GPTLallocate (tablesize * sizeof (Hashentry), thisfunc);
for (i = 0; i < tablesize; i++) {
hashtable[t][i].nument = 0;
hashtable[t][i].entries = 0;
}
/* Make a timer "GPTL_ROOT" to ensure no orphans, and to simplify printing. */
timers[t] = (Timer *) GPTLallocate (sizeof (Timer), thisfunc);
memset (timers[t], 0, sizeof (Timer));
strcpy (timers[t]->name, "GPTL_ROOT");
timers[t]->onflg = true;
last[t] = timers[t];
stackidx[t].val = 0;
callstack[t][0] = timers[t];
for (i = 1; i < MAX_STACK; i++)
callstack[t][i] = 0;
}
#ifdef HAVE_PAPI
if (GPTL_PAPIinitialize (maxthreads, verbose, &GPTLnevents, GPTLeventlist) < 0)
return GPTLerror ("%s: Failure from GPTL_PAPIinitialize\n", thisfunc);
#endif
/* Call init routine for underlying timing routine. */
if ((*funclist[funcidx].funcinit)() < 0) {
fprintf (stderr, "%s: Failure initializing %s. Reverting underlying timer to %s\n",
thisfunc, funclist[funcidx].name, funclist[0].name);
funcidx = 0;
}
ptr2wtimefunc = funclist[funcidx].func;
if (verbose) {
t1 = (*ptr2wtimefunc) ();
t2 = (*ptr2wtimefunc) ();
if (t1 > t2)
fprintf (stderr, "%s: negative delta-t=%g\n", thisfunc, t2-t1);
printf ("Per call overhead est. t2-t1=%g should be near zero\n", t2-t1);
printf ("Underlying wallclock timing routine is %s\n", funclist[funcidx].name);
}
imperfect_nest = false;
initialized = true;
return 0;
}
/*
** GPTLfinalize (): Finalization routine must be called from single-threaded
** region. Free all malloc'd space
**
** return value: 0 (success) or GPTLerror (failure)
*/
int GPTLfinalize (void)
{
int t; /* thread index */
int n; /* array index */
Timer *ptr, *ptrnext; /* ll indices */
static const char *thisfunc = "GPTLfinalize";
if ( ! initialized)
return GPTLerror ("%s: initialization was not completed\n", thisfunc);
for (t = 0; t < maxthreads; ++t) {
for (n = 0; n < tablesize; ++n) {
if (hashtable[t][n].nument > 0)
free (hashtable[t][n].entries);
}
free (hashtable[t]);
hashtable[t] = NULL;
free (callstack[t]);
for (ptr = timers[t]; ptr; ptr = ptrnext) {
ptrnext = ptr->next;
if (ptr->nparent > 0) {
free (ptr->parent);
free (ptr->parent_count);
}
if (ptr->nchildren > 0)
free (ptr->children);
free (ptr);
}
}
free (callstack);
free (stackidx);
free (timers);
free (last);
free (max_depth);
free (max_name_len);
free (hashtable);
threadfinalize ();
GPTLreset_errors ();
#ifdef HAVE_PAPI
GPTL_PAPIfinalize (maxthreads);
#endif
/* Reset initial values */
timers = 0;
last = 0;
max_depth = 0;
max_name_len = 0;
nthreads = -1;
#ifdef THREADED_PTHREADS
maxthreads = MAX_THREADS;
#else
maxthreads = -1;
#endif
depthlimit = 99999;
disabled = false;
initialized = false;
pr_has_been_called = false;
dousepapi = false;
verbose = false;
percent = false;
dopr_preamble = true;
dopr_threadsort = true;
dopr_multparent = true;
dopr_collision = true;
ref_gettimeofday = -1;
ref_clock_gettime = -1;
#ifdef _AIX
ref_read_real_time = -1;
#endif
ref_papitime = -1;
funcidx = 0;
#ifdef HAVE_NANOTIME
cpumhz= 0;
cyc2sec = -1;
#endif
tablesize = DEFAULT_TABLE_SIZE;
tablesizem1 = tablesize - 1;
return 0;
}
/*
** GPTLstart_instr: start a timer (auto-instrumented)
**
** Input arguments:
** self: function address
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLstart_instr (void *self)
{
Timer *ptr; /* linked list pointer */
int t; /* thread index (of this thread) */
unsigned int indx; /* hash table index */
static const char *thisfunc = "GPTLstart_instr";
if (disabled)
return 0;
if ( ! initialized)
return GPTLerror ("%s self=%p: GPTLinitialize has not been called\n", thisfunc, self);
if ((t = get_thread_num ()) < 0)
return GPTLerror ("%s: bad return from get_thread_num\n", thisfunc);
/* If current depth exceeds a user-specified limit for print, just increment and return */
if (stackidx[t].val >= depthlimit) {
++stackidx[t].val;
return 0;
}
ptr = getentry_instr (hashtable[t], self, &indx);
/*
** Recursion => increment depth in recursion and return. We need to return
** because we don't want to restart the timer. We want the reported time for
** the timer to reflect the outermost layer of recursion.
*/
if (ptr && ptr->onflg) {
++ptr->recurselvl;
return 0;
}
/*
** Increment stackidx[t] unconditionally. This is necessary to ensure the correct
** behavior when GPTLstop_instr decrements stackidx[t] unconditionally.
*/
if (++stackidx[t].val > MAX_STACK-1)
return GPTLerror ("%s: stack too big\n", thisfunc);
if ( ! ptr) { /* Add a new entry and initialize */
ptr = (Timer *) GPTLallocate (sizeof (Timer), thisfunc);
memset (ptr, 0, sizeof (Timer));
/*
** Need to save the address string for later conversion back to a real
** name by an offline tool.
*/
snprintf (ptr->name, MAX_CHARS+1, "%lx", (unsigned long) self);
ptr->address = self;
if (update_ll_hash (ptr, t, indx) != 0)
return GPTLerror ("%s: update_ll_hash error\n", thisfunc);
}
if (update_parent_info (ptr, callstack[t], stackidx[t].val) != 0)
return GPTLerror ("%s: update_parent_info error\n", thisfunc);
if (update_ptr (ptr, t) != 0)
return GPTLerror ("%s: update_ptr error\n", thisfunc);
return (0);
}
/*
** GPTLstart: start a timer
**
** Input arguments:
** name: timer name
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLstart (const char *name) /* timer name */
{
Timer *ptr; /* linked list pointer */
int t; /* thread index (of this thread) */
int numchars; /* number of characters to copy */
unsigned int indx; /* hash table index */
static const char *thisfunc = "GPTLstart";
if (disabled)
return 0;
if ( ! initialized)
return GPTLerror ("%s name=%s: GPTLinitialize has not been called\n", thisfunc, name);
if ((t = get_thread_num ()) < 0)
return GPTLerror ("%s: bad return from get_thread_num\n", thisfunc);
/*
** If current depth exceeds a user-specified limit for print, just
** increment and return
*/
if (stackidx[t].val >= depthlimit) {
++stackidx[t].val;
return 0;
}
/* ptr will point to the requested timer in the current list, or NULL if this is a new entry */
indx = genhashidx (name);
ptr = getentry (hashtable[t], name, indx);
/*
** Recursion => increment depth in recursion and return. We need to return
** because we don't want to restart the timer. We want the reported time for
** the timer to reflect the outermost layer of recursion.
*/
if (ptr && ptr->onflg) {
++ptr->recurselvl;
return 0;
}
/*
** Increment stackidx[t] unconditionally. This is necessary to ensure the correct
** behavior when GPTLstop decrements stackidx[t] unconditionally.
*/
if (++stackidx[t].val > MAX_STACK-1)
return GPTLerror ("%s: stack too big\n", thisfunc);
if ( ! ptr) { /* Add a new entry and initialize */
ptr = (Timer *) GPTLallocate (sizeof (Timer), thisfunc);
memset (ptr, 0, sizeof (Timer));
numchars = MIN (strlen (name), MAX_CHARS);
strncpy (ptr->name, name, numchars);
ptr->name[numchars] = '\0';
if (update_ll_hash (ptr, t, indx) != 0)
return GPTLerror ("%s: update_ll_hash error\n", thisfunc);
}
if (update_parent_info (ptr, callstack[t], stackidx[t].val) != 0)
return GPTLerror ("%s: update_parent_info error\n", thisfunc);
if (update_ptr (ptr, t) != 0)
return GPTLerror ("%s: update_ptr error\n", thisfunc);
return (0);
}
/*
** GPTLinit_handle: Initialize a handle for further use by GPTLstart_handle() and GPTLstop_handle()
**
** Input arguments:
** name: timer name
**
** Output arguments:
** handle: hash value corresponding to "name"
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLinit_handle (const char *name, /* timer name */
int *handle) /* handle (output if input value is zero) */
{
if (disabled)
return 0;
*handle = (int) genhashidx (name);
return 0;
}
/*
** GPTLstart_handle: start a timer based on a handle
**
** Input arguments:
** name: timer name (required when on input, handle=0)
** handle: pointer to timer matching "name"
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLstart_handle (const char *name, /* timer name */
int *handle) /* handle (output if input value is zero) */
{
Timer *ptr; /* linked list pointer */
int t; /* thread index (of this thread) */
int numchars; /* number of characters to copy */
static const char *thisfunc = "GPTLstart_handle";
if (disabled)
return 0;
if ( ! initialized)
return GPTLerror ("%s name=%s: GPTLinitialize has not been called\n", thisfunc, name);
if ((t = get_thread_num ()) < 0)
return GPTLerror ("%s: bad return from get_thread_num\n", thisfunc);
/* If current depth exceeds a user-specified limit for print, just increment and return */
if (stackidx[t].val >= depthlimit) {
++stackidx[t].val;
return 0;
}
/*
** If handle is zero on input, generate the hash entry and return it to the user.
** Otherwise assume it's a previously generated hash index passed in by the user.
** Don't need a critical section here--worst case multiple threads will generate the
** same handle and store to the same memory location, and this will only happen once.
*/
if (*handle == 0) {
*handle = (int) genhashidx (name);
#ifdef VERBOSE
printf ("%s: name=%s thread %d generated handle=%d\n", thisfunc, name, t, *handle);
#endif
} else if ((unsigned int) *handle > tablesizem1) {
return GPTLerror ("%s: Bad input handle=%u exceeds tablesizem1=%d\n",
thisfunc, (unsigned int) *handle, tablesizem1);
}
ptr = getentry (hashtable[t], name, (unsigned int) *handle);
/*
** Recursion => increment depth in recursion and return. We need to return
** because we don't want to restart the timer. We want the reported time for
** the timer to reflect the outermost layer of recursion.
*/
if (ptr && ptr->onflg) {
++ptr->recurselvl;
return 0;
}
/*
** Increment stackidx[t] unconditionally. This is necessary to ensure the correct
** behavior when GPTLstop decrements stackidx[t] unconditionally.
*/
if (++stackidx[t].val > MAX_STACK-1)
return GPTLerror ("%s: stack too big\n", thisfunc);
if ( ! ptr) { /* Add a new entry and initialize */
ptr = (Timer *) GPTLallocate (sizeof (Timer), thisfunc);
memset (ptr, 0, sizeof (Timer));
numchars = MIN (strlen (name), MAX_CHARS);
strncpy (ptr->name, name, numchars);
ptr->name[numchars] = '\0';
if (update_ll_hash (ptr, t, (unsigned int) *handle) != 0)
return GPTLerror ("%s: update_ll_hash error\n", thisfunc);
}
if (update_parent_info (ptr, callstack[t], stackidx[t].val) != 0)
return GPTLerror ("%s: update_parent_info error\n", thisfunc);
if (update_ptr (ptr, t) != 0)
return GPTLerror ("%s: update_ptr error\n", thisfunc);
return (0);
}
/*
** update_ll_hash: Update linked list and hash table.
** Called by all GPTLstart* routines when there is a new entry
**
** Input arguments:
** ptr: pointer to timer
** t: thread index
** indx: hash index
**
** Return value: 0 (success) or GPTLerror (failure)
*/
static int update_ll_hash (Timer *ptr, int t, unsigned int indx)
{
int nchars; /* number of chars */
int nument; /* number of entries */
Timer **eptr; /* for realloc */
nchars = strlen (ptr->name);
if (nchars > max_name_len[t])
max_name_len[t] = nchars;
last[t]->next = ptr;
last[t] = ptr;
++hashtable[t][indx].nument;
nument = hashtable[t][indx].nument;
eptr = (Timer **) realloc (hashtable[t][indx].entries, nument * sizeof (Timer *));
if ( ! eptr)
return GPTLerror ("update_ll_hash: realloc error\n");
hashtable[t][indx].entries = eptr;
hashtable[t][indx].entries[nument-1] = ptr;
return 0;
}
/*
** update_ptr: Update timer contents. Called by GPTLstart, GPTLstart_instr and GPTLstart_handle
**
** Input arguments:
** ptr: pointer to timer
** t: thread index
**
** Return value: 0 (success) or GPTLerror (failure)
*/
static inline int update_ptr (Timer *ptr, const int t)
{
double tp2; /* time stamp */
ptr->onflg = true;
if (cpustats.enabled && get_cpustamp (&ptr->cpu.last_utime, &ptr->cpu.last_stime) < 0)
return GPTLerror ("update_ptr: get_cpustamp error");
if (wallstats.enabled) {
tp2 = (*ptr2wtimefunc) ();
ptr->wall.last = tp2;
}
#ifdef HAVE_PAPI
if (dousepapi && GPTL_PAPIstart (t, &ptr->aux) < 0)
return GPTLerror ("update_ptr: error from GPTL_PAPIstart\n");
#endif
return 0;
}
/*
** update_parent_info: update info about parent, and in the parent about this child
** Called by all GPTLstart* routines
**
** Arguments:
** ptr: pointer to timer
** callstackt: callstack for this thread
** stackidxt: stack index for this thread
**
** Return value: 0 (success) or GPTLerror (failure)
*/
static inline int update_parent_info (Timer *ptr,
Timer **callstackt,
int stackidxt)
{
int n; /* loop index through known parents */
Timer *pptr; /* pointer to parent in callstack */
Timer **pptrtmp; /* for realloc parent pointer array */
int nparent; /* number of parents */
int *parent_count; /* number of times parent invoked this child */
static const char *thisfunc = "update_parent_info";
if ( ! ptr )
return -1;
if (stackidxt < 0)
return GPTLerror ("%s: called with negative stackidx\n", thisfunc);
callstackt[stackidxt] = ptr;
/* Bump orphan count if the region has no parent (should never happen since "GPTL_ROOT" added) */
if (stackidxt == 0) {
++ptr->norphan;
return 0;
}
pptr = callstackt[stackidxt-1];
/* If this parent occurred before, bump its count */
for (n = 0; n < ptr->nparent; ++n) {
if (ptr->parent[n] == pptr) {
++ptr->parent_count[n];
break;
}
}
/* If this is a new parent, update info */
if (n == ptr->nparent) {
++ptr->nparent;
nparent = ptr->nparent;
pptrtmp = (Timer **) realloc (ptr->parent, nparent * sizeof (Timer *));
if ( ! pptrtmp)
return GPTLerror ("%s: realloc error pptrtmp nparent=%d\n", thisfunc, nparent);
ptr->parent = pptrtmp;
ptr->parent[nparent-1] = pptr;
parent_count = (int *) realloc (ptr->parent_count, nparent * sizeof (int));
if ( ! parent_count)
return GPTLerror ("%s: realloc error parent_count nparent=%d\n", thisfunc, nparent);
ptr->parent_count = parent_count;
ptr->parent_count[nparent-1] = 1;
}
return 0;
}
/*
** GPTLstop_instr: stop a timer (auto-instrumented)
**
** Input arguments:
** self: function address
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLstop_instr (void *self)
{
double tp1 = 0.0; /* time stamp */
Timer *ptr; /* linked list pointer */
int t; /* thread number for this process */
unsigned int indx; /* index into hash table */
long usr = 0; /* user time (returned from get_cpustamp) */
long sys = 0; /* system time (returned from get_cpustamp) */
static const char *thisfunc = "GPTLstop_instr";
if (disabled)
return 0;
if ( ! initialized)
return GPTLerror ("%s: GPTLinitialize has not been called\n", thisfunc);
/* Get the timestamp */
if (wallstats.enabled) {
tp1 = (*ptr2wtimefunc) ();
}
if (cpustats.enabled && get_cpustamp (&usr, &sys) < 0)
return GPTLerror ("%s: bad return from get_cpustamp\n", thisfunc);
if ((t = get_thread_num ()) < 0)
return GPTLerror ("%s: bad return from get_thread_num\n", thisfunc);
/* If current depth exceeds a user-specified limit for print, just decrement and return */
if (stackidx[t].val > depthlimit) {
--stackidx[t].val;
return 0;
}
ptr = getentry_instr (hashtable[t], self, &indx);
if ( ! ptr)
return GPTLerror ("%s: timer for %p had not been started.\n", thisfunc, self);
if ( ! ptr->onflg )
return GPTLerror ("%s: timer %s was already off.\n", thisfunc, ptr->name);