/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 | 26 | #define NANOS_PER_SEC 1000000000ll | 
| 29 | 0 | #define NANOS_PER_MILLIS 1000000ll | 
| 30 | 744k | #define NANOS_PER_MICRO 1000ll | 
| 31 | 748k | #define MICROS_PER_SEC 1000000ll | 
| 32 | 628k | #define MICROS_PER_MILLI 1000ll | 
| 33 | 298 | #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 | 26 | inline int64_t MonotonicNanos() { | 
| 43 | 26 |     timespec ts; | 
| 44 | 26 |     clock_gettime(CLOCK_MONOTONIC, &ts); | 
| 45 | 26 |     return ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; | Line | Count | Source |  | 28 | 26 | #define NANOS_PER_SEC 1000000000ll | 
 | 
| 46 | 26 | } | 
| 47 |  |  | 
| 48 | 111k | inline int64_t GetMonoTimeMicros() { | 
| 49 | 111k |     timespec ts; | 
| 50 | 111k |     clock_gettime(CLOCK_MONOTONIC, &ts); | 
| 51 | 111k |     return ts.tv_sec * MICROS_PER_SEC + ts.tv_nsec / NANOS_PER_MICRO; | Line | Count | Source |  | 31 | 111k | #define MICROS_PER_SEC 1000000ll | 
 |     return ts.tv_sec * MICROS_PER_SEC + ts.tv_nsec / NANOS_PER_MICRO; | Line | Count | Source |  | 30 | 111k | #define NANOS_PER_MICRO 1000ll | 
 | 
| 52 | 111k | } | 
| 53 |  |  | 
| 54 | 111k | inline int64_t MonotonicMicros() { // 63 bits ~= 5K years uptime | 
| 55 | 111k |     return GetMonoTimeMicros(); | 
| 56 | 111k | } | 
| 57 |  |  | 
| 58 | 95 | inline int64_t MonotonicMillis() { | 
| 59 | 95 |     return GetMonoTimeMicros() / MICROS_PER_MILLI; | Line | Count | Source |  | 32 | 95 | #define MICROS_PER_MILLI 1000ll | 
 | 
| 60 | 95 | } | 
| 61 |  |  | 
| 62 | 323 | inline int64_t MonotonicSeconds() { | 
| 63 | 323 |     return GetMonoTimeMicros() / MICROS_PER_SEC; | Line | Count | Source |  | 31 | 323 | #define MICROS_PER_SEC 1000000ll | 
 | 
| 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 | 633k | inline int64_t GetCurrentTimeMicros() { | 
| 72 | 633k |     timespec ts; | 
| 73 | 633k |     clock_gettime(CLOCK_REALTIME, &ts); | 
| 74 | 633k |     return ts.tv_sec * MICROS_PER_SEC + ts.tv_nsec / NANOS_PER_MICRO; | Line | Count | Source |  | 31 | 633k | #define MICROS_PER_SEC 1000000ll | 
 |     return ts.tv_sec * MICROS_PER_SEC + ts.tv_nsec / NANOS_PER_MICRO; | Line | Count | Source |  | 30 | 633k | #define NANOS_PER_MICRO 1000ll | 
 | 
| 75 | 633k | } | 
| 76 |  |  | 
| 77 |  | // Returns the time since the Epoch measured in nanoseconds. | 
| 78 | 0 | inline int64_t GetCurrentTimeNanos() { | 
| 79 | 0 |     timespec ts; | 
| 80 | 0 |     clock_gettime(CLOCK_REALTIME, &ts); | 
| 81 | 0 |     return ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; | Line | Count | Source |  | 28 | 0 | #define NANOS_PER_SEC 1000000000ll | 
 | 
| 82 | 0 | } | 
| 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 | 628k | inline int64_t UnixMillis() { | 
| 89 | 628k |     return GetCurrentTimeMicros() / MICROS_PER_MILLI; | Line | Count | Source |  | 32 | 628k | #define MICROS_PER_MILLI 1000ll | 
 | 
| 90 | 628k | } | 
| 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 | 2.61k | inline int64_t UnixSeconds() { | 
| 97 | 2.61k |     return GetCurrentTimeMicros() / MICROS_PER_SEC; | Line | Count | Source |  | 31 | 2.61k | #define MICROS_PER_SEC 1000000ll | 
 | 
| 98 | 2.61k | } | 
| 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 |