internal_linux.h 3.8 KB

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