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