1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // clock --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Thu Apr 12 14:36:06 2018
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Sun Apr 18 08:12:16 2021
|
---|
13 | // Update Count : 28
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <time.hfa>
|
---|
17 |
|
---|
18 | //######################### C time #########################
|
---|
19 |
|
---|
20 | static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
|
---|
21 | static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
|
---|
22 | static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
|
---|
23 | static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
|
---|
24 | static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
|
---|
25 | static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
|
---|
26 |
|
---|
27 | //######################### Clock #########################
|
---|
28 |
|
---|
29 | struct Clock { // virtual clock
|
---|
30 | // private
|
---|
31 | Duration offset; // offset from computer real-time
|
---|
32 | };
|
---|
33 |
|
---|
34 | static inline {
|
---|
35 | void reset( Clock & clk, Duration adj ) with( clk ) { // change offset
|
---|
36 | offset = adj + __timezone`s; // timezone (global) is (UTC - local time) in seconds
|
---|
37 | } // reset
|
---|
38 |
|
---|
39 | void ?{}( Clock & clk ) { reset( clk, (Duration){ 0 } ); } // create no offset
|
---|
40 | void ?{}( Clock & clk, Duration adj ) { reset( clk, adj ); } // create with offset
|
---|
41 |
|
---|
42 | // System-wide clock that measures real, i.e., wall-clock) time. This clock is affected by discontinuous jumps in
|
---|
43 | // the system time. For example, manual changes of the clock, and incremental adjustments performed by adjtime(3)
|
---|
44 | // and NTP (daylight saving (Fall back).
|
---|
45 | Duration resolutionHi() { // clock resolution in nanoseconds (fine)
|
---|
46 | struct timespec res;
|
---|
47 | clock_getres( CLOCK_REALTIME, &res );
|
---|
48 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
|
---|
49 | } // resolutionHi
|
---|
50 |
|
---|
51 | Duration resolution() { // clock resolution without nanoseconds (coarse)
|
---|
52 | struct timespec res;
|
---|
53 | clock_getres( CLOCK_REALTIME_COARSE, &res );
|
---|
54 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
|
---|
55 | } // resolution
|
---|
56 |
|
---|
57 | Time timeHiRes() { // real time with nanoseconds
|
---|
58 | timespec curr;
|
---|
59 | clock_gettime( CLOCK_REALTIME, &curr );
|
---|
60 | return (Time){ curr };
|
---|
61 | } // timeHiRes
|
---|
62 |
|
---|
63 | Time time() { // real time without nanoseconds
|
---|
64 | timespec curr;
|
---|
65 | clock_gettime( CLOCK_REALTIME_COARSE, &curr );
|
---|
66 | curr.tv_nsec = 0;
|
---|
67 | return (Time){ curr };
|
---|
68 | } // time
|
---|
69 |
|
---|
70 | Time time( Clock & clk ) with( clk ) { // real time for given clock
|
---|
71 | return time() + offset;
|
---|
72 | } // time
|
---|
73 |
|
---|
74 | Time ?()( Clock & clk ) with( clk ) { // alternative syntax
|
---|
75 | return time() + offset;
|
---|
76 | } // ?()
|
---|
77 |
|
---|
78 | timeval time( Clock & clk ) { // convert to C time format
|
---|
79 | return (timeval){ clk() };
|
---|
80 | } // time
|
---|
81 |
|
---|
82 | tm time( Clock & clk ) with( clk ) {
|
---|
83 | tm ret;
|
---|
84 | localtime_r( time( clk ).tv_sec, &ret );
|
---|
85 | return ret;
|
---|
86 | } // time
|
---|
87 |
|
---|
88 | // CFA processor CPU-time watch that ticks when the processor (kernel thread) is running. This watch is affected by
|
---|
89 | // discontinuous jumps when the OS is not running the kernal thread. A duration is returned because the value is
|
---|
90 | // relative and cannot be converted to real-time (wall-clock) time.
|
---|
91 | Duration processor() { // non-monotonic duration of kernel thread
|
---|
92 | timespec ts;
|
---|
93 | clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
|
---|
94 | return (Duration){ ts };
|
---|
95 | } // processor
|
---|
96 |
|
---|
97 | // Program CPU-time watch measures CPU time consumed by all processors (kernel threads) in the UNIX process. This
|
---|
98 | // watch is affected by discontinuous jumps when the OS is not running the kernel threads. A duration is returned
|
---|
99 | // because the value is relative and cannot be converted to real-time (wall-clock) time.
|
---|
100 | Duration program() { // non-monotonic duration of program CPU
|
---|
101 | timespec ts;
|
---|
102 | clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
|
---|
103 | return (Duration){ ts };
|
---|
104 | } // program
|
---|
105 |
|
---|
106 | // Monotonic duration from machine boot and including system suspension. This watch is unaffected by discontinuous
|
---|
107 | // jumps resulting from manual changes of the clock, and incremental adjustments performed by adjtime(3) and NTP
|
---|
108 | // (Fall back). A duration is returned because the value is relative and cannot be converted to real-time
|
---|
109 | // (wall-clock) time.
|
---|
110 | Duration boot() { // monotonic duration since computer boot
|
---|
111 | timespec ts;
|
---|
112 | clock_gettime( CLOCK_BOOTTIME, &ts );
|
---|
113 | return (Duration){ ts };
|
---|
114 | } // boot
|
---|
115 | } // distribution
|
---|
116 |
|
---|
117 | // Local Variables: //
|
---|
118 | // mode: c //
|
---|
119 | // tab-width: 4 //
|
---|
120 | // End: //
|
---|