Skip to content

Commit

Permalink
Bug 1883574 - Switch ProcInfo to use TASK_POWER_INFO instead of proc_…
Browse files Browse the repository at this point in the history
…pidinfo. r=florian

and build it on iOS.

Differential Revision: https://phabricator.services.mozilla.com/D203565
  • Loading branch information
glandium committed Mar 11, 2024
1 parent 6cf1b1a commit 4ef3e72
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
12 changes: 6 additions & 6 deletions toolkit/components/processtools/ProcInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,21 @@ struct ProcInfoRequest {
ProcInfoRequest(base::ProcessId aPid, ProcType aProcessType,
const nsACString& aOrigin, nsTArray<WindowInfo>&& aWindowInfo,
nsTArray<UtilityInfo>&& aUtilityInfo, uint32_t aChildId = 0
#ifdef XP_MACOSX
#ifdef XP_DARWIN
,
mach_port_t aChildTask = 0
#endif // XP_MACOSX
#endif // XP_DARWIN
)
: pid(aPid),
processType(aProcessType),
origin(aOrigin),
windowInfo(std::move(aWindowInfo)),
utilityInfo(std::move(aUtilityInfo)),
childId(aChildId)
#ifdef XP_MACOSX
#ifdef XP_DARWIN
,
childTask(aChildTask)
#endif // XP_MACOSX
#endif // XP_DARWIN
{
}
const base::ProcessId pid;
Expand All @@ -195,9 +195,9 @@ struct ProcInfoRequest {
const nsTArray<UtilityInfo> utilityInfo;
// If the process is a child, its child id, otherwise `0`.
const int32_t childId;
#ifdef XP_MACOSX
#ifdef XP_DARWIN
const mach_port_t childTask;
#endif // XP_MACOSX
#endif // XP_DARWIN
};

/**
Expand Down
45 changes: 23 additions & 22 deletions toolkit/components/processtools/ProcInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <cstring>
#include <unistd.h>

#include <libproc.h>
#include <sys/sysctl.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
Expand All @@ -30,18 +29,19 @@ static void GetTimeBase(mach_timebase_info_data_t* timebase) {
namespace mozilla {

nsresult GetCpuTimeSinceProcessStartInMs(uint64_t* aResult) {
struct proc_taskinfo pti;
if ((unsigned long)proc_pidinfo(getpid(), PROC_PIDTASKINFO, 0, &pti,
PROC_PIDTASKINFO_SIZE) <
PROC_PIDTASKINFO_SIZE) {
task_power_info_data_t task_power_info;
mach_msg_type_number_t count = TASK_POWER_INFO_COUNT;
kern_return_t kr = task_info(mach_task_self(), TASK_POWER_INFO,
(task_info_t)&task_power_info, &count);
if (kr != KERN_SUCCESS) {
return NS_ERROR_FAILURE;
}

mach_timebase_info_data_t timebase;
GetTimeBase(&timebase);

*aResult = (pti.pti_total_user + pti.pti_total_system) * timebase.numer /
timebase.denom / PR_NSEC_PER_MSEC;
*aResult = (task_power_info.total_user + task_power_info.total_system) *
timebase.numer / timebase.denom / PR_NSEC_PER_MSEC;
return NS_OK;
}

Expand Down Expand Up @@ -82,18 +82,6 @@ nsresult GetGpuTimeSinceProcessStartInMs(uint64_t* aResult) {
info.windows = std::move(request.windowInfo);
info.utilityActors = std::move(request.utilityInfo);

struct proc_taskinfo pti;
if ((unsigned long)proc_pidinfo(request.pid, PROC_PIDTASKINFO, 0, &pti,
PROC_PIDTASKINFO_SIZE) <
PROC_PIDTASKINFO_SIZE) {
// Can't read data for this process.
// Probably either a sandboxing issue or a race condition, e.g.
// the process has been just been killed. Regardless, skip process.
continue;
}
info.cpuTime = (pti.pti_total_user + pti.pti_total_system) *
timebase.numer / timebase.denom;

mach_port_t selectedTask;
// If we did not get a task from a child process, we use mach_task_self()
if (request.childTask == MACH_PORT_NULL) {
Expand All @@ -102,12 +90,25 @@ nsresult GetGpuTimeSinceProcessStartInMs(uint64_t* aResult) {
selectedTask = request.childTask;
}

task_power_info_data_t task_power_info;
mach_msg_type_number_t count = TASK_POWER_INFO_COUNT;
kern_return_t kr = task_info(selectedTask, TASK_POWER_INFO,
(task_info_t)&task_power_info, &count);
if (kr != KERN_SUCCESS) {
// Can't read data for this process.
// Probably either a sandboxing issue or a race condition, e.g.
// the process has been just been killed. Regardless, skip process.
continue;
}
info.cpuTime = (task_power_info.total_user + task_power_info.total_system) *
timebase.numer / timebase.denom;

// The phys_footprint value (introduced in 10.11) of the TASK_VM_INFO data
// matches the value in the 'Memory' column of the Activity Monitor.
task_vm_info_data_t task_vm_info;
mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
kern_return_t kr = task_info(selectedTask, TASK_VM_INFO,
(task_info_t)&task_vm_info, &count);
count = TASK_VM_INFO_COUNT;
kr = task_info(selectedTask, TASK_VM_INFO, (task_info_t)&task_vm_info,
&count);
info.memory = kr == KERN_SUCCESS ? task_vm_info.phys_footprint : 0;

// Now getting threads info
Expand Down
2 changes: 1 addition & 1 deletion toolkit/components/processtools/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if toolkit == "gtk" or toolkit == "android":
UNIFIED_SOURCES += ["ProcInfo_linux.cpp"]
elif toolkit == "windows":
UNIFIED_SOURCES += ["ProcInfo_win.cpp"]
elif toolkit == "cocoa":
elif toolkit in ("cocoa", "uikit"):
UNIFIED_SOURCES += ["ProcInfo.mm"]

include("/ipc/chromium/chromium-config.mozbuild")

0 comments on commit 4ef3e72

Please sign in to comment.