gprof 사용법 (예제 중심으로...)

antz의 이미지

gprof 사용법

내용이 모자란 부분이 많을겁니다.
예제 중심으로 설명하겠습니다.

우선, 간단히 gprof를 설명하면,
(저도 이제 알아서 잘은 모릅니다만...)
프로그램에 대한 함수들의 사용빈도, 사용시간등을
측정할 수 있는 툴입니다.
프로그램을 개발하고나서 프로그램을 검증하는데
좋은 툴같습니다.

이제 설명에 들어갑니다.

프로그램은 하나이상의 사용자 함수가 있어야합니다.

다음 코드는 thread를 시험하던 코드에
gprof를 적용해 봤습니다.
우선 쓰레드 루틴은 살리되 thread로 함수를 call
하는 대신 직접 함수를 call하게 바꾸었습니다.

th.c

#include <stdio.h>
#include <pthread.h>

void func()
{
        int i;

        while(1)
        {
                if (i==10) break;
                printf("ID: %d \n", pthread_self());
                sleep(1);
                i++;
        }
        // pthread_join(pthread_self(), NULL);
}

int main(void)
{
        int i;
        // pthread_t hThread;

        for (i=0; i<2; i++)
        {
                // pthread_create(&hThread, NULL, (void *)func, NULL);
                func();
        }
}

이제 컴파일을 합니다.

Quote:
$ gcc -o th th.c -pg

실행을 합니다.

Quote:
$ ./th
ID: 0
ID: 0
ID: 0
ID: 0
ID: 0
ID: 0
ID: 0
ID: 0
ID: 0
ID: 0

th를 실행해서 정상적으로 종료되면,
gmon.out 파일이 만들어 진것을 보실 수 있습니다.

Quote:
$ ls
gmon.out th th.c th.o

이제 gprof로 프로그램에대한 profile을 만들어봅니다.
gprof 사용법은,
gprof [실행 binary] [실행으로 생성된 gmon.out]
입니다.

Quote:
$ gprof th gmon.out > th_profile.txt

th_profile.txt을 첨부합니다.

사용자 함수가 하나 뿐이어서 결과가 보잘것 없지만,
복잡한 프로그램은 각각 함수에대한 점유시간이 %로 나와서
어떤 함수를 고쳐야 속도가 빠르게 된다던지
하는 판단을 할 수 있습니다.

File attachments: 
첨부파일 크기
Plain text icon th_profile.txt4.97 KB
Forums: 

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.