• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 namespace base {
14 
15 SystemMemoryInfoKB::SystemMemoryInfoKB() = default;
16 
17 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) =
18     default;
19 
SystemMetrics()20 SystemMetrics::SystemMetrics() {
21   committed_memory_ = 0;
22 }
23 
Sample()24 SystemMetrics SystemMetrics::Sample() {
25   SystemMetrics system_metrics;
26 
27   system_metrics.committed_memory_ = GetSystemCommitCharge();
28 #if defined(OS_LINUX) || defined(OS_ANDROID)
29   GetSystemMemoryInfo(&system_metrics.memory_info_);
30   GetSystemDiskInfo(&system_metrics.disk_info_);
31 #endif
32 #if defined(OS_CHROMEOS)
33   GetSwapInfo(&system_metrics.swap_info_);
34 #endif
35 
36   return system_metrics;
37 }
38 
ToValue() const39 std::unique_ptr<Value> SystemMetrics::ToValue() const {
40   std::unique_ptr<DictionaryValue> res(new DictionaryValue());
41 
42   res->SetInteger("committed_memory", static_cast<int>(committed_memory_));
43 #if defined(OS_LINUX) || defined(OS_ANDROID)
44   res->Set("meminfo", memory_info_.ToValue());
45   res->Set("diskinfo", disk_info_.ToValue());
46 #endif
47 #if defined(OS_CHROMEOS)
48   res->Set("swapinfo", swap_info_.ToValue());
49 #endif
50 
51   return std::move(res);
52 }
53 
CreateCurrentProcessMetrics()54 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateCurrentProcessMetrics() {
55 #if !defined(OS_MACOSX) || defined(OS_IOS)
56   return CreateProcessMetrics(base::GetCurrentProcessHandle());
57 #else
58   return CreateProcessMetrics(base::GetCurrentProcessHandle(), nullptr);
59 #endif  // !defined(OS_MACOSX) || defined(OS_IOS)
60 }
61 
GetPlatformIndependentCPUUsage()62 double ProcessMetrics::GetPlatformIndependentCPUUsage() {
63 #if defined(OS_WIN)
64   return GetCPUUsage() * processor_count_;
65 #else
66   return GetCPUUsage();
67 #endif
68 }
69 
70 #if defined(OS_MACOSX) || defined(OS_LINUX)
CalculateIdleWakeupsPerSecond(uint64_t absolute_idle_wakeups)71 int ProcessMetrics::CalculateIdleWakeupsPerSecond(
72     uint64_t absolute_idle_wakeups) {
73   TimeTicks time = TimeTicks::Now();
74 
75   if (last_absolute_idle_wakeups_ == 0) {
76     // First call, just set the last values.
77     last_idle_wakeups_time_ = time;
78     last_absolute_idle_wakeups_ = absolute_idle_wakeups;
79     return 0;
80   }
81 
82   int64_t wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_;
83   int64_t time_delta = (time - last_idle_wakeups_time_).InMicroseconds();
84   if (time_delta == 0) {
85     NOTREACHED();
86     return 0;
87   }
88 
89   last_idle_wakeups_time_ = time;
90   last_absolute_idle_wakeups_ = absolute_idle_wakeups;
91 
92   int64_t wakeups_delta_for_ms = wakeups_delta * Time::kMicrosecondsPerSecond;
93   // Round the result up by adding 1/2 (the second term resolves to 1/2 without
94   // dropping down into floating point).
95   return (wakeups_delta_for_ms + time_delta / 2) / time_delta;
96 }
97 #else
GetIdleWakeupsPerSecond()98 int ProcessMetrics::GetIdleWakeupsPerSecond() {
99   NOTIMPLEMENTED();  // http://crbug.com/120488
100   return 0;
101 }
102 #endif  // defined(OS_MACOSX) || defined(OS_LINUX)
103 
104 }  // namespace base
105