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
  • 相关阅读:
    让你彻底明白什么叫游戏引擎(1)
    今后几年将有多于28部游戏电影面世
    Symbian系统体系结构
    让你彻底明白什么叫游戏引擎(2)
    网易程序笔试题
    [转贴]暴雪的霸王条款是否合理?
    CPU GPU设计工作原理《转》
    求伯君:向暴雪学习 金山不求一夜暴富
    我的职业规划
    网络游戏程序员新手入门 [转]
  • 原文地址:https://www.cnblogs.com/xjsxjtu/p/3893390.html
Copyright © 2011-2022 走看看