Created
March 10, 2018 20:24
-
-
Save Leandros/35b8af120f861ef94a462f57fa10dc05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdatomic.h> | |
#include <stdint.h> | |
#include <x86intrin.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <linux/futex.h> | |
#include <sys/time.h> | |
#include <pthread.h> | |
static uint64_t | |
average(short *arr, int count) | |
{ | |
uint64_t sum = 0; | |
for (int i = 0; i < count; ++i) | |
sum += arr[i]; | |
return sum / count; | |
} | |
static int | |
comp(void const *lhs, void const *rhs) | |
{ | |
return *(short const *)lhs - *(short const *)rhs; | |
} | |
static uint64_t | |
median(short *arr, int count) | |
{ | |
qsort(arr, count, sizeof(short), &comp); | |
return arr[count / 2]; | |
} | |
static void | |
escape(void *p) | |
{ | |
asm volatile("" :: "g"(p) : "memory"); | |
} | |
static void | |
clobber(void) | |
{ | |
asm volatile("" ::: "memory"); | |
} | |
#define ITERATIONS 100000 | |
#define CYCLES_START \ | |
{ \ | |
int _count = 0; \ | |
short _results[ITERATIONS]; \ | |
for (int i = 0; i < ITERATIONS; ++i) { \ | |
uint64_t _start = __rdtsc(); | |
#define CYCLES_END \ | |
uint64_t _stop = __rdtsc(); \ | |
_results[_count++] = (short)(_stop - _start); \ | |
} \ | |
printf("avg: %lld | median: %lld\n", average(_results, ITERATIONS), median(_results, ITERATIONS)); \ | |
} | |
struct lstack_head { | |
uintptr_t aba; | |
void *node; | |
}; | |
struct lstack_head | |
load(_Atomic volatile struct lstack_head *head) | |
{ | |
return atomic_load(head); | |
} | |
int main() | |
{ | |
_Atomic volatile struct lstack_head head = { .aba = 0, .node = 0 }; | |
pthread_mutex_t mtx; | |
CYCLES_START | |
{ | |
/* struct lstack_head ret = load(&head); */ | |
/* escape(&ret); */ | |
/* clobber(); */ | |
pthread_mutex_lock(&mtx); | |
escape(&mtx); | |
clobber(); | |
pthread_mutex_unlock(&mtx); | |
} | |
CYCLES_END | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment