Commands To Understand Page Faults Stats In Linux

January 10, 2013 | By
| Reply More

Virtual memory is the concept when we realize the memory more than physical memory. The big issue with virtual memory is page fault. The basic transfer unit is a fixed length block of data called page. When a process doesn't get a page in main memory, the hardware raises an exception (called page fault) to the software. It happens when a page has been mapped to an address space but not loaded in physical memory. If the requested page does not reside in main memory or CPU cache, the page has to swap from an external storage. It is called major page fault. If the requested page resides in main memory but the process cannot access it because of either uninitialized memory or a COW (copy on write) page, it’s called minor page fault. Here I/O from hard disk is not required so less time is required.

The major and minor page fault can be found with the following commands in Linux.

ps command

ps command reports the status of a process.

[root@redhat-server ~]# ps
PID TTY TIME CMD
2636 pts/0 00:00:00 bash
2827 pts/0 00:00:05 makewhatis
11578 pts/0 00:00:00 makewhatis
11579 pts/0 00:00:00 ps

With -o option, the output can be formatted. Here, we are interested in page faults. The "min_flt" option reports minor faults and "maj_flt" reports major faults.

[root@redhat-server ~]# ps -o pid,min_flt,maj_flt,cmd
PID MINFL MAJFL CMD
2636 2357 0 -bash
2827 146187 0 /bin/bash /usr/sbin/makewhatis
11723 133 0 [makewhatis]
11724 116 0 [gawk]
11725 254 0 ps -o pid,min_flt,maj_flt,cmd

By default ps command displays processes associated with current terminal only. To display all processes, -A option is used.

[root@redhat-server ~]# ps -A -o pid,min_flt,maj_flt,cmd
PID MINFL MAJFL CMD
1 2927 48 init [3]
2 0 0 [migration/0]
3 0 0 [ksoftirqd/0]
4 0 0 [watchdog/0]
5 0 0 [events/0]
6 0 0 [khelper]
---output truncated---

time command

The GNU time command runs a command and reports system resource usage by that command. It also tells about the major and minor page faults. The GNU time command must be run as "/usr/bin/time" and not as just "time", because "time" will run the BASH builtin time command that reports the time of execution of a command.

$ /usr/bin/time ls /etc/passwd
/etc/passwd
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3232maxresident)k
0inputs+0outputs (0major+260minor)pagefaults 0swaps

The -f option can be used to format output.

$ /usr/bin/time -f "(%Fmajor %Rminor)pagefaults" ls /etc/passwd
/etc/passwd
(0major 260minor)pagefaults

%F - major page faults ("Number of major, or I/O-requiring, page faults that occurred while the process was running. These are faults where the page has actually migrated out of primary memory." - from manual page of time command)

%R - minor page faults ("Number of minor, or recoverable, page faults. These are pages that are not valid (so they fault) but which have not yet been claimed by other virtual pages. Thus the data in the page is still valid but the system tables must be updated." - from manual page of time command)

The verbose output is also useful with -v option:

$ /usr/bin/time -v ls /etc/passwd
/etc/passwd
Command being timed: "ls /etc/passwd"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 3216
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 261
Voluntary context switches: 1
Involuntary context switches: 3
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0

$ /usr/bin/time -v cp -r jvm/ Documents/
Command being timed: "cp -r jvm/ Documents/"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.37
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 3312
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 267
Voluntary context switches: 4
Involuntary context switches: 2
Swaps: 0
File system inputs: 24
File system outputs: 16
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0

top command

top command displays Linux tasks. It is an interactive command that provides real-time view of the system. running top command displays the following information:

$ top
top - 13:28:40 up 3:22, 2 users, load average: 0.09, 0.09, 0.07
Tasks: 150 total, 1 running, 148 sleeping, 0 stopped, 1 zombie
Cpu(s): 1.7%us, 1.0%sy, 0.1%ni, 96.0%id, 1.3%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 3058016k total, 968816k used, 2089200k free, 72660k buffers
Swap: 4095996k total, 0k used, 4095996k free, 493212k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1903 raghu 20 0 126m 36m 18m S 2 1.2 1:14.64 plugin-containe
3152 raghu 20 0 2656 1132 844 R 2 0.0 0:00.01 top
1 root 20 0 3044 1848 1284 S 0 0.1 0:00.86 init
2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:01.40 ksoftirqd/0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0
11 root 0 -20 0 0 0 S 0 0.0 0:00.00 cpuset
12 root 0 -20 0 0 0 S 0 0.0 0:00.00 khelper
13 root 0 -20 0 0 0 S 0 0.0 0:00.00 netns
15 root 20 0 0 0 0 S 0 0.0 0:00.00 sync_supers
16 root 20 0 0 0 0 S 0 0.0 0:00.00 bdi-default
17 root 0 -20 0 0 0 S 0 0.0 0:00.00 kintegrityd
18 root 0 -20 0 0 0 S 0 0.0 0:00.00 kblockd
19 root 0 -20 0 0 0 S 0 0.0 0:00.00 kacpid
20 root 0 -20 0 0 0 S 0 0.0 0:00.00 kacpi_notify
21 root 0 -20 0 0 0 S 0 0.0 0:00.00 kacpi_hotplug
22 root 0 -20 0 0 0 S 0 0.0 0:00.00 ata_sff
23 root 20 0 0 0 0 S 0 0.0 0:00.00 khubd
24 root 0 -20 0 0 0 S 0 0.0 0:00.00 md
26 root 20 0 0 0 0 S 0 0.0 0:00.00 khungtaskd
27 root 20 0 0 0 0 S 0 0.0 0:00.00 kswapd0
28 root 25 5 0 0 0 S 0 0.0 0:00.00 ksmd
29 root 20 0 0 0 0 S 0 0.0 0:00.00 fsnotify_mark
30 root 0 -20 0 0 0 S 0 0.0 0:00.00 aio
31 root 20 0 0 0 0 S 0 0.0 0:00.00 ecryptfs-kthrea
32 root 0 -20 0 0 0 S 0 0.0 0:00.00 crypto
36 root 0 -20 0 0 0 S 0 0.0 0:00.00 kthrotld
44 root 20 0 0 0 0 S 0 0.0 0:00.00 scsi_eh_0
45 root 20 0 0 0 0 S 0 0.0 0:00.34 scsi_eh_1
46 root 20 0 0 0 0 S 0 0.0 0:00.03 scsi_eh_2
47 root 20 0 0 0 0 S 0 0.0 0:00.03 scsi_eh_3
50 root 0 -20 0 0 0 S 0 0.0 0:00.00 kmpathd

Hit 'f' to select fields manually, i.e. to add or remove fields from the above list. Pressing 'f' displays the following screen:

Current Fields: AEHIOQTWKNMbcdfgjplrsuvyzX for window 1:Def
Toggle fields via field letter, type any other key to return

* A: PID = Process Id 0x00000040 PF_FORKNOEXEC
* E: USER = User Name 0x00000100 PF_SUPERPRIV
* H: PR = Priority 0x00000200 PF_DUMPCORE
* I: NI = Nice value 0x00000400 PF_SIGNALED
* O: VIRT = Virtual Image (kb) 0x00000800 PF_MEMALLOC
* Q: RES = Resident size (kb) 0x00002000 PF_FREE_PAGES (2.5)
* T: SHR = Shared Mem size (kb) 0x00008000 debug flag (2.5)
* W: S = Process Status 0x00024000 special threads (2.5)
* K: %CPU = CPU usage 0x001D0000 special states (2.5)
* N: %MEM = Memory usage (RES) 0x00100000 PF_USEDFPU (thru 2.4)
* M: TIME+ = CPU Time, hundredths
b: PPID = Parent Process Pid
c: RUSER = Real user name
d: UID = User Id
f: GROUP = Group Name
g: TTY = Controlling Tty
j: P = Last used cpu (SMP)
p: SWAP = Swapped size (kb)
l: TIME = CPU Time
r: CODE = Code size (kb)
s: DATA = Data+Stack size (kb)
u: nFLT = Page Fault count
v: nDRT = Dirty Pages count
y: WCHAN = Sleeping in Function
z: Flags = Task Flags
* X: COMMAND = Command name/line

