From 8f61ee9aa689cae0cdf2fbc5634aa0331eca53c9 Mon Sep 17 00:00:00 2001 From: Hailong Liu Date: Tue, 12 Apr 2022 16:51:12 +0800 Subject: [PATCH] irqoff: record the logs to logfile Signed-off-by: Hailong Liu --- source/tools/detect/irqoff/irqoff.c | 59 +++++++++++++++++++++++-- source/tools/detect/irqoff/stacktrace.c | 5 ++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/source/tools/detect/irqoff/irqoff.c b/source/tools/detect/irqoff/irqoff.c index 41428416..852fe557 100644 --- a/source/tools/detect/irqoff/irqoff.c +++ b/source/tools/detect/irqoff/irqoff.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -23,10 +25,15 @@ struct env { time_t duration; bool verbose; } env = { - .sample_period = 2*1000*1000 + 1, //1ms + .sample_period = 4*1000*1000 + 1, //4ms .duration = 10, }; +FILE *filep = NULL; +char filename[256] = {0}; +char log_dir[] = "/var/log/sysak/irqoff"; +char defaultfile[] = "/var/log/sysak/irqoff/irqoff.log"; + static struct ksym *ksyms; static int stackmp_fd; static __u64 threshold; @@ -39,11 +46,18 @@ const char *argp_program_version = "irqoff 0.1"; const char argp_program_doc[] = "Catch the irq-off time more than threshold.\n" "\n" -"USAGE: irqoff [--help] [-c SAMPLE_PERIOD(ns)] [-t THRESH(ns)] [duration(s)]\n"; +"USAGE: irqoff [--help] [-c SAMPLE_PERIOD(ns)] [-t THRESH(ns)] [-f LOGFILE] [duration(s)]\n" +"\n" +"EXAMPLES:\n" +" irqoff # run 10s, and detect irqoff more than 10ms(default)\n" +" irqoff -c 1000000 # detect irqoff with period 1ms (default 4ms)\n" +" irqoff -t 15000000 # detect irqoff with threshold 15ms (default 10ms)\n" +" irqoff -f a.log # record result to a.log (default to ~sysak/irqoff/irqoff.log)\n"; static const struct argp_option opts[] = { { "sample_period", 'c', "SAMPLE_PERIOD", 0, "Period default to 2ms"}, { "threshold", 't', "THRESH", 0, "Threshold to detect, default 10ms"}, + { "logfile", 'f', "LOGFILE", 0, "logfile for result"}, { "verbose", 'v', NULL, 0, "Verbose debug output" }, { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" }, {}, @@ -76,6 +90,19 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) argp_usage(state); } break; + case 'f': + if (strlen(arg) < 2) + strncpy(filename, defaultfile, sizeof(filename)); + else + strncpy(filename, arg, sizeof(filename)); + filep = fopen(filename, "w+"); + if (!filep) { + int ret = errno; + fprintf(stderr, "%s :fopen %s\n", + strerror(errno), filename); + return ret; + } + break; case ARGP_KEY_ARG: if (pos_args++) { fprintf(stderr, @@ -92,6 +119,15 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) default: return ARGP_ERR_UNKNOWN; } + if (!filep) { + filep = fopen(defaultfile, "w+"); + if (!filep) { + int ret = errno; + fprintf(stderr, "%s :fopen %s\n", + strerror(errno), defaultfile); + return ret; + } + } return 0; } @@ -152,7 +188,7 @@ void handle_event(void *ctx, int cpu, void *data, __u32 data_sz) time(&t); tm = localtime(&t); strftime(ts, sizeof(ts), "%F_%H:%M:%S", tm); - fprintf(stdout, "%-21s %-5d %-15s %-8d %-10llu\n", + fprintf(filep, "%-21s %-5d %-15s %-8d %-10llu\n", ts, e->cpu, e->comm, e->pid, e->delay); print_stack(stackmp_fd, e->ret, ksyms); } @@ -163,7 +199,7 @@ void irqoff_handler(int poll_fd) struct perf_buffer *pb = NULL; struct perf_buffer_opts pb_opts = {}; - fprintf(stdout, "%-21s %-5s %-15s %-8s %-10s\n", "TIME(irqoff)", "CPU", "COMM", "TID", "LAT(us)"); + fprintf(filep, "%-21s %-5s %-15s %-8s %-10s\n", "TIME(irqoff)", "CPU", "COMM", "TID", "LAT(us)"); pb_opts.sample_cb = handle_event; pb = perf_buffer__new(poll_fd, 64, &pb_opts); @@ -194,6 +230,17 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va return vfprintf(stderr, format, args); } +static int prepare_dictory(char *path) +{ + int ret; + + ret = mkdir(path, 0777); + if (ret < 0 && errno != EEXIST) + return errno; + else + return 0; +} + static void sig_alarm(int signo) { exiting = 1; @@ -216,6 +263,10 @@ int main(int argc, char **argv) .doc = argp_program_doc, }; + err = prepare_dictory(log_dir); + if (err) + return err; + ksyms = NULL; threshold = LAT_THRESH_NS; err = argp_parse(&argp, argc, argv, 0, NULL, NULL); diff --git a/source/tools/detect/irqoff/stacktrace.c b/source/tools/detect/irqoff/stacktrace.c index a47aef20..6c03ddf6 100644 --- a/source/tools/detect/irqoff/stacktrace.c +++ b/source/tools/detect/irqoff/stacktrace.c @@ -17,6 +17,7 @@ #define PERF_MAX_STACK_DEPTH 127 static int sym_cnt; +extern FILE *filep; static int ksym_cmp(const void *p1, const void *p2) { @@ -101,7 +102,7 @@ static void print_ksym(__u64 addr, struct ksym *psym) return; sym = ksym_search(addr, psym); - fprintf(stdout, "<0x%llx> %s\n", addr, sym->name); + fprintf(filep, "<0x%llx> %s\n", addr, sym->name); } void print_stack(int fd, __u32 ret, struct ksym *syms) @@ -114,7 +115,7 @@ void print_stack(int fd, __u32 ret, struct ksym *syms) print_ksym(ip[i], syms); } else { if ((int)(ret) < 0) - fprintf(stdout, "<0x0000000000000000>:error=%d\n", (int)(ret)); + fprintf(filep, "<0x0000000000000000>:error=%d\n", (int)(ret)); } } -- Gitee