// SPDX-License-Identifier: GPL-2.0+ #define pr_fmt(fmt) "avm_kpi_test: " fmt #include "avm_kpi.h" #include #include #include #define MAX_TEST_ATTR 32u #define MAX_TEST_SAMPLER 16u static struct kpi_node *test_node; static struct kpi_attribute_absolute *attributes[MAX_TEST_ATTR]; static struct kpi_sampler *samplers[MAX_TEST_SAMPLER]; static u32 positive_sequence(__maybe_unused void *ctx) { static u32 i; return i++; } static s32 negative_sequence(__maybe_unused void *ctx) { static s32 i; return 0 - i++; } static s32 random_32(__maybe_unused void *ctx) { s32 num; get_random_bytes(&num, sizeof(num)); return num; } static s64 random_64(__maybe_unused void *ctx) { s64 num; get_random_bytes(&num, sizeof(num)); return num; } static void str_collect(__maybe_unused void *ctx, char *buf, __maybe_unused size_t buffer_len) { strcpy(buf, "Hello, World!"); } static int __init avm_kpi_test_init(void) { const enum kpi_reducer reducers[] = { KPI_REDUCER_NONE, KPI_REDUCER_MIN, KPI_REDUCER_MAX, KPI_REDUCER_SUM, KPI_REDUCER_AVG, KPI_REDUCER_MEDIAN, KPI_REDUCER_VARIANCE, _KPI_REDUCER_BOTTOM_, }; const struct kpi_sliding_window window = { .size_seconds = 60, .num_samples = 12, }; struct kpi_node *system_node; unsigned int a, s; system_node = kpi_get_section("system"); if (!system_node) return -EPERM; test_node = create_kpi_node("test", NULL); if (!test_node) return -EPERM; attributes[a++] = kpi_add_attr_u32("sessions", test_node, positive_sequence, NULL); attributes[a++] = kpi_add_attr_string("version", test_node, str_collect, NULL, 100u); attributes[a++] = kpi_add_attr_string("test", system_node, str_collect, NULL, 100u); samplers[s++] = kpi_add_sampler_u32("psequence", test_node, positive_sequence, NULL, window, reducers); samplers[s++] = kpi_add_sampler_s32("nsequence", test_node, negative_sequence, NULL, window, reducers); samplers[s++] = kpi_add_sampler_s32("rand32", test_node, random_32, NULL, window, reducers); samplers[s++] = kpi_add_sampler_s64("rand64", test_node, random_64, NULL, window, reducers); WARN_ON(a > MAX_TEST_ATTR); WARN_ON(s > MAX_TEST_SAMPLER); return 0; } module_init(avm_kpi_test_init); static void __exit avm_kpi_test_exit(void) { unsigned int i; for (i = 0; attributes[i]; ++i) kpi_delete_attribute(attributes[i]); for (i = 0; samplers[i]; ++i) kpi_delete_sampler(samplers[i]); destroy_kpi_node(test_node); } module_exit(avm_kpi_test_exit);