From b8712f8ecc524fc42f6af2aab17052f1b05f1ec5 Mon Sep 17 00:00:00 2001 From: compudj Date: Tue, 9 Jan 2007 00:11:15 +0000 Subject: [PATCH] add test local git-svn-id: http://ltt.polymtl.ca/svn@2325 04897980-b3bd-0310-b5e0-8ef037075253 --- tests/kernel/Makefile | 1 + tests/kernel/test-local.c | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/kernel/test-local.c diff --git a/tests/kernel/Makefile b/tests/kernel/Makefile index 07661469..23b718bd 100644 --- a/tests/kernel/Makefile +++ b/tests/kernel/Makefile @@ -11,6 +11,7 @@ endif obj-m += test-debugfs.o obj-m += test-hotplug.o obj-m += rdtsc-smp.o + obj-m += test-local.o # obj-m += test-cmpxchg.o # obj-m += test-cmpxchg-nolock.o # obj-m += test-spinlock.o diff --git a/tests/kernel/test-local.c b/tests/kernel/test-local.c new file mode 100644 index 00000000..3f74375a --- /dev/null +++ b/tests/kernel/test-local.c @@ -0,0 +1,69 @@ +/* test-local.c + * + * Sample module for local.h usage. + */ + + +#include +#include +#include +#include + +static DEFINE_PER_CPU(local_t, counters) = LOCAL_INIT(0); + +static struct timer_list test_timer; + +/* IPI called on each CPU. */ +static void test_each(void *info) +{ + /* Increment the counter from a non preemptible context */ + printk("Increment on cpu %d\n", smp_processor_id()); + local_inc(&__get_cpu_var(counters)); + + /* This is what incrementing the variable would look like within a + * preemptible context (it disables preemption) : + * + * local_inc(&get_cpu_var(counters)); + * put_cpu_var(counters); + */ +} +static void do_test_timer(unsigned long data) +{ + int cpu; + + /* Increment the counters */ + on_each_cpu(test_each, NULL, 0, 1); + /* Read all the counters */ + printk("Counters read from CPU %d\n", smp_processor_id()); + for_each_online_cpu(cpu) { + printk("Read : CPU %d, count %ld\n", cpu, + local_read(&per_cpu(counters, cpu))); + } + del_timer(&test_timer); + test_timer.expires = jiffies + 1000; + add_timer(&test_timer); +} + +static int __init test_init(void) +{ + /* initialize the timer that will increment the counter */ + init_timer(&test_timer); + test_timer.function = do_test_timer; + test_timer.expires = jiffies + 1; + add_timer(&test_timer); + + return 0; +} + +static void __exit test_exit(void) +{ + del_timer_sync(&test_timer); +} + +module_init(test_init); +module_exit(test_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Mathieu Desnoyers"); +MODULE_DESCRIPTION("Local Atomic Ops"); + -- 2.34.1