In C/C++ programming world, performance is the key element to separate these languages from other high-level languages. C/C++ developers remain busy in tuning their code to run faster. But you can’t always rely on human skills to optimize your program for performance. There are many code profiling tools available to help you, one of the most popular is Valgrind. We’ll learn it today.
Apart from programming fixes, there are other ways to optimize a program for performance. One of the noted methods is to use code profiling tools that allow dynamic analysis of code.
Code profiling tools not only support run-time analysis, but they also work as leak checker. You can use them to find memory leakage in your source code.
In today’s article, we’ll talk about Valgrind, which is a famous and free code profiling tool on Linux platforms. But let’s first understand code profiling and leak checker concept in detail.
What is code profiling?
Code profiling is an essential facet of programming. Some of you may already know about code profiling yet few of us could wonder what it is. Code profiling is the process to determine which part of the program is depleting resources. And it can expose the program sections which are consuming time within the application.
For example, say your program spends half of the time in string-handling routines. And there is a possibility to stimulate these functions by 10 percent. It means the overall application’s execution time will improve by 5 percent.
In the next sections, we’ll discuss Valgrind commands and some useful tips-tricks for code profiling and leak detection.
What is Valgrind?
It is an open source framework, popular code profiling, and leak checker tool. You can instantly use it to detect dynamic memory and threading bugs, and profile application in detail. Alternatively, you can utilize it to develop new tools.
So this tool offers much more than just code profiling and finding leaks.
How to install Valgrind?
You can install it using the Package manager for your OS, but we’ll guide you to build it from the source code. It will help when you run and debug your program on multiple OS.
Follow the steps given below to download and build source code.
Alternatively, you can use the below automated script to build and install Valgrind.
# 1) Fetch the source (try wget if curl is not available).
curl -O http://valgrind.org/downloads/valgrind-3.6.1.tar.bz2
# Run md5sum to ensure it equals the one on the site.
# 2) Uncompress it.
tar -xjvf valgrind-3.6.1.tar.bz2
# Enter into the freshly created folder.
# 3) Launch configure to set up prerequisites.
# 4) To perform, run the make command.
# 5) Finally install it with root user.
sudo make install
Valgrind Command Syntax and Options:
Once you successfully make theValgrind, you can explore the various commands and options it brings to the plate. Here we are presenting most needed and useful one for you.
valgrind [options] ./[exe_fileName]
Commands and Options:
Don’t presume that we are now closer to the end of the article. There is still one more crucial concept which we want to touch upon in the next section.
What is Suppression and why it is needed?
So far we’ve seen a lot of code profiling and leak checker stuff. Now the turn is to suppress errors that you don’t want to see. These are the leaks outside of your code i.e. in shared libraries and are false positive.
Steps for suppression
1. Run your program using the following command.
valgrind -v –-leak-check=yes --gen-suppressions=yes ./exeName
This command produces the suppression code for each and every error it finds in your program.
2. Now locate the system library errors that we are not going to fix. Copy suppression code for those errors in a separate file.
3. Next, save the above file with .supp extension.
4. Again run your program with the following command.
valgrind -v --leak-check=yes --gen-suppressions=yes suppressions=/path/file.supp ./exeName
5. This time, Valgrind will ignore all the errors which are in the suppression file. And you will only see the suppressed error count instead of the defects in the end.
Let’s conclude the review of code profiling and leak checker tool – Valgrind:
We wish that there is something new for you to learn from the above extract about Valgrind. And you could apply this knowledge in your production environment.
Our team is already benefiting from this fantastic code profiling tool, and this article is the crux of their real-time experience with it. In the end, we request you to share this post with your friends and on the social media of your choice.