如何找到哪个CPU /核心我跑? [英] How do I find on which CPU/core I'm running?

查看:137
本文介绍了如何找到哪个CPU /核心我跑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关基准目的,我使用 RDTSC 来确定多少伪时间我花了执行一个关键的循环中code的一些块。由于我的code可以在进程间随时被重新安排,我想只是倾销的数据,如果我发现,我已经改变了启动和微观措施的停止之间的CPU,以尽量减少噪音。

For benchmarking purposes, I'm using rdtsc to determine how much pseudo-time I have spent executing some chunks of code inside a critical loop. Since my code can be rescheduled between processes at any moment, I would like to minimize the noise by just dumping the data if I find out that I have changed CPU between the start and the stop of the micro-measure.

是否有一个x86指令,我可以用它来确定哪些CPU上/核心我跑?东西,会给我任一个唯一的标识符,或CPU#和芯#等

Is there an x86 instruction I could use to identify on which CPU/core I'm running? Something that would give me either a unique identifier, or a CPU# and a core#, etc.

显然, CPUID 已不提供的信息,所以我在寻找一种替代方法。

Apparently, cpuid doesn't provide the information anymore, so I'm looking for an alternative.

推荐答案

不是真的。你不是真的想要一个独立的指令,因为你的线程可能已被执行的指令后立即之前你可以做任何有用的事情,其结果迁移到另一个核心(或RDTSC和核心是什么我是吗?之间检查)。 RDTSCP方便避免了这一点,因为它都开出的TSC和返回任何操作系统告诉它在单个指令中,但它需要OS支持,并且是连载指令(以便比RDTSC更重量级,这可​​能影响细粒度定时测量)

Not really. You don't really want a separate instruction, since your thread could have been migrated to another core immediately after executing the instruction and before you could do anything useful with its result (or between the RDTSC and the "what core am I on?" check). RDTSCP conveniently avoids this, because it both delivers the TSC and returns whatever the OS told it to in a single instruction, but it requires OS support and is a serialising instruction (so more heavyweight than RDTSC, which may affect fine-grained timing measurements).

正如有人在评论中指出,如果每次测量的precision对你很重要,你可能需要使用操作系统API来针线程,以确保它不会迁移。此外,迁移将是比较少见的,所以如果你能采取足够的测量,然后偶尔的异常值,它会导致将是显而易见的,容易排除。

As someone noted in the comments, if the precision of every measurement is important to you, you probably need to use an OS API to pin the thread to ensure it doesn't migrate. Alternatively, migration will be relatively rare, so if you can take enough measurements, then the occasional outliers that it causes will be obvious and easy to exclude.

这篇关于如何找到哪个CPU /核心我跑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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