[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
|
---|
[4aa495f] | 12 | // Last Modified On : Sun Apr 18 08:12:16 2021
|
---|
| 13 | // Update Count : 28
|
---|
[4834563] | 14 | //
|
---|
| 15 |
|
---|
[43db35e] | 16 | #pragma once
|
---|
| 17 |
|
---|
[4834563] | 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 |
|
---|
[4aa495f] | 31 | struct Clock { // virtual clock
|
---|
| 32 | // private
|
---|
| 33 | Duration offset; // offset from computer real-time
|
---|
[4834563] | 34 | };
|
---|
| 35 |
|
---|
| 36 | static inline {
|
---|
[4aa495f] | 37 | void reset( Clock & clk, Duration adj ) with( clk ) { // change offset
|
---|
[4834563] | 38 | offset = adj + __timezone`s; // timezone (global) is (UTC - local time) in seconds
|
---|
[6645cda] | 39 | } // reset
|
---|
[4834563] | 40 |
|
---|
[4aa495f] | 41 | void ?{}( Clock & clk ) { reset( clk, (Duration){ 0 } ); } // create no offset
|
---|
| 42 | void ?{}( Clock & clk, Duration adj ) { reset( clk, adj ); } // create with offset
|
---|
[4834563] | 43 |
|
---|
[6645cda] | 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).
|
---|
[4aa495f] | 47 | Duration resolutionHi() { // clock resolution in nanoseconds (fine)
|
---|
[4834563] | 48 | struct timespec res;
|
---|
| 49 | clock_getres( CLOCK_REALTIME, &res );
|
---|
| 50 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
|
---|
[4aa495f] | 51 | } // resolutionHi
|
---|
[4834563] | 52 |
|
---|
[4aa495f] | 53 | Duration resolution() { // clock resolution without nanoseconds (coarse)
|
---|
[4834563] | 54 | struct timespec res;
|
---|
| 55 | clock_getres( CLOCK_REALTIME_COARSE, &res );
|
---|
| 56 | return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
|
---|
[6645cda] | 57 | } // resolution
|
---|
[4834563] | 58 |
|
---|
[4aa495f] | 59 | Time timeHiRes() { // real time with nanoseconds
|
---|
[4834563] | 60 | timespec curr;
|
---|
| 61 | clock_gettime( CLOCK_REALTIME, &curr );
|
---|
| 62 | return (Time){ curr };
|
---|
[4aa495f] | 63 | } // timeHiRes
|
---|
[4834563] | 64 |
|
---|
[4aa495f] | 65 | Time time() { // real time without nanoseconds
|
---|
[4834563] | 66 | timespec curr;
|
---|
| 67 | clock_gettime( CLOCK_REALTIME_COARSE, &curr );
|
---|
| 68 | curr.tv_nsec = 0;
|
---|
| 69 | return (Time){ curr };
|
---|
[6645cda] | 70 | } // time
|
---|
[4834563] | 71 |
|
---|
[4aa495f] | 72 | Time time( Clock & clk ) with( clk ) { // real time for given clock
|
---|
[6645cda] | 73 | return time() + offset;
|
---|
| 74 | } // time
|
---|
[4834563] | 75 |
|
---|
| 76 | Time ?()( Clock & clk ) with( clk ) { // alternative syntax
|
---|
[6645cda] | 77 | return time() + offset;
|
---|
| 78 | } // ?()
|
---|
[4834563] | 79 |
|
---|
[4aa495f] | 80 | timeval time( Clock & clk ) { // convert to C time format
|
---|
[4834563] | 81 | return (timeval){ clk() };
|
---|
[6645cda] | 82 | } // time
|
---|
[4834563] | 83 |
|
---|
[6645cda] | 84 | tm time( Clock & clk ) with( clk ) {
|
---|
[4834563] | 85 | tm ret;
|
---|
[6645cda] | 86 | localtime_r( time( clk ).tv_sec, &ret );
|
---|
[4834563] | 87 | return ret;
|
---|
[6645cda] | 88 | } // time
|
---|
[4834563] | 89 |
|
---|
[6645cda] | 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.
|
---|
[4aa495f] | 93 | Duration processor() { // non-monotonic duration of kernel thread
|
---|
[4834563] | 94 | timespec ts;
|
---|
| 95 | clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
|
---|
[6645cda] | 96 | return (Duration){ ts };
|
---|
| 97 | } // processor
|
---|
| 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.
|
---|
[4aa495f] | 102 | Duration program() { // non-monotonic duration of program CPU
|
---|
[6645cda] | 103 | timespec ts;
|
---|
| 104 | clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
|
---|
| 105 | return (Duration){ ts };
|
---|
| 106 | } // program
|
---|
| 107 |
|
---|
[4aa495f] | 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
|
---|
[6645cda] | 113 | timespec ts;
|
---|
| 114 | clock_gettime( CLOCK_BOOTTIME, &ts );
|
---|
| 115 | return (Duration){ ts };
|
---|
| 116 | } // boot
|
---|
[4834563] | 117 | } // distribution
|
---|
| 118 |
|
---|
| 119 | // Local Variables: //
|
---|
| 120 | // mode: c //
|
---|
| 121 | // tab-width: 4 //
|
---|
| 122 | // End: //
|
---|