public
Last active

ugly testing harness

  • Download Gist
build.sh
Shell
1 2 3 4
rm *.o test
as -a --gstabs --64 -march=core2 -o funcs64.o funcs64.S
gcc test.c -Wall -m64 -march=core2 -c
gcc funcs64.o test.o -Wall -m64 -march=core2 -o test
funcs64.S
GAS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
.text
.align 8
.globl test1
.type test1,@function
.globl test2
.type test2,@function
test1:
pushq %rbp
movq %rsp,%rbp
leave
ret
 
test2:
sub $0x8, %rsp
movq %rbp, (%rsp)
movq (%rsp), %rbp
add $0x8, %rsp
ret
go.sh
Shell
1 2 3 4
for i in {1..2500}
do
./test >> results.txt
done
test.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.