zoukankan      html  css  js  c++  java
  • 【valgrind】How to get hotspot in 10 mins

    1. Overview

    Assume you know nothing about the source code in detal, but you have a test/profile case in your hand. How can you find the hotspot in 10 mins? The answer is Calgrind in Valgrind!!

    Here is a very good introduction about Callgrind from http://baptiste-wicht.com/posts/2011/09/profile-c-application-with-callgrind-kcachegrind.html

    "Callgrind is a tool in part of the Valgrind toolchain. It is running in Valgrind framework. The principle is not the same. When you use Callgrind to profile an application, your application is transformed in an intermediate language and then ran in a virtual processor emulated by valgrind. This has a huge run-time overhead, but the precision is really good and your profiling data is complete. An application running in Callgrind can be 10 to 50 times slower than normally."

    2. Install

    sudo apt-get install valgrind kcachegrind graphviz

    Valgrind: a tool box which include memory leak checker, Calgrind and so on.

    kcachegrind: "the profile data visualization" (ref from: http://kcachegrind.sourceforge.net/html/Home.html)

    graphviz: in order to view the call graph in KCachegrind.(ref from: http://baptiste-wicht.com/posts/2011/09/profile-c-application-with-callgrind-kcachegrind.html)

    2.1. specific for Putty@Windows

    If you are working on Putty@Windows to remote to a Linux server, You also need:

    3. Using Callgrind and Kcachegrind

    3.1. Example code:

    /* add_mul.c */
    #include "stdio.h"
    #include "stdlib.h"
    void add_float(float *a, float *b, int len)
    {
        int i=0;
        for(i=0; i<len; i++)
        {
            a[i] = a[i] + b[i];
        }
        return;
    }
    void mul_float(float *a, float *b, float len)
    {
        int i=0;
        for(i=0; i<len; i++)
        {
            a[i] = a[i] * b[i];
        }
        return;
    }
    void add_double(double *a, double *b, int len)
    {
        int i=0;
        for(i=0; i<len; i++)
        {
            a[i] = a[i] + b[i];
        }
        return;
    }
    void mul_double(double *a, double *b, double len)
    {
        int i=0;
        for(i=0; i<len; i++)
        {
            a[i] = a[i] * b[i];
        }
        return;
    }
    add_mul.c
    #include "stdio.h"
    #include "stdlib.h"
    void add_float(float *a, float *b, int len);
    void mul_float(float *a, float *b, float len);
    void add_double(double *a, double *b, int len);
    void mul_double(double *a, double *b, double len);
    int main()
    {
        int len=10000;
        int i=0;
         
        {
            float *a = malloc(sizeof(float) * len);
            float *b = malloc(sizeof(float) * len);
            for(i=0; i<len; i++)
            {
                a[i] = i;
                b[i] = -2 * i;
            }
            add_float(a, b, len);
            mul_float(a, b, len);
        }
        {
            double *a = malloc(sizeof(double) * len);
            double *b = malloc(sizeof(double) * len);
            for(i=0; i<len; i++)
            {
                a[i] = i;
                b[i] = -2 * i;
            }
            add_double(a, b, len);
            mul_double(a, b, len);
        }
         
        return 0;
    }
    test_profile.c

    3.2. Shell command to profile with Callgrind and Kcachegrind:

    josh@josh-VirtualBox:~/test_profile$ gcc test_profile.c add_mul.c -O3 -o x
    josh@josh-VirtualBox:~/test_profile$ valgrind --tool=callgrind ./x
    ==7066== Callgrind, a call-graph generating cache profiler
    ==7066== Copyright (C) 2002-2011, and GNU GPL'd, by Josef Weidendorfer et al.
    ==7066== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
    ==7066== Command: ./x
    ==7066==
    ==7066== For interactive control, run 'callgrind_control -h'.
    ==7066==
    ==7066== Events    : Ir
    ==7066== Collected : 568917
    ==7066==
    ==7066== I   refs:      568,917
    josh@josh-VirtualBox:~/test_profile$ kcachegrind callgrind.out.70

    3.3. Result:

    Here I only paste the "call graph". See more details yourself(smile)

    4. Others

    You can even profile a Shell command, Python script using this method.

    profile Shell command:

    $ valgrind --tool=callgrind ls -l

    profile Python script:

    josh@josh-VirtualBox:~$ cat test.py
    #!/usr/bin/python
    r=range(0,10000)
    for x in r:
        print x
    josh@josh-VirtualBox:~$ valgrind --tool=callgrind ./test.py
  • 相关阅读:
    [源码]一个简单的源代码行数统计器
    [转载]一个java程序员的面试
    [源码]用c#创建支持多语言的应用程序
    Effective C#
    [源码]类似于 word 的颜色选择器 ColorPicker
    opera中开启WebGL
    Java学习之路
    邮箱正则表达式写法
    AX 2009 扩展类型控件的Lookup的写法
    唐骏的管理名言
  • 原文地址:https://www.cnblogs.com/xjsxjtu/p/3893390.html
Copyright © 2011-2022 走看看