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