| 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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) | 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>/sched and look for |field|. On succes, 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 ReadProcSchedAndGetFieldAsUint64(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 sched_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; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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; |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 return false; | 125 return false; |
| 126 } | 126 } |
| 127 #endif // defined(OS_LINUX) | 127 #endif // defined(OS_LINUX) || defined(OS_AIX) |
| 128 | 128 |
| 129 // Get the total CPU of a single process. Return value is number of jiffies | 129 // Get the total CPU of a single process. Return value is number of jiffies |
| 130 // on success or -1 on error. | 130 // on success or -1 on error. |
| 131 int GetProcessCPU(pid_t pid) { | 131 int GetProcessCPU(pid_t pid) { |
| 132 // Use /proc/<pid>/task to find all threads and parse their /stat file. | 132 // Use /proc/<pid>/task to find all threads and parse their /stat file. |
| 133 FilePath task_path = internal::GetProcPidDir(pid).Append("task"); | 133 FilePath task_path = internal::GetProcPidDir(pid).Append("task"); |
| 134 | 134 |
| 135 DIR* dir = opendir(task_path.value().c_str()); | 135 DIR* dir = opendir(task_path.value().c_str()); |
| 136 if (!dir) { | 136 if (!dir) { |
| 137 DPLOG(ERROR) << "opendir(" << task_path.value() << ")"; | 137 DPLOG(ERROR) << "opendir(" << task_path.value() << ")"; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 else if (key == "wchar") | 286 else if (key == "wchar") |
| 287 target_counter = &io_counters->WriteTransferCount; | 287 target_counter = &io_counters->WriteTransferCount; |
| 288 if (!target_counter) | 288 if (!target_counter) |
| 289 continue; | 289 continue; |
| 290 bool converted = StringToUint64(value_str, target_counter); | 290 bool converted = StringToUint64(value_str, target_counter); |
| 291 DCHECK(converted); | 291 DCHECK(converted); |
| 292 } | 292 } |
| 293 return true; | 293 return true; |
| 294 } | 294 } |
| 295 | 295 |
| 296 #if defined(OS_LINUX) | 296 #if defined(OS_LINUX) || defined(OS_AIX) |
| 297 int ProcessMetrics::GetOpenFdCount() const { | 297 int ProcessMetrics::GetOpenFdCount() const { |
| 298 // Use /proc/<pid>/fd to count the number of entries there. | 298 // Use /proc/<pid>/fd to count the number of entries there. |
| 299 FilePath fd_path = internal::GetProcPidDir(process_).Append("fd"); | 299 FilePath fd_path = internal::GetProcPidDir(process_).Append("fd"); |
| 300 | 300 |
| 301 DirReaderPosix dir_reader(fd_path.value().c_str()); | 301 DirReaderPosix dir_reader(fd_path.value().c_str()); |
| 302 if (!dir_reader.IsValid()) | 302 if (!dir_reader.IsValid()) |
| 303 return -1; | 303 return -1; |
| 304 | 304 |
| 305 int total_count = 0; | 305 int total_count = 0; |
| 306 for (; dir_reader.Next(); ) { | 306 for (; dir_reader.Next(); ) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 330 int limit = -1; | 330 int limit = -1; |
| 331 if (StringToInt(tokens[3], &limit)) | 331 if (StringToInt(tokens[3], &limit)) |
| 332 return limit; | 332 return limit; |
| 333 return -1; | 333 return -1; |
| 334 } | 334 } |
| 335 } | 335 } |
| 336 } | 336 } |
| 337 return -1; | 337 return -1; |
| 338 } | 338 } |
| 339 | 339 |
| 340 #endif // defined(OS_LINUX) | 340 #endif // defined(OS_LINUX) || defined(OS_AIX) |
| 341 | 341 |
| 342 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 342 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 343 : process_(process), | 343 : process_(process), |
| 344 last_system_time_(0), | 344 last_system_time_(0), |
| 345 #if defined(OS_LINUX) | 345 #if defined(OS_LINUX) || defined(OS_AIX) |
| 346 last_absolute_idle_wakeups_(0), | 346 last_absolute_idle_wakeups_(0), |
| 347 #endif | 347 #endif |
| 348 last_cpu_(0) { | 348 last_cpu_(0) { |
| 349 processor_count_ = SysInfo::NumberOfProcessors(); | 349 processor_count_ = SysInfo::NumberOfProcessors(); |
| 350 } | 350 } |
| 351 | 351 |
| 352 #if defined(OS_CHROMEOS) | 352 #if defined(OS_CHROMEOS) |
| 353 // Private, Shared and Proportional working set sizes are obtained from | 353 // Private, Shared and Proportional working set sizes are obtained from |
| 354 // /proc/<pid>/totmaps | 354 // /proc/<pid>/totmaps |
| 355 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) | 355 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 swap_info->orig_data_size = orig_data_size; | 950 swap_info->orig_data_size = orig_data_size; |
| 951 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); | 951 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); |
| 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) | 960 #if defined(OS_LINUX) || defined(OS_AIX) |
| 961 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 961 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
| 962 uint64_t wake_ups; | 962 uint64_t wake_ups; |
| 963 const char kWakeupStat[] = "se.statistics.nr_wakeups"; | 963 const char kWakeupStat[] = "se.statistics.nr_wakeups"; |
| 964 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? | 964 return ReadProcSchedAndGetFieldAsUint64(process_, kWakeupStat, &wake_ups) ? |
| 965 CalculateIdleWakeupsPerSecond(wake_ups) : 0; | 965 CalculateIdleWakeupsPerSecond(wake_ups) : 0; |
| 966 } | 966 } |
| 967 #endif // defined(OS_LINUX) | 967 #endif // defined(OS_LINUX) || defined(OS_AIX) |
| 968 | 968 |
| 969 } // namespace base | 969 } // namespace base |
| OLD | NEW |