| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process/internal_linux.h" | 5 #include "base/process/internal_linux.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include <map> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 19 #include "base/time/time.h" |
| 18 | 20 |
| 19 namespace base { | 21 namespace base { |
| 20 namespace internal { | 22 namespace internal { |
| 21 | 23 |
| 22 const char kProcDir[] = "/proc"; | 24 const char kProcDir[] = "/proc"; |
| 23 | 25 |
| 24 const char kStatFile[] = "stat"; | 26 const char kStatFile[] = "stat"; |
| 25 | 27 |
| 26 base::FilePath GetProcPidDir(pid_t pid) { | 28 base::FilePath GetProcPidDir(pid_t pid) { |
| 27 return base::FilePath(kProcDir).Append(IntToString(pid)); | 29 return base::FilePath(kProcDir).Append(IntToString(pid)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 // Read the process's command line. | 42 // Read the process's command line. |
| 41 pid_t pid; | 43 pid_t pid; |
| 42 std::string pid_string(d_name); | 44 std::string pid_string(d_name); |
| 43 if (!StringToInt(pid_string, &pid)) { | 45 if (!StringToInt(pid_string, &pid)) { |
| 44 NOTREACHED(); | 46 NOTREACHED(); |
| 45 return 0; | 47 return 0; |
| 46 } | 48 } |
| 47 return pid; | 49 return pid; |
| 48 } | 50 } |
| 49 | 51 |
| 50 bool ReadProcStats(pid_t pid, std::string* buffer) { | 52 bool ReadProcFile(const FilePath& file, std::string* buffer) { |
| 51 buffer->clear(); | 53 buffer->clear(); |
| 52 // Synchronously reading files in /proc is safe. | 54 // Synchronously reading files in /proc is safe. |
| 53 ThreadRestrictions::ScopedAllowIO allow_io; | 55 ThreadRestrictions::ScopedAllowIO allow_io; |
| 54 | 56 |
| 55 FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile); | 57 if (!file_util::ReadFileToString(file, buffer)) { |
| 56 if (!file_util::ReadFileToString(stat_file, buffer)) { | 58 DLOG(WARNING) << "Failed to read " << file.MaybeAsASCII(); |
| 57 DLOG(WARNING) << "Failed to get process stats."; | |
| 58 return false; | 59 return false; |
| 59 } | 60 } |
| 60 return !buffer->empty(); | 61 return !buffer->empty(); |
| 61 } | 62 } |
| 62 | 63 |
| 64 bool ReadProcStats(pid_t pid, std::string* buffer) { |
| 65 FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile); |
| 66 return ReadProcFile(stat_file, buffer); |
| 67 } |
| 68 |
| 63 bool ParseProcStats(const std::string& stats_data, | 69 bool ParseProcStats(const std::string& stats_data, |
| 64 std::vector<std::string>* proc_stats) { | 70 std::vector<std::string>* proc_stats) { |
| 65 // |stats_data| may be empty if the process disappeared somehow. | 71 // |stats_data| may be empty if the process disappeared somehow. |
| 66 // e.g. http://crbug.com/145811 | 72 // e.g. http://crbug.com/145811 |
| 67 if (stats_data.empty()) | 73 if (stats_data.empty()) |
| 68 return false; | 74 return false; |
| 69 | 75 |
| 70 // The stat file is formatted as: | 76 // The stat file is formatted as: |
| 71 // pid (process name) data1 data2 .... dataN | 77 // pid (process name) data1 data2 .... dataN |
| 72 // Look for the closing paren by scanning backwards, to avoid being fooled by | 78 // Look for the closing paren by scanning backwards, to avoid being fooled by |
| (...skipping 18 matching lines...) Expand all Loading... |
| 91 close_parens_idx - (open_parens_idx + 1))); | 97 close_parens_idx - (open_parens_idx + 1))); |
| 92 | 98 |
| 93 // Split the rest. | 99 // Split the rest. |
| 94 std::vector<std::string> other_stats; | 100 std::vector<std::string> other_stats; |
| 95 SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats); | 101 SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats); |
| 96 for (size_t i = 0; i < other_stats.size(); ++i) | 102 for (size_t i = 0; i < other_stats.size(); ++i) |
| 97 proc_stats->push_back(other_stats[i]); | 103 proc_stats->push_back(other_stats[i]); |
| 98 return true; | 104 return true; |
| 99 } | 105 } |
| 100 | 106 |
| 107 typedef std::map<std::string, std::string> ProcStatMap; |
| 108 void ParseProcStat(const std::string& contents, ProcStatMap* output) { |
| 109 typedef std::pair<std::string, std::string> StringPair; |
| 110 std::vector<StringPair> key_value_pairs; |
| 111 SplitStringIntoKeyValuePairs(contents, ' ', '\n', &key_value_pairs); |
| 112 for (size_t i = 0; i < key_value_pairs.size(); ++i) { |
| 113 const StringPair& key_value_pair = key_value_pairs[i]; |
| 114 output->insert(key_value_pair); |
| 115 } |
| 116 } |
| 117 |
| 101 int GetProcStatsFieldAsInt(const std::vector<std::string>& proc_stats, | 118 int GetProcStatsFieldAsInt(const std::vector<std::string>& proc_stats, |
| 102 ProcStatsFields field_num) { | 119 ProcStatsFields field_num) { |
| 103 DCHECK_GE(field_num, VM_PPID); | 120 DCHECK_GE(field_num, VM_PPID); |
| 104 CHECK_LT(static_cast<size_t>(field_num), proc_stats.size()); | 121 CHECK_LT(static_cast<size_t>(field_num), proc_stats.size()); |
| 105 | 122 |
| 106 int value; | 123 int value; |
| 107 return StringToInt(proc_stats[field_num], &value) ? value : 0; | 124 return StringToInt(proc_stats[field_num], &value) ? value : 0; |
| 108 } | 125 } |
| 109 | 126 |
| 110 size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats, | 127 size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 131 ProcStatsFields field_num) { | 148 ProcStatsFields field_num) { |
| 132 std::string stats_data; | 149 std::string stats_data; |
| 133 if (!ReadProcStats(pid, &stats_data)) | 150 if (!ReadProcStats(pid, &stats_data)) |
| 134 return 0; | 151 return 0; |
| 135 std::vector<std::string> proc_stats; | 152 std::vector<std::string> proc_stats; |
| 136 if (!ParseProcStats(stats_data, &proc_stats)) | 153 if (!ParseProcStats(stats_data, &proc_stats)) |
| 137 return 0; | 154 return 0; |
| 138 return GetProcStatsFieldAsSizeT(proc_stats, field_num); | 155 return GetProcStatsFieldAsSizeT(proc_stats, field_num); |
| 139 } | 156 } |
| 140 | 157 |
| 158 Time GetBootTime() { |
| 159 FilePath path("/proc/stat"); |
| 160 std::string contents; |
| 161 if (!ReadProcFile(path, &contents)) |
| 162 return Time(); |
| 163 ProcStatMap proc_stat; |
| 164 ParseProcStat(contents, &proc_stat); |
| 165 ProcStatMap::const_iterator btime_it = proc_stat.find("btime"); |
| 166 if (btime_it == proc_stat.end()) |
| 167 return Time(); |
| 168 int btime; |
| 169 if (!StringToInt(btime_it->second, &btime)) |
| 170 return Time(); |
| 171 return Time::FromTimeT(btime); |
| 172 } |
| 173 |
| 174 TimeDelta ClockTicksToTimeDelta(int clock_ticks) { |
| 175 // This queries the /proc-specific scaling factor which is |
| 176 // conceptually the system hertz. To dump this value on another |
| 177 // system, try |
| 178 // od -t dL /proc/self/auxv |
| 179 // and look for the number after 17 in the output; mine is |
| 180 // 0000040 17 100 3 134512692 |
| 181 // which means the answer is 100. |
| 182 // It may be the case that this value is always 100. |
| 183 static const int kHertz = sysconf(_SC_CLK_TCK); |
| 184 |
| 185 return TimeDelta::FromMicroseconds( |
| 186 Time::kMicrosecondsPerSecond * clock_ticks / kHertz); |
| 187 } |
| 188 |
| 141 } // namespace internal | 189 } // namespace internal |
| 142 } // namespace base | 190 } // namespace base |
| OLD | NEW |