Coverage Report

Created: 2026-01-24 05:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/pipeline/pipeline_tracing.h
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
18
#pragma once
19
20
#include <concurrentqueue.h>
21
#include <fmt/format.h>
22
#include <gen_cpp/Types_types.h>
23
#include <parallel_hashmap/phmap.h>
24
25
#include <cstdint>
26
#include <cstdlib>
27
#include <filesystem>
28
29
#include "common/atomic_shared_ptr.h"
30
#include "common/config.h"
31
#include "util/hash_util.hpp" // IWYU pragma: keep
32
#include "util/thrift_util.h"
33
#include "util/time.h"
34
35
namespace doris::pipeline {
36
37
struct ScheduleRecord {
38
    TUniqueId query_id;
39
    std::string task_id;
40
    uint32_t core_id;
41
    uint64_t thread_id;
42
    uint64_t start_time;
43
    uint64_t end_time;
44
45
0
    bool operator<(const ScheduleRecord& rhs) const { return start_time < rhs.start_time; }
46
0
    std::string to_string(uint64_t append_value) const {
47
0
        return fmt::format("{}|{}|{}|{}|{}|{}|{}\n", doris::to_string(query_id), task_id, core_id,
48
0
                           thread_id, start_time, end_time, append_value);
49
0
    }
50
};
51
52
struct QueryID {
53
    TUniqueId query_id;
54
0
    bool operator<(const QueryID& query_id_) const {
55
0
        return query_id.hi < query_id_.query_id.hi ||
56
0
               (query_id.hi == query_id_.query_id.hi && query_id.lo < query_id_.query_id.lo);
57
0
    }
58
0
    bool operator==(const QueryID& query_id_) const { return query_id == query_id_.query_id; }
59
};
60
61
// all tracing datas of ONE specific query
62
using OneQueryTraces = moodycamel::ConcurrentQueue<ScheduleRecord>;
63
using OneQueryTracesSPtr = std::shared_ptr<moodycamel::ConcurrentQueue<ScheduleRecord>>;
64
using QueryTracesMap = std::map<QueryID, OneQueryTracesSPtr>;
65
66
// belongs to exec_env, for all query, if enabled
67
class PipelineTracerContext {
68
public:
69
11
    PipelineTracerContext() : _data(std::make_shared<QueryTracesMap>()) {}
70
    enum class RecordType {
71
        None,     // disable
72
        PerQuery, // record per query. one query one file.
73
        Periodic  // record per times. one timeslice one file.
74
    };
75
    void record(ScheduleRecord record); // record one schedule record
76
    void end_query(TUniqueId query_id,
77
                   uint64_t workload_group); // tell context this query is end. may leads to dump.
78
    Status change_record_params(const std::map<std::string, std::string>& params);
79
80
0
    bool enabled() const { return !(_dump_type == RecordType::None); }
81
82
private:
83
    // dump data to disk. one query or all.
84
    void _dump_query(TUniqueId query_id);
85
    void _dump_timeslice();
86
    void _update(std::function<void(QueryTracesMap&)>&& handler);
87
88
11
    std::filesystem::path _log_dir = []() {
89
11
        const char* env_log_dir = std::getenv("LOG_DIR");
90
11
        if (env_log_dir != nullptr && env_log_dir[0] != '\0') {
91
11
            return std::filesystem::path(fmt::format("{}/pipe_tracing", env_log_dir));
92
11
        }
93
0
        if (!config::sys_log_dir.empty()) {
94
0
            return std::filesystem::path(fmt::format("{}/pipe_tracing", config::sys_log_dir));
95
0
        }
96
0
        return std::filesystem::path("pipe_tracing");
97
0
    }();
98
99
    atomic_shared_ptr<QueryTracesMap> _data;
100
    std::mutex _tg_lock; //TODO: use an lockfree DS
101
    phmap::flat_hash_map<TUniqueId, uint64_t>
102
            _id_to_workload_group; // save query's workload group number
103
104
    RecordType _dump_type = RecordType::None;
105
    decltype(MonotonicSeconds()) _last_dump_time;
106
    decltype(MonotonicSeconds()) _dump_interval_s =
107
            60; // effective iff Periodic mode. 1 minute default.
108
};
109
} // namespace doris::pipeline