Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1262998
  • 博文数量: 185
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 1418
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-02 15:12
个人简介

治肾虚不含糖,专注内核性能优化二十年。 https://github.com/KnightKu

文章分类

全部博文(185)

文章存档

2019年(1)

2018年(12)

2017年(5)

2016年(23)

2015年(1)

2014年(22)

2013年(82)

2012年(39)

分类: LINUX

2013-05-13 14:37:21

前几天写的一个demo,没几行代码,但是涵盖的东西比较多:
撰写模块,创建内核线程,节点及cpu亲缘性设置,内核态获取时间,内核态分配大内存,时间处理等等...
已备后用。

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kernel.h>
  4. #include <linux/jiffies.h>
  5. #include <linux/sched.h>
  6. #include <linux/kthread.h>

  7. MODULE_LICENSE("GPL");
  8. MODULE_AUTHOR("Gu Zheng ");

  9. #define COUNT 5*1024*1024
  10. #define SIZE 4*1024

  11. void *addrs[COUNT];

  12. static int mem_alloc(void)
  13. {
  14.     int i = 0;
  15.     while (i < COUNT) {
  16.         addrs[i] = vmalloc_node(SIZE, 1);
  17.         if (addrs[i] == NULL)
  18.             break;
  19.         i++;
  20.     }
  21.     return i;
  22. }

  23. static void write_node1_mem(void)
  24. {
  25.     int i = 0;
  26.     while (i < COUNT) {
  27.         memset(addrs[i], 1, SIZE);
  28.         i++;
  29.     }
  30. }

  31. static void mem_free(void)
  32. {
  33.     int i = 0;
  34.     while (i < COUNT) {
  35.         vfree(addrs[i]);
  36.         i++;
  37.     }
  38. }

  39. static struct timeval timeval_sub(struct timeval lhs, struct timeval rhs)
  40. {
  41.     struct timeval tv_delta;
  42.     __kernel_time_t sec = lhs.tv_sec - rhs.tv_sec;
  43.     __kernel_suseconds_t usec = lhs.tv_usec - rhs.tv_usec;
  44.     if (usec < 0) {
  45.         usec += USEC_PER_SEC;
  46.         --sec;
  47.     }
  48.     tv_delta.tv_sec = sec;
  49.     tv_delta.tv_usec = usec;
  50.     return tv_delta;
  51. }

  52. static int test_func(void *data)
  53. {
  54.     unsigned long jiff_1, jiff_2;
  55.     struct timeval time_start, time_end, time_cost;
  56.     int count = 0;
  57.     
  58.     jiff_1 = (unsigned long)jiffies;
  59.     do_gettimeofday(&time_start);
  60.     count = mem_alloc();
  61.     do_gettimeofday(&time_end);
  62.     jiff_2 = (unsigned long)jiffies;
  63.     printk("==========alloc cost time=========n");
  64.     time_cost = timeval_sub(time_end, time_start);
  65.     printk("==========jiffies: %ld=======n", jiff_2 - jiff_1);
  66.     printk("==========%ld seconds, %ld micro-seconds====n",
  67.             time_cost.tv_sec, time_cost.tv_usec);
  68.     if (count != COUNT) {
  69.         printk("====vzalloc failed!====n");
  70.         return -1;
  71.     }
  72.     memset(&time_start, 0, sizeof(struct timeval));
  73.     memset(&time_end, 0, sizeof(struct timeval));
  74.     memset(&time_cost, 0, sizeof(struct timeval));

  75.     jiff_1 = (unsigned long)jiffies;
  76.     do_gettimeofday(&time_start);
  77.     write_node1_mem();
  78.     do_gettimeofday(&time_end);
  79.     jiff_2 = (unsigned long)jiffies;
  80.     printk("==========write cost time=========n");
  81.     time_cost = timeval_sub(time_end, time_start);
  82.     printk("==========jiffies: %ld=======n", jiff_2 - jiff_1);
  83.     printk("==========%ld seconds, %ld micro-seconds====n",
  84.             time_cost.tv_sec, time_cost.tv_usec);
  85.     do_exit(0);
  86. }

  87. static int __init mem_test_mod_init(void)
  88. {
  89.     struct task_struct *my_task = NULL;
  90.     my_task = kthread_create_on_node(test_func, NULL, 1, "mem_test_deamon");
  91.     if (IS_ERR(my_task))
  92.         printk("====Create kthread failed!=====n");
  93.     kthread_bind(my_task, 25);
  94.     wake_up_process(my_task);
  95.     return 0;
  96. }

  97. static void __exit mem_test_mod_exit(void)
  98. {
  99.     mem_free();
  100. }

  101. module_init(mem_test_mod_init);
  102. module_exit(mem_test_mod_exit);


阅读(1444) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~