cregit-Linux how code gets into the kernel

Release 4.10 tools/perf/builtin-script.c

Directory: tools/perf
#include "builtin.h"

#include "perf.h"
#include "util/cache.h"
#include "util/debug.h"
#include <subcmd/exec-cmd.h>
#include "util/header.h"
#include <subcmd/parse-options.h>
#include "util/perf_regs.h"
#include "util/session.h"
#include "util/tool.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/trace-event.h"
#include "util/util.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/sort.h"
#include "util/data.h"
#include "util/auxtrace.h"
#include "util/cpumap.h"
#include "util/thread_map.h"
#include "util/stat.h"
#include "util/thread-stack.h"
#include "util/time-utils.h"
#include <linux/bitmap.h>
#include <linux/stringify.h>
#include <linux/time64.h>
#include "asm/bug.h"
#include "util/mem-events.h"


static char const		*script_name;

static char const		*generate_script_lang;

static bool			debug_mode;

static u64			last_timestamp;

static u64			nr_unordered;

static bool			no_callchain;

static bool			latency_format;

static bool			system_wide;

static bool			print_flags;

static bool			nanosecs;

static const char		*cpu_list;
static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);

static struct perf_stat_config	stat_config;


unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;


enum perf_output_field {
	
PERF_OUTPUT_COMM            = 1U << 0,
	
PERF_OUTPUT_TID             = 1U << 1,
	
PERF_OUTPUT_PID             = 1U << 2,
	
PERF_OUTPUT_TIME            = 1U << 3,
	
PERF_OUTPUT_CPU             = 1U << 4,
	
PERF_OUTPUT_EVNAME          = 1U << 5,
	
PERF_OUTPUT_TRACE           = 1U << 6,
	
PERF_OUTPUT_IP              = 1U << 7,
	
PERF_OUTPUT_SYM             = 1U << 8,
	
PERF_OUTPUT_DSO             = 1U << 9,
	
PERF_OUTPUT_ADDR            = 1U << 10,
	
PERF_OUTPUT_SYMOFFSET       = 1U << 11,
	
PERF_OUTPUT_SRCLINE         = 1U << 12,
	
PERF_OUTPUT_PERIOD          = 1U << 13,
	
PERF_OUTPUT_IREGS	    = 1U << 14,
	
PERF_OUTPUT_BRSTACK	    = 1U << 15,
	
PERF_OUTPUT_BRSTACKSYM	    = 1U << 16,
	
PERF_OUTPUT_DATA_SRC	    = 1U << 17,
	
PERF_OUTPUT_WEIGHT	    = 1U << 18,
	
PERF_OUTPUT_BPF_OUTPUT	    = 1U << 19,
	
PERF_OUTPUT_CALLINDENT	    = 1U << 20,
	
PERF_OUTPUT_INSN	    = 1U << 21,
	
PERF_OUTPUT_INSNLEN	    = 1U << 22,
};


