Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/process/process_metrics.h" |
| 6 | |
| 7 | #include <dirent.h> |
davemoore@chromium.org | 7b9c9887 | 2013-05-29 15:42:04 | [diff] [blame] | 8 | #include <fcntl.h> |
Eric Seckler | dbc29da | 2020-07-10 11:54:05 | [diff] [blame] | 9 | #include <inttypes.h> |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
davemoore@chromium.org | 7b9c9887 | 2013-05-29 15:42:04 | [diff] [blame] | 12 | #include <sys/stat.h> |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 13 | #include <sys/time.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <unistd.h> |
Eric Seckler | dbc29da | 2020-07-10 11:54:05 | [diff] [blame] | 16 | |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 17 | #include <utility> |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 18 | |
Eric Seckler | d58a1b0 | 2020-08-03 07:51:15 | [diff] [blame] | 19 | #include "base/cpu.h" |
thestig | cf4c85b | 2015-12-16 08:33:30 | [diff] [blame] | 20 | #include "base/files/dir_reader_posix.h" |
brettw@chromium.org | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 21 | #include "base/files/file_util.h" |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 22 | #include "base/logging.h" |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 23 | #include "base/memory/ptr_util.h" |
Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame] | 24 | #include "base/notreached.h" |
Joe Mason | 87c33f1 | 2024-02-09 15:47:04 | [diff] [blame] | 25 | #include "base/numerics/clamped_math.h" |
Peter Kasting | 25acd594 | 2022-07-07 17:45:15 | [diff] [blame] | 26 | #include "base/numerics/safe_conversions.h" |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 27 | #include "base/process/internal_linux.h" |
Bruce Dawson | aa08ad82 | 2018-01-20 00:31:05 | [diff] [blame] | 28 | #include "base/process/process_metrics_iocounters.h" |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 29 | #include "base/strings/string_number_conversions.h" |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 30 | #include "base/strings/string_piece.h" |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 31 | #include "base/strings/string_split.h" |
| 32 | #include "base/strings/string_tokenizer.h" |
avi@chromium.org | d1a5a2f | 2013-06-10 21:17:40 | [diff] [blame] | 33 | #include "base/strings/string_util.h" |
Kuo-Hsin Yang | 7cc65fc | 2020-10-12 14:02:27 | [diff] [blame] | 34 | #include "base/system/sys_info.h" |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 35 | #include "base/threading/thread_restrictions.h" |
Hans Wennborg | 9e7d902 | 2021-05-03 12:56:20 | [diff] [blame] | 36 | #include "base/values.h" |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 37 | #include "build/build_config.h" |
Peter Kasting | 2c0c3ea | 2023-08-05 18:46:16 | [diff] [blame] | 38 | #include "third_party/abseil-cpp/absl/strings/ascii.h" |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 39 | #include "third_party/abseil-cpp/absl/types/optional.h" |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 40 | |
| 41 | namespace base { |
| 42 | |
Etienne Pierre-doray | 8927291 | 2022-11-07 22:35:38 | [diff] [blame] | 43 | class ScopedAllowBlockingForProcessMetrics : public ScopedAllowBlocking {}; |
| 44 | |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 45 | namespace { |
| 46 | |
Eric Willigers | 611cf54 | 2022-04-28 02:22:14 | [diff] [blame] | 47 | #if BUILDFLAG(IS_CHROMEOS) |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 48 | // Read a file with a single number string and return the number as a uint64_t. |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 49 | uint64_t ReadFileToUint64(const FilePath& file) { |
| 50 | std::string file_contents; |
| 51 | if (!ReadFileToString(file, &file_contents)) |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 52 | return 0; |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 53 | TrimWhitespaceASCII(file_contents, TRIM_ALL, &file_contents); |
| 54 | uint64_t file_contents_uint64 = 0; |
| 55 | if (!StringToUint64(file_contents, &file_contents_uint64)) |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 56 | return 0; |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 57 | return file_contents_uint64; |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 58 | } |
| 59 | #endif |
| 60 | |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 61 | // Get the total CPU from a proc stat buffer. Return value is number of jiffies |
| 62 | // on success or 0 if parsing failed. |
| 63 | int64_t ParseTotalCPUTimeFromStats(const std::vector<std::string>& proc_stats) { |
| 64 | return internal::GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME) + |
| 65 | internal::GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); |
| 66 | } |
| 67 | |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 68 | // Get the total CPU of a single process. Return value is number of jiffies |
| 69 | // on success or -1 on error. |
Sigurdur Asgeirsson | 6c062bad | 2018-05-14 22:56:11 | [diff] [blame] | 70 | int64_t GetProcessCPU(pid_t pid) { |
| 71 | std::string buffer; |
| 72 | std::vector<std::string> proc_stats; |
| 73 | if (!internal::ReadProcStats(pid, &buffer) || |
| 74 | !internal::ParseProcStats(buffer, &proc_stats)) { |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 78 | return ParseTotalCPUTimeFromStats(proc_stats); |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | // static |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 84 | std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 85 | ProcessHandle process) { |
| 86 | return WrapUnique(new ProcessMetrics(process)); |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 87 | } |
| 88 | |
erikchen | ce05551 | 2018-03-09 23:49:03 | [diff] [blame] | 89 | size_t ProcessMetrics::GetResidentSetSize() const { |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 90 | return internal::ReadProcStatsAndGetFieldAsSizeT(process_, internal::VM_RSS) * |
Peter Kasting | 25acd594 | 2022-07-07 17:45:15 | [diff] [blame] | 91 | checked_cast<size_t>(getpagesize()); |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 92 | } |
| 93 | |
Sigurdur Asgeirsson | eb27eae7 | 2018-05-16 15:29:10 | [diff] [blame] | 94 | TimeDelta ProcessMetrics::GetCumulativeCPUUsage() { |
| 95 | return internal::ClockTicksToTimeDelta(GetProcessCPU(process_)); |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 96 | } |
| 97 | |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 98 | bool ProcessMetrics::GetCumulativeCPUUsagePerThread( |
| 99 | CPUUsagePerThread& cpu_per_thread) { |
| 100 | cpu_per_thread.clear(); |
| 101 | |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 102 | internal::ForEachProcessTask( |
| 103 | process_, |
| 104 | [&cpu_per_thread](PlatformThreadId tid, const FilePath& task_path) { |
| 105 | FilePath thread_stat_path = task_path.Append("stat"); |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 106 | |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 107 | std::string buffer; |
| 108 | std::vector<std::string> proc_stats; |
| 109 | if (!internal::ReadProcFile(thread_stat_path, &buffer) || |
| 110 | !internal::ParseProcStats(buffer, &proc_stats)) { |
| 111 | return; |
| 112 | } |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 113 | |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 114 | TimeDelta thread_time = internal::ClockTicksToTimeDelta( |
| 115 | ParseTotalCPUTimeFromStats(proc_stats)); |
| 116 | cpu_per_thread.emplace_back(tid, thread_time); |
| 117 | }); |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 118 | |
| 119 | return !cpu_per_thread.empty(); |
| 120 | } |
| 121 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 122 | // For the /proc/self/io file to exist, the Linux kernel must have |
| 123 | // CONFIG_TASK_IO_ACCOUNTING enabled. |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 124 | bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 125 | StringPairs pairs; |
Matthew Denton | f969883 | 2023-08-08 01:44:34 | [diff] [blame] | 126 | if (!internal::ReadProcFileToTrimmedStringPairs(process_, "io", &pairs)) { |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 127 | return false; |
Matthew Denton | f969883 | 2023-08-08 01:44:34 | [diff] [blame] | 128 | } |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 129 | |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 130 | io_counters->OtherOperationCount = 0; |
| 131 | io_counters->OtherTransferCount = 0; |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 132 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 133 | for (const auto& pair : pairs) { |
| 134 | const std::string& key = pair.first; |
| 135 | const std::string& value_str = pair.second; |
| 136 | uint64_t* target_counter = nullptr; |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 137 | if (key == "syscr") |
| 138 | target_counter = &io_counters->ReadOperationCount; |
| 139 | else if (key == "syscw") |
| 140 | target_counter = &io_counters->WriteOperationCount; |
| 141 | else if (key == "rchar") |
| 142 | target_counter = &io_counters->ReadTransferCount; |
| 143 | else if (key == "wchar") |
| 144 | target_counter = &io_counters->WriteTransferCount; |
| 145 | if (!target_counter) |
| 146 | continue; |
| 147 | bool converted = StringToUint64(value_str, target_counter); |
| 148 | DCHECK(converted); |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 153 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) |
hjd | bae6b3b | 2017-05-17 04:18:05 | [diff] [blame] | 154 | uint64_t ProcessMetrics::GetVmSwapBytes() const { |
Matthew Denton | f969883 | 2023-08-08 01:44:34 | [diff] [blame] | 155 | return internal::ReadProcStatusAndGetKbFieldAsSizeT(process_, "VmSwap") * |
| 156 | 1024; |
hjd | bae6b3b | 2017-05-17 04:18:05 | [diff] [blame] | 157 | } |
hjd | bae6b3b | 2017-05-17 04:18:05 | [diff] [blame] | 158 | |
Benoit Lize | 448ee88 | 2017-08-31 13:32:12 | [diff] [blame] | 159 | bool ProcessMetrics::GetPageFaultCounts(PageFaultCounts* counts) const { |
| 160 | // We are not using internal::ReadStatsFileAndGetFieldAsInt64(), since it |
| 161 | // would read the file twice, and return inconsistent numbers. |
| 162 | std::string stats_data; |
| 163 | if (!internal::ReadProcStats(process_, &stats_data)) |
| 164 | return false; |
| 165 | std::vector<std::string> proc_stats; |
| 166 | if (!internal::ParseProcStats(stats_data, &proc_stats)) |
| 167 | return false; |
| 168 | |
| 169 | counts->minor = |
| 170 | internal::GetProcStatsFieldAsInt64(proc_stats, internal::VM_MINFLT); |
| 171 | counts->major = |
| 172 | internal::GetProcStatsFieldAsInt64(proc_stats, internal::VM_MAJFLT); |
| 173 | return true; |
| 174 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 175 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || |
| 176 | // BUILDFLAG(IS_ANDROID) |
Benoit Lize | 448ee88 | 2017-08-31 13:32:12 | [diff] [blame] | 177 | |
afakhry | 43b3bb7 | 2015-12-15 21:57:59 | [diff] [blame] | 178 | int ProcessMetrics::GetOpenFdCount() const { |
| 179 | // Use /proc/<pid>/fd to count the number of entries there. |
| 180 | FilePath fd_path = internal::GetProcPidDir(process_).Append("fd"); |
| 181 | |
thestig | cf4c85b | 2015-12-16 08:33:30 | [diff] [blame] | 182 | DirReaderPosix dir_reader(fd_path.value().c_str()); |
| 183 | if (!dir_reader.IsValid()) |
afakhry | 43b3bb7 | 2015-12-15 21:57:59 | [diff] [blame] | 184 | return -1; |
afakhry | 43b3bb7 | 2015-12-15 21:57:59 | [diff] [blame] | 185 | |
| 186 | int total_count = 0; |
thestig | cf4c85b | 2015-12-16 08:33:30 | [diff] [blame] | 187 | for (; dir_reader.Next(); ) { |
| 188 | const char* name = dir_reader.name(); |
| 189 | if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0) |
| 190 | ++total_count; |
| 191 | } |
afakhry | 43b3bb7 | 2015-12-15 21:57:59 | [diff] [blame] | 192 | |
| 193 | return total_count; |
| 194 | } |
dcastagna | f454af4e | 2017-03-15 23:34:14 | [diff] [blame] | 195 | |
| 196 | int ProcessMetrics::GetOpenFdSoftLimit() const { |
| 197 | // Use /proc/<pid>/limits to read the open fd limit. |
| 198 | FilePath fd_path = internal::GetProcPidDir(process_).Append("limits"); |
| 199 | |
| 200 | std::string limits_contents; |
Brian Geffon | 49ddbf0 | 2020-06-19 22:57:56 | [diff] [blame] | 201 | if (!ReadFileToStringNonBlocking(fd_path, &limits_contents)) |
dcastagna | f454af4e | 2017-03-15 23:34:14 | [diff] [blame] | 202 | return -1; |
| 203 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 204 | for (const auto& line : SplitStringPiece( |
| 205 | limits_contents, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) { |
Jan Wilken Dörrie | f05bb10 | 2020-08-18 19:35:56 | [diff] [blame] | 206 | if (!StartsWith(line, "Max open files")) |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 207 | continue; |
| 208 | |
| 209 | auto tokens = |
| 210 | SplitStringPiece(line, " ", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 211 | if (tokens.size() > 3) { |
| 212 | int limit = -1; |
| 213 | if (!StringToInt(tokens[3], &limit)) |
dcastagna | f454af4e | 2017-03-15 23:34:14 | [diff] [blame] | 214 | return -1; |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 215 | return limit; |
dcastagna | f454af4e | 2017-03-15 23:34:14 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | return -1; |
| 219 | } |
afakhry | 43b3bb7 | 2015-12-15 21:57:59 | [diff] [blame] | 220 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 221 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX) |
Sigurdur Asgeirsson | eb27eae7 | 2018-05-16 15:29:10 | [diff] [blame] | 222 | ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 223 | : process_(process), last_absolute_idle_wakeups_(0) {} |
| 224 | #else |
| 225 | ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process) {} |
thestig | 75844fdb | 2014-09-09 19:47:10 | [diff] [blame] | 226 | #endif |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 227 | |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 228 | size_t GetSystemCommitCharge() { |
| 229 | SystemMemoryInfoKB meminfo; |
| 230 | if (!GetSystemMemoryInfo(&meminfo)) |
| 231 | return 0; |
Joe Mason | 87c33f1 | 2024-02-09 15:47:04 | [diff] [blame] | 232 | return GetSystemCommitChargeFromMeminfo(meminfo); |
| 233 | } |
| 234 | |
| 235 | size_t GetSystemCommitChargeFromMeminfo(const SystemMemoryInfoKB& meminfo) { |
| 236 | // TODO(crbug.com/315988925): This math is incorrect: `cached` can be very |
| 237 | // large so that `free` + `buffers` + `cached` > `total`. Replace this with a |
| 238 | // more meaningful metric or remove it. In the meantime, convert underflows to |
| 239 | // 0 instead of crashing. |
| 240 | return ClampedNumeric<size_t>(meminfo.total) - meminfo.free - |
| 241 | meminfo.buffers - meminfo.cached; |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 242 | } |
| 243 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 244 | int ParseProcStatCPU(StringPiece input) { |
afakhry | 597a851 | 2015-02-05 00:51:57 | [diff] [blame] | 245 | // |input| may be empty if the process disappeared somehow. |
| 246 | // e.g. http://crbug.com/145811. |
| 247 | if (input.empty()) |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 248 | return -1; |
| 249 | |
afakhry | 597a851 | 2015-02-05 00:51:57 | [diff] [blame] | 250 | size_t start = input.find_last_of(')'); |
| 251 | if (start == input.npos) |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 252 | return -1; |
afakhry | 597a851 | 2015-02-05 00:51:57 | [diff] [blame] | 253 | |
| 254 | // Number of spaces remaining until reaching utime's index starting after the |
| 255 | // last ')'. |
| 256 | int num_spaces_remaining = internal::VM_UTIME - 1; |
| 257 | |
| 258 | size_t i = start; |
| 259 | while ((i = input.find(' ', i + 1)) != input.npos) { |
| 260 | // Validate the assumption that there aren't any contiguous spaces |
| 261 | // in |input| before utime. |
| 262 | DCHECK_NE(input[i - 1], ' '); |
| 263 | if (--num_spaces_remaining == 0) { |
| 264 | int utime = 0; |
| 265 | int stime = 0; |
| 266 | if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2) |
| 267 | return -1; |
| 268 | |
| 269 | return utime + stime; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return -1; |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 274 | } |
| 275 | |
Peter Kasting | 25acd594 | 2022-07-07 17:45:15 | [diff] [blame] | 276 | int64_t GetNumberOfThreads(ProcessHandle process) { |
sheu@chromium.org | a9808479 | 2014-01-09 01:39:24 | [diff] [blame] | 277 | return internal::ReadProcStatsAndGetFieldAsInt64(process, |
| 278 | internal::VM_NUMTHREADS); |
jwmak@chromium.org | 103ff351 | 2013-08-22 07:19:30 | [diff] [blame] | 279 | } |
| 280 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 281 | const char kProcSelfExe[] = "/proc/self/exe"; |
| 282 | |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 283 | namespace { |
| 284 | |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 285 | // The format of /proc/diskstats is: |
| 286 | // Device major number |
| 287 | // Device minor number |
| 288 | // Device name |
| 289 | // Field 1 -- # of reads completed |
| 290 | // This is the total number of reads completed successfully. |
| 291 | // Field 2 -- # of reads merged, field 6 -- # of writes merged |
| 292 | // Reads and writes which are adjacent to each other may be merged for |
| 293 | // efficiency. Thus two 4K reads may become one 8K read before it is |
| 294 | // ultimately handed to the disk, and so it will be counted (and queued) |
| 295 | // as only one I/O. This field lets you know how often this was done. |
| 296 | // Field 3 -- # of sectors read |
| 297 | // This is the total number of sectors read successfully. |
| 298 | // Field 4 -- # of milliseconds spent reading |
| 299 | // This is the total number of milliseconds spent by all reads (as |
| 300 | // measured from __make_request() to end_that_request_last()). |
| 301 | // Field 5 -- # of writes completed |
| 302 | // This is the total number of writes completed successfully. |
| 303 | // Field 6 -- # of writes merged |
| 304 | // See the description of field 2. |
| 305 | // Field 7 -- # of sectors written |
| 306 | // This is the total number of sectors written successfully. |
| 307 | // Field 8 -- # of milliseconds spent writing |
| 308 | // This is the total number of milliseconds spent by all writes (as |
| 309 | // measured from __make_request() to end_that_request_last()). |
| 310 | // Field 9 -- # of I/Os currently in progress |
| 311 | // The only field that should go to zero. Incremented as requests are |
| 312 | // given to appropriate struct request_queue and decremented as they |
| 313 | // finish. |
| 314 | // Field 10 -- # of milliseconds spent doing I/Os |
| 315 | // This field increases so long as field 9 is nonzero. |
| 316 | // Field 11 -- weighted # of milliseconds spent doing I/Os |
| 317 | // This field is incremented at each I/O start, I/O completion, I/O |
| 318 | // merge, or read of these stats by the number of I/Os in progress |
| 319 | // (field 9) times the number of milliseconds spent doing I/O since the |
| 320 | // last update of this field. This can provide an easy measure of both |
| 321 | // I/O completion time and the backlog that may be accumulating. |
| 322 | |
| 323 | const size_t kDiskDriveName = 2; |
| 324 | const size_t kDiskReads = 3; |
| 325 | const size_t kDiskReadsMerged = 4; |
| 326 | const size_t kDiskSectorsRead = 5; |
| 327 | const size_t kDiskReadTime = 6; |
| 328 | const size_t kDiskWrites = 7; |
| 329 | const size_t kDiskWritesMerged = 8; |
| 330 | const size_t kDiskSectorsWritten = 9; |
| 331 | const size_t kDiskWriteTime = 10; |
| 332 | const size_t kDiskIO = 11; |
| 333 | const size_t kDiskIOTime = 12; |
| 334 | const size_t kDiskWeightedIOTime = 13; |
| 335 | |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 336 | } // namespace |
| 337 | |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 338 | Value::Dict SystemMemoryInfoKB::ToDict() const { |
| 339 | Value::Dict res; |
| 340 | res.Set("total", total); |
| 341 | res.Set("free", free); |
| 342 | res.Set("available", available); |
| 343 | res.Set("buffers", buffers); |
| 344 | res.Set("cached", cached); |
| 345 | res.Set("active_anon", active_anon); |
| 346 | res.Set("inactive_anon", inactive_anon); |
| 347 | res.Set("active_file", active_file); |
| 348 | res.Set("inactive_file", inactive_file); |
| 349 | res.Set("swap_total", swap_total); |
| 350 | res.Set("swap_free", swap_free); |
| 351 | res.Set("swap_used", swap_total - swap_free); |
| 352 | res.Set("dirty", dirty); |
| 353 | res.Set("reclaimable", reclaimable); |
Eric Willigers | 611cf54 | 2022-04-28 02:22:14 | [diff] [blame] | 354 | #if BUILDFLAG(IS_CHROMEOS) |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 355 | res.Set("shmem", shmem); |
| 356 | res.Set("slab", slab); |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 357 | #endif |
| 358 | |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 359 | return res; |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 360 | } |
| 361 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 362 | bool ParseProcMeminfo(StringPiece meminfo_data, SystemMemoryInfoKB* meminfo) { |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 363 | // The format of /proc/meminfo is: |
| 364 | // |
| 365 | // MemTotal: 8235324 kB |
| 366 | // MemFree: 1628304 kB |
| 367 | // Buffers: 429596 kB |
| 368 | // Cached: 4728232 kB |
| 369 | // ... |
| 370 | // There is no guarantee on the ordering or position |
| 371 | // though it doesn't appear to change very often |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 372 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 373 | // As a basic sanity check at the end, make sure the MemTotal value will be at |
| 374 | // least non-zero. So start off with a zero total. |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 375 | meminfo->total = 0; |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 376 | |
brettw | 37fd9d7d | 2015-06-24 02:07:16 | [diff] [blame] | 377 | for (const StringPiece& line : SplitStringPiece( |
| 378 | meminfo_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) { |
| 379 | std::vector<StringPiece> tokens = SplitStringPiece( |
| 380 | line, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 381 | // HugePages_* only has a number and no suffix so there may not be exactly 3 |
| 382 | // tokens. |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 383 | if (tokens.size() <= 1) { |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 384 | DLOG(WARNING) << "meminfo: tokens: " << tokens.size() |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 385 | << " malformed line: " << line; |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 386 | continue; |
| 387 | } |
| 388 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 389 | int* target = nullptr; |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 390 | if (tokens[0] == "MemTotal:") |
| 391 | target = &meminfo->total; |
| 392 | else if (tokens[0] == "MemFree:") |
| 393 | target = &meminfo->free; |
halliwell | c2eb0f97 | 2016-06-17 19:04:52 | [diff] [blame] | 394 | else if (tokens[0] == "MemAvailable:") |
| 395 | target = &meminfo->available; |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 396 | else if (tokens[0] == "Buffers:") |
| 397 | target = &meminfo->buffers; |
| 398 | else if (tokens[0] == "Cached:") |
| 399 | target = &meminfo->cached; |
| 400 | else if (tokens[0] == "Active(anon):") |
| 401 | target = &meminfo->active_anon; |
| 402 | else if (tokens[0] == "Inactive(anon):") |
| 403 | target = &meminfo->inactive_anon; |
| 404 | else if (tokens[0] == "Active(file):") |
| 405 | target = &meminfo->active_file; |
| 406 | else if (tokens[0] == "Inactive(file):") |
| 407 | target = &meminfo->inactive_file; |
| 408 | else if (tokens[0] == "SwapTotal:") |
| 409 | target = &meminfo->swap_total; |
| 410 | else if (tokens[0] == "SwapFree:") |
| 411 | target = &meminfo->swap_free; |
| 412 | else if (tokens[0] == "Dirty:") |
| 413 | target = &meminfo->dirty; |
mkolom | 01ac10b | 2017-03-22 01:47:29 | [diff] [blame] | 414 | else if (tokens[0] == "SReclaimable:") |
| 415 | target = &meminfo->reclaimable; |
Eric Willigers | 611cf54 | 2022-04-28 02:22:14 | [diff] [blame] | 416 | #if BUILDFLAG(IS_CHROMEOS) |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 417 | // Chrome OS has a tweaked kernel that allows querying Shmem, which is |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 418 | // usually video memory otherwise invisible to the OS. |
| 419 | else if (tokens[0] == "Shmem:") |
| 420 | target = &meminfo->shmem; |
| 421 | else if (tokens[0] == "Slab:") |
| 422 | target = &meminfo->slab; |
| 423 | #endif |
| 424 | if (target) |
| 425 | StringToInt(tokens[1], target); |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 426 | } |
| 427 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 428 | // Make sure the MemTotal is valid. |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 429 | return meminfo->total > 0; |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 430 | } |
| 431 | |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 432 | bool ParseProcVmstat(StringPiece vmstat_data, VmStatInfo* vmstat) { |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 433 | // The format of /proc/vmstat is: |
| 434 | // |
| 435 | // nr_free_pages 299878 |
| 436 | // nr_inactive_anon 239863 |
| 437 | // nr_active_anon 1318966 |
| 438 | // nr_inactive_file 2015629 |
| 439 | // ... |
| 440 | // |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 441 | // Iterate through the whole file because the position of the |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 442 | // fields are dependent on the kernel version and configuration. |
Kuo-Hsin Yang | a141f59 | 2020-09-10 07:09:33 | [diff] [blame] | 443 | |
| 444 | // Returns true if all of these 3 fields are present. |
Lei Zhang | 912563e | 2017-07-20 16:49:58 | [diff] [blame] | 445 | bool has_pswpin = false; |
| 446 | bool has_pswpout = false; |
| 447 | bool has_pgmajfault = false; |
Kuo-Hsin Yang | a141f59 | 2020-09-10 07:09:33 | [diff] [blame] | 448 | |
| 449 | // The oom_kill field is optional. The vmstat oom_kill field is available on |
| 450 | // upstream kernel 4.13. It's backported to Chrome OS kernel 3.10. |
| 451 | bool has_oom_kill = false; |
| 452 | vmstat->oom_kill = 0; |
| 453 | |
brettw | 37fd9d7d | 2015-06-24 02:07:16 | [diff] [blame] | 454 | for (const StringPiece& line : SplitStringPiece( |
| 455 | vmstat_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) { |
| 456 | std::vector<StringPiece> tokens = SplitStringPiece( |
| 457 | line, " ", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 458 | if (tokens.size() != 2) |
| 459 | continue; |
| 460 | |
thomasanderson | 2b684a8 | 2016-07-29 21:38:36 | [diff] [blame] | 461 | uint64_t val; |
| 462 | if (!StringToUint64(tokens[1], &val)) |
| 463 | continue; |
| 464 | |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 465 | if (tokens[0] == "pswpin") { |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 466 | vmstat->pswpin = val; |
Lei Zhang | 912563e | 2017-07-20 16:49:58 | [diff] [blame] | 467 | DCHECK(!has_pswpin); |
| 468 | has_pswpin = true; |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 469 | } else if (tokens[0] == "pswpout") { |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 470 | vmstat->pswpout = val; |
Lei Zhang | 912563e | 2017-07-20 16:49:58 | [diff] [blame] | 471 | DCHECK(!has_pswpout); |
| 472 | has_pswpout = true; |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 473 | } else if (tokens[0] == "pgmajfault") { |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 474 | vmstat->pgmajfault = val; |
Lei Zhang | 912563e | 2017-07-20 16:49:58 | [diff] [blame] | 475 | DCHECK(!has_pgmajfault); |
| 476 | has_pgmajfault = true; |
Kuo-Hsin Yang | a141f59 | 2020-09-10 07:09:33 | [diff] [blame] | 477 | } else if (tokens[0] == "oom_kill") { |
| 478 | vmstat->oom_kill = val; |
| 479 | DCHECK(!has_oom_kill); |
| 480 | has_oom_kill = true; |
sonnyrao@chromium.org | ded8c42b | 2013-11-05 22:15:03 | [diff] [blame] | 481 | } |
| 482 | } |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 483 | |
Kuo-Hsin Yang | a141f59 | 2020-09-10 07:09:33 | [diff] [blame] | 484 | return has_pswpin && has_pswpout && has_pgmajfault; |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 488 | // Used memory is: total - free - buffers - caches |
Kuo-Hsin Yang | 6d452a9 | 2021-01-12 01:40:26 | [diff] [blame] | 489 | // ReadFileToStringNonBlocking doesn't require ScopedAllowIO, and reading |
| 490 | // /proc/meminfo is fast. See crbug.com/1160988 for details. |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 491 | FilePath meminfo_file("/proc/meminfo"); |
| 492 | std::string meminfo_data; |
Brian Geffon | 49ddbf0 | 2020-06-19 22:57:56 | [diff] [blame] | 493 | if (!ReadFileToStringNonBlocking(meminfo_file, &meminfo_data)) { |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 494 | DLOG(WARNING) << "Failed to open " << meminfo_file.value(); |
| 495 | return false; |
| 496 | } |
| 497 | |
| 498 | if (!ParseProcMeminfo(meminfo_data, meminfo)) { |
| 499 | DLOG(WARNING) << "Failed to parse " << meminfo_file.value(); |
| 500 | return false; |
| 501 | } |
| 502 | |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 506 | Value::Dict VmStatInfo::ToDict() const { |
| 507 | Value::Dict res; |
Peter Kasting | 6a4bf14c | 2022-07-13 14:53:33 | [diff] [blame] | 508 | // TODO(crbug.com/1334256): Make base::Value able to hold uint64_t and remove |
| 509 | // casts below. |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 510 | res.Set("pswpin", static_cast<int>(pswpin)); |
| 511 | res.Set("pswpout", static_cast<int>(pswpout)); |
| 512 | res.Set("pgmajfault", static_cast<int>(pgmajfault)); |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 513 | return res; |
| 514 | } |
| 515 | |
| 516 | bool GetVmStatInfo(VmStatInfo* vmstat) { |
Kuo-Hsin Yang | 7cc65fc | 2020-10-12 14:02:27 | [diff] [blame] | 517 | // Synchronously reading files in /proc is safe. |
Etienne Pierre-doray | 8927291 | 2022-11-07 22:35:38 | [diff] [blame] | 518 | ScopedAllowBlockingForProcessMetrics allow_blocking; |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 519 | |
jwmak@chromium.org | 49b0cf8 | 2013-09-03 23:46:54 | [diff] [blame] | 520 | FilePath vmstat_file("/proc/vmstat"); |
| 521 | std::string vmstat_data; |
Brian Geffon | 49ddbf0 | 2020-06-19 22:57:56 | [diff] [blame] | 522 | if (!ReadFileToStringNonBlocking(vmstat_file, &vmstat_data)) { |
jwmak@chromium.org | 49b0cf8 | 2013-09-03 23:46:54 | [diff] [blame] | 523 | DLOG(WARNING) << "Failed to open " << vmstat_file.value(); |
| 524 | return false; |
| 525 | } |
Kenichi Ishibashi | 4487152 | 2017-10-23 07:12:19 | [diff] [blame] | 526 | if (!ParseProcVmstat(vmstat_data, vmstat)) { |
davemoore@chromium.org | 70baebc | 2013-11-02 16:58:44 | [diff] [blame] | 527 | DLOG(WARNING) << "Failed to parse " << vmstat_file.value(); |
| 528 | return false; |
| 529 | } |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 530 | return true; |
| 531 | } |
| 532 | |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 533 | SystemDiskInfo::SystemDiskInfo() { |
| 534 | reads = 0; |
| 535 | reads_merged = 0; |
| 536 | sectors_read = 0; |
| 537 | read_time = 0; |
| 538 | writes = 0; |
| 539 | writes_merged = 0; |
| 540 | sectors_written = 0; |
| 541 | write_time = 0; |
| 542 | io = 0; |
| 543 | io_time = 0; |
| 544 | weighted_io_time = 0; |
| 545 | } |
| 546 | |
Peter Kasting | e08ca1334 | 2021-07-08 02:46:57 | [diff] [blame] | 547 | SystemDiskInfo::SystemDiskInfo(const SystemDiskInfo&) = default; |
| 548 | |
| 549 | SystemDiskInfo& SystemDiskInfo::operator=(const SystemDiskInfo&) = default; |
vmpstr | 7c787706 | 2016-02-18 22:12:24 | [diff] [blame] | 550 | |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 551 | Value::Dict SystemDiskInfo::ToDict() const { |
| 552 | Value::Dict res; |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 553 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 554 | // Write out uint64_t variables as doubles. |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 555 | // Note: this may discard some precision, but for JS there's no other option. |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 556 | res.Set("reads", static_cast<double>(reads)); |
| 557 | res.Set("reads_merged", static_cast<double>(reads_merged)); |
| 558 | res.Set("sectors_read", static_cast<double>(sectors_read)); |
| 559 | res.Set("read_time", static_cast<double>(read_time)); |
| 560 | res.Set("writes", static_cast<double>(writes)); |
| 561 | res.Set("writes_merged", static_cast<double>(writes_merged)); |
| 562 | res.Set("sectors_written", static_cast<double>(sectors_written)); |
| 563 | res.Set("write_time", static_cast<double>(write_time)); |
| 564 | res.Set("io", static_cast<double>(io)); |
| 565 | res.Set("io_time", static_cast<double>(io_time)); |
| 566 | res.Set("weighted_io_time", static_cast<double>(weighted_io_time)); |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 567 | |
Sylvain Defresne | 309ed42 | 2021-07-06 13:38:15 | [diff] [blame] | 568 | return res; |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 569 | } |
| 570 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 571 | bool IsValidDiskName(StringPiece candidate) { |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 572 | if (candidate.length() < 3) |
| 573 | return false; |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 574 | |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 575 | if (candidate[1] == 'd' && |
| 576 | (candidate[0] == 'h' || candidate[0] == 's' || candidate[0] == 'v')) { |
| 577 | // [hsv]d[a-z]+ case |
| 578 | for (size_t i = 2; i < candidate.length(); ++i) { |
Peter Kasting | 2c0c3ea | 2023-08-05 18:46:16 | [diff] [blame] | 579 | if (!absl::ascii_islower(static_cast<unsigned char>(candidate[i]))) { |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 580 | return false; |
Peter Kasting | 2c0c3ea | 2023-08-05 18:46:16 | [diff] [blame] | 581 | } |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 582 | } |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 583 | return true; |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 584 | } |
| 585 | |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 586 | const char kMMCName[] = "mmcblk"; |
Jan Wilken Dörrie | f05bb10 | 2020-08-18 19:35:56 | [diff] [blame] | 587 | if (!StartsWith(candidate, kMMCName)) |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 588 | return false; |
| 589 | |
| 590 | // mmcblk[0-9]+ case |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 591 | for (size_t i = strlen(kMMCName); i < candidate.length(); ++i) { |
Peter Kasting | 2c0c3ea | 2023-08-05 18:46:16 | [diff] [blame] | 592 | if (!absl::ascii_isdigit(static_cast<unsigned char>(candidate[i]))) { |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 593 | return false; |
Peter Kasting | 2c0c3ea | 2023-08-05 18:46:16 | [diff] [blame] | 594 | } |
thestig | a0d051ed | 2014-09-08 21:41:17 | [diff] [blame] | 595 | } |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 596 | return true; |
| 597 | } |
| 598 | |
| 599 | bool GetSystemDiskInfo(SystemDiskInfo* diskinfo) { |
thestig | 75844fdb | 2014-09-09 19:47:10 | [diff] [blame] | 600 | // Synchronously reading files in /proc does not hit the disk. |
Etienne Pierre-doray | 8927291 | 2022-11-07 22:35:38 | [diff] [blame] | 601 | ScopedAllowBlockingForProcessMetrics allow_blocking; |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 602 | |
| 603 | FilePath diskinfo_file("/proc/diskstats"); |
| 604 | std::string diskinfo_data; |
Brian Geffon | 49ddbf0 | 2020-06-19 22:57:56 | [diff] [blame] | 605 | if (!ReadFileToStringNonBlocking(diskinfo_file, &diskinfo_data)) { |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 606 | DLOG(WARNING) << "Failed to open " << diskinfo_file.value(); |
| 607 | return false; |
| 608 | } |
| 609 | |
brettw | 37fd9d7d | 2015-06-24 02:07:16 | [diff] [blame] | 610 | std::vector<StringPiece> diskinfo_lines = SplitStringPiece( |
| 611 | diskinfo_data, "\n", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 612 | if (diskinfo_lines.empty()) { |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 613 | DLOG(WARNING) << "No lines found"; |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | diskinfo->reads = 0; |
| 618 | diskinfo->reads_merged = 0; |
| 619 | diskinfo->sectors_read = 0; |
| 620 | diskinfo->read_time = 0; |
| 621 | diskinfo->writes = 0; |
| 622 | diskinfo->writes_merged = 0; |
| 623 | diskinfo->sectors_written = 0; |
| 624 | diskinfo->write_time = 0; |
| 625 | diskinfo->io = 0; |
| 626 | diskinfo->io_time = 0; |
| 627 | diskinfo->weighted_io_time = 0; |
| 628 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 629 | uint64_t reads = 0; |
| 630 | uint64_t reads_merged = 0; |
| 631 | uint64_t sectors_read = 0; |
| 632 | uint64_t read_time = 0; |
| 633 | uint64_t writes = 0; |
| 634 | uint64_t writes_merged = 0; |
| 635 | uint64_t sectors_written = 0; |
| 636 | uint64_t write_time = 0; |
| 637 | uint64_t io = 0; |
| 638 | uint64_t io_time = 0; |
| 639 | uint64_t weighted_io_time = 0; |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 640 | |
brettw | 37fd9d7d | 2015-06-24 02:07:16 | [diff] [blame] | 641 | for (const StringPiece& line : diskinfo_lines) { |
| 642 | std::vector<StringPiece> disk_fields = SplitStringPiece( |
| 643 | line, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 644 | |
| 645 | // Fields may have overflowed and reset to zero. |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 646 | if (!IsValidDiskName(disk_fields[kDiskDriveName])) |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 647 | continue; |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 648 | |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 649 | StringToUint64(disk_fields[kDiskReads], &reads); |
| 650 | StringToUint64(disk_fields[kDiskReadsMerged], &reads_merged); |
| 651 | StringToUint64(disk_fields[kDiskSectorsRead], §ors_read); |
| 652 | StringToUint64(disk_fields[kDiskReadTime], &read_time); |
| 653 | StringToUint64(disk_fields[kDiskWrites], &writes); |
| 654 | StringToUint64(disk_fields[kDiskWritesMerged], &writes_merged); |
| 655 | StringToUint64(disk_fields[kDiskSectorsWritten], §ors_written); |
| 656 | StringToUint64(disk_fields[kDiskWriteTime], &write_time); |
| 657 | StringToUint64(disk_fields[kDiskIO], &io); |
| 658 | StringToUint64(disk_fields[kDiskIOTime], &io_time); |
| 659 | StringToUint64(disk_fields[kDiskWeightedIOTime], &weighted_io_time); |
| 660 | |
| 661 | diskinfo->reads += reads; |
| 662 | diskinfo->reads_merged += reads_merged; |
| 663 | diskinfo->sectors_read += sectors_read; |
| 664 | diskinfo->read_time += read_time; |
| 665 | diskinfo->writes += writes; |
| 666 | diskinfo->writes_merged += writes_merged; |
| 667 | diskinfo->sectors_written += sectors_written; |
| 668 | diskinfo->write_time += write_time; |
| 669 | diskinfo->io += io; |
| 670 | diskinfo->io_time += io_time; |
| 671 | diskinfo->weighted_io_time += weighted_io_time; |
nduca@chromium.org | 669a0933 | 2013-08-30 22:59:14 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | return true; |
| 675 | } |
| 676 | |
thomasanderson | fc4688ab | 2016-09-10 01:19:43 | [diff] [blame] | 677 | TimeDelta GetUserCpuTimeSinceBoot() { |
| 678 | return internal::GetUserCpuTimeSinceBoot(); |
| 679 | } |
| 680 | |
Eric Willigers | 611cf54 | 2022-04-28 02:22:14 | [diff] [blame] | 681 | #if BUILDFLAG(IS_CHROMEOS) |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 682 | Value::Dict SwapInfo::ToDict() const { |
| 683 | Value::Dict res; |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 684 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 685 | // Write out uint64_t variables as doubles. |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 686 | // Note: this may discard some precision, but for JS there's no other option. |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 687 | res.Set("num_reads", static_cast<double>(num_reads)); |
| 688 | res.Set("num_writes", static_cast<double>(num_writes)); |
| 689 | res.Set("orig_data_size", static_cast<double>(orig_data_size)); |
| 690 | res.Set("compr_data_size", static_cast<double>(compr_data_size)); |
| 691 | res.Set("mem_used_total", static_cast<double>(mem_used_total)); |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 692 | double ratio = compr_data_size ? static_cast<double>(orig_data_size) / |
| 693 | static_cast<double>(compr_data_size) |
| 694 | : 0; |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 695 | res.Set("compression_ratio", ratio); |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 696 | |
Sylvain Defresne | 309ed42 | 2021-07-06 13:38:15 | [diff] [blame] | 697 | return res; |
jwmak@chromium.org | c2ef94e | 2013-09-06 13:13:23 | [diff] [blame] | 698 | } |
| 699 | |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 700 | Value::Dict GraphicsMemoryInfoKB::ToDict() const { |
| 701 | Value::Dict res; |
Kuo-Hsin Yang | 7cc65fc | 2020-10-12 14:02:27 | [diff] [blame] | 702 | |
Roman Sorokin | a8cf50eb6 | 2023-01-18 11:22:26 | [diff] [blame] | 703 | res.Set("gpu_objects", gpu_objects); |
| 704 | res.Set("gpu_memory_size", static_cast<double>(gpu_memory_size)); |
Sylvain Defresne | 309ed42 | 2021-07-06 13:38:15 | [diff] [blame] | 705 | |
| 706 | return res; |
Kuo-Hsin Yang | 7cc65fc | 2020-10-12 14:02:27 | [diff] [blame] | 707 | } |
| 708 | |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 709 | bool ParseZramMmStat(StringPiece mm_stat_data, SwapInfo* swap_info) { |
| 710 | // There are 7 columns in /sys/block/zram0/mm_stat, |
| 711 | // split by several spaces. The first three columns |
| 712 | // are orig_data_size, compr_data_size and mem_used_total. |
| 713 | // Example: |
| 714 | // 17715200 5008166 566062 0 1225715712 127 183842 |
| 715 | // |
| 716 | // For more details: |
| 717 | // https://www.kernel.org/doc/Documentation/blockdev/zram.txt |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 718 | |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 719 | std::vector<StringPiece> tokens = SplitStringPiece( |
| 720 | mm_stat_data, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 721 | if (tokens.size() < 7) { |
| 722 | DLOG(WARNING) << "zram mm_stat: tokens: " << tokens.size() |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 723 | << " malformed line: " << mm_stat_data; |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
| 727 | if (!StringToUint64(tokens[0], &swap_info->orig_data_size)) |
| 728 | return false; |
| 729 | if (!StringToUint64(tokens[1], &swap_info->compr_data_size)) |
| 730 | return false; |
| 731 | if (!StringToUint64(tokens[2], &swap_info->mem_used_total)) |
| 732 | return false; |
| 733 | |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | bool ParseZramStat(StringPiece stat_data, SwapInfo* swap_info) { |
| 738 | // There are 11 columns in /sys/block/zram0/stat, |
| 739 | // split by several spaces. The first column is read I/Os |
| 740 | // and fifth column is write I/Os. |
| 741 | // Example: |
| 742 | // 299 0 2392 0 1 0 8 0 0 0 0 |
| 743 | // |
| 744 | // For more details: |
| 745 | // https://www.kernel.org/doc/Documentation/blockdev/zram.txt |
| 746 | |
| 747 | std::vector<StringPiece> tokens = SplitStringPiece( |
| 748 | stat_data, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 749 | if (tokens.size() < 11) { |
| 750 | DLOG(WARNING) << "zram stat: tokens: " << tokens.size() |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 751 | << " malformed line: " << stat_data; |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | |
| 755 | if (!StringToUint64(tokens[0], &swap_info->num_reads)) |
| 756 | return false; |
| 757 | if (!StringToUint64(tokens[4], &swap_info->num_writes)) |
| 758 | return false; |
| 759 | |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | namespace { |
| 764 | |
| 765 | bool IgnoreZramFirstPage(uint64_t orig_data_size, SwapInfo* swap_info) { |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 766 | if (orig_data_size <= 4096) { |
| 767 | // A single page is compressed at startup, and has a high compression |
thestig | bc11dd6 | 2017-05-04 18:55:08 | [diff] [blame] | 768 | // ratio. Ignore this as it doesn't indicate any real swapping. |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 769 | swap_info->orig_data_size = 0; |
| 770 | swap_info->num_reads = 0; |
| 771 | swap_info->num_writes = 0; |
| 772 | swap_info->compr_data_size = 0; |
| 773 | swap_info->mem_used_total = 0; |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 774 | return true; |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 775 | } |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | |
| 779 | void ParseZramPath(SwapInfo* swap_info) { |
| 780 | FilePath zram_path("/sys/block/zram0"); |
| 781 | uint64_t orig_data_size = |
| 782 | ReadFileToUint64(zram_path.Append("orig_data_size")); |
| 783 | if (IgnoreZramFirstPage(orig_data_size, swap_info)) |
| 784 | return; |
| 785 | |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 786 | swap_info->orig_data_size = orig_data_size; |
| 787 | swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); |
| 788 | swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); |
| 789 | swap_info->compr_data_size = |
| 790 | ReadFileToUint64(zram_path.Append("compr_data_size")); |
| 791 | swap_info->mem_used_total = |
| 792 | ReadFileToUint64(zram_path.Append("mem_used_total")); |
| 793 | } |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 794 | |
| 795 | bool GetSwapInfoImpl(SwapInfo* swap_info) { |
| 796 | // Synchronously reading files in /sys/block/zram0 does not hit the disk. |
Etienne Pierre-doray | 8927291 | 2022-11-07 22:35:38 | [diff] [blame] | 797 | ScopedAllowBlockingForProcessMetrics allow_blocking; |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 798 | |
| 799 | // Since ZRAM update, it shows the usage data in different places. |
| 800 | // If file "/sys/block/zram0/mm_stat" exists, use the new way, otherwise, |
| 801 | // use the old way. |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 802 | static absl::optional<bool> use_new_zram_interface; |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 803 | FilePath zram_mm_stat_file("/sys/block/zram0/mm_stat"); |
| 804 | if (!use_new_zram_interface.has_value()) { |
| 805 | use_new_zram_interface = PathExists(zram_mm_stat_file); |
| 806 | } |
| 807 | |
Chung-Sheng Wu | 74f0a0e9 | 2017-08-01 07:17:02 | [diff] [blame] | 808 | if (!use_new_zram_interface.value()) { |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 809 | ParseZramPath(swap_info); |
| 810 | return true; |
| 811 | } |
| 812 | |
| 813 | std::string mm_stat_data; |
Brian Geffon | 49ddbf0 | 2020-06-19 22:57:56 | [diff] [blame] | 814 | if (!ReadFileToStringNonBlocking(zram_mm_stat_file, &mm_stat_data)) { |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 815 | DLOG(WARNING) << "Failed to open " << zram_mm_stat_file.value(); |
| 816 | return false; |
| 817 | } |
| 818 | if (!ParseZramMmStat(mm_stat_data, swap_info)) { |
| 819 | DLOG(WARNING) << "Failed to parse " << zram_mm_stat_file.value(); |
| 820 | return false; |
| 821 | } |
| 822 | if (IgnoreZramFirstPage(swap_info->orig_data_size, swap_info)) |
| 823 | return true; |
| 824 | |
| 825 | FilePath zram_stat_file("/sys/block/zram0/stat"); |
| 826 | std::string stat_data; |
Brian Geffon | 49ddbf0 | 2020-06-19 22:57:56 | [diff] [blame] | 827 | if (!ReadFileToStringNonBlocking(zram_stat_file, &stat_data)) { |
Chung-Sheng Wu | 838f843b | 2017-07-21 04:17:58 | [diff] [blame] | 828 | DLOG(WARNING) << "Failed to open " << zram_stat_file.value(); |
| 829 | return false; |
| 830 | } |
| 831 | if (!ParseZramStat(stat_data, swap_info)) { |
| 832 | DLOG(WARNING) << "Failed to parse " << zram_stat_file.value(); |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | } // namespace |
| 840 | |
| 841 | bool GetSwapInfo(SwapInfo* swap_info) { |
| 842 | if (!GetSwapInfoImpl(swap_info)) { |
| 843 | *swap_info = SwapInfo(); |
| 844 | return false; |
| 845 | } |
| 846 | return true; |
| 847 | } |
Kuo-Hsin Yang | 7cc65fc | 2020-10-12 14:02:27 | [diff] [blame] | 848 | |
| 849 | bool GetGraphicsMemoryInfo(GraphicsMemoryInfoKB* gpu_meminfo) { |
| 850 | #if defined(ARCH_CPU_X86_FAMILY) |
| 851 | // Reading i915_gem_objects on intel platform with kernel 5.4 is slow and is |
| 852 | // prohibited. |
| 853 | // TODO(b/170397975): Update if i915_gem_objects reading time is improved. |
| 854 | static bool is_newer_kernel = |
| 855 | base::StartsWith(base::SysInfo::KernelVersion(), "5."); |
| 856 | static bool is_intel_cpu = base::CPU().vendor_name() == "GenuineIntel"; |
| 857 | if (is_newer_kernel && is_intel_cpu) |
| 858 | return false; |
| 859 | #endif |
| 860 | |
| 861 | #if defined(ARCH_CPU_ARM_FAMILY) |
| 862 | const FilePath geminfo_path("/run/debugfs_gpu/exynos_gem_objects"); |
| 863 | #else |
| 864 | const FilePath geminfo_path("/run/debugfs_gpu/i915_gem_objects"); |
| 865 | #endif |
| 866 | std::string geminfo_data; |
| 867 | gpu_meminfo->gpu_objects = -1; |
| 868 | gpu_meminfo->gpu_memory_size = -1; |
| 869 | if (ReadFileToStringNonBlocking(geminfo_path, &geminfo_data)) { |
| 870 | int gpu_objects = -1; |
| 871 | int64_t gpu_memory_size = -1; |
| 872 | int num_res = sscanf(geminfo_data.c_str(), "%d objects, %" SCNd64 " bytes", |
| 873 | &gpu_objects, &gpu_memory_size); |
| 874 | if (num_res == 2) { |
| 875 | gpu_meminfo->gpu_objects = gpu_objects; |
| 876 | gpu_meminfo->gpu_memory_size = gpu_memory_size; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | #if defined(ARCH_CPU_ARM_FAMILY) |
| 881 | // Incorporate Mali graphics memory if present. |
| 882 | FilePath mali_memory_file("/sys/class/misc/mali0/device/memory"); |
| 883 | std::string mali_memory_data; |
| 884 | if (ReadFileToStringNonBlocking(mali_memory_file, &mali_memory_data)) { |
| 885 | int64_t mali_size = -1; |
| 886 | int num_res = |
| 887 | sscanf(mali_memory_data.c_str(), "%" SCNd64 " bytes", &mali_size); |
| 888 | if (num_res == 1) |
| 889 | gpu_meminfo->gpu_memory_size += mali_size; |
| 890 | } |
| 891 | #endif // defined(ARCH_CPU_ARM_FAMILY) |
| 892 | |
| 893 | return gpu_meminfo->gpu_memory_size != -1; |
| 894 | } |
| 895 | |
Eric Willigers | 611cf54 | 2022-04-28 02:22:14 | [diff] [blame] | 896 | #endif // BUILDFLAG(IS_CHROMEOS) |
nduca@chromium.org | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 897 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 898 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX) |
thestig | 75844fdb | 2014-09-09 19:47:10 | [diff] [blame] | 899 | int ProcessMetrics::GetIdleWakeupsPerSecond() { |
sabbakumov | 93c0968 | 2017-05-04 05:10:29 | [diff] [blame] | 900 | uint64_t num_switches; |
| 901 | static const char kSwitchStat[] = "voluntary_ctxt_switches"; |
Matthew Denton | f969883 | 2023-08-08 01:44:34 | [diff] [blame] | 902 | return internal::ReadProcStatusAndGetFieldAsUint64(process_, kSwitchStat, |
| 903 | &num_switches) |
sabbakumov | 93c0968 | 2017-05-04 05:10:29 | [diff] [blame] | 904 | ? CalculateIdleWakeupsPerSecond(num_switches) |
| 905 | : 0; |
thestig | 75844fdb | 2014-09-09 19:47:10 | [diff] [blame] | 906 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 907 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX) |
thestig | 75844fdb | 2014-09-09 19:47:10 | [diff] [blame] | 908 | |
rsesek@chromium.org | 32f5e9a | 2013-05-23 12:59:54 | [diff] [blame] | 909 | } // namespace base |