Chinaunix首页 | 论坛 | 博客
  • 博客访问: 384667
  • 博文数量: 39
  • 博客积分: 1771
  • 博客等级: 上尉
  • 技术积分: 1231
  • 用 户 组: 普通用户
  • 注册时间: 2005-05-01 14:21
文章分类

全部博文(39)

文章存档

2013年(1)

2012年(4)

2011年(16)

2010年(6)

2009年(12)

分类: LINUX

2011-09-28 15:14:48

有时候我们要每隔一段时间打印一些统计信息来辅助调试。利用内核定时器可以方便的实现这个功能。

在内核文件(譬如:[kernel dir]/init/main.c)末尾加入如下代码:
  1. static struct timer_list leon_timer;
  2. static void leon_timer_func(unsigned long data)
  3. {
  4.     printk("<7>###leon, timeout.\n");
  5.     // schedule timer in 10s.
  6.     mod_timer(&leon_timer, jiffies + HZ * 10);
  7. }

  8. static int __init leon_timer_init(void)
  9. {
  10.     init_timer(&leon_timer);
  11.     leon_timer.data = NULL;//(unsigned long)priv;
  12.     leon_timer.function = (unsigned long)leon_timer_func;
  13.     leon_timer.expires = jiffies + HZ * 10; //10s
  14.     add_timer(&leon_timer);
  15.     return 0;
  16. }

  17. late_initcall(leon_timer_init);
这样就会每隔10s打印###leon, timeout.这句话。定义一个全局的变量,在需要的地方更新这个变量的值,然后在leon_timer_func函数中打印这个变量的值就可以看到系统的状态了。

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