struct output_option {
	
const char *str;
	
enum perf_output_field field;
} 
all_output_options[] = {
	{.str = "comm",  .field = PERF_OUTPUT_COMM},
	{.str = "tid",   .field = PERF_OUTPUT_TID},
	{.str = "pid",   .field = PERF_OUTPUT_PID},
	{.str = "time",  .field = PERF_OUTPUT_TIME},
	{.str = "cpu",   .field = PERF_OUTPUT_CPU},
	{.str = "event", .field = PERF_OUTPUT_EVNAME},
	{.str = "trace", .field = PERF_OUTPUT_TRACE},
	{.str = "ip",    .field = PERF_OUTPUT_IP},
	{.str = "sym",   .field = PERF_OUTPUT_SYM},
	{.str = "dso",   .field = PERF_OUTPUT_DSO},
	{.str = "addr",  .field = PERF_OUTPUT_ADDR},
	{.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
	{.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
	{.str = "period", .field = PERF_OUTPUT_PERIOD},
	{.str = "iregs", .field = PERF_OUTPUT_IREGS},
	{.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
	{.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
	{.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
	{.str = "weight",   .field = PERF_OUTPUT_WEIGHT},
	{.str = "bpf-output",   .field = PERF_OUTPUT_BPF_OUTPUT},
	{.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
	{.str = "insn", .field = PERF_OUTPUT_INSN},
	{.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
};

/* default set to maintain compatibility with current format */
static struct {
	
bool user_set;
	
bool wildcard_set;
	
unsigned int print_ip_opts;
	
u64 fields;
	
u64 invalid_fields;
} 
output[PERF_TYPE_MAX] = {

	[PERF_TYPE_HARDWARE] = {
		.user_set = false,

		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
			      PERF_OUTPUT_PERIOD,

		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
        },

	[PERF_TYPE_SOFTWARE] = {
		.user_set = false,

		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
			      PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,

		.invalid_fields = PERF_OUTPUT_TRACE,
        },

	[PERF_TYPE_TRACEPOINT] = {
		.user_set = false,

		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
				  PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
				  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
	},

	[PERF_TYPE_RAW] = {
		.user_set = false,

		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
			      PERF_OUTPUT_PERIOD |  PERF_OUTPUT_ADDR |
			      PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,

		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
        },

	[PERF_TYPE_BREAKPOINT] = {
		.user_set = false,

		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
			      PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
			      PERF_OUTPUT_PERIOD,

		.invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
        },
};


static bool output_set_by_user(void) { int j; for (j = 0; j < PERF_TYPE_MAX; ++j) { if (output[j].user_set) return true; } return false; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern41100.00%2100.00%
Total41100.00%2100.00%


static const char *output_field2str(enum perf_output_field field) { int i, imax = ARRAY_SIZE(all_output_options); const char *str = ""; for (i = 0; i < imax; ++i) { if (all_output_options[i].field == field) { str = all_output_options[i].str; break; } } return str; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern70100.00%1100.00%
Total70100.00%1100.00%

#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
static int perf_evsel__do_check_stype(struct perf_evsel *evsel, u64 sample_type, const char *sample_msg, enum perf_output_field field, bool allow_user_set) { struct perf_event_attr *attr = &evsel->attr; int type = attr->type; const char *evname; if (attr->sample_type & sample_type) return 0; if (output[type].user_set) { if (allow_user_set) return 0; evname = perf_evsel__name(evsel); pr_err("Samples for '%s' event do not have %s attribute set. " "Cannot print '%s' field.\n", evname, sample_msg, output_field2str(field)); return -1; } /* user did not ask for it explicitly so remove from the default list */ output[type].fields &= ~field; evname = perf_evsel__name(evsel); pr_debug("Samples for '%s' event do not have %s attribute set. " "Skipping '%s' field.\n", evname, sample_msg, output_field2str(field)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern11280.58%133.33%
arnaldo carvalho de meloarnaldo carvalho de melo1611.51%133.33%
adrian hunteradrian hunter117.91%133.33%
Total139100.00%3100.00%


static int perf_evsel__check_stype(struct perf_evsel *evsel, u64 sample_type, const char *sample_msg, enum perf_output_field field) { return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field, false); }

Contributors

PersonTokensPropCommitsCommitProp
adrian hunteradrian hunter37100.00%1100.00%
Total37100.00%1100.00%


static int perf_evsel__check_attr(struct perf_evsel *evsel, struct perf_session *session) { struct perf_event_attr *attr = &evsel->attr; bool allow_user_set; if (perf_header__has_feat(&session->header, HEADER_STAT)) return 0; allow_user_set = perf_header__has_feat(&session->header, HEADER_AUXTRACE); if (PRINT_FIELD(TRACE) && !perf_session__has_traces(session, "record -R")) return -EINVAL; if (PRINT_FIELD(IP)) { if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP)) return -EINVAL; } if (PRINT_FIELD(ADDR) && perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set)) return -EINVAL; if (PRINT_FIELD(DATA_SRC) && perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC)) return -EINVAL; if (PRINT_FIELD(WEIGHT) && perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT", PERF_OUTPUT_WEIGHT)) return -EINVAL; if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { pr_err("Display of symbols requested but neither sample IP nor " "sample address\nis selected. Hence, no addresses to convert " "to symbols.\n"); return -EINVAL; } if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) { pr_err("Display of offsets requested but symbol is not" "selected.\n"); return -EINVAL; } if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) { pr_err("Display of DSO requested but neither sample IP nor " "sample address\nis selected. Hence, no addresses to convert " "to DSO.\n"); return -EINVAL; } if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) { pr_err("Display of source line number requested but sample IP is not\n" "selected. Hence, no address to lookup the source line number.\n"); return -EINVAL; } if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID)) return -EINVAL; if (PRINT_FIELD(TIME) && perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME)) return -EINVAL; if (PRINT_FIELD(CPU) && perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set)) return -EINVAL; if (PRINT_FIELD(PERIOD) && perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD", PERF_OUTPUT_PERIOD)) return -EINVAL; if (PRINT_FIELD(IREGS) && perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS)) return -EINVAL; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern22555.28%538.46%
jiri olsajiri olsa8119.90%323.08%
adrian hunteradrian hunter4611.30%215.38%
akihiro nagaiakihiro nagai256.14%17.69%
stephane eranianstephane eranian225.41%17.69%
arnaldo carvalho de meloarnaldo carvalho de melo81.97%17.69%
Total407100.00%13100.00%


static void set_print_ip_opts(struct perf_event_attr *attr) { unsigned int type = attr->type; output[type].print_ip_opts = 0; if (PRINT_FIELD(IP)) output[type].print_ip_opts |= EVSEL__PRINT_IP; if (PRINT_FIELD(SYM)) output[type].print_ip_opts |= EVSEL__PRINT_SYM; if (PRINT_FIELD(DSO)) output[type].print_ip_opts |= EVSEL__PRINT_DSO; if (PRINT_FIELD(SYMOFFSET)) output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET; if (PRINT_FIELD(SRCLINE)) output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE; }

Contributors

PersonTokensPropCommitsCommitProp
adrian hunteradrian hunter10395.37%266.67%
arnaldo carvalho de meloarnaldo carvalho de melo54.63%133.33%
Total108100.00%3100.00%

/* * verify all user requested events exist and the samples * have the expected data */
static int perf_session__check_output_opt(struct perf_session *session) { unsigned int j; struct perf_evsel *evsel; for (j = 0; j < PERF_TYPE_MAX; ++j) { evsel = perf_session__find_first_evtype(session, j); /* * even if fields is set to 0 (ie., show nothing) event must * exist if user explicitly includes it on the command line */ if (!evsel && output[j].user_set && !output[j].wildcard_set) { pr_err("%s events do not exist. " "Remove corresponding -f option to proceed.\n", event_type(j)); return -1; } if (evsel && output[j].fields && perf_evsel__check_attr(evsel, session)) return -1; if (evsel == NULL) continue; set_print_ip_opts(&evsel->attr); } if (!no_callchain) { bool use_callchain = false; bool not_pipe = false; evlist__for_each_entry(session->evlist, evsel) { not_pipe = true; if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { use_callchain = true; break; } } if (not_pipe && !use_callchain) symbol_conf.use_callchain = false; } /* * set default for tracepoints to print symbols only * if callchains are present */ if (symbol_conf.use_callchain && !output[PERF_TYPE_TRACEPOINT].user_set) { struct perf_event_attr *attr; j = PERF_TYPE_TRACEPOINT; evlist__for_each_entry(session->evlist, evsel) { if (evsel->attr.type != j) continue; attr = &evsel->attr; if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) { output[j].fields |= PERF_OUTPUT_IP; output[j].fields |= PERF_OUTPUT_SYM; output[j].fields |= PERF_OUTPUT_DSO; set_print_ip_opts(attr); goto out; } } } out: return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern20372.50%444.44%
adrian hunteradrian hunter4917.50%222.22%
he kuanghe kuang269.29%222.22%
arnaldo carvalho de meloarnaldo carvalho de melo20.71%111.11%
Total280100.00%9100.00%


static void print_sample_iregs(struct perf_sample *sample, struct perf_event_attr *attr) { struct regs_dump *regs = &sample->intr_regs; uint64_t mask = attr->sample_regs_intr; unsigned i = 0, r; if (!regs) return; for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) { u64 val = regs->regs[i++]; printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val); } }

Contributors

PersonTokensPropCommitsCommitProp
stephane eranianstephane eranian82100.00%1100.00%
Total82100.00%1100.00%


static void print_sample_start(struct perf_sample *sample, struct thread *thread, struct perf_evsel *evsel) { struct perf_event_attr *attr = &evsel->attr; unsigned long secs; unsigned long long nsecs; if (PRINT_FIELD(COMM)) { if (latency_format) printf("%8.8s ", thread__comm_str(thread)); else if (PRINT_FIELD(IP) && symbol_conf.use_callchain) printf("%s ", thread__comm_str(thread)); else printf("%16s ", thread__comm_str(thread)); } if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) printf("%5d/%-5d ", sample->pid, sample->tid); else if (PRINT_FIELD(PID)) printf("%5d ", sample->pid); else if (PRINT_FIELD(TID)) printf("%5d ", sample->tid); if (PRINT_FIELD(CPU)) { if (latency_format) printf("%3d ", sample->cpu); else printf("[%03d] ", sample->cpu); } if (PRINT_FIELD(TIME)) { nsecs = sample->time; secs = nsecs / NSEC_PER_SEC; nsecs -= secs * NSEC_PER_SEC; if (nanosecs) printf("%5lu.%09llu: ", secs, nsecs); else { char sample_time[32]; timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time)); printf("%12s: ", sample_time); } } }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern19776.65%550.00%
namhyung kimnamhyung kim249.34%110.00%
adrian hunteradrian hunter145.45%110.00%
arnaldo carvalho de meloarnaldo carvalho de melo135.06%220.00%
frederic weisbeckerfrederic weisbecker93.50%110.00%
Total257100.00%10100.00%


