Coverage Report

Created: 2024-11-20 12:56

/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 <string>
30
#include <vector>
31
32
#include "gutil/macros.h"
33
#include "gutil/strings/numbers.h"
34
#include "gutil/strings/split.h"
35
#include "gutil/strings/substitute.h"
36
#include "io/fs/local_file_system.h"
37
38
using std::string;
39
using std::vector;
40
using strings::Split;
41
using strings::Substitute;
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 = tmp * (1e9 / kTicksPerSec);
87
0
    }
88
0
    if (safe_strto64(splits[kKernelTicks], &tmp)) {
89
0
        stats->kernel_ns = tmp * (1e9 / kTicksPerSec);
90
0
    }
91
0
    if (safe_strto64(splits[kIoWait], &tmp)) {
92
0
        stats->iowait_ns = 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
    RETURN_IF_ERROR(io::global_local_filesystem()->read_file_to_string(
107
0
            strings::Substitute("/proc/self/task/$0/stat", tid), &buf));
108
0
    return parse_stat(buf, nullptr, stats);
109
0
}
110
0
void disable_core_dumps() {
111
0
    struct rlimit lim;
112
0
    PCHECK(getrlimit(RLIMIT_CORE, &lim) == 0);
113
0
    lim.rlim_cur = 0;
114
0
    PCHECK(setrlimit(RLIMIT_CORE, &lim) == 0);
115
116
    // Set coredump_filter to not dump any parts of the address space.
117
    // Although the above disables core dumps to files, if core_pattern
118
    // is set to a pipe rather than a file, it's not sufficient. Setting
119
    // this pattern results in piping a very minimal dump into the core
120
    // processor (eg abrtd), thus speeding up the crash.
121
0
    int f;
122
0
    RETRY_ON_EINTR(f, open("/proc/self/coredump_filter", O_WRONLY));
123
0
    if (f >= 0) {
124
0
        ssize_t ret;
125
0
        RETRY_ON_EINTR(ret, write(f, "00000000", 8));
126
0
        int close_ret;
127
0
        RETRY_ON_EINTR(close_ret, close(f));
128
0
    }
129
0
}
130
131
} // namespace doris