diff options
Diffstat (limited to 'tools')
52 files changed, 2604 insertions, 302 deletions
diff --git a/tools/Makefile b/tools/Makefile index 41067f3..a9b0200 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -15,6 +15,7 @@ help: @echo ' net - misc networking tools' @echo ' vm - misc vm tools' @echo ' x86_energy_perf_policy - Intel energy policy tool' + @echo ' tmon - thermal monitoring and tuning tool' @echo '' @echo 'You can do:' @echo ' $$ make -C tools/ <tool>_install' @@ -50,6 +51,9 @@ selftests: FORCE turbostat x86_energy_perf_policy: FORCE $(call descend,power/x86/$@) +tmon: FORCE + $(call descend,thermal/$@) + cpupower_install: $(call descend,power/$(@:_install=),install) @@ -62,9 +66,13 @@ selftests_install: turbostat_install x86_energy_perf_policy_install: $(call descend,power/x86/$(@:_install=),install) +tmon_install: + $(call descend,thermal/$(@:_install=),install) + install: cgroup_install cpupower_install firewire_install lguest_install \ perf_install selftests_install turbostat_install usb_install \ - virtio_install vm_install net_install x86_energy_perf_policy_install + virtio_install vm_install net_install x86_energy_perf_policy_install \ + tmon cpupower_clean: $(call descend,power/cpupower,clean) @@ -84,8 +92,11 @@ selftests_clean: turbostat_clean x86_energy_perf_policy_clean: $(call descend,power/x86/$(@:_clean=),clean) +tmon_clean: + $(call descend,thermal/tmon,clean) + clean: cgroup_clean cpupower_clean firewire_clean lguest_clean perf_clean \ selftests_clean turbostat_clean usb_clean virtio_clean \ - vm_clean net_clean x86_energy_perf_policy_clean + vm_clean net_clean x86_energy_perf_policy_clean tmon_clean .PHONY: FORCE diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c index 8f450ad..0362d57 100644 --- a/tools/lib/traceevent/event-parse.c +++ b/tools/lib/traceevent/event-parse.c @@ -3435,6 +3435,19 @@ eval_num_arg(void *data, int size, struct event_format *event, struct print_arg goto out_warning_op; } break; + case PRINT_DYNAMIC_ARRAY: + /* Without [], we pass the address to the dynamic data */ + offset = pevent_read_number(pevent, + data + arg->dynarray.field->offset, + arg->dynarray.field->size); + /* + * The actual length of the dynamic array is stored + * in the top half of the field, and the offset + * is in the bottom half of the 32 bit field. + */ + offset &= 0xffff; + val = (unsigned long long)(data + offset); + break; default: /* not sure what to do there */ return 0; } diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt index 052f7c4..43b42c4 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt @@ -201,6 +201,12 @@ abort events and some memory events in precise mode on modern Intel CPUs. --transaction:: Record transaction flags for transaction related events. +--force-per-cpu:: +Force the use of per-cpu mmaps. By default, when tasks are specified (i.e. -p, +-t or -u options) per-thread mmaps are created. This option overrides that and +forces per-cpu mmaps. A side-effect of that is that inheritance is +automatically enabled. Add the -i option also to disable inheritance. + SEE ALSO -------- linkperf:perf-stat[1], linkperf:perf-list[1] diff --git a/tools/perf/Documentation/perf-trace.txt b/tools/perf/Documentation/perf-trace.txt index 7b0497f..fae38d9 100644 --- a/tools/perf/Documentation/perf-trace.txt +++ b/tools/perf/Documentation/perf-trace.txt @@ -93,9 +93,15 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. --comm:: Show process COMM right beside its ID, on by default, disable with --no-comm. +-s:: --summary:: - Show a summary of syscalls by thread with min, max, and average times (in - msec) and relative stddev. + Show only a summary of syscalls by thread with min, max, and average times + (in msec) and relative stddev. + +-S:: +--with-summary:: + Show all syscalls followed by a summary by thread with min, max, and + average times (in msec) and relative stddev. --tool_stats:: Show tool stats such as number of times fd->pathname was discovered thru diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c index cd9f920..f8bf5f2 100644 --- a/tools/perf/builtin-kvm.c +++ b/tools/perf/builtin-kvm.c @@ -1510,13 +1510,13 @@ static int kvm_events_live(struct perf_kvm_stat *kvm, /* * target related setups */ - err = perf_target__validate(&kvm->opts.target); + err = target__validate(&kvm->opts.target); if (err) { - perf_target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ); + target__strerror(&kvm->opts.target, err, errbuf, BUFSIZ); ui__warning("%s", errbuf); } - if (perf_target__none(&kvm->opts.target)) + if (target__none(&kvm->opts.target)) kvm->opts.target.system_wide = true; @@ -1544,18 +1544,8 @@ static int kvm_events_live(struct perf_kvm_stat *kvm, } kvm->session->evlist = kvm->evlist; perf_session__set_id_hdr_size(kvm->session); - - - if (perf_target__has_task(&kvm->opts.target)) - perf_event__synthesize_thread_map(&kvm->tool, - kvm->evlist->threads, - perf_event__process, - &kvm->session->machines.host); - else - perf_event__synthesize_threads(&kvm->tool, perf_event__process, - &kvm->session->machines.host); - - + machine__synthesize_threads(&kvm->session->machines.host, &kvm->opts.target, + kvm->evlist->threads, false); err = kvm_live_open_events(kvm); if (err) goto out; diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c index 89acc17..6ea9e85 100644 --- a/tools/perf/builtin-probe.c +++ b/tools/perf/builtin-probe.c @@ -325,6 +325,8 @@ int cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused) opt_set_filter), OPT_CALLBACK('x', "exec", NULL, "executable|path", "target executable name or path", opt_set_target), + OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, + "Disable symbol demangling"), OPT_END() }; int ret; diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 15280b5..7c8020a 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -76,12 +76,12 @@ struct perf_record { long samples; }; -static int write_output(struct perf_record *rec, void *buf, size_t size) +static int do_write_output(struct perf_record *rec, void *buf, size_t size) { struct perf_data_file *file = &rec->file; while (size) { - int ret = write(file->fd, buf, size); + ssize_t ret = write(file->fd, buf, size); if (ret < 0) { pr_err("failed to write perf data, error: %m\n"); @@ -97,6 +97,11 @@ static int write_output(struct perf_record *rec, void *buf, size_t size) return 0; } +static int write_output(struct perf_record *rec, void *buf, size_t size) +{ + return do_write_output(rec, buf, size); +} + static int process_synthesized_event(struct perf_tool *tool, union perf_event *event, struct perf_sample *sample __maybe_unused, @@ -480,16 +485,8 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv) perf_event__synthesize_guest_os, tool); } - if (perf_target__has_task(&opts->target)) - err = perf_event__synthesize_thread_map(tool, evsel_list->threads, - process_synthesized_event, - machine); - else if (perf_target__has_cpu(&opts->target)) - err = perf_event__synthesize_threads(tool, process_synthesized_event, - machine); - else /* command specified */ - err = 0; - + err = __machine__synthesize_threads(machine, tool, &opts->target, evsel_list->threads, + process_synthesized_event, opts->sample_address); if (err != 0) goto out_delete_session; @@ -509,7 +506,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv) * (apart from group members) have enable_on_exec=1 set, * so don't spoil it by prematurely enabling them. */ - if (!perf_target__none(&opts->target)) + if (!target__none(&opts->target)) perf_evlist__enable(evsel_list); /* @@ -538,7 +535,7 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv) * die with the process and we wait for that. Thus no need to * disable events in this case. */ - if (done && !disabled && !perf_target__none(&opts->target)) { + if (done && !disabled && !target__none(&opts->target)) { perf_evlist__disable(evsel_list); disabled = true; } @@ -891,6 +888,8 @@ const struct option record_options[] = { "sample by weight (on special events only)"), OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, "sample transaction flags (special events only)"), + OPT_BOOLEAN(0, "force-per-cpu", &record.opts.target.force_per_cpu, + "force the use of per-cpu mmaps"), OPT_END() }; @@ -909,7 +908,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) argc = parse_options(argc, argv, record_options, record_usage, PARSE_OPT_STOP_AT_NON_OPTION); - if (!argc && perf_target__none(&rec->opts.target)) + if (!argc && target__none(&rec->opts.target)) usage_with_options(record_usage, record_options); if (nr_cgroups && !rec->opts.target.system_wide) { @@ -939,17 +938,17 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) goto out_symbol_exit; } - err = perf_target__validate(&rec->opts.target); + err = target__validate(&rec->opts.target); if (err) { - perf_target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); + target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); ui__warning("%s", errbuf); } - err = perf_target__parse_uid(&rec->opts.target); + err = target__parse_uid(&rec->opts.target); if (err) { int saved_errno = errno; - perf_target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); + target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); ui__error("%s", errbuf); err = -saved_errno; diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 0fc1c94..ee0d565 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -108,7 +108,7 @@ enum { static struct perf_evlist *evsel_list; -static struct perf_target target = { +static struct target target = { .uid = UINT_MAX, }; @@ -294,11 +294,10 @@ static int create_perf_stat_counter(struct perf_evsel *evsel) attr->inherit = !no_inherit; - if (perf_target__has_cpu(&target)) + if (target__has_cpu(&target)) return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); - if (!perf_target__has_task(&target) && - perf_evsel__is_group_leader(evsel)) { + if (!target__has_task(&target) && perf_evsel__is_group_leader(evsel)) { attr->disabled = 1; if (!initial_delay) attr->enable_on_exec = 1; @@ -1236,7 +1235,7 @@ static void print_stat(int argc, const char **argv) fprintf(output, "\'system wide"); else if (target.cpu_list) fprintf(output, "\'CPU(s) %s", target.cpu_list); - else if (!perf_target__has_task(&target)) { + else if (!target__has_task(&target)) { fprintf(output, "\'%s", argv[0]); for (i = 1; i < argc; i++) fprintf(output, " %s", argv[i]); @@ -1667,7 +1666,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) } else if (big_num_opt == 0) /* User passed --no-big-num */ big_num = false; - if (!argc && perf_target__none(&target)) + if (!argc && target__none(&target)) usage_with_options(stat_usage, options); if (run_count < 0) { @@ -1680,8 +1679,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) } /* no_aggr, cgroup are for system-wide only */ - if ((aggr_mode != AGGR_GLOBAL || nr_cgroups) - && !perf_target__has_cpu(&target)) { + if ((aggr_mode != AGGR_GLOBAL || nr_cgroups) && + !target__has_cpu(&target)) { fprintf(stderr, "both cgroup and no-aggregation " "modes only available in system-wide mode\n"); @@ -1694,14 +1693,14 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) if (add_default_attributes()) goto out; - perf_target__validate(&target); + target__validate(&target); if (perf_evlist__create_maps(evsel_list, &target) < 0) { - if (perf_target__has_task(&target)) { + if (target__has_task(&target)) { pr_err("Problems finding threads of monitor\n"); parse_options_usage(stat_usage, options, "p", 1); parse_options_usage(NULL, options, "t", 1); - } else if (perf_target__has_cpu(&target)) { + } else if (target__has_cpu(&target)) { perror("failed to parse CPUs map"); parse_options_usage(stat_usage, options, "C", 1); parse_options_usage(NULL, options, "a", 1); diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 9acca88..71e6402 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -950,14 +950,8 @@ static int __cmd_top(struct perf_top *top) if (ret) goto out_delete; - if (perf_target__has_task(&opts->target)) - perf_event__synthesize_thread_map(&top->tool, top->evlist->threads, - perf_event__process, - &top->session->machines.host); - else - perf_event__synthesize_threads(&top->tool, perf_event__process, - &top->session->machines.host); - + machine__synthesize_threads(&top->session->machines.host, &opts->target, + top->evlist->threads, false); ret = perf_top__start_counters(top); if (ret) goto out_delete; @@ -973,7 +967,7 @@ static int __cmd_top(struct perf_top *top) * XXX 'top' still doesn't start workloads like record, trace, but should, * so leave the check here. */ - if (!perf_target__none(&opts->target)) + if (!target__none(&opts->target)) perf_evlist__enable(top->evlist); /* Wait for a minimal set of events before starting the snapshot */ @@ -1059,7 +1053,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused) .sym_pcnt_filter = 5, }; struct perf_record_opts *opts = &top.record_opts; - struct perf_target *target = &opts->target; + struct target *target = &opts->target; const struct option options[] = { OPT_CALLBACK('e', "event", &top.evlist, "event", "event selector. use 'perf list' to list available events", @@ -1175,24 +1169,24 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused) setup_browser(false); - status = perf_target__validate(target); + status = target__validate(target); if (status) { - perf_target__strerror(target, status, errbuf, BUFSIZ); - ui__warning("%s", errbuf); + target__strerror(target, status, errbuf, BUFSIZ); + ui__warning("%s\n", errbuf); } - status = perf_target__parse_uid(target); + status = target__parse_uid(target); if (status) { int saved_errno = errno; - perf_target__strerror(target, status, errbuf, BUFSIZ); - ui__error("%s", errbuf); + target__strerror(target, status, errbuf, BUFSIZ); + ui__error("%s\n", errbuf); status = -saved_errno; goto out_delete_evlist; } - if (perf_target__none(target)) + if (target__none(target)) target->system_wide = true; if (perf_evlist__create_maps(top.evlist, target) < 0) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 329b783..8be17fc 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -149,21 +149,32 @@ static void perf_evsel__delete_priv(struct perf_evsel *evsel) perf_evsel__delete(evsel); } -static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, - void *handler, int idx) +static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler) { - struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction, idx); - - if (evsel) { - evsel->priv = malloc(sizeof(struct syscall_tp)); - - if (evsel->priv == NULL) - goto out_delete; - + evsel->priv = malloc(sizeof(struct syscall_tp)); + if (evsel->priv != NULL) { if (perf_evsel__init_sc_tp_uint_field(evsel, id)) goto out_delete; evsel->handler = handler; + return 0; + } + + return -ENOMEM; + +out_delete: + free(evsel->priv); + evsel->priv = NULL; + return -ENOENT; +} + +static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, void *handler) +{ + struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction); + + if (evsel) { + if (perf_evsel__init_syscall_tp(evsel, handler)) + goto out_delete; } return evsel; @@ -186,17 +197,16 @@ static int perf_evlist__add_syscall_newtp(struct perf_evlist *evlist, void *sys_exit_handler) { int ret = -1; - int idx = evlist->nr_entries; struct perf_evsel *sys_enter, *sys_exit; - sys_enter = perf_evsel__syscall_newtp("sys_enter", sys_enter_handler, idx++); + sys_enter = perf_evsel__syscall_newtp("sys_enter", sys_enter_handler); if (sys_enter == NULL) goto out; if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args)) goto out_delete_sys_enter; - sys_exit = perf_evsel__syscall_newtp("sys_exit", sys_exit_handler, idx++); + sys_exit = perf_evsel__syscall_newtp("sys_exit", sys_exit_handler); if (sys_exit == NULL) goto out_delete_sys_enter; @@ -953,7 +963,8 @@ static struct syscall_fmt { { .name = "mmap", .hexret = true, .arg_scnprintf = { [0] = SCA_HEX, /* addr */ [2] = SCA_MMAP_PROT, /* prot */ - [3] = SCA_MMAP_FLAGS, /* flags */ }, }, + [3] = SCA_MMAP_FLAGS, /* flags */ + [4] = SCA_FD, /* fd */ }, }, { .name = "mprotect", .errmsg = true, .arg_scnprintf = { [0] = SCA_HEX, /* start */ [2] = SCA_MMAP_PROT, /* prot */ }, }, @@ -1157,6 +1168,7 @@ struct trace { bool sched; bool multiple_threads; bool summary; + bool summary_only; bool show_comm; bool show_tool_stats; double duration_filter; @@ -1342,15 +1354,8 @@ static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) if (trace->host == NULL) return -ENOMEM; - if (perf_target__has_task(&trace->opts.target)) { - err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads, - trace__tool_process, - trace->host); - } else { - err = perf_event__synthesize_threads(&trace->tool, trace__tool_process, - trace->host); - } - + err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, + evlist->threads, trace__tool_process, false); if (err) symbol__exit(); @@ -1607,7 +1612,7 @@ static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, args, trace, thread); if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { - if (!trace->duration_filter) { + if (!trace->duration_filter && !trace->summary_only) { trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output); fprintf(trace->output, "%-70s\n", ttrace->entry_str); } @@ -1660,6 +1665,9 @@ static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, } else if (trace->duration_filter) goto out; + if (trace->summary_only) + goto out; + trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output); if (ttrace->entry_pending) { @@ -1762,16 +1770,6 @@ static int trace__process_sample(struct perf_tool *tool, return err; } -static bool -perf_session__has_tp(struct perf_session *session, const char *name) -{ - struct perf_evsel *evsel; - - evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name); - - return evsel != NULL; -} - static int parse_target_str(struct trace *trace) { if (trace->opts.target.pid) { @@ -1824,8 +1822,7 @@ static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp); static void perf_evlist__add_vfs_getname(struct perf_evlist *evlist) { - struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname", - evlist->nr_entries); + struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname"); if (evsel == NULL) return; @@ -2009,8 +2006,6 @@ out_error: static int trace__replay(struct trace *trace) { const struct perf_evsel_str_handler handlers[] = { - { "raw_syscalls:sys_enter", trace__sys_enter, }, - { "raw_syscalls:sys_exit", trace__sys_exit, }, { "probe:vfs_getname", trace__vfs_getname, }, }; struct perf_data_file file = { @@ -2018,6 +2013,7 @@ static int trace__replay(struct trace *trace) .mode = PERF_DATA_MODE_READ, }; struct perf_session *session; + struct perf_evsel *evsel; int err = -1; trace->tool.sample = trace__process_sample; @@ -2049,13 +2045,29 @@ static int trace__replay(struct trace *trace) if (err) goto out; - if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) { - pr_err("Data file does not have raw_syscalls:sys_enter events\n"); + evsel = perf_evlist__find_tracepoint_by_name(session->evlist, + "raw_syscalls:sys_enter"); + if (evsel == NULL) { + pr_err("Data file does not have raw_syscalls:sys_enter event\n"); goto out; } - if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) { - pr_err("Data file does not have raw_syscalls:sys_exit events\n"); + if (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 || + perf_evsel__init_sc_tp_ptr_field(evsel, args)) { + pr_err("Error during initialize raw_syscalls:sys_enter event\n"); + goto out; + } + + evsel = perf_evlist__find_tracepoint_by_name(session->evlist, + "raw_syscalls:sys_exit"); + if (evsel == NULL) { + pr_err("Data file does not have raw_syscalls:sys_exit event\n"); + goto out; + } + + if (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 || + perf_evsel__init_sc_tp_uint_field(evsel, ret)) { + pr_err("Error during initialize raw_syscalls:sys_exit event\n"); goto out; } @@ -2082,12 +2094,7 @@ static size_t trace__fprintf_threads_header(FILE *fp) { size_t printed; - printed = fprintf(fp, "\n _____________________________________________________________________________\n"); - printed += fprintf(fp, " __) Summary of events (__\n\n"); - printed += fprintf(fp, " [ task - pid ] [ events ] [ ratio ] [ runtime ]\n"); - printed += fprintf(fp, " syscall count min max avg stddev\n"); - printed += fprintf(fp, " msec msec msec %%\n"); - printed += fprintf(fp, " _____________________________________________________________________________\n\n"); + printed = fprintf(fp, "\n Summary of events:\n\n"); return printed; } @@ -2105,6 +2112,10 @@ static size_t thread__dump_stats(struct thread_trace *ttrace, printed += fprintf(fp, "\n"); + printed += fprintf(fp, " syscall calls min avg max stddev\n"); + printed += fprintf(fp, " (msec) (msec) (msec) (%%)\n"); + printed += fprintf(fp, " --------------- -------- --------- --------- --------- ------\n"); + /* each int_node is a syscall */ while (inode) { stats = inode->priv; @@ -2119,10 +2130,10 @@ static size_t thread__dump_stats(struct thread_trace *ttrace, avg /= NSEC_PER_MSEC; sc = &trace->syscalls.table[inode->i]; - printed += fprintf(fp, "%24s %14s : ", "", sc->name); - printed += fprintf(fp, "%5" PRIu64 " %8.3f %8.3f", - n, min, max); - printed += fprintf(fp, " %8.3f %6.2f\n", avg, pct); + printed += fprintf(fp, " %-15s", sc->name); + printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f", + n, min, avg); + printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct); } inode = intlist__next(inode); @@ -2163,10 +2174,10 @@ static int trace__fprintf_one_thread(struct thread *thread, void *priv) else if (ratio > 5.0) color = PERF_COLOR_YELLOW; - printed += color_fprintf(fp, color, "%20s", thread__comm_str(thread)); - printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events); - printed += color_fprintf(fp, color, "%5.1f%%", ratio); - printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms); + printed += color_fprintf(fp, color, " %s (%d), ", thread__comm_str(thread), thread->tid); + printed += fprintf(fp, "%lu events, ", ttrace->nr_events); + printed += color_fprintf(fp, color, "%.1f%%", ratio); + printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms); printed += thread__dump_stats(ttrace, trace, fp); data->printed += printed; @@ -2275,8 +2286,10 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) OPT_INCR('v', "verbose", &verbose, "be more verbose"), OPT_BOOLEAN('T', "time", &trace.full_time, "Show full timestamp, not time relative to first start"), - OPT_BOOLEAN(0, "summary", &trace.summary, - "Show syscall summary with statistics"), + OPT_BOOLEAN('s', "summary", &trace.summary_only, + "Show only syscall summary with statistics"), + OPT_BOOLEAN('S', "with-summary", &trace.summary, + "Show all syscalls and summary with statistics"), OPT_END() }; int err; @@ -2287,6 +2300,10 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) argc = parse_options(argc, argv, trace_options, trace_usage, 0); + /* summary_only implies summary option, but don't overwrite summary if set */ + if (trace.summary_only) + trace.summary = trace.summary_only; + if (output_name != NULL) { err = trace__open_output(&trace, output_name); if (err < 0) { @@ -2310,21 +2327,21 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) } } - err = perf_target__validate(&trace.opts.target); + err = target__validate(&trace.opts.target); if (err) { - perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); + target__strerror(&trace.opts.target, err, bf, sizeof(bf)); fprintf(trace.output, "%s", bf); goto out_close; } - err = perf_target__parse_uid(&trace.opts.target); + err = target__parse_uid(&trace.opts.target); if (err) { - perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); + target__strerror(&trace.opts.target, err, bf, sizeof(bf)); fprintf(trace.output, "%s", bf); goto out_close; } - if (!argc && perf_target__none(&trace.opts.target)) + if (!argc && target__none(&trace.opts.target)) trace.opts.target.system_wide = true; if (input_name) diff --git a/tools/perf/config/Makefile b/tools/perf/config/Makefile index f5905f2..f7d11a8 100644 --- a/tools/perf/config/Makefile +++ b/tools/perf/config/Makefile @@ -142,7 +142,8 @@ CORE_FEATURE_TESTS = \ libunwind \ on-exit \ stackprotector \ - stackprotector-all + stackprotector-all \ + timerfd # # So here we detect whether test-all was rebuilt, to be able @@ -328,8 +329,14 @@ ifndef NO_LIBUNWIND msg := $(warning No libunwind found, disabling post unwind support. Please install libunwind-dev[el] >= 1.1); NO_LIBUNWIND := 1 else - ifneq ($(feature-libunwind-debug-frame), 1) - msg := $(warning No debug_frame support found in libunwind); + ifeq ($(ARCH),arm) + $(call feature_check,libunwind-debug-frame) + ifneq ($(feature-libunwind-debug-frame), 1) + msg := $(warning No debug_frame support found in libunwind); + CFLAGS += -DNO_LIBUNWIND_DEBUG_FRAME + endif + else + # non-ARM has no dwarf_find_debug_frame() function: CFLAGS += -DNO_LIBUNWIND_DEBUG_FRAME endif endif @@ -405,7 +412,6 @@ else endif endif -$(call feature_check,timerfd) ifeq ($(feature-timerfd), 1) CFLAGS += -DHAVE_TIMERFD_SUPPORT else diff --git a/tools/perf/config/feature-checks/Makefile b/tools/perf/config/feature-checks/Makefile index e8e195f..87e7900 100644 --- a/tools/perf/config/feature-checks/Makefile +++ b/tools/perf/config/feature-checks/Makefile @@ -76,6 +76,9 @@ test-libnuma: test-libunwind: $(BUILD) $(LIBUNWIND_LIBS) -lelf +test-libunwind-debug-frame: + $(BUILD) $(LIBUNWIND_LIBS) -lelf + test-libaudit: $(BUILD) -laudit diff --git a/tools/perf/config/feature-checks/test-all.c b/tools/perf/config/feature-checks/test-all.c index 799865b..59e7a70 100644 --- a/tools/perf/config/feature-checks/test-all.c +++ b/tools/perf/config/feature-checks/test-all.c @@ -49,10 +49,6 @@ # include "test-libunwind.c" #undef main -#define main main_test_libunwind_debug_frame -# include "test-libunwind-debug-frame.c" -#undef main - #define main main_test_libaudit # include "test-libaudit.c" #undef main diff --git a/tools/perf/perf.h b/tools/perf/perf.h index 6a587e84..b079304 100644 --- a/tools/perf/perf.h +++ b/tools/perf/perf.h @@ -248,7 +248,7 @@ enum perf_call_graph_mode { }; struct perf_record_opts { - struct perf_target target; + struct target target; int call_graph; bool group; bool inherit_stat; diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c index 49ccc3b..85d4919 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c @@ -275,8 +275,19 @@ static int process_event(struct machine *machine, struct perf_evlist *evlist, if (event->header.type == PERF_RECORD_SAMPLE) return process_sample_event(machine, evlist, event, state); - if (event->header.type < PERF_RECORD_MAX) - return machine__process_event(machine, event, NULL); + if (event->header.type == PERF_RECORD_THROTTLE || + event->header.type == PERF_RECORD_UNTHROTTLE) + return 0; + + if (event->header.type < PERF_RECORD_MAX) { + int ret; + + ret = machine__process_event(machine, event, NULL); + if (ret < 0) + pr_debug("machine__process_event failed, event type %u\n", + event->header.type); + return ret; + } return 0; } @@ -441,7 +452,7 @@ static int do_test_code_reading(bool try_kcore) } ret = perf_event__synthesize_thread_map(NULL, threads, - perf_event__process, machine); + perf_event__process, machine, false); if (ret < 0) { pr_debug("perf_event__synthesize_thread_map failed\n"); goto out_err; diff --git a/tools/perf/tests/evsel-tp-sched.c b/tools/perf/tests/evsel-tp-sched.c index 9b98c15..4774f7f 100644 --- a/tools/perf/tests/evsel-tp-sched.c +++ b/tools/perf/tests/evsel-tp-sched.c @@ -32,7 +32,7 @@ static int perf_evsel__test_field(struct perf_evsel *evsel, const char *name, int test__perf_evsel__tp_sched_test(void) { - struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch", 0); + struct perf_evsel *evsel = perf_evsel__newtp("sched", "sched_switch"); int ret = 0; if (evsel == NULL) { @@ -63,7 +63,7 @@ int test__perf_evsel__tp_sched_test(void) perf_evsel__delete(evsel); - evsel = perf_evsel__newtp("sched", "sched_wakeup", 0); + evsel = perf_evsel__newtp("sched", "sched_wakeup"); if (perf_evsel__test_field(evsel, "comm", 16, true)) ret = -1; diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index a7232c2..d64ab79 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c @@ -65,7 +65,7 @@ int test__basic_mmap(void) char name[64]; snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]); - evsels[i] = perf_evsel__newtp("syscalls", name, i); + evsels[i] = perf_evsel__newtp("syscalls", name); if (evsels[i] == NULL) { pr_debug("perf_evsel__new\n"); goto out_free_evlist; diff --git a/tools/perf/tests/open-syscall-all-cpus.c b/tools/perf/tests/open-syscall-all-cpus.c index b0657a9..5fecdbd 100644 --- a/tools/perf/tests/open-syscall-all-cpus.c +++ b/tools/perf/tests/open-syscall-all-cpus.c @@ -26,7 +26,7 @@ int test__open_syscall_event_on_all_cpus(void) CPU_ZERO(&cpu_set); - evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0); + evsel = perf_evsel__newtp("syscalls", "sys_enter_open"); if (evsel == NULL) { pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); goto out_thread_map_delete; diff --git a/tools/perf/tests/open-syscall-tp-fields.c b/tools/perf/tests/open-syscall-tp-fields.c index 524b221..41cc0ba 100644 --- a/tools/perf/tests/open-syscall-tp-fields.c +++ b/tools/perf/tests/open-syscall-tp-fields.c @@ -27,7 +27,7 @@ int test__syscall_open_tp_fields(void) goto out; } - evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0); + evsel = perf_evsel__newtp("syscalls", "sys_enter_open"); if (evsel == NULL) { pr_debug("%s: perf_evsel__newtp\n", __func__); goto out_delete_evlist; diff --git a/tools/perf/tests/open-syscall.c b/tools/perf/tests/open-syscall.c index befc067..c1dc7d2 100644 --- a/tools/perf/tests/open-syscall.c +++ b/tools/perf/tests/open-syscall.c @@ -15,7 +15,7 @@ int test__open_syscall_event(void) return -1; } - evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0); + evsel = perf_evsel__newtp("syscalls", "sys_enter_open"); if (evsel == NULL) { pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); goto out_thread_map_delete; diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-events.c index ef671cd..3cbd104 100644 --- a/tools/perf/tests/parse-events.c +++ b/tools/perf/tests/parse-events.c @@ -441,9 +441,8 @@ static int test__checkevent_pmu_name(struct perf_evlist *evlist) static int test__checkevent_pmu_events(struct perf_evlist *evlist) { - struct perf_evsel *evsel; + struct perf_evsel *evsel = perf_evlist__first(evlist); - evsel = list_entry(evlist->entries.next, struct perf_evsel, node); TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries); TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type); TEST_ASSERT_VAL("wrong exclude_user", diff --git a/tools/perf/tests/sw-clock.c b/tools/perf/tests/sw-clock.c index 6e2b44e..6664a7c 100644 --- a/tools/perf/tests/sw-clock.c +++ b/tools/perf/tests/sw-clock.c @@ -9,7 +9,7 @@ #include "util/cpumap.h" #include "util/thread_map.h" -#define NR_LOOPS 1000000 +#define NR_LOOPS 10000000 /* * This test will open software clock events (cpu-clock, task-clock) @@ -34,7 +34,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) .freq = 1, }; - attr.sample_freq = 10000; + attr.sample_freq = 500; evlist = perf_evlist__new(); if (evlist == NULL) { @@ -42,7 +42,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) return -1; } - evsel = perf_evsel__new(&attr, 0); + evsel = perf_evsel__new(&attr); if (evsel == NULL) { pr_debug("perf_evsel__new\n"); goto out_free_evlist; @@ -57,7 +57,14 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id) goto out_delete_maps; } - perf_evlist__open(evlist); + if (perf_evlist__open(evlist)) { + const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate"; + + err = -errno; + pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n", + strerror(errno), knob, (u64)attr.sample_freq); + goto out_delete_maps; + } err = perf_evlist__mmap(evlist, 128, true); if (err < 0) { diff --git a/tools/perf/tests/task-exit.c b/tools/perf/tests/task-exit.c index c33d95f..d09ab57 100644 --- a/tools/perf/tests/task-exit.c +++ b/tools/perf/tests/task-exit.c @@ -28,7 +28,7 @@ int test__task_exit(void) union perf_event *event; struct perf_evsel *evsel; struct perf_evlist *evlist; - struct perf_target target = { + struct target target = { .uid = UINT_MAX, .uses_mmap = true, }; diff --git a/tools/perf/ui/browser.c b/tools/perf/ui/browser.c index bbc782e..cbaa7af 100644 --- a/tools/perf/ui/browser.c +++ b/tools/perf/ui/browser.c @@ -569,7 +569,7 @@ void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence) browser->top = browser->top + browser->top_idx + offset; break; case SEEK_END: - browser->top = browser->top + browser->nr_entries + offset; + browser->top = browser->top + browser->nr_entries - 1 + offset; break; default: return; @@ -680,7 +680,7 @@ static void __ui_browser__line_arrow_down(struct ui_browser *browser, if (end >= browser->top_idx + browser->height) end_row = browser->height - 1; else - end_row = end - browser->top_idx;; + end_row = end - browser->top_idx; ui_browser__gotorc(browser, row, column); SLsmg_draw_vline(end_row - row + 1); diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c index 16848bb..a440e03 100644 --- a/tools/perf/ui/browsers/hists.c +++ b/tools/perf/ui/browsers/hists.c @@ -1847,15 +1847,15 @@ browse_hists: switch (key) { case K_TAB: if (pos->node.next == &evlist->entries) - pos = list_entry(evlist->entries.next, struct perf_evsel, node); + pos = perf_evlist__first(evlist); else - pos = list_entry(pos->node.next, struct perf_evsel, node); + pos = perf_evsel__next(pos); goto browse_hists; case K_UNTAB: if (pos->node.prev == &evlist->entries) - pos = list_entry(evlist->entries.prev, struct perf_evsel, node); + pos = perf_evlist__last(evlist); else - pos = list_entry(pos->node.prev, struct perf_evsel, node); + pos = perf_evsel__prev(pos); goto browse_hists; case K_ESC: if (!ui_browser__dialog_yesno(&menu->b, @@ -1943,8 +1943,7 @@ int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help, single_entry: if (nr_entries == 1) { - struct perf_evsel *first = list_entry(evlist->entries.next, - struct perf_evsel, node); + struct perf_evsel *first = perf_evlist__first(evlist); const char *ev_name = perf_evsel__name(first); return perf_evsel__hists_browse(first, nr_entries, help, diff --git a/tools/perf/ui/tui/progress.c b/tools/perf/ui/tui/progress.c index 3e2d936..c61d14b 100644 --- a/tools/perf/ui/tui/progress.c +++ b/tools/perf/ui/tui/progress.c @@ -18,13 +18,14 @@ static void tui_progress__update(struct ui_progress *p) if (p->total == 0) return; - ui__refresh_dimensions(true); + ui__refresh_dimensions(false); pthread_mutex_lock(&ui__lock); y = SLtt_Screen_Rows / 2 - 2; SLsmg_set_color(0); SLsmg_draw_box(y, 0, 3, SLtt_Screen_Cols); SLsmg_gotorc(y++, 1); SLsmg_write_string((char *)p->title); + SLsmg_fill_region(y, 1, 1, SLtt_Screen_Cols - 2, ' '); SLsmg_set_color(HE_COLORSET_SELECTED); bar = ((SLtt_Screen_Cols - 2) * p->curr) / p->total; SLsmg_fill_region(y, 1, 1, bar, ' '); diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index ec9ae11..bb788c1 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -170,7 +170,8 @@ static int perf_event__synthesize_mmap_events(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid, perf_event__handler_t process, - struct machine *machine) + struct machine *machine, + bool mmap_data) { char filename[PATH_MAX]; FILE *fp; @@ -188,10 +189,6 @@ static int perf_event__synthesize_mmap_events(struct perf_tool *tool, } event->header.type = PERF_RECORD_MMAP; - /* - * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c - */ - event->header.misc = PERF_RECORD_MISC_USER; while (1) { char bf[BUFSIZ]; @@ -212,12 +209,22 @@ static int perf_event__synthesize_mmap_events(struct perf_tool *tool, &event->mmap.start, &event->mmap.len, prot, &event->mmap.pgoff, execname); - - if (n != 5) + /* + * Anon maps don't have the execname. + */ + if (n < 4) continue; + /* + * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c + */ + event->header.misc = PERF_RECORD_MISC_USER; - if (prot[2] != 'x') - continue; + if (prot[2] != 'x') { + if (!mmap_data || prot[0] != 'r') + continue; + + event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; + } if (!strcmp(execname, "")) strcpy(execname, anonstr); @@ -304,20 +311,21 @@ static int __event__synthesize_thread(union perf_event *comm_event, pid_t pid, int full, perf_event__handler_t process, struct perf_tool *tool, - struct machine *machine) + struct machine *machine, bool mmap_data) { pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full, process, machine); if (tgid == -1) return -1; return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, - process, machine); + process, machine, mmap_data); } int perf_event__synthesize_thread_map(struct perf_tool *tool, struct thread_map *threads, perf_event__handler_t process, - struct machine *machine) + struct machine *machine, + bool mmap_data) { union perf_event *comm_event, *mmap_event; int err = -1, thread, j; @@ -334,7 +342,8 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, for (thread = 0; thread < threads->nr; ++thread) { if (__event__synthesize_thread(comm_event, mmap_event, threads->map[thread], 0, - process, tool, machine)) { + process, tool, machine, + mmap_data)) { err = -1; break; } @@ -356,10 +365,10 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool, /* if not, generate events for it */ if (need_leader && - __event__synthesize_thread(comm_event, - mmap_event, - comm_event->comm.pid, 0, - process, tool, machine)) { + __event__synthesize_thread(comm_event, mmap_event, + comm_event->comm.pid, 0, + process, tool, machine, + mmap_data)) { err = -1; break; } @@ -374,7 +383,7 @@ out: int perf_event__synthesize_threads(struct perf_tool *tool, perf_event__handler_t process, - struct machine *machine) + struct machine *machine, bool mmap_data) { DIR *proc; struct dirent dirent, *next; @@ -404,7 +413,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool, * one thread couldn't be synthesized. */ __event__synthesize_thread(comm_event, mmap_event, pid, 1, - process, tool, machine); + process, tool, machine, mmap_data); } err = 0; @@ -528,19 +537,22 @@ int perf_event__process_lost(struct perf_tool *tool __maybe_unused, size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) { - return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %s\n", + return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n", event->mmap.pid, event->mmap.tid, event->mmap.start, - event->mmap.len, event->mmap.pgoff, event->mmap.filename); + event->mmap.len, event->mmap.pgoff, + (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', + event->mmap.filename); } size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) { return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 - " %02x:%02x %"PRIu64" %"PRIu64"]: %s\n", + " %02x:%02x %"PRIu64" %"PRIu64"]: %c %s\n", event->mmap2.pid, event->mmap2.tid, event->mmap2.start, event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, event->mmap2.min, event->mmap2.ino, event->mmap2.ino_generation, + (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', event->mmap2.filename); } diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index f8d70f3..30fec99 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h @@ -208,10 +208,10 @@ typedef int (*perf_event__handler_t)(struct perf_tool *tool, int perf_event__synthesize_thread_map(struct perf_tool *tool, struct thread_map *threads, perf_event__handler_t process, - struct machine *machine); + struct machine *machine, bool mmap_data); int perf_event__synthesize_threads(struct perf_tool *tool, perf_event__handler_t process, - struct machine *machine); + struct machine *machine, bool mmap_data); int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, perf_event__handler_t process, struct machine *machine, diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index b939221..bbc746a 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -117,6 +117,8 @@ void perf_evlist__delete(struct perf_evlist *evlist) void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) { list_add_tail(&entry->node, &evlist->entries); + entry->idx = evlist->nr_entries; + if (!evlist->nr_entries++) perf_evlist__set_id_pos(evlist); } @@ -165,7 +167,7 @@ int perf_evlist__add_default(struct perf_evlist *evlist) event_attr_init(&attr); - evsel = perf_evsel__new(&attr, 0); + evsel = perf_evsel__new(&attr); if (evsel == NULL) goto error; @@ -190,7 +192,7 @@ static int perf_evlist__add_attrs(struct perf_evlist *evlist, size_t i; for (i = 0; i < nr_attrs; i++) { - evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); + evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); if (evsel == NULL) goto out_delete_partial_list; list_add_tail(&evsel->node, &head); @@ -249,9 +251,8 @@ perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, int perf_evlist__add_newtp(struct perf_evlist *evlist, const char *sys, const char *name, void *handler) { - struct perf_evsel *evsel; + struct perf_evsel *evsel = perf_evsel__newtp(sys, name); - evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); if (evsel == NULL) return -1; @@ -704,12 +705,10 @@ static size_t perf_evlist__mmap_size(unsigned long pages) return (pages + 1) * page_size; } -int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, - int unset __maybe_unused) +static long parse_pages_arg(const char *str, unsigned long min, + unsigned long max) { - unsigned int *mmap_pages = opt->value; unsigned long pages, val; - size_t size; static struct parse_tag tags[] = { { .tag = 'B', .mult = 1 }, { .tag = 'K', .mult = 1 << 10 }, @@ -718,33 +717,49 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, { .tag = 0 }, }; + if (str == NULL) + return -EINVAL; + val = parse_tag_value(str, tags); if (val != (unsigned long) -1) { /* we got file size value */ pages = PERF_ALIGN(val, page_size) / page_size; - if (pages < (1UL << 31) && !is_power_of_2(pages)) { - pages = next_pow2(pages); - pr_info("rounding mmap pages size to %lu (%lu pages)\n", - pages * page_size, pages); - } } else { /* we got pages count value */ char *eptr; pages = strtoul(str, &eptr, 10); - if (*eptr != '\0') { - pr_err("failed to parse --mmap_pages/-m value\n"); - return -1; - } + if (*eptr != '\0') + return -EINVAL; } - if (pages > UINT_MAX || pages > SIZE_MAX / page_size) { - pr_err("--mmap_pages/-m value too big\n"); - return -1; + if ((pages == 0) && (min == 0)) { + /* leave number of pages at 0 */ + } else if (pages < (1UL << 31) && !is_power_of_2(pages)) { + /* round pages up to next power of 2 */ + pages = next_pow2(pages); + pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", + pages * page_size, pages); } - size = perf_evlist__mmap_size(pages); - if (!size) { - pr_err("--mmap_pages/-m value must be a power of two."); + if (pages > max) + return -EINVAL; + + return pages; +} + +int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, + int unset __maybe_unused) +{ + unsigned int *mmap_pages = opt->value; + unsigned long max = UINT_MAX; + long pages; + + if (max < SIZE_MAX / page_size) + max = SIZE_MAX / page_size; + + pages = parse_pages_arg(str, 1, max); + if (pages < 0) { + pr_err("Invalid argument for --mmap_pages/-m\n"); return -1; } @@ -796,8 +811,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, return perf_evlist__mmap_per_cpu(evlist, prot, mask); } -int perf_evlist__create_maps(struct perf_evlist *evlist, - struct perf_target *target) +int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) { evlist->threads = thread_map__new_str(target->pid, target->tid, target->uid); @@ -805,9 +819,11 @@ int perf_evlist__create_maps(struct perf_evlist *evlist, if (evlist->threads == NULL) return -1; - if (perf_target__has_task(target)) + if (target->force_per_cpu) + evlist->cpus = cpu_map__new(target->cpu_list); + else if (target__has_task(target)) evlist->cpus = cpu_map__dummy_new(); - else if (!perf_target__has_cpu(target) && !target->uses_mmap) + else if (!target__has_cpu(target) && !target->uses_mmap) evlist->cpus = cpu_map__dummy_new(); else evlist->cpus = cpu_map__new(target->cpu_list); @@ -1016,8 +1032,7 @@ out_err: return err; } -int perf_evlist__prepare_workload(struct perf_evlist *evlist, - struct perf_target *target, +int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, const char *argv[], bool pipe_output, bool want_signal) { @@ -1069,7 +1084,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, exit(-1); } - if (perf_target__none(target)) + if (target__none(target)) evlist->threads->map[0] = evlist->workload.pid; close(child_ready_pipe[1]); @@ -1135,7 +1150,7 @@ size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) perf_evsel__name(evsel)); } - return printed + fprintf(fp, "\n");; + return printed + fprintf(fp, "\n"); } int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index ecaa582..649d6ea 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -102,7 +102,7 @@ void perf_evlist__config(struct perf_evlist *evlist, int perf_record_opts__config(struct perf_record_opts *opts); int perf_evlist__prepare_workload(struct perf_evlist *evlist, - struct perf_target *target, + struct target *target, const char *argv[], bool pipe_output, bool want_signal); int perf_evlist__start_workload(struct perf_evlist *evlist); @@ -134,8 +134,7 @@ static inline void perf_evlist__set_maps(struct perf_evlist *evlist, evlist->threads = threads; } -int perf_evlist__create_maps(struct perf_evlist *evlist, - struct perf_target *target); +int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target); void perf_evlist__delete_maps(struct perf_evlist *evlist); int perf_evlist__apply_filters(struct perf_evlist *evlist); diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 5280820..46dd4c2 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -168,7 +168,7 @@ void perf_evsel__init(struct perf_evsel *evsel, perf_evsel__calc_id_pos(evsel); } -struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) +struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx) { struct perf_evsel *evsel = zalloc(sizeof(*evsel)); @@ -219,7 +219,7 @@ out: return format; } -struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx) +struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx) { struct perf_evsel *evsel = zalloc(sizeof(*evsel)); @@ -645,7 +645,7 @@ void perf_evsel__config(struct perf_evsel *evsel, } } - if (perf_target__has_cpu(&opts->target)) + if (target__has_cpu(&opts->target) || opts->target.force_per_cpu) perf_evsel__set_sample_bit(evsel, CPU); if (opts->period) @@ -653,7 +653,7 @@ void perf_evsel__config(struct perf_evsel *evsel, if (!perf_missing_features.sample_id_all && (opts->sample_time || !opts->no_inherit || - perf_target__has_cpu(&opts->target))) + target__has_cpu(&opts->target) || opts->target.force_per_cpu)) perf_evsel__set_sample_bit(evsel, TIME); if (opts->raw_samples) { @@ -696,7 +696,7 @@ void perf_evsel__config(struct perf_evsel *evsel, * Setting enable_on_exec for independent events and * group leaders for traced executed by perf. */ - if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel)) + if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel)) attr->enable_on_exec = 1; } @@ -2006,8 +2006,7 @@ bool perf_evsel__fallback(struct perf_evsel *evsel, int err, return false; } -int perf_evsel__open_strerror(struct perf_evsel *evsel, - struct perf_target *target, +int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target, int err, char *msg, size_t size) { switch (err) { diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 64ec8e1..1ea7c92 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -96,8 +96,19 @@ struct thread_map; struct perf_evlist; struct perf_record_opts; -struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx); -struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx); +struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx); + +static inline struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr) +{ + return perf_evsel__new_idx(attr, 0); +} + +struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx); + +static inline struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name) +{ + return perf_evsel__newtp_idx(sys, name, 0); +} struct event_format *event_format__new(const char *sys, const char *name); @@ -268,6 +279,11 @@ static inline struct perf_evsel *perf_evsel__next(struct perf_evsel *evsel) return list_entry(evsel->node.next, struct perf_evsel, node); } +static inline struct perf_evsel *perf_evsel__prev(struct perf_evsel *evsel) +{ + return list_entry(evsel->node.prev, struct perf_evsel, node); +} + /** * perf_evsel__is_group_leader - Return whether given evsel is a leader event * @@ -307,8 +323,7 @@ int perf_evsel__fprintf(struct perf_evsel *evsel, bool perf_evsel__fallback(struct perf_evsel *evsel, int err, char *msg, size_t msgsize); -int perf_evsel__open_strerror(struct perf_evsel *evsel, - struct perf_target *target, +int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target, int err, char *msg, size_t size); static inline int perf_evsel__group_idx(struct perf_evsel *evsel) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 26d9520..369c036 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -2797,7 +2797,7 @@ int perf_session__read_header(struct perf_session *session) perf_event__attr_swap(&f_attr.attr); tmp = lseek(fd, 0, SEEK_CUR); - evsel = perf_evsel__new(&f_attr.attr, i); + evsel = perf_evsel__new(&f_attr.attr); if (evsel == NULL) goto out_delete_evlist; @@ -2916,7 +2916,7 @@ int perf_event__process_attr(struct perf_tool *tool __maybe_unused, return -ENOMEM; } - evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); + evsel = perf_evsel__new(&event->attr.attr); if (evsel == NULL) return -ENOMEM; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index ce034c1..84cdb07 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1368,7 +1368,7 @@ int machine__resolve_callchain(struct machine *machine, return unwind__get_entries(unwind_entry, &callchain_cursor, machine, thread, evsel->attr.sample_regs_user, - sample); + sample, max_stack); } @@ -1394,3 +1394,15 @@ int machine__for_each_thread(struct machine *machine, } return rc; } + +int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, + struct target *target, struct thread_map *threads, + perf_event__handler_t process, bool data_mmap) +{ + if (target__has_task(target)) + return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap); + else if (target__has_cpu(target)) + return perf_event__synthesize_threads(tool, process, machine, data_mmap); + /* command specified */ + return 0; +} diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 2389ba8..4771330 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -4,6 +4,7 @@ #include <sys/types.h> #include <linux/rbtree.h> #include "map.h" +#include "event.h" struct addr_location; struct branch_stack; @@ -178,4 +179,15 @@ int machine__for_each_thread(struct machine *machine, int (*fn)(struct thread *thread, void *p), void *priv); +int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, + struct target *target, struct thread_map *threads, + perf_event__handler_t process, bool data_mmap); +static inline +int machine__synthesize_threads(struct machine *machine, struct target *target, + struct thread_map *threads, bool data_mmap) +{ + return __machine__synthesize_threads(machine, NULL, target, threads, + perf_event__process, data_mmap); +} + #endif /* __PERF_MACHINE_H */ diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index c90e55c..6de6f89 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -277,7 +277,7 @@ static int __add_event(struct list_head *list, int *idx, event_attr_init(attr); - evsel = perf_evsel__new(attr, (*idx)++); + evsel = perf_evsel__new_idx(attr, (*idx)++); if (!evsel) return -ENOMEM; @@ -378,7 +378,7 @@ static int add_tracepoint(struct list_head *list, int *idx, { struct perf_evsel *evsel; - evsel = perf_evsel__newtp(sys_name, evt_name, (*idx)++); + evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); if (!evsel) return -ENOMEM; @@ -1097,7 +1097,7 @@ static bool is_event_supported(u8 type, unsigned config) .threads = { 0 }, }; - evsel = perf_evsel__new(&attr, 0); + evsel = perf_evsel__new(&attr); if (evsel) { ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; perf_evsel__delete(evsel); diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c index 3c1b75c..8b0bb1f 100644 --- a/tools/perf/util/sort.c +++ b/tools/perf/util/sort.c @@ -1137,6 +1137,8 @@ static void sort_entry__setup_elide(struct sort_entry *se, void sort__setup_elide(FILE *output) { + struct sort_entry *se; + sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", output); sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, @@ -1172,4 +1174,15 @@ void sort__setup_elide(FILE *output) "snoop", output); } + /* + * It makes no sense to elide all of sort entries. + * Just revert them to show up again. + */ + list_for_each_entry(se, &hist_entry__sort_list, list) { + if (!se->elide) + return; + } + + list_for_each_entry(se, &hist_entry__sort_list, list) + se->elide = false; } diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c index 065528b..3c778a0 100644 --- a/tools/perf/util/target.c +++ b/tools/perf/util/target.c @@ -13,9 +13,9 @@ #include <string.h> -enum perf_target_errno perf_target__validate(struct perf_target *target) +enum target_errno target__validate(struct target *target) { - enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS; + enum target_errno ret = TARGET_ERRNO__SUCCESS; if (target->pid) target->tid = target->pid; @@ -23,42 +23,42 @@ enum perf_target_errno perf_target__validate(struct perf_target *target) /* CPU and PID are mutually exclusive */ if (target->tid && target->cpu_list) { target->cpu_list = NULL; - if (ret == PERF_ERRNO_TARGET__SUCCESS) - ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU; + if (ret == TARGET_ERRNO__SUCCESS) + ret = TARGET_ERRNO__PID_OVERRIDE_CPU; } /* UID and PID are mutually exclusive */ if (target->tid && target->uid_str) { target->uid_str = NULL; - if (ret == PERF_ERRNO_TARGET__SUCCESS) - ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID; + if (ret == TARGET_ERRNO__SUCCESS) + ret = TARGET_ERRNO__PID_OVERRIDE_UID; } /* UID and CPU are mutually exclusive */ if (target->uid_str && target->cpu_list) { target->cpu_list = NULL; - if (ret == PERF_ERRNO_TARGET__SUCCESS) - ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU; + if (ret == TARGET_ERRNO__SUCCESS) + ret = TARGET_ERRNO__UID_OVERRIDE_CPU; } /* PID and SYSTEM are mutually exclusive */ if (target->tid && target->system_wide) { target->system_wide = false; - if (ret == PERF_ERRNO_TARGET__SUCCESS) - ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM; + if (ret == TARGET_ERRNO__SUCCESS) + ret = TARGET_ERRNO__PID_OVERRIDE_SYSTEM; } /* UID and SYSTEM are mutually exclusive */ if (target->uid_str && target->system_wide) { target->system_wide = false; - if (ret == PERF_ERRNO_TARGET__SUCCESS) - ret = PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM; + if (ret == TARGET_ERRNO__SUCCESS) + ret = TARGET_ERRNO__UID_OVERRIDE_SYSTEM; } return ret; } -enum perf_target_errno perf_target__parse_uid(struct perf_target *target) +enum target_errno target__parse_uid(struct target *target) { struct passwd pwd, *result; char buf[1024]; @@ -66,7 +66,7 @@ enum perf_target_errno perf_target__parse_uid(struct perf_target *target) target->uid = UINT_MAX; if (str == NULL) - return PERF_ERRNO_TARGET__SUCCESS; + return TARGET_ERRNO__SUCCESS; /* Try user name first */ getpwnam_r(str, &pwd, buf, sizeof(buf), &result); @@ -79,22 +79,22 @@ enum perf_target_errno perf_target__parse_uid(struct perf_target *target) int uid = strtol(str, &endptr, 10); if (*endptr != '\0') - return PERF_ERRNO_TARGET__INVALID_UID; + return TARGET_ERRNO__INVALID_UID; getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); if (result == NULL) - return PERF_ERRNO_TARGET__USER_NOT_FOUND; + return TARGET_ERRNO__USER_NOT_FOUND; } target->uid = result->pw_uid; - return PERF_ERRNO_TARGET__SUCCESS; + return TARGET_ERRNO__SUCCESS; } /* - * This must have a same ordering as the enum perf_target_errno. + * This must have a same ordering as the enum target_errno. */ -static const char *perf_target__error_str[] = { +static const char *target__error_str[] = { "PID/TID switch overriding CPU", "PID/TID switch overriding UID", "UID switch overriding CPU", @@ -104,7 +104,7 @@ static const char *perf_target__error_str[] = { "Problems obtaining information for user %s", }; -int perf_target__strerror(struct perf_target *target, int errnum, +int target__strerror(struct target *target, int errnum, char *buf, size_t buflen) { int idx; @@ -124,21 +124,19 @@ int perf_target__strerror(struct perf_target *target, int errnum, return 0; } - if (errnum < __PERF_ERRNO_TARGET__START || - errnum >= __PERF_ERRNO_TARGET__END) + if (errnum < __TARGET_ERRNO__START || errnum >= __TARGET_ERRNO__END) return -1; - idx = errnum - __PERF_ERRNO_TARGET__START; - msg = perf_target__error_str[idx]; + idx = errnum - __TARGET_ERRNO__START; + msg = target__error_str[idx]; switch (errnum) { - case PERF_ERRNO_TARGET__PID_OVERRIDE_CPU - ... PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM: + case TARGET_ERRNO__PID_OVERRIDE_CPU ... TARGET_ERRNO__UID_OVERRIDE_SYSTEM: snprintf(buf, buflen, "%s", msg); break; - case PERF_ERRNO_TARGET__INVALID_UID: - case PERF_ERRNO_TARGET__USER_NOT_FOUND: + case TARGET_ERRNO__INVALID_UID: + case TARGET_ERRNO__USER_NOT_FOUND: snprintf(buf, buflen, msg, target->uid_str); break; diff --git a/tools/perf/util/target.h b/tools/perf/util/target.h index a4be857..2d0c506 100644 --- a/tools/perf/util/target.h +++ b/tools/perf/util/target.h @@ -4,7 +4,7 @@ #include <stdbool.h> #include <sys/types.h> -struct perf_target { +struct target { const char *pid; const char *tid; const char *cpu_list; @@ -12,10 +12,11 @@ struct perf_target { uid_t uid; bool system_wide; bool uses_mmap; + bool force_per_cpu; }; -enum perf_target_errno { - PERF_ERRNO_TARGET__SUCCESS = 0, +enum target_errno { + TARGET_ERRNO__SUCCESS = 0, /* * Choose an arbitrary negative big number not to clash with standard @@ -24,42 +25,40 @@ enum perf_target_errno { * * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html */ - __PERF_ERRNO_TARGET__START = -10000, + __TARGET_ERRNO__START = -10000, + /* for target__validate() */ + TARGET_ERRNO__PID_OVERRIDE_CPU = __TARGET_ERRNO__START, + TARGET_ERRNO__PID_OVERRIDE_UID, + TARGET_ERRNO__UID_OVERRIDE_CPU, + TARGET_ERRNO__PID_OVERRIDE_SYSTEM, + TARGET_ERRNO__UID_OVERRIDE_SYSTEM, - /* for perf_target__validate() */ - PERF_ERRNO_TARGET__PID_OVERRIDE_CPU = __PERF_ERRNO_TARGET__START, - PERF_ERRNO_TARGET__PID_OVERRIDE_UID, - PERF_ERRNO_TARGET__UID_OVERRIDE_CPU, - PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM, - PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM, + /* for target__parse_uid() */ + TARGET_ERRNO__INVALID_UID, + TARGET_ERRNO__USER_NOT_FOUND, - /* for perf_target__parse_uid() */ - PERF_ERRNO_TARGET__INVALID_UID, - PERF_ERRNO_TARGET__USER_NOT_FOUND, - - __PERF_ERRNO_TARGET__END, + __TARGET_ERRNO__END, }; -enum perf_target_errno perf_target__validate(struct perf_target *target); -enum perf_target_errno perf_target__parse_uid(struct perf_target *target); +enum target_errno target__validate(struct target *target); +enum target_errno target__parse_uid(struct target *target); -int perf_target__strerror(struct perf_target *target, int errnum, char *buf, - size_t buflen); +int target__strerror(struct target *target, int errnum, char *buf, size_t buflen); -static inline bool perf_target__has_task(struct perf_target *target) +static inline bool target__has_task(struct target *target) { return target->tid || target->pid || target->uid_str; } -static inline bool perf_target__has_cpu(struct perf_target *target) +static inline bool target__has_cpu(struct target *target) { return target->system_wide || target->cpu_list; } -static inline bool perf_target__none(struct perf_target *target) +static inline bool target__none(struct target *target) { - return !perf_target__has_task(target) && !perf_target__has_cpu(target); + return !target__has_task(target) && !target__has_cpu(target); } #endif /* _PERF_TARGET_H */ diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c index f857b51..ce793c7 100644 --- a/tools/perf/util/top.c +++ b/tools/perf/util/top.c @@ -27,7 +27,7 @@ size_t perf_top__header_snprintf(struct perf_top *top, char *bf, size_t size) float ksamples_per_sec; float esamples_percent; struct perf_record_opts *opts = &top->record_opts; - struct perf_target *target = &opts->target; + struct target *target = &opts->target; size_t ret = 0; if (top->samples) { diff --git a/tools/perf/util/unwind.c b/tools/perf/util/unwind.c index 5390d0b..0efd539 100644 --- a/tools/perf/util/unwind.c +++ b/tools/perf/util/unwind.c @@ -559,7 +559,7 @@ static unw_accessors_t accessors = { }; static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, - void *arg) + void *arg, int max_stack) { unw_addr_space_t addr_space; unw_cursor_t c; @@ -575,7 +575,7 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, if (ret) display_error(ret); - while (!ret && (unw_step(&c) > 0)) { + while (!ret && (unw_step(&c) > 0) && max_stack--) { unw_word_t ip; unw_get_reg(&c, UNW_REG_IP, &ip); @@ -588,7 +588,8 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, int unwind__get_entries(unwind_entry_cb_t cb, void *arg, struct machine *machine, struct thread *thread, - u64 sample_uregs, struct perf_sample *data) + u64 sample_uregs, struct perf_sample *data, + int max_stack) { unw_word_t ip; struct unwind_info ui = { @@ -610,5 +611,5 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg, if (ret) return -ENOMEM; - return get_entries(&ui, cb, arg); + return get_entries(&ui, cb, arg, max_stack); } diff --git a/tools/perf/util/unwind.h b/tools/perf/util/unwind.h index ec0c71a..d5966f49 100644 --- a/tools/perf/util/unwind.h +++ b/tools/perf/util/unwind.h @@ -18,7 +18,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg, struct machine *machine, struct thread *thread, u64 sample_uregs, - struct perf_sample *data); + struct perf_sample *data, int max_stack); int unwind__arch_reg_id(int regnum); #else static inline int @@ -27,7 +27,8 @@ unwind__get_entries(unwind_entry_cb_t cb __maybe_unused, struct machine *machine __maybe_unused, struct thread *thread __maybe_unused, u64 sample_uregs __maybe_unused, - struct perf_sample *data __maybe_unused) + struct perf_sample *data __maybe_unused, + int max_stack __maybe_unused) { return 0; } diff --git a/tools/thermal/tmon/Makefile b/tools/thermal/tmon/Makefile new file mode 100644 index 0000000..4473211 --- /dev/null +++ b/tools/thermal/tmon/Makefile @@ -0,0 +1,47 @@ +VERSION = 1.0 + +BINDIR=usr/bin +WARNFLAGS=-Wall -Wshadow -W -Wformat -Wimplicit-function-declaration -Wimplicit-int +CFLAGS= -O1 ${WARNFLAGS} -fstack-protector +CC=gcc + +CFLAGS+=-D VERSION=\"$(VERSION)\" +LDFLAGS+= +TARGET=tmon + +INSTALL_PROGRAM=install -m 755 -p +DEL_FILE=rm -f + +INSTALL_CONFIGFILE=install -m 644 -p +CONFIG_FILE= +CONFIG_PATH= + + +OBJS = tmon.o tui.o sysfs.o pid.o +OBJS += + +tmon: $(OBJS) Makefile tmon.h + $(CC) ${CFLAGS} $(LDFLAGS) $(OBJS) -o $(TARGET) -lm -lpanel -lncursesw -lpthread + +valgrind: tmon + sudo valgrind -v --track-origins=yes --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./$(TARGET) 1> /dev/null + +install: + - mkdir -p $(INSTALL_ROOT)/$(BINDIR) + - $(INSTALL_PROGRAM) "$(TARGET)" "$(INSTALL_ROOT)/$(BINDIR)/$(TARGET)" + - mkdir -p $(INSTALL_ROOT)/$(CONFIG_PATH) + - $(INSTALL_CONFIGFILE) "$(CONFIG_FILE)" "$(INSTALL_ROOT)/$(CONFIG_PATH)" + +uninstall: + $(DEL_FILE) "$(INSTALL_ROOT)/$(BINDIR)/$(TARGET)" + $(CONFIG_FILE) "$(CONFIG_PATH)" + + +clean: + find . -name "*.o" | xargs $(DEL_FILE) + rm -f $(TARGET) + +dist: + git tag v$(VERSION) + git archive --format=tar --prefix="$(TARGET)-$(VERSION)/" v$(VERSION) | \ + gzip > $(TARGET)-$(VERSION).tar.gz diff --git a/tools/thermal/tmon/README b/tools/thermal/tmon/README new file mode 100644 index 0000000..4579498 --- /dev/null +++ b/tools/thermal/tmon/README @@ -0,0 +1,50 @@ +TMON - A Monitoring and Testing Tool for Linux kernel thermal subsystem + +Why TMON? +========== +Increasingly, Linux is running on thermally constrained devices. The simple +thermal relationship between processor and fan has become past for modern +computers. + +As hardware vendors cope with the thermal constraints on their products, more +and more sensors are added, new cooling capabilities are introduced. The +complexity of the thermal relationship can grow exponentially among cooling +devices, zones, sensors, and trip points. They can also change dynamically. + +To expose such relationship to the userspace, Linux generic thermal layer +introduced sysfs entry at /sys/class/thermal with a matrix of symbolic +links, trip point bindings, and device instances. To traverse such +matrix by hand is not a trivial task. Testing is also difficult in that +thermal conditions are often exception cases that hard to reach in +normal operations. + +TMON is conceived as a tool to help visualize, tune, and test the +complex thermal subsystem. + +Files +===== + tmon.c : main function for set up and configurations. + tui.c : handles ncurses based user interface + sysfs.c : access to the generic thermal sysfs + pid.c : a proportional-integral-derivative (PID) controller + that can be used for thermal relationship training. + +Requirements +============ +Depends on ncurses + +Build +========= +$ make +$ sudo ./tmon -h +Usage: tmon [OPTION...] + -c, --control cooling device in control + -d, --daemon run as daemon, no TUI + -l, --log log data to /var/tmp/tmon.log + -h, --help show this help message + -t, --time-interval set time interval for sampling + -v, --version show version + -g, --debug debug message in syslog + +1. For monitoring only: +$ sudo ./tmon diff --git a/tools/thermal/tmon/pid.c b/tools/thermal/tmon/pid.c new file mode 100644 index 0000000..fd7e9e9 --- /dev/null +++ b/tools/thermal/tmon/pid.c @@ -0,0 +1,131 @@ +/* + * pid.c PID controller for testing cooling devices + * + * + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <sys/types.h> +#include <dirent.h> +#include <libintl.h> +#include <ctype.h> +#include <assert.h> +#include <time.h> +#include <limits.h> +#include <math.h> +#include <sys/stat.h> +#include <syslog.h> + +#include "tmon.h" + +/************************************************************************** + * PID (Proportional-Integral-Derivative) controller is commonly used in + * linear control system, consider the the process. + * G(s) = U(s)/E(s) + * kp = proportional gain + * ki = integral gain + * kd = derivative gain + * Ts + * We use type C Alan Bradley equation which takes set point off the + * output dependency in P and D term. + * + * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k] + * - 2*x[k-1]+x[k-2])/Ts + * + * + ***********************************************************************/ +struct pid_params p_param; +/* cached data from previous loop */ +static double xk_1, xk_2; /* input temperature x[k-#] */ + +/* + * TODO: make PID parameters tuned automatically, + * 1. use CPU burn to produce open loop unit step response + * 2. calculate PID based on Ziegler-Nichols rule + * + * add a flag for tuning PID + */ +int init_thermal_controller(void) +{ + int ret = 0; + + /* init pid params */ + p_param.ts = ticktime; + /* TODO: get it from TUI tuning tab */ + p_param.kp = .36; + p_param.ki = 5.0; + p_param.kd = 0.19; + + p_param.t_target = target_temp_user; + + return ret; +} + +void controller_reset(void) +{ + /* TODO: relax control data when not over thermal limit */ + syslog(LOG_DEBUG, "TC inactive, relax p-state\n"); + p_param.y_k = 0.0; + xk_1 = 0.0; + xk_2 = 0.0; + set_ctrl_state(0); +} + +/* To be called at time interval Ts. Type C PID controller. + * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k] + * - 2*x[k-1]+x[k-2])/Ts + * TODO: add low pass filter for D term + */ +#define GUARD_BAND (2) +void controller_handler(const double xk, double *yk) +{ + double ek; + double p_term, i_term, d_term; + + ek = p_param.t_target - xk; /* error */ + if (ek >= 3.0) { + syslog(LOG_DEBUG, "PID: %3.1f Below set point %3.1f, stop\n", + xk, p_param.t_target); + controller_reset(); + *yk = 0.0; + return; + } + /* compute intermediate PID terms */ + p_term = -p_param.kp * (xk - xk_1); + i_term = p_param.kp * p_param.ki * p_param.ts * ek; + d_term = -p_param.kp * p_param.kd * (xk - 2 * xk_1 + xk_2) / p_param.ts; + /* compute output */ + *yk += p_term + i_term + d_term; + /* update sample data */ + xk_1 = xk; + xk_2 = xk_1; + + /* clamp output adjustment range */ + if (*yk < -LIMIT_HIGH) + *yk = -LIMIT_HIGH; + else if (*yk > -LIMIT_LOW) + *yk = -LIMIT_LOW; + + p_param.y_k = *yk; + + set_ctrl_state(lround(fabs(p_param.y_k))); + +} diff --git a/tools/thermal/tmon/sysfs.c b/tools/thermal/tmon/sysfs.c new file mode 100644 index 0000000..dfe4548 --- /dev/null +++ b/tools/thermal/tmon/sysfs.c @@ -0,0 +1,596 @@ +/* + * sysfs.c sysfs ABI access functions for TMON program + * + * Copyright (C) 2013 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author: Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <dirent.h> +#include <libintl.h> +#include <ctype.h> +#include <time.h> +#include <syslog.h> +#include <sys/time.h> +#include <errno.h> + +#include "tmon.h" + +struct tmon_platform_data ptdata; +const char *trip_type_name[] = { + "critical", + "hot", + "passive", + "active", +}; + +int sysfs_set_ulong(char *path, char *filename, unsigned long val) +{ + FILE *fd; + int ret = -1; + char filepath[256]; + + snprintf(filepath, 256, "%s/%s", path, filename); + + fd = fopen(filepath, "w"); + if (!fd) { + syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath); + return ret; + } + ret = fprintf(fd, "%lu", val); + fclose(fd); + + return 0; +} + +/* history of thermal data, used for control algo */ +#define NR_THERMAL_RECORDS 3 +struct thermal_data_record trec[NR_THERMAL_RECORDS]; +int cur_thermal_record; /* index to the trec array */ + +static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong) +{ + FILE *fd; + int ret = -1; + char filepath[256]; + + snprintf(filepath, 256, "%s/%s", path, filename); + + fd = fopen(filepath, "r"); + if (!fd) { + syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath); + return ret; + } + ret = fscanf(fd, "%lu", p_ulong); + fclose(fd); + + return 0; +} + +static int sysfs_get_string(char *path, char *filename, char *str) +{ + FILE *fd; + int ret = -1; + char filepath[256]; + + snprintf(filepath, 256, "%s/%s", path, filename); + + fd = fopen(filepath, "r"); + if (!fd) { + syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath); + return ret; + } + ret = fscanf(fd, "%256s", str); + fclose(fd); + + return ret; +} + +/* get states of the cooling device instance */ +static int probe_cdev(struct cdev_info *cdi, char *path) +{ + sysfs_get_string(path, "type", cdi->type); + sysfs_get_ulong(path, "max_state", &cdi->max_state); + sysfs_get_ulong(path, "cur_state", &cdi->cur_state); + + syslog(LOG_INFO, "%s: %s: type %s, max %lu, curr %lu inst %d\n", + __func__, path, + cdi->type, cdi->max_state, cdi->cur_state, cdi->instance); + + return 0; +} + +static int str_to_trip_type(char *name) +{ + int i; + + for (i = 0; i < NR_THERMAL_TRIP_TYPE; i++) { + if (!strcmp(name, trip_type_name[i])) + return i; + } + + return -ENOENT; +} + +/* scan and fill in trip point info for a thermal zone and trip point id */ +static int get_trip_point_data(char *tz_path, int tzid, int tpid) +{ + char filename[256]; + char temp_str[256]; + int trip_type; + + if (tpid >= MAX_NR_TRIP) + return -EINVAL; + /* check trip point type */ + snprintf(filename, sizeof(filename), "trip_point_%d_type", tpid); + sysfs_get_string(tz_path, filename, temp_str); + trip_type = str_to_trip_type(temp_str); + if (trip_type < 0) { + syslog(LOG_ERR, "%s:%s no matching type\n", __func__, temp_str); + return -ENOENT; + } + ptdata.tzi[tzid].tp[tpid].type = trip_type; + syslog(LOG_INFO, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__, tzid, + tpid, temp_str, trip_type); + + /* TODO: check attribute */ + + return 0; +} + +/* return instance id for file format such as trip_point_4_temp */ +static int get_instance_id(char *name, int pos, int skip) +{ + char *ch; + int i = 0; + + ch = strtok(name, "_"); + while (ch != NULL) { + ++i; + syslog(LOG_INFO, "%s:%s:%s:%d", __func__, name, ch, i); + ch = strtok(NULL, "_"); + if (pos == i) + return atol(ch + skip); + } + + return -1; +} + +/* Find trip point info of a thermal zone */ +static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi, + int tz_id) +{ + int tp_id; + unsigned long temp_ulong; + + if (strstr(d_name, "trip_point") && + strstr(d_name, "temp")) { + /* check if trip point temp is non-zero + * ignore 0/invalid trip points + */ + sysfs_get_ulong(tz_name, d_name, &temp_ulong); + if (temp_ulong < MAX_TEMP_KC) { + tzi->nr_trip_pts++; + /* found a valid trip point */ + tp_id = get_instance_id(d_name, 2, 0); + syslog(LOG_DEBUG, "tzone %s trip %d temp %lu tpnode %s", + tz_name, tp_id, temp_ulong, d_name); + if (tp_id < 0 || tp_id >= MAX_NR_TRIP) { + syslog(LOG_ERR, "Failed to find TP inst %s\n", + d_name); + return -1; + } + get_trip_point_data(tz_name, tz_id, tp_id); + tzi->tp[tp_id].temp = temp_ulong; + } + } + + return 0; +} + +/* check cooling devices for binding info. */ +static int find_tzone_cdev(struct dirent *nl, char *tz_name, + struct tz_info *tzi, int tz_id, int cid) +{ + unsigned long trip_instance = 0; + char cdev_name_linked[256]; + char cdev_name[256]; + char cdev_trip_name[256]; + int cdev_id; + + if (nl->d_type == DT_LNK) { + syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name, + cid); + tzi->nr_cdev++; + if (tzi->nr_cdev > ptdata.nr_cooling_dev) { + syslog(LOG_ERR, "Err: Too many cdev? %d\n", + tzi->nr_cdev); + return -EINVAL; + } + /* find the link to real cooling device record binding */ + snprintf(cdev_name, 256, "%s/%s", tz_name, nl->d_name); + memset(cdev_name_linked, 0, sizeof(cdev_name_linked)); + if (readlink(cdev_name, cdev_name_linked, + sizeof(cdev_name_linked) - 1) != -1) { + cdev_id = get_instance_id(cdev_name_linked, 1, + sizeof("device") - 1); + syslog(LOG_DEBUG, "cdev %s linked to %s : %d\n", + cdev_name, cdev_name_linked, cdev_id); + tzi->cdev_binding |= (1 << cdev_id); + + /* find the trip point in which the cdev is binded to + * in this tzone + */ + snprintf(cdev_trip_name, 256, "%s%s", nl->d_name, + "_trip_point"); + sysfs_get_ulong(tz_name, cdev_trip_name, + &trip_instance); + /* validate trip point range, e.g. trip could return -1 + * when passive is enabled + */ + if (trip_instance > MAX_NR_TRIP) + trip_instance = 0; + tzi->trip_binding[cdev_id] |= 1 << trip_instance; + syslog(LOG_DEBUG, "cdev %s -> trip:%lu: 0x%lx %d\n", + cdev_name, trip_instance, + tzi->trip_binding[cdev_id], + cdev_id); + + + } + return 0; + } + + return -ENODEV; +} + + + +/***************************************************************************** + * Before calling scan_tzones, thermal sysfs must be probed to determine + * the number of thermal zones and cooling devices. + * We loop through each thermal zone and fill in tz_info struct, i.e. + * ptdata.tzi[] +root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0 +/sys/class/thermal/thermal_zone0 +|-- cdev0 -> ../cooling_device4 +|-- cdev1 -> ../cooling_device3 +|-- cdev10 -> ../cooling_device7 +|-- cdev11 -> ../cooling_device6 +|-- cdev12 -> ../cooling_device5 +|-- cdev2 -> ../cooling_device2 +|-- cdev3 -> ../cooling_device1 +|-- cdev4 -> ../cooling_device0 +|-- cdev5 -> ../cooling_device12 +|-- cdev6 -> ../cooling_device11 +|-- cdev7 -> ../cooling_device10 +|-- cdev8 -> ../cooling_device9 +|-- cdev9 -> ../cooling_device8 +|-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00 +|-- power +`-- subsystem -> ../../../../class/thermal +*****************************************************************************/ +static int scan_tzones(void) +{ + DIR *dir; + struct dirent **namelist; + char tz_name[256]; + int i, j, n, k = 0; + + if (!ptdata.nr_tz_sensor) + return -1; + + for (i = 0; i <= ptdata.max_tz_instance; i++) { + memset(tz_name, 0, sizeof(tz_name)); + snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, i); + + dir = opendir(tz_name); + if (!dir) { + syslog(LOG_INFO, "Thermal zone %s skipped\n", tz_name); + continue; + } + /* keep track of valid tzones */ + n = scandir(tz_name, &namelist, 0, alphasort); + if (n < 0) + syslog(LOG_ERR, "scandir failed in %s", tz_name); + else { + sysfs_get_string(tz_name, "type", ptdata.tzi[k].type); + ptdata.tzi[k].instance = i; + /* detect trip points and cdev attached to this tzone */ + j = 0; /* index for cdev */ + ptdata.tzi[k].nr_cdev = 0; + ptdata.tzi[k].nr_trip_pts = 0; + while (n--) { + char *temp_str; + + if (find_tzone_tp(tz_name, namelist[n]->d_name, + &ptdata.tzi[k], k)) + break; + temp_str = strstr(namelist[n]->d_name, "cdev"); + if (!temp_str) { + free(namelist[n]); + continue; + } + if (!find_tzone_cdev(namelist[n], tz_name, + &ptdata.tzi[k], i, j)) + j++; /* increment cdev index */ + free(namelist[n]); + } + free(namelist); + } + /*TODO: reverse trip points */ + closedir(dir); + syslog(LOG_INFO, "TZ %d has %d cdev\n", i, + ptdata.tzi[k].nr_cdev); + k++; + } + + return 0; +} + +static int scan_cdevs(void) +{ + DIR *dir; + struct dirent **namelist; + char cdev_name[256]; + int i, n, k = 0; + + if (!ptdata.nr_cooling_dev) { + fprintf(stderr, "No cooling devices found\n"); + return 0; + } + for (i = 0; i <= ptdata.max_cdev_instance; i++) { + memset(cdev_name, 0, sizeof(cdev_name)); + snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, i); + + dir = opendir(cdev_name); + if (!dir) { + syslog(LOG_INFO, "Cooling dev %s skipped\n", cdev_name); + /* there is a gap in cooling device id, check again + * for the same index. + */ + continue; + } + + n = scandir(cdev_name, &namelist, 0, alphasort); + if (n < 0) + syslog(LOG_ERR, "scandir failed in %s", cdev_name); + else { + sysfs_get_string(cdev_name, "type", ptdata.cdi[k].type); + ptdata.cdi[k].instance = i; + if (strstr(ptdata.cdi[k].type, ctrl_cdev)) { + ptdata.cdi[k].flag |= CDEV_FLAG_IN_CONTROL; + syslog(LOG_DEBUG, "control cdev id %d\n", i); + } + while (n--) + free(namelist[n]); + free(namelist); + } + closedir(dir); + k++; + } + return 0; +} + + +int probe_thermal_sysfs(void) +{ + DIR *dir; + struct dirent **namelist; + int n; + + dir = opendir(THERMAL_SYSFS); + if (!dir) { + fprintf(stderr, "\nNo thermal sysfs, exit\n"); + return -1; + } + n = scandir(THERMAL_SYSFS, &namelist, 0, alphasort); + if (n < 0) + syslog(LOG_ERR, "scandir failed in thermal sysfs"); + else { + /* detect number of thermal zones and cooling devices */ + while (n--) { + int inst; + + if (strstr(namelist[n]->d_name, CDEV)) { + inst = get_instance_id(namelist[n]->d_name, 1, + sizeof("device") - 1); + /* keep track of the max cooling device since + * there may be gaps. + */ + if (inst > ptdata.max_cdev_instance) + ptdata.max_cdev_instance = inst; + + syslog(LOG_DEBUG, "found cdev: %s %d %d\n", + namelist[n]->d_name, + ptdata.nr_cooling_dev, + ptdata.max_cdev_instance); + ptdata.nr_cooling_dev++; + } else if (strstr(namelist[n]->d_name, TZONE)) { + inst = get_instance_id(namelist[n]->d_name, 1, + sizeof("zone") - 1); + if (inst > ptdata.max_tz_instance) + ptdata.max_tz_instance = inst; + + syslog(LOG_DEBUG, "found tzone: %s %d %d\n", + namelist[n]->d_name, + ptdata.nr_tz_sensor, + ptdata.max_tz_instance); + ptdata.nr_tz_sensor++; + } + free(namelist[n]); + } + free(namelist); + } + syslog(LOG_INFO, "found %d tzone(s), %d cdev(s), target zone %d\n", + ptdata.nr_tz_sensor, ptdata.nr_cooling_dev, + target_thermal_zone); + closedir(dir); + + if (!ptdata.nr_tz_sensor) { + fprintf(stderr, "\nNo thermal zones found, exit\n\n"); + return -1; + } + + ptdata.tzi = calloc(sizeof(struct tz_info), ptdata.max_tz_instance+1); + if (!ptdata.tzi) { + fprintf(stderr, "Err: allocate tz_info\n"); + return -1; + } + + /* we still show thermal zone information if there is no cdev */ + if (ptdata.nr_cooling_dev) { + ptdata.cdi = calloc(sizeof(struct cdev_info), + ptdata.max_cdev_instance + 1); + if (!ptdata.cdi) { + free(ptdata.tzi); + fprintf(stderr, "Err: allocate cdev_info\n"); + return -1; + } + } + + /* now probe tzones */ + if (scan_tzones()) + return -1; + if (scan_cdevs()) + return -1; + return 0; +} + +/* convert sysfs zone instance to zone array index */ +int zone_instance_to_index(int zone_inst) +{ + int i; + + for (i = 0; i < ptdata.nr_tz_sensor; i++) + if (ptdata.tzi[i].instance == zone_inst) + return i; + return -ENOENT; +} + +/* read temperature of all thermal zones */ +int update_thermal_data() +{ + int i; + char tz_name[256]; + static unsigned long samples; + + if (!ptdata.nr_tz_sensor) { + syslog(LOG_ERR, "No thermal zones found!\n"); + return -1; + } + + /* circular buffer for keeping historic data */ + if (cur_thermal_record >= NR_THERMAL_RECORDS) + cur_thermal_record = 0; + gettimeofday(&trec[cur_thermal_record].tv, NULL); + if (tmon_log) { + fprintf(tmon_log, "%lu ", ++samples); + fprintf(tmon_log, "%3.1f ", p_param.t_target); + } + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + memset(tz_name, 0, sizeof(tz_name)); + snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, + ptdata.tzi[i].instance); + sysfs_get_ulong(tz_name, "temp", + &trec[cur_thermal_record].temp[i]); + if (tmon_log) + fprintf(tmon_log, "%lu ", + trec[cur_thermal_record].temp[i]/1000); + } + for (i = 0; i < ptdata.nr_cooling_dev; i++) { + char cdev_name[256]; + unsigned long val; + + snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, + ptdata.cdi[i].instance); + probe_cdev(&ptdata.cdi[i], cdev_name); + val = ptdata.cdi[i].cur_state; + if (val > 1000000) + val = 0; + if (tmon_log) + fprintf(tmon_log, "%lu ", val); + } + + if (tmon_log) { + fprintf(tmon_log, "\n"); + fflush(tmon_log); + } + + return 0; +} + +void set_ctrl_state(unsigned long state) +{ + char ctrl_cdev_path[256]; + int i; + unsigned long cdev_state; + + if (no_control) + return; + /* set all ctrl cdev to the same state */ + for (i = 0; i < ptdata.nr_cooling_dev; i++) { + if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) { + if (ptdata.cdi[i].max_state < 10) { + strcpy(ctrl_cdev, "None."); + return; + } + /* scale to percentage of max_state */ + cdev_state = state * ptdata.cdi[i].max_state/100; + syslog(LOG_DEBUG, + "ctrl cdev %d set state %lu scaled to %lu\n", + ptdata.cdi[i].instance, state, cdev_state); + snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS, + CDEV, ptdata.cdi[i].instance); + syslog(LOG_DEBUG, "ctrl cdev path %s", ctrl_cdev_path); + sysfs_set_ulong(ctrl_cdev_path, "cur_state", + cdev_state); + } + } +} + +void get_ctrl_state(unsigned long *state) +{ + char ctrl_cdev_path[256]; + int ctrl_cdev_id = -1; + int i; + + /* TODO: take average of all ctrl types. also consider change based on + * uevent. Take the first reading for now. + */ + for (i = 0; i < ptdata.nr_cooling_dev; i++) { + if (ptdata.cdi[i].flag & CDEV_FLAG_IN_CONTROL) { + ctrl_cdev_id = ptdata.cdi[i].instance; + syslog(LOG_INFO, "ctrl cdev %d get state\n", + ptdata.cdi[i].instance); + break; + } + } + if (ctrl_cdev_id == -1) { + *state = 0; + return; + } + snprintf(ctrl_cdev_path, 256, "%s/%s%d", THERMAL_SYSFS, + CDEV, ctrl_cdev_id); + sysfs_get_ulong(ctrl_cdev_path, "cur_state", state); +} + +void free_thermal_data(void) +{ + free(ptdata.tzi); + free(ptdata.cdi); +} diff --git a/tools/thermal/tmon/tmon.8 b/tools/thermal/tmon/tmon.8 new file mode 100644 index 0000000..0be727c --- /dev/null +++ b/tools/thermal/tmon/tmon.8 @@ -0,0 +1,142 @@ +.TH TMON 8 +.SH NAME +\fBtmon\fP - A monitoring and testing tool for Linux kernel thermal subsystem + +.SH SYNOPSIS +.ft B +.B tmon +.RB [ Options ] +.br +.SH DESCRIPTION +\fBtmon \fP can be used to visualize thermal relationship and +real-time thermal data; tune +and test cooling devices and sensors; collect thermal data for offline +analysis and plot. \fBtmon\fP must be run as root in order to control device +states via sysfs. +.PP +\fBFunctions\fP +.PP +.nf +1. Thermal relationships: +- show thermal zone information +- show cooling device information +- show trip point binding within each thermal zone +- show trip point and cooling device instance bindings +.PP +2. Real time data display +- show temperature of all thermal zones w.r.t. its trip points and types +- show states of all cooling devices +.PP +3. Thermal relationship learning and device tuning +- with a built-in Proportional Integral Derivative (\fBPID\fP) +controller, user can pair a cooling device to a thermal sensor for +testing the effectiveness and learn about the thermal distance between the two +- allow manual control of cooling device states and target temperature +.PP +4. Data logging in /var/tmp/tmon.log +- contains thermal configuration data, i.e. cooling device, thermal + zones, and trip points. Can be used for data collection in remote + debugging. +- log real-time thermal data into space separated format that can be + directly consumed by plotting tools such as Rscript. + +.SS Options +.PP +The \fB-c --control\fP option sets a cooling device type to control temperature +of a thermal zone +.PP +The \fB-d --daemon\fP option runs \fBtmon \fP as daemon without user interface +.PP +The \fB-g --debug\fP option allow debug messages to be stored in syslog +.PP +The \fB-h --help\fP option shows help message +.PP +The \fB-l --log\fP option write data to /var/tmp/tmon.log +.PP +The \fB-t --time-interval\fP option sets the polling interval in seconds +.PP +The \fB-v --version\fP option shows the version of \fBtmon \fP +.PP +The \fB-z --zone\fP option sets the target therma zone instance to be controlled +.PP + +.SH FIELD DESCRIPTIONS +.nf +.PP +\fBP \fP passive cooling trip point type +\fBA \fP active cooling trip point type (fan) +\fBC \fP critical trip point type +\fBA \fP hot trip point type +\fBkp \fP proportional gain of \fBPID\fP controller +\fBki \fP integral gain of \fBPID\fP controller +\fBkd \fP derivative gain of \fBPID\fP controller + +.SH REQUIREMENT +Build depends on ncurses +.PP +Runtime depends on window size large enough to show the number of +devices found on the system. + +.PP + +.SH INTERACTIVE COMMANDS +.pp +.nf +\fBCtrl-C, q/Q\fP stops \fBtmon\fP +\fBTAB\fP shows tuning pop up panel, choose a letter to modify + +.SH EXAMPLES +Without any parameters, tmon is in monitoring only mode and refresh +screen every 1 second. +.PP +1. For monitoring only: +.nf +$ sudo ./tmon + +2. Use Processor cooling device to control thermal zone 0 at default 65C. +$ sudo ./tmon -c Processor -z 0 + +3. Use intel_powerclamp(idle injection) cooling device to control thermal zone 1 +$ sudo ./tmon -c intel_powerclamp -z 1 + +4. Turn on debug and collect data log at /var/tmp/tmon.log +$ sudo ./tmon -g -l + +For example, the log below shows PID controller was adjusting current states +for all cooling devices with "Processor" type such that thermal zone 0 +can stay below 65 dC. + +#---------- THERMAL DATA LOG STARTED ----------- +Samples TargetTemp acpitz0 acpitz1 Fan0 Fan1 Fan2 Fan3 Fan4 Fan5 +Fan6 Fan7 Fan8 Fan9 Processor10 Processor11 Processor12 Processor13 +LCD14 intel_powerclamp15 1 65.0 65 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 2 +65.0 66 65 0 0 0 0 0 0 0 0 0 0 4 4 4 4 6 0 3 65.0 60 54 0 0 0 0 0 0 0 0 +0 0 4 4 4 4 6 0 4 65.0 53 53 0 0 0 0 0 0 0 0 0 0 4 4 4 4 6 0 +5 65.0 52 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 +6 65.0 53 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 +7 65.0 68 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 +8 65.0 68 68 0 0 0 0 0 0 0 0 0 0 5 5 5 5 6 0 +9 65.0 68 68 0 0 0 0 0 0 0 0 0 0 6 6 6 6 6 0 +10 65.0 67 67 0 0 0 0 0 0 0 0 0 0 7 7 7 7 6 0 +11 65.0 67 67 0 0 0 0 0 0 0 0 0 0 8 8 8 8 6 0 +12 65.0 67 67 0 0 0 0 0 0 0 0 0 0 8 8 8 8 6 0 +13 65.0 67 67 0 0 0 0 0 0 0 0 0 0 9 9 9 9 6 0 +14 65.0 66 66 0 0 0 0 0 0 0 0 0 0 10 10 10 10 6 0 +15 65.0 66 67 0 0 0 0 0 0 0 0 0 0 10 10 10 10 6 0 +16 65.0 66 66 0 0 0 0 0 0 0 0 0 0 11 11 11 11 6 0 +17 65.0 66 66 0 0 0 0 0 0 0 0 0 0 11 11 11 11 6 0 +18 65.0 64 61 0 0 0 0 0 0 0 0 0 0 11 11 11 11 6 0 +19 65.0 60 59 0 0 0 0 0 0 0 0 0 0 12 12 12 12 6 0 + +Data can be read directly into an array by an example R-script below: + +#!/usr/bin/Rscript +tdata <- read.table("/var/tmp/tmon.log", header=T, comment.char="#") +attach(tdata) +jpeg("tmon.jpg") +X11() +g_range <- range(0, intel_powerclamp15, TargetTemp, acpitz0) +plot( Samples, intel_powerclamp15, col="blue", ylim=g_range, axes=FALSE, ann=FALSE) +par(new=TRUE) +lines(TargetTemp, type="o", pch=22, lty=2, col="red") +dev.off() diff --git a/tools/thermal/tmon/tmon.c b/tools/thermal/tmon/tmon.c new file mode 100644 index 0000000..b30f531 --- /dev/null +++ b/tools/thermal/tmon/tmon.c @@ -0,0 +1,352 @@ +/* + * tmon.c Thermal Monitor (TMON) main function and entry point + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author: Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ + +#include <getopt.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <ncurses.h> +#include <ctype.h> +#include <time.h> +#include <signal.h> +#include <limits.h> +#include <sys/time.h> +#include <pthread.h> +#include <math.h> +#include <stdarg.h> +#include <syslog.h> + +#include "tmon.h" + +unsigned long ticktime = 1; /* seconds */ +unsigned long no_control = 1; /* monitoring only or use cooling device for + * temperature control. + */ +double time_elapsed = 0.0; +unsigned long target_temp_user = 65; /* can be select by tui later */ +int dialogue_on; +int tmon_exit; +static short daemon_mode; +static int logging; /* for recording thermal data to a file */ +static int debug_on; +FILE *tmon_log; +/*cooling device used for the PID controller */ +char ctrl_cdev[CDEV_NAME_SIZE] = "None"; +int target_thermal_zone; /* user selected target zone instance */ +static void start_daemon_mode(void); + +pthread_t event_tid; +pthread_mutex_t input_lock; +void usage() +{ + printf("Usage: tmon [OPTION...]\n"); + printf(" -c, --control cooling device in control\n"); + printf(" -d, --daemon run as daemon, no TUI\n"); + printf(" -g, --debug debug message in syslog\n"); + printf(" -h, --help show this help message\n"); + printf(" -l, --log log data to /var/tmp/tmon.log\n"); + printf(" -t, --time-interval sampling time interval, > 1 sec.\n"); + printf(" -v, --version show version\n"); + printf(" -z, --zone target thermal zone id\n"); + + exit(0); +} + +void version() +{ + printf("TMON version %s\n", VERSION); + exit(EXIT_SUCCESS); +} + +static void tmon_cleanup(void) +{ + + syslog(LOG_INFO, "TMON exit cleanup\n"); + fflush(stdout); + refresh(); + if (tmon_log) + fclose(tmon_log); + if (event_tid) { + pthread_mutex_lock(&input_lock); + pthread_cancel(event_tid); + pthread_mutex_unlock(&input_lock); + pthread_mutex_destroy(&input_lock); + } + closelog(); + /* relax control knobs, undo throttling */ + set_ctrl_state(0); + + keypad(stdscr, FALSE); + echo(); + nocbreak(); + close_windows(); + endwin(); + free_thermal_data(); + + exit(1); +} + + +static void tmon_sig_handler(int sig) +{ + syslog(LOG_INFO, "TMON caught signal %d\n", sig); + refresh(); + switch (sig) { + case SIGTERM: + printf("sigterm, exit and clean up\n"); + fflush(stdout); + break; + case SIGKILL: + printf("sigkill, exit and clean up\n"); + fflush(stdout); + break; + case SIGINT: + printf("ctrl-c, exit and clean up\n"); + fflush(stdout); + break; + default: + break; + } + tmon_exit = true; +} + + +static void start_syslog(void) +{ + if (debug_on) + setlogmask(LOG_UPTO(LOG_DEBUG)); + else + setlogmask(LOG_UPTO(LOG_ERR)); + openlog("tmon.log", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); + syslog(LOG_NOTICE, "TMON started by User %d", getuid()); +} + +static void prepare_logging(void) +{ + int i; + + if (!logging) + return; + /* open local data log file */ + tmon_log = fopen(TMON_LOG_FILE, "w+"); + if (!tmon_log) { + syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE); + return; + } + + fprintf(tmon_log, "#----------- THERMAL SYSTEM CONFIG -------------\n"); + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + char binding_str[33]; /* size of long + 1 */ + int j; + + memset(binding_str, 0, sizeof(binding_str)); + for (j = 0; j < 32; j++) + binding_str[j] = (ptdata.tzi[i].cdev_binding & 1<<j) ? + '1' : '0'; + + fprintf(tmon_log, "#thermal zone %s%02d cdevs binding: %32s\n", + ptdata.tzi[i].type, + ptdata.tzi[i].instance, + binding_str); + for (j = 0; j < ptdata.tzi[i].nr_trip_pts; j++) { + fprintf(tmon_log, "#\tTP%02d type:%s, temp:%lu\n", j, + trip_type_name[ptdata.tzi[i].tp[j].type], + ptdata.tzi[i].tp[j].temp); + } + + } + + for (i = 0; i < ptdata.nr_cooling_dev; i++) + fprintf(tmon_log, "#cooling devices%02d: %s\n", + i, ptdata.cdi[i].type); + + fprintf(tmon_log, "#---------- THERMAL DATA LOG STARTED -----------\n"); + fprintf(tmon_log, "Samples TargetTemp "); + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + fprintf(tmon_log, "%s%d ", ptdata.tzi[i].type, + ptdata.tzi[i].instance); + } + for (i = 0; i < ptdata.nr_cooling_dev; i++) + fprintf(tmon_log, "%s%d ", ptdata.cdi[i].type, + ptdata.cdi[i].instance); + + fprintf(tmon_log, "\n"); +} + +static struct option opts[] = { + { "control", 1, NULL, 'c' }, + { "daemon", 0, NULL, 'd' }, + { "time-interval", 1, NULL, 't' }, + { "log", 0, NULL, 'l' }, + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'v' }, + { "debug", 0, NULL, 'g' }, + { 0, 0, NULL, 0 } +}; + + +int main(int argc, char **argv) +{ + int err = 0; + int id2 = 0, c; + double yk = 0.0; /* controller output */ + int target_tz_index; + + if (geteuid() != 0) { + printf("TMON needs to be run as root\n"); + exit(EXIT_FAILURE); + } + + while ((c = getopt_long(argc, argv, "c:dlht:vgz:", opts, &id2)) != -1) { + switch (c) { + case 'c': + no_control = 0; + strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE); + break; + case 'd': + start_daemon_mode(); + printf("Run TMON in daemon mode\n"); + break; + case 't': + ticktime = strtod(optarg, NULL); + if (ticktime < 1) + ticktime = 1; + break; + case 'l': + printf("Logging data to /var/tmp/tmon.log\n"); + logging = 1; + break; + case 'h': + usage(); + break; + case 'v': + version(); + break; + case 'g': + debug_on = 1; + break; + case 'z': + target_thermal_zone = strtod(optarg, NULL); + break; + default: + break; + } + } + if (pthread_mutex_init(&input_lock, NULL) != 0) { + fprintf(stderr, "\n mutex init failed, exit\n"); + return 1; + } + start_syslog(); + if (signal(SIGINT, tmon_sig_handler) == SIG_ERR) + syslog(LOG_DEBUG, "Cannot handle SIGINT\n"); + if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR) + syslog(LOG_DEBUG, "Cannot handle SIGINT\n"); + + if (probe_thermal_sysfs()) { + pthread_mutex_destroy(&input_lock); + closelog(); + return -1; + } + initialize_curses(); + setup_windows(); + signal(SIGWINCH, resize_handler); + show_title_bar(); + show_sensors_w(); + show_cooling_device(); + update_thermal_data(); + show_data_w(); + prepare_logging(); + init_thermal_controller(); + + nodelay(stdscr, TRUE); + err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL); + if (err != 0) { + printf("\ncan't create thread :[%s]", strerror(err)); + tmon_cleanup(); + exit(EXIT_FAILURE); + } + + /* validate range of user selected target zone, default to the first + * instance if out of range + */ + target_tz_index = zone_instance_to_index(target_thermal_zone); + if (target_tz_index < 0) { + target_thermal_zone = ptdata.tzi[0].instance; + syslog(LOG_ERR, "target zone is not found, default to %d\n", + target_thermal_zone); + } + while (1) { + sleep(ticktime); + show_title_bar(); + show_sensors_w(); + update_thermal_data(); + if (!dialogue_on) { + show_data_w(); + show_cooling_device(); + } + cur_thermal_record++; + time_elapsed += ticktime; + controller_handler(trec[0].temp[target_tz_index] / 1000, + &yk); + trec[0].pid_out_pct = yk; + if (!dialogue_on) + show_control_w(); + if (tmon_exit) + break; + } + tmon_cleanup(); + return 0; +} + +static void start_daemon_mode() +{ + daemon_mode = 1; + /* fork */ + pid_t sid, pid = fork(); + if (pid < 0) { + exit(EXIT_FAILURE); + } else if (pid > 0) + /* kill parent */ + exit(EXIT_SUCCESS); + + /* disable TUI, it may not be necessary, but saves some resource */ + disable_tui(); + + /* change the file mode mask */ + umask(0); + + /* new SID for the daemon process */ + sid = setsid(); + if (sid < 0) + exit(EXIT_FAILURE); + + /* change working directory */ + if ((chdir("/")) < 0) + exit(EXIT_FAILURE); + + + sleep(10); + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + +} diff --git a/tools/thermal/tmon/tmon.h b/tools/thermal/tmon/tmon.h new file mode 100644 index 0000000..9e3c49c --- /dev/null +++ b/tools/thermal/tmon/tmon.h @@ -0,0 +1,204 @@ +/* + * tmon.h contains data structures and constants used by TMON + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ + +#ifndef TMON_H +#define TMON_H + +#define MAX_DISP_TEMP 125 +#define MAX_CTRL_TEMP 105 +#define MIN_CTRL_TEMP 40 +#define MAX_NR_TZONE 16 +#define MAX_NR_CDEV 32 +#define MAX_NR_TRIP 16 +#define MAX_NR_CDEV_TRIP 12 /* number of cooling devices that can bind + * to a thermal zone trip. + */ +#define MAX_TEMP_KC 140000 +/* starting char position to draw sensor data, such as tz names + * trip point list, etc. + */ +#define DATA_LEFT_ALIGN 10 +#define NR_LINES_TZDATA 1 +#define TMON_LOG_FILE "/var/tmp/tmon.log" + +extern unsigned long ticktime; +extern double time_elapsed; +extern unsigned long target_temp_user; +extern int dialogue_on; +extern char ctrl_cdev[]; +extern pthread_mutex_t input_lock; +extern int tmon_exit; +extern int target_thermal_zone; +/* use fixed size record to simplify data processing and transfer + * TBD: more info to be added, e.g. programmable trip point data. +*/ +struct thermal_data_record { + struct timeval tv; + unsigned long temp[MAX_NR_TZONE]; + double pid_out_pct; +}; + +struct cdev_info { + char type[64]; + int instance; + unsigned long max_state; + unsigned long cur_state; + unsigned long flag; +}; + +enum trip_type { + THERMAL_TRIP_CRITICAL, + THERMAL_TRIP_HOT, + THERMAL_TRIP_PASSIVE, + THERMAL_TRIP_ACTIVE, + NR_THERMAL_TRIP_TYPE, +}; + +struct trip_point { + enum trip_type type; + unsigned long temp; + unsigned long hysteresis; + int attribute; /* programmability etc. */ +}; + +/* thermal zone configuration information, binding with cooling devices could + * change at runtime. + */ +struct tz_info { + char type[256]; /* e.g. acpitz */ + int instance; + int passive; /* active zone has passive node to force passive mode */ + int nr_cdev; /* number of cooling device binded */ + int nr_trip_pts; + struct trip_point tp[MAX_NR_TRIP]; + unsigned long cdev_binding; /* bitmap for attached cdevs */ + /* cdev bind trip points, allow one cdev bind to multiple trips */ + unsigned long trip_binding[MAX_NR_CDEV]; +}; + +struct tmon_platform_data { + int nr_tz_sensor; + int nr_cooling_dev; + /* keep track of instance ids since there might be gaps */ + int max_tz_instance; + int max_cdev_instance; + struct tz_info *tzi; + struct cdev_info *cdi; +}; + +struct control_ops { + void (*set_ratio)(unsigned long ratio); + unsigned long (*get_ratio)(unsigned long ratio); + +}; + +enum cdev_types { + CDEV_TYPE_PROC, + CDEV_TYPE_FAN, + CDEV_TYPE_MEM, + CDEV_TYPE_NR, +}; + +/* REVISIT: the idea is to group sensors if possible, e.g. on intel mid + * we have "skin0", "skin1", "sys", "msicdie" + * on DPTF enabled systems, we might have PCH, TSKN, TAMB, etc. + */ +enum tzone_types { + TZONE_TYPE_ACPI, + TZONE_TYPE_PCH, + TZONE_TYPE_NR, +}; + +/* limit the output of PID controller adjustment */ +#define LIMIT_HIGH (95) +#define LIMIT_LOW (2) + +struct pid_params { + double kp; /* Controller gain from Dialog Box */ + double ki; /* Time-constant for I action from Dialog Box */ + double kd; /* Time-constant for D action from Dialog Box */ + double ts; + double k_lpf; + + double t_target; + double y_k; +}; + +extern int init_thermal_controller(void); +extern void controller_handler(const double xk, double *yk); + +extern struct tmon_platform_data ptdata; +extern struct pid_params p_param; + +extern FILE *tmon_log; +extern int cur_thermal_record; /* index to the trec array */ +extern struct thermal_data_record trec[]; +extern const char *trip_type_name[]; +extern unsigned long no_control; + +extern void initialize_curses(void); +extern void show_controller_stats(char *line); +extern void show_title_bar(void); +extern void setup_windows(void); +extern void disable_tui(void); +extern void show_sensors_w(void); +extern void show_data_w(void); +extern void write_status_bar(int x, char *line); +extern void show_control_w(); + +extern void show_cooling_device(void); +extern void show_dialogue(void); +extern int update_thermal_data(void); + +extern int probe_thermal_sysfs(void); +extern void free_thermal_data(void); +extern void resize_handler(int sig); +extern void set_ctrl_state(unsigned long state); +extern void get_ctrl_state(unsigned long *state); +extern void *handle_tui_events(void *arg); +extern int sysfs_set_ulong(char *path, char *filename, unsigned long val); +extern int zone_instance_to_index(int zone_inst); +extern void close_windows(void); + +#define PT_COLOR_DEFAULT 1 +#define PT_COLOR_HEADER_BAR 2 +#define PT_COLOR_ERROR 3 +#define PT_COLOR_RED 4 +#define PT_COLOR_YELLOW 5 +#define PT_COLOR_GREEN 6 +#define PT_COLOR_BRIGHT 7 +#define PT_COLOR_BLUE 8 + +/* each thermal zone uses 12 chars, 8 for name, 2 for instance, 2 space + * also used to list trip points in forms of AAAC, which represents + * A: Active + * C: Critical + */ +#define TZONE_RECORD_SIZE 12 +#define TZ_LEFT_ALIGN 32 +#define CDEV_NAME_SIZE 20 +#define CDEV_FLAG_IN_CONTROL (1 << 0) + +/* dialogue box starts */ +#define DIAG_X 48 +#define DIAG_Y 8 +#define THERMAL_SYSFS "/sys/class/thermal" +#define CDEV "cooling_device" +#define TZONE "thermal_zone" +#define TDATA_LEFT 16 +#endif /* TMON_H */ diff --git a/tools/thermal/tmon/tui.c b/tools/thermal/tmon/tui.c new file mode 100644 index 0000000..89f8ef0 --- /dev/null +++ b/tools/thermal/tmon/tui.c @@ -0,0 +1,638 @@ +/* + * tui.c ncurses text user interface for TMON program + * + * Copyright (C) 2013 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author: Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <ncurses.h> +#include <time.h> +#include <syslog.h> +#include <panel.h> +#include <pthread.h> +#include <signal.h> + +#include "tmon.h" + +static PANEL *data_panel; +static PANEL *dialogue_panel; +static PANEL *top; + +static WINDOW *title_bar_window; +static WINDOW *tz_sensor_window; +static WINDOW *cooling_device_window; +static WINDOW *control_window; +static WINDOW *status_bar_window; +static WINDOW *thermal_data_window; +static WINDOW *dialogue_window; + +char status_bar_slots[10][40]; +static void draw_hbar(WINDOW *win, int y, int start, int len, + unsigned long pattern, bool end); + +static int maxx, maxy; +static int maxwidth = 200; + +#define TITLE_BAR_HIGHT 1 +#define SENSOR_WIN_HIGHT 4 /* one row for tz name, one for trip points */ + + +/* daemon mode flag (set by startup parameter -d) */ +static int tui_disabled; + +static void close_panel(PANEL *p) +{ + if (p) { + del_panel(p); + p = NULL; + } +} + +static void close_window(WINDOW *win) +{ + if (win) { + delwin(win); + win = NULL; + } +} + +void close_windows(void) +{ + if (tui_disabled) + return; + /* must delete panels before their attached windows */ + if (dialogue_window) + close_panel(dialogue_panel); + if (cooling_device_window) + close_panel(data_panel); + + close_window(title_bar_window); + close_window(tz_sensor_window); + close_window(status_bar_window); + close_window(cooling_device_window); + close_window(control_window); + close_window(thermal_data_window); + close_window(dialogue_window); + +} + +void write_status_bar(int x, char *line) +{ + mvwprintw(status_bar_window, 0, x, "%s", line); + wrefresh(status_bar_window); +} + +void setup_windows(void) +{ + int y_begin = 1; + + if (tui_disabled) + return; + + getmaxyx(stdscr, maxy, maxx); + resizeterm(maxy, maxx); + + title_bar_window = subwin(stdscr, TITLE_BAR_HIGHT, maxx, 0, 0); + y_begin += TITLE_BAR_HIGHT; + + tz_sensor_window = subwin(stdscr, SENSOR_WIN_HIGHT, maxx, y_begin, 0); + y_begin += SENSOR_WIN_HIGHT; + + cooling_device_window = subwin(stdscr, ptdata.nr_cooling_dev + 3, maxx, + y_begin, 0); + y_begin += ptdata.nr_cooling_dev + 3; /* 2 lines for border */ + /* two lines to show borders, one line per tz show trip point position + * and value. + * dialogue window is a pop-up, when needed it lays on top of cdev win + */ + + dialogue_window = subwin(stdscr, ptdata.nr_cooling_dev+5, maxx-50, + DIAG_Y, DIAG_X); + + thermal_data_window = subwin(stdscr, ptdata.nr_tz_sensor * + NR_LINES_TZDATA + 3, maxx, y_begin, 0); + y_begin += ptdata.nr_tz_sensor * NR_LINES_TZDATA + 3; + control_window = subwin(stdscr, 4, maxx, y_begin, 0); + + scrollok(cooling_device_window, TRUE); + maxwidth = maxx - 18; + status_bar_window = subwin(stdscr, 1, maxx, maxy-1, 0); + + strcpy(status_bar_slots[0], " Ctrl-c - Quit "); + strcpy(status_bar_slots[1], " TAB - Tuning "); + wmove(status_bar_window, 1, 30); + + /* prepare panels for dialogue, if panel already created then we must + * be doing resizing, so just replace windows with new ones, old ones + * should have been deleted by close_window + */ + data_panel = new_panel(cooling_device_window); + if (!data_panel) + syslog(LOG_DEBUG, "No data panel\n"); + else { + if (dialogue_window) { + dialogue_panel = new_panel(dialogue_window); + if (!dialogue_panel) + syslog(LOG_DEBUG, "No dialogue panel\n"); + else { + /* Set up the user pointer to the next panel*/ + set_panel_userptr(data_panel, dialogue_panel); + set_panel_userptr(dialogue_panel, data_panel); + top = data_panel; + } + } else + syslog(LOG_INFO, "no dialogue win, term too small\n"); + } + doupdate(); + werase(stdscr); + refresh(); +} + +void resize_handler(int sig) +{ + /* start over when term gets resized, but first we clean up */ + close_windows(); + endwin(); + refresh(); + clear(); + getmaxyx(stdscr, maxy, maxx); /* get the new screen size */ + setup_windows(); + /* rate limit */ + sleep(1); + syslog(LOG_DEBUG, "SIG %d, term resized to %d x %d\n", + sig, maxy, maxx); + signal(SIGWINCH, resize_handler); +} + +const char cdev_title[] = " COOLING DEVICES "; +void show_cooling_device(void) +{ + int i, j, x, y = 0; + + if (tui_disabled || !cooling_device_window) + return; + + werase(cooling_device_window); + wattron(cooling_device_window, A_BOLD); + mvwprintw(cooling_device_window, 1, 1, + "ID Cooling Dev Cur Max Thermal Zone Binding"); + wattroff(cooling_device_window, A_BOLD); + for (j = 0; j < ptdata.nr_cooling_dev; j++) { + /* draw cooling device list on the left in the order of + * cooling device instances. skip unused idr. + */ + mvwprintw(cooling_device_window, j + 2, 1, + "%02d %12.12s%6d %6d", + ptdata.cdi[j].instance, + ptdata.cdi[j].type, + ptdata.cdi[j].cur_state, + ptdata.cdi[j].max_state); + } + + /* show cdev binding, y is the global cooling device instance */ + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + int tz_inst = ptdata.tzi[i].instance; + for (j = 0; j < ptdata.nr_cooling_dev; j++) { + int cdev_inst; + y = j; + x = tz_inst * TZONE_RECORD_SIZE + TZ_LEFT_ALIGN; + + draw_hbar(cooling_device_window, y+2, x, + TZONE_RECORD_SIZE-1, ACS_VLINE, false); + + /* draw a column of spaces to separate thermal zones */ + mvwprintw(cooling_device_window, y+2, x-1, " "); + if (ptdata.tzi[i].cdev_binding) { + cdev_inst = ptdata.cdi[j].instance; + unsigned long trip_binding = + ptdata.tzi[i].trip_binding[cdev_inst]; + int k = 0; /* per zone trip point id that + * binded to this cdev, one to + * many possible based on the + * binding bitmask. + */ + syslog(LOG_DEBUG, + "bind tz%d cdev%d tp%lx %d cdev%lx\n", + i, j, trip_binding, y, + ptdata.tzi[i].cdev_binding); + /* draw each trip binding for the cdev */ + while (trip_binding >>= 1) { + k++; + if (!(trip_binding & 1)) + continue; + /* draw '*' to show binding */ + mvwprintw(cooling_device_window, + y + 2, + x + ptdata.tzi[i].nr_trip_pts - + k - 1, "*"); + } + } + } + } + /* draw border after data so that border will not be messed up + * even there is not enough space for all the data to be shown + */ + wborder(cooling_device_window, 0, 0, 0, 0, 0, 0, 0, 0); + wattron(cooling_device_window, A_BOLD); + mvwprintw(cooling_device_window, 0, maxx/2 - sizeof(cdev_title), + cdev_title); + wattroff(cooling_device_window, A_BOLD); + + wrefresh(cooling_device_window); +} + +const char DIAG_TITLE[] = "[ TUNABLES ]"; +#define DIAG_DEV_ROWS 5 +void show_dialogue(void) +{ + int j, x = 0, y = 0; + WINDOW *w = dialogue_window; + + if (tui_disabled || !w) + return; + + werase(w); + box(w, 0, 0); + mvwprintw(w, 0, maxx/4, DIAG_TITLE); + /* list all the available tunables */ + for (j = 0; j <= ptdata.nr_cooling_dev; j++) { + y = j % DIAG_DEV_ROWS; + if (y == 0 && j != 0) + x += 20; + if (j == ptdata.nr_cooling_dev) + /* save last choice for target temp */ + mvwprintw(w, y+1, x+1, "%C-%.12s", 'A'+j, "Set Temp"); + else + mvwprintw(w, y+1, x+1, "%C-%.10s-%2d", 'A'+j, + ptdata.cdi[j].type, ptdata.cdi[j].instance); + } + wattron(w, A_BOLD); + mvwprintw(w, DIAG_DEV_ROWS+1, 1, "Enter Choice [A-Z]?"); + wattroff(w, A_BOLD); + /* y size of dialogue win is nr cdev + 5, so print legend + * at the bottom line + */ + mvwprintw(w, ptdata.nr_cooling_dev+3, 1, + "Legend: A=Active, P=Passive, C=Critical"); + + wrefresh(dialogue_window); +} + +void write_dialogue_win(char *buf, int y, int x) +{ + WINDOW *w = dialogue_window; + + mvwprintw(w, y, x, "%s", buf); +} + +const char control_title[] = " CONTROLS "; +void show_control_w(void) +{ + unsigned long state; + + get_ctrl_state(&state); + + if (tui_disabled || !control_window) + return; + + werase(control_window); + mvwprintw(control_window, 1, 1, + "PID gain: kp=%2.2f ki=%2.2f kd=%2.2f Output %2.2f", + p_param.kp, p_param.ki, p_param.kd, p_param.y_k); + + mvwprintw(control_window, 2, 1, + "Target Temp: %2.1fC, Zone: %d, Control Device: %.12s", + p_param.t_target, target_thermal_zone, ctrl_cdev); + + /* draw border last such that everything is within boundary */ + wborder(control_window, 0, 0, 0, 0, 0, 0, 0, 0); + wattron(control_window, A_BOLD); + mvwprintw(control_window, 0, maxx/2 - sizeof(control_title), + control_title); + wattroff(control_window, A_BOLD); + + wrefresh(control_window); +} + +void initialize_curses(void) +{ + if (tui_disabled) + return; + + initscr(); + start_color(); + keypad(stdscr, TRUE); /* enable keyboard mapping */ + nonl(); /* tell curses not to do NL->CR/NL on output */ + cbreak(); /* take input chars one at a time */ + noecho(); /* dont echo input */ + curs_set(0); /* turn off cursor */ + use_default_colors(); + + init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK); + init_pair(PT_COLOR_HEADER_BAR, COLOR_BLACK, COLOR_WHITE); + init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED); + init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED); + init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW); + init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN); + init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE); + init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK); + +} + +void show_title_bar(void) +{ + int i; + int x = 0; + + if (tui_disabled || !title_bar_window) + return; + + wattrset(title_bar_window, COLOR_PAIR(PT_COLOR_HEADER_BAR)); + wbkgd(title_bar_window, COLOR_PAIR(PT_COLOR_HEADER_BAR)); + werase(title_bar_window); + + mvwprintw(title_bar_window, 0, 0, + " TMON v%s", VERSION); + + wrefresh(title_bar_window); + + werase(status_bar_window); + + for (i = 0; i < 10; i++) { + if (strlen(status_bar_slots[i]) == 0) + continue; + wattron(status_bar_window, A_REVERSE); + mvwprintw(status_bar_window, 0, x, "%s", status_bar_slots[i]); + wattroff(status_bar_window, A_REVERSE); + x += strlen(status_bar_slots[i]) + 1; + } + wrefresh(status_bar_window); +} + +static void handle_input_val(int ch) +{ + char buf[32]; + int val; + char path[256]; + WINDOW *w = dialogue_window; + + echo(); + keypad(w, TRUE); + wgetnstr(w, buf, 31); + val = atoi(buf); + + if (ch == ptdata.nr_cooling_dev) { + snprintf(buf, 31, "Invalid Temp %d! %d-%d", val, + MIN_CTRL_TEMP, MAX_CTRL_TEMP); + if (val < MIN_CTRL_TEMP || val > MAX_CTRL_TEMP) + write_status_bar(40, buf); + else { + p_param.t_target = val; + snprintf(buf, 31, "Set New Target Temp %d", val); + write_status_bar(40, buf); + } + } else { + snprintf(path, 256, "%s/%s%d", THERMAL_SYSFS, + CDEV, ptdata.cdi[ch].instance); + sysfs_set_ulong(path, "cur_state", val); + } + noecho(); + dialogue_on = 0; + show_data_w(); + show_control_w(); + + top = (PANEL *)panel_userptr(top); + top_panel(top); +} + +static void handle_input_choice(int ch) +{ + char buf[48]; + int base = 0; + int cdev_id = 0; + + if ((ch >= 'A' && ch <= 'A' + ptdata.nr_cooling_dev) || + (ch >= 'a' && ch <= 'a' + ptdata.nr_cooling_dev)) { + base = (ch < 'a') ? 'A' : 'a'; + cdev_id = ch - base; + if (ptdata.nr_cooling_dev == cdev_id) + snprintf(buf, sizeof(buf), "New Target Temp:"); + else + snprintf(buf, sizeof(buf), "New Value for %.10s-%2d: ", + ptdata.cdi[cdev_id].type, + ptdata.cdi[cdev_id].instance); + write_dialogue_win(buf, DIAG_DEV_ROWS+2, 2); + handle_input_val(cdev_id); + } else { + snprintf(buf, sizeof(buf), "Invalid selection %d", ch); + write_dialogue_win(buf, 8, 2); + } +} + +void *handle_tui_events(void *arg) +{ + int ch; + + keypad(cooling_device_window, TRUE); + while ((ch = wgetch(cooling_device_window)) != EOF) { + if (tmon_exit) + break; + /* when term size is too small, no dialogue panels are set. + * we need to filter out such cases. + */ + if (!data_panel || !dialogue_panel || + !cooling_device_window || + !dialogue_window) { + + continue; + } + pthread_mutex_lock(&input_lock); + if (dialogue_on) { + handle_input_choice(ch); + /* top panel filter */ + if (ch == 'q' || ch == 'Q') + ch = 0; + } + switch (ch) { + case KEY_LEFT: + box(cooling_device_window, 10, 0); + break; + case 9: /* TAB */ + top = (PANEL *)panel_userptr(top); + top_panel(top); + if (top == dialogue_panel) { + dialogue_on = 1; + show_dialogue(); + } else { + dialogue_on = 0; + /* force refresh */ + show_data_w(); + show_control_w(); + } + break; + case 'q': + case 'Q': + tmon_exit = 1; + break; + } + update_panels(); + doupdate(); + pthread_mutex_unlock(&input_lock); + } + + if (arg) + *(int *)arg = 0; /* make gcc happy */ + + return NULL; +} + +/* draw a horizontal bar in given pattern */ +static void draw_hbar(WINDOW *win, int y, int start, int len, unsigned long ptn, + bool end) +{ + mvwaddch(win, y, start, ptn); + whline(win, ptn, len); + if (end) + mvwaddch(win, y, MAX_DISP_TEMP+TDATA_LEFT, ']'); +} + +static char trip_type_to_char(int type) +{ + switch (type) { + case THERMAL_TRIP_CRITICAL: return 'C'; + case THERMAL_TRIP_HOT: return 'H'; + case THERMAL_TRIP_PASSIVE: return 'P'; + case THERMAL_TRIP_ACTIVE: return 'A'; + default: + return '?'; + } +} + +/* fill a string with trip point type and value in one line + * e.g. P(56) C(106) + * maintain the distance one degree per char + */ +static void draw_tp_line(int tz, int y) +{ + int j; + int x; + + for (j = 0; j < ptdata.tzi[tz].nr_trip_pts; j++) { + x = ptdata.tzi[tz].tp[j].temp / 1000; + mvwprintw(thermal_data_window, y + 0, x + TDATA_LEFT, + "%c%d", trip_type_to_char(ptdata.tzi[tz].tp[j].type), + x); + syslog(LOG_INFO, "%s:tz %d tp %d temp = %lu\n", __func__, + tz, j, ptdata.tzi[tz].tp[j].temp); + } +} + +const char data_win_title[] = " THERMAL DATA "; +void show_data_w(void) +{ + int i; + + + if (tui_disabled || !thermal_data_window) + return; + + werase(thermal_data_window); + wattron(thermal_data_window, A_BOLD); + mvwprintw(thermal_data_window, 0, maxx/2 - sizeof(data_win_title), + data_win_title); + wattroff(thermal_data_window, A_BOLD); + /* draw a line as ruler */ + for (i = 10; i < MAX_DISP_TEMP; i += 10) + mvwprintw(thermal_data_window, 1, i+TDATA_LEFT, "%2d", i); + + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + int temp = trec[cur_thermal_record].temp[i] / 1000; + int y = 0; + + y = i * NR_LINES_TZDATA + 2; + /* y at tz temp data line */ + mvwprintw(thermal_data_window, y, 1, "%6.6s%2d:[%3d][", + ptdata.tzi[i].type, + ptdata.tzi[i].instance, temp); + draw_hbar(thermal_data_window, y, TDATA_LEFT, temp, ACS_RARROW, + true); + draw_tp_line(i, y); + } + wborder(thermal_data_window, 0, 0, 0, 0, 0, 0, 0, 0); + wrefresh(thermal_data_window); +} + +const char tz_title[] = "THERMAL ZONES(SENSORS)"; + +void show_sensors_w(void) +{ + int i, j; + char buffer[512]; + + if (tui_disabled || !tz_sensor_window) + return; + + werase(tz_sensor_window); + + memset(buffer, 0, sizeof(buffer)); + wattron(tz_sensor_window, A_BOLD); + mvwprintw(tz_sensor_window, 1, 1, "Thermal Zones:"); + wattroff(tz_sensor_window, A_BOLD); + + mvwprintw(tz_sensor_window, 1, TZ_LEFT_ALIGN, "%s", buffer); + /* fill trip points for each tzone */ + wattron(tz_sensor_window, A_BOLD); + mvwprintw(tz_sensor_window, 2, 1, "Trip Points:"); + wattroff(tz_sensor_window, A_BOLD); + + /* draw trip point from low to high for each tz */ + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + int inst = ptdata.tzi[i].instance; + + mvwprintw(tz_sensor_window, 1, + TZ_LEFT_ALIGN+TZONE_RECORD_SIZE * inst, "%.9s%02d", + ptdata.tzi[i].type, ptdata.tzi[i].instance); + for (j = ptdata.tzi[i].nr_trip_pts - 1; j >= 0; j--) { + /* loop through all trip points */ + char type; + int tp_pos; + /* reverse the order here since trips are sorted + * in ascending order in terms of temperature. + */ + tp_pos = ptdata.tzi[i].nr_trip_pts - j - 1; + + type = trip_type_to_char(ptdata.tzi[i].tp[j].type); + mvwaddch(tz_sensor_window, 2, + inst * TZONE_RECORD_SIZE + TZ_LEFT_ALIGN + + tp_pos, type); + syslog(LOG_DEBUG, "draw tz %d tp %d ch:%c\n", + inst, j, type); + } + } + wborder(tz_sensor_window, 0, 0, 0, 0, 0, 0, 0, 0); + wattron(tz_sensor_window, A_BOLD); + mvwprintw(tz_sensor_window, 0, maxx/2 - sizeof(tz_title), tz_title); + wattroff(tz_sensor_window, A_BOLD); + wrefresh(tz_sensor_window); +} + +void disable_tui(void) +{ + tui_disabled = 1; +} diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c index da7a195..bdb71a2 100644 --- a/tools/virtio/virtio_test.c +++ b/tools/virtio/virtio_test.c @@ -41,13 +41,14 @@ struct vdev_info { struct vhost_memory *mem; }; -void vq_notify(struct virtqueue *vq) +bool vq_notify(struct virtqueue *vq) { struct vq_info *info = vq->priv; unsigned long long v = 1; int r; r = write(info->kick, &v, sizeof v); assert(r == sizeof v); + return true; } void vq_callback(struct virtqueue *vq) @@ -171,7 +172,8 @@ static void run_test(struct vdev_info *dev, struct vq_info *vq, GFP_ATOMIC); if (likely(r == 0)) { ++started; - virtqueue_kick(vq->vq); + if (unlikely(!virtqueue_kick(vq->vq)) + r = -1; } } else r = -1; diff --git a/tools/virtio/vringh_test.c b/tools/virtio/vringh_test.c index d053ea4..14a4f4c 100644 --- a/tools/virtio/vringh_test.c +++ b/tools/virtio/vringh_test.c @@ -22,7 +22,7 @@ static u64 user_addr_offset; #define RINGSIZE 256 #define ALIGN 4096 -static void never_notify_host(struct virtqueue *vq) +static bool never_notify_host(struct virtqueue *vq) { abort(); } @@ -65,17 +65,22 @@ struct guest_virtio_device { unsigned long notifies; }; -static void parallel_notify_host(struct virtqueue *vq) +static bool parallel_notify_host(struct virtqueue *vq) { + int rc; struct guest_virtio_device *gvdev; gvdev = container_of(vq->vdev, struct guest_virtio_device, vdev); - write(gvdev->to_host_fd, "", 1); + rc = write(gvdev->to_host_fd, "", 1); + if (rc < 0) + return false; gvdev->notifies++; + return true; } -static void no_notify_host(struct virtqueue *vq) +static bool no_notify_host(struct virtqueue *vq) { + return true; } #define NUM_XFERS (10000000) |