| 1 |
#include <davix_internal_config.hpp> |
| 2 |
#include "timepoint.hpp" |
| 3 |
#include <sys/time.h> |
| 4 |
|
| 5 |
#if __cplusplus >= 201103L |
| 6 |
#include <chrono> |
| 7 |
#else |
| 8 |
#include <sys/syscall.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
static void gettimeofday_timespec(struct timespec *time_value) { |
| 12 |
struct timeval now; |
| 13 |
(void) gettimeofday(&now, NULL); |
| 14 |
time_value->tv_sec = now.tv_sec; |
| 15 |
time_value->tv_nsec = now.tv_usec * 1000; |
| 16 |
} |
| 17 |
|
| 18 |
// also for use inside libneon |
| 19 |
extern "C" { |
| 20 |
|
| 21 |
// we should get rid of ifdefs as soon as we have C++11 on all platforms we care about |
| 22 |
void davix_get_monotonic_time(struct timespec *time_value){ |
| 23 |
#if __cplusplus >= 201103L |
| 24 |
using namespace std; |
| 25 |
auto now = chrono::steady_clock::now().time_since_epoch(); |
| 26 |
chrono::seconds sec = chrono::duration_cast<chrono::seconds>(now); |
| 27 |
|
| 28 |
time_value->tv_sec = sec.count(); |
| 29 |
time_value->tv_nsec = chrono::duration_cast<chrono::nanoseconds>(now - sec).count(); |
| 30 |
#else |
| 31 |
#warning "Using clock_gettime" |
| 32 |
// todo: use the glibc wrapper for clock_gettime once it's available |
| 33 |
// on all the platforms we care about, instead of issuing a syscall |
| 34 |
// directly like barbarians |
| 35 |
// |
| 36 |
// linking with -lrt is NOT an option because the runtime dependency |
| 37 |
// is passed on to ROOT, which often needs a static build of davix |
| 38 |
int ret = syscall(SYS_clock_gettime, CLOCK_MONOTONIC, time_value); |
| 39 |
if(ret < 0) { |
| 40 |
gettimeofday_timespec(time_value); |
| 41 |
} |
| 42 |
#endif |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
namespace A_LIB_NAMESPACE{ |
| 47 |
|
| 48 |
|
| 49 |
namespace Chrono { |
| 50 |
|
| 51 |
TimePoint::TimePoint() : t() |
| 52 |
{ |
| 53 |
t.tv_nsec =0; |
| 54 |
t.tv_sec =0; |
| 55 |
} |
| 56 |
|
| 57 |
TimePoint::~TimePoint(){ |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
bool TimePoint::operator ==(const TimePoint & other) const{ |
| 63 |
return ( (t.tv_nsec == other.t.tv_nsec) && (t.tv_sec == other.t.tv_sec)); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
bool TimePoint::operator<(const TimePoint & other) const{ |
| 68 |
return ( (t.tv_sec < other.t.tv_sec) |
| 69 |
|| (( t.tv_sec == other.t.tv_sec) && (t.tv_nsec < other.t.tv_nsec))); |
| 70 |
} |
| 71 |
|
| 72 |
TimePoint & TimePoint::operator+=(const Duration & duration){ |
| 73 |
t.tv_sec += duration.t.tv_sec; |
| 74 |
t.tv_nsec += duration.t.tv_nsec; |
| 75 |
return *this; |
| 76 |
} |
| 77 |
|
| 78 |
TimePoint & TimePoint::operator-=(const Duration & duration){ |
| 79 |
t.tv_sec -= duration.t.tv_sec; |
| 80 |
t.tv_nsec -= duration.t.tv_nsec; |
| 81 |
return *this; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
Duration TimePoint::operator-(const TimePoint & timepoint) const{ |
| 86 |
Duration d; |
| 87 |
if(*this < timepoint){ |
| 88 |
throw ChronoException("Negative duration between two TimePoint"); |
| 89 |
} |
| 90 |
d.t.tv_sec = (t.tv_sec - timepoint.t.tv_sec); |
| 91 |
d.t.tv_nsec = (t.tv_nsec - timepoint.t.tv_nsec); |
| 92 |
return d; |
| 93 |
} |
| 94 |
|
| 95 |
Type::UInt64 TimePoint::toTimestamp() const{ |
| 96 |
return static_cast<Type::UInt64>(t.tv_sec); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
bool TimePoint::isValid() const { |
| 101 |
return (t.tv_sec != 0 && t.tv_nsec != 0); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
//////////////////////////////// |
| 107 |
//////////////////////////////// |
| 108 |
|
| 109 |
Duration::Duration(){ |
| 110 |
t.tv_sec =0; |
| 111 |
t.tv_nsec =0; |
| 112 |
} |
| 113 |
|
| 114 |
Duration::Duration(Type::UInt64 seconds){ |
| 115 |
t.tv_nsec =0; |
| 116 |
t.tv_sec = seconds; |
| 117 |
} |
| 118 |
|
| 119 |
Duration::~Duration(){ |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
Duration Duration::milliseconds(Type::UInt64 v) { |
| 124 |
Duration d; |
| 125 |
d.t.tv_sec = v / 1000; |
| 126 |
d.t.tv_nsec = (v % 1000) * 1000; |
| 127 |
return d; |
| 128 |
} |
| 129 |
|
| 130 |
Type::UInt64 Duration::toTimeValue() const{ |
| 131 |
return t.tv_sec; |
| 132 |
} |
| 133 |
|
| 134 |
Type::UInt64 Duration::toMilliseconds() const { |
| 135 |
return (t.tv_sec * 1000) + (t.tv_nsec / 1000000); |
| 136 |
} |
| 137 |
|
| 138 |
/////////////////////////////// |
| 139 |
////////////////////////////// |
| 140 |
|
| 141 |
|
| 142 |
static void get_time(Clock::Type clock_type, struct timespec & time_value) { |
| 143 |
switch(clock_type) { |
| 144 |
case Clock::Monolitic: |
| 145 |
davix_get_monotonic_time(&time_value); |
| 146 |
break; |
| 147 |
default: |
| 148 |
gettimeofday_timespec(&time_value); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
Clock::Clock(Type clock_type, Precision tick) : _type(clock_type), _precision(tick){ |
| 154 |
(void) _precision; |
| 155 |
} |
| 156 |
|
| 157 |
Clock::~Clock(){ |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
TimePoint Clock::now() const{ |
| 162 |
TimePoint res; |
| 163 |
get_time(_type, res.t); |
| 164 |
return res; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
std::ostream& operator<<(std::ostream& os, const Duration & d){ |
| 169 |
os << d.t.tv_sec; |
| 170 |
return os; |
| 171 |
} |
| 172 |
|
| 173 |
std::ostream& operator<<(std::ostream& os, const TimePoint & t){ |
| 174 |
os << t.t.tv_sec; |
| 175 |
return os; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
} // namespace Chrono |
| 181 |
|
| 182 |
|
| 183 |
} |