process_metrics.cc revision 0d205d712abd16eeed2f5d5b1052a367d23a223f
1// Copyright 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#include <utility>
8
9#include "base/logging.h"
10#include "base/values.h"
11#include "build/build_config.h"
12
13namespace base {
14
15SystemMetrics::SystemMetrics() {
16  committed_memory_ = 0;
17}
18
19SystemMetrics SystemMetrics::Sample() {
20  SystemMetrics system_metrics;
21
22  system_metrics.committed_memory_ = GetSystemCommitCharge();
23#if defined(OS_LINUX) || defined(OS_ANDROID)
24  GetSystemMemoryInfo(&system_metrics.memory_info_);
25  GetSystemDiskInfo(&system_metrics.disk_info_);
26#endif
27#if defined(OS_CHROMEOS)
28  GetSwapInfo(&system_metrics.swap_info_);
29#endif
30
31  return system_metrics;
32}
33
34scoped_ptr<Value> SystemMetrics::ToValue() const {
35  scoped_ptr<DictionaryValue> res(new DictionaryValue());
36
37  res->SetInteger("committed_memory", static_cast<int>(committed_memory_));
38#if defined(OS_LINUX) || defined(OS_ANDROID)
39  res->Set("meminfo", memory_info_.ToValue());
40  res->Set("diskinfo", disk_info_.ToValue());
41#endif
42#if defined(OS_CHROMEOS)
43  res->Set("swapinfo", swap_info_.ToValue());
44#endif
45
46  return std::move(res);
47}
48
49ProcessMetrics* ProcessMetrics::CreateCurrentProcessMetrics() {
50#if !defined(OS_MACOSX) || defined(OS_IOS)
51  return CreateProcessMetrics(base::GetCurrentProcessHandle());
52#else
53  return CreateProcessMetrics(base::GetCurrentProcessHandle(), nullptr);
54#endif  // !defined(OS_MACOSX) || defined(OS_IOS)
55}
56
57double ProcessMetrics::GetPlatformIndependentCPUUsage() {
58#if defined(OS_WIN)
59  return GetCPUUsage() * processor_count_;
60#else
61  return GetCPUUsage();
62#endif
63}
64
65#if defined(OS_MACOSX) || defined(OS_LINUX)
66int ProcessMetrics::CalculateIdleWakeupsPerSecond(
67    uint64_t absolute_idle_wakeups) {
68  TimeTicks time = TimeTicks::Now();
69
70  if (last_absolute_idle_wakeups_ == 0) {
71    // First call, just set the last values.
72    last_idle_wakeups_time_ = time;
73    last_absolute_idle_wakeups_ = absolute_idle_wakeups;
74    return 0;
75  }
76
77  int64_t wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_;
78  int64_t time_delta = (time - last_idle_wakeups_time_).InMicroseconds();
79  if (time_delta == 0) {
80    NOTREACHED();
81    return 0;
82  }
83
84  last_idle_wakeups_time_ = time;
85  last_absolute_idle_wakeups_ = absolute_idle_wakeups;
86
87  // Round to average wakeups per second.
88  int64_t wakeups_delta_for_ms = wakeups_delta * Time::kMicrosecondsPerSecond;
89  return (wakeups_delta_for_ms + time_delta / 2) / time_delta;
90}
91#else
92int ProcessMetrics::GetIdleWakeupsPerSecond() {
93  NOTIMPLEMENTED();  // http://crbug.com/120488
94  return 0;
95}
96#endif  // defined(OS_MACOSX) || defined(OS_LINUX)
97
98}  // namespace base
99