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.
5 #include "base/process/process_metrics.h"
8 #include <mach/mach_vm.h>
9 #include <mach/shared_region.h>
10 #include <sys/sysctl.h>
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"
21 bool GetTaskInfo(mach_port_t task
, task_basic_info_64
* task_info_data
) {
22 if (task
== MACH_PORT_NULL
)
24 mach_msg_type_number_t count
= TASK_BASIC_INFO_64_COUNT
;
25 kern_return_t kr
= task_info(task
,
27 reinterpret_cast<task_info_t
>(task_info_data
),
29 // Most likely cause for failure: |task| is a zombie.
30 return kr
== KERN_SUCCESS
;
33 bool GetCPUTypeForProcess(pid_t pid
, cpu_type_t
* cpu_type
) {
34 size_t len
= sizeof(*cpu_type
);
35 int result
= sysctlbyname("sysctl.proc_cputype",
41 DPLOG(ERROR
) << "sysctlbyname(""sysctl.proc_cputype"")";
48 bool IsAddressInSharedRegion(mach_vm_address_t addr
, cpu_type_t type
) {
49 if (type
== CPU_TYPE_I386
) {
50 return addr
>= SHARED_REGION_BASE_I386
&&
51 addr
< (SHARED_REGION_BASE_I386
+ SHARED_REGION_SIZE_I386
);
52 } else if (type
== CPU_TYPE_X86_64
) {
53 return addr
>= SHARED_REGION_BASE_X86_64
&&
54 addr
< (SHARED_REGION_BASE_X86_64
+ SHARED_REGION_SIZE_X86_64
);
62 // Getting a mach task from a pid for another process requires permissions in
63 // general, so there doesn't really seem to be a way to do these (and spinning
64 // up ps to fetch each stats seems dangerous to put in a base api for anyone to
65 // call). Child processes ipc their port, so return something if available,
66 // otherwise return 0.
69 ProcessMetrics
* ProcessMetrics::CreateProcessMetrics(
70 ProcessHandle process
,
71 ProcessMetrics::PortProvider
* port_provider
) {
72 return new ProcessMetrics(process
, port_provider
);
75 size_t ProcessMetrics::GetPagefileUsage() const {
76 task_basic_info_64 task_info_data
;
77 if (!GetTaskInfo(TaskForPid(process_
), &task_info_data
))
79 return task_info_data
.virtual_size
;
82 size_t ProcessMetrics::GetPeakPagefileUsage() const {
86 size_t ProcessMetrics::GetWorkingSetSize() const {
87 task_basic_info_64 task_info_data
;
88 if (!GetTaskInfo(TaskForPid(process_
), &task_info_data
))
90 return task_info_data
.resident_size
;
93 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
97 // This is a rough approximation of the algorithm that libtop uses.
98 // private_bytes is the size of private resident memory.
99 // shared_bytes is the size of shared resident memory.
100 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes
,
101 size_t* shared_bytes
) {
103 size_t private_pages_count
= 0;
104 size_t shared_pages_count
= 0;
106 if (!private_bytes
&& !shared_bytes
)
109 mach_port_t task
= TaskForPid(process_
);
110 if (task
== MACH_PORT_NULL
) {
111 DLOG(ERROR
) << "Invalid process";
116 if (!GetCPUTypeForProcess(process_
, &cpu_type
))
119 // The same region can be referenced multiple times. To avoid double counting
120 // we need to keep track of which regions we've already counted.
121 base::hash_set
<int> seen_objects
;
123 // We iterate through each VM region in the task's address map. For shared
124 // memory we add up all the pages that are marked as shared. Like libtop we
125 // try to avoid counting pages that are also referenced by other tasks. Since
126 // we don't have access to the VM regions of other tasks the only hint we have
127 // is if the address is in the shared region area.
129 // Private memory is much simpler. We simply count the pages that are marked
130 // as private or copy on write (COW).
132 // See libtop_update_vm_regions in
133 // http://www.opensource.apple.com/source/top/top-67/libtop.c
134 mach_vm_size_t size
= 0;
135 for (mach_vm_address_t address
= MACH_VM_MIN_ADDRESS
;; address
+= size
) {
136 vm_region_top_info_data_t info
;
137 mach_msg_type_number_t info_count
= VM_REGION_TOP_INFO_COUNT
;
138 mach_port_t object_name
;
139 kr
= mach_vm_region(task
,
143 (vm_region_info_t
)&info
,
146 if (kr
== KERN_INVALID_ADDRESS
) {
147 // We're at the end of the address space.
149 } else if (kr
!= KERN_SUCCESS
) {
150 DLOG(ERROR
) << "Calling mach_vm_region failed with error: "
151 << mach_error_string(kr
);
155 if (IsAddressInSharedRegion(address
, cpu_type
) &&
156 info
.share_mode
!= SM_PRIVATE
)
159 if (info
.share_mode
== SM_COW
&& info
.ref_count
== 1)
160 info
.share_mode
= SM_PRIVATE
;
162 switch (info
.share_mode
) {
164 private_pages_count
+= info
.private_pages_resident
;
165 private_pages_count
+= info
.shared_pages_resident
;
168 private_pages_count
+= info
.private_pages_resident
;
171 if (seen_objects
.count(info
.obj_id
) == 0) {
172 // Only count the first reference to this region.
173 seen_objects
.insert(info
.obj_id
);
174 shared_pages_count
+= info
.shared_pages_resident
;
183 kr
= host_page_size(task
, &page_size
);
184 if (kr
!= KERN_SUCCESS
) {
185 DLOG(ERROR
) << "Failed to fetch host page size, error: "
186 << mach_error_string(kr
);
191 *private_bytes
= private_pages_count
* page_size
;
193 *shared_bytes
= shared_pages_count
* page_size
;
198 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes
* usage
) const {
201 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes
* ws_usage
) const {
202 size_t priv
= GetWorkingSetSize();
205 ws_usage
->priv
= priv
/ 1024;
206 ws_usage
->shareable
= 0;
207 ws_usage
->shared
= 0;
211 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \
212 (r)->tv_sec = (a)->seconds; \
213 (r)->tv_usec = (a)->microseconds; \
216 double ProcessMetrics::GetCPUUsage() {
217 mach_port_t task
= TaskForPid(process_
);
218 if (task
== MACH_PORT_NULL
)
223 // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage()
224 // in libtop.c), but this is more concise and gives the same results:
225 task_thread_times_info thread_info_data
;
226 mach_msg_type_number_t thread_info_count
= TASK_THREAD_TIMES_INFO_COUNT
;
228 TASK_THREAD_TIMES_INFO
,
229 reinterpret_cast<task_info_t
>(&thread_info_data
),
231 if (kr
!= KERN_SUCCESS
) {
232 // Most likely cause: |task| is a zombie.
236 task_basic_info_64 task_info_data
;
237 if (!GetTaskInfo(task
, &task_info_data
))
240 /* Set total_time. */
241 // thread info contains live time...
242 struct timeval user_timeval
, system_timeval
, task_timeval
;
243 TIME_VALUE_TO_TIMEVAL(&thread_info_data
.user_time
, &user_timeval
);
244 TIME_VALUE_TO_TIMEVAL(&thread_info_data
.system_time
, &system_timeval
);
245 timeradd(&user_timeval
, &system_timeval
, &task_timeval
);
247 // ... task info contains terminated time.
248 TIME_VALUE_TO_TIMEVAL(&task_info_data
.user_time
, &user_timeval
);
249 TIME_VALUE_TO_TIMEVAL(&task_info_data
.system_time
, &system_timeval
);
250 timeradd(&user_timeval
, &task_timeval
, &task_timeval
);
251 timeradd(&system_timeval
, &task_timeval
, &task_timeval
);
254 int retval
= gettimeofday(&now
, NULL
);
258 int64 time
= TimeValToMicroseconds(now
);
259 int64 task_time
= TimeValToMicroseconds(task_timeval
);
261 if ((last_system_time_
== 0) || (last_time_
== 0)) {
262 // First call, just set the last values.
263 last_system_time_
= task_time
;
268 int64 system_time_delta
= task_time
- last_system_time_
;
269 int64 time_delta
= time
- last_time_
;
270 DCHECK_NE(0U, time_delta
);
274 last_system_time_
= task_time
;
277 return static_cast<double>(system_time_delta
* 100.0) / time_delta
;
280 bool ProcessMetrics::GetIOCounters(IoCounters
* io_counters
) const {
284 ProcessMetrics::ProcessMetrics(ProcessHandle process
,
285 ProcessMetrics::PortProvider
* port_provider
)
288 last_system_time_(0),
289 port_provider_(port_provider
) {
290 processor_count_
= SysInfo::NumberOfProcessors();
293 mach_port_t
ProcessMetrics::TaskForPid(ProcessHandle process
) const {
294 mach_port_t task
= MACH_PORT_NULL
;
296 task
= port_provider_
->TaskForPid(process_
);
297 if (task
== MACH_PORT_NULL
&& process_
== getpid())
298 task
= mach_task_self();
302 // Bytes committed by the system.
303 size_t GetSystemCommitCharge() {
304 base::mac::ScopedMachPort
host(mach_host_self());
305 mach_msg_type_number_t count
= HOST_VM_INFO_COUNT
;
306 vm_statistics_data_t data
;
307 kern_return_t kr
= host_statistics(host
, HOST_VM_INFO
,
308 reinterpret_cast<host_info_t
>(&data
),
311 DLOG(WARNING
) << "Failed to fetch host statistics.";
316 kr
= host_page_size(host
, &page_size
);
318 DLOG(ERROR
) << "Failed to fetch host page size.";
322 return (data
.active_count
* page_size
) / 1024;