Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/clock.hfa

    r4aa495f r4834563  
    1010// Created On       : Thu Apr 12 14:36:06 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Apr 18 08:12:16 2021
    13 // Update Count     : 28
     12// Last Modified On : Mon Jan  6 12:49:58 2020
     13// Update Count     : 9
    1414//
    1515
     
    2727//######################### Clock #########################
    2828
    29 struct Clock {                                                                                  // virtual clock
    30         // private
    31         Duration offset;                                                                        // offset from computer real-time
     29struct Clock {                                                                                  // private
     30        Duration offset;                                                                        // for virtual clock: contains offset from real-time
    3231};
    3332
    3433static inline {
    35         void reset( Clock & clk, Duration adj ) with( clk ) { // change offset
     34        void resetClock( Clock & clk, Duration adj ) with( clk ) {
    3635                offset = adj + __timezone`s;                                    // timezone (global) is (UTC - local time) in seconds
    37         } // reset
     36        } // resetClock
    3837
    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 ); }
    4139
    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() {
    4641                struct timespec res;
    4742                clock_getres( CLOCK_REALTIME, &res );
    4843                return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
    49         } // resolutionHi
     44        } // getRes
    5045
    51         Duration resolution() {                                                         // clock resolution without nanoseconds (coarse)
     46        Duration getRes() {
    5247                struct timespec res;
    5348                clock_getres( CLOCK_REALTIME_COARSE, &res );
    5449                return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
    55         } // resolution
     50        } // getRes
    5651
    57         Time timeHiRes() {                                                                      // real time with nanoseconds
     52        Time getTimeNsec() {                                                            // with nanoseconds
    5853                timespec curr;
    5954                clock_gettime( CLOCK_REALTIME, &curr );
    6055                return (Time){ curr };
    61         } // timeHiRes
     56        } // getTimeNsec
    6257
    63         Time time() {                                                                           // real time without nanoseconds
     58        Time getTime() {                                                                        // without nanoseconds
    6459                timespec curr;
    6560                clock_gettime( CLOCK_REALTIME_COARSE, &curr );
    6661                curr.tv_nsec = 0;
    6762                return (Time){ curr };
    68         } // time
     63        } // getTime
    6964
    70         Time time( Clock & clk ) with( clk ) {                          // real time for given clock
    71                 return time() + offset;
    72         } // time
     65        Time getTime( Clock & clk ) with( clk ) {
     66                return getTime() + offset;
     67        } // getTime
    7368
    7469        Time ?()( Clock & clk ) with( clk ) {                           // alternative syntax
    75                 return time() + offset;
    76         } // ?()
     70                return getTime() + offset;
     71        } // getTime
    7772
    78         timeval time( Clock & clk ) {                                           // convert to C time format
     73        timeval getTime( Clock & clk ) {
    7974                return (timeval){ clk() };
    80         } // time
     75        } // getTime
    8176
    82         tm time( Clock & clk ) with( clk ) {
     77        tm getTime( Clock & clk ) with( clk ) {
    8378                tm ret;
    84                 localtime_r( time( clk ).tv_sec, &ret );
     79                localtime_r( getTime( clk ).tv_sec, &ret );
    8580                return ret;
    86         } // time
     81        } // getTime
    8782
    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() {
    9284                timespec ts;
    9385                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
    11588} // distribution
    11689
Note: See TracChangeset for help on using the changeset viewer.