,_________________________________________________________________________,
, Get Memory And CPU Performance Statistic ,
, ======================================== ,
,_________________________________________________________________________,
Every Unix system has amount of physical memory (RAM). With this physical memory, Unix doing addressing and break it up into chunks or "pages" of memory.
Pages of memory is the fixed length contiguous block of virtual memory that is the smallest unit of data for memory allocation and transfer [wikipedia].
In linux system, we can get the number of bytes per page using function 'getpagesize()' from standar library, for the example as follow:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Page size: %d\n", getpagesize());
}
So, how about the tools to measure the performances?. I will show you one greath tool to measure the memory and CPU. The tool are:
----[ 1. Virtual Memory Statistic (vmstat)
Collect and display the summary of the virtual memory, paging, and etc in the system. Here are the syntack:
vmstat -a -s -m
Option Explaination
-----------+---------------------------------------------------------------------
-a | Indicates active/inactive amount of memory
-s | Print out the vm table, contains both memory and CPU statistic
-m | Print out the kernel slab info, same as info from /proc/slabinfo,
| This describes full detail how the kernel memory is allocated and
| helpfull to determine area of the kernel consuming the most memory.
-----------+----------------------------------------------------------------------
bash-2.05b$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 511012 14840 4412 642072 33 31 204 247 1110 1548 8 5 73 14
------------------------------------------------------------------------------
At the example above, vmstat display average value for performance statistic. We can see 500 MB is swapped on disk, 14 MB memory is free, 4 MB used for buffer. 642 MB is used for disk cache contain data that had been read off the disk in the past.
bash-2.05b$ vmstat -a
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free inact active si so bi bo in cs us sy id wa
2 1 514004 5640 79816 1341208 33 31 204 247 1111 1548 8 5 73 14
------------------------------------------------------------------------------
Vmstat -a display information the number of active/inactive pages.
~war49~$ vmstat 1 100
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
2 1 131560 2320 8640 53036 1 9 107 69 1137 426 10 7 74 9
0 1 131560 2244 8640 53076 430 0 716 0 1048 207 6 1 0 93
1 2 132476 3424 8592 53272 932 916 1356 916 1259 692 11 4 0 85
1 0 132476 2400 8600 53280 734 8 1040 40 1288 762 14 5 0 81
0 1 133544 2656 8624 53392 444 1068 1096 1068 1217 436 8 3 5 84
0 1 133988 2300 8620 54288 312 344 1796 444 1090 230 5 1 2 92
0 0 134780 3148 8612 53688 0 892 0 792 1040 166 5 1 92 2
0 0 134780 3148 8612 53688 0 0 0 0 1050 158 4 1 95 0
0 0 134780 3148 8612 53688 0 0 0 0 1148 451 7 2 91 0
0 0 134780 3148 8620 53680 0 0 0 12 1196 477 8 2 78 12
....
In the example above, we can see in the swap coloumn. For swap input (si), swap data has been read in at rate 430KB, 932KB, 734KB, 444KB, 312KB, etc. For swap output (so), has been write to swap in rate 9KB, 0KB, 916KB, 8KB, 1068KB, 344KB, 892KB, etc. So, we can assume that system not have enough memory to handle all running process. So, high swap in and swap out can occur simultaneously when a process memory is being saved to make way for application that had been previously saved into disk.
bash-2.05b$ vmstat -s
1552528 total memory
1546692 used memory
1410448 active memory
11100 inactive memory
5836 free memory
2676 buffer memory
645864 swap cache
2097096 total swap
526280 used swap
1570816 free swap
20293225 non-nice user cpu ticks
18284715 nice user cpu ticks
17687435 system cpu ticks
357314699 idle cpu ticks
67673539 IO-wait cpu ticks
352225 IRQ cpu ticks
4872449 softirq cpu ticks
495248623 pages paged in
600129070 pages paged out
19877382 pages swapped in
18874460 pages swapped out
2702803833 interrupts
3763550322 CPU context switches
1094067854 boot time
20158151 forks
We can see the summary of memory and swap, memory total is 1552528 used memory is 1546692 active memory is 1410448 inactive memory is 11100, total swap is 2097096 used swap is 526280 and free swap is 1570816.
bash-2.05b$ vmstat -m
Cache Num Total Size Pages
udf_inode_cache 0 0 416 9
fib6_nodes 7 113 32 113
ip6_dst_cache 9 17 224 17
ndisc_cache 1 24 160 24
raw6_sock 0 0 672 6
udp6_sock 0 0 640 6
tcp6_sock 404 441 1120 7
ip_fib_hash 39 202 16 202
ext3_inode_cache 1714 3632 512 8
...
The linux kernel has slabs (cache), so with the -m option, we can see how does kernel allocates number of cache memory. From the output of vmstat -m above, show number, total (total allocated), size (element) and pages of series of cache, sock, nodes and hash.
----[ 2. Procinfo
Like the vmstat and top, procinfo has similar function. It display statistic of system wide performances ofcourse it will display few memory statistic. Procinfo doesn't has options and arguments, so it would cleanly executed by typed only the file name ./procinfo.
bash-2.05b$ ./procinfo
Linux 2.6.6-1.435.2.3smp (bhcompile@tweety.build.redhat.com) (gcc 3.3.3 20040412 )
#1
1CPU [localhost]
Memory: Total Used Free Shared Buffers
Mem: 320468 308776 11692 0 11604
Swap: 655192 220696 434496
Bootup: Sun Oct 24 10:03:43 2004 Load average: 0.44 0.53 0.51 3/110 32243
user : 0:57:58.92 9.0% page in : 0
nice : 0:02:51.09 0.4% page out: 0
system: 0:20:18.43 3.2% swap in : 0
idle : 8:47:31.54 81.9% swap out: 0
uptime: 10:44:01.94 context : 13368094
irq 0: 38645994 timer irq 7: 2
irq 1: 90516 i8042 irq 8: 1 rtc
irq 2: 0 cascade [4] irq 9: 2
irq 3: 742857 prism2_cs irq 10: 2
irq 4: 6 irq 11: 562551 uhci_hcd, yenta, yen
irq 5: 2 irq 12: 1000803 i8042
irq 6: 8 irq 14: 207681 ide0
----[ 3. Free
Free is a simply tool to display short of memory summarize. With the tool, we can see the total of memory and swap, used memory and swap, free memory and swap, shared memory, buffer and cached. You can use free tool as shown bellow:
bash-2.05b$ free
total used free shared buffers cached
Mem: 385148 234720 150428 0 8016 103932
-/+ buffers/cache: 122772 262376
Swap: 394080 81756 312324
bash-2.05b$ free -t
total used free shared buffers cached
Mem: 385148 247088 138060 0 9052 115024
-/+ buffers/cache: 123012 262136
Swap: 394080 81756 312324
Total: 779228 328844 450384
war49% free -l
total used free shared buffers cached
Mem: 1552528 1546472 6056 0 7544 701408
Low: 897192 892800 4392
High: 655336 653672 1664
-/+ buffers/cache: 837520 715008
Swap: 2097096 566316 1530780
----[ 4. SAR
Sar provides similar information to the other memory statistic tools, such as amount of free memory, buffer, cached and swap. But, it also provide rate of percentage of physical memory and swap is being consumed. The different thing to previous tool is total number of faults that memory subsystem need to fill.
bash-2.05b$ sar -r 1 5
Linux 2.6.8-1.521smp (scrffy) 10/25/2004
09:45:30 AM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad
09:45:31 AM 11732 1022588 98.87 12636 272284 1816140 224104 10.98 66080
09:45:32 AM 10068 1024252 99.03 12660 273300 1816140 224104 10.98 66080
09:45:33 AM 5348 1028972 99.48 12748 275292 1816140 224104 10.98 66080
09:45:35 AM 4932 1029388 99.52 12732 273748 1816140 224104 10.98 66080
09:45:36 AM 6968 1027352 99.33 12724 271876 1815560 224684 11.01 66660
Average: 7810 1026510 99.25 12700 273300 1816024 224220 10.99 66196
bash-2.05b$ sar -R 1 5
Linux 2.6.8-1.521smp (scrffy) 10/25/2004
09:57:22 AM frmpg/s bufpg/s campg/s
09:57:23 AM -81.19 0.00 0.00
09:57:24 AM 8.00 0.00 0.00
09:57:25 AM 0.00 2.02 -2.02
09:57:26 AM 15.84 0.00 0.00
09:57:27 AM -19.80 0.00 64.36
Average: -15.54 0.40 12.55
----[ 5. /proc/meminfo
Linux kernel provide readable text file called meminfo stored in /proc directory. This file contains many information about the memory statistic. Like the previous tool above, this text file contain information about amount of total memory, free memory, swap, and cached. You can display this file using cat, less and more tool as shown bellow:
bash-2.05b$ cat /proc/meminfo
bash-2.05b$ more /proc/meminfo
bash-2.05b$ less /proc/meminfo
Output:
bash-2.05b$ cat /proc/meminfo
MemTotal: 1034320 kB
MemFree: 10788 kB
Buffers: 29692 kB
Cached: 359496 kB
SwapCached: 113912 kB
Active: 704928 kB
Inactive: 222764 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 1034320 kB
LowFree: 10788 kB
SwapTotal: 2040244 kB
SwapFree: 1756832 kB
Dirty: 24 kB
Writeback: 0 kB
Mapped: 604248 kB
Slab: 51352 kB
Committed_AS: 1093856 kB
PageTables: 9560 kB
VmallocTotal: 3088376 kB
VmallocUsed: 26600 kB
VmallocChunk: 3058872 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB