#define _GNU_SOURCE | |
#include <errno.h> | |
#include <sched.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/time.h> | |
#define TEST_RUNS 500000000 | |
extern void | |
test1(); | |
extern void | |
test2(); | |
static __inline__ unsigned long long | |
rdtsc(void) | |
{ | |
unsigned long hi = 0, lo = 0; | |
__asm__ __volatile__ ("lfence\n\trdtsc" : "=a"(lo), "=d"(hi)); | |
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); | |
} | |
static void | |
run_test(void (*fn)(void), const char *id) | |
{ | |
size_t i = 0; | |
long long before_cycles = 0, after_cycles = 0; | |
struct timeval before_tv, after_tv; | |
memset(&before_tv, 0, sizeof before_tv); | |
memset(&after_tv, 0, sizeof after_tv); | |
gettimeofday(&before_tv, NULL); | |
before_cycles = rdtsc(); | |
for (; i < TEST_RUNS; i++) { | |
fn(); | |
} | |
after_cycles = rdtsc(); | |
gettimeofday(&after_tv, NULL); | |
printf("Result %s: %lld cycles\n", id, after_cycles - before_cycles); | |
printf("Result %s: %ld microsecs\n", | |
id, | |
((after_tv.tv_sec - before_tv.tv_sec) * 1000000) + | |
(after_tv.tv_usec - before_tv.tv_usec)); | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
int ret = 0; | |
cpu_set_t mask; | |
CPU_ZERO(&mask); | |
CPU_SET((int)3, &mask); | |
ret = sched_setaffinity(0, sizeof mask, &mask); | |
if (ret == -1) { | |
perror("setting cpu affinity failed"); | |
exit(EXIT_FAILURE); | |
} | |
sleep(2); | |
run_test(test1, "test1 (frame pointer)"); | |
run_test(test2, "test2 (no frame pointer)"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment