How can I find which processor / core I'm running on?

For benchmarking purposes, I use rdtsc

to determine how much time I spent executing certain pieces of code inside a critical loop. Since my code can be ported between processes at any time, I would like to minimize noise by simply dumping the data if I find out that I have changed the CPU between starting and stopping the micro measure.

Is there an x86 instruction I could use to determine which CPU / core I'm running on? Something that will give me either a unique id, CPU #, and kernel #, etc.

Apparently, cpuid

no longer provides information, so I'm looking for an alternative.

+3


source to share


1 answer


Not really. You really don't need a separate instruction, as your thread could have been ported to another kernel right after the instruction was executed and before you could do anything useful with its result (or between RDTSC and "where am I?" . check). RDTSCP conveniently avoids this as it both transmits TSC and returns what the OS tells it in one command, but it requires OS support and is a serialization instruction (therefore heavier than RDTSC, which can affect fine-grained timing measurements) ...



As someone pointed out in the comments, if the precision of each measurement is important to you, you probably need to use the OS API to bind the stream to make sure it is not portable. Alternatively, migration will be relatively rare, so if you can make enough measurements, then the random outliers that it causes will be obvious and easily excluded.

+1


source







All Articles