Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 32f5e9a0 | 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 | |
Tom Sepez | 8726d30e | 2025-01-29 02:11:08 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/390223051): Remove C-library calls to fix the errors. |
| 7 | #pragma allow_unsafe_libc_calls |
| 8 | #endif |
| 9 | |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 10 | // This file contains internal routines that are called by other files in |
| 11 | // base/process/. |
| 12 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 13 | #ifndef BASE_PROCESS_INTERNAL_LINUX_H_ |
| 14 | #define BASE_PROCESS_INTERNAL_LINUX_H_ |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 15 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 16 | #include <stddef.h> |
| 17 | #include <stdint.h> |
[email protected] | 50b710c | 2013-11-13 19:18:59 | [diff] [blame] | 18 | #include <unistd.h> |
Helmut Januschka | bfa62f7 | 2024-04-04 15:18:31 | [diff] [blame] | 19 | |
Joe Mason | ba14528 | 2024-03-12 20:18:19 | [diff] [blame] | 20 | #include <optional> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 21 | #include <string> |
Helmut Januschka | bfa62f7 | 2024-04-04 15:18:31 | [diff] [blame] | 22 | #include <string_view> |
Sumaid Syed | 22f60eeb | 2021-08-26 05:16:26 | [diff] [blame] | 23 | #include <vector> |
[email protected] | 50b710c | 2013-11-13 19:18:59 | [diff] [blame] | 24 | |
Joe Mason | ba14528 | 2024-03-12 20:18:19 | [diff] [blame] | 25 | #include "base/containers/span.h" |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 26 | #include "base/files/dir_reader_posix.h" |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 27 | #include "base/files/file_path.h" |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 28 | #include "base/process/process_handle.h" |
| 29 | #include "base/strings/string_number_conversions.h" |
Matthew Denton | f96988342 | 2023-08-08 01:44:34 | [diff] [blame] | 30 | #include "base/strings/string_split.h" |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 31 | #include "base/threading/platform_thread.h" |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 32 | |
| 33 | namespace base { |
[email protected] | 36e8fd4 | 2013-08-08 17:24:18 | [diff] [blame] | 34 | |
| 35 | class Time; |
| 36 | class TimeDelta; |
| 37 | |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 38 | namespace internal { |
| 39 | |
| 40 | // "/proc" |
| 41 | extern const char kProcDir[]; |
| 42 | |
| 43 | // "stat" |
| 44 | extern const char kStatFile[]; |
| 45 | |
| 46 | // Returns a FilePath to "/proc/pid". |
Joel Fernandes | 9a9d757 | 2023-09-22 20:46:05 | [diff] [blame] | 47 | BASE_EXPORT base::FilePath GetProcPidDir(pid_t pid); |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 48 | |
Eric Seckler | 9db2139 | 2020-06-12 08:48:19 | [diff] [blame] | 49 | // Reads a file from /proc into a string. This is allowed on any thread as |
| 50 | // reading from /proc does not hit the disk. Returns true if the file can be |
| 51 | // read and is non-empty. |
| 52 | bool ReadProcFile(const FilePath& file, std::string* buffer); |
| 53 | |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 54 | // Take a /proc directory entry named |d_name|, and if it is the directory for |
| 55 | // a process, convert it to a pid_t. |
| 56 | // Returns 0 on failure. |
| 57 | // e.g. /proc/self/ will return 0, whereas /proc/1234 will return 1234. |
Jan Keitel | 26cbcde5 | 2024-08-12 07:02:00 | [diff] [blame] | 58 | pid_t ProcDirSlotToPid(std::string_view d_name); |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 59 | |
Joe Mason | 484bfa5 | 2025-05-16 21:14:40 | [diff] [blame] | 60 | // Read `filename` in /proc/<pid>/, split the entries into key/value pairs, and |
| 61 | // trim the key and value. On success, writes the file contents into `buffer` |
| 62 | // and returns the trimmed key/value pairs as views into that buffer. On |
| 63 | // failure, returns nullopt (the buffer contents may or may not be changed). |
| 64 | std::optional<StringViewPairs> ReadProcFileToTrimmedStringPairs( |
| 65 | pid_t pid, |
| 66 | std::string_view filename, |
| 67 | std::string* buffer); |
Matthew Denton | f96988342 | 2023-08-08 01:44:34 | [diff] [blame] | 68 | |
| 69 | // Read /proc/<pid>/status and return the value for |field|, or 0 on failure. |
| 70 | // Only works for fields in the form of "Field: value kB". |
Helmut Januschka | bfa62f7 | 2024-04-04 15:18:31 | [diff] [blame] | 71 | size_t ReadProcStatusAndGetKbFieldAsSizeT(pid_t pid, std::string_view field); |
Matthew Denton | f96988342 | 2023-08-08 01:44:34 | [diff] [blame] | 72 | |
| 73 | // Read /proc/<pid>/status and look for |field|. On success, return true and |
| 74 | // write the value for |field| into |result|. |
| 75 | // Only works for fields in the form of "field : uint_value" |
| 76 | bool ReadProcStatusAndGetFieldAsUint64(pid_t pid, |
Helmut Januschka | bfa62f7 | 2024-04-04 15:18:31 | [diff] [blame] | 77 | std::string_view field, |
Matthew Denton | f96988342 | 2023-08-08 01:44:34 | [diff] [blame] | 78 | uint64_t* result); |
| 79 | |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 80 | // Reads /proc/<pid>/stat into |buffer|. Returns true if the file can be read |
| 81 | // and is non-empty. |
| 82 | bool ReadProcStats(pid_t pid, std::string* buffer); |
| 83 | |
Joe Mason | 484bfa5 | 2025-05-16 21:14:40 | [diff] [blame] | 84 | // Takes `stats_data` and populates `proc_stats` with the values split by |
| 85 | // spaces, as views into `stats_data`, taking into account the 2nd field may |
| 86 | // itself contain spaces. Returns true if successful. |
| 87 | bool ParseProcStats(std::string_view stats_data, |
| 88 | std::vector<std::string_view>* proc_stats); |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 89 | |
| 90 | // Fields from /proc/<pid>/stat, 0-based. See man 5 proc. |
| 91 | // If the ordering ever changes, carefully review functions that use these |
| 92 | // values. |
| 93 | enum ProcStatsFields { |
Benoit Lize | 448ee88 | 2017-08-31 13:32:12 | [diff] [blame] | 94 | VM_COMM = 1, // Filename of executable, without parentheses. |
| 95 | VM_STATE = 2, // Letter indicating the state of the process. |
| 96 | VM_PPID = 3, // PID of the parent. |
| 97 | VM_PGRP = 4, // Process group id. |
| 98 | VM_MINFLT = 9, // Minor page fault count excluding children. |
| 99 | VM_MAJFLT = 11, // Major page fault count excluding children. |
| 100 | VM_UTIME = 13, // Time scheduled in user mode in clock ticks. |
| 101 | VM_STIME = 14, // Time scheduled in kernel mode in clock ticks. |
| 102 | VM_NUMTHREADS = 19, // Number of threads. |
| 103 | VM_STARTTIME = 21, // The time the process started in clock ticks. |
| 104 | VM_VSIZE = 22, // Virtual memory size in bytes. |
| 105 | VM_RSS = 23, // Resident Set Size in pages. |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | // Reads the |field_num|th field from |proc_stats|. Returns 0 on failure. |
| 109 | // This version does not handle the first 3 values, since the first value is |
| 110 | // simply |pid|, and the next two values are strings. |
Joe Mason | 484bfa5 | 2025-05-16 21:14:40 | [diff] [blame] | 111 | int64_t GetProcStatsFieldAsInt64(base::span<std::string_view> proc_stats, |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 112 | ProcStatsFields field_num); |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 113 | |
Joe Mason | ba14528 | 2024-03-12 20:18:19 | [diff] [blame] | 114 | // Reads the `field_num`th field from `proc_stats`. Asserts that `field_num` is |
| 115 | // a valid index into `proc_stats`. Returns nullopt if the field doesn't contain |
| 116 | // a valid integer. |
| 117 | std::optional<int64_t> GetProcStatsFieldAsOptionalInt64( |
Joe Mason | 484bfa5 | 2025-05-16 21:14:40 | [diff] [blame] | 118 | base::span<std::string_view> proc_stats, |
Joe Mason | ba14528 | 2024-03-12 20:18:19 | [diff] [blame] | 119 | ProcStatsFields field_num); |
| 120 | |
[email protected] | a9808479 | 2014-01-09 01:39:24 | [diff] [blame] | 121 | // Same as GetProcStatsFieldAsInt64(), but for size_t values. |
Joe Mason | 484bfa5 | 2025-05-16 21:14:40 | [diff] [blame] | 122 | size_t GetProcStatsFieldAsSizeT(base::span<std::string_view> proc_stats, |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 123 | ProcStatsFields field_num); |
| 124 | |
dcastagna | 4c25edc | 2017-02-23 19:35:51 | [diff] [blame] | 125 | // Convenience wrappers around GetProcStatsFieldAsInt64(), ParseProcStats() and |
[email protected] | a9808479 | 2014-01-09 01:39:24 | [diff] [blame] | 126 | // ReadProcStats(). See GetProcStatsFieldAsInt64() for details. |
dcastagna | 4c25edc | 2017-02-23 19:35:51 | [diff] [blame] | 127 | int64_t ReadStatsFilendGetFieldAsInt64(const FilePath& stat_file, |
| 128 | ProcStatsFields field_num); |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 129 | int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num); |
dcastagna | 4c25edc | 2017-02-23 19:35:51 | [diff] [blame] | 130 | int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num); |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 131 | |
[email protected] | a9808479 | 2014-01-09 01:39:24 | [diff] [blame] | 132 | // Same as ReadProcStatsAndGetFieldAsInt64() but for size_t values. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 133 | size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid, ProcStatsFields field_num); |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 134 | |
[email protected] | 36e8fd4 | 2013-08-08 17:24:18 | [diff] [blame] | 135 | // Returns the time that the OS started. Clock ticks are relative to this. |
| 136 | Time GetBootTime(); |
| 137 | |
thomasanderson | fc4688ab | 2016-09-10 01:19:43 | [diff] [blame] | 138 | // Returns the amount of time spent in user space since boot across all CPUs. |
| 139 | TimeDelta GetUserCpuTimeSinceBoot(); |
| 140 | |
[email protected] | 36e8fd4 | 2013-08-08 17:24:18 | [diff] [blame] | 141 | // Converts Linux clock ticks to a wall time delta. |
Peter Kasting | aca170a | 2022-06-30 17:18:48 | [diff] [blame] | 142 | TimeDelta ClockTicksToTimeDelta(int64_t clock_ticks); |
[email protected] | 36e8fd4 | 2013-08-08 17:24:18 | [diff] [blame] | 143 | |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 144 | // Executes the lambda for every task in the process's /proc/<pid>/task |
| 145 | // directory. The thread id and file path of the task directory are provided as |
| 146 | // arguments to the lambda. |
| 147 | template <typename Lambda> |
| 148 | void ForEachProcessTask(base::ProcessHandle process, Lambda&& lambda) { |
| 149 | // Iterate through the different threads tracked in /proc/<pid>/task. |
| 150 | FilePath fd_path = GetProcPidDir(process).Append("task"); |
| 151 | |
| 152 | DirReaderPosix dir_reader(fd_path.value().c_str()); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 153 | if (!dir_reader.IsValid()) { |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 154 | return; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 155 | } |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 156 | |
| 157 | for (; dir_reader.Next();) { |
| 158 | const char* tid_str = dir_reader.name(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 159 | if (strcmp(tid_str, ".") == 0 || strcmp(tid_str, "..") == 0) { |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 160 | continue; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 161 | } |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 162 | |
Leszek Swirski | b3fb4232 | 2025-02-06 12:16:57 | [diff] [blame] | 163 | PlatformThreadId::UnderlyingType tid_value; |
| 164 | if (!StringToInt(tid_str, &tid_value)) { |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 165 | continue; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 166 | } |
Leszek Swirski | b3fb4232 | 2025-02-06 12:16:57 | [diff] [blame] | 167 | PlatformThreadId tid(tid_value); |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 168 | |
| 169 | FilePath task_path = fd_path.Append(tid_str); |
| 170 | lambda(tid, task_path); |
| 171 | } |
| 172 | } |
| 173 | |
[email protected] | 32f5e9a0 | 2013-05-23 12:59:54 | [diff] [blame] | 174 | } // namespace internal |
| 175 | } // namespace base |
| 176 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 177 | #endif // BASE_PROCESS_INTERNAL_LINUX_H_ |