diff options
author | Andy Lutomirski <luto@kernel.org> | 2015-04-23 09:35:50 -0700 |
---|---|---|
committer | Andy Lutomirski <luto@kernel.org> | 2015-04-23 09:35:50 -0700 |
commit | 628f134de4b404d8290c64c513ea119159cf23d5 (patch) | |
tree | 886704968dea9031cf0bb68bf528561e7ecdddf4 | |
parent | 53e5b21fd612efea629630ae9ac24df3c32f63fd (diff) | |
download | misc-tests-628f134de4b404d8290c64c513ea119159cf23d5.tar.gz |
perf_self_monitor: Start working on stats
-rw-r--r-- | tight_loop/perf_self_monitor.c | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/tight_loop/perf_self_monitor.c b/tight_loop/perf_self_monitor.c index f3de608..33c2be2 100644 --- a/tight_loop/perf_self_monitor.c +++ b/tight_loop/perf_self_monitor.c @@ -6,6 +6,7 @@ #include <string.h> #include <stdbool.h> #include <stdint.h> +#include <stdlib.h> #include <stdio.h> #include <err.h> @@ -151,6 +152,49 @@ bool psm_atomic_sample_empty(uint64_t *count, return psm_atomic_elapsed(count, &duration, ctr); } +bool psm_atomic_sample_enosys(uint64_t *count, + const struct psm_counter *ctr, void *opaque) +{ + struct psm_atomic duration = psm_atomic_start(ctr); + unsigned long rax; + rax = 0xbfffffff; + asm volatile ("syscall" : "+a" (rax) : : "rcx", "r11"); + return psm_atomic_elapsed(count, &duration, ctr); +} + +static int compare_ui64(const void *a_void, const void *b_void) +{ + const uint64_t a = *(uint64_t*)a_void; + const uint64_t b = *(uint64_t*)b_void; + if (a < b) + return -1; + else if (a > b) + return 1; + else + return 0; +} + +uint64_t psm_integer_quantile(const struct psm_counter *ctr, psm_sample_fn fn, + void *opaque, size_t q, size_t n) +{ + if (q >= n) + abort(); + + uint64_t *array = calloc(sizeof(uint64_t), n); + for (size_t i = 0; i < n; ) { + uint64_t sample; + if (!fn(&sample, ctr, opaque)) + continue; + array[i++] = sample; + } + + qsort(array, n, sizeof(uint64_t), compare_ui64); + + uint64_t ret = array[q]; + free(array); + return ret; +} + void psm_settle(const struct psm_counter *ctr, psm_sample_fn fn, void *opaque) { uint64_t val = UINT64_MAX; @@ -184,18 +228,7 @@ int main() psm_settle(ctr, psm_atomic_sample_empty, NULL); for (int i = 0; i < 20; i++) { - struct psm_atomic duration = psm_atomic_start(ctr); - unsigned long rax; - rax = 0xbfffffff; - asm volatile ("syscall" : "+a" (rax) : : "rcx", "r11"); - rax = 0xbfffffff; - asm volatile ("syscall" : "+a" (rax) : : "rcx", "r11"); - uint64_t cycles; - bool ok = psm_atomic_elapsed(&cycles, &duration, ctr); - if (ok) - printf("%d: %lld\n", i, (unsigned long long)cycles); - else - printf("sadness\n"); + printf("%llu\n", (unsigned long long)psm_integer_quantile(ctr, psm_atomic_sample_enosys, NULL, 0, 500)); } psm_counter_destroy(ctr); |