Virtual memory is the concept when we realize the memory used 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.
Using ps command
Linux ps command with
-o option we can use to print page faults.
Here, we are interested in page faults. The 'min_flt' option reports minor faults and 'maj_flt' reports major faults.
# 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 the current terminal only. To display all processes,
-A option is used.
# ps -A -o pid,min_flt,maj_flt,cmd PID MINFL MAJFL CMD 1 2927 48 init  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---
Using time command
The GNU time command runs a command and reports system resource usage by that command. It can 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 built-in 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
-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
$ /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
Using top command
Top command is an interactive command that provides a real-time view of the system.
f to select fields manually, i.e. to add or remove fields from the above list.
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
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
After toggling these two fields, top displays as 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
The pgfault value in '/proc/vmstat' report the virtual memory statistics. Page fault value can be extracted using grep command.
$ grep pgfault /proc/vmstat pgfault 5229281
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 using 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
In this tutorial, we learned the ways to find page faults using linux basic commands. Let me know your suggestions on the below comment box.