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 <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 namespace internal { | 22 namespace internal { |
23 | 23 |
24 const char kProcDir[] = "/proc"; | 24 const char kProcDir[] = "/proc"; |
25 | 25 |
26 const char kStatFile[] = "stat"; | 26 const char kStatFile[] = "stat"; |
27 | 27 |
28 base::FilePath GetProcPidDir(pid_t pid) { | 28 FilePath GetProcPidDir(pid_t pid) { |
29 return base::FilePath(kProcDir).Append(IntToString(pid)); | 29 return FilePath(kProcDir).Append(IntToString(pid)); |
30 } | 30 } |
31 | 31 |
32 pid_t ProcDirSlotToPid(const char* d_name) { | 32 pid_t ProcDirSlotToPid(const char* d_name) { |
33 int i; | 33 int i; |
34 for (i = 0; i < NAME_MAX && d_name[i]; ++i) { | 34 for (i = 0; i < NAME_MAX && d_name[i]; ++i) { |
35 if (!IsAsciiDigit(d_name[i])) { | 35 if (!IsAsciiDigit(d_name[i])) { |
36 return 0; | 36 return 0; |
37 } | 37 } |
38 } | 38 } |
39 if (i == NAME_MAX) | 39 if (i == NAME_MAX) |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // Split the rest. | 99 // Split the rest. |
100 std::vector<std::string> other_stats; | 100 std::vector<std::string> other_stats; |
101 SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats); | 101 SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats); |
102 for (size_t i = 0; i < other_stats.size(); ++i) | 102 for (size_t i = 0; i < other_stats.size(); ++i) |
103 proc_stats->push_back(other_stats[i]); | 103 proc_stats->push_back(other_stats[i]); |
104 return true; | 104 return true; |
105 } | 105 } |
106 | 106 |
107 typedef std::map<std::string, std::string> ProcStatMap; | 107 typedef std::map<std::string, std::string> ProcStatMap; |
108 void ParseProcStat(const std::string& contents, ProcStatMap* output) { | 108 void ParseProcStat(const std::string& contents, ProcStatMap* output) { |
109 base::StringPairs key_value_pairs; | 109 StringPairs key_value_pairs; |
110 SplitStringIntoKeyValuePairs(contents, ' ', '\n', &key_value_pairs); | 110 SplitStringIntoKeyValuePairs(contents, ' ', '\n', &key_value_pairs); |
111 for (size_t i = 0; i < key_value_pairs.size(); ++i) { | 111 for (size_t i = 0; i < key_value_pairs.size(); ++i) { |
112 output->insert(key_value_pairs[i]); | 112 output->insert(key_value_pairs[i]); |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
116 int64 GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats, | 116 int64 GetProcStatsFieldAsInt64(const std::vector<std::string>& proc_stats, |
117 ProcStatsFields field_num) { | 117 ProcStatsFields field_num) { |
118 DCHECK_GE(field_num, VM_PPID); | 118 DCHECK_GE(field_num, VM_PPID); |
119 CHECK_LT(static_cast<size_t>(field_num), proc_stats.size()); | 119 CHECK_LT(static_cast<size_t>(field_num), proc_stats.size()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 // which means the answer is 100. | 178 // which means the answer is 100. |
179 // It may be the case that this value is always 100. | 179 // It may be the case that this value is always 100. |
180 static const int kHertz = sysconf(_SC_CLK_TCK); | 180 static const int kHertz = sysconf(_SC_CLK_TCK); |
181 | 181 |
182 return TimeDelta::FromMicroseconds( | 182 return TimeDelta::FromMicroseconds( |
183 Time::kMicrosecondsPerSecond * clock_ticks / kHertz); | 183 Time::kMicrosecondsPerSecond * clock_ticks / kHertz); |
184 } | 184 } |
185 | 185 |
186 } // namespace internal | 186 } // namespace internal |
187 } // namespace base | 187 } // namespace base |
OLD | NEW |