Coverage Report

Created: 2026-05-17 18:25

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
5.87M
    CustomStopWatch(bool auto_start = false) {
43
5.87M
        _total_time = 0;
44
5.87M
        _running = false;
45
5.87M
        if (auto_start) {
46
0
            start();
47
0
        }
48
5.87M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
42
5.87M
    CustomStopWatch(bool auto_start = false) {
43
5.87M
        _total_time = 0;
44
5.87M
        _running = false;
45
5.87M
        if (auto_start) {
46
0
            start();
47
0
        }
48
5.87M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
42
8.05k
    CustomStopWatch(bool auto_start = false) {
43
8.05k
        _total_time = 0;
44
8.05k
        _running = false;
45
8.05k
        if (auto_start) {
46
0
            start();
47
0
        }
48
8.05k
    }
49
50
    timespec start_time() const { return _start; }
51
52
5.25M
    void start() {
53
5.25M
        if (!_running) {
54
5.24M
            _record_start_thread_id();
55
5.24M
            clock_gettime(Clock, &_start);
56
5.24M
            _running = true;
57
5.24M
        }
58
5.25M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
52
5.24M
    void start() {
53
5.24M
        if (!_running) {
54
5.23M
            _record_start_thread_id();
55
5.23M
            clock_gettime(Clock, &_start);
56
5.23M
            _running = true;
57
5.23M
        }
58
5.24M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
52
8.07k
    void start() {
53
8.07k
        if (!_running) {
54
8.02k
            _record_start_thread_id();
55
8.02k
            clock_gettime(Clock, &_start);
56
8.02k
            _running = true;
57
8.02k
        }
58
8.07k
    }
59
60
2.74M
    void stop() {
61
2.74M
        if (_running) {
62
2.69M
            _total_time += elapsed_time();
63
2.69M
            _running = false;
64
2.69M
        }
65
2.74M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
60
2.74M
    void stop() {
61
2.74M
        if (_running) {
62
2.69M
            _total_time += elapsed_time();
63
2.69M
            _running = false;
64
2.69M
        }
65
2.74M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
60
5.28k
    void stop() {
61
5.28k
        if (_running) {
62
5.28k
            _total_time += elapsed_time();
63
5.28k
            _running = false;
64
5.28k
        }
65
5.28k
    }
66
67
    // Restarts the timer. Returns the elapsed time until this point.
68
36.5k
    int64_t reset() {
69
36.5k
        int64_t ret = elapsed_time();
70
71
36.5k
        if (_running) {
72
35.5k
            _record_start_thread_id();
73
35.5k
            clock_gettime(Clock, &_start);
74
35.5k
        }
75
76
36.5k
        return ret;
77
36.5k
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
68
230
    int64_t reset() {
69
230
        int64_t ret = elapsed_time();
70
71
230
        if (_running) {
72
54
            _record_start_thread_id();
73
54
            clock_gettime(Clock, &_start);
74
54
        }
75
76
230
        return ret;
77
230
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
68
36.3k
    int64_t reset() {
69
36.3k
        int64_t ret = elapsed_time();
70
71
36.3k
        if (_running) {
72
35.4k
            _record_start_thread_id();
73
35.4k
            clock_gettime(Clock, &_start);
74
35.4k
        }
75
76
36.3k
        return ret;
77
36.3k
    }
78
79
    // Returns time in nanosecond.
80
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
81
7.91M
    int64_t elapsed_time() const {
82
7.91M
        if (!_running) {
83
2.79M
            return _total_time;
84
2.79M
        }
85
86
5.11M
        _check_thread_id();
87
5.11M
        timespec end;
88
5.11M
        clock_gettime(Clock, &end);
89
5.11M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
5.11M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
5.11M
        if (end_nanos < start_nanos) {
92
2
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
2
            return 0;
94
2
        }
95
5.11M
        return end_nanos - start_nanos;
96
5.11M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
81
7.89M
    int64_t elapsed_time() const {
82
7.89M
        if (!_running) {
83
2.79M
            return _total_time;
84
2.79M
        }
85
86
5.10M
        _check_thread_id();
87
5.10M
        timespec end;
88
5.10M
        clock_gettime(Clock, &end);
89
5.10M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
5.10M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
5.10M
        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
5.10M
        return end_nanos - start_nanos;
96
5.10M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
81
13.5k
    int64_t elapsed_time() const {
82
13.5k
        if (!_running) {
83
5.45k
            return _total_time;
84
5.45k
        }
85
86
8.11k
        _check_thread_id();
87
8.11k
        timespec end;
88
8.11k
        clock_gettime(Clock, &end);
89
8.11k
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
8.11k
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
8.11k
        if (end_nanos < start_nanos) {
92
2
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
2
            return 0;
94
2
        }
95
8.11k
        return end_nanos - start_nanos;
96
8.11k
    }
97
98
    // Return time in microseconds
99
22
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
100
101
    // Return time in milliseconds
102
6.39k
    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
28
    int64_t elapsed_time_seconds(timespec end) const {
107
28
        if (!_running) {
108
0
            return _total_time / 1000L / 1000L / 1000L;
109
0
        }
110
28
        if (end.tv_sec < _start.tv_sec) {
111
0
            auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
112
0
            auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
113
0
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
114
0
            return 0;
115
0
        }
116
28
        return end.tv_sec - _start.tv_sec;
117
28
    }
118
119
private:
120
16.1k
    static std::string _get_thread_id() {
121
16.1k
        std::stringstream ss;
122
16.1k
        ss << std::this_thread::get_id();
123
16.1k
        return ss.str();
124
16.1k
    }
125
5.28M
    void _record_start_thread_id() {
126
5.28M
#ifndef NDEBUG
127
5.28M
        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
8.07k
            _start_thread_id = _get_thread_id();
131
8.07k
        }
132
5.28M
#endif
133
5.28M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
125
5.27M
    void _record_start_thread_id() {
126
5.27M
#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
5.27M
#endif
133
5.27M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
125
8.07k
    void _record_start_thread_id() {
126
8.07k
#ifndef NDEBUG
127
8.07k
        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
8.07k
            _start_thread_id = _get_thread_id();
131
8.07k
        }
132
8.07k
#endif
133
8.07k
    }
134
135
5.11M
    void _check_thread_id() const {
136
5.11M
#ifndef NDEBUG
137
5.11M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
8.14k
            auto current_thread_id = _get_thread_id();
139
8.14k
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
4
                             << " but stopped in thread " << current_thread_id;
142
4
            }
143
8.14k
        }
144
5.11M
#endif
145
5.11M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
135
5.10M
    void _check_thread_id() const {
136
5.10M
#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
5.10M
#endif
145
5.10M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
135
8.14k
    void _check_thread_id() const {
136
8.14k
#ifndef NDEBUG
137
8.14k
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
8.14k
            auto current_thread_id = _get_thread_id();
139
8.14k
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
4
                             << " but stopped in thread " << current_thread_id;
142
4
            }
143
8.14k
        }
144
8.14k
#endif
145
8.14k
    }
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