Coverage Report

Created: 2024-11-22 20:18

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