watch_arg: greater_flag -> above_threshold

This commit is contained in:
zy
2023-11-27 04:21:47 -05:00
parent a56fa77dc8
commit 7e3b4ded0c
9 changed files with 202 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,13 @@
#ifndef UAPI_MONITOR_H
#define UAPI_MONITOR_H
// monitor_interface.h
#include "monitor_user_sw.h"
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#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);
int cancel_watch(void);
#endif

View File

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

View File

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