source: libcfa/src/clock.hfa @ 13888c0

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 13888c0 was 4aa495f, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

More fixes after another change to getTimeNsec()

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[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
16#include <time.hfa>
17
18//######################### C time #########################
19
20static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
21static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
22static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
23static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
24static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
25static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
26
27//######################### Clock #########################
28
[4aa495f]29struct Clock {                                                                                  // virtual clock
30        // private
31        Duration offset;                                                                        // offset from computer real-time
[4834563]32};
33
34static inline {
[4aa495f]35        void reset( Clock & clk, Duration adj ) with( clk ) { // change offset
[4834563]36                offset = adj + __timezone`s;                                    // timezone (global) is (UTC - local time) in seconds
[6645cda]37        } // reset
[4834563]38
[4aa495f]39        void ?{}( Clock & clk ) { reset( clk, (Duration){ 0 } ); } // create no offset
40        void ?{}( Clock & clk, Duration adj ) { reset( clk, adj ); } // create with offset
[4834563]41
[6645cda]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).
[4aa495f]45        Duration resolutionHi() {                                                       // clock resolution in nanoseconds (fine)
[4834563]46                struct timespec res;
47                clock_getres( CLOCK_REALTIME, &res );
48                return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
[4aa495f]49        } // resolutionHi
[4834563]50
[4aa495f]51        Duration resolution() {                                                         // clock resolution without nanoseconds (coarse)
[4834563]52                struct timespec res;
53                clock_getres( CLOCK_REALTIME_COARSE, &res );
54                return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
[6645cda]55        } // resolution
[4834563]56
[4aa495f]57        Time timeHiRes() {                                                                      // real time with nanoseconds
[4834563]58                timespec curr;
59                clock_gettime( CLOCK_REALTIME, &curr );
60                return (Time){ curr };
[4aa495f]61        } // timeHiRes
[4834563]62
[4aa495f]63        Time time() {                                                                           // real time without nanoseconds
[4834563]64                timespec curr;
65                clock_gettime( CLOCK_REALTIME_COARSE, &curr );
66                curr.tv_nsec = 0;
67                return (Time){ curr };
[6645cda]68        } // time
[4834563]69
[4aa495f]70        Time time( Clock & clk ) with( clk ) {                          // real time for given clock
[6645cda]71                return time() + offset;
72        } // time
[4834563]73
74        Time ?()( Clock & clk ) with( clk ) {                           // alternative syntax
[6645cda]75                return time() + offset;
76        } // ?()
[4834563]77
[4aa495f]78        timeval time( Clock & clk ) {                                           // convert to C time format
[4834563]79                return (timeval){ clk() };
[6645cda]80        } // time
[4834563]81
[6645cda]82        tm time( Clock & clk ) with( clk ) {
[4834563]83                tm ret;
[6645cda]84                localtime_r( time( clk ).tv_sec, &ret );
[4834563]85                return ret;
[6645cda]86        } // time
[4834563]87
[6645cda]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.
[4aa495f]91        Duration processor() {                                                          // non-monotonic duration of kernel thread
[4834563]92                timespec ts;
93                clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
[6645cda]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.
[4aa495f]100        Duration program() {                                                            // non-monotonic duration of program CPU
[6645cda]101                timespec ts;
102                clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
103                return (Duration){ ts };
104        } // program
105
[4aa495f]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
[6645cda]111                timespec ts;
112                clock_gettime( CLOCK_BOOTTIME, &ts );
113                return (Duration){ ts };
114        } // boot
[4834563]115} // distribution
116
117// Local Variables: //
118// mode: c //
119// tab-width: 4 //
120// End: //
Note: See TracBrowser for help on using the repository browser.