PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/base/process/internal_linux.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 188 lines | 138 code | 28 blank | 22 comment | 21 complexity | 46f38af769d21184064e8c236929b345 MD5 | raw file
  1. // Copyright (c) 2012 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. #include "base/process/internal_linux.h"
  5. #include <unistd.h>
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/files/file_util.h"
  10. #include "base/logging.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/strings/string_split.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/threading/thread_restrictions.h"
  15. #include "base/time/time.h"
  16. namespace base {
  17. namespace internal {
  18. const char kProcDir[] = "/proc";
  19. const char kStatFile[] = "stat";
  20. FilePath GetProcPidDir(pid_t pid) {
  21. return FilePath(kProcDir).Append(IntToString(pid));
  22. }
  23. pid_t ProcDirSlotToPid(const char* d_name) {
  24. int i;
  25. for (i = 0; i < NAME_MAX && d_name[i]; ++i) {
  26. if (!IsAsciiDigit(d_name[i])) {
  27. return 0;
  28. }
  29. }
  30. if (i == NAME_MAX)
  31. return 0;
  32. // Read the process's command line.
  33. pid_t pid;
  34. std::string pid_string(d_name);
  35. if (!StringToInt(pid_string, &pid)) {
  36. NOTREACHED();
  37. return 0;
  38. }
  39. return pid;
  40. }
  41. bool ReadProcFile(const FilePath& file, std::string* buffer) {
  42. buffer->clear();
  43. // Synchronously reading files in /proc is safe.
  44. ThreadRestrictions::ScopedAllowIO allow_io;
  45. if (!ReadFileToString(file, buffer)) {
  46. DLOG(WARNING) << "Failed to read " << file.MaybeAsASCII();
  47. return false;
  48. }
  49. return !buffer->empty();
  50. }
  51. bool ReadProcStats(pid_t pid, std::string* buffer) {
  52. FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile);
  53. return ReadProcFile(stat_file, buffer);
  54. }
  55. bool ParseProcStats(const std::string& stats_data,
  56. std::vector<std::string>* proc_stats) {
  57. // |stats_data| may be empty if the process disappeared somehow.
  58. // e.g. http://crbug.com/145811
  59. if (stats_data.empty())
  60. return false;
  61. // The stat file is formatted as:
  62. // pid (process name) data1 data2 .... dataN
  63. // Look for the closing paren by scanning backwards, to avoid being fooled by
  64. // processes with ')' in the name.
  65. size_t open_parens_idx = stats_data.find(" (");
  66. size_t close_parens_idx = stats_data.rfind(") ");
  67. if (open_parens_idx == std::string::npos ||
  68. close_parens_idx == std::string::npos ||
  69. open_parens_idx > close_parens_idx) {
  70. DLOG(WARNING) << "Failed to find matched parens in '" << stats_data << "'";
  71. NOTREACHED();
  72. return false;
  73. }
  74. open_parens_idx++;
  75. proc_stats->clear();
  76. // PID.
  77. proc_stats->push_back(stats_data.substr(0, open_parens_idx));
  78. // Process name without parentheses.
  79. proc_stats->push_back(
  80. stats_data.substr(open_parens_idx + 1,
  81. close_parens_idx - (open_parens_idx + 1)));
  82. // Split the rest.
  83. std::vector<std::string> other_stats = SplitString(
  84. stats_data.substr(close_parens_idx + 2), " ",
  85. base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  86. for (size_t i = 0; i < other_stats.size(); ++i)
  87. proc_stats->push_back(other_stats[i]);
  88. return true;
  89. }
  90. typedef std::map<std::string, std::string> ProcStatMap;
  91. void ParseProcStat(const std::string& contents, ProcStatMap* output) {
  92. StringPairs key_value_pairs;
  93. SplitStringIntoKeyValuePairs(contents, ' ', '\n', &key_value_pairs);
  94. for (size_t i = 0; i < key_value_pairs.size(); ++i) {
  95. output->insert(key_value_pairs[i]);
  96. }
  97. }
  98. int64 GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats,
  99. ProcStatsFields field_num) {
  100. DCHECK_GE(field_num, VM_PPID);
  101. CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());
  102. int64 value;
  103. return StringToInt64(proc_stats[field_num], &value) ? value : 0;
  104. }
  105. size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
  106. ProcStatsFields field_num) {
  107. DCHECK_GE(field_num, VM_PPID);
  108. CHECK_LT(static_cast<size_t>(field_num), proc_stats.size());
  109. size_t value;
  110. return StringToSizeT(proc_stats[field_num], &value) ? value : 0;
  111. }
  112. int64 ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num) {
  113. std::string stats_data;
  114. if (!ReadProcStats(pid, &stats_data))
  115. return 0;
  116. std::vector<std::string> proc_stats;
  117. if (!ParseProcStats(stats_data, &proc_stats))
  118. return 0;
  119. return GetProcStatsFieldAsInt64(proc_stats, field_num);
  120. }
  121. size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
  122. ProcStatsFields field_num) {
  123. std::string stats_data;
  124. if (!ReadProcStats(pid, &stats_data))
  125. return 0;
  126. std::vector<std::string> proc_stats;
  127. if (!ParseProcStats(stats_data, &proc_stats))
  128. return 0;
  129. return GetProcStatsFieldAsSizeT(proc_stats, field_num);
  130. }
  131. Time GetBootTime() {
  132. FilePath path("/proc/stat");
  133. std::string contents;
  134. if (!ReadProcFile(path, &contents))
  135. return Time();
  136. ProcStatMap proc_stat;
  137. ParseProcStat(contents, &proc_stat);
  138. ProcStatMap::const_iterator btime_it = proc_stat.find("btime");
  139. if (btime_it == proc_stat.end())
  140. return Time();
  141. int btime;
  142. if (!StringToInt(btime_it->second, &btime))
  143. return Time();
  144. return Time::FromTimeT(btime);
  145. }
  146. TimeDelta ClockTicksToTimeDelta(int clock_ticks) {
  147. // This queries the /proc-specific scaling factor which is
  148. // conceptually the system hertz. To dump this value on another
  149. // system, try
  150. // od -t dL /proc/self/auxv
  151. // and look for the number after 17 in the output; mine is
  152. // 0000040 17 100 3 134512692
  153. // which means the answer is 100.
  154. // It may be the case that this value is always 100.
  155. static const int kHertz = sysconf(_SC_CLK_TCK);
  156. return TimeDelta::FromMicroseconds(
  157. Time::kMicrosecondsPerSecond * clock_ticks / kHertz);
  158. }
  159. } // namespace internal
  160. } // namespace base