Re: [PATCH] show per-process swap usage via procfs

From: KAMEZAWA Hiroyuki
Date: Thu Nov 05 2009 - 18:50:47 EST


On Fri, 6 Nov 2009 00:11:32 +0900
Minchan Kim <minchan.kim@xxxxxxxxx> wrote:

> Hi, Kame.
>
Hi,

<snip>
> > Â/*
> > @@ -597,7 +600,9 @@ copy_one_pte(struct mm_struct *dst_mm, s
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &src_mm->mmlist);
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âspin_unlock(&mmlist_lock);
> > Â Â Â Â Â Â Â Â Â Â Â Â}
> > - Â Â Â Â Â Â Â Â Â Â Â if (is_write_migration_entry(entry) &&
> > + Â Â Â Â Â Â Â Â Â Â Â if (!is_migration_entry(entry))
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â rss[2]++;
>
> First thought I come to is that we believe !is_migration_entry(entry) equal
> swap entry?
> We began supporting HWPOISON.
> HWPOISON would be rare event so some less exact swap accouting may
> be allowed, I think. Is this enough to jusitfy that?
>
Ah, ok, I'll fix here.


> > + Â Â Â Â Â Â Â Â Â Â Â else if (is_write_migration_entry(entry) &&
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âis_cow_mapping(vm_flags)) {
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â/*
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â * COW mappings require pages in both parent
> > @@ -648,11 +653,11 @@ static int copy_pte_range(struct mm_stru
> > Â Â Â Âpte_t *src_pte, *dst_pte;
> > Â Â Â Âspinlock_t *src_ptl, *dst_ptl;
> > Â Â Â Âint progress = 0;
> > - Â Â Â int rss[2];
> > + Â Â Â int rss[3];
> > Â Â Â Âswp_entry_t entry = (swp_entry_t){0};
> >
> > Âagain:
> > - Â Â Â rss[1] = rss[0] = 0;
> > + Â Â Â rss[2] = rss[1] = rss[0] = 0;
> > Â Â Â Âdst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
> > Â Â Â Âif (!dst_pte)
> > Â Â Â Â Â Â Â Âreturn -ENOMEM;
> > @@ -688,7 +693,7 @@ again:
> > Â Â Â Âarch_leave_lazy_mmu_mode();
> > Â Â Â Âspin_unlock(src_ptl);
> > Â Â Â Âpte_unmap_nested(orig_src_pte);
> > - Â Â Â add_mm_rss(dst_mm, rss[0], rss[1]);
> > + Â Â Â add_mm_rss(dst_mm, rss[0], rss[1], rss[2]);
> > Â Â Â Âpte_unmap_unlock(orig_dst_pte, dst_ptl);
> > Â Â Â Âcond_resched();
> >
> > @@ -818,6 +823,7 @@ static unsigned long zap_pte_range(struc
> > Â Â Â Âspinlock_t *ptl;
> > Â Â Â Âint file_rss = 0;
> > Â Â Â Âint anon_rss = 0;
> > + Â Â Â int swap_usage = 0;
> >
> > Â Â Â Âpte = pte_offset_map_lock(mm, pmd, addr, &ptl);
> > Â Â Â Âarch_enter_lazy_mmu_mode();
> > @@ -887,13 +893,18 @@ static unsigned long zap_pte_range(struc
> > Â Â Â Â Â Â Â Âif (pte_file(ptent)) {
> > Â Â Â Â Â Â Â Â Â Â Â Âif (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âprint_bad_pte(vma, addr, ptent, NULL);
> > - Â Â Â Â Â Â Â } else if
> > - Â Â Â Â Â Â Â Â (unlikely(!free_swap_and_cache(pte_to_swp_entry(ptent))))
> > - Â Â Â Â Â Â Â Â Â Â Â print_bad_pte(vma, addr, ptent, NULL);
> > + Â Â Â Â Â Â Â } else {
> > + Â Â Â Â Â Â Â Â Â Â Â swp_entry_t ent = pte_to_swp_entry(ptent);
> > +
> > + Â Â Â Â Â Â Â Â Â Â Â if (!is_migration_entry(ent))
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â swap_usage--;
>
> ditto
>
ok, will do.


> > + Â Â Â Â Â Â Â Â Â Â Â if (unlikely(!free_swap_and_cache(ent)))
> > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print_bad_pte(vma, addr, ptent, NULL);
> > + Â Â Â Â Â Â Â }
> > Â Â Â Â Â Â Â Âpte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
> > Â Â Â Â} while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
> >
> > - Â Â Â add_mm_rss(mm, file_rss, anon_rss);
> > + Â Â Â add_mm_rss(mm, file_rss, anon_rss, swap_usage);
> > Â Â Â Âarch_leave_lazy_mmu_mode();
> > Â Â Â Âpte_unmap_unlock(pte - 1, ptl);
> >
> > @@ -2595,6 +2606,7 @@ static int do_swap_page(struct mm_struct
> > Â Â Â Â */
> >
> > Â Â Â Âinc_mm_counter(mm, anon_rss);
> > + Â Â Â dec_mm_counter(mm, swap_usage);
> > Â Â Â Âpte = mk_pte(page, vma->vm_page_prot);
> > Â Â Â Âif ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
> > Â Â Â Â Â Â Â Âpte = maybe_mkwrite(pte_mkdirty(pte), vma);
> > Index: mmotm-2.6.32-Nov2/mm/swapfile.c
> > ===================================================================
> > --- mmotm-2.6.32-Nov2.orig/mm/swapfile.c
> > +++ mmotm-2.6.32-Nov2/mm/swapfile.c
> > @@ -837,6 +837,7 @@ static int unuse_pte(struct vm_area_stru
> > Â Â Â Â}
> >
> > Â Â Â Âinc_mm_counter(vma->vm_mm, anon_rss);
> > + Â Â Â dec_mm_counter(vma->vm_mm, swap_usage);
> > Â Â Â Âget_page(page);
> > Â Â Â Âset_pte_at(vma->vm_mm, addr, pte,
> > Â Â Â Â Â Â Â Â Â pte_mkold(mk_pte(page, vma->vm_page_prot)));
> > Index: mmotm-2.6.32-Nov2/fs/proc/task_mmu.c
> > ===================================================================
> > --- mmotm-2.6.32-Nov2.orig/fs/proc/task_mmu.c
> > +++ mmotm-2.6.32-Nov2/fs/proc/task_mmu.c
> > @@ -17,7 +17,7 @@
> > Âvoid task_mem(struct seq_file *m, struct mm_struct *mm)
> > Â{
> > Â Â Â Âunsigned long data, text, lib;
> > - Â Â Â unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
> > + Â Â Â unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss, swap;
> >
> > Â Â Â Â/*
> > Â Â Â Â * Note: to minimize their overhead, mm maintains hiwater_vm and
> > @@ -36,6 +36,7 @@ void task_mem(struct seq_file *m, struct
> > Â Â Â Â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;
> > + Â Â Â swap = get_mm_counter(mm, swap_usage);
> > Â Â Â Âseq_printf(m,
> > Â Â Â Â Â Â Â Â"VmPeak:\t%8lu kB\n"
> > Â Â Â Â Â Â Â Â"VmSize:\t%8lu kB\n"
> > @@ -46,7 +47,8 @@ void task_mem(struct seq_file *m, struct
> > Â Â Â Â Â Â Â Â"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"
> > + Â Â Â Â Â Â Â "VmSwap:\t%8lu kB\n",
> > Â Â Â Â Â Â Â Âhiwater_vm << (PAGE_SHIFT-10),
> > Â Â Â Â Â Â Â Â(total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
> > Â Â Â Â Â Â Â Âmm->locked_vm << (PAGE_SHIFT-10),
> > @@ -54,7 +56,8 @@ void task_mem(struct seq_file *m, struct
> > Â Â Â Â Â Â Â Â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,
> > + Â Â Â Â Â Â Â swap << (PAGE_SHIFT - 10));
> > Â}
> >
> > Âunsigned long task_vsize(struct mm_struct *mm)
> > Index: mmotm-2.6.32-Nov2/mm/rmap.c
> > ===================================================================
> > --- mmotm-2.6.32-Nov2.orig/mm/rmap.c
> > +++ mmotm-2.6.32-Nov2/mm/rmap.c
> > @@ -834,6 +834,7 @@ static int try_to_unmap_one(struct page
> > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âspin_unlock(&mmlist_lock);
> > Â Â Â Â Â Â Â Â Â Â Â Â}
> > Â Â Â Â Â Â Â Â Â Â Â Âdec_mm_counter(mm, anon_rss);
> > + Â Â Â Â Â Â Â Â Â Â Â inc_mm_counter(mm, swap_usage);
> > Â Â Â Â Â Â Â Â} else if (PAGE_MIGRATION) {
> > Â Â Â Â Â Â Â Â Â Â Â Â/*
> > Â Â Â Â Â Â Â Â Â Â Â Â * Store the pfn of the page in a special migration
> > Index: mmotm-2.6.32-Nov2/kernel/fork.c
> > ===================================================================
> > --- mmotm-2.6.32-Nov2.orig/kernel/fork.c
> > +++ mmotm-2.6.32-Nov2/kernel/fork.c
> > @@ -454,6 +454,7 @@ static struct mm_struct * mm_init(struct
> > Â Â Â Âmm->nr_ptes = 0;
> > Â Â Â Âset_mm_counter(mm, file_rss, 0);
> > Â Â Â Âset_mm_counter(mm, anon_rss, 0);
> > + Â Â Â set_mm_counter(mm, swap_usage, 0);
> > Â Â Â Âspin_lock_init(&mm->page_table_lock);
> > Â Â Â Âmm->free_area_cache = TASK_UNMAPPED_BASE;
> > Â Â Â Âmm->cached_hole_size = ~0UL;
> >
> > --
> > To unsubscribe, send a message with 'unsubscribe linux-mm' in
> > the body to majordomo@xxxxxxxxxx ÂFor more info on Linux MM,
> > see: http://www.linux-mm.org/ .
> > Don't email: <a href=mailto:"dont@xxxxxxxxx";> email@xxxxxxxxx </a>
> >
>
> That's good.
> From now on, we can chagne scanning of pte to find swap pte
> in smaps_pte_rangem, too. :)
>

Thanks, I'll update this.
-Kame


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