DABC (Data Acquisition Backbone Core)  2.9.9
timing.h
Go to the documentation of this file.
1 // $Id: timing.h 4482 2020-04-15 14:47:18Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_timing
17 #define DABC_timing
18 
19 #ifndef DABC_string
20 #include "dabc/string.h"
21 #endif
22 
23 namespace dabc {
24 
25  class Profiler;
26 
40  struct TimeStamp {
41 
42  friend class Profiler;
43 
44  protected:
45  double fValue;
46 
47  static bool gFast;
48 
49  typedef long long int slowclock_t;
50 
52 
53  static slowclock_t GetSlowClock();
54 
55  #if defined (__x86_64__) || defined(__i386__)
56  typedef unsigned long long fastclock_t;
57  static inline fastclock_t GetFastClock()
58  {
59  unsigned low, high;
60  asm volatile ("rdtsc" : "=a" (low), "=d" (high));
61  return (fastclock_t(high) << 32) | low;
62  }
63  #elif defined(__PPC__) || defined(__PPC64__)
64  typedef unsigned long fastclock_t;
65  static inline fastclock_t GetFastClock()
66  {
67  fastclock_t ret;
68  asm volatile ("mftb %0" : "=r" (ret) : );
69  return ret;
70  }
71  #elif defined(__ia64__)
72  typedef unsigned long fastclock_t;
73  static inline fastclock_t GetFastClock()
74  {
75  fastclock_t ret;
76  asm volatile ("mov %0=ar.itc" : "=r" (ret));
77  return ret;
78  }
79  #else
81  static inline fastclock_t GetFastClock() { return GetSlowClock(); }
82  #endif
83 
85  static double gFastClockMult;
86 
87  static double CalculateFastClockMult();
88 
89  static bool CheckLinuxTSC();
90 
91  TimeStamp(double v) : fValue(v) {}
92 
95  static inline TimeStamp Slow() { return TimeStamp((GetSlowClock() - gSlowClockZero) * 1e-9); }
96 
99  static inline TimeStamp Fast() { return TimeStamp((GetFastClock() - gFastClockZero) * gFastClockMult); }
100 
101  public:
102 
103  TimeStamp() : fValue(0) {}
104 
105  TimeStamp(const TimeStamp& src) : fValue(src.fValue) {}
106 
107  TimeStamp& operator=(const TimeStamp& src) { fValue = src.fValue; return *this; }
108 
109  TimeStamp& operator+=(double _add) { fValue+=_add; return *this; }
110 
111  TimeStamp& operator-=(double _sub) { fValue-=_sub; return *this; }
112 
113  TimeStamp operator+(double _add) const { return TimeStamp(fValue+_add); }
114 
115  TimeStamp operator-(double _sub) const { return TimeStamp(fValue-_sub); }
116 
117  double operator()() const { return fValue; }
118 
120  double AsDouble() const { return fValue; }
121 
122  double operator-(const TimeStamp& src) const { return fValue - src.fValue; }
123 
124  bool operator<(const TimeStamp& src) const { return fValue < src.fValue; }
125 
126  bool operator>(const TimeStamp& src) const { return fValue > src.fValue; }
127 
128  bool operator==(const TimeStamp& src) const { return fValue == src.fValue; }
129 
131  bool null() const { return fValue <= 0.; }
132 
134  void Reset() { fValue = 0.; }
135 
138 
140  inline void GetNow(double shift) { GetNow(); fValue += shift; }
141 
144  inline double SpentTillNow() const
145  {
146  return null() ? 0 : Now().AsDouble() - AsDouble();
147  }
148 
152  inline double SpentTillNow(bool set_to_now)
153  {
154  double res = 0.;
155  TimeStamp now = Now();
156  if (!null()) res = now.AsDouble() - AsDouble();
157  if (set_to_now) *this = now;
158  return res;
159  }
160 
163  inline bool Expired(double interval = 0.) const { return Now().AsDouble() > AsDouble() + interval; }
164 
171  inline bool Expired(const TimeStamp& curr, double interval) const { return curr.AsDouble() > AsDouble() + interval; }
172 
176  static inline TimeStamp Now() { return gFast ? Fast() : Slow(); }
177 
178  static void SetUseSlow() { gFast = false; }
179  };
180 
181  // ==========================================================================
182 
190  class DateTime {
191  protected:
192  unsigned tv_sec; // GMT time in seconds, since 1.1.1970
193  unsigned tv_nsec; // fractional part of time in nanoseconds
194  public:
195  DateTime() : tv_sec(0), tv_nsec(0) {}
196 
197  DateTime(uint64_t jsdate) : tv_sec(0), tv_nsec(0) { SetJSDate(jsdate); }
198 
199  DateTime(const DateTime& src) : tv_sec(src.tv_sec), tv_nsec(src.tv_nsec) {}
200 
202  {
203  tv_sec = src.tv_sec;
204  tv_nsec = src.tv_nsec;
205  return *this;
206  }
207 
208  double operator-(const DateTime& src) const { return src.DistanceTo(*this); }
209 
210  bool null() const { return (tv_sec==0) && (tv_nsec==0); }
211 
212  DateTime& GetNow();
213 
215  double AsDouble() const;
216 
218  uint64_t AsJSDate() const;
219 
221  unsigned AsUTCSeconds() const { return tv_sec; }
222 
224  void SetJSDate(uint64_t jsdate)
225  {
226  tv_sec = jsdate / 1000;
227  tv_nsec = ((jsdate % 1000)*1000000);
228  }
229 
231  void SetDouble(double v)
232  {
233  SetJSDate(v > 0 ? (uint64_t) v * 1000 : 0);
234  }
235 
237  std::string AsString(int ndecimal = 0, bool localtime = false) const;
238 
242  std::string AsJSString(int ndecimal = 3) const;
243 
245  std::string OnlyDateAsString(const char *separ = nullptr, bool localtime = false) const;
246 
248  std::string OnlyTimeAsString(const char *separ = nullptr, bool localtime = false) const;
249 
251  bool SetOnlyDate(const char* sbuf);
252 
254  bool SetOnlyTime(const char* sbuf);
255 
257  double DistanceTo(const DateTime& src) const;
258  };
259 
260  inline TimeStamp Now() { return TimeStamp::Now(); }
261 
262  void Sleep(double tm);
263 }
264 
265 #endif
266 
Class for holding GMT time with precision of nanoseconds.
Definition: timing.h:190
unsigned tv_nsec
Definition: timing.h:193
DateTime(uint64_t jsdate)
Definition: timing.h:197
DateTime(const DateTime &src)
Definition: timing.h:199
bool SetOnlyTime(const char *sbuf)
Set only time part of DateTime.
Definition: timing.cxx:308
std::string OnlyTimeAsString(const char *separ=nullptr, bool localtime=false) const
Fills only time as string.
Definition: timing.cxx:273
std::string AsJSString(int ndecimal=3) const
convert string into sec.frac format, can be interpret directly in JavaScript ISO 8601 standard is use...
Definition: timing.cxx:212
DateTime & operator=(const DateTime &src)
Definition: timing.h:201
double operator-(const DateTime &src) const
Definition: timing.h:208
double AsDouble() const
Return date and time in seconds since 1.1.1970.
Definition: timing.cxx:206
void SetJSDate(uint64_t jsdate)
Set value in form of JS date - milliseconds since 1.1.1970.
Definition: timing.h:224
uint64_t AsJSDate() const
Return date and time in JS format - number of millisecond since 1.1.1970.
Definition: timing.cxx:158
std::string AsString(int ndecimal=0, bool localtime=false) const
convert string into human-readable format, cannot be interpret directly in JavaScript
Definition: timing.cxx:164
DateTime & GetNow()
Definition: timing.cxx:147
unsigned AsUTCSeconds() const
Returns only seconds since 1.1.1970.
Definition: timing.h:221
double DistanceTo(const DateTime &src) const
Return distance in seconds to provided date.
Definition: timing.cxx:332
bool SetOnlyDate(const char *sbuf)
Set only date part from the string.
Definition: timing.cxx:289
void SetDouble(double v)
Set value in form of double - seconds since 1.1.1970.
Definition: timing.h:231
std::string OnlyDateAsString(const char *separ=nullptr, bool localtime=false) const
Fills only date as string.
Definition: timing.cxx:257
unsigned tv_sec
Definition: timing.h:192
Event manipulation API.
Definition: api.h:23
void Sleep(double tm)
Definition: timing.cxx:129
TimeStamp Now()
Definition: timing.h:260
Class for acquiring and holding timestamps.
Definition: timing.h:40
double SpentTillNow(bool set_to_now)
Method return time in second, spent from the time kept in TimeStamp instance If specified,...
Definition: timing.h:152
double fValue
time since start of the application in seconds
Definition: timing.h:45
TimeStamp operator-(double _sub) const
Definition: timing.h:115
double SpentTillNow() const
Method return time in second, spent from the time kept in TimeStamp instance If time was not set befo...
Definition: timing.h:144
TimeStamp & operator+=(double _add)
Definition: timing.h:109
static slowclock_t GetSlowClock()
Definition: timing.cxx:31
void GetNow(double shift)
Method to acquire current time stamp plus shift in seconds.
Definition: timing.h:140
TimeStamp(const TimeStamp &src)
Definition: timing.h:105
static double gFastClockMult
Definition: timing.h:85
TimeStamp & operator=(const TimeStamp &src)
Definition: timing.h:107
TimeStamp(double v)
Definition: timing.h:91
slowclock_t fastclock_t
Definition: timing.h:80
bool operator==(const TimeStamp &src) const
Definition: timing.h:128
void Reset()
Set time stamp value to null.
Definition: timing.h:134
static double CalculateFastClockMult()
Definition: timing.cxx:39
bool Expired(const TimeStamp &curr, double interval) const
Method returns true if specified time interval expired relative to time, kept in TimeStamp instance.
Definition: timing.h:171
static bool gFast
indicates if fast or slow method is used for time measurement
Definition: timing.h:47
static TimeStamp Now()
Method returns TimeStamp instance with current time stamp value, measured either by fast TSC (if it i...
Definition: timing.h:176
static slowclock_t gSlowClockZero
Definition: timing.h:51
bool Expired(double interval=0.) const
Method returns true if specified time interval expired relative to time, kept in TimeStamp instance.
Definition: timing.h:163
static fastclock_t GetFastClock()
Definition: timing.h:81
void GetNow()
Method to acquire current time stamp.
Definition: timing.h:137
TimeStamp & operator-=(double _sub)
Definition: timing.h:111
bool operator<(const TimeStamp &src) const
Definition: timing.h:124
double AsDouble() const
Return time stamp in form of double (in seconds)
Definition: timing.h:120
static TimeStamp Fast()
Method returns TimeStamp instance with current time, measure by fast TSC clock.
Definition: timing.h:99
double operator()() const
Definition: timing.h:117
static fastclock_t gFastClockZero
Definition: timing.h:84
bool operator>(const TimeStamp &src) const
Definition: timing.h:126
TimeStamp operator+(double _add) const
Definition: timing.h:113
static bool CheckLinuxTSC()
Definition: timing.cxx:102
double operator-(const TimeStamp &src) const
Definition: timing.h:122
static TimeStamp Slow()
Method returns TimeStamp instance with current time, measured by 'slow' getclock() function.
Definition: timing.h:95
long long int slowclock_t
Definition: timing.h:49
static void SetUseSlow()
Definition: timing.h:178