Define process information programmatically in Darwin / OSX

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; 

The duty of this class is to return the process information about the caller. The size of physical memory can be easily determined by calling sysctl, and pid is trivial, but the remaining calls elude me, except calling popen on ps or top and parsing the output, which is unacceptable. Any help would be greatly appreciated.

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

+15
c ++ c operating-system darwin macos
Oct 20 '08 at 23:38
source share
5 answers

Information about the process 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); 

Download cpu 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 information, check out the sources for top and lsof , they are open source (you need to register as an Apple developer, but it's free):

http://www.opensource.apple.com/darwinsource/Current/top-38/libtop.c

Further editing: All these interfaces are version-specific, so you need to consider this 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. */ 
+11
Oct 21 '08 at 6:00
source share

Since you say no to Objective-C, we will exclude most MacOS frameworks.

You can get processor time using getrusage (), which gives the total amount of User and System CPU load times for your process. To get the percentage of CPU, you will need to instantly display getrusage values ​​once per second (or, as necessary).

 #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 it always looks zero in MacOS X 10.5. Michael Knight wrote a blog post a few years ago on how to spot RSS.

+6
Oct 21 '08 at 2:11
source share

I think most of these values ​​are available in the Mach API, but some time has passed since I poked there. Alternatively, you can simply look at the source code for the ps or top commands and see how they do it.

+2
Oct 20 '08 at 23:51
source share

You can use the code below for process information on 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 the process. Such a process identifier (pid), process group, process status, etc.

+2
May 13 '13 at 12:52
source share

Most of this information can be obtained from GetProcessInformation () .

By the way, why virtual methods for functions that return volume information?

This is CARBON and will not work with cocoa

0
Oct 20 '08 at 23:55
source share



All Articles