Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample a process on Mac OS X from a C/C++ program

The Sample Process feature in Activity Monitor is quite a useful thing. However, I need to do the same thing (take samples) of a certain process from another running process (C/C++) or a command line.

Is there any way to do this? I have been googling for this since a few days without any luck.

like image 761
Chirag Avatar asked Dec 31 '09 20:12

Chirag


2 Answers

There is a command-line utility sample.

Example:

sample Safari -file /dev/stdout

It will get exactly the same output with Activity Monitor.

like image 90
iamamac Avatar answered Nov 06 '22 17:11

iamamac


There are some few commandsline application that come in handy: sample and top.

If you want to write your own program, you can use the sysctl system call to get such information. However, it's quite tedious.

I would recommend installing procfs file system (built with MacFUSE). This would create a new "directory" at /proc that contains a lot of useful information for each application (e.g. memory usage, cpu usage, locks, opened files, sockets, threads, etc). The site gives a sample of how it can be accessed. Then you can simply script your access to those files.

like image 25
notnoop Avatar answered Nov 06 '22 15:11

notnoop