测量任务在linux中2点之间花费的时间(任务分析)

时间:2014-02-11 14:19:12

标签: multithreading linux-kernel scheduler thread-priority preemption

我很快就会开始把头撞在墙上:

这很简单,我想测量一个任务花费2点之间的时间(在Linux中 - 1核 - 1个CPU)。 在此期间,任务必须完全控制CPU,并且不会被任何其他任务或HW中断中断。

为实现这一目标,我创建了一个内核模块,以确保满足上述标准。 在这个内核模块中,我试过:

首先,禁用IRQ:

  • 我使用了spin_lock_irqsave()/ spin_lock_irqrestore() - 我认为这是确保所有本地中断都被禁用的正确方法,并且我的任务在关键区域内有自己的cpu。

然后,

  • 使用preempt_disable() - >由于current =我的任务,所以逻辑上内核应该继续运行我的任务,直到我重新启用抢占 - >不起作用(my_task-> nvcsw和my_task-> nivcsw显示已发生csw - > my-task被抢占)

我尝试通过将my_task-> prio和my_task-> static_prio更改为1 - >来提高我的任务的优先级。最高实时prio(my_task-> policy = SCHED_FIFO)......

无法正常工作(my_task-> nvcsw和my_task-> nivcsw显示已发生csw - > my-task被抢占)和my_task-> prio由调度程序获得新的prio(120)我猜是......

有没有办法确定地保证任务不会在Linux中被中断/抢占?有没有办法强制调度程序运行任务(短时间50-500us),直到它完成?

这是启用/禁用操作系统部分的代码(有问题的任务使用procfs在关键区域之前和之后发送启用/禁用命令并由此开关处理):

// Handle request
switch( enable ){
    // Disable OS
    case COS_OS_DISABLE:
                    // Disable preemption
                    preempt_disable()
        // Save policy
        last_policy         = pTask->policy;
        // Save task priorities
        last_prio       = pTask->prio;
        last_static_prio    = pTask->static_prio;
        last_normal_prio    = pTask->normal_prio;
        last_rt_priority    = pTask->rt_priority;
        // Set priorities to highest real time prio 
        pTask->prio         = 1;
        pTask->static_prio  = 1;
        pTask->normal_prio  = 1;
        pTask->rt_priority  = 1;
        // Set scheduler policy to FIFO
        pTask->policy       = SCHED_FIFO;
        // Lock kernel: It will disable interrupts _locally_, but the spinlock itself will guarantee the global lock, so it will guarantee that there is only one thread-of-control within the region(s) protected by that lock.
        spin_lock_irqsave( &mr_lock , flags );
        break;
    // Default: Enable OS always
    case COS_OS_ENABLE:
    default:
        // Reset task priorities
        pTask->prio         = last_prio;
        pTask->static_prio  = last_static_prio;
        pTask->normal_prio  = last_normal_prio;
        pTask->rt_priority  = last_rt_priority;
        // Reset scheduler policy
        pTask->policy       = last_policy;
        // Unlock kernel
        spin_unlock_irqrestore( &mr_lock , flags );
                    // Enable preemption
                    preempt_enable();
        break;
}

1 个答案:

答案 0 :(得分:1)

仅允许内核代码禁用中断,且只能在短时间内执行。 使用stock内核,无法为用户空间任务提供对CPU的完全控制。

如果您只想测量用户空间任务所用的时间,您可以正常运行任务并使用u modifer of perf忽略中断;但是,这不会阻止中断处理程序的任何缓存效果。

相关问题