aws 콘솔에 모니터링 페이지가 있지만 cpu 최적화할 때 측정용으로는 애매한 것 같다.

top -b

다양한 cpu 모니터링 툴이 있지만 top 이 무난하고 좋은 것 같다. top 은 화면으로 출력되는 콘솔 모니터링 프로그램인데 파일로 저장하려면 배치 모드(-b)를 이용해야 한다. 간격은 -d 옵션을 이용하면 된다.

-b : Batch mode operation

Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.

-d : Delay time interval as: -d ss.tt (seconds.tenths)

Specifies the delay between screen updates, and overrides the corresponding value in one's personal configuration file or the startup default. Later this can be changed with the 'd' or 's' interactive commands.

Fractional seconds are honored, but a negative number is not allowed. In all cases, however, such changes are prohibited if top is running in 'Secure mode', except for root (unless the 's' command-line option was used). For additional information on 'Secure mode' see topic 5a. SYSTEM Configuration File.

ex) top -b -d 30

: 30초 간격으로 출력

참고 :

https://linux.die.net/man/1/top

https://www.tecmint.com/save-top-command-output-to-a-file/

grep -A

기본으로는 모든 프로세스가 cpu 사용량 별로 소팅되어 보이는데 사실 상위 n 개만 관심이 있다. 이럴 때는 grep -A 옵션을 이용해서 "load average"가 매치되는 줄에서 n 라인을 가져오도록 한다.

ex) top -d 5 -b | grep "load average" -A 15
: 5초 간격으로 출력된 결과물 중 "load average" 아래 15개 줄을 가져온다.

참고 : 

tee

결과를 리디렉터(>)를 이용해서 파일로 저장할 수 있지만 화면으로 확인하고 싶다면 tee 를 이용하자.

ex) top -d 5 -b | grep "load average" -A 15 | tee cpu_us.log
: 5초 마다 cpu 사용량 상위 10개 프로세스를 cpu_us.log 파일로 저장

grep --line-buffered

하지만 화면에 뭔가 이상하게 출력되고 파일로 저장된다. 파이프( | ) 나 리디렉터( > ) 문제인 줄 알았는데 각각의 프로그램이 원인이라고 한다. grep 옵션을 보니 --line-buffered 를 이용하면 라인단위 처리를 해서 성능이 떨어진다고 하는데 이 옵션을 사용하고 문제가 해결되었다.

       --line-buffered
              Use  line  buffering  on  output.   This can cause a performance
              penalty.

ex) top -d 5 -b | grep "load average" -A 15 --line-buffered | tee cpu_us.log

참고 : 


728x90

+ Recent posts