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