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
Process info comes from
pidinfo:cpu load comes from
host_statistics:For more details, check out sources for
topandlsof, 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):