Flags field:
0x00000001 PF_ALIGNWARN
0x00000002 PF_STARTING
0x00000004 PF_EXITING

Now, the field (column) for page faults is "nFLT", that can be toggled with 'u' key as specified in the above output. The "nDRT" tells dirty page count, i.e. the number of pages that have been modified since they were last written to disk. It can be toggled with 'v' key. After toggling these two fields, top displays the following:

top - 13:42:32 up 3:36, 2 users, load average: 0.00, 0.02, 0.05
Tasks: 148 total, 2 running, 145 sleeping, 0 stopped, 1 zombie
Cpu(s): 2.0%us, 1.7%sy, 0.0%ni, 96.2%id, 0.0%wa, 0.0%hi, 0.2%si, 0.0%st
Mem: 3058016k total, 978396k used, 2079620k free, 73936k buffers
Swap: 4095996k total, 0k used, 4095996k free, 499432k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ nFLT nDRT COMMAND
1069 root 20 0 88628 24m 11m S 3 0.8 1:15.12 54 0 Xorg
3093 raghu 20 0 94044 14m 10m S 2 0.5 0:02.34 1 0 gnome-terminal
1903 raghu 20 0 126m 37m 18m S 1 1.3 1:26.27 17 0 plugin-containe
3177 raghu 20 0 2660 1132 836 R 1 0.0 0:00.09 0 0 top
1573 raghu 20 0 74740 16m 9m S 0 0.6 0:20.45 35 0 compiz
1843 raghu 20 0 465m 155m 31m S 0 5.2 1:00.59 43 0 firefox
1847 raghu 20 0 77084 30m 10m S 0 1.0 0:11.58 16 0 ubuntuone-syncd
2331 root 20 0 0 0 0 R 0 0.0 0:02.13 0 0 kworker/0:0
1 root 20 0 3044 1848 1284 S 0 0.1 0:00.86 23 0 init
2 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:03.11 0 0 ksoftirqd/0
6 root RT 0 0 0 0 S 0 0.0 0:00.00 0 0 migration/0
11 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 cpuset
12 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 khelper
13 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 netns
15 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 sync_supers
16 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 bdi-default
17 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 kintegrityd
18 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 kblockd
19 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 kacpid
20 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 kacpi_notify
21 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 kacpi_hotplug
22 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 ata_sff
23 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 khubd
24 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 md
26 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 khungtaskd
27 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 kswapd0
28 root 25 5 0 0 0 S 0 0.0 0:00.00 0 0 ksmd
29 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 fsnotify_mark
30 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 aio
31 root 20 0 0 0 0 S 0 0.0 0:00.00 0 0 ecryptfs-kthrea
32 root 0 -20 0 0 0 S 0 0.0 0:00.00 0 0 crypto

File /proc/vmstat

The pgfault value in /proc/vmstat report the virtual memory statistics. This can be extracted using grep command

$ grep pgfault /proc/vmstat
pgfault 5229281

File /proc/PID/stat

The process information can be looked into /proc directory. This directory contains a directory for each process with the name of PID of that process. Under that directory, stat file contains statistics of the process. Following fields are of interest:

Field 1 -
Field 2 - filename of the executable
Field 10 - number of minor page faults
Field 12 - number of major page faults

These fields can be extracted uding cut command:

$ cut -d " " -f 1,2,10,12 /proc/1/stat
1 (init) 6798 22

$ cut -d " " -f 1,2,10,12 /proc/1874/stat
1874 (firefox) 840829 67

$ cut -d " " -f 1,2,10,12 /proc/4691/stat
4691 (bash) 2146 0

Filed Under : LINUX COMMANDS, LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.