ClockService.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "ClockService.hh"
18 //#include "ClockService.ih"
19 
20 // Custom includes
21 #include <boost/regex.hpp>
23 
24 // this include needed to add dependency on Scheduler. This should really be in
25 // ClockService.cci which is not possible due to recursive includes ...
27 
28 #define prefix_
29 
30 #ifdef SENF_DEBUG
31 # define SENF_CLOCKTYPEVAL(clock) (clock).value()
32 #else
33 # define SENF_CLOCKTYPEVAL(clock) (clock)
34 #endif
35 
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37 
38 template <class T> static senf::ClockService::clock_type
39 parseClockServiceInterval__(std::string const & value)
40 {
42 
43  static boost::sregex_iterator::regex_type rx ("[mun]?[dhms]");
44  boost::sregex_iterator i (value.begin(), value.end(), rx);
45  boost::sregex_iterator const i_end;
46  std::string::const_iterator j (value.begin());
47  for (; i != i_end; ++i) {
48  boost::sregex_iterator::value_type::value_type match ((*i)[0]);
49  T v (boost::lexical_cast<T>(std::string(j, match.first)));
50  char scale (0);
51  char unit (0);
52  if (match.length() == 2) {
53  scale = *match.first;
54  unit = *boost::next(match.first);
55  } else {
56  SENF_ASSERT( match.length() == 1,
57  "Internal failure: RegEx match returns weird number of matches" );
58  unit = *match.first;
59  }
60  switch (unit) {
61  case 'd': v *= 24; // fall through
62  case 'h': v *= 60; // fall through
63  case 'm': v *= 60; // fall through
64  case 's': v *= 1000000000;
65  }
66  switch (scale) {
67  case 0: break;
68  case 'n': v /= 1000; // fall through
69  case 'u': v /= 1000; // fall through
70  case 'm': v /= 1000;
71  }
73  j = match.second;
74  }
75  if (j != value.end())
77 
78  return out;
79 }
80 
81 prefix_ void
84 {
86  std::string value;
87  {
89  console::parse( *(arg++), value );
90  }
91 
92  // only use the double parser for float values
93  if (value.find('.') != std::string::npos) {
94  out = parseClockServiceInterval__<long double>(value);
95  } else {
96  out = parseClockServiceInterval__<ClockService::int64_type>(value);
97  }
98 }
99 
100 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type const & time)
101 {
102  if (scheduler::now() - baseClock_ > clock_type(1000000000ll))
103  restart_m();
104  boost::posix_time::time_duration delta (time - baseAbstime_);
105  return baseClock_ + clock_type( delta.ticks() )
106  * clock_type( 1000000000UL / boost::posix_time::time_duration::ticks_per_second() );
107 }
108 
109 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_type const & clock)
110 {
111  if (clock == clock_type(0))
112  return abstime_type();
113  if (scheduler::now() - baseClock_ > clock_type(1000000000ll))
114  restart_m();
115 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
116  return baseAbstime_ + boost::posix_time::nanoseconds(
117  SENF_CLOCKTYPEVAL( clock-baseClock_));
118 #else
119  return baseAbstime_ + boost::posix_time::microseconds(
120  SENF_CLOCKTYPEVAL( (clock-baseClock_+clock_type(500))/1000));
121 #endif
122  return abstime_type();
123 }
124 
126  std::ostream & os)
127 {
128  os << interval << "ns";
129 }
130 
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 #undef prefix_
133 //#include "ClockService.mpp"
134 
135 
136 // Local Variables:
137 // mode: c++
138 // fill-column: 100
139 // comment-column: 40
140 // c-file-style: "senf"
141 // indent-tabs-mode: nil
142 // ispell-local-dictionary: "american"
143 // compile-command: "scons -u test"
144 // End:
config::time_type clock_type
ClockService timer data type.
Definition: ClockService.hh:78
Scheduler public header.
ClockService::clock_type const & now()
Return (approximate) current time.
#define SENF_CLOCKTYPEVAL(clock)
Definition: ClockService.cc:33
void formatClockServiceInterval(ClockService::clock_type interval, std::ostream &os)
std::int64_t int64_type
Supplementary integer type.
Definition: ClockService.hh:84
#define SENF_ASSERT(x, comment)
boost::iterator_range< token_iterator > TokensRange
boost::posix_time::ptime abstime_type
Absolute time data type.
Definition: ClockService.hh:91
ClockService public header.
void parseClockServiceInterval(console::ParseCommandInfo::TokensRange const &tokens, ClockService::clock_type &out)
Console argument parser to parse value as time interval.
Definition: ClockService.cc:82
#define prefix_
Definition: ClockService.cc:28
__s8 delta
static clock_type clock(abstime_type const &time)
Convert absolute time to clock value.
static SENF_CLOCKSERVICE_CONSTEXPR clock_type nanoseconds(int64_type const &v)
Convert v nanoseconds to clock_type.