MFENCE/SFENCE/etc“序列化内存但不执行指令"? [英] MFENCE/SFENCE/etc "serialize memory but not instruction execution"?

查看:30
本文介绍了MFENCE/SFENCE/etc“序列化内存但不执行指令"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

英特尔的系统编程指南,第 8.3 节,关于 MFENCE/SFENCE/LFENCE 的说明:

Intel's System Programming Guide, section 8.3, states regarding MFENCE/SFENCE/LFENCE:

以下指令是内存排序指令,不是序列化指令.它们会耗尽数据内存子系统.它们不会序列化指令执行流."

"The following instructions are memory-ordering instructions, not serializing instructions. These drain the data memory subsystem. They do not serialize the instruction execution stream."

我想弄清楚为什么这很重要.在多线程代码中,对内存的写入/读取正是需要以明确定义的顺序发生的.当然,I/O 发生的顺序可能很重要,但无论如何 I/O 指令都是序列化指令".CPU 应该可以重新排序指令,这些指令(例如)根据需要在寄存器中进行算术运算;我认为您没有任何理由想要序列化"此类操作.

I'm trying to figure out why this matters. In multi-threaded code, writes/reads to memory are exactly what need to happen in a well-defined order. Of course, the order which I/O happens in might matter, but I/O instructions are "serializing instructions" anyways. It should be possible for the CPU to reorder instructions which (for example) do arithmetic in registers as it likes; I don't think there is any reason why you ever want to "serialize" such operations.

是否存在真正需要完全序列化指令,而MFENCE仅加载和存储的序列化不够"的情况?

Is there any case where a fully serializing instruction is really needed, and MFENCE's serialization of only loads and stores is "not enough"?

推荐答案

是否存在真正需要完全序列化指令,而MFENCE仅加载和存储的序列化不够"的情况?

Is there any case where a fully serializing instruction is really needed, and MFENCE's serialization of only loads and stores is "not enough"?

基准测试和代码分析.

如果您要测量代码序列的性能,特别是当它非常短时,确保基准操作的某些部分不会在定时序列之外执行可能很重要.例如,如果您的代码类似于以下伪代码:

If you're trying to measure the performance of a code sequence, particularly if it's very short, it can be important to ensure that parts of the benchmarked operations aren't being executed outside the timed sequence. For instance, if your code looks something like this pseudocode:

start = RDTSC()
do some stuff
end = RDTSC()
cycles = end - start

确保中间的任何代码都不会在第一个 RDTSC 之前或第二个之后执行,这一点很重要.

It's important to make sure that none of the code in the middle is executed before the first RDTSC, or after the second one.

令人高兴的是,有一条完美的指令可用于此:CPUID 已完全序列化.

Happily, there's a perfect instruction available for this: CPUID is fully serializing.

这篇关于MFENCE/SFENCE/etc“序列化内存但不执行指令"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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