This is Unofficial EPICS BASE Doxygen Site
osdMonotonic.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2015 Michael Davidsaver
3 * EPICS BASE is distributed subject to a Software License Agreement found
4 * in file LICENSE that is included with this distribution.
5 \*************************************************************************/
6 
7 #define EPICS_EXPOSE_LIBCOM_MONOTONIC_PRIVATE
8 #include "dbDefs.h"
9 #include "errlog.h"
10 #include "epicsTime.h"
11 #include "generalTimeSup.h"
12 
13 static clockid_t osdMonotonicID;
14 static epicsUInt64 osdMonotonicResolution;
15 
16 void osdMonotonicInit(void)
17 {
18  unsigned i;
19 clockid_t ids[] = {
20 #ifdef CLOCK_HIGHRES
21  CLOCK_HIGHRES, /* solaris specific */
22 #endif
23 #ifdef CLOCK_MONOTONIC
24  CLOCK_MONOTONIC, /* Linux, RTEMS, and probably others */
25 #endif
26  /* fallback and vxWorks, not actually monotonic, but always available */
27  CLOCK_REALTIME
28  };
29 
30  for(i=0; i<NELEMENTS(ids); i++) {
31  epicsUInt64 res;
32  struct timespec ts;
33  int ret = clock_getres(ids[i], &ts);
34  if(ret) continue;
35 
36  res = ts.tv_sec;
37  res *= 1000000000;
38  res += ts.tv_nsec;
39 
40  /* fetch the time once to see that it actually succeeds */
41  ret = clock_gettime(ids[i], &ts);
42  if(ret) continue;
43 
44  osdMonotonicID = ids[i];
45  osdMonotonicResolution = res;
46  return;
47  }
48 
49  errMessage(errlogMinor, "Warning: failed to setup monotonic time source\n");
50 }
51 
53 {
54  return osdMonotonicResolution;
55 }
56 
58 {
59  struct timespec ts;
60  int ret = clock_gettime(osdMonotonicID, &ts);
61  if(ret) {
62  errlogPrintf("Warning: failed to fetch monotonic time %d %d\n",
63  (int)osdMonotonicID, ret);
64  return 0;
65  } else {
66  epicsUInt64 ret = ts.tv_sec;
67  ret *= 1000000000;
68  ret += ts.tv_nsec;
69  return ret;
70  }
71 }
int i
Definition: scan.c:967
void osdMonotonicInit(void)
Definition: osdMonotonic.c:19
epicsUInt64 epicsMonotonicGet(void)
Fetch monotonic counter, returns the number of nanoseconds since some unspecified time...
Definition: osdMonotonic.c:29
#define errMessage(S, PM)
Definition: errlog.h:48
time_t tv_sec
Definition: osdTime.h:22
Miscellaneous macro definitions.
unsigned long long epicsUInt64
Definition: epicsTypes.h:45
long tv_nsec
Definition: osdTime.h:23
epicsUInt64 epicsMonotonicResolution(void)
Monotonic time resolution, may not be accurate. Returns the minimum non-zero time difference between ...
Definition: osdMonotonic.c:24
#define NELEMENTS(A)
Definition: aToIPAddr.c:21
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
Defined by POSIX Real Time.
Definition: osdTime.h:21
EPICS time-stamps (epicsTimeStamp), epicsTime C++ class and C functions for handling wall-clock times...