/root/doris/be/src/util/stopwatch.hpp
Line | Count | Source |
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 | 475M | CustomStopWatch() { |
37 | 475M | _total_time = 0; |
38 | 475M | _running = false; |
39 | 475M | } _ZN5doris15CustomStopWatchILi1EEC2Ev Line | Count | Source | 36 | 466M | CustomStopWatch() { | 37 | 466M | _total_time = 0; | 38 | 466M | _running = false; | 39 | 466M | } |
_ZN5doris15CustomStopWatchILi3EEC2Ev Line | Count | Source | 36 | 8.99M | CustomStopWatch() { | 37 | 8.99M | _total_time = 0; | 38 | 8.99M | _running = false; | 39 | 8.99M | } |
|
40 | | |
41 | 481M | void start() { |
42 | 481M | if (!_running) { |
43 | 480M | clock_gettime(Clock, &_start); |
44 | 480M | _running = true; |
45 | 480M | } |
46 | 481M | } _ZN5doris15CustomStopWatchILi1EE5startEv Line | Count | Source | 41 | 472M | void start() { | 42 | 472M | if (!_running) { | 43 | 471M | clock_gettime(Clock, &_start); | 44 | 471M | _running = true; | 45 | 471M | } | 46 | 472M | } |
_ZN5doris15CustomStopWatchILi3EE5startEv Line | Count | Source | 41 | 9.04M | void start() { | 42 | 9.04M | if (!_running) { | 43 | 9.00M | clock_gettime(Clock, &_start); | 44 | 9.00M | _running = true; | 45 | 9.00M | } | 46 | 9.04M | } |
|
47 | | |
48 | 80.8M | void stop() { |
49 | 80.8M | if (_running) { |
50 | 80.5M | _total_time += elapsed_time(); |
51 | 80.5M | _running = false; |
52 | 80.5M | } |
53 | 80.8M | } _ZN5doris15CustomStopWatchILi1EE4stopEv Line | Count | Source | 48 | 78.8M | void stop() { | 49 | 78.8M | if (_running) { | 50 | 78.5M | _total_time += elapsed_time(); | 51 | 78.5M | _running = false; | 52 | 78.5M | } | 53 | 78.8M | } |
_ZN5doris15CustomStopWatchILi3EE4stopEv Line | Count | Source | 48 | 2.00M | void stop() { | 49 | 2.00M | if (_running) { | 50 | 2.00M | _total_time += elapsed_time(); | 51 | 2.00M | _running = false; | 52 | 2.00M | } | 53 | 2.00M | } |
|
54 | | |
55 | | // Restarts the timer. Returns the elapsed time until this point. |
56 | 6.30M | uint64_t reset() { |
57 | 6.30M | uint64_t ret = elapsed_time(); |
58 | | |
59 | 6.30M | if (_running) { |
60 | 2.33M | clock_gettime(Clock, &_start); |
61 | 2.33M | } |
62 | | |
63 | 6.30M | return ret; |
64 | 6.30M | } _ZN5doris15CustomStopWatchILi1EE5resetEv Line | Count | Source | 56 | 4.28M | uint64_t reset() { | 57 | 4.28M | uint64_t ret = elapsed_time(); | 58 | | | 59 | 4.28M | if (_running) { | 60 | 2.30M | clock_gettime(Clock, &_start); | 61 | 2.30M | } | 62 | | | 63 | 4.28M | return ret; | 64 | 4.28M | } |
_ZN5doris15CustomStopWatchILi3EE5resetEv Line | Count | Source | 56 | 2.01M | uint64_t reset() { | 57 | 2.01M | uint64_t ret = elapsed_time(); | 58 | | | 59 | 2.01M | if (_running) { | 60 | 33.5k | clock_gettime(Clock, &_start); | 61 | 33.5k | } | 62 | | | 63 | 2.01M | return ret; | 64 | 2.01M | } |
|
65 | | |
66 | | // Returns time in nanosecond. |
67 | 558M | uint64_t elapsed_time() const { |
68 | 558M | if (!_running) { |
69 | 77.7M | return _total_time; |
70 | 77.7M | } |
71 | | |
72 | 480M | timespec end; |
73 | 480M | clock_gettime(Clock, &end); |
74 | 480M | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + |
75 | 480M | (end.tv_nsec - _start.tv_nsec); |
76 | 558M | } _ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv Line | Count | Source | 67 | 545M | uint64_t elapsed_time() const { | 68 | 545M | if (!_running) { | 69 | 73.7M | return _total_time; | 70 | 73.7M | } | 71 | | | 72 | 471M | timespec end; | 73 | 471M | clock_gettime(Clock, &end); | 74 | 471M | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + | 75 | 471M | (end.tv_nsec - _start.tv_nsec); | 76 | 545M | } |
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv Line | Count | Source | 67 | 13.0M | uint64_t elapsed_time() const { | 68 | 13.0M | if (!_running) { | 69 | 3.99M | return _total_time; | 70 | 3.99M | } | 71 | | | 72 | 9.10M | timespec end; | 73 | 9.10M | clock_gettime(Clock, &end); | 74 | 9.10M | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + | 75 | 9.10M | (end.tv_nsec - _start.tv_nsec); | 76 | 13.0M | } |
|
77 | | |
78 | | // Return time in microseconds |
79 | 1.13k | uint64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; } |
80 | | |
81 | | // Return time in milliseconds |
82 | 348 | 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 |