Line data Source code
1 : #pragma once 2 : 3 : // This component provides wrappers around some `std::chrono` vocabulary types: 4 : // `Duration` for an interval of time, and `TimePoint` for an instant in time. 5 : // 6 : // Each `Span` has a start time and a duration. The start time ought to be 7 : // measured using a system clock, so that Network Time Protocol adjustments and 8 : // other time settings are accurately reflected in the span start time. The 9 : // span's duration, however, is better measured using a steady (monotonic) clock 10 : // so that adjustments to the system clock made during the extent of the span do 11 : // not skew the span's measured duration. 12 : // 13 : // `Duration` is an alias for `std::chrono::steady_clock::duration`, while 14 : // `struct TimePoint` contains two `time_point` values: one from the system 15 : // clock, used for the start time, and another from the steady (monotonic) 16 : // clock, used for determining span duration. 17 : // 18 : // `Clock` is an alias for `std::function<TimePoint()>`, and the default 19 : // `Clock`, `default_clock`, gives a `TimePoint` using the 20 : // `std::chrono::system_clock` and `std::chrono::steady_clock`. 21 : 22 : #include <chrono> 23 : #include <functional> 24 : 25 : namespace datadog { 26 : namespace tracing { 27 : 28 : using Duration = std::chrono::steady_clock::duration; 29 : 30 : struct TimePoint { 31 : std::chrono::system_clock::time_point wall = 32 : std::chrono::system_clock::time_point(); 33 : std::chrono::steady_clock::time_point tick = 34 : std::chrono::steady_clock::time_point(); 35 : }; 36 : 37 82915 : inline Duration operator-(const TimePoint& after, const TimePoint& before) { 38 82915 : return after.tick - before.tick; 39 : } 40 : 41 1 : inline TimePoint operator-(const TimePoint& origin, Duration offset) { 42 1 : return {origin.wall - 43 2 : std::chrono::duration_cast<std::chrono::system_clock::duration>( 44 1 : offset), 45 2 : origin.tick - offset}; 46 : } 47 : 48 13 : inline TimePoint& operator+=(TimePoint& self, Duration offset) { 49 : self.wall += 50 13 : std::chrono::duration_cast<std::chrono::system_clock::duration>(offset); 51 13 : self.tick += offset; 52 13 : return self; 53 : } 54 : 55 : using Clock = std::function<TimePoint()>; 56 : 57 : extern const Clock default_clock; 58 : 59 : } // namespace tracing 60 : } // namespace datadog