treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / perf / ui / browsers / res_sample.c
blob7cb2d6678039749813c405ac1f515c7796ce8b53
1 // SPDX-License-Identifier: GPL-2.0
2 /* Display a menu with individual samples to browse with perf script */
3 #include "hist.h"
4 #include "evsel.h"
5 #include "hists.h"
6 #include "sort.h"
7 #include "config.h"
8 #include "time-utils.h"
9 #include "../util.h"
10 #include "../../util/util.h" // perf_exe()
11 #include "../../perf.h"
12 #include <stdlib.h>
13 #include <string.h>
14 #include <linux/time64.h>
15 #include <linux/zalloc.h>
17 static u64 context_len = 10 * NSEC_PER_MSEC;
19 static int res_sample_config(const char *var, const char *value, void *data __maybe_unused)
21 if (!strcmp(var, "samples.context"))
22 return perf_config_u64(&context_len, var, value);
23 return 0;
26 void res_sample_init(void)
28 perf_config(res_sample_config, NULL);
31 int res_sample_browse(struct res_sample *res_samples, int num_res,
32 struct evsel *evsel, enum rstype rstype)
34 char **names;
35 int i, n;
36 int choice;
37 char *cmd;
38 char pbuf[256], tidbuf[32], cpubuf[32];
39 const char *perf = perf_exe(pbuf, sizeof pbuf);
40 char trange[128], tsample[64];
41 struct res_sample *r;
42 char extra_format[256];
44 names = calloc(num_res, sizeof(char *));
45 if (!names)
46 return -1;
47 for (i = 0; i < num_res; i++) {
48 char tbuf[64];
50 timestamp__scnprintf_nsec(res_samples[i].time, tbuf, sizeof tbuf);
51 if (asprintf(&names[i], "%s: CPU %d tid %d", tbuf,
52 res_samples[i].cpu, res_samples[i].tid) < 0) {
53 while (--i >= 0)
54 zfree(&names[i]);
55 free(names);
56 return -1;
59 choice = ui__popup_menu(num_res, names, NULL);
60 for (i = 0; i < num_res; i++)
61 zfree(&names[i]);
62 free(names);
64 if (choice < 0 || choice >= num_res)
65 return -1;
66 r = &res_samples[choice];
68 n = timestamp__scnprintf_nsec(r->time - context_len, trange, sizeof trange);
69 trange[n++] = ',';
70 timestamp__scnprintf_nsec(r->time + context_len, trange + n, sizeof trange - n);
72 timestamp__scnprintf_nsec(r->time, tsample, sizeof tsample);
74 attr_to_script(extra_format, &evsel->core.attr);
76 if (asprintf(&cmd, "%s script %s%s --time %s %s%s %s%s --ns %s %s %s %s %s | less +/%s",
77 perf,
78 input_name ? "-i " : "",
79 input_name ? input_name : "",
80 trange,
81 r->cpu >= 0 ? "--cpu " : "",
82 r->cpu >= 0 ? (sprintf(cpubuf, "%d", r->cpu), cpubuf) : "",
83 r->tid ? "--tid " : "",
84 r->tid ? (sprintf(tidbuf, "%d", r->tid), tidbuf) : "",
85 extra_format,
86 rstype == A_ASM ? "-F +insn --xed" :
87 rstype == A_SOURCE ? "-F +srcline,+srccode" : "",
88 symbol_conf.inline_name ? "--inline" : "",
89 "--show-lost-events ",
90 r->tid ? "--show-switch-events --show-task-events " : "",
91 tsample) < 0)
92 return -1;
93 run_script(cmd);
94 free(cmd);
95 return 0;