/* gcc -W -Wall -O2 -o port80 port80.c */ #include #include #include #define LOOPS 10000 inline unsigned long long rdtsc(void) { unsigned long long tsc; asm volatile ("rdtsc": "=A" (tsc)); return tsc; } inline void serialize(void) { asm volatile ("cpuid": : : "eax", "ebx", "ecx", "edx"); } int main(void) { unsigned long long start; unsigned long long overhead; unsigned long long output; unsigned long long input; int i; if (iopl(3) < 0) { perror("iopl"); return EXIT_FAILURE; } asm volatile ("cli"); start = rdtsc(); for (i = 0; i < LOOPS; i++) { serialize(); serialize(); } overhead = rdtsc() - start; start = rdtsc() + overhead; for (i = 0; i < LOOPS; i++) { serialize(); asm volatile ("outb %al, $0x80"); serialize(); } output = rdtsc() - start; start = rdtsc() + overhead; for (i = 0; i < LOOPS; i++) { serialize(); asm volatile ("inb $0x80, %%al": : : "al"); serialize(); } input = rdtsc() - start; asm volatile ("sti"); output /= LOOPS; input /= LOOPS; printf("cycles: out %llu, in %llu\n", output, input); return EXIT_SUCCESS; }