Determine Process Info Programmatically in Darwin/OSX
Asked Answered
M

5

15

I have a class with the following member functions:


/// caller pid
virtual pid_t Pid() const = 0; 

/// physical memory size in KB
virtual uint64_t Size() const = 0;  

/// resident memory for this process
virtual uint64_t Rss() const = 0; 

/// cpu used by this process
virtual double PercentCpu() const = 0; 

/// memory used by this process
virtual double PercentMemory() const = 0; 

/// number of threads in this process
virtual int32_t Lwps() const = 0; 

This class' duty is to return process information about caller. Physical memory size can easily determined by a sysctl call, and pid is trivial, but the remaining calls have eluded me, aside from invoking a popen on ps or top and parsing the output - which isn't acceptable. Any help would be greatly appreciated.

Requirements:
Compiles on g++ 4.0
No obj-c
OSX 10.5

Marchal answered 20/10, 2008 at 23:38 Comment(1)
Please describe why you want to create a class that does this. Most software running on Mac OS X shouldn't need to care about any of the things you've described. Also, "no Objective-C" isn't a reasonable policy when developing for Mac OS X.Westberg
E
14

Process info comes from pidinfo:

cristi:~ diciu$ grep proc_pidinfo /usr/include/libproc.h

int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize);

cpu load comes from host_statistics:

cristi:~ diciu$ grep -r host_statistics /usr/include/

/usr/include/mach/host_info.h:/* host_statistics() */

/usr/include/mach/mach_host.defs:routine host_statistics(

/usr/include/mach/mach_host.h:/* Routine host_statistics */

/usr/include/mach/mach_host.h:kern_return_t host_statistics

For more details, check out sources for top and lsof, they are open source (you need to register as an Apple developer but that's free of charge):

https://opensource.apple.com/source/top/top-111.20.1/libtop.c.auto.html

Later edit: All these interfaces are version specific, so you need to take that into account when writing production code (libproc.h):

/*
 * This header file contains private interfaces to obtain process information.
 * These interfaces are subject to change in future releases.
 */
Ennead answered 21/10, 2008 at 6:0 Comment(0)
S
7

Since you say no Objective-C we'll rule out most of the MacOS frameworks.

You can get CPU time using getrusage(), which gives the total amount of User and System CPU time charged to your process. To get a CPU percentage you'd need to snapshot the getrusage values once per second (or however granular you want to be).

#include <sys/resource.h>

struct rusage r_usage;

if (getrusage(RUSAGE_SELF, &r_usage)) {
    /* ... error handling ... */
}

printf("Total User CPU = %ld.%ld\n",
        r_usage.ru_utime.tv_sec,
        r_usage.ru_utime.tv_usec);
printf("Total System CPU = %ld.%ld\n",
        r_usage.ru_stime.tv_sec,
        r_usage.ru_stime.tv_usec);

There is an RSS field in the getrusage structure, but is appears to always be zero in MacOS X 10.5. Michael Knight wrote a blog post several years ago about how to determine the RSS.

Saleable answered 21/10, 2008 at 2:11 Comment(1)
can you also explain how to get memory usage of running process in cocoa?Leg
A
2

You can use below code for process info in mac OS:

void IsInBSDProcessList(char *name)    { 
  assert( name != NULL); 
  kinfo_proc *result; 
  size_t count = 0; 
  result = (kinfo_proc *)malloc(sizeof(kinfo_proc)); 
  if(GetBSDProcessList(&result,&count) == 0) { 
    for (int i = 0; i < count; i++) { 
      kinfo_proc *proc = NULL; 
      proc = &result[i]; 
      }
  } 
  free(result);
}

kinfo_proc struct contains all the information about a process.such as Process identifier (pid), process group, process status and etc.

Airtoair answered 13/5, 2013 at 12:52 Comment(0)
D
1

I think most of these values are available in the Mach API, but it's been a while since I've poked around in there. Alternatively, you could just look at the source code for the "ps" or "top" commands, and see how they do it.

Desmoid answered 20/10, 2008 at 23:51 Comment(0)
R
0

Most of this info can be gotten from GetProcessInformation().

By the way, why virtual methods for functions that return processwide info?

This is CARBON only, and won't work with cocoa

Rogue answered 20/10, 2008 at 23:55 Comment(3)
Note that GetProcessInformation() is a Win32 API function, not Mac OS X as the OP's talking about.Ascocarp
Actually @benhoyt, his link points at Apple documentation that includes a function called GetProcessInformation(), but it is however part of a framework.Almallah
GetProcessInformation() is a Mac call, but it doesn't measure CPU usage. Also, it deprecated. It is part of the old Process Manager from the Mac Toolbox days (prior to MacOS X). I would not recommend using it at all on MacOS X. It returns information that isn't even relevant such as information about the process's memory. This was a concept from MacOS 9 and earlier where it didn't have modern virtual memory. Apps just directly accessed one single memory partition of the computer (and could modify each other's memory regardless of privileges). Don't use it.Yeasty

© 2022 - 2024 — McMap. All rights reserved.