기본 콘텐츠로 건너뛰기

라벨이 proc인 게시물 표시

/proc/{PID}/environ 출력하기

/proc/{PID}/environ 파일은 각 환경변수마다 0x00으로 끝나는 형식을 가지고 있다. 이것을 cat 하면 제대로 볼 수 없는데, xargs를 이용하면 간단하게 볼 수 있다. $ xargs -0 -L1 -a /proc/${PID}/environ 참조: https://askubuntu.com/questions/978711/how-do-i-split-a-proc-environ-file-in-separate-lines

Linux 프로세스별 CPU 점유율 구하기

이 방법은 전체시간에 대해 프로세스 소모시간을 구한 것이고, 순간순간마다 구하려면 이전 상태를 저장하고 있어야한다. /proc/uptime에서 첫번째 항목을 저장한다. - uptime /proc/pid/stat 에서 starttime, utime, stime을 각각 저장한다. percent = (utime+stime) / (uptime-starttime/HZ) * HZ는 <sys/param.h>에서 sysconf(_SC_CLK_TCK)로 정의함. Powered by ScribeFire . 원본 위치: http://purewell.egloos.com/3516509

Linux /proc/pid/stat 구조

Linux 2.6 / 출처: http://linux.die.net/man/5/proc ㅡ_-) 중간에 쓰이지 않는 '0' 필드가 있으니 주의!!! pid %d     The process ID. comm %s     The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out. state %c     One character from the string "RSDZTW" where R is running, S is sleeping in an interruptible wait, D is waiting in uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging. ppid %d     The PID of the parent. pgrp %d     The process group ID of the process. session %d     The session ID of the process. tty_nr %d     The tty the process uses. tpgid %d     The process group ID of the process which currently owns the tty that the process is connected to. flags %lu     The kernel flags word of the process. For bit meanings, see the PF_* defines in <linux/sched.h>. Details depe...

Linux에서 CPU 사용률 구하기

procps 패키지에서 top 소스를 까보면 쉽게 알 수 있다. 일단 CPU개수를 구해야하는데, sysconf(_SC_NPROCESSORS_ONLN)로 쉽게 구할 수 있다. (아쉽게도 표준이 아니므로 다른 OS에서 잘 돌아간다는 보장은 없다) 다음은 현재 CPU 사용률을 구해야하는데 리눅스는 이것을 /proc/stat에 텍스트 형태로 저장한다. 형태는 아래와 같다. cpu user system nice idle wait hi si zero cpu? user system nice idle wait hi si zero (반복...) 첫 라인은 전체 cpu 평균을 가르키는 것 같다.(막가는거지요?) 다음 줄부터는 CPU 정보를 cpu0부터 표기하였다. 각 필드는 리눅스 시간 단위인 jiffies로 내용은 아래와 같다. user : 사용자 영역 코드 실행 시간 system: 커널 영역 코드 실행 시간 nice : 기본보다 낮은 우선순위로 실행한 사용자 영역 코드 실행 시간 idle : I/O 완료가 아닌 대기 시간 wait : I/O 완료 대기 시간 hi : Hard Interrupt(IRQ) si : Soft Interrupt(SoftIRQ) zero : 끝 위 필드는 stat파일을 열 때, 컴퓨터를 켠 시점부터 jiffies누적이므로 top에서 보여주는 사용률을 구하기 위해서 이전 값을 저장해서 현재 값과 차를 구해야한다. 각 사용률(percent)를 위한 총합은 위에 제시한 모든 필드를 몽땅 더하는 것으로 구할 수 있다. 참고로 위 값은 커널 버전에 따라 wait, hi, si 필드가 없을 수 있다. 2.6 이상 버전은 모두 표시하기때문에 가볍게 넘어가도록 하자. Powered by ScribeFire . 원본 위치: http://purewell.egloos.com/3513312