-
Notifications
You must be signed in to change notification settings - Fork 5
/
runtimelib.c
1940 lines (1679 loc) · 66.4 KB
/
runtimelib.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
/* ----------------------------------------------------------------------
*
* coNCePTuaL run-time library:
* core operations
*
* By Scott Pakin <[email protected]>
*
* ----------------------------------------------------------------------
*
*
* Copyright (C) 2003, Triad National Security, LLC
* All rights reserved.
*
* Copyright (2003). Triad National Security, LLC. This software
* was produced under U.S. Government contract 89233218CNA000001 for
* Los Alamos National Laboratory (LANL), which is operated by Los
* Alamos National Security, LLC (Triad) for the U.S. Department
* of Energy. The U.S. Government has rights to use, reproduce,
* and distribute this software. NEITHER THE GOVERNMENT NOR TRIAD
* MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY
* FOR THE USE OF THIS SOFTWARE. If software is modified to produce
* derivative works, such modified software should be clearly marked,
* so as not to confuse it with the version available from LANL.
*
* Additionally, redistribution and use in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Triad National Security, LLC, Los Alamos
* National Laboratory, the U.S. Government, nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TRIAD AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TRIAD OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ----------------------------------------------------------------------
*/
#include "runtimelib.h"
/**********
* Macros *
**********/
/* ASCI Red lacks a getppid() function. */
#ifndef HAVE_GETPPID
# define getppid() (-1)
#endif
/* Define a macro to abort the program with an internal error. */
#define NCPTL_FATAL_INTERNAL() \
do { \
ncptl_fatal ("Internal error in %s, line %d", __FILE__, __LINE__); \
} while (0)
/* MinGW (on Microsoft Windows) lacks a kill() function. */
#ifndef HAVE_KILL
# define kill(PID,SIG)
#endif
/* Define a sleep() function if we don't have sleep() itself. */
#ifndef HAVE_SLEEP
# ifdef _WIN32
# define sleep(S) Sleep(1000*(S))
# define HAVE_SLEEP
# else
# define sleep(S)
# endif
#endif
/*********************
* Type declarations *
*********************/
/* Define a key:value type that matches what ncptl_sig2num() returns. */
typedef struct {
char *name;
int number;
} NAMENUMBER;
/************************************
* Imported variables and functions *
************************************/
extern int ncptl_unsync_rand_state_seeded;
extern void ncptl_log_shutdown (const char *, va_list);
extern void ncptl_init_genrand(RNG_STATE *, uint64_t);
extern uint32_t ncptl_genrand_int32(RNG_STATE *);
extern int64_t ncptl_genrand_int63(RNG_STATE *);
extern uint64_t ncptl_genrand_int64(RNG_STATE *);
extern const NAMENUMBER *ncptl_sig2num (const char *, unsigned int);
extern void ncptl_log_add_comment (const char *, const char *);
extern void ncptl_discern_system_information (SYSTEM_INFORMATION *);
#ifdef HAVE_DECL_STRSIGNAL
# if HAVE_DECL_STRSIGNAL == 0
/* I've used an ecc installation that fails to declare strsignal(). */
extern char *strsignal (int);
# endif
#endif
/*******************************
* Internal variable (needs to *
* appear early in this file) *
*******************************/
/* Dummy variable to prevent whiny C compilers from complaining about
* unused function parameters. */
static volatile int dummyvar;
/******************************************
* Library-global variables and functions *
******************************************/
/* Name of the executable file and program arguments (set if and when
* ncptl_parse_command_line() is called) */
char *ncptl_progname = "coNCePTuaL";
int ncptl_argc_copy = 0;
char **ncptl_argv_copy = NULL;
/* Information about the hardware and operating system */
SYSTEM_INFORMATION systeminfo;
/* # of timebase (and usually CPU) cycles per microsecond */
#ifdef HAVE_CYCLES_PER_USEC
uint64_t ncptl_cycles_per_usec = 1; /* Until ncptl_init() is called, pretend we have a really, really slow clock (1 MHz). */
# ifdef _WIN32
uint64_t ncptl_cycles_per_sec = 1; /* Microsoft Windows has only low precision timers. */
# endif
#endif
/* Various HPET-related values */
#ifdef USE_HPET
volatile uint64_t *ncptl_hpet_timer = NULL; /* Pointer to the main HPET counter */
uint64_t ncptl_hpet_period_fs = 0; /* HPET period in femptoseconds */
int ncptl_hpet_works = 0; /* 1=HPET works; 0=it doesn't */
#endif
/* Mean # of microseconds of overhead involved in invoking ncptl_time() */
uint64_t ncptl_time_overhead = (uint64_t)(~0);
/* Difference between successive readings of ncptl_time() */
double ncptl_time_delta_mean = 0.0;
double ncptl_time_delta_stddev = 0.0;
/* Actual delay incurred when sleeping using ncptl_udelay() */
double ncptl_sleep_mean = 0.0;
double ncptl_sleep_stddev = 0.0;
/* Difference between successive readings of ncptl_process_time() */
double ncptl_proc_time_delta_mean = 0.0;
double ncptl_proc_time_delta_stddev = 0.0;
/* Seed for the random-number generator */
int ncptl_rng_seed = 0;
/* The process's physical ID */
ncptl_int ncptl_self_proc;
/* List of signals the user doesn't want us to trap */
int ncptl_no_trap_signal[NUM_SIGNALS];
/* Some systems that have a working fork() function that happens to be
* incompatible with a particular communication library. We therefore
* enable the user to set the NCPTL_NOFORK environment variable to
* inhibit all fork()-calling functions (fork(), system(), popen(),
* etc.). */
#ifdef HAVE_WORKING_FORK
int ncptl_fork_works = 1;
#else
int ncptl_fork_works = 0;
#endif
/* Specify the log-file checkpoint interval in microseconds. */
uint64_t ncptl_log_checkpoint_interval = (uint64_t) 60000000;
/* Install a signal handler and store the value of the previous signal
* handler. Errors can be ignored or cause the application to
* abort. */
void ncptl_install_signal_handler (int signalnum,
SIGHANDLER newhandler,
SIGHANDLER *oldhandler,
int abort_on_failure)
{
int success; /* 1=success; 0=failure */
SIGHANDLER prevhandler; /* Value to assign to oldhandler */
/* Install the signal handler. */
#ifdef HAVE_SIGACTION
struct sigaction newhandlerinfo;
struct sigaction oldhandlerinfo;
newhandlerinfo.sa_handler = newhandler;
sigemptyset (&newhandlerinfo.sa_mask);
newhandlerinfo.sa_flags = 0;
success = (sigaction(signalnum, &newhandlerinfo, &oldhandlerinfo) != -1);
prevhandler = oldhandlerinfo.sa_handler;
#else
prevhandler = signal (signalnum, newhandler);
success = (prevhandler != SIG_ERR);
#endif
/* Optionally store the old signal handler and optionally abort the
* program on failure. */
if (success) {
if (oldhandler)
*oldhandler = prevhandler;
}
else
if (abort_on_failure) {
#ifdef HAVE_STRSIGNAL
ncptl_fatal ("Failed to register a handler for signal %d (%s)",
signalnum, strsignal(signalnum));
#else
ncptl_fatal ("Failed to register a handler for signal %d",
signalnum);
#endif
}
}
#ifdef HAVE_GETRUSAGE
/* Return the process time (user or OS) in microseconds. */
uint64_t ncptl_process_time (int user0sys1)
{
struct rusage usageinfo;
if (getrusage (RUSAGE_SELF, &usageinfo) == -1)
NCPTL_SYSTEM_ERROR ("getrusage() failed");
if (user0sys1 == 0)
return usageinfo.ru_utime.tv_sec*INT64_C(1000000) + usageinfo.ru_utime.tv_usec;
if (user0sys1 == 1)
return usageinfo.ru_stime.tv_sec*INT64_C(1000000) + usageinfo.ru_stime.tv_usec;
NCPTL_FATAL_INTERNAL();
return (uint32_t)(-1); /* Appease idiotic compilers. */
}
/* Return the number of major and minor page faults. */
void ncptl_page_fault_count (uint64_t *major, uint64_t *minor)
{
struct rusage usageinfo;
if (getrusage (RUSAGE_SELF, &usageinfo) == -1)
NCPTL_SYSTEM_ERROR ("getrusage() failed");
*major = (uint64_t) usageinfo.ru_majflt;
*minor = (uint64_t) usageinfo.ru_minflt;
}
#endif
/* Return the total number of interrupts received by the node since
* boot time or -1 if unknown. */
uint64_t ncptl_interrupt_count (void)
{
uint64_t numints = (uint64_t)(~0); /* Total number of interrupts seen on all CPUs */
#if defined(HAVE_KSTAT_DATA_LOOKUP)
/* Use the kstat interface to tally the number of interrupts. */
kstat_ctl_t *kcontrol; /* kstat control */
kstat_t *thekstat; /* The kstat itself */
kstat_named_t *kstatdata; /* Value encountered */
/* Open the kernel statistics. */
if (!(kcontrol=kstat_open()))
return numints;
/* Iterate over all kstats until we find all cpu/intrstat values. */
numints = 0;
for (thekstat=kstat_lookup(kcontrol, "cpu", -1, "intrstat");
thekstat;
thekstat=(kstat_t *)thekstat->ks_next) {
int level; /* Interrupt level number */
/* Find a cpu/intrstat kstat. */
if (kstat_read(kcontrol, thekstat, NULL) == -1)
/* We're doomed if we can't read the kstat. */
return (uint64_t)(~0);
if (strcmp(thekstat->ks_module, "cpu") || strcmp(thekstat->ks_name, "intrstat"))
/* This isn't a kstat we care about -- continue with the next one. */
continue;
/* Search for interrupt levels 0..255 and store each interrupt's tally. */
for (level=0; level<256; level++) {
char levelname[15]; /* "level-" followed by the level number */
sprintf (levelname, "level-%d", level);
if ((kstatdata=(kstat_named_t *)kstat_data_lookup (thekstat, levelname))
&& kstatdata->data_type == KSTAT_DATA_UINT64)
numints += kstatdata->value.ui64;
}
}
/* Close the kernel statistics and return. */
(void) kstat_close (kcontrol);
return numints;
#elif defined(USE_PROC_INTERRUPTS)
/* Attempt to parse the contents of the /proc/interrupts file. */
# define MAX_INTR_LINE_LEN 1048576
FILE *intfile; /* Handle to /proc/interrupts */
char *oneline = (char *) ncptl_malloc(MAX_INTR_LINE_LEN, 0); /* One line of interrupt counts */
/* Open the file and discard the header line. */
if (!(intfile=fopen("/proc/interrupts", "r")))
return numints;
if (!fgets(oneline, MAX_INTR_LINE_LEN, intfile)) {
fclose (intfile);
return numints;
}
/* Read the remaining lines one-by-one. Each line consists of an
* interrupt number, a tally for each CPU, and a description of the
* interrupt type and corresponding device driver:
*
* 50: 0 1430 IO-SAPIC-level usb-ohci
* 53: 843677863 0 IO-SAPIC-level eth0
* 54: 0 43858189 IO-SAPIC-level ioc0
*
* Note that the following code will be confused if the description
* column begins with a number.
*/
numints = 0;
while (fgets(oneline, MAX_INTR_LINE_LEN, intfile)) {
char *word; /* The current word on the current line */
for (word=strtok(oneline, " "); word; word=strtok(NULL, " "))
if (isdigit(word[0])) {
uint64_t wordints = (uint64_t) strtoll (word, NULL, 10);
if (errno == ERANGE)
break;
numints += wordints;
}
else
break;
}
/* Return the tally. */
fclose (intfile);
ncptl_free (oneline);
return numints;
# undef MAX_INTR_LINE_LEN
#else
/* We don't know how to tally interrupts on this platform. */
return numints;
#endif
}
/* Return the time of day in seconds. */
unsigned long ncptl_time_of_day (void)
{
#if defined(HAVE_TIME)
return (unsigned long) time(NULL);
#elif defined(HAVE_GETTIMEOFDAY)
{
struct timeval now;
int result;
if ((result=gettimeofday(&now, NULL)) == -1)
ncptl_fatal ("gettimeofday() failed with error code %d", result);
return now.tv_sec + 1000000*tv_usec;
}
#else
# error Unable to read the time of day.
#endif
}
/* Parse an environment variable, ENVVAR, as an unsigned 64-bit number
* and assign the value to VALUE. Return 1 on success (which includes
* an unset environment variable) and 0 on failure (viz., a parse
* error). VALUE is modified only if ENVVAR is defined and parses
* correctly. */
int ncptl_envvar_to_uint64 (const char *envvar, uint64_t *value)
{
char *envstring = getenv(envvar); /* Value of ENVVAR as a string */
char *firstbad; /* Pointer to first non-digit */
uint64_t envvalue; /* Numerical value of ENVVAR */
/* An undefined variable is treated as a success but VALUE is left
* untouched. */
if (!envstring)
return 1;
/* Reject negative numbers even if strtoull() doesn't. */
if (envstring[0] == '-')
return 0;
/* Parse the environment variable using strtoull(). */
errno = 0; /* BSD's strtoull() doesn't reset errno on success. */
envvalue = strtoull (envstring, &firstbad, 10);
if (errno || firstbad==envstring || *firstbad)
return 0; /* Parse error */
*value = envvalue;
return 1;
}
/* Wrap a non-inlined ncptl_time_no_hpet() function around the inlined
* version to ensure that ncptl_time_no_hpet() can be called
* externally. */
static uint64_t inlined_time_no_hpet (void);
uint64_t ncptl_time_no_hpet (void)
{
return inlined_time_no_hpet();
}
/************************************
* Internal variables and functions *
************************************/
/* Pointer to a variable to set upon a SIGALRM */
static volatile int *flag_to_set = NULL;
/* Min. # of iterations of a tight loop we can perform in one microsecond */
static uint64_t spinsperusec = 0;
/* ncptl_udelay() polls cycle counters but not timers that require OS
* intervention because that might introduce excessive load on the
* system. We assume that if ncptl_time_overhead is less than one
* microsecond then we must have a cycle counter; otherwise, we assume
* that we're going through the OS. */
#if NCPTL_TIMER_TYPE == 3 || NCPTL_TIMER_TYPE == 4
static int cycle_counter_delay = 1; /* We know a priori that we have a cycle counter. */
#else
static int cycle_counter_delay = -1; /* We need to determine dynamically what we have. */
#endif
#ifdef USE_HPET
static int hpet_fd; /* HPET device file descripor */
static volatile char *hpet_data; /* Pointer to device memory */
#endif
/* Random variable used by ncptl_random_task() (initialized by
* ncptl_seed_random_task()) */
static RNG_STATE random_task_state;
/* Allocate space for storing old signal handlers. */
static SIGHANDLER original_handler[NUM_SIGNALS];
/* Signal to send ourself as part of abnormal exit handling. */
static int exit_signal = 0;
/* Restore all signal handlers to their original values. */
static void reinstate_all_signal_handlers (void)
{
int i;
for (i=1; i<NUM_SIGNALS; i++)
if (!ncptl_no_trap_signal[i])
ncptl_install_signal_handler (i, original_handler[i], NULL, 0);
}
/* Signal handler for SIGALRM */
static RETSIGTYPE set_flag_on_interrupt (int signalnum)
{
if (flag_to_set) {
*flag_to_set = 1;
flag_to_set = NULL;
}
#ifndef HAVE_SIGACTION
ncptl_install_signal_handler (signalnum, original_handler[signalnum], NULL, 0);
#else
dummyvar = signalnum; /* Convince the compiler that signalnum is not an unused parameter. */
#endif
return RETSIGVALUE;
}
/* Send a signal to ourself. */
static void signal_self (void)
{
#ifdef HAVE_SIGACTION
sigset_t selfset;
/* Make it possible to send exit_signal to ourself from within an
* exit_signal handler. Otherwise, the exit_signal would be
* deferred but never sent because the process would exit first. */
(void) sigemptyset (&selfset);
(void) sigaddset (&selfset, exit_signal);
(void) sigprocmask (SIG_UNBLOCK, &selfset, NULL);
#endif
kill (getpid(), exit_signal);
}
/* Signal handler for all other signals */
static RETSIGTYPE abort_on_signal (int signalnum)
{
/* Restore all original signal handlers and prepare to have
* signalnum resent to ourself when ncptl_fatal() invokes exit(). */
reinstate_all_signal_handlers();
exit_signal = signalnum;
atexit (signal_self);
/* Notify the user that we're going down on a signal. */
#ifdef HAVE_STRSIGNAL
ncptl_fatal ("Received signal %d (%s); specify --no-trap=%d to ignore",
signalnum, strsignal(signalnum), signalnum);
#else
ncptl_fatal ("Received signal %d; specify --no-trap=%d to ignore",
signalnum, signalnum);
#endif
return RETSIGVALUE;
}
/* Convert signal_str to an integer or die trying. */
static int parse_signal (const char *signal_str)
{
int signalnum; /* Signal number to return */
char *endptr; /* Pointer to the first non-numeric value */
const NAMENUMBER *signalpair; /* {signal name, signal number} pair */
/* See if the signal was specified by name. */
signalpair = ncptl_sig2num (signal_str, (unsigned int) strlen(signal_str));
if (signalpair)
return (int) signalpair->number;
/* See if the signal was specified by number. */
signalnum = (int) strtol (signal_str, &endptr, 10);
if (*endptr)
ncptl_fatal ("Unable to parse signal \"%s\"", signal_str);
if (signalnum<0 || signalnum>=NUM_SIGNALS)
ncptl_fatal ("Signal number \"%d\" is not between 0 and %d",
signalnum, NUM_SIGNALS-1);
return signalnum;
}
/* Parse a comma-separated list of dash-separated numbers. */
static void parse_signal_list (const char *signallist)
{
char *signalstring; /* Mutable version of signallist */
char *range; /* A single signal or range of signals */
int i;
/* Initialize the routine. */
signalstring = ncptl_strdup (signallist);
for (i=(int)strlen(signalstring)-1; i>=0; i--) /* Allow either spaces or commas. */
if (signalstring[i] == ' ')
signalstring[i] = ',';
/* Loop over all comma-separated values. */
for (range=strtok (signalstring, ",");
range;
range=strtok (NULL, ",")) {
char *dashptr = strchr (range, '-'); /* Pointer to the first dash */
int firstsignal, lastsignal; /* Beginning and ending of range */
/* Parse the range or individual number. */
if (dashptr && dashptr!=range) {
*dashptr = '\0';
firstsignal = parse_signal (range);
lastsignal = parse_signal (dashptr+1);
}
else
firstsignal = lastsignal = parse_signal (range);
if (firstsignal > lastsignal)
ncptl_fatal ("Signal range \"%d-%d\" needs to be written as \"%d-%d\"",
firstsignal, lastsignal, lastsignal, firstsignal);
/* Set a flag for each number in the range. */
for (i=firstsignal; i<=lastsignal; i++)
ncptl_no_trap_signal[i] = 1;
}
/* Finish up cleanly. */
ncptl_free (signalstring);
}
/* Convert a string to its numeric value. The input string is a
* number with optional coNCePTuaL suffixes. */
static ncptl_int string_to_integer (char *stringval)
{
char *badintmsg = "\"%s\" is not a valid integer";
char *suffix;
int64_t intval = strtoll (stringval, &suffix, 10);
/* Abort if no characters were valid. */
if (suffix == stringval)
ncptl_fatal (badintmsg, stringval);
/* Process the suffix */
switch (suffix[0]) {
/* No suffix */
case '\0':
break;
/* Tebibytes */
case 'T':
case 't':
intval *= 1024;
/* No break */
/* Gibibytes */
case 'G':
case 'g':
intval *= 1024;
/* No break */
/* Mebibytes */
case 'M':
case 'm':
intval *= 1024;
/* No break */
/* Kibibytes */
case 'K':
case 'k':
intval *= 1024;
if (suffix[1]) /* Something came after the suffix. */
ncptl_fatal (badintmsg, stringval);
break;
/* Base 10 exponent */
case 'E':
case 'e': {
char *expstr = suffix + 1;
int64_t exponent = strtoll (expstr, &suffix, 10);
if (suffix[0]) /* We must consume the entire string. */
ncptl_fatal (badintmsg, stringval);
intval *= ncptl_func_power ((ncptl_int)10, (ncptl_int)exponent);
}
break;
/* None of the above */
default:
ncptl_fatal (badintmsg, stringval);
break;
}
/* Return the modified integer. */
return intval;
}
#if NCPTL_TIMER_TYPE == 2
/* Define a function that uses a low-precision gettimeofday() with a
* high-precision but 32-bit cycle counter to return a reasonable
* 64-bit timer reading. */
static uint64_t fabricate_64_bit_cycle_counter (void)
{
static uint64_t wrapcycles = UINT64_C(4294967296); /* Wrap time in cycles */
struct timeval tod; /* Current time from gettimeofday() */
uint64_t fnow; /* Current cycle time (fine-grained) */
static uint64_t fthen = 0; /* Previous cycle time (fine-grained) */
uint64_t cnow; /* Current cycle time (coarse-grained) */
static uint64_t cthen = 0; /* Previous cycle time (coarse-grained) */
uint64_t felapsed; /* Elapsed cycle time (fine-grained) */
uint64_t celapsed; /* Elapsed cycle time (coarse-grained) */
uint64_t true_elapsed; /* Adjusted elapsed cycle time (coarse+fine) */
static uint64_t true_time = 0; /* Adjusted cycle time (coarse+fine) */
/* Measure the current cycle time on both timers. */
READ_CYCLE_COUNTER (fnow);
if (gettimeofday(&tod, NULL) == -1)
NCPTL_SYSTEM_ERROR ("Failed to read the current time");
cnow = ncptl_cycles_per_usec * (tod.tv_sec*UINT64_C(1000000) + tod.tv_usec);
/* Use the fine-grained timer to adjust the coarse-grained timer. */
celapsed = cnow - cthen;
felapsed = fnow - fthen;
true_elapsed = wrapcycles*(celapsed/wrapcycles) + felapsed;
if (fthen >= fnow)
true_elapsed += wrapcycles;
/* Prepare for our next invocation. */
cthen = cnow;
fthen = fnow;
true_time += true_elapsed;
return true_time;
}
#endif
/* Enable ncptl_log_shutdown() to be called from a non-stdarg function. */
static void invoke_ncptl_log_shutdown (const char *format, ...)
{
va_list args; /* Argument list */
va_start (args, format);
ncptl_log_shutdown (format, args);
va_end (args);
}
/* Determine if ncptl_time() increments extremely slowly (e.g., in
* 1-second increments). Store the given number of data points in
* timerdeltas[] and return 1 if the average delta is greater than 1
* ms, otherwise 0.
*/
static int timer_increments_slowly (uint64_t numdeltas, uint64_t *timerdeltas)
{
const uint64_t maxtrialcalls = UINT64_C(10000000000); /* Give up if the timer doesn't increment after this many iterations. */
uint64_t meandelta = UINT64_C(0); /* Average timer increment */
uint64_t i, j;
/* Keep probing until we get numdeltas valid readings. */
for (i=0; i<numdeltas; i++) {
uint64_t starttime, stoptime=UINT64_C(0); /* Clock readings */
starttime = ncptl_time();
for (j=0; j<maxtrialcalls && (stoptime=ncptl_time()) == starttime; j++)
;
if (j >= maxtrialcalls)
/* We got the same time every reading -- the clock must be
* seriously broken. */
ncptl_fatal ("The timer function returns a constant value of %" PRIu64 " and therefore completely unusable", starttime);
timerdeltas[i] = stoptime - starttime;
meandelta += timerdeltas[i];
}
meandelta /= numdeltas;
return meandelta > UINT64_C(1000);
}
/* Calculate the mean delay in calling ncptl_time(). */
static void calculate_mean_time_delay (void)
{
const uint64_t mintrialcalls = 100000; /* Min. # of measurements to take */
uint64_t trialcalls; /* # of calls to ncptl_time() in each trial */
const uint64_t mindatapoints = 1000; /* Min. # of nonzero readings we require */
uint64_t *timerdeltas; /* List of all non-zero deltas */
uint64_t numdeltas = 0; /* # of entries in the above */
timerdeltas = (uint64_t *) ncptl_malloc (mintrialcalls * sizeof(uint64_t), sizeof(uint64_t));
(void) ncptl_time(); /* fabricate_64_bit_cycle_counter() needs a warmup call. */
for (trialcalls=mintrialcalls; numdeltas<mindatapoints; trialcalls*=10) {
uint64_t starttime, stoptime; /* Clock readings */
uint64_t i;
/* Take trialcalls readings and hope for mindatapoints nonzero deltas. */
numdeltas = 0;
sleep (0);
for (i=0; i<trialcalls; i++) {
starttime = ncptl_time();
stoptime = ncptl_time();
ncptl_time_overhead += stoptime - starttime;
if (stoptime!=starttime && numdeltas<mintrialcalls)
timerdeltas[numdeltas++] = stoptime - starttime;
}
ncptl_time_overhead /= trialcalls;
/* Finish up if we received a sufficient number of nonzero deltas
* or if we can determine that we're unlikely ever to receive a
* sufficient number. */
if (numdeltas >= mindatapoints
|| (numdeltas < 5 && timer_increments_slowly(numdeltas=5, timerdeltas))) {
double meandelta = 0.0;
double stddevdelta = 0.0;
/* Calculate the mean and standard deviation. */
for (i=0; i<numdeltas; i++)
meandelta += (double) timerdeltas[i];
meandelta /= (double) numdeltas;
for (i=0; i<numdeltas; i++) {
double num = timerdeltas[i] - meandelta;
stddevdelta += num * num;
}
stddevdelta = sqrt (stddevdelta / (numdeltas-1));
ncptl_time_delta_mean = meandelta;
ncptl_time_delta_stddev = stddevdelta;
break;
}
}
if (cycle_counter_delay == -1)
cycle_counter_delay = ncptl_time_overhead < 1;
ncptl_free (timerdeltas);
}
/* Calculate the mean delay in sleeping with ncptl_udelay(). */
static void calculate_mean_sleep_delay (void)
{
#ifdef HAVE_NANOSLEEP
uint64_t *timerdeltas; /* List of all timing measurements */
const int numdeltas = 25; /* # of entries in the above */
uint64_t starttime, stoptime; /* Clock readings */
int i;
/* Take a few delay measurements. */
timerdeltas = (uint64_t *) ncptl_malloc (numdeltas * sizeof(uint64_t), sizeof(uint64_t));
for (i=0; i<numdeltas; i++) {
starttime = ncptl_time();
ncptl_udelay (1, 1);
stoptime = ncptl_time();
timerdeltas[i] = stoptime - starttime;
}
/* Calculate the mean and standard deviation. */
for (i=0; i<numdeltas; i++)
ncptl_sleep_mean += (double) timerdeltas[i];
ncptl_sleep_mean /= (double) numdeltas;
for (i=0; i<numdeltas; i++) {
double num = timerdeltas[i] - ncptl_sleep_mean;
ncptl_sleep_stddev += num * num;
}
ncptl_sleep_stddev = sqrt (ncptl_sleep_stddev / (numdeltas-1));
ncptl_free (timerdeltas);
#endif
}
/* Calculate the quality of the user/system time read from
* ncptl_process_time(). */
static void calculate_process_time_quality (void)
{
#ifdef HAVE_GETRUSAGE
const int datapoints = 100; /* # of nonzero readings we require */
uint64_t *timerdeltas; /* List of nonzero readings */
double meandelta = 0.0; /* Mean nonzero delta */
double stddevdelta = 0.0; /* Standard deviation delta */
int i;
/* Take a number of back-to-back measurements and record the nonzeroes. */
timerdeltas =
(uint64_t *) ncptl_malloc (datapoints * sizeof(uint64_t),
sizeof(uint64_t));
for (i=0; i<datapoints; i++) {
uint64_t initial = ncptl_process_time(0) + ncptl_process_time(1);
uint64_t final;
do
final = ncptl_process_time(0) + ncptl_process_time(1);
while (initial == final);
timerdeltas[i] = final - initial;
meandelta += (double) timerdeltas[i];
}
/* Calculate the mean and standard deviation of the timing deltas. */
meandelta /= datapoints;
for (i=0; i<datapoints; i++) {
double num = timerdeltas[i] - meandelta;
stddevdelta += num * num;
}
stddevdelta = sqrt (stddevdelta / (datapoints-1));
ncptl_proc_time_delta_mean = meandelta;
ncptl_proc_time_delta_stddev = stddevdelta;
ncptl_free (timerdeltas);
#endif
}
/* Calibrate the number of spins per microsecond. We spin for
* TRIALSPINS iterations and divide TRIALSPINS into the elapsed number
* of microseconds. We take the minimum of NUMTRIALS trials because
* we can always delay longer if necessary. */
static void calibrate_spins_per_usec (void)
{
int numtrials = 2; /* # of trials to perform */
uint64_t trialspins = 10000; /* # of spins in each trial (adaptive) */
uint64_t trial_spinsperusec; /* One trial's spins/usecs */
uint64_t starttime, stoptime; /* Clock readings */
const uint64_t target_usecs = 500000; /* Target time to spin for */
uint64_t i;
/* We might be able to make a better initial estimate. */
#ifdef HAVE_CYCLES_PER_USEC
trialspins = ncptl_cycles_per_usec * target_usecs;
#endif
/* Estimate the minimum number of spins per microsecond. */
spinsperusec = ~0;
while (numtrials--)
while (1) {
sleep (0); /* Try to refresh our time quantum. */
starttime = ncptl_time();
for (i=0; i<trialspins; i++)
dummyvar = 0;
stoptime = ncptl_time();
if (stoptime - starttime >= target_usecs) {
trial_spinsperusec = trialspins / (stoptime-starttime);
if (spinsperusec > trial_spinsperusec)
spinsperusec = trial_spinsperusec;
break;
}
trialspins = (stoptime==starttime ?
trialspins*2 :
(target_usecs*trialspins)/(stoptime-starttime));
}
}
/* Return the current time in microseconds without using HPET.
* NOTE: This function must be kept up-to-date with
* log_write_prologue_timer(). */
static uint64_t inlined_time_no_hpet (void)
{
#if NCPTL_TIMER_TYPE == 1
/* Use gettimeofday() if we were forced to or if nothing else is
* available. */
struct timeval now;
if (gettimeofday(&now, NULL) == -1)
NCPTL_SYSTEM_ERROR ("Failed to read the current time");
return (uint64_t)now.tv_sec*(uint64_t)1000000 + (uint64_t)now.tv_usec;
#elif NCPTL_TIMER_TYPE == 2
return fabricate_64_bit_cycle_counter() / ncptl_cycles_per_usec;
#elif NCPTL_TIMER_TYPE == 3
/* Read the hardware real-time cycle counter using inline assembly
* language. */
uint64_t now;
READ_CYCLE_COUNTER(now);
return now / ncptl_cycles_per_usec;
#elif NCPTL_TIMER_TYPE == 4
/* Read Linux's real-time cycle counter. */
return get_cycles() / ncptl_cycles_per_usec;
#elif NCPTL_TIMER_TYPE == 5
/* Utilize PAPI's real-time cycle counter. */
int64_t now = PAPI_get_real_usec();
if (now < 0)
ncptl_fatal ("Failed to read the current time (%s)",
PAPI_strerror(now));
return (uint64_t) now;
#elif NCPTL_TIMER_TYPE == 6
/* Utilize a high-resolution clock function. */
struct timespec now;
if (clock_gettime(CLOCKID, &now) == -1)
NCPTL_SYSTEM_ERROR ("Failed to read the current time");
return (uint64_t)now.tv_sec*(uint64_t)1000000 + (uint64_t)now.tv_nsec/(uint64_t)1000;
#elif NCPTL_TIMER_TYPE == 7
/* Utilize a high-resolution time-in-seconds function. */
return (uint64_t)(dclock()*1.0e6);
#elif NCPTL_TIMER_TYPE == 8
/* Invoke the Win32 high-resolution timer function. */
LARGE_INTEGER now;
if (!QueryPerformanceCounter(&now))
ncptl_fatal ("Failed to read the current time");
return (1000000 * (uint64_t)now.QuadPart) / ncptl_cycles_per_sec;
#elif NCPTL_TIMER_TYPE == 9
/* Use MPI's MPI_Wtime() function, which returns the time in seconds. */
return (uint64_t) (MPI_Wtime()*1.0e6);
#else
# error Unable to implement a microsecond-timer function
#endif
}
/* Attempt to open and memory-map the High-Precision Event Timer
* (HPET) device. If anything goes wrong, set the HPET memory pointer
* to a hardwired NULL value. On success, override systeminfo's
* timer_freq field. */
static void initialize_hpet (void)
{
#ifdef USE_HPET
uint64_t gencap; /* The HPET General Capabilities and ID register */
uint64_t now1, now2; /* Subsequent values of HPET */
/* Open and memory-map the HPET device. */
if ((hpet_fd=open(HPET_DEVICE, O_RDONLY)) == -1)
return;
if ((hpet_data=(volatile char *)mmap (NULL, 1024, PROT_READ, MAP_SHARED, hpet_fd, 0)) == (void *)-1)
return;
/* Read the timer's General Capabilities and ID register. */
gencap = *(volatile uint64_t *)hpet_data;
if ((gencap>>13&1) != 1)
return; /* Don't even bother with 32-bit HPET devices. */
ncptl_hpet_period_fs = gencap >> 32;
if (ncptl_hpet_period_fs == 0 || ncptl_hpet_period_fs > 0x05F5E100)
return; /* The specification dictates a non-zero period of <100ns. */
/* At this point, if the main counter isn't stuck on some value then
* we assume it works. */
ncptl_hpet_timer = (volatile uint64_t *) (hpet_data + 0xF0);
now1 = *ncptl_hpet_timer;
ncptl_udelay (3, 0);
now2 = *ncptl_hpet_timer;
if (now1 == now2)
return;
ncptl_hpet_works = 1;
systeminfo.timer_freq = 1e15 / ncptl_hpet_period_fs;
#endif