Update ASan/Android runtime and setup script to LLVM r200682.
[chromium-blink-merge.git] / base / process / process_metrics_freebsd.cc
blobfbb8a889457e593dc56e53005815a0da479c92d7
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 namespace base {
9 ProcessMetrics::ProcessMetrics(ProcessHandle process)
10 : process_(process),
11 last_cpu_time_(0),
12 last_system_time_(0),
13 last_cpu_(0) {
14 processor_count_ = base::SysInfo::NumberOfProcessors();
17 // static
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)
28 return 0;
30 return info.ki_size;
33 size_t ProcessMetrics::GetPeakPagefileUsage() const {
34 return 0;
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)
43 return 0;
45 return info.ki_rssize * getpagesize();
48 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
49 return 0;
52 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
53 size_t* shared_bytes) {
54 WorkingSetKBytes ws_usage;
55 if (!GetWorkingSetKBytes(&ws_usage))
56 return false;
58 if (private_bytes)
59 *private_bytes = ws_usage.priv << 10;
61 if (shared_bytes)
62 *shared_bytes = ws_usage.shared * 1024;
64 return true;
67 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
68 // TODO(bapt) be sure we can't be precise
69 size_t priv = GetWorkingSetSize();
70 if (!priv)
71 return false;
72 ws_usage->priv = priv / 1024;
73 ws_usage->shareable = 0;
74 ws_usage->shared = 0;
76 return true;
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);
84 struct timeval now;
85 int retval = gettimeofday(&now, NULL);
86 if (retval)
87 return 0;
89 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
90 return 0;
92 return (info.ki_pctcpu / FSCALE) * 100.0;
95 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
96 return false;
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)
105 return 0;
107 length = sizeof(mem_free);
108 if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0)
109 return 0;
111 length = sizeof(mem_inactive);
112 if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length,
113 NULL, 0) < 0) {
114 return 0;
117 pagesize = getpagesize();
119 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
122 } // namespace base