LLVM  5.0.0
Chrono.h
Go to the documentation of this file.
1 //===- llvm/Support/Chrono.h - Utilities for Timing Manipulation-*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_CHRONO_H
11 #define LLVM_SUPPORT_CHRONO_H
12 
13 #include "llvm/Support/Compiler.h"
15 
16 #include <chrono>
17 #include <ctime>
18 
19 namespace llvm {
20 
21 class raw_ostream;
22 
23 namespace sys {
24 
25 /// A time point on the system clock. This is provided for two reasons:
26 /// - to insulate us agains subtle differences in behavoir to differences in
27 /// system clock precision (which is implementation-defined and differs between
28 /// platforms).
29 /// - to shorten the type name
30 /// The default precision is nanoseconds. If need a specific precision specify
31 /// it explicitly. If unsure, use the default. If you need a time point on a
32 /// clock other than the system_clock, use std::chrono directly.
33 template <typename D = std::chrono::nanoseconds>
34 using TimePoint = std::chrono::time_point<std::chrono::system_clock, D>;
35 
36 /// Convert a TimePoint to std::time_t
38  using namespace std::chrono;
39  return system_clock::to_time_t(
40  time_point_cast<system_clock::time_point::duration>(TP));
41 }
42 
43 /// Convert a std::time_t to a TimePoint
45 toTimePoint(std::time_t T) {
46  using namespace std::chrono;
47  return time_point_cast<seconds>(system_clock::from_time_t(T));
48 }
49 
50 } // namespace sys
51 
52 raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP);
53 
54 /// Implementation of format_provider<T> for duration types.
55 ///
56 /// The options string of a duration type has the grammar:
57 ///
58 /// duration_options ::= [unit][show_unit [number_options]]
59 /// unit ::= `h`|`m`|`s`|`ms|`us`|`ns`
60 /// show_unit ::= `+` | `-`
61 /// number_options ::= options string for a integral or floating point type
62 ///
63 /// Examples
64 /// =================================
65 /// | options | Input | Output |
66 /// =================================
67 /// | "" | 1s | 1 s |
68 /// | "ms" | 1s | 1000 ms |
69 /// | "ms-" | 1s | 1000 |
70 /// | "ms-n" | 1s | 1,000 |
71 /// | "" | 1.0s | 1.00 s |
72 /// =================================
73 ///
74 /// If the unit of the duration type is not one of the units specified above,
75 /// it is still possible to format it, provided you explicitly request a
76 /// display unit or you request that the unit is not displayed.
77 
78 namespace detail {
79 template <typename Period> struct unit { static const char value[]; };
80 template <typename Period> const char unit<Period>::value[] = "";
81 
82 template <> struct unit<std::ratio<3600>> { static const char value[]; };
83 template <> struct unit<std::ratio<60>> { static const char value[]; };
84 template <> struct unit<std::ratio<1>> { static const char value[]; };
85 template <> struct unit<std::milli> { static const char value[]; };
86 template <> struct unit<std::micro> { static const char value[]; };
87 template <> struct unit<std::nano> { static const char value[]; };
88 } // namespace detail
89 
90 template <typename Rep, typename Period>
91 struct format_provider<std::chrono::duration<Rep, Period>> {
92 private:
93  typedef std::chrono::duration<Rep, Period> Dur;
94  typedef typename std::conditional<
95  std::chrono::treat_as_floating_point<Rep>::value, double, intmax_t>::type
96  InternalRep;
97 
98  template <typename AsPeriod> static InternalRep getAs(const Dur &D) {
99  using namespace std::chrono;
100  return duration_cast<duration<InternalRep, AsPeriod>>(D).count();
101  }
102 
103  static std::pair<InternalRep, StringRef> consumeUnit(StringRef &Style,
104  const Dur &D) {
105  using namespace std::chrono;
106  if (Style.consume_front("ns"))
107  return {getAs<std::nano>(D), "ns"};
108  if (Style.consume_front("us"))
109  return {getAs<std::micro>(D), "us"};
110  if (Style.consume_front("ms"))
111  return {getAs<std::milli>(D), "ms"};
112  if (Style.consume_front("s"))
113  return {getAs<std::ratio<1>>(D), "s"};
114  if (Style.consume_front("m"))
115  return {getAs<std::ratio<60>>(D), "m"};
116  if (Style.consume_front("h"))
117  return {getAs<std::ratio<3600>>(D), "h"};
118  return {D.count(), detail::unit<Period>::value};
119  }
120 
121  static bool consumeShowUnit(StringRef &Style) {
122  if (Style.empty())
123  return true;
124  if (Style.consume_front("-"))
125  return false;
126  if (Style.consume_front("+"))
127  return true;
128  assert(0 && "Unrecognised duration format");
129  return true;
130  }
131 
132 public:
133  static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style) {
134  InternalRep count;
135  StringRef unit;
136  std::tie(count, unit) = consumeUnit(Style, D);
137  bool show_unit = consumeShowUnit(Style);
138 
139  format_provider<InternalRep>::format(count, Stream, Style);
140 
141  if (show_unit) {
142  assert(!unit.empty());
143  Stream << " " << unit;
144  }
145  }
146 };
147 
148 } // namespace llvm
149 
150 #endif // LLVM_SUPPORT_CHRONO_H
LLVM_ATTRIBUTE_ALWAYS_INLINE std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:37
static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style)
Definition: Chrono.h:133
LLVM_ATTRIBUTE_ALWAYS_INLINE TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:45
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do so, mark a method "always...
Definition: Compiler.h:198
LLVM_ATTRIBUTE_ALWAYS_INLINE bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:678
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::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:880
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
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:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define D
Definition: LargeTest.cpp:26
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:34