CPU Usage for specific PID

Hi,

I 'm working on a Mac App which I wanna distribute over the Mac App Store.

I'm using NSWorkspace to get all running applications.

What I want is to get the CPU usage % for each process identifier of all running applications.

Now I'm doing it with NSTask and NSPipe using the top command. This works perfect when App Sandboxing is disabled.

When App Sandboxing is enabled I get the following error: Couldn't posix_spawn: error 1

Is there anything I can do to make this work with App Sandboxing on? Is there another way to get CPU usage for a specific PID, without using NSTask and top or ps ?


Thanks for your help.


Steve

Information like this is typically available via one of the following APIs:

  • low-level Mach APIs — For example, the APIs in

    <mach/task_info.h>
    let you get at a lot of different process-level statistics (a Mach task is pretty much synonymous with a BSD process).
  • libproc — This includes

    <libproc.h>
    and
    <sys/proc_info.h>
    .

Digesting this information into a coherent whole is tricky. If you want your results to match

top
, you may want to look at the
top
source code.

WARNING System-level information like this is tightly bound to the system implementation. It’s very easy for that code to break as the system evolves to undermine your assumptions. For example, if you had written code like this prior to the introduction of multiple cores, that would be broken by that feature. I generally recommend that folks try to avoid going down the implementing system-level functionality.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Eskimo,


I just tried liproc.h to get the app name from proc_pidpath and I got back the correct name even when sandbox is enabled.

Now I will try to get the cpu usage from proc_info.h.


Here my quick dirty code to get proc_pidpath (from liproc.h) and from it the name of the app.

- (NSString*) getPIDName:(pid_t) pidvalue
{
  
    pid_t pid; int ret;
    char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
    NSString *name=@"";
    pid = pidvalue;
    ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
    if ( ret <= 0 ) {
        fprintf(stderr, "    %s\n", strerror(errno));
    } else {
        printf("proc %d: %s\n", pid, pathbuf);
        name= [[NSString stringWithCString:pathbuf encoding:NSASCIIStringEncoding]lastPathComponent];
    }
  
    return name;
}


You just made my day, thanx


Cheers Steve

Hi,


unfortunately it's not allowed to use task_for_pid() on something other than itself when sandbox is enabled, so I can't get the task port, and without it I can't get the thread list for each task and hence no thread info. If anyone can tell me how I can get the thread info and cpu usage for each PID I would be very grateful.


Cheers Steve

ps -emo %cpu,pid,user,args


...works/dumps the list, from the cmd line - can you leverage it?

CPU Usage for specific PID
 
 
Q