1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/ProcInfo.h"
8 #include "mozilla/ScopeExit.h"
9 #include "mozilla/ipc/GeckoChildProcessHost.h"
11 #include "nsMemoryReporterManager.h"
17 #include <sys/sysctl.h>
18 #include <mach/mach.h>
19 #include <mach/mach_time.h>
21 static void GetTimeBase(mach_timebase_info_data_t* timebase) {
22 // Expected results are 125/3 on aarch64, and 1/1 on Intel CPUs.
23 if (mach_timebase_info(timebase) != KERN_SUCCESS) {
31 nsresult GetCpuTimeSinceProcessStartInMs(uint64_t* aResult) {
32 task_power_info_data_t task_power_info;
33 mach_msg_type_number_t count = TASK_POWER_INFO_COUNT;
34 kern_return_t kr = task_info(mach_task_self(), TASK_POWER_INFO,
35 (task_info_t)&task_power_info, &count);
36 if (kr != KERN_SUCCESS) {
37 return NS_ERROR_FAILURE;
40 mach_timebase_info_data_t timebase;
41 GetTimeBase(&timebase);
43 *aResult = (task_power_info.total_user + task_power_info.total_system) *
44 timebase.numer / timebase.denom / PR_NSEC_PER_MSEC;
48 nsresult GetGpuTimeSinceProcessStartInMs(uint64_t* aResult) {
49 task_power_info_v2_data_t task_power_info;
50 mach_msg_type_number_t count = TASK_POWER_INFO_V2_COUNT;
51 kern_return_t kr = task_info(mach_task_self(), TASK_POWER_INFO_V2,
52 (task_info_t)&task_power_info, &count);
53 if (kr != KERN_SUCCESS) {
54 return NS_ERROR_FAILURE;
57 *aResult = task_power_info.gpu_energy.task_gpu_utilisation / PR_NSEC_PER_MSEC;
61 int GetCycleTimeFrequencyMHz() { return 0; }
63 ProcInfoPromise::ResolveOrRejectValue GetProcInfoSync(
64 nsTArray<ProcInfoRequest>&& aRequests) {
65 ProcInfoPromise::ResolveOrRejectValue result;
67 HashMap<base::ProcessId, ProcInfo> gathered;
68 if (!gathered.reserve(aRequests.Length())) {
69 result.SetReject(NS_ERROR_OUT_OF_MEMORY);
73 mach_timebase_info_data_t timebase;
74 GetTimeBase(&timebase);
76 for (const auto& request : aRequests) {
78 info.pid = request.pid;
79 info.childId = request.childId;
80 info.type = request.processType;
81 info.origin = std::move(request.origin);
82 info.windows = std::move(request.windowInfo);
83 info.utilityActors = std::move(request.utilityInfo);
85 mach_port_t selectedTask;
86 // If we did not get a task from a child process, we use mach_task_self()
87 if (request.childTask == MACH_PORT_NULL) {
88 selectedTask = mach_task_self();
90 selectedTask = request.childTask;
93 task_power_info_data_t task_power_info;
94 mach_msg_type_number_t count = TASK_POWER_INFO_COUNT;
95 kern_return_t kr = task_info(selectedTask, TASK_POWER_INFO,
96 (task_info_t)&task_power_info, &count);
97 if (kr != KERN_SUCCESS) {
98 // Can't read data for this process.
99 // Probably either a sandboxing issue or a race condition, e.g.
100 // the process has been just been killed. Regardless, skip process.
103 info.cpuTime = (task_power_info.total_user + task_power_info.total_system) *
104 timebase.numer / timebase.denom;
106 // The phys_footprint value (introduced in 10.11) of the TASK_VM_INFO data
107 // matches the value in the 'Memory' column of the Activity Monitor.
108 task_vm_info_data_t task_vm_info;
109 count = TASK_VM_INFO_COUNT;
110 kr = task_info(selectedTask, TASK_VM_INFO, (task_info_t)&task_vm_info,
112 info.memory = kr == KERN_SUCCESS ? task_vm_info.phys_footprint : 0;
114 // Now getting threads info
116 // task_threads() gives us a snapshot of the process threads
117 // but those threads can go away. All the code below makes
118 // the assumption that thread_info() calls may fail, and
119 // these errors will be ignored.
120 thread_act_port_array_t threadList;
121 mach_msg_type_number_t threadCount;
122 kern_return_t kret = task_threads(selectedTask, &threadList, &threadCount);
123 if (kret != KERN_SUCCESS) {
124 // For some reason, we have no data on the threads for this process.
125 // Most likely reason is that we have just lost a race condition and
126 // the process is dead.
127 // Let's stop here and ignore the entire process.
131 // Deallocate the thread list.
132 // Note that this deallocation is entirely undocumented, so the following
133 // code is based on guesswork and random examples found on the web.
134 auto guardThreadCount = MakeScopeExit([&] {
135 if (threadList == nullptr) {
138 // Free each thread to avoid leaks.
139 for (mach_msg_type_number_t i = 0; i < threadCount; i++) {
140 mach_port_deallocate(mach_task_self(), threadList[i]);
142 vm_deallocate(mach_task_self(), /* address */ (vm_address_t)threadList,
143 /* size */ sizeof(thread_t) * threadCount);
146 for (mach_msg_type_number_t i = 0; i < threadCount; i++) {
147 // Basic thread info.
148 thread_extended_info_data_t threadInfoData;
149 count = THREAD_EXTENDED_INFO_COUNT;
150 kret = thread_info(threadList[i], THREAD_EXTENDED_INFO,
151 (thread_info_t)&threadInfoData, &count);
152 if (kret != KERN_SUCCESS) {
156 // Getting the thread id.
157 thread_identifier_info identifierInfo;
158 count = THREAD_IDENTIFIER_INFO_COUNT;
159 kret = thread_info(threadList[i], THREAD_IDENTIFIER_INFO,
160 (thread_info_t)&identifierInfo, &count);
161 if (kret != KERN_SUCCESS) {
165 // The two system calls were successful, let's add that thread
166 ThreadInfo* thread = info.threads.AppendElement(fallible);
168 result.SetReject(NS_ERROR_OUT_OF_MEMORY);
172 threadInfoData.pth_user_time + threadInfoData.pth_system_time;
173 thread->name.AssignASCII(threadInfoData.pth_name);
174 thread->tid = identifierInfo.thread_id;
177 if (!gathered.put(request.pid, std::move(info))) {
178 result.SetReject(NS_ERROR_OUT_OF_MEMORY);
183 result.SetResolve(std::move(gathered));
187 } // namespace mozilla