1 编写代码pslist.c
1: #include<linux/init.h>
2: #include<linux/module.h>
3: #include<linux/list.h>
4: #include<linux/sched.h>
5: #include<linux/proc_fs.h>
6: #include<linux/mm_types.h>
7: #include<linux/fs.h>
8: #include<linux/path.h>
9: #include<linux/dcache.h>
10: #include<linux/mm.h>
11: #include<linux/mmzone.h>
12: #include<linux/vmalloc.h>
13:
14: MODULE_LICENSE("GPL");
15:
16: int analyzeUMANode()
17: {
18: int i,j;
19: unsigned long node_present_pages;
20: unsigned long node_spanned_pages;
21: struct pglist_data* node_0;
22:
23: struct zone* pzone;
24: unsigned long start_pfn;
25: unsigned long present_pages;
26: unsigned long spanned_pages;
27: struct free_area* pfreearea;
28:
29: printk("################################################################### ");
30:
31: node_0 = NODE_DATA(0);
32: printk("node_0 at 0x%08x ", node_0);
33: if (node_0 == NULL)
34: return 0;
35:
36: node_present_pages = node_0->node_present_pages;
37: node_spanned_pages = node_0->node_spanned_pages;
38: printk("present pages: %d ", node_present_pages);
39: printk("spanned pages: %d ", node_spanned_pages);
40:
41: for (i = 0; i < MAX_NR_ZONES; ++i)
42: {
43: pzone = &node_0->node_zones[i];
44: if (pzone == NULL)
45: continue;
46: printk("Zone %d Name: %s ", i, pzone->name);
47: start_pfn = pzone->zone_start_pfn;
48: printk("start_pfn : %d ", start_pfn);
49: present_pages = pzone->present_pages;
50: printk("present_pages : %d ", present_pages);
51: spanned_pages = pzone->spanned_pages;
52: printk("spanned_pages : %d ", spanned_pages);
53: printk("%8d MB - %8d MB ", start_pfn * PAGE_SIZE / 1024 / 1024, (start_pfn + spanned_pages) * PAGE_SIZE / 1024 / 1024);
54: pfreearea = &pzone->free_area;
55: if (pfreearea == NULL)
56: continue;
57: for (j = 0; j < MAX_ORDER; ++j)
58: {
59: printk("%5d", (pfreearea + j)->nr_free);
60: }
61: printk(" ");
62: }
63: return 0;
64: }
65:
66: static int pslist_init()
67: {
68: analyzeUMANode();
69:
70: }
71:
72: static void pslist_exit()
73: {
74: printk("################################################################### ");
75: }
76:
77: module_init(pslist_init);
78: module_exit(pslist_exit);
2 编写Makefile
1: obj-m += pslist.o
2: #generate the path
3: CURRENT_PATH:=$(shell pwd)
4: #the current kernel version number
5: LINUX_KERNEL:=$(shell uname -r)
6: #the absolute path
7: LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
8: #complie object
9: all:
10: make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
11: #clean
12: clean:
13: make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
3 编译
1: sudo make clean
2: sudo make
4 加载模块,卸载模块
1: sudo insmod pslist.ko
2: sudo rmmod pslist
5 收集打印出来的信息
1: dmesg | tail -100
示例如下:
1: [ 4011.124611] ###################################################################
2: [ 4011.124614] node_0 at 0xc17b11c0
3: [ 4011.124615] present pages: 262015
4: [ 4011.124616] spanned pages: 262112
5: [ 4011.124617] Zone 0 Name: DMA
6: [ 4011.124618] start_pfn : 16
7: [ 4011.124619] present_pages : 3951
8: [ 4011.124619] spanned_pages : 4080
9: [ 4011.124620] 0 MB - 16 MB
10: [ 4011.124621] 2 2 1 2 3 2 2 1 2 2 2
11: [ 4011.124624] Zone 1 Name: Normal
12: [ 4011.124625] start_pfn : 4096
13: [ 4011.124626] present_pages : 221486
14: [ 4011.124626] spanned_pages : 223230
15: [ 4011.124627] 16 MB - 887 MB
16: [ 4011.124628] 161 65 150 197 48 54 34 12 7 2 55
17: [ 4011.124631] Zone 2 Name: HighMem
18: [ 4011.124632] start_pfn : 227326
19: [ 4011.124632] present_pages : 34530
20: [ 4011.124633] spanned_pages : 34802
21: [ 4011.124634] 887 MB - 1023 MB
22: [ 4011.124635] 76 98 31 6 1 0 0 0 0 0 0
23: [ 4011.124637] Zone 3 Name: Movable
24: [ 4011.124638] start_pfn : 0
25: [ 4011.124638] present_pages : 0
26: [ 4011.124639] spanned_pages : 0
27: [ 4011.124640] 0 MB - 0 MB
28: [ 4011.124641] 0 0 0 0 0 0 0 0 0 0 0
29: [ 4095.143430] ###################################################################
6 与proc文件系统打印出来的信息做对比
1: $ cat /proc/buddyinfo
2: Node 0, zone DMA 2 2 1 2 3 2 2 1 2 2 2
3: Node 0, zone Normal 371 196 176 229 81 57 39 12 3 3 53
4: Node 0, zone HighMem 76 98 31 6 1 0 0 0 0 0 0