static inline char mispred_str(struct branch_entry *br) { if (!(br->flags.mispred || br->flags.predicted)) return '-'; return br->flags.predicted ? 'P' : 'M'; }

Contributors

PersonTokensPropCommitsCommitProp
stephane eranianstephane eranian43100.00%1100.00%
Total43100.00%1100.00%


static void print_sample_brstack(struct perf_sample *sample) { struct branch_stack *br = sample->branch_stack; u64 i; if (!(br && br->nr)) return; for (i = 0; i < br->nr; i++) { printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ", br->entries[i].from, br->entries[i].to, mispred_str( br->entries + i), br->entries[i].flags.in_tx? 'X' : '-', br->entries[i].flags.abort? 'A' : '-', br->entries[i].flags.cycles); } }

Contributors

PersonTokensPropCommitsCommitProp
stephane eranianstephane eranian129100.00%1100.00%
Total129100.00%1100.00%


static void print_sample_brstacksym(struct perf_sample *sample, struct thread *thread) { struct branch_stack *br = sample->branch_stack; struct addr_location alf, alt; u64 i, from, to; if (!(br && br->nr)) return; for (i = 0; i < br->nr; i++) { memset(&alf, 0, sizeof(alf)); memset(&alt, 0, sizeof(alt)); from = br->entries[i].from; to = br->entries[i].to; thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf); if (alf.map) alf.sym = map__find_symbol(alf.map, alf.addr); thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt); if (alt.map) alt.sym = map__find_symbol(alt.map, alt.addr); symbol__fprintf_symname_offs(alf.sym, &alf, stdout); putchar('/'); symbol__fprintf_symname_offs(alt.sym, &alt, stdout); printf("/%c/%c/%c/%d ", mispred_str( br->entries + i), br->entries[i].flags.in_tx? 'X' : '-', br->entries[i].flags.abort? 'A' : '-', br->entries[i].flags.cycles); } }

