counting swapped out pages from certain task

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi guys,

Recently, I did a personal kernel hack simply to count how many 
anonymous pages that is currently swapped out. Please take a look on 
the embedded patch after the end of this message. The patch is made on 
top of 2.6.16.1 and tested inside Qemu 0.7.2 using FC2 disk image.

So far, this patch doesn't work as expected.  First I check the counter 
when nothing swaps out (SwpOut shows zero for all tasks). Then I ran a 
memory hogger application (such as "tail -f /dev/zero") to force other 
applications to be swapped out partially, then I stopped the mem-hogger 
application (since I don't need to fully occupy the RAM anyway). Again, 
I checked the counters and some task's counters showed non-zero numbers 
(meaning they were swapped out).

The quirk happened here. I disabled the swap areas (swapoff -a) and 
AFAIK this would force the kernel to swap-in all the currently swapped 
out pages. I re-checked the counters and strangely the swapped counters 
didn't go back to zero. What did I do wrong here?

Any suggestions are welcome...

regards,

Mulyadi.

 fs/proc/task_mmu.c |   14 ++++++++++++--
 include/linux/mm.h |    1 +
 mm/memory.c        |    2 ++
 mm/rmap.c          |    1 +
 4 files changed, 16 insertions(+), 2 deletions(-)

--- fs/proc/task_mmu.c.bak	2006-03-28 13:49:02.000000000 +0700
+++ fs/proc/task_mmu.c	2006-07-22 19:18:33.000000000 +0700
@@ -15,6 +15,8 @@ char *task_mem(struct mm_struct *mm, cha
 {
 	unsigned long data, text, lib;
 	unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
+	struct vm_area_struct * this_vma;
+	unsigned long swp_out_pages=0;
 
 	/*
 	 * Note: to minimize their overhead, mm maintains hiwater_vm and
@@ -33,6 +35,12 @@ char *task_mem(struct mm_struct *mm, cha
 	data = mm->total_vm - mm->shared_vm - mm->stack_vm;
 	text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 
10;
 	lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
+
+	for(this_vma = mm->mmap;this_vma;this_vma = this_vma->vm_next)
+	{
+		swp_out_pages += atomic_read(&(this_vma)->swapped_out);
+	}
+
 	buffer += sprintf(buffer,
 		"VmPeak:\t%8lu kB\n"
 		"VmSize:\t%8lu kB\n"
@@ -43,7 +51,8 @@ char *task_mem(struct mm_struct *mm, cha
 		"VmStk:\t%8lu kB\n"
 		"VmExe:\t%8lu kB\n"
 		"VmLib:\t%8lu kB\n"
-		"VmPTE:\t%8lu kB\n",
+		"VmPTE:\t%8lu kB\n"
+		"VmSwp:\t%8lu kB\n",
 		hiwater_vm << (PAGE_SHIFT-10),
 		(total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
 		mm->locked_vm << (PAGE_SHIFT-10),
@@ -51,7 +60,8 @@ char *task_mem(struct mm_struct *mm, cha
 		total_rss << (PAGE_SHIFT-10),
 		data << (PAGE_SHIFT-10),
 		mm->stack_vm << (PAGE_SHIFT-10), text, lib,
-		(PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10);
+		(PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
+		swp_out_pages << (PAGE_SHIFT-10) );
 	return buffer;
 }
 
--- include/linux/mm.h.bak	2006-03-28 13:49:02.000000000 +0700
+++ include/linux/mm.h	2006-07-22 19:20:34.000000000 +0700
@@ -111,6 +111,7 @@ struct vm_area_struct {
 #ifdef CONFIG_NUMA
 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
 #endif
+	atomic_t  swapped_out;	/* swapped out pages in this VMA */
 };
 
 /*
--- mm/rmap.c.bak	2006-03-28 13:49:02.000000000 +0700
+++ mm/rmap.c	2006-07-22 19:19:47.000000000 +0700
@@ -635,6 +635,7 @@ static int try_to_unmap_one(struct page 
 		set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
 		BUG_ON(pte_file(*pte));
 		dec_mm_counter(mm, anon_rss);
+		atomic_inc(&(vma)->swapped_out);
 	} else
 		dec_mm_counter(mm, file_rss);
 
--- mm/memory.c.bak	2006-03-28 13:49:02.000000000 +0700
+++ mm/memory.c	2006-07-22 19:20:10.000000000 +0700
@@ -1928,6 +1928,8 @@ again:
 	/* The page isn't present yet, go ahead with the fault. */
 
 	inc_mm_counter(mm, anon_rss);
+	printk("Reducing swapped_out variable...\n");
+	atomic_dec(&(vma)->swapped_out);
 	pte = mk_pte(page, vma->vm_page_prot);
 	if (write_access && can_share_swap_page(page)) {
 		pte = maybe_mkwrite(pte_mkdirty(pte), vma);


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux