From 7e3b4ded0ccfe149a96aec009836bcb35293fb63 Mon Sep 17 00:00:00 2001 From: zy Date: Mon, 27 Nov 2023 04:21:47 -0500 Subject: [PATCH] watch_arg: greater_flag -> above_threshold --- source/module/monitor_kernel.c | 1 + source/module/monitor_kernel_lib.c | 4 +- source/module/monitor_mem.c | 4 +- source/module/monitor_mem.h | 2 +- source/module/monitor_timer.h | 4 +- source/uapi/monitor_user.c | 14 +++ source/uapi/monitor_user.h | 16 ++- source/uapi/monitor_user_sw.h | 165 +++++++++++++++++++++++++++++ testcase/hptest.c | 2 +- 9 files changed, 202 insertions(+), 10 deletions(-) create mode 100644 source/uapi/monitor_user_sw.h diff --git a/source/module/monitor_kernel.c b/source/module/monitor_kernel.c index fd26828..2e6e35c 100644 --- a/source/module/monitor_kernel.c +++ b/source/module/monitor_kernel.c @@ -67,6 +67,7 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num, "time_ns=%ld, threshold=%lld\n", warg.task_id, warg.name, warg.ptr, warg.length_byte, warg.time_ns, warg.threshold); + warg.time_ns = warg.time_ns == 0 ? 10000 : warg.time_ns; // default 10us // start watch variable start_watch_variable(warg); break; diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c index f7032f1..2d12ef6 100644 --- a/source/module/monitor_kernel_lib.c +++ b/source/module/monitor_kernel_lib.c @@ -29,7 +29,7 @@ static unsigned char w_arg2k_w_arg(void *kptr, watch_arg warg, k_watch_arg->length_byte = warg.length_byte; k_watch_arg->threshold = warg.threshold; k_watch_arg->unsigned_flag = warg.unsigned_flag; - k_watch_arg->greater_flag = warg.greater_flag; + k_watch_arg->above_threshold = warg.above_threshold; return 0; } @@ -296,7 +296,7 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { // check all watched kernel_watch_arg for (i = 0; i < k_watch_timer->sentinel; i++) { kwarg = &k_watch_timer->k_watch_args[i]; - if (read_and_compare(kwarg->kptr, kwarg->length_byte, kwarg->greater_flag, + if (read_and_compare(kwarg->kptr, kwarg->length_byte, kwarg->above_threshold, kwarg->unsigned_flag, kwarg->threshold)) { k_watch_timer->threshold_buffer[j] = i; j++; diff --git a/source/module/monitor_mem.c b/source/module/monitor_mem.c index 17375a0..e2dc096 100644 --- a/source/module/monitor_mem.c +++ b/source/module/monitor_mem.c @@ -161,7 +161,7 @@ static int func_indices[2][9] = {{0, 0, 1, 0, 2, 0, 0, 0, 3}, /// @brief read k_arg->kptr and compare with threshold /// @param k_arg /// @return result of compare -unsigned char read_and_compare(void *ptr, int len, unsigned char greater_flag, +unsigned char read_and_compare(void *ptr, int len, unsigned char above_threshold, unsigned char is_unsigned, long long threshold) { unsigned char result = 0; @@ -177,7 +177,7 @@ unsigned char read_and_compare(void *ptr, int len, unsigned char greater_flag, // %d \n", k_arg->name, *(int *)ptr, // threshold, result); - if (greater_flag) + if (above_threshold) return result; else return !result; diff --git a/source/module/monitor_mem.h b/source/module/monitor_mem.h index 391c609..394d900 100644 --- a/source/module/monitor_mem.h +++ b/source/module/monitor_mem.h @@ -23,5 +23,5 @@ void *convert_user_space_ptr(pid_t pid, unsigned long kaddr); void free_page_list(pid_t task_id); void free_all_page_list(void); -unsigned char read_and_compare(void *ptr, int len, unsigned char greater_flag, +unsigned char read_and_compare(void *ptr, int len, unsigned char above_threshold, unsigned char is_unsigned, long long threshold); diff --git a/source/module/monitor_timer.h b/source/module/monitor_timer.h index 3253ba9..3db413e 100644 --- a/source/module/monitor_timer.h +++ b/source/module/monitor_timer.h @@ -12,7 +12,7 @@ typedef struct { int length_byte; // byte long long threshold; // threshold value unsigned char unsigned_flag; // unsigned flag (true: unsigned, false: signed) - unsigned char greater_flag; // reverse flag (true: >, false: <) + unsigned char above_threshold; // reverse flag (true: >, false: <) unsigned long time_ns; // timer interval (ns) } watch_arg; @@ -24,7 +24,7 @@ typedef struct { int length_byte; // byte long long threshold; // threshold value unsigned char unsigned_flag; // unsigned flag (true: unsigned, false: signed) - unsigned char greater_flag; // reverse flag (true: >, false: <) + unsigned char above_threshold; // reverse flag (true: >, false: <) } kernel_watch_arg; typedef struct { diff --git a/source/uapi/monitor_user.c b/source/uapi/monitor_user.c index 9305675..1a90008 100644 --- a/source/uapi/monitor_user.c +++ b/source/uapi/monitor_user.c @@ -42,3 +42,17 @@ int cancel_watch() { file_desc = -1; return 0; } + +void init_watch_arg(watch_arg *wg, char *name, void *ptr, + int length_byte, long long threshold, + unsigned char unsigned_flag, + unsigned char above_threshold, unsigned long time_ns){ + wg->task_id = getpid(); + strncpy(wg->name, name, (MAX_NAME_LEN + 1)); + wg->ptr = ptr; + wg->length_byte = length_byte; + wg->threshold = threshold; + wg->unsigned_flag = unsigned_flag; + wg->above_threshold = above_threshold; + wg->time_ns = time_ns; +} diff --git a/source/uapi/monitor_user.h b/source/uapi/monitor_user.h index f4d9df1..257217f 100644 --- a/source/uapi/monitor_user.h +++ b/source/uapi/monitor_user.h @@ -1,7 +1,13 @@ +#ifndef UAPI_MONITOR_H +#define UAPI_MONITOR_H // monitor_interface.h +#include "monitor_user_sw.h" +#include #include +#include #define MAX_NAME_LEN (15) // max name length + typedef struct { pid_t task_id; // current process id char name[MAX_NAME_LEN + 1]; // name @@ -9,12 +15,18 @@ typedef struct { int length_byte; // byte long long threshold; // threshold value unsigned char unsigned_flag; // unsigned flag (true: unsigned, false: signed) - unsigned char greater_flag; // reverse flag (true: >, false: <) + unsigned char above_threshold; // reverse flag (true: >, false: <) unsigned long time_ns; // timer interval (ns) } watch_arg; +void init_watch_arg(watch_arg *wg, char *name, void *ptr, int length_byte, + long long threshold, unsigned char unsigned_flag, + unsigned char above_threshold, unsigned long time_ns); + // start watch int start_watch(watch_arg w_arg); // cancel watch -int cancel_watch(void); \ No newline at end of file +int cancel_watch(void); + +#endif \ No newline at end of file diff --git a/source/uapi/monitor_user_sw.h b/source/uapi/monitor_user_sw.h new file mode 100644 index 0000000..15c19e5 --- /dev/null +++ b/source/uapi/monitor_user_sw.h @@ -0,0 +1,165 @@ +#ifndef UAPI_MONITOR_SW_H +#define UAPI_MONITOR_SW_H + +#define SWATCH_CHAR(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(char); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 0; \ + w_arg.above_threshold = 0; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_CHAR_LESS(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(char); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 0; \ + w_arg.above_threshold = 1; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_UCHAR(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(unsigned char); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 1; \ + w_arg.above_threshold = 0; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_UCHAR_LESS(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(unsigned char); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 1; \ + w_arg.above_threshold = 1; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_INT(name, ptr, threshold) \ + do { \ + watch_arg w_arg = {0}; \ + init_watch_arg(&w_arg, name, ptr, sizeof(int), threshold, 0, 1, 0); \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_INT_LESS(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(int); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 0; \ + w_arg.above_threshold = 1; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_UINT(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(unsigned int); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 1; \ + w_arg.above_threshold = 0; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_UINT_LESS(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(unsigned int); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 1; \ + w_arg.above_threshold = 1; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_LONG(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(long); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 0; \ + w_arg.above_threshold = 0; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_LONG_LESS(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(long); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 0; \ + w_arg.above_threshold = 1; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_ULONG(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(unsigned long); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 1; \ + w_arg.above_threshold = 0; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#define SWATCH_ULONG_LESS(name, ptr, threshold) \ + do { \ + watch_arg w_arg; \ + w_arg.task_id = getpid(); \ + strncpy(w_arg.name, name, MAX_NAME_LEN); \ + w_arg.ptr = ptr; \ + w_arg.length_byte = sizeof(unsigned long); \ + w_arg.threshold = threshold; \ + w_arg.unsigned_flag = 1; \ + w_arg.above_threshold = 1; \ + w_arg.time_ns = 0; \ + start_watch(w_arg); \ + } while (0) + +#endif \ No newline at end of file diff --git a/testcase/hptest.c b/testcase/hptest.c index 2b53088..82263c1 100644 --- a/testcase/hptest.c +++ b/testcase/hptest.c @@ -37,7 +37,7 @@ int main() { .length_byte = sizeof(int), .threshold = 20, .unsigned_flag = 0, - .greater_flag = 1, + .above_threshold = 1, .time_ns = 2000, // on hyper-v, 1us will block all system. 2us just fine, maybe 1us is too short for hyper-v }; start_watch(w_arg);