在Linux中测量任务在2点之间花费的时间(任务分析) [英] Measure time a task spends between 2 points in linux (task profiling)

查看:153
本文介绍了在Linux中测量任务在2点之间花费的时间(任务分析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很快就会开始在墙上敲打我的头:

I'll soon start banging my head on the wall:

真的很简单,我想测量一个任务花费在2点之间的时间(在Linux中-1个核心-1个CPU). 在这段时间内,任务必须完全控制CPU,并且不能被任何其他任务或硬件中断打断.

It's very simple really, I want to measure the time a task spends between 2 points (in Linux - 1 core - 1 CPU). During this time the task must have total control over the CPU and NOT get interrupted by any other task or HW interrupts.

为此,我创建了一个内核模块以确保满足上述条件. 在此内核模块中,我尝试:

To achieve this, I'v created a kernel module to make sure the above criterions are met. In this kernel module I've tried to:

首先,禁用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)来增加任务的优先级...

I've tried to increase the priority of my task by changing my_task->prio and my_task->static_prio to 1 -> highest real-time prio (my_task->policy = SCHED_FIFO)...

也不起作用(my_task-> nvcsw和my_task-> nivcsw表明发生了csw-> my-task被抢占了),并且my_task-> prio由我假定的调度程序得到了新的prio(120). ..

Did not work either (my_task->nvcsw and my_task->nivcsw show that a csw has occurred -> my-task got preempted) and my_task->prio got a new prio (120) by the scheduler I presume....

有什么方法可以确定地保证任务不会在Linux中被中断/抢占吗?有什么方法可以强制调度程序在完成任务之前(短时间50-500us)运行任务?

Is there any way to deterministically garantee that a task does not get interrupted/preeempted in Linux? Is there any way to force the scheduler to run a task (for a short time 50-500us) until it's done?

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

Here is my code to enable/disable parts of the OS (the task in question sends a enable/disable commands before and after the critical region using procfs and handled by this switch):

// 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的控制.

Disabling interrupts is allowed only for kernel code, and only for a short time. With the stock kernel, it is not possible to give a user-space task total control of the CPU.

如果只想测量用户空间任务使用的时间,则可以正常运行任务并使用

If you want to measure only the time used by your user-space task, you could run your task normally and use the u modifer of perf to ignore interrupts; however, this would not prevent any cache effects of the interrupt handlers.

这篇关于在Linux中测量任务在2点之间花费的时间(任务分析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