Coverage Report

Created: 2024-11-20 21:49

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