679 lines · 24.5 KB · Text
RawDownload
write a useful trace script.  The sections below cover the rest. SCRIPT LAYOUT------------- Every perf script Python script should start by setting up a Pythonmodule search path and 'import'ing a few support modules (see moduledescriptions below): ---- import os import sys  sys.path.append(os.environ['PERF_EXEC_PATH'] + \	      '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')  from perf_trace_context import * from Core import *---- The rest of the script can contain handler functions and supportfunctions in any order. Aside from the event handler functions discussed above, every scriptcan implement a set of optional functions: *trace_begin*, if defined, is called before any event is processed andgives scripts a chance to do setup tasks: ----def trace_begin():    pass---- *trace_end*, if defined, is called after all events have been processed and gives scripts a chance to do end-of-script tasks, such as display results: ----def trace_end():    pass---- *trace_unhandled*, if defined, is called after for any event that doesn't have a handler explicitly defined for it.  The standard set of common arguments are passed into it: ----def trace_unhandled(event_name, context, event_fields_dict):    pass---- *process_event*, if defined, is called for any non-tracepoint event ----def process_event(param_dict):    pass---- *context_switch*, if defined, is called for any context switch ----def context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x):    pass---- *auxtrace_error*, if defined, is called for any AUX area tracing error ----def auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x):    pass---- The remaining sections provide descriptions of each of the availablebuilt-in perf script Python modules and their associated functions. AVAILABLE MODULES AND FUNCTIONS------------------------------- The following sections describe the functions and variables availablevia the various perf script Python modules.  To use the functions andvariables from the given module, add the corresponding 'from XXXXimport' line to your perf script script. Core.py Module~~~~~~~~~~~~~~ These functions provide some essential functions to user scripts. The *flag_str* and *symbol_str* functions provide human-readablestrings for flag and symbolic fields.  These correspond to the stringsand values parsed from the 'print fmt' fields of the event formatfiles:   flag_str(event_name, field_name, field_value) - returns the string representation corresponding to field_value for the flag field field_name of event event_name  symbol_str(event_name, field_name, field_value) - returns the string representation corresponding to field_value for the symbolic field field_name of event event_name The *autodict* function returns a special kind of Pythondictionary that implements Perl's 'autovivifying' hashes in Pythoni.e. with autovivifying hashes, you can assign nested hash valueswithout having to go to the trouble of creating intermediate levels ifthey don't exist.   autodict() - returns an autovivifying dictionary instance  perf_trace_context Module~~~~~~~~~~~~~~~~~~~~~~~~~ Some of the 'common' fields in the event format file aren't all thatcommon, but need to be made accessible to user scripts nonetheless. perf_trace_context defines a set of functions that can be used toaccess this data in the context of the current event.  Each of thesefunctions expects a context variable, which is the same as thecontext variable passed into every tracepoint event handler as the secondargument. For non-tracepoint events, the context variable is also presentas perf_trace_context.perf_script_context .  common_pc(context) - returns common_preempt count for the current event common_flags(context) - returns common_flags for the current event common_lock_depth(context) - returns common_lock_depth for the current event perf_sample_insn(context) - returns the machine code instruction perf_set_itrace_options(context, itrace_options) - set --itrace options if they have not been set already perf_sample_srcline(context) - returns source_file_name, line_number perf_sample_srccode(context) - returns source_file_name, line_number, source_line perf_config_get(config_name) - returns the value of the named config item, or None if unset Util.py Module~~~~~~~~~~~~~~ Various utility functions for use with perf script:   nsecs(secs, nsecs) - returns total nsecs given secs/nsecs pair  nsecs_secs(nsecs) - returns whole secs portion given nsecs  nsecs_nsecs(nsecs) - returns nsecs remainder given nsecs  nsecs_str(nsecs) - returns printable string in the form secs.nsecs  avg(total, n) - returns average given a sum and a total number of values SUPPORTED FIELDS---------------- Currently supported fields: ev_name, comm, id, stream_id, pid, tid, cpu, ip, time, period, phys_addr,addr, symbol, symoff, dso, time_enabled, time_running, values, callchain,brstack, brstacksym, datasrc, datasrc_decode, iregs, uregs,weight, transaction, raw_buf, attr, cpumode. Fields that may also be present:  flags - sample flags flags_disp - sample flags display insn_cnt - instruction count for determining instructions-per-cycle (IPC) cyc_cnt - cycle count for determining IPC addr_correlates_sym - addr can correlate to a symbol addr_dso - addr dso addr_symbol - addr symbol addr_symoff - addr symbol offset Some fields have sub items: brstack:    from, to, from_dsoname, to_dsoname, mispred,    predicted, in_tx, abort, cycles. brstacksym:    items: from, to, pred, in_tx, abort (converted string) For example,We can use this code to print brstack "from", "to", "cycles". if 'brstack' in dict:	for entry in dict['brstack']:		print "from %s, to %s, cycles %s" % (entry["from"], entry["to"], entry["cycles"]) SEE ALSO--------linkperf:perf-script[1]

Lines 501–679 of 679. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t