Profile with oprofile

Table of Contents

SUMMARY

After my previous article of how to profile a program with gprof, I think many guys has recognize the importance of profile. Today the new blog for introducing how to profile a program with oprofile.

Why oprofile

Many guys may asked why oprofile? There is gprof we can use. Ok, cool down, I’ll take a little time to explain why do this. As we know, gprof has some defects, which can not profile a program in the some scenes(See detail). So, in a given scene, when a daemon program and you has no permission to re-compile it. So, what would you do this? The answer is that gprof cannot do this. Because without re-compile it with “-pg”, the program cannot generate a “gmon.out” result file. And another reason is that your program will run very slowly, since you re-compile with “-pg”, you may cannot find the correct hot point. So, you need a tool, which can do profile at runtime and run as fast as possible, for lucky, oprofile can do it.

How to

Let’s go forward to doing it as fast as we can, I think most of guys hate reading a long article, especially the redundancy one. Ok, drop it, free your mind and follow me to take a look at what we have to.

Work flow

  • First, you need to download or apt-get or yum to install oprofile.
  • opcontrol –vmlinux=/path/vmlinux –start
  • run your program
  • opcontrol –stop
  • opcontrol –dump
  • opreport -cfg | gpref2dot.py -f oprofile | dot -Tpng -o out.png

Ok, now you can open the out.png to research, the call graph, hot point function in it.

CONCLUSION

So that’s also simple, isn’t it? But these are some notes you need to know before you want to try it:

  • oprofile need kernel support, that means you may not run it sucessful in some kernel, such as redhat. In this case, you need to find the specify oprofile release for redhat.
  • oprofile is the kernel level tool of profile.
  • oprofile can do profile at runtime without any compile step.
  • If you cannot find your “vmlinux” image, you may need to specify the –no-vmlinux argument or download your kernel image for skipping that error.
  • Compile your program with “-g” is necessary or you won’t see any useful graph.
  • Use –callgraph argument in some scenes, which your program will call in nest.
  • Use –separate argument when you want to see the information more clearly. Specify the multiple threads program.

Ok, there are a lot of features you need to find out. Have a fun to try it.