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

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.

There is a command-line utility sample.
Example:
sample Safari -file /dev/stdout
It will get exactly the same output with Activity Monitor.

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.

Related

How to monitor memory usage of all processes in Linux?

I'm developing a program running on embedded Linux (Debian Buster), and I found the program sometimes has performance issues. After some debugging process, I doubt the issue might not be in my program. Instead, somehow the OS start doing memory swap and my program was swapped to the file system.
Therefore, I use the code here to verify. And it turns out my program occupied much less physical memory after about 500 seconds, and it matches the hypothesis.
Now I want to find which process suddenly takes lots of memory at that point, but I don't know how.
Is there anyway to keep monitoring memory usage of all processes (or the top 10) of the system and dump to a log file? Any tools or commands would be good.
Thanks.
I'm developing a program running on embedded Linux
It will be helpful, if you could specify which embedded Linux you are working on.
Based on that, there are tools that someone could suggest.
For Linux, I would say, you could use:
top -p [PID]
you can get PID by:
ps [options]
I am not sure if there is a problem while using the command line?
dump to a log file
I think you could use grep to dump the terminal output to a log file you can create using touch command.

Read Performance counter in Mac from terminal

I would like to know how to get
performance monitoring counter measurements on a Mac. With Linux, we
can use the "perf stat" command to get measurements. With a Mac it's
harder. Specifically, I would like to know how I can get the number of
branch mispredictions and the number of branches executed for a
program running from the command line, preferably without recompiling
the program. I have done some Googling but what I need is a solution;
if you can show me how you brought up a terminal, typed in some
command (maybe using some tool you downloaded), and got the number of
mispredictions for, say /bin/ls, that would be great.
Official macOS tool to work with profiling, both based on software events and on hardware events (performance counters), is Instruments tool - https://help.apple.com/instruments/mac/current/. It seems to be part of XCode development tools.
This tool have some limited variants to collect profiles from command-line mode, check https://help.apple.com/instruments/mac/current/#/devb14ffaa5
https://help.apple.com/instruments/mac/current/#/devba105ecc
Launch Instruments from the command line
You can use the open command to launch any app in OS X via Terminal,
including Instruments.
Launch Terminal (in /Applications/Utilities/).
Run the following command:
open /Applications/Xcode.app/Contents/Applications/Instruments.app
The Instruments app launches.
Note: You can also use either of two command-line utilities to profile
an app without actually displaying the Instruments user interface.
instruments — This utility profiles an app using a specified template. The results can be saved to a file and then manually opened
in the main Instruments app for viewing and analysis. To learn more,
go to Profile with the instruments command-line tool.
To get IPC or branches, use GUI to create profile with usage of counters usage (1 or 2) and save it as template, and then use CLI tool instruments to collect trace data. Trace file can be viewed with Instruments GUI; there was incomplete attempt to decode that files.
The page https://medium.com/#pavelkucera/counting-branch-mispredictions-on-macos-7397ae8c5b51 also lists another variant to work with hardware counters on macOS, the https://github.com/opcm/pcm project:
You first have to build the tool, but then it is easy to use:
run pmu-query.py
enter “BR_INST_RETIRED.NOT_TAKEN”, the result should be similar to: cpu/umask=0x10,event=0xC4,name=BR_INST_RETIRED.NOT_TAKEN/
run ./pcm-core.x -e event where “event” stands for the result from the previous step
This gives you continuous results for all the running processes. Find
info on how to profile a single process in ./pmc-core.x --help
The good news is that results are easily readable as they can be
output as csv file. The bad news is that profiling a single process
still includes activity from other processes.

What file access woke the sleeping disk in Windows?

Every now and then, my sleeping disk wakes up, does what sounds like a single read, and then sits idle until it falls asleep again. Sometimes a program that I am using completely freezes for about 10 seconds while the disk spins up, even though that program doesn't seem to need to read from that drive.
Is there an api for listening to file accesses as they happen, or similar, so I can figure out what is read from that drive, so I can move it? If not on Windows, can I do this on Linux?
This is also applicable for figuring out what files/folders a program is accessing in general, so I wouldn't say it only applies to my very narrow problem.
There's a simple tool called What's My Computer Doing? that you can use to get a quick idea of what's causing activity on your computer.
Install and run it, and leave it running in the background. Once you use this tool to narrow down which process is causing the disk activity, you'll want a more comprehensive tool. I use Process Monitor from Sysinternals/Microsoft.
It can be a bit daunting at first, but that's mainly because it is so powerful. It can also alter the behavior of the computer. When it's running, it backs up the huge quantity of data it collects to the disk. So that's why I suggest using the 'What's My Computer Doing?' tool first. Once you know which process is generating the disk access you can add a new filter rule (keep all the defaults, as they mask out a bunch of normal system processes) and select "Process Name" "is" "process_name", or select "PID" "is" "actual_PID".
There are plenty of tutorials like this one that can help you get started with Process Monitor.

How does Mac OS X know what programs are using a mounted filesystem?

This may sound like a silly question but up until recently if you tried to unmount a volume that was in use the Finder reported that it was in use, but not by whom. This is simple Unix functionality, if a file is open on a mount point, do not allow it to eject. But now they seem to have added functionality that lets the user know what programs are currently using a mounted system, and I have been looking through man pages of fopen,stat, etc. for Unix like operating systems(distros of linux) and I can't seem to find similar functionality.
Is this functionality specialized, or am I just looking in the wrong place?
There are BSD-level calls (mainly lsof, whose source is at http://www.opensource.apple.com/source/lsof/) that let you examine the list of files open in a process. Activity Monitor, for example, uses them.
Using lsof as a starting point, you can iterate through processes and see if any of them are using a file under the mount point you're examining. There may be more efficient ways to do it though, of which I'm not aware. :)
It's somewhat specialized. Check out the lsof utility.
Check the man page for fuser, and run fuser -c /mountpoint

Application to watch what an executable does?

I need to find out exactly what files/directories a Lua program uses so I can try to only pack what it needs into a ZIP file, and come up with a simple way to deploy this script.
I used SysInternals' Process Monitor, but I'm surprised by the small amount of information it returned while it watched the program (For Lua users out there, it's wsapi.exe, which is the launcher for the light-weight Xavante web server).
Does someone know of a good Windows application that can completely monitor what a program does, eg. something like a live version of the venerable PCMag's InCtrl5.
Thank you.
Process monitor will catch everything. If it's not catching the action then it must be happening in a different process. Try filtering based on the files you expect to be used rather than the process you expect it to happen in.

Resources