Coverage Report

Created: 2025-06-18 15:39

/root/doris/be/src/util/os_util.cpp
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/os-util.cpp
19
// and modified by Doris
20
21
#include "util/os_util.h"
22
23
#include <absl/strings/numbers.h>
24
#include <absl/strings/str_split.h>
25
#include <fcntl.h>
26
#include <glog/logging.h>
27
#include <sys/resource.h>
28
#include <unistd.h>
29
30
#include <fstream>
31
#include <string>
32
#include <vector>
33
34
#include "common/macros.h"
35
36
using std::string;
37
using std::vector;
38
39
namespace doris {
40
41
// Ensure that Impala compiles on earlier kernels. If the target kernel does not support
42
// _SC_CLK_TCK, sysconf(_SC_CLK_TCK) will return -1.
43
#ifndef _SC_CLK_TCK
44
#define _SC_CLK_TCK 2
45
#endif
46
47
static const int64_t kTicksPerSec = sysconf(_SC_CLK_TCK);
48
49
// Offsets into the ../stat file array of per-thread statistics.
50
//
51
// They are themselves offset by two because the pid and comm fields of the
52
// file are parsed separately.
53
static const int64_t kUserTicks = 13 - 2;
54
static const int64_t kKernelTicks = 14 - 2;
55
static const int64_t kIoWait = 41 - 2;
56
57
// Largest offset we are interested in, to check we get a well formed stat file.
58
static const int64_t kMaxOffset = kIoWait;
59
60
0
Status parse_stat(const std::string& buffer, std::string* name, ThreadStats* stats) {
61
0
    DCHECK(stats != nullptr);
62
63
    // The thread name should be the only field with parentheses. But the name
64
    // itself may contain parentheses.
65
0
    size_t open_paren = buffer.find('(');
66
0
    size_t close_paren = buffer.rfind(')');
67
0
    if (open_paren == string::npos ||       // '(' must exist
68
0
        close_paren == string::npos ||      // ')' must exist
69
0
        open_paren >= close_paren ||        // '(' must come before ')'
70
0
        close_paren + 2 == buffer.size()) { // there must be at least two chars after ')'
71
0
        return Status::IOError("Unrecognised /proc format");
72
0
    }
73
0
    string extracted_name = buffer.substr(open_paren + 1, close_paren - (open_paren + 1));
74
0
    string rest = buffer.substr(close_paren + 2);
75
0
    std::vector<string> splits = absl::StrSplit(rest, " ", absl::SkipEmpty());
76
0
    if (splits.size() < kMaxOffset) {
77
0
        return Status::IOError("Unrecognised /proc format");
78
0
    }
79
80
0
    int64_t tmp;
81
0
    if (absl::SimpleAtoi(splits[kUserTicks], &tmp)) {
82
0
        stats->user_ns = int64_t(tmp * (1e9 / kTicksPerSec));
83
0
    }
84
0
    if (absl::SimpleAtoi(splits[kKernelTicks], &tmp)) {
85
0
        stats->kernel_ns = int64_t(tmp * (1e9 / kTicksPerSec));
86
0
    }
87
0
    if (absl::SimpleAtoi(splits[kIoWait], &tmp)) {
88
0
        stats->iowait_ns = int64_t(tmp * (1e9 / kTicksPerSec));
89
0
    }
90
0
    if (name != nullptr) {
91
0
        *name = extracted_name;
92
0
    }
93
0
    return Status::OK();
94
0
}
95
96
0
Status get_thread_stats(int64_t tid, ThreadStats* stats) {
97
0
    DCHECK(stats != nullptr);
98
0
    if (kTicksPerSec <= 0) {
99
0
        return Status::NotSupported("ThreadStats not supported");
100
0
    }
101
0
    std::string buf;
102
0
    auto path = fmt::format("/proc/self/task/{}/stat", tid);
103
0
    std::ifstream file(path);
104
0
    if (file.is_open()) {
105
0
        std::ostringstream oss;
106
0
        oss << file.rdbuf();
107
0
        buf = oss.str();
108
0
        file.close();
109
0
    } else {
110
0
        return Status::InternalError("failed to open {}: {}", path, std::strerror(errno));
111
0
    }
112
113
0
    return parse_stat(buf, nullptr, stats);
114
0
}
115
0
void disable_core_dumps() {
116
0
    struct rlimit lim;
117
0
    PCHECK(getrlimit(RLIMIT_CORE, &lim) == 0);
118
0
    lim.rlim_cur = 0;
119
0
    PCHECK(setrlimit(RLIMIT_CORE, &lim) == 0);
120
121
    // Set coredump_filter to not dump any parts of the address space.
122
    // Although the above disables core dumps to files, if core_pattern
123
    // is set to a pipe rather than a file, it's not sufficient. Setting
124
    // this pattern results in piping a very minimal dump into the core
125
    // processor (eg abrtd), thus speeding up the crash.
126
0
    int f;
127
0
    RETRY_ON_EINTR(f, open("/proc/self/coredump_filter", O_WRONLY));
128
0
    if (f >= 0) {
129
0
        ssize_t ret;
130
0
        RETRY_ON_EINTR(ret, write(f, "00000000", 8));
131
0
        int close_ret;
132
0
        RETRY_ON_EINTR(close_ret, close(f));
133
0
    }
134
0
}
135
136
} // namespace doris