sched_getcpu()等价于OS X吗?

时间:2022-12-02 02:15:04

On OS X, Is there a way to find out which CPU a thread is running on? An equivalent function for Linux is sched_getcpu

在OS X上,是否有办法找出线程运行在哪个CPU上?Linux的一个等价函数是sched_getcpu

1 个解决方案

#1


3  

GetCurrentProcessorNumber example shows code that implements this functionality using the CPUID instruction. I have tried it myself and can confirm it works on Mac OS X.

GetCurrentProcessorNumber示例显示了使用CPUID指令实现此功能的代码。我自己也试用过,可以确认它在Mac OS X上是有效的。

Here is my version which I used on Mac OS X

这是我在Mac OS X上使用的版本

#include <cpuid.h>

#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])

#define GETCPU(CPU) {                              \
        uint32_t CPUInfo[4];                           \
        CPUID(CPUInfo, 1, 0);                          \
        /* CPUInfo[1] is EBX, bits 24-31 are APIC ID */ \
        if ( (CPUInfo[3] & (1 << 9)) == 0) {           \
          CPU = -1;  /* no APIC on chip */             \
        }                                              \
        else {                                         \
          CPU = (unsigned)CPUInfo[1] >> 24;                    \
        }                                              \
        if (CPU < 0) CPU = 0;                          \
      }

#1


3  

GetCurrentProcessorNumber example shows code that implements this functionality using the CPUID instruction. I have tried it myself and can confirm it works on Mac OS X.

GetCurrentProcessorNumber示例显示了使用CPUID指令实现此功能的代码。我自己也试用过,可以确认它在Mac OS X上是有效的。

Here is my version which I used on Mac OS X

这是我在Mac OS X上使用的版本

#include <cpuid.h>

#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])

#define GETCPU(CPU) {                              \
        uint32_t CPUInfo[4];                           \
        CPUID(CPUInfo, 1, 0);                          \
        /* CPUInfo[1] is EBX, bits 24-31 are APIC ID */ \
        if ( (CPUInfo[3] & (1 << 9)) == 0) {           \
          CPU = -1;  /* no APIC on chip */             \
        }                                              \
        else {                                         \
          CPU = (unsigned)CPUInfo[1] >> 24;                    \
        }                                              \
        if (CPU < 0) CPU = 0;                          \
      }