1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process/internal_linux.h"
#include <limits.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <string>
#include <string_view>
#include <vector>
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "build/build_config.h"
// Not defined on AIX by default.
#if BUILDFLAG(IS_AIX)
#define NAME_MAX 255
#endif
namespace base::internal {
const char kProcDir[] = "/proc";
const char kStatFile[] = "stat";
FilePath GetProcPidDir(pid_t pid) {
return FilePath(kProcDir).Append(NumberToString(pid));
}
pid_t ProcDirSlotToPid(std::string_view d_name) {
if (d_name.size() >= NAME_MAX ||
!std::ranges::all_of(d_name, &IsAsciiDigit<char>)) {
return 0;
}
// Read the process's command line.
pid_t pid;
if (!StringToInt(d_name, &pid)) {
NOTREACHED();
}
return pid;
}
bool ReadProcFile(const FilePath& file, std::string* buffer) {
DCHECK(FilePath(kProcDir).IsParent(file));
buffer->clear();
// Synchronously reading files in /proc is safe.
ScopedAllowBlocking scoped_allow_blocking;
if (!ReadFileToString(file, buffer)) {
return false;
}
return !buffer->empty();
}
std::optional<StringViewPairs> ReadProcFileToTrimmedStringPairs(
pid_t pid,
std::string_view filename,
std::string* buffer) {
FilePath status_file = GetProcPidDir(pid).Append(filename);
if (!ReadProcFile(status_file, buffer)) {
return std::nullopt;
}
StringViewPairs key_value_pairs;
SplitStringIntoKeyValueViewPairs(*buffer, ':', '\n', &key_value_pairs);
for (auto& [key, value] : key_value_pairs) {
key = TrimWhitespaceASCII(key, TRIM_ALL);
value = TrimWhitespaceASCII(value, TRIM_ALL);
}
return key_value_pairs;
}
size_t ReadProcStatusAndGetKbFieldAsSizeT(pid_t pid, std::string_view field) {
std::string buffer;
std::optional<StringViewPairs> pairs =
ReadProcFileToTrimmedStringPairs(pid, "status", &buffer);
if (!pairs) {
return 0;
}
for (const auto& [key, value_str] : *pairs) {
if (key != field) {
continue;
}
std::vector<std::string_view> split_value_str =
SplitStringPiece(value_str, " ", TRIM_WHITESPACE, SPLIT_WANT_ALL);
if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
NOTREACHED();
}
size_t value;
if (!StringToSizeT(split_value_str[0], &value)) {
NOTREACHED();
}
return value;
}
// This can be reached if the process dies when proc is read -- in that case,
// the kernel can return missing fields.
return 0;
}
bool ReadProcStatusAndGetFieldAsUint64(pid_t pid,
std::string_view field,
uint64_t* result) {
std::string buffer;
std::optional<StringViewPairs> pairs =
ReadProcFileToTrimmedStringPairs(pid, "status", &buffer);
if (!pairs) {
return false;
}
for (const auto& [key, value_str] : *pairs) {
if (key != field) {
continue;
}
uint64_t value;
if (!StringToUint64(value_str, &value)) {
return false;
}
*result = value;
return true;
}
return false;
}
bool ReadProcStats(pid_t pid, std::string* buffer) {
FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile);
return ReadProcFile(stat_file, buffer);
}
bool ParseProcStats(std::string_view stats_data,
std::vector<std::string_view>* proc_stats) {
// |stats_data| may be empty if the process disappeared somehow.
// e.g. http://crbug.com/145811
if (stats_data.empty()) {
return false;
}
// The stat file is formatted as:
// pid (process name) data1 data2 .... dataN
// Look for the closing paren by scanning backwards, to avoid being fooled by
// processes with ')' in the name.
size_t open_parens_idx = stats_data.find(" (");
size_t close_parens_idx = stats_data.rfind(") ");
if (open_parens_idx == std::string::npos ||
close_parens_idx == std::string::npos ||
open_parens_idx > close_parens_idx) {
DLOG(WARNING) << "Failed to find matched parens in '" << stats_data << "'";
NOTREACHED();
}
open_parens_idx++;
proc_stats->clear();
// PID.
proc_stats->push_back(stats_data.substr(0, open_parens_idx));
// Process name without parentheses.
proc_stats->push_back(stats_data.substr(
open_parens_idx + 1, close_parens_idx - (open_parens_idx + 1)));
// Split the rest.
std::vector<std::string_view> other_stats =
SplitStringPiece(stats_data.substr(close_parens_idx + 2), " ",
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
proc_stats->insert(proc_stats->end(), other_stats.begin(), other_stats.end());
return true;
}
int64_t GetProcStatsFieldAsInt64(base::span<std::string_view> proc_stats,
ProcStatsFields field_num) {
DCHECK_GE(field_num, VM_PPID);
return GetProcStatsFieldAsOptionalInt64(proc_stats, field_num).value_or(0);
}
std::optional<int64_t> GetProcStatsFieldAsOptionalInt64(
base::span<std::string_view> proc_stats,
ProcStatsFields field_num) {
int64_t value;
if (StringToInt64(proc_stats.at(field_num), &value)) {
return value;
}
return std::nullopt;
}
size_t GetProcStatsFieldAsSizeT(base::span<std::string_view> proc_stats,
ProcStatsFields field_num) {
DCHECK_GE(field_num, VM_PPID);
size_t value;
return StringToSizeT(proc_stats.at(field_num), &value) ? value : 0;
}
int64_t ReadStatFileAndGetFieldAsInt64(const FilePath& stat_file,
ProcStatsFields field_num) {
std::string stats_data;
if (!ReadProcFile(stat_file, &stats_data)) {
return 0;
}
std::vector<std::string_view> proc_stats;
if (!ParseProcStats(stats_data, &proc_stats)) {
return 0;
}
return GetProcStatsFieldAsInt64(proc_stats, field_num);
}
int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num) {
FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile);
return ReadStatFileAndGetFieldAsInt64(stat_file, field_num);
}
int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num) {
FilePath stat_file = FilePath(kProcDir).Append("self").Append(kStatFile);
return ReadStatFileAndGetFieldAsInt64(stat_file, field_num);
}
size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid, ProcStatsFields field_num) {
std::string stats_data;
if (!ReadProcStats(pid, &stats_data)) {
return 0;
}
std::vector<std::string_view> proc_stats;
if (!ParseProcStats(stats_data, &proc_stats)) {
return 0;
}
return GetProcStatsFieldAsSizeT(proc_stats, field_num);
}
Time GetBootTime() {
FilePath path("/proc/stat");
std::string contents;
if (!ReadProcFile(path, &contents)) {
return Time();
}
StringViewPairs key_value_pairs;
SplitStringIntoKeyValueViewPairs(contents, ' ', '\n', &key_value_pairs);
for (const auto& [key, value] : key_value_pairs) {
if (key == "btime") {
int btime;
if (!StringToInt(value, &btime)) {
return Time();
}
return Time::FromTimeT(btime);
}
}
return Time();
}
TimeDelta GetUserCpuTimeSinceBoot() {
FilePath path("/proc/stat");
std::string contents;
if (!ReadProcFile(path, &contents)) {
return TimeDelta();
}
StringViewPairs key_value_pairs;
SplitStringIntoKeyValueViewPairs(contents, ' ', '\n', &key_value_pairs);
for (const auto& [key, value] : key_value_pairs) {
if (key == "cpu") {
std::vector<std::string_view> cpu = SplitStringPiece(
value, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
if (cpu.size() < 2 || cpu[0] != "cpu") {
return TimeDelta();
}
uint64_t user;
uint64_t nice;
if (!StringToUint64(cpu[0], &user) || !StringToUint64(cpu[1], &nice)) {
return TimeDelta();
}
return ClockTicksToTimeDelta(checked_cast<int64_t>(user + nice));
}
}
return TimeDelta();
}
TimeDelta ClockTicksToTimeDelta(int64_t clock_ticks) {
// This queries the /proc-specific scaling factor which is
// conceptually the system hertz. To dump this value on another
// system, try
// od -t dL /proc/self/auxv
// and look for the number after 17 in the output; mine is
// 0000040 17 100 3 134512692
// which means the answer is 100.
// It may be the case that this value is always 100.
static const long kHertz = sysconf(_SC_CLK_TCK);
return Microseconds(Time::kMicrosecondsPerSecond * clock_ticks / kHertz);
}
} // namespace base::internal
|