Coverage Report

Created: 2026-05-19 12:38

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
98.7M
    CustomStopWatch(bool auto_start = false) {
43
98.7M
        _total_time = 0;
44
98.7M
        _running = false;
45
98.7M
        if (auto_start) {
46
1.99k
            start();
47
1.99k
        }
48
98.7M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
42
92.5M
    CustomStopWatch(bool auto_start = false) {
43
92.5M
        _total_time = 0;
44
92.5M
        _running = false;
45
92.5M
        if (auto_start) {
46
1.99k
            start();
47
1.99k
        }
48
92.5M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
42
6.17M
    CustomStopWatch(bool auto_start = false) {
43
6.17M
        _total_time = 0;
44
6.17M
        _running = false;
45
6.17M
        if (auto_start) {
46
0
            start();
47
0
        }
48
6.17M
    }
49
50
    timespec start_time() const { return _start; }
51
52
102M
    void start() {
53
102M
        if (!_running) {
54
98.6M
            _record_start_thread_id();
55
98.6M
            clock_gettime(Clock, &_start);
56
98.6M
            _running = true;
57
98.6M
        }
58
102M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
52
96.7M
    void start() {
53
96.7M
        if (!_running) {
54
92.4M
            _record_start_thread_id();
55
92.4M
            clock_gettime(Clock, &_start);
56
92.4M
            _running = true;
57
92.4M
        }
58
96.7M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
52
6.21M
    void start() {
53
6.21M
        if (!_running) {
54
6.16M
            _record_start_thread_id();
55
6.16M
            clock_gettime(Clock, &_start);
56
6.16M
            _running = true;
57
6.16M
        }
58
6.21M
    }
59
60
32.2M
    void stop() {
61
32.2M
        if (_running) {
62
31.7M
            _total_time += elapsed_time();
63
31.7M
            _running = false;
64
31.7M
        }
65
32.2M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
60
28.2M
    void stop() {
61
28.2M
        if (_running) {
62
27.7M
            _total_time += elapsed_time();
63
27.7M
            _running = false;
64
27.7M
        }
65
28.2M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
60
3.96M
    void stop() {
61
3.96M
        if (_running) {
62
3.96M
            _total_time += elapsed_time();
63
3.96M
            _running = false;
64
3.96M
        }
65
3.96M
    }
66
67
    // Restarts the timer. Returns the elapsed time until this point.
68
5.73M
    int64_t reset() {
69
5.73M
        int64_t ret = elapsed_time();
70
71
5.73M
        if (_running) {
72
4.40M
            _record_start_thread_id();
73
4.40M
            clock_gettime(Clock, &_start);
74
4.40M
        }
75
76
5.73M
        return ret;
77
5.73M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
68
488k
    int64_t reset() {
69
488k
        int64_t ret = elapsed_time();
70
71
488k
        if (_running) {
72
49.3k
            _record_start_thread_id();
73
49.3k
            clock_gettime(Clock, &_start);
74
49.3k
        }
75
76
488k
        return ret;
77
488k
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
68
5.24M
    int64_t reset() {
69
5.24M
        int64_t ret = elapsed_time();
70
71
5.24M
        if (_running) {
72
4.35M
            _record_start_thread_id();
73
4.35M
            clock_gettime(Clock, &_start);
74
4.35M
        }
75
76
5.24M
        return ret;
77
5.24M
    }
78
79
    // Returns time in nanosecond.
80
    // Clamped to 0 to guard against rare CLOCK_MONOTONIC rollbacks.
81
132M
    int64_t elapsed_time() const {
82
132M
        if (!_running) {
83
32.2M
            return _total_time;
84
32.2M
        }
85
86
100M
        _check_thread_id();
87
100M
        timespec end;
88
100M
        clock_gettime(Clock, &end);
89
100M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
100M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
100M
        if (end_nanos < start_nanos) {
92
7.83k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
7.83k
            return 0;
94
7.83k
        }
95
100M
        return end_nanos - start_nanos;
96
100M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
81
122M
    int64_t elapsed_time() const {
82
122M
        if (!_running) {
83
27.8M
            return _total_time;
84
27.8M
        }
85
86
94.2M
        _check_thread_id();
87
94.2M
        timespec end;
88
94.2M
        clock_gettime(Clock, &end);
89
94.2M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
94.2M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
94.2M
        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
94.2M
        return end_nanos - start_nanos;
96
94.2M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
81
10.6M
    int64_t elapsed_time() const {
82
10.6M
        if (!_running) {
83
4.39M
            return _total_time;
84
4.39M
        }
85
86
6.22M
        _check_thread_id();
87
6.22M
        timespec end;
88
6.22M
        clock_gettime(Clock, &end);
89
6.22M
        auto start_nanos = _start.tv_sec * NANOS_PER_SEC + _start.tv_nsec;
90
6.22M
        auto end_nanos = end.tv_sec * NANOS_PER_SEC + end.tv_nsec;
91
6.22M
        if (end_nanos < start_nanos) {
92
7.83k
            LOG(INFO) << "WARNING: time went backwards from " << start_nanos << " to " << end_nanos;
93
7.83k
            return 0;
94
7.83k
        }
95
6.22M
        return end_nanos - start_nanos;
96
6.22M
    }
97
98
    // Return time in microseconds
99
2.29k
    int64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
100
101
    // Return time in milliseconds
102
64.1k
    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
13.1k
    int64_t elapsed_time_seconds(timespec end) const {
107
13.1k
        if (!_running) {
108
0
            return _total_time / 1000L / 1000L / 1000L;
109
0
        }
110
13.1k
        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
13.1k
        return end.tv_sec - _start.tv_sec;
117
13.1k
    }
118
119
private:
120
12.4M
    static std::string _get_thread_id() {
121
12.4M
        std::stringstream ss;
122
12.4M
        ss << std::this_thread::get_id();
123
12.4M
        return ss.str();
124
12.4M
    }
125
102M
    void _record_start_thread_id() {
126
102M
#ifndef NDEBUG
127
102M
        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
6.21M
            _start_thread_id = _get_thread_id();
131
6.21M
        }
132
102M
#endif
133
102M
    }
_ZN5doris15CustomStopWatchILi1EE23_record_start_thread_idEv
Line
Count
Source
125
96.6M
    void _record_start_thread_id() {
126
96.6M
#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
96.6M
#endif
133
96.6M
    }
_ZN5doris15CustomStopWatchILi3EE23_record_start_thread_idEv
Line
Count
Source
125
6.21M
    void _record_start_thread_id() {
126
6.21M
#ifndef NDEBUG
127
6.21M
        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
6.21M
            _start_thread_id = _get_thread_id();
131
6.21M
        }
132
6.21M
#endif
133
6.21M
    }
134
135
101M
    void _check_thread_id() const {
136
101M
#ifndef NDEBUG
137
101M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
6.26M
            auto current_thread_id = _get_thread_id();
139
6.26M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
16.1k
                             << " but stopped in thread " << current_thread_id;
142
16.1k
            }
143
6.26M
        }
144
101M
#endif
145
101M
    }
_ZNK5doris15CustomStopWatchILi1EE16_check_thread_idEv
Line
Count
Source
135
94.9M
    void _check_thread_id() const {
136
94.9M
#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
94.9M
#endif
145
94.9M
    }
_ZNK5doris15CustomStopWatchILi3EE16_check_thread_idEv
Line
Count
Source
135
6.26M
    void _check_thread_id() const {
136
6.26M
#ifndef NDEBUG
137
6.26M
        if constexpr (Clock == CLOCK_THREAD_CPUTIME_ID) {
138
6.26M
            auto current_thread_id = _get_thread_id();
139
6.26M
            if (current_thread_id != _start_thread_id) {
140
                LOG(WARNING) << "StopWatch started in thread " << _start_thread_id
141
16.1k
                             << " but stopped in thread " << current_thread_id;
142
16.1k
            }
143
6.26M
        }
144
6.26M
#endif
145
6.26M
    }
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