上下文切换测量时间

sok*_*ker 2 linux kernel

我想知道你是否有人知道如何使用函数get_timer()来测量上下文切换的时间

如何找到平均值?什么时候显示它?

有人可以帮我解决这个问题.

是知道这个的专家吗?

Max*_*kin 5

一种相当简单的方法是让两个线程通过管道进行通信.一个线程会做(伪代码):

for(n = 1000; n--;) {
    now = clock_gettime(CLOCK_MONOTONIC_RAW);
    write(pipe, now);
    sleep(1msec); // to make sure that the other thread blocks again on pipe read
}
Run Code Online (Sandbox Code Playgroud)

另一个线程会做:

context_switch_times[1000];
while(n = 1000; n--;) {
    time = read(pipe);
    now = clock_gettime(CLOCK_MONOTONIC_RAW);
    context_switch_times[n] = now - time;
}
Run Code Online (Sandbox Code Playgroud)

也就是说,它将测量一个线程将数据写入管道的时间与另一个线程唤醒并读取该数据的时间之间的持续时间.context_switch_times数组的直方图将显示上下文切换时间的分布.

时间将包括管道读写的开销和获取时间,但是,它给出了很大的上下文切换时间感.

在过去,我使用Fedora 13内核和实时FIFO线程进行了类似的测试.我得到的最小上下文切换时间约为4-5 usec.