From 59ce941b3b4111bc38ed808022d17ec922fb5bbe Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 4 Mar 2022 15:57:36 -0700 Subject: Use PMC --- main.c | 33 ++++++++++++++++++++++++++++----- run.sh | 24 ------------------------ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/main.c b/main.c index cab65b3..4c4d8bb 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -23,11 +24,12 @@ module_param(stamp, ulong, 0); #define do_it(name) do { \ u32 eax = 0, ebx = 0, ecx = 0, edx = 0; \ - for (i = 0; i < WARMUP; ++i) \ + for (i = 0; i < TRIALS; ++i) \ + asm volatile("cpuid" : "+a" (eax), "=b" (ebx), "=d" (edx), "+c" (ecx)); \ + for (i = 0; i < TRIALS; ++i) \ ret |= mix_ ##name(pool, input); \ - asm volatile("cpuid" : "+a" (eax), "=b" (ebx), "=d" (edx), "+c" (ecx)); \ for (i = 0; i <= TRIALS; ++i) { \ - trial_times[i] = get_cycles(); \ + trial_times[i] = native_read_pmc(pmc_index); \ ret |= mix_ ##name(pool, input); \ } \ for (i = 0; i < TRIALS; ++i) \ @@ -61,23 +63,44 @@ static int compare_cycles(const void *a, const void *b) static int __init mod_init(void) { - enum { WARMUP = 6000, TRIALS = 100000, IDLE = 1 * 1000 }; + enum { TRIALS = 300000 }; int ret = 0, i; cycles_t *trial_times; forall(local_it) unsigned long flags; + struct perf_event *cycles_event; + int pmc_index; + static struct perf_event_attr perf_cycles_attr = { + .type = PERF_TYPE_HARDWARE, + .config = PERF_COUNT_HW_CPU_CYCLES, + .size = sizeof(struct perf_event_attr), + .pinned = 1, + .disabled = 0, + .exclude_user = 1 + }; DEFINE_SPINLOCK(lock); u32 pool[4] = { 0 }, input[4]; + get_random_bytes(input, sizeof(input)); trial_times = kcalloc(TRIALS + 1, sizeof(cycles_t), GFP_KERNEL); if (!trial_times) return -ENOMEM; - msleep(IDLE); + msleep(1000); spin_lock_irqsave(&lock, flags); + cycles_event = perf_event_create_kernel_counter(&perf_cycles_attr, raw_smp_processor_id(), NULL, NULL, NULL); + if (IS_ERR(cycles_event)) { + pr_err("unable to create perf counter: %ld\n", PTR_ERR(cycles_event)); + goto skip; + } + pmc_index = cycles_event->hw.event_base_rdpmc; + forall(do_it) + + perf_event_release_kernel(cycles_event); +skip: spin_unlock_irqrestore(&lock, flags); forall(report_it) kfree(trial_times); diff --git a/run.sh b/run.sh index 39c4719..7e94e4c 100755 --- a/run.sh +++ b/run.sh @@ -1,32 +1,8 @@ #!/bin/bash set -e -nob_cpus() { - echo "[+] Setting non-boot CPUs to status $1" - for i in /sys/devices/system/cpu/*/online; do - echo "$1" > "$i" - done -} - -noturbo() { - echo "[+] Setting no-turbo to status $1" - if [[ -e /sys/devices/system/cpu/intel_pstate/no_turbo ]]; then - echo "$1" > /sys/devices/system/cpu/intel_pstate/no_turbo - else - local val - [[ $1 == 0 ]] && val=0x850089 - [[ $1 == 1 ]] && val=0x4000850089 - [[ -n $val ]] || return 0 - wrmsr -a 0x1a0 $val - fi -} - [[ -e kbench9000.ko ]] -trap "nob_cpus 1; noturbo 0;" INT TERM EXIT -noturbo 1 -nob_cpus 0 - echo "[+] Inserting module to run tests" stamp="$(date +%s)" insmod kbench9000.ko stamp="$stamp" -- cgit v1.2.3-59-g8ed1b