3

I've got a relatively long function that's dominant in the Instruments Time Profiler. Is there a way to add additional symbols to this function so the sampling will show time allocated to different parts of the function? I'm looking for something like the MARK macro that existed for prof(1) years ago.

Scott
  • 113
  • 5

4 Answers4

6

Using the macro:

#define MARK(K) asm("M."#K":");

has been working well for me. This is really just a simplification of the old MARK macro I mentioned in my original question. Placing MARK(LOOP1); somewhere in a function will add a new symbol, M.LOOP1 that will show up in a list of functions shown by shark or instruments.

Scott
  • 113
  • 5
5

I discovered recently that in the time profiler in instruments, if you double-click on a method, it'll show you your source code with percentages of time spent on each line.

http://douglasheriot.com/blog/2011/04/xcode-4-instruments-awesomeness/

I've found it very useful, but I'm not sure if that's what you're asking for.

DouglasHeriot
  • 1,674
  • 2
  • 13
  • 19
1

I'm told that Shark can do this, so Instruments should also, but you have to tell it what to do:

  • Do sampling, on wall-clock time (not just CPU time), of the function call stack (not just the program counter PC).

  • To tell you the lines of code (not just functions) that appear on a good percentage of stack samples.

A stack sample includes the PC and every call instruction leading to where the PC is. Every instruction on the stack is jointly responsible for that slice of time being spent.

So any line of code responsible for X% of the time will be on the stack X% of the time. If it's big enough to be worth looking at, you will see it on the samples. You may get a lot of samples, but you don't actually need a lot. This is because it's more important to locate the problem than to measure it with much precision.

If your biggest problem, when fixed, would save you 5%, it will appear on about 5% or more of samples. If it's any smaller than that, your code's pretty optimal. Chances are it's a lot bigger than that, so you won't have any trouble seeing precisely where it is.

Added: An example of a profiler that does wall-time stack sampling and shows percent-by-line is Zoom, so I suggest you watch that video. Then, try to get Instruments to do the same thing.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
  • Shark will do per-line reporting, but I'd prefer to be able to adjust the granularity and get reporting for sections of code I specify. It doesn't appear Instruments can do the per-line reporting. – Scott Apr 03 '11 at 19:48
  • @Scott: Really? I thought Instruments was everything Shark was and more. If they dropped out per-line reporting I'm confused. In any case, the method I depend on is [random-pausing](http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux/378024#378024). You may want to adjust the granularity, but I would suggest the method that really works is wall-clock time (not CPU) sampling of the stack. I look at individual samples (and not many are needed) and look at the lines that show up on multiple samples. – Mike Dunlavey Apr 03 '11 at 22:01
1

UPDATE:

I have updated the code and created a separate project:

https://github.com/nielsbot/Profiler


I have some code that can do this here: I have some code you might fin that can do this here: https://gist.github.com/952456 HTH

You can profile sections of your function using this code like this:

-(void)myMethod
{
    ProfilerEnter( __PRETTY_FUNCTION__ );

    // ... code ...
    {
        ProfilerEnter("operation x");

        // your code here
        // ...

        ProfilerExit("operation x");
    }

    ProfilerExit(__PRETTY_FUNCTION__);
}
nielsbot
  • 15,922
  • 4
  • 48
  • 73