| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 uint64_t file_as_uint64 = 0; | 50 uint64_t file_as_uint64 = 0; |
| 51 if (!StringToUint64(file_as_string, &file_as_uint64)) | 51 if (!StringToUint64(file_as_string, &file_as_uint64)) |
| 52 return 0; | 52 return 0; |
| 53 return file_as_uint64; | 53 return file_as_uint64; |
| 54 } | 54 } |
| 55 #endif | 55 #endif |
| 56 | 56 |
| 57 // Read /proc/<pid>/status and return the value for |field|, or 0 on failure. | 57 // Read /proc/<pid>/status and return the value for |field|, or 0 on failure. |
| 58 // Only works for fields in the form of "Field: value kB". | 58 // Only works for fields in the form of "Field: value kB". |
| 59 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) { | 59 size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) { |
| 60 std::string status; | 60 std::string status_data; |
| 61 { | 61 { |
| 62 // Synchronously reading files in /proc does not hit the disk. | 62 // Synchronously reading files in /proc does not hit the disk. |
| 63 ThreadRestrictions::ScopedAllowIO allow_io; | 63 ThreadRestrictions::ScopedAllowIO allow_io; |
| 64 FilePath stat_file = internal::GetProcPidDir(pid).Append("status"); | 64 FilePath status_file = internal::GetProcPidDir(pid).Append("status"); |
| 65 if (!ReadFileToString(stat_file, &status)) | 65 if (!ReadFileToString(status_file, &status_data)) |
| 66 return 0; | 66 return 0; |
| 67 } | 67 } |
| 68 | 68 |
| 69 StringPairs pairs; | 69 StringPairs pairs; |
| 70 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); | 70 SplitStringIntoKeyValuePairs(status_data, ':', '\n', &pairs); |
| 71 TrimKeyValuePairs(&pairs); | 71 TrimKeyValuePairs(&pairs); |
| 72 for (size_t i = 0; i < pairs.size(); ++i) { | 72 for (size_t i = 0; i < pairs.size(); ++i) { |
| 73 const std::string& key = pairs[i].first; | 73 const std::string& key = pairs[i].first; |
| 74 const std::string& value_str = pairs[i].second; | 74 const std::string& value_str = pairs[i].second; |
| 75 if (key == field) { | 75 if (key == field) { |
| 76 std::vector<StringPiece> split_value_str = | 76 std::vector<StringPiece> split_value_str = |
| 77 SplitStringPiece(value_str, " ", TRIM_WHITESPACE, SPLIT_WANT_ALL); | 77 SplitStringPiece(value_str, " ", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
| 78 if (split_value_str.size() != 2 || split_value_str[1] != "kB") { | 78 if (split_value_str.size() != 2 || split_value_str[1] != "kB") { |
| 79 NOTREACHED(); | 79 NOTREACHED(); |
| 80 return 0; | 80 return 0; |
| 81 } | 81 } |
| 82 size_t value; | 82 size_t value; |
| 83 if (!StringToSizeT(split_value_str[0], &value)) { | 83 if (!StringToSizeT(split_value_str[0], &value)) { |
| 84 NOTREACHED(); | 84 NOTREACHED(); |
| 85 return 0; | 85 return 0; |
| 86 } | 86 } |
| 87 return value; | 87 return value; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 // This can be reached if the process dies when proc is read -- in that case, | 90 // This can be reached if the process dies when proc is read -- in that case, |
| 91 // the kernel can return missing fields. | 91 // the kernel can return missing fields. |
| 92 return 0; | 92 return 0; |
| 93 } | 93 } |
| 94 | 94 |
| 95 #if defined(OS_LINUX) || defined(OS_AIX) | 95 #if defined(OS_LINUX) || defined(OS_AIX) |
| 96 // Read /proc/<pid>/sched and look for |field|. On succes, return true and | 96 // Read /proc/<pid>/status and look for |field|. On success, return true and |
| 97 // write the value for |field| into |result|. | 97 // write the value for |field| into |result|. |
| 98 // Only works for fields in the form of "field : uint_value" | 98 // Only works for fields in the form of "field : uint_value" |
| 99 bool ReadProcSchedAndGetFieldAsUint64(pid_t pid, | 99 bool ReadProcStatusAndGetFieldAsUint64(pid_t pid, |
| 100 const std::string& field, | 100 const std::string& field, |
| 101 uint64_t* result) { | 101 uint64_t* result) { |
| 102 std::string sched_data; | 102 std::string status_data; |
| 103 { | 103 { |
| 104 // Synchronously reading files in /proc does not hit the disk. | 104 // Synchronously reading files in /proc does not hit the disk. |
| 105 ThreadRestrictions::ScopedAllowIO allow_io; | 105 ThreadRestrictions::ScopedAllowIO allow_io; |
| 106 FilePath sched_file = internal::GetProcPidDir(pid).Append("sched"); | 106 FilePath status_file = internal::GetProcPidDir(pid).Append("status"); |
| 107 if (!ReadFileToString(sched_file, &sched_data)) | 107 if (!ReadFileToString(status_file, &status_data)) |
| 108 return false; | 108 return false; |
| 109 } | 109 } |
| 110 | 110 |
| 111 StringPairs pairs; | 111 StringPairs pairs; |
| 112 SplitStringIntoKeyValuePairs(sched_data, ':', '\n', &pairs); | 112 SplitStringIntoKeyValuePairs(status_data, ':', '\n', &pairs); |
| 113 TrimKeyValuePairs(&pairs); | 113 TrimKeyValuePairs(&pairs); |
| 114 for (size_t i = 0; i < pairs.size(); ++i) { | 114 for (size_t i = 0; i < pairs.size(); ++i) { |
| 115 const std::string& key = pairs[i].first; | 115 const std::string& key = pairs[i].first; |
| 116 const std::string& value_str = pairs[i].second; | 116 const std::string& value_str = pairs[i].second; |
| 117 if (key == field) { | 117 if (key == field) { |
| 118 uint64_t value; | 118 uint64_t value; |
| 119 if (!StringToUint64(value_str, &value)) | 119 if (!StringToUint64(value_str, &value)) |
| 120 return false; | 120 return false; |
| 121 *result = value; | 121 *result = value; |
| 122 return true; | 122 return true; |
| (...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); | 952 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); |
| 953 swap_info->compr_data_size = | 953 swap_info->compr_data_size = |
| 954 ReadFileToUint64(zram_path.Append("compr_data_size")); | 954 ReadFileToUint64(zram_path.Append("compr_data_size")); |
| 955 swap_info->mem_used_total = | 955 swap_info->mem_used_total = |
| 956 ReadFileToUint64(zram_path.Append("mem_used_total")); | 956 ReadFileToUint64(zram_path.Append("mem_used_total")); |
| 957 } | 957 } |
| 958 #endif // defined(OS_CHROMEOS) | 958 #endif // defined(OS_CHROMEOS) |
| 959 | 959 |
| 960 #if defined(OS_LINUX) || defined(OS_AIX) | 960 #if defined(OS_LINUX) || defined(OS_AIX) |
| 961 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 961 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
| 962 uint64_t wake_ups; | 962 uint64_t num_switches; |
| 963 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 963 static const char kSwitchStat[] = "voluntary_ctxt_switches"; |
| 964 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 964 return ReadProcStatusAndGetFieldAsUint64(process_, kSwitchStat, &num_switches) |
| 965 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 965 ? CalculateIdleWakeupsPerSecond(num_switches) |
| 966 : 0; |
| 966 } | 967 } |
| 967 #endif // defined(OS_LINUX) || defined(OS_AIX) | 968 #endif // defined(OS_LINUX) || defined(OS_AIX) |
| 968 | 969 |
| 969 } // namespace base | 970 } // namespace base |
| OLD | NEW |