[4834563] | 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 |
---|
[6645cda] | 12 | // Last Modified On : Wed Apr 14 17:48:25 2021 |
---|
| 13 | // Update Count : 20 |
---|
[4834563] | 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 { // private |
---|
| 30 | Duration offset; // for virtual clock: contains offset from real-time |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | static inline { |
---|
[6645cda] | 34 | void reset( Clock & clk, Duration adj ) with( clk ) { |
---|
[4834563] | 35 | offset = adj + __timezone`s; // timezone (global) is (UTC - local time) in seconds |
---|
[6645cda] | 36 | } // reset |
---|
[4834563] | 37 | |
---|
[6645cda] | 38 | void ?{}( Clock & clk ) { reset( clk, (Duration){ 0 } ); } |
---|
| 39 | void ?{}( Clock & clk, Duration adj ) { reset( clk, adj ); } |
---|
[4834563] | 40 | |
---|
[6645cda] | 41 | // System-wide clock that measures real, i.e., wall-clock) time. This clock is affected by discontinuous jumps in |
---|
| 42 | // the system time. For example, manual changes of the clock, and incremental adjustments performed by adjtime(3) |
---|
| 43 | // and NTP (daylight saving (Fall back). |
---|
| 44 | Duration resolutionNsec() { |
---|
[4834563] | 45 | struct timespec res; |
---|
| 46 | clock_getres( CLOCK_REALTIME, &res ); |
---|
| 47 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; |
---|
[6645cda] | 48 | } // resolutionNsec |
---|
[4834563] | 49 | |
---|
[6645cda] | 50 | Duration resolution() { |
---|
[4834563] | 51 | struct timespec res; |
---|
| 52 | clock_getres( CLOCK_REALTIME_COARSE, &res ); |
---|
| 53 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; |
---|
[6645cda] | 54 | } // resolution |
---|
[4834563] | 55 | |
---|
[6645cda] | 56 | Time timeNsec() { // with nanoseconds |
---|
[4834563] | 57 | timespec curr; |
---|
| 58 | clock_gettime( CLOCK_REALTIME, &curr ); |
---|
| 59 | return (Time){ curr }; |
---|
[6645cda] | 60 | } // timeNsec |
---|
[4834563] | 61 | |
---|
[6645cda] | 62 | Time time() { // without nanoseconds |
---|
[4834563] | 63 | timespec curr; |
---|
| 64 | clock_gettime( CLOCK_REALTIME_COARSE, &curr ); |
---|
| 65 | curr.tv_nsec = 0; |
---|
| 66 | return (Time){ curr }; |
---|
[6645cda] | 67 | } // time |
---|
[4834563] | 68 | |
---|
[6645cda] | 69 | Time time( Clock & clk ) with( clk ) { |
---|
| 70 | return time() + offset; |
---|
| 71 | } // time |
---|
[4834563] | 72 | |
---|
| 73 | Time ?()( Clock & clk ) with( clk ) { // alternative syntax |
---|
[6645cda] | 74 | return time() + offset; |
---|
| 75 | } // ?() |
---|
[4834563] | 76 | |
---|
[6645cda] | 77 | timeval time( Clock & clk ) { |
---|
[4834563] | 78 | return (timeval){ clk() }; |
---|
[6645cda] | 79 | } // time |
---|
[4834563] | 80 | |
---|
[6645cda] | 81 | tm time( Clock & clk ) with( clk ) { |
---|
[4834563] | 82 | tm ret; |
---|
[6645cda] | 83 | localtime_r( time( clk ).tv_sec, &ret ); |
---|
[4834563] | 84 | return ret; |
---|
[6645cda] | 85 | } // time |
---|
[4834563] | 86 | |
---|
[6645cda] | 87 | // CFA processor CPU-time watch that ticks when the processor (kernel thread) is running. This watch is affected by |
---|
| 88 | // discontinuous jumps when the OS is not running the kernal thread. A duration is returned because the value is |
---|
| 89 | // relative and cannot be converted to real-time (wall-clock) time. |
---|
| 90 | Duration processor() { |
---|
[4834563] | 91 | timespec ts; |
---|
| 92 | clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ); |
---|
[6645cda] | 93 | return (Duration){ ts }; |
---|
| 94 | } // processor |
---|
| 95 | |
---|
| 96 | // Program CPU-time watch measures CPU time consumed by all processors (kernel threads) in the UNIX process. This |
---|
| 97 | // watch is affected by discontinuous jumps when the OS is not running the kernel threads. A duration is returned |
---|
| 98 | // because the value is relative and cannot be converted to real-time (wall-clock) time. |
---|
| 99 | Duration program() { |
---|
| 100 | timespec ts; |
---|
| 101 | clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts ); |
---|
| 102 | return (Duration){ ts }; |
---|
| 103 | } // program |
---|
| 104 | |
---|
| 105 | // Monotonic stopwatch starting at machine boot and includes system suspension. This watch is unaffected by |
---|
| 106 | // discontinuous jumps resulting from manual changes of the clock, and incremental adjustments performed by |
---|
| 107 | // adjtime(3) and NTP (Fall back). A duration is returned because the value is relative and cannot be converted to |
---|
| 108 | // real-time (wall-clock) time. |
---|
| 109 | Duration boot() { |
---|
| 110 | timespec ts; |
---|
| 111 | clock_gettime( CLOCK_BOOTTIME, &ts ); |
---|
| 112 | return (Duration){ ts }; |
---|
| 113 | } // boot |
---|
[4834563] | 114 | } // distribution |
---|
| 115 | |
---|
| 116 | // Local Variables: // |
---|
| 117 | // mode: c // |
---|
| 118 | // tab-width: 4 // |
---|
| 119 | // End: // |
---|