해당 값을 구하기 위하여 /proc/meminfo 와 /proc/stat을 활용하였다.
먼저현재 CPU 사용률을 구해야하는데 리눅스는 이것을 /proc/stat에 텍스트 형태로 저장한다.
각 필드는 리눅스 시간 단위인 jiffies로 내용은 아래와 같다.
- user: normal processes executing in user mode
- nice: niced processes executing in user mode
- system: processes executing in kernel mode
- idle: twiddling thumbs
- iowait: waiting for I/O to complete
- irq: servicing interrupts
- softirq: servicing softirqs
- steal: involuntary wait
jiffies 는 리눅스 커널에서의 동작 시간을 나타내는 전역 변수이다. HZ값에 알맞게 세팅된 타이머
인터럽트가 주기적으로 발생하면 커널은 시스템 기준 시간을 유지하기 위해서 jiffies라는 변수값을
타이머 인터럽트가 뜰 때마다 1씩 증가 시킨다.
리눅스에서 man 5 proc 이라고 치면 다음과 같은 내용을 알 수 있다
/proc/stat kernel/system statistics. Varies with architecture. Common entries include: cpu 3357 0 4313 1362393 The amount of time, measured in units of USER_HZ (1/100ths of a second on most archi- tectures), that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively. The last value should be USER_HZ times the second entry in the uptime pseudo-file. In Linux 2.6 this line includes three additional columns: iowait - time waiting for I/O to complete (since 2.5.41); irq - time servicing interrupts (since 2.6.0-test4); softirq - time servicing softirqs (since 2.6.0-test4). Since Linux 2.6.11, there is an eighth column, steal - stolen time, which is the time spent in other operating systems when running in a virtualized environment page 5741 1808 The number of pages the system paged in and the number that were paged out (from disk). swap 1 0 The number of swap pages that have been brought in and out. intr 1462898 This line shows counts of interrupts serviced since boot time, for each of the possi- ble system interrupts. The first column is the total of all interrupts serviced; each subsequent column is the total for a particular interrupt. disk_io: (2,0):(31,30,5764,1,2) (3,0):... (major,minor):(noinfo, read_io_ops, blks_read, write_io_ops, blks_written) (Linux 2.4 only) ctxt 115315 The number of context switches that the system underwent. btime 769041601 boot time, in seconds since the Epoch (January 1, 1970). processes 86031 Number of forks since boot. procs_running 6 Number of processes in runnable state. (Linux 2.5.45 onwards.) procs_blocked 2 Number of processes blocked waiting for I/O to complete. (Linux 2.5.45 onwards.) |
위 글을 보면 Jiffes는 HZ 값에 따라 1분에 카운터 되는수치가 다르다는 것을 알 수 있다. 근데보통 100이라고하는데확인할필요성이있다. kerenl_source_dir/arch/Architecture_Type/param.h에보면 HZ값이디파인값이확인되어있으니확인해보자
필자가 사용하는 보드인 경우에는 100으로 설정되어 있어 소스코드 또한 그에 맞게 코딩하였다. 만약 HZ가 200이면 0.005초 간격으로 Jiffies값이 1식 증가한다.
그리고 보드에서 사용할 수 있도록 busybox에 추가하였다. busybox 추가를 위한 수정은 diff_log를 참조하길 바란다.
cpuRate 함수를 보면 리턴을 다음 과 같이 한다.
58 double result =(double)(CpuAfterStat.user + CpuAfterStat.nice +
CpuAfterStat.system) - (double)(CpuBeforeStat.user + CpuBeforeStat.nice
+ CpuBeforeStat.system); 59 60 return (result / ((afterTime-beforeTime)*100))*100; |
측정 시간을 1초로 한경우에는 result 자체가 CPU 사용률이 된다. 이유 인 즉슨, 1초에 jiffes는 총 100개 증가하며 그중 User/Nice/System에 사용된 jiffes는 N/100 이다. 따라서 백분률로 따지면 N 그 자체가 되는 것이다.
(afterTime-beforeTime)*100) 으로 한 것은 1초에 jiffes가 100증가해서 이다. HZ가 200이면 200을 넣어줘야할 것이다. 그 뒤에 100은 백분률을 위한 100 - _