Linux外部事件处理 - IRQ与轮询kthread

时间:2013-06-28 20:40:03

标签: linux linux-kernel linux-device-driver interrupt irq

我正在将设备驱动程序从QNX移植到Linux。在QNX中,旧驱动程序使用带有无限循环的pthread来监视中断的发生,而不是注册真正的中断处理程序。为了演示使用register_irq()而不是专用轮询线程的功效,我在Linux中编写了两个驱动程序。每个的相关代码如下所示,问题位于底部。

IRQ

编写处理程序

irqreturn_t timing_interrupt_handler(int irq, void *dev_id) {

  u32 test;

  /* read device interrupt command/status register */
  test = ioread32(timing_card[3].base);

  /* sanity check that the device reported the interrupt */
  if ( test & (1 << 2) ) {

    /* clear interrupt status */
    iowrite32( 0x0d, timing_card[3].base);

    /* toggle digital output line */
    test = ioread32(timing_card[2].base);
    if ( test & 0x01 ) 
      iowrite32(test & ~0x1, timing_card[2].base);
    else
      iowrite32(test |  0x1, timing_card[2].base);

  }

  return IRQ_HANDLED;
}

注册处理程序

rc = request_irq(irq_line, timing_interrupt_handler,
               IRQF_SHARED, "timing", timing_card);
if ( rc ) {
  printk(KERN_ALERT "Failed to register irq %d\n", irq_line);
  return rc;
}

POLLING THREAD

编写线程函数

int poll_irq(void *data) {

  u32 test;

  /* until module unload */
  while ( !kthread_should_stop() ) {

    /* read device interrupt command/status register */
    test = ioread32(timing_card[3].base);

    /* sanity check that the device reported the interrupt */
    if ( test & (1 << 2) ) {

      /* clear interrupt status */
      iowrite32( 0x0d, timing_card[3].base);

      /* toggle digital output line */
      test = ioread32(timing_card[2].base);
      if ( test & 0x01 ) 
        iowrite32(test & ~0x1, timing_card[2].base);
      else
        iowrite32(test |  0x1, timing_card[2].base);      
      }
      else
        usleep_range(9, 11);
  }

  return 0;
}

开始线程

kthread = kthread_create(poll_irq, 0x0, "poll_IRQ_test");
wake_up_process(kthread);

问题

当我在示波器上放置两条迹线时 - 一条监控卡的数字输入(会触发中断),一条监控卡的数字输出(这会对中断作出反应),我可以测量一个事件的反应时间。

注册IRQ的第一个“正确”方法大约需要80微秒。

运行无限线程的第二种方法大约需要15-30微秒。

是什么给出的?第一个好处是它不会浪费那么多的处理能力,但为什么响应时间如此剧烈?真的,这个投票线程有多糟糕?怎么可以调查并最终证明轮询线程给CPU带来的额外收费呢?

谢谢你的时间!

最佳

斯科特

1 个答案:

答案 0 :(得分:1)

中断响应时间受系统(无论是什么)需要传递中断的时间以及CPU(无论是什么)需要从某些节能睡眠模式唤醒的时间影响。

轮询线程占用CPU时间和功率。 要测量它们,请使用toppowertop之类的内容,或直接在硬件上测量功耗。