Coverage Report

Created: 2026-07-30 07:32

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