1*6777b538SAndroid Build Coastguard Worker // Copyright 2023 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #include "base/process/process_metrics.h"
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard Worker #include <AvailabilityMacros.h>
8*6777b538SAndroid Build Coastguard Worker #include <mach/mach.h>
9*6777b538SAndroid Build Coastguard Worker #include <mach/mach_time.h>
10*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
11*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
12*6777b538SAndroid Build Coastguard Worker #include <sys/sysctl.h>
13*6777b538SAndroid Build Coastguard Worker
14*6777b538SAndroid Build Coastguard Worker #include <optional>
15*6777b538SAndroid Build Coastguard Worker
16*6777b538SAndroid Build Coastguard Worker #include "base/apple/mach_logging.h"
17*6777b538SAndroid Build Coastguard Worker #include "base/apple/scoped_mach_port.h"
18*6777b538SAndroid Build Coastguard Worker #include "base/logging.h"
19*6777b538SAndroid Build Coastguard Worker #include "base/mac/mac_util.h"
20*6777b538SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
21*6777b538SAndroid Build Coastguard Worker #include "base/notimplemented.h"
22*6777b538SAndroid Build Coastguard Worker #include "base/numerics/safe_math.h"
23*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
24*6777b538SAndroid Build Coastguard Worker #include "base/types/expected.h"
25*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
26*6777b538SAndroid Build Coastguard Worker
27*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC)
28*6777b538SAndroid Build Coastguard Worker #include <libproc.h>
29*6777b538SAndroid Build Coastguard Worker #include <mach/mach_vm.h>
30*6777b538SAndroid Build Coastguard Worker #include <mach/shared_region.h>
31*6777b538SAndroid Build Coastguard Worker #else
32*6777b538SAndroid Build Coastguard Worker #include <mach/vm_region.h>
33*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(USE_BLINK)
34*6777b538SAndroid Build Coastguard Worker #include "base/ios/sim_header_shims.h"
35*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(USE_BLINK)
36*6777b538SAndroid Build Coastguard Worker #endif
37*6777b538SAndroid Build Coastguard Worker
38*6777b538SAndroid Build Coastguard Worker namespace base {
39*6777b538SAndroid Build Coastguard Worker
40*6777b538SAndroid Build Coastguard Worker #define TIME_VALUE_TO_TIMEVAL(a, r) \
41*6777b538SAndroid Build Coastguard Worker do { \
42*6777b538SAndroid Build Coastguard Worker (r)->tv_sec = (a)->seconds; \
43*6777b538SAndroid Build Coastguard Worker (r)->tv_usec = (a)->microseconds; \
44*6777b538SAndroid Build Coastguard Worker } while (0)
45*6777b538SAndroid Build Coastguard Worker
46*6777b538SAndroid Build Coastguard Worker namespace {
47*6777b538SAndroid Build Coastguard Worker
GetTaskInfo(mach_port_t task)48*6777b538SAndroid Build Coastguard Worker base::expected<task_basic_info_64, ProcessCPUUsageError> GetTaskInfo(
49*6777b538SAndroid Build Coastguard Worker mach_port_t task) {
50*6777b538SAndroid Build Coastguard Worker if (task == MACH_PORT_NULL) {
51*6777b538SAndroid Build Coastguard Worker return base::unexpected(ProcessCPUUsageError::kProcessNotFound);
52*6777b538SAndroid Build Coastguard Worker }
53*6777b538SAndroid Build Coastguard Worker task_basic_info_64 task_info_data{};
54*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
55*6777b538SAndroid Build Coastguard Worker kern_return_t kr =
56*6777b538SAndroid Build Coastguard Worker task_info(task, TASK_BASIC_INFO_64,
57*6777b538SAndroid Build Coastguard Worker reinterpret_cast<task_info_t>(&task_info_data), &count);
58*6777b538SAndroid Build Coastguard Worker // Most likely cause for failure: |task| is a zombie.
59*6777b538SAndroid Build Coastguard Worker if (kr != KERN_SUCCESS) {
60*6777b538SAndroid Build Coastguard Worker return base::unexpected(ProcessCPUUsageError::kSystemError);
61*6777b538SAndroid Build Coastguard Worker }
62*6777b538SAndroid Build Coastguard Worker return base::ok(task_info_data);
63*6777b538SAndroid Build Coastguard Worker }
64*6777b538SAndroid Build Coastguard Worker
ParseOutputFromMachVMRegion(kern_return_t kr)65*6777b538SAndroid Build Coastguard Worker MachVMRegionResult ParseOutputFromMachVMRegion(kern_return_t kr) {
66*6777b538SAndroid Build Coastguard Worker if (kr == KERN_INVALID_ADDRESS) {
67*6777b538SAndroid Build Coastguard Worker // We're at the end of the address space.
68*6777b538SAndroid Build Coastguard Worker return MachVMRegionResult::Finished;
69*6777b538SAndroid Build Coastguard Worker } else if (kr != KERN_SUCCESS) {
70*6777b538SAndroid Build Coastguard Worker return MachVMRegionResult::Error;
71*6777b538SAndroid Build Coastguard Worker }
72*6777b538SAndroid Build Coastguard Worker return MachVMRegionResult::Success;
73*6777b538SAndroid Build Coastguard Worker }
74*6777b538SAndroid Build Coastguard Worker
GetPowerInfo(mach_port_t task,task_power_info * power_info_data)75*6777b538SAndroid Build Coastguard Worker bool GetPowerInfo(mach_port_t task, task_power_info* power_info_data) {
76*6777b538SAndroid Build Coastguard Worker if (task == MACH_PORT_NULL) {
77*6777b538SAndroid Build Coastguard Worker return false;
78*6777b538SAndroid Build Coastguard Worker }
79*6777b538SAndroid Build Coastguard Worker
80*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT;
81*6777b538SAndroid Build Coastguard Worker kern_return_t kr = task_info(task, TASK_POWER_INFO,
82*6777b538SAndroid Build Coastguard Worker reinterpret_cast<task_info_t>(power_info_data),
83*6777b538SAndroid Build Coastguard Worker &power_info_count);
84*6777b538SAndroid Build Coastguard Worker // Most likely cause for failure: |task| is a zombie.
85*6777b538SAndroid Build Coastguard Worker return kr == KERN_SUCCESS;
86*6777b538SAndroid Build Coastguard Worker }
87*6777b538SAndroid Build Coastguard Worker
88*6777b538SAndroid Build Coastguard Worker } // namespace
89*6777b538SAndroid Build Coastguard Worker
90*6777b538SAndroid Build Coastguard Worker // Implementations of ProcessMetrics class shared by Mac and iOS.
TaskForHandle(ProcessHandle process_handle) const91*6777b538SAndroid Build Coastguard Worker mach_port_t ProcessMetrics::TaskForHandle(ProcessHandle process_handle) const {
92*6777b538SAndroid Build Coastguard Worker mach_port_t task = MACH_PORT_NULL;
93*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC)
94*6777b538SAndroid Build Coastguard Worker if (port_provider_) {
95*6777b538SAndroid Build Coastguard Worker task = port_provider_->TaskForHandle(process_);
96*6777b538SAndroid Build Coastguard Worker }
97*6777b538SAndroid Build Coastguard Worker #endif
98*6777b538SAndroid Build Coastguard Worker if (task == MACH_PORT_NULL && process_handle == getpid()) {
99*6777b538SAndroid Build Coastguard Worker task = mach_task_self();
100*6777b538SAndroid Build Coastguard Worker }
101*6777b538SAndroid Build Coastguard Worker return task;
102*6777b538SAndroid Build Coastguard Worker }
103*6777b538SAndroid Build Coastguard Worker
104*6777b538SAndroid Build Coastguard Worker base::expected<TimeDelta, ProcessCPUUsageError>
GetCumulativeCPUUsage()105*6777b538SAndroid Build Coastguard Worker ProcessMetrics::GetCumulativeCPUUsage() {
106*6777b538SAndroid Build Coastguard Worker mach_port_t task = TaskForHandle(process_);
107*6777b538SAndroid Build Coastguard Worker if (task == MACH_PORT_NULL) {
108*6777b538SAndroid Build Coastguard Worker return base::unexpected(ProcessCPUUsageError::kProcessNotFound);
109*6777b538SAndroid Build Coastguard Worker }
110*6777b538SAndroid Build Coastguard Worker
111*6777b538SAndroid Build Coastguard Worker // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage()
112*6777b538SAndroid Build Coastguard Worker // in libtop.c), but this is more concise and gives the same results:
113*6777b538SAndroid Build Coastguard Worker task_thread_times_info thread_info_data;
114*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
115*6777b538SAndroid Build Coastguard Worker kern_return_t kr = task_info(task, TASK_THREAD_TIMES_INFO,
116*6777b538SAndroid Build Coastguard Worker reinterpret_cast<task_info_t>(&thread_info_data),
117*6777b538SAndroid Build Coastguard Worker &thread_info_count);
118*6777b538SAndroid Build Coastguard Worker if (kr != KERN_SUCCESS) {
119*6777b538SAndroid Build Coastguard Worker // Most likely cause: |task| is a zombie.
120*6777b538SAndroid Build Coastguard Worker return base::unexpected(ProcessCPUUsageError::kSystemError);
121*6777b538SAndroid Build Coastguard Worker }
122*6777b538SAndroid Build Coastguard Worker
123*6777b538SAndroid Build Coastguard Worker const base::expected<task_basic_info_64, ProcessCPUUsageError>
124*6777b538SAndroid Build Coastguard Worker task_info_data = GetTaskInfo(task);
125*6777b538SAndroid Build Coastguard Worker if (!task_info_data.has_value()) {
126*6777b538SAndroid Build Coastguard Worker return base::unexpected(task_info_data.error());
127*6777b538SAndroid Build Coastguard Worker }
128*6777b538SAndroid Build Coastguard Worker
129*6777b538SAndroid Build Coastguard Worker /* Set total_time. */
130*6777b538SAndroid Build Coastguard Worker // thread info contains live time...
131*6777b538SAndroid Build Coastguard Worker struct timeval user_timeval, system_timeval, task_timeval;
132*6777b538SAndroid Build Coastguard Worker TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);
133*6777b538SAndroid Build Coastguard Worker TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);
134*6777b538SAndroid Build Coastguard Worker timeradd(&user_timeval, &system_timeval, &task_timeval);
135*6777b538SAndroid Build Coastguard Worker
136*6777b538SAndroid Build Coastguard Worker // ... task info contains terminated time.
137*6777b538SAndroid Build Coastguard Worker TIME_VALUE_TO_TIMEVAL(&task_info_data->user_time, &user_timeval);
138*6777b538SAndroid Build Coastguard Worker TIME_VALUE_TO_TIMEVAL(&task_info_data->system_time, &system_timeval);
139*6777b538SAndroid Build Coastguard Worker timeradd(&user_timeval, &task_timeval, &task_timeval);
140*6777b538SAndroid Build Coastguard Worker timeradd(&system_timeval, &task_timeval, &task_timeval);
141*6777b538SAndroid Build Coastguard Worker
142*6777b538SAndroid Build Coastguard Worker const TimeDelta measured_cpu =
143*6777b538SAndroid Build Coastguard Worker Microseconds(TimeValToMicroseconds(task_timeval));
144*6777b538SAndroid Build Coastguard Worker if (measured_cpu < last_measured_cpu_) {
145*6777b538SAndroid Build Coastguard Worker // When a thread terminates, its CPU time is immediately removed from the
146*6777b538SAndroid Build Coastguard Worker // running thread times returned by TASK_THREAD_TIMES_INFO, but there can be
147*6777b538SAndroid Build Coastguard Worker // a lag before it shows up in the terminated thread times returned by
148*6777b538SAndroid Build Coastguard Worker // GetTaskInfo(). Make sure CPU usage doesn't appear to go backwards if
149*6777b538SAndroid Build Coastguard Worker // GetCumulativeCPUUsage() is called in the interval.
150*6777b538SAndroid Build Coastguard Worker return base::ok(last_measured_cpu_);
151*6777b538SAndroid Build Coastguard Worker }
152*6777b538SAndroid Build Coastguard Worker last_measured_cpu_ = measured_cpu;
153*6777b538SAndroid Build Coastguard Worker return base::ok(measured_cpu);
154*6777b538SAndroid Build Coastguard Worker }
155*6777b538SAndroid Build Coastguard Worker
GetPackageIdleWakeupsPerSecond()156*6777b538SAndroid Build Coastguard Worker int ProcessMetrics::GetPackageIdleWakeupsPerSecond() {
157*6777b538SAndroid Build Coastguard Worker mach_port_t task = TaskForHandle(process_);
158*6777b538SAndroid Build Coastguard Worker task_power_info power_info_data;
159*6777b538SAndroid Build Coastguard Worker
160*6777b538SAndroid Build Coastguard Worker GetPowerInfo(task, &power_info_data);
161*6777b538SAndroid Build Coastguard Worker
162*6777b538SAndroid Build Coastguard Worker // The task_power_info struct contains two wakeup counters:
163*6777b538SAndroid Build Coastguard Worker // task_interrupt_wakeups and task_platform_idle_wakeups.
164*6777b538SAndroid Build Coastguard Worker // task_interrupt_wakeups is the total number of wakeups generated by the
165*6777b538SAndroid Build Coastguard Worker // process, and is the number that Activity Monitor reports.
166*6777b538SAndroid Build Coastguard Worker // task_platform_idle_wakeups is a subset of task_interrupt_wakeups that
167*6777b538SAndroid Build Coastguard Worker // tallies the number of times the processor was taken out of its low-power
168*6777b538SAndroid Build Coastguard Worker // idle state to handle a wakeup. task_platform_idle_wakeups therefore result
169*6777b538SAndroid Build Coastguard Worker // in a greater power increase than the other interrupts which occur while the
170*6777b538SAndroid Build Coastguard Worker // CPU is already working, and reducing them has a greater overall impact on
171*6777b538SAndroid Build Coastguard Worker // power usage. See the powermetrics man page for more info.
172*6777b538SAndroid Build Coastguard Worker return CalculatePackageIdleWakeupsPerSecond(
173*6777b538SAndroid Build Coastguard Worker power_info_data.task_platform_idle_wakeups);
174*6777b538SAndroid Build Coastguard Worker }
175*6777b538SAndroid Build Coastguard Worker
GetIdleWakeupsPerSecond()176*6777b538SAndroid Build Coastguard Worker int ProcessMetrics::GetIdleWakeupsPerSecond() {
177*6777b538SAndroid Build Coastguard Worker mach_port_t task = TaskForHandle(process_);
178*6777b538SAndroid Build Coastguard Worker task_power_info power_info_data;
179*6777b538SAndroid Build Coastguard Worker
180*6777b538SAndroid Build Coastguard Worker GetPowerInfo(task, &power_info_data);
181*6777b538SAndroid Build Coastguard Worker
182*6777b538SAndroid Build Coastguard Worker return CalculateIdleWakeupsPerSecond(power_info_data.task_interrupt_wakeups);
183*6777b538SAndroid Build Coastguard Worker }
184*6777b538SAndroid Build Coastguard Worker
185*6777b538SAndroid Build Coastguard Worker // Bytes committed by the system.
GetSystemCommitCharge()186*6777b538SAndroid Build Coastguard Worker size_t GetSystemCommitCharge() {
187*6777b538SAndroid Build Coastguard Worker base::apple::ScopedMachSendRight host(mach_host_self());
188*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
189*6777b538SAndroid Build Coastguard Worker vm_statistics_data_t data;
190*6777b538SAndroid Build Coastguard Worker kern_return_t kr = host_statistics(
191*6777b538SAndroid Build Coastguard Worker host.get(), HOST_VM_INFO, reinterpret_cast<host_info_t>(&data), &count);
192*6777b538SAndroid Build Coastguard Worker if (kr != KERN_SUCCESS) {
193*6777b538SAndroid Build Coastguard Worker MACH_DLOG(WARNING, kr) << "host_statistics";
194*6777b538SAndroid Build Coastguard Worker return 0;
195*6777b538SAndroid Build Coastguard Worker }
196*6777b538SAndroid Build Coastguard Worker
197*6777b538SAndroid Build Coastguard Worker return (data.active_count * PAGE_SIZE) / 1024;
198*6777b538SAndroid Build Coastguard Worker }
199*6777b538SAndroid Build Coastguard Worker
GetSystemMemoryInfo(SystemMemoryInfoKB * meminfo)200*6777b538SAndroid Build Coastguard Worker bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
201*6777b538SAndroid Build Coastguard Worker struct host_basic_info hostinfo;
202*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
203*6777b538SAndroid Build Coastguard Worker base::apple::ScopedMachSendRight host(mach_host_self());
204*6777b538SAndroid Build Coastguard Worker int result = host_info(host.get(), HOST_BASIC_INFO,
205*6777b538SAndroid Build Coastguard Worker reinterpret_cast<host_info_t>(&hostinfo), &count);
206*6777b538SAndroid Build Coastguard Worker if (result != KERN_SUCCESS) {
207*6777b538SAndroid Build Coastguard Worker return false;
208*6777b538SAndroid Build Coastguard Worker }
209*6777b538SAndroid Build Coastguard Worker
210*6777b538SAndroid Build Coastguard Worker DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
211*6777b538SAndroid Build Coastguard Worker meminfo->total = static_cast<int>(hostinfo.max_mem / 1024);
212*6777b538SAndroid Build Coastguard Worker
213*6777b538SAndroid Build Coastguard Worker vm_statistics64_data_t vm_info;
214*6777b538SAndroid Build Coastguard Worker count = HOST_VM_INFO64_COUNT;
215*6777b538SAndroid Build Coastguard Worker
216*6777b538SAndroid Build Coastguard Worker if (host_statistics64(host.get(), HOST_VM_INFO64,
217*6777b538SAndroid Build Coastguard Worker reinterpret_cast<host_info64_t>(&vm_info),
218*6777b538SAndroid Build Coastguard Worker &count) != KERN_SUCCESS) {
219*6777b538SAndroid Build Coastguard Worker return false;
220*6777b538SAndroid Build Coastguard Worker }
221*6777b538SAndroid Build Coastguard Worker DCHECK_EQ(HOST_VM_INFO64_COUNT, count);
222*6777b538SAndroid Build Coastguard Worker
223*6777b538SAndroid Build Coastguard Worker #if defined(ARCH_CPU_ARM64) || \
224*6777b538SAndroid Build Coastguard Worker MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_16
225*6777b538SAndroid Build Coastguard Worker // PAGE_SIZE is vm_page_size on arm or for deployment targets >= 10.16,
226*6777b538SAndroid Build Coastguard Worker // and vm_page_size isn't constexpr.
227*6777b538SAndroid Build Coastguard Worker DCHECK_EQ(PAGE_SIZE % 1024, 0u) << "Invalid page size";
228*6777b538SAndroid Build Coastguard Worker #else
229*6777b538SAndroid Build Coastguard Worker static_assert(PAGE_SIZE % 1024 == 0, "Invalid page size");
230*6777b538SAndroid Build Coastguard Worker #endif
231*6777b538SAndroid Build Coastguard Worker
232*6777b538SAndroid Build Coastguard Worker if (vm_info.speculative_count <= vm_info.free_count) {
233*6777b538SAndroid Build Coastguard Worker meminfo->free = saturated_cast<int>(
234*6777b538SAndroid Build Coastguard Worker PAGE_SIZE / 1024 * (vm_info.free_count - vm_info.speculative_count));
235*6777b538SAndroid Build Coastguard Worker } else {
236*6777b538SAndroid Build Coastguard Worker // Inside the `host_statistics64` call above, `speculative_count` is
237*6777b538SAndroid Build Coastguard Worker // computed later than `free_count`, so these values are snapshots of two
238*6777b538SAndroid Build Coastguard Worker // (slightly) different points in time. As a result, it is possible for
239*6777b538SAndroid Build Coastguard Worker // `speculative_count` to have increased significantly since `free_count`
240*6777b538SAndroid Build Coastguard Worker // was computed, even to a point where `speculative_count` is greater than
241*6777b538SAndroid Build Coastguard Worker // the computed value of `free_count`. See
242*6777b538SAndroid Build Coastguard Worker // https://github.com/apple-oss-distributions/xnu/blob/aca3beaa3dfbd42498b42c5e5ce20a938e6554e5/osfmk/kern/host.c#L788
243*6777b538SAndroid Build Coastguard Worker // In this case, 0 is the best approximation for `meminfo->free`. This is
244*6777b538SAndroid Build Coastguard Worker // inexact, but even in the case where `speculative_count` is less than
245*6777b538SAndroid Build Coastguard Worker // `free_count`, the computed `meminfo->free` will only be an approximation
246*6777b538SAndroid Build Coastguard Worker // given that the two inputs come from different points in time.
247*6777b538SAndroid Build Coastguard Worker meminfo->free = 0;
248*6777b538SAndroid Build Coastguard Worker }
249*6777b538SAndroid Build Coastguard Worker
250*6777b538SAndroid Build Coastguard Worker meminfo->speculative =
251*6777b538SAndroid Build Coastguard Worker saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count);
252*6777b538SAndroid Build Coastguard Worker meminfo->file_backed =
253*6777b538SAndroid Build Coastguard Worker saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count);
254*6777b538SAndroid Build Coastguard Worker meminfo->purgeable =
255*6777b538SAndroid Build Coastguard Worker saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count);
256*6777b538SAndroid Build Coastguard Worker
257*6777b538SAndroid Build Coastguard Worker return true;
258*6777b538SAndroid Build Coastguard Worker }
259*6777b538SAndroid Build Coastguard Worker
260*6777b538SAndroid Build Coastguard Worker // Both |size| and |address| are in-out parameters.
261*6777b538SAndroid Build Coastguard Worker // |info| is an output parameter, only valid on Success.
GetTopInfo(mach_port_t task,mach_vm_size_t * size,mach_vm_address_t * address,vm_region_top_info_data_t * info)262*6777b538SAndroid Build Coastguard Worker MachVMRegionResult GetTopInfo(mach_port_t task,
263*6777b538SAndroid Build Coastguard Worker mach_vm_size_t* size,
264*6777b538SAndroid Build Coastguard Worker mach_vm_address_t* address,
265*6777b538SAndroid Build Coastguard Worker vm_region_top_info_data_t* info) {
266*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
267*6777b538SAndroid Build Coastguard Worker // The kernel always returns a null object for VM_REGION_TOP_INFO, but
268*6777b538SAndroid Build Coastguard Worker // balance it with a deallocate in case this ever changes. See 10.9.2
269*6777b538SAndroid Build Coastguard Worker // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region.
270*6777b538SAndroid Build Coastguard Worker apple::ScopedMachSendRight object_name;
271*6777b538SAndroid Build Coastguard Worker
272*6777b538SAndroid Build Coastguard Worker kern_return_t kr =
273*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC)
274*6777b538SAndroid Build Coastguard Worker mach_vm_region(task, address, size, VM_REGION_TOP_INFO,
275*6777b538SAndroid Build Coastguard Worker reinterpret_cast<vm_region_info_t>(info), &info_count,
276*6777b538SAndroid Build Coastguard Worker apple::ScopedMachSendRight::Receiver(object_name).get());
277*6777b538SAndroid Build Coastguard Worker #else
278*6777b538SAndroid Build Coastguard Worker vm_region_64(task, reinterpret_cast<vm_address_t*>(address),
279*6777b538SAndroid Build Coastguard Worker reinterpret_cast<vm_size_t*>(size), VM_REGION_TOP_INFO,
280*6777b538SAndroid Build Coastguard Worker reinterpret_cast<vm_region_info_t>(info), &info_count,
281*6777b538SAndroid Build Coastguard Worker apple::ScopedMachSendRight::Receiver(object_name).get());
282*6777b538SAndroid Build Coastguard Worker #endif
283*6777b538SAndroid Build Coastguard Worker return ParseOutputFromMachVMRegion(kr);
284*6777b538SAndroid Build Coastguard Worker }
285*6777b538SAndroid Build Coastguard Worker
GetBasicInfo(mach_port_t task,mach_vm_size_t * size,mach_vm_address_t * address,vm_region_basic_info_64 * info)286*6777b538SAndroid Build Coastguard Worker MachVMRegionResult GetBasicInfo(mach_port_t task,
287*6777b538SAndroid Build Coastguard Worker mach_vm_size_t* size,
288*6777b538SAndroid Build Coastguard Worker mach_vm_address_t* address,
289*6777b538SAndroid Build Coastguard Worker vm_region_basic_info_64* info) {
290*6777b538SAndroid Build Coastguard Worker mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
291*6777b538SAndroid Build Coastguard Worker // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but
292*6777b538SAndroid Build Coastguard Worker // balance it with a deallocate in case this ever changes. See 10.9.2
293*6777b538SAndroid Build Coastguard Worker // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region.
294*6777b538SAndroid Build Coastguard Worker apple::ScopedMachSendRight object_name;
295*6777b538SAndroid Build Coastguard Worker
296*6777b538SAndroid Build Coastguard Worker kern_return_t kr =
297*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC)
298*6777b538SAndroid Build Coastguard Worker mach_vm_region(task, address, size, VM_REGION_BASIC_INFO_64,
299*6777b538SAndroid Build Coastguard Worker reinterpret_cast<vm_region_info_t>(info), &info_count,
300*6777b538SAndroid Build Coastguard Worker apple::ScopedMachSendRight::Receiver(object_name).get());
301*6777b538SAndroid Build Coastguard Worker
302*6777b538SAndroid Build Coastguard Worker #else
303*6777b538SAndroid Build Coastguard Worker vm_region_64(task, reinterpret_cast<vm_address_t*>(address),
304*6777b538SAndroid Build Coastguard Worker reinterpret_cast<vm_size_t*>(size), VM_REGION_BASIC_INFO_64,
305*6777b538SAndroid Build Coastguard Worker reinterpret_cast<vm_region_info_t>(info), &info_count,
306*6777b538SAndroid Build Coastguard Worker apple::ScopedMachSendRight::Receiver(object_name).get());
307*6777b538SAndroid Build Coastguard Worker #endif
308*6777b538SAndroid Build Coastguard Worker return ParseOutputFromMachVMRegion(kr);
309*6777b538SAndroid Build Coastguard Worker }
310*6777b538SAndroid Build Coastguard Worker
GetOpenFdCount() const311*6777b538SAndroid Build Coastguard Worker int ProcessMetrics::GetOpenFdCount() const {
312*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(USE_BLINK)
313*6777b538SAndroid Build Coastguard Worker // In order to get a true count of the open number of FDs, PROC_PIDLISTFDS
314*6777b538SAndroid Build Coastguard Worker // is used. This is done twice: first to get the appropriate size of a
315*6777b538SAndroid Build Coastguard Worker // buffer, and then secondly to fill the buffer with the actual FD info.
316*6777b538SAndroid Build Coastguard Worker //
317*6777b538SAndroid Build Coastguard Worker // The buffer size returned in the first call is an estimate, based on the
318*6777b538SAndroid Build Coastguard Worker // number of allocated fileproc structures in the kernel. This number can be
319*6777b538SAndroid Build Coastguard Worker // greater than the actual number of open files, since the structures are
320*6777b538SAndroid Build Coastguard Worker // allocated in slabs. The value returned in proc_bsdinfo::pbi_nfiles is
321*6777b538SAndroid Build Coastguard Worker // also the number of allocated fileprocs, not the number in use.
322*6777b538SAndroid Build Coastguard Worker //
323*6777b538SAndroid Build Coastguard Worker // However, the buffer size returned in the second call is an accurate count
324*6777b538SAndroid Build Coastguard Worker // of the open number of descriptors. The contents of the buffer are unused.
325*6777b538SAndroid Build Coastguard Worker int rv = proc_pidinfo(process_, PROC_PIDLISTFDS, 0, nullptr, 0);
326*6777b538SAndroid Build Coastguard Worker if (rv < 0) {
327*6777b538SAndroid Build Coastguard Worker return -1;
328*6777b538SAndroid Build Coastguard Worker }
329*6777b538SAndroid Build Coastguard Worker
330*6777b538SAndroid Build Coastguard Worker std::unique_ptr<char[]> buffer(new char[static_cast<size_t>(rv)]);
331*6777b538SAndroid Build Coastguard Worker rv = proc_pidinfo(process_, PROC_PIDLISTFDS, 0, buffer.get(), rv);
332*6777b538SAndroid Build Coastguard Worker if (rv < 0) {
333*6777b538SAndroid Build Coastguard Worker return -1;
334*6777b538SAndroid Build Coastguard Worker }
335*6777b538SAndroid Build Coastguard Worker return static_cast<int>(static_cast<unsigned long>(rv) / PROC_PIDLISTFD_SIZE);
336*6777b538SAndroid Build Coastguard Worker #else
337*6777b538SAndroid Build Coastguard Worker NOTIMPLEMENTED_LOG_ONCE();
338*6777b538SAndroid Build Coastguard Worker return -1;
339*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(USE_BLINK)
340*6777b538SAndroid Build Coastguard Worker }
341*6777b538SAndroid Build Coastguard Worker
GetOpenFdSoftLimit() const342*6777b538SAndroid Build Coastguard Worker int ProcessMetrics::GetOpenFdSoftLimit() const {
343*6777b538SAndroid Build Coastguard Worker return checked_cast<int>(GetMaxFds());
344*6777b538SAndroid Build Coastguard Worker }
345*6777b538SAndroid Build Coastguard Worker
346*6777b538SAndroid Build Coastguard Worker } // namespace base
347