# Building a CPU Topology on MacOS X

DevFeed: [Building a CPU Topology on MacOS X](<https://devfeed.tech/articles/building-a-cpu-topology-on-macos-x-30640.md>)

Original publisher: [Read original article](<http://bad-concurrency.blogspot.com/2012/01/buliding-cpu-topology-on-macos-x.html>)

Author: Michael Barker (noreply@blogger.com)

Published: 2012-01-08T19:44:00Z

Content type: tutorial

Language: en

Sources: [Bad Concurrency](<https://devfeed.tech/sources/bad-concurrency.md>)

Topics: [cpu](<https://devfeed.tech/topics/cpu.md>), [macOS](<https://devfeed.tech/topics/macos.md>), [Assembly](<https://devfeed.tech/topics/assembly.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [x86](<https://devfeed.tech/topics/x86.md>), [Hardware](<https://devfeed.tech/topics/hardware.md>), [Kernel](<https://devfeed.tech/topics/kernel.md>)

Tags: [assembly](<https://devfeed.tech/tags/assembly.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [hardware](<https://devfeed.tech/tags/hardware.md>), [kernel](<https://devfeed.tech/tags/kernel.md>), [macos](<https://devfeed.tech/tags/macos.md>), [x86](<https://devfeed.tech/tags/x86.md>)

## AI overview

A tutorial on building a CPU topology on MacOS X to map operating-system processors to hardware threads, cores, and sockets. It describes using Intel's CPUID instruction with C++, x86 inline assembly, and a MacOS kernel extension.

## Source excerpt

Within a project I've been working on I've had the need to simulate the capabilities of Linux's /proc/cpuinfo on Mac OS. Specifically I needed to build a topology of the CPUs on a given system. I.e. I need to map the operating system's processors to hardware threads, then build a picture of which cores and sockets those threads reside. For example my Mac looks something like: CPU0 (Thread 0) ---+ |---> Core 0 ---+ CPU1 (Thread 1) ---+ | | ----> Socket 0 CPU2 (Thread 2) ---+ | |---> Core 1 ---+ CPU3 (Thread 3) ---+ While this sounds very simple, it's actually fraught with a number of little niggles. Not only did it require getting down and dirty with a bit of C++ and X86 Assembly, it also required writing a MacOS kernel extension. The first step was to understand what information was available from the CPU. Intel exposes an instruction called CPUID. The is the primary mechanism for getting information about the CPU. There is a raft of information available from listing of the CPU features available (e.g. hyperthreading) to sizes of the various levels of cache and the associated cache lines. To access the CPUID instruction we need a little bit of inline assembler. The code shows how to get the vendor string from the CPU. On my Mac I get the following: // Output: Vendor String: GenuineIntel For those unfamiliar with Intel inline assembly, the Intel CPU defines a number of registers. The ones used for the CPUID instruction are EAX, EBX, ECX, and EDX (referenced as RAX, RBX, etc if using 64 bit instructions via the REX extension). These used for both input and output. An inline asm segment consists of 3 parts. The first part is the instruction to be executed. In this case the "cpuid" instruction. The second line defines the output parameters. The snippet "=a" (data[0]) means store the result in the EAX register in the variable data[0]. The "=a" refers to the 2nd letter of the register designation. The 3rd and final section are the input parameters. The CPUID instruction