i386: vgetcpu()
In-Reply-To: <[email protected]>
Quoted 8 lines · Andi Kleen
Quoted 3 lines · Chuck Ebbert
Use the limit field of a GDT entry to store the current CPU number for fast userspace access. This still leaves 12 bits free for other information.
Nice trick. Maybe I'll even add that to the x86-64 implementation if it's fast enough. Do you have numbers?
I got ~13 clocks on x86_64 and 21 on PII. Test program below.
Quoted 2 lines
But it needs to be encapsulated in a wrapper I think. Just exposing it to user space is the wrong way to do this.
Well there's no real way to hide it, but something more is definitely needed. A new vdso entry point is easy but how do you tell userspace it's there? Does glibc look at the kernel version in the note?
/* test how fast lsl, test for success + mask result runs
* leave DO_TEST undefined to measure overhead */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#define rdtscll(t) asm volatile ("rdtsc" : "=A" (t))
#ifndef ITERS
#define ITERS 1000000
#endif
int main(int argc, char * const argv[])
{
unsigned long long tsc1, tsc2;
int count, cpu, junk;
rdtscll(tsc1);
asm (
" pushl %%ds \n"
" popl %2 \n"
"1: \n"
#ifdef DO_TEST
" lsl %2,%0 \n"
" jnz 2f \n"
" and $0xff,%0 \n"
#endif
" dec %1 \n"
" jnz 1b \n"
"2: \n"
: "=&r" (cpu), "=&r" (count), "=&r" (junk)
: "1" (ITERS), "0" (-1)
);
rdtscll(tsc2);
if (count == 0)
printf("loops: %d, avg: %llu clocks\n",
ITERS, (tsc2 - tsc1) / ITERS);
return 0;
}Signature · 2 lines
Chuck "You can't read a newspaper if you can't read." --George W. Bush