Is there an `x86` instruction to tell which core the instruction is being run on?

前端 未结 5 1560
一向
一向 2020-12-29 08:55

When I cat /proc/cpuinfo, I see 8 cores, with ID\'s from 0 to 7.

Is there an x86 instruction that will report th

5条回答
  •  攒了一身酷
    2020-12-29 09:19

    taskset + __rdtscp runnable example

    And at last, for those that want to have some fun with x86 intrinsics + taskset:

    rdtscp.c

    #include 
    #include 
    #include 
    #include 
    
    #include 
    
    int main(void) {
        uint32_t pid;
        printf("0x%016" PRIX64 "\n", (uint64_t)__rdtscp(&pid));
        printf("0x%08" PRIX32 "\n", pid);
        return EXIT_SUCCESS;
    }
    

    GitHub upstream.

    then compile and run while controlling which core it runs on with taskset:

    gcc -ggdb3 -O0 -std=c99 -Wall -Wextra -pedantic -o rdtscp.out rdtscp.c
    ./taskset -c 0 ./rdtscp.out
    ./taskset -c 1 ./rdtscp.out
    

    then for each run, the second line, which shows the CPU ID, matches the value set by taskset.

    Tested in Ubuntu 19.04 amd64 with an Intel Core i7-7820HQ.

提交回复
热议问题