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" 1558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "base/process/process_handle.h" 16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_MACOSX) 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <mach/mach.h> 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace base { 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_WIN) 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct IoCounters : public IO_COUNTERS { 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#elif defined(OS_POSIX) 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct IoCounters { 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t ReadOperationCount; 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t WriteOperationCount; 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t OtherOperationCount; 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t ReadTransferCount; 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t WriteTransferCount; 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) uint64_t OtherTransferCount; 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Working Set (resident) memory usage broken down by 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// On Windows: 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// priv (private): These pages (kbytes) cannot be shared with any other process. 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shareable: These pages (kbytes) can be shared with other processes under 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// the right circumstances. 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shared : These pages (kbytes) are currently shared with at least one 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// other process. 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// On Linux: 487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// priv: Pages mapped only by this process. 497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// shared: PSS or 0 if the kernel doesn't support this. 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shareable: 0 517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// On ChromeOS: 537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// priv: Pages mapped only by this process. 547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// shared: PSS or 0 if the kernel doesn't support this. 557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// shareable: 0 567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// swapped Pages swapped out to zram. 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// On OS X: TODO(thakis): Revise. 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// priv: Memory. 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shared: 0 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shareable: 0 627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct WorkingSetKBytes { 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) WorkingSetKBytes() : priv(0), shareable(0), shared(0) {} 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t priv; 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t shareable; 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t shared; 687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_CHROMEOS) 697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch size_t swapped; 707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Committed (resident + paged) memory usage broken down by 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// private: These pages cannot be shared with any other process. 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// mapped: These pages are mapped into the view of a section (backed by 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// pagefile.sys) 7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// image: These pages are mapped into the view of an image section (backed by 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// file system) 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct CommittedKBytes { 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) CommittedKBytes() : priv(0), mapped(0), image(0) {} 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t priv; 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t mapped; 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t image; 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Free memory (Megabytes marked as free) in the 2G process address space. 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// total : total amount in megabytes marked as free. Maximum value is 2048. 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// largest : size of the largest contiguous amount of memory found. It is 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// always smaller or equal to FreeMBytes::total. 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// largest_ptr: starting address of the largest memory block. 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct FreeMBytes { 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t total; 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t largest; 9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void* largest_ptr; 9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Convert a POSIX timeval to microseconds. 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT int64 TimeValToMicroseconds(const struct timeval& tv); 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Provides performance metrics for a specified process (CPU usage, memory and 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// IO counters). To use it, invoke CreateProcessMetrics() to get an instance 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// for a specific process, then access the information with the different get 10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// methods. 10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class BASE_EXPORT ProcessMetrics { 10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ~ProcessMetrics(); 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Creates a ProcessMetrics for the specified process. 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The caller owns the returned object. 11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_MACOSX) || defined(OS_IOS) 11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static ProcessMetrics* CreateProcessMetrics(ProcessHandle process); 11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else 11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) class PortProvider { 11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual ~PortProvider() {} 11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Should return the mach task for |process| if possible, or else 11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // |MACH_PORT_NULL|. Only processes that this returns tasks for will have 11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // metrics on OS X (except for the current process, which always gets 12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // metrics). 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual mach_port_t TaskForPid(ProcessHandle process) const = 0; 12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }; 12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The port provider needs to outlive the ProcessMetrics object returned by 12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // this function. If NULL is passed as provider, the returned object 12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // only returns valid metrics if |process| is the current process. 12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static ProcessMetrics* CreateProcessMetrics(ProcessHandle process, 12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) PortProvider* port_provider); 12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // !defined(OS_MACOSX) || defined(OS_IOS) 13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the current space allocated for the pagefile, in bytes (these pages 13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // may or may not be in memory). On Linux, this returns the total virtual 13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // memory size. 13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetPagefileUsage() const; 13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the peak space allocated for the pagefile, in bytes. 13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetPeakPagefileUsage() const; 13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the current working set size, in bytes. On Linux, this returns 13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // the resident set size. 13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetWorkingSetSize() const; 14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the peak working set size, in bytes. 14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t GetPeakWorkingSetSize() const; 14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns private and sharedusage, in bytes. Private bytes is the amount of 14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // memory currently allocated to a process that cannot be shared. Returns 14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // false on platform specific error conditions. Note: |private_bytes| 14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // returns 0 on unsupported OSes: prior to XP SP2. 14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetMemoryBytes(size_t* private_bytes, 14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t* shared_bytes); 14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Fills a CommittedKBytes with both resident and paged 14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // memory usage as per definition of CommittedBytes. 15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void GetCommittedKBytes(CommittedKBytes* usage) const; 15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Fills a WorkingSetKBytes containing resident private and shared memory 15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // usage in bytes, as per definition of WorkingSetBytes. 15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; 15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Computes the current process available memory for allocation. 15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // It does a linear scan of the address space querying each memory region 15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // for its free (unallocated) status. It is useful for estimating the memory 15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // load and fragmentation. 15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool CalculateFreeMemory(FreeMBytes* free) const; 16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns the CPU usage in percent since the last time this method was 16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // called. The first time this method is called it returns 0 and will return 16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // the actual CPU info on subsequent calls. 16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // On Windows, the CPU usage value is for all CPUs. So if you have 2 CPUs and 16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // your process is using all the cycles of 1 CPU and not the other CPU, this 16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // method returns 50. 16790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) double GetCPUUsage(); 16890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 16990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Retrieves accounting information for all I/O operations performed by the 17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // process. 17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // If IO information is retrieved successfully, the function returns true 17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // and fills in the IO_COUNTERS passed in. The function returns false 17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // otherwise. 17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool GetIOCounters(IoCounters* io_counters) const; 17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private: 17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_MACOSX) || defined(OS_IOS) 17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) explicit ProcessMetrics(ProcessHandle process); 17990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else 18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ProcessMetrics(ProcessHandle process, PortProvider* port_provider); 18190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // !defined(OS_MACOSX) || defined(OS_IOS) 18290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_LINUX) || defined(OS_ANDROID) 184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage) const; 185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif 186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_CHROMEOS) 188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) const; 189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif 190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 19190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ProcessHandle process_; 19290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 19390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int processor_count_; 19490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 19590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Used to store the previous times and CPU usage counts so we can 19690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // compute the CPU usage between calls. 19790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int64 last_time_; 19890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int64 last_system_time_; 19990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 20090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(OS_IOS) 20190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_MACOSX) 20290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Queries the port provider if it's set. 20390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) mach_port_t TaskForPid(ProcessHandle process) const; 20490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 20590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) PortProvider* port_provider_; 20690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#elif defined(OS_POSIX) 20790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Jiffie count at the last_time_ we updated. 20890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int last_cpu_; 20990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // defined(OS_POSIX) 21090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // !defined(OS_IOS) 21190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 21290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ProcessMetrics); 21390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 21590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns the memory committed by the system in KBytes. 21690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns 0 if it can't compute the commit charge. 21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT size_t GetSystemCommitCharge(); 21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 21990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_LINUX) || defined(OS_ANDROID) 22090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Parse the data found in /proc/<pid>/stat and return the sum of the 22190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// CPU-related ticks. Returns -1 on parse error. 22290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Exposed for testing. 22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT int ParseProcStatCPU(const std::string& input); 22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Data from /proc/meminfo about system-wide memory consumption. 22690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Values are in KB. 22790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)struct BASE_EXPORT SystemMemoryInfoKB { 22890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SystemMemoryInfoKB(); 22990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int total; 23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int free; 23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int buffers; 23390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int cached; 23490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int active_anon; 23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int inactive_anon; 23690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int active_file; 23790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int inactive_file; 23890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int shmem; 23990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 24090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Gem data will be -1 if not supported. 24190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int gem_objects; 24290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) long long gem_size; 24390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 24490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Retrieves data from /proc/meminfo about system-wide memory consumption. 24590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Fills in the provided |meminfo| structure. Returns true on success. 24690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Exposed for memory debugging widget. 24790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo); 24890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // defined(OS_LINUX) || defined(OS_ANDROID) 24990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_LINUX) || defined(OS_ANDROID) 2517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Get the number of threads of |process| as available in /proc/<pid>/stat. 2527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// This should be used with care as no synchronization with running threads is 2537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// done. This is mostly useful to guarantee being single-threaded. 2547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Returns 0 on failure. 2557dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT int GetNumberOfThreads(ProcessHandle process); 2567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 2577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// /proc/self/exe refers to the current executable. 2587dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochBASE_EXPORT extern const char kProcSelfExe[]; 2597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif // defined(OS_LINUX) || defined(OS_ANDROID) 2607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 2617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#if defined(OS_POSIX) 2627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// Returns the maximum number of file descriptors that can be open by a process 2637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// at once. If the number is unavailable, a conservative best guess is returned. 2647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochsize_t GetMaxFds(); 2657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#endif // defined(OS_POSIX) 2667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace base 26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // BASE_PROCESS_PROCESS_METRICS_H_ 270