I prefer rdtscp. IA32_TSC_AUX is only a model specific register. Reading it can be faster than memory access.
EDIT: And when booting, you can store APIC ID in IA32_TSC_AUX so that you can read it faster later.
Best or fastest way to determine which CPU is running (SMP)
Re: Best or fastest way to determine which CPU is running (S
Doing steadfastly, or doing nil.
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: Best or fastest way to determine which CPU is running (S
If you are reading the task register, why not simply use the index part of the TSS selector itself as a core ID, since it is unique per core? This way you don't need another memory access to the TSS. You can use sequential selectors, and so your core ID function becomes something likesamiam95124 wrote:Task register, to unique TSS, (which is usually per core in any case), then to a value in the TSS. The TSS span is set in the descriptor, which implies that you can make it longer than needed to store goodies in the TSS data, so voila, there is a place for a core number using a simple offset. You will have more than one thread per core, but in most implementations you don't use the hardware task switching but rather use the (fake) tss to bounce the stack pointer. Thus all threads use the same TSS, and thus all threads on the same core yield the same core number from the TSS.
Other idea: since you are not actually using the TSS to store registers, you can repurpose those fields so that you are not wasting the whole TSS per core.
Code: Select all
(ReadTaskRegister() - FIRST_TSS) >> 3