如何从用户空间应用程序(Linux,C)中获取CPU内核号? [英] How can I get the CPU core number from within a user-space app (Linux, C)?

查看:88
本文介绍了如何从用户空间应用程序(Linux,C)中获取CPU内核号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大概有一个库或简单的asm blob,可以为我获取正在执行的当前CPU的编号.

Presumably there is a library or simple asm blob that can get me the number of the current CPU that I am executing on.

推荐答案

使用sched_getcpu确定在其上运行调用线程的CPU.请参见man getcpu(系统调用)和man sched_getcpu(库包装器).但是,请注意其内容:

Use sched_getcpu to determine the CPU on which the calling thread is running. See man getcpu (the system call) and man sched_getcpu (a library wrapper). However, note what it says:

仅在调用时保证放置在cpu中的信息是最新的:除非已使用sched_setaffinity(2)修复了CPU关联性,否则内核可能随时更改CPU. (通常不会发生这种情况,因为调度程序会尝试最大程度地减少CPU之间的移动以保持高速缓存保持高速,但有可能这样做.)当cpu和node不再是当前的CPU和节点时,调用者必须做好处理这种情况的准备. >

The information placed in cpu is only guaranteed to be current at the time of the call: unless the CPU affinity has been fixed using sched_setaffinity(2), the kernel might change the CPU at any time. (Normally this does not happen because the scheduler tries to minimize movements between CPUs to keep caches hot, but it is possible.) The caller must be prepared to handle the situation when cpu and node are no longer the current CPU and node.

这篇关于如何从用户空间应用程序(Linux,C)中获取CPU内核号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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