views:

167

answers:

1

Hi all,

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.

A: 

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
Paul R
Thanks for your reply.It's very difficult to modify the behavior of my c-programs.
Peter Lee
@Peter: it shouldn't be that hard - all you need to add is an extra loop in main() that will repeatedly run your code for each file on the command line.
Paul R
Thanks for your reply. I understand what you say.The example I posted here is a very simple one. There are a lot of conditions in that for loop in the shell script, so it's very difficult to for me to change the source code of the c-program.I just want to know if there are any solutions that use shark programatic control or something else.Another quick question here:I used:shark -i mycprog -o mycprogsessionnamethe -o option does not work at all, which is very weird to me. I googled a lot, but no success.
Peter Lee
@Peter: I've added an alternative suggestion to my answer now - use chudRemoteCtrl to start/stop sampling
Paul R
Hi Paul,I used your idea. Set Sampling->Programmatic (Remote), and then I ran my shell script (chudRemoteCtrl added), it told me "ERROR: No samples taken. [CHUDDataSource]". So I changed the "Sample Interval" to "20us", and this time I did get some results, but (1) before my for-loop had been finished, Shark GUI already gave me the result (the "Time Limit" is set to "30s", and "Sample Limit" to maximum, which should be enough for the for-loop); (2) The result has no mycprog symbols at all (for example: the functions I called in main() in mycprog.c.)
Peter Lee
@Peter: you should probably take a little time to familiarise yourself with Shark. Try profiling just one run of your program - launch it from the Shark GUI rather than the command line, to keep things simple for now. Also 20 µs is probably too short and you will get way too many samples - I typically use 100 µs for short periods (e.g. 1s max) or 1 ms for longer periods (e.g. 10s max). You might also want to put a `sleep` after the initial `chudRemoteCtrl -s` in case you're not allowing enough time for sampling to begin. I've updated the answer above.
Paul R
Hi Paul, I agree with you, since I'm not so familiar with Shark. Anyway, I did try use different Sample Intervals, such as 20us, 200us, 300us.
Peter Lee
@Peter: note that the choice of sampling interval has an impact on maximum sample time.
Paul R