简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM