Why top and ps not showing the same cpu result

왜 ps 와 top 은 서로 다른 cpu 사용률을 보여주는 것일까? 결론부터 이야기하면, top 과 ps 는 서로 다른 명령어이다.

top 은 우리가 흔히 생각하는 cpu 사용률을 보여주는 것이 맞지만, ps 는 약간 다른 부분을 보여준다. 다음은 ps 명령어의 man page 의 일부분이다.

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process.  This is not ideal, and it does not conform to the standards that ps otherwise conforms to.  CPU usage is unlikely to add up to exactly 100%.

즉, ps 명령어는 프로세스가 동작한 전체 시간에서의 cpu 점유율을 나타낸다는 것이다. 따라서 지정된 시간 동안에서의 cpu 점유율을 보여주는 top 과는 서로 다른 결과를 보여줄 수 밖에 없는 것이다.

top 은 모니터링, ps 는 스냅샷으로 생각을 하면 이해가 쉬워진다.

참조: http://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result

How to clear memory cache in Linux

Linux 에서 캐시된 메모리를 어떻게 정리할 수 있을까? 결론적으로 세 가지 방법으로 캐시된 메모리 삭제가 가능하다.

1. 페이지 캐시 해제

2. 해제 가능 오브젝트(dentry, inode) 해제
– dentry에 대해서는 여기(http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file)를 참조하자.

3. 해제 가능 오브젝트 + 페이지 캐시 해제

아래는 캐시된 메모리 해제와 관련한 자세한 내용이다.(https://www.kernel.org/doc/Documentation/sysctl/vm.txt)

drop_caches

Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes.  Once dropped, their
memory becomes free.

To free pagecache:
	echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
	echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
	echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync' prior to writing to /proc/sys/vm/drop_caches.  This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.

This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...)  These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems.  Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use.  Because of this,
use outside of a testing or debugging environment is not recommended.

You may see informational messages in your kernel log when this file is
used:

	cat (1234): drop_caches: 3

These are informational only.  They do not mean that anything is wrong
with your system.  To disable them, echo 4 (bit 3) into drop_caches.

실제로 캐시를 줄이는 명령어는 원하는 모드에 따라 아래와 같은 방식으로 입력하면 된다.

$ echo 1 > /proc/sys/vm/drop_caches
$ echo 2 > /proc/sys/vm/drop_caches
$ echo 3 > /proc/sys/vm/drop_caches

다음은 테스트 결과이다. 확실히 캐시된 메모리가 줄어드는 것을 확인할 수 있다.

 

root@mywork:~# free -m
total       used       free     shared    buffers     cached
Mem:          7703       3618       4084        433        309       1239
-/+ buffers/cache:       2069       5633
Swap:         7627          0       7627

root@mywork:~# echo 1 > /proc/sys/vm/drop_caches

root@mywork:~# free -m
total       used       free     shared    buffers     cached
Mem:          7703       2700       5003        432          0        641
-/+ buffers/cache:       2058       5644
Swap:         7627          0       7627

 

참조: http://unix.stackexchange.com/questions/58553/how-to-clear-memory-cache-in-linux

why ps is wrong?

오늘 kldp 에 ps로 메모리를 확인하려고 하는데 잘 안된다는 질문 내용을 봤다.

https://kldp.org/node/152025

답글을 작성하다가 재미있는 내용을 알게 되어 이곳에도 같이 담아둔다.

 

질문 내용

프로그램 안에서 10 바이트씩 malloc 으로 메모리를 할당했습니다.
그리고 ps -eo user,size,cmd 명령으로 메모리 증가량으로 확인하려고 했지만 전혀 메모리 증가가 안되네요.

왜 그런거죠?

답변

두 가지 이유가 있습니다.

1. 확인하고자 하시는 메모리 증가량이 너무 작습니다.

