LLVM  14.0.0git
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 namespace detail {
76 template <typename Period> struct unit { static const char value[]; };
77 template <typename Period> const char unit<Period>::value[] = "";
78 
79 template <> struct unit<std::ratio<3600>> { static const char value[]; };
80 template <> struct unit<std::ratio<60>> { static const char value[]; };
81 template <> struct unit<std::ratio<1>> { static const char value[]; };
82 template <> struct unit<std::milli> { static const char value[]; };
83 template <> struct unit<std::micro> { static const char value[]; };
84 template <> struct unit<std::nano> { static const char value[]; };
85 } // namespace detail
86 
87 /// Implementation of format_provider<T> for duration types.
88 ///
89 /// The options string of a duration type has the grammar:
90 ///
91 /// duration_options ::= [unit][show_unit [number_options]]
92 /// unit ::= `h`|`m`|`s`|`ms|`us`|`ns`
93 /// show_unit ::= `+` | `-`
94 /// number_options ::= options string for a integral or floating point type
95 ///
96 /// Examples
97 /// =================================
98 /// | options | Input | Output |
99 /// =================================
100 /// | "" | 1s | 1 s |
101 /// | "ms" | 1s | 1000 ms |
102 /// | "ms-" | 1s | 1000 |
103 /// | "ms-n" | 1s | 1,000 |
104 /// | "" | 1.0s | 1.00 s |
105 /// =================================
106 ///
107 /// If the unit of the duration type is not one of the units specified above,
108 /// it is still possible to format it, provided you explicitly request a
109 /// display unit or you request that the unit is not displayed.
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 std::conditional_t<std::chrono::treat_as_floating_point<Rep>::value,
116  double, intmax_t>
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 
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
llvm::detail::unit
Definition: Chrono.h:76
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
FormatProviders.h
llvm::format_provider
Definition: FormatVariadicDetails.h:18
T
double
into xmm2 addss xmm2 xmm1 xmm3 addss xmm3 movaps xmm0 unpcklps xmm0 ret seems silly when it could just be one addps Expand libm rounding functions main should enable SSE DAZ mode and other fast SSE modes Think about doing i64 math in SSE regs on x86 This testcase should have no SSE instructions in and only one load from a constant double
Definition: README-SSE.txt:85
test::unit
unit
Definition: README.txt:46
llvm::format_provider< std::chrono::duration< Rep, Period > >::format
static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style)
Definition: Chrono.h:154
llvm::sys::TimePoint
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1634
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
llvm::HexStyle::Style
Style
Definition: MCInstPrinter.h:32
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
Compiler.h
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::sys::toTimeT
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:36
std
Definition: BitVector.h:838
llvm::sys::toTimePoint
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:44
llvm::sys::operator<<
raw_ostream & operator<<(raw_ostream &OS, const Memory::ProtectionFlags &PF)
Debugging output for Memory::ProtectionFlags.
Definition: Memory.cpp:35