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