#include #include #include #define LOOPS 1000 unsigned long cycles[LOOPS]; int main(void) { unsigned long overhead; unsigned long total; int i; if (iopl(3) < 0) { perror("iopl"); return EXIT_FAILURE; } /* pull it in */ for (i = 0; i < LOOPS; i++) cycles[i] = 0; asm volatile ("cli"); for (i = 0; i < LOOPS; i++) asm ( "xor %%eax, %%eax \n\t" "cpuid \n\t" "rdtsc \n\t" "movl %%eax, %%esi \n\t" "xor %%eax, %%eax \n\t" "cpuid \n\t" "rdtsc \n\t" "subl %%esi, %%eax \n\t" : "=a" (cycles[i]) : : "ecx", "edx", "ebx", "esi"); asm volatile ("sti"); overhead = 0; for (i = 0; i < LOOPS; i++) overhead += cycles[i]; asm volatile ("cli"); for (i = 0; i < LOOPS; i++) asm ( "xor %%eax, %%eax \n\t" "cpuid \n\t" "rdtsc \n\t" "movl %%eax, %%esi \n\t" "outb %%al, $0x80 \n\t" "xor %%eax, %%eax \n\t" "cpuid \n\t" "rdtsc \n\t" "subl %%esi, %%eax \n\t" : "=a" (cycles[i]) : : "ecx", "edx", "ebx", "esi"); asm volatile ("sti"); total = 0; for (i = 0; i < LOOPS; i++) total += cycles[i]; total -= overhead; printf("out: %lu\n", total / LOOPS); asm volatile ("cli"); for (i = 0; i < LOOPS; i++) asm ( "xor %%eax, %%eax \n\t" "cpuid \n\t" "rdtsc \n\t" "movl %%eax, %%esi \n\t" "inb $0x80, %%al \n\t" "xor %%eax, %%eax \n\t" "cpuid \n\t" "rdtsc \n\t" "subl %%esi, %%eax \n\t" : "=a" (cycles[i]) : : "ecx", "edx", "ebx", "esi"); asm volatile ("sti"); total = 0; for (i = 0; i < LOOPS; i++) total += cycles[i]; total -= overhead; printf("in : %lu\n", total / LOOPS); return EXIT_SUCCESS; }