#include #include #include #if !defined(__GNUC__) && !defined(__i386__) #error This code only works with GCC/i386. #endif /* The reason this only works under GNU C and the x86 is we're using the * rdtsc instruction. */ static inline unsigned long long rdtsc() { unsigned long long rval; asm volatile ("rdtsc" : "=A" (rval)); return rval; } static sem_t waiting_thread_semaphore; static pthread_mutex_t mutex; void * waiting_thread_func(void * param __attribute__((unused))) { sem_wait(&waiting_thread_semaphore); return NULL; } int main(void) { int i; pthread_t waiting_thread; void * trash; unsigned long long start, stop, time, min; /* Create a thread to force us to actually do multi-threaded work */ sem_init(&waiting_thread_semaphore, 1, 0); pthread_create(&waiting_thread, NULL, waiting_thread_func, NULL); pthread_mutex_init(&mutex, NULL); /* Time how long a rdtsc takes- we do this ten times and take the * cheapest run. */ min = ~0ull; for (i = 0; i < 10; ++i) { start = rdtsc(); stop = rdtsc(); time = stop - start; if (time < min) { min = time; } } printf("Minimum time for a rdtsc instruction (in clocks): %llu\n", min); /* Now time how long the pair of mutex lock + unlock take */ min = ~0ull; for (i = 0; i < 10; ++i) { start = rdtsc(); pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); stop = rdtsc(); time = stop - start; if (time < min) { min = time; } } printf("Minimum time for a mutex lock+unlock + rdtsc (in clocks): %llu\n", min); /* Clean up the waiting thread we spawned just to be multithreaded. */ sem_post(&waiting_thread_semaphore); pthread_join(waiting_thread, &trash); sem_destroy(&waiting_thread_semaphore); return 0; }