/root/doris/be/src/util/time.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/time.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <stdint.h> |
24 | | #include <time.h> |
25 | | |
26 | | #include <string> |
27 | | |
28 | | #define NANOS_PER_SEC 1000000000ll |
29 | | #define NANOS_PER_MILLIS 1000000ll |
30 | 553k | #define NANOS_PER_MICRO 1000ll |
31 | 554k | #define MICROS_PER_SEC 1000000ll |
32 | 546k | #define MICROS_PER_MILLI 1000ll |
33 | | #define MILLIS_PER_SEC 1000ll |
34 | | |
35 | | /// Utilities for collecting timings. |
36 | | namespace doris { |
37 | | |
38 | | /// Returns a value representing a point in time that is unaffected by daylight savings or |
39 | | /// manual adjustments to the system clock. This should not be assumed to be a Unix |
40 | | /// time. Typically the value corresponds to elapsed time since the system booted. See |
41 | | /// UnixMillis() below if you need to send a time to a different host. |
42 | | inline int64_t MonotonicNanos() { |
43 | | timespec ts; |
44 | | clock_gettime(CLOCK_MONOTONIC, &ts); |
45 | | return ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
46 | | } |
47 | | |
48 | 4.85k | inline int64_t GetMonoTimeMicros() { |
49 | 4.85k | timespec ts; |
50 | 4.85k | clock_gettime(CLOCK_MONOTONIC, &ts); |
51 | 4.85k | return ts.tv_sec * MICROS_PER_SEC + ts.tv_nsec / NANOS_PER_MICRO; |
52 | 4.85k | } |
53 | | |
54 | | inline int64_t MonotonicMicros() { // 63 bits ~= 5K years uptime |
55 | | return GetMonoTimeMicros(); |
56 | | } |
57 | | |
58 | | inline int64_t MonotonicMillis() { |
59 | | return GetMonoTimeMicros() / MICROS_PER_MILLI; |
60 | | } |
61 | | |
62 | 323 | inline int64_t MonotonicSeconds() { |
63 | 323 | return GetMonoTimeMicros() / MICROS_PER_SEC; |
64 | 323 | } |
65 | | |
66 | 0 | inline double GetMonoTimeSecondsAsDouble() { |
67 | 0 | return GetMonoTimeMicros() / static_cast<double>(MICROS_PER_SEC); |
68 | 0 | } |
69 | | |
70 | | // Returns the time since the Epoch measured in microseconds. |
71 | 548k | inline int64_t GetCurrentTimeMicros() { |
72 | 548k | timespec ts; |
73 | 548k | clock_gettime(CLOCK_REALTIME, &ts); |
74 | 548k | return ts.tv_sec * MICROS_PER_SEC + ts.tv_nsec / NANOS_PER_MICRO; |
75 | 548k | } |
76 | | |
77 | | // Returns the time since the Epoch measured in nanoseconds. |
78 | | inline int64_t GetCurrentTimeNanos() { |
79 | | timespec ts; |
80 | | clock_gettime(CLOCK_REALTIME, &ts); |
81 | | return ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
82 | | } |
83 | | |
84 | | /// Returns the number of milliseconds that have passed since the Unix epoch. This is |
85 | | /// affected by manual changes to the system clock but is more suitable for use across |
86 | | /// a cluster. For more accurate timings on the local host use the monotonic functions |
87 | | /// above. |
88 | 546k | inline int64_t UnixMillis() { |
89 | 546k | return GetCurrentTimeMicros() / MICROS_PER_MILLI; |
90 | 546k | } |
91 | | |
92 | | /// Returns the number of seconds that have passed since the Unix epoch. This is |
93 | | /// affected by manual changes to the system clock but is more suitable for use across |
94 | | /// a cluster. For more accurate timings on the local host use the monotonic functions |
95 | | /// above. |
96 | 1.15k | inline int64_t UnixSeconds() { |
97 | 1.15k | return GetCurrentTimeMicros() / MICROS_PER_SEC; |
98 | 1.15k | } |
99 | | |
100 | | /// Returns the number of microseconds that have passed since the Unix epoch. This is |
101 | | /// affected by manual changes to the system clock but is more suitable for use across |
102 | | /// a cluster. For more accurate timings on the local host use the monotonic functions |
103 | | /// above. |
104 | 0 | inline int64_t UnixMicros() { |
105 | 0 | return GetCurrentTimeMicros(); |
106 | 0 | } |
107 | | |
108 | | /// Sleeps the current thread for at least duration_ms milliseconds. |
109 | | void SleepForMs(const int64_t duration_ms); |
110 | | |
111 | | // An enum class to use as precision argument for the ToString*() functions below |
112 | | enum TimePrecision { Second, Millisecond, Microsecond, Nanosecond }; |
113 | | |
114 | | /// Converts the input Unix time, 's', specified in seconds since the Unix epoch, to a |
115 | | /// date-time string in the local time zone. The precision in the output date-time string |
116 | | /// is specified by the second argument, 'p'. The returned string is of the format |
117 | | /// yyyy-MM-dd HH:mm:SS[.ms[us[ns]]. It's worth noting that if the precision specified |
118 | | /// by 'p' is higher than that of the input timestamp, the part corresponding to |
119 | | /// 'p' in the fractional second part of the output will just be zero-padded. |
120 | | std::string ToStringFromUnix(int64_t s, TimePrecision p = TimePrecision::Second); |
121 | | |
122 | | /// Converts input seconds-since-epoch to date-time string in UTC time zone. |
123 | | std::string ToUtcStringFromUnix(int64_t s, TimePrecision p = TimePrecision::Second); |
124 | | |
125 | | /// Converts input milliseconds-since-epoch to date-time string in local time zone. |
126 | | std::string ToStringFromUnixMillis(int64_t ms, TimePrecision p = TimePrecision::Millisecond); |
127 | | |
128 | | /// Converts input milliseconds-since-epoch to date-time string in UTC time zone. |
129 | | std::string ToUtcStringFromUnixMillis(int64_t ms, TimePrecision p = TimePrecision::Millisecond); |
130 | | |
131 | | /// Converts input microseconds-since-epoch to date-time string in local time zone. |
132 | | std::string ToStringFromUnixMicros(int64_t us, TimePrecision p = TimePrecision::Microsecond); |
133 | | |
134 | | /// Converts input microseconds-since-epoch to date-time string in UTC time zone. |
135 | | std::string ToUtcStringFromUnixMicros(int64_t us, TimePrecision p = TimePrecision::Microsecond); |
136 | | |
137 | | } // namespace doris |