diff --git a/source/module/monitor_kernel.h b/source/module/monitor_kernel.h index dda9108..de394c7 100644 --- a/source/module/monitor_kernel.h +++ b/source/module/monitor_kernel.h @@ -11,3 +11,5 @@ void monitor_exit(void); int start_watch_variable(watch_arg warg); void clear_watch(pid_t pid); + +enum hrtimer_restart check_variable_cb(struct hrtimer *timer); // callback diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c index b2b0260..6c42005 100644 --- a/source/module/monitor_kernel_lib.c +++ b/source/module/monitor_kernel_lib.c @@ -127,4 +127,56 @@ void clear_watch(pid_t pid) { del_all_kwarg_by_pid(pid); // delete all kwarg with pid free_page_list(pid); // free page with pid start_all_hrTimer(); // restart timer +} + +/** + * @brief main callback function + * + * @param timer + * @return enum hrtimer_restart + */ +enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { + kernel_watch_timer *k_watch_timer = + container_of(timer, kernel_watch_timer, hr_timer); + int i = 0, j = 0; + int buffer[TIMER_MAX_WATCH_NUM]; // Buffer to store the messages + + // // check all watched kernel_watch_arg + // for (i = 0; i < k_watch_timer->sentinel; i++) { + // if (read_and_compare(&k_watch_timer->k_watch_args[i])) { + // // snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), " + // // name: %s, threshold: %lld, pid: %d\n", + // // k_watch_timer->k_watch_args[i].name, + // // k_watch_timer->k_watch_args[i].threshold, + // // k_watch_timer->k_watch_args[i].task_id); + // buffer[j] = i; + // j++; + + // // printk(KERN_INFO "j: name %s, threshold: %lld\n", + // // k_watch_timer->k_watch_args[i].name, + // // k_watch_timer->k_watch_args[i].threshold); + // // printk(KERN_INFO "j: %d\n", j); + // } + // } + if (j > 0) // if any threshold reached + { + printk("-------------------------------------\n"); + printk("-------------watch monitor-----------\n"); + printk("Threshold reached:\n"); + + for (i = 0; i < j; i++) { + printk(" name: %s, threshold: %lld, pid: %d\n", + k_watch_timer->k_watch_args[buffer[i]].name, //! todo + k_watch_timer->k_watch_args[buffer[i]].threshold, + k_watch_timer->k_watch_args[buffer[i]].task_id); + } + // print_task_stack(); + // restart timer after 1s + hrtimer_forward(timer, timer->base->get_time(), ktime_set(1, 0)); //! todo + printk("-------------------------------------\n"); + } else { + // keep frequency + hrtimer_forward(timer, timer->base->get_time(), k_watch_timer->kt); + } + return HRTIMER_RESTART; // restart timer } \ No newline at end of file diff --git a/source/module/monitor_timer.c b/source/module/monitor_timer.c index 2bef95d..dbc57ea 100644 --- a/source/module/monitor_timer.c +++ b/source/module/monitor_timer.c @@ -115,53 +115,6 @@ unsigned char timer_del_watch_by_pid(kernel_watch_timer *timer, pid_t pid) { return 0; } -/// @brief hrTimer handler -enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { - kernel_watch_timer *k_watch_timer = - container_of(timer, kernel_watch_timer, hr_timer); - int i = 0, j = 0; - int buffer[TIMER_MAX_WATCH_NUM]; // Buffer to store the messages - - // // check all watched kernel_watch_arg - // for (i = 0; i < k_watch_timer->sentinel; i++) { - // if (read_and_compare(&k_watch_timer->k_watch_args[i])) { - // // snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), " - // // name: %s, threshold: %lld, pid: %d\n", - // // k_watch_timer->k_watch_args[i].name, - // // k_watch_timer->k_watch_args[i].threshold, - // // k_watch_timer->k_watch_args[i].task_id); - // buffer[j] = i; - // j++; - - // // printk(KERN_INFO "j: name %s, threshold: %lld\n", - // // k_watch_timer->k_watch_args[i].name, - // // k_watch_timer->k_watch_args[i].threshold); - // // printk(KERN_INFO "j: %d\n", j); - // } - // } - if (j > 0) // if any threshold reached - { - printk("-------------------------------------\n"); - printk("-------------watch monitor-----------\n"); - printk("Threshold reached:\n"); - - for (i = 0; i < j; i++) { - printk(" name: %s, threshold: %lld, pid: %d\n", - k_watch_timer->k_watch_args[buffer[i]].name, //! todo - k_watch_timer->k_watch_args[buffer[i]].threshold, - k_watch_timer->k_watch_args[buffer[i]].task_id); - } - // print_task_stack(); - // restart timer after 1s - hrtimer_forward(timer, timer->base->get_time(), ktime_set(1, 0)); //! todo - printk("-------------------------------------\n"); - } else { - // keep frequency - hrtimer_forward(timer, timer->base->get_time(), k_watch_timer->kt); - } - return HRTIMER_RESTART; // restart timer -} - /// @brief start hrTimer /// @param timeout: timeout in us /// @return 0 is success diff --git a/source/module/monitor_timer.h b/source/module/monitor_timer.h index 2367dcc..0feb57c 100644 --- a/source/module/monitor_timer.h +++ b/source/module/monitor_timer.h @@ -54,6 +54,6 @@ unsigned char timer_add_watch(kernel_watch_timer *timer, kernel_watch_arg k_watch_arg); unsigned char timer_del_watch_by_pid(kernel_watch_timer *timer, pid_t pid); -enum hrtimer_restart check_variable_cb(struct hrtimer *timer); // callback +extern enum hrtimer_restart check_variable_cb(struct hrtimer *timer); // callback void start_all_hrTimer(void); void cancel_all_hrTimer(void); \ No newline at end of file