Home | History | Annotate | Download | only in Support
      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"
     14 #include "llvm/Support/FormatProviders.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
     37 LLVM_ATTRIBUTE_ALWAYS_INLINE inline std::time_t toTimeT(TimePoint<> TP) {
     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
     44 LLVM_ATTRIBUTE_ALWAYS_INLINE inline TimePoint<std::chrono::seconds>
     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 /// Format provider for TimePoint<>
     55 ///
     56 /// The options string is a strftime format string, with extensions:
     57 ///   - %L is millis: 000-999
     58 ///   - %f is micros: 000000-999999
     59 ///   - %N is nanos: 000000000 - 999999999
     60 ///
     61 /// If no options are given, the default format is "%Y-%m-%d %H:%M:%S.%N".
     62 template <>
     63 struct format_provider<sys::TimePoint<>> {
     64   static void format(const sys::TimePoint<> &TP, llvm::raw_ostream &OS,
     65                      StringRef Style);
     66 };
     67 
     68 /// Implementation of format_provider<T> for duration types.
     69 ///
     70 /// The options string of a duration  type has the grammar:
     71 ///
     72 ///   duration_options  ::= [unit][show_unit [number_options]]
     73 ///   unit              ::= `h`|`m`|`s`|`ms|`us`|`ns`
     74 ///   show_unit         ::= `+` | `-`
     75 ///   number_options    ::= options string for a integral or floating point type
     76 ///
     77 ///   Examples
     78 ///   =================================
     79 ///   |  options  | Input | Output    |
     80 ///   =================================
     81 ///   | ""        | 1s    | 1 s       |
     82 ///   | "ms"      | 1s    | 1000 ms   |
     83 ///   | "ms-"     | 1s    | 1000      |
     84 ///   | "ms-n"    | 1s    | 1,000     |
     85 ///   | ""        | 1.0s  | 1.00 s    |
     86 ///   =================================
     87 ///
     88 ///  If the unit of the duration type is not one of the units specified above,
     89 ///  it is still possible to format it, provided you explicitly request a
     90 ///  display unit or you request that the unit is not displayed.
     91 
     92 namespace detail {
     93 template <typename Period> struct unit { static const char value[]; };
     94 template <typename Period> const char unit<Period>::value[] = "";
     95 
     96 template <> struct unit<std::ratio<3600>> { static const char value[]; };
     97 template <> struct unit<std::ratio<60>> { static const char value[]; };
     98 template <> struct unit<std::ratio<1>> { static const char value[]; };
     99 template <> struct unit<std::milli> { static const char value[]; };
    100 template <> struct unit<std::micro> { static const char value[]; };
    101 template <> struct unit<std::nano> { static const char value[]; };
    102 } // namespace detail
    103 
    104 template <typename Rep, typename Period>
    105 struct format_provider<std::chrono::duration<Rep, Period>> {
    106 private:
    107   typedef std::chrono::duration<Rep, Period> Dur;
    108   typedef typename std::conditional<
    109       std::chrono::treat_as_floating_point<Rep>::value, double, intmax_t>::type
    110       InternalRep;
    111 
    112   template <typename AsPeriod> static InternalRep getAs(const Dur &D) {
    113     using namespace std::chrono;
    114     return duration_cast<duration<InternalRep, AsPeriod>>(D).count();
    115   }
    116 
    117   static std::pair<InternalRep, StringRef> consumeUnit(StringRef &Style,
    118                                                         const Dur &D) {
    119     using namespace std::chrono;
    120     if (Style.consume_front("ns"))
    121       return {getAs<std::nano>(D), "ns"};
    122     if (Style.consume_front("us"))
    123       return {getAs<std::micro>(D), "us"};
    124     if (Style.consume_front("ms"))
    125       return {getAs<std::milli>(D), "ms"};
    126     if (Style.consume_front("s"))
    127       return {getAs<std::ratio<1>>(D), "s"};
    128     if (Style.consume_front("m"))
    129       return {getAs<std::ratio<60>>(D), "m"};
    130     if (Style.consume_front("h"))
    131       return {getAs<std::ratio<3600>>(D), "h"};
    132     return {D.count(), detail::unit<Period>::value};
    133   }
    134 
    135   static bool consumeShowUnit(StringRef &Style) {
    136     if (Style.empty())
    137       return true;
    138     if (Style.consume_front("-"))
    139       return false;
    140     if (Style.consume_front("+"))
    141       return true;
    142     assert(0 && "Unrecognised duration format");
    143     return true;
    144   }
    145 
    146 public:
    147   static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style) {
    148     InternalRep count;
    149     StringRef unit;
    150     std::tie(count, unit) = consumeUnit(Style, D);
    151     bool show_unit = consumeShowUnit(Style);
    152 
    153     format_provider<InternalRep>::format(count, Stream, Style);
    154 
    155     if (show_unit) {
    156       assert(!unit.empty());
    157       Stream << " " << unit;
    158     }
    159   }
    160 };
    161 
    162 } // namespace llvm
    163 
    164 #endif // LLVM_SUPPORT_CHRONO_H
    165