| 6 |
| 7 | #include <stddef.h> |
| 8 | #include <sys/sysctl.h> |
| 9 | #include <sys/user.h> |
| 10 | #include <unistd.h> |
| 11 |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ptr_util.h" |
| 14 | #include "base/process/process_metrics_iocounters.h" |
| 15 | #include "base/stl_util.h" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 20 | : process_(process), |
| 21 | last_cpu_(0) {} |
| 22 |
| 23 | // static |
| 24 | std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 25 | ProcessHandle process) { |
| 26 | return WrapUnique(new ProcessMetrics(process)); |
| 27 | } |
| 28 |
| 29 | double ProcessMetrics::GetPlatformIndependentCPUUsage() { |
| 30 | struct kinfo_proc info; |
| 31 | int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, process_}; |
| 32 | size_t length = sizeof(info); |
| 33 |
| 34 | if (sysctl(mib, base::size(mib), &info, &length, NULL, 0) < 0) |
| 35 | return 0; |
| 36 |
| 37 | return (info.ki_pctcpu / FSCALE) * 100.0; |
| 38 | } |
| 39 |
| 40 | TimeDelta ProcessMetrics::GetCumulativeCPUUsage() { |
| 41 | NOTREACHED(); |
| 42 | return TimeDelta(); |
| 43 | } |
| 44 |
| 45 | bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| 46 | return false; |
| 47 | } |
| 48 |
| 49 | size_t GetSystemCommitCharge() { |
| 50 | int mib[2], pagesize; |
| 51 | unsigned long mem_total, mem_free, mem_inactive; |
| 52 | size_t length = sizeof(mem_total); |
| 53 |
| 54 | if (sysctl(mib, base::size(mib), &mem_total, &length, NULL, 0) < 0) |
| 55 | return 0; |
| 56 |
| 57 | length = sizeof(mem_free); |
| 58 | if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0) |
| 59 | return 0; |
| 60 |
| 61 | length = sizeof(mem_inactive); |
| 62 | if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length, |
| 63 | NULL, 0) < 0) { |
| 64 | return 0; |
| 65 | } |
| 66 |
| 67 | pagesize = getpagesize(); |
| 68 |
| 69 | return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); |
| 70 | } |
| 71 |
| 72 | } // namespace base |
| 73 |