【问题标题】:Given using C++ or C, how could I measure how long it takes to do a thread switch under linux?Is it possible?给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?有可能吗?
【发布时间】:2020-05-28 14:09:37
【问题描述】:

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

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

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

如果您有任何关于这个问题的提示,我将不胜感激。

【问题讨论】:

  • 使用经过的 CPU 计时?
  • @m0skit0 滴答声或真正的挂钟都可以。
  • 好的,然后用那个?
  • @d.olinger & selbie 我不这么认为。 我确实不想测量线程量子。我希望找到一种实用的方法来测量一个线程切换到另一个线程的持续时间(而不是一个线程可以运行多长时间)。

标签: c++ linux-kernel scheduler scheduling schedule


【解决方案1】:

再见:)

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

您可以查看我的blogsource code 来解决问题。抢占调度会做时间检查,如下图

/*
 * 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 : 历史总时间

【讨论】:

  • 陈,也很高兴见到你。谢谢你的澄清。
  • 不客气。如果您还有其他问题,也许我们可以讨论。
  • 如果我理解正确的话,如果不修改任何内核源代码就无法在用户空间下完成,因为你根本不知道当前线程什么时候开始被切换出来。对吗?
  • 你是对的,你需要在内核中添加一些小功能,或者也许有一些功能可以在用户空间中完成但我不知道。我认为 ptrace 可能有效,但我没有尝试过,所以我不确定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 2014-01-19
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2016-07-04
  • 2020-01-27
相关资源
最近更新 更多