Coverage Report

Created: 2026-05-15 08:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
#include <ctime>
26
#include <sstream>
27
#include <thread>
28
29
#include "util/time.h"
30
31
namespace doris {
32
33
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
34
// It is as fast as Rdtsc.
35
// It is also accurate because it not affected by cpu frequency changes and
36
// it is not affected by user setting the system clock.
37
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
38
// It is good for computing elapsed time.
39
template <clockid_t Clock>
40
class CustomStopWatch {
41
public:
42
281M
    CustomStopWatch(bool auto_start = false) {
43
281M
        _total_time = 0;
44
281M
        _running = false;
45
281M
        if (auto_start) {
46
935
            start();
47
935
        }
48
281M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
42
261M
    CustomStopWatch(bool auto_start = false) {
43
261M
        _total_time = 0;
44
261M
        _running = false;
45
261M
        if (auto_start) {
46
935
            start();
47
935
        }
48
261M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
42
19.2M
    CustomStopWatch(bool auto_start = false) {
43
19.2M
        _total_time = 0;
44
19.2M
        _running = false;
45
19.2M
        if (auto_start) {
46
0
            start();
47
0
        }
48
19.2M
    }
49
50
    timespec start_time() const { return _start; }
51
52
292M
    void start() {
53
292M
        if (!_running) {
54
280M
            _record_start_thread_id();
55
280M
            clock_gettime(Clock, &_start);
56
280M
            _running = true;
57
280M
        }
58
292M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
52
273M
    void start() {
53
273M
        if (!_running) {
54
260M
            _record_start_thread_id();
55
260M
            clock_gettime(Clock, &_start);
56
260M
            _running = true;
57
260M
        }
58
273M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
52
19.2M
    void start() {
53
19.2M
        if (!_running) {
54
19.2M
            _record_start_thread_id();
55
19.2M
            clock_gettime(Clock, &_start);
56
19.2M
            _running = true;
57
19.2M
        }
58
19.2M
    }
59
60
105M
    void stop() {
61
105M
        if (_running) {
62
104M
            _total_time += elapsed_time();
63
104M
            _running = false;
64
104M
        }
65
105M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
60
93.7M
    void stop() {
61
93.7M
        if (_running) {
62
92.4M
            _total_time += elapsed_time();
63
92.4M
            _running = false;
64
92.4M
        }
65
93.7M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
60
11.7M
    void stop() {
61
11.7M
        if (_running) {
62
11.7M
            _total_time += elapsed_time();
63
11.7M
            _running = false;
64
11.7M
        }
65
11.7M
    }
66
67
    // Restarts the timer. Returns the elapsed time until this point.
68
14.9M
    int64_t reset() {
69
14.9M
        int64_t ret = elapsed_time();
70
71
14.9M
        if (_running) {
72
11.8M
            _record_start_thread_id();
73
11.8M
            clock_gettime(Clock, &_start);
74
11.8M
        }
75
76
14.9M
        return ret;
77
14.9M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
68
887k
    int64_t reset() {
69
887k
        int64_t ret = elapsed_time();
70
71
887k
        if (_running) {
72
50.9k
            _record_start_thread_id();
73
50.9k
            clock_gettime(Clock, &_start);
74
50.9k
        }
75
76
887k
        return ret;
77
887k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
68
14.1M
    int64_t reset() {
69
14.1M
        int64_t ret = elapsed_time();
70
71
14.1M
        if (_running) {
72
11.7M
            _record_start_thread_id();
73
11.7M
            clock_gettime(Clock, &_start);
74
11.7M
        }
75
76
14.1M
        return ret;
77
14.1M
    }
78
79
    // Returns time in nanosecond.
80
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
81
387M
    int64_t elapsed_time() const {
82
387M
        if (!_running) {
83
108M
            return _total_time;
84
108M
        }
85
86
279M
        _check_thread_id();
87
279M
        timespec end;
88
279M
        clock_gettime(Clock, &end);
89
279M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
279M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
279M
        if (end_nanos < start_nanos) {
92
12.3k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
12.3k
            return 0;
94
12.3k
        }
95
279M
        return end_nanos - start_nanos;
96
279M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
81
356M
    int64_t elapsed_time() const {
82
356M
        if (!_running) {
83
95.4M
            return _total_time;
84
95.4M
        }
85
86
260M
        _check_thread_id();
87
260M
        timespec end;
88
260M
        clock_gettime(Clock, &end);
89
260M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
260M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
260M
        if (end_nanos < start_nanos) {
92
0
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
0
            return 0;
94
0
        }
95
260M
        return end_nanos - start_nanos;
96
260M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
81
30.7M
    int64_t elapsed_time() const {
82
30.7M
        if (!_running) {
83
12.6M
            return _total_time;
84
12.6M
        }
85
86
18.1M
        _check_thread_id();
87
18.1M
        timespec end;
88
18.1M
        clock_gettime(Clock, &end);
89
18.1M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
18.1M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
18.1M
        if (end_nanos < start_nanos) {
92
12.3k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
12.3k
            return 0;
94
12.3k
        }
95
18.1M
        return end_nanos - start_nanos;
96
18.1M
    }
97
98
    // Return time in microseconds
99
268k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
100
101
    // Return time in milliseconds
102
298k
    int64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000 / 1000; }
103
104
    // Returns time in seconds.
105
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
106
59.3k
    int64_t elapsed_time_seconds(timespec end) const {
107
59.3k
        if (!_running) {
108
0
            return _total_time / 1000L / 1000L / 1000L;
109
0
        }
110
59.3k
        if (end.tv_sec < _start.tv_sec) {
111
25
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
112
25
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
113
25
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
114
25
            return 0;
115
25
        }
116
59.3k
        return end.tv_sec - _start.tv_sec;
117
59.3k
    }
118
119
private:
120
37.2M
    static std::string _get_thread_id() {
121
37.2M
        std::stringstream ss;
122
37.2M
        ss << std::this_thread::get_id();
123
37.2M
        return ss.str();
124
37.2M
    }
125
291M
    void _record_start_thread_id() {
126
291M
#ifndef NDEBUG
127
291M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
128
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
129
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
130
19.2M
            _start_thread_id = _get_thread_id();
131
19.2M
        }
132
291M
#endif
133
291M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
125
271M
    void _record_start_thread_id() {
126
271M
#ifndef NDEBUG
127
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
128
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
129
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
130
            _start_thread_id = _get_thread_id();
131
        }
132
271M
#endif
133
271M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
125
19.2M
    void _record_start_thread_id() {
126
19.2M
#ifndef NDEBUG
127
19.2M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
128
            // CLOCK_THREAD_CPUTIME_ID is not supported on some platforms, e.g. macOS.
129
            // So that we need to check it at runtime and fallback to CLOCK_MONOTONIC if it is not supported.
130
19.2M
            _start_thread_id = _get_thread_id();
131
19.2M
        }
132
19.2M
#endif
133
19.2M
    }
134
135
282M
    void _check_thread_id() const {
136
282M
#ifndef NDEBUG
137
282M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
18.2M
            auto current_thread_id = _get_thread_id();
139
18.2M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
24.8k
                             << " but stopped in thread " << current_thread_id;
142
24.8k
            }
143
18.2M
        }
144
282M
#endif
145
282M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
135
264M
    void _check_thread_id() const {
136
264M
#ifndef NDEBUG
137
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
            auto current_thread_id = _get_thread_id();
139
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
                             << " but stopped in thread " << current_thread_id;
142
            }
143
        }
144
264M
#endif
145
264M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
135
18.2M
    void _check_thread_id() const {
136
18.2M
#ifndef NDEBUG
137
18.2M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
18.2M
            auto current_thread_id = _get_thread_id();
139
18.2M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
24.8k
                             << " but stopped in thread " << current_thread_id;
142
24.8k
            }
143
18.2M
        }
144
18.2M
#endif
145
18.2M
    }
146
147
    timespec _start;
148
    int64_t _total_time; // in nanosec
149
    bool _running;
150
#ifndef NDEBUG
151
    std::string _start_thread_id;
152
#endif
153
};
154
155
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
156
// It is as fast as Rdtsc.
157
// It is also accurate because it not affected by cpu frequency changes and
158
// it is not affected by user setting the system clock.
159
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
160
// It is good for computing elapsed time.
161
using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>;
162
163
// Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID.
164
using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>;
165
166
} // namespace doris