[LWN Logo]

Date:	Fri, 24 Mar 2000 13:20:14 -0500
From:	"Pat O'Rourke" <orourke@mail.mclinux.com>
To:	linux-kernel@vger.rutgers.edu
Subject: [PATCH] Add fault statistics to /proc/stat

This patch is based on 2.3.99-pre2 and appends various page fault information
to /proc/stat. Here is an example of the proposed new fault line in the
/proc/stat file:

> faults: 3994205 1239306 0 1764606 554473 0

These numbers represent: total faults, major faults (i.e. page retrieved from
file system), minor faults (i.e. a fault not covered by a zfod, cow or swap
reclaim), zero fill on demand faults (zfod), copy on write (cow) faults and
pages reclaimed from the swap cache respectively.

This data can be reported by the vmstat command to report the system's fault
activity.  One possibility would be to add a '-f' option to vmstat as follows
(note: the memory / fault information below is reported in units of pages):

[root@test /root]# vmstat -f 1
   procs  pages (page =  4kb)                       faults    system         cpu
 r  b  w  tot free  buf cach  tot  maj  min zfod  cow   re   in   cs  us  sy  id
22  0  0 988k 334k   1k  20k   4m   1m    5   1m 669k  634   39   96  47   5  48
18  0  0 988k 330k   1k  20k   9k   1k    0   4k   1k    0  112  639  95   5   0
18  2  0 988k 332k   1k  20k   8k   1k    0   3k   2k    0  109  561  95   5   0

diff -u --new-file --recursive linux-2.3.99-pre2/fs/proc/proc_misc.c kstat-99/fs/proc/proc_misc.c
--- linux-2.3.99-pre2/fs/proc/proc_misc.c	Fri Mar  3 01:46:07 2000
+++ kstat-99/fs/proc/proc_misc.c	Fri Mar 24 10:36:08 2000
@@ -356,6 +356,9 @@
 		kstat.context_swtch,
 		xtime.tv_sec - jif / HZ,
 		total_forks);
+	len += sprintf(page + len, "faults: %u %u %u %u %u %u\n",
+		kstat.total_faults, kstat.major_faults, kstat.minor_faults,
+		kstat.zfod_faults, kstat.cow_faults, kstat.reclaims);
 	if (len <= off+count) *eof = 1;
 	*start = page + off;
 	len -= off;
diff -u --new-file --recursive linux-2.3.99-pre2/include/linux/kernel_stat.h kstat-99/include/linux/kernel_stat.h
--- linux-2.3.99-pre2/include/linux/kernel_stat.h	Wed Mar 22 10:27:13 2000
+++ kstat-99/include/linux/kernel_stat.h	Fri Mar 24 11:09:58 2000
@@ -30,6 +30,12 @@
 	unsigned int ierrors, oerrors;
 	unsigned int collisions;
 	unsigned int context_swtch;
+	unsigned int total_faults;
+	unsigned int major_faults;
+	unsigned int minor_faults;
+	unsigned int zfod_faults;
+	unsigned int cow_faults;
+	unsigned int reclaims;
 };
 
 extern struct kernel_stat kstat;
diff -u --new-file --recursive linux-2.3.99-pre2/mm/memory.c kstat-99/mm/memory.c
--- linux-2.3.99-pre2/mm/memory.c	Wed Mar 15 12:59:06 2000
+++ kstat-99/mm/memory.c	Fri Mar 24 10:59:49 2000
@@ -46,6 +46,7 @@
 #include <asm/pgalloc.h>
 #include <linux/highmem.h>
 #include <linux/pagemap.h>
+#include <linux/kernel_stat.h>
 
 
 unsigned long max_mapnr = 0;
@@ -862,6 +863,7 @@
 	}
 	spin_unlock(&tsk->mm->page_table_lock);
 	__free_page(new_page);
+	++kstat.cow_faults;
 	return 1;
 
 bad_wp_page:
@@ -1028,6 +1030,8 @@
 
 		flush_page_to_ram(page);
 		flush_icache_page(vma, page);
+	} else {
+		++kstat.reclaims;
 	}
 
 	vma->vm_mm->rss++;
@@ -1082,6 +1086,7 @@
 	set_pte(page_table, entry);
 	/* No need to invalidate - it was non-present before */
 	update_mmu_cache(vma, addr, entry);
+	++kstat.zfod_faults;
 	return 1;
 }
 
@@ -1117,6 +1122,7 @@
 		return -1;
 	++tsk->maj_flt;
 	++vma->vm_mm->rss;
+	++kstat.major_faults;
 	/*
 	 * This silly early PAGE_DIRTY setting removes a race
 	 * due to the bad i386 page protection. But it's valid
@@ -1189,6 +1195,7 @@
 		establish_pte(vma, address, pte, entry);
 	}
 	spin_unlock(&tsk->mm->page_table_lock);
+	++kstat.minor_faults;
 	return 1;
 }
 
@@ -1201,6 +1208,8 @@
 	int ret = -1;
 	pgd_t *pgd;
 	pmd_t *pmd;
+
+	kstat.total_faults++;
 
 	pgd = pgd_offset(vma->vm_mm, address);
 	pmd = pmd_alloc(pgd, address);

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/