internal_linux.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/dir_reader_posix.h"
  12. #include "base/files/file_path.h"
  13. #include "base/process/process_handle.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/threading/platform_thread.h"
  16. namespace base {
  17. class Time;
  18. class TimeDelta;
  19. namespace internal {
  20. // "/proc"
  21. extern const char kProcDir[];
  22. // "stat"
  23. extern const char kStatFile[];
  24. // Returns a FilePath to "/proc/pid".
  25. base::FilePath GetProcPidDir(pid_t pid);
  26. // Reads a file from /proc into a string. This is allowed on any thread as
  27. // reading from /proc does not hit the disk. Returns true if the file can be
  28. // read and is non-empty.
  29. bool ReadProcFile(const FilePath& file, std::string* buffer);
  30. // Take a /proc directory entry named |d_name|, and if it is the directory for
  31. // a process, convert it to a pid_t.
  32. // Returns 0 on failure.
  33. // e.g. /proc/self/ will return 0, whereas /proc/1234 will return 1234.
  34. pid_t ProcDirSlotToPid(const char* d_name);
  35. // Reads /proc/<pid>/stat into |buffer|. Returns true if the file can be read
  36. // and is non-empty.
  37. bool ReadProcStats(pid_t pid, std::string* buffer);
  38. // Takes |stats_data| and populates |proc_stats| with the values split by
  39. // spaces. Taking into account the 2nd field may, in itself, contain spaces.
  40. // Returns true if successful.
  41. bool ParseProcStats(const std::string& stats_data,
  42. std::vector<std::string>* proc_stats);
  43. // Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
  44. // If the ordering ever changes, carefully review functions that use these
  45. // values.
  46. enum ProcStatsFields {
  47. VM_COMM = 1, // Filename of executable, without parentheses.
  48. VM_STATE = 2, // Letter indicating the state of the process.
  49. VM_PPID = 3, // PID of the parent.
  50. VM_PGRP = 4, // Process group id.
  51. VM_MINFLT = 9, // Minor page fault count excluding children.
  52. VM_MAJFLT = 11, // Major page fault count excluding children.
  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. // Reads the |field_num|th field from |proc_stats|. Returns 0 on failure.
  61. // This version does not handle the first 3 values, since the first value is
  62. // simply |pid|, and the next two values are strings.
  63. int64_t GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
  64. ProcStatsFields field_num);
  65. // Same as GetProcStatsFieldAsInt64(), but for size_t values.
  66. size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
  67. ProcStatsFields field_num);
  68. // Convenience wrappers around GetProcStatsFieldAsInt64(), ParseProcStats() and
  69. // ReadProcStats(). See GetProcStatsFieldAsInt64() for details.
  70. int64_t ReadStatsFilendGetFieldAsInt64(const FilePath& stat_file,
  71. ProcStatsFields field_num);
  72. int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num);
  73. int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num);
  74. // Same as ReadProcStatsAndGetFieldAsInt64() but for size_t values.
  75. size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
  76. ProcStatsFields field_num);
  77. // Returns the time that the OS started. Clock ticks are relative to this.
  78. Time GetBootTime();
  79. // Returns the amount of time spent in user space since boot across all CPUs.
  80. TimeDelta GetUserCpuTimeSinceBoot();
  81. // Converts Linux clock ticks to a wall time delta.
  82. TimeDelta ClockTicksToTimeDelta(int clock_ticks);
  83. // Executes the lambda for every task in the process's /proc/<pid>/task
  84. // directory. The thread id and file path of the task directory are provided as
  85. // arguments to the lambda.
  86. template <typename Lambda>
  87. void ForEachProcessTask(base::ProcessHandle process, Lambda&& lambda) {
  88. // Iterate through the different threads tracked in /proc/<pid>/task.
  89. FilePath fd_path = GetProcPidDir(process).Append("task");
  90. DirReaderPosix dir_reader(fd_path.value().c_str());
  91. if (!dir_reader.IsValid())
  92. return;
  93. for (; dir_reader.Next();) {
  94. const char* tid_str = dir_reader.name();
  95. if (strcmp(tid_str, ".") == 0 || strcmp(tid_str, "..") == 0)
  96. continue;
  97. PlatformThreadId tid;
  98. if (!StringToInt(tid_str, &tid))
  99. continue;
  100. FilePath task_path = fd_path.Append(tid_str);
  101. lambda(tid, task_path);
  102. }
  103. }
  104. } // namespace internal
  105. } // namespace base
  106. #endif // BASE_PROCESS_INTERNAL_LINUX_H_