process_metrics_mac.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1// Copyright (c) 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 <mach/mach.h>
8#include <mach/mach_vm.h>
9#include <mach/shared_region.h>
10#include <sys/sysctl.h>
11
12#include "base/containers/hash_tables.h"
13#include "base/logging.h"
14#include "base/mac/scoped_mach_port.h"
15#include "base/sys_info.h"
16
17#if !defined(TASK_POWER_INFO)
18// Doesn't exist in the 10.6 or 10.7 SDKs.
19#define TASK_POWER_INFO        21
20struct task_power_info {
21        uint64_t                total_user;
22        uint64_t                total_system;
23        uint64_t                task_interrupt_wakeups;
24        uint64_t                task_platform_idle_wakeups;
25        uint64_t                task_timer_wakeups_bin_1;
26        uint64_t                task_timer_wakeups_bin_2;
27};
28typedef struct task_power_info        task_power_info_data_t;
29typedef struct task_power_info        *task_power_info_t;
30#define TASK_POWER_INFO_COUNT        ((mach_msg_type_number_t) \
31                (sizeof (task_power_info_data_t) / sizeof (natural_t)))
32#endif
33
34namespace base {
35
36namespace {
37
38bool GetTaskInfo(mach_port_t task, task_basic_info_64* task_info_data) {
39  if (task == MACH_PORT_NULL)
40    return false;
41  mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
42  kern_return_t kr = task_info(task,
43                               TASK_BASIC_INFO_64,
44                               reinterpret_cast<task_info_t>(task_info_data),
45                               &count);
46  // Most likely cause for failure: |task| is a zombie.
47  return kr == KERN_SUCCESS;
48}
49
50bool GetCPUTypeForProcess(pid_t pid, cpu_type_t* cpu_type) {
51  size_t len = sizeof(*cpu_type);
52  int result = sysctlbyname("sysctl.proc_cputype",
53                            cpu_type,
54                            &len,
55                            NULL,
56                            0);
57  if (result != 0) {
58    DPLOG(ERROR) << "sysctlbyname(""sysctl.proc_cputype"")";
59    return false;
60  }
61
62  return true;
63}
64
65bool IsAddressInSharedRegion(mach_vm_address_t addr, cpu_type_t type) {
66  if (type == CPU_TYPE_I386) {
67    return addr >= SHARED_REGION_BASE_I386 &&
68           addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386);
69  } else if (type == CPU_TYPE_X86_64) {
70    return addr >= SHARED_REGION_BASE_X86_64 &&
71           addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64);
72  } else {
73    return false;
74  }
75}
76
77}  // namespace
78
79// Getting a mach task from a pid for another process requires permissions in
80// general, so there doesn't really seem to be a way to do these (and spinning
81// up ps to fetch each stats seems dangerous to put in a base api for anyone to
82// call). Child processes ipc their port, so return something if available,
83// otherwise return 0.
84
85// static
86ProcessMetrics* ProcessMetrics::CreateProcessMetrics(
87    ProcessHandle process,
88    ProcessMetrics::PortProvider* port_provider) {
89  return new ProcessMetrics(process, port_provider);
90}
91
92size_t ProcessMetrics::GetPagefileUsage() const {
93  task_basic_info_64 task_info_data;
94  if (!GetTaskInfo(TaskForPid(process_), &task_info_data))
95    return 0;
96  return task_info_data.virtual_size;
97}
98
99size_t ProcessMetrics::GetPeakPagefileUsage() const {
100  return 0;
101}
102
103size_t ProcessMetrics::GetWorkingSetSize() const {
104  task_basic_info_64 task_info_data;
105  if (!GetTaskInfo(TaskForPid(process_), &task_info_data))
106    return 0;
107  return task_info_data.resident_size;
108}
109
110size_t ProcessMetrics::GetPeakWorkingSetSize() const {
111  return 0;
112}
113
114// This is a rough approximation of the algorithm that libtop uses.
115// private_bytes is the size of private resident memory.
116// shared_bytes is the size of shared resident memory.
117bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
118                                    size_t* shared_bytes) {
119  kern_return_t kr;
120  size_t private_pages_count = 0;
121  size_t shared_pages_count = 0;
122
123  if (!private_bytes && !shared_bytes)
124    return true;
125
126  mach_port_t task = TaskForPid(process_);
127  if (task == MACH_PORT_NULL) {
128    DLOG(ERROR) << "Invalid process";
129    return false;
130  }
131
132  cpu_type_t cpu_type;
133  if (!GetCPUTypeForProcess(process_, &cpu_type))
134    return false;
135
136  // The same region can be referenced multiple times. To avoid double counting
137  // we need to keep track of which regions we've already counted.
138  base::hash_set<int> seen_objects;
139
140  // We iterate through each VM region in the task's address map. For shared
141  // memory we add up all the pages that are marked as shared. Like libtop we
142  // try to avoid counting pages that are also referenced by other tasks. Since
143  // we don't have access to the VM regions of other tasks the only hint we have
144  // is if the address is in the shared region area.
145  //
146  // Private memory is much simpler. We simply count the pages that are marked
147  // as private or copy on write (COW).
148  //
149  // See libtop_update_vm_regions in
150  // http://www.opensource.apple.com/source/top/top-67/libtop.c
151  mach_vm_size_t size = 0;
152  for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) {
153    vm_region_top_info_data_t info;
154    mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
155    mach_port_t object_name;
156    kr = mach_vm_region(task,
157                        &address,
158                        &size,
159                        VM_REGION_TOP_INFO,
160                        (vm_region_info_t)&info,
161                        &info_count,
162                        &object_name);
163    if (kr == KERN_INVALID_ADDRESS) {
164      // We're at the end of the address space.
165      break;
166    } else if (kr != KERN_SUCCESS) {
167      DLOG(ERROR) << "Calling mach_vm_region failed with error: "
168                 << mach_error_string(kr);
169      return false;
170    }
171
172    if (IsAddressInSharedRegion(address, cpu_type) &&
173        info.share_mode != SM_PRIVATE)
174      continue;
175
176    if (info.share_mode == SM_COW && info.ref_count == 1)
177      info.share_mode = SM_PRIVATE;
178
179    switch (info.share_mode) {
180      case SM_PRIVATE:
181        private_pages_count += info.private_pages_resident;
182        private_pages_count += info.shared_pages_resident;
183        break;
184      case SM_COW:
185        private_pages_count += info.private_pages_resident;
186        // Fall through
187      case SM_SHARED:
188        if (seen_objects.count(info.obj_id) == 0) {
189          // Only count the first reference to this region.
190          seen_objects.insert(info.obj_id);
191          shared_pages_count += info.shared_pages_resident;
192        }
193        break;
194      default:
195        break;
196    }
197  }
198
199  vm_size_t page_size;
200  kr = host_page_size(task, &page_size);
201  if (kr != KERN_SUCCESS) {
202    DLOG(ERROR) << "Failed to fetch host page size, error: "
203               << mach_error_string(kr);
204    return false;
205  }
206
207  if (private_bytes)
208    *private_bytes = private_pages_count * page_size;
209  if (shared_bytes)
210    *shared_bytes = shared_pages_count * page_size;
211
212  return true;
213}
214
215void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const {
216}
217
218bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
219  size_t priv = GetWorkingSetSize();
220  if (!priv)
221    return false;
222  ws_usage->priv = priv / 1024;
223  ws_usage->shareable = 0;
224  ws_usage->shared = 0;
225  return true;
226}
227
228#define TIME_VALUE_TO_TIMEVAL(a, r) do {  \
229  (r)->tv_sec = (a)->seconds;             \
230  (r)->tv_usec = (a)->microseconds;       \
231} while (0)
232
233double ProcessMetrics::GetCPUUsage() {
234  mach_port_t task = TaskForPid(process_);
235  if (task == MACH_PORT_NULL)
236    return 0;
237
238  kern_return_t kr;
239
240  // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage()
241  // in libtop.c), but this is more concise and gives the same results:
242  task_thread_times_info thread_info_data;
243  mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
244  kr = task_info(task,
245                 TASK_THREAD_TIMES_INFO,
246                 reinterpret_cast<task_info_t>(&thread_info_data),
247                 &thread_info_count);
248  if (kr != KERN_SUCCESS) {
249    // Most likely cause: |task| is a zombie.
250    return 0;
251  }
252
253  task_basic_info_64 task_info_data;
254  if (!GetTaskInfo(task, &task_info_data))
255    return 0;
256
257  /* Set total_time. */
258  // thread info contains live time...
259  struct timeval user_timeval, system_timeval, task_timeval;
260  TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);
261  TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);
262  timeradd(&user_timeval, &system_timeval, &task_timeval);
263
264  // ... task info contains terminated time.
265  TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval);
266  TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval);
267  timeradd(&user_timeval, &task_timeval, &task_timeval);
268  timeradd(&system_timeval, &task_timeval, &task_timeval);
269
270  TimeTicks time = TimeTicks::Now();
271  int64 task_time = TimeValToMicroseconds(task_timeval);
272
273  if (last_system_time_ == 0) {
274    // First call, just set the last values.
275    last_cpu_time_ = time;
276    last_system_time_ = task_time;
277    return 0;
278  }
279
280  int64 system_time_delta = task_time - last_system_time_;
281  int64 time_delta = (time - last_cpu_time_).InMicroseconds();
282  DCHECK_NE(0U, time_delta);
283  if (time_delta == 0)
284    return 0;
285
286  last_cpu_time_ = time;
287  last_system_time_ = task_time;
288
289  return static_cast<double>(system_time_delta * 100.0) / time_delta;
290}
291
292int ProcessMetrics::GetIdleWakeupsPerSecond() {
293  mach_port_t task = TaskForPid(process_);
294  if (task == MACH_PORT_NULL)
295    return 0;
296
297  kern_return_t kr;
298
299  task_power_info power_info_data;
300  mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT;
301  kr = task_info(task,
302                 TASK_POWER_INFO,
303                 reinterpret_cast<task_info_t>(&power_info_data),
304                 &power_info_count);
305  if (kr != KERN_SUCCESS) {
306    // Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system
307    // where TASK_POWER_INFO isn't supported yet.
308    return 0;
309  }
310  uint64_t absolute_idle_wakeups = power_info_data.task_platform_idle_wakeups;
311
312  TimeTicks time = TimeTicks::Now();
313
314  if (last_absolute_idle_wakeups_ == 0) {
315    // First call, just set the last values.
316    last_idle_wakeups_time_ = time;
317    last_absolute_idle_wakeups_ = absolute_idle_wakeups;
318    return 0;
319  }
320
321  int64 wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_;
322  int64 time_delta = (time - last_idle_wakeups_time_).InMicroseconds();
323  DCHECK_NE(0U, time_delta);
324  if (time_delta == 0)
325    return 0;
326
327  last_idle_wakeups_time_ = time;
328  last_absolute_idle_wakeups_ = absolute_idle_wakeups;
329
330  // Round to average wakeups per second.
331  const int kMicrosecondsPerSecond = 1000 * 1000;
332  return (wakeups_delta * kMicrosecondsPerSecond + time_delta/2) / time_delta;
333}
334
335bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
336  return false;
337}
338
339ProcessMetrics::ProcessMetrics(ProcessHandle process,
340                               ProcessMetrics::PortProvider* port_provider)
341    : process_(process),
342      last_system_time_(0),
343      last_absolute_idle_wakeups_(0),
344      port_provider_(port_provider) {
345  processor_count_ = SysInfo::NumberOfProcessors();
346}
347
348mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const {
349  mach_port_t task = MACH_PORT_NULL;
350  if (port_provider_)
351    task = port_provider_->TaskForPid(process_);
352  if (task == MACH_PORT_NULL && process_ == getpid())
353    task = mach_task_self();
354  return task;
355}
356
357// Bytes committed by the system.
358size_t GetSystemCommitCharge() {
359  base::mac::ScopedMachPort host(mach_host_self());
360  mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
361  vm_statistics_data_t data;
362  kern_return_t kr = host_statistics(host, HOST_VM_INFO,
363                                     reinterpret_cast<host_info_t>(&data),
364                                     &count);
365  if (kr) {
366    DLOG(WARNING) << "Failed to fetch host statistics.";
367    return 0;
368  }
369
370  vm_size_t page_size;
371  kr = host_page_size(host, &page_size);
372  if (kr) {
373    DLOG(ERROR) << "Failed to fetch host page size.";
374    return 0;
375  }
376
377  return (data.active_count * page_size) / 1024;
378}
379
380}  // namespace base
381