Tutorial :How to Profile a C-Program in a Loop of Shell Script, Using Shark on Mac?



Question:

I have a question about Shark-profiling on mac.

Say if I have a C-program, compiled with:

gcc -o mycprog mycprog.c -g -pg  

and also I have a shell script something like:

for file in ($SomeDirectory)  do      mycprog $file  done  

I need to profile the average performance for all files in $SomeDirectory.

Where should I put the shark -i command? Thanks.


Solution:1

This is not a great approach, for various reasons - ideally you should modify your program's outer loop so that it can process all the files on the command line, then you can just do everything in one run:

$ shark -i  $ mycprog $SomeDirectory/*  

If you can't do that then you will need to set your Shark configuration for system-wide profiling and start profiling before your bash loop and stop profiling afterwards. When you subsequently view the profile in Shark you'll be able to filter out the processes that you're not interested in.

Firstly open Shark (the GUI app), set up your configuration and enable remote control (Sampling => Programmatic). Make sure you have Time Profile and Everything selected.

Then from the command line:

$ chudRemoteCtrl -s "mycprog"  $ sleep 1  $ for f in $SomeDirectory/*  $ do  $     mycprog $SomeDirectory/$f  $ done  $ chudRemoteCtrl -e  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »