Coverage Report

Created: 2026-03-31 08:33

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
namespace doris {
26
27
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
28
// It is as fast as Rdtsc.
29
// It is also accurate because it not affected by cpu frequency changes and
30
// it is not affected by user setting the system clock.
31
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
32
// It is good for computing elapsed time.
33
template <clockid_t Clock>
34
class CustomStopWatch {
35
public:
36
590M
    CustomStopWatch(bool auto_start = false) {
37
590M
        _total_time = 0;
38
590M
        _running = false;
39
590M
        if (auto_start) {
40
2.71k
            start();
41
2.71k
        }
42
590M
    }
_ZN5doris15CustomStopWatchILi1EEC2Eb
Line
Count
Source
36
561M
    CustomStopWatch(bool auto_start = false) {
37
561M
        _total_time = 0;
38
561M
        _running = false;
39
561M
        if (auto_start) {
40
2.71k
            start();
41
2.71k
        }
42
561M
    }
_ZN5doris15CustomStopWatchILi3EEC2Eb
Line
Count
Source
36
28.7M
    CustomStopWatch(bool auto_start = false) {
37
28.7M
        _total_time = 0;
38
28.7M
        _running = false;
39
28.7M
        if (auto_start) {
40
0
            start();
41
0
        }
42
28.7M
    }
43
44
    timespec start_time() const { return _start; }
45
46
606M
    void start() {
47
606M
        if (!_running) {
48
590M
            clock_gettime(Clock, &_start);
49
590M
            _running = true;
50
590M
        }
51
606M
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
46
577M
    void start() {
47
577M
        if (!_running) {
48
561M
            clock_gettime(Clock, &_start);
49
561M
            _running = true;
50
561M
        }
51
577M
    }
_ZN5doris15CustomStopWatchILi3EE5startEv
Line
Count
Source
46
28.7M
    void start() {
47
28.7M
        if (!_running) {
48
28.7M
            clock_gettime(Clock, &_start);
49
28.7M
            _running = true;
50
28.7M
        }
51
28.7M
    }
52
53
326M
    void stop() {
54
326M
        if (_running) {
55
324M
            _total_time += elapsed_time();
56
324M
            _running = false;
57
324M
        }
58
326M
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
53
309M
    void stop() {
54
309M
        if (_running) {
55
307M
            _total_time += elapsed_time();
56
307M
            _running = false;
57
307M
        }
58
309M
    }
_ZN5doris15CustomStopWatchILi3EE4stopEv
Line
Count
Source
53
16.7M
    void stop() {
54
16.7M
        if (_running) {
55
16.7M
            _total_time += elapsed_time();
56
16.7M
            _running = false;
57
16.7M
        }
58
16.7M
    }
59
60
    // Restarts the timer. Returns the elapsed time until this point.
61
19.9M
    uint64_t reset() {
62
19.9M
        uint64_t ret = elapsed_time();
63
64
19.9M
        if (_running) {
65
15.3M
            clock_gettime(Clock, &_start);
66
15.3M
        }
67
68
19.9M
        return ret;
69
19.9M
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
61
18.7M
    uint64_t reset() {
62
18.7M
        uint64_t ret = elapsed_time();
63
64
18.7M
        if (_running) {
65
15.3M
            clock_gettime(Clock, &_start);
66
15.3M
        }
67
68
18.7M
        return ret;
69
18.7M
    }
_ZN5doris15CustomStopWatchILi3EE5resetEv
Line
Count
Source
61
1.26M
    uint64_t reset() {
62
1.26M
        uint64_t ret = elapsed_time();
63
64
1.26M
        if (_running) {
65
10.6k
            clock_gettime(Clock, &_start);
66
10.6k
        }
67
68
1.26M
        return ret;
69
1.26M
    }
70
71
    // Returns time in nanosecond.
72
918M
    uint64_t elapsed_time() const {
73
918M
        if (!_running) {
74
328M
            return _total_time;
75
328M
        }
76
77
590M
        timespec end;
78
590M
        clock_gettime(Clock, &end);
79
590M
        return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L +
80
590M
               (end.tv_nsec - _start.tv_nsec);
81
918M
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
72
875M
    uint64_t elapsed_time() const {
73
875M
        if (!_running) {
74
310M
            return _total_time;
75
310M
        }
76
77
564M
        timespec end;
78
564M
        clock_gettime(Clock, &end);
79
564M
        return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L +
80
564M
               (end.tv_nsec - _start.tv_nsec);
81
875M
    }
_ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
Line
Count
Source
72
43.5M
    uint64_t elapsed_time() const {
73
43.5M
        if (!_running) {
74
17.9M
            return _total_time;
75
17.9M
        }
76
77
25.5M
        timespec end;
78
25.5M
        clock_gettime(Clock, &end);
79
25.5M
        return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L +
80
25.5M
               (end.tv_nsec - _start.tv_nsec);
81
43.5M
    }
82
83
    // Return time in microseconds
84
326k
    uint64_t elapsed_time_microseconds() const { return elapsed_time() / 1000; }
85
86
    // Return time in milliseconds
87
411k
    uint64_t elapsed_time_milliseconds() const { return elapsed_time() / 1000 / 1000; }
88
89
    // Returns time in nanosecond.
90
67.9k
    int64_t elapsed_time_seconds(timespec end) const {
91
67.9k
        if (!_running) {
92
0
            return _total_time / 1000L / 1000L / 1000L;
93
0
        }
94
67.9k
        return end.tv_sec - _start.tv_sec;
95
67.9k
    }
96
97
private:
98
    timespec _start;
99
    uint64_t _total_time; // in nanosec
100
    bool _running;
101
};
102
103
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
104
// It is as fast as Rdtsc.
105
// It is also accurate because it not affected by cpu frequency changes and
106
// it is not affected by user setting the system clock.
107
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
108
// It is good for computing elapsed time.
109
using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>;
110
111
// Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID.
112
using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>;
113
114
} // namespace doris