/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 | 232k | CustomStopWatch() { |
37 | 232k | _total_time = 0; |
38 | 232k | _running = false; |
39 | 232k | } _ZN5doris15CustomStopWatchILi1EEC2Ev Line | Count | Source | 36 | 232k | CustomStopWatch() { | 37 | 232k | _total_time = 0; | 38 | 232k | _running = false; | 39 | 232k | } |
_ZN5doris15CustomStopWatchILi3EEC2Ev Line | Count | Source | 36 | 3 | CustomStopWatch() { | 37 | 3 | _total_time = 0; | 38 | 3 | _running = false; | 39 | 3 | } |
|
40 | | |
41 | 232k | void start() { |
42 | 232k | if (!_running) { |
43 | 232k | clock_gettime(Clock, &_start); |
44 | 232k | _running = true; |
45 | 232k | } |
46 | 232k | } _ZN5doris15CustomStopWatchILi1EE5startEv Line | Count | Source | 41 | 232k | void start() { | 42 | 232k | if (!_running) { | 43 | 232k | clock_gettime(Clock, &_start); | 44 | 232k | _running = true; | 45 | 232k | } | 46 | 232k | } |
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE5startEv |
47 | | |
48 | 787 | void stop() { |
49 | 787 | if (_running) { |
50 | 787 | _total_time += elapsed_time(); |
51 | 787 | _running = false; |
52 | 787 | } |
53 | 787 | } _ZN5doris15CustomStopWatchILi1EE4stopEv Line | Count | Source | 48 | 787 | void stop() { | 49 | 787 | if (_running) { | 50 | 787 | _total_time += elapsed_time(); | 51 | 787 | _running = false; | 52 | 787 | } | 53 | 787 | } |
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE4stopEv |
54 | | |
55 | | // Restarts the timer. Returns the elapsed time until this point. |
56 | 698 | uint64_t reset() { |
57 | 698 | uint64_t ret = elapsed_time(); |
58 | | |
59 | 698 | if (_running) { |
60 | 698 | clock_gettime(Clock, &_start); |
61 | 698 | } |
62 | | |
63 | 698 | return ret; |
64 | 698 | } _ZN5doris15CustomStopWatchILi1EE5resetEv Line | Count | Source | 56 | 698 | uint64_t reset() { | 57 | 698 | uint64_t ret = elapsed_time(); | 58 | | | 59 | 698 | if (_running) { | 60 | 698 | clock_gettime(Clock, &_start); | 61 | 698 | } | 62 | | | 63 | 698 | return ret; | 64 | 698 | } |
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE5resetEv |
65 | | |
66 | | // Returns time in nanosecond. |
67 | 234k | uint64_t elapsed_time() const { |
68 | 234k | if (!_running) { |
69 | 744 | return _total_time; |
70 | 744 | } |
71 | | |
72 | 234k | timespec end; |
73 | 234k | clock_gettime(Clock, &end); |
74 | 234k | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + |
75 | 234k | (end.tv_nsec - _start.tv_nsec); |
76 | 234k | } _ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv Line | Count | Source | 67 | 234k | uint64_t elapsed_time() const { | 68 | 234k | if (!_running) { | 69 | 744 | return _total_time; | 70 | 744 | } | 71 | | | 72 | 234k | timespec end; | 73 | 234k | clock_gettime(Clock, &end); | 74 | 234k | return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L + | 75 | 234k | (end.tv_nsec - _start.tv_nsec); | 76 | 234k | } |
Unexecuted instantiation: _ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv |
77 | | |
78 | | private: |
79 | | timespec _start; |
80 | | uint64_t _total_time; // in nanosec |
81 | | bool _running; |
82 | | }; |
83 | | |
84 | | // Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC. |
85 | | // It is as fast as Rdtsc. |
86 | | // It is also accurate because it not affected by cpu frequency changes and |
87 | | // it is not affected by user setting the system clock. |
88 | | // CLOCK_MONOTONIC represents monotonic time since some unspecified starting point. |
89 | | // It is good for computing elapsed time. |
90 | | using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>; |
91 | | |
92 | | // Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID. |
93 | | using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>; |
94 | | |
95 | | } // namespace doris |