diff --git a/source/module/monitor_kernel.c b/source/module/monitor_kernel.c index a7c1383..644da98 100644 --- a/source/module/monitor_kernel.c +++ b/source/module/monitor_kernel.c @@ -45,6 +45,23 @@ typedef struct { int id; } ioctl_id; +// dump log from buffer +static int dump_log(ioctl_dump_param *dump_param, unsigned long ioctl_param, + struct diag_variant_buffer *buffer) { + int ret = 0; + ret = copy_from_user(dump_param, (ioctl_dump_param *)ioctl_param, + sizeof(ioctl_dump_param)); + printk(KERN_INFO "dump_param: %p %lx %p\n", dump_param->user_ptr_len, + dump_param->user_buf_len, dump_param->user_buf); + if (!ret) { + ret = copy_to_user_variant_buffer(buffer, dump_param->user_ptr_len, + dump_param->user_buf, + dump_param->user_buf_len); + printk(KERN_INFO "ret %d, %lx\n", ret, dump_param->user_buf_len); + } + return ret; +} + static long device_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param) { int ret = 0; @@ -74,17 +91,10 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num, break; case IOCTL_DUMP_LOG: printk(KERN_INFO "variable_monitor IOCTL_DUMP_LOG\n"); - ret = copy_from_user(&dump_param, (ioctl_dump_param *)ioctl_param, - sizeof(ioctl_dump_param)); - printk(KERN_INFO "dump_param: %p %lx %p\n", dump_param.user_ptr_len, - dump_param.user_buf_len, dump_param.user_buf); - if (!ret) { - ret = copy_to_user_variant_buffer( - &load_monitor_variant_buffer, dump_param.user_ptr_len, - dump_param.user_buf, dump_param.user_buf_len); - printk(KERN_INFO "ret %d, %lx\n", ret, dump_param.user_buf_len); + ret = dump_log(&dump_param, ioctl_param, &load_monitor_variant_buffer); + if (ret) { + printk(KERN_INFO "dump_log failed\n"); } - // printk(KERN_INFO "copy_to_user_variant_buffer \n"); break; case IOCTL_PID: printk(KERN_INFO "variable_monitor PID\n"); @@ -96,6 +106,13 @@ static long device_ioctl(struct file *file, unsigned int ioctl_num, ret = copy_from_user(&wid, (ioctl_id *)ioctl_param, sizeof(ioctl_id)); diag_tgid(wid.id); break; + case IOCTL_DUMP_LOG_SA: + printk(KERN_INFO "variable_monitor IOCTL_DUMP_LOG_SA\n"); + ret = dump_log(&dump_param, ioctl_param, &stand_alone_buffer); + if (ret) { + printk(KERN_INFO "dump_log failed\n"); + } + break; default: break; } diff --git a/source/module/monitor_kernel.h b/source/module/monitor_kernel.h index a7c6aa5..759d1ad 100644 --- a/source/module/monitor_kernel.h +++ b/source/module/monitor_kernel.h @@ -12,6 +12,7 @@ #define IOCTL_DUMP_LOG 1 #define IOCTL_PID _IOWR(IOCTL_MAGIC_NUMBER, 2, int) #define IOCTL_TGID _IOWR(IOCTL_MAGIC_NUMBER, 3, int) +#define IOCTL_DUMP_LOG_SA _IOWR(IOCTL_MAGIC_NUMBER, 4, int) // default value extern int def_interval_ns; @@ -19,7 +20,9 @@ extern int dump_reset_sec; extern mm_tree mm_tree_struct; extern struct diag_variant_buffer load_monitor_variant_buffer; // global buffer -#define VARIABLE_MONITOR_BUFFER_SIZE 256 * 1024 * 1024 // 256MB +#define VARIABLE_MONITOR_BUFFER_SIZE 256 * 1024 * 1024 // 256MB +extern struct diag_variant_buffer stand_alone_buffer; // buffer for single work +#define STAND_ALONE_BUFFER_SIZE 50 * 1024 * 1024 // 50 MB int monitor_init(void); // monitor init void monitor_exit(void); // monitor exit @@ -30,5 +33,5 @@ void clear_watch(pid_t pid); // for release enum hrtimer_restart check_variable_cb(struct hrtimer *timer); // hrtimer callback -int diag_pid(int id); // for test +int diag_pid(int id); // for test int diag_tgid(int id); // for test \ No newline at end of file diff --git a/source/module/monitor_kernel_lib.c b/source/module/monitor_kernel_lib.c index be8b943..0bba2c0 100644 --- a/source/module/monitor_kernel_lib.c +++ b/source/module/monitor_kernel_lib.c @@ -83,19 +83,31 @@ static void init_mm_tree(mm_tree *mm_tree) { } /** - * @brief init global variable load_monitor_variant_buffer + * @brief init buffer * * @param buf_size + * @param buffer * @return int */ -static int init_buffer(unsigned int buf_size) { +static int init_buffer(unsigned int buf_size, + struct diag_variant_buffer *buffer) { init_mm_tree(&mm_tree_struct); // init mm_tree - init_diag_variant_buffer(&load_monitor_variant_buffer, buf_size); + init_diag_variant_buffer(buffer, buf_size); int ret = 0; - ret = alloc_diag_variant_buffer(&load_monitor_variant_buffer); + ret = alloc_diag_variant_buffer(buffer); return ret; } +// init load_monitor_variant_buffer +static int init_global_buffer(void) { + return init_buffer(VARIABLE_MONITOR_BUFFER_SIZE, + &load_monitor_variant_buffer); +} +// init stand_alone_buffer +static int init_sa_buffer(void) { + return init_buffer(STAND_ALONE_BUFFER_SIZE, &stand_alone_buffer); +} + /** * @brief diag task info | brief | user stack | kernel stack | proc chains | raw * stack @@ -114,37 +126,64 @@ static void diag_tsk(struct task_struct *p, variable_monitor_task *tsk_info) { diag_task_raw_stack(p, &tsk_info->raw_stack); // raw stack } +static void push_tskinfo_2_buffer_orig(variable_monitor_task *tsk_info, + unsigned long *flags, + struct diag_variant_buffer *buffer) { + // printk(KERN_INFO "push_tsk_info\n"); + diag_variant_buffer_spin_lock(buffer, *flags); + diag_variant_buffer_reserve(buffer, sizeof(variable_monitor_task)); + diag_variant_buffer_write_nolock(buffer, tsk_info, + sizeof(variable_monitor_task)); + diag_variant_buffer_seal(buffer); + diag_variant_buffer_spin_unlock(buffer, *flags); +} + /** * @brief push task info to global buffer * * @param tsk_info * @param flags */ -static void push_tskinfo_2_buffer(variable_monitor_task *tsk_info, - unsigned long *flags) { - // printk(KERN_INFO "push_tsk_info\n"); - 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); +// static void push_tskinfo_2_buffer(variable_monitor_task *tsk_info, +// unsigned long *flags) { +// push_tskinfo_2_buffer_orig(tsk_info, flags, &load_monitor_variant_buffer); +// } + +/** + * @brief push task info to stand_alone_buffer + * + * @param tsk_info + * @param flags + */ +static void push_tskinfo_2_sa_buffer(variable_monitor_task *tsk_info, + unsigned long *flags) { + push_tskinfo_2_buffer_orig(tsk_info, flags, &stand_alone_buffer); } -static void push_tskinfo_22_buffer(variable_monitor_task *tsk_info, unsigned long *flags){ +/** + * @brief push user/sys task info to global buffer + * + * @param tsk_info + * @param flags + */ +static void push_tskinfo_22_buffer(variable_monitor_task *tsk_info, + unsigned long *flags) { variable_monitor_task_system *tsk_info_system; if (tsk_info->task.sys_task == 1) // system task { tsk_info_system = (variable_monitor_task_system *)tsk_info; tsk_info_system->et_type = VARIABLE_MONITOR_TASK_TYPE_SYSTEM; - diag_variant_buffer_reserve(&load_monitor_variant_buffer, sizeof(variable_monitor_task_system)); - diag_variant_buffer_write_nolock(&load_monitor_variant_buffer, tsk_info_system, - sizeof(variable_monitor_task_system)); + diag_variant_buffer_reserve(&load_monitor_variant_buffer, + sizeof(variable_monitor_task_system)); + diag_variant_buffer_write_nolock(&load_monitor_variant_buffer, + tsk_info_system, + sizeof(variable_monitor_task_system)); diag_variant_buffer_seal(&load_monitor_variant_buffer); } else { - 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_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); } } @@ -202,7 +241,8 @@ void diag_task_info_work(struct work_struct *work) { // printk(KERN_INFO "threshold exceeded, Timestamp %lld:\n", vm_record.tv); // for (i = 0; i < vm_record.threshold_num; i++) { - // printk(KERN_INFO "\t: pid: %d, name: %s, ptr: %p, threshold:%lld, true_value:%lld\n", + // printk(KERN_INFO "\t: pid: %d, name: %s, ptr: %p, threshold:%lld, + // true_value:%lld\n", // vm_record.threshold_record[i].task_id, // vm_record.threshold_record[i] // .name, // Assuming name is a null-terminated string @@ -247,7 +287,8 @@ void diag_task_info_work(struct work_struct *work) { printk(KERN_INFO "threshold exceeded, Timestamp %lld:\n", vm_record.tv); for (i = 0; i < vm_record.threshold_num; i++) { - printk(KERN_INFO "\t: pid: %d, name: %s, ptr: %p, threshold:%lld, true_value:%lld\n", + printk(KERN_INFO + "\t: pid: %d, name: %s, ptr: %p, threshold:%lld, true_value:%lld\n", vm_record.threshold_record[i].task_id, vm_record.threshold_record[i] .name, // Assuming name is a null-terminated string @@ -260,7 +301,7 @@ void diag_task_info_work(struct work_struct *work) { return; } /** - * @brief all module function init. orig_X | buffer + * @brief all module function init. orig_X | buffer * * @return int */ @@ -270,7 +311,11 @@ int monitor_init(void) { ret = init_orig_fun(); // init orig_X if (ret) return ret; - ret = init_buffer(VARIABLE_MONITOR_BUFFER_SIZE); // 256M + ret = init_global_buffer(); // 256M + if (ret) + return -1; + + ret = init_sa_buffer(); // 50M if (ret) return -1; return 0; @@ -323,7 +368,9 @@ int start_watch_variable(watch_arg warg) { INIT_WORK(&timer->wk, diag_task_info_work); printk(KERN_INFO "Convert ptr to kptr: %p\n", kptr); - printk(KERN_INFO "Associated timer: %p , there are already %d variables, timer period %lld.\n", timer, timer->sentinel, timer->time_ns); + printk(KERN_INFO "Associated timer: %p , there are already %d variables, " + "timer period %lld.\n", + timer, timer->sentinel, timer->time_ns); // printk(KERN_INFO "timer->hr_timer: %p\n", &timer->hr_timer); TIMER_CANCEL(timer); // just in case @@ -336,7 +383,7 @@ int start_watch_variable(watch_arg warg) { /** * @brief reinit all timer's work - * + * */ void init_work_all_hrTimer(void) { int i = 0; @@ -395,7 +442,7 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { queue_work(system_highpri_wq, &k_watch_timer->wk); // restart timer after 5s hrtimer_forward(timer, timer->base->get_time(), - ktime_set(dump_reset_sec, 0)); //! todo + ktime_set(dump_reset_sec, 0)); } else { // keep frequency hrtimer_forward(timer, timer->base->get_time(), k_watch_timer->kt); @@ -405,9 +452,9 @@ enum hrtimer_restart check_variable_cb(struct hrtimer *timer) { /** * @brief for test only - * - * @param id - * @return int + * + * @param id + * @return int */ int diag_pid(int id) { struct task_struct *tsk; @@ -439,16 +486,16 @@ int diag_pid(int id) { diag_tsk(tsk, &tsk_info); put_task_struct(tsk); // count -1 - push_tskinfo_2_buffer(&tsk_info, &flags); // push to buffer + push_tskinfo_2_sa_buffer(&tsk_info, &flags); // push to buffer return 0; } /** * @brief for test only - * - * @param id - * @return int + * + * @param id + * @return int */ int diag_tgid(int id) { struct task_struct *tsk; @@ -475,15 +522,6 @@ int diag_tgid(int id) { struct task_struct *thread = tsk; while_each_thread(tsk, thread) { - // save_task_info(thread, &detail); - - // diag_variant_buffer_spin_lock(&pupil_variant_buffer, flags); - // diag_variant_buffer_reserve(&pupil_variant_buffer, - // sizeof(struct pupil_task_detail)); - // diag_variant_buffer_write_nolock(&pupil_variant_buffer, &detail, - // sizeof(struct pupil_task_detail)); - // diag_variant_buffer_seal(&pupil_variant_buffer); - // diag_variant_buffer_spin_unlock(&pupil_variant_buffer, flags); get_task_struct(thread); // count +1 @@ -491,10 +529,10 @@ int diag_tgid(int id) { tsk_info.id = event_id; tsk_info.tv = vm_record.tv; diag_tsk(thread, &tsk_info); - + put_task_struct(thread); // count -1 - push_tskinfo_2_buffer(&tsk_info, &flags); // push to buffer + push_tskinfo_2_sa_buffer(&tsk_info, &flags); // push to buffer } return 0; diff --git a/source/module/monitor_trace.c b/source/module/monitor_trace.c index 63e4cca..8a0d59a 100644 --- a/source/module/monitor_trace.c +++ b/source/module/monitor_trace.c @@ -11,6 +11,7 @@ mm_tree mm_tree_struct; struct diag_variant_buffer load_monitor_variant_buffer; +struct diag_variant_buffer stand_alone_buffer; typedef struct { struct rcu_head rcu_head; @@ -382,7 +383,8 @@ void diag_task_brief(struct task_struct *tsk, task_detail *detail) { detail->cgroup_cpuset[CGROUP_NAME_LEN - 1] = 0; // cgroup cpuset name } -// void diag_task_user_stack(struct task_struct *tsk, user_stack_detail *detail) { +// void diag_task_user_stack(struct task_struct *tsk, user_stack_detail *detail) +// { // struct pt_regs *regs; // unsigned long sp, ip, bp; // struct task_struct *leader; @@ -426,10 +428,10 @@ void diag_task_brief(struct task_struct *tsk, task_detail *detail) { /** * @brief diag task kernel stack | -> to orig_stack_trace_save_tsk - * - * @param tsk - * @param detail - * @return unsigned int + * + * @param tsk + * @param detail + * @return unsigned int */ unsigned int diag_task_kern_stack(struct task_struct *tsk, kern_stack_detail *detail) { @@ -438,11 +440,11 @@ unsigned int diag_task_kern_stack(struct task_struct *tsk, /** * @brief diag task proc chains - * - * @param style - * @param tsk - * @param mm_tree - * @param detail + * + * @param style + * @param tsk + * @param mm_tree + * @param detail */ void dump_proc_chains_argv(int style, struct task_struct *tsk, mm_tree *mm_tree, proc_chains_detail *detail) { diff --git a/source/module/monitor_trace.h b/source/module/monitor_trace.h index 9ec2fe8..4cdd24c 100644 --- a/source/module/monitor_trace.h +++ b/source/module/monitor_trace.h @@ -14,7 +14,7 @@ #define PROCESS_CHAINS_COUNT 10 // max count of process chains #define PROCESS_ARGV_LEN 128 // max length of process argv -#define MAX_NAME_LEN (127) // max name length +#define MAX_NAME_LEN (127) // max name length #define TIMER_MAX_WATCH_NUM (32) // A timer max watch number at once time #define DIAG_USER_STACK_SIZE (16 * 1024) @@ -29,7 +29,7 @@ typedef struct { char name[MAX_NAME_LEN + 1]; // name void *ptr; // virtual address long long threshold; // threshold value - long long true_value; // target true value + long long true_value; // target true value } threshold; typedef struct { @@ -107,9 +107,9 @@ typedef struct { int et_type; unsigned long id; unsigned long long tv; - task_detail task; // brief - kern_stack_detail kern_stack; // kernel stack -} variable_monitor_task_system; + task_detail task; // brief + kern_stack_detail kern_stack; // kernel stack +} variable_monitor_task_system; typedef struct { struct radix_tree_root mm_tree; @@ -118,6 +118,7 @@ typedef struct { extern mm_tree mm_tree_struct; extern struct diag_variant_buffer load_monitor_variant_buffer; // Global buffer +extern struct diag_variant_buffer stand_alone_buffer; // for single work void diag_task_brief(struct task_struct *tsk, task_detail *detail); // get task brief diff --git a/source/ucli/ucli.cc b/source/ucli/ucli.cc index d967d67..8b8460f 100644 --- a/source/ucli/ucli.cc +++ b/source/ucli/ucli.cc @@ -119,6 +119,23 @@ static void do_dump(const char *arg) { } } +static void do_dump_sa(const char *arg) { + static char variant_buf[STAND_ALONE_BUFFER_SIZE]; + int len; + int ret = 0; + + struct diag_ioctl_dump_param dump_param = { + .user_ptr_len = &len, + .user_buf_len = STAND_ALONE_BUFFER_SIZE, + .user_buf = variant_buf, + }; + + ret = diag_call_ioctl(IOCTL_DUMP_LOG_SA, (long)&dump_param); + if (ret == 0) { + do_extract(variant_buf, len); + } +} + static void do_pid(char *arg) { int pid = 0; int ret; @@ -133,9 +150,9 @@ static void do_pid(char *arg) { if (ret) { printf("Get pid info err: %d\n", ret); } - sleep(3); // after 3sec + sleep(3); // after 3sec - do_dump(""); + do_dump_sa(""); } static void do_tgid(char *arg) { @@ -154,7 +171,7 @@ static void do_tgid(char *arg) { } sleep(3); - do_dump(""); + do_dump_sa(""); } int main(int argc, char *argv[]) { diff --git a/source/ucli/ucli.h b/source/ucli/ucli.h index e5370f6..af62e91 100644 --- a/source/ucli/ucli.h +++ b/source/ucli/ucli.h @@ -14,6 +14,7 @@ #define IOCTL_MAGIC_NUMBER 'k' #define IOCTL_PID _IOWR(IOCTL_MAGIC_NUMBER, 2, int) #define IOCTL_TGID _IOWR(IOCTL_MAGIC_NUMBER, 3, int) +#define IOCTL_DUMP_LOG_SA _IOWR(IOCTL_MAGIC_NUMBER, 4, int) // dump type #define VARIABLE_MONITOR_RECORD_TYPE 0x0 @@ -35,6 +36,7 @@ #define TIMER_MAX_WATCH_NUM (32) // A timer max watch number at once time #define VARIABLE_MONITOR_BUFFER_SIZE 256 * 1024 * 1024 // 256MB +#define STAND_ALONE_BUFFER_SIZE 50 * 1024 * 1024 // 50 MB #define DIAG_USER_STACK_SIZE (16 * 1024)