[go: nahoru, domu]

blob: a2657a7bc35a9617bdb79d5f49bb21d463583179 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
rsesek@chromium.org32f5e9a2013-05-23 12:59:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains internal routines that are called by other files in
6// base/process/.
7
danakj0a448602015-03-10 00:31:168#ifndef BASE_PROCESS_INTERNAL_LINUX_H_
9#define BASE_PROCESS_INTERNAL_LINUX_H_
rsesek@chromium.org32f5e9a2013-05-23 12:59:5410
avibeced7c2015-12-24 06:47:5911#include <stddef.h>
12#include <stdint.h>
earthdok@chromium.org50b710c2013-11-13 19:18:5913#include <unistd.h>
Sumaid Syed22f60eeb2021-08-26 05:16:2614#include <string>
15#include <vector>
earthdok@chromium.org50b710c2013-11-13 19:18:5916
Eric Seckler384f9532020-08-06 09:41:5617#include "base/files/dir_reader_posix.h"
rsesek@chromium.org32f5e9a2013-05-23 12:59:5418#include "base/files/file_path.h"
Eric Seckler384f9532020-08-06 09:41:5619#include "base/process/process_handle.h"
20#include "base/strings/string_number_conversions.h"
Matthew Dentonf9698832023-08-08 01:44:3421#include "base/strings/string_split.h"
Eric Seckler384f9532020-08-06 09:41:5622#include "base/threading/platform_thread.h"
rsesek@chromium.org32f5e9a2013-05-23 12:59:5423
24namespace base {
simonjam@chromium.org36e8fd42013-08-08 17:24:1825
26class Time;
27class TimeDelta;
28
rsesek@chromium.org32f5e9a2013-05-23 12:59:5429namespace internal {
30
31// "/proc"
32extern const char kProcDir[];
33
34// "stat"
35extern const char kStatFile[];
36
37// Returns a FilePath to "/proc/pid".
Joel Fernandes9a9d7572023-09-22 20:46:0538BASE_EXPORT base::FilePath GetProcPidDir(pid_t pid);
rsesek@chromium.org32f5e9a2013-05-23 12:59:5439
Eric Seckler9db21392020-06-12 08:48:1940// Reads a file from /proc into a string. This is allowed on any thread as
41// reading from /proc does not hit the disk. Returns true if the file can be
42// read and is non-empty.
43bool ReadProcFile(const FilePath& file, std::string* buffer);
44
rsesek@chromium.org32f5e9a2013-05-23 12:59:5445// Take a /proc directory entry named |d_name|, and if it is the directory for
46// a process, convert it to a pid_t.
47// Returns 0 on failure.
48// e.g. /proc/self/ will return 0, whereas /proc/1234 will return 1234.
49pid_t ProcDirSlotToPid(const char* d_name);
50
Matthew Dentonf9698832023-08-08 01:44:3451// Read |filename| in /proc/<pid>/, split the entries into key/value pairs, and
52// trim the key and value. On success, return true and write the trimmed
53// key/value pairs into |key_value_pairs|.
54bool ReadProcFileToTrimmedStringPairs(pid_t pid,
55 StringPiece filename,
56 StringPairs* key_value_pairs);
57
58// Read /proc/<pid>/status and return the value for |field|, or 0 on failure.
59// Only works for fields in the form of "Field: value kB".
60size_t ReadProcStatusAndGetKbFieldAsSizeT(pid_t pid, StringPiece field);
61
62// Read /proc/<pid>/status and look for |field|. On success, return true and
63// write the value for |field| into |result|.
64// Only works for fields in the form of "field : uint_value"
65bool ReadProcStatusAndGetFieldAsUint64(pid_t pid,
66 StringPiece field,
67 uint64_t* result);
68
rsesek@chromium.org32f5e9a2013-05-23 12:59:5469// Reads /proc/<pid>/stat into |buffer|. Returns true if the file can be read
70// and is non-empty.
71bool ReadProcStats(pid_t pid, std::string* buffer);
72
73// Takes |stats_data| and populates |proc_stats| with the values split by
74// spaces. Taking into account the 2nd field may, in itself, contain spaces.
75// Returns true if successful.
76bool ParseProcStats(const std::string& stats_data,
77 std::vector<std::string>* proc_stats);
78
79// Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
80// If the ordering ever changes, carefully review functions that use these
81// values.
82enum ProcStatsFields {
Benoit Lize448ee882017-08-31 13:32:1283 VM_COMM = 1, // Filename of executable, without parentheses.
84 VM_STATE = 2, // Letter indicating the state of the process.
85 VM_PPID = 3, // PID of the parent.
86 VM_PGRP = 4, // Process group id.
87 VM_MINFLT = 9, // Minor page fault count excluding children.
88 VM_MAJFLT = 11, // Major page fault count excluding children.
89 VM_UTIME = 13, // Time scheduled in user mode in clock ticks.
90 VM_STIME = 14, // Time scheduled in kernel mode in clock ticks.
91 VM_NUMTHREADS = 19, // Number of threads.
92 VM_STARTTIME = 21, // The time the process started in clock ticks.
93 VM_VSIZE = 22, // Virtual memory size in bytes.
94 VM_RSS = 23, // Resident Set Size in pages.
rsesek@chromium.org32f5e9a2013-05-23 12:59:5495};
96
97// Reads the |field_num|th field from |proc_stats|. Returns 0 on failure.
98// This version does not handle the first 3 values, since the first value is
99// simply |pid|, and the next two values are strings.
avibeced7c2015-12-24 06:47:59100int64_t GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
101 ProcStatsFields field_num);
rsesek@chromium.org32f5e9a2013-05-23 12:59:54102
sheu@chromium.orga98084792014-01-09 01:39:24103// Same as GetProcStatsFieldAsInt64(), but for size_t values.
rsesek@chromium.org32f5e9a2013-05-23 12:59:54104size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
105 ProcStatsFields field_num);
106
dcastagna4c25edc2017-02-23 19:35:51107// Convenience wrappers around GetProcStatsFieldAsInt64(), ParseProcStats() and
sheu@chromium.orga98084792014-01-09 01:39:24108// ReadProcStats(). See GetProcStatsFieldAsInt64() for details.
dcastagna4c25edc2017-02-23 19:35:51109int64_t ReadStatsFilendGetFieldAsInt64(const FilePath& stat_file,
110 ProcStatsFields field_num);
avibeced7c2015-12-24 06:47:59111int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num);
dcastagna4c25edc2017-02-23 19:35:51112int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num);
rsesek@chromium.org32f5e9a2013-05-23 12:59:54113
sheu@chromium.orga98084792014-01-09 01:39:24114// Same as ReadProcStatsAndGetFieldAsInt64() but for size_t values.
rsesek@chromium.org32f5e9a2013-05-23 12:59:54115size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
116 ProcStatsFields field_num);
117
simonjam@chromium.org36e8fd42013-08-08 17:24:18118// Returns the time that the OS started. Clock ticks are relative to this.
119Time GetBootTime();
120
thomasandersonfc4688ab2016-09-10 01:19:43121// Returns the amount of time spent in user space since boot across all CPUs.
122TimeDelta GetUserCpuTimeSinceBoot();
123
simonjam@chromium.org36e8fd42013-08-08 17:24:18124// Converts Linux clock ticks to a wall time delta.
Peter Kastingaca170a2022-06-30 17:18:48125TimeDelta ClockTicksToTimeDelta(int64_t clock_ticks);
simonjam@chromium.org36e8fd42013-08-08 17:24:18126
Eric Seckler384f9532020-08-06 09:41:56127// Executes the lambda for every task in the process's /proc/<pid>/task
128// directory. The thread id and file path of the task directory are provided as
129// arguments to the lambda.
130template <typename Lambda>
131void ForEachProcessTask(base::ProcessHandle process, Lambda&& lambda) {
132 // Iterate through the different threads tracked in /proc/<pid>/task.
133 FilePath fd_path = GetProcPidDir(process).Append("task");
134
135 DirReaderPosix dir_reader(fd_path.value().c_str());
136 if (!dir_reader.IsValid())
137 return;
138
139 for (; dir_reader.Next();) {
140 const char* tid_str = dir_reader.name();
141 if (strcmp(tid_str, ".") == 0 || strcmp(tid_str, "..") == 0)
142 continue;
143
144 PlatformThreadId tid;
145 if (!StringToInt(tid_str, &tid))
146 continue;
147
148 FilePath task_path = fd_path.Append(tid_str);
149 lambda(tid, task_path);
150 }
151}
152
rsesek@chromium.org32f5e9a2013-05-23 12:59:54153} // namespace internal
154} // namespace base
155
danakj0a448602015-03-10 00:31:16156#endif // BASE_PROCESS_INTERNAL_LINUX_H_