26

I want to use valgrind to do some profiling, since it does not need re-build the program. (the program I want to profile is already build with “-g")

But valgrind(callgrind) is quite slow ... so here's what I to do:

  1. start the server ( I want to profile that server)
  2. kind of attach to that server
  3. before I do some operation on server, start collect profile data
  4. after the operation is done, end collecting profile data
  5. analyze the profiling data.

I can do this kind of thing using sun studio on Solaris. (using dbx ). I just want to know is it possible to do the same thing using valgrind(callgrind)?

Thanks

superb
  • 963
  • 1
  • 10
  • 21
  • I hope Valgrind is not your only option. Have you considered this technique: (http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux/378024#378024) You can also use **pstack** or **lsstack** to do the same thing. For money, you can get RotateRight/Zoom which is quite good. – Mike Dunlavey Mar 08 '10 at 16:08

3 Answers3

42

You should look at callgrind documentation, and read about callgrind_control.

  1. Launch your app : valgrind --tool=callgrind --instr-atstart=no your_server.x
  2. See 1.
  3. start collect profile data: callgrind_control -i on
  4. end collect profile data: callgrind_control -i off
  5. Analyze data with kcachegrind or callgrind_annotate/cg_annotate
Doomsday
  • 2,650
  • 25
  • 33
12

For profiling only some function you can also find useful CALLGRIND_START_INSTRUMENTATION and CALLGRIND_STOP_INSTRUMENTATION from <valgrind/callgrind.h> header and using callgrind's --instr-atstart=no option as suggested in Doomsday's answer.

Community
  • 1
  • 1
Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • I didn't realize that it is still necessary to run your program within valgrind, even with the macros. E.g., `valgrind –tool=callgrind –instr-atstart=no your_server.x` – Walter Nissen Aug 10 '18 at 16:34
4

You don't say what OS - I'm assuming Linux - in which case you might want to look at oprofile (free) or Zoom (not free, but you can get an evaluation licence), both of which are sampling profilers and can profile existing code without re-compilation. Zoom is much nicer and easier to use (it has a GUI and some nice additional features), but you probably already have oprofile on your system.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Yes, I just see some other guys recommended these 2 tools. For me, the point to use valgrind is it does not root access since I am using a public server to do profiling... – superb Mar 08 '10 at 08:32
  • I think you only need root access to *install* these tools - you should be able to run them without root access, no ? (I use Zoom from a normal non-root account, but that's for local profiling, so I can't be sure about the remote profiling case.) – Paul R Mar 08 '10 at 08:44
  • I see, thanks. But I still want to know if it is possible to use valgrind to do the same thing. Sometimes when I can not reproduce performance issue, I need to take look at test machine directly, but install profiling software (which collect hardware counter) may impact system performance, and a lot of baseline need adjusted which is not allowed.. So valgrind is still the best option, if I can use it in the way I described in the question. – superb Mar 08 '10 at 08:51