123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- #ifndef BASE_PROCESS_PROCESS_METRICS_H_
- #define BASE_PROCESS_PROCESS_METRICS_H_
- #include <stddef.h>
- #include <stdint.h>
- #include <memory>
- #include <string>
- #include "base/base_export.h"
- #include "base/gtest_prod_util.h"
- #include "base/macros.h"
- #include "base/process/process_handle.h"
- #include "base/time/time.h"
- #include "base/values.h"
- #include "build/build_config.h"
- #if defined(OS_MACOSX)
- #include <mach/mach.h>
- #include "base/process/port_provider_mac.h"
- #if !defined(OS_IOS)
- #include <mach/mach_vm.h>
- #endif
- #endif
- #if defined(OS_WIN)
- #include "base/win/scoped_handle.h"
- #include "base/win/windows_types.h"
- #endif
- namespace base {
- struct IoCounters;
- #if defined(OS_LINUX) || defined(OS_ANDROID)
- struct PageFaultCounts {
- int64_t minor;
- int64_t major;
- };
- #endif
- BASE_EXPORT int64_t TimeValToMicroseconds(const struct timeval& tv);
- class BASE_EXPORT ProcessMetrics {
- public:
- ~ProcessMetrics();
-
- #if !defined(OS_MACOSX) || defined(OS_IOS)
- static std::unique_ptr<ProcessMetrics> CreateProcessMetrics(
- ProcessHandle process);
- #else
-
-
-
- static std::unique_ptr<ProcessMetrics> CreateProcessMetrics(
- ProcessHandle process,
- PortProvider* port_provider);
- #endif
-
-
- static std::unique_ptr<ProcessMetrics> CreateCurrentProcessMetrics();
- #if defined(OS_LINUX) || defined(OS_ANDROID)
-
-
- BASE_EXPORT size_t GetResidentSetSize() const;
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
- double GetPlatformIndependentCPUUsage();
-
-
-
-
- TimeDelta GetCumulativeCPUUsage();
-
-
- int GetIdleWakeupsPerSecond();
- #if defined(OS_MACOSX)
-
-
-
-
-
-
-
-
-
-
-
-
-
- int GetPackageIdleWakeupsPerSecond();
-
-
- int GetEnergyImpact();
- #endif
-
-
-
-
-
- bool GetIOCounters(IoCounters* io_counters) const;
-
-
-
-
-
-
- uint64_t GetDiskUsageBytesPerSecond();
-
-
- uint64_t GetCumulativeDiskUsageInBytes();
- #if defined(OS_POSIX)
-
-
- int GetOpenFdCount() const;
-
-
- int GetOpenFdSoftLimit() const;
- #endif
- #if defined(OS_LINUX) || defined(OS_ANDROID)
-
- uint64_t GetVmSwapBytes() const;
-
-
- bool GetPageFaultCounts(PageFaultCounts* counts) const;
- #endif
-
- size_t GetMallocUsage();
- private:
- #if !defined(OS_MACOSX) || defined(OS_IOS)
- explicit ProcessMetrics(ProcessHandle process);
- #else
- ProcessMetrics(ProcessHandle process, PortProvider* port_provider);
- #endif
- #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_AIX)
- int CalculateIdleWakeupsPerSecond(uint64_t absolute_idle_wakeups);
- #endif
- #if defined(OS_MACOSX)
-
-
- int CalculatePackageIdleWakeupsPerSecond(
- uint64_t absolute_package_idle_wakeups);
- #endif
- #if defined(OS_WIN)
- win::ScopedHandle process_;
- #else
- ProcessHandle process_;
- #endif
-
-
- TimeTicks last_cpu_time_;
- #if !defined(OS_FREEBSD) || !defined(OS_POSIX)
- TimeDelta last_cumulative_cpu_;
- #endif
-
-
- TimeTicks last_disk_usage_time_;
-
- uint64_t last_cumulative_disk_usage_ = 0;
- #if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_AIX)
-
- TimeTicks last_idle_wakeups_time_;
- uint64_t last_absolute_idle_wakeups_;
- #endif
- #if defined(OS_MACOSX)
-
- TimeTicks last_package_idle_wakeups_time_;
- uint64_t last_absolute_package_idle_wakeups_;
- double last_energy_impact_;
-
- uint64_t last_energy_impact_time_;
- #endif
- #if !defined(OS_IOS)
- #if defined(OS_MACOSX)
-
- mach_port_t TaskForPid(ProcessHandle process) const;
- PortProvider* port_provider_;
- #endif
- #endif
- DISALLOW_COPY_AND_ASSIGN(ProcessMetrics);
- };
- BASE_EXPORT size_t GetSystemCommitCharge();
- BASE_EXPORT size_t GetPageSize();
- BASE_EXPORT size_t GetMaxFds();
- BASE_EXPORT size_t GetHandleLimit();
- #if defined(OS_POSIX)
- BASE_EXPORT void IncreaseFdLimitTo(unsigned int max_descriptors);
- #endif
- #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
- defined(OS_ANDROID) || defined(OS_AIX) || defined(OS_FUCHSIA)
- struct BASE_EXPORT SystemMemoryInfoKB {
- SystemMemoryInfoKB();
- SystemMemoryInfoKB(const SystemMemoryInfoKB& other);
-
- std::unique_ptr<DictionaryValue> ToValue() const;
- int total = 0;
- #if !defined(OS_WIN)
- int free = 0;
- #endif
- #if defined(OS_WIN)
-
-
-
-
-
- int avail_phys = 0;
- #endif
- #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
-
-
-
-
-
- int available = 0;
- #endif
- #if !defined(OS_MACOSX)
- int swap_total = 0;
- int swap_free = 0;
- #endif
- #if defined(OS_ANDROID) || defined(OS_LINUX) || defined(OS_AIX) || \
- defined(OS_FUCHSIA)
- int buffers = 0;
- int cached = 0;
- int active_anon = 0;
- int inactive_anon = 0;
- int active_file = 0;
- int inactive_file = 0;
- int dirty = 0;
- int reclaimable = 0;
- #endif
-
- #if defined(OS_CHROMEOS)
- int shmem = 0;
- int slab = 0;
-
- int gem_objects = -1;
- long long gem_size = -1;
- #endif
- #if defined(OS_MACOSX)
- int speculative = 0;
- int file_backed = 0;
- int purgeable = 0;
- #endif
- };
- BASE_EXPORT bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo);
- #endif
-
- #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_AIX)
- BASE_EXPORT int ParseProcStatCPU(StringPiece input);
- BASE_EXPORT int GetNumberOfThreads(ProcessHandle process);
- BASE_EXPORT extern const char kProcSelfExe[];
- BASE_EXPORT bool ParseProcMeminfo(StringPiece input,
- SystemMemoryInfoKB* meminfo);
- struct BASE_EXPORT VmStatInfo {
-
- std::unique_ptr<DictionaryValue> ToValue() const;
- unsigned long pswpin = 0;
- unsigned long pswpout = 0;
- unsigned long pgmajfault = 0;
- };
- BASE_EXPORT bool GetVmStatInfo(VmStatInfo* vmstat);
- BASE_EXPORT bool ParseProcVmstat(StringPiece input, VmStatInfo* vmstat);
- struct BASE_EXPORT SystemDiskInfo {
- SystemDiskInfo();
- SystemDiskInfo(const SystemDiskInfo& other);
-
- std::unique_ptr<Value> ToValue() const;
- uint64_t reads = 0;
- uint64_t reads_merged = 0;
- uint64_t sectors_read = 0;
- uint64_t read_time = 0;
- uint64_t writes = 0;
- uint64_t writes_merged = 0;
- uint64_t sectors_written = 0;
- uint64_t write_time = 0;
- uint64_t io = 0;
- uint64_t io_time = 0;
- uint64_t weighted_io_time = 0;
- };
- BASE_EXPORT bool IsValidDiskName(StringPiece candidate);
- BASE_EXPORT bool GetSystemDiskInfo(SystemDiskInfo* diskinfo);
- BASE_EXPORT TimeDelta GetUserCpuTimeSinceBoot();
- #endif
- #if defined(OS_CHROMEOS)
- struct BASE_EXPORT SwapInfo {
- SwapInfo()
- : num_reads(0),
- num_writes(0),
- compr_data_size(0),
- orig_data_size(0),
- mem_used_total(0) {
- }
-
- std::unique_ptr<Value> ToValue() const;
- uint64_t num_reads = 0;
- uint64_t num_writes = 0;
- uint64_t compr_data_size = 0;
- uint64_t orig_data_size = 0;
- uint64_t mem_used_total = 0;
- };
- BASE_EXPORT bool ParseZramMmStat(StringPiece mm_stat_data, SwapInfo* swap_info);
- BASE_EXPORT bool ParseZramStat(StringPiece stat_data, SwapInfo* swap_info);
- BASE_EXPORT bool GetSwapInfo(SwapInfo* swap_info);
- #endif
- struct BASE_EXPORT SystemPerformanceInfo {
- SystemPerformanceInfo();
- SystemPerformanceInfo(const SystemPerformanceInfo& other);
-
- std::unique_ptr<Value> ToValue() const;
-
- uint64_t idle_time = 0;
-
- uint64_t read_transfer_count = 0;
-
- uint64_t write_transfer_count = 0;
-
- uint64_t other_transfer_count = 0;
-
- uint64_t read_operation_count = 0;
-
- uint64_t write_operation_count = 0;
-
- uint64_t other_operation_count = 0;
-
- uint64_t pagefile_pages_written = 0;
-
- uint64_t pagefile_pages_write_ios = 0;
-
-
- uint64_t available_pages = 0;
-
- uint64_t pages_read = 0;
-
- uint64_t page_read_ios = 0;
- };
- BASE_EXPORT bool GetSystemPerformanceInfo(SystemPerformanceInfo* info);
- class BASE_EXPORT SystemMetrics {
- public:
- SystemMetrics();
- static SystemMetrics Sample();
-
- std::unique_ptr<Value> ToValue() const;
- private:
- FRIEND_TEST_ALL_PREFIXES(SystemMetricsTest, SystemMetrics);
- size_t committed_memory_;
- #if defined(OS_LINUX) || defined(OS_ANDROID)
- SystemMemoryInfoKB memory_info_;
- VmStatInfo vmstat_info_;
- SystemDiskInfo disk_info_;
- #endif
- #if defined(OS_CHROMEOS)
- SwapInfo swap_info_;
- #endif
- #if defined(OS_WIN)
- SystemPerformanceInfo performance_;
- #endif
- };
- #if defined(OS_MACOSX) && !defined(OS_IOS)
- enum class MachVMRegionResult {
-
-
- Finished,
-
- Error,
-
- Success
- };
- BASE_EXPORT MachVMRegionResult GetTopInfo(mach_port_t task,
- mach_vm_size_t* size,
- mach_vm_address_t* address,
- vm_region_top_info_data_t* info);
- BASE_EXPORT MachVMRegionResult GetBasicInfo(mach_port_t task,
- mach_vm_size_t* size,
- mach_vm_address_t* address,
- vm_region_basic_info_64* info);
- #endif
- }
- #endif
|