Ubuntu, Android

[Ubuntu] 성능평가: perf 사용하기

vhxpffltm 2022. 10. 17. 21:10
반응형

성능 평가를 해야할 일이 있는데.. 

 

과연 이 작업이 유효한가.. 다른 많은 도구들이 있는데 찾아보면 되고..

그중 FlameGraph를 그릴수 있는 perf + FlameGraph 튜토리얼을 작성하고자 한다.

 

Linux에서는 이것을 사용하고 Windows에서는 vTune이나 AMD uprof를 사용하면 좋다.

VS의 성능 프로파일러로는 음 뭔가 확인하는 근거가 어렵다.

 

먼저 perf + FlameGraph 는 아래의 문서에서 확인할 수 있다.

https://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html

 

CPU Flame Graphs

CPU Flame Graphs Determining why CPUs are busy is a routine task for performance analysis, which often involves profiling stack traces. Profiling by sampling at a fixed rate is a coarse but effective way to see which code-paths are hot (busy on-CPU). It us

www.brendangregg.com

 

이 perf를 WSL2 Ubuntu 환경에서 시작해보자.

 

perf를 WSL2 에서 사용하기 위해서는 직접 소스파일을 받아 빌드하여 사용해야 한다.

필자는 빌드한 바이너리를 usr/bin 에 카피하여 사용하였다.

Symbolic link를 걸어 사용할수도 있다.

https://stackoverflow.com/questions/60237123/is-there-any-method-to-run-perf-under-wsl

 

Is there any method to run perf under WSL?

When I wanted to run perf under WSL, I met the follow question: WARNING: perf not found for kernel 4.4.0-18362 You may need to install the following packages for this specific kernel: ...

stackoverflow.com

 

단, WSL에서 perf를 사용하는 평가는 정확하지 않을 수 있다.

 

https://askubuntu.com/questions/1314136/installing-linux-perf-tools-on-ubuntu-20-04-lts-with-wsl2

 

perf list 명령으로 perf가 잘 실행되는지 확인해서 사용하자.

문제없이 명령어가 먹힌다면 이제 바이너리의 성능을 쉽게 확인할 수 있다.

 

1. sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches' 명령어로 캐시를 초기화한다.

2. perf record -g -e "cpu-clock" [실행할 바이너리] 

  - 각 옵션은 help를 확인하자.

-g: enables call-graph recording

-e, --event <event>   event selector. use 'perf list' to list available events

  - 이렇게 perf record 명령으로 기록한다.

  - record 작업이 끝나면 아래와 같은 출력을 확인할 수 있다.

  - 그리고 출력의 내용대로 perf.data를 얻을수 있다. 이 데이터를 읽을수 있는 작업이 필요하다.

 

 

FlameGraph

- 여기서 해당 패키지?가 필요하다. perf.data를 FlameGraph로 읽어 데이터를 확인할 것이다.

- https://github.com/brendangregg/FlameGraph 저장소에 상세히 설명되어 있다.

 

3. perf script -i perf.data | ../../../FlameGraph/stackcollapse-perf.pl | grep "main" > test.out

  - script 명령을 사용해 뽑아낸다. -i, --input <file>    input file name

  - 그리고 FlameGraph를 사용하는데 stackvollapse-perf.pl로 FlameGraph 형식을 만들고

  - Poco 문자열을 가진 경우만 test.out에 뽑아낸다.

  - 이렇게 뽑아내면 이제 모든 작업이 끝이 난다.

* 간혹 -i 옵션 사용시 패키지가 없어 에러가 발생할수 있는데 패키지를 설치하고 perf를 수동으로 다시 설치하면 된다.

 

4. FlameGraph SVG 출력: ../../../FlameGraph/flamegraph.pl test.out > test.svg

  - FlameGraph 경로의 flamegraph.pl 로 svg 그래프를 뽑아낸다.

 

이렇게하면 .svg파일에 예제에서 보이는 그래프를 확인할 수 있다. 이를통해 어떤 함수에서 clock을 많이 먹는지 등 성능을 확인할수 있을 것이다.

이를 잘 활용하기 위해서는 벤치마크 프로그램을 잘 잡아야 성능에 대해 확인할 수 있을것이다. 

반응형