views:

237

answers:

2

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.

+2  A: 

There is a command-line utility sample.

Example:

sample Safari -file /dev/stdout

It will get exactly the same output with Activity Monitor.

Iamamac
Thanks lamamac, exactly what I was looking for.
Chirag
+1  A: 

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.

notnoop
Thanks for your reply. I will take a look at the other options that you gave.
Chirag