1 // SPDX-License-Identifier: GPL-2.0
2 /* Display a menu with individual samples to browse with perf script */
8 #include "time-utils.h"
9 #include <linux/time64.h>
10 #include <linux/zalloc.h>
12 static u64 context_len = 10 * NSEC_PER_MSEC;
14 static int res_sample_config(const char *var, const char *value, void *data __maybe_unused)
16 if (!strcmp(var, "samples.context"))
17 return perf_config_u64(&context_len, var, value);
21 void res_sample_init(void)
23 perf_config(res_sample_config, NULL);
26 int res_sample_browse(struct res_sample *res_samples, int num_res,
27 struct perf_evsel *evsel, enum rstype rstype)
33 char pbuf[256], tidbuf[32], cpubuf[32];
34 const char *perf = perf_exe(pbuf, sizeof pbuf);
35 char trange[128], tsample[64];
37 char extra_format[256];
39 names = calloc(num_res, sizeof(char *));
42 for (i = 0; i < num_res; i++) {
45 timestamp__scnprintf_nsec(res_samples[i].time, tbuf, sizeof tbuf);
46 if (asprintf(&names[i], "%s: CPU %d tid %d", tbuf,
47 res_samples[i].cpu, res_samples[i].tid) < 0) {
54 choice = ui__popup_menu(num_res, names);
55 for (i = 0; i < num_res; i++)
59 if (choice < 0 || choice >= num_res)
61 r = &res_samples[choice];
63 n = timestamp__scnprintf_nsec(r->time - context_len, trange, sizeof trange);
65 timestamp__scnprintf_nsec(r->time + context_len, trange + n, sizeof trange - n);
67 timestamp__scnprintf_nsec(r->time, tsample, sizeof tsample);
69 attr_to_script(extra_format, &evsel->attr);
71 if (asprintf(&cmd, "%s script %s%s --time %s %s%s %s%s --ns %s %s %s %s %s | less +/%s",
73 input_name ? "-i " : "",
74 input_name ? input_name : "",
76 r->cpu >= 0 ? "--cpu " : "",
77 r->cpu >= 0 ? (sprintf(cpubuf, "%d", r->cpu), cpubuf) : "",
78 r->tid ? "--tid " : "",
79 r->tid ? (sprintf(tidbuf, "%d", r->tid), tidbuf) : "",
81 rstype == A_ASM ? "-F +insn --xed" :
82 rstype == A_SOURCE ? "-F +srcline,+srccode" : "",
83 symbol_conf.inline_name ? "--inline" : "",
84 "--show-lost-events ",
85 r->tid ? "--show-switch-events --show-task-events " : "",