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 <hejiaolong@kernelsoft.com>
This commit is contained in:
Jiaolong He 2023-07-13 10:34:11 +08:00 committed by Xinpeng Liu
parent 259eb72da2
commit c7f1251740
3 changed files with 81 additions and 0 deletions

View File

@ -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

View File

@ -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

71
lib/test_trace_mq_msg.c Normal file
View File

@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* messaqge queue tracepooint tester
* Copyright (C) 2023 Jiaolong He <hejiaolong@kernelsoft.com>
*
* This driver provides an example hook of trace_load_msg and
* trace_store_msg to ensure its usability.
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/tracepoint.h>
#include <linux/msg.h>
#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");