Skip to content

Commit 77d3fdd

Browse files
committed
samples: add a benchmark for smp_processor_id()
This module can be used to benchmark different implementations of smp_processor_id(); Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
1 parent 18daea7 commit 77d3fdd

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

samples/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ config SAMPLE_CGROUP
291291
help
292292
Build samples that demonstrate the usage of the cgroup API.
293293

294+
config SAMPLE_SMP_PROCESSOR_ID
295+
tristate "Build a benchmark for smp_processor_id()"
296+
help
297+
This builds a bechmark that calls smp_processor_id() a given
298+
number of times and calculates the runtime overhead.
299+
294300
source "samples/rust/Kconfig"
295301

296302
endif # SAMPLES

samples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ obj-$(CONFIG_SAMPLE_KMEMLEAK) += kmemleak/
3939
obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/
4040
obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/
4141
obj-$(CONFIG_SAMPLES_RUST) += rust/
42+
obj-$(CONFIG_SAMPLE_SMP_PROCESSOR_ID) += smp_processor_id/

samples/smp_processor_id/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
obj-$(CONFIG_SAMPLE_SMP_PROCESSOR_ID) += smp_processor_id.o
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4+
5+
#include <linux/ktime.h>
6+
#include <linux/module.h>
7+
#include <linux/smp.h>
8+
9+
/*
10+
* Arbitrary large value chosen to be sufficiently large to minimize noise but
11+
* sufficiently small to complete quickly.
12+
*/
13+
static unsigned int nr_function_calls = 1000000;
14+
module_param(nr_function_calls, uint, 0);
15+
MODULE_PARM_DESC(nr_function_calls, "How many times to call the smp_processor_id()");
16+
17+
static noinline int get_cpu_id(void)
18+
{
19+
return smp_processor_id();
20+
}
21+
22+
static int __init smp_processor_id_sample_init(void)
23+
{
24+
volatile int cpu;
25+
ktime_t start, end;
26+
u64 period;
27+
28+
start = ktime_get();
29+
for (unsigned int i = 0; i < nr_function_calls; i++)
30+
cpu = get_cpu_id();
31+
end = ktime_get();
32+
33+
period = ktime_to_ns(ktime_sub(end, start));
34+
35+
pr_info("Attempted %u calls to %ps in %lluns (%lluns / call)\n",
36+
nr_function_calls, get_cpu_id,
37+
period, div_u64(period, nr_function_calls));
38+
39+
/*
40+
* The benchmark completed successfully, but there's no reason to keep
41+
* the module around. Return an error do the user doesn't have to
42+
* manually unload the module.
43+
*/
44+
return -EINVAL;
45+
}
46+
module_init(smp_processor_id_sample_init);
47+
48+
static void __exit smp_processor_id_sample_exit(void)
49+
{
50+
}
51+
module_exit(smp_processor_id_sample_exit);
52+
53+
MODULE_AUTHOR("Puranjay Mohan");
54+
MODULE_DESCRIPTION("Benchmark for smp_processor_id function");
55+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)