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)// This file contains routines for gathering resource statistics for processes 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// running on the system. 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef BASE_PROCESS_PROCESS_METRICS_H_ 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define BASE_PROCESS_PROCESS_METRICS_H_ 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <string> 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/base_export.h" 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/basictypes.h" 1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/gtest_prod_util.h" 1658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "base/process/process_handle.h" 17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/values.h" 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_MACOSX) 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <mach/mach.h> 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace base { 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_WIN) 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct IoCounters : public IO_COUNTERS { 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#elif defined(OS_POSIX) 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct IoCounters { 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t ReadOperationCount; 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t WriteOperationCount; 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t OtherOperationCount; 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t ReadTransferCount; 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t WriteTransferCount; 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t OtherTransferCount; 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Working Set (resident) memory usage broken down by 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// On Windows: 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// priv (private): These pages (kbytes) cannot be shared with any other process. 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shareable: These pages (kbytes) can be shared with other processes under 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// the right circumstances. 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shared : These pages (kbytes) are currently shared with at least one 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// other process. 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// On Linux: 507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// priv: Pages mapped only by this process. 517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// shared: PSS or 0 if the kernel doesn't support this. 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shareable: 0 537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// On ChromeOS: 557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// priv: Pages mapped only by this process. 567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// shared: PSS or 0 if the kernel doesn't support this. 577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// shareable: 0 587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// swapped Pages swapped out to zram. 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// On OS X: TODO(thakis): Revise. 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// priv: Memory. 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shared: 0 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shareable: 0 647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct WorkingSetKBytes { 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t priv; 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t shareable; 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t shared; 707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_CHROMEOS) 717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch size_t swapped; 727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Committed (resident + paged) memory usage broken down by 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// private: These pages cannot be shared with any other process. 7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// mapped: These pages are mapped into the view of a section (backed by 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// pagefile.sys) 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// image: These pages are mapped into the view of an image section (backed by 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// file system) 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct CommittedKBytes { 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) CommittedKBytes() : priv(0), mapped(0), image(0) {} 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t priv; 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t mapped; 8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t image; 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Free memory (Megabytes marked as free) in the 2G process address space. 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// total : total amount in megabytes marked as free. Maximum value is 2048. 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// largest : size of the largest contiguous amount of memory found. It is 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// always smaller or equal to FreeMBytes::total. 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// largest_ptr: starting address of the largest memory block. 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct FreeMBytes { 9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t total; 9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t largest; 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void* largest_ptr; 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Convert a POSIX timeval to microseconds. 10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT int64 TimeValToMicroseconds(const struct timeval& tv); 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Provides performance metrics for a specified process (CPU usage, memory and 10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// IO counters). To use it, invoke CreateProcessMetrics() to get an instance 10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// for a specific process, then access the information with the different get 10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// methods. 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class BASE_EXPORT ProcessMetrics { 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ~ProcessMetrics(); 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Creates a ProcessMetrics for the specified process. 11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The caller owns the returned object. 11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_MACOSX) || defined(OS_IOS) 11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); 11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else 11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) class PortProvider { 11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual ~PortProvider() {} 11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Should return the mach task for |process| if possible, or else 12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // |MACH_PORT_NULL|. Only processes that this returns tasks for will have 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // metrics on OS X (except for the current process, which always gets 12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // metrics). 12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; 12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }; 12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The port provider needs to outlive the ProcessMetrics object returned by 12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // this function. If NULL is passed as provider, the returned object 12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // only returns valid metrics if |process| is the current process. 12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, 13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) PortProvider* port_provider); 13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // !defined(OS_MACOSX) || defined(OS_IOS) 13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the current space allocated for the pagefile, in bytes (these pages 13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // may or may not be in memory). On Linux, this returns the total virtual 13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // memory size. 13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetPagefileUsage() const; 13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the peak space allocated for the pagefile, in bytes. 13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetPeakPagefileUsage() const; 13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the current working set size, in bytes. On Linux, this returns 14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // the resident set size. 14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetWorkingSetSize() const; 14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the peak working set size, in bytes. 14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetPeakWorkingSetSize() const; 14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns private and sharedusage, in bytes. Private bytes is the amount of 14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // memory currently allocated to a process that cannot be shared. Returns 14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // false on platform specific error conditions. Note: |private_bytes| 14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // returns 0 on unsupported OSes: prior to XP SP2. 14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetMemoryBytes(size_t* private_bytes, 14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t* shared_bytes); 15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Fills a CommittedKBytes with both resident and paged 15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // memory usage as per definition of CommittedBytes. 15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void GetCommittedKBytes(CommittedKBytes* usage) const; 15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Fills a WorkingSetKBytes containing resident private and shared memory 15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // usage in bytes, as per definition of WorkingSetBytes. 15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; 15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Returns the CPU usage in percent since the last time this method or 1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // GetPlatformIndependentCPUUsage() was called. The first time this method 1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // is called it returns 0 and will return the actual CPU info on subsequent 1601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // calls. On Windows, the CPU usage value is for all CPUs. So if you have 1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // 2 CPUs and your process is using all the cycles of 1 CPU and not the other 1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // CPU, this method returns 50. 16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) double GetCPUUsage(); 16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Returns the number of average idle cpu wakeups per second since the last 1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // call. 1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int GetIdleWakeupsPerSecond(); 1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Same as GetCPUUsage(), but will return consistent values on all platforms 1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // (cancelling the Windows exception mentioned above) by returning a value in 1711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // the range of 0 to (100 * numCPUCores) everywhere. 1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) double GetPlatformIndependentCPUUsage(); 1731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Retrieves accounting information for all I/O operations performed by the 17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // process. 17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // If IO information is retrieved successfully, the function returns true 17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // and fills in the IO_COUNTERS passed in. The function returns false 17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // otherwise. 17990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetIOCounters(IoCounters* io_counters) const; 18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 18190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private: 18290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_MACOSX) || defined(OS_IOS) 18390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) explicit ProcessMetrics(ProcessHandle process); 18490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else 18590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ProcessMetrics(ProcessHandle process, PortProvider* port_provider); 18690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // !defined(OS_MACOSX) || defined(OS_IOS) 18790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_LINUX) || defined(OS_ANDROID) 189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage) const; 190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif 191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_CHROMEOS) 193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) const; 194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif 195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_MACOSX) || defined(OS_LINUX) 1971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci int CalculateIdleWakeupsPerSecond(uint64 absolute_idle_wakeups); 1981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 1991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 20090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ProcessHandle process_; 20190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 20290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int processor_count_; 20390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 20490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Used to store the previous times and CPU usage counts so we can 20590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // compute the CPU usage between calls. 206c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch TimeTicks last_cpu_time_; 20790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int64 last_system_time_; 20890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_MACOSX) || defined(OS_LINUX) 2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Same thing for idle wakeups. 211c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch TimeTicks last_idle_wakeups_time_; 2121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci uint64 last_absolute_idle_wakeups_; 2131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif 2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_IOS) 21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_MACOSX) 21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Queries the port provider if it's set. 21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) mach_port_t TaskForPid(ProcessHandle process) const; 21990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 22090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) PortProvider* port_provider_; 22190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#elif defined(OS_POSIX) 2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Jiffie count at the last_cpu_time_ we updated. 22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int last_cpu_; 22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // defined(OS_POSIX) 22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // !defined(OS_IOS) 22690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 22790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); 22890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns the memory committed by the system in KBytes. 23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns 0 if it can't compute the commit charge. 23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT size_t GetSystemCommitCharge(); 23390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#if defined(OS_POSIX) 2353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Returns the maximum number of file descriptors that can be open by a process 2363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// at once. If the number is unavailable, a conservative best guess is returned. 2373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)size_t GetMaxFds(); 238cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 239cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Sets the file descriptor soft limit to |max_descriptors| or the OS hard 240cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// limit, whichever is lower. 241cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)BASE_EXPORT void SetFdLimit(unsigned int max_descriptors); 2423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif // defined(OS_POSIX) 2433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 24490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_LINUX) || defined(OS_ANDROID) 24590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Parse the data found in /proc/<pid>/stat and return the sum of the 24690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// CPU-related ticks. Returns -1 on parse error. 24790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Exposed for testing. 24890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT int ParseProcStatCPU(const std::string& input); 24990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Get the number of threads of |process| as available in /proc/<pid>/stat. 2513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// This should be used with care as no synchronization with running threads is 2523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// done. This is mostly useful to guarantee being single-threaded. 2533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Returns 0 on failure. 2543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)BASE_EXPORT int GetNumberOfThreads(ProcessHandle process); 2553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 2563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// /proc/self/exe refers to the current executable. 2573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)BASE_EXPORT extern const char kProcSelfExe[]; 2583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Data from /proc/meminfo about system-wide memory consumption. 26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Values are in KB. 26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct BASE_EXPORT SystemMemoryInfoKB { 26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SystemMemoryInfoKB(); 26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 26458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Serializes the platform specific fields to value. 26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) scoped_ptr<Value> ToValue() const; 26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int total; 26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int free; 26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int buffers; 27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int cached; 27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int active_anon; 27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int inactive_anon; 27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int active_file; 27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int inactive_file; 27558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int swap_total; 27658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int swap_free; 27758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int dirty; 27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 27958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // vmstats data. 28058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int pswpin; 28158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int pswpout; 28258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int pgmajfault; 28358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 28458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#ifdef OS_CHROMEOS 28558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int shmem; 28658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) int slab; 28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Gem data will be -1 if not supported. 28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int gem_objects; 28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) long long gem_size; 29058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif 29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 2923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 2930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Parses a string containing the contents of /proc/meminfo 2940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// returns true on success or false for a parsing error 2950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)BASE_EXPORT bool ParseProcMeminfo(const std::string& input, 2960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) SystemMemoryInfoKB* meminfo); 2970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Parses a string containing the contents of /proc/vmstat 2990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// returns true on success or false for a parsing error 3000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)BASE_EXPORT bool ParseProcVmstat(const std::string& input, 3010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) SystemMemoryInfoKB* meminfo); 3020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 30358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Retrieves data from /proc/meminfo and /proc/vmstat 30458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// about system-wide memory consumption. 30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Fills in the provided |meminfo| structure. Returns true on success. 30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Exposed for memory debugging widget. 30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo); 30858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 30958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Data from /proc/diskstats about system-wide disk I/O. 31058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)struct BASE_EXPORT SystemDiskInfo { 31158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) SystemDiskInfo(); 31258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 31358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Serializes the platform specific fields to value. 31458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) scoped_ptr<Value> ToValue() const; 31558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 reads; 31758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 reads_merged; 31858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 sectors_read; 31958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 read_time; 32058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 writes; 32158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 writes_merged; 32258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 sectors_written; 32358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 write_time; 32458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 io; 32558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 io_time; 32658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) uint64 weighted_io_time; 32758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}; 32858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 3291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Checks whether the candidate string is a valid disk name, [hsv]d[a-z]+ 33058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// for a generic disk or mmcblk[0-9]+ for the MMC case. 33158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Names of disk partitions (e.g. sda1) are not valid. 33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)BASE_EXPORT bool IsValidDiskName(const std::string& candidate); 33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 33458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Retrieves data from /proc/diskstats about system-wide disk I/O. 33558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Fills in the provided |diskinfo| structure. Returns true on success. 33658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)BASE_EXPORT bool GetSystemDiskInfo(SystemDiskInfo* diskinfo); 33790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // defined(OS_LINUX) || defined(OS_ANDROID) 33890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 339424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(OS_CHROMEOS) 340424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Data from files in directory /sys/block/zram0 about ZRAM usage. 341424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)struct BASE_EXPORT SwapInfo { 342424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) SwapInfo() 343424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) : num_reads(0), 344424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) num_writes(0), 345424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) compr_data_size(0), 346424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) orig_data_size(0), 347424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) mem_used_total(0) { 348424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } 349424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 35058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Serializes the platform specific fields to value. 35158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) scoped_ptr<Value> ToValue() const; 35258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 353424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) uint64 num_reads; 354424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) uint64 num_writes; 355424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) uint64 compr_data_size; 356424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) uint64 orig_data_size; 357424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) uint64 mem_used_total; 358424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}; 359424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 360424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// In ChromeOS, reads files from /sys/block/zram0 that contain ZRAM usage data. 361424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Fills in the provided |swap_data| structure. 362424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)BASE_EXPORT void GetSwapInfo(SwapInfo* swap_info); 363424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif // defined(OS_CHROMEOS) 364424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 3653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Collects and holds performance metrics for system memory and disk. 3663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Provides functionality to retrieve the data on various platforms and 3673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// to serialize the stored data. 3683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class SystemMetrics { 3693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public: 37058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) SystemMetrics(); 3717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 3723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) static SystemMetrics Sample(); 3737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 37458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Serializes the system metrics to value. 37558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) scoped_ptr<Value> ToValue() const; 37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 3773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private: 37858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(SystemMetricsTest, SystemMetrics); 37958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 3803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) size_t committed_memory_; 3813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#if defined(OS_LINUX) || defined(OS_ANDROID) 3823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) SystemMemoryInfoKB memory_info_; 38358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) SystemDiskInfo disk_info_; 38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif 38558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS) 38658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) SwapInfo swap_info_; 3873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif 3883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}; 3897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 39090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace base 39190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 39290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // BASE_PROCESS_PROCESS_METRICS_H_ 393