Coverage Report

Created: 2026-08-24 12:12

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
13.4M
    CustomStopWatch(bool auto_start = false) {
44
13.4M
        _total_time = 0;
45
13.4M
        _running = false;
46
13.4M
        if (auto_start) {
47
25
            start();
48
25
        }
49
13.4M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
43
13.3M
    CustomStopWatch(bool auto_start = false) {
44
13.3M
        _total_time = 0;
45
13.3M
        _running = false;
46
13.3M
        if (auto_start) {
47
0
            start();
48
0
        }
49
13.3M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
43
151k
    CustomStopWatch(bool auto_start = false) {
44
151k
        _total_time = 0;
45
151k
        _running = false;
46
151k
        if (auto_start) {
47
25
            start();
48
25
        }
49
151k
    }
50
51
    timespec start_time() const { return _start; }
52
53
12.9M
    void start() {
54
12.9M
        if (!_running) {
55
12.8M
            _record_start_thread_id();
56
12.8M
            clock_gettime(Clock, &_start);
57
12.8M
            _running = true;
58
12.8M
        }
59
12.9M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
53
12.8M
    void start() {
54
12.8M
        if (!_running) {
55
12.7M
            _record_start_thread_id();
56
12.7M
            clock_gettime(Clock, &_start);
57
12.7M
            _running = true;
58
12.7M
        }
59
12.8M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
53
151k
    void start() {
54
151k
        if (!_running) {
55
151k
            _record_start_thread_id();
56
151k
            clock_gettime(Clock, &_start);
57
151k
            _running = true;
58
151k
        }
59
151k
    }
60
61
3.24M
    void stop() {
62
3.24M
        if (_running) {
63
3.19M
            _total_time += elapsed_time();
64
3.19M
            _running = false;
65
3.19M
        }
66
3.24M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
61
3.16M
    void stop() {
62
3.16M
        if (_running) {
63
3.10M
            _total_time += elapsed_time();
64
3.10M
            _running = false;
65
3.10M
        }
66
3.16M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
61
87.8k
    void stop() {
62
87.8k
        if (_running) {
63
87.8k
            _total_time += elapsed_time();
64
87.8k
            _running = false;
65
87.8k
        }
66
87.8k
    }
67
68
    // Restarts the timer. Returns the elapsed time until this point.
69
441k
    int64_t reset() {
70
441k
        int64_t ret = elapsed_time();
71
72
441k
        if (_running) {
73
397k
            _record_start_thread_id();
74
397k
            clock_gettime(Clock, &_start);
75
397k
        }
76
77
441k
        return ret;
78
441k
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
69
19.9k
    int64_t reset() {
70
19.9k
        int64_t ret = elapsed_time();
71
72
19.9k
        if (_running) {
73
78
            _record_start_thread_id();
74
78
            clock_gettime(Clock, &_start);
75
78
        }
76
77
19.9k
        return ret;
78
19.9k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
69
422k
    int64_t reset() {
70
422k
        int64_t ret = elapsed_time();
71
72
422k
        if (_running) {
73
397k
            _record_start_thread_id();
74
397k
            clock_gettime(Clock, &_start);
75
397k
        }
76
77
422k
        return ret;
78
422k
    }
79
80
    // Returns time in nanosecond.
81
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
82
15.3M
    int64_t elapsed_time() const {
83
15.3M
        if (!_running) {
84
3.27M
            return _total_time;
85
3.27M
        }
86
87
12.1M
        _check_thread_id();
88
12.1M
        timespec end;
89
12.1M
        clock_gettime(Clock, &end);
90
12.1M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
12.1M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
12.1M
        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
12.1M
        return end_nanos - start_nanos;
98
12.1M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
82
15.1M
    int64_t elapsed_time() const {
83
15.1M
        if (!_running) {
84
3.16M
            return _total_time;
85
3.16M
        }
86
87
11.9M
        _check_thread_id();
88
11.9M
        timespec end;
89
11.9M
        clock_gettime(Clock, &end);
90
11.9M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
11.9M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
11.9M
        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
11.9M
        return end_nanos - start_nanos;
98
11.9M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
82
258k
    int64_t elapsed_time() const {
83
258k
        if (!_running) {
84
107k
            return _total_time;
85
107k
        }
86
87
151k
        _check_thread_id();
88
151k
        timespec end;
89
151k
        clock_gettime(Clock, &end);
90
151k
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
91
151k
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
92
151k
        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
151k
        return end_nanos - start_nanos;
98
151k
    }
99
100
    // Return time in microseconds
101
0
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
102
103
    // Return time in milliseconds
104
12.0k
    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
96
    int64_t elapsed_time_seconds(timespec end) const {
109
96
        if (!_running) {
110
0
            return _total_time / 1000L / 1000L / 1000L;
111
0
        }
112
96
        if (end.tv_sec < _start.tv_sec) {
113
0
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
114
0
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
115
0
            LOG_EVERY_T(WARNING, 1)
116
0
                    << "time went backwards from " << start_nanos << " to " << end_nanos;
117
0
            return 0;
118
0
        }
119
96
        return end.tv_sec - _start.tv_sec;
120
96
    }
121
122
private:
123
302k
    static std::string _get_thread_id() {
124
302k
        std::stringstream ss;
125
302k
        ss << std::this_thread::get_id();
126
302k
        return ss.str();
127
302k
    }
128
13.2M
    void _record_start_thread_id() {
129
13.2M
#ifndef NDEBUG
130
13.2M
        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
151k
            _start_thread_id = _get_thread_id();
134
151k
        }
135
13.2M
#endif
136
13.2M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
128
13.1M
    void _record_start_thread_id() {
129
13.1M
#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
13.1M
#endif
136
13.1M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
128
151k
    void _record_start_thread_id() {
129
151k
#ifndef NDEBUG
130
151k
        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
151k
            _start_thread_id = _get_thread_id();
134
151k
        }
135
151k
#endif
136
151k
    }
137
138
12.1M
    void _check_thread_id() const {
139
12.1M
#ifndef NDEBUG
140
12.1M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
151k
            auto current_thread_id = _get_thread_id();
142
151k
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
0
                             << " but stopped in thread " << current_thread_id;
145
0
            }
146
151k
        }
147
12.1M
#endif
148
12.1M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
138
11.9M
    void _check_thread_id() const {
139
11.9M
#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
11.9M
#endif
148
11.9M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
138
151k
    void _check_thread_id() const {
139
151k
#ifndef NDEBUG
140
151k
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
141
151k
            auto current_thread_id = _get_thread_id();
142
151k
            if (current_thread_id != _start_thread_id) {
143
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
144
0
                             << " but stopped in thread " << current_thread_id;
145
0
            }
146
151k
        }
147
151k
#endif
148
151k
    }
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