Coverage Report

Created: 2024-11-22 16:10

/root/doris/be/src/util/stopwatch.hpp
Line
Count
Source (jump to first uncovered line)
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
234k
    CustomStopWatch() {
37
234k
        _total_time = 0;
38
234k
        _running = false;
39
234k
    }
_ZN5doris15CustomStopWatchILi1EEC2Ev
Line
Count
Source
36
234k
    CustomStopWatch() {
37
234k
        _total_time = 0;
38
234k
        _running = false;
39
234k
    }
_ZN5doris15CustomStopWatchILi3EEC2Ev
Line
Count
Source
36
4
    CustomStopWatch() {
37
4
        _total_time = 0;
38
4
        _running = false;
39
4
    }
40
41
    timespec start_time() const { return _start; }
42
43
234k
    void start() {
44
234k
        if (!_running) {
45
234k
            clock_gettime(Clock, &_start);
46
234k
            _running = true;
47
234k
        }
48
234k
    }
_ZN5doris15CustomStopWatchILi1EE5startEv
Line
Count
Source
43
234k
    void start() {
44
234k
        if (!_running) {
45
234k
            clock_gettime(Clock, &_start);
46
234k
            _running = true;
47
234k
        }
48
234k
    }
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE5startEv
49
50
675
    void stop() {
51
675
        if (_running) {
52
675
            _total_time += elapsed_time();
53
675
            _running = false;
54
675
        }
55
675
    }
_ZN5doris15CustomStopWatchILi1EE4stopEv
Line
Count
Source
50
675
    void stop() {
51
675
        if (_running) {
52
675
            _total_time += elapsed_time();
53
675
            _running = false;
54
675
        }
55
675
    }
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE4stopEv
56
57
    // Restarts the timer. Returns the elapsed time until this point.
58
918
    uint64_t reset() {
59
918
        uint64_t ret = elapsed_time();
60
61
918
        if (_running) {
62
918
            clock_gettime(Clock, &_start);
63
918
        }
64
65
918
        return ret;
66
918
    }
_ZN5doris15CustomStopWatchILi1EE5resetEv
Line
Count
Source
58
918
    uint64_t reset() {
59
918
        uint64_t ret = elapsed_time();
60
61
918
        if (_running) {
62
918
            clock_gettime(Clock, &_start);
63
918
        }
64
65
918
        return ret;
66
918
    }
Unexecuted instantiation: _ZN5doris15CustomStopWatchILi3EE5resetEv
67
68
    // Returns time in nanosecond.
69
236k
    uint64_t elapsed_time() const {
70
236k
        if (!_running) {
71
630
            return _total_time;
72
630
        }
73
74
236k
        timespec end;
75
236k
        clock_gettime(Clock, &end);
76
236k
        return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L +
77
236k
               (end.tv_nsec - _start.tv_nsec);
78
236k
    }
_ZNK5doris15CustomStopWatchILi1EE12elapsed_timeEv
Line
Count
Source
69
236k
    uint64_t elapsed_time() const {
70
236k
        if (!_running) {
71
630
            return _total_time;
72
630
        }
73
74
236k
        timespec end;
75
236k
        clock_gettime(Clock, &end);
76
236k
        return (end.tv_sec - _start.tv_sec) * 1000L * 1000L * 1000L +
77
236k
               (end.tv_nsec - _start.tv_nsec);
78
236k
    }
Unexecuted instantiation: _ZNK5doris15CustomStopWatchILi3EE12elapsed_timeEv
79
80
    // Returns time in nanosecond.
81
0
    int64_t elapsed_time_seconds(timespec end) const {
82
0
        if (!_running) {
83
0
            return _total_time / 1000L / 1000L / 1000L;
84
0
        }
85
0
        return end.tv_sec - _start.tv_sec;
86
0
    }
87
88
private:
89
    timespec _start;
90
    uint64_t _total_time; // in nanosec
91
    bool _running;
92
};
93
94
// Stop watch for reporting elapsed time in nanosec based on CLOCK_MONOTONIC.
95
// It is as fast as Rdtsc.
96
// It is also accurate because it not affected by cpu frequency changes and
97
// it is not affected by user setting the system clock.
98
// CLOCK_MONOTONIC represents monotonic time since some unspecified starting point.
99
// It is good for computing elapsed time.
100
using MonotonicStopWatch = CustomStopWatch<CLOCK_MONOTONIC>;
101
102
// Stop watch for reporting elapsed nanosec based on CLOCK_THREAD_CPUTIME_ID.
103
using ThreadCpuStopWatch = CustomStopWatch<CLOCK_THREAD_CPUTIME_ID>;
104
105
} // namespace doris