Skip to content

Instantly share code, notes, and snippets.

@proger
Created November 20, 2010 19:13
Show Gist options
  • Select an option

  • Save proger/708066 to your computer and use it in GitHub Desktop.

Select an option

Save proger/708066 to your computer and use it in GitHub Desktop.
syscall execution time sampling test
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#define NSAMPLES 64
struct sample {
u_int64_t pre;
u_int64_t post;
u_int64_t cycles;
} sample[NSAMPLES];
static inline u_int64_t
rdtsc(void)
{
u_int32_t lo, hi;
__asm __volatile("rdtsc; movl %%eax, %0; movl %%edx, %1"
: "=r" (lo), "=r" (hi) : : "eax", "edx");
return ((u_int64_t)lo | ((u_int64_t)hi << 32));
}
u_int64_t
call_sample(struct sample *data, int count, void (*fn)(void))
{
int i;
u_int64_t total = 0;
for (i = 0; i < count; i++) {
data[i].pre = rdtsc();
fn();
data[i].post = rdtsc();
}
for (i = 0; i < count; i++) {
total += data[i].cycles = data[i].post - data[i].pre;
printf("sample %d: cycles: %llu\n",
i, data[i].cycles);
}
return (total / count);
}
void
sample_kill(void)
{
return (void)(kill(1, 0));
}
int
main(int argc, char **argv)
{
u_int64_t avg;
printf("sampling: kill(1, 0)\n");
avg = call_sample(sample, NSAMPLES, sample_kill);
printf("average: %llu\n", avg);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment