This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-variable-monitor/source/ucli/ucli.cc

229 lines
5.7 KiB
C++
Raw Normal View History

2023-11-24 05:34:51 -05:00
/**
* various_monitor cli
*/
#include "ucli.h"
2023-12-12 02:59:48 -05:00
#include "helpfun.h"
2023-12-05 00:49:44 -05:00
#include <cstdio>
2023-11-24 05:34:51 -05:00
#include <getopt.h>
#include <string.h>
2023-12-05 00:49:44 -05:00
#include <sys/ioctl.h> // for ioctl
#include <unistd.h>
2023-11-26 20:34:52 -05:00
static int task_info_extract(void *buf, unsigned int len, void *) {
2023-11-24 05:34:51 -05:00
int *et_type;
variable_monitor_record *vm_record;
variable_monitor_task *tsk_info;
2023-12-06 05:03:44 -05:00
variable_monitor_task_system *tsk_info_system;
2023-11-24 05:34:51 -05:00
struct load_monitor_cpu_run *cpu_run;
static int seq = 0;
2023-11-27 03:17:38 -05:00
if (len == 0)
return 0;
2023-11-24 05:34:51 -05:00
et_type = (int *)buf;
switch (*et_type) {
2023-11-27 20:59:18 -05:00
case VARIABLE_MONITOR_RECORD_TYPE:
2023-11-27 03:17:38 -05:00
if (len < sizeof(variable_monitor_record))
2023-11-24 05:34:51 -05:00
break;
2023-11-27 03:17:38 -05:00
vm_record = (variable_monitor_record *)buf;
2023-11-24 05:34:51 -05:00
2023-12-06 03:36:17 -05:00
printf("threshold exceeded, Timestamp %lld :\n", vm_record->tv);
2023-11-24 05:34:51 -05:00
2023-12-13 03:17:52 -05:00
for (int i = 0; i < vm_record->threshold_over_count; i++) {
2023-12-06 03:20:22 -05:00
printf("\t: pid: %d, name: %s, ptr: %p, threshold:%d, true_value:%d\n",
2023-11-27 03:17:38 -05:00
vm_record->threshold_record[i].task_id,
vm_record->threshold_record[i]
.name, // Assuming name is a null-terminated string
vm_record->threshold_record[i].ptr,
2023-12-06 03:20:22 -05:00
vm_record->threshold_record[i].threshold,
2023-12-12 11:55:44 +08:00
vm_record->threshold_record[i].true_value);
2023-11-27 03:17:38 -05:00
}
break;
2023-11-27 20:59:18 -05:00
case VARIABLE_MONITOR_TASK_TYPE:
2023-11-27 03:17:38 -05:00
if (len < sizeof(variable_monitor_task))
break;
tsk_info = (variable_monitor_task *)buf;
seq++;
2023-11-24 05:34:51 -05:00
2023-12-05 22:29:41 -05:00
printf("##CGROUP:[%s] %d [%03d] [%s] state: %d\n",
2023-11-27 03:17:38 -05:00
tsk_info->task.cgroup_buf, tsk_info->task.pid, seq,
2023-12-12 11:55:44 +08:00
state_str(tsk_info->task.state).c_str(), tsk_info->task.state);
2023-11-24 05:34:51 -05:00
2023-11-27 03:17:38 -05:00
printk_task_brief(&tsk_info->task);
2023-12-05 21:56:04 -05:00
2023-12-12 02:59:48 -05:00
// system task no need print user stack | just in case
2023-12-12 11:55:44 +08:00
if (tsk_info->task.sys_task == 1) {
diag_printf_kern_stack(&tsk_info->kern_stack);
} else {
diag_printf_raw_stack(run_in_host ? tsk_info->task.tgid
: tsk_info->task.container_tgid,
tsk_info->task.container_tgid, tsk_info->task.comm,
&tsk_info->raw_stack);
diag_printf_kern_stack(&tsk_info->kern_stack);
2023-12-05 21:44:01 -05:00
}
2023-11-27 03:17:38 -05:00
printf("#* 0xffffffffffffff %s (UNKNOWN)\n", tsk_info->task.comm);
diag_printf_proc_chains(&tsk_info->proc_chains);
printf("##\n");
2023-12-06 05:03:44 -05:00
break;
case VARIABLE_MONITOR_TASK_TYPE_SYSTEM:
if (len < sizeof(variable_monitor_task_system))
break;
tsk_info_system = (variable_monitor_task_system *)buf;
seq++;
printf("##CGROUP:[%s] %d [%03d] [%s] state: %d\n",
tsk_info_system->task.cgroup_buf, tsk_info_system->task.pid, seq,
2023-12-12 11:55:44 +08:00
state_str(tsk_info_system->task.state).c_str(),
tsk_info_system->task.state);
2023-12-06 05:03:44 -05:00
printk_task_brief(&tsk_info_system->task);
// system task no need print raw_stack
diag_printf_kern_stack(&tsk_info_system->kern_stack);
2023-12-12 11:55:44 +08:00
printf("#* 0xffffffffffffff %s (UNKNOWN)\n",
tsk_info_system->task.comm);
2023-12-06 05:03:44 -05:00
printf("##\n");
2023-11-27 03:17:38 -05:00
break;
2023-12-12 11:55:44 +08:00
2023-11-27 03:17:38 -05:00
default:
break;
2023-11-24 05:34:51 -05:00
}
return 0;
}
2023-11-26 20:34:52 -05:00
static void do_extract(char *buf, int len) {
extract_variant_buffer(buf, len, task_info_extract, NULL);
}
2023-11-24 05:34:51 -05:00
static void do_dump(const char *arg) {
2023-12-12 02:59:48 -05:00
static char variant_buf[VARIABLE_MONITOR_BUFFER_SIZE];
2023-11-24 05:34:51 -05:00
int len;
int ret = 0;
// 参数解析, 未实现/ 源码在 SOURCE/diagnose-tools/params_parse.h |
// params_parse.cc
// struct params_parser parse(arg);
struct diag_ioctl_dump_param dump_param = {
.user_ptr_len = &len,
2023-12-12 02:59:48 -05:00
.user_buf_len = VARIABLE_MONITOR_BUFFER_SIZE,
2023-11-24 05:34:51 -05:00
.user_buf = variant_buf,
};
2023-12-12 02:59:48 -05:00
ret = diag_call_ioctl(IOCTL_DUMP_LOG, (long)&dump_param);
2023-11-24 05:34:51 -05:00
if (ret == 0) {
2023-11-26 20:34:52 -05:00
do_extract(variant_buf, len);
2023-11-24 05:34:51 -05:00
}
}
2023-12-12 04:05:56 -05:00
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);
}
}
2023-12-05 00:49:44 -05:00
static void do_pid(char *arg) {
int pid = 0;
int ret;
sscanf(optarg, "%d", &pid);
if (pid <= 0) {
2023-12-05 21:49:08 -05:00
printf("arg err\n");
2023-12-05 00:49:44 -05:00
return;
}
2023-12-05 21:49:08 -05:00
printf("Get pid info: %d\n", pid);
2023-12-05 00:49:44 -05:00
ret = diag_call_ioctl(IOCTL_PID, (long)&pid);
if (ret) {
2023-12-05 21:49:08 -05:00
printf("Get pid info err: %d\n", ret);
2023-12-05 00:49:44 -05:00
}
2023-12-12 04:05:56 -05:00
sleep(3); // after 3sec
2023-12-05 00:49:44 -05:00
2023-12-12 04:05:56 -05:00
do_dump_sa("");
2023-12-05 00:49:44 -05:00
}
2023-12-05 02:42:07 -05:00
static void do_tgid(char *arg) {
int pid = 0;
int ret;
sscanf(optarg, "%d", &pid);
if (pid <= 0) {
2023-12-05 21:49:08 -05:00
printf("arg err\n");
2023-12-05 02:42:07 -05:00
return;
}
2023-12-05 21:49:08 -05:00
printf("Get tgid info: %d\n", pid);
2023-12-05 02:42:07 -05:00
ret = diag_call_ioctl(IOCTL_TGID, (long)&pid);
if (ret) {
2023-12-05 21:49:08 -05:00
printf("Get tgid info err: %d\n", ret);
2023-12-05 02:42:07 -05:00
}
sleep(3);
2023-12-12 04:05:56 -05:00
do_dump_sa("");
2023-12-05 02:42:07 -05:00
}
2023-11-24 05:34:51 -05:00
int main(int argc, char *argv[]) {
2023-12-04 23:03:19 -05:00
run_in_host = check_in_host();
2023-12-05 04:42:53 -05:00
printf("run_in_host: %d\n", run_in_host);
2023-12-04 23:03:19 -05:00
2023-12-12 11:55:44 +08:00
for (int i = 0; i < argc; i++) {
2023-12-05 00:49:44 -05:00
printf("argv[%d]: %s\n", i, argv[i]);
}
static const struct option long_options[] = {
{"help", no_argument, 0, 0},
{"report", no_argument, 0, 0},
{"pid", required_argument, 0, 0},
{"tgid", required_argument, 0, 0},
{0, 0, 0, 0}};
2023-11-24 05:34:51 -05:00
int c;
if (argc <= 1) {
// do something default
2023-11-27 03:17:38 -05:00
do_dump(optarg ? optarg : "");
2023-11-24 05:34:51 -05:00
return 0;
}
while (1) {
int option_index = -1;
c = getopt_long_only(argc, argv, "", long_options, &option_index);
if (c == -1) {
break;
}
2023-12-05 02:42:07 -05:00
// option_index = 2;
2023-11-27 03:17:38 -05:00
2023-11-24 05:34:51 -05:00
switch (option_index) {
2023-11-27 03:17:38 -05:00
case 0: // help
// usage_pupil();
break;
case 1: // report
do_dump(optarg ? optarg : "");
break;
2023-12-05 00:49:44 -05:00
case 2: // pid
do_pid(optarg);
break;
2023-12-05 02:42:07 -05:00
case 3: // tgid
do_tgid(optarg);
break;
2023-11-27 03:17:38 -05:00
default:
// usage_pupil();
break;
2023-11-24 05:34:51 -05:00
}
}
return 0;
}