190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/process/process_metrics.h" 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace base { 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ProcessMetrics::ProcessMetrics(ProcessHandle process) 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) : process_(process), 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) last_time_(0), 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) last_system_time_(0), 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) last_cpu_(0) { 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) processor_count_ = base::SysInfo::NumberOfProcessors(); 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// static 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return new ProcessMetrics(process); 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t ProcessMetrics::GetPagefileUsage() const { 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) struct kinfo_proc info; 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t length = sizeof(info); 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return info.ki_size; 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t ProcessMetrics::GetPeakPagefileUsage() const { 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t ProcessMetrics::GetWorkingSetSize() const { 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) struct kinfo_proc info; 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t length = sizeof(info); 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return info.ki_rssize * getpagesize(); 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t ProcessMetrics::GetPeakWorkingSetSize() const { 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t* shared_bytes) { 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) WorkingSetKBytes ws_usage; 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!GetWorkingSetKBytes(&ws_usage)) 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (private_bytes) 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *private_bytes = ws_usage.priv << 10; 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (shared_bytes) 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *shared_bytes = ws_usage.shared * 1024; 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return true; 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// TODO(bapt) be sure we can't be precise 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t priv = GetWorkingSetSize(); 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!priv) 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ws_usage->priv = priv / 1024; 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ws_usage->shareable = 0; 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ws_usage->shared = 0; 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return true; 7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)double ProcessMetrics::GetCPUUsage() { 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) struct kinfo_proc info; 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t length = sizeof(info); 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) struct timeval now; 8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int retval = gettimeofday(&now, NULL); 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (retval) 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return (info.ki_pctcpu / FSCALE) * 100.0; 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t GetSystemCommitCharge() { 10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int mib[2], pagesize; 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) unsigned long mem_total, mem_free, mem_inactive; 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t length = sizeof(mem_total); 10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (sysctl(mib, arraysize(mib), &mem_total, &length, NULL, 0) < 0) 10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) length = sizeof(mem_free); 10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0) 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) length = sizeof(mem_inactive); 11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length, 11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NULL, 0) < 0) { 11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return 0; 11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) pagesize = getpagesize(); 11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); 12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace base 123