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