| 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/macros.h" | |
| 6 #include "base/process/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
| 7 | 6 |
| 8 #include <stddef.h> | 7 #include <stddef.h> |
| 9 #include <stdint.h> | 8 #include <stdint.h> |
| 10 #include <sys/param.h> | 9 #include <sys/param.h> |
| 11 #include <sys/sysctl.h> | 10 #include <sys/sysctl.h> |
| 12 | 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/sys_info.h" |
| 15 |
| 13 namespace base { | 16 namespace base { |
| 14 | 17 |
| 15 // static | 18 // static |
| 16 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | 19 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 17 return new ProcessMetrics(process); | 20 ProcessHandle process) { |
| 21 return WrapUnique(new ProcessMetrics(process)); |
| 18 } | 22 } |
| 19 | 23 |
| 20 size_t ProcessMetrics::GetPagefileUsage() const { | 24 size_t ProcessMetrics::GetPagefileUsage() const { |
| 21 struct kinfo_proc info; | 25 struct kinfo_proc info; |
| 22 size_t length; | 26 size_t length; |
| 23 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_, | 27 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_, |
| 24 sizeof(struct kinfo_proc), 0 }; | 28 sizeof(struct kinfo_proc), 0 }; |
| 25 | 29 |
| 26 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) | 30 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) |
| 27 return -1; | 31 return -1; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 last_cpu_time_ = time; | 133 last_cpu_time_ = time; |
| 130 last_cpu_ = cpu; | 134 last_cpu_ = cpu; |
| 131 | 135 |
| 132 double percentage = static_cast<double>((cpu * 100.0) / FSCALE); | 136 double percentage = static_cast<double>((cpu * 100.0) / FSCALE); |
| 133 | 137 |
| 134 return percentage; | 138 return percentage; |
| 135 } | 139 } |
| 136 | 140 |
| 137 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 141 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 138 : process_(process), | 142 : process_(process), |
| 143 processor_count_(SysInfo::NumberOfProcessors()), |
| 139 last_system_time_(0), | 144 last_system_time_(0), |
| 140 last_cpu_(0) { | 145 last_cpu_(0) {} |
| 141 | |
| 142 processor_count_ = base::SysInfo::NumberOfProcessors(); | |
| 143 } | |
| 144 | 146 |
| 145 size_t GetSystemCommitCharge() { | 147 size_t GetSystemCommitCharge() { |
| 146 int mib[] = { CTL_VM, VM_METER }; | 148 int mib[] = { CTL_VM, VM_METER }; |
| 147 int pagesize; | 149 int pagesize; |
| 148 struct vmtotal vmtotal; | 150 struct vmtotal vmtotal; |
| 149 unsigned long mem_total, mem_free, mem_inactive; | 151 unsigned long mem_total, mem_free, mem_inactive; |
| 150 size_t len = sizeof(vmtotal); | 152 size_t len = sizeof(vmtotal); |
| 151 | 153 |
| 152 if (sysctl(mib, arraysize(mib), &vmtotal, &len, NULL, 0) < 0) | 154 if (sysctl(mib, arraysize(mib), &vmtotal, &len, NULL, 0) < 0) |
| 153 return 0; | 155 return 0; |
| 154 | 156 |
| 155 mem_total = vmtotal.t_vm; | 157 mem_total = vmtotal.t_vm; |
| 156 mem_free = vmtotal.t_free; | 158 mem_free = vmtotal.t_free; |
| 157 mem_inactive = vmtotal.t_vm - vmtotal.t_avm; | 159 mem_inactive = vmtotal.t_vm - vmtotal.t_avm; |
| 158 | 160 |
| 159 pagesize = getpagesize(); | 161 pagesize = getpagesize(); |
| 160 | 162 |
| 161 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); | 163 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); |
| 162 } | 164 } |
| 163 | 165 |
| 164 } // namespace base | 166 } // namespace base |
| OLD | NEW |