Coverage Report

Created: 2025-06-23 17:55

/root/doris/be/src/runtime/query_statistics.h
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
18
#pragma once
19
20
#include <gen_cpp/FrontendService_types.h>
21
#include <gen_cpp/PaloInternalService_types.h>
22
#include <stdint.h>
23
24
#include <atomic>
25
#include <memory>
26
27
namespace doris {
28
29
class PNodeStatistics;
30
class PQueryStatistics;
31
32
// This is responsible for collecting query statistics, usually it consists of
33
// two parts, one is current fragment or plan's statistics, the other is sub fragment
34
// or plan's statistics and QueryStatisticsRecvr is responsible for collecting it.
35
class QueryStatistics {
36
public:
37
    QueryStatistics()
38
            : scan_rows(0),
39
              scan_bytes(0),
40
              cpu_nanos(0),
41
              returned_rows(0),
42
              max_peak_memory_bytes(0),
43
              current_used_memory_bytes(0),
44
              shuffle_send_bytes(0),
45
19
              shuffle_send_rows(0) {}
46
    virtual ~QueryStatistics();
47
48
    void merge(const QueryStatistics& other);
49
50
0
    void add_scan_rows(int64_t delta_scan_rows) { scan_rows += delta_scan_rows; }
51
52
0
    void add_scan_bytes(int64_t delta_scan_bytes) { scan_bytes += delta_scan_bytes; }
53
54
0
    void add_cpu_nanos(int64_t delta_cpu_time) { cpu_nanos += delta_cpu_time; }
55
56
0
    void add_shuffle_send_bytes(int64_t delta_bytes) { shuffle_send_bytes += delta_bytes; }
57
58
0
    void add_shuffle_send_rows(int64_t delta_rows) { shuffle_send_rows += delta_rows; }
59
60
0
    void add_scan_bytes_from_local_storage(int64_t scan_bytes_from_local_storage) {
61
0
        _scan_bytes_from_local_storage += scan_bytes_from_local_storage;
62
0
    }
63
64
0
    void add_scan_bytes_from_remote_storage(int64_t scan_bytes_from_remote_storage) {
65
0
        _scan_bytes_from_remote_storage += scan_bytes_from_remote_storage;
66
0
    }
67
68
0
    void add_returned_rows(int64_t num_rows) { returned_rows += num_rows; }
69
70
15
    void set_max_peak_memory_bytes(int64_t max_peak_memory_bytes) {
71
15
        this->max_peak_memory_bytes = max_peak_memory_bytes;
72
15
    }
73
74
15
    void set_current_used_memory_bytes(int64_t current_used_memory) {
75
15
        current_used_memory_bytes = current_used_memory;
76
15
    }
77
78
    void to_pb(PQueryStatistics* statistics);
79
    void to_thrift(TQueryStatistics* statistics) const;
80
    void from_pb(const PQueryStatistics& statistics);
81
0
    bool collected() const { return _collected; }
82
83
0
    int64_t get_scan_rows() { return scan_rows; }
84
0
    int64_t get_scan_bytes() { return scan_bytes; }
85
0
    int64_t get_current_used_memory_bytes() { return current_used_memory_bytes; }
86
87
private:
88
    std::atomic<int64_t> scan_rows;
89
    std::atomic<int64_t> scan_bytes;
90
    std::atomic<int64_t> cpu_nanos;
91
    std::atomic<int64_t> _scan_bytes_from_local_storage;
92
    std::atomic<int64_t> _scan_bytes_from_remote_storage;
93
    // number rows returned by query.
94
    // only set once by result sink when closing.
95
    std::atomic<int64_t> returned_rows;
96
    // Maximum memory peak for all backends.
97
    // only set once by result sink when closing.
98
    std::atomic<int64_t> max_peak_memory_bytes;
99
    bool _collected = false;
100
    std::atomic<int64_t> current_used_memory_bytes;
101
102
    std::atomic<int64_t> shuffle_send_bytes;
103
    std::atomic<int64_t> shuffle_send_rows;
104
};
105
using QueryStatisticsPtr = std::shared_ptr<QueryStatistics>;
106
// It is used for collecting sub plan query statistics in DataStreamRecvr.
107
108
} // namespace doris