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

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

这真的很简单,我想测量任务在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-> prio和my_task-> static_prio更改为1 - >最高实时prio(my_task-> policy = SCHED_FIFO)来提高我的任务的优先级...

    也没有工作(my_task-> nvcsw和my_task-> nivcsw显示csw已经发生 - >我的任务被抢占),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;
    }
    

    禁止中断仅适用于内核代码,并且只能在短时间内使用。 使用库存核心,不可能给CPU的用户空间任务总控制权。

    如果要衡量的,只有你的用户空间任务中使用的时候,你可以正常运行你的任务,并使用u的修改器perf忽略中断; 但是,这不会阻止中断处理程序的任何缓存效果。

    链接地址: http://www.djcxy.com/p/84877.html

    上一篇: Measure time a task spends between 2 points in linux (task profiling)

    下一篇: how to schedule a task in MVC4 C#?