Home | History | Annotate | Download | only in process
      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.
      4 
      5 #include "base/process/process_metrics.h"
      6 
      7 namespace base {
      8 
      9 ProcessMetrics::ProcessMetrics(ProcessHandle process)
     10     : process_(process),
     11       last_time_(0),
     12       last_system_time_(0),
     13       last_cpu_(0) {
     14   processor_count_ = base::SysInfo::NumberOfProcessors();
     15 }
     16 
     17 // static
     18 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
     19   return new ProcessMetrics(process);
     20 }
     21 
     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);
     26 
     27   if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
     28     return 0;
     29 
     30   return info.ki_size;
     31 }
     32 
     33 size_t ProcessMetrics::GetPeakPagefileUsage() const {
     34   return 0;
     35 }
     36 
     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);
     41 
     42   if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
     43     return 0;
     44 
     45   return info.ki_rssize * getpagesize();
     46 }
     47 
     48 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
     49   return 0;
     50 }
     51 
     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;
     57 
     58   if (private_bytes)
     59     *private_bytes = ws_usage.priv << 10;
     60 
     61   if (shared_bytes)
     62     *shared_bytes = ws_usage.shared * 1024;
     63 
     64   return true;
     65 }
     66 
     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;
     75 
     76   return true;
     77 }
     78 
     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);
     83 
     84   struct timeval now;
     85   int retval = gettimeofday(&now, NULL);
     86   if (retval)
     87     return 0;
     88 
     89   if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
     90     return 0;
     91 
     92   return (info.ki_pctcpu / FSCALE) * 100.0;
     93 }
     94 
     95 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
     96   return false;
     97 }
     98 
     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);
    103 
    104   if (sysctl(mib, arraysize(mib), &mem_total, &length, NULL, 0) < 0)
    105     return 0;
    106 
    107   length = sizeof(mem_free);
    108   if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0)
    109     return 0;
    110 
    111   length = sizeof(mem_inactive);
    112   if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length,
    113       NULL, 0) < 0) {
    114     return 0;
    115   }
    116 
    117   pagesize = getpagesize();
    118 
    119   return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
    120 }
    121 
    122 }  // namespace base
    123