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> |
11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <sys/time.h> | 12 #include <sys/time.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "base/files/dir_reader_posix.h" | 17 #include "base/files/dir_reader_posix.h" |
18 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/memory/ptr_util.h" |
20 #include "base/process/internal_linux.h" | 21 #include "base/process/internal_linux.h" |
21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
22 #include "base/strings/string_split.h" | 23 #include "base/strings/string_split.h" |
23 #include "base/strings/string_tokenizer.h" | 24 #include "base/strings/string_tokenizer.h" |
24 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
25 #include "base/sys_info.h" | 26 #include "base/sys_info.h" |
26 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
28 | 29 |
29 namespace base { | 30 namespace base { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 return 0; | 66 return 0; |
66 } | 67 } |
67 | 68 |
68 StringPairs pairs; | 69 StringPairs pairs; |
69 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); | 70 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); |
70 TrimKeyValuePairs(&pairs); | 71 TrimKeyValuePairs(&pairs); |
71 for (size_t i = 0; i < pairs.size(); ++i) { | 72 for (size_t i = 0; i < pairs.size(); ++i) { |
72 const std::string& key = pairs[i].first; | 73 const std::string& key = pairs[i].first; |
73 const std::string& value_str = pairs[i].second; | 74 const std::string& value_str = pairs[i].second; |
74 if (key == field) { | 75 if (key == field) { |
75 std::vector<StringPiece> split_value_str = SplitStringPiece( | 76 std::vector<StringPiece> split_value_str = |
76 value_str, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 77 SplitStringPiece(value_str, " ", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
77 if (split_value_str.size() != 2 || split_value_str[1] != "kB") { | 78 if (split_value_str.size() != 2 || split_value_str[1] != "kB") { |
78 NOTREACHED(); | 79 NOTREACHED(); |
79 return 0; | 80 return 0; |
80 } | 81 } |
81 size_t value; | 82 size_t value; |
82 if (!StringToSizeT(split_value_str[0], &value)) { | 83 if (!StringToSizeT(split_value_str[0], &value)) { |
83 NOTREACHED(); | 84 NOTREACHED(); |
84 return 0; | 85 return 0; |
85 } | 86 } |
86 return value; | 87 return value; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 } | 157 } |
157 } | 158 } |
158 closedir(dir); | 159 closedir(dir); |
159 | 160 |
160 return total_cpu; | 161 return total_cpu; |
161 } | 162 } |
162 | 163 |
163 } // namespace | 164 } // namespace |
164 | 165 |
165 // static | 166 // static |
166 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | 167 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
167 return new ProcessMetrics(process); | 168 ProcessHandle process) { |
| 169 return WrapUnique(new ProcessMetrics(process)); |
168 } | 170 } |
169 | 171 |
170 // On linux, we return vsize. | 172 // On linux, we return vsize. |
171 size_t ProcessMetrics::GetPagefileUsage() const { | 173 size_t ProcessMetrics::GetPagefileUsage() const { |
172 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, | 174 return internal::ReadProcStatsAndGetFieldAsSizeT(process_, |
173 internal::VM_VSIZE); | 175 internal::VM_VSIZE); |
174 } | 176 } |
175 | 177 |
176 // On linux, we return the high water mark of vsize. | 178 // On linux, we return the high water mark of vsize. |
177 size_t ProcessMetrics::GetPeakPagefileUsage() const { | 179 size_t ProcessMetrics::GetPeakPagefileUsage() const { |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 std::string totmaps_data; | 349 std::string totmaps_data; |
348 { | 350 { |
349 FilePath totmaps_file = internal::GetProcPidDir(process_).Append("totmaps"); | 351 FilePath totmaps_file = internal::GetProcPidDir(process_).Append("totmaps"); |
350 ThreadRestrictions::ScopedAllowIO allow_io; | 352 ThreadRestrictions::ScopedAllowIO allow_io; |
351 bool ret = ReadFileToString(totmaps_file, &totmaps_data); | 353 bool ret = ReadFileToString(totmaps_file, &totmaps_data); |
352 if (!ret || totmaps_data.length() == 0) | 354 if (!ret || totmaps_data.length() == 0) |
353 return false; | 355 return false; |
354 } | 356 } |
355 | 357 |
356 std::vector<std::string> totmaps_fields = SplitString( | 358 std::vector<std::string> totmaps_fields = SplitString( |
357 totmaps_data, base::kWhitespaceASCII, base::KEEP_WHITESPACE, | 359 totmaps_data, kWhitespaceASCII, KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY); |
358 base::SPLIT_WANT_NONEMPTY); | |
359 | 360 |
360 DCHECK_EQ("Pss:", totmaps_fields[kPssIndex-1]); | 361 DCHECK_EQ("Pss:", totmaps_fields[kPssIndex-1]); |
361 DCHECK_EQ("Private_Clean:", totmaps_fields[kPrivate_CleanIndex - 1]); | 362 DCHECK_EQ("Private_Clean:", totmaps_fields[kPrivate_CleanIndex - 1]); |
362 DCHECK_EQ("Private_Dirty:", totmaps_fields[kPrivate_DirtyIndex - 1]); | 363 DCHECK_EQ("Private_Dirty:", totmaps_fields[kPrivate_DirtyIndex - 1]); |
363 DCHECK_EQ("Swap:", totmaps_fields[kSwapIndex-1]); | 364 DCHECK_EQ("Swap:", totmaps_fields[kSwapIndex-1]); |
364 | 365 |
365 int pss = 0; | 366 int pss = 0; |
366 int private_clean = 0; | 367 int private_clean = 0; |
367 int private_dirty = 0; | 368 int private_dirty = 0; |
368 int swap = 0; | 369 int swap = 0; |
(...skipping 30 matching lines...) Expand all Loading... |
399 std::string statm; | 400 std::string statm; |
400 { | 401 { |
401 FilePath statm_file = internal::GetProcPidDir(process_).Append("statm"); | 402 FilePath statm_file = internal::GetProcPidDir(process_).Append("statm"); |
402 // Synchronously reading files in /proc does not hit the disk. | 403 // Synchronously reading files in /proc does not hit the disk. |
403 ThreadRestrictions::ScopedAllowIO allow_io; | 404 ThreadRestrictions::ScopedAllowIO allow_io; |
404 bool ret = ReadFileToString(statm_file, &statm); | 405 bool ret = ReadFileToString(statm_file, &statm); |
405 if (!ret || statm.length() == 0) | 406 if (!ret || statm.length() == 0) |
406 return false; | 407 return false; |
407 } | 408 } |
408 | 409 |
409 std::vector<StringPiece> statm_vec = SplitStringPiece( | 410 std::vector<StringPiece> statm_vec = |
410 statm, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 411 SplitStringPiece(statm, " ", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
411 if (statm_vec.size() != 7) | 412 if (statm_vec.size() != 7) |
412 return false; // Not the format we expect. | 413 return false; // Not the format we expect. |
413 | 414 |
414 int statm_rss, statm_shared; | 415 int statm_rss, statm_shared; |
415 bool ret = true; | 416 bool ret = true; |
416 ret &= StringToInt(statm_vec[1], &statm_rss); | 417 ret &= StringToInt(statm_vec[1], &statm_rss); |
417 ret &= StringToInt(statm_vec[2], &statm_shared); | 418 ret &= StringToInt(statm_vec[2], &statm_shared); |
418 | 419 |
419 ws_usage->priv = (statm_rss - statm_shared) * page_size_kb; | 420 ws_usage->priv = (statm_rss - statm_shared) * page_size_kb; |
420 ws_usage->shared = statm_shared * page_size_kb; | 421 ws_usage->shared = statm_shared * page_size_kb; |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 #if defined(OS_LINUX) | 958 #if defined(OS_LINUX) |
958 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 959 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
959 uint64_t wake_ups; | 960 uint64_t wake_ups; |
960 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 961 const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
961 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 962 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
962 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 963 CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
963 } | 964 } |
964 #endif // defined(OS_LINUX) | 965 #endif // defined(OS_LINUX) |
965 | 966 |
966 } // namespace base | 967 } // namespace base |
OLD | NEW |