• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/process/process_metrics.h"
6 
7 #include <stddef.h>
8 #include <sys/sysctl.h>
9 #include <sys/user.h>
10 #include <unistd.h>
11 
12 #include "base/memory/ptr_util.h"
13 #include "base/process/process_metrics_iocounters.h"
14 
15 namespace base {
16 
ProcessMetrics(ProcessHandle process)17 ProcessMetrics::ProcessMetrics(ProcessHandle process)
18     : process_(process),
19       last_cpu_(0) {}
20 
21 // static
CreateProcessMetrics(ProcessHandle process)22 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
23     ProcessHandle process) {
24   return WrapUnique(new ProcessMetrics(process));
25 }
26 
GetPlatformIndependentCPUUsage()27 double ProcessMetrics::GetPlatformIndependentCPUUsage() {
28   struct kinfo_proc info;
29   int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, process_};
30   size_t length = sizeof(info);
31 
32   if (sysctl(mib, std::size(mib), &info, &length, NULL, 0) < 0)
33     return 0;
34 
35   return (info.ki_pctcpu / FSCALE) * 100.0;
36 }
37 
GetCumulativeCPUUsage()38 TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
39   NOTREACHED();
40   return TimeDelta();
41 }
42 
GetIOCounters(IoCounters * io_counters) const43 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
44   return false;
45 }
46 
GetSystemCommitCharge()47 size_t GetSystemCommitCharge() {
48   int mib[2], pagesize;
49   unsigned long mem_total, mem_free, mem_inactive;
50   size_t length = sizeof(mem_total);
51 
52   if (sysctl(mib, std::size(mib), &mem_total, &length, NULL, 0) < 0)
53     return 0;
54 
55   length = sizeof(mem_free);
56   if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0)
57     return 0;
58 
59   length = sizeof(mem_inactive);
60   if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length,
61       NULL, 0) < 0) {
62     return 0;
63   }
64 
65   pagesize = getpagesize();
66 
67   return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
68 }
69 
70 }  // namespace base
71