LLVM  9.0.0svn
Chrono.h
Go to the documentation of this file.
1 //===- llvm/Support/Chrono.h - Utilities for Timing Manipulation-*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_SUPPORT_CHRONO_H
10 #define LLVM_SUPPORT_CHRONO_H
11 
12 #include "llvm/Support/Compiler.h"
14 
15 #include <chrono>
16 #include <ctime>
17 
18 namespace llvm {
19 
20 class raw_ostream;
21 
22 namespace sys {
23 
24 /// A time point on the system clock. This is provided for two reasons:
25 /// - to insulate us agains subtle differences in behavoir to differences in
26 /// system clock precision (which is implementation-defined and differs between
27 /// platforms).
28 /// - to shorten the type name
29 /// The default precision is nanoseconds. If need a specific precision specify
30 /// it explicitly. If unsure, use the default. If you need a time point on a
31 /// clock other than the system_clock, use std::chrono directly.
32 template <typename D = std::chrono::nanoseconds>
33 using TimePoint = std::chrono::time_point<std::chrono::system_clock, D>;
34 
35 /// Convert a TimePoint to std::time_t
36 inline std::time_t toTimeT(TimePoint<> TP) {
37  using namespace std::chrono;
38  return system_clock::to_time_t(
39  time_point_cast<system_clock::time_point::duration>(TP));
40 }
41 
42 /// Convert a std::time_t to a TimePoint
44 toTimePoint(std::time_t T) {
45  using namespace std::chrono;
46  return time_point_cast<seconds>(system_clock::from_time_t(T));
47 }
48 
49 /// Convert a std::time_t + nanoseconds to a TimePoint
50 inline TimePoint<>
51 toTimePoint(std::time_t T, uint32_t nsec) {
52  using namespace std::chrono;
53  return time_point_cast<nanoseconds>(system_clock::from_time_t(T))
54  + nanoseconds(nsec);
55 }
56 
57 } // namespace sys
58 
59 raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP);
60 
61 /// Format provider for TimePoint<>
62 ///
63 /// The options string is a strftime format string, with extensions:
64 /// - %L is millis: 000-999
65 /// - %f is micros: 000000-999999
66 /// - %N is nanos: 000000000 - 999999999
67 ///
68 /// If no options are given, the default format is "%Y-%m-%d %H:%M:%S.%N".
69 template <>
70 struct format_provider<sys::TimePoint<>> {
71  static void format(const sys::TimePoint<> &TP, llvm::raw_ostream &OS,
73 };
74 
75 /// Implementation of format_provider<T> for duration types.
76 ///
77 /// The options string of a duration type has the grammar:
78 ///
79 /// duration_options ::= [unit][show_unit [number_options]]
80 /// unit ::= `h`|`m`|`s`|`ms|`us`|`ns`
81 /// show_unit ::= `+` | `-`
82 /// number_options ::= options string for a integral or floating point type
83 ///
84 /// Examples
85 /// =================================
86 /// | options | Input | Output |
87 /// =================================
88 /// | "" | 1s | 1 s |
89 /// | "ms" | 1s | 1000 ms |
90 /// | "ms-" | 1s | 1000 |
91 /// | "ms-n" | 1s | 1,000 |
92 /// | "" | 1.0s | 1.00 s |
93 /// =================================
94 ///
95 /// If the unit of the duration type is not one of the units specified above,
96 /// it is still possible to format it, provided you explicitly request a
97 /// display unit or you request that the unit is not displayed.
98 
99 namespace detail {
100 template <typename Period> struct unit { static const char value[]; };
101 template <typename Period> const char unit<Period>::value[] = "";
102 
103 template <> struct unit<std::ratio<3600>> { static const char value[]; };
104 template <> struct unit<std::ratio<60>> { static const char value[]; };
105 template <> struct unit<std::ratio<1>> { static const char value[]; };
106 template <> struct unit<std::milli> { static const char value[]; };
107 template <> struct unit<std::micro> { static const char value[]; };
108 template <> struct unit<std::nano> { static const char value[]; };
109 } // namespace detail
110 
111 template <typename Rep, typename Period>
112 struct format_provider<std::chrono::duration<Rep, Period>> {
113 private:
114  typedef std::chrono::duration<Rep, Period> Dur;
115  typedef typename std::conditional<
116  std::chrono::treat_as_floating_point<Rep>::value, double, intmax_t>::type
117  InternalRep;
118 
119  template <typename AsPeriod> static InternalRep getAs(const Dur &D) {
120  using namespace std::chrono;
121  return duration_cast<duration<InternalRep, AsPeriod>>(D).count();
122  }
123 
124  static std::pair<InternalRep, StringRef> consumeUnit(StringRef &Style,
125  const Dur &D) {
126  using namespace std::chrono;
127  if (Style.consume_front("ns"))
128  return {getAs<std::nano>(D), "ns"};
129  if (Style.consume_front("us"))
130  return {getAs<std::micro>(D), "us"};
131  if (Style.consume_front("ms"))
132  return {getAs<std::milli>(D), "ms"};
133  if (Style.consume_front("s"))
134  return {getAs<std::ratio<1>>(D), "s"};
135  if (Style.consume_front("m"))
136  return {getAs<std::ratio<60>>(D), "m"};
137  if (Style.consume_front("h"))
138  return {getAs<std::ratio<3600>>(D), "h"};
139  return {D.count(), detail::unit<Period>::value};
140  }
141 
142  static bool consumeShowUnit(StringRef &Style) {
143  if (Style.empty())
144  return true;
145  if (Style.consume_front("-"))
146  return false;
147  if (Style.consume_front("+"))
148  return true;
149  assert(0 && "Unrecognised duration format");
150  return true;
151  }
152 
153 public:
154  static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style) {
155  InternalRep count;
156  StringRef unit;
157  std::tie(count, unit) = consumeUnit(Style, D);
158  bool show_unit = consumeShowUnit(Style);
159 
160  format_provider<InternalRep>::format(count, Stream, Style);
161 
162  if (show_unit) {
163  assert(!unit.empty());
164  Stream << " " << unit;
165  }
166  }
167 };
168 
169 } // namespace llvm
170 
171 #endif // LLVM_SUPPORT_CHRONO_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:649
static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style)
Definition: Chrono.h:154
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:123
Definition: BitVector.h:937
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1258
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:36
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:44
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33