From c7f125174036d0671a7abe682cae61788d3dd275 Mon Sep 17 00:00:00 2001 From: Jiaolong He Date: Thu, 13 Jul 2023 10:34:11 +0800 Subject: [PATCH] lib/test_trace_mq_msg: add tracepoint hook example for load/store_msg this is a messaqge queue tracepooint tester, provide am example hook to ensure its usability. Signed-off-by: Jiaolong He --- lib/Kconfig.debug | 9 ++++++ lib/Makefile | 1 + lib/test_trace_mq_msg.c | 71 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 lib/test_trace_mq_msg.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ff8a6f98a53e..d990e1762b36 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2942,6 +2942,15 @@ config TEST_RT_WQ If unsure, say N. +config TEST_TRACE_MQ_MSG + tristate "message queue tracepoint tester" + depends on TRACEPOINTS + help + This driver provides an example hook of trace_load_msg and + trace_store_msg to ensure its usability. + + If unsure, say N. + endif # RUNTIME_TESTING_MENU config ARCH_USE_MEMTEST diff --git a/lib/Makefile b/lib/Makefile index c56050f49329..25804e9c46fd 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -108,6 +108,7 @@ obj-$(CONFIG_TEST_REF_TRACKER) += test_ref_tracker.o CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE) obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o obj-$(CONFIG_TEST_RT_WQ) += test_rt_wq.o +obj-$(CONFIG_TEST_TRACE_MQ_MSG) += test_trace_mq_msg.o # # CFLAGS for compiling floating point code inside the kernel. x86/Makefile turns diff --git a/lib/test_trace_mq_msg.c b/lib/test_trace_mq_msg.c new file mode 100644 index 000000000000..ae7cb7410f01 --- /dev/null +++ b/lib/test_trace_mq_msg.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * messaqge queue tracepooint tester + * Copyright (C) 2023 Jiaolong He + * + * This driver provides an example hook of trace_load_msg and + * trace_store_msg to ensure its usability. + * + */ + +#include +#include +#include +#include +#include +#include <../ipc/util.h> + + + +static atomic_t load_cnt = ATOMIC_INIT(0); +static atomic_t store_cnt = ATOMIC_INIT(0); + +void load_hook(void *ignore, struct msg_msg *msg, size_t len) +{ + atomic_inc(&load_cnt); +} + + +void store_hook(void *ignore, struct msg_msg *msg, size_t len) +{ + atomic_inc(&store_cnt); +} + + +static void register_tp(struct tracepoint *tp, void *priv) +{ + if (!strcmp(tp->name, "load_msg")) + WARN_ON(tracepoint_probe_register(tp, load_hook, NULL)); + if (!strcmp(tp->name, "store_msg")) + WARN_ON(tracepoint_probe_register(tp, store_hook, NULL)); +} + + +static void unregister_tp(struct tracepoint *tp, void *priv) +{ + if (!strcmp(tp->name, "load_msg")) + WARN_ON(tracepoint_probe_unregister(tp, load_hook, NULL)); + if (!strcmp(tp->name, "store_msg")) + WARN_ON(tracepoint_probe_unregister(tp, store_hook, NULL)); +} + + +static int __init test_mq_trace_init(void) +{ + for_each_kernel_tracepoint(register_tp, NULL); + + return 0; +} + +static void __exit test_mq_trace_exit(void) +{ + for_each_kernel_tracepoint(unregister_tp, NULL); + tracepoint_synchronize_unregister(); + WARN_ON(atomic_read(&load_cnt) != atomic_read(&store_cnt)); +} + +module_init(test_mq_trace_init); +module_exit(test_mq_trace_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("hejiaolong@kernelsoft.com");