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