Contributors

PersonTokensPropCommitsCommitProp
stephane eranianstephane eranian26998.53%150.00%
arnaldo carvalho de meloarnaldo carvalho de melo41.47%150.00%
Total273100.00%2100.00%


static void print_sample_addr(struct perf_sample *sample, struct thread *thread, struct perf_event_attr *attr) { struct addr_location al; printf("%16" PRIx64, sample->addr); if (!sample_addr_correlates_sym(attr)) return; thread__resolve(thread, &al, sample); if (PRINT_FIELD(SYM)) { printf(" "); if (PRINT_FIELD(SYMOFFSET)) symbol__fprintf_symname_offs(al.sym, &al, stdout); else symbol__fprintf_symname(al.sym, stdout); } if (PRINT_FIELD(DSO)) { printf(" ("); map__fprintf_dsoname(al.map, stdout); printf(")"); } }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern8265.60%120.00%
akihiro nagaiakihiro nagai3931.20%240.00%
arnaldo carvalho de meloarnaldo carvalho de melo32.40%120.00%
adrian hunteradrian hunter10.80%120.00%
Total125100.00%5100.00%


static void print_sample_callindent(struct perf_sample *sample, struct perf_evsel *evsel, struct thread *thread, struct addr_location *al) { struct perf_event_attr *attr = &evsel->attr; size_t depth = thread_stack__depth(thread); struct addr_location addr_al; const char *name = NULL; static int