preempt_disable/enable 和 raw_local_irq_save/restore 在基准测试中的作用

Role of preempt_disable/enable and raw_local_irq_save/restore in benchmarking

英特尔 (link) 的以下论文描述了一种准确地对代码进行基准测试的方法。基准测试的核心内容如下(参见第 31 页):

preempt_disable();
raw_local_irq_save(flags);

asm volatile ( 
    "CPUID\n\t" 
    "RDTSC\n\t" 
    "mov %%edx, %0\n\t" 
    "mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: "%rax", "%rbx", "%rcx", "%rdx"
);

/*call the function to measure here*/ 

asm volatile( 
    "CPUID\n\t" 
    "RDTSC\n\t" 
    "mov %%edx, %0\n\t"
    "mov %%eax, %1\n\t": "=r" (cycles_high1), "=r" (cycles_low1):: "%rax", "%rbx", "%rcx", "%rdx"
); 

raw_local_irq_restore(flags);  
preempt_enable(); 

我在想:

在您提供的 link 中,如果您阅读他们实际实现内核模块的第 2.2 节,您会看到一些评论 -

preempt_disable(); /*we disable preemption on our CPU*/

这是一个 Linux 内核 function,它基本上禁止处理器将上下文切换到不同的进程。

第二次调用 -

raw_local_irq_save(flags); /*we disable hard interrupts on our CPU*/  
/*at this stage we exclusively own the CPU*/ 

这会屏蔽硬件上的所有中断。又一个 Linux 内核 function.

这两个一起意味着在基准测试完成之前,没有任何东西,甚至硬件中断都可以干扰处理器。这是为了确保对处理器和其他资源(如高速缓存、TLB 等)的独占访问。我想你可以理解为什么这对于正确的基准测试是必要的。

其他两个函数,顾名思义,在基准测试完成后重新启用抢占并恢复中断掩码。

至于会发生什么,如果删除这些调用,那么 "something" 可能会中断您的基准测试过程,并且您的测量结果可能会有很大的差异。