最近有同事在读<>一文,我也看了下,写得不错,满通俗易懂的,但是其中关于
进程mem map 显示的东西,好象没提供,我搜了下只找到个显示全部mem map 的,于是自己写了一个,
虽然代码很小,但写+调试也花了半天时间,废话少说 ,具体代码如下:
mem_map.c :
- #include <linux/kernel_stat.h>
- #include <linux/mm.h>
- #include <linux/hugetlb.h>
- #include <linux/mman.h>
- #include <linux/swap.h>
- #include <linux/highmem.h>
- #include <linux/pagemap.h>
- #include <linux/rmap.h>
- #include <linux/module.h>
- #include <linux/delayacct.h>
- #include <linux/init.h>
- #include <linux/writeback.h>
- #include <linux/memcontrol.h>
- #include <linux/mmu_notifier.h>
- #include <linux/kallsyms.h>
- #include <linux/swapops.h>
- #include <linux/elf.h>
- #include <asm/pgalloc.h>
- #include <asm/uaccess.h>
- #include <asm/tlb.h>
- #include <asm/tlbflush.h>
- #include <asm/pgtable.h>
- #define liuprintk printk
- unsigned short *proc_memmap;
- int msize;
- struct page *vm_get_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
- {
- unsigned long pfn = pte_pfn(pte);
- if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
- liuprintk("\nliutest: has pure pfn \n");
- #if 0
- if (vma->vm_flags & VM_MIXEDMAP) {
- if (!pfn_valid(pfn))
- return NULL; /* invalid page */
- goto out;
- } else {
- unsigned long off;
- off = (addr - vma->vm_start) >> PAGE_SHIFT; /* VM_PFNMAP 的处理*/
- if (pfn == vma->vm_pgoff + off)
- return NULL;
- if (!is_cow_mapping(vma->vm_flags))
- return NULL;
- }
- #endif
- }
-
- //out:
- return pfn_to_page(pfn);
- }
- static inline void
- show_one_pte(struct mm_struct *mm, pte_t *src_pte, struct vm_area_struct *vma,
- unsigned long addr) /* liunote linux pte */
- {
- unsigned long vm_flags = vma->vm_flags;
- pte_t pte = *src_pte; /* get pte val */
- struct page *page;
- int pm_index;
- if (unlikely(!pte_present(pte))) {
-
- if (!pte_file(pte)) { /* liunote anymous page */
- /* liunote can do it best */
- }
- goto out_show_pte;
- }
- page = vm_get_page(vma, addr, pte); /* 是normal page */
- if (page) {
- /* liunote get share proc num */
- pm_index = (addr - vma->vm_start) /PAGE_SIZE;
- proc_memmap[pm_index] = atomic_read(&(page)->_mapcount);
- //printk("liutest pmd_index=0x%X,addr= 0x%X,vmstart=0x%X:,mapcount=%d", pm_index ,addr , vma->vm_start,atomic_read(&(page)->_mapcount));
- if (proc_memmap[pm_index]==0){
- proc_memmap[pm_index] =1;
- }
- }
- out_show_pte:
- return;
- }
- static int show_ptes(struct mm_struct *mm, pmd_t *pmd, struct vm_area_struct *vma,
- unsigned long addr, unsigned long end)
- {
- pte_t *pte;
- spinlock_t *ptl;
- /* liunote below dst_pte is linux pte ??? */
- pte = pte_offset_map_nested(pmd, addr);
- ptl = pte_lockptr(mm, pmd);
- /* liunote need lock to read it ??? */
- /* spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);*/
-
- do {
- if (pte_none(*pte)) { /* src_pte = 0 */
- //liuprintk("\nliutest: bad pte=0x%X,addr=0x%X\n",pte,addr);
- continue;
- }
- show_one_pte(mm, pte, vma, addr);
- } while (pte++, addr += PAGE_SIZE, addr != end);
- /* spin_unlock(ptl); */
- return 0;
- }
- static inline int show_pmds(struct mm_struct *mm, pud_t *pud, struct vm_area_struct *vma,
- unsigned long addr, unsigned long end)
- {
- pmd_t * pmd;
- unsigned long next;
- pmd = pmd_offset(pud, addr);
- do {
- next = pmd_addr_end(addr, end);
- if (pmd_none_or_clear_bad(pmd)){
- liuprintk("\nliutest: bad pmd=0x%X,addr=0x%X\n",pmd,addr);
- continue;
- }
- if (show_ptes(mm, pmd,
- vma, addr, next))
- return -ENOMEM;
- } while (pmd++, addr = next, addr != end);
- return 0;
- }
- static inline int show_puds(struct mm_struct *mm, pgd_t *pgd, struct vm_area_struct *vma,
- unsigned long addr, unsigned long end)
- {
- pud_t *pud ;
- unsigned long next;
- pud = pud_offset(pgd, addr);
- do {
- next = pud_addr_end(addr, end);
- if (pud_none_or_clear_bad(pud)){
- liuprintk("\n liutest: bad pud=0x%X,addr=0x%X\n",pud,addr);
- continue;
- }
- if (show_pmds(mm, pud,
- vma, addr, next))
- return -ENOMEM;
- } while (pud++, addr = next, addr != end);
- return 0;
- }
- // liuadd for print pte
- int show_pages(struct mm_struct * mm, struct vm_area_struct *vma)
- {
- pgd_t *pgd;
- unsigned long next;
- unsigned long addr = vma->vm_start;
- unsigned long end = vma->vm_end;
- int ret,i;
- int pages =0;
- // according vm size ,malloc proc memmap
- msize = end-addr;
- msize = (msize & (PAGE_SIZE-1)) ? (msize+PAGE_SIZE)/PAGE_SIZE : msize/PAGE_SIZE;
-
- liuprintk("\nliutest: msize =0x%X, PAGE_SIZE=0x%X\n",msize,PAGE_SIZE);
-
- proc_memmap = kmalloc( msize*(sizeof(unsigned short)),GFP_KERNEL);
- if(proc_memmap == NULL){
- printk("\n liutest : proc mem map alloc fail \n");
- return 1;
- }
- for(i=0; i<msize;i++)
- proc_memmap[i] = 0;
- /*
- if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
- if (!vma->anon_vma)
- return 0;
- }
- */
- ret = 0;
- pgd = pgd_offset(mm, addr);
- do {
- next = pgd_addr_end(addr, end);
- if (pgd_none_or_clear_bad(pgd)){
- liuprintk("\nliutest: bad pgd=0x%X,addr=0x%X\n",pgd,addr);
- continue;
- }
- if (unlikely(show_puds(mm, pgd, vma, addr, next))) {
- ret = -ENOMEM;
- break;
- }
- } while (pgd++, addr = next, addr != end);
- #if 1
- for(i=0; i<msize;i++)
- if(proc_memmap[i])
- pages++;
- printk("\nliutest: this vm area has pages =0x%X\n",pages);
-
- // free memmap
- kfree(proc_memmap);
- #endif
- return ret;
- }
- #undef liuprintk
上面mem_map.c 文件加到proc 目录下
然后在task_mmu.c show_map_vma 函数末尾加上:
show_pages(vma->vm_mm, vma);
proc 下面Makefile 加上 mem_map.o
代码有点乱,不过测试过还行,内容全在proc_memmap 数组里,大小为msize,
可根据自己喜好显示从proc 输出
下面是测试busybox 内存情况
Cd proc
Cd 1
Cat maps 显示如下:
00008000-00100000 r-xp 00000000 00:01 60 /bin/busybox
Pages:0xF8-0x4D ( F8 页, 实际占用4D页 ,下面是实际页内存引用情况)
0x0002 0x0002 0x0000 0x0000 0x0002 0x0002 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0000 0x0001 0x0001 0x0000 0x0000 0x0000
0x0000 0x0000 0x0000 0x0000 0x0002 0x0000 0x0002 0x0000 0x0000 0x0002 0x0002 0x0002 0x0002 0x0000 0x0000 0x0000 0x0000 0x0001 0x0001 0x0001 0x0001 0x0000 0x0000 0x0000 0x0001 0x0001 0x0000 0x0000 0x0001 0x0001 0x0001 0x0001
0x0001 0x0001 0x0002 0x0002 0x0002 0x0002 0x0002 0x0001 0x0002 0x0002 0x0001 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0002 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0002 0x0000 0x0000 0x0002 0x0002 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0002 0x0002
0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0002 0x0001 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0000 0x0000 0x0000 0x0002 0x0000 0x0000 0x0000 0x0000 0x0000
0x0000 0x0002 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0002 0x0001 0x0002 0x0000 0x0002 0x0000 0x0001 0x0001 0x0000 0x0000 0x0000
0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0000 0x0000 0x0002 0x0002 0x0000 0x0002 0x0002 0x0000 0x0000 0x0002 0x0000 0x0001 0x0001 0x0001 0x0001 0x0000 0x0000
00107000-00108000 rw-p 000f7000 00:01 60 /bin/busybox
Pages:0x1-0x1
0x0001
00108000-0012e000 rw-p 00108000 00:00 0 [heap]
Pages:0x26-0x6
0x0001 0x0001 0x0000 0x0001 0x0001 0x0001 0x0001 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
0x0000 0x0000 0x0000 0x0000 0x0000 0x0000
40000000-40001000 rw-p 40000000 00:00 0
Pages:0x1-0x1
0x0001
be844000-be859000 rw-p befeb000 00:00 0 [stack]
Pages:0x15-0x2 (这是栈,有15页 , 实际占用2页 ,下面也可以看出,占了倒数的两页符合栈特性,)
0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001 0x0001
阅读(1601) | 评论(0) | 转发(0) |