Changes in libcfa/src/clock.hfa [4aa495f:4834563]
- File:
-
- 1 edited
-
libcfa/src/clock.hfa (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/clock.hfa
r4aa495f r4834563 10 10 // Created On : Thu Apr 12 14:36:06 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Apr 18 08:12:16 202113 // Update Count : 2812 // Last Modified On : Mon Jan 6 12:49:58 2020 13 // Update Count : 9 14 14 // 15 15 … … 27 27 //######################### Clock ######################### 28 28 29 struct Clock { // virtual clock 30 // private 31 Duration offset; // offset from computer real-time 29 struct Clock { // private 30 Duration offset; // for virtual clock: contains offset from real-time 32 31 }; 33 32 34 33 static inline { 35 void reset ( Clock & clk, Duration adj ) with( clk ) { // change offset34 void resetClock( Clock & clk, Duration adj ) with( clk ) { 36 35 offset = adj + __timezone`s; // timezone (global) is (UTC - local time) in seconds 37 } // reset 36 } // resetClock 38 37 39 void ?{}( Clock & clk ) { reset( clk, (Duration){ 0 } ); } // create no offset 40 void ?{}( Clock & clk, Duration adj ) { reset( clk, adj ); } // create with offset 38 void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); } 41 39 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) 40 Duration getResNsec() { 46 41 struct timespec res; 47 42 clock_getres( CLOCK_REALTIME, &res ); 48 43 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; 49 } // resolutionHi44 } // getRes 50 45 51 Duration resolution() { // clock resolution without nanoseconds (coarse)46 Duration getRes() { 52 47 struct timespec res; 53 48 clock_getres( CLOCK_REALTIME_COARSE, &res ); 54 49 return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns; 55 } // resolution50 } // getRes 56 51 57 Time timeHiRes() { // real timewith nanoseconds52 Time getTimeNsec() { // with nanoseconds 58 53 timespec curr; 59 54 clock_gettime( CLOCK_REALTIME, &curr ); 60 55 return (Time){ curr }; 61 } // timeHiRes56 } // getTimeNsec 62 57 63 Time time() { // real timewithout nanoseconds58 Time getTime() { // without nanoseconds 64 59 timespec curr; 65 60 clock_gettime( CLOCK_REALTIME_COARSE, &curr ); 66 61 curr.tv_nsec = 0; 67 62 return (Time){ curr }; 68 } // time63 } // getTime 69 64 70 Time time( Clock & clk ) with( clk ) { // real time for given clock71 return time() + offset;72 } // time65 Time getTime( Clock & clk ) with( clk ) { 66 return getTime() + offset; 67 } // getTime 73 68 74 69 Time ?()( Clock & clk ) with( clk ) { // alternative syntax 75 return time() + offset;76 } // ?()70 return getTime() + offset; 71 } // getTime 77 72 78 timeval time( Clock & clk ) { // convert to C time format73 timeval getTime( Clock & clk ) { 79 74 return (timeval){ clk() }; 80 } // time75 } // getTime 81 76 82 tm time( Clock & clk ) with( clk ) {77 tm getTime( Clock & clk ) with( clk ) { 83 78 tm ret; 84 localtime_r( time( clk ).tv_sec, &ret );79 localtime_r( getTime( clk ).tv_sec, &ret ); 85 80 return ret; 86 } // time81 } // getTime 87 82 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 83 Time getCPUTime() { 92 84 timespec ts; 93 85 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 86 return (Time){ ts }; 87 } // getCPUTime 115 88 } // distribution 116 89
Note:
See TracChangeset
for help on using the changeset viewer.