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"
7 #include <sys/sysctl.h>
11 #include "base/sys_info.h"
15 ProcessMetrics::ProcessMetrics(ProcessHandle process
)
19 processor_count_
= base::SysInfo::NumberOfProcessors();
23 ProcessMetrics
* ProcessMetrics::CreateProcessMetrics(ProcessHandle process
) {
24 return new ProcessMetrics(process
);
27 size_t ProcessMetrics::GetPagefileUsage() const {
28 struct kinfo_proc info
;
29 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, process_
};
30 size_t length
= sizeof(info
);
32 if (sysctl(mib
, arraysize(mib
), &info
, &length
, NULL
, 0) < 0)
38 size_t ProcessMetrics::GetPeakPagefileUsage() const {
42 size_t ProcessMetrics::GetWorkingSetSize() const {
43 struct kinfo_proc info
;
44 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, process_
};
45 size_t length
= sizeof(info
);
47 if (sysctl(mib
, arraysize(mib
), &info
, &length
, NULL
, 0) < 0)
50 return info
.ki_rssize
* getpagesize();
53 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
57 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes
,
58 size_t* shared_bytes
) {
59 WorkingSetKBytes ws_usage
;
60 if (!GetWorkingSetKBytes(&ws_usage
))
64 *private_bytes
= ws_usage
.priv
<< 10;
67 *shared_bytes
= ws_usage
.shared
* 1024;
72 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes
* ws_usage
) const {
73 // TODO(bapt) be sure we can't be precise
74 size_t priv
= GetWorkingSetSize();
77 ws_usage
->priv
= priv
/ 1024;
78 ws_usage
->shareable
= 0;
84 double ProcessMetrics::GetCPUUsage() {
85 struct kinfo_proc info
;
86 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, process_
};
87 size_t length
= sizeof(info
);
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
);