Skip to main content

valgrind를 이용한 메모리 체크

valgrind는  프로그램 설능을 프로파일링 후 메모리 할당 / 초기화 되지 않은 메모리 영역 을 검색할 수 있는데, Redhat 문서를 참고하며 다음과 같은 리포트를 받을 수 있다고 합니다.

  • 실행하지 않아야 하는 메모리 액세스
  • 지정되지 않았거나 초기화되지 않은 값 사용
  • 잘못된 힙 메모리 해제
  • 포인터 중복
  • 메모리 누수

memcheck로 실행되는 어플리케이션은  메모리 사용량 을 확인해야 하기 때문에 일반적으로 실행하는것보다 10~30배 가량 느리게 실행된다고 합니다.

메모리 누수 프로파일링

  1. 패키지 설치
    $> yum install valgrind -y
  2. valgrind를 이용한 메모리 누수 여부 체크
    /usr/bin/test라는 임의 프로그램을 실행하되 하위 라이브러리들도 같이 실행하도록 합니다.
    $ valgrind --tool=memcheck --leak-check=summary --trace-children=yes /usr/bin/test
    ...
    ==21297== Memcheck, a memory error detector
    ==21297== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==21297== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
    ==21297== Command: /usr/sbin/test
    ==21297==
    ERROR: Please define server type (local and/or TCP).
    ==21298==
    ==21298== HEAP SUMMARY:
    ==21298== in use at exit: 101,185 bytes in 3,410 blocks
    ==21298== total heap usage: 5,613 allocs, 2,203 frees, 286,292 bytes allocated
    ==21298==
    ==21298== LEAK SUMMARY:
    ==21298== definitely lost: 0 bytes in 0 blocks
    ==21298== indirectly lost: 0 bytes in 0 blocks
    ==21298== possibly lost: 0 bytes in 0 blocks
    ==21298== still reachable: 101,185 bytes in 3,410 blocks
    ==21298== suppressed: 0 bytes in 0 blocks
    ==21298== Rerun with --leak-check=full to see details of leaked memory
    ==21298==
    ==21298== For lists of detected and suppressed errors, rerun with: -s
    ==21298== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    • leak 옵션은 summary(기본값) / full 둘중하나 선택 가능한듯 하고, full로 실행하게 되면 summary보다는 좀 더 자세한 정보가 보이네요

캐시 사용량 프로파일링

  1. 캐시 사용량은 다음 기능을 하고 있다 합니다.
    • 첫번째 레벨 지시 캐시 읽기 / 읽기 미스 /
    • 데이터 캐시 읽기 (메모리 읽기), 읽기 미스,
    • 데이터 캐시 쓰기 (메모리 쓰기), 쓰기 미스
    • 실행 및 잘못 예측된 조건 분기
    • 실행 및 잘못 예측된 간접 분기
    • 패키지 설치
      $ yum install valgrind -y
  • valgrind 를 이용한 캐쉬 사용량 확인
    $ valgrind --tool=cachegrind --trace-children=yes /usr/sbin/test
    ==21906== Cachegrind, a cache and branch-prediction profiler
    ==21906== Copyright (C) 2002-2017, and GNU GPL'd, by Nicholas Nethercote et al.
    ==21906== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
    ==21906== Command: /usr/sbin/clamd
    ==21906==
    --21906-- warning: L3 cache found, using its data for the LL simulation.
    ERROR: Please define server type (local and/or TCP).
    ==21907==
    ==21907== error: can't open cache simulation output file '/root/cachegrind.out.21907'
    ==21907== ... so simulation results will be missing.
    ==21907== I refs: 0
    ==21907== I1 misses: 0
    ==21907== LLi misses: 0
    ==21907== I1 miss rate: 0.00%
    ==21907== LLi miss rate: 0.00%
    ==21907==
    ==21907== D refs: 0 (0 rd + 0 wr)
    ==21907== D1 misses: 0 (0 rd + 0 wr)
    ==21907== LLd misses: 0 (0 rd + 0 wr)
    ==21907== D1 miss rate: 0.0% (0.0% + 0.0% )
    ==21907== LLd miss rate: 0.0% (0.0% + 0.0% )
    ==21907==
    ==21907== LL refs: 0 (0 rd + 0 wr)
    ==21907== LL misses: 0 (0 rd + 0 wr)
    ==21907== LL miss rate: 0.0% (0.0% + 0.0% )
  1. cache 내용 확인* IL / D / 정보

    • I캐시 읽기 (Ir, 실행된 명령어 수), I1, 캐시 읽기 미스 (I1mr), LL캐시 명령어 읽기 미스 (ILmr)
    • D캐시 읽기 (Dr, 메모리 읽기 수), D1 캐시 읽기 미스 (D1mr), LL 캐시 데이터 읽기 미스 (DLmr)
    • D캐시 쓰기 (Dw, 메모리 쓰기 수), D1 캐시 쓰기 누락 (D1mw), LL 캐시 데이터 쓰기 누락 (Dlmw)
    • 전체 프로그램에 대한 요약 통계
      ——————————————————————————–
      Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
      ——————————————————————————–
      6,122,264 3,538 2,481 1,725,087 79,539 16,924 655,284 10,140 8,215 PROGRAM TOTALS

    • 기능별 통계
      ——————————————————————————–
      Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw file:function
      ——————————————————————————–
      2,074,204 48 14 699,500 46,437 1,226 284,276 70 12 ???:do_lookup_x


reference