| 6 |
| 7 | #include <libproc.h> |
| 8 | #include <mach/mach.h> |
| 9 | #include <mach/mach_time.h> |
| 10 | #include <mach/mach_vm.h> |
| 11 | #include <mach/shared_region.h> |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | #include <sys/sysctl.h> |
| 15 |
| 16 | #include "base/logging.h" |
| 17 | #include "base/mac/mac_util.h" |
| 18 | #include "base/mac/mach_logging.h" |
| 19 | #include "base/mac/scoped_mach_port.h" |
| 20 | #include "base/memory/ptr_util.h" |
| 21 | #include "base/numerics/safe_conversions.h" |
| 22 | #include "base/numerics/safe_math.h" |
| 23 | #include "base/process/process_metrics_iocounters.h" |
| 24 | #include "base/time/time.h" |
| 25 |
| 26 | namespace { |
| 27 |
| 28 | // This is a standin for the private pm_task_energy_data_t struct. |
| 29 | struct OpaquePMTaskEnergyData { |
| 30 | // Empirical size of the private struct. |
| 31 | uint8_t data[384]; |
| 32 | }; |
| 33 |
| 34 | // Sample everything but network usage, since fetching network |
| 35 | // usage can hang. |
| 36 | static constexpr uint8_t kPMSampleFlags = 0xff & ~0x8; |
| 37 |
| 38 | } // namespace |
| 39 |
| 40 | extern "C" { |
| 41 |
| 42 | // From libpmsample.dylib |
| 43 | int pm_sample_task(mach_port_t task, |
| 44 | OpaquePMTaskEnergyData* pm_energy, |
| 45 | uint64_t mach_time, |
| 46 | uint8_t flags); |
| 47 |
| 48 | // From libpmenergy.dylib |
| 49 | double pm_energy_impact(OpaquePMTaskEnergyData* pm_energy); |
| 50 |
| 51 | } // extern "C" |
| 52 |
| 53 | namespace base { |
| 54 |
| 55 | namespace { |
| 56 |
| 57 | bool GetTaskInfo(mach_port_t task, task_basic_info_64* task_info_data) { |
| 58 | if (task == MACH_PORT_NULL) |
| 59 | return false; |
| 60 | mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT; |
| 61 | kern_return_t kr = task_info(task, |
| 62 | TASK_BASIC_INFO_64, |
| 63 | reinterpret_cast<task_info_t>(task_info_data), |
| 64 | &count); |
| 65 | // Most likely cause for failure: |task| is a zombie. |
| 66 | return kr == KERN_SUCCESS; |
| 67 | } |
| 68 |
| 69 | MachVMRegionResult ParseOutputFromMachVMRegion(kern_return_t kr) { |
| 70 | if (kr == KERN_INVALID_ADDRESS) { |
| 71 | // We're at the end of the address space. |
| 72 | return MachVMRegionResult::Finished; |
| 73 | } else if (kr != KERN_SUCCESS) { |
| 74 | return MachVMRegionResult::Error; |
| 75 | } |
| 76 | return MachVMRegionResult::Success; |
| 77 | } |
| 78 |
| 79 | bool GetPowerInfo(mach_port_t task, task_power_info* power_info_data) { |
| 80 | if (task == MACH_PORT_NULL) |
| 81 | return false; |
| 82 |
| 83 | mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT; |
| 84 | kern_return_t kr = task_info(task, TASK_POWER_INFO, |
| 85 | reinterpret_cast<task_info_t>(power_info_data), |
| 86 | &power_info_count); |
| 87 | // Most likely cause for failure: |task| is a zombie. |
| 88 | return kr == KERN_SUCCESS; |
| 89 | } |
| 90 |
| 91 | double GetEnergyImpactInternal(mach_port_t task, uint64_t mach_time) { |
| 92 | OpaquePMTaskEnergyData energy_info{}; |
| 93 |
| 94 | if (pm_sample_task(task, &energy_info, mach_time, kPMSampleFlags) != 0) |
| 95 | return 0.0; |
| 96 | return pm_energy_impact(&energy_info); |
| 97 | } |
| 98 |
| 99 | } // namespace |
| 100 |
| 101 | // Getting a mach task from a pid for another process requires permissions in |
| 102 | // general, so there doesn't really seem to be a way to do these (and spinning |
| 103 | // up ps to fetch each stats seems dangerous to put in a base api for anyone to |
| 104 | // call). Child processes ipc their port, so return something if available, |
| 105 | // otherwise return 0. |
| 106 |
| 107 | // static |
| 108 | std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 109 | ProcessHandle process, |
| 110 | PortProvider* port_provider) { |
| 111 | return WrapUnique(new ProcessMetrics(process, port_provider)); |
| 112 | } |
| 113 |
| 114 | #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ |
| 115 | (r)->tv_sec = (a)->seconds; \ |
| 116 | (r)->tv_usec = (a)->microseconds; \ |
| 117 | } while (0) |
| 118 |
| 119 | TimeDelta ProcessMetrics::GetCumulativeCPUUsage() { |
| 120 | mach_port_t task = TaskForPid(process_); |
| 121 | if (task == MACH_PORT_NULL) |
| 122 | return TimeDelta(); |
| 123 |
| 124 | // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage() |
| 125 | // in libtop.c), but this is more concise and gives the same results: |
| 126 | task_thread_times_info thread_info_data; |
| 127 | mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; |
| 128 | kern_return_t kr = task_info(task, |
| 129 | TASK_THREAD_TIMES_INFO, |
| 130 | reinterpret_cast<task_info_t>(&thread_info_data), |
| 131 | &thread_info_count); |
| 132 | if (kr != KERN_SUCCESS) { |
| 133 | // Most likely cause: |task| is a zombie. |
| 134 | return TimeDelta(); |
| 135 | } |
| 136 |
| 137 | task_basic_info_64 task_info_data; |
| 138 | if (!GetTaskInfo(task, &task_info_data)) |
| 139 | return TimeDelta(); |
| 140 |
| 141 | /* Set total_time. */ |
| 142 | // thread info contains live time... |
| 143 | struct timeval user_timeval, system_timeval, task_timeval; |
| 144 | TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval); |
| 145 | TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval); |
| 146 | timeradd(&user_timeval, &system_timeval, &task_timeval); |
| 147 |
| 148 | // ... task info contains terminated time. |
| 149 | TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval); |
| 150 | TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval); |
| 151 | timeradd(&user_timeval, &task_timeval, &task_timeval); |
| 152 | timeradd(&system_timeval, &task_timeval, &task_timeval); |
| 153 |
| 154 | return TimeDelta::FromMicroseconds(TimeValToMicroseconds(task_timeval)); |
| 155 | } |
| 156 |
| 157 | int ProcessMetrics::GetPackageIdleWakeupsPerSecond() { |
| 158 | mach_port_t task = TaskForPid(process_); |
| 159 | task_power_info power_info_data; |
| 160 |
| 161 | GetPowerInfo(task, &power_info_data); |
| 162 |
| 163 | // The task_power_info struct contains two wakeup counters: |
| 164 | // task_interrupt_wakeups and task_platform_idle_wakeups. |
| 165 | // task_interrupt_wakeups is the total number of wakeups generated by the |
| 166 | // process, and is the number that Activity Monitor reports. |
| 167 | // task_platform_idle_wakeups is a subset of task_interrupt_wakeups that |
| 168 | // tallies the number of times the processor was taken out of its low-power |
| 169 | // idle state to handle a wakeup. task_platform_idle_wakeups therefore result |
| 170 | // in a greater power increase than the other interrupts which occur while the |
| 171 | // CPU is already working, and reducing them has a greater overall impact on |
| 172 | // power usage. See the powermetrics man page for more info. |
| 173 | return CalculatePackageIdleWakeupsPerSecond( |
| 174 | power_info_data.task_platform_idle_wakeups); |
| 175 | } |
| 176 |
| 177 | int ProcessMetrics::GetIdleWakeupsPerSecond() { |
| 178 | mach_port_t task = TaskForPid(process_); |
| 179 | task_power_info power_info_data; |
| 180 |
| 181 | GetPowerInfo(task, &power_info_data); |
| 182 |
| 183 | return CalculateIdleWakeupsPerSecond(power_info_data.task_interrupt_wakeups); |
| 184 | } |
| 185 |
| 186 | int ProcessMetrics::GetEnergyImpact() { |
| 187 | uint64_t now = mach_absolute_time(); |
| 188 | if (last_energy_impact_ == 0) { |
| 189 | last_energy_impact_ = GetEnergyImpactInternal(TaskForPid(process_), now); |
| 190 | last_energy_impact_time_ = now; |
| 191 | return 0; |
| 192 | } |
| 193 |
| 194 | double total_energy_impact = |
| 195 | GetEnergyImpactInternal(TaskForPid(process_), now); |
| 196 | uint64_t delta = now - last_energy_impact_time_; |
| 197 | if (delta == 0) |
| 198 | return 0; |
| 199 |
| 200 | // Scale by 100 since the histogram is integral. |
| 201 | double seconds_since_last_measurement = |
| 202 | base::TimeTicks::FromMachAbsoluteTime(delta).since_origin().InSecondsF(); |
| 203 | int energy_impact = 100 * (total_energy_impact - last_energy_impact_) / |
| 204 | seconds_since_last_measurement; |
| 205 | last_energy_impact_ = total_energy_impact; |
| 206 | last_energy_impact_time_ = now; |
| 207 |
| 208 | return energy_impact; |
| 209 | } |
| 210 |
| 211 | int ProcessMetrics::GetOpenFdCount() const { |
| 212 | // In order to get a true count of the open number of FDs, PROC_PIDLISTFDS |
| 213 | // is used. This is done twice: first to get the appropriate size of a |
| 214 | // buffer, and then secondly to fill the buffer with the actual FD info. |
| 215 | // |
| 216 | // The buffer size returned in the first call is an estimate, based on the |
| 217 | // number of allocated fileproc structures in the kernel. This number can be |
| 218 | // greater than the actual number of open files, since the structures are |
| 219 | // allocated in slabs. The value returned in proc_bsdinfo::pbi_nfiles is |
| 220 | // also the number of allocated fileprocs, not the number in use. |
| 221 | // |
| 222 | // However, the buffer size returned in the second call is an accurate count |
| 223 | // of the open number of descriptors. The contents of the buffer are unused. |
| 224 | int rv = proc_pidinfo(process_, PROC_PIDLISTFDS, 0, nullptr, 0); |
| 225 | if (rv < 0) |
| 226 | return -1; |
| 227 |
| 228 | std::unique_ptr<char[]> buffer(new char[rv]); |
| 229 | rv = proc_pidinfo(process_, PROC_PIDLISTFDS, 0, buffer.get(), rv); |
| 230 | if (rv < 0) |
| 231 | return -1; |
| 232 | return rv / PROC_PIDLISTFD_SIZE; |
| 233 | } |
| 234 |
| 235 | int ProcessMetrics::GetOpenFdSoftLimit() const { |
| 236 | return GetMaxFds(); |
| 237 | } |
| 238 |
| 239 | bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| 240 | return false; |
| 241 | } |
| 242 |
| 243 | ProcessMetrics::ProcessMetrics(ProcessHandle process, |
| 244 | PortProvider* port_provider) |
| 245 | : process_(process), |
| 246 | last_absolute_idle_wakeups_(0), |
| 247 | last_absolute_package_idle_wakeups_(0), |
| 248 | last_energy_impact_(0), |
| 249 | port_provider_(port_provider) {} |
| 250 |
| 251 | mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { |
| 252 | mach_port_t task = MACH_PORT_NULL; |
| 253 | if (port_provider_) |
| 254 | task = port_provider_->TaskForPid(process_); |
| 255 | if (task == MACH_PORT_NULL && process_ == getpid()) |
| 256 | task = mach_task_self(); |
| 257 | return task; |
| 258 | } |
| 259 |
| 260 | // Bytes committed by the system. |
| 261 | size_t GetSystemCommitCharge() { |
| 262 | base::mac::ScopedMachSendRight host(mach_host_self()); |
| 263 | mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
| 264 | vm_statistics_data_t data; |
| 265 | kern_return_t kr = host_statistics(host.get(), HOST_VM_INFO, |
| 266 | reinterpret_cast<host_info_t>(&data), |
| 267 | &count); |
| 268 | if (kr != KERN_SUCCESS) { |
| 269 | MACH_DLOG(WARNING, kr) << "host_statistics"; |
| 270 | return 0; |
| 271 | } |
| 272 |
| 273 | return (data.active_count * PAGE_SIZE) / 1024; |
| 274 | } |
| 275 |
| 276 | bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { |
| 277 | struct host_basic_info hostinfo; |
| 278 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 279 | base::mac::ScopedMachSendRight host(mach_host_self()); |
| 280 | int result = host_info(host.get(), HOST_BASIC_INFO, |
| 281 | reinterpret_cast<host_info_t>(&hostinfo), &count); |
| 282 | if (result != KERN_SUCCESS) |
| 283 | return false; |
| 284 |
| 285 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 286 | meminfo->total = static_cast<int>(hostinfo.max_mem / 1024); |
| 287 |
| 288 | vm_statistics64_data_t vm_info; |
| 289 | count = HOST_VM_INFO64_COUNT; |
| 290 |
| 291 | if (host_statistics64(host.get(), HOST_VM_INFO64, |
| 292 | reinterpret_cast<host_info64_t>(&vm_info), |
| 293 | &count) != KERN_SUCCESS) { |
| 294 | return false; |
| 295 | } |
| 296 | DCHECK_EQ(HOST_VM_INFO64_COUNT, count); |
| 297 |
| 298 | static_assert(PAGE_SIZE % 1024 == 0, "Invalid page size"); |
| 299 | meminfo->free = saturated_cast<int>( |
| 300 | PAGE_SIZE / 1024 * (vm_info.free_count - vm_info.speculative_count)); |
| 301 | meminfo->speculative = |
| 302 | saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count); |
| 303 | meminfo->file_backed = |
| 304 | saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count); |
| 305 | meminfo->purgeable = |
| 306 | saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count); |
| 307 |
| 308 | return true; |
| 309 | } |
| 310 |
| 311 | // Both |size| and |address| are in-out parameters. |
| 312 | // |info| is an output parameter, only valid on Success. |
| 313 | MachVMRegionResult GetTopInfo(mach_port_t task, |
| 314 | mach_vm_size_t* size, |
| 315 | mach_vm_address_t* address, |
| 316 | vm_region_top_info_data_t* info) { |
| 317 | mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; |
| 318 | mach_port_t object_name; |
| 319 | kern_return_t kr = mach_vm_region(task, address, size, VM_REGION_TOP_INFO, |
| 320 | reinterpret_cast<vm_region_info_t>(info), |
| 321 | &info_count, &object_name); |
| 322 | // The kernel always returns a null object for VM_REGION_TOP_INFO, but |
| 323 | // balance it with a deallocate in case this ever changes. See 10.9.2 |
| 324 | // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. |
| 325 | mach_port_deallocate(task, object_name); |
| 326 | return ParseOutputFromMachVMRegion(kr); |
| 327 | } |
| 328 |
| 329 | MachVMRegionResult GetBasicInfo(mach_port_t task, |
| 330 | mach_vm_size_t* size, |
| 331 | mach_vm_address_t* address, |
| 332 | vm_region_basic_info_64* info) { |
| 333 | mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64; |
| 334 | mach_port_t object_name; |
| 335 | kern_return_t kr = mach_vm_region( |
| 336 | task, address, size, VM_REGION_BASIC_INFO_64, |
| 337 | reinterpret_cast<vm_region_info_t>(info), &info_count, &object_name); |
| 338 | // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but |
| 339 | // balance it with a deallocate in case this ever changes. See 10.9.2 |
| 340 | // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. |
| 341 | mach_port_deallocate(task, object_name); |
| 342 | return ParseOutputFromMachVMRegion(kr); |
| 343 | } |
| 344 |
| 345 | } // namespace base |
| 346 |