OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 if (!ReadFileToString(file, &file_as_string)) | 46 if (!ReadFileToString(file, &file_as_string)) |
47 return 0; | 47 return 0; |
48 TrimWhitespaceASCII(file_as_string, TRIM_ALL, &file_as_string); | 48 TrimWhitespaceASCII(file_as_string, TRIM_ALL, &file_as_string); |
49 uint64_t file_as_uint64 = 0; | 49 uint64_t file_as_uint64 = 0; |
50 if (!StringToUint64(file_as_string, &file_as_uint64)) | 50 if (!StringToUint64(file_as_string, &file_as_uint64)) |
51 return 0; | 51 return 0; |
52 return file_as_uint64; | 52 return file_as_uint64; |
53 } | 53 } |
54 #endif | 54 #endif |
55 | 55 |
56 // Read /proc/<pid>/status and return the value for |field|, or 0 on failure. | 56 // Read /proc/<pid>/status and writes the value for |field| into |result|. |
57 // Only works for fields in the form of "Field: value kB". | 57 // Returns true on success. Only works for fields in the form of |
58 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) { | 58 // "Field: value kB". |
59 bool ReadProcStatusAndGetFieldAsSizeT(pid_t pid, | |
60 const std::string& field, | |
61 size_t* result) { | |
62 *result = 0; | |
59 std::string status; | 63 std::string status; |
60 { | 64 { |
61 // Synchronously reading files in /proc does not hit the disk. | 65 // Synchronously reading files in /proc does not hit the disk. |
62 ThreadRestrictions::ScopedAllowIO allow_io; | 66 ThreadRestrictions::ScopedAllowIO allow_io; |
63 FilePath stat_file = internal::GetProcPidDir(pid).Append("status"); | 67 FilePath stat_file = internal::GetProcPidDir(pid).Append("status"); |
64 if (!ReadFileToString(stat_file, &status)) | 68 if (!ReadFileToString(stat_file, &status)) |
65 return 0; | 69 return false; |
66 } | 70 } |
67 | 71 |
68 StringPairs pairs; | 72 StringPairs pairs; |
69 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); | 73 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); |
70 TrimKeyValuePairs(&pairs); | 74 TrimKeyValuePairs(&pairs); |
71 for (size_t i = 0; i < pairs.size(); ++i) { | 75 for (size_t i = 0; i < pairs.size(); ++i) { |
72 const std::string& key = pairs[i].first; | 76 const std::string& key = pairs[i].first; |
73 const std::string& value_str = pairs[i].second; | 77 const std::string& value_str = pairs[i].second; |
74 if (key == field) { | 78 if (key == field) { |
75 std::vector<StringPiece> split_value_str = SplitStringPiece( | 79 std::vector<StringPiece> split_value_str = SplitStringPiece( |
76 value_str, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 80 value_str, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
77 if (split_value_str.size() != 2 || split_value_str[1] != "kB") { | 81 if (split_value_str.size() != 2 || split_value_str[1] != "kB") { |
78 NOTREACHED(); | 82 NOTREACHED(); |
79 return 0; | 83 return false; |
80 } | 84 } |
81 size_t value; | 85 if (!StringToSizeT(split_value_str[0], result)) { |
82 if (!StringToSizeT(split_value_str[0], &value)) { | |
83 NOTREACHED(); | 86 NOTREACHED(); |
84 return 0; | 87 return false; |
85 } | 88 } |
86 return value; | 89 return true; |
87 } | 90 } |
88 } | 91 } |
89 NOTREACHED(); | 92 return false; |
90 return 0; | |
91 } | 93 } |
92 | 94 |
93 #if defined(OS_LINUX) | 95 #if defined(OS_LINUX) |
94 // Read /proc/<pid>/sched and look for |field|. On succes, return true and | 96 // Read /proc/<pid>/sched and look for |field|. On succes, return true and |
95 // write the value for |field| into |result|. | 97 // write the value for |field| into |result|. |
96 // Only works for fields in the form of "field : uint_value" | 98 // Only works for fields in the form of "field : uint_value" |
97 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid, | 99 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid, |
98 const std::string& field, | 100 const std::string& field, |
99 uint64_t* result) { | 101 uint64_t* result) { |
100 std::string sched_data; | 102 std::string sched_data; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 } | 169 } |
168 | 170 |
169 // On linux, we return vsize. | 171 // On linux, we return vsize. |
170 size_t ProcessMetrics::GetPagefileUsage() const { | 172 size_t ProcessMetrics::GetPagefileUsage() const { |
171 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, | 173 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, |
172 internal::VM_VSIZE); | 174 internal::VM_VSIZE); |
173 } | 175 } |
174 | 176 |
175 // On linux, we return the high water mark of vsize. | 177 // On linux, we return the high water mark of vsize. |
176 size_t ProcessMetrics::GetPeakPagefileUsage() const { | 178 size_t ProcessMetrics::GetPeakPagefileUsage() const { |
177 return ReadProcStatusAndGetFieldAsSizeT(process_, "VmPeak") * 1024; | 179 size_t value = 0; |
180 bool res = ReadProcStatusAndGetFieldAsSizeT(process_, "VmPeak", &value); | |
181 DCHECK(res); | |
182 return value * 1024; | |
178 } | 183 } |
179 | 184 |
180 // On linux, we return RSS. | 185 // On linux, we return RSS. |
181 size_t ProcessMetrics::GetWorkingSetSize() const { | 186 size_t ProcessMetrics::GetWorkingSetSize() const { |
182 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, internal::VM_RSS) * | 187 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, internal::VM_RSS) * |
183 getpagesize(); | 188 getpagesize(); |
184 } | 189 } |
185 | 190 |
186 // On linux, we return the high water mark of RSS. | 191 // On linux, we return the high water mark of RSS. |
187 size_t ProcessMetrics::GetPeakWorkingSetSize() const { | 192 size_t ProcessMetrics::GetPeakWorkingSetSize() const { |
188 return ReadProcStatusAndGetFieldAsSizeT(process_, "VmHWM") * 1024; | 193 size_t value = 0; |
Primiano Tucci (use gerrit)
2016/01/07 11:34:35
Oooh I see, you still cannot have the DCHECK here.
ssid
2016/01/07 13:03:09
Done.
| |
194 ReadProcStatusAndGetFieldAsSizeT(process_, "VmHWM", &value); | |
195 return value * 1024; | |
189 } | 196 } |
190 | 197 |
191 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | 198 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
192 size_t* shared_bytes) { | 199 size_t* shared_bytes) { |
193 WorkingSetKBytes ws_usage; | 200 WorkingSetKBytes ws_usage; |
194 if (!GetWorkingSetKBytes(&ws_usage)) | 201 if (!GetWorkingSetKBytes(&ws_usage)) |
195 return false; | 202 return false; |
196 | 203 |
197 if (private_bytes) | 204 if (private_bytes) |
198 *private_bytes = ws_usage.priv * 1024; | 205 *private_bytes = ws_usage.priv * 1024; |
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
941 #if defined(OS_LINUX) | 948 #if defined(OS_LINUX) |
942 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 949 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
943 uint64_t wake_ups; | 950 uint64_t wake_ups; |
944 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 951 const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
945 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 952 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
946 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 953 CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
947 } | 954 } |
948 #endif // defined(OS_LINUX) | 955 #endif // defined(OS_LINUX) |
949 | 956 |
950 } // namespace base | 957 } // namespace base |
OLD | NEW |