aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/ubench.c
blob: 89220a577759de45416a9720fbb4355c4f146b51 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * ubench.c
 *
 * The sole purpose of this module is to help with debugging of system
 * debug tools.
 *
 * Copyright (C) 2015 Linaro Limited
 *                    Daniel Thompson <daniel.thompson@linaro.org>
 */

#define pr_fmt(fmt) "ubench[%u]: " fmt, smp_processor_id()

#include <linux/smp.h>
#include <linux/debugfs.h>
#include <linux/debug_locks.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/irq_work.h>


static int ubench_do_action_on_cpu(int cpu, long (*action)(void *))
{
	if (cpu == -1)
		(void) action(NULL);

	if (cpu < 0 || cpu > num_possible_cpus())
		return -EINVAL;

	pr_info("About to run %pf on cpu %d\n", action, cpu);

	/* work_on_cpu() ends up performing an uninterruptible
	 * wait-for-completion. This means we'll lose the prompt
	 * whilst things run regardless of the CPU we send the
	 * work to.
	 */
	work_on_cpu(cpu, action, NULL);

	return 0;
}

#define DEFINE_UBENCH_ATTRIBUTE(__fops, __action)                              \
	static int __fops##_get(void *data, u64 *val)                          \
	{                                                                      \
		*val = __action(NULL);                                         \
		return 0;                                                      \
	}                                                                      \
	static int __fops##_set(void *data, u64 val)                           \
	{                                                                      \
		ubench_do_action_on_cpu(val, __action);                        \
		return 0;                                                      \
	}                                                                      \
	DEFINE_SIMPLE_ATTRIBUTE(__fops, __fops##_get, __fops##_set, "%llu\n")

static long do_ubench_local_irq_disable(void *info)
{
	int i;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		local_irq_disable();
		local_irq_enable();
	}

	pr_info("local_irq_disable %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_local_irq_disable_fops,
			do_ubench_local_irq_disable);

static long do_ubench_local_irq_save(void *info)
{
	int i;
	unsigned long flags;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		local_irq_save(flags);
		local_irq_restore(flags);
	}

	pr_info("local_irq_save %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_local_irq_save_fops, do_ubench_local_irq_save);

static DEFINE_SPINLOCK(ubench_lock);

static long do_ubench_spin_lock_irq(void *info)
{
	int i;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		spin_lock_irq(&ubench_lock);
		spin_unlock_irq(&ubench_lock);
	}

	pr_info("spin_lock_irq %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_spin_lock_irq_fops, do_ubench_spin_lock_irq);

static long do_ubench_spin_lock_irqsave(void *info)
{
	int i;
	unsigned long flags;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		spin_lock_irqsave(&ubench_lock, flags);
		spin_unlock_irqrestore(&ubench_lock, flags);
	}

	pr_info("spin_lock_irqsave %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_spin_lock_irqsave_fops,
			do_ubench_spin_lock_irqsave);

static DEFINE_RWLOCK(ubench_rwlock);

static long do_ubench_read_lock_irq(void *info)
{
	int i;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		read_lock_irq(&ubench_rwlock);
		read_unlock_irq(&ubench_rwlock);
	}

	pr_info("read_lock_irq %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_read_lock_irq_fops, do_ubench_read_lock_irq);

static long do_ubench_read_lock_irqsave(void *info)
{
	int i;
	unsigned long flags;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		read_lock_irqsave(&ubench_rwlock, flags);
		read_unlock_irqrestore(&ubench_rwlock, flags);
	}

	pr_info("read_lock_irqsave %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_read_lock_irqsave_fops,
			do_ubench_read_lock_irqsave);

static long do_ubench_write_lock_irq(void *info)
{
	int i;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		write_lock_irq(&ubench_rwlock);
		write_unlock_irq(&ubench_rwlock);
	}

	pr_info("write_lock_irq %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_write_lock_irq_fops, do_ubench_write_lock_irq);

static long do_ubench_write_lock_irqsave(void *info)
{
	int i;
	unsigned long flags;
	unsigned long long t = sched_clock();

	for (i = 0; i < 1000000; i++) {
		write_lock_irqsave(&ubench_rwlock, flags);
		write_unlock_irqrestore(&ubench_rwlock, flags);
	}

	pr_info("write_lock_irqsave %llu\n", sched_clock() - t);
	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_write_lock_irqsave_fops,
			do_ubench_write_lock_irqsave);

static long do_ubench_all(void *info)
{
	do_ubench_local_irq_disable(NULL);
	do_ubench_local_irq_save(NULL);
	do_ubench_spin_lock_irq(NULL);
	do_ubench_spin_lock_irqsave(NULL);
	do_ubench_read_lock_irq(NULL);
	do_ubench_read_lock_irqsave(NULL);
	do_ubench_write_lock_irq(NULL);
	do_ubench_write_lock_irqsave(NULL);

	return 0;
}

DEFINE_UBENCH_ATTRIBUTE(ubench_all_fops, do_ubench_all);

#define E(x) { #x, &ubench_##x##_fops }
const struct
{
	const char *name;
	const struct file_operations *fops;
} ubench_fops_table[] = {
	E(local_irq_disable),
	E(local_irq_save),
	E(spin_lock_irq),
	E(spin_lock_irqsave),
	E(read_lock_irq),
	E(read_lock_irqsave),
	E(write_lock_irq),
	E(write_lock_irqsave),
	E(all),
};
#undef E

static int __init ubench_init(void)
{
	struct dentry *dir;
	unsigned int i;

	dir = debugfs_create_dir("ubench", NULL);
	if (dir)
		for (i = 0; i < ARRAY_SIZE(ubench_fops_table); i++)
			(void)debugfs_create_file(ubench_fops_table[i].name,
						  S_IRUGO | S_IWUSR, dir, NULL,
						  ubench_fops_table[i].fops);

	return 0;
}

module_init(ubench_init);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Daniel Thompson");