Skip to content

Commit

Permalink
samples: add a benchmark for smp_processor_id()
Browse files Browse the repository at this point in the history
This module can be used to benchmark different implementations of
smp_processor_id();

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
  • Loading branch information
puranjaymohan committed May 1, 2024
1 parent 18daea7 commit 77d3fdd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions samples/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ config SAMPLE_CGROUP
help
Build samples that demonstrate the usage of the cgroup API.

config SAMPLE_SMP_PROCESSOR_ID
tristate "Build a benchmark for smp_processor_id()"
help
This builds a bechmark that calls smp_processor_id() a given
number of times and calculates the runtime overhead.

source "samples/rust/Kconfig"

endif # SAMPLES
Expand Down
1 change: 1 addition & 0 deletions samples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ obj-$(CONFIG_SAMPLE_KMEMLEAK) += kmemleak/
obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/
obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/
obj-$(CONFIG_SAMPLES_RUST) += rust/
obj-$(CONFIG_SAMPLE_SMP_PROCESSOR_ID) += smp_processor_id/
3 changes: 3 additions & 0 deletions samples/smp_processor_id/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only

obj-$(CONFIG_SAMPLE_SMP_PROCESSOR_ID) += smp_processor_id.o
55 changes: 55 additions & 0 deletions samples/smp_processor_id/smp_processor_id.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: GPL-2.0-only

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/ktime.h>
#include <linux/module.h>
#include <linux/smp.h>

/*
* Arbitrary large value chosen to be sufficiently large to minimize noise but
* sufficiently small to complete quickly.
*/
static unsigned int nr_function_calls = 1000000;
module_param(nr_function_calls, uint, 0);
MODULE_PARM_DESC(nr_function_calls, "How many times to call the smp_processor_id()");

static noinline int get_cpu_id(void)
{
return smp_processor_id();
}

static int __init smp_processor_id_sample_init(void)
{
volatile int cpu;
ktime_t start, end;
u64 period;

start = ktime_get();
for (unsigned int i = 0; i < nr_function_calls; i++)
cpu = get_cpu_id();
end = ktime_get();

period = ktime_to_ns(ktime_sub(end, start));

pr_info("Attempted %u calls to %ps in %lluns (%lluns / call)\n",
nr_function_calls, get_cpu_id,
period, div_u64(period, nr_function_calls));

/*
* The benchmark completed successfully, but there's no reason to keep
* the module around. Return an error do the user doesn't have to
* manually unload the module.
*/
return -EINVAL;
}
module_init(smp_processor_id_sample_init);

static void __exit smp_processor_id_sample_exit(void)
{
}
module_exit(smp_processor_id_sample_exit);

MODULE_AUTHOR("Puranjay Mohan");
MODULE_DESCRIPTION("Benchmark for smp_processor_id function");
MODULE_LICENSE("GPL");

0 comments on commit 77d3fdd

Please sign in to comment.