/root/doris/be/src/util/stopwatch.hpp
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/stopwatch.hpp |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <time.h> |
24 | | |
25 | | namespace doris { |
26 | | |
27 | | // Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC. |
28 | | // It is as fast as Rdtsc. |
29 | | // It is also accurate because it not affected by cpu frequency changes and |
30 | | // it is not affected by user setting the system clock. |
31 | | // CLOCK_MONOTONIC represents monotonic time since some unspecified starting point. |
32 | | // It is good for computing elapsed time. |
33 | | template <clockid_t Clock> |
34 | | class CustomStopWatch { |
35 | | public: |
36 | 302k | CustomStopWatch() { |
37 | 302k | _total_time = 0; |
38 | 302k | _running = false; |
39 | 302k | } _ZN5doris15CustomStopWatchILi1EEC2Ev Line | Count | Source | 36 | 302k | CustomStopWatch() { | 37 | 302k | _total_time = 0; | 38 | 302k | _running = false; | 39 | 302k | } |
_ZN5doris15CustomStopWatchILi3EEC2Ev Line | Count | Source | 36 | 3 | CustomStopWatch() { | 37 | 3 | _total_time = 0; | 38 | 3 | _running = false; | 39 | 3 | } |
|
40 | | |
41 | 302k | void start() { |
42 | 302k | if (!_running) { |
43 | 302k | clock_gettime(Clock, &_start); |
44 | 302k | _running = true; |
45 | 302k | } |
46 | 302k | } _ZN5doris15CustomStopWatchILi1EE5startEv Line | Count | Source | 41 | 302k | void start() { | 42 | 302k | if (!_running) { | 43 | 302k | clock_gettime(Clock, &_start); | 44 | 302k | _running = true; | 45 | 302k | } | 46 | 302k | } |
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE5startEv |
47 | | |
48 | 1.60k | void stop() { |
49 | 1.60k | if (_running) { |
50 | 1.60k | _total_time += elapsed_time(); |
51 | 1.60k | _running = false; |
52 | 1.60k | } |
53 | 1.60k | } _ZN5doris15CustomStopWatchILi1EE4stopEv Line | Count | Source | 48 | 1.60k | void stop() { | 49 | 1.60k | if (_running) { | 50 | 1.60k | _total_time += elapsed_time(); | 51 | 1.60k | _running = false; | 52 | 1.60k | } | 53 | 1.60k | } |
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE4stopEv |
54 | | |
55 | | // Restarts the timer. Returns the elapsed time until this point. |
56 | 2.95k | uint64_t reset() { |
57 | 2.95k | uint64_t ret = elapsed_time(); |
58 | | |
59 | 2.95k | if (_running) { |
60 | 2.95k | clock_gettime(Clock, &_start); |
61 | 2.95k | } |
62 | | |
63 | 2.95k | return ret; |
64 | 2.95k | } _ZN5doris15CustomStopWatchILi1EE5resetEv Line | Count | Source | 56 | 2.95k | uint64_t reset() { | 57 | 2.95k | uint64_t ret = elapsed_time(); | 58 | | | 59 | 2.95k | if (_running) { | 60 | 2.95k | clock_gettime(Clock, &_start); | 61 | 2.95k | } | 62 | | | 63 | 2.95k | return ret; | 64 | 2.95k | } |
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE5resetEv |
65 | | |
66 | | // Returns time in nanosecond. |
67 | 307k | uint64_t elapsed_time() const { |
68 | 307k | if (!_running) { |
69 | 1.56k | return _total_time; |
70 | 1.56k | } |
71 | | |
72 | 305k | timespec end; |
73 | 305k | clock_gettime(Clock, &end); |
74 | 305k | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + |
75 | 305k | (end.tv_nsec - _start.tv_nsec); |
76 | 307k | } _ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv Line | Count | Source | 67 | 307k | uint64_t elapsed_time() const { | 68 | 307k | if (!_running) { | 69 | 1.56k | return _total_time; | 70 | 1.56k | } | 71 | | | 72 | 305k | timespec end; | 73 | 305k | clock_gettime(Clock, &end); | 74 | 305k | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + | 75 | 305k | (end.tv_nsec - _start.tv_nsec); | 76 | 307k | } |
Unexecuted instantiation: _ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv |
77 | | |
78 | | // Return time in microseconds |
79 | 0 | uint64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; } |
80 | | |
81 | | // Return time in milliseconds |
82 | 136 | uint64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000 / 1000; } |
83 | | |
84 | | // Returns time in nanosecond. |
85 | | int64_t elapsed_time_seconds(timespec end) const { |
86 | | if (!_running) { |
87 | | return _total_time / 1000L / 1000L / 1000L; |
88 | | } |
89 | | return end.tv_sec - _start.tv_sec; |
90 | | } |
91 | | |
92 | | private: |
93 | | timespec _start; |
94 | | uint64_t _total_time; // in nanosec |
95 | | bool _running; |
96 | | }; |
97 | | |
98 | | // Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC. |
99 | | // It is as fast as Rdtsc. |
100 | | // It is also accurate because it not affected by cpu frequency changes and |
101 | | // it is not affected by user setting the system clock. |
102 | | // CLOCK_MONOTONIC represents monotonic time since some unspecified starting point. |
103 | | // It is good for computing elapsed time. |
104 | | using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>; |
105 | | |
106 | | // Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID. |
107 | | using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>; |
108 | | |
109 | | } // namespace doris |