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("###################################################################\n");

  30:  

  31:     node_0 = NODE_DATA(0);

  32:     printk("node_0 at 0x%08x\n", 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\n", node_present_pages);

  39:     printk("spanned pages: %d\n", 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\n", i, pzone->name);

  47:         start_pfn = pzone->zone_start_pfn;

  48:         printk("start_pfn : %d\n", start_pfn);

  49:         present_pages = pzone->present_pages;

  50:         printk("present_pages : %d\n", present_pages);

  51:         spanned_pages = pzone->spanned_pages;

  52:         printk("spanned_pages : %d\n", spanned_pages);

  53:         printk("%8d MB - %8d MB\n", 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("\n");

  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("###################################################################\n");

  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 

自己写Linux module来收集buddy info的更多相关文章

  1. <linux/init.h>,<linux/module.h>头文件不存在等问题的解决方法

    这个问题真心是处理了一个下午,还自己去下载了个最新的内核拿来编译,其实是完全没必要的,因为ubuntu系统是可以直接下载新内核的. 你可以在/usr/src/文件夹下找到这些内核文件夹,比如说我自己的 ...

  2. 手把手教你写Linux设备驱动---中断(三)--workqueue实现(基于友善之臂4412开发板) 【转】

    转自:http://blog.csdn.net/morixinguan/article/details/69680909 上节,我们讲到如何来实现tasklet小任务机制 http://blog.cs ...

  3. 超简单易用的 “在 pcduino 开发板上写 Linux 驱动控制板载 LED 的闪烁”

    版权声明:本文为博主原创文章,未经博主同意不得转载.转载联系 QQ 30952589,加好友请注明来意. https://blog.csdn.net/sleks/article/details/251 ...

  4. Linux Module

    catalog . 概述 . 使用模块 . 插入和删除模块 . 自动化与热插拔 . 版本控制 1. 概述 模块(module)是一种向Linux内核添加设备驱动程序.文件系统及其他组件的有效方法,而无 ...

  5. linux/module.h: No such file or directory 内核模块编译过程

    1.缺少Linux kernel头文件 To install just the headers in Ubuntu: sudo apt-get install linux-headers-$(unam ...

  6. linux Module驱动开发-一切刚刚开始

    linux内核是可以高度定制的,通过配置编译选项达到定制的目的. 在配置kernel编译选项时驱动程序的编译选项一般有三种,不编译.编译为内核驱动.编译为模块驱动.所以linux驱动一般分为两类,内核 ...

  7. My First Linux Module

    My First Linux Module Today, I successfully build my first linux hello module. First of all add a di ...

  8. Linux Module框架【转】

    转自:http://www.cnblogs.com/LittleHann/p/4558719.html catalog 1. 概述 2. 使用模块 3. 插入和删除模块 4. 自动化与热插拔 5. 版 ...

  9. 教你写Linux设备驱动程序:一个简短的教程

    教你写Linux设备驱动程序:一个简短的教程 http://blog.chinaunix.net/uid-20799298-id-99675.html

随机推荐

  1. “void * __cdecl operator new(unsigned int)”(??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)

    转自VC错误:http://www.vcerror.com/?p=1377 问题描述: 当 C 运行时 (CRT) 库和 Microsoft 基础类 (MFC) 库的链接顺序有误时,可能会出现以下 L ...

  2. ASP.NET Core学习——7

    多环境ASP.NET Core介绍了支持在多种环境中管理应用程序行为的改进,如开发(devlopment),预演(staging)和生成(production).环境变量用来指示应用程序正在运行的环境 ...

  3. strcpy函数学习

    strcpy的功能如下: 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #includ ...

  4. HTML5: HTML5 Web 存储

    ylbtech-HTML5: HTML5 Web 存储 1.返回顶部 1. HTML5 Web 存储 HTML5 web 存储,一个比cookie更好的本地存储方式. 什么是 HTML5 Web 存储 ...

  5. quartz的初步总结及配置优化

    1.scheduler 1. Scheduler就是Quartz的大脑,所有任务都是由它来设施.Scheduler包含一个两个重要组件: JobStore和ThreadPool.JobStore是会来 ...

  6. 对于Final关键字的总结

    1.final关键字可以用于成员变量.本地变量.方法以及类. 2. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. 3. 你不能够对final变量再次赋值. 4.  ...

  7. PAT甲级——A1130 Infix Expression【25】

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  8. 无法启动此程序 ,因为计算机中丢失MSVCP120.dll

    1.文件丢失问题 无法启动此程序 ,因为计算机中丢失MSVCP120.dll 具体如下图所示: 等dll文件丢失,可以去下载 DirectX修复工具去修复即可 http://www.pc6.com/s ...

  9. 每天一个Linux常用命令 cp命令

    Linux cp命令主要用于复制文件或目录 -a:此选项通常在复制目录时使用,它保留链接.文件属性,并复制目录下的所有内容.其作用等于dpR参数组合. -d:复制时保留链接.这里所说的链接相当于Win ...

  10. celery使用多队列

    生产者: 文件1: 定义任务 #!/usr/bin/env python3 # coding: utf-8 from celery import Celery import settings pw = ...