xref: /aosp_15_r20/external/libchrome/base/process/internal_linux.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker // This file contains internal routines that are called by other files in
6*635a8641SAndroid Build Coastguard Worker // base/process/.
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_INTERNAL_LINUX_H_
9*635a8641SAndroid Build Coastguard Worker #define BASE_PROCESS_INTERNAL_LINUX_H_
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
12*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
13*635a8641SAndroid Build Coastguard Worker #include <unistd.h>
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace base {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker class Time;
20*635a8641SAndroid Build Coastguard Worker class TimeDelta;
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace internal {
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker // "/proc"
25*635a8641SAndroid Build Coastguard Worker extern const char kProcDir[];
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker // "stat"
28*635a8641SAndroid Build Coastguard Worker extern const char kStatFile[];
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker // Returns a FilePath to "/proc/pid".
31*635a8641SAndroid Build Coastguard Worker base::FilePath GetProcPidDir(pid_t pid);
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker // Take a /proc directory entry named |d_name|, and if it is the directory for
34*635a8641SAndroid Build Coastguard Worker // a process, convert it to a pid_t.
35*635a8641SAndroid Build Coastguard Worker // Returns 0 on failure.
36*635a8641SAndroid Build Coastguard Worker // e.g. /proc/self/ will return 0, whereas /proc/1234 will return 1234.
37*635a8641SAndroid Build Coastguard Worker pid_t ProcDirSlotToPid(const char* d_name);
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker // Reads /proc/<pid>/stat into |buffer|. Returns true if the file can be read
40*635a8641SAndroid Build Coastguard Worker // and is non-empty.
41*635a8641SAndroid Build Coastguard Worker bool ReadProcStats(pid_t pid, std::string* buffer);
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker // Takes |stats_data| and populates |proc_stats| with the values split by
44*635a8641SAndroid Build Coastguard Worker // spaces. Taking into account the 2nd field may, in itself, contain spaces.
45*635a8641SAndroid Build Coastguard Worker // Returns true if successful.
46*635a8641SAndroid Build Coastguard Worker bool ParseProcStats(const std::string& stats_data,
47*635a8641SAndroid Build Coastguard Worker                     std::vector<std::string>* proc_stats);
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker // Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
50*635a8641SAndroid Build Coastguard Worker // If the ordering ever changes, carefully review functions that use these
51*635a8641SAndroid Build Coastguard Worker // values.
52*635a8641SAndroid Build Coastguard Worker enum ProcStatsFields {
53*635a8641SAndroid Build Coastguard Worker   VM_COMM = 1,         // Filename of executable, without parentheses.
54*635a8641SAndroid Build Coastguard Worker   VM_STATE = 2,        // Letter indicating the state of the process.
55*635a8641SAndroid Build Coastguard Worker   VM_PPID = 3,         // PID of the parent.
56*635a8641SAndroid Build Coastguard Worker   VM_PGRP = 4,         // Process group id.
57*635a8641SAndroid Build Coastguard Worker   VM_MINFLT = 9,       // Minor page fault count excluding children.
58*635a8641SAndroid Build Coastguard Worker   VM_MAJFLT = 11,      // Major page fault count excluding children.
59*635a8641SAndroid Build Coastguard Worker   VM_UTIME = 13,       // Time scheduled in user mode in clock ticks.
60*635a8641SAndroid Build Coastguard Worker   VM_STIME = 14,       // Time scheduled in kernel mode in clock ticks.
61*635a8641SAndroid Build Coastguard Worker   VM_NUMTHREADS = 19,  // Number of threads.
62*635a8641SAndroid Build Coastguard Worker   VM_STARTTIME = 21,   // The time the process started in clock ticks.
63*635a8641SAndroid Build Coastguard Worker   VM_VSIZE = 22,       // Virtual memory size in bytes.
64*635a8641SAndroid Build Coastguard Worker   VM_RSS = 23,         // Resident Set Size in pages.
65*635a8641SAndroid Build Coastguard Worker };
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker // Reads the |field_num|th field from |proc_stats|. Returns 0 on failure.
68*635a8641SAndroid Build Coastguard Worker // This version does not handle the first 3 values, since the first value is
69*635a8641SAndroid Build Coastguard Worker // simply |pid|, and the next two values are strings.
70*635a8641SAndroid Build Coastguard Worker int64_t GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
71*635a8641SAndroid Build Coastguard Worker                                  ProcStatsFields field_num);
72*635a8641SAndroid Build Coastguard Worker 
73*635a8641SAndroid Build Coastguard Worker // Same as GetProcStatsFieldAsInt64(), but for size_t values.
74*635a8641SAndroid Build Coastguard Worker size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
75*635a8641SAndroid Build Coastguard Worker                                 ProcStatsFields field_num);
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker // Convenience wrappers around GetProcStatsFieldAsInt64(), ParseProcStats() and
78*635a8641SAndroid Build Coastguard Worker // ReadProcStats(). See GetProcStatsFieldAsInt64() for details.
79*635a8641SAndroid Build Coastguard Worker int64_t ReadStatsFilendGetFieldAsInt64(const FilePath& stat_file,
80*635a8641SAndroid Build Coastguard Worker                                        ProcStatsFields field_num);
81*635a8641SAndroid Build Coastguard Worker int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num);
82*635a8641SAndroid Build Coastguard Worker int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num);
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker // Same as ReadProcStatsAndGetFieldAsInt64() but for size_t values.
85*635a8641SAndroid Build Coastguard Worker size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
86*635a8641SAndroid Build Coastguard Worker                                        ProcStatsFields field_num);
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker // Returns the time that the OS started. Clock ticks are relative to this.
89*635a8641SAndroid Build Coastguard Worker Time GetBootTime();
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker // Returns the amount of time spent in user space since boot across all CPUs.
92*635a8641SAndroid Build Coastguard Worker TimeDelta GetUserCpuTimeSinceBoot();
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker // Converts Linux clock ticks to a wall time delta.
95*635a8641SAndroid Build Coastguard Worker TimeDelta ClockTicksToTimeDelta(int clock_ticks);
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker }  // namespace internal
98*635a8641SAndroid Build Coastguard Worker }  // namespace base
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker #endif  // BASE_PROCESS_INTERNAL_LINUX_H_
101