给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?可能吗?

Given using C++ or C, how could I measure how long it takes to do a thread switch under linux?Is it possible?

本文关键字:线程 长时间 linux C++ 测量 何测量      更新时间:2023-10-16

使用 C++ 或 C 给定,我如何测量在 Linux 下进行线程切换需要多长时间?可能吗?最多平台,需要多长时间?谁能给我一些典型的价值?

我确实不想测量线程量子。我希望找到一种实用的方法来测量将一个线程转移到另一个线程的持续时间(而不是线程可以运行多长时间(。

如果您能提供代码,我将不胜感激。

如果能就此问题提供任何提示,我将不胜感激。

再见 :)

这是可能的,但您需要对内核代码进行一些更改。所以 openwrt 或其他东西可能对你有好处。

您可以看到我的博客和源代码来解决问题。抢占时间表会做时间检查,如下所示

/*
* Preempt the current task with a newly woken task if needed:
*/
static void
check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
{
unsigned long ideal_runtime, delta_exec;
struct sched_entity *se;
s64 delta;
ideal_runtime = sched_slice(cfs_rq, curr);
delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
if (delta_exec > ideal_runtime) {
resched_curr(rq_of(cfs_rq));
/*
* The current task ran long enough, ensure it doesn't get
* re-elected due to buddy favours.
*/
clear_buddies(cfs_rq, curr);
return;
}
/*
* Ensure that a task that missed wakeup preemption by a
* narrow margin doesn't have to wait for a full slice.
* This also mitigates buddy induced latencies under load.
*/
if (delta_exec < sysctl_sched_min_granularity)
return;
se = __pick_first_entity(cfs_rq);
delta = curr->vruntime - se->vruntime;
if (delta < 0)
return;
if (delta > ideal_runtime)
resched_curr(rq_of(cfs_rq));
}

在这里,我们有几个时间对您有帮助

ideal_runtime:此任务在此计划周期内运行的时间

sum_exec_runtime:任务运行的总时间

prev_sum_exec_runtime : 历史总时间