man ps 에서 size 항목을 찾아봤는데 따로 단위는 안나오네요.
하지만 테스트 해보니 단위가 1K 였습니다.
프로그램을 수정해서 한번에 1M 씩 증가하도록 해봤는데, 증가량이 확인되네요.
할당하는 메모리 크기를 1M 단위로 하신다면 쉽게 증가 내역을 확인하실 수 있습니다.

2. ps 명령은 정확한 메모리 량을 확인하기에는 부족한 유틸리티 입니다.
출처 : http://stackoverflow.com/questions/131303/how-to-measure-actual-memory-u…

ps 명령은 사실 어플리케이션에서 사용하는 정확한 메모리 양을 나타내지 않습니다. 단지 예약된 메모리 양을 나타낼 뿐입니다.
달리 말하면 때에 따라(커널 레벨에서 사용되는 페이지가 공유 되거나 할 경우)변동될 소지가 있다는 것입니다. (예를 들면 여러개의 쓰레드나 동적 라이브러리를 사용하는 경우가 있습니다.)

그리고 만약 정확한 메모리 크기를 확인하고자 하신다면 다른 프로그램을 사용하셔야 합니다. valgrind 가 대표적이죠. 주로 메모리 누수 탐지에 사용되지만 메모리 사용량을 확인할 수도 있습니다.

————————————————————————————

실제로 위에 인용한 스택 오버플로우의 내용 말고도 ps 는 메모리 사용량을 확인하는데 부족하다는 내용의 많은 양의 문서를 확인할 수 있었다.

정확한 메모리 사용량을 확인하고자 한다면, 다음의 링크에 소개된 프로그램을 이용하자.

http://www.binarytides.com/linux-command-check-memory-usage/

Evoland error patch

Ubuntu 에서 Evoland 실행시 다음과 같은 오류가 발생했다.

Flash Player PPAPI not detected in /opt/google/chrome/PepperFlash/libpepflashplayer

해결책은 다음 링크에 있었다.

http://steamcommunity.com/app/233470/discussions/0/617329150698258331/?insideModal=1

 

해결 방법이 몇가지 있었는데, 가장 쉽고 빠른 방법은 3번 방법으로 패치 파일을 적용하는 방법이었다.

링크에 걸려진 패치 파일을 다운 받아 Evoland 가 설치된 디렉토리에 덮어쓰기를 하면 된다.

보통은 다음의 위치에 설치되어 있다.

~/.steam/steam/steamapps/common/Evoland

혹시나 싶어 해당 파일을 같이 올려 놓는다.

Evoland Fix (64bit users)

 

Upgrade openssl-1.0.1 on Debian 6

Freeswitch 를 설치하는데 openssl-1.0 이상 버전을 요구했다.

현재 운용중인 서버 버전은 Debian-6.0
apt-get 으로 설치 가능한 openssl 버전은 0.9.x 버전이었다.

 

즉, openssl 버전 문제로 정상적인 설치가 안되는 상황.
openssl 최신버전이야 소스 설치를 하면 되겠는데.. 문제는 이미 사용중인 openssl-0.9.x 버전은 어떻게 하느냐였다.
이미 많은 수의 패키지들이 라이브러리를 참조하고 있었다.
실제로 apt-get remove 명령어로 해당 패키지를 지워볼까 생각도 했는데, 의존성 문제로 상당히 많은 수의 패키지들을 같이 지워야 해서 관두었다.

어쩔까…
Linux HanIRC 채널에 문의를 해보니 다행히 답변이 나왔다.

그냥 덮어쓰면 된다는 것.

 wget http://openssl.org/source/openssl-1.0.1.tar.gz 
 tar xfz openssl-1.0.1.tar.gz 
 cd openssl-* 
 ./config --prefix=/usr zlib-dynamic --openssldir=/etc/ssl shared 
 make 
 sudo make install

 

출처 : http://mariobrandt.de/archives/linux/upgrading-openssl-on-debian-6-squeeze-or-ubuntu-8-04-hardy-456