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