aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 492ddfb0c1948f2defeb32f5979d3c7788015db8 (plain) (blame)
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
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/random.h>

enum { WARMUP = 50, TRIALS = 1000, IDLE = 1 * 1000 };

static unsigned long stamp = 0;
module_param(stamp, ulong, 0);
int dummy;

static char dummy_out[64 * 1024];

static int function(void)
{
	get_random_bytes(dummy_out, sizeof(dummy_out));
	return dummy_out[sizeof(dummy_out) - 1];
}

static int __init mod_init(void)
{
	int ret = 0, i;
	cycles_t start, end;
	unsigned long flags;
	DEFINE_SPINLOCK(lock);
	
	msleep(IDLE);

	spin_lock_irqsave(&lock, flags);
	
	for (i = 0; i < WARMUP; ++i)
		ret |= function();

	start = get_cycles();
	for (i = 0; i < TRIALS; ++i)
		ret |= function();
	end = get_cycles();

	spin_unlock_irqrestore(&lock, flags);
	
	pr_err("%lu: %llu cycles per call\n", stamp, (end - start) / TRIALS);

	/* Don't let compiler be too clever. */
	dummy = ret;
	
	/* We should never actually agree to insert the module. Choosing
	 * -0x1000 here is an amazing hack. It causes the kernel to not
	 * actually load the module, while the standard userspace tools
	 * don't return an error, because it's too big. */
	return -0x1000;
}

module_init(mod_init);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("kBench9000 Cycle Counter");
MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");