Coverage Report

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