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
3 // found in the LICENSE file.
5 #include "base/process/process_metrics.h"
9 ProcessMetrics::ProcessMetrics(ProcessHandle process
)
14 processor_count_
= base::SysInfo::NumberOfProcessors();
18 ProcessMetrics
* ProcessMetrics::CreateProcessMetrics(ProcessHandle process
) {
19 return new ProcessMetrics(process
);
22 size_t ProcessMetrics::GetPagefileUsage() const {
23 struct kinfo_proc info
;
24 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, process_
};
25 size_t length
= sizeof(info
);
27 if (sysctl(mib
, arraysize(mib
), &info
, &length
, NULL
, 0) < 0)
33 size_t ProcessMetrics::GetPeakPagefileUsage() const {
37 size_t ProcessMetrics::GetWorkingSetSize() const {
38 struct kinfo_proc info
;
39 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, process_
};
40 size_t length
= sizeof(info
);
42 if (sysctl(mib
, arraysize(mib
), &info
, &length
, NULL
, 0) < 0)
45 return info
.ki_rssize
* getpagesize();
48 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
52 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes
,
53 size_t* shared_bytes
) {
54 WorkingSetKBytes ws_usage
;
55 if (!GetWorkingSetKBytes(&ws_usage
))
59 *private_bytes
= ws_usage
.priv
<< 10;
62 *shared_bytes
= ws_usage
.shared
* 1024;
67 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes
* ws_usage
) const {
68 // TODO(bapt) be sure we can't be precise
69 size_t priv
= GetWorkingSetSize();
72 ws_usage
->priv
= priv
/ 1024;
73 ws_usage
->shareable
= 0;
79 double ProcessMetrics::GetCPUUsage() {
80 struct kinfo_proc info
;
81 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, process_
};
82 size_t length
= sizeof(info
);
85 int retval
= gettimeofday(&now
, NULL
);
89 if (sysctl(mib
, arraysize(mib
), &info
, &length
, NULL
, 0) < 0)
92 return (info
.ki_pctcpu
/ FSCALE
) * 100.0;
95 bool ProcessMetrics::GetIOCounters(IoCounters
* io_counters
) const {
99 size_t GetSystemCommitCharge() {
100 int mib
[2], pagesize
;
101 unsigned long mem_total
, mem_free
, mem_inactive
;
102 size_t length
= sizeof(mem_total
);
104 if (sysctl(mib
, arraysize(mib
), &mem_total
, &length
, NULL
, 0) < 0)
107 length
= sizeof(mem_free
);
108 if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free
, &length
, NULL
, 0) < 0)
111 length
= sizeof(mem_inactive
);
112 if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive
, &length
,
117 pagesize
= getpagesize();
119 return mem_total
- (mem_free
*pagesize
) - (mem_inactive
*pagesize
);