check_variable_cb

This commit is contained in:
zy
2023-11-17 13:43:19 +08:00
parent eebf3a59b2
commit 7dfe7c520b
2 changed files with 52 additions and 61 deletions

View File

@@ -1,12 +1,12 @@
#include "monitor_kernel.h"
#include <linux/sched.h>
#include <linux/sched/task.h>
#include <linux/sched/mm.h>
// #include <linux/sched/task.h>
// #include <linux/sched/mm.h>
#define __task_contributes_to_load(task) \
((READ_ONCE(task->__state) & TASK_UNINTERRUPTIBLE) != 0 && \
(task->flags & PF_FROZEN) == 0 && \
#define __task_contributes_to_load(task) \
((READ_ONCE(task->__state) & TASK_UNINTERRUPTIBLE) != 0 && \
(task->flags & PF_FROZEN) == 0 && \
(READ_ONCE(task->__state) & TASK_NOLOAD) == 0)
/**
@@ -21,8 +21,8 @@ static unsigned char w_arg2k_w_arg(void *kptr, watch_arg warg,
kernel_watch_arg *k_watch_arg) {
// k_watch_arg init
k_watch_arg->task_id = warg.task_id;
strncpy(k_watch_arg->name, warg.name, MAX_NAME_LEN + 1); // name
k_watch_arg->name[MAX_NAME_LEN + 1] = '\0'; // just in case
strncpy(k_watch_arg->name, warg.name, MAX_NAME_LEN + 1); // name
k_watch_arg->name[MAX_NAME_LEN + 1] = '\0'; // just in case
k_watch_arg->ptr = warg.ptr;
k_watch_arg->kptr = kptr;
k_watch_arg->length_byte = warg.length_byte;
@@ -47,7 +47,7 @@ static void init_mm_tree(mm_tree *mm_tree) {
}
static int init_buffer(unsigned int buf_size) {
init_mm_tree(&mm_tree_struct); // init mm_tree
init_mm_tree(&mm_tree_struct); // init mm_tree
init_diag_variant_buffer(&load_monitor_variant_buffer, buf_size);
int ret = 0;
ret = alloc_diag_variant_buffer(&load_monitor_variant_buffer);
@@ -75,10 +75,12 @@ static void clear_all_watch(void) {
int monitor_init(void) {
int ret = 0;
ret = init_orig_fun(); // init orig_X
if (ret) return ret;
ret = init_buffer(50 * 1024 * 1024); // 50M
if (ret) return -1;
ret = init_orig_fun(); // init orig_X
if (ret)
return ret;
ret = init_buffer(50 * 1024 * 1024); // 50M
if (ret)
return -1;
return 0;
}
@@ -120,7 +122,7 @@ int start_watch_variable(watch_arg warg) {
}
// k_watch_arg init
w_arg2k_w_arg(kptr, warg, &k_watch_arg);
timer = get_timer(warg.time_ns); // get a valuable timer
timer = get_timer(warg.time_ns); // get a valuable timer
printk(KERN_INFO "ptr transform kptr: %p\n", kptr);
printk(KERN_INFO "timer: %p\n", timer);
@@ -128,7 +130,7 @@ int start_watch_variable(watch_arg warg) {
timer->sentinel, timer->time_ns);
printk(KERN_INFO "timer->hr_timer: %p\n", &timer->hr_timer);
TIMER_CANCEL(timer); // just in case
TIMER_CANCEL(timer); // just in case
timer_add_watch(timer, k_watch_arg);
TIMER_START(timer);
@@ -143,10 +145,10 @@ int start_watch_variable(watch_arg warg) {
*/
void clear_watch(pid_t pid) {
printk(KERN_INFO "clear pid %d 's watch variable\n", pid);
cancel_all_hrTimer(); // just in case
del_all_kwarg_by_pid(pid); // delete all kwarg with pid
free_page_list(pid); // free page with pid
start_all_hrTimer(); // restart timer
cancel_all_hrTimer(); // just in case
del_all_kwarg_by_pid(pid); // delete all kwarg with pid
free_page_list(pid); // free page with pid
start_all_hrTimer(); // restart timer
}
/**
@@ -159,7 +161,7 @@ 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
int buffer[TIMER_MAX_WATCH_NUM]; // Buffer to store the messages
kernel_watch_arg *kwarg;
// check all watched kernel_watch_arg
@@ -171,14 +173,14 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) {
j++;
}
}
if (j > 0) // if any threshold reached
if (j > 0) // if any threshold reached
{
struct task_struct *g, *p; // g: task group; p: task
struct task_struct *g, *p; // g: task group; p: task
unsigned long flags;
variable_monitor_task tsk_info;
unsigned long event_id = get_cycles();
variable_monitor_record vm_record = {.id = event_id,
.et_type = 1, //! todo event type
.et_type = 1, //! todo event type
.tv = ktime_get_real(),
.threshold_record = {0}};
@@ -198,52 +200,40 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) {
diag_variant_buffer_seal(&load_monitor_variant_buffer);
diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags);
// do_each_thread(g, p) {
// if (p->__state == TASK_RUNNING || __task_contributes_to_load(p) ||
// p->__state == TASK_IDLE) {
// tsk_info.et_type = 2; //! todo event type
// tsk_info.id = event_id;
// tsk_info.tv = vm_record.tv;
do_each_thread(g, p) {
if (p->__state == TASK_RUNNING || __task_contributes_to_load(p) ||
p->__state == TASK_IDLE) {
tsk_info.et_type = 2; //! todo event type
tsk_info.id = event_id;
tsk_info.tv = vm_record.tv;
// diag_task_brief(p, &tsk_info.task); // task 相关信息
// diag_task_user_stack(tsk, &tsk_info.user_stack);
// diag_task_kern_stack(p, &tsk_info.kern_stack); // 内核栈相关信息
// dump_proc_chains_argv(load_monitor_settings.style, &mm_tree, p,
// &tsk_info.proc_chains); // 进程链相关信息
// }
// } while_each_thread(g, p);
diag_task_brief(p, &tsk_info.task); // task brief
diag_task_user_stack(p, &tsk_info.user_stack); // user stack
diag_task_kern_stack(p, &tsk_info.kern_stack); // kernel stack
dump_proc_chains_argv(1, p, &mm_tree_struct,
&tsk_info.proc_chains); // proc chains
diag_variant_buffer_spin_lock(&load_monitor_variant_buffer, flags);
diag_variant_buffer_reserve(&load_monitor_variant_buffer,
sizeof(variable_monitor_task));
diag_variant_buffer_write_nolock(&load_monitor_variant_buffer,
&tsk_info,
sizeof(variable_monitor_task));
diag_variant_buffer_seal(&load_monitor_variant_buffer);
diag_variant_buffer_spin_unlock(&load_monitor_variant_buffer, flags);
}
}
while_each_thread(g, p);
rcu_read_unlock();
// print_task_stack();
// restart timer after 1s
hrtimer_forward(timer, timer->base->get_time(), ktime_set(1, 0)); //! todo
// restart timer after 5s
hrtimer_forward(timer, timer->base->get_time(), ktime_set(5, 0)); //! todo
printk("-------------------------------------\n");
} else {
// keep frequency
hrtimer_forward(timer, timer->base->get_time(), k_watch_timer->kt);
}
return HRTIMER_RESTART; // restart timer
}
// static void print_task_stack(void) {
// struct task_struct *g, *p; // g: task group; p: task
// unsigned long backtrace[BACKTRACE_DEPTH]; // save stack
// unsigned int nr_bt; // stack depth
// unsigned long long current_time; // last time
// current_time = ktime_get_real();
// printk("Timestamp (ns): %lld\n", current_time);
// rcu_read_lock(); // lock run queue
// // printk("Running task\n");
// do_each_thread(g, p) {
// if (p->__state == TASK_RUNNING || __task_contributes_to_load(p) ||
// p->__state == TASK_IDLE) {
// printk("task: %s, pid %d, state %d\n", p->comm, p->pid,
// p->__state); //! todo
// nr_bt = orig_stack_trace_save_tsk(p, backtrace, BACKTRACE_DEPTH, 0);
// stack_trace_print(backtrace, nr_bt, 0); // print
// }
// }
// while_each_thread(g, p);
// rcu_read_unlock(); // unlock run queue
// }
return HRTIMER_RESTART; // restart timer
}