diff options
Diffstat (limited to 'tools/perf')
126 files changed, 15622 insertions, 3951 deletions
diff --git a/tools/perf/.gitignore b/tools/perf/.gitignore index d69a759a1046..fe08660ce0bd 100644 --- a/tools/perf/.gitignore +++ b/tools/perf/.gitignore | |||
| @@ -10,7 +10,9 @@ perf-stat | |||
| 10 | perf-top | 10 | perf-top |
| 11 | perf*.1 | 11 | perf*.1 |
| 12 | perf*.xml | 12 | perf*.xml |
| 13 | perf*.html | ||
| 13 | common-cmds.h | 14 | common-cmds.h |
| 15 | perf.data | ||
| 14 | tags | 16 | tags |
| 15 | TAGS | 17 | TAGS |
| 16 | cscope* | 18 | cscope* |
diff --git a/tools/perf/Documentation/perf-bench.txt b/tools/perf/Documentation/perf-bench.txt new file mode 100644 index 000000000000..ae525ac5a2ce --- /dev/null +++ b/tools/perf/Documentation/perf-bench.txt | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | perf-bench(1) | ||
| 2 | ============ | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-bench - General framework for benchmark suites | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf bench' [<common options>] <subsystem> <suite> [<options>] | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | This 'perf bench' command is general framework for benchmark suites. | ||
| 16 | |||
| 17 | COMMON OPTIONS | ||
| 18 | -------------- | ||
| 19 | -f:: | ||
| 20 | --format=:: | ||
| 21 | Specify format style. | ||
| 22 | Current available format styles are, | ||
| 23 | |||
| 24 | 'default':: | ||
| 25 | Default style. This is mainly for human reading. | ||
| 26 | --------------------- | ||
| 27 | % perf bench sched pipe # with no style specify | ||
| 28 | (executing 1000000 pipe operations between two tasks) | ||
| 29 | Total time:5.855 sec | ||
| 30 | 5.855061 usecs/op | ||
| 31 | 170792 ops/sec | ||
| 32 | --------------------- | ||
| 33 | |||
| 34 | 'simple':: | ||
| 35 | This simple style is friendly for automated | ||
| 36 | processing by scripts. | ||
| 37 | --------------------- | ||
| 38 | % perf bench --format=simple sched pipe # specified simple | ||
| 39 | 5.988 | ||
| 40 | --------------------- | ||
| 41 | |||
| 42 | SUBSYSTEM | ||
| 43 | --------- | ||
| 44 | |||
| 45 | 'sched':: | ||
| 46 | Scheduler and IPC mechanisms. | ||
| 47 | |||
| 48 | SUITES FOR 'sched' | ||
| 49 | ~~~~~~~~~~~~~~~~~~ | ||
| 50 | *messaging*:: | ||
| 51 | Suite for evaluating performance of scheduler and IPC mechanisms. | ||
| 52 | Based on hackbench by Rusty Russell. | ||
| 53 | |||
| 54 | Options of *pipe* | ||
| 55 | ^^^^^^^^^^^^^^^^^ | ||
| 56 | -p:: | ||
| 57 | --pipe:: | ||
| 58 | Use pipe() instead of socketpair() | ||
| 59 | |||
| 60 | -t:: | ||
| 61 | --thread:: | ||
| 62 | Be multi thread instead of multi process | ||
| 63 | |||
| 64 | -g:: | ||
| 65 | --group=:: | ||
| 66 | Specify number of groups | ||
| 67 | |||
| 68 | -l:: | ||
| 69 | --loop=:: | ||
| 70 | Specify number of loops | ||
| 71 | |||
| 72 | Example of *messaging* | ||
| 73 | ^^^^^^^^^^^^^^^^^^^^^^ | ||
| 74 | |||
| 75 | --------------------- | ||
| 76 | % perf bench sched messaging # run with default | ||
| 77 | options (20 sender and receiver processes per group) | ||
| 78 | (10 groups == 400 processes run) | ||
| 79 | |||
| 80 | Total time:0.308 sec | ||
| 81 | |||
| 82 | % perf bench sched messaging -t -g 20 # be multi-thread,with 20 groups | ||
| 83 | (20 sender and receiver threads per group) | ||
| 84 | (20 groups == 800 threads run) | ||
| 85 | |||
| 86 | Total time:0.582 sec | ||
| 87 | --------------------- | ||
| 88 | |||
| 89 | *pipe*:: | ||
| 90 | Suite for pipe() system call. | ||
| 91 | Based on pipe-test-1m.c by Ingo Molnar. | ||
| 92 | |||
| 93 | Options of *pipe* | ||
| 94 | ^^^^^^^^^^^^^^^^^ | ||
| 95 | -l:: | ||
| 96 | --loop=:: | ||
| 97 | Specify number of loops. | ||
| 98 | |||
| 99 | Example of *pipe* | ||
| 100 | ^^^^^^^^^^^^^^^^^ | ||
| 101 | |||
| 102 | --------------------- | ||
| 103 | % perf bench sched pipe | ||
| 104 | (executing 1000000 pipe operations between two tasks) | ||
| 105 | |||
| 106 | Total time:8.091 sec | ||
| 107 | 8.091833 usecs/op | ||
| 108 | 123581 ops/sec | ||
| 109 | |||
| 110 | % perf bench sched pipe -l 1000 # loop 1000 | ||
| 111 | (executing 1000 pipe operations between two tasks) | ||
| 112 | |||
| 113 | Total time:0.016 sec | ||
| 114 | 16.948000 usecs/op | ||
| 115 | 59004 ops/sec | ||
| 116 | --------------------- | ||
| 117 | |||
| 118 | SEE ALSO | ||
| 119 | -------- | ||
| 120 | linkperf:perf[1] | ||
diff --git a/tools/perf/Documentation/perf-buildid-list.txt b/tools/perf/Documentation/perf-buildid-list.txt new file mode 100644 index 000000000000..01b642c0bf8f --- /dev/null +++ b/tools/perf/Documentation/perf-buildid-list.txt | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | perf-buildid-list(1) | ||
| 2 | ==================== | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-buildid-list - List the buildids in a perf.data file | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf buildid-list <options>' | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | This command displays the buildids found in a perf.data file, so that other | ||
| 16 | tools can be used to fetch packages with matching symbol tables for use by | ||
| 17 | perf report. | ||
| 18 | |||
| 19 | OPTIONS | ||
| 20 | ------- | ||
| 21 | -i:: | ||
| 22 | --input=:: | ||
| 23 | Input file name. (default: perf.data) | ||
| 24 | -f:: | ||
| 25 | --force:: | ||
| 26 | Don't do ownership validation. | ||
| 27 | -v:: | ||
| 28 | --verbose:: | ||
| 29 | Be more verbose. | ||
| 30 | |||
| 31 | SEE ALSO | ||
| 32 | -------- | ||
| 33 | linkperf:perf-record[1], linkperf:perf-top[1], | ||
| 34 | linkperf:perf-report[1] | ||
diff --git a/tools/perf/Documentation/perf-kmem.txt b/tools/perf/Documentation/perf-kmem.txt new file mode 100644 index 000000000000..44b0ce35c28a --- /dev/null +++ b/tools/perf/Documentation/perf-kmem.txt | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | perf-kmem(1) | ||
| 2 | ============== | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-kmem - Tool to trace/measure kernel memory(slab) properties | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf kmem' {record} [<options>] | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | There's two variants of perf kmem: | ||
| 16 | |||
| 17 | 'perf kmem record <command>' to record the kmem events | ||
| 18 | of an arbitrary workload. | ||
| 19 | |||
| 20 | 'perf kmem' to report kernel memory statistics. | ||
| 21 | |||
| 22 | OPTIONS | ||
| 23 | ------- | ||
| 24 | -i <file>:: | ||
| 25 | --input=<file>:: | ||
| 26 | Select the input file (default: perf.data) | ||
| 27 | |||
| 28 | --stat=<caller|alloc>:: | ||
| 29 | Select per callsite or per allocation statistics | ||
| 30 | |||
| 31 | -s <key[,key2...]>:: | ||
| 32 | --sort=<key[,key2...]>:: | ||
| 33 | Sort the output (default: frag,hit,bytes) | ||
| 34 | |||
| 35 | -l <num>:: | ||
| 36 | --line=<num>:: | ||
| 37 | Print n lines only | ||
| 38 | |||
| 39 | --raw-ip:: | ||
| 40 | Print raw ip instead of symbol | ||
| 41 | |||
| 42 | SEE ALSO | ||
| 43 | -------- | ||
| 44 | linkperf:perf-record[1] | ||
diff --git a/tools/perf/Documentation/perf-probe.txt b/tools/perf/Documentation/perf-probe.txt new file mode 100644 index 000000000000..9270594e6dfd --- /dev/null +++ b/tools/perf/Documentation/perf-probe.txt | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | perf-probe(1) | ||
| 2 | ============= | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-probe - Define new dynamic tracepoints | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf probe' [options] --add 'PROBE' [--add 'PROBE' ...] | ||
| 12 | or | ||
| 13 | 'perf probe' [options] 'PROBE' ['PROBE' ...] | ||
| 14 | |||
| 15 | |||
| 16 | DESCRIPTION | ||
| 17 | ----------- | ||
| 18 | This command defines dynamic tracepoint events, by symbol and registers | ||
| 19 | without debuginfo, or by C expressions (C line numbers, C function names, | ||
| 20 | and C local variables) with debuginfo. | ||
| 21 | |||
| 22 | |||
| 23 | OPTIONS | ||
| 24 | ------- | ||
| 25 | -k:: | ||
| 26 | --vmlinux=PATH:: | ||
| 27 | Specify vmlinux path which has debuginfo (Dwarf binary). | ||
| 28 | |||
| 29 | -v:: | ||
| 30 | --verbose:: | ||
| 31 | Be more verbose (show parsed arguments, etc). | ||
| 32 | |||
| 33 | -a:: | ||
| 34 | --add:: | ||
| 35 | Define a probe point (see PROBE SYNTAX for detail) | ||
| 36 | |||
| 37 | PROBE SYNTAX | ||
| 38 | ------------ | ||
| 39 | Probe points are defined by following syntax. | ||
| 40 | |||
| 41 | "FUNC[+OFFS|:RLN|%return][@SRC]|SRC:ALN [ARG ...]" | ||
| 42 | |||
| 43 | 'FUNC' specifies a probed function name, and it may have one of the following options; '+OFFS' is the offset from function entry address in bytes, 'RLN' is the relative-line number from function entry line, and '%return' means that it probes function return. In addition, 'SRC' specifies a source file which has that function. | ||
| 44 | It is also possible to specify a probe point by the source line number by using 'SRC:ALN' syntax, where 'SRC' is the source file path and 'ALN' is the line number. | ||
| 45 | 'ARG' specifies the arguments of this probe point. You can use the name of local variable, or kprobe-tracer argument format (e.g. $retval, %ax, etc). | ||
| 46 | |||
| 47 | SEE ALSO | ||
| 48 | -------- | ||
| 49 | linkperf:perf-trace[1], linkperf:perf-record[1] | ||
diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt index 0ff23de9e453..fc46c0b40f6e 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt | |||
| @@ -26,11 +26,19 @@ OPTIONS | |||
| 26 | 26 | ||
| 27 | -e:: | 27 | -e:: |
| 28 | --event=:: | 28 | --event=:: |
| 29 | Select the PMU event. Selection can be a symbolic event name | 29 | Select the PMU event. Selection can be: |
| 30 | (use 'perf list' to list all events) or a raw PMU | ||
| 31 | event (eventsel+umask) in the form of rNNN where NNN is a | ||
| 32 | hexadecimal event descriptor. | ||
| 33 | 30 | ||
| 31 | - a symbolic event name (use 'perf list' to list all events) | ||
| 32 | |||
| 33 | - a raw PMU event (eventsel+umask) in the form of rNNN where NNN is a | ||
| 34 | hexadecimal event descriptor. | ||
| 35 | |||
| 36 | - a hardware breakpoint event in the form of '\mem:addr[:access]' | ||
| 37 | where addr is the address in memory you want to break in. | ||
| 38 | Access is the memory access type (read, write, execute) it can | ||
| 39 | be passed as follows: '\mem:addr[:[r][w][x]]'. | ||
| 40 | If you want to profile read-write accesses in 0x1000, just set | ||
| 41 | 'mem:0x1000:rw'. | ||
| 34 | -a:: | 42 | -a:: |
| 35 | System-wide collection. | 43 | System-wide collection. |
| 36 | 44 | ||
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt index 59f0b846cd71..9dccb180b7af 100644 --- a/tools/perf/Documentation/perf-report.txt +++ b/tools/perf/Documentation/perf-report.txt | |||
| @@ -24,11 +24,11 @@ OPTIONS | |||
| 24 | --dsos=:: | 24 | --dsos=:: |
| 25 | Only consider symbols in these dsos. CSV that understands | 25 | Only consider symbols in these dsos. CSV that understands |
| 26 | file://filename entries. | 26 | file://filename entries. |
| 27 | -n | 27 | -n:: |
| 28 | --show-nr-samples | 28 | --show-nr-samples:: |
| 29 | Show the number of samples for each symbol | 29 | Show the number of samples for each symbol |
| 30 | -T | 30 | -T:: |
| 31 | --threads | 31 | --threads:: |
| 32 | Show per-thread event counters | 32 | Show per-thread event counters |
| 33 | -C:: | 33 | -C:: |
| 34 | --comms=:: | 34 | --comms=:: |
diff --git a/tools/perf/Documentation/perf-sched.txt b/tools/perf/Documentation/perf-sched.txt new file mode 100644 index 000000000000..1ce79198997b --- /dev/null +++ b/tools/perf/Documentation/perf-sched.txt | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | perf-sched(1) | ||
| 2 | ============== | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-sched - Tool to trace/measure scheduler properties (latencies) | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf sched' {record|latency|replay|trace} | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | There's four variants of perf sched: | ||
| 16 | |||
| 17 | 'perf sched record <command>' to record the scheduling events | ||
| 18 | of an arbitrary workload. | ||
| 19 | |||
| 20 | 'perf sched latency' to report the per task scheduling latencies | ||
| 21 | and other scheduling properties of the workload. | ||
| 22 | |||
| 23 | 'perf sched trace' to see a detailed trace of the workload that | ||
| 24 | was recorded. | ||
| 25 | |||
| 26 | 'perf sched replay' to simulate the workload that was recorded | ||
| 27 | via perf sched record. (this is done by starting up mockup threads | ||
| 28 | that mimic the workload based on the events in the trace. These | ||
| 29 | threads can then replay the timings (CPU runtime and sleep patterns) | ||
| 30 | of the workload as it occured when it was recorded - and can repeat | ||
| 31 | it a number of times, measuring its performance.) | ||
| 32 | |||
| 33 | OPTIONS | ||
| 34 | ------- | ||
| 35 | -D:: | ||
| 36 | --dump-raw-trace=:: | ||
| 37 | Display verbose dump of the sched data. | ||
| 38 | |||
| 39 | SEE ALSO | ||
| 40 | -------- | ||
| 41 | linkperf:perf-record[1] | ||
diff --git a/tools/perf/Documentation/perf-timechart.txt b/tools/perf/Documentation/perf-timechart.txt new file mode 100644 index 000000000000..4b1788355eca --- /dev/null +++ b/tools/perf/Documentation/perf-timechart.txt | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | perf-timechart(1) | ||
| 2 | ================= | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-timechart - Tool to visualize total system behavior during a workload | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf timechart' {record} | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | There are two variants of perf timechart: | ||
| 16 | |||
| 17 | 'perf timechart record <command>' to record the system level events | ||
| 18 | of an arbitrary workload. | ||
| 19 | |||
| 20 | 'perf timechart' to turn a trace into a Scalable Vector Graphics file, | ||
| 21 | that can be viewed with popular SVG viewers such as 'Inkscape'. | ||
| 22 | |||
| 23 | OPTIONS | ||
| 24 | ------- | ||
| 25 | -o:: | ||
| 26 | --output=:: | ||
| 27 | Select the output file (default: output.svg) | ||
| 28 | -i:: | ||
| 29 | --input=:: | ||
| 30 | Select the input file (default: perf.data) | ||
| 31 | -w:: | ||
| 32 | --width=:: | ||
| 33 | Select the width of the SVG file (default: 1000) | ||
| 34 | -P:: | ||
| 35 | --power-only:: | ||
| 36 | Only output the CPU power section of the diagram | ||
| 37 | -p:: | ||
| 38 | --process:: | ||
| 39 | Select the processes to display, by name or PID | ||
| 40 | |||
| 41 | |||
| 42 | SEE ALSO | ||
| 43 | -------- | ||
| 44 | linkperf:perf-record[1] | ||
diff --git a/tools/perf/Documentation/perf-trace-perl.txt b/tools/perf/Documentation/perf-trace-perl.txt new file mode 100644 index 000000000000..c5f55f439091 --- /dev/null +++ b/tools/perf/Documentation/perf-trace-perl.txt | |||
| @@ -0,0 +1,219 @@ | |||
| 1 | perf-trace-perl(1) | ||
| 2 | ================== | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-trace-perl - Process trace data with a Perl script | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf trace' [-s [lang]:script[.ext] ] | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | |||
| 16 | This perf trace option is used to process perf trace data using perf's | ||
| 17 | built-in Perl interpreter. It reads and processes the input file and | ||
| 18 | displays the results of the trace analysis implemented in the given | ||
| 19 | Perl script, if any. | ||
| 20 | |||
| 21 | STARTER SCRIPTS | ||
| 22 | --------------- | ||
| 23 | |||
| 24 | You can avoid reading the rest of this document by running 'perf trace | ||
| 25 | -g perl' in the same directory as an existing perf.data trace file. | ||
| 26 | That will generate a starter script containing a handler for each of | ||
| 27 | the event types in the trace file; it simply prints every available | ||
| 28 | field for each event in the trace file. | ||
| 29 | |||
| 30 | You can also look at the existing scripts in | ||
| 31 | ~/libexec/perf-core/scripts/perl for typical examples showing how to | ||
| 32 | do basic things like aggregate event data, print results, etc. Also, | ||
| 33 | the check-perf-trace.pl script, while not interesting for its results, | ||
| 34 | attempts to exercise all of the main scripting features. | ||
| 35 | |||
| 36 | EVENT HANDLERS | ||
| 37 | -------------- | ||
| 38 | |||
| 39 | When perf trace is invoked using a trace script, a user-defined | ||
| 40 | 'handler function' is called for each event in the trace. If there's | ||
| 41 | no handler function defined for a given event type, the event is | ||
| 42 | ignored (or passed to a 'trace_handled' function, see below) and the | ||
| 43 | next event is processed. | ||
| 44 | |||
| 45 | Most of the event's field values are passed as arguments to the | ||
| 46 | handler function; some of the less common ones aren't - those are | ||
| 47 | available as calls back into the perf executable (see below). | ||
| 48 | |||
| 49 | As an example, the following perf record command can be used to record | ||
| 50 | all sched_wakeup events in the system: | ||
| 51 | |||
| 52 | # perf record -c 1 -f -a -M -R -e sched:sched_wakeup | ||
| 53 | |||
| 54 | Traces meant to be processed using a script should be recorded with | ||
| 55 | the above options: -c 1 says to sample every event, -a to enable | ||
| 56 | system-wide collection, -M to multiplex the output, and -R to collect | ||
| 57 | raw samples. | ||
| 58 | |||
| 59 | The format file for the sched_wakep event defines the following fields | ||
| 60 | (see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format): | ||
| 61 | |||
| 62 | ---- | ||
| 63 | format: | ||
| 64 | field:unsigned short common_type; | ||
| 65 | field:unsigned char common_flags; | ||
| 66 | field:unsigned char common_preempt_count; | ||
| 67 | field:int common_pid; | ||
| 68 | field:int common_lock_depth; | ||
| 69 | |||
| 70 | field:char comm[TASK_COMM_LEN]; | ||
| 71 | field:pid_t pid; | ||
| 72 | field:int prio; | ||
| 73 | field:int success; | ||
| 74 | field:int target_cpu; | ||
| 75 | ---- | ||
| 76 | |||
| 77 | The handler function for this event would be defined as: | ||
| 78 | |||
| 79 | ---- | ||
| 80 | sub sched::sched_wakeup | ||
| 81 | { | ||
| 82 | my ($event_name, $context, $common_cpu, $common_secs, | ||
| 83 | $common_nsecs, $common_pid, $common_comm, | ||
| 84 | $comm, $pid, $prio, $success, $target_cpu) = @_; | ||
| 85 | } | ||
| 86 | ---- | ||
| 87 | |||
| 88 | The handler function takes the form subsystem::event_name. | ||
| 89 | |||
| 90 | The $common_* arguments in the handler's argument list are the set of | ||
| 91 | arguments passed to all event handlers; some of the fields correspond | ||
| 92 | to the common_* fields in the format file, but some are synthesized, | ||
| 93 | and some of the common_* fields aren't common enough to to be passed | ||
| 94 | to every event as arguments but are available as library functions. | ||
| 95 | |||
| 96 | Here's a brief description of each of the invariant event args: | ||
| 97 | |||
| 98 | $event_name the name of the event as text | ||
| 99 | $context an opaque 'cookie' used in calls back into perf | ||
| 100 | $common_cpu the cpu the event occurred on | ||
| 101 | $common_secs the secs portion of the event timestamp | ||
| 102 | $common_nsecs the nsecs portion of the event timestamp | ||
| 103 | $common_pid the pid of the current task | ||
| 104 | $common_comm the name of the current process | ||
| 105 | |||
| 106 | All of the remaining fields in the event's format file have | ||
| 107 | counterparts as handler function arguments of the same name, as can be | ||
| 108 | seen in the example above. | ||
| 109 | |||
| 110 | The above provides the basics needed to directly access every field of | ||
| 111 | every event in a trace, which covers 90% of what you need to know to | ||
| 112 | write a useful trace script. The sections below cover the rest. | ||
| 113 | |||
| 114 | SCRIPT LAYOUT | ||
| 115 | ------------- | ||
| 116 | |||
| 117 | Every perf trace Perl script should start by setting up a Perl module | ||
| 118 | search path and 'use'ing a few support modules (see module | ||
| 119 | descriptions below): | ||
| 120 | |||
| 121 | ---- | ||
| 122 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; | ||
| 123 | use lib "./Perf-Trace-Util/lib"; | ||
| 124 | use Perf::Trace::Core; | ||
| 125 | use Perf::Trace::Context; | ||
| 126 | use Perf::Trace::Util; | ||
| 127 | ---- | ||
| 128 | |||
| 129 | The rest of the script can contain handler functions and support | ||
| 130 | functions in any order. | ||
| 131 | |||
| 132 | Aside from the event handler functions discussed above, every script | ||
| 133 | can implement a set of optional functions: | ||
| 134 | |||
| 135 | *trace_begin*, if defined, is called before any event is processed and | ||
| 136 | gives scripts a chance to do setup tasks: | ||
| 137 | |||
| 138 | ---- | ||
| 139 | sub trace_begin | ||
| 140 | { | ||
| 141 | } | ||
| 142 | ---- | ||
| 143 | |||
| 144 | *trace_end*, if defined, is called after all events have been | ||
| 145 | processed and gives scripts a chance to do end-of-script tasks, such | ||
| 146 | as display results: | ||
| 147 | |||
| 148 | ---- | ||
| 149 | sub trace_end | ||
| 150 | { | ||
| 151 | } | ||
| 152 | ---- | ||
| 153 | |||
| 154 | *trace_unhandled*, if defined, is called after for any event that | ||
| 155 | doesn't have a handler explicitly defined for it. The standard set | ||
| 156 | of common arguments are passed into it: | ||
| 157 | |||
| 158 | ---- | ||
| 159 | sub trace_unhandled | ||
| 160 | { | ||
| 161 | my ($event_name, $context, $common_cpu, $common_secs, | ||
| 162 | $common_nsecs, $common_pid, $common_comm) = @_; | ||
| 163 | } | ||
| 164 | ---- | ||
| 165 | |||
| 166 | The remaining sections provide descriptions of each of the available | ||
| 167 | built-in perf trace Perl modules and their associated functions. | ||
| 168 | |||
| 169 | AVAILABLE MODULES AND FUNCTIONS | ||
| 170 | ------------------------------- | ||
| 171 | |||
| 172 | The following sections describe the functions and variables available | ||
| 173 | via the various Perf::Trace::* Perl modules. To use the functions and | ||
| 174 | variables from the given module, add the corresponding 'use | ||
| 175 | Perf::Trace::XXX' line to your perf trace script. | ||
| 176 | |||
| 177 | Perf::Trace::Core Module | ||
| 178 | ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 179 | |||
| 180 | These functions provide some essential functions to user scripts. | ||
| 181 | |||
| 182 | The *flag_str* and *symbol_str* functions provide human-readable | ||
| 183 | strings for flag and symbolic fields. These correspond to the strings | ||
| 184 | and values parsed from the 'print fmt' fields of the event format | ||
| 185 | files: | ||
| 186 | |||
| 187 | flag_str($event_name, $field_name, $field_value) - returns the string represention corresponding to $field_value for the flag field $field_name of event $event_name | ||
| 188 | symbol_str($event_name, $field_name, $field_value) - returns the string represention corresponding to $field_value for the symbolic field $field_name of event $event_name | ||
| 189 | |||
| 190 | Perf::Trace::Context Module | ||
| 191 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 192 | |||
| 193 | Some of the 'common' fields in the event format file aren't all that | ||
| 194 | common, but need to be made accessible to user scripts nonetheless. | ||
| 195 | |||
| 196 | Perf::Trace::Context defines a set of functions that can be used to | ||
| 197 | access this data in the context of the current event. Each of these | ||
| 198 | functions expects a $context variable, which is the same as the | ||
| 199 | $context variable passed into every event handler as the second | ||
| 200 | argument. | ||
| 201 | |||
| 202 | common_pc($context) - returns common_preempt count for the current event | ||
| 203 | common_flags($context) - returns common_flags for the current event | ||
| 204 | common_lock_depth($context) - returns common_lock_depth for the current event | ||
| 205 | |||
| 206 | Perf::Trace::Util Module | ||
| 207 | ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 208 | |||
| 209 | Various utility functions for use with perf trace: | ||
| 210 | |||
| 211 | nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair | ||
| 212 | nsecs_secs($nsecs) - returns whole secs portion given nsecs | ||
| 213 | nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs | ||
| 214 | nsecs_str($nsecs) - returns printable string in the form secs.nsecs | ||
| 215 | avg($total, $n) - returns average given a sum and a total number of values | ||
| 216 | |||
| 217 | SEE ALSO | ||
| 218 | -------- | ||
| 219 | linkperf:perf-trace[1] | ||
diff --git a/tools/perf/Documentation/perf-trace.txt b/tools/perf/Documentation/perf-trace.txt new file mode 100644 index 000000000000..07065efa60e0 --- /dev/null +++ b/tools/perf/Documentation/perf-trace.txt | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | perf-trace(1) | ||
| 2 | ============== | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-trace - Read perf.data (created by perf record) and display trace output | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf trace' [-i <file> | --input=file] symbol_name | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | This command reads the input file and displays the trace recorded. | ||
| 16 | |||
| 17 | OPTIONS | ||
| 18 | ------- | ||
| 19 | -D:: | ||
| 20 | --dump-raw-trace=:: | ||
| 21 | Display verbose dump of the trace data. | ||
| 22 | |||
| 23 | -s:: | ||
| 24 | --script=:: | ||
| 25 | Process trace data with the given script ([lang]:script[.ext]). | ||
| 26 | |||
| 27 | -g:: | ||
| 28 | --gen-script=:: | ||
| 29 | Generate perf-trace.[ext] starter script for given language, | ||
| 30 | using current perf.data. | ||
| 31 | |||
| 32 | SEE ALSO | ||
| 33 | -------- | ||
| 34 | linkperf:perf-record[1], linkperf:perf-trace-perl[1] | ||
diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 9f8d207a91bf..23ec66098bdc 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | all:: | 2 | all:: |
| 3 | 3 | ||
| 4 | # Define V=1 to have a more verbose compile. | 4 | # Define V=1 to have a more verbose compile. |
| 5 | # Define V=2 to have an even more verbose compile. | ||
| 5 | # | 6 | # |
| 6 | # Define SNPRINTF_RETURNS_BOGUS if your are on a system which snprintf() | 7 | # Define SNPRINTF_RETURNS_BOGUS if your are on a system which snprintf() |
| 7 | # or vsnprintf() return -1 instead of number of characters which would | 8 | # or vsnprintf() return -1 instead of number of characters which would |
| @@ -145,6 +146,10 @@ all:: | |||
| 145 | # Define NO_EXTERNAL_GREP if you don't want "perf grep" to ever call | 146 | # Define NO_EXTERNAL_GREP if you don't want "perf grep" to ever call |
| 146 | # your external grep (e.g., if your system lacks grep, if its grep is | 147 | # your external grep (e.g., if your system lacks grep, if its grep is |
| 147 | # broken, or spawning external process is slower than built-in grep perf has). | 148 | # broken, or spawning external process is slower than built-in grep perf has). |
| 149 | # | ||
| 150 | # Define LDFLAGS=-static to build a static binary. | ||
| 151 | # | ||
| 152 | # Define EXTRA_CFLAGS=-m64 or EXTRA_CFLAGS=-m32 as appropriate for cross-builds. | ||
| 148 | 153 | ||
| 149 | PERF-VERSION-FILE: .FORCE-PERF-VERSION-FILE | 154 | PERF-VERSION-FILE: .FORCE-PERF-VERSION-FILE |
| 150 | @$(SHELL_PATH) util/PERF-VERSION-GEN | 155 | @$(SHELL_PATH) util/PERF-VERSION-GEN |
| @@ -157,21 +162,13 @@ uname_R := $(shell sh -c 'uname -r 2>/dev/null || echo not') | |||
| 157 | uname_P := $(shell sh -c 'uname -p 2>/dev/null || echo not') | 162 | uname_P := $(shell sh -c 'uname -p 2>/dev/null || echo not') |
| 158 | uname_V := $(shell sh -c 'uname -v 2>/dev/null || echo not') | 163 | uname_V := $(shell sh -c 'uname -v 2>/dev/null || echo not') |
| 159 | 164 | ||
| 160 | # If we're on a 64-bit kernel, use -m64 | ||
| 161 | ifndef NO_64BIT | ||
| 162 | ifneq ($(patsubst %64,%,$(uname_M)),$(uname_M)) | ||
| 163 | M64 := -m64 | ||
| 164 | endif | ||
| 165 | endif | ||
| 166 | |||
| 167 | # CFLAGS and LDFLAGS are for the users to override from the command line. | 165 | # CFLAGS and LDFLAGS are for the users to override from the command line. |
| 168 | 166 | ||
| 169 | # | 167 | # |
| 170 | # Include saner warnings here, which can catch bugs: | 168 | # Include saner warnings here, which can catch bugs: |
| 171 | # | 169 | # |
| 172 | 170 | ||
| 173 | EXTRA_WARNINGS := -Wcast-align | 171 | EXTRA_WARNINGS := -Wformat |
| 174 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wformat | ||
| 175 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wformat-security | 172 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wformat-security |
| 176 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wformat-y2k | 173 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wformat-y2k |
| 177 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wshadow | 174 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wshadow |
| @@ -194,8 +191,15 @@ EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wold-style-definition | |||
| 194 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wstrict-prototypes | 191 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wstrict-prototypes |
| 195 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wdeclaration-after-statement | 192 | EXTRA_WARNINGS := $(EXTRA_WARNINGS) -Wdeclaration-after-statement |
| 196 | 193 | ||
| 197 | CFLAGS = $(M64) -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -fstack-protector-all -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) | 194 | ifeq ("$(origin DEBUG)", "command line") |
| 198 | LDFLAGS = -lpthread -lrt -lelf -lm | 195 | PERF_DEBUG = $(DEBUG) |
| 196 | endif | ||
| 197 | ifndef PERF_DEBUG | ||
| 198 | CFLAGS_OPTIMIZE = -O6 | ||
| 199 | endif | ||
| 200 | |||
| 201 | CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) | ||
| 202 | EXTLIBS = -lpthread -lrt -lelf -lm | ||
| 199 | ALL_CFLAGS = $(CFLAGS) | 203 | ALL_CFLAGS = $(CFLAGS) |
| 200 | ALL_LDFLAGS = $(LDFLAGS) | 204 | ALL_LDFLAGS = $(LDFLAGS) |
| 201 | STRIP ?= strip | 205 | STRIP ?= strip |
| @@ -246,6 +250,9 @@ PTHREAD_LIBS = -lpthread | |||
| 246 | # explicitly what architecture to check for. Fix this up for yours.. | 250 | # explicitly what architecture to check for. Fix this up for yours.. |
| 247 | SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__ | 251 | SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__ |
| 248 | 252 | ||
| 253 | ifeq ($(shell sh -c "echo 'int foo(void) {char X[2]; return 3;}' | $(CC) -x c -c -Werror -fstack-protector-all - -o /dev/null "$(QUIET_STDERR)" && echo y"), y) | ||
| 254 | CFLAGS := $(CFLAGS) -fstack-protector-all | ||
| 255 | endif | ||
| 249 | 256 | ||
| 250 | 257 | ||
| 251 | ### --- END CONFIGURATION SECTION --- | 258 | ### --- END CONFIGURATION SECTION --- |
| @@ -318,11 +325,31 @@ export PERL_PATH | |||
| 318 | 325 | ||
| 319 | LIB_FILE=libperf.a | 326 | LIB_FILE=libperf.a |
| 320 | 327 | ||
| 321 | LIB_H += ../../include/linux/perf_counter.h | 328 | LIB_H += ../../include/linux/perf_event.h |
| 322 | LIB_H += ../../include/linux/rbtree.h | 329 | LIB_H += ../../include/linux/rbtree.h |
| 323 | LIB_H += ../../include/linux/list.h | 330 | LIB_H += ../../include/linux/list.h |
| 331 | LIB_H += ../../include/linux/stringify.h | ||
| 332 | LIB_H += util/include/linux/bitmap.h | ||
| 333 | LIB_H += util/include/linux/bitops.h | ||
| 334 | LIB_H += util/include/linux/compiler.h | ||
| 335 | LIB_H += util/include/linux/ctype.h | ||
| 336 | LIB_H += util/include/linux/kernel.h | ||
| 324 | LIB_H += util/include/linux/list.h | 337 | LIB_H += util/include/linux/list.h |
| 338 | LIB_H += util/include/linux/module.h | ||
| 339 | LIB_H += util/include/linux/poison.h | ||
| 340 | LIB_H += util/include/linux/prefetch.h | ||
| 341 | LIB_H += util/include/linux/rbtree.h | ||
| 342 | LIB_H += util/include/linux/string.h | ||
| 343 | LIB_H += util/include/linux/types.h | ||
| 344 | LIB_H += util/include/asm/asm-offsets.h | ||
| 345 | LIB_H += util/include/asm/bitops.h | ||
| 346 | LIB_H += util/include/asm/byteorder.h | ||
| 347 | LIB_H += util/include/asm/swab.h | ||
| 348 | LIB_H += util/include/asm/system.h | ||
| 349 | LIB_H += util/include/asm/uaccess.h | ||
| 325 | LIB_H += perf.h | 350 | LIB_H += perf.h |
| 351 | LIB_H += util/debugfs.h | ||
| 352 | LIB_H += util/event.h | ||
| 326 | LIB_H += util/types.h | 353 | LIB_H += util/types.h |
| 327 | LIB_H += util/levenshtein.h | 354 | LIB_H += util/levenshtein.h |
| 328 | LIB_H += util/parse-options.h | 355 | LIB_H += util/parse-options.h |
| @@ -336,15 +363,22 @@ LIB_H += util/strlist.h | |||
| 336 | LIB_H += util/run-command.h | 363 | LIB_H += util/run-command.h |
| 337 | LIB_H += util/sigchain.h | 364 | LIB_H += util/sigchain.h |
| 338 | LIB_H += util/symbol.h | 365 | LIB_H += util/symbol.h |
| 339 | LIB_H += util/module.h | ||
| 340 | LIB_H += util/color.h | 366 | LIB_H += util/color.h |
| 341 | LIB_H += util/values.h | 367 | LIB_H += util/values.h |
| 368 | LIB_H += util/sort.h | ||
| 369 | LIB_H += util/hist.h | ||
| 370 | LIB_H += util/thread.h | ||
| 371 | LIB_H += util/data_map.h | ||
| 372 | LIB_H += util/probe-finder.h | ||
| 373 | LIB_H += util/probe-event.h | ||
| 342 | 374 | ||
| 343 | LIB_OBJS += util/abspath.o | 375 | LIB_OBJS += util/abspath.o |
| 344 | LIB_OBJS += util/alias.o | 376 | LIB_OBJS += util/alias.o |
| 345 | LIB_OBJS += util/config.o | 377 | LIB_OBJS += util/config.o |
| 346 | LIB_OBJS += util/ctype.o | 378 | LIB_OBJS += util/ctype.o |
| 379 | LIB_OBJS += util/debugfs.o | ||
| 347 | LIB_OBJS += util/environment.o | 380 | LIB_OBJS += util/environment.o |
| 381 | LIB_OBJS += util/event.o | ||
| 348 | LIB_OBJS += util/exec_cmd.o | 382 | LIB_OBJS += util/exec_cmd.o |
| 349 | LIB_OBJS += util/help.o | 383 | LIB_OBJS += util/help.o |
| 350 | LIB_OBJS += util/levenshtein.o | 384 | LIB_OBJS += util/levenshtein.o |
| @@ -352,6 +386,9 @@ LIB_OBJS += util/parse-options.o | |||
| 352 | LIB_OBJS += util/parse-events.o | 386 | LIB_OBJS += util/parse-events.o |
| 353 | LIB_OBJS += util/path.o | 387 | LIB_OBJS += util/path.o |
| 354 | LIB_OBJS += util/rbtree.o | 388 | LIB_OBJS += util/rbtree.o |
| 389 | LIB_OBJS += util/bitmap.o | ||
| 390 | LIB_OBJS += util/hweight.o | ||
| 391 | LIB_OBJS += util/find_next_bit.o | ||
| 355 | LIB_OBJS += util/run-command.o | 392 | LIB_OBJS += util/run-command.o |
| 356 | LIB_OBJS += util/quote.o | 393 | LIB_OBJS += util/quote.o |
| 357 | LIB_OBJS += util/strbuf.o | 394 | LIB_OBJS += util/strbuf.o |
| @@ -361,7 +398,6 @@ LIB_OBJS += util/usage.o | |||
| 361 | LIB_OBJS += util/wrapper.o | 398 | LIB_OBJS += util/wrapper.o |
| 362 | LIB_OBJS += util/sigchain.o | 399 | LIB_OBJS += util/sigchain.o |
| 363 | LIB_OBJS += util/symbol.o | 400 | LIB_OBJS += util/symbol.o |
| 364 | LIB_OBJS += util/module.o | ||
| 365 | LIB_OBJS += util/color.o | 401 | LIB_OBJS += util/color.o |
| 366 | LIB_OBJS += util/pager.o | 402 | LIB_OBJS += util/pager.o |
| 367 | LIB_OBJS += util/header.o | 403 | LIB_OBJS += util/header.o |
| @@ -373,18 +409,42 @@ LIB_OBJS += util/thread.o | |||
| 373 | LIB_OBJS += util/trace-event-parse.o | 409 | LIB_OBJS += util/trace-event-parse.o |
| 374 | LIB_OBJS += util/trace-event-read.o | 410 | LIB_OBJS += util/trace-event-read.o |
| 375 | LIB_OBJS += util/trace-event-info.o | 411 | LIB_OBJS += util/trace-event-info.o |
| 412 | LIB_OBJS += util/trace-event-perl.o | ||
| 413 | LIB_OBJS += util/svghelper.o | ||
| 414 | LIB_OBJS += util/sort.o | ||
| 415 | LIB_OBJS += util/hist.o | ||
| 416 | LIB_OBJS += util/data_map.o | ||
| 417 | LIB_OBJS += util/probe-event.o | ||
| 376 | 418 | ||
| 377 | BUILTIN_OBJS += builtin-annotate.o | 419 | BUILTIN_OBJS += builtin-annotate.o |
| 420 | |||
| 421 | BUILTIN_OBJS += builtin-bench.o | ||
| 422 | |||
| 423 | # Benchmark modules | ||
| 424 | BUILTIN_OBJS += bench/sched-messaging.o | ||
| 425 | BUILTIN_OBJS += bench/sched-pipe.o | ||
| 426 | BUILTIN_OBJS += bench/mem-memcpy.o | ||
| 427 | |||
| 378 | BUILTIN_OBJS += builtin-help.o | 428 | BUILTIN_OBJS += builtin-help.o |
| 429 | BUILTIN_OBJS += builtin-sched.o | ||
| 430 | BUILTIN_OBJS += builtin-buildid-list.o | ||
| 379 | BUILTIN_OBJS += builtin-list.o | 431 | BUILTIN_OBJS += builtin-list.o |
| 380 | BUILTIN_OBJS += builtin-record.o | 432 | BUILTIN_OBJS += builtin-record.o |
| 381 | BUILTIN_OBJS += builtin-report.o | 433 | BUILTIN_OBJS += builtin-report.o |
| 382 | BUILTIN_OBJS += builtin-stat.o | 434 | BUILTIN_OBJS += builtin-stat.o |
| 435 | BUILTIN_OBJS += builtin-timechart.o | ||
| 383 | BUILTIN_OBJS += builtin-top.o | 436 | BUILTIN_OBJS += builtin-top.o |
| 384 | BUILTIN_OBJS += builtin-trace.o | 437 | BUILTIN_OBJS += builtin-trace.o |
| 438 | BUILTIN_OBJS += builtin-probe.o | ||
| 439 | BUILTIN_OBJS += builtin-kmem.o | ||
| 385 | 440 | ||
| 386 | PERFLIBS = $(LIB_FILE) | 441 | PERFLIBS = $(LIB_FILE) |
| 387 | 442 | ||
| 443 | ifeq ($(V), 2) | ||
| 444 | QUIET_STDERR = ">/dev/null" | ||
| 445 | else | ||
| 446 | QUIET_STDERR = ">/dev/null 2>&1" | ||
| 447 | endif | ||
| 388 | # | 448 | # |
| 389 | # Platform specific tweaks | 449 | # Platform specific tweaks |
| 390 | # | 450 | # |
| @@ -412,32 +472,58 @@ ifeq ($(uname_S),Darwin) | |||
| 412 | PTHREAD_LIBS = | 472 | PTHREAD_LIBS = |
| 413 | endif | 473 | endif |
| 414 | 474 | ||
| 415 | ifneq ($(shell sh -c "(echo '\#include <libelf.h>'; echo 'int main(void) { Elf * elf = elf_begin(0, ELF_C_READ_MMAP, 0); return (long)elf; }') | $(CC) -x c - $(ALL_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -o /dev/null $(ALL_LDFLAGS) > /dev/null 2>&1 && echo y"), y) | 475 | ifeq ($(shell sh -c "(echo '\#include <libelf.h>'; echo 'int main(void) { Elf * elf = elf_begin(0, ELF_C_READ, 0); return (long)elf; }') | $(CC) -x c - $(ALL_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) "$(QUIET_STDERR)" && echo y"), y) |
| 416 | msg := $(error No libelf.h/libelf found, please install libelf-dev/elfutils-libelf-devel); | 476 | ifneq ($(shell sh -c "(echo '\#include <gnu/libc-version.h>'; echo 'int main(void) { const char * version = gnu_get_libc_version(); return (long)version; }') | $(CC) -x c - $(ALL_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) "$(QUIET_STDERR)" && echo y"), y) |
| 477 | msg := $(error No gnu/libc-version.h found, please install glibc-dev[el]/glibc-static); | ||
| 478 | endif | ||
| 479 | |||
| 480 | ifneq ($(shell sh -c "(echo '\#include <libelf.h>'; echo 'int main(void) { Elf * elf = elf_begin(0, ELF_C_READ_MMAP, 0); return (long)elf; }') | $(CC) -x c - $(ALL_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) "$(QUIET_STDERR)" && echo y"), y) | ||
| 481 | BASIC_CFLAGS += -DLIBELF_NO_MMAP | ||
| 482 | endif | ||
| 483 | else | ||
| 484 | msg := $(error No libelf.h/libelf found, please install libelf-dev/elfutils-libelf-devel and glibc-dev[el]); | ||
| 485 | endif | ||
| 486 | |||
| 487 | ifneq ($(shell sh -c "(echo '\#include <libdwarf/dwarf.h>'; echo '\#include <libdwarf/libdwarf.h>'; echo 'int main(void) { Dwarf_Debug dbg; Dwarf_Error err; Dwarf_Ranges *rng; dwarf_init(0, DW_DLC_READ, 0, 0, &dbg, &err); dwarf_get_ranges(dbg, 0, &rng, 0, 0, &err); return (long)dbg; }') | $(CC) -x c - $(ALL_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -ldwarf -lelf -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) "$(QUIET_STDERR)" && echo y"), y) | ||
| 488 | msg := $(warning No libdwarf.h found or old libdwarf.h found, disables dwarf support. Please install libdwarf-dev/libdwarf-devel >= 20081231); | ||
| 489 | BASIC_CFLAGS += -DNO_LIBDWARF | ||
| 490 | else | ||
| 491 | EXTLIBS += -lelf -ldwarf | ||
| 492 | LIB_OBJS += util/probe-finder.o | ||
| 493 | endif | ||
| 494 | |||
| 495 | PERL_EMBED_LDOPTS = `perl -MExtUtils::Embed -e ldopts 2>/dev/null` | ||
| 496 | PERL_EMBED_CCOPTS = `perl -MExtUtils::Embed -e ccopts 2>/dev/null` | ||
| 497 | |||
| 498 | ifneq ($(shell sh -c "(echo '\#include <EXTERN.h>'; echo '\#include <perl.h>'; echo 'int main(void) { perl_alloc(); return 0; }') | $(CC) -x c - $(PERL_EMBED_CCOPTS) -o /dev/null $(PERL_EMBED_LDOPTS) > /dev/null 2>&1 && echo y"), y) | ||
| 499 | BASIC_CFLAGS += -DNO_LIBPERL | ||
| 500 | else | ||
| 501 | ALL_LDFLAGS += $(PERL_EMBED_LDOPTS) | ||
| 502 | LIB_OBJS += scripts/perl/Perf-Trace-Util/Context.o | ||
| 417 | endif | 503 | endif |
| 418 | 504 | ||
| 419 | ifdef NO_DEMANGLE | 505 | ifdef NO_DEMANGLE |
| 420 | BASIC_CFLAGS += -DNO_DEMANGLE | 506 | BASIC_CFLAGS += -DNO_DEMANGLE |
| 421 | else | 507 | else |
| 422 | has_bfd := $(shell sh -c "(echo '\#include <bfd.h>'; echo 'int main(void) { bfd_demangle(0, 0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) -lbfd > /dev/null 2>&1 && echo y") | 508 | has_bfd := $(shell sh -c "(echo '\#include <bfd.h>'; echo 'int main(void) { bfd_demangle(0, 0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) -lbfd "$(QUIET_STDERR)" && echo y") |
| 423 | 509 | ||
| 424 | ifeq ($(has_bfd),y) | 510 | ifeq ($(has_bfd),y) |
| 425 | EXTLIBS += -lbfd | 511 | EXTLIBS += -lbfd |
| 426 | else | 512 | else |
| 427 | has_bfd_iberty := $(shell sh -c "(echo '\#include <bfd.h>'; echo 'int main(void) { bfd_demangle(0, 0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) -lbfd -liberty > /dev/null 2>&1 && echo y") | 513 | has_bfd_iberty := $(shell sh -c "(echo '\#include <bfd.h>'; echo 'int main(void) { bfd_demangle(0, 0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) -lbfd -liberty "$(QUIET_STDERR)" && echo y") |
| 428 | ifeq ($(has_bfd_iberty),y) | 514 | ifeq ($(has_bfd_iberty),y) |
| 429 | EXTLIBS += -lbfd -liberty | 515 | EXTLIBS += -lbfd -liberty |
| 430 | else | 516 | else |
| 431 | has_bfd_iberty_z := $(shell sh -c "(echo '\#include <bfd.h>'; echo 'int main(void) { bfd_demangle(0, 0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) -lbfd -liberty -lz > /dev/null 2>&1 && echo y") | 517 | has_bfd_iberty_z := $(shell sh -c "(echo '\#include <bfd.h>'; echo 'int main(void) { bfd_demangle(0, 0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) -lbfd -liberty -lz "$(QUIET_STDERR)" && echo y") |
| 432 | ifeq ($(has_bfd_iberty_z),y) | 518 | ifeq ($(has_bfd_iberty_z),y) |
| 433 | EXTLIBS += -lbfd -liberty -lz | 519 | EXTLIBS += -lbfd -liberty -lz |
| 434 | else | 520 | else |
| 435 | has_cplus_demangle := $(shell sh -c "(echo 'extern char *cplus_demangle(const char *, int);'; echo 'int main(void) { cplus_demangle(0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) -liberty > /dev/null 2>&1 && echo y") | 521 | has_cplus_demangle := $(shell sh -c "(echo 'extern char *cplus_demangle(const char *, int);'; echo 'int main(void) { cplus_demangle(0, 0); return 0; }') | $(CC) -x c - $(ALL_CFLAGS) -o /dev/null $(ALL_LDFLAGS) $(EXTLIBS) -liberty "$(QUIET_STDERR)" && echo y") |
| 436 | ifeq ($(has_cplus_demangle),y) | 522 | ifeq ($(has_cplus_demangle),y) |
| 437 | EXTLIBS += -liberty | 523 | EXTLIBS += -liberty |
| 438 | BASIC_CFLAGS += -DHAVE_CPLUS_DEMANGLE | 524 | BASIC_CFLAGS += -DHAVE_CPLUS_DEMANGLE |
| 439 | else | 525 | else |
| 440 | msg := $(warning No bfd.h/libbfd found, install binutils-dev[el] to gain symbol demangling) | 526 | msg := $(warning No bfd.h/libbfd found, install binutils-dev[el]/zlib-static to gain symbol demangling) |
| 441 | BASIC_CFLAGS += -DNO_DEMANGLE | 527 | BASIC_CFLAGS += -DNO_DEMANGLE |
| 442 | endif | 528 | endif |
| 443 | endif | 529 | endif |
| @@ -710,6 +796,12 @@ builtin-help.o: builtin-help.c common-cmds.h PERF-CFLAGS | |||
| 710 | '-DPERF_MAN_PATH="$(mandir_SQ)"' \ | 796 | '-DPERF_MAN_PATH="$(mandir_SQ)"' \ |
| 711 | '-DPERF_INFO_PATH="$(infodir_SQ)"' $< | 797 | '-DPERF_INFO_PATH="$(infodir_SQ)"' $< |
| 712 | 798 | ||
| 799 | builtin-timechart.o: builtin-timechart.c common-cmds.h PERF-CFLAGS | ||
| 800 | $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) \ | ||
| 801 | '-DPERF_HTML_PATH="$(htmldir_SQ)"' \ | ||
| 802 | '-DPERF_MAN_PATH="$(mandir_SQ)"' \ | ||
| 803 | '-DPERF_INFO_PATH="$(infodir_SQ)"' $< | ||
| 804 | |||
| 713 | $(BUILT_INS): perf$X | 805 | $(BUILT_INS): perf$X |
| 714 | $(QUIET_BUILT_IN)$(RM) $@ && \ | 806 | $(QUIET_BUILT_IN)$(RM) $@ && \ |
| 715 | ln perf$X $@ 2>/dev/null || \ | 807 | ln perf$X $@ 2>/dev/null || \ |
| @@ -719,7 +811,7 @@ $(BUILT_INS): perf$X | |||
| 719 | common-cmds.h: util/generate-cmdlist.sh command-list.txt | 811 | common-cmds.h: util/generate-cmdlist.sh command-list.txt |
| 720 | 812 | ||
| 721 | common-cmds.h: $(wildcard Documentation/perf-*.txt) | 813 | common-cmds.h: $(wildcard Documentation/perf-*.txt) |
| 722 | $(QUIET_GEN)util/generate-cmdlist.sh > $@+ && mv $@+ $@ | 814 | $(QUIET_GEN). util/generate-cmdlist.sh > $@+ && mv $@+ $@ |
| 723 | 815 | ||
| 724 | $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh | 816 | $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh |
| 725 | $(QUIET_GEN)$(RM) $@ $@+ && \ | 817 | $(QUIET_GEN)$(RM) $@ $@+ && \ |
| @@ -768,6 +860,25 @@ util/config.o: util/config.c PERF-CFLAGS | |||
| 768 | util/rbtree.o: ../../lib/rbtree.c PERF-CFLAGS | 860 | util/rbtree.o: ../../lib/rbtree.c PERF-CFLAGS |
| 769 | $(QUIET_CC)$(CC) -o util/rbtree.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< | 861 | $(QUIET_CC)$(CC) -o util/rbtree.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< |
| 770 | 862 | ||
| 863 | # some perf warning policies can't fit to lib/bitmap.c, eg: it warns about variable shadowing | ||
| 864 | # from <string.h> that comes from kernel headers wrapping. | ||
| 865 | KBITMAP_FLAGS=`echo $(ALL_CFLAGS) | sed s/-Wshadow// | sed s/-Wswitch-default// | sed s/-Wextra//` | ||
| 866 | |||
| 867 | util/bitmap.o: ../../lib/bitmap.c PERF-CFLAGS | ||
| 868 | $(QUIET_CC)$(CC) -o util/bitmap.o -c $(KBITMAP_FLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< | ||
| 869 | |||
| 870 | util/hweight.o: ../../lib/hweight.c PERF-CFLAGS | ||
| 871 | $(QUIET_CC)$(CC) -o util/hweight.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< | ||
| 872 | |||
| 873 | util/find_next_bit.o: ../../lib/find_next_bit.c PERF-CFLAGS | ||
| 874 | $(QUIET_CC)$(CC) -o util/find_next_bit.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< | ||
| 875 | |||
| 876 | util/trace-event-perl.o: util/trace-event-perl.c PERF-CFLAGS | ||
| 877 | $(QUIET_CC)$(CC) -o util/trace-event-perl.o -c $(ALL_CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow $< | ||
| 878 | |||
| 879 | scripts/perl/Perf-Trace-Util/Context.o: scripts/perl/Perf-Trace-Util/Context.c PERF-CFLAGS | ||
| 880 | $(QUIET_CC)$(CC) -o scripts/perl/Perf-Trace-Util/Context.o -c $(ALL_CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs $< | ||
| 881 | |||
| 771 | perf-%$X: %.o $(PERFLIBS) | 882 | perf-%$X: %.o $(PERFLIBS) |
| 772 | $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) | 883 | $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) |
| 773 | 884 | ||
| @@ -875,6 +986,13 @@ export perfexec_instdir | |||
| 875 | install: all | 986 | install: all |
| 876 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(bindir_SQ)' | 987 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(bindir_SQ)' |
| 877 | $(INSTALL) perf$X '$(DESTDIR_SQ)$(bindir_SQ)' | 988 | $(INSTALL) perf$X '$(DESTDIR_SQ)$(bindir_SQ)' |
| 989 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util/lib/Perf/Trace' | ||
| 990 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin' | ||
| 991 | $(INSTALL) scripts/perl/Perf-Trace-Util/lib/Perf/Trace/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util/lib/Perf/Trace' | ||
| 992 | $(INSTALL) scripts/perl/*.pl -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl' | ||
| 993 | $(INSTALL) scripts/perl/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin' | ||
| 994 | $(INSTALL) scripts/perl/Perf-Trace-Util/Makefile.PL -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util' | ||
| 995 | $(INSTALL) scripts/perl/Perf-Trace-Util/README -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util' | ||
| 878 | ifdef BUILT_INS | 996 | ifdef BUILT_INS |
| 879 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)' | 997 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)' |
| 880 | $(INSTALL) $(BUILT_INS) '$(DESTDIR_SQ)$(perfexec_instdir_SQ)' | 998 | $(INSTALL) $(BUILT_INS) '$(DESTDIR_SQ)$(perfexec_instdir_SQ)' |
| @@ -960,7 +1078,7 @@ distclean: clean | |||
| 960 | # $(RM) configure | 1078 | # $(RM) configure |
| 961 | 1079 | ||
| 962 | clean: | 1080 | clean: |
| 963 | $(RM) *.o */*.o $(LIB_FILE) | 1081 | $(RM) *.o */*.o */*/*.o */*/*/*.o $(LIB_FILE) |
| 964 | $(RM) $(ALL_PROGRAMS) $(BUILT_INS) perf$X | 1082 | $(RM) $(ALL_PROGRAMS) $(BUILT_INS) perf$X |
| 965 | $(RM) $(TEST_PROGRAMS) | 1083 | $(RM) $(TEST_PROGRAMS) |
| 966 | $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags cscope* | 1084 | $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags cscope* |
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h new file mode 100644 index 000000000000..f7781c6267c0 --- /dev/null +++ b/tools/perf/bench/bench.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef BENCH_H | ||
| 2 | #define BENCH_H | ||
| 3 | |||
| 4 | extern int bench_sched_messaging(int argc, const char **argv, const char *prefix); | ||
| 5 | extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); | ||
| 6 | extern int bench_mem_memcpy(int argc, const char **argv, const char *prefix __used); | ||
| 7 | |||
| 8 | #define BENCH_FORMAT_DEFAULT_STR "default" | ||
| 9 | #define BENCH_FORMAT_DEFAULT 0 | ||
| 10 | #define BENCH_FORMAT_SIMPLE_STR "simple" | ||
| 11 | #define BENCH_FORMAT_SIMPLE 1 | ||
| 12 | |||
| 13 | #define BENCH_FORMAT_UNKNOWN -1 | ||
| 14 | |||
| 15 | extern int bench_format; | ||
| 16 | |||
| 17 | #endif | ||
diff --git a/tools/perf/bench/mem-memcpy.c b/tools/perf/bench/mem-memcpy.c new file mode 100644 index 000000000000..89773178e894 --- /dev/null +++ b/tools/perf/bench/mem-memcpy.c | |||
| @@ -0,0 +1,193 @@ | |||
| 1 | /* | ||
| 2 | * mem-memcpy.c | ||
| 3 | * | ||
| 4 | * memcpy: Simple memory copy in various ways | ||
| 5 | * | ||
| 6 | * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | ||
| 7 | */ | ||
| 8 | #include <ctype.h> | ||
| 9 | |||
| 10 | #include "../perf.h" | ||
| 11 | #include "../util/util.h" | ||
| 12 | #include "../util/parse-options.h" | ||
| 13 | #include "../util/string.h" | ||
| 14 | #include "../util/header.h" | ||
| 15 | #include "bench.h" | ||
| 16 | |||
| 17 | #include <stdio.h> | ||
| 18 | #include <stdlib.h> | ||
| 19 | #include <string.h> | ||
| 20 | #include <sys/time.h> | ||
| 21 | #include <errno.h> | ||
| 22 | |||
| 23 | #define K 1024 | ||
| 24 | |||
| 25 | static const char *length_str = "1MB"; | ||
| 26 | static const char *routine = "default"; | ||
| 27 | static int use_clock = 0; | ||
| 28 | static int clock_fd; | ||
| 29 | |||
| 30 | static const struct option options[] = { | ||
| 31 | OPT_STRING('l', "length", &length_str, "1MB", | ||
| 32 | "Specify length of memory to copy. " | ||
| 33 | "available unit: B, MB, GB (upper and lower)"), | ||
| 34 | OPT_STRING('r', "routine", &routine, "default", | ||
| 35 | "Specify routine to copy"), | ||
| 36 | OPT_BOOLEAN('c', "clock", &use_clock, | ||
| 37 | "Use CPU clock for measuring"), | ||
| 38 | OPT_END() | ||
| 39 | }; | ||
| 40 | |||
| 41 | struct routine { | ||
| 42 | const char *name; | ||
| 43 | const char *desc; | ||
| 44 | void * (*fn)(void *dst, const void *src, size_t len); | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct routine routines[] = { | ||
| 48 | { "default", | ||
| 49 | "Default memcpy() provided by glibc", | ||
| 50 | memcpy }, | ||
| 51 | { NULL, | ||
| 52 | NULL, | ||
| 53 | NULL } | ||
| 54 | }; | ||
| 55 | |||
| 56 | static const char * const bench_mem_memcpy_usage[] = { | ||
| 57 | "perf bench mem memcpy <options>", | ||
| 58 | NULL | ||
| 59 | }; | ||
| 60 | |||
| 61 | static struct perf_event_attr clock_attr = { | ||
| 62 | .type = PERF_TYPE_HARDWARE, | ||
| 63 | .config = PERF_COUNT_HW_CPU_CYCLES | ||
| 64 | }; | ||
| 65 | |||
| 66 | static void init_clock(void) | ||
| 67 | { | ||
| 68 | clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0); | ||
| 69 | |||
| 70 | if (clock_fd < 0 && errno == ENOSYS) | ||
| 71 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); | ||
| 72 | else | ||
| 73 | BUG_ON(clock_fd < 0); | ||
| 74 | } | ||
| 75 | |||
| 76 | static u64 get_clock(void) | ||
| 77 | { | ||
| 78 | int ret; | ||
| 79 | u64 clk; | ||
| 80 | |||
| 81 | ret = read(clock_fd, &clk, sizeof(u64)); | ||
| 82 | BUG_ON(ret != sizeof(u64)); | ||
| 83 | |||
| 84 | return clk; | ||
| 85 | } | ||
| 86 | |||
| 87 | static double timeval2double(struct timeval *ts) | ||
| 88 | { | ||
| 89 | return (double)ts->tv_sec + | ||
| 90 | (double)ts->tv_usec / (double)1000000; | ||
| 91 | } | ||
| 92 | |||
| 93 | int bench_mem_memcpy(int argc, const char **argv, | ||
| 94 | const char *prefix __used) | ||
| 95 | { | ||
| 96 | int i; | ||
| 97 | void *dst, *src; | ||
| 98 | size_t length; | ||
| 99 | double bps = 0.0; | ||
| 100 | struct timeval tv_start, tv_end, tv_diff; | ||
| 101 | u64 clock_start, clock_end, clock_diff; | ||
| 102 | |||
| 103 | clock_start = clock_end = clock_diff = 0ULL; | ||
| 104 | argc = parse_options(argc, argv, options, | ||
| 105 | bench_mem_memcpy_usage, 0); | ||
| 106 | |||
| 107 | tv_diff.tv_sec = 0; | ||
| 108 | tv_diff.tv_usec = 0; | ||
| 109 | length = (size_t)perf_atoll((char *)length_str); | ||
| 110 | |||
| 111 | if ((s64)length <= 0) { | ||
| 112 | fprintf(stderr, "Invalid length:%s\n", length_str); | ||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | for (i = 0; routines[i].name; i++) { | ||
| 117 | if (!strcmp(routines[i].name, routine)) | ||
| 118 | break; | ||
| 119 | } | ||
| 120 | if (!routines[i].name) { | ||
| 121 | printf("Unknown routine:%s\n", routine); | ||
| 122 | printf("Available routines...\n"); | ||
| 123 | for (i = 0; routines[i].name; i++) { | ||
| 124 | printf("\t%s ... %s\n", | ||
| 125 | routines[i].name, routines[i].desc); | ||
| 126 | } | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | dst = zalloc(length); | ||
| 131 | if (!dst) | ||
| 132 | die("memory allocation failed - maybe length is too large?\n"); | ||
| 133 | |||
| 134 | src = zalloc(length); | ||
| 135 | if (!src) | ||
| 136 | die("memory allocation failed - maybe length is too large?\n"); | ||
| 137 | |||
| 138 | if (bench_format == BENCH_FORMAT_DEFAULT) { | ||
| 139 | printf("# Copying %s Bytes from %p to %p ...\n\n", | ||
| 140 | length_str, src, dst); | ||
| 141 | } | ||
| 142 | |||
| 143 | if (use_clock) { | ||
| 144 | init_clock(); | ||
| 145 | clock_start = get_clock(); | ||
| 146 | } else { | ||
| 147 | BUG_ON(gettimeofday(&tv_start, NULL)); | ||
| 148 | } | ||
| 149 | |||
| 150 | routines[i].fn(dst, src, length); | ||
| 151 | |||
| 152 | if (use_clock) { | ||
| 153 | clock_end = get_clock(); | ||
| 154 | clock_diff = clock_end - clock_start; | ||
| 155 | } else { | ||
| 156 | BUG_ON(gettimeofday(&tv_end, NULL)); | ||
| 157 | timersub(&tv_end, &tv_start, &tv_diff); | ||
| 158 | bps = (double)((double)length / timeval2double(&tv_diff)); | ||
| 159 | } | ||
| 160 | |||
| 161 | switch (bench_format) { | ||
| 162 | case BENCH_FORMAT_DEFAULT: | ||
| 163 | if (use_clock) { | ||
| 164 | printf(" %14lf Clock/Byte\n", | ||
| 165 | (double)clock_diff / (double)length); | ||
| 166 | } else { | ||
| 167 | if (bps < K) | ||
| 168 | printf(" %14lf B/Sec\n", bps); | ||
| 169 | else if (bps < K * K) | ||
| 170 | printf(" %14lfd KB/Sec\n", bps / 1024); | ||
| 171 | else if (bps < K * K * K) | ||
| 172 | printf(" %14lf MB/Sec\n", bps / 1024 / 1024); | ||
| 173 | else { | ||
| 174 | printf(" %14lf GB/Sec\n", | ||
| 175 | bps / 1024 / 1024 / 1024); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | break; | ||
| 179 | case BENCH_FORMAT_SIMPLE: | ||
| 180 | if (use_clock) { | ||
| 181 | printf("%14lf\n", | ||
| 182 | (double)clock_diff / (double)length); | ||
| 183 | } else | ||
| 184 | printf("%lf\n", bps); | ||
| 185 | break; | ||
| 186 | default: | ||
| 187 | /* reaching this means there's some disaster: */ | ||
| 188 | die("unknown format: %d\n", bench_format); | ||
| 189 | break; | ||
| 190 | } | ||
| 191 | |||
| 192 | return 0; | ||
| 193 | } | ||
diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c new file mode 100644 index 000000000000..605a2a959aa8 --- /dev/null +++ b/tools/perf/bench/sched-messaging.c | |||
| @@ -0,0 +1,336 @@ | |||
| 1 | /* | ||
| 2 | * | ||
| 3 | * builtin-bench-messaging.c | ||
| 4 | * | ||
| 5 | * messaging: Benchmark for scheduler and IPC mechanisms | ||
| 6 | * | ||
| 7 | * Based on hackbench by Rusty Russell <rusty@rustcorp.com.au> | ||
| 8 | * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | ||
| 9 | * | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../perf.h" | ||
| 13 | #include "../util/util.h" | ||
| 14 | #include "../util/parse-options.h" | ||
| 15 | #include "../builtin.h" | ||
| 16 | #include "bench.h" | ||
| 17 | |||
| 18 | /* Test groups of 20 processes spraying to 20 receivers */ | ||
| 19 | #include <pthread.h> | ||
| 20 | #include <stdio.h> | ||
| 21 | #include <stdlib.h> | ||
| 22 | #include <string.h> | ||
| 23 | #include <errno.h> | ||
| 24 | #include <unistd.h> | ||
| 25 | #include <sys/types.h> | ||
| 26 | #include <sys/socket.h> | ||
| 27 | #include <sys/wait.h> | ||
| 28 | #include <sys/time.h> | ||
| 29 | #include <sys/poll.h> | ||
| 30 | #include <limits.h> | ||
| 31 | |||
| 32 | #define DATASIZE 100 | ||
| 33 | |||
| 34 | static int use_pipes = 0; | ||
| 35 | static unsigned int loops = 100; | ||
| 36 | static unsigned int thread_mode = 0; | ||
| 37 | static unsigned int num_groups = 10; | ||
| 38 | |||
| 39 | struct sender_context { | ||
| 40 | unsigned int num_fds; | ||
| 41 | int ready_out; | ||
| 42 | int wakefd; | ||
| 43 | int out_fds[0]; | ||
| 44 | }; | ||
| 45 | |||
| 46 | struct receiver_context { | ||
| 47 | unsigned int num_packets; | ||
| 48 | int in_fds[2]; | ||
| 49 | int ready_out; | ||
| 50 | int wakefd; | ||
| 51 | }; | ||
| 52 | |||
| 53 | static void barf(const char *msg) | ||
| 54 | { | ||
| 55 | fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno)); | ||
| 56 | exit(1); | ||
| 57 | } | ||
| 58 | |||
| 59 | static void fdpair(int fds[2]) | ||
| 60 | { | ||
| 61 | if (use_pipes) { | ||
| 62 | if (pipe(fds) == 0) | ||
| 63 | return; | ||
| 64 | } else { | ||
| 65 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0) | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | barf(use_pipes ? "pipe()" : "socketpair()"); | ||
| 70 | } | ||
| 71 | |||
| 72 | /* Block until we're ready to go */ | ||
| 73 | static void ready(int ready_out, int wakefd) | ||
| 74 | { | ||
| 75 | char dummy; | ||
| 76 | struct pollfd pollfd = { .fd = wakefd, .events = POLLIN }; | ||
| 77 | |||
| 78 | /* Tell them we're ready. */ | ||
| 79 | if (write(ready_out, &dummy, 1) != 1) | ||
| 80 | barf("CLIENT: ready write"); | ||
| 81 | |||
| 82 | /* Wait for "GO" signal */ | ||
| 83 | if (poll(&pollfd, 1, -1) != 1) | ||
| 84 | barf("poll"); | ||
| 85 | } | ||
| 86 | |||
| 87 | /* Sender sprays loops messages down each file descriptor */ | ||
| 88 | static void *sender(struct sender_context *ctx) | ||
| 89 | { | ||
| 90 | char data[DATASIZE]; | ||
| 91 | unsigned int i, j; | ||
| 92 | |||
| 93 | ready(ctx->ready_out, ctx->wakefd); | ||
| 94 | |||
| 95 | /* Now pump to every receiver. */ | ||
| 96 | for (i = 0; i < loops; i++) { | ||
| 97 | for (j = 0; j < ctx->num_fds; j++) { | ||
| 98 | int ret, done = 0; | ||
| 99 | |||
| 100 | again: | ||
| 101 | ret = write(ctx->out_fds[j], data + done, | ||
| 102 | sizeof(data)-done); | ||
| 103 | if (ret < 0) | ||
| 104 | barf("SENDER: write"); | ||
| 105 | done += ret; | ||
| 106 | if (done < DATASIZE) | ||
| 107 | goto again; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | return NULL; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | /* One receiver per fd */ | ||
| 116 | static void *receiver(struct receiver_context* ctx) | ||
| 117 | { | ||
| 118 | unsigned int i; | ||
| 119 | |||
| 120 | if (!thread_mode) | ||
| 121 | close(ctx->in_fds[1]); | ||
| 122 | |||
| 123 | /* Wait for start... */ | ||
| 124 | ready(ctx->ready_out, ctx->wakefd); | ||
| 125 | |||
| 126 | /* Receive them all */ | ||
| 127 | for (i = 0; i < ctx->num_packets; i++) { | ||
| 128 | char data[DATASIZE]; | ||
| 129 | int ret, done = 0; | ||
| 130 | |||
| 131 | again: | ||
| 132 | ret = read(ctx->in_fds[0], data + done, DATASIZE - done); | ||
| 133 | if (ret < 0) | ||
| 134 | barf("SERVER: read"); | ||
| 135 | done += ret; | ||
| 136 | if (done < DATASIZE) | ||
| 137 | goto again; | ||
| 138 | } | ||
| 139 | |||
| 140 | return NULL; | ||
| 141 | } | ||
| 142 | |||
| 143 | static pthread_t create_worker(void *ctx, void *(*func)(void *)) | ||
| 144 | { | ||
| 145 | pthread_attr_t attr; | ||
| 146 | pthread_t childid; | ||
| 147 | int err; | ||
| 148 | |||
| 149 | if (!thread_mode) { | ||
| 150 | /* process mode */ | ||
| 151 | /* Fork the receiver. */ | ||
| 152 | switch (fork()) { | ||
| 153 | case -1: | ||
| 154 | barf("fork()"); | ||
| 155 | break; | ||
| 156 | case 0: | ||
| 157 | (*func) (ctx); | ||
| 158 | exit(0); | ||
| 159 | break; | ||
| 160 | default: | ||
| 161 | break; | ||
| 162 | } | ||
| 163 | |||
| 164 | return (pthread_t)0; | ||
| 165 | } | ||
| 166 | |||
| 167 | if (pthread_attr_init(&attr) != 0) | ||
| 168 | barf("pthread_attr_init:"); | ||
| 169 | |||
| 170 | #ifndef __ia64__ | ||
| 171 | if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0) | ||
| 172 | barf("pthread_attr_setstacksize"); | ||
| 173 | #endif | ||
| 174 | |||
| 175 | err = pthread_create(&childid, &attr, func, ctx); | ||
| 176 | if (err != 0) { | ||
| 177 | fprintf(stderr, "pthread_create failed: %s (%d)\n", | ||
| 178 | strerror(err), err); | ||
| 179 | exit(-1); | ||
| 180 | } | ||
| 181 | return childid; | ||
| 182 | } | ||
| 183 | |||
| 184 | static void reap_worker(pthread_t id) | ||
| 185 | { | ||
| 186 | int proc_status; | ||
| 187 | void *thread_status; | ||
| 188 | |||
| 189 | if (!thread_mode) { | ||
| 190 | /* process mode */ | ||
| 191 | wait(&proc_status); | ||
| 192 | if (!WIFEXITED(proc_status)) | ||
| 193 | exit(1); | ||
| 194 | } else { | ||
| 195 | pthread_join(id, &thread_status); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | /* One group of senders and receivers */ | ||
| 200 | static unsigned int group(pthread_t *pth, | ||
| 201 | unsigned int num_fds, | ||
| 202 | int ready_out, | ||
| 203 | int wakefd) | ||
| 204 | { | ||
| 205 | unsigned int i; | ||
| 206 | struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) | ||
| 207 | + num_fds * sizeof(int)); | ||
| 208 | |||
| 209 | if (!snd_ctx) | ||
| 210 | barf("malloc()"); | ||
| 211 | |||
| 212 | for (i = 0; i < num_fds; i++) { | ||
| 213 | int fds[2]; | ||
| 214 | struct receiver_context *ctx = malloc(sizeof(*ctx)); | ||
| 215 | |||
| 216 | if (!ctx) | ||
| 217 | barf("malloc()"); | ||
| 218 | |||
| 219 | |||
| 220 | /* Create the pipe between client and server */ | ||
| 221 | fdpair(fds); | ||
| 222 | |||
| 223 | ctx->num_packets = num_fds * loops; | ||
| 224 | ctx->in_fds[0] = fds[0]; | ||
| 225 | ctx->in_fds[1] = fds[1]; | ||
| 226 | ctx->ready_out = ready_out; | ||
| 227 | ctx->wakefd = wakefd; | ||
| 228 | |||
| 229 | pth[i] = create_worker(ctx, (void *)receiver); | ||
| 230 | |||
| 231 | snd_ctx->out_fds[i] = fds[1]; | ||
| 232 | if (!thread_mode) | ||
| 233 | close(fds[0]); | ||
| 234 | } | ||
| 235 | |||
| 236 | /* Now we have all the fds, fork the senders */ | ||
| 237 | for (i = 0; i < num_fds; i++) { | ||
| 238 | snd_ctx->ready_out = ready_out; | ||
| 239 | snd_ctx->wakefd = wakefd; | ||
| 240 | snd_ctx->num_fds = num_fds; | ||
| 241 | |||
| 242 | pth[num_fds+i] = create_worker(snd_ctx, (void *)sender); | ||
| 243 | } | ||
| 244 | |||
| 245 | /* Close the fds we have left */ | ||
| 246 | if (!thread_mode) | ||
| 247 | for (i = 0; i < num_fds; i++) | ||
| 248 | close(snd_ctx->out_fds[i]); | ||
| 249 | |||
| 250 | /* Return number of children to reap */ | ||
| 251 | return num_fds * 2; | ||
| 252 | } | ||
| 253 | |||
| 254 | static const struct option options[] = { | ||
| 255 | OPT_BOOLEAN('p', "pipe", &use_pipes, | ||
| 256 | "Use pipe() instead of socketpair()"), | ||
| 257 | OPT_BOOLEAN('t', "thread", &thread_mode, | ||
| 258 | "Be multi thread instead of multi process"), | ||
| 259 | OPT_INTEGER('g', "group", &num_groups, | ||
| 260 | "Specify number of groups"), | ||
| 261 | OPT_INTEGER('l', "loop", &loops, | ||
| 262 | "Specify number of loops"), | ||
| 263 | OPT_END() | ||
| 264 | }; | ||
| 265 | |||
| 266 | static const char * const bench_sched_message_usage[] = { | ||
| 267 | "perf bench sched messaging <options>", | ||
| 268 | NULL | ||
| 269 | }; | ||
| 270 | |||
| 271 | int bench_sched_messaging(int argc, const char **argv, | ||
| 272 | const char *prefix __used) | ||
| 273 | { | ||
| 274 | unsigned int i, total_children; | ||
| 275 | struct timeval start, stop, diff; | ||
| 276 | unsigned int num_fds = 20; | ||
| 277 | int readyfds[2], wakefds[2]; | ||
| 278 | char dummy; | ||
| 279 | pthread_t *pth_tab; | ||
| 280 | |||
| 281 | argc = parse_options(argc, argv, options, | ||
| 282 | bench_sched_message_usage, 0); | ||
| 283 | |||
| 284 | pth_tab = malloc(num_fds * 2 * num_groups * sizeof(pthread_t)); | ||
| 285 | if (!pth_tab) | ||
| 286 | barf("main:malloc()"); | ||
| 287 | |||
| 288 | fdpair(readyfds); | ||
| 289 | fdpair(wakefds); | ||
| 290 | |||
| 291 | total_children = 0; | ||
| 292 | for (i = 0; i < num_groups; i++) | ||
| 293 | total_children += group(pth_tab+total_children, num_fds, | ||
| 294 | readyfds[1], wakefds[0]); | ||
| 295 | |||
| 296 | /* Wait for everyone to be ready */ | ||
| 297 | for (i = 0; i < total_children; i++) | ||
| 298 | if (read(readyfds[0], &dummy, 1) != 1) | ||
| 299 | barf("Reading for readyfds"); | ||
| 300 | |||
| 301 | gettimeofday(&start, NULL); | ||
| 302 | |||
| 303 | /* Kick them off */ | ||
| 304 | if (write(wakefds[1], &dummy, 1) != 1) | ||
| 305 | barf("Writing to start them"); | ||
| 306 | |||
| 307 | /* Reap them all */ | ||
| 308 | for (i = 0; i < total_children; i++) | ||
| 309 | reap_worker(pth_tab[i]); | ||
| 310 | |||
| 311 | gettimeofday(&stop, NULL); | ||
| 312 | |||
| 313 | timersub(&stop, &start, &diff); | ||
| 314 | |||
| 315 | switch (bench_format) { | ||
| 316 | case BENCH_FORMAT_DEFAULT: | ||
| 317 | printf("# %d sender and receiver %s per group\n", | ||
| 318 | num_fds, thread_mode ? "threads" : "processes"); | ||
| 319 | printf("# %d groups == %d %s run\n\n", | ||
| 320 | num_groups, num_groups * 2 * num_fds, | ||
| 321 | thread_mode ? "threads" : "processes"); | ||
| 322 | printf(" %14s: %lu.%03lu [sec]\n", "Total time", | ||
| 323 | diff.tv_sec, diff.tv_usec/1000); | ||
| 324 | break; | ||
| 325 | case BENCH_FORMAT_SIMPLE: | ||
| 326 | printf("%lu.%03lu\n", diff.tv_sec, diff.tv_usec/1000); | ||
| 327 | break; | ||
| 328 | default: | ||
| 329 | /* reaching here is something disaster */ | ||
| 330 | fprintf(stderr, "Unknown format:%d\n", bench_format); | ||
| 331 | exit(1); | ||
| 332 | break; | ||
| 333 | } | ||
| 334 | |||
| 335 | return 0; | ||
| 336 | } | ||
diff --git a/tools/perf/bench/sched-pipe.c b/tools/perf/bench/sched-pipe.c new file mode 100644 index 000000000000..238185f97977 --- /dev/null +++ b/tools/perf/bench/sched-pipe.c | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | /* | ||
| 2 | * | ||
| 3 | * builtin-bench-pipe.c | ||
| 4 | * | ||
| 5 | * pipe: Benchmark for pipe() | ||
| 6 | * | ||
| 7 | * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com> | ||
| 8 | * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c | ||
| 9 | * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | ||
| 10 | * | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "../perf.h" | ||
| 14 | #include "../util/util.h" | ||
| 15 | #include "../util/parse-options.h" | ||
| 16 | #include "../builtin.h" | ||
| 17 | #include "bench.h" | ||
| 18 | |||
| 19 | #include <unistd.h> | ||
| 20 | #include <stdio.h> | ||
| 21 | #include <stdlib.h> | ||
| 22 | #include <signal.h> | ||
| 23 | #include <sys/wait.h> | ||
| 24 | #include <linux/unistd.h> | ||
| 25 | #include <string.h> | ||
| 26 | #include <errno.h> | ||
| 27 | #include <assert.h> | ||
| 28 | #include <sys/time.h> | ||
| 29 | #include <sys/types.h> | ||
| 30 | |||
| 31 | #define LOOPS_DEFAULT 1000000 | ||
| 32 | static int loops = LOOPS_DEFAULT; | ||
| 33 | |||
| 34 | static const struct option options[] = { | ||
| 35 | OPT_INTEGER('l', "loop", &loops, | ||
| 36 | "Specify number of loops"), | ||
| 37 | OPT_END() | ||
| 38 | }; | ||
| 39 | |||
| 40 | static const char * const bench_sched_pipe_usage[] = { | ||
| 41 | "perf bench sched pipe <options>", | ||
| 42 | NULL | ||
| 43 | }; | ||
| 44 | |||
| 45 | int bench_sched_pipe(int argc, const char **argv, | ||
| 46 | const char *prefix __used) | ||
| 47 | { | ||
| 48 | int pipe_1[2], pipe_2[2]; | ||
| 49 | int m = 0, i; | ||
| 50 | struct timeval start, stop, diff; | ||
| 51 | unsigned long long result_usec = 0; | ||
| 52 | |||
| 53 | /* | ||
| 54 | * why does "ret" exist? | ||
| 55 | * discarding returned value of read(), write() | ||
| 56 | * causes error in building environment for perf | ||
| 57 | */ | ||
| 58 | int ret, wait_stat; | ||
| 59 | pid_t pid, retpid; | ||
| 60 | |||
| 61 | argc = parse_options(argc, argv, options, | ||
| 62 | bench_sched_pipe_usage, 0); | ||
| 63 | |||
| 64 | assert(!pipe(pipe_1)); | ||
| 65 | assert(!pipe(pipe_2)); | ||
| 66 | |||
| 67 | pid = fork(); | ||
| 68 | assert(pid >= 0); | ||
| 69 | |||
| 70 | gettimeofday(&start, NULL); | ||
| 71 | |||
| 72 | if (!pid) { | ||
| 73 | for (i = 0; i < loops; i++) { | ||
| 74 | ret = read(pipe_1[0], &m, sizeof(int)); | ||
| 75 | ret = write(pipe_2[1], &m, sizeof(int)); | ||
| 76 | } | ||
| 77 | } else { | ||
| 78 | for (i = 0; i < loops; i++) { | ||
| 79 | ret = write(pipe_1[1], &m, sizeof(int)); | ||
| 80 | ret = read(pipe_2[0], &m, sizeof(int)); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | gettimeofday(&stop, NULL); | ||
| 85 | timersub(&stop, &start, &diff); | ||
| 86 | |||
| 87 | if (pid) { | ||
| 88 | retpid = waitpid(pid, &wait_stat, 0); | ||
| 89 | assert((retpid == pid) && WIFEXITED(wait_stat)); | ||
| 90 | return 0; | ||
| 91 | } | ||
| 92 | |||
| 93 | switch (bench_format) { | ||
| 94 | case BENCH_FORMAT_DEFAULT: | ||
| 95 | printf("# Extecuted %d pipe operations between two tasks\n\n", | ||
| 96 | loops); | ||
| 97 | |||
| 98 | result_usec = diff.tv_sec * 1000000; | ||
| 99 | result_usec += diff.tv_usec; | ||
| 100 | |||
| 101 | printf(" %14s: %lu.%03lu [sec]\n\n", "Total time", | ||
| 102 | diff.tv_sec, diff.tv_usec/1000); | ||
| 103 | |||
| 104 | printf(" %14lf usecs/op\n", | ||
| 105 | (double)result_usec / (double)loops); | ||
| 106 | printf(" %14d ops/sec\n", | ||
| 107 | (int)((double)loops / | ||
| 108 | ((double)result_usec / (double)1000000))); | ||
| 109 | break; | ||
| 110 | |||
| 111 | case BENCH_FORMAT_SIMPLE: | ||
| 112 | printf("%lu.%03lu\n", | ||
| 113 | diff.tv_sec, diff.tv_usec / 1000); | ||
| 114 | break; | ||
| 115 | |||
| 116 | default: | ||
| 117 | /* reaching here is something disaster */ | ||
| 118 | fprintf(stderr, "Unknown format:%d\n", bench_format); | ||
| 119 | exit(1); | ||
| 120 | break; | ||
| 121 | } | ||
| 122 | |||
| 123 | return 0; | ||
| 124 | } | ||
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c index 043d85b7e254..0bf2e8f9af57 100644 --- a/tools/perf/builtin-annotate.c +++ b/tools/perf/builtin-annotate.c | |||
| @@ -19,29 +19,26 @@ | |||
| 19 | #include "perf.h" | 19 | #include "perf.h" |
| 20 | #include "util/debug.h" | 20 | #include "util/debug.h" |
| 21 | 21 | ||
| 22 | #include "util/event.h" | ||
| 22 | #include "util/parse-options.h" | 23 | #include "util/parse-options.h" |
| 23 | #include "util/parse-events.h" | 24 | #include "util/parse-events.h" |
| 24 | #include "util/thread.h" | 25 | #include "util/thread.h" |
| 26 | #include "util/sort.h" | ||
| 27 | #include "util/hist.h" | ||
| 28 | #include "util/data_map.h" | ||
| 25 | 29 | ||
| 26 | static char const *input_name = "perf.data"; | 30 | static char const *input_name = "perf.data"; |
| 27 | 31 | ||
| 28 | static char default_sort_order[] = "comm,symbol"; | ||
| 29 | static char *sort_order = default_sort_order; | ||
| 30 | |||
| 31 | static int force; | 32 | static int force; |
| 32 | static int input; | ||
| 33 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; | ||
| 34 | 33 | ||
| 35 | static int full_paths; | 34 | static int full_paths; |
| 36 | 35 | ||
| 37 | static int print_line; | 36 | static int print_line; |
| 38 | 37 | ||
| 39 | static unsigned long page_size; | 38 | struct sym_hist { |
| 40 | static unsigned long mmap_window = 32; | 39 | u64 sum; |
| 41 | 40 | u64 ip[0]; | |
| 42 | static struct rb_root threads; | 41 | }; |
| 43 | static struct thread *last_match; | ||
| 44 | |||
| 45 | 42 | ||
| 46 | struct sym_ext { | 43 | struct sym_ext { |
| 47 | struct rb_node node; | 44 | struct rb_node node; |
| @@ -49,247 +46,38 @@ struct sym_ext { | |||
| 49 | char *path; | 46 | char *path; |
| 50 | }; | 47 | }; |
| 51 | 48 | ||
| 52 | /* | 49 | struct sym_priv { |
| 53 | * histogram, sorted on item, collects counts | 50 | struct sym_hist *hist; |
| 54 | */ | 51 | struct sym_ext *ext; |
| 55 | |||
| 56 | static struct rb_root hist; | ||
| 57 | |||
| 58 | struct hist_entry { | ||
| 59 | struct rb_node rb_node; | ||
| 60 | |||
| 61 | struct thread *thread; | ||
| 62 | struct map *map; | ||
| 63 | struct dso *dso; | ||
| 64 | struct symbol *sym; | ||
| 65 | u64 ip; | ||
| 66 | char level; | ||
| 67 | |||
| 68 | uint32_t count; | ||
| 69 | }; | ||
| 70 | |||
| 71 | /* | ||
| 72 | * configurable sorting bits | ||
| 73 | */ | ||
| 74 | |||
| 75 | struct sort_entry { | ||
| 76 | struct list_head list; | ||
| 77 | |||
| 78 | const char *header; | ||
| 79 | |||
| 80 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); | ||
| 81 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); | ||
| 82 | size_t (*print)(FILE *fp, struct hist_entry *); | ||
| 83 | }; | ||
| 84 | |||
| 85 | /* --sort pid */ | ||
| 86 | |||
| 87 | static int64_t | ||
| 88 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 89 | { | ||
| 90 | return right->thread->pid - left->thread->pid; | ||
| 91 | } | ||
| 92 | |||
| 93 | static size_t | ||
| 94 | sort__thread_print(FILE *fp, struct hist_entry *self) | ||
| 95 | { | ||
| 96 | return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid); | ||
| 97 | } | ||
| 98 | |||
| 99 | static struct sort_entry sort_thread = { | ||
| 100 | .header = " Command: Pid", | ||
| 101 | .cmp = sort__thread_cmp, | ||
| 102 | .print = sort__thread_print, | ||
| 103 | }; | ||
| 104 | |||
| 105 | /* --sort comm */ | ||
| 106 | |||
| 107 | static int64_t | ||
| 108 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 109 | { | ||
| 110 | return right->thread->pid - left->thread->pid; | ||
| 111 | } | ||
| 112 | |||
| 113 | static int64_t | ||
| 114 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) | ||
| 115 | { | ||
| 116 | char *comm_l = left->thread->comm; | ||
| 117 | char *comm_r = right->thread->comm; | ||
| 118 | |||
| 119 | if (!comm_l || !comm_r) { | ||
| 120 | if (!comm_l && !comm_r) | ||
| 121 | return 0; | ||
| 122 | else if (!comm_l) | ||
| 123 | return -1; | ||
| 124 | else | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | return strcmp(comm_l, comm_r); | ||
| 129 | } | ||
| 130 | |||
| 131 | static size_t | ||
| 132 | sort__comm_print(FILE *fp, struct hist_entry *self) | ||
| 133 | { | ||
| 134 | return fprintf(fp, "%16s", self->thread->comm); | ||
| 135 | } | ||
| 136 | |||
| 137 | static struct sort_entry sort_comm = { | ||
| 138 | .header = " Command", | ||
| 139 | .cmp = sort__comm_cmp, | ||
| 140 | .collapse = sort__comm_collapse, | ||
| 141 | .print = sort__comm_print, | ||
| 142 | }; | ||
| 143 | |||
| 144 | /* --sort dso */ | ||
| 145 | |||
| 146 | static int64_t | ||
| 147 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 148 | { | ||
| 149 | struct dso *dso_l = left->dso; | ||
| 150 | struct dso *dso_r = right->dso; | ||
| 151 | |||
| 152 | if (!dso_l || !dso_r) { | ||
| 153 | if (!dso_l && !dso_r) | ||
| 154 | return 0; | ||
| 155 | else if (!dso_l) | ||
| 156 | return -1; | ||
| 157 | else | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | return strcmp(dso_l->name, dso_r->name); | ||
| 162 | } | ||
| 163 | |||
| 164 | static size_t | ||
| 165 | sort__dso_print(FILE *fp, struct hist_entry *self) | ||
| 166 | { | ||
| 167 | if (self->dso) | ||
| 168 | return fprintf(fp, "%-25s", self->dso->name); | ||
| 169 | |||
| 170 | return fprintf(fp, "%016llx ", (u64)self->ip); | ||
| 171 | } | ||
| 172 | |||
| 173 | static struct sort_entry sort_dso = { | ||
| 174 | .header = "Shared Object ", | ||
| 175 | .cmp = sort__dso_cmp, | ||
| 176 | .print = sort__dso_print, | ||
| 177 | }; | ||
| 178 | |||
| 179 | /* --sort symbol */ | ||
| 180 | |||
| 181 | static int64_t | ||
| 182 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 183 | { | ||
| 184 | u64 ip_l, ip_r; | ||
| 185 | |||
| 186 | if (left->sym == right->sym) | ||
| 187 | return 0; | ||
| 188 | |||
| 189 | ip_l = left->sym ? left->sym->start : left->ip; | ||
| 190 | ip_r = right->sym ? right->sym->start : right->ip; | ||
| 191 | |||
| 192 | return (int64_t)(ip_r - ip_l); | ||
| 193 | } | ||
| 194 | |||
| 195 | static size_t | ||
| 196 | sort__sym_print(FILE *fp, struct hist_entry *self) | ||
| 197 | { | ||
| 198 | size_t ret = 0; | ||
| 199 | |||
| 200 | if (verbose) | ||
| 201 | ret += fprintf(fp, "%#018llx ", (u64)self->ip); | ||
| 202 | |||
| 203 | if (self->sym) { | ||
| 204 | ret += fprintf(fp, "[%c] %s", | ||
| 205 | self->dso == kernel_dso ? 'k' : '.', self->sym->name); | ||
| 206 | } else { | ||
| 207 | ret += fprintf(fp, "%#016llx", (u64)self->ip); | ||
| 208 | } | ||
| 209 | |||
| 210 | return ret; | ||
| 211 | } | ||
| 212 | |||
| 213 | static struct sort_entry sort_sym = { | ||
| 214 | .header = "Symbol", | ||
| 215 | .cmp = sort__sym_cmp, | ||
| 216 | .print = sort__sym_print, | ||
| 217 | }; | ||
| 218 | |||
| 219 | static int sort__need_collapse = 0; | ||
| 220 | |||
| 221 | struct sort_dimension { | ||
| 222 | const char *name; | ||
| 223 | struct sort_entry *entry; | ||
| 224 | int taken; | ||
| 225 | }; | 52 | }; |
| 226 | 53 | ||
| 227 | static struct sort_dimension sort_dimensions[] = { | 54 | static struct symbol_conf symbol_conf = { |
| 228 | { .name = "pid", .entry = &sort_thread, }, | 55 | .priv_size = sizeof(struct sym_priv), |
| 229 | { .name = "comm", .entry = &sort_comm, }, | 56 | .try_vmlinux_path = true, |
| 230 | { .name = "dso", .entry = &sort_dso, }, | ||
| 231 | { .name = "symbol", .entry = &sort_sym, }, | ||
| 232 | }; | 57 | }; |
| 233 | 58 | ||
| 234 | static LIST_HEAD(hist_entry__sort_list); | 59 | static const char *sym_hist_filter; |
| 235 | 60 | ||
| 236 | static int sort_dimension__add(char *tok) | 61 | static int symbol_filter(struct map *map __used, struct symbol *sym) |
| 237 | { | 62 | { |
| 238 | unsigned int i; | 63 | if (sym_hist_filter == NULL || |
| 239 | 64 | strcmp(sym->name, sym_hist_filter) == 0) { | |
| 240 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { | 65 | struct sym_priv *priv = symbol__priv(sym); |
| 241 | struct sort_dimension *sd = &sort_dimensions[i]; | 66 | const int size = (sizeof(*priv->hist) + |
| 242 | 67 | (sym->end - sym->start) * sizeof(u64)); | |
| 243 | if (sd->taken) | ||
| 244 | continue; | ||
| 245 | |||
| 246 | if (strncasecmp(tok, sd->name, strlen(tok))) | ||
| 247 | continue; | ||
| 248 | |||
| 249 | if (sd->entry->collapse) | ||
| 250 | sort__need_collapse = 1; | ||
| 251 | |||
| 252 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); | ||
| 253 | sd->taken = 1; | ||
| 254 | 68 | ||
| 69 | priv->hist = malloc(size); | ||
| 70 | if (priv->hist) | ||
| 71 | memset(priv->hist, 0, size); | ||
| 255 | return 0; | 72 | return 0; |
| 256 | } | 73 | } |
| 257 | 74 | /* | |
| 258 | return -ESRCH; | 75 | * FIXME: We should really filter it out, as we don't want to go thru symbols |
| 259 | } | 76 | * we're not interested, and if a DSO ends up with no symbols, delete it too, |
| 260 | 77 | * but right now the kernel loading routines in symbol.c bail out if no symbols | |
| 261 | static int64_t | 78 | * are found, fix it later. |
| 262 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | 79 | */ |
| 263 | { | 80 | return 0; |
| 264 | struct sort_entry *se; | ||
| 265 | int64_t cmp = 0; | ||
| 266 | |||
| 267 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
| 268 | cmp = se->cmp(left, right); | ||
| 269 | if (cmp) | ||
| 270 | break; | ||
| 271 | } | ||
| 272 | |||
| 273 | return cmp; | ||
| 274 | } | ||
| 275 | |||
| 276 | static int64_t | ||
| 277 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | ||
| 278 | { | ||
| 279 | struct sort_entry *se; | ||
| 280 | int64_t cmp = 0; | ||
| 281 | |||
| 282 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
| 283 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | ||
| 284 | |||
| 285 | f = se->collapse ?: se->cmp; | ||
| 286 | |||
| 287 | cmp = f(left, right); | ||
| 288 | if (cmp) | ||
| 289 | break; | ||
| 290 | } | ||
| 291 | |||
| 292 | return cmp; | ||
| 293 | } | 81 | } |
| 294 | 82 | ||
| 295 | /* | 83 | /* |
| @@ -299,380 +87,81 @@ static void hist_hit(struct hist_entry *he, u64 ip) | |||
| 299 | { | 87 | { |
| 300 | unsigned int sym_size, offset; | 88 | unsigned int sym_size, offset; |
| 301 | struct symbol *sym = he->sym; | 89 | struct symbol *sym = he->sym; |
| 90 | struct sym_priv *priv; | ||
| 91 | struct sym_hist *h; | ||
| 302 | 92 | ||
| 303 | he->count++; | 93 | he->count++; |
| 304 | 94 | ||
| 305 | if (!sym || !sym->hist) | 95 | if (!sym || !he->map) |
| 96 | return; | ||
| 97 | |||
| 98 | priv = symbol__priv(sym); | ||
| 99 | if (!priv->hist) | ||
| 306 | return; | 100 | return; |
| 307 | 101 | ||
| 308 | sym_size = sym->end - sym->start; | 102 | sym_size = sym->end - sym->start; |
| 309 | offset = ip - sym->start; | 103 | offset = ip - sym->start; |
| 310 | 104 | ||
| 105 | if (verbose) | ||
| 106 | fprintf(stderr, "%s: ip=%Lx\n", __func__, | ||
| 107 | he->map->unmap_ip(he->map, ip)); | ||
| 108 | |||
| 311 | if (offset >= sym_size) | 109 | if (offset >= sym_size) |
| 312 | return; | 110 | return; |
| 313 | 111 | ||
| 314 | sym->hist_sum++; | 112 | h = priv->hist; |
| 315 | sym->hist[offset]++; | 113 | h->sum++; |
| 114 | h->ip[offset]++; | ||
| 316 | 115 | ||
| 317 | if (verbose >= 3) | 116 | if (verbose >= 3) |
| 318 | printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", | 117 | printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", |
| 319 | (void *)(unsigned long)he->sym->start, | 118 | (void *)(unsigned long)he->sym->start, |
| 320 | he->sym->name, | 119 | he->sym->name, |
| 321 | (void *)(unsigned long)ip, ip - he->sym->start, | 120 | (void *)(unsigned long)ip, ip - he->sym->start, |
| 322 | sym->hist[offset]); | 121 | h->ip[offset]); |
| 323 | } | 122 | } |
| 324 | 123 | ||
| 325 | static int | 124 | static int hist_entry__add(struct addr_location *al, u64 count) |
| 326 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, | ||
| 327 | struct symbol *sym, u64 ip, char level) | ||
| 328 | { | 125 | { |
| 329 | struct rb_node **p = &hist.rb_node; | 126 | bool hit; |
| 330 | struct rb_node *parent = NULL; | 127 | struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit); |
| 331 | struct hist_entry *he; | 128 | if (he == NULL) |
| 332 | struct hist_entry entry = { | ||
| 333 | .thread = thread, | ||
| 334 | .map = map, | ||
| 335 | .dso = dso, | ||
| 336 | .sym = sym, | ||
| 337 | .ip = ip, | ||
| 338 | .level = level, | ||
| 339 | .count = 1, | ||
| 340 | }; | ||
| 341 | int cmp; | ||
| 342 | |||
| 343 | while (*p != NULL) { | ||
| 344 | parent = *p; | ||
| 345 | he = rb_entry(parent, struct hist_entry, rb_node); | ||
| 346 | |||
| 347 | cmp = hist_entry__cmp(&entry, he); | ||
| 348 | |||
| 349 | if (!cmp) { | ||
| 350 | hist_hit(he, ip); | ||
| 351 | |||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | if (cmp < 0) | ||
| 356 | p = &(*p)->rb_left; | ||
| 357 | else | ||
| 358 | p = &(*p)->rb_right; | ||
| 359 | } | ||
| 360 | |||
| 361 | he = malloc(sizeof(*he)); | ||
| 362 | if (!he) | ||
| 363 | return -ENOMEM; | 129 | return -ENOMEM; |
| 364 | *he = entry; | 130 | hist_hit(he, al->addr); |
| 365 | rb_link_node(&he->rb_node, parent, p); | ||
| 366 | rb_insert_color(&he->rb_node, &hist); | ||
| 367 | |||
| 368 | return 0; | 131 | return 0; |
| 369 | } | 132 | } |
| 370 | 133 | ||
| 371 | static void hist_entry__free(struct hist_entry *he) | 134 | static int process_sample_event(event_t *event) |
| 372 | { | ||
| 373 | free(he); | ||
| 374 | } | ||
| 375 | |||
| 376 | /* | ||
| 377 | * collapse the histogram | ||
| 378 | */ | ||
| 379 | |||
| 380 | static struct rb_root collapse_hists; | ||
| 381 | |||
| 382 | static void collapse__insert_entry(struct hist_entry *he) | ||
| 383 | { | ||
| 384 | struct rb_node **p = &collapse_hists.rb_node; | ||
| 385 | struct rb_node *parent = NULL; | ||
| 386 | struct hist_entry *iter; | ||
| 387 | int64_t cmp; | ||
| 388 | |||
| 389 | while (*p != NULL) { | ||
| 390 | parent = *p; | ||
| 391 | iter = rb_entry(parent, struct hist_entry, rb_node); | ||
| 392 | |||
| 393 | cmp = hist_entry__collapse(iter, he); | ||
| 394 | |||
| 395 | if (!cmp) { | ||
| 396 | iter->count += he->count; | ||
| 397 | hist_entry__free(he); | ||
| 398 | return; | ||
| 399 | } | ||
| 400 | |||
| 401 | if (cmp < 0) | ||
| 402 | p = &(*p)->rb_left; | ||
| 403 | else | ||
| 404 | p = &(*p)->rb_right; | ||
| 405 | } | ||
| 406 | |||
| 407 | rb_link_node(&he->rb_node, parent, p); | ||
| 408 | rb_insert_color(&he->rb_node, &collapse_hists); | ||
| 409 | } | ||
| 410 | |||
| 411 | static void collapse__resort(void) | ||
| 412 | { | ||
| 413 | struct rb_node *next; | ||
| 414 | struct hist_entry *n; | ||
| 415 | |||
| 416 | if (!sort__need_collapse) | ||
| 417 | return; | ||
| 418 | |||
| 419 | next = rb_first(&hist); | ||
| 420 | while (next) { | ||
| 421 | n = rb_entry(next, struct hist_entry, rb_node); | ||
| 422 | next = rb_next(&n->rb_node); | ||
| 423 | |||
| 424 | rb_erase(&n->rb_node, &hist); | ||
| 425 | collapse__insert_entry(n); | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | /* | ||
| 430 | * reverse the map, sort on count. | ||
| 431 | */ | ||
| 432 | |||
| 433 | static struct rb_root output_hists; | ||
| 434 | |||
| 435 | static void output__insert_entry(struct hist_entry *he) | ||
| 436 | { | 135 | { |
| 437 | struct rb_node **p = &output_hists.rb_node; | 136 | struct addr_location al; |
| 438 | struct rb_node *parent = NULL; | ||
| 439 | struct hist_entry *iter; | ||
| 440 | 137 | ||
| 441 | while (*p != NULL) { | 138 | dump_printf("(IP, %d): %d: %p\n", event->header.misc, |
| 442 | parent = *p; | 139 | event->ip.pid, (void *)(long)event->ip.ip); |
| 443 | iter = rb_entry(parent, struct hist_entry, rb_node); | ||
| 444 | 140 | ||
| 445 | if (he->count > iter->count) | 141 | if (event__preprocess_sample(event, &al, symbol_filter) < 0) { |
| 446 | p = &(*p)->rb_left; | ||
| 447 | else | ||
| 448 | p = &(*p)->rb_right; | ||
| 449 | } | ||
| 450 | |||
| 451 | rb_link_node(&he->rb_node, parent, p); | ||
| 452 | rb_insert_color(&he->rb_node, &output_hists); | ||
| 453 | } | ||
| 454 | |||
| 455 | static void output__resort(void) | ||
| 456 | { | ||
| 457 | struct rb_node *next; | ||
| 458 | struct hist_entry *n; | ||
| 459 | struct rb_root *tree = &hist; | ||
| 460 | |||
| 461 | if (sort__need_collapse) | ||
| 462 | tree = &collapse_hists; | ||
| 463 | |||
| 464 | next = rb_first(tree); | ||
| 465 | |||
| 466 | while (next) { | ||
| 467 | n = rb_entry(next, struct hist_entry, rb_node); | ||
| 468 | next = rb_next(&n->rb_node); | ||
| 469 | |||
| 470 | rb_erase(&n->rb_node, tree); | ||
| 471 | output__insert_entry(n); | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | static unsigned long total = 0, | ||
| 476 | total_mmap = 0, | ||
| 477 | total_comm = 0, | ||
| 478 | total_fork = 0, | ||
| 479 | total_unknown = 0; | ||
| 480 | |||
| 481 | static int | ||
| 482 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 483 | { | ||
| 484 | char level; | ||
| 485 | int show = 0; | ||
| 486 | struct dso *dso = NULL; | ||
| 487 | struct thread *thread; | ||
| 488 | u64 ip = event->ip.ip; | ||
| 489 | struct map *map = NULL; | ||
| 490 | |||
| 491 | thread = threads__findnew(event->ip.pid, &threads, &last_match); | ||
| 492 | |||
| 493 | dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", | ||
| 494 | (void *)(offset + head), | ||
| 495 | (void *)(long)(event->header.size), | ||
| 496 | event->header.misc, | ||
| 497 | event->ip.pid, | ||
| 498 | (void *)(long)ip); | ||
| 499 | |||
| 500 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 501 | |||
| 502 | if (thread == NULL) { | ||
| 503 | fprintf(stderr, "problem processing %d event, skipping it.\n", | 142 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
| 504 | event->header.type); | 143 | event->header.type); |
| 505 | return -1; | 144 | return -1; |
| 506 | } | 145 | } |
| 507 | 146 | ||
| 508 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { | 147 | if (hist_entry__add(&al, 1)) { |
| 509 | show = SHOW_KERNEL; | 148 | fprintf(stderr, "problem incrementing symbol count, " |
| 510 | level = 'k'; | 149 | "skipping event\n"); |
| 511 | |||
| 512 | dso = kernel_dso; | ||
| 513 | |||
| 514 | dump_printf(" ...... dso: %s\n", dso->name); | ||
| 515 | |||
| 516 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { | ||
| 517 | |||
| 518 | show = SHOW_USER; | ||
| 519 | level = '.'; | ||
| 520 | |||
| 521 | map = thread__find_map(thread, ip); | ||
| 522 | if (map != NULL) { | ||
| 523 | ip = map->map_ip(map, ip); | ||
| 524 | dso = map->dso; | ||
| 525 | } else { | ||
| 526 | /* | ||
| 527 | * If this is outside of all known maps, | ||
| 528 | * and is a negative address, try to look it | ||
| 529 | * up in the kernel dso, as it might be a | ||
| 530 | * vsyscall (which executes in user-mode): | ||
| 531 | */ | ||
| 532 | if ((long long)ip < 0) | ||
| 533 | dso = kernel_dso; | ||
| 534 | } | ||
| 535 | dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); | ||
| 536 | |||
| 537 | } else { | ||
| 538 | show = SHOW_HV; | ||
| 539 | level = 'H'; | ||
| 540 | dump_printf(" ...... dso: [hypervisor]\n"); | ||
| 541 | } | ||
| 542 | |||
| 543 | if (show & show_mask) { | ||
| 544 | struct symbol *sym = NULL; | ||
| 545 | |||
| 546 | if (dso) | ||
| 547 | sym = dso->find_symbol(dso, ip); | ||
| 548 | |||
| 549 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { | ||
| 550 | fprintf(stderr, | ||
| 551 | "problem incrementing symbol count, skipping event\n"); | ||
| 552 | return -1; | ||
| 553 | } | ||
| 554 | } | ||
| 555 | total++; | ||
| 556 | |||
| 557 | return 0; | ||
| 558 | } | ||
| 559 | |||
| 560 | static int | ||
| 561 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 562 | { | ||
| 563 | struct thread *thread; | ||
| 564 | struct map *map = map__new(&event->mmap, NULL, 0); | ||
| 565 | |||
| 566 | thread = threads__findnew(event->mmap.pid, &threads, &last_match); | ||
| 567 | |||
| 568 | dump_printf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n", | ||
| 569 | (void *)(offset + head), | ||
| 570 | (void *)(long)(event->header.size), | ||
| 571 | event->mmap.pid, | ||
| 572 | (void *)(long)event->mmap.start, | ||
| 573 | (void *)(long)event->mmap.len, | ||
| 574 | (void *)(long)event->mmap.pgoff, | ||
| 575 | event->mmap.filename); | ||
| 576 | |||
| 577 | if (thread == NULL || map == NULL) { | ||
| 578 | dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n"); | ||
| 579 | return 0; | ||
| 580 | } | ||
| 581 | |||
| 582 | thread__insert_map(thread, map); | ||
| 583 | total_mmap++; | ||
| 584 | |||
| 585 | return 0; | ||
| 586 | } | ||
| 587 | |||
| 588 | static int | ||
| 589 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 590 | { | ||
| 591 | struct thread *thread; | ||
| 592 | |||
| 593 | thread = threads__findnew(event->comm.pid, &threads, &last_match); | ||
| 594 | dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", | ||
| 595 | (void *)(offset + head), | ||
| 596 | (void *)(long)(event->header.size), | ||
| 597 | event->comm.comm, event->comm.pid); | ||
| 598 | |||
| 599 | if (thread == NULL || | ||
| 600 | thread__set_comm(thread, event->comm.comm)) { | ||
| 601 | dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n"); | ||
| 602 | return -1; | ||
| 603 | } | ||
| 604 | total_comm++; | ||
| 605 | |||
| 606 | return 0; | ||
| 607 | } | ||
| 608 | |||
| 609 | static int | ||
| 610 | process_fork_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 611 | { | ||
| 612 | struct thread *thread; | ||
| 613 | struct thread *parent; | ||
| 614 | |||
| 615 | thread = threads__findnew(event->fork.pid, &threads, &last_match); | ||
| 616 | parent = threads__findnew(event->fork.ppid, &threads, &last_match); | ||
| 617 | dump_printf("%p [%p]: PERF_EVENT_FORK: %d:%d\n", | ||
| 618 | (void *)(offset + head), | ||
| 619 | (void *)(long)(event->header.size), | ||
| 620 | event->fork.pid, event->fork.ppid); | ||
| 621 | |||
| 622 | /* | ||
| 623 | * A thread clone will have the same PID for both | ||
| 624 | * parent and child. | ||
| 625 | */ | ||
| 626 | if (thread == parent) | ||
| 627 | return 0; | ||
| 628 | |||
| 629 | if (!thread || !parent || thread__fork(thread, parent)) { | ||
| 630 | dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n"); | ||
| 631 | return -1; | ||
| 632 | } | ||
| 633 | total_fork++; | ||
| 634 | |||
| 635 | return 0; | ||
| 636 | } | ||
| 637 | |||
| 638 | static int | ||
| 639 | process_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 640 | { | ||
| 641 | switch (event->header.type) { | ||
| 642 | case PERF_EVENT_SAMPLE: | ||
| 643 | return process_sample_event(event, offset, head); | ||
| 644 | |||
| 645 | case PERF_EVENT_MMAP: | ||
| 646 | return process_mmap_event(event, offset, head); | ||
| 647 | |||
| 648 | case PERF_EVENT_COMM: | ||
| 649 | return process_comm_event(event, offset, head); | ||
| 650 | |||
| 651 | case PERF_EVENT_FORK: | ||
| 652 | return process_fork_event(event, offset, head); | ||
| 653 | /* | ||
| 654 | * We dont process them right now but they are fine: | ||
| 655 | */ | ||
| 656 | |||
| 657 | case PERF_EVENT_THROTTLE: | ||
| 658 | case PERF_EVENT_UNTHROTTLE: | ||
| 659 | return 0; | ||
| 660 | |||
| 661 | default: | ||
| 662 | return -1; | 150 | return -1; |
| 663 | } | 151 | } |
| 664 | 152 | ||
| 665 | return 0; | 153 | return 0; |
| 666 | } | 154 | } |
| 667 | 155 | ||
| 668 | static int | 156 | static int parse_line(FILE *file, struct hist_entry *he, u64 len) |
| 669 | parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) | ||
| 670 | { | 157 | { |
| 158 | struct symbol *sym = he->sym; | ||
| 671 | char *line = NULL, *tmp, *tmp2; | 159 | char *line = NULL, *tmp, *tmp2; |
| 672 | static const char *prev_line; | 160 | static const char *prev_line; |
| 673 | static const char *prev_color; | 161 | static const char *prev_color; |
| 674 | unsigned int offset; | 162 | unsigned int offset; |
| 675 | size_t line_len; | 163 | size_t line_len; |
| 164 | u64 start; | ||
| 676 | s64 line_ip; | 165 | s64 line_ip; |
| 677 | int ret; | 166 | int ret; |
| 678 | char *c; | 167 | char *c; |
| @@ -709,22 +198,26 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) | |||
| 709 | line_ip = -1; | 198 | line_ip = -1; |
| 710 | } | 199 | } |
| 711 | 200 | ||
| 201 | start = he->map->unmap_ip(he->map, sym->start); | ||
| 202 | |||
| 712 | if (line_ip != -1) { | 203 | if (line_ip != -1) { |
| 713 | const char *path = NULL; | 204 | const char *path = NULL; |
| 714 | unsigned int hits = 0; | 205 | unsigned int hits = 0; |
| 715 | double percent = 0.0; | 206 | double percent = 0.0; |
| 716 | const char *color; | 207 | const char *color; |
| 717 | struct sym_ext *sym_ext = sym->priv; | 208 | struct sym_priv *priv = symbol__priv(sym); |
| 209 | struct sym_ext *sym_ext = priv->ext; | ||
| 210 | struct sym_hist *h = priv->hist; | ||
| 718 | 211 | ||
| 719 | offset = line_ip - start; | 212 | offset = line_ip - start; |
| 720 | if (offset < len) | 213 | if (offset < len) |
| 721 | hits = sym->hist[offset]; | 214 | hits = h->ip[offset]; |
| 722 | 215 | ||
| 723 | if (offset < len && sym_ext) { | 216 | if (offset < len && sym_ext) { |
| 724 | path = sym_ext[offset].path; | 217 | path = sym_ext[offset].path; |
| 725 | percent = sym_ext[offset].percent; | 218 | percent = sym_ext[offset].percent; |
| 726 | } else if (sym->hist_sum) | 219 | } else if (h->sum) |
| 727 | percent = 100.0 * hits / sym->hist_sum; | 220 | percent = 100.0 * hits / h->sum; |
| 728 | 221 | ||
| 729 | color = get_percent_color(percent); | 222 | color = get_percent_color(percent); |
| 730 | 223 | ||
| @@ -777,9 +270,10 @@ static void insert_source_line(struct sym_ext *sym_ext) | |||
| 777 | rb_insert_color(&sym_ext->node, &root_sym_ext); | 270 | rb_insert_color(&sym_ext->node, &root_sym_ext); |
| 778 | } | 271 | } |
| 779 | 272 | ||
| 780 | static void free_source_line(struct symbol *sym, int len) | 273 | static void free_source_line(struct hist_entry *he, int len) |
| 781 | { | 274 | { |
| 782 | struct sym_ext *sym_ext = sym->priv; | 275 | struct sym_priv *priv = symbol__priv(he->sym); |
| 276 | struct sym_ext *sym_ext = priv->ext; | ||
| 783 | int i; | 277 | int i; |
| 784 | 278 | ||
| 785 | if (!sym_ext) | 279 | if (!sym_ext) |
| @@ -789,26 +283,30 @@ static void free_source_line(struct symbol *sym, int len) | |||
| 789 | free(sym_ext[i].path); | 283 | free(sym_ext[i].path); |
| 790 | free(sym_ext); | 284 | free(sym_ext); |
| 791 | 285 | ||
| 792 | sym->priv = NULL; | 286 | priv->ext = NULL; |
| 793 | root_sym_ext = RB_ROOT; | 287 | root_sym_ext = RB_ROOT; |
| 794 | } | 288 | } |
| 795 | 289 | ||
| 796 | /* Get the filename:line for the colored entries */ | 290 | /* Get the filename:line for the colored entries */ |
| 797 | static void | 291 | static void |
| 798 | get_source_line(struct symbol *sym, u64 start, int len, const char *filename) | 292 | get_source_line(struct hist_entry *he, int len, const char *filename) |
| 799 | { | 293 | { |
| 294 | struct symbol *sym = he->sym; | ||
| 295 | u64 start; | ||
| 800 | int i; | 296 | int i; |
| 801 | char cmd[PATH_MAX * 2]; | 297 | char cmd[PATH_MAX * 2]; |
| 802 | struct sym_ext *sym_ext; | 298 | struct sym_ext *sym_ext; |
| 299 | struct sym_priv *priv = symbol__priv(sym); | ||
| 300 | struct sym_hist *h = priv->hist; | ||
| 803 | 301 | ||
| 804 | if (!sym->hist_sum) | 302 | if (!h->sum) |
| 805 | return; | 303 | return; |
| 806 | 304 | ||
| 807 | sym->priv = calloc(len, sizeof(struct sym_ext)); | 305 | sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext)); |
| 808 | if (!sym->priv) | 306 | if (!priv->ext) |
| 809 | return; | 307 | return; |
| 810 | 308 | ||
| 811 | sym_ext = sym->priv; | 309 | start = he->map->unmap_ip(he->map, sym->start); |
| 812 | 310 | ||
| 813 | for (i = 0; i < len; i++) { | 311 | for (i = 0; i < len; i++) { |
| 814 | char *path = NULL; | 312 | char *path = NULL; |
| @@ -816,7 +314,7 @@ get_source_line(struct symbol *sym, u64 start, int len, const char *filename) | |||
| 816 | u64 offset; | 314 | u64 offset; |
| 817 | FILE *fp; | 315 | FILE *fp; |
| 818 | 316 | ||
| 819 | sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum; | 317 | sym_ext[i].percent = 100.0 * h->ip[i] / h->sum; |
| 820 | if (sym_ext[i].percent <= 0.5) | 318 | if (sym_ext[i].percent <= 0.5) |
| 821 | continue; | 319 | continue; |
| 822 | 320 | ||
| @@ -870,33 +368,34 @@ static void print_summary(const char *filename) | |||
| 870 | } | 368 | } |
| 871 | } | 369 | } |
| 872 | 370 | ||
| 873 | static void annotate_sym(struct dso *dso, struct symbol *sym) | 371 | static void annotate_sym(struct hist_entry *he) |
| 874 | { | 372 | { |
| 875 | const char *filename = dso->name, *d_filename; | 373 | struct map *map = he->map; |
| 876 | u64 start, end, len; | 374 | struct dso *dso = map->dso; |
| 375 | struct symbol *sym = he->sym; | ||
| 376 | const char *filename = dso->long_name, *d_filename; | ||
| 377 | u64 len; | ||
| 877 | char command[PATH_MAX*2]; | 378 | char command[PATH_MAX*2]; |
| 878 | FILE *file; | 379 | FILE *file; |
| 879 | 380 | ||
| 880 | if (!filename) | 381 | if (!filename) |
| 881 | return; | 382 | return; |
| 882 | if (sym->module) | 383 | |
| 883 | filename = sym->module->path; | 384 | if (verbose) |
| 884 | else if (dso == kernel_dso) | 385 | fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n", |
| 885 | filename = vmlinux_name; | 386 | __func__, filename, sym->name, |
| 886 | 387 | map->unmap_ip(map, sym->start), | |
| 887 | start = sym->obj_start; | 388 | map->unmap_ip(map, sym->end)); |
| 888 | if (!start) | 389 | |
| 889 | start = sym->start; | ||
| 890 | if (full_paths) | 390 | if (full_paths) |
| 891 | d_filename = filename; | 391 | d_filename = filename; |
| 892 | else | 392 | else |
| 893 | d_filename = basename(filename); | 393 | d_filename = basename(filename); |
| 894 | 394 | ||
| 895 | end = start + sym->end - sym->start + 1; | ||
| 896 | len = sym->end - sym->start; | 395 | len = sym->end - sym->start; |
| 897 | 396 | ||
| 898 | if (print_line) { | 397 | if (print_line) { |
| 899 | get_source_line(sym, start, len, filename); | 398 | get_source_line(he, len, filename); |
| 900 | print_summary(filename); | 399 | print_summary(filename); |
| 901 | } | 400 | } |
| 902 | 401 | ||
| @@ -905,10 +404,12 @@ static void annotate_sym(struct dso *dso, struct symbol *sym) | |||
| 905 | printf("------------------------------------------------\n"); | 404 | printf("------------------------------------------------\n"); |
| 906 | 405 | ||
| 907 | if (verbose >= 2) | 406 | if (verbose >= 2) |
| 908 | printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name); | 407 | printf("annotating [%p] %30s : [%p] %30s\n", |
| 408 | dso, dso->long_name, sym, sym->name); | ||
| 909 | 409 | ||
| 910 | sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", | 410 | sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", |
| 911 | (u64)start, (u64)end, filename, filename); | 411 | map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end), |
| 412 | filename, filename); | ||
| 912 | 413 | ||
| 913 | if (verbose >= 3) | 414 | if (verbose >= 3) |
| 914 | printf("doing: %s\n", command); | 415 | printf("doing: %s\n", command); |
| @@ -918,159 +419,78 @@ static void annotate_sym(struct dso *dso, struct symbol *sym) | |||
| 918 | return; | 419 | return; |
| 919 | 420 | ||
| 920 | while (!feof(file)) { | 421 | while (!feof(file)) { |
| 921 | if (parse_line(file, sym, start, len) < 0) | 422 | if (parse_line(file, he, len) < 0) |
| 922 | break; | 423 | break; |
| 923 | } | 424 | } |
| 924 | 425 | ||
| 925 | pclose(file); | 426 | pclose(file); |
| 926 | if (print_line) | 427 | if (print_line) |
| 927 | free_source_line(sym, len); | 428 | free_source_line(he, len); |
| 928 | } | 429 | } |
| 929 | 430 | ||
| 930 | static void find_annotations(void) | 431 | static void find_annotations(void) |
| 931 | { | 432 | { |
| 932 | struct rb_node *nd; | 433 | struct rb_node *nd; |
| 933 | struct dso *dso; | ||
| 934 | int count = 0; | ||
| 935 | |||
| 936 | list_for_each_entry(dso, &dsos, node) { | ||
| 937 | |||
| 938 | for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) { | ||
| 939 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | ||
| 940 | |||
| 941 | if (sym->hist) { | ||
| 942 | annotate_sym(dso, sym); | ||
| 943 | count++; | ||
| 944 | } | ||
| 945 | } | ||
| 946 | } | ||
| 947 | |||
| 948 | if (!count) | ||
| 949 | printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter); | ||
| 950 | } | ||
| 951 | |||
| 952 | static int __cmd_annotate(void) | ||
| 953 | { | ||
| 954 | int ret, rc = EXIT_FAILURE; | ||
| 955 | unsigned long offset = 0; | ||
| 956 | unsigned long head = 0; | ||
| 957 | struct stat input_stat; | ||
| 958 | event_t *event; | ||
| 959 | uint32_t size; | ||
| 960 | char *buf; | ||
| 961 | |||
| 962 | register_idle_thread(&threads, &last_match); | ||
| 963 | |||
| 964 | input = open(input_name, O_RDONLY); | ||
| 965 | if (input < 0) { | ||
| 966 | perror("failed to open file"); | ||
| 967 | exit(-1); | ||
| 968 | } | ||
| 969 | |||
| 970 | ret = fstat(input, &input_stat); | ||
| 971 | if (ret < 0) { | ||
| 972 | perror("failed to stat file"); | ||
| 973 | exit(-1); | ||
| 974 | } | ||
| 975 | |||
| 976 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { | ||
| 977 | fprintf(stderr, "file: %s not owned by current user or root\n", input_name); | ||
| 978 | exit(-1); | ||
| 979 | } | ||
| 980 | |||
| 981 | if (!input_stat.st_size) { | ||
| 982 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | ||
| 983 | exit(0); | ||
| 984 | } | ||
| 985 | |||
| 986 | if (load_kernel() < 0) { | ||
| 987 | perror("failed to load kernel symbols"); | ||
| 988 | return EXIT_FAILURE; | ||
| 989 | } | ||
| 990 | |||
| 991 | remap: | ||
| 992 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | ||
| 993 | MAP_SHARED, input, offset); | ||
| 994 | if (buf == MAP_FAILED) { | ||
| 995 | perror("failed to mmap file"); | ||
| 996 | exit(-1); | ||
| 997 | } | ||
| 998 | |||
| 999 | more: | ||
| 1000 | event = (event_t *)(buf + head); | ||
| 1001 | 434 | ||
| 1002 | size = event->header.size; | 435 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 1003 | if (!size) | 436 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
| 1004 | size = 8; | 437 | struct sym_priv *priv; |
| 1005 | 438 | ||
| 1006 | if (head + event->header.size >= page_size * mmap_window) { | 439 | if (he->sym == NULL) |
| 1007 | unsigned long shift = page_size * (head / page_size); | 440 | continue; |
| 1008 | int munmap_ret; | ||
| 1009 | |||
| 1010 | munmap_ret = munmap(buf, page_size * mmap_window); | ||
| 1011 | assert(munmap_ret == 0); | ||
| 1012 | |||
| 1013 | offset += shift; | ||
| 1014 | head -= shift; | ||
| 1015 | goto remap; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | size = event->header.size; | ||
| 1019 | |||
| 1020 | dump_printf("%p [%p]: event: %d\n", | ||
| 1021 | (void *)(offset + head), | ||
| 1022 | (void *)(long)event->header.size, | ||
| 1023 | event->header.type); | ||
| 1024 | |||
| 1025 | if (!size || process_event(event, offset, head) < 0) { | ||
| 1026 | |||
| 1027 | dump_printf("%p [%p]: skipping unknown header type: %d\n", | ||
| 1028 | (void *)(offset + head), | ||
| 1029 | (void *)(long)(event->header.size), | ||
| 1030 | event->header.type); | ||
| 1031 | 441 | ||
| 1032 | total_unknown++; | 442 | priv = symbol__priv(he->sym); |
| 443 | if (priv->hist == NULL) | ||
| 444 | continue; | ||
| 1033 | 445 | ||
| 446 | annotate_sym(he); | ||
| 1034 | /* | 447 | /* |
| 1035 | * assume we lost track of the stream, check alignment, and | 448 | * Since we have a hist_entry per IP for the same symbol, free |
| 1036 | * increment a single u64 in the hope to catch on again 'soon'. | 449 | * he->sym->hist to signal we already processed this symbol. |
| 1037 | */ | 450 | */ |
| 1038 | 451 | free(priv->hist); | |
| 1039 | if (unlikely(head & 7)) | 452 | priv->hist = NULL; |
| 1040 | head &= ~7ULL; | ||
| 1041 | |||
| 1042 | size = 8; | ||
| 1043 | } | 453 | } |
| 454 | } | ||
| 1044 | 455 | ||
| 1045 | head += size; | 456 | static struct perf_file_handler file_handler = { |
| 457 | .process_sample_event = process_sample_event, | ||
| 458 | .process_mmap_event = event__process_mmap, | ||
| 459 | .process_comm_event = event__process_comm, | ||
| 460 | .process_fork_event = event__process_task, | ||
| 461 | }; | ||
| 1046 | 462 | ||
| 1047 | if (offset + head < (unsigned long)input_stat.st_size) | 463 | static int __cmd_annotate(void) |
| 1048 | goto more; | 464 | { |
| 465 | struct perf_header *header; | ||
| 466 | struct thread *idle; | ||
| 467 | int ret; | ||
| 1049 | 468 | ||
| 1050 | rc = EXIT_SUCCESS; | 469 | idle = register_idle_thread(); |
| 1051 | close(input); | 470 | register_perf_file_handler(&file_handler); |
| 1052 | 471 | ||
| 1053 | dump_printf(" IP events: %10ld\n", total); | 472 | ret = mmap_dispatch_perf_file(&header, input_name, 0, 0, |
| 1054 | dump_printf(" mmap events: %10ld\n", total_mmap); | 473 | &event__cwdlen, &event__cwd); |
| 1055 | dump_printf(" comm events: %10ld\n", total_comm); | 474 | if (ret) |
| 1056 | dump_printf(" fork events: %10ld\n", total_fork); | 475 | return ret; |
| 1057 | dump_printf(" unknown events: %10ld\n", total_unknown); | ||
| 1058 | 476 | ||
| 1059 | if (dump_trace) | 477 | if (dump_trace) { |
| 478 | event__print_totals(); | ||
| 1060 | return 0; | 479 | return 0; |
| 480 | } | ||
| 1061 | 481 | ||
| 1062 | if (verbose >= 3) | 482 | if (verbose > 3) |
| 1063 | threads__fprintf(stdout, &threads); | 483 | threads__fprintf(stdout); |
| 1064 | 484 | ||
| 1065 | if (verbose >= 2) | 485 | if (verbose > 2) |
| 1066 | dsos__fprintf(stdout); | 486 | dsos__fprintf(stdout); |
| 1067 | 487 | ||
| 1068 | collapse__resort(); | 488 | collapse__resort(); |
| 1069 | output__resort(); | 489 | output__resort(event__total[0]); |
| 1070 | 490 | ||
| 1071 | find_annotations(); | 491 | find_annotations(); |
| 1072 | 492 | ||
| 1073 | return rc; | 493 | return ret; |
| 1074 | } | 494 | } |
| 1075 | 495 | ||
| 1076 | static const char * const annotate_usage[] = { | 496 | static const char * const annotate_usage[] = { |
| @@ -1088,8 +508,9 @@ static const struct option options[] = { | |||
| 1088 | "be more verbose (show symbol address, etc)"), | 508 | "be more verbose (show symbol address, etc)"), |
| 1089 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | 509 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1090 | "dump raw trace in ASCII"), | 510 | "dump raw trace in ASCII"), |
| 1091 | OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), | 511 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 1092 | OPT_BOOLEAN('m', "modules", &modules, | 512 | "file", "vmlinux pathname"), |
| 513 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, | ||
| 1093 | "load module symbols - WARNING: use only with -k and LIVE kernel"), | 514 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
| 1094 | OPT_BOOLEAN('l', "print-line", &print_line, | 515 | OPT_BOOLEAN('l', "print-line", &print_line, |
| 1095 | "print matching source lines (may be slow)"), | 516 | "print matching source lines (may be slow)"), |
| @@ -1115,9 +536,8 @@ static void setup_sorting(void) | |||
| 1115 | 536 | ||
| 1116 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) | 537 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
| 1117 | { | 538 | { |
| 1118 | symbol__init(); | 539 | if (symbol__init(&symbol_conf) < 0) |
| 1119 | 540 | return -1; | |
| 1120 | page_size = getpagesize(); | ||
| 1121 | 541 | ||
| 1122 | argc = parse_options(argc, argv, options, annotate_usage, 0); | 542 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 1123 | 543 | ||
| @@ -1134,10 +554,13 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used) | |||
| 1134 | sym_hist_filter = argv[0]; | 554 | sym_hist_filter = argv[0]; |
| 1135 | } | 555 | } |
| 1136 | 556 | ||
| 1137 | if (!sym_hist_filter) | ||
| 1138 | usage_with_options(annotate_usage, options); | ||
| 1139 | |||
| 1140 | setup_pager(); | 557 | setup_pager(); |
| 1141 | 558 | ||
| 559 | if (field_sep && *field_sep == '.') { | ||
| 560 | fputs("'.' is the only non valid --field-separator argument\n", | ||
| 561 | stderr); | ||
| 562 | exit(129); | ||
| 563 | } | ||
| 564 | |||
| 1142 | return __cmd_annotate(); | 565 | return __cmd_annotate(); |
| 1143 | } | 566 | } |
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c new file mode 100644 index 000000000000..e043eb83092a --- /dev/null +++ b/tools/perf/builtin-bench.c | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | /* | ||
| 2 | * | ||
| 3 | * builtin-bench.c | ||
| 4 | * | ||
| 5 | * General benchmarking subsystem provided by perf | ||
| 6 | * | ||
| 7 | * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | ||
| 8 | * | ||
| 9 | */ | ||
| 10 | |||
| 11 | /* | ||
| 12 | * | ||
| 13 | * Available subsystem list: | ||
| 14 | * sched ... scheduler and IPC mechanism | ||
| 15 | * mem ... memory access performance | ||
| 16 | * | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include "perf.h" | ||
| 20 | #include "util/util.h" | ||
| 21 | #include "util/parse-options.h" | ||
| 22 | #include "builtin.h" | ||
| 23 | #include "bench/bench.h" | ||
| 24 | |||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdlib.h> | ||
| 27 | #include <string.h> | ||
| 28 | |||
| 29 | struct bench_suite { | ||
| 30 | const char *name; | ||
| 31 | const char *summary; | ||
| 32 | int (*fn)(int, const char **, const char *); | ||
| 33 | }; | ||
| 34 | |||
| 35 | static struct bench_suite sched_suites[] = { | ||
| 36 | { "messaging", | ||
| 37 | "Benchmark for scheduler and IPC mechanisms", | ||
| 38 | bench_sched_messaging }, | ||
| 39 | { "pipe", | ||
| 40 | "Flood of communication over pipe() between two processes", | ||
| 41 | bench_sched_pipe }, | ||
| 42 | { NULL, | ||
| 43 | NULL, | ||
| 44 | NULL } | ||
| 45 | }; | ||
| 46 | |||
| 47 | static struct bench_suite mem_suites[] = { | ||
| 48 | { "memcpy", | ||
| 49 | "Simple memory copy in various ways", | ||
| 50 | bench_mem_memcpy }, | ||
| 51 | { NULL, | ||
| 52 | NULL, | ||
| 53 | NULL } | ||
| 54 | }; | ||
| 55 | |||
| 56 | struct bench_subsys { | ||
| 57 | const char *name; | ||
| 58 | const char *summary; | ||
| 59 | struct bench_suite *suites; | ||
| 60 | }; | ||
| 61 | |||
| 62 | static struct bench_subsys subsystems[] = { | ||
| 63 | { "sched", | ||
| 64 | "scheduler and IPC mechanism", | ||
| 65 | sched_suites }, | ||
| 66 | { "mem", | ||
| 67 | "memory access performance", | ||
| 68 | mem_suites }, | ||
| 69 | { NULL, | ||
| 70 | NULL, | ||
| 71 | NULL } | ||
| 72 | }; | ||
| 73 | |||
| 74 | static void dump_suites(int subsys_index) | ||
| 75 | { | ||
| 76 | int i; | ||
| 77 | |||
| 78 | printf("List of available suites for %s...\n\n", | ||
| 79 | subsystems[subsys_index].name); | ||
| 80 | |||
| 81 | for (i = 0; subsystems[subsys_index].suites[i].name; i++) | ||
| 82 | printf("\t%s: %s\n", | ||
| 83 | subsystems[subsys_index].suites[i].name, | ||
| 84 | subsystems[subsys_index].suites[i].summary); | ||
| 85 | |||
| 86 | printf("\n"); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | static char *bench_format_str; | ||
| 91 | int bench_format = BENCH_FORMAT_DEFAULT; | ||
| 92 | |||
| 93 | static const struct option bench_options[] = { | ||
| 94 | OPT_STRING('f', "format", &bench_format_str, "default", | ||
| 95 | "Specify format style"), | ||
| 96 | OPT_END() | ||
| 97 | }; | ||
| 98 | |||
| 99 | static const char * const bench_usage[] = { | ||
| 100 | "perf bench [<common options>] <subsystem> <suite> [<options>]", | ||
| 101 | NULL | ||
| 102 | }; | ||
| 103 | |||
| 104 | static void print_usage(void) | ||
| 105 | { | ||
| 106 | int i; | ||
| 107 | |||
| 108 | printf("Usage: \n"); | ||
| 109 | for (i = 0; bench_usage[i]; i++) | ||
| 110 | printf("\t%s\n", bench_usage[i]); | ||
| 111 | printf("\n"); | ||
| 112 | |||
| 113 | printf("List of available subsystems...\n\n"); | ||
| 114 | |||
| 115 | for (i = 0; subsystems[i].name; i++) | ||
| 116 | printf("\t%s: %s\n", | ||
| 117 | subsystems[i].name, subsystems[i].summary); | ||
| 118 | printf("\n"); | ||
| 119 | } | ||
| 120 | |||
| 121 | static int bench_str2int(char *str) | ||
| 122 | { | ||
| 123 | if (!str) | ||
| 124 | return BENCH_FORMAT_DEFAULT; | ||
| 125 | |||
| 126 | if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) | ||
| 127 | return BENCH_FORMAT_DEFAULT; | ||
| 128 | else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) | ||
| 129 | return BENCH_FORMAT_SIMPLE; | ||
| 130 | |||
| 131 | return BENCH_FORMAT_UNKNOWN; | ||
| 132 | } | ||
| 133 | |||
| 134 | int cmd_bench(int argc, const char **argv, const char *prefix __used) | ||
| 135 | { | ||
| 136 | int i, j, status = 0; | ||
| 137 | |||
| 138 | if (argc < 2) { | ||
| 139 | /* No subsystem specified. */ | ||
| 140 | print_usage(); | ||
| 141 | goto end; | ||
| 142 | } | ||
| 143 | |||
| 144 | argc = parse_options(argc, argv, bench_options, bench_usage, | ||
| 145 | PARSE_OPT_STOP_AT_NON_OPTION); | ||
| 146 | |||
| 147 | bench_format = bench_str2int(bench_format_str); | ||
| 148 | if (bench_format == BENCH_FORMAT_UNKNOWN) { | ||
| 149 | printf("Unknown format descriptor:%s\n", bench_format_str); | ||
| 150 | goto end; | ||
| 151 | } | ||
| 152 | |||
| 153 | if (argc < 1) { | ||
| 154 | print_usage(); | ||
| 155 | goto end; | ||
| 156 | } | ||
| 157 | |||
| 158 | for (i = 0; subsystems[i].name; i++) { | ||
| 159 | if (strcmp(subsystems[i].name, argv[0])) | ||
| 160 | continue; | ||
| 161 | |||
| 162 | if (argc < 2) { | ||
| 163 | /* No suite specified. */ | ||
| 164 | dump_suites(i); | ||
| 165 | goto end; | ||
| 166 | } | ||
| 167 | |||
| 168 | for (j = 0; subsystems[i].suites[j].name; j++) { | ||
| 169 | if (strcmp(subsystems[i].suites[j].name, argv[1])) | ||
| 170 | continue; | ||
| 171 | |||
| 172 | if (bench_format == BENCH_FORMAT_DEFAULT) | ||
| 173 | printf("# Running %s/%s benchmark...\n", | ||
| 174 | subsystems[i].name, | ||
| 175 | subsystems[i].suites[j].name); | ||
| 176 | status = subsystems[i].suites[j].fn(argc - 1, | ||
| 177 | argv + 1, prefix); | ||
| 178 | goto end; | ||
| 179 | } | ||
| 180 | |||
| 181 | if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { | ||
| 182 | dump_suites(i); | ||
| 183 | goto end; | ||
| 184 | } | ||
| 185 | |||
| 186 | printf("Unknown suite:%s for %s\n", argv[1], argv[0]); | ||
| 187 | status = 1; | ||
| 188 | goto end; | ||
| 189 | } | ||
| 190 | |||
| 191 | printf("Unknown subsystem:%s\n", argv[0]); | ||
| 192 | status = 1; | ||
| 193 | |||
| 194 | end: | ||
| 195 | return status; | ||
| 196 | } | ||
diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid-list.c new file mode 100644 index 000000000000..7dee9d19ab7a --- /dev/null +++ b/tools/perf/builtin-buildid-list.c | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | /* | ||
| 2 | * builtin-buildid-list.c | ||
| 3 | * | ||
| 4 | * Builtin buildid-list command: list buildids in perf.data | ||
| 5 | * | ||
| 6 | * Copyright (C) 2009, Red Hat Inc. | ||
| 7 | * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com> | ||
| 8 | */ | ||
| 9 | #include "builtin.h" | ||
| 10 | #include "perf.h" | ||
| 11 | #include "util/cache.h" | ||
| 12 | #include "util/data_map.h" | ||
| 13 | #include "util/debug.h" | ||
| 14 | #include "util/header.h" | ||
| 15 | #include "util/parse-options.h" | ||
| 16 | #include "util/symbol.h" | ||
| 17 | |||
| 18 | static char const *input_name = "perf.data"; | ||
| 19 | static int force; | ||
| 20 | |||
| 21 | static const char *const buildid_list_usage[] = { | ||
| 22 | "perf report [<options>]", | ||
| 23 | NULL | ||
| 24 | }; | ||
| 25 | |||
| 26 | static const struct option options[] = { | ||
| 27 | OPT_STRING('i', "input", &input_name, "file", | ||
| 28 | "input file name"), | ||
| 29 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), | ||
| 30 | OPT_BOOLEAN('v', "verbose", &verbose, | ||
| 31 | "be more verbose"), | ||
| 32 | OPT_END() | ||
| 33 | }; | ||
| 34 | |||
| 35 | static int perf_file_section__process_buildids(struct perf_file_section *self, | ||
| 36 | int feat, int fd) | ||
| 37 | { | ||
| 38 | if (feat != HEADER_BUILD_ID) | ||
| 39 | return 0; | ||
| 40 | |||
| 41 | if (lseek(fd, self->offset, SEEK_SET) < 0) { | ||
| 42 | pr_warning("Failed to lseek to %Ld offset for buildids!\n", | ||
| 43 | self->offset); | ||
| 44 | return -1; | ||
| 45 | } | ||
| 46 | |||
| 47 | if (perf_header__read_build_ids(fd, self->offset, self->size)) { | ||
| 48 | pr_warning("Failed to read buildids!\n"); | ||
| 49 | return -1; | ||
| 50 | } | ||
| 51 | |||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | static int __cmd_buildid_list(void) | ||
| 56 | { | ||
| 57 | int err = -1; | ||
| 58 | struct perf_header *header; | ||
| 59 | struct perf_file_header f_header; | ||
| 60 | struct stat input_stat; | ||
| 61 | int input = open(input_name, O_RDONLY); | ||
| 62 | |||
| 63 | if (input < 0) { | ||
| 64 | pr_err("failed to open file: %s", input_name); | ||
| 65 | if (!strcmp(input_name, "perf.data")) | ||
| 66 | pr_err(" (try 'perf record' first)"); | ||
| 67 | pr_err("\n"); | ||
| 68 | goto out; | ||
| 69 | } | ||
| 70 | |||
| 71 | err = fstat(input, &input_stat); | ||
| 72 | if (err < 0) { | ||
| 73 | perror("failed to stat file"); | ||
| 74 | goto out_close; | ||
| 75 | } | ||
| 76 | |||
| 77 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { | ||
| 78 | pr_err("file %s not owned by current user or root\n", | ||
| 79 | input_name); | ||
| 80 | goto out_close; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (!input_stat.st_size) { | ||
| 84 | pr_info("zero-sized file, nothing to do!\n"); | ||
| 85 | goto out_close; | ||
| 86 | } | ||
| 87 | |||
| 88 | err = -1; | ||
| 89 | header = perf_header__new(); | ||
| 90 | if (header == NULL) | ||
| 91 | goto out_close; | ||
| 92 | |||
| 93 | if (perf_file_header__read(&f_header, header, input) < 0) { | ||
| 94 | pr_warning("incompatible file format"); | ||
| 95 | goto out_close; | ||
| 96 | } | ||
| 97 | |||
| 98 | err = perf_header__process_sections(header, input, | ||
| 99 | perf_file_section__process_buildids); | ||
| 100 | |||
| 101 | if (err < 0) | ||
| 102 | goto out_close; | ||
| 103 | |||
| 104 | dsos__fprintf_buildid(stdout); | ||
| 105 | out_close: | ||
| 106 | close(input); | ||
| 107 | out: | ||
| 108 | return err; | ||
| 109 | } | ||
| 110 | |||
| 111 | int cmd_buildid_list(int argc, const char **argv, const char *prefix __used) | ||
| 112 | { | ||
| 113 | argc = parse_options(argc, argv, options, buildid_list_usage, 0); | ||
| 114 | setup_pager(); | ||
| 115 | return __cmd_buildid_list(); | ||
| 116 | } | ||
diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c index 4fb8734a796e..9f810b17c25c 100644 --- a/tools/perf/builtin-help.c +++ b/tools/perf/builtin-help.c | |||
| @@ -61,8 +61,7 @@ static const char *get_man_viewer_info(const char *name) | |||
| 61 | { | 61 | { |
| 62 | struct man_viewer_info_list *viewer; | 62 | struct man_viewer_info_list *viewer; |
| 63 | 63 | ||
| 64 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) | 64 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) { |
| 65 | { | ||
| 66 | if (!strcasecmp(name, viewer->name)) | 65 | if (!strcasecmp(name, viewer->name)) |
| 67 | return viewer->info; | 66 | return viewer->info; |
| 68 | } | 67 | } |
| @@ -115,7 +114,7 @@ static int check_emacsclient_version(void) | |||
| 115 | return 0; | 114 | return 0; |
| 116 | } | 115 | } |
| 117 | 116 | ||
| 118 | static void exec_woman_emacs(const char* path, const char *page) | 117 | static void exec_woman_emacs(const char *path, const char *page) |
| 119 | { | 118 | { |
| 120 | if (!check_emacsclient_version()) { | 119 | if (!check_emacsclient_version()) { |
| 121 | /* This works only with emacsclient version >= 22. */ | 120 | /* This works only with emacsclient version >= 22. */ |
| @@ -129,7 +128,7 @@ static void exec_woman_emacs(const char* path, const char *page) | |||
| 129 | } | 128 | } |
| 130 | } | 129 | } |
| 131 | 130 | ||
| 132 | static void exec_man_konqueror(const char* path, const char *page) | 131 | static void exec_man_konqueror(const char *path, const char *page) |
| 133 | { | 132 | { |
| 134 | const char *display = getenv("DISPLAY"); | 133 | const char *display = getenv("DISPLAY"); |
| 135 | if (display && *display) { | 134 | if (display && *display) { |
| @@ -157,7 +156,7 @@ static void exec_man_konqueror(const char* path, const char *page) | |||
| 157 | } | 156 | } |
| 158 | } | 157 | } |
| 159 | 158 | ||
| 160 | static void exec_man_man(const char* path, const char *page) | 159 | static void exec_man_man(const char *path, const char *page) |
| 161 | { | 160 | { |
| 162 | if (!path) | 161 | if (!path) |
| 163 | path = "man"; | 162 | path = "man"; |
| @@ -180,7 +179,7 @@ static void add_man_viewer(const char *name) | |||
| 180 | 179 | ||
| 181 | while (*p) | 180 | while (*p) |
| 182 | p = &((*p)->next); | 181 | p = &((*p)->next); |
| 183 | *p = calloc(1, (sizeof(**p) + len + 1)); | 182 | *p = zalloc(sizeof(**p) + len + 1); |
| 184 | strncpy((*p)->name, name, len); | 183 | strncpy((*p)->name, name, len); |
| 185 | } | 184 | } |
| 186 | 185 | ||
| @@ -195,7 +194,7 @@ static void do_add_man_viewer_info(const char *name, | |||
| 195 | size_t len, | 194 | size_t len, |
| 196 | const char *value) | 195 | const char *value) |
| 197 | { | 196 | { |
| 198 | struct man_viewer_info_list *new = calloc(1, sizeof(*new) + len + 1); | 197 | struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1); |
| 199 | 198 | ||
| 200 | strncpy(new->name, name, len); | 199 | strncpy(new->name, name, len); |
| 201 | new->info = strdup(value); | 200 | new->info = strdup(value); |
| @@ -364,9 +363,8 @@ static void show_man_page(const char *perf_cmd) | |||
| 364 | 363 | ||
| 365 | setup_man_path(); | 364 | setup_man_path(); |
| 366 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) | 365 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) |
| 367 | { | ||
| 368 | exec_viewer(viewer->name, page); /* will return when unable */ | 366 | exec_viewer(viewer->name, page); /* will return when unable */ |
| 369 | } | 367 | |
| 370 | if (fallback) | 368 | if (fallback) |
| 371 | exec_viewer(fallback, page); | 369 | exec_viewer(fallback, page); |
| 372 | exec_viewer("man", page); | 370 | exec_viewer("man", page); |
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c new file mode 100644 index 000000000000..047fef74bd52 --- /dev/null +++ b/tools/perf/builtin-kmem.c | |||
| @@ -0,0 +1,807 @@ | |||
| 1 | #include "builtin.h" | ||
| 2 | #include "perf.h" | ||
| 3 | |||
| 4 | #include "util/util.h" | ||
| 5 | #include "util/cache.h" | ||
| 6 | #include "util/symbol.h" | ||
| 7 | #include "util/thread.h" | ||
| 8 | #include "util/header.h" | ||
| 9 | |||
| 10 | #include "util/parse-options.h" | ||
| 11 | #include "util/trace-event.h" | ||
| 12 | |||
| 13 | #include "util/debug.h" | ||
| 14 | #include "util/data_map.h" | ||
| 15 | |||
| 16 | #include <linux/rbtree.h> | ||
| 17 | |||
| 18 | struct alloc_stat; | ||
| 19 | typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *); | ||
| 20 | |||
| 21 | static char const *input_name = "perf.data"; | ||
| 22 | |||
| 23 | static struct perf_header *header; | ||
| 24 | static u64 sample_type; | ||
| 25 | |||
| 26 | static int alloc_flag; | ||
| 27 | static int caller_flag; | ||
| 28 | |||
| 29 | static int alloc_lines = -1; | ||
| 30 | static int caller_lines = -1; | ||
| 31 | |||
| 32 | static bool raw_ip; | ||
| 33 | |||
| 34 | static char default_sort_order[] = "frag,hit,bytes"; | ||
| 35 | |||
| 36 | static int *cpunode_map; | ||
| 37 | static int max_cpu_num; | ||
| 38 | |||
| 39 | struct alloc_stat { | ||
| 40 | u64 call_site; | ||
| 41 | u64 ptr; | ||
| 42 | u64 bytes_req; | ||
| 43 | u64 bytes_alloc; | ||
| 44 | u32 hit; | ||
| 45 | u32 pingpong; | ||
| 46 | |||
| 47 | short alloc_cpu; | ||
| 48 | |||
| 49 | struct rb_node node; | ||
| 50 | }; | ||
| 51 | |||
| 52 | static struct rb_root root_alloc_stat; | ||
| 53 | static struct rb_root root_alloc_sorted; | ||
| 54 | static struct rb_root root_caller_stat; | ||
| 55 | static struct rb_root root_caller_sorted; | ||
| 56 | |||
| 57 | static unsigned long total_requested, total_allocated; | ||
| 58 | static unsigned long nr_allocs, nr_cross_allocs; | ||
| 59 | |||
| 60 | struct raw_event_sample { | ||
| 61 | u32 size; | ||
| 62 | char data[0]; | ||
| 63 | }; | ||
| 64 | |||
| 65 | #define PATH_SYS_NODE "/sys/devices/system/node" | ||
| 66 | |||
| 67 | static void init_cpunode_map(void) | ||
| 68 | { | ||
| 69 | FILE *fp; | ||
| 70 | int i; | ||
| 71 | |||
| 72 | fp = fopen("/sys/devices/system/cpu/kernel_max", "r"); | ||
| 73 | if (!fp) { | ||
| 74 | max_cpu_num = 4096; | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (fscanf(fp, "%d", &max_cpu_num) < 1) | ||
| 79 | die("Failed to read 'kernel_max' from sysfs"); | ||
| 80 | max_cpu_num++; | ||
| 81 | |||
| 82 | cpunode_map = calloc(max_cpu_num, sizeof(int)); | ||
| 83 | if (!cpunode_map) | ||
| 84 | die("calloc"); | ||
| 85 | for (i = 0; i < max_cpu_num; i++) | ||
| 86 | cpunode_map[i] = -1; | ||
| 87 | fclose(fp); | ||
| 88 | } | ||
| 89 | |||
| 90 | static void setup_cpunode_map(void) | ||
| 91 | { | ||
| 92 | struct dirent *dent1, *dent2; | ||
| 93 | DIR *dir1, *dir2; | ||
| 94 | unsigned int cpu, mem; | ||
| 95 | char buf[PATH_MAX]; | ||
| 96 | |||
| 97 | init_cpunode_map(); | ||
| 98 | |||
| 99 | dir1 = opendir(PATH_SYS_NODE); | ||
| 100 | if (!dir1) | ||
| 101 | return; | ||
| 102 | |||
| 103 | while (true) { | ||
| 104 | dent1 = readdir(dir1); | ||
| 105 | if (!dent1) | ||
| 106 | break; | ||
| 107 | |||
| 108 | if (sscanf(dent1->d_name, "node%u", &mem) < 1) | ||
| 109 | continue; | ||
| 110 | |||
| 111 | snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name); | ||
| 112 | dir2 = opendir(buf); | ||
| 113 | if (!dir2) | ||
| 114 | continue; | ||
| 115 | while (true) { | ||
| 116 | dent2 = readdir(dir2); | ||
| 117 | if (!dent2) | ||
| 118 | break; | ||
| 119 | if (sscanf(dent2->d_name, "cpu%u", &cpu) < 1) | ||
| 120 | continue; | ||
| 121 | cpunode_map[cpu] = mem; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | static void insert_alloc_stat(unsigned long call_site, unsigned long ptr, | ||
| 127 | int bytes_req, int bytes_alloc, int cpu) | ||
| 128 | { | ||
| 129 | struct rb_node **node = &root_alloc_stat.rb_node; | ||
| 130 | struct rb_node *parent = NULL; | ||
| 131 | struct alloc_stat *data = NULL; | ||
| 132 | |||
| 133 | while (*node) { | ||
| 134 | parent = *node; | ||
| 135 | data = rb_entry(*node, struct alloc_stat, node); | ||
| 136 | |||
| 137 | if (ptr > data->ptr) | ||
| 138 | node = &(*node)->rb_right; | ||
| 139 | else if (ptr < data->ptr) | ||
| 140 | node = &(*node)->rb_left; | ||
| 141 | else | ||
| 142 | break; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (data && data->ptr == ptr) { | ||
| 146 | data->hit++; | ||
| 147 | data->bytes_req += bytes_req; | ||
| 148 | data->bytes_alloc += bytes_req; | ||
| 149 | } else { | ||
| 150 | data = malloc(sizeof(*data)); | ||
| 151 | if (!data) | ||
| 152 | die("malloc"); | ||
| 153 | data->ptr = ptr; | ||
| 154 | data->pingpong = 0; | ||
| 155 | data->hit = 1; | ||
| 156 | data->bytes_req = bytes_req; | ||
| 157 | data->bytes_alloc = bytes_alloc; | ||
| 158 | |||
| 159 | rb_link_node(&data->node, parent, node); | ||
| 160 | rb_insert_color(&data->node, &root_alloc_stat); | ||
| 161 | } | ||
| 162 | data->call_site = call_site; | ||
| 163 | data->alloc_cpu = cpu; | ||
| 164 | } | ||
| 165 | |||
| 166 | static void insert_caller_stat(unsigned long call_site, | ||
| 167 | int bytes_req, int bytes_alloc) | ||
| 168 | { | ||
| 169 | struct rb_node **node = &root_caller_stat.rb_node; | ||
| 170 | struct rb_node *parent = NULL; | ||
| 171 | struct alloc_stat *data = NULL; | ||
| 172 | |||
| 173 | while (*node) { | ||
| 174 | parent = *node; | ||
| 175 | data = rb_entry(*node, struct alloc_stat, node); | ||
| 176 | |||
| 177 | if (call_site > data->call_site) | ||
| 178 | node = &(*node)->rb_right; | ||
| 179 | else if (call_site < data->call_site) | ||
| 180 | node = &(*node)->rb_left; | ||
| 181 | else | ||
| 182 | break; | ||
| 183 | } | ||
| 184 | |||
| 185 | if (data && data->call_site == call_site) { | ||
| 186 | data->hit++; | ||
| 187 | data->bytes_req += bytes_req; | ||
| 188 | data->bytes_alloc += bytes_req; | ||
| 189 | } else { | ||
| 190 | data = malloc(sizeof(*data)); | ||
| 191 | if (!data) | ||
| 192 | die("malloc"); | ||
| 193 | data->call_site = call_site; | ||
| 194 | data->pingpong = 0; | ||
| 195 | data->hit = 1; | ||
| 196 | data->bytes_req = bytes_req; | ||
| 197 | data->bytes_alloc = bytes_alloc; | ||
| 198 | |||
| 199 | rb_link_node(&data->node, parent, node); | ||
| 200 | rb_insert_color(&data->node, &root_caller_stat); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | static void process_alloc_event(struct raw_event_sample *raw, | ||
| 205 | struct event *event, | ||
| 206 | int cpu, | ||
| 207 | u64 timestamp __used, | ||
| 208 | struct thread *thread __used, | ||
| 209 | int node) | ||
| 210 | { | ||
| 211 | unsigned long call_site; | ||
| 212 | unsigned long ptr; | ||
| 213 | int bytes_req; | ||
| 214 | int bytes_alloc; | ||
| 215 | int node1, node2; | ||
| 216 | |||
| 217 | ptr = raw_field_value(event, "ptr", raw->data); | ||
| 218 | call_site = raw_field_value(event, "call_site", raw->data); | ||
| 219 | bytes_req = raw_field_value(event, "bytes_req", raw->data); | ||
| 220 | bytes_alloc = raw_field_value(event, "bytes_alloc", raw->data); | ||
| 221 | |||
| 222 | insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, cpu); | ||
| 223 | insert_caller_stat(call_site, bytes_req, bytes_alloc); | ||
| 224 | |||
| 225 | total_requested += bytes_req; | ||
| 226 | total_allocated += bytes_alloc; | ||
| 227 | |||
| 228 | if (node) { | ||
| 229 | node1 = cpunode_map[cpu]; | ||
| 230 | node2 = raw_field_value(event, "node", raw->data); | ||
| 231 | if (node1 != node2) | ||
| 232 | nr_cross_allocs++; | ||
| 233 | } | ||
| 234 | nr_allocs++; | ||
| 235 | } | ||
| 236 | |||
| 237 | static int ptr_cmp(struct alloc_stat *, struct alloc_stat *); | ||
| 238 | static int callsite_cmp(struct alloc_stat *, struct alloc_stat *); | ||
| 239 | |||
| 240 | static struct alloc_stat *search_alloc_stat(unsigned long ptr, | ||
| 241 | unsigned long call_site, | ||
| 242 | struct rb_root *root, | ||
| 243 | sort_fn_t sort_fn) | ||
| 244 | { | ||
| 245 | struct rb_node *node = root->rb_node; | ||
| 246 | struct alloc_stat key = { .ptr = ptr, .call_site = call_site }; | ||
| 247 | |||
| 248 | while (node) { | ||
| 249 | struct alloc_stat *data; | ||
| 250 | int cmp; | ||
| 251 | |||
| 252 | data = rb_entry(node, struct alloc_stat, node); | ||
| 253 | |||
| 254 | cmp = sort_fn(&key, data); | ||
| 255 | if (cmp < 0) | ||
| 256 | node = node->rb_left; | ||
| 257 | else if (cmp > 0) | ||
| 258 | node = node->rb_right; | ||
| 259 | else | ||
| 260 | return data; | ||
| 261 | } | ||
| 262 | return NULL; | ||
| 263 | } | ||
| 264 | |||
| 265 | static void process_free_event(struct raw_event_sample *raw, | ||
| 266 | struct event *event, | ||
| 267 | int cpu, | ||
| 268 | u64 timestamp __used, | ||
| 269 | struct thread *thread __used) | ||
| 270 | { | ||
| 271 | unsigned long ptr; | ||
| 272 | struct alloc_stat *s_alloc, *s_caller; | ||
| 273 | |||
| 274 | ptr = raw_field_value(event, "ptr", raw->data); | ||
| 275 | |||
| 276 | s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp); | ||
| 277 | if (!s_alloc) | ||
| 278 | return; | ||
| 279 | |||
| 280 | if (cpu != s_alloc->alloc_cpu) { | ||
| 281 | s_alloc->pingpong++; | ||
| 282 | |||
| 283 | s_caller = search_alloc_stat(0, s_alloc->call_site, | ||
| 284 | &root_caller_stat, callsite_cmp); | ||
| 285 | assert(s_caller); | ||
| 286 | s_caller->pingpong++; | ||
| 287 | } | ||
| 288 | s_alloc->alloc_cpu = -1; | ||
| 289 | } | ||
| 290 | |||
| 291 | static void | ||
| 292 | process_raw_event(event_t *raw_event __used, void *more_data, | ||
| 293 | int cpu, u64 timestamp, struct thread *thread) | ||
| 294 | { | ||
| 295 | struct raw_event_sample *raw = more_data; | ||
| 296 | struct event *event; | ||
| 297 | int type; | ||
| 298 | |||
| 299 | type = trace_parse_common_type(raw->data); | ||
| 300 | event = trace_find_event(type); | ||
| 301 | |||
| 302 | if (!strcmp(event->name, "kmalloc") || | ||
| 303 | !strcmp(event->name, "kmem_cache_alloc")) { | ||
| 304 | process_alloc_event(raw, event, cpu, timestamp, thread, 0); | ||
| 305 | return; | ||
| 306 | } | ||
| 307 | |||
| 308 | if (!strcmp(event->name, "kmalloc_node") || | ||
| 309 | !strcmp(event->name, "kmem_cache_alloc_node")) { | ||
| 310 | process_alloc_event(raw, event, cpu, timestamp, thread, 1); | ||
| 311 | return; | ||
| 312 | } | ||
| 313 | |||
| 314 | if (!strcmp(event->name, "kfree") || | ||
| 315 | !strcmp(event->name, "kmem_cache_free")) { | ||
| 316 | process_free_event(raw, event, cpu, timestamp, thread); | ||
| 317 | return; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | static int process_sample_event(event_t *event) | ||
| 322 | { | ||
| 323 | u64 ip = event->ip.ip; | ||
| 324 | u64 timestamp = -1; | ||
| 325 | u32 cpu = -1; | ||
| 326 | u64 period = 1; | ||
| 327 | void *more_data = event->ip.__more_data; | ||
| 328 | struct thread *thread = threads__findnew(event->ip.pid); | ||
| 329 | |||
| 330 | if (sample_type & PERF_SAMPLE_TIME) { | ||
| 331 | timestamp = *(u64 *)more_data; | ||
| 332 | more_data += sizeof(u64); | ||
| 333 | } | ||
| 334 | |||
| 335 | if (sample_type & PERF_SAMPLE_CPU) { | ||
| 336 | cpu = *(u32 *)more_data; | ||
| 337 | more_data += sizeof(u32); | ||
| 338 | more_data += sizeof(u32); /* reserved */ | ||
| 339 | } | ||
| 340 | |||
| 341 | if (sample_type & PERF_SAMPLE_PERIOD) { | ||
| 342 | period = *(u64 *)more_data; | ||
| 343 | more_data += sizeof(u64); | ||
| 344 | } | ||
| 345 | |||
| 346 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", | ||
| 347 | event->header.misc, | ||
| 348 | event->ip.pid, event->ip.tid, | ||
| 349 | (void *)(long)ip, | ||
| 350 | (long long)period); | ||
| 351 | |||
| 352 | if (thread == NULL) { | ||
| 353 | pr_debug("problem processing %d event, skipping it.\n", | ||
| 354 | event->header.type); | ||
| 355 | return -1; | ||
| 356 | } | ||
| 357 | |||
| 358 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 359 | |||
| 360 | process_raw_event(event, more_data, cpu, timestamp, thread); | ||
| 361 | |||
| 362 | return 0; | ||
| 363 | } | ||
| 364 | |||
| 365 | static int sample_type_check(u64 type) | ||
| 366 | { | ||
| 367 | sample_type = type; | ||
| 368 | |||
| 369 | if (!(sample_type & PERF_SAMPLE_RAW)) { | ||
| 370 | fprintf(stderr, | ||
| 371 | "No trace sample to read. Did you call perf record " | ||
| 372 | "without -R?"); | ||
| 373 | return -1; | ||
| 374 | } | ||
| 375 | |||
| 376 | return 0; | ||
| 377 | } | ||
| 378 | |||
| 379 | static struct perf_file_handler file_handler = { | ||
| 380 | .process_sample_event = process_sample_event, | ||
| 381 | .process_comm_event = event__process_comm, | ||
| 382 | .sample_type_check = sample_type_check, | ||
| 383 | }; | ||
| 384 | |||
| 385 | static int read_events(void) | ||
| 386 | { | ||
| 387 | register_idle_thread(); | ||
| 388 | register_perf_file_handler(&file_handler); | ||
| 389 | |||
| 390 | return mmap_dispatch_perf_file(&header, input_name, 0, 0, | ||
| 391 | &event__cwdlen, &event__cwd); | ||
| 392 | } | ||
| 393 | |||
| 394 | static double fragmentation(unsigned long n_req, unsigned long n_alloc) | ||
| 395 | { | ||
| 396 | if (n_alloc == 0) | ||
| 397 | return 0.0; | ||
| 398 | else | ||
| 399 | return 100.0 - (100.0 * n_req / n_alloc); | ||
| 400 | } | ||
| 401 | |||
| 402 | static void __print_result(struct rb_root *root, int n_lines, int is_caller) | ||
| 403 | { | ||
| 404 | struct rb_node *next; | ||
| 405 | |||
| 406 | printf("%.102s\n", graph_dotted_line); | ||
| 407 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); | ||
| 408 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); | ||
| 409 | printf("%.102s\n", graph_dotted_line); | ||
| 410 | |||
| 411 | next = rb_first(root); | ||
| 412 | |||
| 413 | while (next && n_lines--) { | ||
| 414 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, | ||
| 415 | node); | ||
| 416 | struct symbol *sym = NULL; | ||
| 417 | char buf[BUFSIZ]; | ||
| 418 | u64 addr; | ||
| 419 | |||
| 420 | if (is_caller) { | ||
| 421 | addr = data->call_site; | ||
| 422 | if (!raw_ip) | ||
| 423 | sym = thread__find_function(kthread, addr, NULL); | ||
| 424 | } else | ||
| 425 | addr = data->ptr; | ||
| 426 | |||
| 427 | if (sym != NULL) | ||
| 428 | snprintf(buf, sizeof(buf), "%s+%Lx", sym->name, | ||
| 429 | addr - sym->start); | ||
| 430 | else | ||
| 431 | snprintf(buf, sizeof(buf), "%#Lx", addr); | ||
| 432 | printf(" %-34s |", buf); | ||
| 433 | |||
| 434 | printf(" %9llu/%-5lu | %9llu/%-5lu | %6lu | %8lu | %6.3f%%\n", | ||
| 435 | (unsigned long long)data->bytes_alloc, | ||
| 436 | (unsigned long)data->bytes_alloc / data->hit, | ||
| 437 | (unsigned long long)data->bytes_req, | ||
| 438 | (unsigned long)data->bytes_req / data->hit, | ||
| 439 | (unsigned long)data->hit, | ||
| 440 | (unsigned long)data->pingpong, | ||
| 441 | fragmentation(data->bytes_req, data->bytes_alloc)); | ||
| 442 | |||
| 443 | next = rb_next(next); | ||
| 444 | } | ||
| 445 | |||
| 446 | if (n_lines == -1) | ||
| 447 | printf(" ... | ... | ... | ... | ... | ... \n"); | ||
| 448 | |||
| 449 | printf("%.102s\n", graph_dotted_line); | ||
| 450 | } | ||
| 451 | |||
| 452 | static void print_summary(void) | ||
| 453 | { | ||
| 454 | printf("\nSUMMARY\n=======\n"); | ||
| 455 | printf("Total bytes requested: %lu\n", total_requested); | ||
| 456 | printf("Total bytes allocated: %lu\n", total_allocated); | ||
| 457 | printf("Total bytes wasted on internal fragmentation: %lu\n", | ||
| 458 | total_allocated - total_requested); | ||
| 459 | printf("Internal fragmentation: %f%%\n", | ||
| 460 | fragmentation(total_requested, total_allocated)); | ||
| 461 | printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs); | ||
| 462 | } | ||
| 463 | |||
| 464 | static void print_result(void) | ||
| 465 | { | ||
| 466 | if (caller_flag) | ||
| 467 | __print_result(&root_caller_sorted, caller_lines, 1); | ||
| 468 | if (alloc_flag) | ||
| 469 | __print_result(&root_alloc_sorted, alloc_lines, 0); | ||
| 470 | print_summary(); | ||
| 471 | } | ||
| 472 | |||
| 473 | struct sort_dimension { | ||
| 474 | const char name[20]; | ||
| 475 | sort_fn_t cmp; | ||
| 476 | struct list_head list; | ||
| 477 | }; | ||
| 478 | |||
| 479 | static LIST_HEAD(caller_sort); | ||
| 480 | static LIST_HEAD(alloc_sort); | ||
| 481 | |||
| 482 | static void sort_insert(struct rb_root *root, struct alloc_stat *data, | ||
| 483 | struct list_head *sort_list) | ||
| 484 | { | ||
| 485 | struct rb_node **new = &(root->rb_node); | ||
| 486 | struct rb_node *parent = NULL; | ||
| 487 | struct sort_dimension *sort; | ||
| 488 | |||
| 489 | while (*new) { | ||
| 490 | struct alloc_stat *this; | ||
| 491 | int cmp = 0; | ||
| 492 | |||
| 493 | this = rb_entry(*new, struct alloc_stat, node); | ||
| 494 | parent = *new; | ||
| 495 | |||
| 496 | list_for_each_entry(sort, sort_list, list) { | ||
| 497 | cmp = sort->cmp(data, this); | ||
| 498 | if (cmp) | ||
| 499 | break; | ||
| 500 | } | ||
| 501 | |||
| 502 | if (cmp > 0) | ||
| 503 | new = &((*new)->rb_left); | ||
| 504 | else | ||
| 505 | new = &((*new)->rb_right); | ||
| 506 | } | ||
| 507 | |||
| 508 | rb_link_node(&data->node, parent, new); | ||
| 509 | rb_insert_color(&data->node, root); | ||
| 510 | } | ||
| 511 | |||
| 512 | static void __sort_result(struct rb_root *root, struct rb_root *root_sorted, | ||
| 513 | struct list_head *sort_list) | ||
| 514 | { | ||
| 515 | struct rb_node *node; | ||
| 516 | struct alloc_stat *data; | ||
| 517 | |||
| 518 | for (;;) { | ||
| 519 | node = rb_first(root); | ||
| 520 | if (!node) | ||
| 521 | break; | ||
| 522 | |||
| 523 | rb_erase(node, root); | ||
| 524 | data = rb_entry(node, struct alloc_stat, node); | ||
| 525 | sort_insert(root_sorted, data, sort_list); | ||
| 526 | } | ||
| 527 | } | ||
| 528 | |||
| 529 | static void sort_result(void) | ||
| 530 | { | ||
| 531 | __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort); | ||
| 532 | __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort); | ||
| 533 | } | ||
| 534 | |||
| 535 | static int __cmd_kmem(void) | ||
| 536 | { | ||
| 537 | setup_pager(); | ||
| 538 | read_events(); | ||
| 539 | sort_result(); | ||
| 540 | print_result(); | ||
| 541 | |||
| 542 | return 0; | ||
| 543 | } | ||
| 544 | |||
| 545 | static const char * const kmem_usage[] = { | ||
| 546 | "perf kmem [<options>] {record}", | ||
| 547 | NULL | ||
| 548 | }; | ||
| 549 | |||
| 550 | static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r) | ||
| 551 | { | ||
| 552 | if (l->ptr < r->ptr) | ||
| 553 | return -1; | ||
| 554 | else if (l->ptr > r->ptr) | ||
| 555 | return 1; | ||
| 556 | return 0; | ||
| 557 | } | ||
| 558 | |||
| 559 | static struct sort_dimension ptr_sort_dimension = { | ||
| 560 | .name = "ptr", | ||
| 561 | .cmp = ptr_cmp, | ||
| 562 | }; | ||
| 563 | |||
| 564 | static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r) | ||
| 565 | { | ||
| 566 | if (l->call_site < r->call_site) | ||
| 567 | return -1; | ||
| 568 | else if (l->call_site > r->call_site) | ||
| 569 | return 1; | ||
| 570 | return 0; | ||
| 571 | } | ||
| 572 | |||
| 573 | static struct sort_dimension callsite_sort_dimension = { | ||
| 574 | .name = "callsite", | ||
| 575 | .cmp = callsite_cmp, | ||
| 576 | }; | ||
| 577 | |||
| 578 | static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r) | ||
| 579 | { | ||
| 580 | if (l->hit < r->hit) | ||
| 581 | return -1; | ||
| 582 | else if (l->hit > r->hit) | ||
| 583 | return 1; | ||
| 584 | return 0; | ||
| 585 | } | ||
| 586 | |||
| 587 | static struct sort_dimension hit_sort_dimension = { | ||
| 588 | .name = "hit", | ||
| 589 | .cmp = hit_cmp, | ||
| 590 | }; | ||
| 591 | |||
| 592 | static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r) | ||
| 593 | { | ||
| 594 | if (l->bytes_alloc < r->bytes_alloc) | ||
| 595 | return -1; | ||
| 596 | else if (l->bytes_alloc > r->bytes_alloc) | ||
| 597 | return 1; | ||
| 598 | return 0; | ||
| 599 | } | ||
| 600 | |||
| 601 | static struct sort_dimension bytes_sort_dimension = { | ||
| 602 | .name = "bytes", | ||
| 603 | .cmp = bytes_cmp, | ||
| 604 | }; | ||
| 605 | |||
| 606 | static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r) | ||
| 607 | { | ||
| 608 | double x, y; | ||
| 609 | |||
| 610 | x = fragmentation(l->bytes_req, l->bytes_alloc); | ||
| 611 | y = fragmentation(r->bytes_req, r->bytes_alloc); | ||
| 612 | |||
| 613 | if (x < y) | ||
| 614 | return -1; | ||
| 615 | else if (x > y) | ||
| 616 | return 1; | ||
| 617 | return 0; | ||
| 618 | } | ||
| 619 | |||
| 620 | static struct sort_dimension frag_sort_dimension = { | ||
| 621 | .name = "frag", | ||
| 622 | .cmp = frag_cmp, | ||
| 623 | }; | ||
| 624 | |||
| 625 | static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r) | ||
| 626 | { | ||
| 627 | if (l->pingpong < r->pingpong) | ||
| 628 | return -1; | ||
| 629 | else if (l->pingpong > r->pingpong) | ||
| 630 | return 1; | ||
| 631 | return 0; | ||
| 632 | } | ||
| 633 | |||
| 634 | static struct sort_dimension pingpong_sort_dimension = { | ||
| 635 | .name = "pingpong", | ||
| 636 | .cmp = pingpong_cmp, | ||
| 637 | }; | ||
| 638 | |||
| 639 | static struct sort_dimension *avail_sorts[] = { | ||
| 640 | &ptr_sort_dimension, | ||
| 641 | &callsite_sort_dimension, | ||
| 642 | &hit_sort_dimension, | ||
| 643 | &bytes_sort_dimension, | ||
| 644 | &frag_sort_dimension, | ||
| 645 | &pingpong_sort_dimension, | ||
| 646 | }; | ||
| 647 | |||
| 648 | #define NUM_AVAIL_SORTS \ | ||
| 649 | (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *)) | ||
| 650 | |||
| 651 | static int sort_dimension__add(const char *tok, struct list_head *list) | ||
| 652 | { | ||
| 653 | struct sort_dimension *sort; | ||
| 654 | int i; | ||
| 655 | |||
| 656 | for (i = 0; i < NUM_AVAIL_SORTS; i++) { | ||
| 657 | if (!strcmp(avail_sorts[i]->name, tok)) { | ||
| 658 | sort = malloc(sizeof(*sort)); | ||
| 659 | if (!sort) | ||
| 660 | die("malloc"); | ||
| 661 | memcpy(sort, avail_sorts[i], sizeof(*sort)); | ||
| 662 | list_add_tail(&sort->list, list); | ||
| 663 | return 0; | ||
| 664 | } | ||
| 665 | } | ||
| 666 | |||
| 667 | return -1; | ||
| 668 | } | ||
| 669 | |||
| 670 | static int setup_sorting(struct list_head *sort_list, const char *arg) | ||
| 671 | { | ||
| 672 | char *tok; | ||
| 673 | char *str = strdup(arg); | ||
| 674 | |||
| 675 | if (!str) | ||
| 676 | die("strdup"); | ||
| 677 | |||
| 678 | while (true) { | ||
| 679 | tok = strsep(&str, ","); | ||
| 680 | if (!tok) | ||
| 681 | break; | ||
| 682 | if (sort_dimension__add(tok, sort_list) < 0) { | ||
| 683 | error("Unknown --sort key: '%s'", tok); | ||
| 684 | return -1; | ||
| 685 | } | ||
| 686 | } | ||
| 687 | |||
| 688 | free(str); | ||
| 689 | return 0; | ||
| 690 | } | ||
| 691 | |||
| 692 | static int parse_sort_opt(const struct option *opt __used, | ||
| 693 | const char *arg, int unset __used) | ||
| 694 | { | ||
| 695 | if (!arg) | ||
| 696 | return -1; | ||
| 697 | |||
| 698 | if (caller_flag > alloc_flag) | ||
| 699 | return setup_sorting(&caller_sort, arg); | ||
| 700 | else | ||
| 701 | return setup_sorting(&alloc_sort, arg); | ||
| 702 | |||
| 703 | return 0; | ||
| 704 | } | ||
| 705 | |||
| 706 | static int parse_stat_opt(const struct option *opt __used, | ||
| 707 | const char *arg, int unset __used) | ||
| 708 | { | ||
| 709 | if (!arg) | ||
| 710 | return -1; | ||
| 711 | |||
| 712 | if (strcmp(arg, "alloc") == 0) | ||
| 713 | alloc_flag = (caller_flag + 1); | ||
| 714 | else if (strcmp(arg, "caller") == 0) | ||
| 715 | caller_flag = (alloc_flag + 1); | ||
| 716 | else | ||
| 717 | return -1; | ||
| 718 | return 0; | ||
| 719 | } | ||
| 720 | |||
| 721 | static int parse_line_opt(const struct option *opt __used, | ||
| 722 | const char *arg, int unset __used) | ||
| 723 | { | ||
| 724 | int lines; | ||
| 725 | |||
| 726 | if (!arg) | ||
| 727 | return -1; | ||
| 728 | |||
| 729 | lines = strtoul(arg, NULL, 10); | ||
| 730 | |||
| 731 | if (caller_flag > alloc_flag) | ||
| 732 | caller_lines = lines; | ||
| 733 | else | ||
| 734 | alloc_lines = lines; | ||
| 735 | |||
| 736 | return 0; | ||
| 737 | } | ||
| 738 | |||
| 739 | static const struct option kmem_options[] = { | ||
| 740 | OPT_STRING('i', "input", &input_name, "file", | ||
| 741 | "input file name"), | ||
| 742 | OPT_CALLBACK(0, "stat", NULL, "<alloc>|<caller>", | ||
| 743 | "stat selector, Pass 'alloc' or 'caller'.", | ||
| 744 | parse_stat_opt), | ||
| 745 | OPT_CALLBACK('s', "sort", NULL, "key[,key2...]", | ||
| 746 | "sort by keys: ptr, call_site, bytes, hit, pingpong, frag", | ||
| 747 | parse_sort_opt), | ||
| 748 | OPT_CALLBACK('l', "line", NULL, "num", | ||
| 749 | "show n lins", | ||
| 750 | parse_line_opt), | ||
| 751 | OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), | ||
| 752 | OPT_END() | ||
| 753 | }; | ||
| 754 | |||
| 755 | static const char *record_args[] = { | ||
| 756 | "record", | ||
| 757 | "-a", | ||
| 758 | "-R", | ||
| 759 | "-M", | ||
| 760 | "-f", | ||
| 761 | "-c", "1", | ||
| 762 | "-e", "kmem:kmalloc", | ||
| 763 | "-e", "kmem:kmalloc_node", | ||
| 764 | "-e", "kmem:kfree", | ||
| 765 | "-e", "kmem:kmem_cache_alloc", | ||
| 766 | "-e", "kmem:kmem_cache_alloc_node", | ||
| 767 | "-e", "kmem:kmem_cache_free", | ||
| 768 | }; | ||
| 769 | |||
| 770 | static int __cmd_record(int argc, const char **argv) | ||
| 771 | { | ||
| 772 | unsigned int rec_argc, i, j; | ||
| 773 | const char **rec_argv; | ||
| 774 | |||
| 775 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | ||
| 776 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | ||
| 777 | |||
| 778 | for (i = 0; i < ARRAY_SIZE(record_args); i++) | ||
| 779 | rec_argv[i] = strdup(record_args[i]); | ||
| 780 | |||
| 781 | for (j = 1; j < (unsigned int)argc; j++, i++) | ||
| 782 | rec_argv[i] = argv[j]; | ||
| 783 | |||
| 784 | return cmd_record(i, rec_argv, NULL); | ||
| 785 | } | ||
| 786 | |||
| 787 | int cmd_kmem(int argc, const char **argv, const char *prefix __used) | ||
| 788 | { | ||
| 789 | symbol__init(0); | ||
| 790 | |||
| 791 | argc = parse_options(argc, argv, kmem_options, kmem_usage, 0); | ||
| 792 | |||
| 793 | if (argc && !strncmp(argv[0], "rec", 3)) | ||
| 794 | return __cmd_record(argc, argv); | ||
| 795 | else if (argc) | ||
| 796 | usage_with_options(kmem_usage, kmem_options); | ||
| 797 | |||
| 798 | if (list_empty(&caller_sort)) | ||
| 799 | setup_sorting(&caller_sort, default_sort_order); | ||
| 800 | if (list_empty(&alloc_sort)) | ||
| 801 | setup_sorting(&alloc_sort, default_sort_order); | ||
| 802 | |||
| 803 | setup_cpunode_map(); | ||
| 804 | |||
| 805 | return __cmd_kmem(); | ||
| 806 | } | ||
| 807 | |||
diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c new file mode 100644 index 000000000000..a58e11b7ea80 --- /dev/null +++ b/tools/perf/builtin-probe.c | |||
| @@ -0,0 +1,242 @@ | |||
| 1 | /* | ||
| 2 | * builtin-probe.c | ||
| 3 | * | ||
| 4 | * Builtin probe command: Set up probe events by C expression | ||
| 5 | * | ||
| 6 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU General Public License as published by | ||
| 10 | * the Free Software Foundation; either version 2 of the License, or | ||
| 11 | * (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This program is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU General Public License | ||
| 19 | * along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 21 | * | ||
| 22 | */ | ||
| 23 | #define _GNU_SOURCE | ||
| 24 | #include <sys/utsname.h> | ||
| 25 | #include <sys/types.h> | ||
| 26 | #include <sys/stat.h> | ||
| 27 | #include <fcntl.h> | ||
| 28 | #include <errno.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <unistd.h> | ||
| 31 | #include <stdlib.h> | ||
| 32 | #include <string.h> | ||
| 33 | |||
| 34 | #undef _GNU_SOURCE | ||
| 35 | #include "perf.h" | ||
| 36 | #include "builtin.h" | ||
| 37 | #include "util/util.h" | ||
| 38 | #include "util/event.h" | ||
| 39 | #include "util/debug.h" | ||
| 40 | #include "util/parse-options.h" | ||
| 41 | #include "util/parse-events.h" /* For debugfs_path */ | ||
| 42 | #include "util/probe-finder.h" | ||
| 43 | #include "util/probe-event.h" | ||
| 44 | |||
| 45 | /* Default vmlinux search paths */ | ||
| 46 | #define NR_SEARCH_PATH 3 | ||
| 47 | const char *default_search_path[NR_SEARCH_PATH] = { | ||
| 48 | "/lib/modules/%s/build/vmlinux", /* Custom build kernel */ | ||
| 49 | "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */ | ||
| 50 | "/boot/vmlinux-debug-%s", /* Ubuntu */ | ||
| 51 | }; | ||
| 52 | |||
| 53 | #define MAX_PATH_LEN 256 | ||
| 54 | #define MAX_PROBES 128 | ||
| 55 | |||
| 56 | /* Session management structure */ | ||
| 57 | static struct { | ||
| 58 | char *vmlinux; | ||
| 59 | char *release; | ||
| 60 | int need_dwarf; | ||
| 61 | int nr_probe; | ||
| 62 | struct probe_point probes[MAX_PROBES]; | ||
| 63 | } session; | ||
| 64 | |||
| 65 | static bool listing; | ||
| 66 | |||
| 67 | /* Parse an event definition. Note that any error must die. */ | ||
| 68 | static void parse_probe_event(const char *str) | ||
| 69 | { | ||
| 70 | struct probe_point *pp = &session.probes[session.nr_probe]; | ||
| 71 | |||
| 72 | pr_debug("probe-definition(%d): %s\n", session.nr_probe, str); | ||
| 73 | if (++session.nr_probe == MAX_PROBES) | ||
| 74 | die("Too many probes (> %d) are specified.", MAX_PROBES); | ||
| 75 | |||
| 76 | /* Parse perf-probe event into probe_point */ | ||
| 77 | session.need_dwarf = parse_perf_probe_event(str, pp); | ||
| 78 | |||
| 79 | pr_debug("%d arguments\n", pp->nr_args); | ||
| 80 | } | ||
| 81 | |||
| 82 | static int opt_add_probe_event(const struct option *opt __used, | ||
| 83 | const char *str, int unset __used) | ||
| 84 | { | ||
| 85 | if (str) | ||
| 86 | parse_probe_event(str); | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | #ifndef NO_LIBDWARF | ||
| 91 | static int open_default_vmlinux(void) | ||
| 92 | { | ||
| 93 | struct utsname uts; | ||
| 94 | char fname[MAX_PATH_LEN]; | ||
| 95 | int fd, ret, i; | ||
| 96 | |||
| 97 | ret = uname(&uts); | ||
| 98 | if (ret) { | ||
| 99 | pr_debug("uname() failed.\n"); | ||
| 100 | return -errno; | ||
| 101 | } | ||
| 102 | session.release = uts.release; | ||
| 103 | for (i = 0; i < NR_SEARCH_PATH; i++) { | ||
| 104 | ret = snprintf(fname, MAX_PATH_LEN, | ||
| 105 | default_search_path[i], session.release); | ||
| 106 | if (ret >= MAX_PATH_LEN || ret < 0) { | ||
| 107 | pr_debug("Filename(%d,%s) is too long.\n", i, | ||
| 108 | uts.release); | ||
| 109 | errno = E2BIG; | ||
| 110 | return -E2BIG; | ||
| 111 | } | ||
| 112 | pr_debug("try to open %s\n", fname); | ||
| 113 | fd = open(fname, O_RDONLY); | ||
| 114 | if (fd >= 0) | ||
| 115 | break; | ||
| 116 | } | ||
| 117 | return fd; | ||
| 118 | } | ||
| 119 | #endif | ||
| 120 | |||
| 121 | static const char * const probe_usage[] = { | ||
| 122 | "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]", | ||
| 123 | "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]", | ||
| 124 | "perf probe --list", | ||
| 125 | NULL | ||
| 126 | }; | ||
| 127 | |||
| 128 | static const struct option options[] = { | ||
| 129 | OPT_BOOLEAN('v', "verbose", &verbose, | ||
| 130 | "be more verbose (show parsed arguments, etc)"), | ||
| 131 | #ifndef NO_LIBDWARF | ||
| 132 | OPT_STRING('k', "vmlinux", &session.vmlinux, "file", | ||
| 133 | "vmlinux/module pathname"), | ||
| 134 | #endif | ||
| 135 | OPT_BOOLEAN('l', "list", &listing, "list up current probes"), | ||
| 136 | OPT_CALLBACK('a', "add", NULL, | ||
| 137 | #ifdef NO_LIBDWARF | ||
| 138 | "FUNC[+OFFS|%return] [ARG ...]", | ||
| 139 | #else | ||
| 140 | "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]", | ||
| 141 | #endif | ||
| 142 | "probe point definition, where\n" | ||
| 143 | "\t\tGRP:\tGroup name (optional)\n" | ||
| 144 | "\t\tNAME:\tEvent name\n" | ||
| 145 | "\t\tFUNC:\tFunction name\n" | ||
| 146 | "\t\tOFFS:\tOffset from function entry (in byte)\n" | ||
| 147 | "\t\t%return:\tPut the probe at function return\n" | ||
| 148 | #ifdef NO_LIBDWARF | ||
| 149 | "\t\tARG:\tProbe argument (only \n" | ||
| 150 | #else | ||
| 151 | "\t\tSRC:\tSource code path\n" | ||
| 152 | "\t\tRLN:\tRelative line number from function entry.\n" | ||
| 153 | "\t\tALN:\tAbsolute line number in file.\n" | ||
| 154 | "\t\tARG:\tProbe argument (local variable name or\n" | ||
| 155 | #endif | ||
| 156 | "\t\t\tkprobe-tracer argument format.)\n", | ||
| 157 | opt_add_probe_event), | ||
| 158 | OPT_END() | ||
| 159 | }; | ||
| 160 | |||
| 161 | int cmd_probe(int argc, const char **argv, const char *prefix __used) | ||
| 162 | { | ||
| 163 | int i, j, ret; | ||
| 164 | #ifndef NO_LIBDWARF | ||
| 165 | int fd; | ||
| 166 | #endif | ||
| 167 | struct probe_point *pp; | ||
| 168 | |||
| 169 | argc = parse_options(argc, argv, options, probe_usage, | ||
| 170 | PARSE_OPT_STOP_AT_NON_OPTION); | ||
| 171 | for (i = 0; i < argc; i++) | ||
| 172 | parse_probe_event(argv[i]); | ||
| 173 | |||
| 174 | if ((session.nr_probe == 0 && !listing) || | ||
| 175 | (session.nr_probe != 0 && listing)) | ||
| 176 | usage_with_options(probe_usage, options); | ||
| 177 | |||
| 178 | if (listing) { | ||
| 179 | show_perf_probe_events(); | ||
| 180 | return 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | if (session.need_dwarf) | ||
| 184 | #ifdef NO_LIBDWARF | ||
| 185 | die("Debuginfo-analysis is not supported"); | ||
| 186 | #else /* !NO_LIBDWARF */ | ||
| 187 | pr_debug("Some probes require debuginfo.\n"); | ||
| 188 | |||
| 189 | if (session.vmlinux) | ||
| 190 | fd = open(session.vmlinux, O_RDONLY); | ||
| 191 | else | ||
| 192 | fd = open_default_vmlinux(); | ||
| 193 | if (fd < 0) { | ||
| 194 | if (session.need_dwarf) | ||
| 195 | die("Could not open vmlinux/module file."); | ||
| 196 | |||
| 197 | pr_warning("Could not open vmlinux/module file." | ||
| 198 | " Try to use symbols.\n"); | ||
| 199 | goto end_dwarf; | ||
| 200 | } | ||
| 201 | |||
| 202 | /* Searching probe points */ | ||
| 203 | for (j = 0; j < session.nr_probe; j++) { | ||
| 204 | pp = &session.probes[j]; | ||
| 205 | if (pp->found) | ||
| 206 | continue; | ||
| 207 | |||
| 208 | lseek(fd, SEEK_SET, 0); | ||
| 209 | ret = find_probepoint(fd, pp); | ||
| 210 | if (ret < 0) { | ||
| 211 | if (session.need_dwarf) | ||
| 212 | die("Could not analyze debuginfo."); | ||
| 213 | |||
| 214 | pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n"); | ||
| 215 | break; | ||
| 216 | } | ||
| 217 | if (ret == 0) /* No error but failed to find probe point. */ | ||
| 218 | die("No probe point found."); | ||
| 219 | } | ||
| 220 | close(fd); | ||
| 221 | |||
| 222 | end_dwarf: | ||
| 223 | #endif /* !NO_LIBDWARF */ | ||
| 224 | |||
| 225 | /* Synthesize probes without dwarf */ | ||
| 226 | for (j = 0; j < session.nr_probe; j++) { | ||
| 227 | pp = &session.probes[j]; | ||
| 228 | if (pp->found) /* This probe is already found. */ | ||
| 229 | continue; | ||
| 230 | |||
| 231 | ret = synthesize_trace_kprobe_event(pp); | ||
| 232 | if (ret == -E2BIG) | ||
| 233 | die("probe point definition becomes too long."); | ||
| 234 | else if (ret < 0) | ||
| 235 | die("Failed to synthesize a probe point."); | ||
| 236 | } | ||
| 237 | |||
| 238 | /* Settng up probe points */ | ||
| 239 | add_trace_kprobe_events(session.probes, session.nr_probe); | ||
| 240 | return 0; | ||
| 241 | } | ||
| 242 | |||
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 99a12fe86e9f..0e519c667e3a 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c | |||
| @@ -17,52 +17,52 @@ | |||
| 17 | #include "util/header.h" | 17 | #include "util/header.h" |
| 18 | #include "util/event.h" | 18 | #include "util/event.h" |
| 19 | #include "util/debug.h" | 19 | #include "util/debug.h" |
| 20 | #include "util/trace-event.h" | 20 | #include "util/symbol.h" |
| 21 | 21 | ||
| 22 | #include <unistd.h> | 22 | #include <unistd.h> |
| 23 | #include <sched.h> | 23 | #include <sched.h> |
| 24 | 24 | ||
| 25 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) | ||
| 26 | #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) | ||
| 27 | |||
| 28 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; | 25 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 29 | 26 | ||
| 30 | static long default_interval = 100000; | 27 | static long default_interval = 0; |
| 31 | 28 | ||
| 32 | static int nr_cpus = 0; | 29 | static int nr_cpus = 0; |
| 33 | static unsigned int page_size; | 30 | static unsigned int page_size; |
| 34 | static unsigned int mmap_pages = 128; | 31 | static unsigned int mmap_pages = 128; |
| 35 | static int freq = 0; | 32 | static int freq = 1000; |
| 36 | static int output; | 33 | static int output; |
| 37 | static const char *output_name = "perf.data"; | 34 | static const char *output_name = "perf.data"; |
| 38 | static int group = 0; | 35 | static int group = 0; |
| 39 | static unsigned int realtime_prio = 0; | 36 | static unsigned int realtime_prio = 0; |
| 40 | static int raw_samples = 0; | 37 | static int raw_samples = 0; |
| 41 | static int system_wide = 0; | 38 | static int system_wide = 0; |
| 42 | static int profile_cpu = -1; | 39 | static int profile_cpu = -1; |
| 43 | static pid_t target_pid = -1; | 40 | static pid_t target_pid = -1; |
| 44 | static int inherit = 1; | 41 | static pid_t child_pid = -1; |
| 45 | static int force = 0; | 42 | static int inherit = 1; |
| 46 | static int append_file = 0; | 43 | static int force = 0; |
| 47 | static int call_graph = 0; | 44 | static int append_file = 0; |
| 48 | static int inherit_stat = 0; | 45 | static int call_graph = 0; |
| 49 | static int no_samples = 0; | 46 | static int inherit_stat = 0; |
| 50 | static int sample_address = 0; | 47 | static int no_samples = 0; |
| 51 | 48 | static int sample_address = 0; | |
| 52 | static long samples; | 49 | static int multiplex = 0; |
| 50 | static int multiplex_fd = -1; | ||
| 51 | |||
| 52 | static long samples = 0; | ||
| 53 | static struct timeval last_read; | 53 | static struct timeval last_read; |
| 54 | static struct timeval this_read; | 54 | static struct timeval this_read; |
| 55 | 55 | ||
| 56 | static u64 bytes_written; | 56 | static u64 bytes_written = 0; |
| 57 | 57 | ||
| 58 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; | 58 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
| 59 | 59 | ||
| 60 | static int nr_poll; | 60 | static int nr_poll = 0; |
| 61 | static int nr_cpu; | 61 | static int nr_cpu = 0; |
| 62 | 62 | ||
| 63 | static int file_new = 1; | 63 | static int file_new = 1; |
| 64 | 64 | ||
| 65 | struct perf_header *header; | 65 | struct perf_header *header = NULL; |
| 66 | 66 | ||
| 67 | struct mmap_data { | 67 | struct mmap_data { |
| 68 | int counter; | 68 | int counter; |
| @@ -75,7 +75,7 @@ static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; | |||
| 75 | 75 | ||
| 76 | static unsigned long mmap_read_head(struct mmap_data *md) | 76 | static unsigned long mmap_read_head(struct mmap_data *md) |
| 77 | { | 77 | { |
| 78 | struct perf_counter_mmap_page *pc = md->base; | 78 | struct perf_event_mmap_page *pc = md->base; |
| 79 | long head; | 79 | long head; |
| 80 | 80 | ||
| 81 | head = pc->data_head; | 81 | head = pc->data_head; |
| @@ -86,7 +86,7 @@ static unsigned long mmap_read_head(struct mmap_data *md) | |||
| 86 | 86 | ||
| 87 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) | 87 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) |
| 88 | { | 88 | { |
| 89 | struct perf_counter_mmap_page *pc = md->base; | 89 | struct perf_event_mmap_page *pc = md->base; |
| 90 | 90 | ||
| 91 | /* | 91 | /* |
| 92 | * ensure all reads are done before we write the tail out. | 92 | * ensure all reads are done before we write the tail out. |
| @@ -110,6 +110,24 @@ static void write_output(void *buf, size_t size) | |||
| 110 | } | 110 | } |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | static void write_event(event_t *buf, size_t size) | ||
| 114 | { | ||
| 115 | /* | ||
| 116 | * Add it to the list of DSOs, so that when we finish this | ||
| 117 | * record session we can pick the available build-ids. | ||
| 118 | */ | ||
| 119 | if (buf->header.type == PERF_RECORD_MMAP) | ||
| 120 | dsos__findnew(buf->mmap.filename); | ||
| 121 | |||
| 122 | write_output(buf, size); | ||
| 123 | } | ||
| 124 | |||
| 125 | static int process_synthesized_event(event_t *event) | ||
| 126 | { | ||
| 127 | write_event(event, event->header.size); | ||
| 128 | return 0; | ||
| 129 | } | ||
| 130 | |||
| 113 | static void mmap_read(struct mmap_data *md) | 131 | static void mmap_read(struct mmap_data *md) |
| 114 | { | 132 | { |
| 115 | unsigned int head = mmap_read_head(md); | 133 | unsigned int head = mmap_read_head(md); |
| @@ -158,14 +176,14 @@ static void mmap_read(struct mmap_data *md) | |||
| 158 | size = md->mask + 1 - (old & md->mask); | 176 | size = md->mask + 1 - (old & md->mask); |
| 159 | old += size; | 177 | old += size; |
| 160 | 178 | ||
| 161 | write_output(buf, size); | 179 | write_event(buf, size); |
| 162 | } | 180 | } |
| 163 | 181 | ||
| 164 | buf = &data[old & md->mask]; | 182 | buf = &data[old & md->mask]; |
| 165 | size = head - old; | 183 | size = head - old; |
| 166 | old += size; | 184 | old += size; |
| 167 | 185 | ||
| 168 | write_output(buf, size); | 186 | write_event(buf, size); |
| 169 | 187 | ||
| 170 | md->prev = old; | 188 | md->prev = old; |
| 171 | mmap_write_tail(md, old); | 189 | mmap_write_tail(md, old); |
| @@ -182,6 +200,9 @@ static void sig_handler(int sig) | |||
| 182 | 200 | ||
| 183 | static void sig_atexit(void) | 201 | static void sig_atexit(void) |
| 184 | { | 202 | { |
| 203 | if (child_pid != -1) | ||
| 204 | kill(child_pid, SIGTERM); | ||
| 205 | |||
| 185 | if (signr == -1) | 206 | if (signr == -1) |
| 186 | return; | 207 | return; |
| 187 | 208 | ||
| @@ -189,171 +210,9 @@ static void sig_atexit(void) | |||
| 189 | kill(getpid(), signr); | 210 | kill(getpid(), signr); |
| 190 | } | 211 | } |
| 191 | 212 | ||
| 192 | static pid_t pid_synthesize_comm_event(pid_t pid, int full) | ||
| 193 | { | ||
| 194 | struct comm_event comm_ev; | ||
| 195 | char filename[PATH_MAX]; | ||
| 196 | char bf[BUFSIZ]; | ||
| 197 | FILE *fp; | ||
| 198 | size_t size = 0; | ||
| 199 | DIR *tasks; | ||
| 200 | struct dirent dirent, *next; | ||
| 201 | pid_t tgid = 0; | ||
| 202 | |||
| 203 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | ||
| 204 | |||
| 205 | fp = fopen(filename, "r"); | ||
| 206 | if (fp == NULL) { | ||
| 207 | /* | ||
| 208 | * We raced with a task exiting - just return: | ||
| 209 | */ | ||
| 210 | if (verbose) | ||
| 211 | fprintf(stderr, "couldn't open %s\n", filename); | ||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | memset(&comm_ev, 0, sizeof(comm_ev)); | ||
| 216 | while (!comm_ev.comm[0] || !comm_ev.pid) { | ||
| 217 | if (fgets(bf, sizeof(bf), fp) == NULL) | ||
| 218 | goto out_failure; | ||
| 219 | |||
| 220 | if (memcmp(bf, "Name:", 5) == 0) { | ||
| 221 | char *name = bf + 5; | ||
| 222 | while (*name && isspace(*name)) | ||
| 223 | ++name; | ||
| 224 | size = strlen(name) - 1; | ||
| 225 | memcpy(comm_ev.comm, name, size++); | ||
| 226 | } else if (memcmp(bf, "Tgid:", 5) == 0) { | ||
| 227 | char *tgids = bf + 5; | ||
| 228 | while (*tgids && isspace(*tgids)) | ||
| 229 | ++tgids; | ||
| 230 | tgid = comm_ev.pid = atoi(tgids); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | comm_ev.header.type = PERF_EVENT_COMM; | ||
| 235 | size = ALIGN(size, sizeof(u64)); | ||
| 236 | comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); | ||
| 237 | |||
| 238 | if (!full) { | ||
| 239 | comm_ev.tid = pid; | ||
| 240 | |||
| 241 | write_output(&comm_ev, comm_ev.header.size); | ||
| 242 | goto out_fclose; | ||
| 243 | } | ||
| 244 | |||
| 245 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | ||
| 246 | |||
| 247 | tasks = opendir(filename); | ||
| 248 | while (!readdir_r(tasks, &dirent, &next) && next) { | ||
| 249 | char *end; | ||
| 250 | pid = strtol(dirent.d_name, &end, 10); | ||
| 251 | if (*end) | ||
| 252 | continue; | ||
| 253 | |||
| 254 | comm_ev.tid = pid; | ||
| 255 | |||
| 256 | write_output(&comm_ev, comm_ev.header.size); | ||
| 257 | } | ||
| 258 | closedir(tasks); | ||
| 259 | |||
| 260 | out_fclose: | ||
| 261 | fclose(fp); | ||
| 262 | return tgid; | ||
| 263 | |||
| 264 | out_failure: | ||
| 265 | fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", | ||
| 266 | filename); | ||
| 267 | exit(EXIT_FAILURE); | ||
| 268 | } | ||
| 269 | |||
| 270 | static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid) | ||
| 271 | { | ||
| 272 | char filename[PATH_MAX]; | ||
| 273 | FILE *fp; | ||
| 274 | |||
| 275 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); | ||
| 276 | |||
| 277 | fp = fopen(filename, "r"); | ||
| 278 | if (fp == NULL) { | ||
| 279 | /* | ||
| 280 | * We raced with a task exiting - just return: | ||
| 281 | */ | ||
| 282 | if (verbose) | ||
| 283 | fprintf(stderr, "couldn't open %s\n", filename); | ||
| 284 | return; | ||
| 285 | } | ||
| 286 | while (1) { | ||
| 287 | char bf[BUFSIZ], *pbf = bf; | ||
| 288 | struct mmap_event mmap_ev = { | ||
| 289 | .header = { .type = PERF_EVENT_MMAP }, | ||
| 290 | }; | ||
| 291 | int n; | ||
| 292 | size_t size; | ||
| 293 | if (fgets(bf, sizeof(bf), fp) == NULL) | ||
| 294 | break; | ||
| 295 | |||
| 296 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ | ||
| 297 | n = hex2u64(pbf, &mmap_ev.start); | ||
| 298 | if (n < 0) | ||
| 299 | continue; | ||
| 300 | pbf += n + 1; | ||
| 301 | n = hex2u64(pbf, &mmap_ev.len); | ||
| 302 | if (n < 0) | ||
| 303 | continue; | ||
| 304 | pbf += n + 3; | ||
| 305 | if (*pbf == 'x') { /* vm_exec */ | ||
| 306 | char *execname = strchr(bf, '/'); | ||
| 307 | |||
| 308 | /* Catch VDSO */ | ||
| 309 | if (execname == NULL) | ||
| 310 | execname = strstr(bf, "[vdso]"); | ||
| 311 | |||
| 312 | if (execname == NULL) | ||
| 313 | continue; | ||
| 314 | |||
| 315 | size = strlen(execname); | ||
| 316 | execname[size - 1] = '\0'; /* Remove \n */ | ||
| 317 | memcpy(mmap_ev.filename, execname, size); | ||
| 318 | size = ALIGN(size, sizeof(u64)); | ||
| 319 | mmap_ev.len -= mmap_ev.start; | ||
| 320 | mmap_ev.header.size = (sizeof(mmap_ev) - | ||
| 321 | (sizeof(mmap_ev.filename) - size)); | ||
| 322 | mmap_ev.pid = tgid; | ||
| 323 | mmap_ev.tid = pid; | ||
| 324 | |||
| 325 | write_output(&mmap_ev, mmap_ev.header.size); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | fclose(fp); | ||
| 330 | } | ||
| 331 | |||
| 332 | static void synthesize_all(void) | ||
| 333 | { | ||
| 334 | DIR *proc; | ||
| 335 | struct dirent dirent, *next; | ||
| 336 | |||
| 337 | proc = opendir("/proc"); | ||
| 338 | |||
| 339 | while (!readdir_r(proc, &dirent, &next) && next) { | ||
| 340 | char *end; | ||
| 341 | pid_t pid, tgid; | ||
| 342 | |||
| 343 | pid = strtol(dirent.d_name, &end, 10); | ||
| 344 | if (*end) /* only interested in proper numerical dirents */ | ||
| 345 | continue; | ||
| 346 | |||
| 347 | tgid = pid_synthesize_comm_event(pid, 1); | ||
| 348 | pid_synthesize_mmap_samples(pid, tgid); | ||
| 349 | } | ||
| 350 | |||
| 351 | closedir(proc); | ||
| 352 | } | ||
| 353 | |||
| 354 | static int group_fd; | 213 | static int group_fd; |
| 355 | 214 | ||
| 356 | static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr) | 215 | static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) |
| 357 | { | 216 | { |
| 358 | struct perf_header_attr *h_attr; | 217 | struct perf_header_attr *h_attr; |
| 359 | 218 | ||
| @@ -361,7 +220,11 @@ static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int | |||
| 361 | h_attr = header->attr[nr]; | 220 | h_attr = header->attr[nr]; |
| 362 | } else { | 221 | } else { |
| 363 | h_attr = perf_header_attr__new(a); | 222 | h_attr = perf_header_attr__new(a); |
| 364 | perf_header__add_attr(header, h_attr); | 223 | if (h_attr != NULL) |
| 224 | if (perf_header__add_attr(header, h_attr) < 0) { | ||
| 225 | perf_header_attr__delete(h_attr); | ||
| 226 | h_attr = NULL; | ||
| 227 | } | ||
| 365 | } | 228 | } |
| 366 | 229 | ||
| 367 | return h_attr; | 230 | return h_attr; |
| @@ -369,9 +232,11 @@ static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int | |||
| 369 | 232 | ||
| 370 | static void create_counter(int counter, int cpu, pid_t pid) | 233 | static void create_counter(int counter, int cpu, pid_t pid) |
| 371 | { | 234 | { |
| 372 | struct perf_counter_attr *attr = attrs + counter; | 235 | char *filter = filters[counter]; |
| 236 | struct perf_event_attr *attr = attrs + counter; | ||
| 373 | struct perf_header_attr *h_attr; | 237 | struct perf_header_attr *h_attr; |
| 374 | int track = !counter; /* only the first counter needs these */ | 238 | int track = !counter; /* only the first counter needs these */ |
| 239 | int ret; | ||
| 375 | struct { | 240 | struct { |
| 376 | u64 count; | 241 | u64 count; |
| 377 | u64 time_enabled; | 242 | u64 time_enabled; |
| @@ -415,12 +280,12 @@ static void create_counter(int counter, int cpu, pid_t pid) | |||
| 415 | attr->disabled = 1; | 280 | attr->disabled = 1; |
| 416 | 281 | ||
| 417 | try_again: | 282 | try_again: |
| 418 | fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0); | 283 | fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0); |
| 419 | 284 | ||
| 420 | if (fd[nr_cpu][counter] < 0) { | 285 | if (fd[nr_cpu][counter] < 0) { |
| 421 | int err = errno; | 286 | int err = errno; |
| 422 | 287 | ||
| 423 | if (err == EPERM) | 288 | if (err == EPERM || err == EACCES) |
| 424 | die("Permission error - are you root?\n"); | 289 | die("Permission error - are you root?\n"); |
| 425 | else if (err == ENODEV && profile_cpu != -1) | 290 | else if (err == ENODEV && profile_cpu != -1) |
| 426 | die("No such device - did you specify an out-of-range profile CPU?\n"); | 291 | die("No such device - did you specify an out-of-range profile CPU?\n"); |
| @@ -442,11 +307,19 @@ try_again: | |||
| 442 | printf("\n"); | 307 | printf("\n"); |
| 443 | error("perfcounter syscall returned with %d (%s)\n", | 308 | error("perfcounter syscall returned with %d (%s)\n", |
| 444 | fd[nr_cpu][counter], strerror(err)); | 309 | fd[nr_cpu][counter], strerror(err)); |
| 445 | die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n"); | 310 | |
| 311 | #if defined(__i386__) || defined(__x86_64__) | ||
| 312 | if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) | ||
| 313 | die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n"); | ||
| 314 | #endif | ||
| 315 | |||
| 316 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); | ||
| 446 | exit(-1); | 317 | exit(-1); |
| 447 | } | 318 | } |
| 448 | 319 | ||
| 449 | h_attr = get_header_attr(attr, counter); | 320 | h_attr = get_header_attr(attr, counter); |
| 321 | if (h_attr == NULL) | ||
| 322 | die("nomem\n"); | ||
| 450 | 323 | ||
| 451 | if (!file_new) { | 324 | if (!file_new) { |
| 452 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { | 325 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { |
| @@ -460,7 +333,10 @@ try_again: | |||
| 460 | exit(-1); | 333 | exit(-1); |
| 461 | } | 334 | } |
| 462 | 335 | ||
| 463 | perf_header_attr__add_id(h_attr, read_data.id); | 336 | if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { |
| 337 | pr_warning("Not enough memory to add id\n"); | ||
| 338 | exit(-1); | ||
| 339 | } | ||
| 464 | 340 | ||
| 465 | assert(fd[nr_cpu][counter] >= 0); | 341 | assert(fd[nr_cpu][counter] >= 0); |
| 466 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); | 342 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); |
| @@ -470,22 +346,40 @@ try_again: | |||
| 470 | */ | 346 | */ |
| 471 | if (group && group_fd == -1) | 347 | if (group && group_fd == -1) |
| 472 | group_fd = fd[nr_cpu][counter]; | 348 | group_fd = fd[nr_cpu][counter]; |
| 349 | if (multiplex && multiplex_fd == -1) | ||
| 350 | multiplex_fd = fd[nr_cpu][counter]; | ||
| 473 | 351 | ||
| 474 | event_array[nr_poll].fd = fd[nr_cpu][counter]; | 352 | if (multiplex && fd[nr_cpu][counter] != multiplex_fd) { |
| 475 | event_array[nr_poll].events = POLLIN; | 353 | |
| 476 | nr_poll++; | 354 | ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd); |
| 477 | 355 | assert(ret != -1); | |
| 478 | mmap_array[nr_cpu][counter].counter = counter; | 356 | } else { |
| 479 | mmap_array[nr_cpu][counter].prev = 0; | 357 | event_array[nr_poll].fd = fd[nr_cpu][counter]; |
| 480 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; | 358 | event_array[nr_poll].events = POLLIN; |
| 481 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | 359 | nr_poll++; |
| 482 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); | 360 | |
| 483 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { | 361 | mmap_array[nr_cpu][counter].counter = counter; |
| 484 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | 362 | mmap_array[nr_cpu][counter].prev = 0; |
| 485 | exit(-1); | 363 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; |
| 364 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | ||
| 365 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); | ||
| 366 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { | ||
| 367 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | ||
| 368 | exit(-1); | ||
| 369 | } | ||
| 370 | } | ||
| 371 | |||
| 372 | if (filter != NULL) { | ||
| 373 | ret = ioctl(fd[nr_cpu][counter], | ||
| 374 | PERF_EVENT_IOC_SET_FILTER, filter); | ||
| 375 | if (ret) { | ||
| 376 | error("failed to set filter with %d (%s)\n", errno, | ||
| 377 | strerror(errno)); | ||
| 378 | exit(-1); | ||
| 379 | } | ||
| 486 | } | 380 | } |
| 487 | 381 | ||
| 488 | ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE); | 382 | ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE); |
| 489 | } | 383 | } |
| 490 | 384 | ||
| 491 | static void open_counters(int cpu, pid_t pid) | 385 | static void open_counters(int cpu, pid_t pid) |
| @@ -503,7 +397,7 @@ static void atexit_header(void) | |||
| 503 | { | 397 | { |
| 504 | header->data_size += bytes_written; | 398 | header->data_size += bytes_written; |
| 505 | 399 | ||
| 506 | perf_header__write(header, output); | 400 | perf_header__write(header, output, true); |
| 507 | } | 401 | } |
| 508 | 402 | ||
| 509 | static int __cmd_record(int argc, const char **argv) | 403 | static int __cmd_record(int argc, const char **argv) |
| @@ -512,7 +406,8 @@ static int __cmd_record(int argc, const char **argv) | |||
| 512 | struct stat st; | 406 | struct stat st; |
| 513 | pid_t pid = 0; | 407 | pid_t pid = 0; |
| 514 | int flags; | 408 | int flags; |
| 515 | int ret; | 409 | int err; |
| 410 | unsigned long waking = 0; | ||
| 516 | 411 | ||
| 517 | page_size = sysconf(_SC_PAGE_SIZE); | 412 | page_size = sysconf(_SC_PAGE_SIZE); |
| 518 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); | 413 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| @@ -545,22 +440,29 @@ static int __cmd_record(int argc, const char **argv) | |||
| 545 | exit(-1); | 440 | exit(-1); |
| 546 | } | 441 | } |
| 547 | 442 | ||
| 548 | if (!file_new) | 443 | header = perf_header__new(); |
| 549 | header = perf_header__read(output); | 444 | if (header == NULL) { |
| 550 | else | 445 | pr_err("Not enough memory for reading perf file header\n"); |
| 551 | header = perf_header__new(); | 446 | return -1; |
| 447 | } | ||
| 552 | 448 | ||
| 449 | if (!file_new) { | ||
| 450 | err = perf_header__read(header, output); | ||
| 451 | if (err < 0) | ||
| 452 | return err; | ||
| 453 | } | ||
| 553 | 454 | ||
| 554 | if (raw_samples) { | 455 | if (raw_samples) { |
| 555 | read_tracing_data(attrs, nr_counters); | 456 | perf_header__set_feat(header, HEADER_TRACE_INFO); |
| 556 | } else { | 457 | } else { |
| 557 | for (i = 0; i < nr_counters; i++) { | 458 | for (i = 0; i < nr_counters; i++) { |
| 558 | if (attrs[i].sample_type & PERF_SAMPLE_RAW) { | 459 | if (attrs[i].sample_type & PERF_SAMPLE_RAW) { |
| 559 | read_tracing_data(attrs, nr_counters); | 460 | perf_header__set_feat(header, HEADER_TRACE_INFO); |
| 560 | break; | 461 | break; |
| 561 | } | 462 | } |
| 562 | } | 463 | } |
| 563 | } | 464 | } |
| 465 | |||
| 564 | atexit(atexit_header); | 466 | atexit(atexit_header); |
| 565 | 467 | ||
| 566 | if (!system_wide) { | 468 | if (!system_wide) { |
| @@ -578,26 +480,39 @@ static int __cmd_record(int argc, const char **argv) | |||
| 578 | } | 480 | } |
| 579 | } | 481 | } |
| 580 | 482 | ||
| 581 | if (file_new) | 483 | if (file_new) { |
| 582 | perf_header__write(header, output); | 484 | err = perf_header__write(header, output, false); |
| 485 | if (err < 0) | ||
| 486 | return err; | ||
| 487 | } | ||
| 583 | 488 | ||
| 584 | if (!system_wide) { | 489 | if (!system_wide) |
| 585 | pid_t tgid = pid_synthesize_comm_event(pid, 0); | 490 | event__synthesize_thread(pid, process_synthesized_event); |
| 586 | pid_synthesize_mmap_samples(pid, tgid); | 491 | else |
| 587 | } else | 492 | event__synthesize_threads(process_synthesized_event); |
| 588 | synthesize_all(); | ||
| 589 | 493 | ||
| 590 | if (target_pid == -1 && argc) { | 494 | if (target_pid == -1 && argc) { |
| 591 | pid = fork(); | 495 | pid = fork(); |
| 592 | if (pid < 0) | 496 | if (pid < 0) |
| 593 | perror("failed to fork"); | 497 | die("failed to fork"); |
| 594 | 498 | ||
| 595 | if (!pid) { | 499 | if (!pid) { |
| 596 | if (execvp(argv[0], (char **)argv)) { | 500 | if (execvp(argv[0], (char **)argv)) { |
| 597 | perror(argv[0]); | 501 | perror(argv[0]); |
| 598 | exit(-1); | 502 | exit(-1); |
| 599 | } | 503 | } |
| 504 | } else { | ||
| 505 | /* | ||
| 506 | * Wait a bit for the execv'ed child to appear | ||
| 507 | * and be updated in /proc | ||
| 508 | * FIXME: Do you know a less heuristical solution? | ||
| 509 | */ | ||
| 510 | usleep(1000); | ||
| 511 | event__synthesize_thread(pid, | ||
| 512 | process_synthesized_event); | ||
| 600 | } | 513 | } |
| 514 | |||
| 515 | child_pid = pid; | ||
| 601 | } | 516 | } |
| 602 | 517 | ||
| 603 | if (realtime_prio) { | 518 | if (realtime_prio) { |
| @@ -605,7 +520,7 @@ static int __cmd_record(int argc, const char **argv) | |||
| 605 | 520 | ||
| 606 | param.sched_priority = realtime_prio; | 521 | param.sched_priority = realtime_prio; |
| 607 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | 522 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 608 | printf("Could not set realtime priority.\n"); | 523 | pr_err("Could not set realtime priority.\n"); |
| 609 | exit(-1); | 524 | exit(-1); |
| 610 | } | 525 | } |
| 611 | } | 526 | } |
| @@ -614,17 +529,29 @@ static int __cmd_record(int argc, const char **argv) | |||
| 614 | int hits = samples; | 529 | int hits = samples; |
| 615 | 530 | ||
| 616 | for (i = 0; i < nr_cpu; i++) { | 531 | for (i = 0; i < nr_cpu; i++) { |
| 617 | for (counter = 0; counter < nr_counters; counter++) | 532 | for (counter = 0; counter < nr_counters; counter++) { |
| 618 | mmap_read(&mmap_array[i][counter]); | 533 | if (mmap_array[i][counter].base) |
| 534 | mmap_read(&mmap_array[i][counter]); | ||
| 535 | } | ||
| 619 | } | 536 | } |
| 620 | 537 | ||
| 621 | if (hits == samples) { | 538 | if (hits == samples) { |
| 622 | if (done) | 539 | if (done) |
| 623 | break; | 540 | break; |
| 624 | ret = poll(event_array, nr_poll, 100); | 541 | err = poll(event_array, nr_poll, -1); |
| 542 | waking++; | ||
| 543 | } | ||
| 544 | |||
| 545 | if (done) { | ||
| 546 | for (i = 0; i < nr_cpu; i++) { | ||
| 547 | for (counter = 0; counter < nr_counters; counter++) | ||
| 548 | ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE); | ||
| 549 | } | ||
| 625 | } | 550 | } |
| 626 | } | 551 | } |
| 627 | 552 | ||
| 553 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); | ||
| 554 | |||
| 628 | /* | 555 | /* |
| 629 | * Approximate RIP event size: 24 bytes. | 556 | * Approximate RIP event size: 24 bytes. |
| 630 | */ | 557 | */ |
| @@ -647,6 +574,8 @@ static const struct option options[] = { | |||
| 647 | OPT_CALLBACK('e', "event", NULL, "event", | 574 | OPT_CALLBACK('e', "event", NULL, "event", |
| 648 | "event selector. use 'perf list' to list available events", | 575 | "event selector. use 'perf list' to list available events", |
| 649 | parse_events), | 576 | parse_events), |
| 577 | OPT_CALLBACK(0, "filter", NULL, "filter", | ||
| 578 | "event filter", parse_filter), | ||
| 650 | OPT_INTEGER('p', "pid", &target_pid, | 579 | OPT_INTEGER('p', "pid", &target_pid, |
| 651 | "record events on existing pid"), | 580 | "record events on existing pid"), |
| 652 | OPT_INTEGER('r', "realtime", &realtime_prio, | 581 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| @@ -681,6 +610,8 @@ static const struct option options[] = { | |||
| 681 | "Sample addresses"), | 610 | "Sample addresses"), |
| 682 | OPT_BOOLEAN('n', "no-samples", &no_samples, | 611 | OPT_BOOLEAN('n', "no-samples", &no_samples, |
| 683 | "don't sample"), | 612 | "don't sample"), |
| 613 | OPT_BOOLEAN('M', "multiplex", &multiplex, | ||
| 614 | "multiplex counter output in a single channel"), | ||
| 684 | OPT_END() | 615 | OPT_END() |
| 685 | }; | 616 | }; |
| 686 | 617 | ||
| @@ -688,6 +619,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) | |||
| 688 | { | 619 | { |
| 689 | int counter; | 620 | int counter; |
| 690 | 621 | ||
| 622 | symbol__init(0); | ||
| 623 | |||
| 691 | argc = parse_options(argc, argv, options, record_usage, | 624 | argc = parse_options(argc, argv, options, record_usage, |
| 692 | PARSE_OPT_STOP_AT_NON_OPTION); | 625 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 693 | if (!argc && target_pid == -1 && !system_wide) | 626 | if (!argc && target_pid == -1 && !system_wide) |
| @@ -699,6 +632,18 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) | |||
| 699 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; | 632 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; |
| 700 | } | 633 | } |
| 701 | 634 | ||
| 635 | /* | ||
| 636 | * User specified count overrides default frequency. | ||
| 637 | */ | ||
| 638 | if (default_interval) | ||
| 639 | freq = 0; | ||
| 640 | else if (freq) { | ||
| 641 | default_interval = freq; | ||
| 642 | } else { | ||
| 643 | fprintf(stderr, "frequency and count are zero, aborting\n"); | ||
| 644 | exit(EXIT_FAILURE); | ||
| 645 | } | ||
| 646 | |||
| 702 | for (counter = 0; counter < nr_counters; counter++) { | 647 | for (counter = 0; counter < nr_counters; counter++) { |
| 703 | if (attrs[counter].sample_period) | 648 | if (attrs[counter].sample_period) |
| 704 | continue; | 649 | continue; |
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index cdf9a8d27bb9..383c4ab4f9af 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c | |||
| @@ -26,20 +26,18 @@ | |||
| 26 | #include "util/parse-options.h" | 26 | #include "util/parse-options.h" |
| 27 | #include "util/parse-events.h" | 27 | #include "util/parse-events.h" |
| 28 | 28 | ||
| 29 | #include "util/data_map.h" | ||
| 29 | #include "util/thread.h" | 30 | #include "util/thread.h" |
| 31 | #include "util/sort.h" | ||
| 32 | #include "util/hist.h" | ||
| 30 | 33 | ||
| 31 | static char const *input_name = "perf.data"; | 34 | static char const *input_name = "perf.data"; |
| 32 | 35 | ||
| 33 | static char default_sort_order[] = "comm,dso,symbol"; | ||
| 34 | static char *sort_order = default_sort_order; | ||
| 35 | static char *dso_list_str, *comm_list_str, *sym_list_str, | 36 | static char *dso_list_str, *comm_list_str, *sym_list_str, |
| 36 | *col_width_list_str; | 37 | *col_width_list_str; |
| 37 | static struct strlist *dso_list, *comm_list, *sym_list; | 38 | static struct strlist *dso_list, *comm_list, *sym_list; |
| 38 | static char *field_sep; | ||
| 39 | 39 | ||
| 40 | static int force; | 40 | static int force; |
| 41 | static int input; | ||
| 42 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; | ||
| 43 | 41 | ||
| 44 | static int full_paths; | 42 | static int full_paths; |
| 45 | static int show_nr_samples; | 43 | static int show_nr_samples; |
| @@ -50,374 +48,38 @@ static struct perf_read_values show_threads_values; | |||
| 50 | static char default_pretty_printing_style[] = "normal"; | 48 | static char default_pretty_printing_style[] = "normal"; |
| 51 | static char *pretty_printing_style = default_pretty_printing_style; | 49 | static char *pretty_printing_style = default_pretty_printing_style; |
| 52 | 50 | ||
| 53 | static unsigned long page_size; | ||
| 54 | static unsigned long mmap_window = 32; | ||
| 55 | |||
| 56 | static char default_parent_pattern[] = "^sys_|^do_page_fault"; | ||
| 57 | static char *parent_pattern = default_parent_pattern; | ||
| 58 | static regex_t parent_regex; | ||
| 59 | |||
| 60 | static int exclude_other = 1; | 51 | static int exclude_other = 1; |
| 61 | 52 | ||
| 62 | static char callchain_default_opt[] = "fractal,0.5"; | 53 | static char callchain_default_opt[] = "fractal,0.5"; |
| 63 | 54 | ||
| 64 | static int callchain; | ||
| 65 | |||
| 66 | static char __cwd[PATH_MAX]; | ||
| 67 | static char *cwd = __cwd; | ||
| 68 | static int cwdlen; | ||
| 69 | |||
| 70 | static struct rb_root threads; | ||
| 71 | static struct thread *last_match; | ||
| 72 | |||
| 73 | static struct perf_header *header; | 55 | static struct perf_header *header; |
| 74 | 56 | ||
| 75 | static | ||
| 76 | struct callchain_param callchain_param = { | ||
| 77 | .mode = CHAIN_GRAPH_REL, | ||
| 78 | .min_percent = 0.5 | ||
| 79 | }; | ||
| 80 | |||
| 81 | static u64 sample_type; | 57 | static u64 sample_type; |
| 82 | 58 | ||
| 83 | static int repsep_fprintf(FILE *fp, const char *fmt, ...) | 59 | struct symbol_conf symbol_conf; |
| 84 | { | ||
| 85 | int n; | ||
| 86 | va_list ap; | ||
| 87 | |||
| 88 | va_start(ap, fmt); | ||
| 89 | if (!field_sep) | ||
| 90 | n = vfprintf(fp, fmt, ap); | ||
| 91 | else { | ||
| 92 | char *bf = NULL; | ||
| 93 | n = vasprintf(&bf, fmt, ap); | ||
| 94 | if (n > 0) { | ||
| 95 | char *sep = bf; | ||
| 96 | |||
| 97 | while (1) { | ||
| 98 | sep = strchr(sep, *field_sep); | ||
| 99 | if (sep == NULL) | ||
| 100 | break; | ||
| 101 | *sep = '.'; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | fputs(bf, fp); | ||
| 105 | free(bf); | ||
| 106 | } | ||
| 107 | va_end(ap); | ||
| 108 | return n; | ||
| 109 | } | ||
| 110 | |||
| 111 | static unsigned int dsos__col_width, | ||
| 112 | comms__col_width, | ||
| 113 | threads__col_width; | ||
| 114 | 60 | ||
| 115 | /* | ||
| 116 | * histogram, sorted on item, collects counts | ||
| 117 | */ | ||
| 118 | |||
| 119 | static struct rb_root hist; | ||
| 120 | |||
| 121 | struct hist_entry { | ||
| 122 | struct rb_node rb_node; | ||
| 123 | |||
| 124 | struct thread *thread; | ||
| 125 | struct map *map; | ||
| 126 | struct dso *dso; | ||
| 127 | struct symbol *sym; | ||
| 128 | struct symbol *parent; | ||
| 129 | u64 ip; | ||
| 130 | char level; | ||
| 131 | struct callchain_node callchain; | ||
| 132 | struct rb_root sorted_chain; | ||
| 133 | |||
| 134 | u64 count; | ||
| 135 | }; | ||
| 136 | |||
| 137 | /* | ||
| 138 | * configurable sorting bits | ||
| 139 | */ | ||
| 140 | |||
| 141 | struct sort_entry { | ||
| 142 | struct list_head list; | ||
| 143 | |||
| 144 | const char *header; | ||
| 145 | |||
| 146 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); | ||
| 147 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); | ||
| 148 | size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width); | ||
| 149 | unsigned int *width; | ||
| 150 | bool elide; | ||
| 151 | }; | ||
| 152 | |||
| 153 | static int64_t cmp_null(void *l, void *r) | ||
| 154 | { | ||
| 155 | if (!l && !r) | ||
| 156 | return 0; | ||
| 157 | else if (!l) | ||
| 158 | return -1; | ||
| 159 | else | ||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | /* --sort pid */ | ||
| 164 | |||
| 165 | static int64_t | ||
| 166 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 167 | { | ||
| 168 | return right->thread->pid - left->thread->pid; | ||
| 169 | } | ||
| 170 | 61 | ||
| 171 | static size_t | 62 | static size_t |
| 172 | sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width) | 63 | callchain__fprintf_left_margin(FILE *fp, int left_margin) |
| 173 | { | 64 | { |
| 174 | return repsep_fprintf(fp, "%*s:%5d", width - 6, | 65 | int i; |
| 175 | self->thread->comm ?: "", self->thread->pid); | 66 | int ret; |
| 176 | } | ||
| 177 | |||
| 178 | static struct sort_entry sort_thread = { | ||
| 179 | .header = "Command: Pid", | ||
| 180 | .cmp = sort__thread_cmp, | ||
| 181 | .print = sort__thread_print, | ||
| 182 | .width = &threads__col_width, | ||
| 183 | }; | ||
| 184 | |||
| 185 | /* --sort comm */ | ||
| 186 | |||
| 187 | static int64_t | ||
| 188 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 189 | { | ||
| 190 | return right->thread->pid - left->thread->pid; | ||
| 191 | } | ||
| 192 | |||
| 193 | static int64_t | ||
| 194 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) | ||
| 195 | { | ||
| 196 | char *comm_l = left->thread->comm; | ||
| 197 | char *comm_r = right->thread->comm; | ||
| 198 | |||
| 199 | if (!comm_l || !comm_r) | ||
| 200 | return cmp_null(comm_l, comm_r); | ||
| 201 | |||
| 202 | return strcmp(comm_l, comm_r); | ||
| 203 | } | ||
| 204 | |||
| 205 | static size_t | ||
| 206 | sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 207 | { | ||
| 208 | return repsep_fprintf(fp, "%*s", width, self->thread->comm); | ||
| 209 | } | ||
| 210 | |||
| 211 | static struct sort_entry sort_comm = { | ||
| 212 | .header = "Command", | ||
| 213 | .cmp = sort__comm_cmp, | ||
| 214 | .collapse = sort__comm_collapse, | ||
| 215 | .print = sort__comm_print, | ||
| 216 | .width = &comms__col_width, | ||
| 217 | }; | ||
| 218 | |||
| 219 | /* --sort dso */ | ||
| 220 | |||
| 221 | static int64_t | ||
| 222 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 223 | { | ||
| 224 | struct dso *dso_l = left->dso; | ||
| 225 | struct dso *dso_r = right->dso; | ||
| 226 | |||
| 227 | if (!dso_l || !dso_r) | ||
| 228 | return cmp_null(dso_l, dso_r); | ||
| 229 | |||
| 230 | return strcmp(dso_l->name, dso_r->name); | ||
| 231 | } | ||
| 232 | |||
| 233 | static size_t | ||
| 234 | sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 235 | { | ||
| 236 | if (self->dso) | ||
| 237 | return repsep_fprintf(fp, "%-*s", width, self->dso->name); | ||
| 238 | |||
| 239 | return repsep_fprintf(fp, "%*llx", width, (u64)self->ip); | ||
| 240 | } | ||
| 241 | |||
| 242 | static struct sort_entry sort_dso = { | ||
| 243 | .header = "Shared Object", | ||
| 244 | .cmp = sort__dso_cmp, | ||
| 245 | .print = sort__dso_print, | ||
| 246 | .width = &dsos__col_width, | ||
| 247 | }; | ||
| 248 | |||
| 249 | /* --sort symbol */ | ||
| 250 | |||
| 251 | static int64_t | ||
| 252 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 253 | { | ||
| 254 | u64 ip_l, ip_r; | ||
| 255 | |||
| 256 | if (left->sym == right->sym) | ||
| 257 | return 0; | ||
| 258 | |||
| 259 | ip_l = left->sym ? left->sym->start : left->ip; | ||
| 260 | ip_r = right->sym ? right->sym->start : right->ip; | ||
| 261 | |||
| 262 | return (int64_t)(ip_r - ip_l); | ||
| 263 | } | ||
| 264 | |||
| 265 | static size_t | ||
| 266 | sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used) | ||
| 267 | { | ||
| 268 | size_t ret = 0; | ||
| 269 | 67 | ||
| 270 | if (verbose) | 68 | ret = fprintf(fp, " "); |
| 271 | ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip, | ||
| 272 | dso__symtab_origin(self->dso)); | ||
| 273 | 69 | ||
| 274 | ret += repsep_fprintf(fp, "[%c] ", self->level); | 70 | for (i = 0; i < left_margin; i++) |
| 275 | if (self->sym) { | 71 | ret += fprintf(fp, " "); |
| 276 | ret += repsep_fprintf(fp, "%s", self->sym->name); | ||
| 277 | |||
| 278 | if (self->sym->module) | ||
| 279 | ret += repsep_fprintf(fp, "\t[%s]", | ||
| 280 | self->sym->module->name); | ||
| 281 | } else { | ||
| 282 | ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip); | ||
| 283 | } | ||
| 284 | 72 | ||
| 285 | return ret; | 73 | return ret; |
| 286 | } | 74 | } |
| 287 | 75 | ||
| 288 | static struct sort_entry sort_sym = { | 76 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, |
| 289 | .header = "Symbol", | 77 | int left_margin) |
| 290 | .cmp = sort__sym_cmp, | ||
| 291 | .print = sort__sym_print, | ||
| 292 | }; | ||
| 293 | |||
| 294 | /* --sort parent */ | ||
| 295 | |||
| 296 | static int64_t | ||
| 297 | sort__parent_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 298 | { | ||
| 299 | struct symbol *sym_l = left->parent; | ||
| 300 | struct symbol *sym_r = right->parent; | ||
| 301 | |||
| 302 | if (!sym_l || !sym_r) | ||
| 303 | return cmp_null(sym_l, sym_r); | ||
| 304 | |||
| 305 | return strcmp(sym_l->name, sym_r->name); | ||
| 306 | } | ||
| 307 | |||
| 308 | static size_t | ||
| 309 | sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 310 | { | ||
| 311 | return repsep_fprintf(fp, "%-*s", width, | ||
| 312 | self->parent ? self->parent->name : "[other]"); | ||
| 313 | } | ||
| 314 | |||
| 315 | static unsigned int parent_symbol__col_width; | ||
| 316 | |||
| 317 | static struct sort_entry sort_parent = { | ||
| 318 | .header = "Parent symbol", | ||
| 319 | .cmp = sort__parent_cmp, | ||
| 320 | .print = sort__parent_print, | ||
| 321 | .width = &parent_symbol__col_width, | ||
| 322 | }; | ||
| 323 | |||
| 324 | static int sort__need_collapse = 0; | ||
| 325 | static int sort__has_parent = 0; | ||
| 326 | |||
| 327 | struct sort_dimension { | ||
| 328 | const char *name; | ||
| 329 | struct sort_entry *entry; | ||
| 330 | int taken; | ||
| 331 | }; | ||
| 332 | |||
| 333 | static struct sort_dimension sort_dimensions[] = { | ||
| 334 | { .name = "pid", .entry = &sort_thread, }, | ||
| 335 | { .name = "comm", .entry = &sort_comm, }, | ||
| 336 | { .name = "dso", .entry = &sort_dso, }, | ||
| 337 | { .name = "symbol", .entry = &sort_sym, }, | ||
| 338 | { .name = "parent", .entry = &sort_parent, }, | ||
| 339 | }; | ||
| 340 | |||
| 341 | static LIST_HEAD(hist_entry__sort_list); | ||
| 342 | |||
| 343 | static int sort_dimension__add(const char *tok) | ||
| 344 | { | ||
| 345 | unsigned int i; | ||
| 346 | |||
| 347 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { | ||
| 348 | struct sort_dimension *sd = &sort_dimensions[i]; | ||
| 349 | |||
| 350 | if (sd->taken) | ||
| 351 | continue; | ||
| 352 | |||
| 353 | if (strncasecmp(tok, sd->name, strlen(tok))) | ||
| 354 | continue; | ||
| 355 | |||
| 356 | if (sd->entry->collapse) | ||
| 357 | sort__need_collapse = 1; | ||
| 358 | |||
| 359 | if (sd->entry == &sort_parent) { | ||
| 360 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); | ||
| 361 | if (ret) { | ||
| 362 | char err[BUFSIZ]; | ||
| 363 | |||
| 364 | regerror(ret, &parent_regex, err, sizeof(err)); | ||
| 365 | fprintf(stderr, "Invalid regex: %s\n%s", | ||
| 366 | parent_pattern, err); | ||
| 367 | exit(-1); | ||
| 368 | } | ||
| 369 | sort__has_parent = 1; | ||
| 370 | } | ||
| 371 | |||
| 372 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); | ||
| 373 | sd->taken = 1; | ||
| 374 | |||
| 375 | return 0; | ||
| 376 | } | ||
| 377 | |||
| 378 | return -ESRCH; | ||
| 379 | } | ||
| 380 | |||
| 381 | static int64_t | ||
| 382 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 383 | { | ||
| 384 | struct sort_entry *se; | ||
| 385 | int64_t cmp = 0; | ||
| 386 | |||
| 387 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
| 388 | cmp = se->cmp(left, right); | ||
| 389 | if (cmp) | ||
| 390 | break; | ||
| 391 | } | ||
| 392 | |||
| 393 | return cmp; | ||
| 394 | } | ||
| 395 | |||
| 396 | static int64_t | ||
| 397 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | ||
| 398 | { | ||
| 399 | struct sort_entry *se; | ||
| 400 | int64_t cmp = 0; | ||
| 401 | |||
| 402 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
| 403 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | ||
| 404 | |||
| 405 | f = se->collapse ?: se->cmp; | ||
| 406 | |||
| 407 | cmp = f(left, right); | ||
| 408 | if (cmp) | ||
| 409 | break; | ||
| 410 | } | ||
| 411 | |||
| 412 | return cmp; | ||
| 413 | } | ||
| 414 | |||
| 415 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask) | ||
| 416 | { | 78 | { |
| 417 | int i; | 79 | int i; |
| 418 | size_t ret = 0; | 80 | size_t ret = 0; |
| 419 | 81 | ||
| 420 | ret += fprintf(fp, "%s", " "); | 82 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 421 | 83 | ||
| 422 | for (i = 0; i < depth; i++) | 84 | for (i = 0; i < depth; i++) |
| 423 | if (depth_mask & (1 << i)) | 85 | if (depth_mask & (1 << i)) |
| @@ -432,12 +94,12 @@ static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask) | |||
| 432 | static size_t | 94 | static size_t |
| 433 | ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth, | 95 | ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth, |
| 434 | int depth_mask, int count, u64 total_samples, | 96 | int depth_mask, int count, u64 total_samples, |
| 435 | int hits) | 97 | int hits, int left_margin) |
| 436 | { | 98 | { |
| 437 | int i; | 99 | int i; |
| 438 | size_t ret = 0; | 100 | size_t ret = 0; |
| 439 | 101 | ||
| 440 | ret += fprintf(fp, "%s", " "); | 102 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 441 | for (i = 0; i < depth; i++) { | 103 | for (i = 0; i < depth; i++) { |
| 442 | if (depth_mask & (1 << i)) | 104 | if (depth_mask & (1 << i)) |
| 443 | ret += fprintf(fp, "|"); | 105 | ret += fprintf(fp, "|"); |
| @@ -475,8 +137,9 @@ static void init_rem_hits(void) | |||
| 475 | } | 137 | } |
| 476 | 138 | ||
| 477 | static size_t | 139 | static size_t |
| 478 | callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | 140 | __callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
| 479 | u64 total_samples, int depth, int depth_mask) | 141 | u64 total_samples, int depth, int depth_mask, |
| 142 | int left_margin) | ||
| 480 | { | 143 | { |
| 481 | struct rb_node *node, *next; | 144 | struct rb_node *node, *next; |
| 482 | struct callchain_node *child; | 145 | struct callchain_node *child; |
| @@ -517,7 +180,8 @@ callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |||
| 517 | * But we keep the older depth mask for the line seperator | 180 | * But we keep the older depth mask for the line seperator |
| 518 | * to keep the level link until we reach the last child | 181 | * to keep the level link until we reach the last child |
| 519 | */ | 182 | */ |
| 520 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask); | 183 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, |
| 184 | left_margin); | ||
| 521 | i = 0; | 185 | i = 0; |
| 522 | list_for_each_entry(chain, &child->val, list) { | 186 | list_for_each_entry(chain, &child->val, list) { |
| 523 | if (chain->ip >= PERF_CONTEXT_MAX) | 187 | if (chain->ip >= PERF_CONTEXT_MAX) |
| @@ -525,11 +189,13 @@ callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |||
| 525 | ret += ipchain__fprintf_graph(fp, chain, depth, | 189 | ret += ipchain__fprintf_graph(fp, chain, depth, |
| 526 | new_depth_mask, i++, | 190 | new_depth_mask, i++, |
| 527 | new_total, | 191 | new_total, |
| 528 | cumul); | 192 | cumul, |
| 193 | left_margin); | ||
| 529 | } | 194 | } |
| 530 | ret += callchain__fprintf_graph(fp, child, new_total, | 195 | ret += __callchain__fprintf_graph(fp, child, new_total, |
| 531 | depth + 1, | 196 | depth + 1, |
| 532 | new_depth_mask | (1 << depth)); | 197 | new_depth_mask | (1 << depth), |
| 198 | left_margin); | ||
| 533 | node = next; | 199 | node = next; |
| 534 | } | 200 | } |
| 535 | 201 | ||
| @@ -543,9 +209,48 @@ callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |||
| 543 | 209 | ||
| 544 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, | 210 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, |
| 545 | new_depth_mask, 0, new_total, | 211 | new_depth_mask, 0, new_total, |
| 546 | remaining); | 212 | remaining, left_margin); |
| 213 | } | ||
| 214 | |||
| 215 | return ret; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | static size_t | ||
| 220 | callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | ||
| 221 | u64 total_samples, int left_margin) | ||
| 222 | { | ||
| 223 | struct callchain_list *chain; | ||
| 224 | bool printed = false; | ||
| 225 | int i = 0; | ||
| 226 | int ret = 0; | ||
| 227 | |||
| 228 | list_for_each_entry(chain, &self->val, list) { | ||
| 229 | if (chain->ip >= PERF_CONTEXT_MAX) | ||
| 230 | continue; | ||
| 231 | |||
| 232 | if (!i++ && sort__first_dimension == SORT_SYM) | ||
| 233 | continue; | ||
| 234 | |||
| 235 | if (!printed) { | ||
| 236 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
| 237 | ret += fprintf(fp, "|\n"); | ||
| 238 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
| 239 | ret += fprintf(fp, "---"); | ||
| 240 | |||
| 241 | left_margin += 3; | ||
| 242 | printed = true; | ||
| 243 | } else | ||
| 244 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
| 245 | |||
| 246 | if (chain->sym) | ||
| 247 | ret += fprintf(fp, " %s\n", chain->sym->name); | ||
| 248 | else | ||
| 249 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); | ||
| 547 | } | 250 | } |
| 548 | 251 | ||
| 252 | ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin); | ||
| 253 | |||
| 549 | return ret; | 254 | return ret; |
| 550 | } | 255 | } |
| 551 | 256 | ||
| @@ -577,7 +282,7 @@ callchain__fprintf_flat(FILE *fp, struct callchain_node *self, | |||
| 577 | 282 | ||
| 578 | static size_t | 283 | static size_t |
| 579 | hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, | 284 | hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, |
| 580 | u64 total_samples) | 285 | u64 total_samples, int left_margin) |
| 581 | { | 286 | { |
| 582 | struct rb_node *rb_node; | 287 | struct rb_node *rb_node; |
| 583 | struct callchain_node *chain; | 288 | struct callchain_node *chain; |
| @@ -597,8 +302,8 @@ hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, | |||
| 597 | break; | 302 | break; |
| 598 | case CHAIN_GRAPH_ABS: /* Falldown */ | 303 | case CHAIN_GRAPH_ABS: /* Falldown */ |
| 599 | case CHAIN_GRAPH_REL: | 304 | case CHAIN_GRAPH_REL: |
| 600 | ret += callchain__fprintf_graph(fp, chain, | 305 | ret += callchain__fprintf_graph(fp, chain, total_samples, |
| 601 | total_samples, 1, 1); | 306 | left_margin); |
| 602 | case CHAIN_NONE: | 307 | case CHAIN_NONE: |
| 603 | default: | 308 | default: |
| 604 | break; | 309 | break; |
| @@ -610,7 +315,6 @@ hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, | |||
| 610 | return ret; | 315 | return ret; |
| 611 | } | 316 | } |
| 612 | 317 | ||
| 613 | |||
| 614 | static size_t | 318 | static size_t |
| 615 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) | 319 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) |
| 616 | { | 320 | { |
| @@ -644,8 +348,19 @@ hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) | |||
| 644 | 348 | ||
| 645 | ret += fprintf(fp, "\n"); | 349 | ret += fprintf(fp, "\n"); |
| 646 | 350 | ||
| 647 | if (callchain) | 351 | if (callchain) { |
| 648 | hist_entry_callchain__fprintf(fp, self, total_samples); | 352 | int left_margin = 0; |
| 353 | |||
| 354 | if (sort__first_dimension == SORT_COMM) { | ||
| 355 | se = list_first_entry(&hist_entry__sort_list, typeof(*se), | ||
| 356 | list); | ||
| 357 | left_margin = se->width ? *se->width : 0; | ||
| 358 | left_margin -= thread__comm_len(self->thread); | ||
| 359 | } | ||
| 360 | |||
| 361 | hist_entry_callchain__fprintf(fp, self, total_samples, | ||
| 362 | left_margin); | ||
| 363 | } | ||
| 649 | 364 | ||
| 650 | return ret; | 365 | return ret; |
| 651 | } | 366 | } |
| @@ -693,63 +408,6 @@ static int thread__set_comm_adjust(struct thread *self, const char *comm) | |||
| 693 | return 0; | 408 | return 0; |
| 694 | } | 409 | } |
| 695 | 410 | ||
| 696 | |||
| 697 | static struct symbol * | ||
| 698 | resolve_symbol(struct thread *thread, struct map **mapp, | ||
| 699 | struct dso **dsop, u64 *ipp) | ||
| 700 | { | ||
| 701 | struct dso *dso = dsop ? *dsop : NULL; | ||
| 702 | struct map *map = mapp ? *mapp : NULL; | ||
| 703 | u64 ip = *ipp; | ||
| 704 | |||
| 705 | if (!thread) | ||
| 706 | return NULL; | ||
| 707 | |||
| 708 | if (dso) | ||
| 709 | goto got_dso; | ||
| 710 | |||
| 711 | if (map) | ||
| 712 | goto got_map; | ||
| 713 | |||
| 714 | map = thread__find_map(thread, ip); | ||
| 715 | if (map != NULL) { | ||
| 716 | /* | ||
| 717 | * We have to do this here as we may have a dso | ||
| 718 | * with no symbol hit that has a name longer than | ||
| 719 | * the ones with symbols sampled. | ||
| 720 | */ | ||
| 721 | if (!sort_dso.elide && !map->dso->slen_calculated) | ||
| 722 | dso__calc_col_width(map->dso); | ||
| 723 | |||
| 724 | if (mapp) | ||
| 725 | *mapp = map; | ||
| 726 | got_map: | ||
| 727 | ip = map->map_ip(map, ip); | ||
| 728 | |||
| 729 | dso = map->dso; | ||
| 730 | } else { | ||
| 731 | /* | ||
| 732 | * If this is outside of all known maps, | ||
| 733 | * and is a negative address, try to look it | ||
| 734 | * up in the kernel dso, as it might be a | ||
| 735 | * vsyscall (which executes in user-mode): | ||
| 736 | */ | ||
| 737 | if ((long long)ip < 0) | ||
| 738 | dso = kernel_dso; | ||
| 739 | } | ||
| 740 | dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); | ||
| 741 | dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip); | ||
| 742 | *ipp = ip; | ||
| 743 | |||
| 744 | if (dsop) | ||
| 745 | *dsop = dso; | ||
| 746 | |||
| 747 | if (!dso) | ||
| 748 | return NULL; | ||
| 749 | got_dso: | ||
| 750 | return dso->find_symbol(dso, ip); | ||
| 751 | } | ||
| 752 | |||
| 753 | static int call__match(struct symbol *sym) | 411 | static int call__match(struct symbol *sym) |
| 754 | { | 412 | { |
| 755 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) | 413 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| @@ -758,11 +416,11 @@ static int call__match(struct symbol *sym) | |||
| 758 | return 0; | 416 | return 0; |
| 759 | } | 417 | } |
| 760 | 418 | ||
| 761 | static struct symbol ** | 419 | static struct symbol **resolve_callchain(struct thread *thread, |
| 762 | resolve_callchain(struct thread *thread, struct map *map __used, | 420 | struct ip_callchain *chain, |
| 763 | struct ip_callchain *chain, struct hist_entry *entry) | 421 | struct symbol **parent) |
| 764 | { | 422 | { |
| 765 | u64 context = PERF_CONTEXT_MAX; | 423 | u8 cpumode = PERF_RECORD_MISC_USER; |
| 766 | struct symbol **syms = NULL; | 424 | struct symbol **syms = NULL; |
| 767 | unsigned int i; | 425 | unsigned int i; |
| 768 | 426 | ||
| @@ -776,34 +434,31 @@ resolve_callchain(struct thread *thread, struct map *map __used, | |||
| 776 | 434 | ||
| 777 | for (i = 0; i < chain->nr; i++) { | 435 | for (i = 0; i < chain->nr; i++) { |
| 778 | u64 ip = chain->ips[i]; | 436 | u64 ip = chain->ips[i]; |
| 779 | struct dso *dso = NULL; | 437 | struct addr_location al; |
| 780 | struct symbol *sym; | ||
| 781 | 438 | ||
| 782 | if (ip >= PERF_CONTEXT_MAX) { | 439 | if (ip >= PERF_CONTEXT_MAX) { |
| 783 | context = ip; | 440 | switch (ip) { |
| 441 | case PERF_CONTEXT_HV: | ||
| 442 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; | ||
| 443 | case PERF_CONTEXT_KERNEL: | ||
| 444 | cpumode = PERF_RECORD_MISC_KERNEL; break; | ||
| 445 | case PERF_CONTEXT_USER: | ||
| 446 | cpumode = PERF_RECORD_MISC_USER; break; | ||
| 447 | default: | ||
| 448 | break; | ||
| 449 | } | ||
| 784 | continue; | 450 | continue; |
| 785 | } | 451 | } |
| 786 | 452 | ||
| 787 | switch (context) { | 453 | thread__find_addr_location(thread, cpumode, MAP__FUNCTION, |
| 788 | case PERF_CONTEXT_HV: | 454 | ip, &al, NULL); |
| 789 | dso = hypervisor_dso; | 455 | if (al.sym != NULL) { |
| 790 | break; | 456 | if (sort__has_parent && !*parent && |
| 791 | case PERF_CONTEXT_KERNEL: | 457 | call__match(al.sym)) |
| 792 | dso = kernel_dso; | 458 | *parent = al.sym; |
| 793 | break; | ||
| 794 | default: | ||
| 795 | break; | ||
| 796 | } | ||
| 797 | |||
| 798 | sym = resolve_symbol(thread, NULL, &dso, &ip); | ||
| 799 | |||
| 800 | if (sym) { | ||
| 801 | if (sort__has_parent && call__match(sym) && | ||
| 802 | !entry->parent) | ||
| 803 | entry->parent = sym; | ||
| 804 | if (!callchain) | 459 | if (!callchain) |
| 805 | break; | 460 | break; |
| 806 | syms[i] = sym; | 461 | syms[i] = al.sym; |
| 807 | } | 462 | } |
| 808 | } | 463 | } |
| 809 | 464 | ||
| @@ -814,178 +469,33 @@ resolve_callchain(struct thread *thread, struct map *map __used, | |||
| 814 | * collect histogram counts | 469 | * collect histogram counts |
| 815 | */ | 470 | */ |
| 816 | 471 | ||
| 817 | static int | 472 | static int hist_entry__add(struct addr_location *al, |
| 818 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, | 473 | struct ip_callchain *chain, u64 count) |
| 819 | struct symbol *sym, u64 ip, struct ip_callchain *chain, | ||
| 820 | char level, u64 count) | ||
| 821 | { | 474 | { |
| 822 | struct rb_node **p = &hist.rb_node; | 475 | struct symbol **syms = NULL, *parent = NULL; |
| 823 | struct rb_node *parent = NULL; | 476 | bool hit; |
| 824 | struct hist_entry *he; | 477 | struct hist_entry *he; |
| 825 | struct symbol **syms = NULL; | ||
| 826 | struct hist_entry entry = { | ||
| 827 | .thread = thread, | ||
| 828 | .map = map, | ||
| 829 | .dso = dso, | ||
| 830 | .sym = sym, | ||
| 831 | .ip = ip, | ||
| 832 | .level = level, | ||
| 833 | .count = count, | ||
| 834 | .parent = NULL, | ||
| 835 | .sorted_chain = RB_ROOT | ||
| 836 | }; | ||
| 837 | int cmp; | ||
| 838 | 478 | ||
| 839 | if ((sort__has_parent || callchain) && chain) | 479 | if ((sort__has_parent || callchain) && chain) |
| 840 | syms = resolve_callchain(thread, map, chain, &entry); | 480 | syms = resolve_callchain(al->thread, chain, &parent); |
| 841 | 481 | ||
| 842 | while (*p != NULL) { | 482 | he = __hist_entry__add(al, parent, count, &hit); |
| 843 | parent = *p; | 483 | if (he == NULL) |
| 844 | he = rb_entry(parent, struct hist_entry, rb_node); | 484 | return -ENOMEM; |
| 845 | 485 | ||
| 846 | cmp = hist_entry__cmp(&entry, he); | 486 | if (hit) |
| 487 | he->count += count; | ||
| 847 | 488 | ||
| 848 | if (!cmp) { | ||
| 849 | he->count += count; | ||
| 850 | if (callchain) { | ||
| 851 | append_chain(&he->callchain, chain, syms); | ||
| 852 | free(syms); | ||
| 853 | } | ||
| 854 | return 0; | ||
| 855 | } | ||
| 856 | |||
| 857 | if (cmp < 0) | ||
| 858 | p = &(*p)->rb_left; | ||
| 859 | else | ||
| 860 | p = &(*p)->rb_right; | ||
| 861 | } | ||
| 862 | |||
| 863 | he = malloc(sizeof(*he)); | ||
| 864 | if (!he) | ||
| 865 | return -ENOMEM; | ||
| 866 | *he = entry; | ||
| 867 | if (callchain) { | 489 | if (callchain) { |
| 868 | callchain_init(&he->callchain); | 490 | if (!hit) |
| 491 | callchain_init(&he->callchain); | ||
| 869 | append_chain(&he->callchain, chain, syms); | 492 | append_chain(&he->callchain, chain, syms); |
| 870 | free(syms); | 493 | free(syms); |
| 871 | } | 494 | } |
| 872 | rb_link_node(&he->rb_node, parent, p); | ||
| 873 | rb_insert_color(&he->rb_node, &hist); | ||
| 874 | 495 | ||
| 875 | return 0; | 496 | return 0; |
| 876 | } | 497 | } |
| 877 | 498 | ||
| 878 | static void hist_entry__free(struct hist_entry *he) | ||
| 879 | { | ||
| 880 | free(he); | ||
| 881 | } | ||
| 882 | |||
| 883 | /* | ||
| 884 | * collapse the histogram | ||
| 885 | */ | ||
| 886 | |||
| 887 | static struct rb_root collapse_hists; | ||
| 888 | |||
| 889 | static void collapse__insert_entry(struct hist_entry *he) | ||
| 890 | { | ||
| 891 | struct rb_node **p = &collapse_hists.rb_node; | ||
| 892 | struct rb_node *parent = NULL; | ||
| 893 | struct hist_entry *iter; | ||
| 894 | int64_t cmp; | ||
| 895 | |||
| 896 | while (*p != NULL) { | ||
| 897 | parent = *p; | ||
| 898 | iter = rb_entry(parent, struct hist_entry, rb_node); | ||
| 899 | |||
| 900 | cmp = hist_entry__collapse(iter, he); | ||
| 901 | |||
| 902 | if (!cmp) { | ||
| 903 | iter->count += he->count; | ||
| 904 | hist_entry__free(he); | ||
| 905 | return; | ||
| 906 | } | ||
| 907 | |||
| 908 | if (cmp < 0) | ||
| 909 | p = &(*p)->rb_left; | ||
| 910 | else | ||
| 911 | p = &(*p)->rb_right; | ||
| 912 | } | ||
| 913 | |||
| 914 | rb_link_node(&he->rb_node, parent, p); | ||
| 915 | rb_insert_color(&he->rb_node, &collapse_hists); | ||
| 916 | } | ||
| 917 | |||
| 918 | static void collapse__resort(void) | ||
| 919 | { | ||
| 920 | struct rb_node *next; | ||
| 921 | struct hist_entry *n; | ||
| 922 | |||
| 923 | if (!sort__need_collapse) | ||
| 924 | return; | ||
| 925 | |||
| 926 | next = rb_first(&hist); | ||
| 927 | while (next) { | ||
| 928 | n = rb_entry(next, struct hist_entry, rb_node); | ||
| 929 | next = rb_next(&n->rb_node); | ||
| 930 | |||
| 931 | rb_erase(&n->rb_node, &hist); | ||
| 932 | collapse__insert_entry(n); | ||
| 933 | } | ||
| 934 | } | ||
| 935 | |||
| 936 | /* | ||
| 937 | * reverse the map, sort on count. | ||
| 938 | */ | ||
| 939 | |||
| 940 | static struct rb_root output_hists; | ||
| 941 | |||
| 942 | static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits) | ||
| 943 | { | ||
| 944 | struct rb_node **p = &output_hists.rb_node; | ||
| 945 | struct rb_node *parent = NULL; | ||
| 946 | struct hist_entry *iter; | ||
| 947 | |||
| 948 | if (callchain) | ||
| 949 | callchain_param.sort(&he->sorted_chain, &he->callchain, | ||
| 950 | min_callchain_hits, &callchain_param); | ||
| 951 | |||
| 952 | while (*p != NULL) { | ||
| 953 | parent = *p; | ||
| 954 | iter = rb_entry(parent, struct hist_entry, rb_node); | ||
| 955 | |||
| 956 | if (he->count > iter->count) | ||
| 957 | p = &(*p)->rb_left; | ||
| 958 | else | ||
| 959 | p = &(*p)->rb_right; | ||
| 960 | } | ||
| 961 | |||
| 962 | rb_link_node(&he->rb_node, parent, p); | ||
| 963 | rb_insert_color(&he->rb_node, &output_hists); | ||
| 964 | } | ||
| 965 | |||
| 966 | static void output__resort(u64 total_samples) | ||
| 967 | { | ||
| 968 | struct rb_node *next; | ||
| 969 | struct hist_entry *n; | ||
| 970 | struct rb_root *tree = &hist; | ||
| 971 | u64 min_callchain_hits; | ||
| 972 | |||
| 973 | min_callchain_hits = total_samples * (callchain_param.min_percent / 100); | ||
| 974 | |||
| 975 | if (sort__need_collapse) | ||
| 976 | tree = &collapse_hists; | ||
| 977 | |||
| 978 | next = rb_first(tree); | ||
| 979 | |||
| 980 | while (next) { | ||
| 981 | n = rb_entry(next, struct hist_entry, rb_node); | ||
| 982 | next = rb_next(&n->rb_node); | ||
| 983 | |||
| 984 | rb_erase(&n->rb_node, tree); | ||
| 985 | output__insert_entry(n, min_callchain_hits); | ||
| 986 | } | ||
| 987 | } | ||
| 988 | |||
| 989 | static size_t output__fprintf(FILE *fp, u64 total_samples) | 499 | static size_t output__fprintf(FILE *fp, u64 total_samples) |
| 990 | { | 500 | { |
| 991 | struct hist_entry *pos; | 501 | struct hist_entry *pos; |
| @@ -1080,13 +590,6 @@ print_entries: | |||
| 1080 | return ret; | 590 | return ret; |
| 1081 | } | 591 | } |
| 1082 | 592 | ||
| 1083 | static unsigned long total = 0, | ||
| 1084 | total_mmap = 0, | ||
| 1085 | total_comm = 0, | ||
| 1086 | total_fork = 0, | ||
| 1087 | total_unknown = 0, | ||
| 1088 | total_lost = 0; | ||
| 1089 | |||
| 1090 | static int validate_chain(struct ip_callchain *chain, event_t *event) | 593 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
| 1091 | { | 594 | { |
| 1092 | unsigned int chain_size; | 595 | unsigned int chain_size; |
| @@ -1100,30 +603,22 @@ static int validate_chain(struct ip_callchain *chain, event_t *event) | |||
| 1100 | return 0; | 603 | return 0; |
| 1101 | } | 604 | } |
| 1102 | 605 | ||
| 1103 | static int | 606 | static int process_sample_event(event_t *event) |
| 1104 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1105 | { | 607 | { |
| 1106 | char level; | ||
| 1107 | int show = 0; | ||
| 1108 | struct dso *dso = NULL; | ||
| 1109 | struct thread *thread; | ||
| 1110 | u64 ip = event->ip.ip; | 608 | u64 ip = event->ip.ip; |
| 1111 | u64 period = 1; | 609 | u64 period = 1; |
| 1112 | struct map *map = NULL; | ||
| 1113 | void *more_data = event->ip.__more_data; | 610 | void *more_data = event->ip.__more_data; |
| 1114 | struct ip_callchain *chain = NULL; | 611 | struct ip_callchain *chain = NULL; |
| 1115 | int cpumode; | 612 | int cpumode; |
| 1116 | 613 | struct addr_location al; | |
| 1117 | thread = threads__findnew(event->ip.pid, &threads, &last_match); | 614 | struct thread *thread = threads__findnew(event->ip.pid); |
| 1118 | 615 | ||
| 1119 | if (sample_type & PERF_SAMPLE_PERIOD) { | 616 | if (sample_type & PERF_SAMPLE_PERIOD) { |
| 1120 | period = *(u64 *)more_data; | 617 | period = *(u64 *)more_data; |
| 1121 | more_data += sizeof(u64); | 618 | more_data += sizeof(u64); |
| 1122 | } | 619 | } |
| 1123 | 620 | ||
| 1124 | dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", | 621 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
| 1125 | (void *)(offset + head), | ||
| 1126 | (void *)(long)(event->header.size), | ||
| 1127 | event->header.misc, | 622 | event->header.misc, |
| 1128 | event->ip.pid, event->ip.tid, | 623 | event->ip.pid, event->ip.tid, |
| 1129 | (void *)(long)ip, | 624 | (void *)(long)ip, |
| @@ -1137,7 +632,8 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 1137 | dump_printf("... chain: nr:%Lu\n", chain->nr); | 632 | dump_printf("... chain: nr:%Lu\n", chain->nr); |
| 1138 | 633 | ||
| 1139 | if (validate_chain(chain, event) < 0) { | 634 | if (validate_chain(chain, event) < 0) { |
| 1140 | eprintf("call-chain problem with event, skipping it.\n"); | 635 | pr_debug("call-chain problem with event, " |
| 636 | "skipping it.\n"); | ||
| 1141 | return 0; | 637 | return 0; |
| 1142 | } | 638 | } |
| 1143 | 639 | ||
| @@ -1147,165 +643,66 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 1147 | } | 643 | } |
| 1148 | } | 644 | } |
| 1149 | 645 | ||
| 1150 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 1151 | |||
| 1152 | if (thread == NULL) { | 646 | if (thread == NULL) { |
| 1153 | eprintf("problem processing %d event, skipping it.\n", | 647 | pr_debug("problem processing %d event, skipping it.\n", |
| 1154 | event->header.type); | 648 | event->header.type); |
| 1155 | return -1; | 649 | return -1; |
| 1156 | } | 650 | } |
| 1157 | 651 | ||
| 652 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 653 | |||
| 1158 | if (comm_list && !strlist__has_entry(comm_list, thread->comm)) | 654 | if (comm_list && !strlist__has_entry(comm_list, thread->comm)) |
| 1159 | return 0; | 655 | return 0; |
| 1160 | 656 | ||
| 1161 | cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK; | 657 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1162 | |||
| 1163 | if (cpumode == PERF_EVENT_MISC_KERNEL) { | ||
| 1164 | show = SHOW_KERNEL; | ||
| 1165 | level = 'k'; | ||
| 1166 | |||
| 1167 | dso = kernel_dso; | ||
| 1168 | |||
| 1169 | dump_printf(" ...... dso: %s\n", dso->name); | ||
| 1170 | |||
| 1171 | } else if (cpumode == PERF_EVENT_MISC_USER) { | ||
| 1172 | |||
| 1173 | show = SHOW_USER; | ||
| 1174 | level = '.'; | ||
| 1175 | |||
| 1176 | } else { | ||
| 1177 | show = SHOW_HV; | ||
| 1178 | level = 'H'; | ||
| 1179 | |||
| 1180 | dso = hypervisor_dso; | ||
| 1181 | |||
| 1182 | dump_printf(" ...... dso: [hypervisor]\n"); | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | if (show & show_mask) { | ||
| 1186 | struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip); | ||
| 1187 | 658 | ||
| 1188 | if (dso_list && (!dso || !dso->name || | 659 | thread__find_addr_location(thread, cpumode, |
| 1189 | !strlist__has_entry(dso_list, dso->name))) | 660 | MAP__FUNCTION, ip, &al, NULL); |
| 1190 | return 0; | 661 | /* |
| 1191 | 662 | * We have to do this here as we may have a dso with no symbol hit that | |
| 1192 | if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name))) | 663 | * has a name longer than the ones with symbols sampled. |
| 1193 | return 0; | 664 | */ |
| 1194 | 665 | if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated) | |
| 1195 | if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) { | 666 | dso__calc_col_width(al.map->dso); |
| 1196 | eprintf("problem incrementing symbol count, skipping event\n"); | 667 | |
| 1197 | return -1; | 668 | if (dso_list && |
| 1198 | } | 669 | (!al.map || !al.map->dso || |
| 1199 | } | 670 | !(strlist__has_entry(dso_list, al.map->dso->short_name) || |
| 1200 | total += period; | 671 | (al.map->dso->short_name != al.map->dso->long_name && |
| 1201 | 672 | strlist__has_entry(dso_list, al.map->dso->long_name))))) | |
| 1202 | return 0; | 673 | return 0; |
| 1203 | } | ||
| 1204 | 674 | ||
| 1205 | static int | 675 | if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name)) |
| 1206 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1207 | { | ||
| 1208 | struct thread *thread; | ||
| 1209 | struct map *map = map__new(&event->mmap, cwd, cwdlen); | ||
| 1210 | |||
| 1211 | thread = threads__findnew(event->mmap.pid, &threads, &last_match); | ||
| 1212 | |||
| 1213 | dump_printf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n", | ||
| 1214 | (void *)(offset + head), | ||
| 1215 | (void *)(long)(event->header.size), | ||
| 1216 | event->mmap.pid, | ||
| 1217 | event->mmap.tid, | ||
| 1218 | (void *)(long)event->mmap.start, | ||
| 1219 | (void *)(long)event->mmap.len, | ||
| 1220 | (void *)(long)event->mmap.pgoff, | ||
| 1221 | event->mmap.filename); | ||
| 1222 | |||
| 1223 | if (thread == NULL || map == NULL) { | ||
| 1224 | dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n"); | ||
| 1225 | return 0; | 676 | return 0; |
| 677 | |||
| 678 | if (hist_entry__add(&al, chain, period)) { | ||
| 679 | pr_debug("problem incrementing symbol count, skipping event\n"); | ||
| 680 | return -1; | ||
| 1226 | } | 681 | } |
| 1227 | 682 | ||
| 1228 | thread__insert_map(thread, map); | 683 | event__stats.total += period; |
| 1229 | total_mmap++; | ||
| 1230 | 684 | ||
| 1231 | return 0; | 685 | return 0; |
| 1232 | } | 686 | } |
| 1233 | 687 | ||
| 1234 | static int | 688 | static int process_comm_event(event_t *event) |
| 1235 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1236 | { | 689 | { |
| 1237 | struct thread *thread; | 690 | struct thread *thread = threads__findnew(event->comm.pid); |
| 1238 | |||
| 1239 | thread = threads__findnew(event->comm.pid, &threads, &last_match); | ||
| 1240 | 691 | ||
| 1241 | dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", | 692 | dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid); |
| 1242 | (void *)(offset + head), | ||
| 1243 | (void *)(long)(event->header.size), | ||
| 1244 | event->comm.comm, event->comm.pid); | ||
| 1245 | 693 | ||
| 1246 | if (thread == NULL || | 694 | if (thread == NULL || |
| 1247 | thread__set_comm_adjust(thread, event->comm.comm)) { | 695 | thread__set_comm_adjust(thread, event->comm.comm)) { |
| 1248 | dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n"); | 696 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
| 1249 | return -1; | ||
| 1250 | } | ||
| 1251 | total_comm++; | ||
| 1252 | |||
| 1253 | return 0; | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | static int | ||
| 1257 | process_task_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1258 | { | ||
| 1259 | struct thread *thread; | ||
| 1260 | struct thread *parent; | ||
| 1261 | |||
| 1262 | thread = threads__findnew(event->fork.pid, &threads, &last_match); | ||
| 1263 | parent = threads__findnew(event->fork.ppid, &threads, &last_match); | ||
| 1264 | |||
| 1265 | dump_printf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n", | ||
| 1266 | (void *)(offset + head), | ||
| 1267 | (void *)(long)(event->header.size), | ||
| 1268 | event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT", | ||
| 1269 | event->fork.pid, event->fork.tid, | ||
| 1270 | event->fork.ppid, event->fork.ptid); | ||
| 1271 | |||
| 1272 | /* | ||
| 1273 | * A thread clone will have the same PID for both | ||
| 1274 | * parent and child. | ||
| 1275 | */ | ||
| 1276 | if (thread == parent) | ||
| 1277 | return 0; | ||
| 1278 | |||
| 1279 | if (event->header.type == PERF_EVENT_EXIT) | ||
| 1280 | return 0; | ||
| 1281 | |||
| 1282 | if (!thread || !parent || thread__fork(thread, parent)) { | ||
| 1283 | dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n"); | ||
| 1284 | return -1; | 697 | return -1; |
| 1285 | } | 698 | } |
| 1286 | total_fork++; | ||
| 1287 | 699 | ||
| 1288 | return 0; | 700 | return 0; |
| 1289 | } | 701 | } |
| 1290 | 702 | ||
| 1291 | static int | 703 | static int process_read_event(event_t *event) |
| 1292 | process_lost_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1293 | { | 704 | { |
| 1294 | dump_printf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n", | 705 | struct perf_event_attr *attr; |
| 1295 | (void *)(offset + head), | ||
| 1296 | (void *)(long)(event->header.size), | ||
| 1297 | event->lost.id, | ||
| 1298 | event->lost.lost); | ||
| 1299 | |||
| 1300 | total_lost += event->lost.lost; | ||
| 1301 | |||
| 1302 | return 0; | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | static int | ||
| 1306 | process_read_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1307 | { | ||
| 1308 | struct perf_counter_attr *attr; | ||
| 1309 | 706 | ||
| 1310 | attr = perf_header__find_attr(event->read.id, header); | 707 | attr = perf_header__find_attr(event->read.id, header); |
| 1311 | 708 | ||
| @@ -1319,238 +716,91 @@ process_read_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 1319 | event->read.value); | 716 | event->read.value); |
| 1320 | } | 717 | } |
| 1321 | 718 | ||
| 1322 | dump_printf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n", | 719 | dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid, |
| 1323 | (void *)(offset + head), | 720 | attr ? __event_name(attr->type, attr->config) : "FAIL", |
| 1324 | (void *)(long)(event->header.size), | 721 | event->read.value); |
| 1325 | event->read.pid, | ||
| 1326 | event->read.tid, | ||
| 1327 | attr ? __event_name(attr->type, attr->config) | ||
| 1328 | : "FAIL", | ||
| 1329 | event->read.value); | ||
| 1330 | |||
| 1331 | return 0; | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | static int | ||
| 1335 | process_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 1336 | { | ||
| 1337 | trace_event(event); | ||
| 1338 | |||
| 1339 | switch (event->header.type) { | ||
| 1340 | case PERF_EVENT_SAMPLE: | ||
| 1341 | return process_sample_event(event, offset, head); | ||
| 1342 | |||
| 1343 | case PERF_EVENT_MMAP: | ||
| 1344 | return process_mmap_event(event, offset, head); | ||
| 1345 | |||
| 1346 | case PERF_EVENT_COMM: | ||
| 1347 | return process_comm_event(event, offset, head); | ||
| 1348 | |||
| 1349 | case PERF_EVENT_FORK: | ||
| 1350 | case PERF_EVENT_EXIT: | ||
| 1351 | return process_task_event(event, offset, head); | ||
| 1352 | |||
| 1353 | case PERF_EVENT_LOST: | ||
| 1354 | return process_lost_event(event, offset, head); | ||
| 1355 | |||
| 1356 | case PERF_EVENT_READ: | ||
| 1357 | return process_read_event(event, offset, head); | ||
| 1358 | |||
| 1359 | /* | ||
| 1360 | * We dont process them right now but they are fine: | ||
| 1361 | */ | ||
| 1362 | |||
| 1363 | case PERF_EVENT_THROTTLE: | ||
| 1364 | case PERF_EVENT_UNTHROTTLE: | ||
| 1365 | return 0; | ||
| 1366 | |||
| 1367 | default: | ||
| 1368 | return -1; | ||
| 1369 | } | ||
| 1370 | 722 | ||
| 1371 | return 0; | 723 | return 0; |
| 1372 | } | 724 | } |
| 1373 | 725 | ||
| 1374 | static int __cmd_report(void) | 726 | static int sample_type_check(u64 type) |
| 1375 | { | 727 | { |
| 1376 | int ret, rc = EXIT_FAILURE; | 728 | sample_type = type; |
| 1377 | unsigned long offset = 0; | ||
| 1378 | unsigned long head, shift; | ||
| 1379 | struct stat input_stat; | ||
| 1380 | struct thread *idle; | ||
| 1381 | event_t *event; | ||
| 1382 | uint32_t size; | ||
| 1383 | char *buf; | ||
| 1384 | |||
| 1385 | idle = register_idle_thread(&threads, &last_match); | ||
| 1386 | thread__comm_adjust(idle); | ||
| 1387 | |||
| 1388 | if (show_threads) | ||
| 1389 | perf_read_values_init(&show_threads_values); | ||
| 1390 | |||
| 1391 | input = open(input_name, O_RDONLY); | ||
| 1392 | if (input < 0) { | ||
| 1393 | fprintf(stderr, " failed to open file: %s", input_name); | ||
| 1394 | if (!strcmp(input_name, "perf.data")) | ||
| 1395 | fprintf(stderr, " (try 'perf record' first)"); | ||
| 1396 | fprintf(stderr, "\n"); | ||
| 1397 | exit(-1); | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | ret = fstat(input, &input_stat); | ||
| 1401 | if (ret < 0) { | ||
| 1402 | perror("failed to stat file"); | ||
| 1403 | exit(-1); | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { | ||
| 1407 | fprintf(stderr, "file: %s not owned by current user or root\n", input_name); | ||
| 1408 | exit(-1); | ||
| 1409 | } | ||
| 1410 | |||
| 1411 | if (!input_stat.st_size) { | ||
| 1412 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | ||
| 1413 | exit(0); | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | header = perf_header__read(input); | ||
| 1417 | head = header->data_offset; | ||
| 1418 | |||
| 1419 | sample_type = perf_header__sample_type(header); | ||
| 1420 | 729 | ||
| 1421 | if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { | 730 | if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { |
| 1422 | if (sort__has_parent) { | 731 | if (sort__has_parent) { |
| 1423 | fprintf(stderr, "selected --sort parent, but no" | 732 | fprintf(stderr, "selected --sort parent, but no" |
| 1424 | " callchain data. Did you call" | 733 | " callchain data. Did you call" |
| 1425 | " perf record without -g?\n"); | 734 | " perf record without -g?\n"); |
| 1426 | exit(-1); | 735 | return -1; |
| 1427 | } | 736 | } |
| 1428 | if (callchain) { | 737 | if (callchain) { |
| 1429 | fprintf(stderr, "selected -g but no callchain data." | 738 | fprintf(stderr, "selected -g but no callchain data." |
| 1430 | " Did you call perf record without" | 739 | " Did you call perf record without" |
| 1431 | " -g?\n"); | 740 | " -g?\n"); |
| 1432 | exit(-1); | 741 | return -1; |
| 1433 | } | 742 | } |
| 1434 | } else if (callchain_param.mode != CHAIN_NONE && !callchain) { | 743 | } else if (callchain_param.mode != CHAIN_NONE && !callchain) { |
| 1435 | callchain = 1; | 744 | callchain = 1; |
| 1436 | if (register_callchain_param(&callchain_param) < 0) { | 745 | if (register_callchain_param(&callchain_param) < 0) { |
| 1437 | fprintf(stderr, "Can't register callchain" | 746 | fprintf(stderr, "Can't register callchain" |
| 1438 | " params\n"); | 747 | " params\n"); |
| 1439 | exit(-1); | 748 | return -1; |
| 1440 | } | 749 | } |
| 1441 | } | 750 | } |
| 1442 | 751 | ||
| 1443 | if (load_kernel() < 0) { | 752 | return 0; |
| 1444 | perror("failed to load kernel symbols"); | 753 | } |
| 1445 | return EXIT_FAILURE; | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | if (!full_paths) { | ||
| 1449 | if (getcwd(__cwd, sizeof(__cwd)) == NULL) { | ||
| 1450 | perror("failed to get the current directory"); | ||
| 1451 | return EXIT_FAILURE; | ||
| 1452 | } | ||
| 1453 | cwdlen = strlen(cwd); | ||
| 1454 | } else { | ||
| 1455 | cwd = NULL; | ||
| 1456 | cwdlen = 0; | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | shift = page_size * (head / page_size); | ||
| 1460 | offset += shift; | ||
| 1461 | head -= shift; | ||
| 1462 | |||
| 1463 | remap: | ||
| 1464 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | ||
| 1465 | MAP_SHARED, input, offset); | ||
| 1466 | if (buf == MAP_FAILED) { | ||
| 1467 | perror("failed to mmap file"); | ||
| 1468 | exit(-1); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | more: | ||
| 1472 | event = (event_t *)(buf + head); | ||
| 1473 | |||
| 1474 | size = event->header.size; | ||
| 1475 | if (!size) | ||
| 1476 | size = 8; | ||
| 1477 | |||
| 1478 | if (head + event->header.size >= page_size * mmap_window) { | ||
| 1479 | int munmap_ret; | ||
| 1480 | |||
| 1481 | shift = page_size * (head / page_size); | ||
| 1482 | |||
| 1483 | munmap_ret = munmap(buf, page_size * mmap_window); | ||
| 1484 | assert(munmap_ret == 0); | ||
| 1485 | |||
| 1486 | offset += shift; | ||
| 1487 | head -= shift; | ||
| 1488 | goto remap; | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | size = event->header.size; | ||
| 1492 | |||
| 1493 | dump_printf("\n%p [%p]: event: %d\n", | ||
| 1494 | (void *)(offset + head), | ||
| 1495 | (void *)(long)event->header.size, | ||
| 1496 | event->header.type); | ||
| 1497 | |||
| 1498 | if (!size || process_event(event, offset, head) < 0) { | ||
| 1499 | |||
| 1500 | dump_printf("%p [%p]: skipping unknown header type: %d\n", | ||
| 1501 | (void *)(offset + head), | ||
| 1502 | (void *)(long)(event->header.size), | ||
| 1503 | event->header.type); | ||
| 1504 | |||
| 1505 | total_unknown++; | ||
| 1506 | |||
| 1507 | /* | ||
| 1508 | * assume we lost track of the stream, check alignment, and | ||
| 1509 | * increment a single u64 in the hope to catch on again 'soon'. | ||
| 1510 | */ | ||
| 1511 | 754 | ||
| 1512 | if (unlikely(head & 7)) | 755 | static struct perf_file_handler file_handler = { |
| 1513 | head &= ~7ULL; | 756 | .process_sample_event = process_sample_event, |
| 757 | .process_mmap_event = event__process_mmap, | ||
| 758 | .process_comm_event = process_comm_event, | ||
| 759 | .process_exit_event = event__process_task, | ||
| 760 | .process_fork_event = event__process_task, | ||
| 761 | .process_lost_event = event__process_lost, | ||
| 762 | .process_read_event = process_read_event, | ||
| 763 | .sample_type_check = sample_type_check, | ||
| 764 | }; | ||
| 1514 | 765 | ||
| 1515 | size = 8; | ||
| 1516 | } | ||
| 1517 | 766 | ||
| 1518 | head += size; | 767 | static int __cmd_report(void) |
| 768 | { | ||
| 769 | struct thread *idle; | ||
| 770 | int ret; | ||
| 1519 | 771 | ||
| 1520 | if (offset + head >= header->data_offset + header->data_size) | 772 | idle = register_idle_thread(); |
| 1521 | goto done; | 773 | thread__comm_adjust(idle); |
| 1522 | 774 | ||
| 1523 | if (offset + head < (unsigned long)input_stat.st_size) | 775 | if (show_threads) |
| 1524 | goto more; | 776 | perf_read_values_init(&show_threads_values); |
| 1525 | 777 | ||
| 1526 | done: | 778 | register_perf_file_handler(&file_handler); |
| 1527 | rc = EXIT_SUCCESS; | ||
| 1528 | close(input); | ||
| 1529 | 779 | ||
| 1530 | dump_printf(" IP events: %10ld\n", total); | 780 | ret = mmap_dispatch_perf_file(&header, input_name, force, |
| 1531 | dump_printf(" mmap events: %10ld\n", total_mmap); | 781 | full_paths, &event__cwdlen, &event__cwd); |
| 1532 | dump_printf(" comm events: %10ld\n", total_comm); | 782 | if (ret) |
| 1533 | dump_printf(" fork events: %10ld\n", total_fork); | 783 | return ret; |
| 1534 | dump_printf(" lost events: %10ld\n", total_lost); | ||
| 1535 | dump_printf(" unknown events: %10ld\n", total_unknown); | ||
| 1536 | 784 | ||
| 1537 | if (dump_trace) | 785 | if (dump_trace) { |
| 786 | event__print_totals(); | ||
| 1538 | return 0; | 787 | return 0; |
| 788 | } | ||
| 1539 | 789 | ||
| 1540 | if (verbose >= 3) | 790 | if (verbose > 3) |
| 1541 | threads__fprintf(stdout, &threads); | 791 | threads__fprintf(stdout); |
| 1542 | 792 | ||
| 1543 | if (verbose >= 2) | 793 | if (verbose > 2) |
| 1544 | dsos__fprintf(stdout); | 794 | dsos__fprintf(stdout); |
| 1545 | 795 | ||
| 1546 | collapse__resort(); | 796 | collapse__resort(); |
| 1547 | output__resort(total); | 797 | output__resort(event__stats.total); |
| 1548 | output__fprintf(stdout, total); | 798 | output__fprintf(stdout, event__stats.total); |
| 1549 | 799 | ||
| 1550 | if (show_threads) | 800 | if (show_threads) |
| 1551 | perf_read_values_destroy(&show_threads_values); | 801 | perf_read_values_destroy(&show_threads_values); |
| 1552 | 802 | ||
| 1553 | return rc; | 803 | return ret; |
| 1554 | } | 804 | } |
| 1555 | 805 | ||
| 1556 | static int | 806 | static int |
| @@ -1606,7 +856,8 @@ setup: | |||
| 1606 | return 0; | 856 | return 0; |
| 1607 | } | 857 | } |
| 1608 | 858 | ||
| 1609 | static const char * const report_usage[] = { | 859 | //static const char * const report_usage[] = { |
| 860 | const char * const report_usage[] = { | ||
| 1610 | "perf report [<options>] <command>", | 861 | "perf report [<options>] <command>", |
| 1611 | NULL | 862 | NULL |
| 1612 | }; | 863 | }; |
| @@ -1618,9 +869,10 @@ static const struct option options[] = { | |||
| 1618 | "be more verbose (show symbol address, etc)"), | 869 | "be more verbose (show symbol address, etc)"), |
| 1619 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | 870 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1620 | "dump raw trace in ASCII"), | 871 | "dump raw trace in ASCII"), |
| 1621 | OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), | 872 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 873 | "file", "vmlinux pathname"), | ||
| 1622 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), | 874 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
| 1623 | OPT_BOOLEAN('m', "modules", &modules, | 875 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
| 1624 | "load module symbols - WARNING: use only with -k and LIVE kernel"), | 876 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
| 1625 | OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples, | 877 | OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples, |
| 1626 | "Show a column with the number of samples"), | 878 | "Show a column with the number of samples"), |
| @@ -1690,9 +942,8 @@ static void setup_list(struct strlist **list, const char *list_str, | |||
| 1690 | 942 | ||
| 1691 | int cmd_report(int argc, const char **argv, const char *prefix __used) | 943 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
| 1692 | { | 944 | { |
| 1693 | symbol__init(); | 945 | if (symbol__init(&symbol_conf) < 0) |
| 1694 | 946 | return -1; | |
| 1695 | page_size = getpagesize(); | ||
| 1696 | 947 | ||
| 1697 | argc = parse_options(argc, argv, options, report_usage, 0); | 948 | argc = parse_options(argc, argv, options, report_usage, 0); |
| 1698 | 949 | ||
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c new file mode 100644 index 000000000000..26b782f26ee1 --- /dev/null +++ b/tools/perf/builtin-sched.c | |||
| @@ -0,0 +1,1945 @@ | |||
| 1 | #include "builtin.h" | ||
| 2 | #include "perf.h" | ||
| 3 | |||
| 4 | #include "util/util.h" | ||
| 5 | #include "util/cache.h" | ||
| 6 | #include "util/symbol.h" | ||
| 7 | #include "util/thread.h" | ||
| 8 | #include "util/header.h" | ||
| 9 | |||
| 10 | #include "util/parse-options.h" | ||
| 11 | #include "util/trace-event.h" | ||
| 12 | |||
| 13 | #include "util/debug.h" | ||
| 14 | #include "util/data_map.h" | ||
| 15 | |||
| 16 | #include <sys/types.h> | ||
| 17 | #include <sys/prctl.h> | ||
| 18 | |||
| 19 | #include <semaphore.h> | ||
| 20 | #include <pthread.h> | ||
| 21 | #include <math.h> | ||
| 22 | |||
| 23 | static char const *input_name = "perf.data"; | ||
| 24 | |||
| 25 | static struct perf_header *header; | ||
| 26 | static u64 sample_type; | ||
| 27 | |||
| 28 | static char default_sort_order[] = "avg, max, switch, runtime"; | ||
| 29 | static char *sort_order = default_sort_order; | ||
| 30 | |||
| 31 | static int profile_cpu = -1; | ||
| 32 | |||
| 33 | #define PR_SET_NAME 15 /* Set process name */ | ||
| 34 | #define MAX_CPUS 4096 | ||
| 35 | |||
| 36 | static u64 run_measurement_overhead; | ||
| 37 | static u64 sleep_measurement_overhead; | ||
| 38 | |||
| 39 | #define COMM_LEN 20 | ||
| 40 | #define SYM_LEN 129 | ||
| 41 | |||
| 42 | #define MAX_PID 65536 | ||
| 43 | |||
| 44 | static unsigned long nr_tasks; | ||
| 45 | |||
| 46 | struct sched_atom; | ||
| 47 | |||
| 48 | struct task_desc { | ||
| 49 | unsigned long nr; | ||
| 50 | unsigned long pid; | ||
| 51 | char comm[COMM_LEN]; | ||
| 52 | |||
| 53 | unsigned long nr_events; | ||
| 54 | unsigned long curr_event; | ||
| 55 | struct sched_atom **atoms; | ||
| 56 | |||
| 57 | pthread_t thread; | ||
| 58 | sem_t sleep_sem; | ||
| 59 | |||
| 60 | sem_t ready_for_work; | ||
| 61 | sem_t work_done_sem; | ||
| 62 | |||
| 63 | u64 cpu_usage; | ||
| 64 | }; | ||
| 65 | |||
| 66 | enum sched_event_type { | ||
| 67 | SCHED_EVENT_RUN, | ||
| 68 | SCHED_EVENT_SLEEP, | ||
| 69 | SCHED_EVENT_WAKEUP, | ||
| 70 | SCHED_EVENT_MIGRATION, | ||
| 71 | }; | ||
| 72 | |||
| 73 | struct sched_atom { | ||
| 74 | enum sched_event_type type; | ||
| 75 | u64 timestamp; | ||
| 76 | u64 duration; | ||
| 77 | unsigned long nr; | ||
| 78 | int specific_wait; | ||
| 79 | sem_t *wait_sem; | ||
| 80 | struct task_desc *wakee; | ||
| 81 | }; | ||
| 82 | |||
| 83 | static struct task_desc *pid_to_task[MAX_PID]; | ||
| 84 | |||
| 85 | static struct task_desc **tasks; | ||
| 86 | |||
| 87 | static pthread_mutex_t start_work_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 88 | static u64 start_time; | ||
| 89 | |||
| 90 | static pthread_mutex_t work_done_wait_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 91 | |||
| 92 | static unsigned long nr_run_events; | ||
| 93 | static unsigned long nr_sleep_events; | ||
| 94 | static unsigned long nr_wakeup_events; | ||
| 95 | |||
| 96 | static unsigned long nr_sleep_corrections; | ||
| 97 | static unsigned long nr_run_events_optimized; | ||
| 98 | |||
| 99 | static unsigned long targetless_wakeups; | ||
| 100 | static unsigned long multitarget_wakeups; | ||
| 101 | |||
| 102 | static u64 cpu_usage; | ||
| 103 | static u64 runavg_cpu_usage; | ||
| 104 | static u64 parent_cpu_usage; | ||
| 105 | static u64 runavg_parent_cpu_usage; | ||
| 106 | |||
| 107 | static unsigned long nr_runs; | ||
| 108 | static u64 sum_runtime; | ||
| 109 | static u64 sum_fluct; | ||
| 110 | static u64 run_avg; | ||
| 111 | |||
| 112 | static unsigned long replay_repeat = 10; | ||
| 113 | static unsigned long nr_timestamps; | ||
| 114 | static unsigned long nr_unordered_timestamps; | ||
| 115 | static unsigned long nr_state_machine_bugs; | ||
| 116 | static unsigned long nr_context_switch_bugs; | ||
| 117 | static unsigned long nr_events; | ||
| 118 | static unsigned long nr_lost_chunks; | ||
| 119 | static unsigned long nr_lost_events; | ||
| 120 | |||
| 121 | #define TASK_STATE_TO_CHAR_STR "RSDTtZX" | ||
| 122 | |||
| 123 | enum thread_state { | ||
| 124 | THREAD_SLEEPING = 0, | ||
| 125 | THREAD_WAIT_CPU, | ||
| 126 | THREAD_SCHED_IN, | ||
| 127 | THREAD_IGNORE | ||
| 128 | }; | ||
| 129 | |||
| 130 | struct work_atom { | ||
| 131 | struct list_head list; | ||
| 132 | enum thread_state state; | ||
| 133 | u64 sched_out_time; | ||
| 134 | u64 wake_up_time; | ||
| 135 | u64 sched_in_time; | ||
| 136 | u64 runtime; | ||
| 137 | }; | ||
| 138 | |||
| 139 | struct work_atoms { | ||
| 140 | struct list_head work_list; | ||
| 141 | struct thread *thread; | ||
| 142 | struct rb_node node; | ||
| 143 | u64 max_lat; | ||
| 144 | u64 total_lat; | ||
| 145 | u64 nb_atoms; | ||
| 146 | u64 total_runtime; | ||
| 147 | }; | ||
| 148 | |||
| 149 | typedef int (*sort_fn_t)(struct work_atoms *, struct work_atoms *); | ||
| 150 | |||
| 151 | static struct rb_root atom_root, sorted_atom_root; | ||
| 152 | |||
| 153 | static u64 all_runtime; | ||
| 154 | static u64 all_count; | ||
| 155 | |||
| 156 | |||
| 157 | static u64 get_nsecs(void) | ||
| 158 | { | ||
| 159 | struct timespec ts; | ||
| 160 | |||
| 161 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
| 162 | |||
| 163 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; | ||
| 164 | } | ||
| 165 | |||
| 166 | static void burn_nsecs(u64 nsecs) | ||
| 167 | { | ||
| 168 | u64 T0 = get_nsecs(), T1; | ||
| 169 | |||
| 170 | do { | ||
| 171 | T1 = get_nsecs(); | ||
| 172 | } while (T1 + run_measurement_overhead < T0 + nsecs); | ||
| 173 | } | ||
| 174 | |||
| 175 | static void sleep_nsecs(u64 nsecs) | ||
| 176 | { | ||
| 177 | struct timespec ts; | ||
| 178 | |||
| 179 | ts.tv_nsec = nsecs % 999999999; | ||
| 180 | ts.tv_sec = nsecs / 999999999; | ||
| 181 | |||
| 182 | nanosleep(&ts, NULL); | ||
| 183 | } | ||
| 184 | |||
| 185 | static void calibrate_run_measurement_overhead(void) | ||
| 186 | { | ||
| 187 | u64 T0, T1, delta, min_delta = 1000000000ULL; | ||
| 188 | int i; | ||
| 189 | |||
| 190 | for (i = 0; i < 10; i++) { | ||
| 191 | T0 = get_nsecs(); | ||
| 192 | burn_nsecs(0); | ||
| 193 | T1 = get_nsecs(); | ||
| 194 | delta = T1-T0; | ||
| 195 | min_delta = min(min_delta, delta); | ||
| 196 | } | ||
| 197 | run_measurement_overhead = min_delta; | ||
| 198 | |||
| 199 | printf("run measurement overhead: %Ld nsecs\n", min_delta); | ||
| 200 | } | ||
| 201 | |||
| 202 | static void calibrate_sleep_measurement_overhead(void) | ||
| 203 | { | ||
| 204 | u64 T0, T1, delta, min_delta = 1000000000ULL; | ||
| 205 | int i; | ||
| 206 | |||
| 207 | for (i = 0; i < 10; i++) { | ||
| 208 | T0 = get_nsecs(); | ||
| 209 | sleep_nsecs(10000); | ||
| 210 | T1 = get_nsecs(); | ||
| 211 | delta = T1-T0; | ||
| 212 | min_delta = min(min_delta, delta); | ||
| 213 | } | ||
| 214 | min_delta -= 10000; | ||
| 215 | sleep_measurement_overhead = min_delta; | ||
| 216 | |||
| 217 | printf("sleep measurement overhead: %Ld nsecs\n", min_delta); | ||
| 218 | } | ||
| 219 | |||
| 220 | static struct sched_atom * | ||
| 221 | get_new_event(struct task_desc *task, u64 timestamp) | ||
| 222 | { | ||
| 223 | struct sched_atom *event = zalloc(sizeof(*event)); | ||
| 224 | unsigned long idx = task->nr_events; | ||
| 225 | size_t size; | ||
| 226 | |||
| 227 | event->timestamp = timestamp; | ||
| 228 | event->nr = idx; | ||
| 229 | |||
| 230 | task->nr_events++; | ||
| 231 | size = sizeof(struct sched_atom *) * task->nr_events; | ||
| 232 | task->atoms = realloc(task->atoms, size); | ||
| 233 | BUG_ON(!task->atoms); | ||
| 234 | |||
| 235 | task->atoms[idx] = event; | ||
| 236 | |||
| 237 | return event; | ||
| 238 | } | ||
| 239 | |||
| 240 | static struct sched_atom *last_event(struct task_desc *task) | ||
| 241 | { | ||
| 242 | if (!task->nr_events) | ||
| 243 | return NULL; | ||
| 244 | |||
| 245 | return task->atoms[task->nr_events - 1]; | ||
| 246 | } | ||
| 247 | |||
| 248 | static void | ||
| 249 | add_sched_event_run(struct task_desc *task, u64 timestamp, u64 duration) | ||
| 250 | { | ||
| 251 | struct sched_atom *event, *curr_event = last_event(task); | ||
| 252 | |||
| 253 | /* | ||
| 254 | * optimize an existing RUN event by merging this one | ||
| 255 | * to it: | ||
| 256 | */ | ||
| 257 | if (curr_event && curr_event->type == SCHED_EVENT_RUN) { | ||
| 258 | nr_run_events_optimized++; | ||
| 259 | curr_event->duration += duration; | ||
| 260 | return; | ||
| 261 | } | ||
| 262 | |||
| 263 | event = get_new_event(task, timestamp); | ||
| 264 | |||
| 265 | event->type = SCHED_EVENT_RUN; | ||
| 266 | event->duration = duration; | ||
| 267 | |||
| 268 | nr_run_events++; | ||
| 269 | } | ||
| 270 | |||
| 271 | static void | ||
| 272 | add_sched_event_wakeup(struct task_desc *task, u64 timestamp, | ||
| 273 | struct task_desc *wakee) | ||
| 274 | { | ||
| 275 | struct sched_atom *event, *wakee_event; | ||
| 276 | |||
| 277 | event = get_new_event(task, timestamp); | ||
| 278 | event->type = SCHED_EVENT_WAKEUP; | ||
| 279 | event->wakee = wakee; | ||
| 280 | |||
| 281 | wakee_event = last_event(wakee); | ||
| 282 | if (!wakee_event || wakee_event->type != SCHED_EVENT_SLEEP) { | ||
| 283 | targetless_wakeups++; | ||
| 284 | return; | ||
| 285 | } | ||
| 286 | if (wakee_event->wait_sem) { | ||
| 287 | multitarget_wakeups++; | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | |||
| 291 | wakee_event->wait_sem = zalloc(sizeof(*wakee_event->wait_sem)); | ||
| 292 | sem_init(wakee_event->wait_sem, 0, 0); | ||
| 293 | wakee_event->specific_wait = 1; | ||
| 294 | event->wait_sem = wakee_event->wait_sem; | ||
| 295 | |||
| 296 | nr_wakeup_events++; | ||
| 297 | } | ||
| 298 | |||
| 299 | static void | ||
| 300 | add_sched_event_sleep(struct task_desc *task, u64 timestamp, | ||
| 301 | u64 task_state __used) | ||
| 302 | { | ||
| 303 | struct sched_atom *event = get_new_event(task, timestamp); | ||
| 304 | |||
| 305 | event->type = SCHED_EVENT_SLEEP; | ||
| 306 | |||
| 307 | nr_sleep_events++; | ||
| 308 | } | ||
| 309 | |||
| 310 | static struct task_desc *register_pid(unsigned long pid, const char *comm) | ||
| 311 | { | ||
| 312 | struct task_desc *task; | ||
| 313 | |||
| 314 | BUG_ON(pid >= MAX_PID); | ||
| 315 | |||
| 316 | task = pid_to_task[pid]; | ||
| 317 | |||
| 318 | if (task) | ||
| 319 | return task; | ||
| 320 | |||
| 321 | task = zalloc(sizeof(*task)); | ||
| 322 | task->pid = pid; | ||
| 323 | task->nr = nr_tasks; | ||
| 324 | strcpy(task->comm, comm); | ||
| 325 | /* | ||
| 326 | * every task starts in sleeping state - this gets ignored | ||
| 327 | * if there's no wakeup pointing to this sleep state: | ||
| 328 | */ | ||
| 329 | add_sched_event_sleep(task, 0, 0); | ||
| 330 | |||
| 331 | pid_to_task[pid] = task; | ||
| 332 | nr_tasks++; | ||
| 333 | tasks = realloc(tasks, nr_tasks*sizeof(struct task_task *)); | ||
| 334 | BUG_ON(!tasks); | ||
| 335 | tasks[task->nr] = task; | ||
| 336 | |||
| 337 | if (verbose) | ||
| 338 | printf("registered task #%ld, PID %ld (%s)\n", nr_tasks, pid, comm); | ||
| 339 | |||
| 340 | return task; | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | static void print_task_traces(void) | ||
| 345 | { | ||
| 346 | struct task_desc *task; | ||
| 347 | unsigned long i; | ||
| 348 | |||
| 349 | for (i = 0; i < nr_tasks; i++) { | ||
| 350 | task = tasks[i]; | ||
| 351 | printf("task %6ld (%20s:%10ld), nr_events: %ld\n", | ||
| 352 | task->nr, task->comm, task->pid, task->nr_events); | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | static void add_cross_task_wakeups(void) | ||
| 357 | { | ||
| 358 | struct task_desc *task1, *task2; | ||
| 359 | unsigned long i, j; | ||
| 360 | |||
| 361 | for (i = 0; i < nr_tasks; i++) { | ||
| 362 | task1 = tasks[i]; | ||
| 363 | j = i + 1; | ||
| 364 | if (j == nr_tasks) | ||
| 365 | j = 0; | ||
| 366 | task2 = tasks[j]; | ||
| 367 | add_sched_event_wakeup(task1, 0, task2); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | static void | ||
| 372 | process_sched_event(struct task_desc *this_task __used, struct sched_atom *atom) | ||
| 373 | { | ||
| 374 | int ret = 0; | ||
| 375 | u64 now; | ||
| 376 | long long delta; | ||
| 377 | |||
| 378 | now = get_nsecs(); | ||
| 379 | delta = start_time + atom->timestamp - now; | ||
| 380 | |||
| 381 | switch (atom->type) { | ||
| 382 | case SCHED_EVENT_RUN: | ||
| 383 | burn_nsecs(atom->duration); | ||
| 384 | break; | ||
| 385 | case SCHED_EVENT_SLEEP: | ||
| 386 | if (atom->wait_sem) | ||
| 387 | ret = sem_wait(atom->wait_sem); | ||
| 388 | BUG_ON(ret); | ||
| 389 | break; | ||
| 390 | case SCHED_EVENT_WAKEUP: | ||
| 391 | if (atom->wait_sem) | ||
| 392 | ret = sem_post(atom->wait_sem); | ||
| 393 | BUG_ON(ret); | ||
| 394 | break; | ||
| 395 | case SCHED_EVENT_MIGRATION: | ||
| 396 | break; | ||
| 397 | default: | ||
| 398 | BUG_ON(1); | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | static u64 get_cpu_usage_nsec_parent(void) | ||
| 403 | { | ||
| 404 | struct rusage ru; | ||
| 405 | u64 sum; | ||
| 406 | int err; | ||
| 407 | |||
| 408 | err = getrusage(RUSAGE_SELF, &ru); | ||
| 409 | BUG_ON(err); | ||
| 410 | |||
| 411 | sum = ru.ru_utime.tv_sec*1e9 + ru.ru_utime.tv_usec*1e3; | ||
| 412 | sum += ru.ru_stime.tv_sec*1e9 + ru.ru_stime.tv_usec*1e3; | ||
| 413 | |||
| 414 | return sum; | ||
| 415 | } | ||
| 416 | |||
| 417 | static u64 get_cpu_usage_nsec_self(void) | ||
| 418 | { | ||
| 419 | char filename [] = "/proc/1234567890/sched"; | ||
| 420 | unsigned long msecs, nsecs; | ||
| 421 | char *line = NULL; | ||
| 422 | u64 total = 0; | ||
| 423 | size_t len = 0; | ||
| 424 | ssize_t chars; | ||
| 425 | FILE *file; | ||
| 426 | int ret; | ||
| 427 | |||
| 428 | sprintf(filename, "/proc/%d/sched", getpid()); | ||
| 429 | file = fopen(filename, "r"); | ||
| 430 | BUG_ON(!file); | ||
| 431 | |||
| 432 | while ((chars = getline(&line, &len, file)) != -1) { | ||
| 433 | ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n", | ||
| 434 | &msecs, &nsecs); | ||
| 435 | if (ret == 2) { | ||
| 436 | total = msecs*1e6 + nsecs; | ||
| 437 | break; | ||
| 438 | } | ||
| 439 | } | ||
| 440 | if (line) | ||
| 441 | free(line); | ||
| 442 | fclose(file); | ||
| 443 | |||
| 444 | return total; | ||
| 445 | } | ||
| 446 | |||
| 447 | static void *thread_func(void *ctx) | ||
| 448 | { | ||
| 449 | struct task_desc *this_task = ctx; | ||
| 450 | u64 cpu_usage_0, cpu_usage_1; | ||
| 451 | unsigned long i, ret; | ||
| 452 | char comm2[22]; | ||
| 453 | |||
| 454 | sprintf(comm2, ":%s", this_task->comm); | ||
| 455 | prctl(PR_SET_NAME, comm2); | ||
| 456 | |||
| 457 | again: | ||
| 458 | ret = sem_post(&this_task->ready_for_work); | ||
| 459 | BUG_ON(ret); | ||
| 460 | ret = pthread_mutex_lock(&start_work_mutex); | ||
| 461 | BUG_ON(ret); | ||
| 462 | ret = pthread_mutex_unlock(&start_work_mutex); | ||
| 463 | BUG_ON(ret); | ||
| 464 | |||
| 465 | cpu_usage_0 = get_cpu_usage_nsec_self(); | ||
| 466 | |||
| 467 | for (i = 0; i < this_task->nr_events; i++) { | ||
| 468 | this_task->curr_event = i; | ||
| 469 | process_sched_event(this_task, this_task->atoms[i]); | ||
| 470 | } | ||
| 471 | |||
| 472 | cpu_usage_1 = get_cpu_usage_nsec_self(); | ||
| 473 | this_task->cpu_usage = cpu_usage_1 - cpu_usage_0; | ||
| 474 | |||
| 475 | ret = sem_post(&this_task->work_done_sem); | ||
| 476 | BUG_ON(ret); | ||
| 477 | |||
| 478 | ret = pthread_mutex_lock(&work_done_wait_mutex); | ||
| 479 | BUG_ON(ret); | ||
| 480 | ret = pthread_mutex_unlock(&work_done_wait_mutex); | ||
| 481 | BUG_ON(ret); | ||
| 482 | |||
| 483 | goto again; | ||
| 484 | } | ||
| 485 | |||
| 486 | static void create_tasks(void) | ||
| 487 | { | ||
| 488 | struct task_desc *task; | ||
| 489 | pthread_attr_t attr; | ||
| 490 | unsigned long i; | ||
| 491 | int err; | ||
| 492 | |||
| 493 | err = pthread_attr_init(&attr); | ||
| 494 | BUG_ON(err); | ||
| 495 | err = pthread_attr_setstacksize(&attr, (size_t)(16*1024)); | ||
| 496 | BUG_ON(err); | ||
| 497 | err = pthread_mutex_lock(&start_work_mutex); | ||
| 498 | BUG_ON(err); | ||
| 499 | err = pthread_mutex_lock(&work_done_wait_mutex); | ||
| 500 | BUG_ON(err); | ||
| 501 | for (i = 0; i < nr_tasks; i++) { | ||
| 502 | task = tasks[i]; | ||
| 503 | sem_init(&task->sleep_sem, 0, 0); | ||
| 504 | sem_init(&task->ready_for_work, 0, 0); | ||
| 505 | sem_init(&task->work_done_sem, 0, 0); | ||
| 506 | task->curr_event = 0; | ||
| 507 | err = pthread_create(&task->thread, &attr, thread_func, task); | ||
| 508 | BUG_ON(err); | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | static void wait_for_tasks(void) | ||
| 513 | { | ||
| 514 | u64 cpu_usage_0, cpu_usage_1; | ||
| 515 | struct task_desc *task; | ||
| 516 | unsigned long i, ret; | ||
| 517 | |||
| 518 | start_time = get_nsecs(); | ||
| 519 | cpu_usage = 0; | ||
| 520 | pthread_mutex_unlock(&work_done_wait_mutex); | ||
| 521 | |||
| 522 | for (i = 0; i < nr_tasks; i++) { | ||
| 523 | task = tasks[i]; | ||
| 524 | ret = sem_wait(&task->ready_for_work); | ||
| 525 | BUG_ON(ret); | ||
| 526 | sem_init(&task->ready_for_work, 0, 0); | ||
| 527 | } | ||
| 528 | ret = pthread_mutex_lock(&work_done_wait_mutex); | ||
| 529 | BUG_ON(ret); | ||
| 530 | |||
| 531 | cpu_usage_0 = get_cpu_usage_nsec_parent(); | ||
| 532 | |||
| 533 | pthread_mutex_unlock(&start_work_mutex); | ||
| 534 | |||
| 535 | for (i = 0; i < nr_tasks; i++) { | ||
| 536 | task = tasks[i]; | ||
| 537 | ret = sem_wait(&task->work_done_sem); | ||
| 538 | BUG_ON(ret); | ||
| 539 | sem_init(&task->work_done_sem, 0, 0); | ||
| 540 | cpu_usage += task->cpu_usage; | ||
| 541 | task->cpu_usage = 0; | ||
| 542 | } | ||
| 543 | |||
| 544 | cpu_usage_1 = get_cpu_usage_nsec_parent(); | ||
| 545 | if (!runavg_cpu_usage) | ||
| 546 | runavg_cpu_usage = cpu_usage; | ||
| 547 | runavg_cpu_usage = (runavg_cpu_usage*9 + cpu_usage)/10; | ||
| 548 | |||
| 549 | parent_cpu_usage = cpu_usage_1 - cpu_usage_0; | ||
| 550 | if (!runavg_parent_cpu_usage) | ||
| 551 | runavg_parent_cpu_usage = parent_cpu_usage; | ||
| 552 | runavg_parent_cpu_usage = (runavg_parent_cpu_usage*9 + | ||
| 553 | parent_cpu_usage)/10; | ||
| 554 | |||
| 555 | ret = pthread_mutex_lock(&start_work_mutex); | ||
| 556 | BUG_ON(ret); | ||
| 557 | |||
| 558 | for (i = 0; i < nr_tasks; i++) { | ||
| 559 | task = tasks[i]; | ||
| 560 | sem_init(&task->sleep_sem, 0, 0); | ||
| 561 | task->curr_event = 0; | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | static void run_one_test(void) | ||
| 566 | { | ||
| 567 | u64 T0, T1, delta, avg_delta, fluct, std_dev; | ||
| 568 | |||
| 569 | T0 = get_nsecs(); | ||
| 570 | wait_for_tasks(); | ||
| 571 | T1 = get_nsecs(); | ||
| 572 | |||
| 573 | delta = T1 - T0; | ||
| 574 | sum_runtime += delta; | ||
| 575 | nr_runs++; | ||
| 576 | |||
| 577 | avg_delta = sum_runtime / nr_runs; | ||
| 578 | if (delta < avg_delta) | ||
| 579 | fluct = avg_delta - delta; | ||
| 580 | else | ||
| 581 | fluct = delta - avg_delta; | ||
| 582 | sum_fluct += fluct; | ||
| 583 | std_dev = sum_fluct / nr_runs / sqrt(nr_runs); | ||
| 584 | if (!run_avg) | ||
| 585 | run_avg = delta; | ||
| 586 | run_avg = (run_avg*9 + delta)/10; | ||
| 587 | |||
| 588 | printf("#%-3ld: %0.3f, ", | ||
| 589 | nr_runs, (double)delta/1000000.0); | ||
| 590 | |||
| 591 | printf("ravg: %0.2f, ", | ||
| 592 | (double)run_avg/1e6); | ||
| 593 | |||
| 594 | printf("cpu: %0.2f / %0.2f", | ||
| 595 | (double)cpu_usage/1e6, (double)runavg_cpu_usage/1e6); | ||
| 596 | |||
| 597 | #if 0 | ||
| 598 | /* | ||
| 599 | * rusage statistics done by the parent, these are less | ||
| 600 | * accurate than the sum_exec_runtime based statistics: | ||
| 601 | */ | ||
| 602 | printf(" [%0.2f / %0.2f]", | ||
| 603 | (double)parent_cpu_usage/1e6, | ||
| 604 | (double)runavg_parent_cpu_usage/1e6); | ||
| 605 | #endif | ||
| 606 | |||
| 607 | printf("\n"); | ||
| 608 | |||
| 609 | if (nr_sleep_corrections) | ||
| 610 | printf(" (%ld sleep corrections)\n", nr_sleep_corrections); | ||
| 611 | nr_sleep_corrections = 0; | ||
| 612 | } | ||
| 613 | |||
| 614 | static void test_calibrations(void) | ||
| 615 | { | ||
| 616 | u64 T0, T1; | ||
| 617 | |||
| 618 | T0 = get_nsecs(); | ||
| 619 | burn_nsecs(1e6); | ||
| 620 | T1 = get_nsecs(); | ||
| 621 | |||
| 622 | printf("the run test took %Ld nsecs\n", T1-T0); | ||
| 623 | |||
| 624 | T0 = get_nsecs(); | ||
| 625 | sleep_nsecs(1e6); | ||
| 626 | T1 = get_nsecs(); | ||
| 627 | |||
| 628 | printf("the sleep test took %Ld nsecs\n", T1-T0); | ||
| 629 | } | ||
| 630 | |||
| 631 | struct raw_event_sample { | ||
| 632 | u32 size; | ||
| 633 | char data[0]; | ||
| 634 | }; | ||
| 635 | |||
| 636 | #define FILL_FIELD(ptr, field, event, data) \ | ||
| 637 | ptr.field = (typeof(ptr.field)) raw_field_value(event, #field, data) | ||
| 638 | |||
| 639 | #define FILL_ARRAY(ptr, array, event, data) \ | ||
| 640 | do { \ | ||
| 641 | void *__array = raw_field_ptr(event, #array, data); \ | ||
| 642 | memcpy(ptr.array, __array, sizeof(ptr.array)); \ | ||
| 643 | } while(0) | ||
| 644 | |||
| 645 | #define FILL_COMMON_FIELDS(ptr, event, data) \ | ||
| 646 | do { \ | ||
| 647 | FILL_FIELD(ptr, common_type, event, data); \ | ||
| 648 | FILL_FIELD(ptr, common_flags, event, data); \ | ||
| 649 | FILL_FIELD(ptr, common_preempt_count, event, data); \ | ||
| 650 | FILL_FIELD(ptr, common_pid, event, data); \ | ||
| 651 | FILL_FIELD(ptr, common_tgid, event, data); \ | ||
| 652 | } while (0) | ||
| 653 | |||
| 654 | |||
| 655 | |||
| 656 | struct trace_switch_event { | ||
| 657 | u32 size; | ||
| 658 | |||
| 659 | u16 common_type; | ||
| 660 | u8 common_flags; | ||
| 661 | u8 common_preempt_count; | ||
| 662 | u32 common_pid; | ||
| 663 | u32 common_tgid; | ||
| 664 | |||
| 665 | char prev_comm[16]; | ||
| 666 | u32 prev_pid; | ||
| 667 | u32 prev_prio; | ||
| 668 | u64 prev_state; | ||
| 669 | char next_comm[16]; | ||
| 670 | u32 next_pid; | ||
| 671 | u32 next_prio; | ||
| 672 | }; | ||
| 673 | |||
| 674 | struct trace_runtime_event { | ||
| 675 | u32 size; | ||
| 676 | |||
| 677 | u16 common_type; | ||
| 678 | u8 common_flags; | ||
| 679 | u8 common_preempt_count; | ||
| 680 | u32 common_pid; | ||
| 681 | u32 common_tgid; | ||
| 682 | |||
| 683 | char comm[16]; | ||
| 684 | u32 pid; | ||
| 685 | u64 runtime; | ||
| 686 | u64 vruntime; | ||
| 687 | }; | ||
| 688 | |||
| 689 | struct trace_wakeup_event { | ||
| 690 | u32 size; | ||
| 691 | |||
| 692 | u16 common_type; | ||
| 693 | u8 common_flags; | ||
| 694 | u8 common_preempt_count; | ||
| 695 | u32 common_pid; | ||
| 696 | u32 common_tgid; | ||
| 697 | |||
| 698 | char comm[16]; | ||
| 699 | u32 pid; | ||
| 700 | |||
| 701 | u32 prio; | ||
| 702 | u32 success; | ||
| 703 | u32 cpu; | ||
| 704 | }; | ||
| 705 | |||
| 706 | struct trace_fork_event { | ||
| 707 | u32 size; | ||
| 708 | |||
| 709 | u16 common_type; | ||
| 710 | u8 common_flags; | ||
| 711 | u8 common_preempt_count; | ||
| 712 | u32 common_pid; | ||
| 713 | u32 common_tgid; | ||
| 714 | |||
| 715 | char parent_comm[16]; | ||
| 716 | u32 parent_pid; | ||
| 717 | char child_comm[16]; | ||
| 718 | u32 child_pid; | ||
| 719 | }; | ||
| 720 | |||
| 721 | struct trace_migrate_task_event { | ||
| 722 | u32 size; | ||
| 723 | |||
| 724 | u16 common_type; | ||
| 725 | u8 common_flags; | ||
| 726 | u8 common_preempt_count; | ||
| 727 | u32 common_pid; | ||
| 728 | u32 common_tgid; | ||
| 729 | |||
| 730 | char comm[16]; | ||
| 731 | u32 pid; | ||
| 732 | |||
| 733 | u32 prio; | ||
| 734 | u32 cpu; | ||
| 735 | }; | ||
| 736 | |||
| 737 | struct trace_sched_handler { | ||
| 738 | void (*switch_event)(struct trace_switch_event *, | ||
| 739 | struct event *, | ||
| 740 | int cpu, | ||
| 741 | u64 timestamp, | ||
| 742 | struct thread *thread); | ||
| 743 | |||
| 744 | void (*runtime_event)(struct trace_runtime_event *, | ||
| 745 | struct event *, | ||
| 746 | int cpu, | ||
| 747 | u64 timestamp, | ||
| 748 | struct thread *thread); | ||
| 749 | |||
| 750 | void (*wakeup_event)(struct trace_wakeup_event *, | ||
| 751 | struct event *, | ||
| 752 | int cpu, | ||
| 753 | u64 timestamp, | ||
| 754 | struct thread *thread); | ||
| 755 | |||
| 756 | void (*fork_event)(struct trace_fork_event *, | ||
| 757 | struct event *, | ||
| 758 | int cpu, | ||
| 759 | u64 timestamp, | ||
| 760 | struct thread *thread); | ||
| 761 | |||
| 762 | void (*migrate_task_event)(struct trace_migrate_task_event *, | ||
| 763 | struct event *, | ||
| 764 | int cpu, | ||
| 765 | u64 timestamp, | ||
| 766 | struct thread *thread); | ||
| 767 | }; | ||
| 768 | |||
| 769 | |||
| 770 | static void | ||
| 771 | replay_wakeup_event(struct trace_wakeup_event *wakeup_event, | ||
| 772 | struct event *event, | ||
| 773 | int cpu __used, | ||
| 774 | u64 timestamp __used, | ||
| 775 | struct thread *thread __used) | ||
| 776 | { | ||
| 777 | struct task_desc *waker, *wakee; | ||
| 778 | |||
| 779 | if (verbose) { | ||
| 780 | printf("sched_wakeup event %p\n", event); | ||
| 781 | |||
| 782 | printf(" ... pid %d woke up %s/%d\n", | ||
| 783 | wakeup_event->common_pid, | ||
| 784 | wakeup_event->comm, | ||
| 785 | wakeup_event->pid); | ||
| 786 | } | ||
| 787 | |||
| 788 | waker = register_pid(wakeup_event->common_pid, "<unknown>"); | ||
| 789 | wakee = register_pid(wakeup_event->pid, wakeup_event->comm); | ||
| 790 | |||
| 791 | add_sched_event_wakeup(waker, timestamp, wakee); | ||
| 792 | } | ||
| 793 | |||
| 794 | static u64 cpu_last_switched[MAX_CPUS]; | ||
| 795 | |||
| 796 | static void | ||
| 797 | replay_switch_event(struct trace_switch_event *switch_event, | ||
| 798 | struct event *event, | ||
| 799 | int cpu, | ||
| 800 | u64 timestamp, | ||
| 801 | struct thread *thread __used) | ||
| 802 | { | ||
| 803 | struct task_desc *prev, *next; | ||
| 804 | u64 timestamp0; | ||
| 805 | s64 delta; | ||
| 806 | |||
| 807 | if (verbose) | ||
| 808 | printf("sched_switch event %p\n", event); | ||
| 809 | |||
| 810 | if (cpu >= MAX_CPUS || cpu < 0) | ||
| 811 | return; | ||
| 812 | |||
| 813 | timestamp0 = cpu_last_switched[cpu]; | ||
| 814 | if (timestamp0) | ||
| 815 | delta = timestamp - timestamp0; | ||
| 816 | else | ||
| 817 | delta = 0; | ||
| 818 | |||
| 819 | if (delta < 0) | ||
| 820 | die("hm, delta: %Ld < 0 ?\n", delta); | ||
| 821 | |||
| 822 | if (verbose) { | ||
| 823 | printf(" ... switch from %s/%d to %s/%d [ran %Ld nsecs]\n", | ||
| 824 | switch_event->prev_comm, switch_event->prev_pid, | ||
| 825 | switch_event->next_comm, switch_event->next_pid, | ||
| 826 | delta); | ||
| 827 | } | ||
| 828 | |||
| 829 | prev = register_pid(switch_event->prev_pid, switch_event->prev_comm); | ||
| 830 | next = register_pid(switch_event->next_pid, switch_event->next_comm); | ||
| 831 | |||
| 832 | cpu_last_switched[cpu] = timestamp; | ||
| 833 | |||
| 834 | add_sched_event_run(prev, timestamp, delta); | ||
| 835 | add_sched_event_sleep(prev, timestamp, switch_event->prev_state); | ||
| 836 | } | ||
| 837 | |||
| 838 | |||
| 839 | static void | ||
| 840 | replay_fork_event(struct trace_fork_event *fork_event, | ||
| 841 | struct event *event, | ||
| 842 | int cpu __used, | ||
| 843 | u64 timestamp __used, | ||
| 844 | struct thread *thread __used) | ||
| 845 | { | ||
| 846 | if (verbose) { | ||
| 847 | printf("sched_fork event %p\n", event); | ||
| 848 | printf("... parent: %s/%d\n", fork_event->parent_comm, fork_event->parent_pid); | ||
| 849 | printf("... child: %s/%d\n", fork_event->child_comm, fork_event->child_pid); | ||
| 850 | } | ||
| 851 | register_pid(fork_event->parent_pid, fork_event->parent_comm); | ||
| 852 | register_pid(fork_event->child_pid, fork_event->child_comm); | ||
| 853 | } | ||
| 854 | |||
| 855 | static struct trace_sched_handler replay_ops = { | ||
| 856 | .wakeup_event = replay_wakeup_event, | ||
| 857 | .switch_event = replay_switch_event, | ||
| 858 | .fork_event = replay_fork_event, | ||
| 859 | }; | ||
| 860 | |||
| 861 | struct sort_dimension { | ||
| 862 | const char *name; | ||
| 863 | sort_fn_t cmp; | ||
| 864 | struct list_head list; | ||
| 865 | }; | ||
| 866 | |||
| 867 | static LIST_HEAD(cmp_pid); | ||
| 868 | |||
| 869 | static int | ||
| 870 | thread_lat_cmp(struct list_head *list, struct work_atoms *l, struct work_atoms *r) | ||
| 871 | { | ||
| 872 | struct sort_dimension *sort; | ||
| 873 | int ret = 0; | ||
| 874 | |||
| 875 | BUG_ON(list_empty(list)); | ||
| 876 | |||
| 877 | list_for_each_entry(sort, list, list) { | ||
| 878 | ret = sort->cmp(l, r); | ||
| 879 | if (ret) | ||
| 880 | return ret; | ||
| 881 | } | ||
| 882 | |||
| 883 | return ret; | ||
| 884 | } | ||
| 885 | |||
| 886 | static struct work_atoms * | ||
| 887 | thread_atoms_search(struct rb_root *root, struct thread *thread, | ||
| 888 | struct list_head *sort_list) | ||
| 889 | { | ||
| 890 | struct rb_node *node = root->rb_node; | ||
| 891 | struct work_atoms key = { .thread = thread }; | ||
| 892 | |||
| 893 | while (node) { | ||
| 894 | struct work_atoms *atoms; | ||
| 895 | int cmp; | ||
| 896 | |||
| 897 | atoms = container_of(node, struct work_atoms, node); | ||
| 898 | |||
| 899 | cmp = thread_lat_cmp(sort_list, &key, atoms); | ||
| 900 | if (cmp > 0) | ||
| 901 | node = node->rb_left; | ||
| 902 | else if (cmp < 0) | ||
| 903 | node = node->rb_right; | ||
| 904 | else { | ||
| 905 | BUG_ON(thread != atoms->thread); | ||
| 906 | return atoms; | ||
| 907 | } | ||
| 908 | } | ||
| 909 | return NULL; | ||
| 910 | } | ||
| 911 | |||
| 912 | static void | ||
| 913 | __thread_latency_insert(struct rb_root *root, struct work_atoms *data, | ||
| 914 | struct list_head *sort_list) | ||
| 915 | { | ||
| 916 | struct rb_node **new = &(root->rb_node), *parent = NULL; | ||
| 917 | |||
| 918 | while (*new) { | ||
| 919 | struct work_atoms *this; | ||
| 920 | int cmp; | ||
| 921 | |||
| 922 | this = container_of(*new, struct work_atoms, node); | ||
| 923 | parent = *new; | ||
| 924 | |||
| 925 | cmp = thread_lat_cmp(sort_list, data, this); | ||
| 926 | |||
| 927 | if (cmp > 0) | ||
| 928 | new = &((*new)->rb_left); | ||
| 929 | else | ||
| 930 | new = &((*new)->rb_right); | ||
| 931 | } | ||
| 932 | |||
| 933 | rb_link_node(&data->node, parent, new); | ||
| 934 | rb_insert_color(&data->node, root); | ||
| 935 | } | ||
| 936 | |||
| 937 | static void thread_atoms_insert(struct thread *thread) | ||
| 938 | { | ||
| 939 | struct work_atoms *atoms = zalloc(sizeof(*atoms)); | ||
| 940 | if (!atoms) | ||
| 941 | die("No memory"); | ||
| 942 | |||
| 943 | atoms->thread = thread; | ||
| 944 | INIT_LIST_HEAD(&atoms->work_list); | ||
| 945 | __thread_latency_insert(&atom_root, atoms, &cmp_pid); | ||
| 946 | } | ||
| 947 | |||
| 948 | static void | ||
| 949 | latency_fork_event(struct trace_fork_event *fork_event __used, | ||
| 950 | struct event *event __used, | ||
| 951 | int cpu __used, | ||
| 952 | u64 timestamp __used, | ||
| 953 | struct thread *thread __used) | ||
| 954 | { | ||
| 955 | /* should insert the newcomer */ | ||
| 956 | } | ||
| 957 | |||
| 958 | __used | ||
| 959 | static char sched_out_state(struct trace_switch_event *switch_event) | ||
| 960 | { | ||
| 961 | const char *str = TASK_STATE_TO_CHAR_STR; | ||
| 962 | |||
| 963 | return str[switch_event->prev_state]; | ||
| 964 | } | ||
| 965 | |||
| 966 | static void | ||
| 967 | add_sched_out_event(struct work_atoms *atoms, | ||
| 968 | char run_state, | ||
| 969 | u64 timestamp) | ||
| 970 | { | ||
| 971 | struct work_atom *atom = zalloc(sizeof(*atom)); | ||
| 972 | if (!atom) | ||
| 973 | die("Non memory"); | ||
| 974 | |||
| 975 | atom->sched_out_time = timestamp; | ||
| 976 | |||
| 977 | if (run_state == 'R') { | ||
| 978 | atom->state = THREAD_WAIT_CPU; | ||
| 979 | atom->wake_up_time = atom->sched_out_time; | ||
| 980 | } | ||
| 981 | |||
| 982 | list_add_tail(&atom->list, &atoms->work_list); | ||
| 983 | } | ||
| 984 | |||
| 985 | static void | ||
| 986 | add_runtime_event(struct work_atoms *atoms, u64 delta, u64 timestamp __used) | ||
| 987 | { | ||
| 988 | struct work_atom *atom; | ||
| 989 | |||
| 990 | BUG_ON(list_empty(&atoms->work_list)); | ||
| 991 | |||
| 992 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | ||
| 993 | |||
| 994 | atom->runtime += delta; | ||
| 995 | atoms->total_runtime += delta; | ||
| 996 | } | ||
| 997 | |||
| 998 | static void | ||
| 999 | add_sched_in_event(struct work_atoms *atoms, u64 timestamp) | ||
| 1000 | { | ||
| 1001 | struct work_atom *atom; | ||
| 1002 | u64 delta; | ||
| 1003 | |||
| 1004 | if (list_empty(&atoms->work_list)) | ||
| 1005 | return; | ||
| 1006 | |||
| 1007 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | ||
| 1008 | |||
| 1009 | if (atom->state != THREAD_WAIT_CPU) | ||
| 1010 | return; | ||
| 1011 | |||
| 1012 | if (timestamp < atom->wake_up_time) { | ||
| 1013 | atom->state = THREAD_IGNORE; | ||
| 1014 | return; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | atom->state = THREAD_SCHED_IN; | ||
| 1018 | atom->sched_in_time = timestamp; | ||
| 1019 | |||
| 1020 | delta = atom->sched_in_time - atom->wake_up_time; | ||
| 1021 | atoms->total_lat += delta; | ||
| 1022 | if (delta > atoms->max_lat) | ||
| 1023 | atoms->max_lat = delta; | ||
| 1024 | atoms->nb_atoms++; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | static void | ||
| 1028 | latency_switch_event(struct trace_switch_event *switch_event, | ||
| 1029 | struct event *event __used, | ||
| 1030 | int cpu, | ||
| 1031 | u64 timestamp, | ||
| 1032 | struct thread *thread __used) | ||
| 1033 | { | ||
| 1034 | struct work_atoms *out_events, *in_events; | ||
| 1035 | struct thread *sched_out, *sched_in; | ||
| 1036 | u64 timestamp0; | ||
| 1037 | s64 delta; | ||
| 1038 | |||
| 1039 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); | ||
| 1040 | |||
| 1041 | timestamp0 = cpu_last_switched[cpu]; | ||
| 1042 | cpu_last_switched[cpu] = timestamp; | ||
| 1043 | if (timestamp0) | ||
| 1044 | delta = timestamp - timestamp0; | ||
| 1045 | else | ||
| 1046 | delta = 0; | ||
| 1047 | |||
| 1048 | if (delta < 0) | ||
| 1049 | die("hm, delta: %Ld < 0 ?\n", delta); | ||
| 1050 | |||
| 1051 | |||
| 1052 | sched_out = threads__findnew(switch_event->prev_pid); | ||
| 1053 | sched_in = threads__findnew(switch_event->next_pid); | ||
| 1054 | |||
| 1055 | out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid); | ||
| 1056 | if (!out_events) { | ||
| 1057 | thread_atoms_insert(sched_out); | ||
| 1058 | out_events = thread_atoms_search(&atom_root, sched_out, &cmp_pid); | ||
| 1059 | if (!out_events) | ||
| 1060 | die("out-event: Internal tree error"); | ||
| 1061 | } | ||
| 1062 | add_sched_out_event(out_events, sched_out_state(switch_event), timestamp); | ||
| 1063 | |||
| 1064 | in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid); | ||
| 1065 | if (!in_events) { | ||
| 1066 | thread_atoms_insert(sched_in); | ||
| 1067 | in_events = thread_atoms_search(&atom_root, sched_in, &cmp_pid); | ||
| 1068 | if (!in_events) | ||
| 1069 | die("in-event: Internal tree error"); | ||
| 1070 | /* | ||
| 1071 | * Take came in we have not heard about yet, | ||
| 1072 | * add in an initial atom in runnable state: | ||
| 1073 | */ | ||
| 1074 | add_sched_out_event(in_events, 'R', timestamp); | ||
| 1075 | } | ||
| 1076 | add_sched_in_event(in_events, timestamp); | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | static void | ||
| 1080 | latency_runtime_event(struct trace_runtime_event *runtime_event, | ||
| 1081 | struct event *event __used, | ||
| 1082 | int cpu, | ||
| 1083 | u64 timestamp, | ||
| 1084 | struct thread *this_thread __used) | ||
| 1085 | { | ||
| 1086 | struct thread *thread = threads__findnew(runtime_event->pid); | ||
| 1087 | struct work_atoms *atoms = thread_atoms_search(&atom_root, thread, &cmp_pid); | ||
| 1088 | |||
| 1089 | BUG_ON(cpu >= MAX_CPUS || cpu < 0); | ||
| 1090 | if (!atoms) { | ||
| 1091 | thread_atoms_insert(thread); | ||
| 1092 | atoms = thread_atoms_search(&atom_root, thread, &cmp_pid); | ||
| 1093 | if (!atoms) | ||
| 1094 | die("in-event: Internal tree error"); | ||
| 1095 | add_sched_out_event(atoms, 'R', timestamp); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | add_runtime_event(atoms, runtime_event->runtime, timestamp); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | static void | ||
| 1102 | latency_wakeup_event(struct trace_wakeup_event *wakeup_event, | ||
| 1103 | struct event *__event __used, | ||
| 1104 | int cpu __used, | ||
| 1105 | u64 timestamp, | ||
| 1106 | struct thread *thread __used) | ||
| 1107 | { | ||
| 1108 | struct work_atoms *atoms; | ||
| 1109 | struct work_atom *atom; | ||
| 1110 | struct thread *wakee; | ||
| 1111 | |||
| 1112 | /* Note for later, it may be interesting to observe the failing cases */ | ||
| 1113 | if (!wakeup_event->success) | ||
| 1114 | return; | ||
| 1115 | |||
| 1116 | wakee = threads__findnew(wakeup_event->pid); | ||
| 1117 | atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid); | ||
| 1118 | if (!atoms) { | ||
| 1119 | thread_atoms_insert(wakee); | ||
| 1120 | atoms = thread_atoms_search(&atom_root, wakee, &cmp_pid); | ||
| 1121 | if (!atoms) | ||
| 1122 | die("wakeup-event: Internal tree error"); | ||
| 1123 | add_sched_out_event(atoms, 'S', timestamp); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | BUG_ON(list_empty(&atoms->work_list)); | ||
| 1127 | |||
| 1128 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | ||
| 1129 | |||
| 1130 | /* | ||
| 1131 | * You WILL be missing events if you've recorded only | ||
| 1132 | * one CPU, or are only looking at only one, so don't | ||
| 1133 | * make useless noise. | ||
| 1134 | */ | ||
| 1135 | if (profile_cpu == -1 && atom->state != THREAD_SLEEPING) | ||
| 1136 | nr_state_machine_bugs++; | ||
| 1137 | |||
| 1138 | nr_timestamps++; | ||
| 1139 | if (atom->sched_out_time > timestamp) { | ||
| 1140 | nr_unordered_timestamps++; | ||
| 1141 | return; | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | atom->state = THREAD_WAIT_CPU; | ||
| 1145 | atom->wake_up_time = timestamp; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | static void | ||
| 1149 | latency_migrate_task_event(struct trace_migrate_task_event *migrate_task_event, | ||
| 1150 | struct event *__event __used, | ||
| 1151 | int cpu __used, | ||
| 1152 | u64 timestamp, | ||
| 1153 | struct thread *thread __used) | ||
| 1154 | { | ||
| 1155 | struct work_atoms *atoms; | ||
| 1156 | struct work_atom *atom; | ||
| 1157 | struct thread *migrant; | ||
| 1158 | |||
| 1159 | /* | ||
| 1160 | * Only need to worry about migration when profiling one CPU. | ||
| 1161 | */ | ||
| 1162 | if (profile_cpu == -1) | ||
| 1163 | return; | ||
| 1164 | |||
| 1165 | migrant = threads__findnew(migrate_task_event->pid); | ||
| 1166 | atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid); | ||
| 1167 | if (!atoms) { | ||
| 1168 | thread_atoms_insert(migrant); | ||
| 1169 | register_pid(migrant->pid, migrant->comm); | ||
| 1170 | atoms = thread_atoms_search(&atom_root, migrant, &cmp_pid); | ||
| 1171 | if (!atoms) | ||
| 1172 | die("migration-event: Internal tree error"); | ||
| 1173 | add_sched_out_event(atoms, 'R', timestamp); | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | BUG_ON(list_empty(&atoms->work_list)); | ||
| 1177 | |||
| 1178 | atom = list_entry(atoms->work_list.prev, struct work_atom, list); | ||
| 1179 | atom->sched_in_time = atom->sched_out_time = atom->wake_up_time = timestamp; | ||
| 1180 | |||
| 1181 | nr_timestamps++; | ||
| 1182 | |||
| 1183 | if (atom->sched_out_time > timestamp) | ||
| 1184 | nr_unordered_timestamps++; | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | static struct trace_sched_handler lat_ops = { | ||
| 1188 | .wakeup_event = latency_wakeup_event, | ||
| 1189 | .switch_event = latency_switch_event, | ||
| 1190 | .runtime_event = latency_runtime_event, | ||
| 1191 | .fork_event = latency_fork_event, | ||
| 1192 | .migrate_task_event = latency_migrate_task_event, | ||
| 1193 | }; | ||
| 1194 | |||
| 1195 | static void output_lat_thread(struct work_atoms *work_list) | ||
| 1196 | { | ||
| 1197 | int i; | ||
| 1198 | int ret; | ||
| 1199 | u64 avg; | ||
| 1200 | |||
| 1201 | if (!work_list->nb_atoms) | ||
| 1202 | return; | ||
| 1203 | /* | ||
| 1204 | * Ignore idle threads: | ||
| 1205 | */ | ||
| 1206 | if (!strcmp(work_list->thread->comm, "swapper")) | ||
| 1207 | return; | ||
| 1208 | |||
| 1209 | all_runtime += work_list->total_runtime; | ||
| 1210 | all_count += work_list->nb_atoms; | ||
| 1211 | |||
| 1212 | ret = printf(" %s:%d ", work_list->thread->comm, work_list->thread->pid); | ||
| 1213 | |||
| 1214 | for (i = 0; i < 24 - ret; i++) | ||
| 1215 | printf(" "); | ||
| 1216 | |||
| 1217 | avg = work_list->total_lat / work_list->nb_atoms; | ||
| 1218 | |||
| 1219 | printf("|%11.3f ms |%9llu | avg:%9.3f ms | max:%9.3f ms |\n", | ||
| 1220 | (double)work_list->total_runtime / 1e6, | ||
| 1221 | work_list->nb_atoms, (double)avg / 1e6, | ||
| 1222 | (double)work_list->max_lat / 1e6); | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | static int pid_cmp(struct work_atoms *l, struct work_atoms *r) | ||
| 1226 | { | ||
| 1227 | if (l->thread->pid < r->thread->pid) | ||
| 1228 | return -1; | ||
| 1229 | if (l->thread->pid > r->thread->pid) | ||
| 1230 | return 1; | ||
| 1231 | |||
| 1232 | return 0; | ||
| 1233 | } | ||
| 1234 | |||
| 1235 | static struct sort_dimension pid_sort_dimension = { | ||
| 1236 | .name = "pid", | ||
| 1237 | .cmp = pid_cmp, | ||
| 1238 | }; | ||
| 1239 | |||
| 1240 | static int avg_cmp(struct work_atoms *l, struct work_atoms *r) | ||
| 1241 | { | ||
| 1242 | u64 avgl, avgr; | ||
| 1243 | |||
| 1244 | if (!l->nb_atoms) | ||
| 1245 | return -1; | ||
| 1246 | |||
| 1247 | if (!r->nb_atoms) | ||
| 1248 | return 1; | ||
| 1249 | |||
| 1250 | avgl = l->total_lat / l->nb_atoms; | ||
| 1251 | avgr = r->total_lat / r->nb_atoms; | ||
| 1252 | |||
| 1253 | if (avgl < avgr) | ||
| 1254 | return -1; | ||
| 1255 | if (avgl > avgr) | ||
| 1256 | return 1; | ||
| 1257 | |||
| 1258 | return 0; | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | static struct sort_dimension avg_sort_dimension = { | ||
| 1262 | .name = "avg", | ||
| 1263 | .cmp = avg_cmp, | ||
| 1264 | }; | ||
| 1265 | |||
| 1266 | static int max_cmp(struct work_atoms *l, struct work_atoms *r) | ||
| 1267 | { | ||
| 1268 | if (l->max_lat < r->max_lat) | ||
| 1269 | return -1; | ||
| 1270 | if (l->max_lat > r->max_lat) | ||
| 1271 | return 1; | ||
| 1272 | |||
| 1273 | return 0; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | static struct sort_dimension max_sort_dimension = { | ||
| 1277 | .name = "max", | ||
| 1278 | .cmp = max_cmp, | ||
| 1279 | }; | ||
| 1280 | |||
| 1281 | static int switch_cmp(struct work_atoms *l, struct work_atoms *r) | ||
| 1282 | { | ||
| 1283 | if (l->nb_atoms < r->nb_atoms) | ||
| 1284 | return -1; | ||
| 1285 | if (l->nb_atoms > r->nb_atoms) | ||
| 1286 | return 1; | ||
| 1287 | |||
| 1288 | return 0; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | static struct sort_dimension switch_sort_dimension = { | ||
| 1292 | .name = "switch", | ||
| 1293 | .cmp = switch_cmp, | ||
| 1294 | }; | ||
| 1295 | |||
| 1296 | static int runtime_cmp(struct work_atoms *l, struct work_atoms *r) | ||
| 1297 | { | ||
| 1298 | if (l->total_runtime < r->total_runtime) | ||
| 1299 | return -1; | ||
| 1300 | if (l->total_runtime > r->total_runtime) | ||
| 1301 | return 1; | ||
| 1302 | |||
| 1303 | return 0; | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | static struct sort_dimension runtime_sort_dimension = { | ||
| 1307 | .name = "runtime", | ||
| 1308 | .cmp = runtime_cmp, | ||
| 1309 | }; | ||
| 1310 | |||
| 1311 | static struct sort_dimension *available_sorts[] = { | ||
| 1312 | &pid_sort_dimension, | ||
| 1313 | &avg_sort_dimension, | ||
| 1314 | &max_sort_dimension, | ||
| 1315 | &switch_sort_dimension, | ||
| 1316 | &runtime_sort_dimension, | ||
| 1317 | }; | ||
| 1318 | |||
| 1319 | #define NB_AVAILABLE_SORTS (int)(sizeof(available_sorts) / sizeof(struct sort_dimension *)) | ||
| 1320 | |||
| 1321 | static LIST_HEAD(sort_list); | ||
| 1322 | |||
| 1323 | static int sort_dimension__add(const char *tok, struct list_head *list) | ||
| 1324 | { | ||
| 1325 | int i; | ||
| 1326 | |||
| 1327 | for (i = 0; i < NB_AVAILABLE_SORTS; i++) { | ||
| 1328 | if (!strcmp(available_sorts[i]->name, tok)) { | ||
| 1329 | list_add_tail(&available_sorts[i]->list, list); | ||
| 1330 | |||
| 1331 | return 0; | ||
| 1332 | } | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | return -1; | ||
| 1336 | } | ||
| 1337 | |||
| 1338 | static void setup_sorting(void); | ||
| 1339 | |||
| 1340 | static void sort_lat(void) | ||
| 1341 | { | ||
| 1342 | struct rb_node *node; | ||
| 1343 | |||
| 1344 | for (;;) { | ||
| 1345 | struct work_atoms *data; | ||
| 1346 | node = rb_first(&atom_root); | ||
| 1347 | if (!node) | ||
| 1348 | break; | ||
| 1349 | |||
| 1350 | rb_erase(node, &atom_root); | ||
| 1351 | data = rb_entry(node, struct work_atoms, node); | ||
| 1352 | __thread_latency_insert(&sorted_atom_root, data, &sort_list); | ||
| 1353 | } | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | static struct trace_sched_handler *trace_handler; | ||
| 1357 | |||
| 1358 | static void | ||
| 1359 | process_sched_wakeup_event(struct raw_event_sample *raw, | ||
| 1360 | struct event *event, | ||
| 1361 | int cpu __used, | ||
| 1362 | u64 timestamp __used, | ||
| 1363 | struct thread *thread __used) | ||
| 1364 | { | ||
| 1365 | struct trace_wakeup_event wakeup_event; | ||
| 1366 | |||
| 1367 | FILL_COMMON_FIELDS(wakeup_event, event, raw->data); | ||
| 1368 | |||
| 1369 | FILL_ARRAY(wakeup_event, comm, event, raw->data); | ||
| 1370 | FILL_FIELD(wakeup_event, pid, event, raw->data); | ||
| 1371 | FILL_FIELD(wakeup_event, prio, event, raw->data); | ||
| 1372 | FILL_FIELD(wakeup_event, success, event, raw->data); | ||
| 1373 | FILL_FIELD(wakeup_event, cpu, event, raw->data); | ||
| 1374 | |||
| 1375 | if (trace_handler->wakeup_event) | ||
| 1376 | trace_handler->wakeup_event(&wakeup_event, event, cpu, timestamp, thread); | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | /* | ||
| 1380 | * Track the current task - that way we can know whether there's any | ||
| 1381 | * weird events, such as a task being switched away that is not current. | ||
| 1382 | */ | ||
| 1383 | static int max_cpu; | ||
| 1384 | |||
| 1385 | static u32 curr_pid[MAX_CPUS] = { [0 ... MAX_CPUS-1] = -1 }; | ||
| 1386 | |||
| 1387 | static struct thread *curr_thread[MAX_CPUS]; | ||
| 1388 | |||
| 1389 | static char next_shortname1 = 'A'; | ||
| 1390 | static char next_shortname2 = '0'; | ||
| 1391 | |||
| 1392 | static void | ||
| 1393 | map_switch_event(struct trace_switch_event *switch_event, | ||
| 1394 | struct event *event __used, | ||
| 1395 | int this_cpu, | ||
| 1396 | u64 timestamp, | ||
| 1397 | struct thread *thread __used) | ||
| 1398 | { | ||
| 1399 | struct thread *sched_out, *sched_in; | ||
| 1400 | int new_shortname; | ||
| 1401 | u64 timestamp0; | ||
| 1402 | s64 delta; | ||
| 1403 | int cpu; | ||
| 1404 | |||
| 1405 | BUG_ON(this_cpu >= MAX_CPUS || this_cpu < 0); | ||
| 1406 | |||
| 1407 | if (this_cpu > max_cpu) | ||
| 1408 | max_cpu = this_cpu; | ||
| 1409 | |||
| 1410 | timestamp0 = cpu_last_switched[this_cpu]; | ||
| 1411 | cpu_last_switched[this_cpu] = timestamp; | ||
| 1412 | if (timestamp0) | ||
| 1413 | delta = timestamp - timestamp0; | ||
| 1414 | else | ||
| 1415 | delta = 0; | ||
| 1416 | |||
| 1417 | if (delta < 0) | ||
| 1418 | die("hm, delta: %Ld < 0 ?\n", delta); | ||
| 1419 | |||
| 1420 | |||
| 1421 | sched_out = threads__findnew(switch_event->prev_pid); | ||
| 1422 | sched_in = threads__findnew(switch_event->next_pid); | ||
| 1423 | |||
| 1424 | curr_thread[this_cpu] = sched_in; | ||
| 1425 | |||
| 1426 | printf(" "); | ||
| 1427 | |||
| 1428 | new_shortname = 0; | ||
| 1429 | if (!sched_in->shortname[0]) { | ||
| 1430 | sched_in->shortname[0] = next_shortname1; | ||
| 1431 | sched_in->shortname[1] = next_shortname2; | ||
| 1432 | |||
| 1433 | if (next_shortname1 < 'Z') { | ||
| 1434 | next_shortname1++; | ||
| 1435 | } else { | ||
| 1436 | next_shortname1='A'; | ||
| 1437 | if (next_shortname2 < '9') { | ||
| 1438 | next_shortname2++; | ||
| 1439 | } else { | ||
| 1440 | next_shortname2='0'; | ||
| 1441 | } | ||
| 1442 | } | ||
| 1443 | new_shortname = 1; | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | for (cpu = 0; cpu <= max_cpu; cpu++) { | ||
| 1447 | if (cpu != this_cpu) | ||
| 1448 | printf(" "); | ||
| 1449 | else | ||
| 1450 | printf("*"); | ||
| 1451 | |||
| 1452 | if (curr_thread[cpu]) { | ||
| 1453 | if (curr_thread[cpu]->pid) | ||
| 1454 | printf("%2s ", curr_thread[cpu]->shortname); | ||
| 1455 | else | ||
| 1456 | printf(". "); | ||
| 1457 | } else | ||
| 1458 | printf(" "); | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | printf(" %12.6f secs ", (double)timestamp/1e9); | ||
| 1462 | if (new_shortname) { | ||
| 1463 | printf("%s => %s:%d\n", | ||
| 1464 | sched_in->shortname, sched_in->comm, sched_in->pid); | ||
| 1465 | } else { | ||
| 1466 | printf("\n"); | ||
| 1467 | } | ||
| 1468 | } | ||
| 1469 | |||
| 1470 | |||
| 1471 | static void | ||
| 1472 | process_sched_switch_event(struct raw_event_sample *raw, | ||
| 1473 | struct event *event, | ||
| 1474 | int this_cpu, | ||
| 1475 | u64 timestamp __used, | ||
| 1476 | struct thread *thread __used) | ||
| 1477 | { | ||
| 1478 | struct trace_switch_event switch_event; | ||
| 1479 | |||
| 1480 | FILL_COMMON_FIELDS(switch_event, event, raw->data); | ||
| 1481 | |||
| 1482 | FILL_ARRAY(switch_event, prev_comm, event, raw->data); | ||
| 1483 | FILL_FIELD(switch_event, prev_pid, event, raw->data); | ||
| 1484 | FILL_FIELD(switch_event, prev_prio, event, raw->data); | ||
| 1485 | FILL_FIELD(switch_event, prev_state, event, raw->data); | ||
| 1486 | FILL_ARRAY(switch_event, next_comm, event, raw->data); | ||
| 1487 | FILL_FIELD(switch_event, next_pid, event, raw->data); | ||
| 1488 | FILL_FIELD(switch_event, next_prio, event, raw->data); | ||
| 1489 | |||
| 1490 | if (curr_pid[this_cpu] != (u32)-1) { | ||
| 1491 | /* | ||
| 1492 | * Are we trying to switch away a PID that is | ||
| 1493 | * not current? | ||
| 1494 | */ | ||
| 1495 | if (curr_pid[this_cpu] != switch_event.prev_pid) | ||
| 1496 | nr_context_switch_bugs++; | ||
| 1497 | } | ||
| 1498 | if (trace_handler->switch_event) | ||
| 1499 | trace_handler->switch_event(&switch_event, event, this_cpu, timestamp, thread); | ||
| 1500 | |||
| 1501 | curr_pid[this_cpu] = switch_event.next_pid; | ||
| 1502 | } | ||
| 1503 | |||
| 1504 | static void | ||
| 1505 | process_sched_runtime_event(struct raw_event_sample *raw, | ||
| 1506 | struct event *event, | ||
| 1507 | int cpu __used, | ||
| 1508 | u64 timestamp __used, | ||
| 1509 | struct thread *thread __used) | ||
| 1510 | { | ||
| 1511 | struct trace_runtime_event runtime_event; | ||
| 1512 | |||
| 1513 | FILL_ARRAY(runtime_event, comm, event, raw->data); | ||
| 1514 | FILL_FIELD(runtime_event, pid, event, raw->data); | ||
| 1515 | FILL_FIELD(runtime_event, runtime, event, raw->data); | ||
| 1516 | FILL_FIELD(runtime_event, vruntime, event, raw->data); | ||
| 1517 | |||
| 1518 | if (trace_handler->runtime_event) | ||
| 1519 | trace_handler->runtime_event(&runtime_event, event, cpu, timestamp, thread); | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | static void | ||
| 1523 | process_sched_fork_event(struct raw_event_sample *raw, | ||
| 1524 | struct event *event, | ||
| 1525 | int cpu __used, | ||
| 1526 | u64 timestamp __used, | ||
| 1527 | struct thread *thread __used) | ||
| 1528 | { | ||
| 1529 | struct trace_fork_event fork_event; | ||
| 1530 | |||
| 1531 | FILL_COMMON_FIELDS(fork_event, event, raw->data); | ||
| 1532 | |||
| 1533 | FILL_ARRAY(fork_event, parent_comm, event, raw->data); | ||
| 1534 | FILL_FIELD(fork_event, parent_pid, event, raw->data); | ||
| 1535 | FILL_ARRAY(fork_event, child_comm, event, raw->data); | ||
| 1536 | FILL_FIELD(fork_event, child_pid, event, raw->data); | ||
| 1537 | |||
| 1538 | if (trace_handler->fork_event) | ||
| 1539 | trace_handler->fork_event(&fork_event, event, cpu, timestamp, thread); | ||
| 1540 | } | ||
| 1541 | |||
| 1542 | static void | ||
| 1543 | process_sched_exit_event(struct event *event, | ||
| 1544 | int cpu __used, | ||
| 1545 | u64 timestamp __used, | ||
| 1546 | struct thread *thread __used) | ||
| 1547 | { | ||
| 1548 | if (verbose) | ||
| 1549 | printf("sched_exit event %p\n", event); | ||
| 1550 | } | ||
| 1551 | |||
| 1552 | static void | ||
| 1553 | process_sched_migrate_task_event(struct raw_event_sample *raw, | ||
| 1554 | struct event *event, | ||
| 1555 | int cpu __used, | ||
| 1556 | u64 timestamp __used, | ||
| 1557 | struct thread *thread __used) | ||
| 1558 | { | ||
| 1559 | struct trace_migrate_task_event migrate_task_event; | ||
| 1560 | |||
| 1561 | FILL_COMMON_FIELDS(migrate_task_event, event, raw->data); | ||
| 1562 | |||
| 1563 | FILL_ARRAY(migrate_task_event, comm, event, raw->data); | ||
| 1564 | FILL_FIELD(migrate_task_event, pid, event, raw->data); | ||
| 1565 | FILL_FIELD(migrate_task_event, prio, event, raw->data); | ||
| 1566 | FILL_FIELD(migrate_task_event, cpu, event, raw->data); | ||
| 1567 | |||
| 1568 | if (trace_handler->migrate_task_event) | ||
| 1569 | trace_handler->migrate_task_event(&migrate_task_event, event, cpu, timestamp, thread); | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | static void | ||
| 1573 | process_raw_event(event_t *raw_event __used, void *more_data, | ||
| 1574 | int cpu, u64 timestamp, struct thread *thread) | ||
| 1575 | { | ||
| 1576 | struct raw_event_sample *raw = more_data; | ||
| 1577 | struct event *event; | ||
| 1578 | int type; | ||
| 1579 | |||
| 1580 | type = trace_parse_common_type(raw->data); | ||
| 1581 | event = trace_find_event(type); | ||
| 1582 | |||
| 1583 | if (!strcmp(event->name, "sched_switch")) | ||
| 1584 | process_sched_switch_event(raw, event, cpu, timestamp, thread); | ||
| 1585 | if (!strcmp(event->name, "sched_stat_runtime")) | ||
| 1586 | process_sched_runtime_event(raw, event, cpu, timestamp, thread); | ||
| 1587 | if (!strcmp(event->name, "sched_wakeup")) | ||
| 1588 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); | ||
| 1589 | if (!strcmp(event->name, "sched_wakeup_new")) | ||
| 1590 | process_sched_wakeup_event(raw, event, cpu, timestamp, thread); | ||
| 1591 | if (!strcmp(event->name, "sched_process_fork")) | ||
| 1592 | process_sched_fork_event(raw, event, cpu, timestamp, thread); | ||
| 1593 | if (!strcmp(event->name, "sched_process_exit")) | ||
| 1594 | process_sched_exit_event(event, cpu, timestamp, thread); | ||
| 1595 | if (!strcmp(event->name, "sched_migrate_task")) | ||
| 1596 | process_sched_migrate_task_event(raw, event, cpu, timestamp, thread); | ||
| 1597 | } | ||
| 1598 | |||
| 1599 | static int process_sample_event(event_t *event) | ||
| 1600 | { | ||
| 1601 | struct thread *thread; | ||
| 1602 | u64 ip = event->ip.ip; | ||
| 1603 | u64 timestamp = -1; | ||
| 1604 | u32 cpu = -1; | ||
| 1605 | u64 period = 1; | ||
| 1606 | void *more_data = event->ip.__more_data; | ||
| 1607 | |||
| 1608 | if (!(sample_type & PERF_SAMPLE_RAW)) | ||
| 1609 | return 0; | ||
| 1610 | |||
| 1611 | thread = threads__findnew(event->ip.pid); | ||
| 1612 | |||
| 1613 | if (sample_type & PERF_SAMPLE_TIME) { | ||
| 1614 | timestamp = *(u64 *)more_data; | ||
| 1615 | more_data += sizeof(u64); | ||
| 1616 | } | ||
| 1617 | |||
| 1618 | if (sample_type & PERF_SAMPLE_CPU) { | ||
| 1619 | cpu = *(u32 *)more_data; | ||
| 1620 | more_data += sizeof(u32); | ||
| 1621 | more_data += sizeof(u32); /* reserved */ | ||
| 1622 | } | ||
| 1623 | |||
| 1624 | if (sample_type & PERF_SAMPLE_PERIOD) { | ||
| 1625 | period = *(u64 *)more_data; | ||
| 1626 | more_data += sizeof(u64); | ||
| 1627 | } | ||
| 1628 | |||
| 1629 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", | ||
| 1630 | event->header.misc, | ||
| 1631 | event->ip.pid, event->ip.tid, | ||
| 1632 | (void *)(long)ip, | ||
| 1633 | (long long)period); | ||
| 1634 | |||
| 1635 | if (thread == NULL) { | ||
| 1636 | pr_debug("problem processing %d event, skipping it.\n", | ||
| 1637 | event->header.type); | ||
| 1638 | return -1; | ||
| 1639 | } | ||
| 1640 | |||
| 1641 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 1642 | |||
| 1643 | if (profile_cpu != -1 && profile_cpu != (int) cpu) | ||
| 1644 | return 0; | ||
| 1645 | |||
| 1646 | process_raw_event(event, more_data, cpu, timestamp, thread); | ||
| 1647 | |||
| 1648 | return 0; | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | static int process_lost_event(event_t *event __used) | ||
| 1652 | { | ||
| 1653 | nr_lost_chunks++; | ||
| 1654 | nr_lost_events += event->lost.lost; | ||
| 1655 | |||
| 1656 | return 0; | ||
| 1657 | } | ||
| 1658 | |||
| 1659 | static int sample_type_check(u64 type) | ||
| 1660 | { | ||
| 1661 | sample_type = type; | ||
| 1662 | |||
| 1663 | if (!(sample_type & PERF_SAMPLE_RAW)) { | ||
| 1664 | fprintf(stderr, | ||
| 1665 | "No trace sample to read. Did you call perf record " | ||
| 1666 | "without -R?"); | ||
| 1667 | return -1; | ||
| 1668 | } | ||
| 1669 | |||
| 1670 | return 0; | ||
| 1671 | } | ||
| 1672 | |||
| 1673 | static struct perf_file_handler file_handler = { | ||
| 1674 | .process_sample_event = process_sample_event, | ||
| 1675 | .process_comm_event = event__process_comm, | ||
| 1676 | .process_lost_event = process_lost_event, | ||
| 1677 | .sample_type_check = sample_type_check, | ||
| 1678 | }; | ||
| 1679 | |||
| 1680 | static int read_events(void) | ||
| 1681 | { | ||
| 1682 | register_idle_thread(); | ||
| 1683 | register_perf_file_handler(&file_handler); | ||
| 1684 | |||
| 1685 | return mmap_dispatch_perf_file(&header, input_name, 0, 0, | ||
| 1686 | &event__cwdlen, &event__cwd); | ||
| 1687 | } | ||
| 1688 | |||
| 1689 | static void print_bad_events(void) | ||
| 1690 | { | ||
| 1691 | if (nr_unordered_timestamps && nr_timestamps) { | ||
| 1692 | printf(" INFO: %.3f%% unordered timestamps (%ld out of %ld)\n", | ||
| 1693 | (double)nr_unordered_timestamps/(double)nr_timestamps*100.0, | ||
| 1694 | nr_unordered_timestamps, nr_timestamps); | ||
| 1695 | } | ||
| 1696 | if (nr_lost_events && nr_events) { | ||
| 1697 | printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n", | ||
| 1698 | (double)nr_lost_events/(double)nr_events*100.0, | ||
| 1699 | nr_lost_events, nr_events, nr_lost_chunks); | ||
| 1700 | } | ||
| 1701 | if (nr_state_machine_bugs && nr_timestamps) { | ||
| 1702 | printf(" INFO: %.3f%% state machine bugs (%ld out of %ld)", | ||
| 1703 | (double)nr_state_machine_bugs/(double)nr_timestamps*100.0, | ||
| 1704 | nr_state_machine_bugs, nr_timestamps); | ||
| 1705 | if (nr_lost_events) | ||
| 1706 | printf(" (due to lost events?)"); | ||
| 1707 | printf("\n"); | ||
| 1708 | } | ||
| 1709 | if (nr_context_switch_bugs && nr_timestamps) { | ||
| 1710 | printf(" INFO: %.3f%% context switch bugs (%ld out of %ld)", | ||
| 1711 | (double)nr_context_switch_bugs/(double)nr_timestamps*100.0, | ||
| 1712 | nr_context_switch_bugs, nr_timestamps); | ||
| 1713 | if (nr_lost_events) | ||
| 1714 | printf(" (due to lost events?)"); | ||
| 1715 | printf("\n"); | ||
| 1716 | } | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | static void __cmd_lat(void) | ||
| 1720 | { | ||
| 1721 | struct rb_node *next; | ||
| 1722 | |||
| 1723 | setup_pager(); | ||
| 1724 | read_events(); | ||
| 1725 | sort_lat(); | ||
| 1726 | |||
| 1727 | printf("\n -----------------------------------------------------------------------------------------\n"); | ||
| 1728 | printf(" Task | Runtime ms | Switches | Average delay ms | Maximum delay ms |\n"); | ||
| 1729 | printf(" -----------------------------------------------------------------------------------------\n"); | ||
| 1730 | |||
| 1731 | next = rb_first(&sorted_atom_root); | ||
| 1732 | |||
| 1733 | while (next) { | ||
| 1734 | struct work_atoms *work_list; | ||
| 1735 | |||
| 1736 | work_list = rb_entry(next, struct work_atoms, node); | ||
| 1737 | output_lat_thread(work_list); | ||
| 1738 | next = rb_next(next); | ||
| 1739 | } | ||
| 1740 | |||
| 1741 | printf(" -----------------------------------------------------------------------------------------\n"); | ||
| 1742 | printf(" TOTAL: |%11.3f ms |%9Ld |\n", | ||
| 1743 | (double)all_runtime/1e6, all_count); | ||
| 1744 | |||
| 1745 | printf(" ---------------------------------------------------\n"); | ||
| 1746 | |||
| 1747 | print_bad_events(); | ||
| 1748 | printf("\n"); | ||
| 1749 | |||
| 1750 | } | ||
| 1751 | |||
| 1752 | static struct trace_sched_handler map_ops = { | ||
| 1753 | .wakeup_event = NULL, | ||
| 1754 | .switch_event = map_switch_event, | ||
| 1755 | .runtime_event = NULL, | ||
| 1756 | .fork_event = NULL, | ||
| 1757 | }; | ||
| 1758 | |||
| 1759 | static void __cmd_map(void) | ||
| 1760 | { | ||
| 1761 | max_cpu = sysconf(_SC_NPROCESSORS_CONF); | ||
| 1762 | |||
| 1763 | setup_pager(); | ||
| 1764 | read_events(); | ||
| 1765 | print_bad_events(); | ||
| 1766 | } | ||
| 1767 | |||
| 1768 | static void __cmd_replay(void) | ||
| 1769 | { | ||
| 1770 | unsigned long i; | ||
| 1771 | |||
| 1772 | calibrate_run_measurement_overhead(); | ||
| 1773 | calibrate_sleep_measurement_overhead(); | ||
| 1774 | |||
| 1775 | test_calibrations(); | ||
| 1776 | |||
| 1777 | read_events(); | ||
| 1778 | |||
| 1779 | printf("nr_run_events: %ld\n", nr_run_events); | ||
| 1780 | printf("nr_sleep_events: %ld\n", nr_sleep_events); | ||
| 1781 | printf("nr_wakeup_events: %ld\n", nr_wakeup_events); | ||
| 1782 | |||
| 1783 | if (targetless_wakeups) | ||
| 1784 | printf("target-less wakeups: %ld\n", targetless_wakeups); | ||
| 1785 | if (multitarget_wakeups) | ||
| 1786 | printf("multi-target wakeups: %ld\n", multitarget_wakeups); | ||
| 1787 | if (nr_run_events_optimized) | ||
| 1788 | printf("run atoms optimized: %ld\n", | ||
| 1789 | nr_run_events_optimized); | ||
| 1790 | |||
| 1791 | print_task_traces(); | ||
| 1792 | add_cross_task_wakeups(); | ||
| 1793 | |||
| 1794 | create_tasks(); | ||
| 1795 | printf("------------------------------------------------------------\n"); | ||
| 1796 | for (i = 0; i < replay_repeat; i++) | ||
| 1797 | run_one_test(); | ||
| 1798 | } | ||
| 1799 | |||
| 1800 | |||
| 1801 | static const char * const sched_usage[] = { | ||
| 1802 | "perf sched [<options>] {record|latency|map|replay|trace}", | ||
| 1803 | NULL | ||
| 1804 | }; | ||
| 1805 | |||
| 1806 | static const struct option sched_options[] = { | ||
| 1807 | OPT_STRING('i', "input", &input_name, "file", | ||
| 1808 | "input file name"), | ||
| 1809 | OPT_BOOLEAN('v', "verbose", &verbose, | ||
| 1810 | "be more verbose (show symbol address, etc)"), | ||
| 1811 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | ||
| 1812 | "dump raw trace in ASCII"), | ||
| 1813 | OPT_END() | ||
| 1814 | }; | ||
| 1815 | |||
| 1816 | static const char * const latency_usage[] = { | ||
| 1817 | "perf sched latency [<options>]", | ||
| 1818 | NULL | ||
| 1819 | }; | ||
| 1820 | |||
| 1821 | static const struct option latency_options[] = { | ||
| 1822 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", | ||
| 1823 | "sort by key(s): runtime, switch, avg, max"), | ||
| 1824 | OPT_BOOLEAN('v', "verbose", &verbose, | ||
| 1825 | "be more verbose (show symbol address, etc)"), | ||
| 1826 | OPT_INTEGER('C', "CPU", &profile_cpu, | ||
| 1827 | "CPU to profile on"), | ||
| 1828 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | ||
| 1829 | "dump raw trace in ASCII"), | ||
| 1830 | OPT_END() | ||
| 1831 | }; | ||
| 1832 | |||
| 1833 | static const char * const replay_usage[] = { | ||
| 1834 | "perf sched replay [<options>]", | ||
| 1835 | NULL | ||
| 1836 | }; | ||
| 1837 | |||
| 1838 | static const struct option replay_options[] = { | ||
| 1839 | OPT_INTEGER('r', "repeat", &replay_repeat, | ||
| 1840 | "repeat the workload replay N times (-1: infinite)"), | ||
| 1841 | OPT_BOOLEAN('v', "verbose", &verbose, | ||
| 1842 | "be more verbose (show symbol address, etc)"), | ||
| 1843 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | ||
| 1844 | "dump raw trace in ASCII"), | ||
| 1845 | OPT_END() | ||
| 1846 | }; | ||
| 1847 | |||
| 1848 | static void setup_sorting(void) | ||
| 1849 | { | ||
| 1850 | char *tmp, *tok, *str = strdup(sort_order); | ||
| 1851 | |||
| 1852 | for (tok = strtok_r(str, ", ", &tmp); | ||
| 1853 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | ||
| 1854 | if (sort_dimension__add(tok, &sort_list) < 0) { | ||
| 1855 | error("Unknown --sort key: `%s'", tok); | ||
| 1856 | usage_with_options(latency_usage, latency_options); | ||
| 1857 | } | ||
| 1858 | } | ||
| 1859 | |||
| 1860 | free(str); | ||
| 1861 | |||
| 1862 | sort_dimension__add("pid", &cmp_pid); | ||
| 1863 | } | ||
| 1864 | |||
| 1865 | static const char *record_args[] = { | ||
| 1866 | "record", | ||
| 1867 | "-a", | ||
| 1868 | "-R", | ||
| 1869 | "-M", | ||
| 1870 | "-f", | ||
| 1871 | "-m", "1024", | ||
| 1872 | "-c", "1", | ||
| 1873 | "-e", "sched:sched_switch:r", | ||
| 1874 | "-e", "sched:sched_stat_wait:r", | ||
| 1875 | "-e", "sched:sched_stat_sleep:r", | ||
| 1876 | "-e", "sched:sched_stat_iowait:r", | ||
| 1877 | "-e", "sched:sched_stat_runtime:r", | ||
| 1878 | "-e", "sched:sched_process_exit:r", | ||
| 1879 | "-e", "sched:sched_process_fork:r", | ||
| 1880 | "-e", "sched:sched_wakeup:r", | ||
| 1881 | "-e", "sched:sched_migrate_task:r", | ||
| 1882 | }; | ||
| 1883 | |||
| 1884 | static int __cmd_record(int argc, const char **argv) | ||
| 1885 | { | ||
| 1886 | unsigned int rec_argc, i, j; | ||
| 1887 | const char **rec_argv; | ||
| 1888 | |||
| 1889 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | ||
| 1890 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | ||
| 1891 | |||
| 1892 | for (i = 0; i < ARRAY_SIZE(record_args); i++) | ||
| 1893 | rec_argv[i] = strdup(record_args[i]); | ||
| 1894 | |||
| 1895 | for (j = 1; j < (unsigned int)argc; j++, i++) | ||
| 1896 | rec_argv[i] = argv[j]; | ||
| 1897 | |||
| 1898 | BUG_ON(i != rec_argc); | ||
| 1899 | |||
| 1900 | return cmd_record(i, rec_argv, NULL); | ||
| 1901 | } | ||
| 1902 | |||
| 1903 | int cmd_sched(int argc, const char **argv, const char *prefix __used) | ||
| 1904 | { | ||
| 1905 | symbol__init(0); | ||
| 1906 | |||
| 1907 | argc = parse_options(argc, argv, sched_options, sched_usage, | ||
| 1908 | PARSE_OPT_STOP_AT_NON_OPTION); | ||
| 1909 | if (!argc) | ||
| 1910 | usage_with_options(sched_usage, sched_options); | ||
| 1911 | |||
| 1912 | if (!strncmp(argv[0], "rec", 3)) { | ||
| 1913 | return __cmd_record(argc, argv); | ||
| 1914 | } else if (!strncmp(argv[0], "lat", 3)) { | ||
| 1915 | trace_handler = &lat_ops; | ||
| 1916 | if (argc > 1) { | ||
| 1917 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); | ||
| 1918 | if (argc) | ||
| 1919 | usage_with_options(latency_usage, latency_options); | ||
| 1920 | } | ||
| 1921 | setup_sorting(); | ||
| 1922 | __cmd_lat(); | ||
| 1923 | } else if (!strcmp(argv[0], "map")) { | ||
| 1924 | trace_handler = &map_ops; | ||
| 1925 | setup_sorting(); | ||
| 1926 | __cmd_map(); | ||
| 1927 | } else if (!strncmp(argv[0], "rep", 3)) { | ||
| 1928 | trace_handler = &replay_ops; | ||
| 1929 | if (argc) { | ||
| 1930 | argc = parse_options(argc, argv, replay_options, replay_usage, 0); | ||
| 1931 | if (argc) | ||
| 1932 | usage_with_options(replay_usage, replay_options); | ||
| 1933 | } | ||
| 1934 | __cmd_replay(); | ||
| 1935 | } else if (!strcmp(argv[0], "trace")) { | ||
| 1936 | /* | ||
| 1937 | * Aliased to 'perf trace' for now: | ||
| 1938 | */ | ||
| 1939 | return cmd_trace(argc, argv, prefix); | ||
| 1940 | } else { | ||
| 1941 | usage_with_options(sched_usage, sched_options); | ||
| 1942 | } | ||
| 1943 | |||
| 1944 | return 0; | ||
| 1945 | } | ||
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 61b828236c11..c70d72003557 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c | |||
| @@ -48,17 +48,19 @@ | |||
| 48 | #include <sys/prctl.h> | 48 | #include <sys/prctl.h> |
| 49 | #include <math.h> | 49 | #include <math.h> |
| 50 | 50 | ||
| 51 | static struct perf_counter_attr default_attrs[] = { | 51 | static struct perf_event_attr default_attrs[] = { |
| 52 | 52 | ||
| 53 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, | 53 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, |
| 54 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES}, | 54 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, |
| 55 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | 55 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, |
| 56 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | 56 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, |
| 57 | 57 | ||
| 58 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, | 58 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, |
| 59 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, | 59 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, |
| 60 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES}, | 60 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
| 61 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES }, | 61 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, |
| 62 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES }, | ||
| 63 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES }, | ||
| 62 | 64 | ||
| 63 | }; | 65 | }; |
| 64 | 66 | ||
| @@ -69,7 +71,8 @@ static int run_idx = 0; | |||
| 69 | static int run_count = 1; | 71 | static int run_count = 1; |
| 70 | static int inherit = 1; | 72 | static int inherit = 1; |
| 71 | static int scale = 1; | 73 | static int scale = 1; |
| 72 | static int target_pid = -1; | 74 | static pid_t target_pid = -1; |
| 75 | static pid_t child_pid = -1; | ||
| 73 | static int null_run = 0; | 76 | static int null_run = 0; |
| 74 | 77 | ||
| 75 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; | 78 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| @@ -124,17 +127,18 @@ struct stats event_res_stats[MAX_COUNTERS][3]; | |||
| 124 | struct stats runtime_nsecs_stats; | 127 | struct stats runtime_nsecs_stats; |
| 125 | struct stats walltime_nsecs_stats; | 128 | struct stats walltime_nsecs_stats; |
| 126 | struct stats runtime_cycles_stats; | 129 | struct stats runtime_cycles_stats; |
| 130 | struct stats runtime_branches_stats; | ||
| 127 | 131 | ||
| 128 | #define MATCH_EVENT(t, c, counter) \ | 132 | #define MATCH_EVENT(t, c, counter) \ |
| 129 | (attrs[counter].type == PERF_TYPE_##t && \ | 133 | (attrs[counter].type == PERF_TYPE_##t && \ |
| 130 | attrs[counter].config == PERF_COUNT_##c) | 134 | attrs[counter].config == PERF_COUNT_##c) |
| 131 | 135 | ||
| 132 | #define ERR_PERF_OPEN \ | 136 | #define ERR_PERF_OPEN \ |
| 133 | "Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n" | 137 | "Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n" |
| 134 | 138 | ||
| 135 | static void create_perf_stat_counter(int counter, int pid) | 139 | static void create_perf_stat_counter(int counter, int pid) |
| 136 | { | 140 | { |
| 137 | struct perf_counter_attr *attr = attrs + counter; | 141 | struct perf_event_attr *attr = attrs + counter; |
| 138 | 142 | ||
| 139 | if (scale) | 143 | if (scale) |
| 140 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | | 144 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| @@ -144,7 +148,7 @@ static void create_perf_stat_counter(int counter, int pid) | |||
| 144 | unsigned int cpu; | 148 | unsigned int cpu; |
| 145 | 149 | ||
| 146 | for (cpu = 0; cpu < nr_cpus; cpu++) { | 150 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 147 | fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0); | 151 | fd[cpu][counter] = sys_perf_event_open(attr, -1, cpu, -1, 0); |
| 148 | if (fd[cpu][counter] < 0 && verbose) | 152 | if (fd[cpu][counter] < 0 && verbose) |
| 149 | fprintf(stderr, ERR_PERF_OPEN, counter, | 153 | fprintf(stderr, ERR_PERF_OPEN, counter, |
| 150 | fd[cpu][counter], strerror(errno)); | 154 | fd[cpu][counter], strerror(errno)); |
| @@ -154,7 +158,7 @@ static void create_perf_stat_counter(int counter, int pid) | |||
| 154 | attr->disabled = 1; | 158 | attr->disabled = 1; |
| 155 | attr->enable_on_exec = 1; | 159 | attr->enable_on_exec = 1; |
| 156 | 160 | ||
| 157 | fd[0][counter] = sys_perf_counter_open(attr, pid, -1, -1, 0); | 161 | fd[0][counter] = sys_perf_event_open(attr, pid, -1, -1, 0); |
| 158 | if (fd[0][counter] < 0 && verbose) | 162 | if (fd[0][counter] < 0 && verbose) |
| 159 | fprintf(stderr, ERR_PERF_OPEN, counter, | 163 | fprintf(stderr, ERR_PERF_OPEN, counter, |
| 160 | fd[0][counter], strerror(errno)); | 164 | fd[0][counter], strerror(errno)); |
| @@ -234,6 +238,8 @@ static void read_counter(int counter) | |||
| 234 | update_stats(&runtime_nsecs_stats, count[0]); | 238 | update_stats(&runtime_nsecs_stats, count[0]); |
| 235 | if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) | 239 | if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) |
| 236 | update_stats(&runtime_cycles_stats, count[0]); | 240 | update_stats(&runtime_cycles_stats, count[0]); |
| 241 | if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter)) | ||
| 242 | update_stats(&runtime_branches_stats, count[0]); | ||
| 237 | } | 243 | } |
| 238 | 244 | ||
| 239 | static int run_perf_stat(int argc __used, const char **argv) | 245 | static int run_perf_stat(int argc __used, const char **argv) |
| @@ -285,6 +291,8 @@ static int run_perf_stat(int argc __used, const char **argv) | |||
| 285 | exit(-1); | 291 | exit(-1); |
| 286 | } | 292 | } |
| 287 | 293 | ||
| 294 | child_pid = pid; | ||
| 295 | |||
| 288 | /* | 296 | /* |
| 289 | * Wait for the child to be ready to exec. | 297 | * Wait for the child to be ready to exec. |
| 290 | */ | 298 | */ |
| @@ -338,14 +346,33 @@ static void nsec_printout(int counter, double avg) | |||
| 338 | 346 | ||
| 339 | static void abs_printout(int counter, double avg) | 347 | static void abs_printout(int counter, double avg) |
| 340 | { | 348 | { |
| 349 | double total, ratio = 0.0; | ||
| 350 | |||
| 341 | fprintf(stderr, " %14.0f %-24s", avg, event_name(counter)); | 351 | fprintf(stderr, " %14.0f %-24s", avg, event_name(counter)); |
| 342 | 352 | ||
| 343 | if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) { | 353 | if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) { |
| 344 | fprintf(stderr, " # %10.3f IPC ", | 354 | total = avg_stats(&runtime_cycles_stats); |
| 345 | avg / avg_stats(&runtime_cycles_stats)); | 355 | |
| 346 | } else { | 356 | if (total) |
| 347 | fprintf(stderr, " # %10.3f M/sec", | 357 | ratio = avg / total; |
| 348 | 1000.0 * avg / avg_stats(&runtime_nsecs_stats)); | 358 | |
| 359 | fprintf(stderr, " # %10.3f IPC ", ratio); | ||
| 360 | } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) && | ||
| 361 | runtime_branches_stats.n != 0) { | ||
| 362 | total = avg_stats(&runtime_branches_stats); | ||
| 363 | |||
| 364 | if (total) | ||
| 365 | ratio = avg * 100 / total; | ||
| 366 | |||
| 367 | fprintf(stderr, " # %10.3f %% ", ratio); | ||
| 368 | |||
| 369 | } else if (runtime_nsecs_stats.n != 0) { | ||
| 370 | total = avg_stats(&runtime_nsecs_stats); | ||
| 371 | |||
| 372 | if (total) | ||
| 373 | ratio = 1000.0 * avg / total; | ||
| 374 | |||
| 375 | fprintf(stderr, " # %10.3f M/sec", ratio); | ||
| 349 | } | 376 | } |
| 350 | } | 377 | } |
| 351 | 378 | ||
| @@ -423,6 +450,9 @@ static void skip_signal(int signo) | |||
| 423 | 450 | ||
| 424 | static void sig_atexit(void) | 451 | static void sig_atexit(void) |
| 425 | { | 452 | { |
| 453 | if (child_pid != -1) | ||
| 454 | kill(child_pid, SIGTERM); | ||
| 455 | |||
| 426 | if (signr == -1) | 456 | if (signr == -1) |
| 427 | return; | 457 | return; |
| 428 | 458 | ||
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c new file mode 100644 index 000000000000..cb58b6605fcc --- /dev/null +++ b/tools/perf/builtin-timechart.c | |||
| @@ -0,0 +1,1183 @@ | |||
| 1 | /* | ||
| 2 | * builtin-timechart.c - make an svg timechart of system activity | ||
| 3 | * | ||
| 4 | * (C) Copyright 2009 Intel Corporation | ||
| 5 | * | ||
| 6 | * Authors: | ||
| 7 | * Arjan van de Ven <arjan@linux.intel.com> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU General Public License | ||
| 11 | * as published by the Free Software Foundation; version 2 | ||
| 12 | * of the License. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include "builtin.h" | ||
| 16 | |||
| 17 | #include "util/util.h" | ||
| 18 | |||
| 19 | #include "util/color.h" | ||
| 20 | #include <linux/list.h> | ||
| 21 | #include "util/cache.h" | ||
| 22 | #include <linux/rbtree.h> | ||
| 23 | #include "util/symbol.h" | ||
| 24 | #include "util/string.h" | ||
| 25 | #include "util/callchain.h" | ||
| 26 | #include "util/strlist.h" | ||
| 27 | |||
| 28 | #include "perf.h" | ||
| 29 | #include "util/header.h" | ||
| 30 | #include "util/parse-options.h" | ||
| 31 | #include "util/parse-events.h" | ||
| 32 | #include "util/event.h" | ||
| 33 | #include "util/data_map.h" | ||
| 34 | #include "util/svghelper.h" | ||
| 35 | |||
| 36 | static char const *input_name = "perf.data"; | ||
| 37 | static char const *output_name = "output.svg"; | ||
| 38 | |||
| 39 | |||
| 40 | static u64 sample_type; | ||
| 41 | |||
| 42 | static unsigned int numcpus; | ||
| 43 | static u64 min_freq; /* Lowest CPU frequency seen */ | ||
| 44 | static u64 max_freq; /* Highest CPU frequency seen */ | ||
| 45 | static u64 turbo_frequency; | ||
| 46 | |||
| 47 | static u64 first_time, last_time; | ||
| 48 | |||
| 49 | static int power_only; | ||
| 50 | |||
| 51 | |||
| 52 | struct per_pid; | ||
| 53 | struct per_pidcomm; | ||
| 54 | |||
| 55 | struct cpu_sample; | ||
| 56 | struct power_event; | ||
| 57 | struct wake_event; | ||
| 58 | |||
| 59 | struct sample_wrapper; | ||
| 60 | |||
| 61 | /* | ||
| 62 | * Datastructure layout: | ||
| 63 | * We keep an list of "pid"s, matching the kernels notion of a task struct. | ||
| 64 | * Each "pid" entry, has a list of "comm"s. | ||
| 65 | * this is because we want to track different programs different, while | ||
| 66 | * exec will reuse the original pid (by design). | ||
| 67 | * Each comm has a list of samples that will be used to draw | ||
| 68 | * final graph. | ||
| 69 | */ | ||
| 70 | |||
| 71 | struct per_pid { | ||
| 72 | struct per_pid *next; | ||
| 73 | |||
| 74 | int pid; | ||
| 75 | int ppid; | ||
| 76 | |||
| 77 | u64 start_time; | ||
| 78 | u64 end_time; | ||
| 79 | u64 total_time; | ||
| 80 | int display; | ||
| 81 | |||
| 82 | struct per_pidcomm *all; | ||
| 83 | struct per_pidcomm *current; | ||
| 84 | |||
| 85 | int painted; | ||
| 86 | }; | ||
| 87 | |||
| 88 | |||
| 89 | struct per_pidcomm { | ||
| 90 | struct per_pidcomm *next; | ||
| 91 | |||
| 92 | u64 start_time; | ||
| 93 | u64 end_time; | ||
| 94 | u64 total_time; | ||
| 95 | |||
| 96 | int Y; | ||
| 97 | int display; | ||
| 98 | |||
| 99 | long state; | ||
| 100 | u64 state_since; | ||
| 101 | |||
| 102 | char *comm; | ||
| 103 | |||
| 104 | struct cpu_sample *samples; | ||
| 105 | }; | ||
| 106 | |||
| 107 | struct sample_wrapper { | ||
| 108 | struct sample_wrapper *next; | ||
| 109 | |||
| 110 | u64 timestamp; | ||
| 111 | unsigned char data[0]; | ||
| 112 | }; | ||
| 113 | |||
| 114 | #define TYPE_NONE 0 | ||
| 115 | #define TYPE_RUNNING 1 | ||
| 116 | #define TYPE_WAITING 2 | ||
| 117 | #define TYPE_BLOCKED 3 | ||
| 118 | |||
| 119 | struct cpu_sample { | ||
| 120 | struct cpu_sample *next; | ||
| 121 | |||
| 122 | u64 start_time; | ||
| 123 | u64 end_time; | ||
| 124 | int type; | ||
| 125 | int cpu; | ||
| 126 | }; | ||
| 127 | |||
| 128 | static struct per_pid *all_data; | ||
| 129 | |||
| 130 | #define CSTATE 1 | ||
| 131 | #define PSTATE 2 | ||
| 132 | |||
| 133 | struct power_event { | ||
| 134 | struct power_event *next; | ||
| 135 | int type; | ||
| 136 | int state; | ||
| 137 | u64 start_time; | ||
| 138 | u64 end_time; | ||
| 139 | int cpu; | ||
| 140 | }; | ||
| 141 | |||
| 142 | struct wake_event { | ||
| 143 | struct wake_event *next; | ||
| 144 | int waker; | ||
| 145 | int wakee; | ||
| 146 | u64 time; | ||
| 147 | }; | ||
| 148 | |||
| 149 | static struct power_event *power_events; | ||
| 150 | static struct wake_event *wake_events; | ||
| 151 | |||
| 152 | struct sample_wrapper *all_samples; | ||
| 153 | |||
| 154 | |||
| 155 | struct process_filter; | ||
| 156 | struct process_filter { | ||
| 157 | char *name; | ||
| 158 | int pid; | ||
| 159 | struct process_filter *next; | ||
| 160 | }; | ||
| 161 | |||
| 162 | static struct process_filter *process_filter; | ||
| 163 | |||
| 164 | |||
| 165 | static struct per_pid *find_create_pid(int pid) | ||
| 166 | { | ||
| 167 | struct per_pid *cursor = all_data; | ||
| 168 | |||
| 169 | while (cursor) { | ||
| 170 | if (cursor->pid == pid) | ||
| 171 | return cursor; | ||
| 172 | cursor = cursor->next; | ||
| 173 | } | ||
| 174 | cursor = malloc(sizeof(struct per_pid)); | ||
| 175 | assert(cursor != NULL); | ||
| 176 | memset(cursor, 0, sizeof(struct per_pid)); | ||
| 177 | cursor->pid = pid; | ||
| 178 | cursor->next = all_data; | ||
| 179 | all_data = cursor; | ||
| 180 | return cursor; | ||
| 181 | } | ||
| 182 | |||
| 183 | static void pid_set_comm(int pid, char *comm) | ||
| 184 | { | ||
| 185 | struct per_pid *p; | ||
| 186 | struct per_pidcomm *c; | ||
| 187 | p = find_create_pid(pid); | ||
| 188 | c = p->all; | ||
| 189 | while (c) { | ||
| 190 | if (c->comm && strcmp(c->comm, comm) == 0) { | ||
| 191 | p->current = c; | ||
| 192 | return; | ||
| 193 | } | ||
| 194 | if (!c->comm) { | ||
| 195 | c->comm = strdup(comm); | ||
| 196 | p->current = c; | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | c = c->next; | ||
| 200 | } | ||
| 201 | c = malloc(sizeof(struct per_pidcomm)); | ||
| 202 | assert(c != NULL); | ||
| 203 | memset(c, 0, sizeof(struct per_pidcomm)); | ||
| 204 | c->comm = strdup(comm); | ||
| 205 | p->current = c; | ||
| 206 | c->next = p->all; | ||
| 207 | p->all = c; | ||
| 208 | } | ||
| 209 | |||
| 210 | static void pid_fork(int pid, int ppid, u64 timestamp) | ||
| 211 | { | ||
| 212 | struct per_pid *p, *pp; | ||
| 213 | p = find_create_pid(pid); | ||
| 214 | pp = find_create_pid(ppid); | ||
| 215 | p->ppid = ppid; | ||
| 216 | if (pp->current && pp->current->comm && !p->current) | ||
| 217 | pid_set_comm(pid, pp->current->comm); | ||
| 218 | |||
| 219 | p->start_time = timestamp; | ||
| 220 | if (p->current) { | ||
| 221 | p->current->start_time = timestamp; | ||
| 222 | p->current->state_since = timestamp; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | static void pid_exit(int pid, u64 timestamp) | ||
| 227 | { | ||
| 228 | struct per_pid *p; | ||
| 229 | p = find_create_pid(pid); | ||
| 230 | p->end_time = timestamp; | ||
| 231 | if (p->current) | ||
| 232 | p->current->end_time = timestamp; | ||
| 233 | } | ||
| 234 | |||
| 235 | static void | ||
| 236 | pid_put_sample(int pid, int type, unsigned int cpu, u64 start, u64 end) | ||
| 237 | { | ||
| 238 | struct per_pid *p; | ||
| 239 | struct per_pidcomm *c; | ||
| 240 | struct cpu_sample *sample; | ||
| 241 | |||
| 242 | p = find_create_pid(pid); | ||
| 243 | c = p->current; | ||
| 244 | if (!c) { | ||
| 245 | c = malloc(sizeof(struct per_pidcomm)); | ||
| 246 | assert(c != NULL); | ||
| 247 | memset(c, 0, sizeof(struct per_pidcomm)); | ||
| 248 | p->current = c; | ||
| 249 | c->next = p->all; | ||
| 250 | p->all = c; | ||
| 251 | } | ||
| 252 | |||
| 253 | sample = malloc(sizeof(struct cpu_sample)); | ||
| 254 | assert(sample != NULL); | ||
| 255 | memset(sample, 0, sizeof(struct cpu_sample)); | ||
| 256 | sample->start_time = start; | ||
| 257 | sample->end_time = end; | ||
| 258 | sample->type = type; | ||
| 259 | sample->next = c->samples; | ||
| 260 | sample->cpu = cpu; | ||
| 261 | c->samples = sample; | ||
| 262 | |||
| 263 | if (sample->type == TYPE_RUNNING && end > start && start > 0) { | ||
| 264 | c->total_time += (end-start); | ||
| 265 | p->total_time += (end-start); | ||
| 266 | } | ||
| 267 | |||
| 268 | if (c->start_time == 0 || c->start_time > start) | ||
| 269 | c->start_time = start; | ||
| 270 | if (p->start_time == 0 || p->start_time > start) | ||
| 271 | p->start_time = start; | ||
| 272 | |||
| 273 | if (cpu > numcpus) | ||
| 274 | numcpus = cpu; | ||
| 275 | } | ||
| 276 | |||
| 277 | #define MAX_CPUS 4096 | ||
| 278 | |||
| 279 | static u64 cpus_cstate_start_times[MAX_CPUS]; | ||
| 280 | static int cpus_cstate_state[MAX_CPUS]; | ||
| 281 | static u64 cpus_pstate_start_times[MAX_CPUS]; | ||
| 282 | static u64 cpus_pstate_state[MAX_CPUS]; | ||
| 283 | |||
| 284 | static int | ||
| 285 | process_comm_event(event_t *event) | ||
| 286 | { | ||
| 287 | pid_set_comm(event->comm.pid, event->comm.comm); | ||
| 288 | return 0; | ||
| 289 | } | ||
| 290 | static int | ||
| 291 | process_fork_event(event_t *event) | ||
| 292 | { | ||
| 293 | pid_fork(event->fork.pid, event->fork.ppid, event->fork.time); | ||
| 294 | return 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | static int | ||
| 298 | process_exit_event(event_t *event) | ||
| 299 | { | ||
| 300 | pid_exit(event->fork.pid, event->fork.time); | ||
| 301 | return 0; | ||
| 302 | } | ||
| 303 | |||
| 304 | struct trace_entry { | ||
| 305 | u32 size; | ||
| 306 | unsigned short type; | ||
| 307 | unsigned char flags; | ||
| 308 | unsigned char preempt_count; | ||
| 309 | int pid; | ||
| 310 | int tgid; | ||
| 311 | }; | ||
| 312 | |||
| 313 | struct power_entry { | ||
| 314 | struct trace_entry te; | ||
| 315 | s64 type; | ||
| 316 | s64 value; | ||
| 317 | }; | ||
| 318 | |||
| 319 | #define TASK_COMM_LEN 16 | ||
| 320 | struct wakeup_entry { | ||
| 321 | struct trace_entry te; | ||
| 322 | char comm[TASK_COMM_LEN]; | ||
| 323 | int pid; | ||
| 324 | int prio; | ||
| 325 | int success; | ||
| 326 | }; | ||
| 327 | |||
| 328 | /* | ||
| 329 | * trace_flag_type is an enumeration that holds different | ||
| 330 | * states when a trace occurs. These are: | ||
| 331 | * IRQS_OFF - interrupts were disabled | ||
| 332 | * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags | ||
| 333 | * NEED_RESCED - reschedule is requested | ||
| 334 | * HARDIRQ - inside an interrupt handler | ||
| 335 | * SOFTIRQ - inside a softirq handler | ||
| 336 | */ | ||
| 337 | enum trace_flag_type { | ||
| 338 | TRACE_FLAG_IRQS_OFF = 0x01, | ||
| 339 | TRACE_FLAG_IRQS_NOSUPPORT = 0x02, | ||
| 340 | TRACE_FLAG_NEED_RESCHED = 0x04, | ||
| 341 | TRACE_FLAG_HARDIRQ = 0x08, | ||
| 342 | TRACE_FLAG_SOFTIRQ = 0x10, | ||
| 343 | }; | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | struct sched_switch { | ||
| 348 | struct trace_entry te; | ||
| 349 | char prev_comm[TASK_COMM_LEN]; | ||
| 350 | int prev_pid; | ||
| 351 | int prev_prio; | ||
| 352 | long prev_state; /* Arjan weeps. */ | ||
| 353 | char next_comm[TASK_COMM_LEN]; | ||
| 354 | int next_pid; | ||
| 355 | int next_prio; | ||
| 356 | }; | ||
| 357 | |||
| 358 | static void c_state_start(int cpu, u64 timestamp, int state) | ||
| 359 | { | ||
| 360 | cpus_cstate_start_times[cpu] = timestamp; | ||
| 361 | cpus_cstate_state[cpu] = state; | ||
| 362 | } | ||
| 363 | |||
| 364 | static void c_state_end(int cpu, u64 timestamp) | ||
| 365 | { | ||
| 366 | struct power_event *pwr; | ||
| 367 | pwr = malloc(sizeof(struct power_event)); | ||
| 368 | if (!pwr) | ||
| 369 | return; | ||
| 370 | memset(pwr, 0, sizeof(struct power_event)); | ||
| 371 | |||
| 372 | pwr->state = cpus_cstate_state[cpu]; | ||
| 373 | pwr->start_time = cpus_cstate_start_times[cpu]; | ||
| 374 | pwr->end_time = timestamp; | ||
| 375 | pwr->cpu = cpu; | ||
| 376 | pwr->type = CSTATE; | ||
| 377 | pwr->next = power_events; | ||
| 378 | |||
| 379 | power_events = pwr; | ||
| 380 | } | ||
| 381 | |||
| 382 | static void p_state_change(int cpu, u64 timestamp, u64 new_freq) | ||
| 383 | { | ||
| 384 | struct power_event *pwr; | ||
| 385 | pwr = malloc(sizeof(struct power_event)); | ||
| 386 | |||
| 387 | if (new_freq > 8000000) /* detect invalid data */ | ||
| 388 | return; | ||
| 389 | |||
| 390 | if (!pwr) | ||
| 391 | return; | ||
| 392 | memset(pwr, 0, sizeof(struct power_event)); | ||
| 393 | |||
| 394 | pwr->state = cpus_pstate_state[cpu]; | ||
| 395 | pwr->start_time = cpus_pstate_start_times[cpu]; | ||
| 396 | pwr->end_time = timestamp; | ||
| 397 | pwr->cpu = cpu; | ||
| 398 | pwr->type = PSTATE; | ||
| 399 | pwr->next = power_events; | ||
| 400 | |||
| 401 | if (!pwr->start_time) | ||
| 402 | pwr->start_time = first_time; | ||
| 403 | |||
| 404 | power_events = pwr; | ||
| 405 | |||
| 406 | cpus_pstate_state[cpu] = new_freq; | ||
| 407 | cpus_pstate_start_times[cpu] = timestamp; | ||
| 408 | |||
| 409 | if ((u64)new_freq > max_freq) | ||
| 410 | max_freq = new_freq; | ||
| 411 | |||
| 412 | if (new_freq < min_freq || min_freq == 0) | ||
| 413 | min_freq = new_freq; | ||
| 414 | |||
| 415 | if (new_freq == max_freq - 1000) | ||
| 416 | turbo_frequency = max_freq; | ||
| 417 | } | ||
| 418 | |||
| 419 | static void | ||
| 420 | sched_wakeup(int cpu, u64 timestamp, int pid, struct trace_entry *te) | ||
| 421 | { | ||
| 422 | struct wake_event *we; | ||
| 423 | struct per_pid *p; | ||
| 424 | struct wakeup_entry *wake = (void *)te; | ||
| 425 | |||
| 426 | we = malloc(sizeof(struct wake_event)); | ||
| 427 | if (!we) | ||
| 428 | return; | ||
| 429 | |||
| 430 | memset(we, 0, sizeof(struct wake_event)); | ||
| 431 | we->time = timestamp; | ||
| 432 | we->waker = pid; | ||
| 433 | |||
| 434 | if ((te->flags & TRACE_FLAG_HARDIRQ) || (te->flags & TRACE_FLAG_SOFTIRQ)) | ||
| 435 | we->waker = -1; | ||
| 436 | |||
| 437 | we->wakee = wake->pid; | ||
| 438 | we->next = wake_events; | ||
| 439 | wake_events = we; | ||
| 440 | p = find_create_pid(we->wakee); | ||
| 441 | |||
| 442 | if (p && p->current && p->current->state == TYPE_NONE) { | ||
| 443 | p->current->state_since = timestamp; | ||
| 444 | p->current->state = TYPE_WAITING; | ||
| 445 | } | ||
| 446 | if (p && p->current && p->current->state == TYPE_BLOCKED) { | ||
| 447 | pid_put_sample(p->pid, p->current->state, cpu, p->current->state_since, timestamp); | ||
| 448 | p->current->state_since = timestamp; | ||
| 449 | p->current->state = TYPE_WAITING; | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | static void sched_switch(int cpu, u64 timestamp, struct trace_entry *te) | ||
| 454 | { | ||
| 455 | struct per_pid *p = NULL, *prev_p; | ||
| 456 | struct sched_switch *sw = (void *)te; | ||
| 457 | |||
| 458 | |||
| 459 | prev_p = find_create_pid(sw->prev_pid); | ||
| 460 | |||
| 461 | p = find_create_pid(sw->next_pid); | ||
| 462 | |||
| 463 | if (prev_p->current && prev_p->current->state != TYPE_NONE) | ||
| 464 | pid_put_sample(sw->prev_pid, TYPE_RUNNING, cpu, prev_p->current->state_since, timestamp); | ||
| 465 | if (p && p->current) { | ||
| 466 | if (p->current->state != TYPE_NONE) | ||
| 467 | pid_put_sample(sw->next_pid, p->current->state, cpu, p->current->state_since, timestamp); | ||
| 468 | |||
| 469 | p->current->state_since = timestamp; | ||
| 470 | p->current->state = TYPE_RUNNING; | ||
| 471 | } | ||
| 472 | |||
| 473 | if (prev_p->current) { | ||
| 474 | prev_p->current->state = TYPE_NONE; | ||
| 475 | prev_p->current->state_since = timestamp; | ||
| 476 | if (sw->prev_state & 2) | ||
| 477 | prev_p->current->state = TYPE_BLOCKED; | ||
| 478 | if (sw->prev_state == 0) | ||
| 479 | prev_p->current->state = TYPE_WAITING; | ||
| 480 | } | ||
| 481 | } | ||
| 482 | |||
| 483 | |||
| 484 | static int | ||
| 485 | process_sample_event(event_t *event) | ||
| 486 | { | ||
| 487 | int cursor = 0; | ||
| 488 | u64 addr = 0; | ||
| 489 | u64 stamp = 0; | ||
| 490 | u32 cpu = 0; | ||
| 491 | u32 pid = 0; | ||
| 492 | struct trace_entry *te; | ||
| 493 | |||
| 494 | if (sample_type & PERF_SAMPLE_IP) | ||
| 495 | cursor++; | ||
| 496 | |||
| 497 | if (sample_type & PERF_SAMPLE_TID) { | ||
| 498 | pid = event->sample.array[cursor]>>32; | ||
| 499 | cursor++; | ||
| 500 | } | ||
| 501 | if (sample_type & PERF_SAMPLE_TIME) { | ||
| 502 | stamp = event->sample.array[cursor++]; | ||
| 503 | |||
| 504 | if (!first_time || first_time > stamp) | ||
| 505 | first_time = stamp; | ||
| 506 | if (last_time < stamp) | ||
| 507 | last_time = stamp; | ||
| 508 | |||
| 509 | } | ||
| 510 | if (sample_type & PERF_SAMPLE_ADDR) | ||
| 511 | addr = event->sample.array[cursor++]; | ||
| 512 | if (sample_type & PERF_SAMPLE_ID) | ||
| 513 | cursor++; | ||
| 514 | if (sample_type & PERF_SAMPLE_STREAM_ID) | ||
| 515 | cursor++; | ||
| 516 | if (sample_type & PERF_SAMPLE_CPU) | ||
| 517 | cpu = event->sample.array[cursor++] & 0xFFFFFFFF; | ||
| 518 | if (sample_type & PERF_SAMPLE_PERIOD) | ||
| 519 | cursor++; | ||
| 520 | |||
| 521 | te = (void *)&event->sample.array[cursor]; | ||
| 522 | |||
| 523 | if (sample_type & PERF_SAMPLE_RAW && te->size > 0) { | ||
| 524 | char *event_str; | ||
| 525 | struct power_entry *pe; | ||
| 526 | |||
| 527 | pe = (void *)te; | ||
| 528 | |||
| 529 | event_str = perf_header__find_event(te->type); | ||
| 530 | |||
| 531 | if (!event_str) | ||
| 532 | return 0; | ||
| 533 | |||
| 534 | if (strcmp(event_str, "power:power_start") == 0) | ||
| 535 | c_state_start(cpu, stamp, pe->value); | ||
| 536 | |||
| 537 | if (strcmp(event_str, "power:power_end") == 0) | ||
| 538 | c_state_end(cpu, stamp); | ||
| 539 | |||
| 540 | if (strcmp(event_str, "power:power_frequency") == 0) | ||
| 541 | p_state_change(cpu, stamp, pe->value); | ||
| 542 | |||
| 543 | if (strcmp(event_str, "sched:sched_wakeup") == 0) | ||
| 544 | sched_wakeup(cpu, stamp, pid, te); | ||
| 545 | |||
| 546 | if (strcmp(event_str, "sched:sched_switch") == 0) | ||
| 547 | sched_switch(cpu, stamp, te); | ||
| 548 | } | ||
| 549 | return 0; | ||
| 550 | } | ||
| 551 | |||
| 552 | /* | ||
| 553 | * After the last sample we need to wrap up the current C/P state | ||
| 554 | * and close out each CPU for these. | ||
| 555 | */ | ||
| 556 | static void end_sample_processing(void) | ||
| 557 | { | ||
| 558 | u64 cpu; | ||
| 559 | struct power_event *pwr; | ||
| 560 | |||
| 561 | for (cpu = 0; cpu <= numcpus; cpu++) { | ||
| 562 | pwr = malloc(sizeof(struct power_event)); | ||
| 563 | if (!pwr) | ||
| 564 | return; | ||
| 565 | memset(pwr, 0, sizeof(struct power_event)); | ||
| 566 | |||
| 567 | /* C state */ | ||
| 568 | #if 0 | ||
| 569 | pwr->state = cpus_cstate_state[cpu]; | ||
| 570 | pwr->start_time = cpus_cstate_start_times[cpu]; | ||
| 571 | pwr->end_time = last_time; | ||
| 572 | pwr->cpu = cpu; | ||
| 573 | pwr->type = CSTATE; | ||
| 574 | pwr->next = power_events; | ||
| 575 | |||
| 576 | power_events = pwr; | ||
| 577 | #endif | ||
| 578 | /* P state */ | ||
| 579 | |||
| 580 | pwr = malloc(sizeof(struct power_event)); | ||
| 581 | if (!pwr) | ||
| 582 | return; | ||
| 583 | memset(pwr, 0, sizeof(struct power_event)); | ||
| 584 | |||
| 585 | pwr->state = cpus_pstate_state[cpu]; | ||
| 586 | pwr->start_time = cpus_pstate_start_times[cpu]; | ||
| 587 | pwr->end_time = last_time; | ||
| 588 | pwr->cpu = cpu; | ||
| 589 | pwr->type = PSTATE; | ||
| 590 | pwr->next = power_events; | ||
| 591 | |||
| 592 | if (!pwr->start_time) | ||
| 593 | pwr->start_time = first_time; | ||
| 594 | if (!pwr->state) | ||
| 595 | pwr->state = min_freq; | ||
| 596 | power_events = pwr; | ||
| 597 | } | ||
| 598 | } | ||
| 599 | |||
| 600 | static u64 sample_time(event_t *event) | ||
| 601 | { | ||
| 602 | int cursor; | ||
| 603 | |||
| 604 | cursor = 0; | ||
| 605 | if (sample_type & PERF_SAMPLE_IP) | ||
| 606 | cursor++; | ||
| 607 | if (sample_type & PERF_SAMPLE_TID) | ||
| 608 | cursor++; | ||
| 609 | if (sample_type & PERF_SAMPLE_TIME) | ||
| 610 | return event->sample.array[cursor]; | ||
| 611 | return 0; | ||
| 612 | } | ||
| 613 | |||
| 614 | |||
| 615 | /* | ||
| 616 | * We first queue all events, sorted backwards by insertion. | ||
| 617 | * The order will get flipped later. | ||
| 618 | */ | ||
| 619 | static int | ||
| 620 | queue_sample_event(event_t *event) | ||
| 621 | { | ||
| 622 | struct sample_wrapper *copy, *prev; | ||
| 623 | int size; | ||
| 624 | |||
| 625 | size = event->sample.header.size + sizeof(struct sample_wrapper) + 8; | ||
| 626 | |||
| 627 | copy = malloc(size); | ||
| 628 | if (!copy) | ||
| 629 | return 1; | ||
| 630 | |||
| 631 | memset(copy, 0, size); | ||
| 632 | |||
| 633 | copy->next = NULL; | ||
| 634 | copy->timestamp = sample_time(event); | ||
| 635 | |||
| 636 | memcpy(©->data, event, event->sample.header.size); | ||
| 637 | |||
| 638 | /* insert in the right place in the list */ | ||
| 639 | |||
| 640 | if (!all_samples) { | ||
| 641 | /* first sample ever */ | ||
| 642 | all_samples = copy; | ||
| 643 | return 0; | ||
| 644 | } | ||
| 645 | |||
| 646 | if (all_samples->timestamp < copy->timestamp) { | ||
| 647 | /* insert at the head of the list */ | ||
| 648 | copy->next = all_samples; | ||
| 649 | all_samples = copy; | ||
| 650 | return 0; | ||
| 651 | } | ||
| 652 | |||
| 653 | prev = all_samples; | ||
| 654 | while (prev->next) { | ||
| 655 | if (prev->next->timestamp < copy->timestamp) { | ||
| 656 | copy->next = prev->next; | ||
| 657 | prev->next = copy; | ||
| 658 | return 0; | ||
| 659 | } | ||
| 660 | prev = prev->next; | ||
| 661 | } | ||
| 662 | /* insert at the end of the list */ | ||
| 663 | prev->next = copy; | ||
| 664 | |||
| 665 | return 0; | ||
| 666 | } | ||
| 667 | |||
| 668 | static void sort_queued_samples(void) | ||
| 669 | { | ||
| 670 | struct sample_wrapper *cursor, *next; | ||
| 671 | |||
| 672 | cursor = all_samples; | ||
| 673 | all_samples = NULL; | ||
| 674 | |||
| 675 | while (cursor) { | ||
| 676 | next = cursor->next; | ||
| 677 | cursor->next = all_samples; | ||
| 678 | all_samples = cursor; | ||
| 679 | cursor = next; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | /* | ||
| 684 | * Sort the pid datastructure | ||
| 685 | */ | ||
| 686 | static void sort_pids(void) | ||
| 687 | { | ||
| 688 | struct per_pid *new_list, *p, *cursor, *prev; | ||
| 689 | /* sort by ppid first, then by pid, lowest to highest */ | ||
| 690 | |||
| 691 | new_list = NULL; | ||
| 692 | |||
| 693 | while (all_data) { | ||
| 694 | p = all_data; | ||
| 695 | all_data = p->next; | ||
| 696 | p->next = NULL; | ||
| 697 | |||
| 698 | if (new_list == NULL) { | ||
| 699 | new_list = p; | ||
| 700 | p->next = NULL; | ||
| 701 | continue; | ||
| 702 | } | ||
| 703 | prev = NULL; | ||
| 704 | cursor = new_list; | ||
| 705 | while (cursor) { | ||
| 706 | if (cursor->ppid > p->ppid || | ||
| 707 | (cursor->ppid == p->ppid && cursor->pid > p->pid)) { | ||
| 708 | /* must insert before */ | ||
| 709 | if (prev) { | ||
| 710 | p->next = prev->next; | ||
| 711 | prev->next = p; | ||
| 712 | cursor = NULL; | ||
| 713 | continue; | ||
| 714 | } else { | ||
| 715 | p->next = new_list; | ||
| 716 | new_list = p; | ||
| 717 | cursor = NULL; | ||
| 718 | continue; | ||
| 719 | } | ||
| 720 | } | ||
| 721 | |||
| 722 | prev = cursor; | ||
| 723 | cursor = cursor->next; | ||
| 724 | if (!cursor) | ||
| 725 | prev->next = p; | ||
| 726 | } | ||
| 727 | } | ||
| 728 | all_data = new_list; | ||
| 729 | } | ||
| 730 | |||
| 731 | |||
| 732 | static void draw_c_p_states(void) | ||
| 733 | { | ||
| 734 | struct power_event *pwr; | ||
| 735 | pwr = power_events; | ||
| 736 | |||
| 737 | /* | ||
| 738 | * two pass drawing so that the P state bars are on top of the C state blocks | ||
| 739 | */ | ||
| 740 | while (pwr) { | ||
| 741 | if (pwr->type == CSTATE) | ||
| 742 | svg_cstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state); | ||
| 743 | pwr = pwr->next; | ||
| 744 | } | ||
| 745 | |||
| 746 | pwr = power_events; | ||
| 747 | while (pwr) { | ||
| 748 | if (pwr->type == PSTATE) { | ||
| 749 | if (!pwr->state) | ||
| 750 | pwr->state = min_freq; | ||
| 751 | svg_pstate(pwr->cpu, pwr->start_time, pwr->end_time, pwr->state); | ||
| 752 | } | ||
| 753 | pwr = pwr->next; | ||
| 754 | } | ||
| 755 | } | ||
| 756 | |||
| 757 | static void draw_wakeups(void) | ||
| 758 | { | ||
| 759 | struct wake_event *we; | ||
| 760 | struct per_pid *p; | ||
| 761 | struct per_pidcomm *c; | ||
| 762 | |||
| 763 | we = wake_events; | ||
| 764 | while (we) { | ||
| 765 | int from = 0, to = 0; | ||
| 766 | char *task_from = NULL, *task_to = NULL; | ||
| 767 | |||
| 768 | /* locate the column of the waker and wakee */ | ||
| 769 | p = all_data; | ||
| 770 | while (p) { | ||
| 771 | if (p->pid == we->waker || p->pid == we->wakee) { | ||
| 772 | c = p->all; | ||
| 773 | while (c) { | ||
| 774 | if (c->Y && c->start_time <= we->time && c->end_time >= we->time) { | ||
| 775 | if (p->pid == we->waker && !from) { | ||
| 776 | from = c->Y; | ||
| 777 | task_from = strdup(c->comm); | ||
| 778 | } | ||
| 779 | if (p->pid == we->wakee && !to) { | ||
| 780 | to = c->Y; | ||
| 781 | task_to = strdup(c->comm); | ||
| 782 | } | ||
| 783 | } | ||
| 784 | c = c->next; | ||
| 785 | } | ||
| 786 | c = p->all; | ||
| 787 | while (c) { | ||
| 788 | if (p->pid == we->waker && !from) { | ||
| 789 | from = c->Y; | ||
| 790 | task_from = strdup(c->comm); | ||
| 791 | } | ||
| 792 | if (p->pid == we->wakee && !to) { | ||
| 793 | to = c->Y; | ||
| 794 | task_to = strdup(c->comm); | ||
| 795 | } | ||
| 796 | c = c->next; | ||
| 797 | } | ||
| 798 | } | ||
| 799 | p = p->next; | ||
| 800 | } | ||
| 801 | |||
| 802 | if (!task_from) { | ||
| 803 | task_from = malloc(40); | ||
| 804 | sprintf(task_from, "[%i]", we->waker); | ||
| 805 | } | ||
| 806 | if (!task_to) { | ||
| 807 | task_to = malloc(40); | ||
| 808 | sprintf(task_to, "[%i]", we->wakee); | ||
| 809 | } | ||
| 810 | |||
| 811 | if (we->waker == -1) | ||
| 812 | svg_interrupt(we->time, to); | ||
| 813 | else if (from && to && abs(from - to) == 1) | ||
| 814 | svg_wakeline(we->time, from, to); | ||
| 815 | else | ||
| 816 | svg_partial_wakeline(we->time, from, task_from, to, task_to); | ||
| 817 | we = we->next; | ||
| 818 | |||
| 819 | free(task_from); | ||
| 820 | free(task_to); | ||
| 821 | } | ||
| 822 | } | ||
| 823 | |||
| 824 | static void draw_cpu_usage(void) | ||
| 825 | { | ||
| 826 | struct per_pid *p; | ||
| 827 | struct per_pidcomm *c; | ||
| 828 | struct cpu_sample *sample; | ||
| 829 | p = all_data; | ||
| 830 | while (p) { | ||
| 831 | c = p->all; | ||
| 832 | while (c) { | ||
| 833 | sample = c->samples; | ||
| 834 | while (sample) { | ||
| 835 | if (sample->type == TYPE_RUNNING) | ||
| 836 | svg_process(sample->cpu, sample->start_time, sample->end_time, "sample", c->comm); | ||
| 837 | |||
| 838 | sample = sample->next; | ||
| 839 | } | ||
| 840 | c = c->next; | ||
| 841 | } | ||
| 842 | p = p->next; | ||
| 843 | } | ||
| 844 | } | ||
| 845 | |||
| 846 | static void draw_process_bars(void) | ||
| 847 | { | ||
| 848 | struct per_pid *p; | ||
| 849 | struct per_pidcomm *c; | ||
| 850 | struct cpu_sample *sample; | ||
| 851 | int Y = 0; | ||
| 852 | |||
| 853 | Y = 2 * numcpus + 2; | ||
| 854 | |||
| 855 | p = all_data; | ||
| 856 | while (p) { | ||
| 857 | c = p->all; | ||
| 858 | while (c) { | ||
| 859 | if (!c->display) { | ||
| 860 | c->Y = 0; | ||
| 861 | c = c->next; | ||
| 862 | continue; | ||
| 863 | } | ||
| 864 | |||
| 865 | svg_box(Y, c->start_time, c->end_time, "process"); | ||
| 866 | sample = c->samples; | ||
| 867 | while (sample) { | ||
| 868 | if (sample->type == TYPE_RUNNING) | ||
| 869 | svg_sample(Y, sample->cpu, sample->start_time, sample->end_time); | ||
| 870 | if (sample->type == TYPE_BLOCKED) | ||
| 871 | svg_box(Y, sample->start_time, sample->end_time, "blocked"); | ||
| 872 | if (sample->type == TYPE_WAITING) | ||
| 873 | svg_waiting(Y, sample->start_time, sample->end_time); | ||
| 874 | sample = sample->next; | ||
| 875 | } | ||
| 876 | |||
| 877 | if (c->comm) { | ||
| 878 | char comm[256]; | ||
| 879 | if (c->total_time > 5000000000) /* 5 seconds */ | ||
| 880 | sprintf(comm, "%s:%i (%2.2fs)", c->comm, p->pid, c->total_time / 1000000000.0); | ||
| 881 | else | ||
| 882 | sprintf(comm, "%s:%i (%3.1fms)", c->comm, p->pid, c->total_time / 1000000.0); | ||
| 883 | |||
| 884 | svg_text(Y, c->start_time, comm); | ||
| 885 | } | ||
| 886 | c->Y = Y; | ||
| 887 | Y++; | ||
| 888 | c = c->next; | ||
| 889 | } | ||
| 890 | p = p->next; | ||
| 891 | } | ||
| 892 | } | ||
| 893 | |||
| 894 | static void add_process_filter(const char *string) | ||
| 895 | { | ||
| 896 | struct process_filter *filt; | ||
| 897 | int pid; | ||
| 898 | |||
| 899 | pid = strtoull(string, NULL, 10); | ||
| 900 | filt = malloc(sizeof(struct process_filter)); | ||
| 901 | if (!filt) | ||
| 902 | return; | ||
| 903 | |||
| 904 | filt->name = strdup(string); | ||
| 905 | filt->pid = pid; | ||
| 906 | filt->next = process_filter; | ||
| 907 | |||
| 908 | process_filter = filt; | ||
| 909 | } | ||
| 910 | |||
| 911 | static int passes_filter(struct per_pid *p, struct per_pidcomm *c) | ||
| 912 | { | ||
| 913 | struct process_filter *filt; | ||
| 914 | if (!process_filter) | ||
| 915 | return 1; | ||
| 916 | |||
| 917 | filt = process_filter; | ||
| 918 | while (filt) { | ||
| 919 | if (filt->pid && p->pid == filt->pid) | ||
| 920 | return 1; | ||
| 921 | if (strcmp(filt->name, c->comm) == 0) | ||
| 922 | return 1; | ||
| 923 | filt = filt->next; | ||
| 924 | } | ||
| 925 | return 0; | ||
| 926 | } | ||
| 927 | |||
| 928 | static int determine_display_tasks_filtered(void) | ||
| 929 | { | ||
| 930 | struct per_pid *p; | ||
| 931 | struct per_pidcomm *c; | ||
| 932 | int count = 0; | ||
| 933 | |||
| 934 | p = all_data; | ||
| 935 | while (p) { | ||
| 936 | p->display = 0; | ||
| 937 | if (p->start_time == 1) | ||
| 938 | p->start_time = first_time; | ||
| 939 | |||
| 940 | /* no exit marker, task kept running to the end */ | ||
| 941 | if (p->end_time == 0) | ||
| 942 | p->end_time = last_time; | ||
| 943 | |||
| 944 | c = p->all; | ||
| 945 | |||
| 946 | while (c) { | ||
| 947 | c->display = 0; | ||
| 948 | |||
| 949 | if (c->start_time == 1) | ||
| 950 | c->start_time = first_time; | ||
| 951 | |||
| 952 | if (passes_filter(p, c)) { | ||
| 953 | c->display = 1; | ||
| 954 | p->display = 1; | ||
| 955 | count++; | ||
| 956 | } | ||
| 957 | |||
| 958 | if (c->end_time == 0) | ||
| 959 | c->end_time = last_time; | ||
| 960 | |||
| 961 | c = c->next; | ||
| 962 | } | ||
| 963 | p = p->next; | ||
| 964 | } | ||
| 965 | return count; | ||
| 966 | } | ||
| 967 | |||
| 968 | static int determine_display_tasks(u64 threshold) | ||
| 969 | { | ||
| 970 | struct per_pid *p; | ||
| 971 | struct per_pidcomm *c; | ||
| 972 | int count = 0; | ||
| 973 | |||
| 974 | if (process_filter) | ||
| 975 | return determine_display_tasks_filtered(); | ||
| 976 | |||
| 977 | p = all_data; | ||
| 978 | while (p) { | ||
| 979 | p->display = 0; | ||
| 980 | if (p->start_time == 1) | ||
| 981 | p->start_time = first_time; | ||
| 982 | |||
| 983 | /* no exit marker, task kept running to the end */ | ||
| 984 | if (p->end_time == 0) | ||
| 985 | p->end_time = last_time; | ||
| 986 | if (p->total_time >= threshold && !power_only) | ||
| 987 | p->display = 1; | ||
| 988 | |||
| 989 | c = p->all; | ||
| 990 | |||
| 991 | while (c) { | ||
| 992 | c->display = 0; | ||
| 993 | |||
| 994 | if (c->start_time == 1) | ||
| 995 | c->start_time = first_time; | ||
| 996 | |||
| 997 | if (c->total_time >= threshold && !power_only) { | ||
| 998 | c->display = 1; | ||
| 999 | count++; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | if (c->end_time == 0) | ||
| 1003 | c->end_time = last_time; | ||
| 1004 | |||
| 1005 | c = c->next; | ||
| 1006 | } | ||
| 1007 | p = p->next; | ||
| 1008 | } | ||
| 1009 | return count; | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | |||
| 1013 | |||
| 1014 | #define TIME_THRESH 10000000 | ||
| 1015 | |||
| 1016 | static void write_svg_file(const char *filename) | ||
| 1017 | { | ||
| 1018 | u64 i; | ||
| 1019 | int count; | ||
| 1020 | |||
| 1021 | numcpus++; | ||
| 1022 | |||
| 1023 | |||
| 1024 | count = determine_display_tasks(TIME_THRESH); | ||
| 1025 | |||
| 1026 | /* We'd like to show at least 15 tasks; be less picky if we have fewer */ | ||
| 1027 | if (count < 15) | ||
| 1028 | count = determine_display_tasks(TIME_THRESH / 10); | ||
| 1029 | |||
| 1030 | open_svg(filename, numcpus, count, first_time, last_time); | ||
| 1031 | |||
| 1032 | svg_time_grid(); | ||
| 1033 | svg_legenda(); | ||
| 1034 | |||
| 1035 | for (i = 0; i < numcpus; i++) | ||
| 1036 | svg_cpu_box(i, max_freq, turbo_frequency); | ||
| 1037 | |||
| 1038 | draw_cpu_usage(); | ||
| 1039 | draw_process_bars(); | ||
| 1040 | draw_c_p_states(); | ||
| 1041 | draw_wakeups(); | ||
| 1042 | |||
| 1043 | svg_close(); | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | static void process_samples(void) | ||
| 1047 | { | ||
| 1048 | struct sample_wrapper *cursor; | ||
| 1049 | event_t *event; | ||
| 1050 | |||
| 1051 | sort_queued_samples(); | ||
| 1052 | |||
| 1053 | cursor = all_samples; | ||
| 1054 | while (cursor) { | ||
| 1055 | event = (void *)&cursor->data; | ||
| 1056 | cursor = cursor->next; | ||
| 1057 | process_sample_event(event); | ||
| 1058 | } | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | static int sample_type_check(u64 type) | ||
| 1062 | { | ||
| 1063 | sample_type = type; | ||
| 1064 | |||
| 1065 | if (!(sample_type & PERF_SAMPLE_RAW)) { | ||
| 1066 | fprintf(stderr, "No trace samples found in the file.\n" | ||
| 1067 | "Have you used 'perf timechart record' to record it?\n"); | ||
| 1068 | return -1; | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | return 0; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | static struct perf_file_handler file_handler = { | ||
| 1075 | .process_comm_event = process_comm_event, | ||
| 1076 | .process_fork_event = process_fork_event, | ||
| 1077 | .process_exit_event = process_exit_event, | ||
| 1078 | .process_sample_event = queue_sample_event, | ||
| 1079 | .sample_type_check = sample_type_check, | ||
| 1080 | }; | ||
| 1081 | |||
| 1082 | static int __cmd_timechart(void) | ||
| 1083 | { | ||
| 1084 | struct perf_header *header; | ||
| 1085 | int ret; | ||
| 1086 | |||
| 1087 | register_perf_file_handler(&file_handler); | ||
| 1088 | |||
| 1089 | ret = mmap_dispatch_perf_file(&header, input_name, 0, 0, | ||
| 1090 | &event__cwdlen, &event__cwd); | ||
| 1091 | if (ret) | ||
| 1092 | return EXIT_FAILURE; | ||
| 1093 | |||
| 1094 | process_samples(); | ||
| 1095 | |||
| 1096 | end_sample_processing(); | ||
| 1097 | |||
| 1098 | sort_pids(); | ||
| 1099 | |||
| 1100 | write_svg_file(output_name); | ||
| 1101 | |||
| 1102 | pr_info("Written %2.1f seconds of trace to %s.\n", | ||
| 1103 | (last_time - first_time) / 1000000000.0, output_name); | ||
| 1104 | |||
| 1105 | return EXIT_SUCCESS; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | static const char * const timechart_usage[] = { | ||
| 1109 | "perf timechart [<options>] {record}", | ||
| 1110 | NULL | ||
| 1111 | }; | ||
| 1112 | |||
| 1113 | static const char *record_args[] = { | ||
| 1114 | "record", | ||
| 1115 | "-a", | ||
| 1116 | "-R", | ||
| 1117 | "-M", | ||
| 1118 | "-f", | ||
| 1119 | "-c", "1", | ||
| 1120 | "-e", "power:power_start", | ||
| 1121 | "-e", "power:power_end", | ||
| 1122 | "-e", "power:power_frequency", | ||
| 1123 | "-e", "sched:sched_wakeup", | ||
| 1124 | "-e", "sched:sched_switch", | ||
| 1125 | }; | ||
| 1126 | |||
| 1127 | static int __cmd_record(int argc, const char **argv) | ||
| 1128 | { | ||
| 1129 | unsigned int rec_argc, i, j; | ||
| 1130 | const char **rec_argv; | ||
| 1131 | |||
| 1132 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | ||
| 1133 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | ||
| 1134 | |||
| 1135 | for (i = 0; i < ARRAY_SIZE(record_args); i++) | ||
| 1136 | rec_argv[i] = strdup(record_args[i]); | ||
| 1137 | |||
| 1138 | for (j = 1; j < (unsigned int)argc; j++, i++) | ||
| 1139 | rec_argv[i] = argv[j]; | ||
| 1140 | |||
| 1141 | return cmd_record(i, rec_argv, NULL); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | static int | ||
| 1145 | parse_process(const struct option *opt __used, const char *arg, int __used unset) | ||
| 1146 | { | ||
| 1147 | if (arg) | ||
| 1148 | add_process_filter(arg); | ||
| 1149 | return 0; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | static const struct option options[] = { | ||
| 1153 | OPT_STRING('i', "input", &input_name, "file", | ||
| 1154 | "input file name"), | ||
| 1155 | OPT_STRING('o', "output", &output_name, "file", | ||
| 1156 | "output file name"), | ||
| 1157 | OPT_INTEGER('w', "width", &svg_page_width, | ||
| 1158 | "page width"), | ||
| 1159 | OPT_BOOLEAN('P', "power-only", &power_only, | ||
| 1160 | "output power data only"), | ||
| 1161 | OPT_CALLBACK('p', "process", NULL, "process", | ||
| 1162 | "process selector. Pass a pid or process name.", | ||
| 1163 | parse_process), | ||
| 1164 | OPT_END() | ||
| 1165 | }; | ||
| 1166 | |||
| 1167 | |||
| 1168 | int cmd_timechart(int argc, const char **argv, const char *prefix __used) | ||
| 1169 | { | ||
| 1170 | symbol__init(0); | ||
| 1171 | |||
| 1172 | argc = parse_options(argc, argv, options, timechart_usage, | ||
| 1173 | PARSE_OPT_STOP_AT_NON_OPTION); | ||
| 1174 | |||
| 1175 | if (argc && !strncmp(argv[0], "rec", 3)) | ||
| 1176 | return __cmd_record(argc, argv); | ||
| 1177 | else if (argc) | ||
| 1178 | usage_with_options(timechart_usage, options); | ||
| 1179 | |||
| 1180 | setup_pager(); | ||
| 1181 | |||
| 1182 | return __cmd_timechart(); | ||
| 1183 | } | ||
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 4002ccb36750..e0a374d0e43a 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c | |||
| @@ -22,6 +22,7 @@ | |||
| 22 | 22 | ||
| 23 | #include "util/symbol.h" | 23 | #include "util/symbol.h" |
| 24 | #include "util/color.h" | 24 | #include "util/color.h" |
| 25 | #include "util/thread.h" | ||
| 25 | #include "util/util.h" | 26 | #include "util/util.h" |
| 26 | #include <linux/rbtree.h> | 27 | #include <linux/rbtree.h> |
| 27 | #include "util/parse-options.h" | 28 | #include "util/parse-options.h" |
| @@ -54,26 +55,31 @@ | |||
| 54 | 55 | ||
| 55 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; | 56 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 56 | 57 | ||
| 57 | static int system_wide = 0; | 58 | static int system_wide = 0; |
| 58 | 59 | ||
| 59 | static int default_interval = 100000; | 60 | static int default_interval = 0; |
| 60 | 61 | ||
| 61 | static int count_filter = 5; | 62 | static int count_filter = 5; |
| 62 | static int print_entries = 15; | 63 | static int print_entries; |
| 63 | 64 | ||
| 64 | static int target_pid = -1; | 65 | static int target_pid = -1; |
| 65 | static int inherit = 0; | 66 | static int inherit = 0; |
| 66 | static int profile_cpu = -1; | 67 | static int profile_cpu = -1; |
| 67 | static int nr_cpus = 0; | 68 | static int nr_cpus = 0; |
| 68 | static unsigned int realtime_prio = 0; | 69 | static unsigned int realtime_prio = 0; |
| 69 | static int group = 0; | 70 | static int group = 0; |
| 70 | static unsigned int page_size; | 71 | static unsigned int page_size; |
| 71 | static unsigned int mmap_pages = 16; | 72 | static unsigned int mmap_pages = 16; |
| 72 | static int freq = 0; | 73 | static int freq = 1000; /* 1 KHz */ |
| 73 | 74 | ||
| 74 | static int delay_secs = 2; | 75 | static int delay_secs = 2; |
| 75 | static int zero; | 76 | static int zero = 0; |
| 76 | static int dump_symtab; | 77 | static int dump_symtab = 0; |
| 78 | |||
| 79 | static bool hide_kernel_symbols = false; | ||
| 80 | static bool hide_user_symbols = false; | ||
| 81 | static struct winsize winsize; | ||
| 82 | struct symbol_conf symbol_conf; | ||
| 77 | 83 | ||
| 78 | /* | 84 | /* |
| 79 | * Source | 85 | * Source |
| @@ -86,83 +92,126 @@ struct source_line { | |||
| 86 | struct source_line *next; | 92 | struct source_line *next; |
| 87 | }; | 93 | }; |
| 88 | 94 | ||
| 89 | static char *sym_filter = NULL; | 95 | static char *sym_filter = NULL; |
| 90 | struct sym_entry *sym_filter_entry = NULL; | 96 | struct sym_entry *sym_filter_entry = NULL; |
| 91 | static int sym_pcnt_filter = 5; | 97 | static int sym_pcnt_filter = 5; |
| 92 | static int sym_counter = 0; | 98 | static int sym_counter = 0; |
| 93 | static int display_weighted = -1; | 99 | static int display_weighted = -1; |
| 94 | 100 | ||
| 95 | /* | 101 | /* |
| 96 | * Symbols | 102 | * Symbols |
| 97 | */ | 103 | */ |
| 98 | 104 | ||
| 99 | static u64 min_ip; | 105 | struct sym_entry_source { |
| 100 | static u64 max_ip = -1ll; | 106 | struct source_line *source; |
| 107 | struct source_line *lines; | ||
| 108 | struct source_line **lines_tail; | ||
| 109 | pthread_mutex_t lock; | ||
| 110 | }; | ||
| 101 | 111 | ||
| 102 | struct sym_entry { | 112 | struct sym_entry { |
| 103 | struct rb_node rb_node; | 113 | struct rb_node rb_node; |
| 104 | struct list_head node; | 114 | struct list_head node; |
| 105 | unsigned long count[MAX_COUNTERS]; | ||
| 106 | unsigned long snap_count; | 115 | unsigned long snap_count; |
| 107 | double weight; | 116 | double weight; |
| 108 | int skip; | 117 | int skip; |
| 109 | struct source_line *source; | 118 | u16 name_len; |
| 110 | struct source_line *lines; | 119 | u8 origin; |
| 111 | struct source_line **lines_tail; | 120 | struct map *map; |
| 112 | pthread_mutex_t source_lock; | 121 | struct sym_entry_source *src; |
| 122 | unsigned long count[0]; | ||
| 113 | }; | 123 | }; |
| 114 | 124 | ||
| 115 | /* | 125 | /* |
| 116 | * Source functions | 126 | * Source functions |
| 117 | */ | 127 | */ |
| 118 | 128 | ||
| 129 | static inline struct symbol *sym_entry__symbol(struct sym_entry *self) | ||
| 130 | { | ||
| 131 | return ((void *)self) + symbol_conf.priv_size; | ||
| 132 | } | ||
| 133 | |||
| 134 | static void get_term_dimensions(struct winsize *ws) | ||
| 135 | { | ||
| 136 | char *s = getenv("LINES"); | ||
| 137 | |||
| 138 | if (s != NULL) { | ||
| 139 | ws->ws_row = atoi(s); | ||
| 140 | s = getenv("COLUMNS"); | ||
| 141 | if (s != NULL) { | ||
| 142 | ws->ws_col = atoi(s); | ||
| 143 | if (ws->ws_row && ws->ws_col) | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | #ifdef TIOCGWINSZ | ||
| 148 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && | ||
| 149 | ws->ws_row && ws->ws_col) | ||
| 150 | return; | ||
| 151 | #endif | ||
| 152 | ws->ws_row = 25; | ||
| 153 | ws->ws_col = 80; | ||
| 154 | } | ||
| 155 | |||
| 156 | static void update_print_entries(struct winsize *ws) | ||
| 157 | { | ||
| 158 | print_entries = ws->ws_row; | ||
| 159 | |||
| 160 | if (print_entries > 9) | ||
| 161 | print_entries -= 9; | ||
| 162 | } | ||
| 163 | |||
| 164 | static void sig_winch_handler(int sig __used) | ||
| 165 | { | ||
| 166 | get_term_dimensions(&winsize); | ||
| 167 | update_print_entries(&winsize); | ||
| 168 | } | ||
| 169 | |||
| 119 | static void parse_source(struct sym_entry *syme) | 170 | static void parse_source(struct sym_entry *syme) |
| 120 | { | 171 | { |
| 121 | struct symbol *sym; | 172 | struct symbol *sym; |
| 122 | struct module *module; | 173 | struct sym_entry_source *source; |
| 123 | struct section *section = NULL; | 174 | struct map *map; |
| 124 | FILE *file; | 175 | FILE *file; |
| 125 | char command[PATH_MAX*2]; | 176 | char command[PATH_MAX*2]; |
| 126 | const char *path = vmlinux_name; | 177 | const char *path; |
| 127 | u64 start, end, len; | 178 | u64 len; |
| 128 | 179 | ||
| 129 | if (!syme) | 180 | if (!syme) |
| 130 | return; | 181 | return; |
| 131 | 182 | ||
| 132 | if (syme->lines) { | 183 | if (syme->src == NULL) { |
| 133 | pthread_mutex_lock(&syme->source_lock); | 184 | syme->src = zalloc(sizeof(*source)); |
| 134 | goto out_assign; | 185 | if (syme->src == NULL) |
| 186 | return; | ||
| 187 | pthread_mutex_init(&syme->src->lock, NULL); | ||
| 135 | } | 188 | } |
| 136 | 189 | ||
| 137 | sym = (struct symbol *)(syme + 1); | 190 | source = syme->src; |
| 138 | module = sym->module; | ||
| 139 | |||
| 140 | if (module) | ||
| 141 | path = module->path; | ||
| 142 | if (!path) | ||
| 143 | return; | ||
| 144 | |||
| 145 | start = sym->obj_start; | ||
| 146 | if (!start) | ||
| 147 | start = sym->start; | ||
| 148 | 191 | ||
| 149 | if (module) { | 192 | if (source->lines) { |
| 150 | section = module->sections->find_section(module->sections, ".text"); | 193 | pthread_mutex_lock(&source->lock); |
| 151 | if (section) | 194 | goto out_assign; |
| 152 | start -= section->vma; | ||
| 153 | } | 195 | } |
| 154 | 196 | ||
| 155 | end = start + sym->end - sym->start + 1; | 197 | sym = sym_entry__symbol(syme); |
| 198 | map = syme->map; | ||
| 199 | path = map->dso->long_name; | ||
| 200 | |||
| 156 | len = sym->end - sym->start; | 201 | len = sym->end - sym->start; |
| 157 | 202 | ||
| 158 | sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", start, end, path); | 203 | sprintf(command, |
| 204 | "objdump --start-address=0x%016Lx " | ||
| 205 | "--stop-address=0x%016Lx -dS %s", | ||
| 206 | map->unmap_ip(map, sym->start), | ||
| 207 | map->unmap_ip(map, sym->end), path); | ||
| 159 | 208 | ||
| 160 | file = popen(command, "r"); | 209 | file = popen(command, "r"); |
| 161 | if (!file) | 210 | if (!file) |
| 162 | return; | 211 | return; |
| 163 | 212 | ||
| 164 | pthread_mutex_lock(&syme->source_lock); | 213 | pthread_mutex_lock(&source->lock); |
| 165 | syme->lines_tail = &syme->lines; | 214 | source->lines_tail = &source->lines; |
| 166 | while (!feof(file)) { | 215 | while (!feof(file)) { |
| 167 | struct source_line *src; | 216 | struct source_line *src; |
| 168 | size_t dummy = 0; | 217 | size_t dummy = 0; |
| @@ -182,24 +231,22 @@ static void parse_source(struct sym_entry *syme) | |||
| 182 | *c = 0; | 231 | *c = 0; |
| 183 | 232 | ||
| 184 | src->next = NULL; | 233 | src->next = NULL; |
| 185 | *syme->lines_tail = src; | 234 | *source->lines_tail = src; |
| 186 | syme->lines_tail = &src->next; | 235 | source->lines_tail = &src->next; |
| 187 | 236 | ||
| 188 | if (strlen(src->line)>8 && src->line[8] == ':') { | 237 | if (strlen(src->line)>8 && src->line[8] == ':') { |
| 189 | src->eip = strtoull(src->line, NULL, 16); | 238 | src->eip = strtoull(src->line, NULL, 16); |
| 190 | if (section) | 239 | src->eip = map->unmap_ip(map, src->eip); |
| 191 | src->eip += section->vma; | ||
| 192 | } | 240 | } |
| 193 | if (strlen(src->line)>8 && src->line[16] == ':') { | 241 | if (strlen(src->line)>8 && src->line[16] == ':') { |
| 194 | src->eip = strtoull(src->line, NULL, 16); | 242 | src->eip = strtoull(src->line, NULL, 16); |
| 195 | if (section) | 243 | src->eip = map->unmap_ip(map, src->eip); |
| 196 | src->eip += section->vma; | ||
| 197 | } | 244 | } |
| 198 | } | 245 | } |
| 199 | pclose(file); | 246 | pclose(file); |
| 200 | out_assign: | 247 | out_assign: |
| 201 | sym_filter_entry = syme; | 248 | sym_filter_entry = syme; |
| 202 | pthread_mutex_unlock(&syme->source_lock); | 249 | pthread_mutex_unlock(&source->lock); |
| 203 | } | 250 | } |
| 204 | 251 | ||
| 205 | static void __zero_source_counters(struct sym_entry *syme) | 252 | static void __zero_source_counters(struct sym_entry *syme) |
| @@ -207,7 +254,7 @@ static void __zero_source_counters(struct sym_entry *syme) | |||
| 207 | int i; | 254 | int i; |
| 208 | struct source_line *line; | 255 | struct source_line *line; |
| 209 | 256 | ||
| 210 | line = syme->lines; | 257 | line = syme->src->lines; |
| 211 | while (line) { | 258 | while (line) { |
| 212 | for (i = 0; i < nr_counters; i++) | 259 | for (i = 0; i < nr_counters; i++) |
| 213 | line->count[i] = 0; | 260 | line->count[i] = 0; |
| @@ -222,13 +269,13 @@ static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) | |||
| 222 | if (syme != sym_filter_entry) | 269 | if (syme != sym_filter_entry) |
| 223 | return; | 270 | return; |
| 224 | 271 | ||
| 225 | if (pthread_mutex_trylock(&syme->source_lock)) | 272 | if (pthread_mutex_trylock(&syme->src->lock)) |
| 226 | return; | 273 | return; |
| 227 | 274 | ||
| 228 | if (!syme->source) | 275 | if (syme->src == NULL || syme->src->source == NULL) |
| 229 | goto out_unlock; | 276 | goto out_unlock; |
| 230 | 277 | ||
| 231 | for (line = syme->lines; line; line = line->next) { | 278 | for (line = syme->src->lines; line; line = line->next) { |
| 232 | if (line->eip == ip) { | 279 | if (line->eip == ip) { |
| 233 | line->count[counter]++; | 280 | line->count[counter]++; |
| 234 | break; | 281 | break; |
| @@ -237,32 +284,25 @@ static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) | |||
| 237 | break; | 284 | break; |
| 238 | } | 285 | } |
| 239 | out_unlock: | 286 | out_unlock: |
| 240 | pthread_mutex_unlock(&syme->source_lock); | 287 | pthread_mutex_unlock(&syme->src->lock); |
| 241 | } | 288 | } |
| 242 | 289 | ||
| 243 | static void lookup_sym_source(struct sym_entry *syme) | 290 | static void lookup_sym_source(struct sym_entry *syme) |
| 244 | { | 291 | { |
| 245 | struct symbol *symbol = (struct symbol *)(syme + 1); | 292 | struct symbol *symbol = sym_entry__symbol(syme); |
| 246 | struct source_line *line; | 293 | struct source_line *line; |
| 247 | char pattern[PATH_MAX]; | 294 | char pattern[PATH_MAX]; |
| 248 | char *idx; | ||
| 249 | 295 | ||
| 250 | sprintf(pattern, "<%s>:", symbol->name); | 296 | sprintf(pattern, "<%s>:", symbol->name); |
| 251 | 297 | ||
| 252 | if (symbol->module) { | 298 | pthread_mutex_lock(&syme->src->lock); |
| 253 | idx = strstr(pattern, "\t"); | 299 | for (line = syme->src->lines; line; line = line->next) { |
| 254 | if (idx) | ||
| 255 | *idx = 0; | ||
| 256 | } | ||
| 257 | |||
| 258 | pthread_mutex_lock(&syme->source_lock); | ||
| 259 | for (line = syme->lines; line; line = line->next) { | ||
| 260 | if (strstr(line->line, pattern)) { | 300 | if (strstr(line->line, pattern)) { |
| 261 | syme->source = line; | 301 | syme->src->source = line; |
| 262 | break; | 302 | break; |
| 263 | } | 303 | } |
| 264 | } | 304 | } |
| 265 | pthread_mutex_unlock(&syme->source_lock); | 305 | pthread_mutex_unlock(&syme->src->lock); |
| 266 | } | 306 | } |
| 267 | 307 | ||
| 268 | static void show_lines(struct source_line *queue, int count, int total) | 308 | static void show_lines(struct source_line *queue, int count, int total) |
| @@ -292,24 +332,24 @@ static void show_details(struct sym_entry *syme) | |||
| 292 | if (!syme) | 332 | if (!syme) |
| 293 | return; | 333 | return; |
| 294 | 334 | ||
| 295 | if (!syme->source) | 335 | if (!syme->src->source) |
| 296 | lookup_sym_source(syme); | 336 | lookup_sym_source(syme); |
| 297 | 337 | ||
| 298 | if (!syme->source) | 338 | if (!syme->src->source) |
| 299 | return; | 339 | return; |
| 300 | 340 | ||
| 301 | symbol = (struct symbol *)(syme + 1); | 341 | symbol = sym_entry__symbol(syme); |
| 302 | printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); | 342 | printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); |
| 303 | printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); | 343 | printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); |
| 304 | 344 | ||
| 305 | pthread_mutex_lock(&syme->source_lock); | 345 | pthread_mutex_lock(&syme->src->lock); |
| 306 | line = syme->source; | 346 | line = syme->src->source; |
| 307 | while (line) { | 347 | while (line) { |
| 308 | total += line->count[sym_counter]; | 348 | total += line->count[sym_counter]; |
| 309 | line = line->next; | 349 | line = line->next; |
| 310 | } | 350 | } |
| 311 | 351 | ||
| 312 | line = syme->source; | 352 | line = syme->src->source; |
| 313 | while (line) { | 353 | while (line) { |
| 314 | float pcnt = 0.0; | 354 | float pcnt = 0.0; |
| 315 | 355 | ||
| @@ -334,13 +374,13 @@ static void show_details(struct sym_entry *syme) | |||
| 334 | line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; | 374 | line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; |
| 335 | line = line->next; | 375 | line = line->next; |
| 336 | } | 376 | } |
| 337 | pthread_mutex_unlock(&syme->source_lock); | 377 | pthread_mutex_unlock(&syme->src->lock); |
| 338 | if (more) | 378 | if (more) |
| 339 | printf("%d lines not displayed, maybe increase display entries [e]\n", more); | 379 | printf("%d lines not displayed, maybe increase display entries [e]\n", more); |
| 340 | } | 380 | } |
| 341 | 381 | ||
| 342 | /* | 382 | /* |
| 343 | * Symbols will be added here in record_ip and will get out | 383 | * Symbols will be added here in event__process_sample and will get out |
| 344 | * after decayed. | 384 | * after decayed. |
| 345 | */ | 385 | */ |
| 346 | static LIST_HEAD(active_symbols); | 386 | static LIST_HEAD(active_symbols); |
| @@ -411,6 +451,8 @@ static void print_sym_table(void) | |||
| 411 | struct sym_entry *syme, *n; | 451 | struct sym_entry *syme, *n; |
| 412 | struct rb_root tmp = RB_ROOT; | 452 | struct rb_root tmp = RB_ROOT; |
| 413 | struct rb_node *nd; | 453 | struct rb_node *nd; |
| 454 | int sym_width = 0, dso_width = 0, max_dso_width; | ||
| 455 | const int win_width = winsize.ws_col - 1; | ||
| 414 | 456 | ||
| 415 | samples = userspace_samples = 0; | 457 | samples = userspace_samples = 0; |
| 416 | 458 | ||
| @@ -422,6 +464,14 @@ static void print_sym_table(void) | |||
| 422 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { | 464 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { |
| 423 | syme->snap_count = syme->count[snap]; | 465 | syme->snap_count = syme->count[snap]; |
| 424 | if (syme->snap_count != 0) { | 466 | if (syme->snap_count != 0) { |
| 467 | |||
| 468 | if ((hide_user_symbols && | ||
| 469 | syme->origin == PERF_RECORD_MISC_USER) || | ||
| 470 | (hide_kernel_symbols && | ||
| 471 | syme->origin == PERF_RECORD_MISC_KERNEL)) { | ||
| 472 | list_remove_active_sym(syme); | ||
| 473 | continue; | ||
| 474 | } | ||
| 425 | syme->weight = sym_weight(syme); | 475 | syme->weight = sym_weight(syme); |
| 426 | rb_insert_active_sym(&tmp, syme); | 476 | rb_insert_active_sym(&tmp, syme); |
| 427 | sum_ksamples += syme->snap_count; | 477 | sum_ksamples += syme->snap_count; |
| @@ -434,8 +484,7 @@ static void print_sym_table(void) | |||
| 434 | 484 | ||
| 435 | puts(CONSOLE_CLEAR); | 485 | puts(CONSOLE_CLEAR); |
| 436 | 486 | ||
| 437 | printf( | 487 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
| 438 | "------------------------------------------------------------------------------\n"); | ||
| 439 | printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [", | 488 | printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [", |
| 440 | samples_per_sec, | 489 | samples_per_sec, |
| 441 | 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec))); | 490 | 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec))); |
| @@ -473,33 +522,57 @@ static void print_sym_table(void) | |||
| 473 | printf(", %d CPUs)\n", nr_cpus); | 522 | printf(", %d CPUs)\n", nr_cpus); |
| 474 | } | 523 | } |
| 475 | 524 | ||
| 476 | printf("------------------------------------------------------------------------------\n\n"); | 525 | printf("%-*.*s\n", win_width, win_width, graph_dotted_line); |
| 477 | 526 | ||
| 478 | if (sym_filter_entry) { | 527 | if (sym_filter_entry) { |
| 479 | show_details(sym_filter_entry); | 528 | show_details(sym_filter_entry); |
| 480 | return; | 529 | return; |
| 481 | } | 530 | } |
| 482 | 531 | ||
| 532 | /* | ||
| 533 | * Find the longest symbol name that will be displayed | ||
| 534 | */ | ||
| 535 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { | ||
| 536 | syme = rb_entry(nd, struct sym_entry, rb_node); | ||
| 537 | if (++printed > print_entries || | ||
| 538 | (int)syme->snap_count < count_filter) | ||
| 539 | continue; | ||
| 540 | |||
| 541 | if (syme->map->dso->long_name_len > dso_width) | ||
| 542 | dso_width = syme->map->dso->long_name_len; | ||
| 543 | |||
| 544 | if (syme->name_len > sym_width) | ||
| 545 | sym_width = syme->name_len; | ||
| 546 | } | ||
| 547 | |||
| 548 | printed = 0; | ||
| 549 | |||
| 550 | max_dso_width = winsize.ws_col - sym_width - 29; | ||
| 551 | if (dso_width > max_dso_width) | ||
| 552 | dso_width = max_dso_width; | ||
| 553 | putchar('\n'); | ||
| 483 | if (nr_counters == 1) | 554 | if (nr_counters == 1) |
| 484 | printf(" samples pcnt"); | 555 | printf(" samples pcnt"); |
| 485 | else | 556 | else |
| 486 | printf(" weight samples pcnt"); | 557 | printf(" weight samples pcnt"); |
| 487 | 558 | ||
| 488 | if (verbose) | 559 | if (verbose) |
| 489 | printf(" RIP "); | 560 | printf(" RIP "); |
| 490 | printf(" kernel function\n"); | 561 | printf(" %-*.*s DSO\n", sym_width, sym_width, "function"); |
| 491 | printf(" %s _______ _____", | 562 | printf(" %s _______ _____", |
| 492 | nr_counters == 1 ? " " : "______"); | 563 | nr_counters == 1 ? " " : "______"); |
| 493 | if (verbose) | 564 | if (verbose) |
| 494 | printf(" ________________"); | 565 | printf(" ________________"); |
| 495 | printf(" _______________\n\n"); | 566 | printf(" %-*.*s", sym_width, sym_width, graph_line); |
| 567 | printf(" %-*.*s", dso_width, dso_width, graph_line); | ||
| 568 | puts("\n"); | ||
| 496 | 569 | ||
| 497 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { | 570 | for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { |
| 498 | struct symbol *sym; | 571 | struct symbol *sym; |
| 499 | double pcnt; | 572 | double pcnt; |
| 500 | 573 | ||
| 501 | syme = rb_entry(nd, struct sym_entry, rb_node); | 574 | syme = rb_entry(nd, struct sym_entry, rb_node); |
| 502 | sym = (struct symbol *)(syme + 1); | 575 | sym = sym_entry__symbol(syme); |
| 503 | 576 | ||
| 504 | if (++printed > print_entries || (int)syme->snap_count < count_filter) | 577 | if (++printed > print_entries || (int)syme->snap_count < count_filter) |
| 505 | continue; | 578 | continue; |
| @@ -508,17 +581,18 @@ static void print_sym_table(void) | |||
| 508 | sum_ksamples)); | 581 | sum_ksamples)); |
| 509 | 582 | ||
| 510 | if (nr_counters == 1 || !display_weighted) | 583 | if (nr_counters == 1 || !display_weighted) |
| 511 | printf("%20.2f - ", syme->weight); | 584 | printf("%20.2f ", syme->weight); |
| 512 | else | 585 | else |
| 513 | printf("%9.1f %10ld - ", syme->weight, syme->snap_count); | 586 | printf("%9.1f %10ld ", syme->weight, syme->snap_count); |
| 514 | 587 | ||
| 515 | percent_color_fprintf(stdout, "%4.1f%%", pcnt); | 588 | percent_color_fprintf(stdout, "%4.1f%%", pcnt); |
| 516 | if (verbose) | 589 | if (verbose) |
| 517 | printf(" - %016llx", sym->start); | 590 | printf(" %016llx", sym->start); |
| 518 | printf(" : %s", sym->name); | 591 | printf(" %-*.*s", sym_width, sym_width, sym->name); |
| 519 | if (sym->module) | 592 | printf(" %-*.*s\n", dso_width, dso_width, |
| 520 | printf("\t[%s]", sym->module->name); | 593 | dso_width >= syme->map->dso->long_name_len ? |
| 521 | printf("\n"); | 594 | syme->map->dso->long_name : |
| 595 | syme->map->dso->short_name); | ||
| 522 | } | 596 | } |
| 523 | } | 597 | } |
| 524 | 598 | ||
| @@ -565,10 +639,10 @@ static void prompt_symbol(struct sym_entry **target, const char *msg) | |||
| 565 | 639 | ||
| 566 | /* zero counters of active symbol */ | 640 | /* zero counters of active symbol */ |
| 567 | if (syme) { | 641 | if (syme) { |
| 568 | pthread_mutex_lock(&syme->source_lock); | 642 | pthread_mutex_lock(&syme->src->lock); |
| 569 | __zero_source_counters(syme); | 643 | __zero_source_counters(syme); |
| 570 | *target = NULL; | 644 | *target = NULL; |
| 571 | pthread_mutex_unlock(&syme->source_lock); | 645 | pthread_mutex_unlock(&syme->src->lock); |
| 572 | } | 646 | } |
| 573 | 647 | ||
| 574 | fprintf(stdout, "\n%s: ", msg); | 648 | fprintf(stdout, "\n%s: ", msg); |
| @@ -584,7 +658,7 @@ static void prompt_symbol(struct sym_entry **target, const char *msg) | |||
| 584 | pthread_mutex_unlock(&active_symbols_lock); | 658 | pthread_mutex_unlock(&active_symbols_lock); |
| 585 | 659 | ||
| 586 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { | 660 | list_for_each_entry_safe_from(syme, n, &active_symbols, node) { |
| 587 | struct symbol *sym = (struct symbol *)(syme + 1); | 661 | struct symbol *sym = sym_entry__symbol(syme); |
| 588 | 662 | ||
| 589 | if (!strcmp(buf, sym->name)) { | 663 | if (!strcmp(buf, sym->name)) { |
| 590 | found = syme; | 664 | found = syme; |
| @@ -608,7 +682,7 @@ static void print_mapped_keys(void) | |||
| 608 | char *name = NULL; | 682 | char *name = NULL; |
| 609 | 683 | ||
| 610 | if (sym_filter_entry) { | 684 | if (sym_filter_entry) { |
| 611 | struct symbol *sym = (struct symbol *)(sym_filter_entry+1); | 685 | struct symbol *sym = sym_entry__symbol(sym_filter_entry); |
| 612 | name = sym->name; | 686 | name = sym->name; |
| 613 | } | 687 | } |
| 614 | 688 | ||
| @@ -621,7 +695,7 @@ static void print_mapped_keys(void) | |||
| 621 | 695 | ||
| 622 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); | 696 | fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); |
| 623 | 697 | ||
| 624 | if (vmlinux_name) { | 698 | if (symbol_conf.vmlinux_name) { |
| 625 | fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); | 699 | fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); |
| 626 | fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); | 700 | fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); |
| 627 | fprintf(stdout, "\t[S] stop annotation.\n"); | 701 | fprintf(stdout, "\t[S] stop annotation.\n"); |
| @@ -630,6 +704,12 @@ static void print_mapped_keys(void) | |||
| 630 | if (nr_counters > 1) | 704 | if (nr_counters > 1) |
| 631 | fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0); | 705 | fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0); |
| 632 | 706 | ||
| 707 | fprintf(stdout, | ||
| 708 | "\t[K] hide kernel_symbols symbols. \t(%s)\n", | ||
| 709 | hide_kernel_symbols ? "yes" : "no"); | ||
| 710 | fprintf(stdout, | ||
| 711 | "\t[U] hide user symbols. \t(%s)\n", | ||
| 712 | hide_user_symbols ? "yes" : "no"); | ||
| 633 | fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0); | 713 | fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0); |
| 634 | fprintf(stdout, "\t[qQ] quit.\n"); | 714 | fprintf(stdout, "\t[qQ] quit.\n"); |
| 635 | } | 715 | } |
| @@ -643,6 +723,8 @@ static int key_mapped(int c) | |||
| 643 | case 'z': | 723 | case 'z': |
| 644 | case 'q': | 724 | case 'q': |
| 645 | case 'Q': | 725 | case 'Q': |
| 726 | case 'K': | ||
| 727 | case 'U': | ||
| 646 | return 1; | 728 | return 1; |
| 647 | case 'E': | 729 | case 'E': |
| 648 | case 'w': | 730 | case 'w': |
| @@ -650,7 +732,7 @@ static int key_mapped(int c) | |||
| 650 | case 'F': | 732 | case 'F': |
| 651 | case 's': | 733 | case 's': |
| 652 | case 'S': | 734 | case 'S': |
| 653 | return vmlinux_name ? 1 : 0; | 735 | return symbol_conf.vmlinux_name ? 1 : 0; |
| 654 | default: | 736 | default: |
| 655 | break; | 737 | break; |
| 656 | } | 738 | } |
| @@ -686,9 +768,16 @@ static void handle_keypress(int c) | |||
| 686 | switch (c) { | 768 | switch (c) { |
| 687 | case 'd': | 769 | case 'd': |
| 688 | prompt_integer(&delay_secs, "Enter display delay"); | 770 | prompt_integer(&delay_secs, "Enter display delay"); |
| 771 | if (delay_secs < 1) | ||
| 772 | delay_secs = 1; | ||
| 689 | break; | 773 | break; |
| 690 | case 'e': | 774 | case 'e': |
| 691 | prompt_integer(&print_entries, "Enter display entries (lines)"); | 775 | prompt_integer(&print_entries, "Enter display entries (lines)"); |
| 776 | if (print_entries == 0) { | ||
| 777 | sig_winch_handler(SIGWINCH); | ||
| 778 | signal(SIGWINCH, sig_winch_handler); | ||
| 779 | } else | ||
| 780 | signal(SIGWINCH, SIG_DFL); | ||
| 692 | break; | 781 | break; |
| 693 | case 'E': | 782 | case 'E': |
| 694 | if (nr_counters > 1) { | 783 | if (nr_counters > 1) { |
| @@ -713,9 +802,14 @@ static void handle_keypress(int c) | |||
| 713 | case 'F': | 802 | case 'F': |
| 714 | prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); | 803 | prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); |
| 715 | break; | 804 | break; |
| 805 | case 'K': | ||
| 806 | hide_kernel_symbols = !hide_kernel_symbols; | ||
| 807 | break; | ||
| 716 | case 'q': | 808 | case 'q': |
| 717 | case 'Q': | 809 | case 'Q': |
| 718 | printf("exiting.\n"); | 810 | printf("exiting.\n"); |
| 811 | if (dump_symtab) | ||
| 812 | dsos__fprintf(stderr); | ||
| 719 | exit(0); | 813 | exit(0); |
| 720 | case 's': | 814 | case 's': |
| 721 | prompt_symbol(&sym_filter_entry, "Enter details symbol"); | 815 | prompt_symbol(&sym_filter_entry, "Enter details symbol"); |
| @@ -726,12 +820,15 @@ static void handle_keypress(int c) | |||
| 726 | else { | 820 | else { |
| 727 | struct sym_entry *syme = sym_filter_entry; | 821 | struct sym_entry *syme = sym_filter_entry; |
| 728 | 822 | ||
| 729 | pthread_mutex_lock(&syme->source_lock); | 823 | pthread_mutex_lock(&syme->src->lock); |
| 730 | sym_filter_entry = NULL; | 824 | sym_filter_entry = NULL; |
| 731 | __zero_source_counters(syme); | 825 | __zero_source_counters(syme); |
| 732 | pthread_mutex_unlock(&syme->source_lock); | 826 | pthread_mutex_unlock(&syme->src->lock); |
| 733 | } | 827 | } |
| 734 | break; | 828 | break; |
| 829 | case 'U': | ||
| 830 | hide_user_symbols = !hide_user_symbols; | ||
| 831 | break; | ||
| 735 | case 'w': | 832 | case 'w': |
| 736 | display_weighted = ~display_weighted; | 833 | display_weighted = ~display_weighted; |
| 737 | break; | 834 | break; |
| @@ -782,12 +879,13 @@ static const char *skip_symbols[] = { | |||
| 782 | "exit_idle", | 879 | "exit_idle", |
| 783 | "mwait_idle", | 880 | "mwait_idle", |
| 784 | "mwait_idle_with_hints", | 881 | "mwait_idle_with_hints", |
| 882 | "poll_idle", | ||
| 785 | "ppc64_runlatch_off", | 883 | "ppc64_runlatch_off", |
| 786 | "pseries_dedicated_idle_sleep", | 884 | "pseries_dedicated_idle_sleep", |
| 787 | NULL | 885 | NULL |
| 788 | }; | 886 | }; |
| 789 | 887 | ||
| 790 | static int symbol_filter(struct dso *self, struct symbol *sym) | 888 | static int symbol_filter(struct map *map, struct symbol *sym) |
| 791 | { | 889 | { |
| 792 | struct sym_entry *syme; | 890 | struct sym_entry *syme; |
| 793 | const char *name = sym->name; | 891 | const char *name = sym->name; |
| @@ -809,8 +907,9 @@ static int symbol_filter(struct dso *self, struct symbol *sym) | |||
| 809 | strstr(name, "_text_end")) | 907 | strstr(name, "_text_end")) |
| 810 | return 1; | 908 | return 1; |
| 811 | 909 | ||
| 812 | syme = dso__sym_priv(self, sym); | 910 | syme = symbol__priv(sym); |
| 813 | pthread_mutex_init(&syme->source_lock, NULL); | 911 | syme->map = map; |
| 912 | syme->src = NULL; | ||
| 814 | if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) | 913 | if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) |
| 815 | sym_filter_entry = syme; | 914 | sym_filter_entry = syme; |
| 816 | 915 | ||
| @@ -821,75 +920,65 @@ static int symbol_filter(struct dso *self, struct symbol *sym) | |||
| 821 | } | 920 | } |
| 822 | } | 921 | } |
| 823 | 922 | ||
| 824 | return 0; | 923 | if (!syme->skip) |
| 825 | } | 924 | syme->name_len = strlen(sym->name); |
| 826 | |||
| 827 | static int parse_symbols(void) | ||
| 828 | { | ||
| 829 | struct rb_node *node; | ||
| 830 | struct symbol *sym; | ||
| 831 | int use_modules = vmlinux_name ? 1 : 0; | ||
| 832 | |||
| 833 | kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry)); | ||
| 834 | if (kernel_dso == NULL) | ||
| 835 | return -1; | ||
| 836 | |||
| 837 | if (dso__load_kernel(kernel_dso, vmlinux_name, symbol_filter, verbose, use_modules) <= 0) | ||
| 838 | goto out_delete_dso; | ||
| 839 | |||
| 840 | node = rb_first(&kernel_dso->syms); | ||
| 841 | sym = rb_entry(node, struct symbol, rb_node); | ||
| 842 | min_ip = sym->start; | ||
| 843 | |||
| 844 | node = rb_last(&kernel_dso->syms); | ||
| 845 | sym = rb_entry(node, struct symbol, rb_node); | ||
| 846 | max_ip = sym->end; | ||
| 847 | |||
| 848 | if (dump_symtab) | ||
| 849 | dso__fprintf(kernel_dso, stderr); | ||
| 850 | 925 | ||
| 851 | return 0; | 926 | return 0; |
| 852 | |||
| 853 | out_delete_dso: | ||
| 854 | dso__delete(kernel_dso); | ||
| 855 | kernel_dso = NULL; | ||
| 856 | return -1; | ||
| 857 | } | 927 | } |
| 858 | 928 | ||
| 859 | /* | 929 | static void event__process_sample(const event_t *self, int counter) |
| 860 | * Binary search in the histogram table and record the hit: | ||
| 861 | */ | ||
| 862 | static void record_ip(u64 ip, int counter) | ||
| 863 | { | 930 | { |
| 864 | struct symbol *sym = dso__find_symbol(kernel_dso, ip); | 931 | u64 ip = self->ip.ip; |
| 865 | 932 | struct sym_entry *syme; | |
| 866 | if (sym != NULL) { | 933 | struct addr_location al; |
| 867 | struct sym_entry *syme = dso__sym_priv(kernel_dso, sym); | 934 | u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 868 | 935 | ||
| 869 | if (!syme->skip) { | 936 | switch (origin) { |
| 870 | syme->count[counter]++; | 937 | case PERF_RECORD_MISC_USER: |
| 871 | record_precise_ip(syme, counter, ip); | 938 | if (hide_user_symbols) |
| 872 | pthread_mutex_lock(&active_symbols_lock); | ||
| 873 | if (list_empty(&syme->node) || !syme->node.next) | ||
| 874 | __list_insert_active_sym(syme); | ||
| 875 | pthread_mutex_unlock(&active_symbols_lock); | ||
| 876 | return; | 939 | return; |
| 877 | } | 940 | break; |
| 941 | case PERF_RECORD_MISC_KERNEL: | ||
| 942 | if (hide_kernel_symbols) | ||
| 943 | return; | ||
| 944 | break; | ||
| 945 | default: | ||
| 946 | return; | ||
| 878 | } | 947 | } |
| 879 | 948 | ||
| 880 | samples--; | 949 | if (event__preprocess_sample(self, &al, symbol_filter) < 0 || |
| 950 | al.sym == NULL) | ||
| 951 | return; | ||
| 952 | |||
| 953 | syme = symbol__priv(al.sym); | ||
| 954 | if (!syme->skip) { | ||
| 955 | syme->count[counter]++; | ||
| 956 | syme->origin = origin; | ||
| 957 | record_precise_ip(syme, counter, ip); | ||
| 958 | pthread_mutex_lock(&active_symbols_lock); | ||
| 959 | if (list_empty(&syme->node) || !syme->node.next) | ||
| 960 | __list_insert_active_sym(syme); | ||
| 961 | pthread_mutex_unlock(&active_symbols_lock); | ||
| 962 | if (origin == PERF_RECORD_MISC_USER) | ||
| 963 | ++userspace_samples; | ||
| 964 | ++samples; | ||
| 965 | } | ||
| 881 | } | 966 | } |
| 882 | 967 | ||
| 883 | static void process_event(u64 ip, int counter, int user) | 968 | static int event__process(event_t *event) |
| 884 | { | 969 | { |
| 885 | samples++; | 970 | switch (event->header.type) { |
| 886 | 971 | case PERF_RECORD_COMM: | |
| 887 | if (user) { | 972 | event__process_comm(event); |
| 888 | userspace_samples++; | 973 | break; |
| 889 | return; | 974 | case PERF_RECORD_MMAP: |
| 975 | event__process_mmap(event); | ||
| 976 | break; | ||
| 977 | default: | ||
| 978 | break; | ||
| 890 | } | 979 | } |
| 891 | 980 | ||
| 892 | record_ip(ip, counter); | 981 | return 0; |
| 893 | } | 982 | } |
| 894 | 983 | ||
| 895 | struct mmap_data { | 984 | struct mmap_data { |
| @@ -901,7 +990,7 @@ struct mmap_data { | |||
| 901 | 990 | ||
| 902 | static unsigned int mmap_read_head(struct mmap_data *md) | 991 | static unsigned int mmap_read_head(struct mmap_data *md) |
| 903 | { | 992 | { |
| 904 | struct perf_counter_mmap_page *pc = md->base; | 993 | struct perf_event_mmap_page *pc = md->base; |
| 905 | int head; | 994 | int head; |
| 906 | 995 | ||
| 907 | head = pc->data_head; | 996 | head = pc->data_head; |
| @@ -910,8 +999,6 @@ static unsigned int mmap_read_head(struct mmap_data *md) | |||
| 910 | return head; | 999 | return head; |
| 911 | } | 1000 | } |
| 912 | 1001 | ||
| 913 | struct timeval last_read, this_read; | ||
| 914 | |||
| 915 | static void mmap_read_counter(struct mmap_data *md) | 1002 | static void mmap_read_counter(struct mmap_data *md) |
| 916 | { | 1003 | { |
| 917 | unsigned int head = mmap_read_head(md); | 1004 | unsigned int head = mmap_read_head(md); |
| @@ -919,8 +1006,6 @@ static void mmap_read_counter(struct mmap_data *md) | |||
| 919 | unsigned char *data = md->base + page_size; | 1006 | unsigned char *data = md->base + page_size; |
| 920 | int diff; | 1007 | int diff; |
| 921 | 1008 | ||
| 922 | gettimeofday(&this_read, NULL); | ||
| 923 | |||
| 924 | /* | 1009 | /* |
| 925 | * If we're further behind than half the buffer, there's a chance | 1010 | * If we're further behind than half the buffer, there's a chance |
| 926 | * the writer will bite our tail and mess up the samples under us. | 1011 | * the writer will bite our tail and mess up the samples under us. |
| @@ -931,14 +1016,7 @@ static void mmap_read_counter(struct mmap_data *md) | |||
| 931 | */ | 1016 | */ |
| 932 | diff = head - old; | 1017 | diff = head - old; |
| 933 | if (diff > md->mask / 2 || diff < 0) { | 1018 | if (diff > md->mask / 2 || diff < 0) { |
| 934 | struct timeval iv; | 1019 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
| 935 | unsigned long msecs; | ||
| 936 | |||
| 937 | timersub(&this_read, &last_read, &iv); | ||
| 938 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; | ||
| 939 | |||
| 940 | fprintf(stderr, "WARNING: failed to keep up with mmap data." | ||
| 941 | " Last read %lu msecs ago.\n", msecs); | ||
| 942 | 1020 | ||
| 943 | /* | 1021 | /* |
| 944 | * head points to a known good entry, start there. | 1022 | * head points to a known good entry, start there. |
| @@ -946,8 +1024,6 @@ static void mmap_read_counter(struct mmap_data *md) | |||
| 946 | old = head; | 1024 | old = head; |
| 947 | } | 1025 | } |
| 948 | 1026 | ||
| 949 | last_read = this_read; | ||
| 950 | |||
| 951 | for (; old != head;) { | 1027 | for (; old != head;) { |
| 952 | event_t *event = (event_t *)&data[old & md->mask]; | 1028 | event_t *event = (event_t *)&data[old & md->mask]; |
| 953 | 1029 | ||
| @@ -975,13 +1051,11 @@ static void mmap_read_counter(struct mmap_data *md) | |||
| 975 | event = &event_copy; | 1051 | event = &event_copy; |
| 976 | } | 1052 | } |
| 977 | 1053 | ||
| 1054 | if (event->header.type == PERF_RECORD_SAMPLE) | ||
| 1055 | event__process_sample(event, md->counter); | ||
| 1056 | else | ||
| 1057 | event__process(event); | ||
| 978 | old += size; | 1058 | old += size; |
| 979 | |||
| 980 | if (event->header.type == PERF_EVENT_SAMPLE) { | ||
| 981 | int user = | ||
| 982 | (event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER; | ||
| 983 | process_event(event->ip.ip, md->counter, user); | ||
| 984 | } | ||
| 985 | } | 1059 | } |
| 986 | 1060 | ||
| 987 | md->prev = old; | 1061 | md->prev = old; |
| @@ -1005,7 +1079,7 @@ int group_fd; | |||
| 1005 | 1079 | ||
| 1006 | static void start_counter(int i, int counter) | 1080 | static void start_counter(int i, int counter) |
| 1007 | { | 1081 | { |
| 1008 | struct perf_counter_attr *attr; | 1082 | struct perf_event_attr *attr; |
| 1009 | int cpu; | 1083 | int cpu; |
| 1010 | 1084 | ||
| 1011 | cpu = profile_cpu; | 1085 | cpu = profile_cpu; |
| @@ -1015,16 +1089,23 @@ static void start_counter(int i, int counter) | |||
| 1015 | attr = attrs + counter; | 1089 | attr = attrs + counter; |
| 1016 | 1090 | ||
| 1017 | attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; | 1091 | attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
| 1018 | attr->freq = freq; | 1092 | |
| 1093 | if (freq) { | ||
| 1094 | attr->sample_type |= PERF_SAMPLE_PERIOD; | ||
| 1095 | attr->freq = 1; | ||
| 1096 | attr->sample_freq = freq; | ||
| 1097 | } | ||
| 1098 | |||
| 1019 | attr->inherit = (cpu < 0) && inherit; | 1099 | attr->inherit = (cpu < 0) && inherit; |
| 1100 | attr->mmap = 1; | ||
| 1020 | 1101 | ||
| 1021 | try_again: | 1102 | try_again: |
| 1022 | fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0); | 1103 | fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0); |
| 1023 | 1104 | ||
| 1024 | if (fd[i][counter] < 0) { | 1105 | if (fd[i][counter] < 0) { |
| 1025 | int err = errno; | 1106 | int err = errno; |
| 1026 | 1107 | ||
| 1027 | if (err == EPERM) | 1108 | if (err == EPERM || err == EACCES) |
| 1028 | die("No permission - are you root?\n"); | 1109 | die("No permission - are you root?\n"); |
| 1029 | /* | 1110 | /* |
| 1030 | * If it's cycles then fall back to hrtimer | 1111 | * If it's cycles then fall back to hrtimer |
| @@ -1044,7 +1125,7 @@ try_again: | |||
| 1044 | printf("\n"); | 1125 | printf("\n"); |
| 1045 | error("perfcounter syscall returned with %d (%s)\n", | 1126 | error("perfcounter syscall returned with %d (%s)\n", |
| 1046 | fd[i][counter], strerror(err)); | 1127 | fd[i][counter], strerror(err)); |
| 1047 | die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n"); | 1128 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
| 1048 | exit(-1); | 1129 | exit(-1); |
| 1049 | } | 1130 | } |
| 1050 | assert(fd[i][counter] >= 0); | 1131 | assert(fd[i][counter] >= 0); |
| @@ -1075,6 +1156,11 @@ static int __cmd_top(void) | |||
| 1075 | int i, counter; | 1156 | int i, counter; |
| 1076 | int ret; | 1157 | int ret; |
| 1077 | 1158 | ||
| 1159 | if (target_pid != -1) | ||
| 1160 | event__synthesize_thread(target_pid, event__process); | ||
| 1161 | else | ||
| 1162 | event__synthesize_threads(event__process); | ||
| 1163 | |||
| 1078 | for (i = 0; i < nr_cpus; i++) { | 1164 | for (i = 0; i < nr_cpus; i++) { |
| 1079 | group_fd = -1; | 1165 | group_fd = -1; |
| 1080 | for (counter = 0; counter < nr_counters; counter++) | 1166 | for (counter = 0; counter < nr_counters; counter++) |
| @@ -1130,7 +1216,10 @@ static const struct option options[] = { | |||
| 1130 | "system-wide collection from all CPUs"), | 1216 | "system-wide collection from all CPUs"), |
| 1131 | OPT_INTEGER('C', "CPU", &profile_cpu, | 1217 | OPT_INTEGER('C', "CPU", &profile_cpu, |
| 1132 | "CPU to profile on"), | 1218 | "CPU to profile on"), |
| 1133 | OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), | 1219 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
| 1220 | "file", "vmlinux pathname"), | ||
| 1221 | OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols, | ||
| 1222 | "hide kernel symbols"), | ||
| 1134 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, | 1223 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
| 1135 | "number of mmap data pages"), | 1224 | "number of mmap data pages"), |
| 1136 | OPT_INTEGER('r', "realtime", &realtime_prio, | 1225 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| @@ -1153,6 +1242,8 @@ static const struct option options[] = { | |||
| 1153 | "profile at this frequency"), | 1242 | "profile at this frequency"), |
| 1154 | OPT_INTEGER('E', "entries", &print_entries, | 1243 | OPT_INTEGER('E', "entries", &print_entries, |
| 1155 | "display this many functions"), | 1244 | "display this many functions"), |
| 1245 | OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols, | ||
| 1246 | "hide user symbols"), | ||
| 1156 | OPT_BOOLEAN('v', "verbose", &verbose, | 1247 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1157 | "be more verbose (show counter open errors, etc)"), | 1248 | "be more verbose (show counter open errors, etc)"), |
| 1158 | OPT_END() | 1249 | OPT_END() |
| @@ -1162,19 +1253,12 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) | |||
| 1162 | { | 1253 | { |
| 1163 | int counter; | 1254 | int counter; |
| 1164 | 1255 | ||
| 1165 | symbol__init(); | ||
| 1166 | |||
| 1167 | page_size = sysconf(_SC_PAGE_SIZE); | 1256 | page_size = sysconf(_SC_PAGE_SIZE); |
| 1168 | 1257 | ||
| 1169 | argc = parse_options(argc, argv, options, top_usage, 0); | 1258 | argc = parse_options(argc, argv, options, top_usage, 0); |
| 1170 | if (argc) | 1259 | if (argc) |
| 1171 | usage_with_options(top_usage, options); | 1260 | usage_with_options(top_usage, options); |
| 1172 | 1261 | ||
| 1173 | if (freq) { | ||
| 1174 | default_interval = freq; | ||
| 1175 | freq = 1; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | /* CPU and PID are mutually exclusive */ | 1262 | /* CPU and PID are mutually exclusive */ |
| 1179 | if (target_pid != -1 && profile_cpu != -1) { | 1263 | if (target_pid != -1 && profile_cpu != -1) { |
| 1180 | printf("WARNING: PID switch overriding CPU\n"); | 1264 | printf("WARNING: PID switch overriding CPU\n"); |
| @@ -1185,13 +1269,31 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) | |||
| 1185 | if (!nr_counters) | 1269 | if (!nr_counters) |
| 1186 | nr_counters = 1; | 1270 | nr_counters = 1; |
| 1187 | 1271 | ||
| 1272 | symbol_conf.priv_size = (sizeof(struct sym_entry) + | ||
| 1273 | (nr_counters + 1) * sizeof(unsigned long)); | ||
| 1274 | if (symbol_conf.vmlinux_name == NULL) | ||
| 1275 | symbol_conf.try_vmlinux_path = true; | ||
| 1276 | if (symbol__init(&symbol_conf) < 0) | ||
| 1277 | return -1; | ||
| 1278 | |||
| 1188 | if (delay_secs < 1) | 1279 | if (delay_secs < 1) |
| 1189 | delay_secs = 1; | 1280 | delay_secs = 1; |
| 1190 | 1281 | ||
| 1191 | parse_symbols(); | ||
| 1192 | parse_source(sym_filter_entry); | 1282 | parse_source(sym_filter_entry); |
| 1193 | 1283 | ||
| 1194 | /* | 1284 | /* |
| 1285 | * User specified count overrides default frequency. | ||
| 1286 | */ | ||
| 1287 | if (default_interval) | ||
| 1288 | freq = 0; | ||
| 1289 | else if (freq) { | ||
| 1290 | default_interval = freq; | ||
| 1291 | } else { | ||
| 1292 | fprintf(stderr, "frequency and count are zero, aborting\n"); | ||
| 1293 | exit(EXIT_FAILURE); | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | /* | ||
| 1195 | * Fill in the ones not specifically initialized via -c: | 1297 | * Fill in the ones not specifically initialized via -c: |
| 1196 | */ | 1298 | */ |
| 1197 | for (counter = 0; counter < nr_counters; counter++) { | 1299 | for (counter = 0; counter < nr_counters; counter++) { |
| @@ -1208,5 +1310,11 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) | |||
| 1208 | if (target_pid != -1 || profile_cpu != -1) | 1310 | if (target_pid != -1 || profile_cpu != -1) |
| 1209 | nr_cpus = 1; | 1311 | nr_cpus = 1; |
| 1210 | 1312 | ||
| 1313 | get_term_dimensions(&winsize); | ||
| 1314 | if (print_entries == 0) { | ||
| 1315 | update_print_entries(&winsize); | ||
| 1316 | signal(SIGWINCH, sig_winch_handler); | ||
| 1317 | } | ||
| 1318 | |||
| 1211 | return __cmd_top(); | 1319 | return __cmd_top(); |
| 1212 | } | 1320 | } |
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 914ab366e369..abb914aa7be6 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c | |||
| @@ -5,66 +5,73 @@ | |||
| 5 | #include "util/symbol.h" | 5 | #include "util/symbol.h" |
| 6 | #include "util/thread.h" | 6 | #include "util/thread.h" |
| 7 | #include "util/header.h" | 7 | #include "util/header.h" |
| 8 | #include "util/exec_cmd.h" | ||
| 9 | #include "util/trace-event.h" | ||
| 8 | 10 | ||
| 9 | #include "util/parse-options.h" | 11 | static char const *script_name; |
| 12 | static char const *generate_script_lang; | ||
| 10 | 13 | ||
| 11 | #include "perf.h" | 14 | static int default_start_script(const char *script __attribute((unused))) |
| 12 | #include "util/debug.h" | 15 | { |
| 16 | return 0; | ||
| 17 | } | ||
| 13 | 18 | ||
| 14 | #include "util/trace-event.h" | 19 | static int default_stop_script(void) |
| 20 | { | ||
| 21 | return 0; | ||
| 22 | } | ||
| 15 | 23 | ||
| 16 | static char const *input_name = "perf.data"; | 24 | static int default_generate_script(const char *outfile __attribute ((unused))) |
| 17 | static int input; | 25 | { |
| 18 | static unsigned long page_size; | 26 | return 0; |
| 19 | static unsigned long mmap_window = 32; | 27 | } |
| 20 | 28 | ||
| 21 | static unsigned long total = 0; | 29 | static struct scripting_ops default_scripting_ops = { |
| 22 | static unsigned long total_comm = 0; | 30 | .start_script = default_start_script, |
| 31 | .stop_script = default_stop_script, | ||
| 32 | .process_event = print_event, | ||
| 33 | .generate_script = default_generate_script, | ||
| 34 | }; | ||
| 35 | |||
| 36 | static struct scripting_ops *scripting_ops; | ||
| 23 | 37 | ||
| 24 | static struct rb_root threads; | 38 | static void setup_scripting(void) |
| 25 | static struct thread *last_match; | 39 | { |
| 40 | /* make sure PERF_EXEC_PATH is set for scripts */ | ||
| 41 | perf_set_argv_exec_path(perf_exec_path()); | ||
| 26 | 42 | ||
| 27 | static struct perf_header *header; | 43 | setup_perl_scripting(); |
| 28 | static u64 sample_type; | ||
| 29 | 44 | ||
| 45 | scripting_ops = &default_scripting_ops; | ||
| 46 | } | ||
| 30 | 47 | ||
| 31 | static int | 48 | static int cleanup_scripting(void) |
| 32 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 33 | { | 49 | { |
| 34 | struct thread *thread; | 50 | return scripting_ops->stop_script(); |
| 51 | } | ||
| 35 | 52 | ||
| 36 | thread = threads__findnew(event->comm.pid, &threads, &last_match); | 53 | #include "util/parse-options.h" |
| 37 | 54 | ||
| 38 | dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", | 55 | #include "perf.h" |
| 39 | (void *)(offset + head), | 56 | #include "util/debug.h" |
| 40 | (void *)(long)(event->header.size), | ||
| 41 | event->comm.comm, event->comm.pid); | ||
| 42 | 57 | ||
| 43 | if (thread == NULL || | 58 | #include "util/trace-event.h" |
| 44 | thread__set_comm(thread, event->comm.comm)) { | 59 | #include "util/data_map.h" |
| 45 | dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n"); | 60 | #include "util/exec_cmd.h" |
| 46 | return -1; | ||
| 47 | } | ||
| 48 | total_comm++; | ||
| 49 | 61 | ||
| 50 | return 0; | 62 | static char const *input_name = "perf.data"; |
| 51 | } | ||
| 52 | 63 | ||
| 53 | static int | 64 | static struct perf_header *header; |
| 54 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | 65 | static u64 sample_type; |
| 66 | |||
| 67 | static int process_sample_event(event_t *event) | ||
| 55 | { | 68 | { |
| 56 | char level; | ||
| 57 | int show = 0; | ||
| 58 | struct dso *dso = NULL; | ||
| 59 | struct thread *thread; | ||
| 60 | u64 ip = event->ip.ip; | 69 | u64 ip = event->ip.ip; |
| 61 | u64 timestamp = -1; | 70 | u64 timestamp = -1; |
| 62 | u32 cpu = -1; | 71 | u32 cpu = -1; |
| 63 | u64 period = 1; | 72 | u64 period = 1; |
| 64 | void *more_data = event->ip.__more_data; | 73 | void *more_data = event->ip.__more_data; |
| 65 | int cpumode; | 74 | struct thread *thread = threads__findnew(event->ip.pid); |
| 66 | |||
| 67 | thread = threads__findnew(event->ip.pid, &threads, &last_match); | ||
| 68 | 75 | ||
| 69 | if (sample_type & PERF_SAMPLE_TIME) { | 76 | if (sample_type & PERF_SAMPLE_TIME) { |
| 70 | timestamp = *(u64 *)more_data; | 77 | timestamp = *(u64 *)more_data; |
| @@ -82,45 +89,19 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 82 | more_data += sizeof(u64); | 89 | more_data += sizeof(u64); |
| 83 | } | 90 | } |
| 84 | 91 | ||
| 85 | dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", | 92 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
| 86 | (void *)(offset + head), | ||
| 87 | (void *)(long)(event->header.size), | ||
| 88 | event->header.misc, | 93 | event->header.misc, |
| 89 | event->ip.pid, event->ip.tid, | 94 | event->ip.pid, event->ip.tid, |
| 90 | (void *)(long)ip, | 95 | (void *)(long)ip, |
| 91 | (long long)period); | 96 | (long long)period); |
| 92 | 97 | ||
| 93 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 94 | |||
| 95 | if (thread == NULL) { | 98 | if (thread == NULL) { |
| 96 | eprintf("problem processing %d event, skipping it.\n", | 99 | pr_debug("problem processing %d event, skipping it.\n", |
| 97 | event->header.type); | 100 | event->header.type); |
| 98 | return -1; | 101 | return -1; |
| 99 | } | 102 | } |
| 100 | 103 | ||
| 101 | cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK; | 104 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 102 | |||
| 103 | if (cpumode == PERF_EVENT_MISC_KERNEL) { | ||
| 104 | show = SHOW_KERNEL; | ||
| 105 | level = 'k'; | ||
| 106 | |||
| 107 | dso = kernel_dso; | ||
| 108 | |||
| 109 | dump_printf(" ...... dso: %s\n", dso->name); | ||
| 110 | |||
| 111 | } else if (cpumode == PERF_EVENT_MISC_USER) { | ||
| 112 | |||
| 113 | show = SHOW_USER; | ||
| 114 | level = '.'; | ||
| 115 | |||
| 116 | } else { | ||
| 117 | show = SHOW_HV; | ||
| 118 | level = 'H'; | ||
| 119 | |||
| 120 | dso = hypervisor_dso; | ||
| 121 | |||
| 122 | dump_printf(" ...... dso: [hypervisor]\n"); | ||
| 123 | } | ||
| 124 | 105 | ||
| 125 | if (sample_type & PERF_SAMPLE_RAW) { | 106 | if (sample_type & PERF_SAMPLE_RAW) { |
| 126 | struct { | 107 | struct { |
| @@ -133,133 +114,189 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 133 | * field, although it should be the same than this perf | 114 | * field, although it should be the same than this perf |
| 134 | * event pid | 115 | * event pid |
| 135 | */ | 116 | */ |
| 136 | print_event(cpu, raw->data, raw->size, timestamp, thread->comm); | 117 | scripting_ops->process_event(cpu, raw->data, raw->size, |
| 118 | timestamp, thread->comm); | ||
| 137 | } | 119 | } |
| 138 | total += period; | 120 | event__stats.total += period; |
| 139 | 121 | ||
| 140 | return 0; | 122 | return 0; |
| 141 | } | 123 | } |
| 142 | 124 | ||
| 143 | static int | 125 | static int sample_type_check(u64 type) |
| 144 | process_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 145 | { | 126 | { |
| 146 | trace_event(event); | 127 | sample_type = type; |
| 147 | |||
| 148 | switch (event->header.type) { | ||
| 149 | case PERF_EVENT_MMAP ... PERF_EVENT_LOST: | ||
| 150 | return 0; | ||
| 151 | |||
| 152 | case PERF_EVENT_COMM: | ||
| 153 | return process_comm_event(event, offset, head); | ||
| 154 | |||
| 155 | case PERF_EVENT_EXIT ... PERF_EVENT_READ: | ||
| 156 | return 0; | ||
| 157 | 128 | ||
| 158 | case PERF_EVENT_SAMPLE: | 129 | if (!(sample_type & PERF_SAMPLE_RAW)) { |
| 159 | return process_sample_event(event, offset, head); | 130 | fprintf(stderr, |
| 160 | 131 | "No trace sample to read. Did you call perf record " | |
| 161 | case PERF_EVENT_MAX: | 132 | "without -R?"); |
| 162 | default: | ||
| 163 | return -1; | 133 | return -1; |
| 164 | } | 134 | } |
| 165 | 135 | ||
| 166 | return 0; | 136 | return 0; |
| 167 | } | 137 | } |
| 168 | 138 | ||
| 139 | static struct perf_file_handler file_handler = { | ||
| 140 | .process_sample_event = process_sample_event, | ||
| 141 | .process_comm_event = event__process_comm, | ||
| 142 | .sample_type_check = sample_type_check, | ||
| 143 | }; | ||
| 144 | |||
| 169 | static int __cmd_trace(void) | 145 | static int __cmd_trace(void) |
| 170 | { | 146 | { |
| 171 | int ret, rc = EXIT_FAILURE; | 147 | register_idle_thread(); |
| 172 | unsigned long offset = 0; | 148 | register_perf_file_handler(&file_handler); |
| 173 | unsigned long head = 0; | ||
| 174 | struct stat perf_stat; | ||
| 175 | event_t *event; | ||
| 176 | uint32_t size; | ||
| 177 | char *buf; | ||
| 178 | |||
| 179 | trace_report(); | ||
| 180 | register_idle_thread(&threads, &last_match); | ||
| 181 | |||
| 182 | input = open(input_name, O_RDONLY); | ||
| 183 | if (input < 0) { | ||
| 184 | perror("failed to open file"); | ||
| 185 | exit(-1); | ||
| 186 | } | ||
| 187 | 149 | ||
| 188 | ret = fstat(input, &perf_stat); | 150 | return mmap_dispatch_perf_file(&header, input_name, |
| 189 | if (ret < 0) { | 151 | 0, 0, &event__cwdlen, &event__cwd); |
| 190 | perror("failed to stat file"); | 152 | } |
| 191 | exit(-1); | ||
| 192 | } | ||
| 193 | 153 | ||
| 194 | if (!perf_stat.st_size) { | 154 | struct script_spec { |
| 195 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | 155 | struct list_head node; |
| 196 | exit(0); | 156 | struct scripting_ops *ops; |
| 197 | } | 157 | char spec[0]; |
| 198 | header = perf_header__read(input); | 158 | }; |
| 199 | head = header->data_offset; | ||
| 200 | sample_type = perf_header__sample_type(header); | ||
| 201 | 159 | ||
| 202 | if (!(sample_type & PERF_SAMPLE_RAW)) | 160 | LIST_HEAD(script_specs); |
| 203 | die("No trace sample to read. Did you call perf record " | ||
| 204 | "without -R?"); | ||
| 205 | 161 | ||
| 206 | if (load_kernel() < 0) { | 162 | static struct script_spec *script_spec__new(const char *spec, |
| 207 | perror("failed to load kernel symbols"); | 163 | struct scripting_ops *ops) |
| 208 | return EXIT_FAILURE; | 164 | { |
| 209 | } | 165 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); |
| 210 | 166 | ||
| 211 | remap: | 167 | if (s != NULL) { |
| 212 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | 168 | strcpy(s->spec, spec); |
| 213 | MAP_SHARED, input, offset); | 169 | s->ops = ops; |
| 214 | if (buf == MAP_FAILED) { | ||
| 215 | perror("failed to mmap file"); | ||
| 216 | exit(-1); | ||
| 217 | } | 170 | } |
| 218 | 171 | ||
| 219 | more: | 172 | return s; |
| 220 | event = (event_t *)(buf + head); | 173 | } |
| 221 | 174 | ||
| 222 | size = event->header.size; | 175 | static void script_spec__delete(struct script_spec *s) |
| 223 | if (!size) | 176 | { |
| 224 | size = 8; | 177 | free(s->spec); |
| 178 | free(s); | ||
| 179 | } | ||
| 225 | 180 | ||
| 226 | if (head + event->header.size >= page_size * mmap_window) { | 181 | static void script_spec__add(struct script_spec *s) |
| 227 | unsigned long shift = page_size * (head / page_size); | 182 | { |
| 228 | int res; | 183 | list_add_tail(&s->node, &script_specs); |
| 184 | } | ||
| 229 | 185 | ||
| 230 | res = munmap(buf, page_size * mmap_window); | 186 | static struct script_spec *script_spec__find(const char *spec) |
| 231 | assert(res == 0); | 187 | { |
| 188 | struct script_spec *s; | ||
| 232 | 189 | ||
| 233 | offset += shift; | 190 | list_for_each_entry(s, &script_specs, node) |
| 234 | head -= shift; | 191 | if (strcasecmp(s->spec, spec) == 0) |
| 235 | goto remap; | 192 | return s; |
| 236 | } | 193 | return NULL; |
| 194 | } | ||
| 237 | 195 | ||
| 238 | size = event->header.size; | 196 | static struct script_spec *script_spec__findnew(const char *spec, |
| 197 | struct scripting_ops *ops) | ||
| 198 | { | ||
| 199 | struct script_spec *s = script_spec__find(spec); | ||
| 239 | 200 | ||
| 201 | if (s) | ||
| 202 | return s; | ||
| 240 | 203 | ||
| 241 | if (!size || process_event(event, offset, head) < 0) { | 204 | s = script_spec__new(spec, ops); |
| 205 | if (!s) | ||
| 206 | goto out_delete_spec; | ||
| 242 | 207 | ||
| 243 | /* | 208 | script_spec__add(s); |
| 244 | * assume we lost track of the stream, check alignment, and | ||
| 245 | * increment a single u64 in the hope to catch on again 'soon'. | ||
| 246 | */ | ||
| 247 | 209 | ||
| 248 | if (unlikely(head & 7)) | 210 | return s; |
| 249 | head &= ~7ULL; | ||
| 250 | 211 | ||
| 251 | size = 8; | 212 | out_delete_spec: |
| 252 | } | 213 | script_spec__delete(s); |
| 253 | 214 | ||
| 254 | head += size; | 215 | return NULL; |
| 216 | } | ||
| 255 | 217 | ||
| 256 | if (offset + head < (unsigned long)perf_stat.st_size) | 218 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
| 257 | goto more; | 219 | { |
| 220 | struct script_spec *s; | ||
| 258 | 221 | ||
| 259 | rc = EXIT_SUCCESS; | 222 | s = script_spec__find(spec); |
| 260 | close(input); | 223 | if (s) |
| 224 | return -1; | ||
| 225 | |||
| 226 | s = script_spec__findnew(spec, ops); | ||
| 227 | if (!s) | ||
| 228 | return -1; | ||
| 261 | 229 | ||
| 262 | return rc; | 230 | return 0; |
| 231 | } | ||
| 232 | |||
| 233 | static struct scripting_ops *script_spec__lookup(const char *spec) | ||
| 234 | { | ||
| 235 | struct script_spec *s = script_spec__find(spec); | ||
| 236 | if (!s) | ||
| 237 | return NULL; | ||
| 238 | |||
| 239 | return s->ops; | ||
| 240 | } | ||
| 241 | |||
| 242 | static void list_available_languages(void) | ||
| 243 | { | ||
| 244 | struct script_spec *s; | ||
| 245 | |||
| 246 | fprintf(stderr, "\n"); | ||
| 247 | fprintf(stderr, "Scripting language extensions (used in " | ||
| 248 | "perf trace -s [spec:]script.[spec]):\n\n"); | ||
| 249 | |||
| 250 | list_for_each_entry(s, &script_specs, node) | ||
| 251 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | ||
| 252 | |||
| 253 | fprintf(stderr, "\n"); | ||
| 254 | } | ||
| 255 | |||
| 256 | static int parse_scriptname(const struct option *opt __used, | ||
| 257 | const char *str, int unset __used) | ||
| 258 | { | ||
| 259 | char spec[PATH_MAX]; | ||
| 260 | const char *script, *ext; | ||
| 261 | int len; | ||
| 262 | |||
| 263 | if (strcmp(str, "list") == 0) { | ||
| 264 | list_available_languages(); | ||
| 265 | return 0; | ||
| 266 | } | ||
| 267 | |||
| 268 | script = strchr(str, ':'); | ||
| 269 | if (script) { | ||
| 270 | len = script - str; | ||
| 271 | if (len >= PATH_MAX) { | ||
| 272 | fprintf(stderr, "invalid language specifier"); | ||
| 273 | return -1; | ||
| 274 | } | ||
| 275 | strncpy(spec, str, len); | ||
| 276 | spec[len] = '\0'; | ||
| 277 | scripting_ops = script_spec__lookup(spec); | ||
| 278 | if (!scripting_ops) { | ||
| 279 | fprintf(stderr, "invalid language specifier"); | ||
| 280 | return -1; | ||
| 281 | } | ||
| 282 | script++; | ||
| 283 | } else { | ||
| 284 | script = str; | ||
| 285 | ext = strchr(script, '.'); | ||
| 286 | if (!ext) { | ||
| 287 | fprintf(stderr, "invalid script extension"); | ||
| 288 | return -1; | ||
| 289 | } | ||
| 290 | scripting_ops = script_spec__lookup(++ext); | ||
| 291 | if (!scripting_ops) { | ||
| 292 | fprintf(stderr, "invalid script extension"); | ||
| 293 | return -1; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | script_name = strdup(script); | ||
| 298 | |||
| 299 | return 0; | ||
| 263 | } | 300 | } |
| 264 | 301 | ||
| 265 | static const char * const annotate_usage[] = { | 302 | static const char * const annotate_usage[] = { |
| @@ -272,13 +309,24 @@ static const struct option options[] = { | |||
| 272 | "dump raw trace in ASCII"), | 309 | "dump raw trace in ASCII"), |
| 273 | OPT_BOOLEAN('v', "verbose", &verbose, | 310 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 274 | "be more verbose (show symbol address, etc)"), | 311 | "be more verbose (show symbol address, etc)"), |
| 312 | OPT_BOOLEAN('l', "latency", &latency_format, | ||
| 313 | "show latency attributes (irqs/preemption disabled, etc)"), | ||
| 314 | OPT_CALLBACK('s', "script", NULL, "name", | ||
| 315 | "script file name (lang:script name, script name, or *)", | ||
| 316 | parse_scriptname), | ||
| 317 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | ||
| 318 | "generate perf-trace.xx script in specified language"), | ||
| 319 | |||
| 275 | OPT_END() | 320 | OPT_END() |
| 276 | }; | 321 | }; |
| 277 | 322 | ||
| 278 | int cmd_trace(int argc, const char **argv, const char *prefix __used) | 323 | int cmd_trace(int argc, const char **argv, const char *prefix __used) |
| 279 | { | 324 | { |
| 280 | symbol__init(); | 325 | int err; |
| 281 | page_size = getpagesize(); | 326 | |
| 327 | symbol__init(0); | ||
| 328 | |||
| 329 | setup_scripting(); | ||
| 282 | 330 | ||
| 283 | argc = parse_options(argc, argv, options, annotate_usage, 0); | 331 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 284 | if (argc) { | 332 | if (argc) { |
| @@ -290,8 +338,52 @@ int cmd_trace(int argc, const char **argv, const char *prefix __used) | |||
| 290 | usage_with_options(annotate_usage, options); | 338 | usage_with_options(annotate_usage, options); |
| 291 | } | 339 | } |
| 292 | 340 | ||
| 293 | |||
| 294 | setup_pager(); | 341 | setup_pager(); |
| 295 | 342 | ||
| 296 | return __cmd_trace(); | 343 | if (generate_script_lang) { |
| 344 | struct stat perf_stat; | ||
| 345 | |||
| 346 | int input = open(input_name, O_RDONLY); | ||
| 347 | if (input < 0) { | ||
| 348 | perror("failed to open file"); | ||
| 349 | exit(-1); | ||
| 350 | } | ||
| 351 | |||
| 352 | err = fstat(input, &perf_stat); | ||
| 353 | if (err < 0) { | ||
| 354 | perror("failed to stat file"); | ||
| 355 | exit(-1); | ||
| 356 | } | ||
| 357 | |||
| 358 | if (!perf_stat.st_size) { | ||
| 359 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | ||
| 360 | exit(0); | ||
| 361 | } | ||
| 362 | |||
| 363 | scripting_ops = script_spec__lookup(generate_script_lang); | ||
| 364 | if (!scripting_ops) { | ||
| 365 | fprintf(stderr, "invalid language specifier"); | ||
| 366 | return -1; | ||
| 367 | } | ||
| 368 | |||
| 369 | header = perf_header__new(); | ||
| 370 | if (header == NULL) | ||
| 371 | return -1; | ||
| 372 | |||
| 373 | perf_header__read(header, input); | ||
| 374 | err = scripting_ops->generate_script("perf-trace"); | ||
| 375 | goto out; | ||
| 376 | } | ||
| 377 | |||
| 378 | if (script_name) { | ||
| 379 | err = scripting_ops->start_script(script_name); | ||
| 380 | if (err) | ||
| 381 | goto out; | ||
| 382 | } | ||
| 383 | |||
| 384 | err = __cmd_trace(); | ||
| 385 | |||
| 386 | cleanup_scripting(); | ||
| 387 | out: | ||
| 388 | return err; | ||
| 297 | } | 389 | } |
diff --git a/tools/perf/builtin.h b/tools/perf/builtin.h index 3a63e41fb44e..a3d8bf65f26c 100644 --- a/tools/perf/builtin.h +++ b/tools/perf/builtin.h | |||
| @@ -15,13 +15,19 @@ extern int read_line_with_nul(char *buf, int size, FILE *file); | |||
| 15 | extern int check_pager_config(const char *cmd); | 15 | extern int check_pager_config(const char *cmd); |
| 16 | 16 | ||
| 17 | extern int cmd_annotate(int argc, const char **argv, const char *prefix); | 17 | extern int cmd_annotate(int argc, const char **argv, const char *prefix); |
| 18 | extern int cmd_bench(int argc, const char **argv, const char *prefix); | ||
| 19 | extern int cmd_buildid_list(int argc, const char **argv, const char *prefix); | ||
| 18 | extern int cmd_help(int argc, const char **argv, const char *prefix); | 20 | extern int cmd_help(int argc, const char **argv, const char *prefix); |
| 21 | extern int cmd_sched(int argc, const char **argv, const char *prefix); | ||
| 22 | extern int cmd_list(int argc, const char **argv, const char *prefix); | ||
| 19 | extern int cmd_record(int argc, const char **argv, const char *prefix); | 23 | extern int cmd_record(int argc, const char **argv, const char *prefix); |
| 20 | extern int cmd_report(int argc, const char **argv, const char *prefix); | 24 | extern int cmd_report(int argc, const char **argv, const char *prefix); |
| 21 | extern int cmd_stat(int argc, const char **argv, const char *prefix); | 25 | extern int cmd_stat(int argc, const char **argv, const char *prefix); |
| 26 | extern int cmd_timechart(int argc, const char **argv, const char *prefix); | ||
| 22 | extern int cmd_top(int argc, const char **argv, const char *prefix); | 27 | extern int cmd_top(int argc, const char **argv, const char *prefix); |
| 23 | extern int cmd_version(int argc, const char **argv, const char *prefix); | ||
| 24 | extern int cmd_list(int argc, const char **argv, const char *prefix); | ||
| 25 | extern int cmd_trace(int argc, const char **argv, const char *prefix); | 28 | extern int cmd_trace(int argc, const char **argv, const char *prefix); |
| 29 | extern int cmd_version(int argc, const char **argv, const char *prefix); | ||
| 30 | extern int cmd_probe(int argc, const char **argv, const char *prefix); | ||
| 31 | extern int cmd_kmem(int argc, const char **argv, const char *prefix); | ||
| 26 | 32 | ||
| 27 | #endif | 33 | #endif |
diff --git a/tools/perf/command-list.txt b/tools/perf/command-list.txt index eebce30afbc0..02b09ea17a3e 100644 --- a/tools/perf/command-list.txt +++ b/tools/perf/command-list.txt | |||
| @@ -3,8 +3,15 @@ | |||
| 3 | # command name category [deprecated] [common] | 3 | # command name category [deprecated] [common] |
| 4 | # | 4 | # |
| 5 | perf-annotate mainporcelain common | 5 | perf-annotate mainporcelain common |
| 6 | perf-bench mainporcelain common | ||
| 7 | perf-buildid-list mainporcelain common | ||
| 6 | perf-list mainporcelain common | 8 | perf-list mainporcelain common |
| 9 | perf-sched mainporcelain common | ||
| 7 | perf-record mainporcelain common | 10 | perf-record mainporcelain common |
| 8 | perf-report mainporcelain common | 11 | perf-report mainporcelain common |
| 9 | perf-stat mainporcelain common | 12 | perf-stat mainporcelain common |
| 13 | perf-timechart mainporcelain common | ||
| 10 | perf-top mainporcelain common | 14 | perf-top mainporcelain common |
| 15 | perf-trace mainporcelain common | ||
| 16 | perf-probe mainporcelain common | ||
| 17 | perf-kmem mainporcelain common | ||
diff --git a/tools/perf/design.txt b/tools/perf/design.txt index f71e0d245cba..f000c30877ac 100644 --- a/tools/perf/design.txt +++ b/tools/perf/design.txt | |||
| @@ -18,10 +18,10 @@ underlying hardware counters. | |||
| 18 | Performance counters are accessed via special file descriptors. | 18 | Performance counters are accessed via special file descriptors. |
| 19 | There's one file descriptor per virtual counter used. | 19 | There's one file descriptor per virtual counter used. |
| 20 | 20 | ||
| 21 | The special file descriptor is opened via the perf_counter_open() | 21 | The special file descriptor is opened via the perf_event_open() |
| 22 | system call: | 22 | system call: |
| 23 | 23 | ||
| 24 | int sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr, | 24 | int sys_perf_event_open(struct perf_event_hw_event *hw_event_uptr, |
| 25 | pid_t pid, int cpu, int group_fd, | 25 | pid_t pid, int cpu, int group_fd, |
| 26 | unsigned long flags); | 26 | unsigned long flags); |
| 27 | 27 | ||
| @@ -32,9 +32,9 @@ can be used to set the blocking mode, etc. | |||
| 32 | Multiple counters can be kept open at a time, and the counters | 32 | Multiple counters can be kept open at a time, and the counters |
| 33 | can be poll()ed. | 33 | can be poll()ed. |
| 34 | 34 | ||
| 35 | When creating a new counter fd, 'perf_counter_hw_event' is: | 35 | When creating a new counter fd, 'perf_event_hw_event' is: |
| 36 | 36 | ||
| 37 | struct perf_counter_hw_event { | 37 | struct perf_event_hw_event { |
| 38 | /* | 38 | /* |
| 39 | * The MSB of the config word signifies if the rest contains cpu | 39 | * The MSB of the config word signifies if the rest contains cpu |
| 40 | * specific (raw) counter configuration data, if unset, the next | 40 | * specific (raw) counter configuration data, if unset, the next |
| @@ -93,7 +93,7 @@ specified by 'event_id': | |||
| 93 | 93 | ||
| 94 | /* | 94 | /* |
| 95 | * Generalized performance counter event types, used by the hw_event.event_id | 95 | * Generalized performance counter event types, used by the hw_event.event_id |
| 96 | * parameter of the sys_perf_counter_open() syscall: | 96 | * parameter of the sys_perf_event_open() syscall: |
| 97 | */ | 97 | */ |
| 98 | enum hw_event_ids { | 98 | enum hw_event_ids { |
| 99 | /* | 99 | /* |
| @@ -137,6 +137,8 @@ enum sw_event_ids { | |||
| 137 | PERF_COUNT_SW_CPU_MIGRATIONS = 4, | 137 | PERF_COUNT_SW_CPU_MIGRATIONS = 4, |
| 138 | PERF_COUNT_SW_PAGE_FAULTS_MIN = 5, | 138 | PERF_COUNT_SW_PAGE_FAULTS_MIN = 5, |
| 139 | PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6, | 139 | PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6, |
| 140 | PERF_COUNT_SW_ALIGNMENT_FAULTS = 7, | ||
| 141 | PERF_COUNT_SW_EMULATION_FAULTS = 8, | ||
| 140 | }; | 142 | }; |
| 141 | 143 | ||
| 142 | Counters of the type PERF_TYPE_TRACEPOINT are available when the ftrace event | 144 | Counters of the type PERF_TYPE_TRACEPOINT are available when the ftrace event |
| @@ -159,7 +161,7 @@ in size. | |||
| 159 | * reads on the counter should return the indicated quantities, | 161 | * reads on the counter should return the indicated quantities, |
| 160 | * in increasing order of bit value, after the counter value. | 162 | * in increasing order of bit value, after the counter value. |
| 161 | */ | 163 | */ |
| 162 | enum perf_counter_read_format { | 164 | enum perf_event_read_format { |
| 163 | PERF_FORMAT_TOTAL_TIME_ENABLED = 1, | 165 | PERF_FORMAT_TOTAL_TIME_ENABLED = 1, |
| 164 | PERF_FORMAT_TOTAL_TIME_RUNNING = 2, | 166 | PERF_FORMAT_TOTAL_TIME_RUNNING = 2, |
| 165 | }; | 167 | }; |
| @@ -178,7 +180,7 @@ interrupt: | |||
| 178 | * Bits that can be set in hw_event.record_type to request information | 180 | * Bits that can be set in hw_event.record_type to request information |
| 179 | * in the overflow packets. | 181 | * in the overflow packets. |
| 180 | */ | 182 | */ |
| 181 | enum perf_counter_record_format { | 183 | enum perf_event_record_format { |
| 182 | PERF_RECORD_IP = 1U << 0, | 184 | PERF_RECORD_IP = 1U << 0, |
| 183 | PERF_RECORD_TID = 1U << 1, | 185 | PERF_RECORD_TID = 1U << 1, |
| 184 | PERF_RECORD_TIME = 1U << 2, | 186 | PERF_RECORD_TIME = 1U << 2, |
| @@ -228,7 +230,7 @@ these events are recorded in the ring-buffer (see below). | |||
| 228 | The 'comm' bit allows tracking of process comm data on process creation. | 230 | The 'comm' bit allows tracking of process comm data on process creation. |
| 229 | This too is recorded in the ring-buffer (see below). | 231 | This too is recorded in the ring-buffer (see below). |
| 230 | 232 | ||
| 231 | The 'pid' parameter to the perf_counter_open() system call allows the | 233 | The 'pid' parameter to the perf_event_open() system call allows the |
| 232 | counter to be specific to a task: | 234 | counter to be specific to a task: |
| 233 | 235 | ||
| 234 | pid == 0: if the pid parameter is zero, the counter is attached to the | 236 | pid == 0: if the pid parameter is zero, the counter is attached to the |
| @@ -258,7 +260,7 @@ The 'flags' parameter is currently unused and must be zero. | |||
| 258 | 260 | ||
| 259 | The 'group_fd' parameter allows counter "groups" to be set up. A | 261 | The 'group_fd' parameter allows counter "groups" to be set up. A |
| 260 | counter group has one counter which is the group "leader". The leader | 262 | counter group has one counter which is the group "leader". The leader |
| 261 | is created first, with group_fd = -1 in the perf_counter_open call | 263 | is created first, with group_fd = -1 in the perf_event_open call |
| 262 | that creates it. The rest of the group members are created | 264 | that creates it. The rest of the group members are created |
| 263 | subsequently, with group_fd giving the fd of the group leader. | 265 | subsequently, with group_fd giving the fd of the group leader. |
| 264 | (A single counter on its own is created with group_fd = -1 and is | 266 | (A single counter on its own is created with group_fd = -1 and is |
| @@ -277,13 +279,13 @@ tracking are logged into a ring-buffer. This ring-buffer is created and | |||
| 277 | accessed through mmap(). | 279 | accessed through mmap(). |
| 278 | 280 | ||
| 279 | The mmap size should be 1+2^n pages, where the first page is a meta-data page | 281 | The mmap size should be 1+2^n pages, where the first page is a meta-data page |
| 280 | (struct perf_counter_mmap_page) that contains various bits of information such | 282 | (struct perf_event_mmap_page) that contains various bits of information such |
| 281 | as where the ring-buffer head is. | 283 | as where the ring-buffer head is. |
| 282 | 284 | ||
| 283 | /* | 285 | /* |
| 284 | * Structure of the page that can be mapped via mmap | 286 | * Structure of the page that can be mapped via mmap |
| 285 | */ | 287 | */ |
| 286 | struct perf_counter_mmap_page { | 288 | struct perf_event_mmap_page { |
| 287 | __u32 version; /* version number of this structure */ | 289 | __u32 version; /* version number of this structure */ |
| 288 | __u32 compat_version; /* lowest version this is compat with */ | 290 | __u32 compat_version; /* lowest version this is compat with */ |
| 289 | 291 | ||
| @@ -317,7 +319,7 @@ struct perf_counter_mmap_page { | |||
| 317 | * Control data for the mmap() data buffer. | 319 | * Control data for the mmap() data buffer. |
| 318 | * | 320 | * |
| 319 | * User-space reading this value should issue an rmb(), on SMP capable | 321 | * User-space reading this value should issue an rmb(), on SMP capable |
| 320 | * platforms, after reading this value -- see perf_counter_wakeup(). | 322 | * platforms, after reading this value -- see perf_event_wakeup(). |
| 321 | */ | 323 | */ |
| 322 | __u32 data_head; /* head in the data section */ | 324 | __u32 data_head; /* head in the data section */ |
| 323 | }; | 325 | }; |
| @@ -327,9 +329,9 @@ NOTE: the hw-counter userspace bits are arch specific and are currently only | |||
| 327 | 329 | ||
| 328 | The following 2^n pages are the ring-buffer which contains events of the form: | 330 | The following 2^n pages are the ring-buffer which contains events of the form: |
| 329 | 331 | ||
| 330 | #define PERF_EVENT_MISC_KERNEL (1 << 0) | 332 | #define PERF_RECORD_MISC_KERNEL (1 << 0) |
| 331 | #define PERF_EVENT_MISC_USER (1 << 1) | 333 | #define PERF_RECORD_MISC_USER (1 << 1) |
| 332 | #define PERF_EVENT_MISC_OVERFLOW (1 << 2) | 334 | #define PERF_RECORD_MISC_OVERFLOW (1 << 2) |
| 333 | 335 | ||
| 334 | struct perf_event_header { | 336 | struct perf_event_header { |
| 335 | __u32 type; | 337 | __u32 type; |
| @@ -353,8 +355,8 @@ enum perf_event_type { | |||
| 353 | * char filename[]; | 355 | * char filename[]; |
| 354 | * }; | 356 | * }; |
| 355 | */ | 357 | */ |
| 356 | PERF_EVENT_MMAP = 1, | 358 | PERF_RECORD_MMAP = 1, |
| 357 | PERF_EVENT_MUNMAP = 2, | 359 | PERF_RECORD_MUNMAP = 2, |
| 358 | 360 | ||
| 359 | /* | 361 | /* |
| 360 | * struct { | 362 | * struct { |
| @@ -364,10 +366,10 @@ enum perf_event_type { | |||
| 364 | * char comm[]; | 366 | * char comm[]; |
| 365 | * }; | 367 | * }; |
| 366 | */ | 368 | */ |
| 367 | PERF_EVENT_COMM = 3, | 369 | PERF_RECORD_COMM = 3, |
| 368 | 370 | ||
| 369 | /* | 371 | /* |
| 370 | * When header.misc & PERF_EVENT_MISC_OVERFLOW the event_type field | 372 | * When header.misc & PERF_RECORD_MISC_OVERFLOW the event_type field |
| 371 | * will be PERF_RECORD_* | 373 | * will be PERF_RECORD_* |
| 372 | * | 374 | * |
| 373 | * struct { | 375 | * struct { |
| @@ -397,7 +399,7 @@ Notification of new events is possible through poll()/select()/epoll() and | |||
| 397 | fcntl() managing signals. | 399 | fcntl() managing signals. |
| 398 | 400 | ||
| 399 | Normally a notification is generated for every page filled, however one can | 401 | Normally a notification is generated for every page filled, however one can |
| 400 | additionally set perf_counter_hw_event.wakeup_events to generate one every | 402 | additionally set perf_event_hw_event.wakeup_events to generate one every |
| 401 | so many counter overflow events. | 403 | so many counter overflow events. |
| 402 | 404 | ||
| 403 | Future work will include a splice() interface to the ring-buffer. | 405 | Future work will include a splice() interface to the ring-buffer. |
| @@ -409,11 +411,11 @@ events but does continue to exist and maintain its count value. | |||
| 409 | 411 | ||
| 410 | An individual counter or counter group can be enabled with | 412 | An individual counter or counter group can be enabled with |
| 411 | 413 | ||
| 412 | ioctl(fd, PERF_COUNTER_IOC_ENABLE); | 414 | ioctl(fd, PERF_EVENT_IOC_ENABLE); |
| 413 | 415 | ||
| 414 | or disabled with | 416 | or disabled with |
| 415 | 417 | ||
| 416 | ioctl(fd, PERF_COUNTER_IOC_DISABLE); | 418 | ioctl(fd, PERF_EVENT_IOC_DISABLE); |
| 417 | 419 | ||
| 418 | Enabling or disabling the leader of a group enables or disables the | 420 | Enabling or disabling the leader of a group enables or disables the |
| 419 | whole group; that is, while the group leader is disabled, none of the | 421 | whole group; that is, while the group leader is disabled, none of the |
| @@ -424,16 +426,16 @@ other counter. | |||
| 424 | 426 | ||
| 425 | Additionally, non-inherited overflow counters can use | 427 | Additionally, non-inherited overflow counters can use |
| 426 | 428 | ||
| 427 | ioctl(fd, PERF_COUNTER_IOC_REFRESH, nr); | 429 | ioctl(fd, PERF_EVENT_IOC_REFRESH, nr); |
| 428 | 430 | ||
| 429 | to enable a counter for 'nr' events, after which it gets disabled again. | 431 | to enable a counter for 'nr' events, after which it gets disabled again. |
| 430 | 432 | ||
| 431 | A process can enable or disable all the counter groups that are | 433 | A process can enable or disable all the counter groups that are |
| 432 | attached to it, using prctl: | 434 | attached to it, using prctl: |
| 433 | 435 | ||
| 434 | prctl(PR_TASK_PERF_COUNTERS_ENABLE); | 436 | prctl(PR_TASK_PERF_EVENTS_ENABLE); |
| 435 | 437 | ||
| 436 | prctl(PR_TASK_PERF_COUNTERS_DISABLE); | 438 | prctl(PR_TASK_PERF_EVENTS_DISABLE); |
| 437 | 439 | ||
| 438 | This applies to all counters on the current process, whether created | 440 | This applies to all counters on the current process, whether created |
| 439 | by this process or by another, and doesn't affect any counters that | 441 | by this process or by another, and doesn't affect any counters that |
| @@ -447,11 +449,14 @@ Arch requirements | |||
| 447 | If your architecture does not have hardware performance metrics, you can | 449 | If your architecture does not have hardware performance metrics, you can |
| 448 | still use the generic software counters based on hrtimers for sampling. | 450 | still use the generic software counters based on hrtimers for sampling. |
| 449 | 451 | ||
| 450 | So to start with, in order to add HAVE_PERF_COUNTERS to your Kconfig, you | 452 | So to start with, in order to add HAVE_PERF_EVENTS to your Kconfig, you |
| 451 | will need at least this: | 453 | will need at least this: |
| 452 | - asm/perf_counter.h - a basic stub will suffice at first | 454 | - asm/perf_event.h - a basic stub will suffice at first |
| 453 | - support for atomic64 types (and associated helper functions) | 455 | - support for atomic64 types (and associated helper functions) |
| 454 | - set_perf_counter_pending() implemented | 456 | - set_perf_event_pending() implemented |
| 455 | 457 | ||
| 456 | If your architecture does have hardware capabilities, you can override the | 458 | If your architecture does have hardware capabilities, you can override the |
| 457 | weak stub hw_perf_counter_init() to register hardware counters. | 459 | weak stub hw_perf_event_init() to register hardware counters. |
| 460 | |||
| 461 | Architectures that have d-cache aliassing issues, such as Sparc and ARM, | ||
| 462 | should select PERF_USE_VMALLOC in order to avoid these for perf mmap(). | ||
diff --git a/tools/perf/perf.c b/tools/perf/perf.c index fe4589dde950..cf64049bc9bd 100644 --- a/tools/perf/perf.c +++ b/tools/perf/perf.c | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include "util/run-command.h" | 14 | #include "util/run-command.h" |
| 15 | #include "util/parse-events.h" | 15 | #include "util/parse-events.h" |
| 16 | #include "util/string.h" | 16 | #include "util/string.h" |
| 17 | #include "util/debugfs.h" | ||
| 17 | 18 | ||
| 18 | const char perf_usage_string[] = | 19 | const char perf_usage_string[] = |
| 19 | "perf [--version] [--help] COMMAND [ARGS]"; | 20 | "perf [--version] [--help] COMMAND [ARGS]"; |
| @@ -89,8 +90,8 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) | |||
| 89 | /* | 90 | /* |
| 90 | * Check remaining flags. | 91 | * Check remaining flags. |
| 91 | */ | 92 | */ |
| 92 | if (!prefixcmp(cmd, "--exec-path")) { | 93 | if (!prefixcmp(cmd, CMD_EXEC_PATH)) { |
| 93 | cmd += 11; | 94 | cmd += strlen(CMD_EXEC_PATH); |
| 94 | if (*cmd == '=') | 95 | if (*cmd == '=') |
| 95 | perf_set_argv_exec_path(cmd + 1); | 96 | perf_set_argv_exec_path(cmd + 1); |
| 96 | else { | 97 | else { |
| @@ -117,8 +118,8 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) | |||
| 117 | (*argv)++; | 118 | (*argv)++; |
| 118 | (*argc)--; | 119 | (*argc)--; |
| 119 | handled++; | 120 | handled++; |
| 120 | } else if (!prefixcmp(cmd, "--perf-dir=")) { | 121 | } else if (!prefixcmp(cmd, CMD_PERF_DIR)) { |
| 121 | setenv(PERF_DIR_ENVIRONMENT, cmd + 10, 1); | 122 | setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1); |
| 122 | if (envchanged) | 123 | if (envchanged) |
| 123 | *envchanged = 1; | 124 | *envchanged = 1; |
| 124 | } else if (!strcmp(cmd, "--work-tree")) { | 125 | } else if (!strcmp(cmd, "--work-tree")) { |
| @@ -131,8 +132,8 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) | |||
| 131 | *envchanged = 1; | 132 | *envchanged = 1; |
| 132 | (*argv)++; | 133 | (*argv)++; |
| 133 | (*argc)--; | 134 | (*argc)--; |
| 134 | } else if (!prefixcmp(cmd, "--work-tree=")) { | 135 | } else if (!prefixcmp(cmd, CMD_WORK_TREE)) { |
| 135 | setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + 12, 1); | 136 | setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1); |
| 136 | if (envchanged) | 137 | if (envchanged) |
| 137 | *envchanged = 1; | 138 | *envchanged = 1; |
| 138 | } else if (!strcmp(cmd, "--debugfs-dir")) { | 139 | } else if (!strcmp(cmd, "--debugfs-dir")) { |
| @@ -146,8 +147,8 @@ static int handle_options(const char*** argv, int* argc, int* envchanged) | |||
| 146 | *envchanged = 1; | 147 | *envchanged = 1; |
| 147 | (*argv)++; | 148 | (*argv)++; |
| 148 | (*argc)--; | 149 | (*argc)--; |
| 149 | } else if (!prefixcmp(cmd, "--debugfs-dir=")) { | 150 | } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) { |
| 150 | strncpy(debugfs_mntpt, cmd + 14, MAXPATHLEN); | 151 | strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN); |
| 151 | debugfs_mntpt[MAXPATHLEN - 1] = '\0'; | 152 | debugfs_mntpt[MAXPATHLEN - 1] = '\0'; |
| 152 | if (envchanged) | 153 | if (envchanged) |
| 153 | *envchanged = 1; | 154 | *envchanged = 1; |
| @@ -284,15 +285,21 @@ static void handle_internal_command(int argc, const char **argv) | |||
| 284 | { | 285 | { |
| 285 | const char *cmd = argv[0]; | 286 | const char *cmd = argv[0]; |
| 286 | static struct cmd_struct commands[] = { | 287 | static struct cmd_struct commands[] = { |
| 287 | { "help", cmd_help, 0 }, | 288 | { "buildid-list", cmd_buildid_list, 0 }, |
| 288 | { "list", cmd_list, 0 }, | 289 | { "help", cmd_help, 0 }, |
| 289 | { "record", cmd_record, 0 }, | 290 | { "list", cmd_list, 0 }, |
| 290 | { "report", cmd_report, 0 }, | 291 | { "record", cmd_record, 0 }, |
| 291 | { "stat", cmd_stat, 0 }, | 292 | { "report", cmd_report, 0 }, |
| 292 | { "top", cmd_top, 0 }, | 293 | { "bench", cmd_bench, 0 }, |
| 293 | { "annotate", cmd_annotate, 0 }, | 294 | { "stat", cmd_stat, 0 }, |
| 294 | { "version", cmd_version, 0 }, | 295 | { "timechart", cmd_timechart, 0 }, |
| 295 | { "trace", cmd_trace, 0 }, | 296 | { "top", cmd_top, 0 }, |
| 297 | { "annotate", cmd_annotate, 0 }, | ||
| 298 | { "version", cmd_version, 0 }, | ||
| 299 | { "trace", cmd_trace, 0 }, | ||
| 300 | { "sched", cmd_sched, 0 }, | ||
| 301 | { "probe", cmd_probe, 0 }, | ||
| 302 | { "kmem", cmd_kmem, 0 }, | ||
| 296 | }; | 303 | }; |
| 297 | unsigned int i; | 304 | unsigned int i; |
| 298 | static const char ext[] = STRIP_EXTENSION; | 305 | static const char ext[] = STRIP_EXTENSION; |
| @@ -380,45 +387,12 @@ static int run_argv(int *argcp, const char ***argv) | |||
| 380 | /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */ | 387 | /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */ |
| 381 | static void get_debugfs_mntpt(void) | 388 | static void get_debugfs_mntpt(void) |
| 382 | { | 389 | { |
| 383 | FILE *file; | 390 | const char *path = debugfs_find_mountpoint(); |
| 384 | char fs_type[100]; | ||
| 385 | char debugfs[MAXPATHLEN]; | ||
| 386 | 391 | ||
| 387 | /* | 392 | if (path) |
| 388 | * try the standard location | 393 | strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt)); |
| 389 | */ | 394 | else |
| 390 | if (valid_debugfs_mount("/sys/kernel/debug/") == 0) { | 395 | debugfs_mntpt[0] = '\0'; |
| 391 | strcpy(debugfs_mntpt, "/sys/kernel/debug/"); | ||
| 392 | return; | ||
| 393 | } | ||
| 394 | |||
| 395 | /* | ||
| 396 | * try the sane location | ||
| 397 | */ | ||
| 398 | if (valid_debugfs_mount("/debug/") == 0) { | ||
| 399 | strcpy(debugfs_mntpt, "/debug/"); | ||
| 400 | return; | ||
| 401 | } | ||
| 402 | |||
| 403 | /* | ||
| 404 | * give up and parse /proc/mounts | ||
| 405 | */ | ||
| 406 | file = fopen("/proc/mounts", "r"); | ||
| 407 | if (file == NULL) | ||
| 408 | return; | ||
| 409 | |||
| 410 | while (fscanf(file, "%*s %" | ||
| 411 | STR(MAXPATHLEN) | ||
| 412 | "s %99s %*s %*d %*d\n", | ||
| 413 | debugfs, fs_type) == 2) { | ||
| 414 | if (strcmp(fs_type, "debugfs") == 0) | ||
| 415 | break; | ||
| 416 | } | ||
| 417 | fclose(file); | ||
| 418 | if (strcmp(fs_type, "debugfs") == 0) { | ||
| 419 | strncpy(debugfs_mntpt, debugfs, MAXPATHLEN); | ||
| 420 | debugfs_mntpt[MAXPATHLEN - 1] = '\0'; | ||
| 421 | } | ||
| 422 | } | 396 | } |
| 423 | 397 | ||
| 424 | int main(int argc, const char **argv) | 398 | int main(int argc, const char **argv) |
diff --git a/tools/perf/perf.h b/tools/perf/perf.h index e5148e2b6134..454d5d55f32d 100644 --- a/tools/perf/perf.h +++ b/tools/perf/perf.h | |||
| @@ -41,20 +41,38 @@ | |||
| 41 | #define cpu_relax() asm volatile("" ::: "memory"); | 41 | #define cpu_relax() asm volatile("" ::: "memory"); |
| 42 | #endif | 42 | #endif |
| 43 | 43 | ||
| 44 | #ifdef __sparc__ | ||
| 45 | #include "../../arch/sparc/include/asm/unistd.h" | ||
| 46 | #define rmb() asm volatile("":::"memory") | ||
| 47 | #define cpu_relax() asm volatile("":::"memory") | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #ifdef __alpha__ | ||
| 51 | #include "../../arch/alpha/include/asm/unistd.h" | ||
| 52 | #define rmb() asm volatile("mb" ::: "memory") | ||
| 53 | #define cpu_relax() asm volatile("" ::: "memory") | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #ifdef __ia64__ | ||
| 57 | #include "../../arch/ia64/include/asm/unistd.h" | ||
| 58 | #define rmb() asm volatile ("mf" ::: "memory") | ||
| 59 | #define cpu_relax() asm volatile ("hint @pause" ::: "memory") | ||
| 60 | #endif | ||
| 61 | |||
| 44 | #include <time.h> | 62 | #include <time.h> |
| 45 | #include <unistd.h> | 63 | #include <unistd.h> |
| 46 | #include <sys/types.h> | 64 | #include <sys/types.h> |
| 47 | #include <sys/syscall.h> | 65 | #include <sys/syscall.h> |
| 48 | 66 | ||
| 49 | #include "../../include/linux/perf_counter.h" | 67 | #include "../../include/linux/perf_event.h" |
| 50 | #include "util/types.h" | 68 | #include "util/types.h" |
| 51 | 69 | ||
| 52 | /* | 70 | /* |
| 53 | * prctl(PR_TASK_PERF_COUNTERS_DISABLE) will (cheaply) disable all | 71 | * prctl(PR_TASK_PERF_EVENTS_DISABLE) will (cheaply) disable all |
| 54 | * counters in the current task. | 72 | * counters in the current task. |
| 55 | */ | 73 | */ |
| 56 | #define PR_TASK_PERF_COUNTERS_DISABLE 31 | 74 | #define PR_TASK_PERF_EVENTS_DISABLE 31 |
| 57 | #define PR_TASK_PERF_COUNTERS_ENABLE 32 | 75 | #define PR_TASK_PERF_EVENTS_ENABLE 32 |
| 58 | 76 | ||
| 59 | #ifndef NSEC_PER_SEC | 77 | #ifndef NSEC_PER_SEC |
| 60 | # define NSEC_PER_SEC 1000000000ULL | 78 | # define NSEC_PER_SEC 1000000000ULL |
| @@ -84,12 +102,12 @@ static inline unsigned long long rdclock(void) | |||
| 84 | _min1 < _min2 ? _min1 : _min2; }) | 102 | _min1 < _min2 ? _min1 : _min2; }) |
| 85 | 103 | ||
| 86 | static inline int | 104 | static inline int |
| 87 | sys_perf_counter_open(struct perf_counter_attr *attr, | 105 | sys_perf_event_open(struct perf_event_attr *attr, |
| 88 | pid_t pid, int cpu, int group_fd, | 106 | pid_t pid, int cpu, int group_fd, |
| 89 | unsigned long flags) | 107 | unsigned long flags) |
| 90 | { | 108 | { |
| 91 | attr->size = sizeof(*attr); | 109 | attr->size = sizeof(*attr); |
| 92 | return syscall(__NR_perf_counter_open, attr, pid, cpu, | 110 | return syscall(__NR_perf_event_open, attr, pid, cpu, |
| 93 | group_fd, flags); | 111 | group_fd, flags); |
| 94 | } | 112 | } |
| 95 | 113 | ||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/Context.c b/tools/perf/scripts/perl/Perf-Trace-Util/Context.c new file mode 100644 index 000000000000..af78d9a52a7d --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/Context.c | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | /* | ||
| 2 | * This file was generated automatically by ExtUtils::ParseXS version 2.18_02 from the | ||
| 3 | * contents of Context.xs. Do not edit this file, edit Context.xs instead. | ||
| 4 | * | ||
| 5 | * ANY CHANGES MADE HERE WILL BE LOST! | ||
| 6 | * | ||
| 7 | */ | ||
| 8 | |||
| 9 | #line 1 "Context.xs" | ||
| 10 | /* | ||
| 11 | * Context.xs. XS interfaces for perf trace. | ||
| 12 | * | ||
| 13 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> | ||
| 14 | * | ||
| 15 | * This program is free software; you can redistribute it and/or modify | ||
| 16 | * it under the terms of the GNU General Public License as published by | ||
| 17 | * the Free Software Foundation; either version 2 of the License, or | ||
| 18 | * (at your option) any later version. | ||
| 19 | * | ||
| 20 | * This program is distributed in the hope that it will be useful, | ||
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | * GNU General Public License for more details. | ||
| 24 | * | ||
| 25 | * You should have received a copy of the GNU General Public License | ||
| 26 | * along with this program; if not, write to the Free Software | ||
| 27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 28 | * | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include "EXTERN.h" | ||
| 32 | #include "perl.h" | ||
| 33 | #include "XSUB.h" | ||
| 34 | #include "../../../util/trace-event-perl.h" | ||
| 35 | |||
| 36 | #ifndef PERL_UNUSED_VAR | ||
| 37 | # define PERL_UNUSED_VAR(var) if (0) var = var | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #line 41 "Context.c" | ||
| 41 | |||
| 42 | XS(XS_Perf__Trace__Context_common_pc); /* prototype to pass -Wmissing-prototypes */ | ||
| 43 | XS(XS_Perf__Trace__Context_common_pc) | ||
| 44 | { | ||
| 45 | #ifdef dVAR | ||
| 46 | dVAR; dXSARGS; | ||
| 47 | #else | ||
| 48 | dXSARGS; | ||
| 49 | #endif | ||
| 50 | if (items != 1) | ||
| 51 | Perl_croak(aTHX_ "Usage: %s(%s)", "Perf::Trace::Context::common_pc", "context"); | ||
| 52 | PERL_UNUSED_VAR(cv); /* -W */ | ||
| 53 | { | ||
| 54 | struct scripting_context * context = INT2PTR(struct scripting_context *,SvIV(ST(0))); | ||
| 55 | int RETVAL; | ||
| 56 | dXSTARG; | ||
| 57 | |||
| 58 | RETVAL = common_pc(context); | ||
| 59 | XSprePUSH; PUSHi((IV)RETVAL); | ||
| 60 | } | ||
| 61 | XSRETURN(1); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | XS(XS_Perf__Trace__Context_common_flags); /* prototype to pass -Wmissing-prototypes */ | ||
| 66 | XS(XS_Perf__Trace__Context_common_flags) | ||
| 67 | { | ||
| 68 | #ifdef dVAR | ||
| 69 | dVAR; dXSARGS; | ||
| 70 | #else | ||
| 71 | dXSARGS; | ||
| 72 | #endif | ||
| 73 | if (items != 1) | ||
| 74 | Perl_croak(aTHX_ "Usage: %s(%s)", "Perf::Trace::Context::common_flags", "context"); | ||
| 75 | PERL_UNUSED_VAR(cv); /* -W */ | ||
| 76 | { | ||
| 77 | struct scripting_context * context = INT2PTR(struct scripting_context *,SvIV(ST(0))); | ||
| 78 | int RETVAL; | ||
| 79 | dXSTARG; | ||
| 80 | |||
| 81 | RETVAL = common_flags(context); | ||
| 82 | XSprePUSH; PUSHi((IV)RETVAL); | ||
| 83 | } | ||
| 84 | XSRETURN(1); | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | XS(XS_Perf__Trace__Context_common_lock_depth); /* prototype to pass -Wmissing-prototypes */ | ||
| 89 | XS(XS_Perf__Trace__Context_common_lock_depth) | ||
| 90 | { | ||
| 91 | #ifdef dVAR | ||
| 92 | dVAR; dXSARGS; | ||
| 93 | #else | ||
| 94 | dXSARGS; | ||
| 95 | #endif | ||
| 96 | if (items != 1) | ||
| 97 | Perl_croak(aTHX_ "Usage: %s(%s)", "Perf::Trace::Context::common_lock_depth", "context"); | ||
| 98 | PERL_UNUSED_VAR(cv); /* -W */ | ||
| 99 | { | ||
| 100 | struct scripting_context * context = INT2PTR(struct scripting_context *,SvIV(ST(0))); | ||
| 101 | int RETVAL; | ||
| 102 | dXSTARG; | ||
| 103 | |||
| 104 | RETVAL = common_lock_depth(context); | ||
| 105 | XSprePUSH; PUSHi((IV)RETVAL); | ||
| 106 | } | ||
| 107 | XSRETURN(1); | ||
| 108 | } | ||
| 109 | |||
| 110 | #ifdef __cplusplus | ||
| 111 | extern "C" | ||
| 112 | #endif | ||
| 113 | XS(boot_Perf__Trace__Context); /* prototype to pass -Wmissing-prototypes */ | ||
| 114 | XS(boot_Perf__Trace__Context) | ||
| 115 | { | ||
| 116 | #ifdef dVAR | ||
| 117 | dVAR; dXSARGS; | ||
| 118 | #else | ||
| 119 | dXSARGS; | ||
| 120 | #endif | ||
| 121 | const char* file = __FILE__; | ||
| 122 | |||
| 123 | PERL_UNUSED_VAR(cv); /* -W */ | ||
| 124 | PERL_UNUSED_VAR(items); /* -W */ | ||
| 125 | XS_VERSION_BOOTCHECK ; | ||
| 126 | |||
| 127 | newXSproto("Perf::Trace::Context::common_pc", XS_Perf__Trace__Context_common_pc, file, "$"); | ||
| 128 | newXSproto("Perf::Trace::Context::common_flags", XS_Perf__Trace__Context_common_flags, file, "$"); | ||
| 129 | newXSproto("Perf::Trace::Context::common_lock_depth", XS_Perf__Trace__Context_common_lock_depth, file, "$"); | ||
| 130 | if (PL_unitcheckav) | ||
| 131 | call_list(PL_scopestack_ix, PL_unitcheckav); | ||
| 132 | XSRETURN_YES; | ||
| 133 | } | ||
| 134 | |||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/Context.xs b/tools/perf/scripts/perl/Perf-Trace-Util/Context.xs new file mode 100644 index 000000000000..fb78006c165e --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/Context.xs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | /* | ||
| 2 | * Context.xs. XS interfaces for perf trace. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 19 | * | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "EXTERN.h" | ||
| 23 | #include "perl.h" | ||
| 24 | #include "XSUB.h" | ||
| 25 | #include "../../../util/trace-event-perl.h" | ||
| 26 | |||
| 27 | MODULE = Perf::Trace::Context PACKAGE = Perf::Trace::Context | ||
| 28 | PROTOTYPES: ENABLE | ||
| 29 | |||
| 30 | int | ||
| 31 | common_pc(context) | ||
| 32 | struct scripting_context * context | ||
| 33 | |||
| 34 | int | ||
| 35 | common_flags(context) | ||
| 36 | struct scripting_context * context | ||
| 37 | |||
| 38 | int | ||
| 39 | common_lock_depth(context) | ||
| 40 | struct scripting_context * context | ||
| 41 | |||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/Makefile.PL b/tools/perf/scripts/perl/Perf-Trace-Util/Makefile.PL new file mode 100644 index 000000000000..decdeb0f6789 --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/Makefile.PL | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | use 5.010000; | ||
| 2 | use ExtUtils::MakeMaker; | ||
| 3 | # See lib/ExtUtils/MakeMaker.pm for details of how to influence | ||
| 4 | # the contents of the Makefile that is written. | ||
| 5 | WriteMakefile( | ||
| 6 | NAME => 'Perf::Trace::Context', | ||
| 7 | VERSION_FROM => 'lib/Perf/Trace/Context.pm', # finds $VERSION | ||
| 8 | PREREQ_PM => {}, # e.g., Module::Name => 1.1 | ||
| 9 | ($] >= 5.005 ? ## Add these new keywords supported since 5.005 | ||
| 10 | (ABSTRACT_FROM => 'lib/Perf/Trace/Context.pm', # retrieve abstract from module | ||
| 11 | AUTHOR => 'Tom Zanussi <tzanussi@gmail.com>') : ()), | ||
| 12 | LIBS => [''], # e.g., '-lm' | ||
| 13 | DEFINE => '-I ../..', # e.g., '-DHAVE_SOMETHING' | ||
| 14 | INC => '-I.', # e.g., '-I. -I/usr/include/other' | ||
| 15 | # Un-comment this if you add C files to link with later: | ||
| 16 | OBJECT => 'Context.o', # link all the C files too | ||
| 17 | ); | ||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/README b/tools/perf/scripts/perl/Perf-Trace-Util/README new file mode 100644 index 000000000000..9a9707630791 --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/README | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | Perf-Trace-Util version 0.01 | ||
| 2 | ============================ | ||
| 3 | |||
| 4 | This module contains utility functions for use with perf trace. | ||
| 5 | |||
| 6 | Core.pm and Util.pm are pure Perl modules; Core.pm contains routines | ||
| 7 | that the core perf support for Perl calls on and should always be | ||
| 8 | 'used', while Util.pm contains useful but optional utility functions | ||
| 9 | that scripts may want to use. Context.pm contains the Perl->C | ||
| 10 | interface that allows scripts to access data in the embedding perf | ||
| 11 | executable; scripts wishing to do that should 'use Context.pm'. | ||
| 12 | |||
| 13 | The Perl->C perf interface is completely driven by Context.xs. If you | ||
| 14 | want to add new Perl functions that end up accessing C data in the | ||
| 15 | perf executable, you add desciptions of the new functions here. | ||
| 16 | scripting_context is a pointer to the perf data in the perf executable | ||
| 17 | that you want to access - it's passed as the second parameter, | ||
| 18 | $context, to all handler functions. | ||
| 19 | |||
| 20 | After you do that: | ||
| 21 | |||
| 22 | perl Makefile.PL # to create a Makefile for the next step | ||
| 23 | make # to create Context.c | ||
| 24 | |||
| 25 | edit Context.c to add const to the char* file = __FILE__ line in | ||
| 26 | XS(boot_Perf__Trace__Context) to silence a warning/error. | ||
| 27 | |||
| 28 | You can delete the Makefile, object files and anything else that was | ||
| 29 | generated e.g. blib and shared library, etc, except for of course | ||
| 30 | Context.c | ||
| 31 | |||
| 32 | You should then be able to run the normal perf make as usual. | ||
| 33 | |||
| 34 | INSTALLATION | ||
| 35 | |||
| 36 | Building perf with perf trace Perl scripting should install this | ||
| 37 | module in the right place. | ||
| 38 | |||
| 39 | You should make sure libperl and ExtUtils/Embed.pm are installed first | ||
| 40 | e.g. apt-get install libperl-dev or yum install perl-ExtUtils-Embed. | ||
| 41 | |||
| 42 | DEPENDENCIES | ||
| 43 | |||
| 44 | This module requires these other modules and libraries: | ||
| 45 | |||
| 46 | None | ||
| 47 | |||
| 48 | COPYRIGHT AND LICENCE | ||
| 49 | |||
| 50 | Copyright (C) 2009 by Tom Zanussi <tzanussi@gmail.com> | ||
| 51 | |||
| 52 | This library is free software; you can redistribute it and/or modify | ||
| 53 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 54 | at your option, any later version of Perl 5 you may have available. | ||
| 55 | |||
| 56 | Alternatively, this software may be distributed under the terms of the | ||
| 57 | GNU General Public License ("GPL") version 2 as published by the Free | ||
| 58 | Software Foundation. | ||
| 59 | |||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Context.pm b/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Context.pm new file mode 100644 index 000000000000..6c7f3659cb17 --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Context.pm | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | package Perf::Trace::Context; | ||
| 2 | |||
| 3 | use 5.010000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 12 | ) ] ); | ||
| 13 | |||
| 14 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 15 | |||
| 16 | our @EXPORT = qw( | ||
| 17 | common_pc common_flags common_lock_depth | ||
| 18 | ); | ||
| 19 | |||
| 20 | our $VERSION = '0.01'; | ||
| 21 | |||
| 22 | require XSLoader; | ||
| 23 | XSLoader::load('Perf::Trace::Context', $VERSION); | ||
| 24 | |||
| 25 | 1; | ||
| 26 | __END__ | ||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Perf::Trace::Context - Perl extension for accessing functions in perf. | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use Perf::Trace::Context; | ||
| 34 | |||
| 35 | =head1 SEE ALSO | ||
| 36 | |||
| 37 | Perf (trace) documentation | ||
| 38 | |||
| 39 | =head1 AUTHOR | ||
| 40 | |||
| 41 | Tom Zanussi, E<lt>tzanussi@gmail.com<gt> | ||
| 42 | |||
| 43 | =head1 COPYRIGHT AND LICENSE | ||
| 44 | |||
| 45 | Copyright (C) 2009 by Tom Zanussi | ||
| 46 | |||
| 47 | This library is free software; you can redistribute it and/or modify | ||
| 48 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 49 | at your option, any later version of Perl 5 you may have available. | ||
| 50 | |||
| 51 | Alternatively, this software may be distributed under the terms of the | ||
| 52 | GNU General Public License ("GPL") version 2 as published by the Free | ||
| 53 | Software Foundation. | ||
| 54 | |||
| 55 | =cut | ||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Core.pm b/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Core.pm new file mode 100644 index 000000000000..9df376a9f629 --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Core.pm | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | package Perf::Trace::Core; | ||
| 2 | |||
| 3 | use 5.010000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 12 | ) ] ); | ||
| 13 | |||
| 14 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 15 | |||
| 16 | our @EXPORT = qw( | ||
| 17 | define_flag_field define_flag_value flag_str dump_flag_fields | ||
| 18 | define_symbolic_field define_symbolic_value symbol_str dump_symbolic_fields | ||
| 19 | trace_flag_str | ||
| 20 | ); | ||
| 21 | |||
| 22 | our $VERSION = '0.01'; | ||
| 23 | |||
| 24 | my %trace_flags = (0x00 => "NONE", | ||
| 25 | 0x01 => "IRQS_OFF", | ||
| 26 | 0x02 => "IRQS_NOSUPPORT", | ||
| 27 | 0x04 => "NEED_RESCHED", | ||
| 28 | 0x08 => "HARDIRQ", | ||
| 29 | 0x10 => "SOFTIRQ"); | ||
| 30 | |||
| 31 | sub trace_flag_str | ||
| 32 | { | ||
| 33 | my ($value) = @_; | ||
| 34 | |||
| 35 | my $string; | ||
| 36 | |||
| 37 | my $print_delim = 0; | ||
| 38 | |||
| 39 | foreach my $idx (sort {$a <=> $b} keys %trace_flags) { | ||
| 40 | if (!$value && !$idx) { | ||
| 41 | $string .= "NONE"; | ||
| 42 | last; | ||
| 43 | } | ||
| 44 | |||
| 45 | if ($idx && ($value & $idx) == $idx) { | ||
| 46 | if ($print_delim) { | ||
| 47 | $string .= " | "; | ||
| 48 | } | ||
| 49 | $string .= "$trace_flags{$idx}"; | ||
| 50 | $print_delim = 1; | ||
| 51 | $value &= ~$idx; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | return $string; | ||
| 56 | } | ||
| 57 | |||
| 58 | my %flag_fields; | ||
| 59 | my %symbolic_fields; | ||
| 60 | |||
| 61 | sub flag_str | ||
| 62 | { | ||
| 63 | my ($event_name, $field_name, $value) = @_; | ||
| 64 | |||
| 65 | my $string; | ||
| 66 | |||
| 67 | if ($flag_fields{$event_name}{$field_name}) { | ||
| 68 | my $print_delim = 0; | ||
| 69 | foreach my $idx (sort {$a <=> $b} keys %{$flag_fields{$event_name}{$field_name}{"values"}}) { | ||
| 70 | if (!$value && !$idx) { | ||
| 71 | $string .= "$flag_fields{$event_name}{$field_name}{'values'}{$idx}"; | ||
| 72 | last; | ||
| 73 | } | ||
| 74 | if ($idx && ($value & $idx) == $idx) { | ||
| 75 | if ($print_delim && $flag_fields{$event_name}{$field_name}{'delim'}) { | ||
| 76 | $string .= " $flag_fields{$event_name}{$field_name}{'delim'} "; | ||
| 77 | } | ||
| 78 | $string .= "$flag_fields{$event_name}{$field_name}{'values'}{$idx}"; | ||
| 79 | $print_delim = 1; | ||
| 80 | $value &= ~$idx; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | return $string; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub define_flag_field | ||
| 89 | { | ||
| 90 | my ($event_name, $field_name, $delim) = @_; | ||
| 91 | |||
| 92 | $flag_fields{$event_name}{$field_name}{"delim"} = $delim; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub define_flag_value | ||
| 96 | { | ||
| 97 | my ($event_name, $field_name, $value, $field_str) = @_; | ||
| 98 | |||
| 99 | $flag_fields{$event_name}{$field_name}{"values"}{$value} = $field_str; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub dump_flag_fields | ||
| 103 | { | ||
| 104 | for my $event (keys %flag_fields) { | ||
| 105 | print "event $event:\n"; | ||
| 106 | for my $field (keys %{$flag_fields{$event}}) { | ||
| 107 | print " field: $field:\n"; | ||
| 108 | print " delim: $flag_fields{$event}{$field}{'delim'}\n"; | ||
| 109 | foreach my $idx (sort {$a <=> $b} keys %{$flag_fields{$event}{$field}{"values"}}) { | ||
| 110 | print " value $idx: $flag_fields{$event}{$field}{'values'}{$idx}\n"; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub symbol_str | ||
| 117 | { | ||
| 118 | my ($event_name, $field_name, $value) = @_; | ||
| 119 | |||
| 120 | if ($symbolic_fields{$event_name}{$field_name}) { | ||
| 121 | foreach my $idx (sort {$a <=> $b} keys %{$symbolic_fields{$event_name}{$field_name}{"values"}}) { | ||
| 122 | if (!$value && !$idx) { | ||
| 123 | return "$symbolic_fields{$event_name}{$field_name}{'values'}{$idx}"; | ||
| 124 | last; | ||
| 125 | } | ||
| 126 | if ($value == $idx) { | ||
| 127 | return "$symbolic_fields{$event_name}{$field_name}{'values'}{$idx}"; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | return undef; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub define_symbolic_field | ||
| 136 | { | ||
| 137 | my ($event_name, $field_name) = @_; | ||
| 138 | |||
| 139 | # nothing to do, really | ||
| 140 | } | ||
| 141 | |||
| 142 | sub define_symbolic_value | ||
| 143 | { | ||
| 144 | my ($event_name, $field_name, $value, $field_str) = @_; | ||
| 145 | |||
| 146 | $symbolic_fields{$event_name}{$field_name}{"values"}{$value} = $field_str; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub dump_symbolic_fields | ||
| 150 | { | ||
| 151 | for my $event (keys %symbolic_fields) { | ||
| 152 | print "event $event:\n"; | ||
| 153 | for my $field (keys %{$symbolic_fields{$event}}) { | ||
| 154 | print " field: $field:\n"; | ||
| 155 | foreach my $idx (sort {$a <=> $b} keys %{$symbolic_fields{$event}{$field}{"values"}}) { | ||
| 156 | print " value $idx: $symbolic_fields{$event}{$field}{'values'}{$idx}\n"; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | __END__ | ||
| 164 | =head1 NAME | ||
| 165 | |||
| 166 | Perf::Trace::Core - Perl extension for perf trace | ||
| 167 | |||
| 168 | =head1 SYNOPSIS | ||
| 169 | |||
| 170 | use Perf::Trace::Core | ||
| 171 | |||
| 172 | =head1 SEE ALSO | ||
| 173 | |||
| 174 | Perf (trace) documentation | ||
| 175 | |||
| 176 | =head1 AUTHOR | ||
| 177 | |||
| 178 | Tom Zanussi, E<lt>tzanussi@gmail.com<gt> | ||
| 179 | |||
| 180 | =head1 COPYRIGHT AND LICENSE | ||
| 181 | |||
| 182 | Copyright (C) 2009 by Tom Zanussi | ||
| 183 | |||
| 184 | This library is free software; you can redistribute it and/or modify | ||
| 185 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 186 | at your option, any later version of Perl 5 you may have available. | ||
| 187 | |||
| 188 | Alternatively, this software may be distributed under the terms of the | ||
| 189 | GNU General Public License ("GPL") version 2 as published by the Free | ||
| 190 | Software Foundation. | ||
| 191 | |||
| 192 | =cut | ||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Util.pm b/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Util.pm new file mode 100644 index 000000000000..052f132ced24 --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Util.pm | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | package Perf::Trace::Util; | ||
| 2 | |||
| 3 | use 5.010000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 12 | ) ] ); | ||
| 13 | |||
| 14 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 15 | |||
| 16 | our @EXPORT = qw( | ||
| 17 | avg nsecs nsecs_secs nsecs_nsecs nsecs_usecs print_nsecs | ||
| 18 | ); | ||
| 19 | |||
| 20 | our $VERSION = '0.01'; | ||
| 21 | |||
| 22 | sub avg | ||
| 23 | { | ||
| 24 | my ($total, $n) = @_; | ||
| 25 | |||
| 26 | return $total / $n; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $NSECS_PER_SEC = 1000000000; | ||
| 30 | |||
| 31 | sub nsecs | ||
| 32 | { | ||
| 33 | my ($secs, $nsecs) = @_; | ||
| 34 | |||
| 35 | return $secs * $NSECS_PER_SEC + $nsecs; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub nsecs_secs { | ||
| 39 | my ($nsecs) = @_; | ||
| 40 | |||
| 41 | return $nsecs / $NSECS_PER_SEC; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub nsecs_nsecs { | ||
| 45 | my ($nsecs) = @_; | ||
| 46 | |||
| 47 | return $nsecs - nsecs_secs($nsecs); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub nsecs_str { | ||
| 51 | my ($nsecs) = @_; | ||
| 52 | |||
| 53 | my $str = sprintf("%5u.%09u", nsecs_secs($nsecs), nsecs_nsecs($nsecs)); | ||
| 54 | |||
| 55 | return $str; | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | __END__ | ||
| 60 | =head1 NAME | ||
| 61 | |||
| 62 | Perf::Trace::Util - Perl extension for perf trace | ||
| 63 | |||
| 64 | =head1 SYNOPSIS | ||
| 65 | |||
| 66 | use Perf::Trace::Util; | ||
| 67 | |||
| 68 | =head1 SEE ALSO | ||
| 69 | |||
| 70 | Perf (trace) documentation | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Tom Zanussi, E<lt>tzanussi@gmail.com<gt> | ||
| 75 | |||
| 76 | =head1 COPYRIGHT AND LICENSE | ||
| 77 | |||
| 78 | Copyright (C) 2009 by Tom Zanussi | ||
| 79 | |||
| 80 | This library is free software; you can redistribute it and/or modify | ||
| 81 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 82 | at your option, any later version of Perl 5 you may have available. | ||
| 83 | |||
| 84 | Alternatively, this software may be distributed under the terms of the | ||
| 85 | GNU General Public License ("GPL") version 2 as published by the Free | ||
| 86 | Software Foundation. | ||
| 87 | |||
| 88 | =cut | ||
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/typemap b/tools/perf/scripts/perl/Perf-Trace-Util/typemap new file mode 100644 index 000000000000..840836804aa7 --- /dev/null +++ b/tools/perf/scripts/perl/Perf-Trace-Util/typemap | |||
| @@ -0,0 +1 @@ | |||
| struct scripting_context * T_PTR | |||
diff --git a/tools/perf/scripts/perl/bin/check-perf-trace-record b/tools/perf/scripts/perl/bin/check-perf-trace-record new file mode 100644 index 000000000000..c7ec5de2f535 --- /dev/null +++ b/tools/perf/scripts/perl/bin/check-perf-trace-record | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf record -c 1 -f -a -M -R -e kmem:kmalloc -e irq:softirq_entry | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
diff --git a/tools/perf/scripts/perl/bin/check-perf-trace-report b/tools/perf/scripts/perl/bin/check-perf-trace-report new file mode 100644 index 000000000000..89948b015020 --- /dev/null +++ b/tools/perf/scripts/perl/bin/check-perf-trace-report | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf trace -s ~/libexec/perf-core/scripts/perl/check-perf-trace.pl | ||
| 3 | |||
| 4 | |||
| 5 | |||
diff --git a/tools/perf/scripts/perl/bin/rw-by-file-record b/tools/perf/scripts/perl/bin/rw-by-file-record new file mode 100644 index 000000000000..b25056ebf963 --- /dev/null +++ b/tools/perf/scripts/perl/bin/rw-by-file-record | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf record -c 1 -f -a -M -R -e syscalls:sys_enter_read -e syscalls:sys_enter_write | ||
diff --git a/tools/perf/scripts/perl/bin/rw-by-file-report b/tools/perf/scripts/perl/bin/rw-by-file-report new file mode 100644 index 000000000000..f5dcf9cb5bd2 --- /dev/null +++ b/tools/perf/scripts/perl/bin/rw-by-file-report | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf trace -s ~/libexec/perf-core/scripts/perl/rw-by-file.pl | ||
| 3 | |||
| 4 | |||
| 5 | |||
diff --git a/tools/perf/scripts/perl/bin/rw-by-pid-record b/tools/perf/scripts/perl/bin/rw-by-pid-record new file mode 100644 index 000000000000..8903979c5b6c --- /dev/null +++ b/tools/perf/scripts/perl/bin/rw-by-pid-record | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf record -c 1 -f -a -M -R -e syscalls:sys_enter_read -e syscalls:sys_exit_read -e syscalls:sys_enter_write -e syscalls:sys_exit_write | ||
diff --git a/tools/perf/scripts/perl/bin/rw-by-pid-report b/tools/perf/scripts/perl/bin/rw-by-pid-report new file mode 100644 index 000000000000..cea16f78a3a2 --- /dev/null +++ b/tools/perf/scripts/perl/bin/rw-by-pid-report | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf trace -s ~/libexec/perf-core/scripts/perl/rw-by-pid.pl | ||
| 3 | |||
| 4 | |||
| 5 | |||
diff --git a/tools/perf/scripts/perl/bin/wakeup-latency-record b/tools/perf/scripts/perl/bin/wakeup-latency-record new file mode 100644 index 000000000000..6abedda911a4 --- /dev/null +++ b/tools/perf/scripts/perl/bin/wakeup-latency-record | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf record -c 1 -f -a -M -R -e sched:sched_switch -e sched:sched_wakeup | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
diff --git a/tools/perf/scripts/perl/bin/wakeup-latency-report b/tools/perf/scripts/perl/bin/wakeup-latency-report new file mode 100644 index 000000000000..85769dc456eb --- /dev/null +++ b/tools/perf/scripts/perl/bin/wakeup-latency-report | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf trace -s ~/libexec/perf-core/scripts/perl/wakeup-latency.pl | ||
| 3 | |||
| 4 | |||
| 5 | |||
diff --git a/tools/perf/scripts/perl/bin/workqueue-stats-record b/tools/perf/scripts/perl/bin/workqueue-stats-record new file mode 100644 index 000000000000..fce6637b19ba --- /dev/null +++ b/tools/perf/scripts/perl/bin/workqueue-stats-record | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf record -c 1 -f -a -M -R -e workqueue:workqueue_creation -e workqueue:workqueue_destruction -e workqueue:workqueue_execution -e workqueue:workqueue_insertion | ||
diff --git a/tools/perf/scripts/perl/bin/workqueue-stats-report b/tools/perf/scripts/perl/bin/workqueue-stats-report new file mode 100644 index 000000000000..aa68435be926 --- /dev/null +++ b/tools/perf/scripts/perl/bin/workqueue-stats-report | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | perf trace -s ~/libexec/perf-core/scripts/perl/workqueue-stats.pl | ||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
diff --git a/tools/perf/scripts/perl/check-perf-trace.pl b/tools/perf/scripts/perl/check-perf-trace.pl new file mode 100644 index 000000000000..4e7dc0a407a5 --- /dev/null +++ b/tools/perf/scripts/perl/check-perf-trace.pl | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | # perf trace event handlers, generated by perf trace -g perl | ||
| 2 | # (c) 2009, Tom Zanussi <tzanussi@gmail.com> | ||
| 3 | # Licensed under the terms of the GNU GPL License version 2 | ||
| 4 | |||
| 5 | # This script tests basic functionality such as flag and symbol | ||
| 6 | # strings, common_xxx() calls back into perf, begin, end, unhandled | ||
| 7 | # events, etc. Basically, if this script runs successfully and | ||
| 8 | # displays expected results, perl scripting support should be ok. | ||
| 9 | |||
| 10 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; | ||
| 11 | use lib "./Perf-Trace-Util/lib"; | ||
| 12 | use Perf::Trace::Core; | ||
| 13 | use Perf::Trace::Context; | ||
| 14 | use Perf::Trace::Util; | ||
| 15 | |||
| 16 | sub trace_begin | ||
| 17 | { | ||
| 18 | print "trace_begin\n"; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub trace_end | ||
| 22 | { | ||
| 23 | print "trace_end\n"; | ||
| 24 | |||
| 25 | print_unhandled(); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub irq::softirq_entry | ||
| 29 | { | ||
| 30 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 31 | $common_pid, $common_comm, | ||
| 32 | $vec) = @_; | ||
| 33 | |||
| 34 | print_header($event_name, $common_cpu, $common_secs, $common_nsecs, | ||
| 35 | $common_pid, $common_comm); | ||
| 36 | |||
| 37 | print_uncommon($context); | ||
| 38 | |||
| 39 | printf("vec=%s\n", | ||
| 40 | symbol_str("irq::softirq_entry", "vec", $vec)); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub kmem::kmalloc | ||
| 44 | { | ||
| 45 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 46 | $common_pid, $common_comm, | ||
| 47 | $call_site, $ptr, $bytes_req, $bytes_alloc, | ||
| 48 | $gfp_flags) = @_; | ||
| 49 | |||
| 50 | print_header($event_name, $common_cpu, $common_secs, $common_nsecs, | ||
| 51 | $common_pid, $common_comm); | ||
| 52 | |||
| 53 | print_uncommon($context); | ||
| 54 | |||
| 55 | printf("call_site=%p, ptr=%p, bytes_req=%u, bytes_alloc=%u, ". | ||
| 56 | "gfp_flags=%s\n", | ||
| 57 | $call_site, $ptr, $bytes_req, $bytes_alloc, | ||
| 58 | |||
| 59 | flag_str("kmem::kmalloc", "gfp_flags", $gfp_flags)); | ||
| 60 | } | ||
| 61 | |||
| 62 | # print trace fields not included in handler args | ||
| 63 | sub print_uncommon | ||
| 64 | { | ||
| 65 | my ($context) = @_; | ||
| 66 | |||
| 67 | printf("common_preempt_count=%d, common_flags=%s, common_lock_depth=%d, ", | ||
| 68 | common_pc($context), trace_flag_str(common_flags($context)), | ||
| 69 | common_lock_depth($context)); | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | my %unhandled; | ||
| 74 | |||
| 75 | sub print_unhandled | ||
| 76 | { | ||
| 77 | if ((scalar keys %unhandled) == 0) { | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | print "\nunhandled events:\n\n"; | ||
| 82 | |||
| 83 | printf("%-40s %10s\n", "event", "count"); | ||
| 84 | printf("%-40s %10s\n", "----------------------------------------", | ||
| 85 | "-----------"); | ||
| 86 | |||
| 87 | foreach my $event_name (keys %unhandled) { | ||
| 88 | printf("%-40s %10d\n", $event_name, $unhandled{$event_name}); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub trace_unhandled | ||
| 93 | { | ||
| 94 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 95 | $common_pid, $common_comm) = @_; | ||
| 96 | |||
| 97 | $unhandled{$event_name}++; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub print_header | ||
| 101 | { | ||
| 102 | my ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_; | ||
| 103 | |||
| 104 | printf("%-20s %5u %05u.%09u %8u %-20s ", | ||
| 105 | $event_name, $cpu, $secs, $nsecs, $pid, $comm); | ||
| 106 | } | ||
diff --git a/tools/perf/scripts/perl/rw-by-file.pl b/tools/perf/scripts/perl/rw-by-file.pl new file mode 100644 index 000000000000..61f91561d848 --- /dev/null +++ b/tools/perf/scripts/perl/rw-by-file.pl | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # (c) 2009, Tom Zanussi <tzanussi@gmail.com> | ||
| 3 | # Licensed under the terms of the GNU GPL License version 2 | ||
| 4 | |||
| 5 | # Display r/w activity for files read/written to for a given program | ||
| 6 | |||
| 7 | # The common_* event handler fields are the most useful fields common to | ||
| 8 | # all events. They don't necessarily correspond to the 'common_*' fields | ||
| 9 | # in the status files. Those fields not available as handler params can | ||
| 10 | # be retrieved via script functions of the form get_common_*(). | ||
| 11 | |||
| 12 | use 5.010000; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | |||
| 16 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; | ||
| 17 | use lib "./Perf-Trace-Util/lib"; | ||
| 18 | use Perf::Trace::Core; | ||
| 19 | use Perf::Trace::Util; | ||
| 20 | |||
| 21 | # change this to the comm of the program you're interested in | ||
| 22 | my $for_comm = "perf"; | ||
| 23 | |||
| 24 | my %reads; | ||
| 25 | my %writes; | ||
| 26 | |||
| 27 | sub syscalls::sys_enter_read | ||
| 28 | { | ||
| 29 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 30 | $common_pid, $common_comm, $nr, $fd, $buf, $count) = @_; | ||
| 31 | |||
| 32 | if ($common_comm eq $for_comm) { | ||
| 33 | $reads{$fd}{bytes_requested} += $count; | ||
| 34 | $reads{$fd}{total_reads}++; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub syscalls::sys_enter_write | ||
| 39 | { | ||
| 40 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 41 | $common_pid, $common_comm, $nr, $fd, $buf, $count) = @_; | ||
| 42 | |||
| 43 | if ($common_comm eq $for_comm) { | ||
| 44 | $writes{$fd}{bytes_written} += $count; | ||
| 45 | $writes{$fd}{total_writes}++; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | sub trace_end | ||
| 50 | { | ||
| 51 | printf("file read counts for $for_comm:\n\n"); | ||
| 52 | |||
| 53 | printf("%6s %10s %10s\n", "fd", "# reads", "bytes_requested"); | ||
| 54 | printf("%6s %10s %10s\n", "------", "----------", "-----------"); | ||
| 55 | |||
| 56 | foreach my $fd (sort {$reads{$b}{bytes_requested} <=> | ||
| 57 | $reads{$a}{bytes_requested}} keys %reads) { | ||
| 58 | my $total_reads = $reads{$fd}{total_reads}; | ||
| 59 | my $bytes_requested = $reads{$fd}{bytes_requested}; | ||
| 60 | printf("%6u %10u %10u\n", $fd, $total_reads, $bytes_requested); | ||
| 61 | } | ||
| 62 | |||
| 63 | printf("\nfile write counts for $for_comm:\n\n"); | ||
| 64 | |||
| 65 | printf("%6s %10s %10s\n", "fd", "# writes", "bytes_written"); | ||
| 66 | printf("%6s %10s %10s\n", "------", "----------", "-----------"); | ||
| 67 | |||
| 68 | foreach my $fd (sort {$writes{$b}{bytes_written} <=> | ||
| 69 | $writes{$a}{bytes_written}} keys %writes) { | ||
| 70 | my $total_writes = $writes{$fd}{total_writes}; | ||
| 71 | my $bytes_written = $writes{$fd}{bytes_written}; | ||
| 72 | printf("%6u %10u %10u\n", $fd, $total_writes, $bytes_written); | ||
| 73 | } | ||
| 74 | |||
| 75 | print_unhandled(); | ||
| 76 | } | ||
| 77 | |||
| 78 | my %unhandled; | ||
| 79 | |||
| 80 | sub print_unhandled | ||
| 81 | { | ||
| 82 | if ((scalar keys %unhandled) == 0) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | print "\nunhandled events:\n\n"; | ||
| 87 | |||
| 88 | printf("%-40s %10s\n", "event", "count"); | ||
| 89 | printf("%-40s %10s\n", "----------------------------------------", | ||
| 90 | "-----------"); | ||
| 91 | |||
| 92 | foreach my $event_name (keys %unhandled) { | ||
| 93 | printf("%-40s %10d\n", $event_name, $unhandled{$event_name}); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub trace_unhandled | ||
| 98 | { | ||
| 99 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 100 | $common_pid, $common_comm) = @_; | ||
| 101 | |||
| 102 | $unhandled{$event_name}++; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
diff --git a/tools/perf/scripts/perl/rw-by-pid.pl b/tools/perf/scripts/perl/rw-by-pid.pl new file mode 100644 index 000000000000..da601fae1a00 --- /dev/null +++ b/tools/perf/scripts/perl/rw-by-pid.pl | |||
| @@ -0,0 +1,170 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # (c) 2009, Tom Zanussi <tzanussi@gmail.com> | ||
| 3 | # Licensed under the terms of the GNU GPL License version 2 | ||
| 4 | |||
| 5 | # Display r/w activity for all processes | ||
| 6 | |||
| 7 | # The common_* event handler fields are the most useful fields common to | ||
| 8 | # all events. They don't necessarily correspond to the 'common_*' fields | ||
| 9 | # in the status files. Those fields not available as handler params can | ||
| 10 | # be retrieved via script functions of the form get_common_*(). | ||
| 11 | |||
| 12 | use 5.010000; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | |||
| 16 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; | ||
| 17 | use lib "./Perf-Trace-Util/lib"; | ||
| 18 | use Perf::Trace::Core; | ||
| 19 | use Perf::Trace::Util; | ||
| 20 | |||
| 21 | my %reads; | ||
| 22 | my %writes; | ||
| 23 | |||
| 24 | sub syscalls::sys_exit_read | ||
| 25 | { | ||
| 26 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 27 | $common_pid, $common_comm, | ||
| 28 | $nr, $ret) = @_; | ||
| 29 | |||
| 30 | if ($ret > 0) { | ||
| 31 | $reads{$common_pid}{bytes_read} += $ret; | ||
| 32 | } else { | ||
| 33 | if (!defined ($reads{$common_pid}{bytes_read})) { | ||
| 34 | $reads{$common_pid}{bytes_read} = 0; | ||
| 35 | } | ||
| 36 | $reads{$common_pid}{errors}{$ret}++; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | sub syscalls::sys_enter_read | ||
| 41 | { | ||
| 42 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 43 | $common_pid, $common_comm, | ||
| 44 | $nr, $fd, $buf, $count) = @_; | ||
| 45 | |||
| 46 | $reads{$common_pid}{bytes_requested} += $count; | ||
| 47 | $reads{$common_pid}{total_reads}++; | ||
| 48 | $reads{$common_pid}{comm} = $common_comm; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub syscalls::sys_exit_write | ||
| 52 | { | ||
| 53 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 54 | $common_pid, $common_comm, | ||
| 55 | $nr, $ret) = @_; | ||
| 56 | |||
| 57 | if ($ret <= 0) { | ||
| 58 | $writes{$common_pid}{errors}{$ret}++; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub syscalls::sys_enter_write | ||
| 63 | { | ||
| 64 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 65 | $common_pid, $common_comm, | ||
| 66 | $nr, $fd, $buf, $count) = @_; | ||
| 67 | |||
| 68 | $writes{$common_pid}{bytes_written} += $count; | ||
| 69 | $writes{$common_pid}{total_writes}++; | ||
| 70 | $writes{$common_pid}{comm} = $common_comm; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub trace_end | ||
| 74 | { | ||
| 75 | printf("read counts by pid:\n\n"); | ||
| 76 | |||
| 77 | printf("%6s %20s %10s %10s %10s\n", "pid", "comm", | ||
| 78 | "# reads", "bytes_requested", "bytes_read"); | ||
| 79 | printf("%6s %-20s %10s %10s %10s\n", "------", "--------------------", | ||
| 80 | "-----------", "----------", "----------"); | ||
| 81 | |||
| 82 | foreach my $pid (sort {$reads{$b}{bytes_read} <=> | ||
| 83 | $reads{$a}{bytes_read}} keys %reads) { | ||
| 84 | my $comm = $reads{$pid}{comm}; | ||
| 85 | my $total_reads = $reads{$pid}{total_reads}; | ||
| 86 | my $bytes_requested = $reads{$pid}{bytes_requested}; | ||
| 87 | my $bytes_read = $reads{$pid}{bytes_read}; | ||
| 88 | |||
| 89 | printf("%6s %-20s %10s %10s %10s\n", $pid, $comm, | ||
| 90 | $total_reads, $bytes_requested, $bytes_read); | ||
| 91 | } | ||
| 92 | |||
| 93 | printf("\nfailed reads by pid:\n\n"); | ||
| 94 | |||
| 95 | printf("%6s %20s %6s %10s\n", "pid", "comm", "error #", "# errors"); | ||
| 96 | printf("%6s %20s %6s %10s\n", "------", "--------------------", | ||
| 97 | "------", "----------"); | ||
| 98 | |||
| 99 | foreach my $pid (keys %reads) { | ||
| 100 | my $comm = $reads{$pid}{comm}; | ||
| 101 | foreach my $err (sort {$reads{$b}{comm} cmp $reads{$a}{comm}} | ||
| 102 | keys %{$reads{$pid}{errors}}) { | ||
| 103 | my $errors = $reads{$pid}{errors}{$err}; | ||
| 104 | |||
| 105 | printf("%6d %-20s %6d %10s\n", $pid, $comm, $err, $errors); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | printf("\nwrite counts by pid:\n\n"); | ||
| 110 | |||
| 111 | printf("%6s %20s %10s %10s\n", "pid", "comm", | ||
| 112 | "# writes", "bytes_written"); | ||
| 113 | printf("%6s %-20s %10s %10s\n", "------", "--------------------", | ||
| 114 | "-----------", "----------"); | ||
| 115 | |||
| 116 | foreach my $pid (sort {$writes{$b}{bytes_written} <=> | ||
| 117 | $writes{$a}{bytes_written}} keys %writes) { | ||
| 118 | my $comm = $writes{$pid}{comm}; | ||
| 119 | my $total_writes = $writes{$pid}{total_writes}; | ||
| 120 | my $bytes_written = $writes{$pid}{bytes_written}; | ||
| 121 | |||
| 122 | printf("%6s %-20s %10s %10s\n", $pid, $comm, | ||
| 123 | $total_writes, $bytes_written); | ||
| 124 | } | ||
| 125 | |||
| 126 | printf("\nfailed writes by pid:\n\n"); | ||
| 127 | |||
| 128 | printf("%6s %20s %6s %10s\n", "pid", "comm", "error #", "# errors"); | ||
| 129 | printf("%6s %20s %6s %10s\n", "------", "--------------------", | ||
| 130 | "------", "----------"); | ||
| 131 | |||
| 132 | foreach my $pid (keys %writes) { | ||
| 133 | my $comm = $writes{$pid}{comm}; | ||
| 134 | foreach my $err (sort {$writes{$b}{comm} cmp $writes{$a}{comm}} | ||
| 135 | keys %{$writes{$pid}{errors}}) { | ||
| 136 | my $errors = $writes{$pid}{errors}{$err}; | ||
| 137 | |||
| 138 | printf("%6d %-20s %6d %10s\n", $pid, $comm, $err, $errors); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | print_unhandled(); | ||
| 143 | } | ||
| 144 | |||
| 145 | my %unhandled; | ||
| 146 | |||
| 147 | sub print_unhandled | ||
| 148 | { | ||
| 149 | if ((scalar keys %unhandled) == 0) { | ||
| 150 | return; | ||
| 151 | } | ||
| 152 | |||
| 153 | print "\nunhandled events:\n\n"; | ||
| 154 | |||
| 155 | printf("%-40s %10s\n", "event", "count"); | ||
| 156 | printf("%-40s %10s\n", "----------------------------------------", | ||
| 157 | "-----------"); | ||
| 158 | |||
| 159 | foreach my $event_name (keys %unhandled) { | ||
| 160 | printf("%-40s %10d\n", $event_name, $unhandled{$event_name}); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | sub trace_unhandled | ||
| 165 | { | ||
| 166 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 167 | $common_pid, $common_comm) = @_; | ||
| 168 | |||
| 169 | $unhandled{$event_name}++; | ||
| 170 | } | ||
diff --git a/tools/perf/scripts/perl/wakeup-latency.pl b/tools/perf/scripts/perl/wakeup-latency.pl new file mode 100644 index 000000000000..ed58ef284e23 --- /dev/null +++ b/tools/perf/scripts/perl/wakeup-latency.pl | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # (c) 2009, Tom Zanussi <tzanussi@gmail.com> | ||
| 3 | # Licensed under the terms of the GNU GPL License version 2 | ||
| 4 | |||
| 5 | # Display avg/min/max wakeup latency | ||
| 6 | |||
| 7 | # The common_* event handler fields are the most useful fields common to | ||
| 8 | # all events. They don't necessarily correspond to the 'common_*' fields | ||
| 9 | # in the status files. Those fields not available as handler params can | ||
| 10 | # be retrieved via script functions of the form get_common_*(). | ||
| 11 | |||
| 12 | use 5.010000; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | |||
| 16 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; | ||
| 17 | use lib "./Perf-Trace-Util/lib"; | ||
| 18 | use Perf::Trace::Core; | ||
| 19 | use Perf::Trace::Util; | ||
| 20 | |||
| 21 | my %last_wakeup; | ||
| 22 | |||
| 23 | my $max_wakeup_latency; | ||
| 24 | my $min_wakeup_latency; | ||
| 25 | my $total_wakeup_latency; | ||
| 26 | my $total_wakeups; | ||
| 27 | |||
| 28 | sub sched::sched_switch | ||
| 29 | { | ||
| 30 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 31 | $common_pid, $common_comm, | ||
| 32 | $prev_comm, $prev_pid, $prev_prio, $prev_state, $next_comm, $next_pid, | ||
| 33 | $next_prio) = @_; | ||
| 34 | |||
| 35 | my $wakeup_ts = $last_wakeup{$common_cpu}{ts}; | ||
| 36 | if ($wakeup_ts) { | ||
| 37 | my $switch_ts = nsecs($common_secs, $common_nsecs); | ||
| 38 | my $wakeup_latency = $switch_ts - $wakeup_ts; | ||
| 39 | if ($wakeup_latency > $max_wakeup_latency) { | ||
| 40 | $max_wakeup_latency = $wakeup_latency; | ||
| 41 | } | ||
| 42 | if ($wakeup_latency < $min_wakeup_latency) { | ||
| 43 | $min_wakeup_latency = $wakeup_latency; | ||
| 44 | } | ||
| 45 | $total_wakeup_latency += $wakeup_latency; | ||
| 46 | $total_wakeups++; | ||
| 47 | } | ||
| 48 | $last_wakeup{$common_cpu}{ts} = 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub sched::sched_wakeup | ||
| 52 | { | ||
| 53 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 54 | $common_pid, $common_comm, | ||
| 55 | $comm, $pid, $prio, $success, $target_cpu) = @_; | ||
| 56 | |||
| 57 | $last_wakeup{$target_cpu}{ts} = nsecs($common_secs, $common_nsecs); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub trace_begin | ||
| 61 | { | ||
| 62 | $min_wakeup_latency = 1000000000; | ||
| 63 | $max_wakeup_latency = 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub trace_end | ||
| 67 | { | ||
| 68 | printf("wakeup_latency stats:\n\n"); | ||
| 69 | print "total_wakeups: $total_wakeups\n"; | ||
| 70 | printf("avg_wakeup_latency (ns): %u\n", | ||
| 71 | avg($total_wakeup_latency, $total_wakeups)); | ||
| 72 | printf("min_wakeup_latency (ns): %u\n", $min_wakeup_latency); | ||
| 73 | printf("max_wakeup_latency (ns): %u\n", $max_wakeup_latency); | ||
| 74 | |||
| 75 | print_unhandled(); | ||
| 76 | } | ||
| 77 | |||
| 78 | my %unhandled; | ||
| 79 | |||
| 80 | sub print_unhandled | ||
| 81 | { | ||
| 82 | if ((scalar keys %unhandled) == 0) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | print "\nunhandled events:\n\n"; | ||
| 87 | |||
| 88 | printf("%-40s %10s\n", "event", "count"); | ||
| 89 | printf("%-40s %10s\n", "----------------------------------------", | ||
| 90 | "-----------"); | ||
| 91 | |||
| 92 | foreach my $event_name (keys %unhandled) { | ||
| 93 | printf("%-40s %10d\n", $event_name, $unhandled{$event_name}); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub trace_unhandled | ||
| 98 | { | ||
| 99 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 100 | $common_pid, $common_comm) = @_; | ||
| 101 | |||
| 102 | $unhandled{$event_name}++; | ||
| 103 | } | ||
diff --git a/tools/perf/scripts/perl/workqueue-stats.pl b/tools/perf/scripts/perl/workqueue-stats.pl new file mode 100644 index 000000000000..511302c8a494 --- /dev/null +++ b/tools/perf/scripts/perl/workqueue-stats.pl | |||
| @@ -0,0 +1,129 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # (c) 2009, Tom Zanussi <tzanussi@gmail.com> | ||
| 3 | # Licensed under the terms of the GNU GPL License version 2 | ||
| 4 | |||
| 5 | # Displays workqueue stats | ||
| 6 | # | ||
| 7 | # Usage: | ||
| 8 | # | ||
| 9 | # perf record -c 1 -f -a -R -e workqueue:workqueue_creation -e | ||
| 10 | # workqueue:workqueue_destruction -e workqueue:workqueue_execution | ||
| 11 | # -e workqueue:workqueue_insertion | ||
| 12 | # | ||
| 13 | # perf trace -p -s tools/perf/scripts/perl/workqueue-stats.pl | ||
| 14 | |||
| 15 | use 5.010000; | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; | ||
| 20 | use lib "./Perf-Trace-Util/lib"; | ||
| 21 | use Perf::Trace::Core; | ||
| 22 | use Perf::Trace::Util; | ||
| 23 | |||
| 24 | my @cpus; | ||
| 25 | |||
| 26 | sub workqueue::workqueue_destruction | ||
| 27 | { | ||
| 28 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 29 | $common_pid, $common_comm, | ||
| 30 | $thread_comm, $thread_pid) = @_; | ||
| 31 | |||
| 32 | $cpus[$common_cpu]{$thread_pid}{destroyed}++; | ||
| 33 | $cpus[$common_cpu]{$thread_pid}{comm} = $thread_comm; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub workqueue::workqueue_creation | ||
| 37 | { | ||
| 38 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 39 | $common_pid, $common_comm, | ||
| 40 | $thread_comm, $thread_pid, $cpu) = @_; | ||
| 41 | |||
| 42 | $cpus[$common_cpu]{$thread_pid}{created}++; | ||
| 43 | $cpus[$common_cpu]{$thread_pid}{comm} = $thread_comm; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub workqueue::workqueue_execution | ||
| 47 | { | ||
| 48 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 49 | $common_pid, $common_comm, | ||
| 50 | $thread_comm, $thread_pid, $func) = @_; | ||
| 51 | |||
| 52 | $cpus[$common_cpu]{$thread_pid}{executed}++; | ||
| 53 | $cpus[$common_cpu]{$thread_pid}{comm} = $thread_comm; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub workqueue::workqueue_insertion | ||
| 57 | { | ||
| 58 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 59 | $common_pid, $common_comm, | ||
| 60 | $thread_comm, $thread_pid, $func) = @_; | ||
| 61 | |||
| 62 | $cpus[$common_cpu]{$thread_pid}{inserted}++; | ||
| 63 | $cpus[$common_cpu]{$thread_pid}{comm} = $thread_comm; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub trace_end | ||
| 67 | { | ||
| 68 | print "workqueue work stats:\n\n"; | ||
| 69 | my $cpu = 0; | ||
| 70 | printf("%3s %6s %6s\t%-20s\n", "cpu", "ins", "exec", "name"); | ||
| 71 | printf("%3s %6s %6s\t%-20s\n", "---", "---", "----", "----"); | ||
| 72 | foreach my $pidhash (@cpus) { | ||
| 73 | while ((my $pid, my $wqhash) = each %$pidhash) { | ||
| 74 | my $ins = $$wqhash{'inserted'}; | ||
| 75 | my $exe = $$wqhash{'executed'}; | ||
| 76 | my $comm = $$wqhash{'comm'}; | ||
| 77 | if ($ins || $exe) { | ||
| 78 | printf("%3u %6u %6u\t%-20s\n", $cpu, $ins, $exe, $comm); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | $cpu++; | ||
| 82 | } | ||
| 83 | |||
| 84 | $cpu = 0; | ||
| 85 | print "\nworkqueue lifecycle stats:\n\n"; | ||
| 86 | printf("%3s %6s %6s\t%-20s\n", "cpu", "created", "destroyed", "name"); | ||
| 87 | printf("%3s %6s %6s\t%-20s\n", "---", "-------", "---------", "----"); | ||
| 88 | foreach my $pidhash (@cpus) { | ||
| 89 | while ((my $pid, my $wqhash) = each %$pidhash) { | ||
| 90 | my $created = $$wqhash{'created'}; | ||
| 91 | my $destroyed = $$wqhash{'destroyed'}; | ||
| 92 | my $comm = $$wqhash{'comm'}; | ||
| 93 | if ($created || $destroyed) { | ||
| 94 | printf("%3u %6u %6u\t%-20s\n", $cpu, $created, $destroyed, | ||
| 95 | $comm); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | $cpu++; | ||
| 99 | } | ||
| 100 | |||
| 101 | print_unhandled(); | ||
| 102 | } | ||
| 103 | |||
| 104 | my %unhandled; | ||
| 105 | |||
| 106 | sub print_unhandled | ||
| 107 | { | ||
| 108 | if ((scalar keys %unhandled) == 0) { | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | print "\nunhandled events:\n\n"; | ||
| 113 | |||
| 114 | printf("%-40s %10s\n", "event", "count"); | ||
| 115 | printf("%-40s %10s\n", "----------------------------------------", | ||
| 116 | "-----------"); | ||
| 117 | |||
| 118 | foreach my $event_name (keys %unhandled) { | ||
| 119 | printf("%-40s %10d\n", $event_name, $unhandled{$event_name}); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | sub trace_unhandled | ||
| 124 | { | ||
| 125 | my ($event_name, $context, $common_cpu, $common_secs, $common_nsecs, | ||
| 126 | $common_pid, $common_comm) = @_; | ||
| 127 | |||
| 128 | $unhandled{$event_name}++; | ||
| 129 | } | ||
diff --git a/tools/perf/util/PERF-VERSION-GEN b/tools/perf/util/PERF-VERSION-GEN index c561d1538c03..54552a00a117 100755 --- a/tools/perf/util/PERF-VERSION-GEN +++ b/tools/perf/util/PERF-VERSION-GEN | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
| 2 | 2 | ||
| 3 | GVF=PERF-VERSION-FILE | 3 | GVF=PERF-VERSION-FILE |
| 4 | DEF_VER=v0.0.1.PERF | 4 | DEF_VER=v0.0.2.PERF |
| 5 | 5 | ||
| 6 | LF=' | 6 | LF=' |
| 7 | ' | 7 | ' |
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h index 6f8ea9d210b6..918eb376abe3 100644 --- a/tools/perf/util/cache.h +++ b/tools/perf/util/cache.h | |||
| @@ -1,10 +1,15 @@ | |||
| 1 | #ifndef CACHE_H | 1 | #ifndef __PERF_CACHE_H |
| 2 | #define CACHE_H | 2 | #define __PERF_CACHE_H |
| 3 | 3 | ||
| 4 | #include "util.h" | 4 | #include "util.h" |
| 5 | #include "strbuf.h" | 5 | #include "strbuf.h" |
| 6 | #include "../perf.h" | 6 | #include "../perf.h" |
| 7 | 7 | ||
| 8 | #define CMD_EXEC_PATH "--exec-path" | ||
| 9 | #define CMD_PERF_DIR "--perf-dir=" | ||
| 10 | #define CMD_WORK_TREE "--work-tree=" | ||
| 11 | #define CMD_DEBUGFS_DIR "--debugfs-dir=" | ||
| 12 | |||
| 8 | #define PERF_DIR_ENVIRONMENT "PERF_DIR" | 13 | #define PERF_DIR_ENVIRONMENT "PERF_DIR" |
| 9 | #define PERF_WORK_TREE_ENVIRONMENT "PERF_WORK_TREE" | 14 | #define PERF_WORK_TREE_ENVIRONMENT "PERF_WORK_TREE" |
| 10 | #define DEFAULT_PERF_DIR_ENVIRONMENT ".perf" | 15 | #define DEFAULT_PERF_DIR_ENVIRONMENT ".perf" |
| @@ -117,4 +122,4 @@ extern char *perf_pathdup(const char *fmt, ...) | |||
| 117 | 122 | ||
| 118 | extern size_t strlcpy(char *dest, const char *src, size_t size); | 123 | extern size_t strlcpy(char *dest, const char *src, size_t size); |
| 119 | 124 | ||
| 120 | #endif /* CACHE_H */ | 125 | #endif /* __PERF_CACHE_H */ |
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c index 3b8380f1b478..b3b71258272a 100644 --- a/tools/perf/util/callchain.c +++ b/tools/perf/util/callchain.c | |||
| @@ -206,7 +206,7 @@ fill_node(struct callchain_node *node, struct ip_callchain *chain, | |||
| 206 | } | 206 | } |
| 207 | node->val_nr = chain->nr - start; | 207 | node->val_nr = chain->nr - start; |
| 208 | if (!node->val_nr) | 208 | if (!node->val_nr) |
| 209 | printf("Warning: empty node in callchain tree\n"); | 209 | pr_warning("Warning: empty node in callchain tree\n"); |
| 210 | } | 210 | } |
| 211 | 211 | ||
| 212 | static void | 212 | static void |
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h index 43cf3ea9e088..ad4626de4c2b 100644 --- a/tools/perf/util/callchain.h +++ b/tools/perf/util/callchain.h | |||
| @@ -58,4 +58,4 @@ static inline u64 cumul_hits(struct callchain_node *node) | |||
| 58 | int register_callchain_param(struct callchain_param *param); | 58 | int register_callchain_param(struct callchain_param *param); |
| 59 | void append_chain(struct callchain_node *root, struct ip_callchain *chain, | 59 | void append_chain(struct callchain_node *root, struct ip_callchain *chain, |
| 60 | struct symbol **syms); | 60 | struct symbol **syms); |
| 61 | #endif | 61 | #endif /* __PERF_CALLCHAIN_H */ |
diff --git a/tools/perf/util/color.h b/tools/perf/util/color.h index 58d597564b99..24e8809210bb 100644 --- a/tools/perf/util/color.h +++ b/tools/perf/util/color.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef COLOR_H | 1 | #ifndef __PERF_COLOR_H |
| 2 | #define COLOR_H | 2 | #define __PERF_COLOR_H |
| 3 | 3 | ||
| 4 | /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */ | 4 | /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */ |
| 5 | #define COLOR_MAXLEN 24 | 5 | #define COLOR_MAXLEN 24 |
| @@ -39,4 +39,4 @@ int color_fwrite_lines(FILE *fp, const char *color, size_t count, const char *bu | |||
| 39 | int percent_color_fprintf(FILE *fp, const char *fmt, double percent); | 39 | int percent_color_fprintf(FILE *fp, const char *fmt, double percent); |
| 40 | const char *get_percent_color(double percent); | 40 | const char *get_percent_color(double percent); |
| 41 | 41 | ||
| 42 | #endif /* COLOR_H */ | 42 | #endif /* __PERF_COLOR_H */ |
diff --git a/tools/perf/util/ctype.c b/tools/perf/util/ctype.c index 0b791bd346bc..35073621e5de 100644 --- a/tools/perf/util/ctype.c +++ b/tools/perf/util/ctype.c | |||
| @@ -29,3 +29,11 @@ unsigned char sane_ctype[256] = { | |||
| 29 | A, A, A, A, A, A, A, A, A, A, A, R, R, P, P, 0, /* 112..127 */ | 29 | A, A, A, A, A, A, A, A, A, A, A, R, R, P, P, 0, /* 112..127 */ |
| 30 | /* Nothing in the 128.. range */ | 30 | /* Nothing in the 128.. range */ |
| 31 | }; | 31 | }; |
| 32 | |||
| 33 | const char *graph_line = | ||
| 34 | "_____________________________________________________________________" | ||
| 35 | "_____________________________________________________________________"; | ||
| 36 | const char *graph_dotted_line = | ||
| 37 | "---------------------------------------------------------------------" | ||
| 38 | "---------------------------------------------------------------------" | ||
| 39 | "---------------------------------------------------------------------"; | ||
diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c new file mode 100644 index 000000000000..ca0bedf637c2 --- /dev/null +++ b/tools/perf/util/data_map.c | |||
| @@ -0,0 +1,291 @@ | |||
| 1 | #include "data_map.h" | ||
| 2 | #include "symbol.h" | ||
| 3 | #include "util.h" | ||
| 4 | #include "debug.h" | ||
| 5 | |||
| 6 | |||
| 7 | static struct perf_file_handler *curr_handler; | ||
| 8 | static unsigned long mmap_window = 32; | ||
| 9 | static char __cwd[PATH_MAX]; | ||
| 10 | |||
| 11 | static int process_event_stub(event_t *event __used) | ||
| 12 | { | ||
| 13 | dump_printf(": unhandled!\n"); | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | |||
| 17 | void register_perf_file_handler(struct perf_file_handler *handler) | ||
| 18 | { | ||
| 19 | if (!handler->process_sample_event) | ||
| 20 | handler->process_sample_event = process_event_stub; | ||
| 21 | if (!handler->process_mmap_event) | ||
| 22 | handler->process_mmap_event = process_event_stub; | ||
| 23 | if (!handler->process_comm_event) | ||
| 24 | handler->process_comm_event = process_event_stub; | ||
| 25 | if (!handler->process_fork_event) | ||
| 26 | handler->process_fork_event = process_event_stub; | ||
| 27 | if (!handler->process_exit_event) | ||
| 28 | handler->process_exit_event = process_event_stub; | ||
| 29 | if (!handler->process_lost_event) | ||
| 30 | handler->process_lost_event = process_event_stub; | ||
| 31 | if (!handler->process_read_event) | ||
| 32 | handler->process_read_event = process_event_stub; | ||
| 33 | if (!handler->process_throttle_event) | ||
| 34 | handler->process_throttle_event = process_event_stub; | ||
| 35 | if (!handler->process_unthrottle_event) | ||
| 36 | handler->process_unthrottle_event = process_event_stub; | ||
| 37 | |||
| 38 | curr_handler = handler; | ||
| 39 | } | ||
| 40 | |||
| 41 | static const char *event__name[] = { | ||
| 42 | [0] = "TOTAL", | ||
| 43 | [PERF_RECORD_MMAP] = "MMAP", | ||
| 44 | [PERF_RECORD_LOST] = "LOST", | ||
| 45 | [PERF_RECORD_COMM] = "COMM", | ||
| 46 | [PERF_RECORD_EXIT] = "EXIT", | ||
| 47 | [PERF_RECORD_THROTTLE] = "THROTTLE", | ||
| 48 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", | ||
| 49 | [PERF_RECORD_FORK] = "FORK", | ||
| 50 | [PERF_RECORD_READ] = "READ", | ||
| 51 | [PERF_RECORD_SAMPLE] = "SAMPLE", | ||
| 52 | }; | ||
| 53 | |||
| 54 | unsigned long event__total[PERF_RECORD_MAX]; | ||
| 55 | |||
| 56 | void event__print_totals(void) | ||
| 57 | { | ||
| 58 | int i; | ||
| 59 | for (i = 0; i < PERF_RECORD_MAX; ++i) | ||
| 60 | pr_info("%10s events: %10ld\n", | ||
| 61 | event__name[i], event__total[i]); | ||
| 62 | } | ||
| 63 | |||
| 64 | static int | ||
| 65 | process_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 66 | { | ||
| 67 | trace_event(event); | ||
| 68 | |||
| 69 | if (event->header.type < PERF_RECORD_MAX) { | ||
| 70 | dump_printf("%p [%p]: PERF_RECORD_%s", | ||
| 71 | (void *)(offset + head), | ||
| 72 | (void *)(long)(event->header.size), | ||
| 73 | event__name[event->header.type]); | ||
| 74 | ++event__total[0]; | ||
| 75 | ++event__total[event->header.type]; | ||
| 76 | } | ||
| 77 | |||
| 78 | switch (event->header.type) { | ||
| 79 | case PERF_RECORD_SAMPLE: | ||
| 80 | return curr_handler->process_sample_event(event); | ||
| 81 | case PERF_RECORD_MMAP: | ||
| 82 | return curr_handler->process_mmap_event(event); | ||
| 83 | case PERF_RECORD_COMM: | ||
| 84 | return curr_handler->process_comm_event(event); | ||
| 85 | case PERF_RECORD_FORK: | ||
| 86 | return curr_handler->process_fork_event(event); | ||
| 87 | case PERF_RECORD_EXIT: | ||
| 88 | return curr_handler->process_exit_event(event); | ||
| 89 | case PERF_RECORD_LOST: | ||
| 90 | return curr_handler->process_lost_event(event); | ||
| 91 | case PERF_RECORD_READ: | ||
| 92 | return curr_handler->process_read_event(event); | ||
| 93 | case PERF_RECORD_THROTTLE: | ||
| 94 | return curr_handler->process_throttle_event(event); | ||
| 95 | case PERF_RECORD_UNTHROTTLE: | ||
| 96 | return curr_handler->process_unthrottle_event(event); | ||
| 97 | default: | ||
| 98 | curr_handler->total_unknown++; | ||
| 99 | return -1; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | int perf_header__read_build_ids(int input, off_t offset, off_t size) | ||
| 104 | { | ||
| 105 | struct build_id_event bev; | ||
| 106 | char filename[PATH_MAX]; | ||
| 107 | off_t limit = offset + size; | ||
| 108 | int err = -1; | ||
| 109 | |||
| 110 | while (offset < limit) { | ||
| 111 | struct dso *dso; | ||
| 112 | ssize_t len; | ||
| 113 | |||
| 114 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) | ||
| 115 | goto out; | ||
| 116 | |||
| 117 | len = bev.header.size - sizeof(bev); | ||
| 118 | if (read(input, filename, len) != len) | ||
| 119 | goto out; | ||
| 120 | |||
| 121 | dso = dsos__findnew(filename); | ||
| 122 | if (dso != NULL) | ||
| 123 | dso__set_build_id(dso, &bev.build_id); | ||
| 124 | |||
| 125 | offset += bev.header.size; | ||
| 126 | } | ||
| 127 | err = 0; | ||
| 128 | out: | ||
| 129 | return err; | ||
| 130 | } | ||
| 131 | |||
| 132 | int mmap_dispatch_perf_file(struct perf_header **pheader, | ||
| 133 | const char *input_name, | ||
| 134 | int force, | ||
| 135 | int full_paths, | ||
| 136 | int *cwdlen, | ||
| 137 | char **cwd) | ||
| 138 | { | ||
| 139 | int err; | ||
| 140 | struct perf_header *header; | ||
| 141 | unsigned long head, shift; | ||
| 142 | unsigned long offset = 0; | ||
| 143 | struct stat input_stat; | ||
| 144 | size_t page_size; | ||
| 145 | u64 sample_type; | ||
| 146 | event_t *event; | ||
| 147 | uint32_t size; | ||
| 148 | int input; | ||
| 149 | char *buf; | ||
| 150 | |||
| 151 | if (curr_handler == NULL) { | ||
| 152 | pr_debug("Forgot to register perf file handler\n"); | ||
| 153 | return -EINVAL; | ||
| 154 | } | ||
| 155 | |||
| 156 | page_size = getpagesize(); | ||
| 157 | |||
| 158 | input = open(input_name, O_RDONLY); | ||
| 159 | if (input < 0) { | ||
| 160 | pr_err("Failed to open file: %s", input_name); | ||
| 161 | if (!strcmp(input_name, "perf.data")) | ||
| 162 | pr_err(" (try 'perf record' first)"); | ||
| 163 | pr_err("\n"); | ||
| 164 | return -errno; | ||
| 165 | } | ||
| 166 | |||
| 167 | if (fstat(input, &input_stat) < 0) { | ||
| 168 | pr_err("failed to stat file"); | ||
| 169 | err = -errno; | ||
| 170 | goto out_close; | ||
| 171 | } | ||
| 172 | |||
| 173 | err = -EACCES; | ||
| 174 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { | ||
| 175 | pr_err("file: %s not owned by current user or root\n", | ||
| 176 | input_name); | ||
| 177 | goto out_close; | ||
| 178 | } | ||
| 179 | |||
| 180 | if (input_stat.st_size == 0) { | ||
| 181 | pr_info("zero-sized file, nothing to do!\n"); | ||
| 182 | goto done; | ||
| 183 | } | ||
| 184 | |||
| 185 | err = -ENOMEM; | ||
| 186 | header = perf_header__new(); | ||
| 187 | if (header == NULL) | ||
| 188 | goto out_close; | ||
| 189 | |||
| 190 | err = perf_header__read(header, input); | ||
| 191 | if (err < 0) | ||
| 192 | goto out_delete; | ||
| 193 | *pheader = header; | ||
| 194 | head = header->data_offset; | ||
| 195 | |||
| 196 | sample_type = perf_header__sample_type(header); | ||
| 197 | |||
| 198 | err = -EINVAL; | ||
| 199 | if (curr_handler->sample_type_check && | ||
| 200 | curr_handler->sample_type_check(sample_type) < 0) | ||
| 201 | goto out_delete; | ||
| 202 | |||
| 203 | if (!full_paths) { | ||
| 204 | if (getcwd(__cwd, sizeof(__cwd)) == NULL) { | ||
| 205 | pr_err("failed to get the current directory\n"); | ||
| 206 | err = -errno; | ||
| 207 | goto out_delete; | ||
| 208 | } | ||
| 209 | *cwd = __cwd; | ||
| 210 | *cwdlen = strlen(*cwd); | ||
| 211 | } else { | ||
| 212 | *cwd = NULL; | ||
| 213 | *cwdlen = 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | shift = page_size * (head / page_size); | ||
| 217 | offset += shift; | ||
| 218 | head -= shift; | ||
| 219 | |||
| 220 | remap: | ||
| 221 | buf = mmap(NULL, page_size * mmap_window, PROT_READ, | ||
| 222 | MAP_SHARED, input, offset); | ||
| 223 | if (buf == MAP_FAILED) { | ||
| 224 | pr_err("failed to mmap file\n"); | ||
| 225 | err = -errno; | ||
| 226 | goto out_delete; | ||
| 227 | } | ||
| 228 | |||
| 229 | more: | ||
| 230 | event = (event_t *)(buf + head); | ||
| 231 | |||
| 232 | size = event->header.size; | ||
| 233 | if (!size) | ||
| 234 | size = 8; | ||
| 235 | |||
| 236 | if (head + event->header.size >= page_size * mmap_window) { | ||
| 237 | int munmap_ret; | ||
| 238 | |||
| 239 | shift = page_size * (head / page_size); | ||
| 240 | |||
| 241 | munmap_ret = munmap(buf, page_size * mmap_window); | ||
| 242 | assert(munmap_ret == 0); | ||
| 243 | |||
| 244 | offset += shift; | ||
| 245 | head -= shift; | ||
| 246 | goto remap; | ||
| 247 | } | ||
| 248 | |||
| 249 | size = event->header.size; | ||
| 250 | |||
| 251 | dump_printf("\n%p [%p]: event: %d\n", | ||
| 252 | (void *)(offset + head), | ||
| 253 | (void *)(long)event->header.size, | ||
| 254 | event->header.type); | ||
| 255 | |||
| 256 | if (!size || process_event(event, offset, head) < 0) { | ||
| 257 | |||
| 258 | dump_printf("%p [%p]: skipping unknown header type: %d\n", | ||
| 259 | (void *)(offset + head), | ||
| 260 | (void *)(long)(event->header.size), | ||
| 261 | event->header.type); | ||
| 262 | |||
| 263 | /* | ||
| 264 | * assume we lost track of the stream, check alignment, and | ||
| 265 | * increment a single u64 in the hope to catch on again 'soon'. | ||
| 266 | */ | ||
| 267 | |||
| 268 | if (unlikely(head & 7)) | ||
| 269 | head &= ~7ULL; | ||
| 270 | |||
| 271 | size = 8; | ||
| 272 | } | ||
| 273 | |||
| 274 | head += size; | ||
| 275 | |||
| 276 | if (offset + head >= header->data_offset + header->data_size) | ||
| 277 | goto done; | ||
| 278 | |||
| 279 | if (offset + head < (unsigned long)input_stat.st_size) | ||
| 280 | goto more; | ||
| 281 | |||
| 282 | done: | ||
| 283 | err = 0; | ||
| 284 | out_close: | ||
| 285 | close(input); | ||
| 286 | |||
| 287 | return err; | ||
| 288 | out_delete: | ||
| 289 | perf_header__delete(header); | ||
| 290 | goto out_close; | ||
| 291 | } | ||
diff --git a/tools/perf/util/data_map.h b/tools/perf/util/data_map.h new file mode 100644 index 000000000000..3180ff7e3633 --- /dev/null +++ b/tools/perf/util/data_map.h | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | #ifndef __PERF_DATAMAP_H | ||
| 2 | #define __PERF_DATAMAP_H | ||
| 3 | |||
| 4 | #include "event.h" | ||
| 5 | #include "header.h" | ||
| 6 | |||
| 7 | typedef int (*event_type_handler_t)(event_t *); | ||
| 8 | |||
| 9 | struct perf_file_handler { | ||
| 10 | event_type_handler_t process_sample_event; | ||
| 11 | event_type_handler_t process_mmap_event; | ||
| 12 | event_type_handler_t process_comm_event; | ||
| 13 | event_type_handler_t process_fork_event; | ||
| 14 | event_type_handler_t process_exit_event; | ||
| 15 | event_type_handler_t process_lost_event; | ||
| 16 | event_type_handler_t process_read_event; | ||
| 17 | event_type_handler_t process_throttle_event; | ||
| 18 | event_type_handler_t process_unthrottle_event; | ||
| 19 | int (*sample_type_check)(u64 sample_type); | ||
| 20 | unsigned long total_unknown; | ||
| 21 | }; | ||
| 22 | |||
| 23 | void register_perf_file_handler(struct perf_file_handler *handler); | ||
| 24 | int mmap_dispatch_perf_file(struct perf_header **pheader, | ||
| 25 | const char *input_name, | ||
| 26 | int force, | ||
| 27 | int full_paths, | ||
| 28 | int *cwdlen, | ||
| 29 | char **cwd); | ||
| 30 | int perf_header__read_build_ids(int input, off_t offset, off_t file_size); | ||
| 31 | |||
| 32 | #endif | ||
diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c index e8ca98fe0bd4..28d520d5a1fb 100644 --- a/tools/perf/util/debug.c +++ b/tools/perf/util/debug.c | |||
| @@ -13,12 +13,12 @@ | |||
| 13 | int verbose = 0; | 13 | int verbose = 0; |
| 14 | int dump_trace = 0; | 14 | int dump_trace = 0; |
| 15 | 15 | ||
| 16 | int eprintf(const char *fmt, ...) | 16 | int eprintf(int level, const char *fmt, ...) |
| 17 | { | 17 | { |
| 18 | va_list args; | 18 | va_list args; |
| 19 | int ret = 0; | 19 | int ret = 0; |
| 20 | 20 | ||
| 21 | if (verbose) { | 21 | if (verbose >= level) { |
| 22 | va_start(args, fmt); | 22 | va_start(args, fmt); |
| 23 | ret = vfprintf(stderr, fmt, args); | 23 | ret = vfprintf(stderr, fmt, args); |
| 24 | va_end(args); | 24 | va_end(args); |
diff --git a/tools/perf/util/debug.h b/tools/perf/util/debug.h index 437eea58ce40..c6c24c522dea 100644 --- a/tools/perf/util/debug.h +++ b/tools/perf/util/debug.h | |||
| @@ -1,8 +1,15 @@ | |||
| 1 | /* For debugging general purposes */ | 1 | /* For debugging general purposes */ |
| 2 | #ifndef __PERF_DEBUG_H | ||
| 3 | #define __PERF_DEBUG_H | ||
| 4 | |||
| 5 | #include "event.h" | ||
| 2 | 6 | ||
| 3 | extern int verbose; | 7 | extern int verbose; |
| 4 | extern int dump_trace; | 8 | extern int dump_trace; |
| 5 | 9 | ||
| 6 | int eprintf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); | 10 | int eprintf(int level, |
| 11 | const char *fmt, ...) __attribute__((format(printf, 2, 3))); | ||
| 7 | int dump_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); | 12 | int dump_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); |
| 8 | void trace_event(event_t *event); | 13 | void trace_event(event_t *event); |
| 14 | |||
| 15 | #endif /* __PERF_DEBUG_H */ | ||
diff --git a/tools/perf/util/debugfs.c b/tools/perf/util/debugfs.c new file mode 100644 index 000000000000..06b73ee02c49 --- /dev/null +++ b/tools/perf/util/debugfs.c | |||
| @@ -0,0 +1,241 @@ | |||
| 1 | #include "util.h" | ||
| 2 | #include "debugfs.h" | ||
| 3 | #include "cache.h" | ||
| 4 | |||
| 5 | static int debugfs_premounted; | ||
| 6 | static char debugfs_mountpoint[MAX_PATH+1]; | ||
| 7 | |||
| 8 | static const char *debugfs_known_mountpoints[] = { | ||
| 9 | "/sys/kernel/debug/", | ||
| 10 | "/debug/", | ||
| 11 | 0, | ||
| 12 | }; | ||
| 13 | |||
| 14 | /* use this to force a umount */ | ||
| 15 | void debugfs_force_cleanup(void) | ||
| 16 | { | ||
| 17 | debugfs_find_mountpoint(); | ||
| 18 | debugfs_premounted = 0; | ||
| 19 | debugfs_umount(); | ||
| 20 | } | ||
| 21 | |||
| 22 | /* construct a full path to a debugfs element */ | ||
| 23 | int debugfs_make_path(const char *element, char *buffer, int size) | ||
| 24 | { | ||
| 25 | int len; | ||
| 26 | |||
| 27 | if (strlen(debugfs_mountpoint) == 0) { | ||
| 28 | buffer[0] = '\0'; | ||
| 29 | return -1; | ||
| 30 | } | ||
| 31 | |||
| 32 | len = strlen(debugfs_mountpoint) + strlen(element) + 1; | ||
| 33 | if (len >= size) | ||
| 34 | return len+1; | ||
| 35 | |||
| 36 | snprintf(buffer, size-1, "%s/%s", debugfs_mountpoint, element); | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | static int debugfs_found; | ||
| 41 | |||
| 42 | /* find the path to the mounted debugfs */ | ||
| 43 | const char *debugfs_find_mountpoint(void) | ||
| 44 | { | ||
| 45 | const char **ptr; | ||
| 46 | char type[100]; | ||
| 47 | FILE *fp; | ||
| 48 | |||
| 49 | if (debugfs_found) | ||
| 50 | return (const char *) debugfs_mountpoint; | ||
| 51 | |||
| 52 | ptr = debugfs_known_mountpoints; | ||
| 53 | while (*ptr) { | ||
| 54 | if (debugfs_valid_mountpoint(*ptr) == 0) { | ||
| 55 | debugfs_found = 1; | ||
| 56 | strcpy(debugfs_mountpoint, *ptr); | ||
| 57 | return debugfs_mountpoint; | ||
| 58 | } | ||
| 59 | ptr++; | ||
| 60 | } | ||
| 61 | |||
| 62 | /* give up and parse /proc/mounts */ | ||
| 63 | fp = fopen("/proc/mounts", "r"); | ||
| 64 | if (fp == NULL) | ||
| 65 | die("Can't open /proc/mounts for read"); | ||
| 66 | |||
| 67 | while (fscanf(fp, "%*s %" | ||
| 68 | STR(MAX_PATH) | ||
| 69 | "s %99s %*s %*d %*d\n", | ||
| 70 | debugfs_mountpoint, type) == 2) { | ||
| 71 | if (strcmp(type, "debugfs") == 0) | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | fclose(fp); | ||
| 75 | |||
| 76 | if (strcmp(type, "debugfs") != 0) | ||
| 77 | return NULL; | ||
| 78 | |||
| 79 | debugfs_found = 1; | ||
| 80 | |||
| 81 | return debugfs_mountpoint; | ||
| 82 | } | ||
| 83 | |||
| 84 | /* verify that a mountpoint is actually a debugfs instance */ | ||
| 85 | |||
| 86 | int debugfs_valid_mountpoint(const char *debugfs) | ||
| 87 | { | ||
| 88 | struct statfs st_fs; | ||
| 89 | |||
| 90 | if (statfs(debugfs, &st_fs) < 0) | ||
| 91 | return -ENOENT; | ||
| 92 | else if (st_fs.f_type != (long) DEBUGFS_MAGIC) | ||
| 93 | return -ENOENT; | ||
| 94 | |||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | int debugfs_valid_entry(const char *path) | ||
| 100 | { | ||
| 101 | struct stat st; | ||
| 102 | |||
| 103 | if (stat(path, &st)) | ||
| 104 | return -errno; | ||
| 105 | |||
| 106 | return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | /* mount the debugfs somewhere */ | ||
| 110 | |||
| 111 | int debugfs_mount(const char *mountpoint) | ||
| 112 | { | ||
| 113 | char mountcmd[128]; | ||
| 114 | |||
| 115 | /* see if it's already mounted */ | ||
| 116 | if (debugfs_find_mountpoint()) { | ||
| 117 | debugfs_premounted = 1; | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | /* if not mounted and no argument */ | ||
| 122 | if (mountpoint == NULL) { | ||
| 123 | /* see if environment variable set */ | ||
| 124 | mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); | ||
| 125 | /* if no environment variable, use default */ | ||
| 126 | if (mountpoint == NULL) | ||
| 127 | mountpoint = "/sys/kernel/debug"; | ||
| 128 | } | ||
| 129 | |||
| 130 | /* save the mountpoint */ | ||
| 131 | strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); | ||
| 132 | |||
| 133 | /* mount it */ | ||
| 134 | snprintf(mountcmd, sizeof(mountcmd), | ||
| 135 | "/bin/mount -t debugfs debugfs %s", mountpoint); | ||
| 136 | return system(mountcmd); | ||
| 137 | } | ||
| 138 | |||
| 139 | /* umount the debugfs */ | ||
| 140 | |||
| 141 | int debugfs_umount(void) | ||
| 142 | { | ||
| 143 | char umountcmd[128]; | ||
| 144 | int ret; | ||
| 145 | |||
| 146 | /* if it was already mounted, leave it */ | ||
| 147 | if (debugfs_premounted) | ||
| 148 | return 0; | ||
| 149 | |||
| 150 | /* make sure it's a valid mount point */ | ||
| 151 | ret = debugfs_valid_mountpoint(debugfs_mountpoint); | ||
| 152 | if (ret) | ||
| 153 | return ret; | ||
| 154 | |||
| 155 | snprintf(umountcmd, sizeof(umountcmd), | ||
| 156 | "/bin/umount %s", debugfs_mountpoint); | ||
| 157 | return system(umountcmd); | ||
| 158 | } | ||
| 159 | |||
| 160 | int debugfs_write(const char *entry, const char *value) | ||
| 161 | { | ||
| 162 | char path[MAX_PATH+1]; | ||
| 163 | int ret, count; | ||
| 164 | int fd; | ||
| 165 | |||
| 166 | /* construct the path */ | ||
| 167 | snprintf(path, sizeof(path), "%s/%s", debugfs_mountpoint, entry); | ||
| 168 | |||
| 169 | /* verify that it exists */ | ||
| 170 | ret = debugfs_valid_entry(path); | ||
| 171 | if (ret) | ||
| 172 | return ret; | ||
| 173 | |||
| 174 | /* get how many chars we're going to write */ | ||
| 175 | count = strlen(value); | ||
| 176 | |||
| 177 | /* open the debugfs entry */ | ||
| 178 | fd = open(path, O_RDWR); | ||
| 179 | if (fd < 0) | ||
| 180 | return -errno; | ||
| 181 | |||
| 182 | while (count > 0) { | ||
| 183 | /* write it */ | ||
| 184 | ret = write(fd, value, count); | ||
| 185 | if (ret <= 0) { | ||
| 186 | if (ret == EAGAIN) | ||
| 187 | continue; | ||
| 188 | close(fd); | ||
| 189 | return -errno; | ||
| 190 | } | ||
| 191 | count -= ret; | ||
| 192 | } | ||
| 193 | |||
| 194 | /* close it */ | ||
| 195 | close(fd); | ||
| 196 | |||
| 197 | /* return success */ | ||
| 198 | return 0; | ||
| 199 | } | ||
| 200 | |||
| 201 | /* | ||
| 202 | * read a debugfs entry | ||
| 203 | * returns the number of chars read or a negative errno | ||
| 204 | */ | ||
| 205 | int debugfs_read(const char *entry, char *buffer, size_t size) | ||
| 206 | { | ||
| 207 | char path[MAX_PATH+1]; | ||
| 208 | int ret; | ||
| 209 | int fd; | ||
| 210 | |||
| 211 | /* construct the path */ | ||
| 212 | snprintf(path, sizeof(path), "%s/%s", debugfs_mountpoint, entry); | ||
| 213 | |||
| 214 | /* verify that it exists */ | ||
| 215 | ret = debugfs_valid_entry(path); | ||
| 216 | if (ret) | ||
| 217 | return ret; | ||
| 218 | |||
| 219 | /* open the debugfs entry */ | ||
| 220 | fd = open(path, O_RDONLY); | ||
| 221 | if (fd < 0) | ||
| 222 | return -errno; | ||
| 223 | |||
| 224 | do { | ||
| 225 | /* read it */ | ||
| 226 | ret = read(fd, buffer, size); | ||
| 227 | if (ret == 0) { | ||
| 228 | close(fd); | ||
| 229 | return EOF; | ||
| 230 | } | ||
| 231 | } while (ret < 0 && errno == EAGAIN); | ||
| 232 | |||
| 233 | /* close it */ | ||
| 234 | close(fd); | ||
| 235 | |||
| 236 | /* make *sure* there's a null character at the end */ | ||
| 237 | buffer[ret] = '\0'; | ||
| 238 | |||
| 239 | /* return the number of chars read */ | ||
| 240 | return ret; | ||
| 241 | } | ||
diff --git a/tools/perf/util/debugfs.h b/tools/perf/util/debugfs.h new file mode 100644 index 000000000000..3cd14f9ae784 --- /dev/null +++ b/tools/perf/util/debugfs.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #ifndef __DEBUGFS_H__ | ||
| 2 | #define __DEBUGFS_H__ | ||
| 3 | |||
| 4 | #include <sys/mount.h> | ||
| 5 | |||
| 6 | #ifndef MAX_PATH | ||
| 7 | # define MAX_PATH 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #ifndef STR | ||
| 11 | # define _STR(x) #x | ||
| 12 | # define STR(x) _STR(x) | ||
| 13 | #endif | ||
| 14 | |||
| 15 | extern const char *debugfs_find_mountpoint(void); | ||
| 16 | extern int debugfs_valid_mountpoint(const char *debugfs); | ||
| 17 | extern int debugfs_valid_entry(const char *path); | ||
| 18 | extern int debugfs_mount(const char *mountpoint); | ||
| 19 | extern int debugfs_umount(void); | ||
| 20 | extern int debugfs_write(const char *entry, const char *value); | ||
| 21 | extern int debugfs_read(const char *entry, char *buffer, size_t size); | ||
| 22 | extern void debugfs_force_cleanup(void); | ||
| 23 | extern int debugfs_make_path(const char *element, char *buffer, int size); | ||
| 24 | |||
| 25 | #endif /* __DEBUGFS_H__ */ | ||
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c new file mode 100644 index 000000000000..414b89d1bde9 --- /dev/null +++ b/tools/perf/util/event.c | |||
| @@ -0,0 +1,312 @@ | |||
| 1 | #include <linux/types.h> | ||
| 2 | #include "event.h" | ||
| 3 | #include "debug.h" | ||
| 4 | #include "string.h" | ||
| 5 | #include "thread.h" | ||
| 6 | |||
| 7 | static pid_t event__synthesize_comm(pid_t pid, int full, | ||
| 8 | int (*process)(event_t *event)) | ||
| 9 | { | ||
| 10 | event_t ev; | ||
| 11 | char filename[PATH_MAX]; | ||
| 12 | char bf[BUFSIZ]; | ||
| 13 | FILE *fp; | ||
| 14 | size_t size = 0; | ||
| 15 | DIR *tasks; | ||
| 16 | struct dirent dirent, *next; | ||
| 17 | pid_t tgid = 0; | ||
| 18 | |||
| 19 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); | ||
| 20 | |||
| 21 | fp = fopen(filename, "r"); | ||
| 22 | if (fp == NULL) { | ||
| 23 | out_race: | ||
| 24 | /* | ||
| 25 | * We raced with a task exiting - just return: | ||
| 26 | */ | ||
| 27 | pr_debug("couldn't open %s\n", filename); | ||
| 28 | return 0; | ||
| 29 | } | ||
| 30 | |||
| 31 | memset(&ev.comm, 0, sizeof(ev.comm)); | ||
| 32 | while (!ev.comm.comm[0] || !ev.comm.pid) { | ||
| 33 | if (fgets(bf, sizeof(bf), fp) == NULL) | ||
| 34 | goto out_failure; | ||
| 35 | |||
| 36 | if (memcmp(bf, "Name:", 5) == 0) { | ||
| 37 | char *name = bf + 5; | ||
| 38 | while (*name && isspace(*name)) | ||
| 39 | ++name; | ||
| 40 | size = strlen(name) - 1; | ||
| 41 | memcpy(ev.comm.comm, name, size++); | ||
| 42 | } else if (memcmp(bf, "Tgid:", 5) == 0) { | ||
| 43 | char *tgids = bf + 5; | ||
| 44 | while (*tgids && isspace(*tgids)) | ||
| 45 | ++tgids; | ||
| 46 | tgid = ev.comm.pid = atoi(tgids); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | ev.comm.header.type = PERF_RECORD_COMM; | ||
| 51 | size = ALIGN(size, sizeof(u64)); | ||
| 52 | ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size); | ||
| 53 | |||
| 54 | if (!full) { | ||
| 55 | ev.comm.tid = pid; | ||
| 56 | |||
| 57 | process(&ev); | ||
| 58 | goto out_fclose; | ||
| 59 | } | ||
| 60 | |||
| 61 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | ||
| 62 | |||
| 63 | tasks = opendir(filename); | ||
| 64 | if (tasks == NULL) | ||
| 65 | goto out_race; | ||
| 66 | |||
| 67 | while (!readdir_r(tasks, &dirent, &next) && next) { | ||
| 68 | char *end; | ||
| 69 | pid = strtol(dirent.d_name, &end, 10); | ||
| 70 | if (*end) | ||
| 71 | continue; | ||
| 72 | |||
| 73 | ev.comm.tid = pid; | ||
| 74 | |||
| 75 | process(&ev); | ||
| 76 | } | ||
| 77 | closedir(tasks); | ||
| 78 | |||
| 79 | out_fclose: | ||
| 80 | fclose(fp); | ||
| 81 | return tgid; | ||
| 82 | |||
| 83 | out_failure: | ||
| 84 | pr_warning("couldn't get COMM and pgid, malformed %s\n", filename); | ||
| 85 | return -1; | ||
| 86 | } | ||
| 87 | |||
| 88 | static int event__synthesize_mmap_events(pid_t pid, pid_t tgid, | ||
| 89 | int (*process)(event_t *event)) | ||
| 90 | { | ||
| 91 | char filename[PATH_MAX]; | ||
| 92 | FILE *fp; | ||
| 93 | |||
| 94 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); | ||
| 95 | |||
| 96 | fp = fopen(filename, "r"); | ||
| 97 | if (fp == NULL) { | ||
| 98 | /* | ||
| 99 | * We raced with a task exiting - just return: | ||
| 100 | */ | ||
| 101 | pr_debug("couldn't open %s\n", filename); | ||
| 102 | return -1; | ||
| 103 | } | ||
| 104 | |||
| 105 | while (1) { | ||
| 106 | char bf[BUFSIZ], *pbf = bf; | ||
| 107 | event_t ev = { | ||
| 108 | .header = { .type = PERF_RECORD_MMAP }, | ||
| 109 | }; | ||
| 110 | int n; | ||
| 111 | size_t size; | ||
| 112 | if (fgets(bf, sizeof(bf), fp) == NULL) | ||
| 113 | break; | ||
| 114 | |||
| 115 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ | ||
| 116 | n = hex2u64(pbf, &ev.mmap.start); | ||
| 117 | if (n < 0) | ||
| 118 | continue; | ||
| 119 | pbf += n + 1; | ||
| 120 | n = hex2u64(pbf, &ev.mmap.len); | ||
| 121 | if (n < 0) | ||
| 122 | continue; | ||
| 123 | pbf += n + 3; | ||
| 124 | if (*pbf == 'x') { /* vm_exec */ | ||
| 125 | char *execname = strchr(bf, '/'); | ||
| 126 | |||
| 127 | /* Catch VDSO */ | ||
| 128 | if (execname == NULL) | ||
| 129 | execname = strstr(bf, "[vdso]"); | ||
| 130 | |||
| 131 | if (execname == NULL) | ||
| 132 | continue; | ||
| 133 | |||
| 134 | size = strlen(execname); | ||
| 135 | execname[size - 1] = '\0'; /* Remove \n */ | ||
| 136 | memcpy(ev.mmap.filename, execname, size); | ||
| 137 | size = ALIGN(size, sizeof(u64)); | ||
| 138 | ev.mmap.len -= ev.mmap.start; | ||
| 139 | ev.mmap.header.size = (sizeof(ev.mmap) - | ||
| 140 | (sizeof(ev.mmap.filename) - size)); | ||
| 141 | ev.mmap.pid = tgid; | ||
| 142 | ev.mmap.tid = pid; | ||
| 143 | |||
| 144 | process(&ev); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | fclose(fp); | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | int event__synthesize_thread(pid_t pid, int (*process)(event_t *event)) | ||
| 153 | { | ||
| 154 | pid_t tgid = event__synthesize_comm(pid, 1, process); | ||
| 155 | if (tgid == -1) | ||
| 156 | return -1; | ||
| 157 | return event__synthesize_mmap_events(pid, tgid, process); | ||
| 158 | } | ||
| 159 | |||
| 160 | void event__synthesize_threads(int (*process)(event_t *event)) | ||
| 161 | { | ||
| 162 | DIR *proc; | ||
| 163 | struct dirent dirent, *next; | ||
| 164 | |||
| 165 | proc = opendir("/proc"); | ||
| 166 | |||
| 167 | while (!readdir_r(proc, &dirent, &next) && next) { | ||
| 168 | char *end; | ||
| 169 | pid_t pid = strtol(dirent.d_name, &end, 10); | ||
| 170 | |||
| 171 | if (*end) /* only interested in proper numerical dirents */ | ||
| 172 | continue; | ||
| 173 | |||
| 174 | event__synthesize_thread(pid, process); | ||
| 175 | } | ||
| 176 | |||
| 177 | closedir(proc); | ||
| 178 | } | ||
| 179 | |||
| 180 | char *event__cwd; | ||
| 181 | int event__cwdlen; | ||
| 182 | |||
| 183 | struct events_stats event__stats; | ||
| 184 | |||
| 185 | int event__process_comm(event_t *self) | ||
| 186 | { | ||
| 187 | struct thread *thread = threads__findnew(self->comm.pid); | ||
| 188 | |||
| 189 | dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid); | ||
| 190 | |||
| 191 | if (thread == NULL || thread__set_comm(thread, self->comm.comm)) { | ||
| 192 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); | ||
| 193 | return -1; | ||
| 194 | } | ||
| 195 | |||
| 196 | return 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | int event__process_lost(event_t *self) | ||
| 200 | { | ||
| 201 | dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost); | ||
| 202 | event__stats.lost += self->lost.lost; | ||
| 203 | return 0; | ||
| 204 | } | ||
| 205 | |||
| 206 | int event__process_mmap(event_t *self) | ||
| 207 | { | ||
| 208 | struct thread *thread = threads__findnew(self->mmap.pid); | ||
| 209 | struct map *map = map__new(&self->mmap, MAP__FUNCTION, | ||
| 210 | event__cwd, event__cwdlen); | ||
| 211 | |||
| 212 | dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n", | ||
| 213 | self->mmap.pid, self->mmap.tid, | ||
| 214 | (void *)(long)self->mmap.start, | ||
| 215 | (void *)(long)self->mmap.len, | ||
| 216 | (void *)(long)self->mmap.pgoff, | ||
| 217 | self->mmap.filename); | ||
| 218 | |||
| 219 | if (thread == NULL || map == NULL) | ||
| 220 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); | ||
| 221 | else | ||
| 222 | thread__insert_map(thread, map); | ||
| 223 | |||
| 224 | return 0; | ||
| 225 | } | ||
| 226 | |||
| 227 | int event__process_task(event_t *self) | ||
| 228 | { | ||
| 229 | struct thread *thread = threads__findnew(self->fork.pid); | ||
| 230 | struct thread *parent = threads__findnew(self->fork.ppid); | ||
| 231 | |||
| 232 | dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid, | ||
| 233 | self->fork.ppid, self->fork.ptid); | ||
| 234 | /* | ||
| 235 | * A thread clone will have the same PID for both parent and child. | ||
| 236 | */ | ||
| 237 | if (thread == parent) | ||
| 238 | return 0; | ||
| 239 | |||
| 240 | if (self->header.type == PERF_RECORD_EXIT) | ||
| 241 | return 0; | ||
| 242 | |||
| 243 | if (thread == NULL || parent == NULL || | ||
| 244 | thread__fork(thread, parent) < 0) { | ||
| 245 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); | ||
| 246 | return -1; | ||
| 247 | } | ||
| 248 | |||
| 249 | return 0; | ||
| 250 | } | ||
| 251 | |||
| 252 | void thread__find_addr_location(struct thread *self, u8 cpumode, | ||
| 253 | enum map_type type, u64 addr, | ||
| 254 | struct addr_location *al, | ||
| 255 | symbol_filter_t filter) | ||
| 256 | { | ||
| 257 | struct thread *thread = al->thread = self; | ||
| 258 | |||
| 259 | al->addr = addr; | ||
| 260 | |||
| 261 | if (cpumode & PERF_RECORD_MISC_KERNEL) { | ||
| 262 | al->level = 'k'; | ||
| 263 | thread = kthread; | ||
| 264 | } else if (cpumode & PERF_RECORD_MISC_USER) | ||
| 265 | al->level = '.'; | ||
| 266 | else { | ||
| 267 | al->level = 'H'; | ||
| 268 | al->map = NULL; | ||
| 269 | al->sym = NULL; | ||
| 270 | return; | ||
| 271 | } | ||
| 272 | try_again: | ||
| 273 | al->map = thread__find_map(thread, type, al->addr); | ||
| 274 | if (al->map == NULL) { | ||
| 275 | /* | ||
| 276 | * If this is outside of all known maps, and is a negative | ||
| 277 | * address, try to look it up in the kernel dso, as it might be | ||
| 278 | * a vsyscall or vdso (which executes in user-mode). | ||
| 279 | * | ||
| 280 | * XXX This is nasty, we should have a symbol list in the | ||
| 281 | * "[vdso]" dso, but for now lets use the old trick of looking | ||
| 282 | * in the whole kernel symbol list. | ||
| 283 | */ | ||
| 284 | if ((long long)al->addr < 0 && thread != kthread) { | ||
| 285 | thread = kthread; | ||
| 286 | goto try_again; | ||
| 287 | } | ||
| 288 | al->sym = NULL; | ||
| 289 | } else { | ||
| 290 | al->addr = al->map->map_ip(al->map, al->addr); | ||
| 291 | al->sym = map__find_symbol(al->map, al->addr, filter); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | int event__preprocess_sample(const event_t *self, struct addr_location *al, | ||
| 296 | symbol_filter_t filter) | ||
| 297 | { | ||
| 298 | u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | ||
| 299 | struct thread *thread = threads__findnew(self->ip.pid); | ||
| 300 | |||
| 301 | if (thread == NULL) | ||
| 302 | return -1; | ||
| 303 | |||
| 304 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 305 | |||
| 306 | thread__find_addr_location(thread, cpumode, MAP__FUNCTION, | ||
| 307 | self->ip.ip, al, filter); | ||
| 308 | dump_printf(" ...... dso: %s\n", | ||
| 309 | al->map ? al->map->dso->long_name : | ||
| 310 | al->level == 'H' ? "[hypervisor]" : "<not found>"); | ||
| 311 | return 0; | ||
| 312 | } | ||
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index fa2d4e91d329..a4cc8105cf67 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h | |||
| @@ -1,14 +1,10 @@ | |||
| 1 | #ifndef __PERF_EVENT_H | 1 | #ifndef __PERF_RECORD_H |
| 2 | #define __PERF_EVENT_H | 2 | #define __PERF_RECORD_H |
| 3 | |||
| 3 | #include "../perf.h" | 4 | #include "../perf.h" |
| 4 | #include "util.h" | 5 | #include "util.h" |
| 5 | #include <linux/list.h> | 6 | #include <linux/list.h> |
| 6 | 7 | #include <linux/rbtree.h> | |
| 7 | enum { | ||
| 8 | SHOW_KERNEL = 1, | ||
| 9 | SHOW_USER = 2, | ||
| 10 | SHOW_HV = 4, | ||
| 11 | }; | ||
| 12 | 8 | ||
| 13 | /* | 9 | /* |
| 14 | * PERF_SAMPLE_IP | PERF_SAMPLE_TID | * | 10 | * PERF_SAMPLE_IP | PERF_SAMPLE_TID | * |
| @@ -39,6 +35,7 @@ struct fork_event { | |||
| 39 | struct perf_event_header header; | 35 | struct perf_event_header header; |
| 40 | u32 pid, ppid; | 36 | u32 pid, ppid; |
| 41 | u32 tid, ptid; | 37 | u32 tid, ptid; |
| 38 | u64 time; | ||
| 42 | }; | 39 | }; |
| 43 | 40 | ||
| 44 | struct lost_event { | 41 | struct lost_event { |
| @@ -52,13 +49,26 @@ struct lost_event { | |||
| 52 | */ | 49 | */ |
| 53 | struct read_event { | 50 | struct read_event { |
| 54 | struct perf_event_header header; | 51 | struct perf_event_header header; |
| 55 | u32 pid,tid; | 52 | u32 pid, tid; |
| 56 | u64 value; | 53 | u64 value; |
| 57 | u64 time_enabled; | 54 | u64 time_enabled; |
| 58 | u64 time_running; | 55 | u64 time_running; |
| 59 | u64 id; | 56 | u64 id; |
| 60 | }; | 57 | }; |
| 61 | 58 | ||
| 59 | struct sample_event{ | ||
| 60 | struct perf_event_header header; | ||
| 61 | u64 array[]; | ||
| 62 | }; | ||
| 63 | |||
| 64 | #define BUILD_ID_SIZE 20 | ||
| 65 | |||
| 66 | struct build_id_event { | ||
| 67 | struct perf_event_header header; | ||
| 68 | u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))]; | ||
| 69 | char filename[]; | ||
| 70 | }; | ||
| 71 | |||
| 62 | typedef union event_union { | 72 | typedef union event_union { |
| 63 | struct perf_event_header header; | 73 | struct perf_event_header header; |
| 64 | struct ip_event ip; | 74 | struct ip_event ip; |
| @@ -67,14 +77,33 @@ typedef union event_union { | |||
| 67 | struct fork_event fork; | 77 | struct fork_event fork; |
| 68 | struct lost_event lost; | 78 | struct lost_event lost; |
| 69 | struct read_event read; | 79 | struct read_event read; |
| 80 | struct sample_event sample; | ||
| 70 | } event_t; | 81 | } event_t; |
| 71 | 82 | ||
| 83 | struct events_stats { | ||
| 84 | unsigned long total; | ||
| 85 | unsigned long lost; | ||
| 86 | }; | ||
| 87 | |||
| 88 | void event__print_totals(void); | ||
| 89 | |||
| 90 | enum map_type { | ||
| 91 | MAP__FUNCTION = 0, | ||
| 92 | |||
| 93 | MAP__NR_TYPES, | ||
| 94 | }; | ||
| 95 | |||
| 72 | struct map { | 96 | struct map { |
| 73 | struct list_head node; | 97 | union { |
| 98 | struct rb_node rb_node; | ||
| 99 | struct list_head node; | ||
| 100 | }; | ||
| 74 | u64 start; | 101 | u64 start; |
| 75 | u64 end; | 102 | u64 end; |
| 103 | enum map_type type; | ||
| 76 | u64 pgoff; | 104 | u64 pgoff; |
| 77 | u64 (*map_ip)(struct map *, u64); | 105 | u64 (*map_ip)(struct map *, u64); |
| 106 | u64 (*unmap_ip)(struct map *, u64); | ||
| 78 | struct dso *dso; | 107 | struct dso *dso; |
| 79 | }; | 108 | }; |
| 80 | 109 | ||
| @@ -83,14 +112,48 @@ static inline u64 map__map_ip(struct map *map, u64 ip) | |||
| 83 | return ip - map->start + map->pgoff; | 112 | return ip - map->start + map->pgoff; |
| 84 | } | 113 | } |
| 85 | 114 | ||
| 86 | static inline u64 vdso__map_ip(struct map *map __used, u64 ip) | 115 | static inline u64 map__unmap_ip(struct map *map, u64 ip) |
| 116 | { | ||
| 117 | return ip + map->start - map->pgoff; | ||
| 118 | } | ||
| 119 | |||
| 120 | static inline u64 identity__map_ip(struct map *map __used, u64 ip) | ||
| 87 | { | 121 | { |
| 88 | return ip; | 122 | return ip; |
| 89 | } | 123 | } |
| 90 | 124 | ||
| 91 | struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen); | 125 | struct symbol; |
| 126 | |||
| 127 | typedef int (*symbol_filter_t)(struct map *map, struct symbol *sym); | ||
| 128 | |||
| 129 | void map__init(struct map *self, enum map_type type, | ||
| 130 | u64 start, u64 end, u64 pgoff, struct dso *dso); | ||
| 131 | struct map *map__new(struct mmap_event *event, enum map_type, | ||
| 132 | char *cwd, int cwdlen); | ||
| 133 | void map__delete(struct map *self); | ||
| 92 | struct map *map__clone(struct map *self); | 134 | struct map *map__clone(struct map *self); |
| 93 | int map__overlap(struct map *l, struct map *r); | 135 | int map__overlap(struct map *l, struct map *r); |
| 94 | size_t map__fprintf(struct map *self, FILE *fp); | 136 | size_t map__fprintf(struct map *self, FILE *fp); |
| 137 | struct symbol *map__find_symbol(struct map *self, u64 addr, | ||
| 138 | symbol_filter_t filter); | ||
| 139 | void map__fixup_start(struct map *self); | ||
| 140 | void map__fixup_end(struct map *self); | ||
| 141 | |||
| 142 | int event__synthesize_thread(pid_t pid, int (*process)(event_t *event)); | ||
| 143 | void event__synthesize_threads(int (*process)(event_t *event)); | ||
| 144 | |||
| 145 | extern char *event__cwd; | ||
| 146 | extern int event__cwdlen; | ||
| 147 | extern struct events_stats event__stats; | ||
| 148 | extern unsigned long event__total[PERF_RECORD_MAX]; | ||
| 149 | |||
| 150 | int event__process_comm(event_t *self); | ||
| 151 | int event__process_lost(event_t *self); | ||
| 152 | int event__process_mmap(event_t *self); | ||
| 153 | int event__process_task(event_t *self); | ||
| 154 | |||
| 155 | struct addr_location; | ||
| 156 | int event__preprocess_sample(const event_t *self, struct addr_location *al, | ||
| 157 | symbol_filter_t filter); | ||
| 95 | 158 | ||
| 96 | #endif | 159 | #endif /* __PERF_RECORD_H */ |
diff --git a/tools/perf/util/exec_cmd.h b/tools/perf/util/exec_cmd.h index effe25eb1545..31647ac92ed1 100644 --- a/tools/perf/util/exec_cmd.h +++ b/tools/perf/util/exec_cmd.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef PERF_EXEC_CMD_H | 1 | #ifndef __PERF_EXEC_CMD_H |
| 2 | #define PERF_EXEC_CMD_H | 2 | #define __PERF_EXEC_CMD_H |
| 3 | 3 | ||
| 4 | extern void perf_set_argv_exec_path(const char *exec_path); | 4 | extern void perf_set_argv_exec_path(const char *exec_path); |
| 5 | extern const char *perf_extract_argv0_path(const char *path); | 5 | extern const char *perf_extract_argv0_path(const char *path); |
| @@ -10,4 +10,4 @@ extern int execv_perf_cmd(const char **argv); /* NULL terminated */ | |||
| 10 | extern int execl_perf_cmd(const char *cmd, ...); | 10 | extern int execl_perf_cmd(const char *cmd, ...); |
| 11 | extern const char *system_path(const char *path); | 11 | extern const char *system_path(const char *path); |
| 12 | 12 | ||
| 13 | #endif /* PERF_EXEC_CMD_H */ | 13 | #endif /* __PERF_EXEC_CMD_H */ |
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index ec4d4c2f9522..4805e6dfd23c 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c | |||
| @@ -2,131 +2,290 @@ | |||
| 2 | #include <unistd.h> | 2 | #include <unistd.h> |
| 3 | #include <stdio.h> | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 5 | #include <linux/list.h> | ||
| 5 | 6 | ||
| 6 | #include "util.h" | 7 | #include "util.h" |
| 7 | #include "header.h" | 8 | #include "header.h" |
| 9 | #include "../perf.h" | ||
| 10 | #include "trace-event.h" | ||
| 11 | #include "symbol.h" | ||
| 12 | #include "data_map.h" | ||
| 13 | #include "debug.h" | ||
| 8 | 14 | ||
| 9 | /* | 15 | /* |
| 10 | * | 16 | * Create new perf.data header attribute: |
| 11 | */ | 17 | */ |
| 12 | 18 | struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr) | |
| 13 | struct perf_header_attr *perf_header_attr__new(struct perf_counter_attr *attr) | ||
| 14 | { | 19 | { |
| 15 | struct perf_header_attr *self = malloc(sizeof(*self)); | 20 | struct perf_header_attr *self = malloc(sizeof(*self)); |
| 16 | 21 | ||
| 17 | if (!self) | 22 | if (self != NULL) { |
| 18 | die("nomem"); | 23 | self->attr = *attr; |
| 19 | 24 | self->ids = 0; | |
| 20 | self->attr = *attr; | 25 | self->size = 1; |
| 21 | self->ids = 0; | 26 | self->id = malloc(sizeof(u64)); |
| 22 | self->size = 1; | 27 | if (self->id == NULL) { |
| 23 | self->id = malloc(sizeof(u64)); | 28 | free(self); |
| 24 | 29 | self = NULL; | |
| 25 | if (!self->id) | 30 | } |
| 26 | die("nomem"); | 31 | } |
| 27 | 32 | ||
| 28 | return self; | 33 | return self; |
| 29 | } | 34 | } |
| 30 | 35 | ||
| 31 | void perf_header_attr__add_id(struct perf_header_attr *self, u64 id) | 36 | void perf_header_attr__delete(struct perf_header_attr *self) |
| 37 | { | ||
| 38 | free(self->id); | ||
| 39 | free(self); | ||
| 40 | } | ||
| 41 | |||
| 42 | int perf_header_attr__add_id(struct perf_header_attr *self, u64 id) | ||
| 32 | { | 43 | { |
| 33 | int pos = self->ids; | 44 | int pos = self->ids; |
| 34 | 45 | ||
| 35 | self->ids++; | 46 | self->ids++; |
| 36 | if (self->ids > self->size) { | 47 | if (self->ids > self->size) { |
| 37 | self->size *= 2; | 48 | int nsize = self->size * 2; |
| 38 | self->id = realloc(self->id, self->size * sizeof(u64)); | 49 | u64 *nid = realloc(self->id, nsize * sizeof(u64)); |
| 39 | if (!self->id) | 50 | |
| 40 | die("nomem"); | 51 | if (nid == NULL) |
| 52 | return -1; | ||
| 53 | |||
| 54 | self->size = nsize; | ||
| 55 | self->id = nid; | ||
| 41 | } | 56 | } |
| 42 | self->id[pos] = id; | 57 | self->id[pos] = id; |
| 58 | return 0; | ||
| 43 | } | 59 | } |
| 44 | 60 | ||
| 45 | /* | 61 | /* |
| 46 | * | 62 | * Create new perf.data header: |
| 47 | */ | 63 | */ |
| 48 | |||
| 49 | struct perf_header *perf_header__new(void) | 64 | struct perf_header *perf_header__new(void) |
| 50 | { | 65 | { |
| 51 | struct perf_header *self = malloc(sizeof(*self)); | 66 | struct perf_header *self = zalloc(sizeof(*self)); |
| 52 | 67 | ||
| 53 | if (!self) | 68 | if (self != NULL) { |
| 54 | die("nomem"); | 69 | self->size = 1; |
| 70 | self->attr = malloc(sizeof(void *)); | ||
| 55 | 71 | ||
| 56 | self->frozen = 0; | 72 | if (self->attr == NULL) { |
| 73 | free(self); | ||
| 74 | self = NULL; | ||
| 75 | } | ||
| 76 | } | ||
| 57 | 77 | ||
| 58 | self->attrs = 0; | 78 | return self; |
| 59 | self->size = 1; | 79 | } |
| 60 | self->attr = malloc(sizeof(void *)); | ||
| 61 | 80 | ||
| 62 | if (!self->attr) | 81 | void perf_header__delete(struct perf_header *self) |
| 63 | die("nomem"); | 82 | { |
| 83 | int i; | ||
| 64 | 84 | ||
| 65 | self->data_offset = 0; | 85 | for (i = 0; i < self->attrs; ++i) |
| 66 | self->data_size = 0; | 86 | perf_header_attr__delete(self->attr[i]); |
| 67 | 87 | ||
| 68 | return self; | 88 | free(self->attr); |
| 89 | free(self); | ||
| 69 | } | 90 | } |
| 70 | 91 | ||
| 71 | void perf_header__add_attr(struct perf_header *self, | 92 | int perf_header__add_attr(struct perf_header *self, |
| 72 | struct perf_header_attr *attr) | 93 | struct perf_header_attr *attr) |
| 73 | { | 94 | { |
| 74 | int pos = self->attrs; | ||
| 75 | |||
| 76 | if (self->frozen) | 95 | if (self->frozen) |
| 77 | die("frozen"); | 96 | return -1; |
| 97 | |||
| 98 | if (self->attrs == self->size) { | ||
| 99 | int nsize = self->size * 2; | ||
| 100 | struct perf_header_attr **nattr; | ||
| 101 | |||
| 102 | nattr = realloc(self->attr, nsize * sizeof(void *)); | ||
| 103 | if (nattr == NULL) | ||
| 104 | return -1; | ||
| 105 | |||
| 106 | self->size = nsize; | ||
| 107 | self->attr = nattr; | ||
| 108 | } | ||
| 109 | |||
| 110 | self->attr[self->attrs++] = attr; | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | #define MAX_EVENT_NAME 64 | ||
| 115 | |||
| 116 | struct perf_trace_event_type { | ||
| 117 | u64 event_id; | ||
| 118 | char name[MAX_EVENT_NAME]; | ||
| 119 | }; | ||
| 120 | |||
| 121 | static int event_count; | ||
| 122 | static struct perf_trace_event_type *events; | ||
| 123 | |||
| 124 | void perf_header__push_event(u64 id, const char *name) | ||
| 125 | { | ||
| 126 | if (strlen(name) > MAX_EVENT_NAME) | ||
| 127 | pr_warning("Event %s will be truncated\n", name); | ||
| 78 | 128 | ||
| 79 | self->attrs++; | 129 | if (!events) { |
| 80 | if (self->attrs > self->size) { | 130 | events = malloc(sizeof(struct perf_trace_event_type)); |
| 81 | self->size *= 2; | 131 | if (!events) |
| 82 | self->attr = realloc(self->attr, self->size * sizeof(void *)); | 132 | die("nomem"); |
| 83 | if (!self->attr) | 133 | } else { |
| 134 | events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type)); | ||
| 135 | if (!events) | ||
| 84 | die("nomem"); | 136 | die("nomem"); |
| 85 | } | 137 | } |
| 86 | self->attr[pos] = attr; | 138 | memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); |
| 139 | events[event_count].event_id = id; | ||
| 140 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); | ||
| 141 | event_count++; | ||
| 142 | } | ||
| 143 | |||
| 144 | char *perf_header__find_event(u64 id) | ||
| 145 | { | ||
| 146 | int i; | ||
| 147 | for (i = 0 ; i < event_count; i++) { | ||
| 148 | if (events[i].event_id == id) | ||
| 149 | return events[i].name; | ||
| 150 | } | ||
| 151 | return NULL; | ||
| 87 | } | 152 | } |
| 88 | 153 | ||
| 89 | static const char *__perf_magic = "PERFFILE"; | 154 | static const char *__perf_magic = "PERFFILE"; |
| 90 | 155 | ||
| 91 | #define PERF_MAGIC (*(u64 *)__perf_magic) | 156 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 92 | 157 | ||
| 93 | struct perf_file_section { | ||
| 94 | u64 offset; | ||
| 95 | u64 size; | ||
| 96 | }; | ||
| 97 | |||
| 98 | struct perf_file_attr { | 158 | struct perf_file_attr { |
| 99 | struct perf_counter_attr attr; | 159 | struct perf_event_attr attr; |
| 100 | struct perf_file_section ids; | 160 | struct perf_file_section ids; |
| 101 | }; | 161 | }; |
| 102 | 162 | ||
| 103 | struct perf_file_header { | 163 | void perf_header__set_feat(struct perf_header *self, int feat) |
| 104 | u64 magic; | 164 | { |
| 105 | u64 size; | 165 | set_bit(feat, self->adds_features); |
| 106 | u64 attr_size; | 166 | } |
| 107 | struct perf_file_section attrs; | 167 | |
| 108 | struct perf_file_section data; | 168 | bool perf_header__has_feat(const struct perf_header *self, int feat) |
| 109 | }; | 169 | { |
| 170 | return test_bit(feat, self->adds_features); | ||
| 171 | } | ||
| 110 | 172 | ||
| 111 | static void do_write(int fd, void *buf, size_t size) | 173 | static int do_write(int fd, const void *buf, size_t size) |
| 112 | { | 174 | { |
| 113 | while (size) { | 175 | while (size) { |
| 114 | int ret = write(fd, buf, size); | 176 | int ret = write(fd, buf, size); |
| 115 | 177 | ||
| 116 | if (ret < 0) | 178 | if (ret < 0) |
| 117 | die("failed to write"); | 179 | return -errno; |
| 118 | 180 | ||
| 119 | size -= ret; | 181 | size -= ret; |
| 120 | buf += ret; | 182 | buf += ret; |
| 121 | } | 183 | } |
| 184 | |||
| 185 | return 0; | ||
| 186 | } | ||
| 187 | |||
| 188 | static int __dsos__write_buildid_table(struct list_head *head, int fd) | ||
| 189 | { | ||
| 190 | struct dso *pos; | ||
| 191 | |||
| 192 | list_for_each_entry(pos, head, node) { | ||
| 193 | int err; | ||
| 194 | struct build_id_event b; | ||
| 195 | size_t len; | ||
| 196 | |||
| 197 | if (!pos->has_build_id) | ||
| 198 | continue; | ||
| 199 | len = pos->long_name_len + 1; | ||
| 200 | len = ALIGN(len, 64); | ||
| 201 | memset(&b, 0, sizeof(b)); | ||
| 202 | memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); | ||
| 203 | b.header.size = sizeof(b) + len; | ||
| 204 | err = do_write(fd, &b, sizeof(b)); | ||
| 205 | if (err < 0) | ||
| 206 | return err; | ||
| 207 | err = do_write(fd, pos->long_name, len); | ||
| 208 | if (err < 0) | ||
| 209 | return err; | ||
| 210 | } | ||
| 211 | |||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | static int dsos__write_buildid_table(int fd) | ||
| 216 | { | ||
| 217 | int err = __dsos__write_buildid_table(&dsos__kernel, fd); | ||
| 218 | if (err == 0) | ||
| 219 | err = __dsos__write_buildid_table(&dsos__user, fd); | ||
| 220 | return err; | ||
| 221 | } | ||
| 222 | |||
| 223 | static int perf_header__adds_write(struct perf_header *self, int fd) | ||
| 224 | { | ||
| 225 | int nr_sections; | ||
| 226 | struct perf_file_section *feat_sec; | ||
| 227 | int sec_size; | ||
| 228 | u64 sec_start; | ||
| 229 | int idx = 0, err; | ||
| 230 | |||
| 231 | if (dsos__read_build_ids()) | ||
| 232 | perf_header__set_feat(self, HEADER_BUILD_ID); | ||
| 233 | |||
| 234 | nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); | ||
| 235 | if (!nr_sections) | ||
| 236 | return 0; | ||
| 237 | |||
| 238 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); | ||
| 239 | if (feat_sec == NULL) | ||
| 240 | return -ENOMEM; | ||
| 241 | |||
| 242 | sec_size = sizeof(*feat_sec) * nr_sections; | ||
| 243 | |||
| 244 | sec_start = self->data_offset + self->data_size; | ||
| 245 | lseek(fd, sec_start + sec_size, SEEK_SET); | ||
| 246 | |||
| 247 | if (perf_header__has_feat(self, HEADER_TRACE_INFO)) { | ||
| 248 | struct perf_file_section *trace_sec; | ||
| 249 | |||
| 250 | trace_sec = &feat_sec[idx++]; | ||
| 251 | |||
| 252 | /* Write trace info */ | ||
| 253 | trace_sec->offset = lseek(fd, 0, SEEK_CUR); | ||
| 254 | read_tracing_data(fd, attrs, nr_counters); | ||
| 255 | trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset; | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | if (perf_header__has_feat(self, HEADER_BUILD_ID)) { | ||
| 260 | struct perf_file_section *buildid_sec; | ||
| 261 | |||
| 262 | buildid_sec = &feat_sec[idx++]; | ||
| 263 | |||
| 264 | /* Write build-ids */ | ||
| 265 | buildid_sec->offset = lseek(fd, 0, SEEK_CUR); | ||
| 266 | err = dsos__write_buildid_table(fd); | ||
| 267 | if (err < 0) { | ||
| 268 | pr_debug("failed to write buildid table\n"); | ||
| 269 | goto out_free; | ||
| 270 | } | ||
| 271 | buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset; | ||
| 272 | } | ||
| 273 | |||
| 274 | lseek(fd, sec_start, SEEK_SET); | ||
| 275 | err = do_write(fd, feat_sec, sec_size); | ||
| 276 | if (err < 0) | ||
| 277 | pr_debug("failed to write feature section\n"); | ||
| 278 | out_free: | ||
| 279 | free(feat_sec); | ||
| 280 | return err; | ||
| 122 | } | 281 | } |
| 123 | 282 | ||
| 124 | void perf_header__write(struct perf_header *self, int fd) | 283 | int perf_header__write(struct perf_header *self, int fd, bool at_exit) |
| 125 | { | 284 | { |
| 126 | struct perf_file_header f_header; | 285 | struct perf_file_header f_header; |
| 127 | struct perf_file_attr f_attr; | 286 | struct perf_file_attr f_attr; |
| 128 | struct perf_header_attr *attr; | 287 | struct perf_header_attr *attr; |
| 129 | int i; | 288 | int i, err; |
| 130 | 289 | ||
| 131 | lseek(fd, sizeof(f_header), SEEK_SET); | 290 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 132 | 291 | ||
| @@ -135,7 +294,11 @@ void perf_header__write(struct perf_header *self, int fd) | |||
| 135 | attr = self->attr[i]; | 294 | attr = self->attr[i]; |
| 136 | 295 | ||
| 137 | attr->id_offset = lseek(fd, 0, SEEK_CUR); | 296 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
| 138 | do_write(fd, attr->id, attr->ids * sizeof(u64)); | 297 | err = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 298 | if (err < 0) { | ||
| 299 | pr_debug("failed to write perf header\n"); | ||
| 300 | return err; | ||
| 301 | } | ||
| 139 | } | 302 | } |
| 140 | 303 | ||
| 141 | 304 | ||
| @@ -151,12 +314,31 @@ void perf_header__write(struct perf_header *self, int fd) | |||
| 151 | .size = attr->ids * sizeof(u64), | 314 | .size = attr->ids * sizeof(u64), |
| 152 | } | 315 | } |
| 153 | }; | 316 | }; |
| 154 | do_write(fd, &f_attr, sizeof(f_attr)); | 317 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 318 | if (err < 0) { | ||
| 319 | pr_debug("failed to write perf header attribute\n"); | ||
| 320 | return err; | ||
| 321 | } | ||
| 155 | } | 322 | } |
| 156 | 323 | ||
| 324 | self->event_offset = lseek(fd, 0, SEEK_CUR); | ||
| 325 | self->event_size = event_count * sizeof(struct perf_trace_event_type); | ||
| 326 | if (events) { | ||
| 327 | err = do_write(fd, events, self->event_size); | ||
| 328 | if (err < 0) { | ||
| 329 | pr_debug("failed to write perf header events\n"); | ||
| 330 | return err; | ||
| 331 | } | ||
| 332 | } | ||
| 157 | 333 | ||
| 158 | self->data_offset = lseek(fd, 0, SEEK_CUR); | 334 | self->data_offset = lseek(fd, 0, SEEK_CUR); |
| 159 | 335 | ||
| 336 | if (at_exit) { | ||
| 337 | err = perf_header__adds_write(self, fd); | ||
| 338 | if (err < 0) | ||
| 339 | return err; | ||
| 340 | } | ||
| 341 | |||
| 160 | f_header = (struct perf_file_header){ | 342 | f_header = (struct perf_file_header){ |
| 161 | .magic = PERF_MAGIC, | 343 | .magic = PERF_MAGIC, |
| 162 | .size = sizeof(f_header), | 344 | .size = sizeof(f_header), |
| @@ -169,13 +351,24 @@ void perf_header__write(struct perf_header *self, int fd) | |||
| 169 | .offset = self->data_offset, | 351 | .offset = self->data_offset, |
| 170 | .size = self->data_size, | 352 | .size = self->data_size, |
| 171 | }, | 353 | }, |
| 354 | .event_types = { | ||
| 355 | .offset = self->event_offset, | ||
| 356 | .size = self->event_size, | ||
| 357 | }, | ||
| 172 | }; | 358 | }; |
| 173 | 359 | ||
| 360 | memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features)); | ||
| 361 | |||
| 174 | lseek(fd, 0, SEEK_SET); | 362 | lseek(fd, 0, SEEK_SET); |
| 175 | do_write(fd, &f_header, sizeof(f_header)); | 363 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 364 | if (err < 0) { | ||
| 365 | pr_debug("failed to write perf header\n"); | ||
| 366 | return err; | ||
| 367 | } | ||
| 176 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); | 368 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
| 177 | 369 | ||
| 178 | self->frozen = 1; | 370 | self->frozen = 1; |
| 371 | return 0; | ||
| 179 | } | 372 | } |
| 180 | 373 | ||
| 181 | static void do_read(int fd, void *buf, size_t size) | 374 | static void do_read(int fd, void *buf, size_t size) |
| @@ -193,22 +386,109 @@ static void do_read(int fd, void *buf, size_t size) | |||
| 193 | } | 386 | } |
| 194 | } | 387 | } |
| 195 | 388 | ||
| 196 | struct perf_header *perf_header__read(int fd) | 389 | int perf_header__process_sections(struct perf_header *self, int fd, |
| 390 | int (*process)(struct perf_file_section *self, | ||
| 391 | int feat, int fd)) | ||
| 392 | { | ||
| 393 | struct perf_file_section *feat_sec; | ||
| 394 | int nr_sections; | ||
| 395 | int sec_size; | ||
| 396 | int idx = 0; | ||
| 397 | int err = 0, feat = 1; | ||
| 398 | |||
| 399 | nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); | ||
| 400 | if (!nr_sections) | ||
| 401 | return 0; | ||
| 402 | |||
| 403 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); | ||
| 404 | if (!feat_sec) | ||
| 405 | return -1; | ||
| 406 | |||
| 407 | sec_size = sizeof(*feat_sec) * nr_sections; | ||
| 408 | |||
| 409 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); | ||
| 410 | |||
| 411 | do_read(fd, feat_sec, sec_size); | ||
| 412 | |||
| 413 | while (idx < nr_sections && feat < HEADER_LAST_FEATURE) { | ||
| 414 | if (perf_header__has_feat(self, feat)) { | ||
| 415 | struct perf_file_section *sec = &feat_sec[idx++]; | ||
| 416 | |||
| 417 | err = process(sec, feat, fd); | ||
| 418 | if (err < 0) | ||
| 419 | break; | ||
| 420 | } | ||
| 421 | ++feat; | ||
| 422 | } | ||
| 423 | |||
| 424 | free(feat_sec); | ||
| 425 | return err; | ||
| 426 | }; | ||
| 427 | |||
| 428 | int perf_file_header__read(struct perf_file_header *self, | ||
| 429 | struct perf_header *ph, int fd) | ||
| 430 | { | ||
| 431 | lseek(fd, 0, SEEK_SET); | ||
| 432 | do_read(fd, self, sizeof(*self)); | ||
| 433 | |||
| 434 | if (self->magic != PERF_MAGIC || | ||
| 435 | self->attr_size != sizeof(struct perf_file_attr)) | ||
| 436 | return -1; | ||
| 437 | |||
| 438 | if (self->size != sizeof(*self)) { | ||
| 439 | /* Support the previous format */ | ||
| 440 | if (self->size == offsetof(typeof(*self), adds_features)) | ||
| 441 | bitmap_zero(self->adds_features, HEADER_FEAT_BITS); | ||
| 442 | else | ||
| 443 | return -1; | ||
| 444 | } | ||
| 445 | |||
| 446 | memcpy(&ph->adds_features, &self->adds_features, | ||
| 447 | sizeof(self->adds_features)); | ||
| 448 | |||
| 449 | ph->event_offset = self->event_types.offset; | ||
| 450 | ph->event_size = self->event_types.size; | ||
| 451 | ph->data_offset = self->data.offset; | ||
| 452 | ph->data_size = self->data.size; | ||
| 453 | return 0; | ||
| 454 | } | ||
| 455 | |||
| 456 | static int perf_file_section__process(struct perf_file_section *self, | ||
| 457 | int feat, int fd) | ||
| 458 | { | ||
| 459 | if (lseek(fd, self->offset, SEEK_SET) < 0) { | ||
| 460 | pr_debug("Failed to lseek to %Ld offset for feature %d, " | ||
| 461 | "continuing...\n", self->offset, feat); | ||
| 462 | return 0; | ||
| 463 | } | ||
| 464 | |||
| 465 | switch (feat) { | ||
| 466 | case HEADER_TRACE_INFO: | ||
| 467 | trace_report(fd); | ||
| 468 | break; | ||
| 469 | |||
| 470 | case HEADER_BUILD_ID: | ||
| 471 | if (perf_header__read_build_ids(fd, self->offset, self->size)) | ||
| 472 | pr_debug("Failed to read buildids, continuing...\n"); | ||
| 473 | break; | ||
| 474 | default: | ||
| 475 | pr_debug("unknown feature %d, continuing...\n", feat); | ||
| 476 | } | ||
| 477 | |||
| 478 | return 0; | ||
| 479 | } | ||
| 480 | |||
| 481 | int perf_header__read(struct perf_header *self, int fd) | ||
| 197 | { | 482 | { |
| 198 | struct perf_header *self = perf_header__new(); | ||
| 199 | struct perf_file_header f_header; | 483 | struct perf_file_header f_header; |
| 200 | struct perf_file_attr f_attr; | 484 | struct perf_file_attr f_attr; |
| 201 | u64 f_id; | 485 | u64 f_id; |
| 202 | |||
| 203 | int nr_attrs, nr_ids, i, j; | 486 | int nr_attrs, nr_ids, i, j; |
| 204 | 487 | ||
| 205 | lseek(fd, 0, SEEK_SET); | 488 | if (perf_file_header__read(&f_header, self, fd) < 0) { |
| 206 | do_read(fd, &f_header, sizeof(f_header)); | 489 | pr_debug("incompatible file format\n"); |
| 207 | 490 | return -EINVAL; | |
| 208 | if (f_header.magic != PERF_MAGIC || | 491 | } |
| 209 | f_header.size != sizeof(f_header) || | ||
| 210 | f_header.attr_size != sizeof(f_attr)) | ||
| 211 | die("incompatible file format"); | ||
| 212 | 492 | ||
| 213 | nr_attrs = f_header.attrs.size / sizeof(f_attr); | 493 | nr_attrs = f_header.attrs.size / sizeof(f_attr); |
| 214 | lseek(fd, f_header.attrs.offset, SEEK_SET); | 494 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| @@ -221,6 +501,8 @@ struct perf_header *perf_header__read(int fd) | |||
| 221 | tmp = lseek(fd, 0, SEEK_CUR); | 501 | tmp = lseek(fd, 0, SEEK_CUR); |
| 222 | 502 | ||
| 223 | attr = perf_header_attr__new(&f_attr.attr); | 503 | attr = perf_header_attr__new(&f_attr.attr); |
| 504 | if (attr == NULL) | ||
| 505 | return -ENOMEM; | ||
| 224 | 506 | ||
| 225 | nr_ids = f_attr.ids.size / sizeof(u64); | 507 | nr_ids = f_attr.ids.size / sizeof(u64); |
| 226 | lseek(fd, f_attr.ids.offset, SEEK_SET); | 508 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| @@ -228,20 +510,34 @@ struct perf_header *perf_header__read(int fd) | |||
| 228 | for (j = 0; j < nr_ids; j++) { | 510 | for (j = 0; j < nr_ids; j++) { |
| 229 | do_read(fd, &f_id, sizeof(f_id)); | 511 | do_read(fd, &f_id, sizeof(f_id)); |
| 230 | 512 | ||
| 231 | perf_header_attr__add_id(attr, f_id); | 513 | if (perf_header_attr__add_id(attr, f_id) < 0) { |
| 514 | perf_header_attr__delete(attr); | ||
| 515 | return -ENOMEM; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | if (perf_header__add_attr(self, attr) < 0) { | ||
| 519 | perf_header_attr__delete(attr); | ||
| 520 | return -ENOMEM; | ||
| 232 | } | 521 | } |
| 233 | perf_header__add_attr(self, attr); | 522 | |
| 234 | lseek(fd, tmp, SEEK_SET); | 523 | lseek(fd, tmp, SEEK_SET); |
| 235 | } | 524 | } |
| 236 | 525 | ||
| 237 | self->data_offset = f_header.data.offset; | 526 | if (f_header.event_types.size) { |
| 238 | self->data_size = f_header.data.size; | 527 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 528 | events = malloc(f_header.event_types.size); | ||
| 529 | if (events == NULL) | ||
| 530 | return -ENOMEM; | ||
| 531 | do_read(fd, events, f_header.event_types.size); | ||
| 532 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); | ||
| 533 | } | ||
| 534 | |||
| 535 | perf_header__process_sections(self, fd, perf_file_section__process); | ||
| 239 | 536 | ||
| 240 | lseek(fd, self->data_offset, SEEK_SET); | 537 | lseek(fd, self->data_offset, SEEK_SET); |
| 241 | 538 | ||
| 242 | self->frozen = 1; | 539 | self->frozen = 1; |
| 243 | 540 | return 0; | |
| 244 | return self; | ||
| 245 | } | 541 | } |
| 246 | 542 | ||
| 247 | u64 perf_header__sample_type(struct perf_header *header) | 543 | u64 perf_header__sample_type(struct perf_header *header) |
| @@ -261,7 +557,7 @@ u64 perf_header__sample_type(struct perf_header *header) | |||
| 261 | return type; | 557 | return type; |
| 262 | } | 558 | } |
| 263 | 559 | ||
| 264 | struct perf_counter_attr * | 560 | struct perf_event_attr * |
| 265 | perf_header__find_attr(u64 id, struct perf_header *header) | 561 | perf_header__find_attr(u64 id, struct perf_header *header) |
| 266 | { | 562 | { |
| 267 | int i; | 563 | int i; |
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index 5d0a72ecc919..d1dbe2b79c42 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h | |||
| @@ -1,41 +1,85 @@ | |||
| 1 | #ifndef _PERF_HEADER_H | 1 | #ifndef __PERF_HEADER_H |
| 2 | #define _PERF_HEADER_H | 2 | #define __PERF_HEADER_H |
| 3 | 3 | ||
| 4 | #include "../../../include/linux/perf_counter.h" | 4 | #include "../../../include/linux/perf_event.h" |
| 5 | #include <sys/types.h> | 5 | #include <sys/types.h> |
| 6 | #include <stdbool.h> | ||
| 6 | #include "types.h" | 7 | #include "types.h" |
| 7 | 8 | ||
| 9 | #include <linux/bitmap.h> | ||
| 10 | |||
| 8 | struct perf_header_attr { | 11 | struct perf_header_attr { |
| 9 | struct perf_counter_attr attr; | 12 | struct perf_event_attr attr; |
| 10 | int ids, size; | 13 | int ids, size; |
| 11 | u64 *id; | 14 | u64 *id; |
| 12 | off_t id_offset; | 15 | off_t id_offset; |
| 13 | }; | 16 | }; |
| 14 | 17 | ||
| 18 | enum { | ||
| 19 | HEADER_TRACE_INFO = 1, | ||
| 20 | HEADER_BUILD_ID, | ||
| 21 | HEADER_LAST_FEATURE, | ||
| 22 | }; | ||
| 23 | |||
| 24 | #define HEADER_FEAT_BITS 256 | ||
| 25 | |||
| 26 | struct perf_file_section { | ||
| 27 | u64 offset; | ||
| 28 | u64 size; | ||
| 29 | }; | ||
| 30 | |||
| 31 | struct perf_file_header { | ||
| 32 | u64 magic; | ||
| 33 | u64 size; | ||
| 34 | u64 attr_size; | ||
| 35 | struct perf_file_section attrs; | ||
| 36 | struct perf_file_section data; | ||
| 37 | struct perf_file_section event_types; | ||
| 38 | DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS); | ||
| 39 | }; | ||
| 40 | |||
| 41 | struct perf_header; | ||
| 42 | |||
| 43 | int perf_file_header__read(struct perf_file_header *self, | ||
| 44 | struct perf_header *ph, int fd); | ||
| 45 | |||
| 15 | struct perf_header { | 46 | struct perf_header { |
| 16 | int frozen; | 47 | int frozen; |
| 17 | int attrs, size; | 48 | int attrs, size; |
| 18 | struct perf_header_attr **attr; | 49 | struct perf_header_attr **attr; |
| 19 | s64 attr_offset; | 50 | s64 attr_offset; |
| 20 | u64 data_offset; | 51 | u64 data_offset; |
| 21 | u64 data_size; | 52 | u64 data_size; |
| 53 | u64 event_offset; | ||
| 54 | u64 event_size; | ||
| 55 | DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS); | ||
| 22 | }; | 56 | }; |
| 23 | 57 | ||
| 24 | struct perf_header *perf_header__read(int fd); | 58 | struct perf_header *perf_header__new(void); |
| 25 | void perf_header__write(struct perf_header *self, int fd); | 59 | void perf_header__delete(struct perf_header *self); |
| 60 | |||
| 61 | int perf_header__read(struct perf_header *self, int fd); | ||
| 62 | int perf_header__write(struct perf_header *self, int fd, bool at_exit); | ||
| 26 | 63 | ||
| 27 | void perf_header__add_attr(struct perf_header *self, | 64 | int perf_header__add_attr(struct perf_header *self, |
| 28 | struct perf_header_attr *attr); | 65 | struct perf_header_attr *attr); |
| 29 | 66 | ||
| 30 | struct perf_header_attr * | 67 | void perf_header__push_event(u64 id, const char *name); |
| 31 | perf_header_attr__new(struct perf_counter_attr *attr); | 68 | char *perf_header__find_event(u64 id); |
| 32 | void perf_header_attr__add_id(struct perf_header_attr *self, u64 id); | 69 | |
| 70 | struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr); | ||
| 71 | void perf_header_attr__delete(struct perf_header_attr *self); | ||
| 72 | |||
| 73 | int perf_header_attr__add_id(struct perf_header_attr *self, u64 id); | ||
| 33 | 74 | ||
| 34 | u64 perf_header__sample_type(struct perf_header *header); | 75 | u64 perf_header__sample_type(struct perf_header *header); |
| 35 | struct perf_counter_attr * | 76 | struct perf_event_attr * |
| 36 | perf_header__find_attr(u64 id, struct perf_header *header); | 77 | perf_header__find_attr(u64 id, struct perf_header *header); |
| 78 | void perf_header__set_feat(struct perf_header *self, int feat); | ||
| 79 | bool perf_header__has_feat(const struct perf_header *self, int feat); | ||
| 37 | 80 | ||
| 81 | int perf_header__process_sections(struct perf_header *self, int fd, | ||
| 82 | int (*process)(struct perf_file_section *self, | ||
| 83 | int feat, int fd)); | ||
| 38 | 84 | ||
| 39 | struct perf_header *perf_header__new(void); | 85 | #endif /* __PERF_HEADER_H */ |
| 40 | |||
| 41 | #endif /* _PERF_HEADER_H */ | ||
diff --git a/tools/perf/util/help.h b/tools/perf/util/help.h index 7128783637b4..7f5c6dedd714 100644 --- a/tools/perf/util/help.h +++ b/tools/perf/util/help.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef HELP_H | 1 | #ifndef __PERF_HELP_H |
| 2 | #define HELP_H | 2 | #define __PERF_HELP_H |
| 3 | 3 | ||
| 4 | struct cmdnames { | 4 | struct cmdnames { |
| 5 | size_t alloc; | 5 | size_t alloc; |
| @@ -26,4 +26,4 @@ int is_in_cmdlist(struct cmdnames *c, const char *s); | |||
| 26 | void list_commands(const char *title, struct cmdnames *main_cmds, | 26 | void list_commands(const char *title, struct cmdnames *main_cmds, |
| 27 | struct cmdnames *other_cmds); | 27 | struct cmdnames *other_cmds); |
| 28 | 28 | ||
| 29 | #endif /* HELP_H */ | 29 | #endif /* __PERF_HELP_H */ |
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c new file mode 100644 index 000000000000..0ebf6ee16caa --- /dev/null +++ b/tools/perf/util/hist.c | |||
| @@ -0,0 +1,202 @@ | |||
| 1 | #include "hist.h" | ||
| 2 | |||
| 3 | struct rb_root hist; | ||
| 4 | struct rb_root collapse_hists; | ||
| 5 | struct rb_root output_hists; | ||
| 6 | int callchain; | ||
| 7 | |||
| 8 | struct callchain_param callchain_param = { | ||
| 9 | .mode = CHAIN_GRAPH_REL, | ||
| 10 | .min_percent = 0.5 | ||
| 11 | }; | ||
| 12 | |||
| 13 | /* | ||
| 14 | * histogram, sorted on item, collects counts | ||
| 15 | */ | ||
| 16 | |||
| 17 | struct hist_entry *__hist_entry__add(struct addr_location *al, | ||
| 18 | struct symbol *sym_parent, | ||
| 19 | u64 count, bool *hit) | ||
| 20 | { | ||
| 21 | struct rb_node **p = &hist.rb_node; | ||
| 22 | struct rb_node *parent = NULL; | ||
| 23 | struct hist_entry *he; | ||
| 24 | struct hist_entry entry = { | ||
| 25 | .thread = al->thread, | ||
| 26 | .map = al->map, | ||
| 27 | .sym = al->sym, | ||
| 28 | .ip = al->addr, | ||
| 29 | .level = al->level, | ||
| 30 | .count = count, | ||
| 31 | .parent = sym_parent, | ||
| 32 | }; | ||
| 33 | int cmp; | ||
| 34 | |||
| 35 | while (*p != NULL) { | ||
| 36 | parent = *p; | ||
| 37 | he = rb_entry(parent, struct hist_entry, rb_node); | ||
| 38 | |||
| 39 | cmp = hist_entry__cmp(&entry, he); | ||
| 40 | |||
| 41 | if (!cmp) { | ||
| 42 | *hit = true; | ||
| 43 | return he; | ||
| 44 | } | ||
| 45 | |||
| 46 | if (cmp < 0) | ||
| 47 | p = &(*p)->rb_left; | ||
| 48 | else | ||
| 49 | p = &(*p)->rb_right; | ||
| 50 | } | ||
| 51 | |||
| 52 | he = malloc(sizeof(*he)); | ||
| 53 | if (!he) | ||
| 54 | return NULL; | ||
| 55 | *he = entry; | ||
| 56 | rb_link_node(&he->rb_node, parent, p); | ||
| 57 | rb_insert_color(&he->rb_node, &hist); | ||
| 58 | *hit = false; | ||
| 59 | return he; | ||
| 60 | } | ||
| 61 | |||
| 62 | int64_t | ||
| 63 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 64 | { | ||
| 65 | struct sort_entry *se; | ||
| 66 | int64_t cmp = 0; | ||
| 67 | |||
| 68 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
| 69 | cmp = se->cmp(left, right); | ||
| 70 | if (cmp) | ||
| 71 | break; | ||
| 72 | } | ||
| 73 | |||
| 74 | return cmp; | ||
| 75 | } | ||
| 76 | |||
| 77 | int64_t | ||
| 78 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | ||
| 79 | { | ||
| 80 | struct sort_entry *se; | ||
| 81 | int64_t cmp = 0; | ||
| 82 | |||
| 83 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
| 84 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | ||
| 85 | |||
| 86 | f = se->collapse ?: se->cmp; | ||
| 87 | |||
| 88 | cmp = f(left, right); | ||
| 89 | if (cmp) | ||
| 90 | break; | ||
| 91 | } | ||
| 92 | |||
| 93 | return cmp; | ||
| 94 | } | ||
| 95 | |||
| 96 | void hist_entry__free(struct hist_entry *he) | ||
| 97 | { | ||
| 98 | free(he); | ||
| 99 | } | ||
| 100 | |||
| 101 | /* | ||
| 102 | * collapse the histogram | ||
| 103 | */ | ||
| 104 | |||
| 105 | void collapse__insert_entry(struct hist_entry *he) | ||
| 106 | { | ||
| 107 | struct rb_node **p = &collapse_hists.rb_node; | ||
| 108 | struct rb_node *parent = NULL; | ||
| 109 | struct hist_entry *iter; | ||
| 110 | int64_t cmp; | ||
| 111 | |||
| 112 | while (*p != NULL) { | ||
| 113 | parent = *p; | ||
| 114 | iter = rb_entry(parent, struct hist_entry, rb_node); | ||
| 115 | |||
| 116 | cmp = hist_entry__collapse(iter, he); | ||
| 117 | |||
| 118 | if (!cmp) { | ||
| 119 | iter->count += he->count; | ||
| 120 | hist_entry__free(he); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | if (cmp < 0) | ||
| 125 | p = &(*p)->rb_left; | ||
| 126 | else | ||
| 127 | p = &(*p)->rb_right; | ||
| 128 | } | ||
| 129 | |||
| 130 | rb_link_node(&he->rb_node, parent, p); | ||
| 131 | rb_insert_color(&he->rb_node, &collapse_hists); | ||
| 132 | } | ||
| 133 | |||
| 134 | void collapse__resort(void) | ||
| 135 | { | ||
| 136 | struct rb_node *next; | ||
| 137 | struct hist_entry *n; | ||
| 138 | |||
| 139 | if (!sort__need_collapse) | ||
| 140 | return; | ||
| 141 | |||
| 142 | next = rb_first(&hist); | ||
| 143 | while (next) { | ||
| 144 | n = rb_entry(next, struct hist_entry, rb_node); | ||
| 145 | next = rb_next(&n->rb_node); | ||
| 146 | |||
| 147 | rb_erase(&n->rb_node, &hist); | ||
| 148 | collapse__insert_entry(n); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | /* | ||
| 153 | * reverse the map, sort on count. | ||
| 154 | */ | ||
| 155 | |||
| 156 | void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits) | ||
| 157 | { | ||
| 158 | struct rb_node **p = &output_hists.rb_node; | ||
| 159 | struct rb_node *parent = NULL; | ||
| 160 | struct hist_entry *iter; | ||
| 161 | |||
| 162 | if (callchain) | ||
| 163 | callchain_param.sort(&he->sorted_chain, &he->callchain, | ||
| 164 | min_callchain_hits, &callchain_param); | ||
| 165 | |||
| 166 | while (*p != NULL) { | ||
| 167 | parent = *p; | ||
| 168 | iter = rb_entry(parent, struct hist_entry, rb_node); | ||
| 169 | |||
| 170 | if (he->count > iter->count) | ||
| 171 | p = &(*p)->rb_left; | ||
| 172 | else | ||
| 173 | p = &(*p)->rb_right; | ||
| 174 | } | ||
| 175 | |||
| 176 | rb_link_node(&he->rb_node, parent, p); | ||
| 177 | rb_insert_color(&he->rb_node, &output_hists); | ||
| 178 | } | ||
| 179 | |||
| 180 | void output__resort(u64 total_samples) | ||
| 181 | { | ||
| 182 | struct rb_node *next; | ||
| 183 | struct hist_entry *n; | ||
| 184 | struct rb_root *tree = &hist; | ||
| 185 | u64 min_callchain_hits; | ||
| 186 | |||
| 187 | min_callchain_hits = | ||
| 188 | total_samples * (callchain_param.min_percent / 100); | ||
| 189 | |||
| 190 | if (sort__need_collapse) | ||
| 191 | tree = &collapse_hists; | ||
| 192 | |||
| 193 | next = rb_first(tree); | ||
| 194 | |||
| 195 | while (next) { | ||
| 196 | n = rb_entry(next, struct hist_entry, rb_node); | ||
| 197 | next = rb_next(&n->rb_node); | ||
| 198 | |||
| 199 | rb_erase(&n->rb_node, tree); | ||
| 200 | output__insert_entry(n, min_callchain_hits); | ||
| 201 | } | ||
| 202 | } | ||
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h new file mode 100644 index 000000000000..3020db0c9292 --- /dev/null +++ b/tools/perf/util/hist.h | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #ifndef __PERF_HIST_H | ||
| 2 | #define __PERF_HIST_H | ||
| 3 | #include "../builtin.h" | ||
| 4 | |||
| 5 | #include "util.h" | ||
| 6 | |||
| 7 | #include "color.h" | ||
| 8 | #include <linux/list.h> | ||
| 9 | #include "cache.h" | ||
| 10 | #include <linux/rbtree.h> | ||
| 11 | #include "symbol.h" | ||
| 12 | #include "string.h" | ||
| 13 | #include "callchain.h" | ||
| 14 | #include "strlist.h" | ||
| 15 | #include "values.h" | ||
| 16 | |||
| 17 | #include "../perf.h" | ||
| 18 | #include "debug.h" | ||
| 19 | #include "header.h" | ||
| 20 | |||
| 21 | #include "parse-options.h" | ||
| 22 | #include "parse-events.h" | ||
| 23 | |||
| 24 | #include "thread.h" | ||
| 25 | #include "sort.h" | ||
| 26 | |||
| 27 | extern struct rb_root hist; | ||
| 28 | extern struct rb_root collapse_hists; | ||
| 29 | extern struct rb_root output_hists; | ||
| 30 | extern int callchain; | ||
| 31 | extern struct callchain_param callchain_param; | ||
| 32 | extern unsigned long total; | ||
| 33 | extern unsigned long total_mmap; | ||
| 34 | extern unsigned long total_comm; | ||
| 35 | extern unsigned long total_fork; | ||
| 36 | extern unsigned long total_unknown; | ||
| 37 | extern unsigned long total_lost; | ||
| 38 | |||
| 39 | struct hist_entry *__hist_entry__add(struct addr_location *al, | ||
| 40 | struct symbol *parent, | ||
| 41 | u64 count, bool *hit); | ||
| 42 | extern int64_t hist_entry__cmp(struct hist_entry *, struct hist_entry *); | ||
| 43 | extern int64_t hist_entry__collapse(struct hist_entry *, struct hist_entry *); | ||
| 44 | extern void hist_entry__free(struct hist_entry *); | ||
| 45 | extern void collapse__insert_entry(struct hist_entry *); | ||
| 46 | extern void collapse__resort(void); | ||
| 47 | extern void output__insert_entry(struct hist_entry *, u64); | ||
| 48 | extern void output__resort(u64); | ||
| 49 | |||
| 50 | #endif /* __PERF_HIST_H */ | ||
diff --git a/tools/perf/util/include/asm/asm-offsets.h b/tools/perf/util/include/asm/asm-offsets.h new file mode 100644 index 000000000000..ed538942523d --- /dev/null +++ b/tools/perf/util/include/asm/asm-offsets.h | |||
| @@ -0,0 +1 @@ | |||
| /* stub */ | |||
diff --git a/tools/perf/util/include/asm/bitops.h b/tools/perf/util/include/asm/bitops.h new file mode 100644 index 000000000000..58e9817ffae0 --- /dev/null +++ b/tools/perf/util/include/asm/bitops.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef _PERF_ASM_BITOPS_H_ | ||
| 2 | #define _PERF_ASM_BITOPS_H_ | ||
| 3 | |||
| 4 | #include <sys/types.h> | ||
| 5 | #include "../../types.h" | ||
| 6 | #include <linux/compiler.h> | ||
| 7 | |||
| 8 | /* CHECKME: Not sure both always match */ | ||
| 9 | #define BITS_PER_LONG __WORDSIZE | ||
| 10 | |||
| 11 | #include "../../../../include/asm-generic/bitops/__fls.h" | ||
| 12 | #include "../../../../include/asm-generic/bitops/fls.h" | ||
| 13 | #include "../../../../include/asm-generic/bitops/fls64.h" | ||
| 14 | #include "../../../../include/asm-generic/bitops/__ffs.h" | ||
| 15 | #include "../../../../include/asm-generic/bitops/ffz.h" | ||
| 16 | #include "../../../../include/asm-generic/bitops/hweight.h" | ||
| 17 | |||
| 18 | #endif | ||
diff --git a/tools/perf/util/include/asm/bug.h b/tools/perf/util/include/asm/bug.h new file mode 100644 index 000000000000..7fcc6810adc2 --- /dev/null +++ b/tools/perf/util/include/asm/bug.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #ifndef _PERF_ASM_GENERIC_BUG_H | ||
| 2 | #define _PERF_ASM_GENERIC_BUG_H | ||
| 3 | |||
| 4 | #define __WARN_printf(arg...) do { fprintf(stderr, arg); } while (0) | ||
| 5 | |||
| 6 | #define WARN(condition, format...) ({ \ | ||
| 7 | int __ret_warn_on = !!(condition); \ | ||
| 8 | if (unlikely(__ret_warn_on)) \ | ||
| 9 | __WARN_printf(format); \ | ||
| 10 | unlikely(__ret_warn_on); \ | ||
| 11 | }) | ||
| 12 | |||
| 13 | #define WARN_ONCE(condition, format...) ({ \ | ||
| 14 | static int __warned; \ | ||
| 15 | int __ret_warn_once = !!(condition); \ | ||
| 16 | \ | ||
| 17 | if (unlikely(__ret_warn_once)) \ | ||
| 18 | if (WARN(!__warned, format)) \ | ||
| 19 | __warned = 1; \ | ||
| 20 | unlikely(__ret_warn_once); \ | ||
| 21 | }) | ||
| 22 | #endif | ||
diff --git a/tools/perf/util/include/asm/byteorder.h b/tools/perf/util/include/asm/byteorder.h new file mode 100644 index 000000000000..b722abe3a626 --- /dev/null +++ b/tools/perf/util/include/asm/byteorder.h | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | #include <asm/types.h> | ||
| 2 | #include "../../../../include/linux/swab.h" | ||
diff --git a/tools/perf/util/include/asm/swab.h b/tools/perf/util/include/asm/swab.h new file mode 100644 index 000000000000..ed538942523d --- /dev/null +++ b/tools/perf/util/include/asm/swab.h | |||
| @@ -0,0 +1 @@ | |||
| /* stub */ | |||
diff --git a/tools/perf/util/include/asm/uaccess.h b/tools/perf/util/include/asm/uaccess.h new file mode 100644 index 000000000000..d0f72b8fcc35 --- /dev/null +++ b/tools/perf/util/include/asm/uaccess.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #ifndef _PERF_ASM_UACCESS_H_ | ||
| 2 | #define _PERF_ASM_UACCESS_H_ | ||
| 3 | |||
| 4 | #define __get_user(src, dest) \ | ||
| 5 | ({ \ | ||
| 6 | (src) = *dest; \ | ||
| 7 | 0; \ | ||
| 8 | }) | ||
| 9 | |||
| 10 | #define get_user __get_user | ||
| 11 | |||
| 12 | #define access_ok(type, addr, size) 1 | ||
| 13 | |||
| 14 | #endif | ||
diff --git a/tools/perf/util/include/linux/bitmap.h b/tools/perf/util/include/linux/bitmap.h new file mode 100644 index 000000000000..94507639a8c4 --- /dev/null +++ b/tools/perf/util/include/linux/bitmap.h | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | #include "../../../../include/linux/bitmap.h" | ||
| 2 | #include "../../../../include/asm-generic/bitops/find.h" | ||
| 3 | #include <linux/errno.h> | ||
diff --git a/tools/perf/util/include/linux/bitops.h b/tools/perf/util/include/linux/bitops.h new file mode 100644 index 000000000000..8d63116e9435 --- /dev/null +++ b/tools/perf/util/include/linux/bitops.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #ifndef _PERF_LINUX_BITOPS_H_ | ||
| 2 | #define _PERF_LINUX_BITOPS_H_ | ||
| 3 | |||
| 4 | #define __KERNEL__ | ||
| 5 | |||
| 6 | #define CONFIG_GENERIC_FIND_NEXT_BIT | ||
| 7 | #define CONFIG_GENERIC_FIND_FIRST_BIT | ||
| 8 | #include "../../../../include/linux/bitops.h" | ||
| 9 | |||
| 10 | #undef __KERNEL__ | ||
| 11 | |||
| 12 | static inline void set_bit(int nr, unsigned long *addr) | ||
| 13 | { | ||
| 14 | addr[nr / BITS_PER_LONG] |= 1UL << (nr % BITS_PER_LONG); | ||
| 15 | } | ||
| 16 | |||
| 17 | static __always_inline int test_bit(unsigned int nr, const unsigned long *addr) | ||
| 18 | { | ||
| 19 | return ((1UL << (nr % BITS_PER_LONG)) & | ||
| 20 | (((unsigned long *)addr)[nr / BITS_PER_LONG])) != 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned | ||
| 24 | long size, unsigned long offset); | ||
| 25 | |||
| 26 | unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned | ||
| 27 | long size, unsigned long offset); | ||
| 28 | |||
| 29 | #endif | ||
diff --git a/tools/perf/util/include/linux/compiler.h b/tools/perf/util/include/linux/compiler.h new file mode 100644 index 000000000000..dfb0713ed47f --- /dev/null +++ b/tools/perf/util/include/linux/compiler.h | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #ifndef _PERF_LINUX_COMPILER_H_ | ||
| 2 | #define _PERF_LINUX_COMPILER_H_ | ||
| 3 | |||
| 4 | #ifndef __always_inline | ||
| 5 | #define __always_inline inline | ||
| 6 | #endif | ||
| 7 | #define __user | ||
| 8 | #define __attribute_const__ | ||
| 9 | |||
| 10 | #endif | ||
diff --git a/tools/perf/util/include/linux/ctype.h b/tools/perf/util/include/linux/ctype.h new file mode 100644 index 000000000000..a53d4ee1e0b7 --- /dev/null +++ b/tools/perf/util/include/linux/ctype.h | |||
| @@ -0,0 +1 @@ | |||
| #include "../util.h" | |||
diff --git a/tools/perf/util/include/linux/kernel.h b/tools/perf/util/include/linux/kernel.h index a6b87390cb52..21c0274c02fa 100644 --- a/tools/perf/util/include/linux/kernel.h +++ b/tools/perf/util/include/linux/kernel.h | |||
| @@ -1,6 +1,16 @@ | |||
| 1 | #ifndef PERF_LINUX_KERNEL_H_ | 1 | #ifndef PERF_LINUX_KERNEL_H_ |
| 2 | #define PERF_LINUX_KERNEL_H_ | 2 | #define PERF_LINUX_KERNEL_H_ |
| 3 | 3 | ||
| 4 | #include <stdarg.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <assert.h> | ||
| 8 | |||
| 9 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) | ||
| 10 | |||
| 11 | #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) | ||
| 12 | #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) | ||
| 13 | |||
| 4 | #ifndef offsetof | 14 | #ifndef offsetof |
| 5 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | 15 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 6 | #endif | 16 | #endif |
| @@ -26,4 +36,70 @@ | |||
| 26 | _max1 > _max2 ? _max1 : _max2; }) | 36 | _max1 > _max2 ? _max1 : _max2; }) |
| 27 | #endif | 37 | #endif |
| 28 | 38 | ||
| 39 | #ifndef min | ||
| 40 | #define min(x, y) ({ \ | ||
| 41 | typeof(x) _min1 = (x); \ | ||
| 42 | typeof(y) _min2 = (y); \ | ||
| 43 | (void) (&_min1 == &_min2); \ | ||
| 44 | _min1 < _min2 ? _min1 : _min2; }) | ||
| 45 | #endif | ||
| 46 | |||
| 47 | #ifndef BUG_ON | ||
| 48 | #define BUG_ON(cond) assert(!(cond)) | ||
| 49 | #endif | ||
| 50 | |||
| 51 | /* | ||
| 52 | * Both need more care to handle endianness | ||
| 53 | * (Don't use bitmap_copy_le() for now) | ||
| 54 | */ | ||
| 55 | #define cpu_to_le64(x) (x) | ||
| 56 | #define cpu_to_le32(x) (x) | ||
| 57 | |||
| 58 | static inline int | ||
| 59 | vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | ||
| 60 | { | ||
| 61 | int i; | ||
| 62 | ssize_t ssize = size; | ||
| 63 | |||
| 64 | i = vsnprintf(buf, size, fmt, args); | ||
| 65 | |||
| 66 | return (i >= ssize) ? (ssize - 1) : i; | ||
| 67 | } | ||
| 68 | |||
| 69 | static inline int scnprintf(char * buf, size_t size, const char * fmt, ...) | ||
| 70 | { | ||
| 71 | va_list args; | ||
| 72 | ssize_t ssize = size; | ||
| 73 | int i; | ||
| 74 | |||
| 75 | va_start(args, fmt); | ||
| 76 | i = vsnprintf(buf, size, fmt, args); | ||
| 77 | va_end(args); | ||
| 78 | |||
| 79 | return (i >= ssize) ? (ssize - 1) : i; | ||
| 80 | } | ||
| 81 | |||
| 82 | static inline unsigned long | ||
| 83 | simple_strtoul(const char *nptr, char **endptr, int base) | ||
| 84 | { | ||
| 85 | return strtoul(nptr, endptr, base); | ||
| 86 | } | ||
| 87 | |||
| 88 | #ifndef pr_fmt | ||
| 89 | #define pr_fmt(fmt) fmt | ||
| 90 | #endif | ||
| 91 | |||
| 92 | #define pr_err(fmt, ...) \ | ||
| 93 | do { fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__); } while (0) | ||
| 94 | #define pr_warning(fmt, ...) \ | ||
| 95 | do { fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__); } while (0) | ||
| 96 | #define pr_info(fmt, ...) \ | ||
| 97 | do { fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__); } while (0) | ||
| 98 | #define pr_debug(fmt, ...) \ | ||
| 99 | eprintf(1, pr_fmt(fmt), ##__VA_ARGS__) | ||
| 100 | #define pr_debugN(n, fmt, ...) \ | ||
| 101 | eprintf(n, pr_fmt(fmt), ##__VA_ARGS__) | ||
| 102 | #define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__) | ||
| 103 | #define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__) | ||
| 104 | |||
| 29 | #endif | 105 | #endif |
diff --git a/tools/perf/util/include/linux/string.h b/tools/perf/util/include/linux/string.h new file mode 100644 index 000000000000..3b2f5900276f --- /dev/null +++ b/tools/perf/util/include/linux/string.h | |||
| @@ -0,0 +1 @@ | |||
| #include <string.h> | |||
diff --git a/tools/perf/util/include/linux/types.h b/tools/perf/util/include/linux/types.h new file mode 100644 index 000000000000..196862a81a21 --- /dev/null +++ b/tools/perf/util/include/linux/types.h | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #ifndef _PERF_LINUX_TYPES_H_ | ||
| 2 | #define _PERF_LINUX_TYPES_H_ | ||
| 3 | |||
| 4 | #include <asm/types.h> | ||
| 5 | |||
| 6 | #define DECLARE_BITMAP(name,bits) \ | ||
| 7 | unsigned long name[BITS_TO_LONGS(bits)] | ||
| 8 | |||
| 9 | #endif | ||
diff --git a/tools/perf/util/levenshtein.h b/tools/perf/util/levenshtein.h index 0173abeef52c..b0fcb6d8a881 100644 --- a/tools/perf/util/levenshtein.h +++ b/tools/perf/util/levenshtein.h | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | #ifndef LEVENSHTEIN_H | 1 | #ifndef __PERF_LEVENSHTEIN_H |
| 2 | #define LEVENSHTEIN_H | 2 | #define __PERF_LEVENSHTEIN_H |
| 3 | 3 | ||
| 4 | int levenshtein(const char *string1, const char *string2, | 4 | int levenshtein(const char *string1, const char *string2, |
| 5 | int swap_penalty, int substition_penalty, | 5 | int swap_penalty, int substition_penalty, |
| 6 | int insertion_penalty, int deletion_penalty); | 6 | int insertion_penalty, int deletion_penalty); |
| 7 | 7 | ||
| 8 | #endif | 8 | #endif /* __PERF_LEVENSHTEIN_H */ |
diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 804e02382739..69f94fe9db20 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
| 4 | #include <string.h> | 4 | #include <string.h> |
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | #include "debug.h" | ||
| 6 | 7 | ||
| 7 | static inline int is_anon_memory(const char *filename) | 8 | static inline int is_anon_memory(const char *filename) |
| 8 | { | 9 | { |
| @@ -19,13 +20,28 @@ static int strcommon(const char *pathname, char *cwd, int cwdlen) | |||
| 19 | return n; | 20 | return n; |
| 20 | } | 21 | } |
| 21 | 22 | ||
| 22 | struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen) | 23 | void map__init(struct map *self, enum map_type type, |
| 24 | u64 start, u64 end, u64 pgoff, struct dso *dso) | ||
| 25 | { | ||
| 26 | self->type = type; | ||
| 27 | self->start = start; | ||
| 28 | self->end = end; | ||
| 29 | self->pgoff = pgoff; | ||
| 30 | self->dso = dso; | ||
| 31 | self->map_ip = map__map_ip; | ||
| 32 | self->unmap_ip = map__unmap_ip; | ||
| 33 | RB_CLEAR_NODE(&self->rb_node); | ||
| 34 | } | ||
| 35 | |||
| 36 | struct map *map__new(struct mmap_event *event, enum map_type type, | ||
| 37 | char *cwd, int cwdlen) | ||
| 23 | { | 38 | { |
| 24 | struct map *self = malloc(sizeof(*self)); | 39 | struct map *self = malloc(sizeof(*self)); |
| 25 | 40 | ||
| 26 | if (self != NULL) { | 41 | if (self != NULL) { |
| 27 | const char *filename = event->filename; | 42 | const char *filename = event->filename; |
| 28 | char newfilename[PATH_MAX]; | 43 | char newfilename[PATH_MAX]; |
| 44 | struct dso *dso; | ||
| 29 | int anon; | 45 | int anon; |
| 30 | 46 | ||
| 31 | if (cwd) { | 47 | if (cwd) { |
| @@ -45,18 +61,15 @@ static int strcommon(const char *pathname, char *cwd, int cwdlen) | |||
| 45 | filename = newfilename; | 61 | filename = newfilename; |
| 46 | } | 62 | } |
| 47 | 63 | ||
| 48 | self->start = event->start; | 64 | dso = dsos__findnew(filename); |
| 49 | self->end = event->start + event->len; | 65 | if (dso == NULL) |
| 50 | self->pgoff = event->pgoff; | ||
| 51 | |||
| 52 | self->dso = dsos__findnew(filename); | ||
| 53 | if (self->dso == NULL) | ||
| 54 | goto out_delete; | 66 | goto out_delete; |
| 55 | 67 | ||
| 68 | map__init(self, type, event->start, event->start + event->len, | ||
| 69 | event->pgoff, dso); | ||
| 70 | |||
| 56 | if (self->dso == vdso || anon) | 71 | if (self->dso == vdso || anon) |
| 57 | self->map_ip = vdso__map_ip; | 72 | self->map_ip = self->unmap_ip = identity__map_ip; |
| 58 | else | ||
| 59 | self->map_ip = map__map_ip; | ||
| 60 | } | 73 | } |
| 61 | return self; | 74 | return self; |
| 62 | out_delete: | 75 | out_delete: |
| @@ -64,6 +77,72 @@ out_delete: | |||
| 64 | return NULL; | 77 | return NULL; |
| 65 | } | 78 | } |
| 66 | 79 | ||
| 80 | void map__delete(struct map *self) | ||
| 81 | { | ||
| 82 | free(self); | ||
| 83 | } | ||
| 84 | |||
| 85 | void map__fixup_start(struct map *self) | ||
| 86 | { | ||
| 87 | struct rb_root *symbols = &self->dso->symbols[self->type]; | ||
| 88 | struct rb_node *nd = rb_first(symbols); | ||
| 89 | if (nd != NULL) { | ||
| 90 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | ||
| 91 | self->start = sym->start; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | void map__fixup_end(struct map *self) | ||
| 96 | { | ||
| 97 | struct rb_root *symbols = &self->dso->symbols[self->type]; | ||
| 98 | struct rb_node *nd = rb_last(symbols); | ||
| 99 | if (nd != NULL) { | ||
| 100 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | ||
| 101 | self->end = sym->end; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | #define DSO__DELETED "(deleted)" | ||
| 106 | |||
| 107 | struct symbol *map__find_symbol(struct map *self, u64 addr, | ||
| 108 | symbol_filter_t filter) | ||
| 109 | { | ||
| 110 | if (!dso__loaded(self->dso, self->type)) { | ||
| 111 | int nr = dso__load(self->dso, self, filter); | ||
| 112 | |||
| 113 | if (nr < 0) { | ||
| 114 | if (self->dso->has_build_id) { | ||
| 115 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | ||
| 116 | |||
| 117 | build_id__sprintf(self->dso->build_id, | ||
| 118 | sizeof(self->dso->build_id), | ||
| 119 | sbuild_id); | ||
| 120 | pr_warning("%s with build id %s not found", | ||
| 121 | self->dso->long_name, sbuild_id); | ||
| 122 | } else | ||
| 123 | pr_warning("Failed to open %s", | ||
| 124 | self->dso->long_name); | ||
| 125 | pr_warning(", continuing without symbols\n"); | ||
| 126 | return NULL; | ||
| 127 | } else if (nr == 0) { | ||
| 128 | const char *name = self->dso->long_name; | ||
| 129 | const size_t len = strlen(name); | ||
| 130 | const size_t real_len = len - sizeof(DSO__DELETED); | ||
| 131 | |||
| 132 | if (len > sizeof(DSO__DELETED) && | ||
| 133 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { | ||
| 134 | pr_warning("%.*s was updated, restart the long running apps that use it!\n", | ||
| 135 | (int)real_len, name); | ||
| 136 | } else { | ||
| 137 | pr_warning("no symbols found in %s, maybe install a debug package?\n", name); | ||
| 138 | } | ||
| 139 | return NULL; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | return self->dso->find_symbol(self->dso, self->type, addr); | ||
| 144 | } | ||
| 145 | |||
| 67 | struct map *map__clone(struct map *self) | 146 | struct map *map__clone(struct map *self) |
| 68 | { | 147 | { |
| 69 | struct map *map = malloc(sizeof(*self)); | 148 | struct map *map = malloc(sizeof(*self)); |
diff --git a/tools/perf/util/module.c b/tools/perf/util/module.c deleted file mode 100644 index 3d567fe59c79..000000000000 --- a/tools/perf/util/module.c +++ /dev/null | |||
| @@ -1,509 +0,0 @@ | |||
| 1 | #include "util.h" | ||
| 2 | #include "../perf.h" | ||
| 3 | #include "string.h" | ||
| 4 | #include "module.h" | ||
| 5 | |||
| 6 | #include <libelf.h> | ||
| 7 | #include <gelf.h> | ||
| 8 | #include <elf.h> | ||
| 9 | #include <dirent.h> | ||
| 10 | #include <sys/utsname.h> | ||
| 11 | |||
| 12 | static unsigned int crc32(const char *p, unsigned int len) | ||
| 13 | { | ||
| 14 | int i; | ||
| 15 | unsigned int crc = 0; | ||
| 16 | |||
| 17 | while (len--) { | ||
| 18 | crc ^= *p++; | ||
| 19 | for (i = 0; i < 8; i++) | ||
| 20 | crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0); | ||
| 21 | } | ||
| 22 | return crc; | ||
| 23 | } | ||
| 24 | |||
| 25 | /* module section methods */ | ||
| 26 | |||
| 27 | struct sec_dso *sec_dso__new_dso(const char *name) | ||
| 28 | { | ||
| 29 | struct sec_dso *self = malloc(sizeof(*self) + strlen(name) + 1); | ||
| 30 | |||
| 31 | if (self != NULL) { | ||
| 32 | strcpy(self->name, name); | ||
| 33 | self->secs = RB_ROOT; | ||
| 34 | self->find_section = sec_dso__find_section; | ||
| 35 | } | ||
| 36 | |||
| 37 | return self; | ||
| 38 | } | ||
| 39 | |||
| 40 | static void sec_dso__delete_section(struct section *self) | ||
| 41 | { | ||
| 42 | free(((void *)self)); | ||
| 43 | } | ||
| 44 | |||
| 45 | void sec_dso__delete_sections(struct sec_dso *self) | ||
| 46 | { | ||
| 47 | struct section *pos; | ||
| 48 | struct rb_node *next = rb_first(&self->secs); | ||
| 49 | |||
| 50 | while (next) { | ||
| 51 | pos = rb_entry(next, struct section, rb_node); | ||
| 52 | next = rb_next(&pos->rb_node); | ||
| 53 | rb_erase(&pos->rb_node, &self->secs); | ||
| 54 | sec_dso__delete_section(pos); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | void sec_dso__delete_self(struct sec_dso *self) | ||
| 59 | { | ||
| 60 | sec_dso__delete_sections(self); | ||
| 61 | free(self); | ||
| 62 | } | ||
| 63 | |||
| 64 | static void sec_dso__insert_section(struct sec_dso *self, struct section *sec) | ||
| 65 | { | ||
| 66 | struct rb_node **p = &self->secs.rb_node; | ||
| 67 | struct rb_node *parent = NULL; | ||
| 68 | const u64 hash = sec->hash; | ||
| 69 | struct section *s; | ||
| 70 | |||
| 71 | while (*p != NULL) { | ||
| 72 | parent = *p; | ||
| 73 | s = rb_entry(parent, struct section, rb_node); | ||
| 74 | if (hash < s->hash) | ||
| 75 | p = &(*p)->rb_left; | ||
| 76 | else | ||
| 77 | p = &(*p)->rb_right; | ||
| 78 | } | ||
| 79 | rb_link_node(&sec->rb_node, parent, p); | ||
| 80 | rb_insert_color(&sec->rb_node, &self->secs); | ||
| 81 | } | ||
| 82 | |||
| 83 | struct section *sec_dso__find_section(struct sec_dso *self, const char *name) | ||
| 84 | { | ||
| 85 | struct rb_node *n; | ||
| 86 | u64 hash; | ||
| 87 | int len; | ||
| 88 | |||
| 89 | if (self == NULL) | ||
| 90 | return NULL; | ||
| 91 | |||
| 92 | len = strlen(name); | ||
| 93 | hash = crc32(name, len); | ||
| 94 | |||
| 95 | n = self->secs.rb_node; | ||
| 96 | |||
| 97 | while (n) { | ||
| 98 | struct section *s = rb_entry(n, struct section, rb_node); | ||
| 99 | |||
| 100 | if (hash < s->hash) | ||
| 101 | n = n->rb_left; | ||
| 102 | else if (hash > s->hash) | ||
| 103 | n = n->rb_right; | ||
| 104 | else { | ||
| 105 | if (!strcmp(name, s->name)) | ||
| 106 | return s; | ||
| 107 | else | ||
| 108 | n = rb_next(&s->rb_node); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | return NULL; | ||
| 113 | } | ||
| 114 | |||
| 115 | static size_t sec_dso__fprintf_section(struct section *self, FILE *fp) | ||
| 116 | { | ||
| 117 | return fprintf(fp, "name:%s vma:%llx path:%s\n", | ||
| 118 | self->name, self->vma, self->path); | ||
| 119 | } | ||
| 120 | |||
| 121 | size_t sec_dso__fprintf(struct sec_dso *self, FILE *fp) | ||
| 122 | { | ||
| 123 | size_t ret = fprintf(fp, "dso: %s\n", self->name); | ||
| 124 | |||
| 125 | struct rb_node *nd; | ||
| 126 | for (nd = rb_first(&self->secs); nd; nd = rb_next(nd)) { | ||
| 127 | struct section *pos = rb_entry(nd, struct section, rb_node); | ||
| 128 | ret += sec_dso__fprintf_section(pos, fp); | ||
| 129 | } | ||
| 130 | |||
| 131 | return ret; | ||
| 132 | } | ||
| 133 | |||
| 134 | static struct section *section__new(const char *name, const char *path) | ||
| 135 | { | ||
| 136 | struct section *self = calloc(1, sizeof(*self)); | ||
| 137 | |||
| 138 | if (!self) | ||
| 139 | goto out_failure; | ||
| 140 | |||
| 141 | self->name = calloc(1, strlen(name) + 1); | ||
| 142 | if (!self->name) | ||
| 143 | goto out_failure; | ||
| 144 | |||
| 145 | self->path = calloc(1, strlen(path) + 1); | ||
| 146 | if (!self->path) | ||
| 147 | goto out_failure; | ||
| 148 | |||
| 149 | strcpy(self->name, name); | ||
| 150 | strcpy(self->path, path); | ||
| 151 | self->hash = crc32(self->name, strlen(name)); | ||
| 152 | |||
| 153 | return self; | ||
| 154 | |||
| 155 | out_failure: | ||
| 156 | if (self) { | ||
| 157 | if (self->name) | ||
| 158 | free(self->name); | ||
| 159 | if (self->path) | ||
| 160 | free(self->path); | ||
| 161 | free(self); | ||
| 162 | } | ||
| 163 | |||
| 164 | return NULL; | ||
| 165 | } | ||
| 166 | |||
| 167 | /* module methods */ | ||
| 168 | |||
| 169 | struct mod_dso *mod_dso__new_dso(const char *name) | ||
| 170 | { | ||
| 171 | struct mod_dso *self = malloc(sizeof(*self) + strlen(name) + 1); | ||
| 172 | |||
| 173 | if (self != NULL) { | ||
| 174 | strcpy(self->name, name); | ||
| 175 | self->mods = RB_ROOT; | ||
| 176 | self->find_module = mod_dso__find_module; | ||
| 177 | } | ||
| 178 | |||
| 179 | return self; | ||
| 180 | } | ||
| 181 | |||
| 182 | static void mod_dso__delete_module(struct module *self) | ||
| 183 | { | ||
| 184 | free(((void *)self)); | ||
| 185 | } | ||
| 186 | |||
| 187 | void mod_dso__delete_modules(struct mod_dso *self) | ||
| 188 | { | ||
| 189 | struct module *pos; | ||
| 190 | struct rb_node *next = rb_first(&self->mods); | ||
| 191 | |||
| 192 | while (next) { | ||
| 193 | pos = rb_entry(next, struct module, rb_node); | ||
| 194 | next = rb_next(&pos->rb_node); | ||
| 195 | rb_erase(&pos->rb_node, &self->mods); | ||
| 196 | mod_dso__delete_module(pos); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | void mod_dso__delete_self(struct mod_dso *self) | ||
| 201 | { | ||
| 202 | mod_dso__delete_modules(self); | ||
| 203 | free(self); | ||
| 204 | } | ||
| 205 | |||
| 206 | static void mod_dso__insert_module(struct mod_dso *self, struct module *mod) | ||
| 207 | { | ||
| 208 | struct rb_node **p = &self->mods.rb_node; | ||
| 209 | struct rb_node *parent = NULL; | ||
| 210 | const u64 hash = mod->hash; | ||
| 211 | struct module *m; | ||
| 212 | |||
| 213 | while (*p != NULL) { | ||
| 214 | parent = *p; | ||
| 215 | m = rb_entry(parent, struct module, rb_node); | ||
| 216 | if (hash < m->hash) | ||
| 217 | p = &(*p)->rb_left; | ||
| 218 | else | ||
| 219 | p = &(*p)->rb_right; | ||
| 220 | } | ||
| 221 | rb_link_node(&mod->rb_node, parent, p); | ||
| 222 | rb_insert_color(&mod->rb_node, &self->mods); | ||
| 223 | } | ||
| 224 | |||
| 225 | struct module *mod_dso__find_module(struct mod_dso *self, const char *name) | ||
| 226 | { | ||
| 227 | struct rb_node *n; | ||
| 228 | u64 hash; | ||
| 229 | int len; | ||
| 230 | |||
| 231 | if (self == NULL) | ||
| 232 | return NULL; | ||
| 233 | |||
| 234 | len = strlen(name); | ||
| 235 | hash = crc32(name, len); | ||
| 236 | |||
| 237 | n = self->mods.rb_node; | ||
| 238 | |||
| 239 | while (n) { | ||
| 240 | struct module *m = rb_entry(n, struct module, rb_node); | ||
| 241 | |||
| 242 | if (hash < m->hash) | ||
| 243 | n = n->rb_left; | ||
| 244 | else if (hash > m->hash) | ||
| 245 | n = n->rb_right; | ||
| 246 | else { | ||
| 247 | if (!strcmp(name, m->name)) | ||
| 248 | return m; | ||
| 249 | else | ||
| 250 | n = rb_next(&m->rb_node); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | return NULL; | ||
| 255 | } | ||
| 256 | |||
| 257 | static size_t mod_dso__fprintf_module(struct module *self, FILE *fp) | ||
| 258 | { | ||
| 259 | return fprintf(fp, "name:%s path:%s\n", self->name, self->path); | ||
| 260 | } | ||
| 261 | |||
| 262 | size_t mod_dso__fprintf(struct mod_dso *self, FILE *fp) | ||
| 263 | { | ||
| 264 | struct rb_node *nd; | ||
| 265 | size_t ret; | ||
| 266 | |||
| 267 | ret = fprintf(fp, "dso: %s\n", self->name); | ||
| 268 | |||
| 269 | for (nd = rb_first(&self->mods); nd; nd = rb_next(nd)) { | ||
| 270 | struct module *pos = rb_entry(nd, struct module, rb_node); | ||
| 271 | |||
| 272 | ret += mod_dso__fprintf_module(pos, fp); | ||
| 273 | } | ||
| 274 | |||
| 275 | return ret; | ||
| 276 | } | ||
| 277 | |||
| 278 | static struct module *module__new(const char *name, const char *path) | ||
| 279 | { | ||
| 280 | struct module *self = calloc(1, sizeof(*self)); | ||
| 281 | |||
| 282 | if (!self) | ||
| 283 | goto out_failure; | ||
| 284 | |||
| 285 | self->name = calloc(1, strlen(name) + 1); | ||
| 286 | if (!self->name) | ||
| 287 | goto out_failure; | ||
| 288 | |||
| 289 | self->path = calloc(1, strlen(path) + 1); | ||
| 290 | if (!self->path) | ||
| 291 | goto out_failure; | ||
| 292 | |||
| 293 | strcpy(self->name, name); | ||
| 294 | strcpy(self->path, path); | ||
| 295 | self->hash = crc32(self->name, strlen(name)); | ||
| 296 | |||
| 297 | return self; | ||
| 298 | |||
| 299 | out_failure: | ||
| 300 | if (self) { | ||
| 301 | if (self->name) | ||
| 302 | free(self->name); | ||
| 303 | if (self->path) | ||
| 304 | free(self->path); | ||
| 305 | free(self); | ||
| 306 | } | ||
| 307 | |||
| 308 | return NULL; | ||
| 309 | } | ||
| 310 | |||
| 311 | static int mod_dso__load_sections(struct module *mod) | ||
| 312 | { | ||
| 313 | int count = 0, path_len; | ||
| 314 | struct dirent *entry; | ||
| 315 | char *line = NULL; | ||
| 316 | char *dir_path; | ||
| 317 | DIR *dir; | ||
| 318 | size_t n; | ||
| 319 | |||
| 320 | path_len = strlen("/sys/module/"); | ||
| 321 | path_len += strlen(mod->name); | ||
| 322 | path_len += strlen("/sections/"); | ||
| 323 | |||
| 324 | dir_path = calloc(1, path_len + 1); | ||
| 325 | if (dir_path == NULL) | ||
| 326 | goto out_failure; | ||
| 327 | |||
| 328 | strcat(dir_path, "/sys/module/"); | ||
| 329 | strcat(dir_path, mod->name); | ||
| 330 | strcat(dir_path, "/sections/"); | ||
| 331 | |||
| 332 | dir = opendir(dir_path); | ||
| 333 | if (dir == NULL) | ||
| 334 | goto out_free; | ||
| 335 | |||
| 336 | while ((entry = readdir(dir))) { | ||
| 337 | struct section *section; | ||
| 338 | char *path, *vma; | ||
| 339 | int line_len; | ||
| 340 | FILE *file; | ||
| 341 | |||
| 342 | if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name)) | ||
| 343 | continue; | ||
| 344 | |||
| 345 | path = calloc(1, path_len + strlen(entry->d_name) + 1); | ||
| 346 | if (path == NULL) | ||
| 347 | break; | ||
| 348 | strcat(path, dir_path); | ||
| 349 | strcat(path, entry->d_name); | ||
| 350 | |||
| 351 | file = fopen(path, "r"); | ||
| 352 | if (file == NULL) { | ||
| 353 | free(path); | ||
| 354 | break; | ||
| 355 | } | ||
| 356 | |||
| 357 | line_len = getline(&line, &n, file); | ||
| 358 | if (line_len < 0) { | ||
| 359 | free(path); | ||
| 360 | fclose(file); | ||
| 361 | break; | ||
| 362 | } | ||
| 363 | |||
| 364 | if (!line) { | ||
| 365 | free(path); | ||
| 366 | fclose(file); | ||
| 367 | break; | ||
| 368 | } | ||
| 369 | |||
| 370 | line[--line_len] = '\0'; /* \n */ | ||
| 371 | |||
| 372 | vma = strstr(line, "0x"); | ||
| 373 | if (!vma) { | ||
| 374 | free(path); | ||
| 375 | fclose(file); | ||
| 376 | break; | ||
| 377 | } | ||
| 378 | vma += 2; | ||
| 379 | |||
| 380 | section = section__new(entry->d_name, path); | ||
| 381 | if (!section) { | ||
| 382 | fprintf(stderr, "load_sections: allocation error\n"); | ||
| 383 | free(path); | ||
| 384 | fclose(file); | ||
| 385 | break; | ||
| 386 | } | ||
| 387 | |||
| 388 | hex2u64(vma, §ion->vma); | ||
| 389 | sec_dso__insert_section(mod->sections, section); | ||
| 390 | |||
| 391 | free(path); | ||
| 392 | fclose(file); | ||
| 393 | count++; | ||
| 394 | } | ||
| 395 | |||
| 396 | closedir(dir); | ||
| 397 | free(line); | ||
| 398 | free(dir_path); | ||
| 399 | |||
| 400 | return count; | ||
| 401 | |||
| 402 | out_free: | ||
| 403 | free(dir_path); | ||
| 404 | |||
| 405 | out_failure: | ||
| 406 | return count; | ||
| 407 | } | ||
| 408 | |||
| 409 | static int mod_dso__load_module_paths(struct mod_dso *self) | ||
| 410 | { | ||
| 411 | struct utsname uts; | ||
| 412 | int count = 0, len; | ||
| 413 | char *line = NULL; | ||
| 414 | FILE *file; | ||
| 415 | char *path; | ||
| 416 | size_t n; | ||
| 417 | |||
| 418 | if (uname(&uts) < 0) | ||
| 419 | goto out_failure; | ||
| 420 | |||
| 421 | len = strlen("/lib/modules/"); | ||
| 422 | len += strlen(uts.release); | ||
| 423 | len += strlen("/modules.dep"); | ||
| 424 | |||
| 425 | path = calloc(1, len); | ||
| 426 | if (path == NULL) | ||
| 427 | goto out_failure; | ||
| 428 | |||
| 429 | strcat(path, "/lib/modules/"); | ||
| 430 | strcat(path, uts.release); | ||
| 431 | strcat(path, "/modules.dep"); | ||
| 432 | |||
| 433 | file = fopen(path, "r"); | ||
| 434 | free(path); | ||
| 435 | if (file == NULL) | ||
| 436 | goto out_failure; | ||
| 437 | |||
| 438 | while (!feof(file)) { | ||
| 439 | char *name, *tmp; | ||
| 440 | struct module *module; | ||
| 441 | int line_len; | ||
| 442 | |||
| 443 | line_len = getline(&line, &n, file); | ||
| 444 | if (line_len < 0) | ||
| 445 | break; | ||
| 446 | |||
| 447 | if (!line) | ||
| 448 | goto out_failure; | ||
| 449 | |||
| 450 | line[--line_len] = '\0'; /* \n */ | ||
| 451 | |||
| 452 | path = strtok(line, ":"); | ||
| 453 | if (!path) | ||
| 454 | goto out_failure; | ||
| 455 | |||
| 456 | name = strdup(path); | ||
| 457 | name = strtok(name, "/"); | ||
| 458 | |||
| 459 | tmp = name; | ||
| 460 | |||
| 461 | while (tmp) { | ||
| 462 | tmp = strtok(NULL, "/"); | ||
| 463 | if (tmp) | ||
| 464 | name = tmp; | ||
| 465 | } | ||
| 466 | name = strsep(&name, "."); | ||
| 467 | |||
| 468 | /* Quirk: replace '-' with '_' in sound modules */ | ||
| 469 | for (len = strlen(name); len; len--) { | ||
| 470 | if (*(name+len) == '-') | ||
| 471 | *(name+len) = '_'; | ||
| 472 | } | ||
| 473 | |||
| 474 | module = module__new(name, path); | ||
| 475 | if (!module) { | ||
| 476 | fprintf(stderr, "load_module_paths: allocation error\n"); | ||
| 477 | goto out_failure; | ||
| 478 | } | ||
| 479 | mod_dso__insert_module(self, module); | ||
| 480 | |||
| 481 | module->sections = sec_dso__new_dso("sections"); | ||
| 482 | if (!module->sections) { | ||
| 483 | fprintf(stderr, "load_module_paths: allocation error\n"); | ||
| 484 | goto out_failure; | ||
| 485 | } | ||
| 486 | |||
| 487 | module->active = mod_dso__load_sections(module); | ||
| 488 | |||
| 489 | if (module->active > 0) | ||
| 490 | count++; | ||
| 491 | } | ||
| 492 | |||
| 493 | free(line); | ||
| 494 | fclose(file); | ||
| 495 | |||
| 496 | return count; | ||
| 497 | |||
| 498 | out_failure: | ||
| 499 | return -1; | ||
| 500 | } | ||
| 501 | |||
| 502 | int mod_dso__load_modules(struct mod_dso *dso) | ||
| 503 | { | ||
| 504 | int err; | ||
| 505 | |||
| 506 | err = mod_dso__load_module_paths(dso); | ||
| 507 | |||
| 508 | return err; | ||
| 509 | } | ||
diff --git a/tools/perf/util/module.h b/tools/perf/util/module.h deleted file mode 100644 index 8a592ef641ca..000000000000 --- a/tools/perf/util/module.h +++ /dev/null | |||
| @@ -1,53 +0,0 @@ | |||
| 1 | #ifndef _PERF_MODULE_ | ||
| 2 | #define _PERF_MODULE_ 1 | ||
| 3 | |||
| 4 | #include <linux/types.h> | ||
| 5 | #include "../types.h" | ||
| 6 | #include <linux/list.h> | ||
| 7 | #include <linux/rbtree.h> | ||
| 8 | |||
| 9 | struct section { | ||
| 10 | struct rb_node rb_node; | ||
| 11 | u64 hash; | ||
| 12 | u64 vma; | ||
| 13 | char *name; | ||
| 14 | char *path; | ||
| 15 | }; | ||
| 16 | |||
| 17 | struct sec_dso { | ||
| 18 | struct list_head node; | ||
| 19 | struct rb_root secs; | ||
| 20 | struct section *(*find_section)(struct sec_dso *, const char *name); | ||
| 21 | char name[0]; | ||
| 22 | }; | ||
| 23 | |||
| 24 | struct module { | ||
| 25 | struct rb_node rb_node; | ||
| 26 | u64 hash; | ||
| 27 | char *name; | ||
| 28 | char *path; | ||
| 29 | struct sec_dso *sections; | ||
| 30 | int active; | ||
| 31 | }; | ||
| 32 | |||
| 33 | struct mod_dso { | ||
| 34 | struct list_head node; | ||
| 35 | struct rb_root mods; | ||
| 36 | struct module *(*find_module)(struct mod_dso *, const char *name); | ||
| 37 | char name[0]; | ||
| 38 | }; | ||
| 39 | |||
| 40 | struct sec_dso *sec_dso__new_dso(const char *name); | ||
| 41 | void sec_dso__delete_sections(struct sec_dso *self); | ||
| 42 | void sec_dso__delete_self(struct sec_dso *self); | ||
| 43 | size_t sec_dso__fprintf(struct sec_dso *self, FILE *fp); | ||
| 44 | struct section *sec_dso__find_section(struct sec_dso *self, const char *name); | ||
| 45 | |||
| 46 | struct mod_dso *mod_dso__new_dso(const char *name); | ||
| 47 | void mod_dso__delete_modules(struct mod_dso *self); | ||
| 48 | void mod_dso__delete_self(struct mod_dso *self); | ||
| 49 | size_t mod_dso__fprintf(struct mod_dso *self, FILE *fp); | ||
| 50 | struct module *mod_dso__find_module(struct mod_dso *self, const char *name); | ||
| 51 | int mod_dso__load_modules(struct mod_dso *dso); | ||
| 52 | |||
| 53 | #endif /* _PERF_MODULE_ */ | ||
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index a587d41ae3c9..9e5dbd66d34d 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | 1 | #include "../../../include/linux/hw_breakpoint.h" | |
| 2 | #include "util.h" | 2 | #include "util.h" |
| 3 | #include "../perf.h" | 3 | #include "../perf.h" |
| 4 | #include "parse-options.h" | 4 | #include "parse-options.h" |
| @@ -6,10 +6,13 @@ | |||
| 6 | #include "exec_cmd.h" | 6 | #include "exec_cmd.h" |
| 7 | #include "string.h" | 7 | #include "string.h" |
| 8 | #include "cache.h" | 8 | #include "cache.h" |
| 9 | #include "header.h" | ||
| 10 | #include "debugfs.h" | ||
| 9 | 11 | ||
| 10 | int nr_counters; | 12 | int nr_counters; |
| 11 | 13 | ||
| 12 | struct perf_counter_attr attrs[MAX_COUNTERS]; | 14 | struct perf_event_attr attrs[MAX_COUNTERS]; |
| 15 | char *filters[MAX_COUNTERS]; | ||
| 13 | 16 | ||
| 14 | struct event_symbol { | 17 | struct event_symbol { |
| 15 | u8 type; | 18 | u8 type; |
| @@ -18,6 +21,12 @@ struct event_symbol { | |||
| 18 | const char *alias; | 21 | const char *alias; |
| 19 | }; | 22 | }; |
| 20 | 23 | ||
| 24 | enum event_result { | ||
| 25 | EVT_FAILED, | ||
| 26 | EVT_HANDLED, | ||
| 27 | EVT_HANDLED_ALL | ||
| 28 | }; | ||
| 29 | |||
| 21 | char debugfs_path[MAXPATHLEN]; | 30 | char debugfs_path[MAXPATHLEN]; |
| 22 | 31 | ||
| 23 | #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x | 32 | #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x |
| @@ -39,15 +48,17 @@ static struct event_symbol event_symbols[] = { | |||
| 39 | { CSW(PAGE_FAULTS_MAJ), "major-faults", "" }, | 48 | { CSW(PAGE_FAULTS_MAJ), "major-faults", "" }, |
| 40 | { CSW(CONTEXT_SWITCHES), "context-switches", "cs" }, | 49 | { CSW(CONTEXT_SWITCHES), "context-switches", "cs" }, |
| 41 | { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" }, | 50 | { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" }, |
| 51 | { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" }, | ||
| 52 | { CSW(EMULATION_FAULTS), "emulation-faults", "" }, | ||
| 42 | }; | 53 | }; |
| 43 | 54 | ||
| 44 | #define __PERF_COUNTER_FIELD(config, name) \ | 55 | #define __PERF_EVENT_FIELD(config, name) \ |
| 45 | ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT) | 56 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) |
| 46 | 57 | ||
| 47 | #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW) | 58 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
| 48 | #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG) | 59 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) |
| 49 | #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE) | 60 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) |
| 50 | #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT) | 61 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) |
| 51 | 62 | ||
| 52 | static const char *hw_event_names[] = { | 63 | static const char *hw_event_names[] = { |
| 53 | "cycles", | 64 | "cycles", |
| @@ -67,6 +78,8 @@ static const char *sw_event_names[] = { | |||
| 67 | "CPU-migrations", | 78 | "CPU-migrations", |
| 68 | "minor-faults", | 79 | "minor-faults", |
| 69 | "major-faults", | 80 | "major-faults", |
| 81 | "alignment-faults", | ||
| 82 | "emulation-faults", | ||
| 70 | }; | 83 | }; |
| 71 | 84 | ||
| 72 | #define MAX_ALIASES 8 | 85 | #define MAX_ALIASES 8 |
| @@ -139,18 +152,8 @@ static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) | |||
| 139 | (strcmp(evt_dirent.d_name, "..")) && \ | 152 | (strcmp(evt_dirent.d_name, "..")) && \ |
| 140 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) | 153 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) |
| 141 | 154 | ||
| 142 | #define MAX_EVENT_LENGTH 30 | 155 | #define MAX_EVENT_LENGTH 512 |
| 143 | |||
| 144 | int valid_debugfs_mount(const char *debugfs) | ||
| 145 | { | ||
| 146 | struct statfs st_fs; | ||
| 147 | 156 | ||
| 148 | if (statfs(debugfs, &st_fs) < 0) | ||
| 149 | return -ENOENT; | ||
| 150 | else if (st_fs.f_type != (long) DEBUGFS_MAGIC) | ||
| 151 | return -ENOENT; | ||
| 152 | return 0; | ||
| 153 | } | ||
| 154 | 157 | ||
| 155 | struct tracepoint_path *tracepoint_id_to_path(u64 config) | 158 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
| 156 | { | 159 | { |
| @@ -158,33 +161,31 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config) | |||
| 158 | DIR *sys_dir, *evt_dir; | 161 | DIR *sys_dir, *evt_dir; |
| 159 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | 162 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
| 160 | char id_buf[4]; | 163 | char id_buf[4]; |
| 161 | int sys_dir_fd, fd; | 164 | int fd; |
| 162 | u64 id; | 165 | u64 id; |
| 163 | char evt_path[MAXPATHLEN]; | 166 | char evt_path[MAXPATHLEN]; |
| 167 | char dir_path[MAXPATHLEN]; | ||
| 164 | 168 | ||
| 165 | if (valid_debugfs_mount(debugfs_path)) | 169 | if (debugfs_valid_mountpoint(debugfs_path)) |
| 166 | return NULL; | 170 | return NULL; |
| 167 | 171 | ||
| 168 | sys_dir = opendir(debugfs_path); | 172 | sys_dir = opendir(debugfs_path); |
| 169 | if (!sys_dir) | 173 | if (!sys_dir) |
| 170 | goto cleanup; | 174 | return NULL; |
| 171 | sys_dir_fd = dirfd(sys_dir); | ||
| 172 | 175 | ||
| 173 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | 176 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
| 174 | int dfd = openat(sys_dir_fd, sys_dirent.d_name, | 177 | |
| 175 | O_RDONLY|O_DIRECTORY), evt_dir_fd; | 178 | snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path, |
| 176 | if (dfd == -1) | 179 | sys_dirent.d_name); |
| 177 | continue; | 180 | evt_dir = opendir(dir_path); |
| 178 | evt_dir = fdopendir(dfd); | 181 | if (!evt_dir) |
| 179 | if (!evt_dir) { | ||
| 180 | close(dfd); | ||
| 181 | continue; | 182 | continue; |
| 182 | } | 183 | |
| 183 | evt_dir_fd = dirfd(evt_dir); | ||
| 184 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { | 184 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
| 185 | snprintf(evt_path, MAXPATHLEN, "%s/id", | 185 | |
| 186 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, | ||
| 186 | evt_dirent.d_name); | 187 | evt_dirent.d_name); |
| 187 | fd = openat(evt_dir_fd, evt_path, O_RDONLY); | 188 | fd = open(evt_path, O_RDONLY); |
| 188 | if (fd < 0) | 189 | if (fd < 0) |
| 189 | continue; | 190 | continue; |
| 190 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | 191 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { |
| @@ -196,7 +197,7 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config) | |||
| 196 | if (id == config) { | 197 | if (id == config) { |
| 197 | closedir(evt_dir); | 198 | closedir(evt_dir); |
| 198 | closedir(sys_dir); | 199 | closedir(sys_dir); |
| 199 | path = calloc(1, sizeof(path)); | 200 | path = zalloc(sizeof(path)); |
| 200 | path->system = malloc(MAX_EVENT_LENGTH); | 201 | path->system = malloc(MAX_EVENT_LENGTH); |
| 201 | if (!path->system) { | 202 | if (!path->system) { |
| 202 | free(path); | 203 | free(path); |
| @@ -218,7 +219,6 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config) | |||
| 218 | closedir(evt_dir); | 219 | closedir(evt_dir); |
| 219 | } | 220 | } |
| 220 | 221 | ||
| 221 | cleanup: | ||
| 222 | closedir(sys_dir); | 222 | closedir(sys_dir); |
| 223 | return NULL; | 223 | return NULL; |
| 224 | } | 224 | } |
| @@ -344,8 +344,8 @@ static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int | |||
| 344 | return -1; | 344 | return -1; |
| 345 | } | 345 | } |
| 346 | 346 | ||
| 347 | static int | 347 | static enum event_result |
| 348 | parse_generic_hw_event(const char **str, struct perf_counter_attr *attr) | 348 | parse_generic_hw_event(const char **str, struct perf_event_attr *attr) |
| 349 | { | 349 | { |
| 350 | const char *s = *str; | 350 | const char *s = *str; |
| 351 | int cache_type = -1, cache_op = -1, cache_result = -1; | 351 | int cache_type = -1, cache_op = -1, cache_result = -1; |
| @@ -356,7 +356,7 @@ parse_generic_hw_event(const char **str, struct perf_counter_attr *attr) | |||
| 356 | * then bail out: | 356 | * then bail out: |
| 357 | */ | 357 | */ |
| 358 | if (cache_type == -1) | 358 | if (cache_type == -1) |
| 359 | return 0; | 359 | return EVT_FAILED; |
| 360 | 360 | ||
| 361 | while ((cache_op == -1 || cache_result == -1) && *s == '-') { | 361 | while ((cache_op == -1 || cache_result == -1) && *s == '-') { |
| 362 | ++s; | 362 | ++s; |
| @@ -402,27 +402,115 @@ parse_generic_hw_event(const char **str, struct perf_counter_attr *attr) | |||
| 402 | attr->type = PERF_TYPE_HW_CACHE; | 402 | attr->type = PERF_TYPE_HW_CACHE; |
| 403 | 403 | ||
| 404 | *str = s; | 404 | *str = s; |
| 405 | return 1; | 405 | return EVT_HANDLED; |
| 406 | } | 406 | } |
| 407 | 407 | ||
| 408 | static int parse_tracepoint_event(const char **strp, | 408 | static enum event_result |
| 409 | struct perf_counter_attr *attr) | 409 | parse_single_tracepoint_event(char *sys_name, |
| 410 | const char *evt_name, | ||
| 411 | unsigned int evt_length, | ||
| 412 | char *flags, | ||
| 413 | struct perf_event_attr *attr, | ||
| 414 | const char **strp) | ||
| 415 | { | ||
| 416 | char evt_path[MAXPATHLEN]; | ||
| 417 | char id_buf[4]; | ||
| 418 | u64 id; | ||
| 419 | int fd; | ||
| 420 | |||
| 421 | if (flags) { | ||
| 422 | if (!strncmp(flags, "record", strlen(flags))) { | ||
| 423 | attr->sample_type |= PERF_SAMPLE_RAW; | ||
| 424 | attr->sample_type |= PERF_SAMPLE_TIME; | ||
| 425 | attr->sample_type |= PERF_SAMPLE_CPU; | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path, | ||
| 430 | sys_name, evt_name); | ||
| 431 | |||
| 432 | fd = open(evt_path, O_RDONLY); | ||
| 433 | if (fd < 0) | ||
| 434 | return EVT_FAILED; | ||
| 435 | |||
| 436 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | ||
| 437 | close(fd); | ||
| 438 | return EVT_FAILED; | ||
| 439 | } | ||
| 440 | |||
| 441 | close(fd); | ||
| 442 | id = atoll(id_buf); | ||
| 443 | attr->config = id; | ||
| 444 | attr->type = PERF_TYPE_TRACEPOINT; | ||
| 445 | *strp = evt_name + evt_length; | ||
| 446 | |||
| 447 | return EVT_HANDLED; | ||
| 448 | } | ||
| 449 | |||
| 450 | /* sys + ':' + event + ':' + flags*/ | ||
| 451 | #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128) | ||
| 452 | static enum event_result | ||
| 453 | parse_subsystem_tracepoint_event(char *sys_name, char *flags) | ||
| 454 | { | ||
| 455 | char evt_path[MAXPATHLEN]; | ||
| 456 | struct dirent *evt_ent; | ||
| 457 | DIR *evt_dir; | ||
| 458 | |||
| 459 | snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name); | ||
| 460 | evt_dir = opendir(evt_path); | ||
| 461 | |||
| 462 | if (!evt_dir) { | ||
| 463 | perror("Can't open event dir"); | ||
| 464 | return EVT_FAILED; | ||
| 465 | } | ||
| 466 | |||
| 467 | while ((evt_ent = readdir(evt_dir))) { | ||
| 468 | char event_opt[MAX_EVOPT_LEN + 1]; | ||
| 469 | int len; | ||
| 470 | unsigned int rem = MAX_EVOPT_LEN; | ||
| 471 | |||
| 472 | if (!strcmp(evt_ent->d_name, ".") | ||
| 473 | || !strcmp(evt_ent->d_name, "..") | ||
| 474 | || !strcmp(evt_ent->d_name, "enable") | ||
| 475 | || !strcmp(evt_ent->d_name, "filter")) | ||
| 476 | continue; | ||
| 477 | |||
| 478 | len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name, | ||
| 479 | evt_ent->d_name); | ||
| 480 | if (len < 0) | ||
| 481 | return EVT_FAILED; | ||
| 482 | |||
| 483 | rem -= len; | ||
| 484 | if (flags) { | ||
| 485 | if (rem < strlen(flags) + 1) | ||
| 486 | return EVT_FAILED; | ||
| 487 | |||
| 488 | strcat(event_opt, ":"); | ||
| 489 | strcat(event_opt, flags); | ||
| 490 | } | ||
| 491 | |||
| 492 | if (parse_events(NULL, event_opt, 0)) | ||
| 493 | return EVT_FAILED; | ||
| 494 | } | ||
| 495 | |||
| 496 | return EVT_HANDLED_ALL; | ||
| 497 | } | ||
| 498 | |||
| 499 | |||
| 500 | static enum event_result parse_tracepoint_event(const char **strp, | ||
| 501 | struct perf_event_attr *attr) | ||
| 410 | { | 502 | { |
| 411 | const char *evt_name; | 503 | const char *evt_name; |
| 412 | char *flags; | 504 | char *flags; |
| 413 | char sys_name[MAX_EVENT_LENGTH]; | 505 | char sys_name[MAX_EVENT_LENGTH]; |
| 414 | char id_buf[4]; | ||
| 415 | int fd; | ||
| 416 | unsigned int sys_length, evt_length; | 506 | unsigned int sys_length, evt_length; |
| 417 | u64 id; | ||
| 418 | char evt_path[MAXPATHLEN]; | ||
| 419 | 507 | ||
| 420 | if (valid_debugfs_mount(debugfs_path)) | 508 | if (debugfs_valid_mountpoint(debugfs_path)) |
| 421 | return 0; | 509 | return 0; |
| 422 | 510 | ||
| 423 | evt_name = strchr(*strp, ':'); | 511 | evt_name = strchr(*strp, ':'); |
| 424 | if (!evt_name) | 512 | if (!evt_name) |
| 425 | return 0; | 513 | return EVT_FAILED; |
| 426 | 514 | ||
| 427 | sys_length = evt_name - *strp; | 515 | sys_length = evt_name - *strp; |
| 428 | if (sys_length >= MAX_EVENT_LENGTH) | 516 | if (sys_length >= MAX_EVENT_LENGTH) |
| @@ -434,32 +522,97 @@ static int parse_tracepoint_event(const char **strp, | |||
| 434 | 522 | ||
| 435 | flags = strchr(evt_name, ':'); | 523 | flags = strchr(evt_name, ':'); |
| 436 | if (flags) { | 524 | if (flags) { |
| 437 | *flags = '\0'; | 525 | /* split it out: */ |
| 526 | evt_name = strndup(evt_name, flags - evt_name); | ||
| 438 | flags++; | 527 | flags++; |
| 439 | if (!strncmp(flags, "record", strlen(flags))) | ||
| 440 | attr->sample_type |= PERF_SAMPLE_RAW; | ||
| 441 | } | 528 | } |
| 442 | 529 | ||
| 443 | evt_length = strlen(evt_name); | 530 | evt_length = strlen(evt_name); |
| 444 | if (evt_length >= MAX_EVENT_LENGTH) | 531 | if (evt_length >= MAX_EVENT_LENGTH) |
| 445 | return 0; | 532 | return EVT_FAILED; |
| 446 | 533 | ||
| 447 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path, | 534 | if (!strcmp(evt_name, "*")) { |
| 448 | sys_name, evt_name); | 535 | *strp = evt_name + evt_length; |
| 449 | fd = open(evt_path, O_RDONLY); | 536 | return parse_subsystem_tracepoint_event(sys_name, flags); |
| 450 | if (fd < 0) | 537 | } else |
| 451 | return 0; | 538 | return parse_single_tracepoint_event(sys_name, evt_name, |
| 539 | evt_length, flags, | ||
| 540 | attr, strp); | ||
| 541 | } | ||
| 452 | 542 | ||
| 453 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | 543 | static enum event_result |
| 454 | close(fd); | 544 | parse_breakpoint_type(const char *type, const char **strp, |
| 455 | return 0; | 545 | struct perf_event_attr *attr) |
| 546 | { | ||
| 547 | int i; | ||
| 548 | |||
| 549 | for (i = 0; i < 3; i++) { | ||
| 550 | if (!type[i]) | ||
| 551 | break; | ||
| 552 | |||
| 553 | switch (type[i]) { | ||
| 554 | case 'r': | ||
| 555 | attr->bp_type |= HW_BREAKPOINT_R; | ||
| 556 | break; | ||
| 557 | case 'w': | ||
| 558 | attr->bp_type |= HW_BREAKPOINT_W; | ||
| 559 | break; | ||
| 560 | case 'x': | ||
| 561 | attr->bp_type |= HW_BREAKPOINT_X; | ||
| 562 | break; | ||
| 563 | default: | ||
| 564 | return EVT_FAILED; | ||
| 565 | } | ||
| 456 | } | 566 | } |
| 457 | close(fd); | 567 | if (!attr->bp_type) /* Default */ |
| 458 | id = atoll(id_buf); | 568 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; |
| 459 | attr->config = id; | 569 | |
| 460 | attr->type = PERF_TYPE_TRACEPOINT; | 570 | *strp = type + i; |
| 461 | *strp = evt_name + evt_length; | 571 | |
| 462 | return 1; | 572 | return EVT_HANDLED; |
| 573 | } | ||
| 574 | |||
| 575 | static enum event_result | ||
| 576 | parse_breakpoint_event(const char **strp, struct perf_event_attr *attr) | ||
| 577 | { | ||
| 578 | const char *target; | ||
| 579 | const char *type; | ||
| 580 | char *endaddr; | ||
| 581 | u64 addr; | ||
| 582 | enum event_result err; | ||
| 583 | |||
| 584 | target = strchr(*strp, ':'); | ||
| 585 | if (!target) | ||
| 586 | return EVT_FAILED; | ||
| 587 | |||
| 588 | if (strncmp(*strp, "mem", target - *strp) != 0) | ||
| 589 | return EVT_FAILED; | ||
| 590 | |||
| 591 | target++; | ||
| 592 | |||
| 593 | addr = strtoull(target, &endaddr, 0); | ||
| 594 | if (target == endaddr) | ||
| 595 | return EVT_FAILED; | ||
| 596 | |||
| 597 | attr->bp_addr = addr; | ||
| 598 | *strp = endaddr; | ||
| 599 | |||
| 600 | type = strchr(target, ':'); | ||
| 601 | |||
| 602 | /* If no type is defined, just rw as default */ | ||
| 603 | if (!type) { | ||
| 604 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; | ||
| 605 | } else { | ||
| 606 | err = parse_breakpoint_type(++type, strp, attr); | ||
| 607 | if (err == EVT_FAILED) | ||
| 608 | return EVT_FAILED; | ||
| 609 | } | ||
| 610 | |||
| 611 | /* We should find a nice way to override the access type */ | ||
| 612 | attr->bp_len = HW_BREAKPOINT_LEN_4; | ||
| 613 | attr->type = PERF_TYPE_BREAKPOINT; | ||
| 614 | |||
| 615 | return EVT_HANDLED; | ||
| 463 | } | 616 | } |
| 464 | 617 | ||
| 465 | static int check_events(const char *str, unsigned int i) | 618 | static int check_events(const char *str, unsigned int i) |
| @@ -477,8 +630,8 @@ static int check_events(const char *str, unsigned int i) | |||
| 477 | return 0; | 630 | return 0; |
| 478 | } | 631 | } |
| 479 | 632 | ||
| 480 | static int | 633 | static enum event_result |
| 481 | parse_symbolic_event(const char **strp, struct perf_counter_attr *attr) | 634 | parse_symbolic_event(const char **strp, struct perf_event_attr *attr) |
| 482 | { | 635 | { |
| 483 | const char *str = *strp; | 636 | const char *str = *strp; |
| 484 | unsigned int i; | 637 | unsigned int i; |
| @@ -490,32 +643,33 @@ parse_symbolic_event(const char **strp, struct perf_counter_attr *attr) | |||
| 490 | attr->type = event_symbols[i].type; | 643 | attr->type = event_symbols[i].type; |
| 491 | attr->config = event_symbols[i].config; | 644 | attr->config = event_symbols[i].config; |
| 492 | *strp = str + n; | 645 | *strp = str + n; |
| 493 | return 1; | 646 | return EVT_HANDLED; |
| 494 | } | 647 | } |
| 495 | } | 648 | } |
| 496 | return 0; | 649 | return EVT_FAILED; |
| 497 | } | 650 | } |
| 498 | 651 | ||
| 499 | static int parse_raw_event(const char **strp, struct perf_counter_attr *attr) | 652 | static enum event_result |
| 653 | parse_raw_event(const char **strp, struct perf_event_attr *attr) | ||
| 500 | { | 654 | { |
| 501 | const char *str = *strp; | 655 | const char *str = *strp; |
| 502 | u64 config; | 656 | u64 config; |
| 503 | int n; | 657 | int n; |
| 504 | 658 | ||
| 505 | if (*str != 'r') | 659 | if (*str != 'r') |
| 506 | return 0; | 660 | return EVT_FAILED; |
| 507 | n = hex2u64(str + 1, &config); | 661 | n = hex2u64(str + 1, &config); |
| 508 | if (n > 0) { | 662 | if (n > 0) { |
| 509 | *strp = str + n + 1; | 663 | *strp = str + n + 1; |
| 510 | attr->type = PERF_TYPE_RAW; | 664 | attr->type = PERF_TYPE_RAW; |
| 511 | attr->config = config; | 665 | attr->config = config; |
| 512 | return 1; | 666 | return EVT_HANDLED; |
| 513 | } | 667 | } |
| 514 | return 0; | 668 | return EVT_FAILED; |
| 515 | } | 669 | } |
| 516 | 670 | ||
| 517 | static int | 671 | static enum event_result |
| 518 | parse_numeric_event(const char **strp, struct perf_counter_attr *attr) | 672 | parse_numeric_event(const char **strp, struct perf_event_attr *attr) |
| 519 | { | 673 | { |
| 520 | const char *str = *strp; | 674 | const char *str = *strp; |
| 521 | char *endp; | 675 | char *endp; |
| @@ -530,14 +684,14 @@ parse_numeric_event(const char **strp, struct perf_counter_attr *attr) | |||
| 530 | attr->type = type; | 684 | attr->type = type; |
| 531 | attr->config = config; | 685 | attr->config = config; |
| 532 | *strp = endp; | 686 | *strp = endp; |
| 533 | return 1; | 687 | return EVT_HANDLED; |
| 534 | } | 688 | } |
| 535 | } | 689 | } |
| 536 | return 0; | 690 | return EVT_FAILED; |
| 537 | } | 691 | } |
| 538 | 692 | ||
| 539 | static int | 693 | static enum event_result |
| 540 | parse_event_modifier(const char **strp, struct perf_counter_attr *attr) | 694 | parse_event_modifier(const char **strp, struct perf_event_attr *attr) |
| 541 | { | 695 | { |
| 542 | const char *str = *strp; | 696 | const char *str = *strp; |
| 543 | int eu = 1, ek = 1, eh = 1; | 697 | int eu = 1, ek = 1, eh = 1; |
| @@ -569,37 +723,92 @@ parse_event_modifier(const char **strp, struct perf_counter_attr *attr) | |||
| 569 | * Each event can have multiple symbolic names. | 723 | * Each event can have multiple symbolic names. |
| 570 | * Symbolic names are (almost) exactly matched. | 724 | * Symbolic names are (almost) exactly matched. |
| 571 | */ | 725 | */ |
| 572 | static int parse_event_symbols(const char **str, struct perf_counter_attr *attr) | 726 | static enum event_result |
| 727 | parse_event_symbols(const char **str, struct perf_event_attr *attr) | ||
| 573 | { | 728 | { |
| 574 | if (!(parse_tracepoint_event(str, attr) || | 729 | enum event_result ret; |
| 575 | parse_raw_event(str, attr) || | 730 | |
| 576 | parse_numeric_event(str, attr) || | 731 | ret = parse_tracepoint_event(str, attr); |
| 577 | parse_symbolic_event(str, attr) || | 732 | if (ret != EVT_FAILED) |
| 578 | parse_generic_hw_event(str, attr))) | 733 | goto modifier; |
| 579 | return 0; | 734 | |
| 735 | ret = parse_raw_event(str, attr); | ||
| 736 | if (ret != EVT_FAILED) | ||
| 737 | goto modifier; | ||
| 738 | |||
| 739 | ret = parse_numeric_event(str, attr); | ||
| 740 | if (ret != EVT_FAILED) | ||
| 741 | goto modifier; | ||
| 742 | |||
| 743 | ret = parse_symbolic_event(str, attr); | ||
| 744 | if (ret != EVT_FAILED) | ||
| 745 | goto modifier; | ||
| 746 | |||
| 747 | ret = parse_generic_hw_event(str, attr); | ||
| 748 | if (ret != EVT_FAILED) | ||
| 749 | goto modifier; | ||
| 580 | 750 | ||
| 751 | ret = parse_breakpoint_event(str, attr); | ||
| 752 | if (ret != EVT_FAILED) | ||
| 753 | goto modifier; | ||
| 754 | |||
| 755 | fprintf(stderr, "invalid or unsupported event: '%s'\n", *str); | ||
| 756 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); | ||
| 757 | return EVT_FAILED; | ||
| 758 | |||
| 759 | modifier: | ||
| 581 | parse_event_modifier(str, attr); | 760 | parse_event_modifier(str, attr); |
| 582 | 761 | ||
| 583 | return 1; | 762 | return ret; |
| 763 | } | ||
| 764 | |||
| 765 | static void store_event_type(const char *orgname) | ||
| 766 | { | ||
| 767 | char filename[PATH_MAX], *c; | ||
| 768 | FILE *file; | ||
| 769 | int id; | ||
| 770 | |||
| 771 | sprintf(filename, "%s/", debugfs_path); | ||
| 772 | strncat(filename, orgname, strlen(orgname)); | ||
| 773 | strcat(filename, "/id"); | ||
| 774 | |||
| 775 | c = strchr(filename, ':'); | ||
| 776 | if (c) | ||
| 777 | *c = '/'; | ||
| 778 | |||
| 779 | file = fopen(filename, "r"); | ||
| 780 | if (!file) | ||
| 781 | return; | ||
| 782 | if (fscanf(file, "%i", &id) < 1) | ||
| 783 | die("cannot store event ID"); | ||
| 784 | fclose(file); | ||
| 785 | perf_header__push_event(id, orgname); | ||
| 584 | } | 786 | } |
| 585 | 787 | ||
| 586 | int parse_events(const struct option *opt __used, const char *str, int unset __used) | 788 | int parse_events(const struct option *opt __used, const char *str, int unset __used) |
| 587 | { | 789 | { |
| 588 | struct perf_counter_attr attr; | 790 | struct perf_event_attr attr; |
| 791 | enum event_result ret; | ||
| 792 | |||
| 793 | if (strchr(str, ':')) | ||
| 794 | store_event_type(str); | ||
| 589 | 795 | ||
| 590 | for (;;) { | 796 | for (;;) { |
| 591 | if (nr_counters == MAX_COUNTERS) | 797 | if (nr_counters == MAX_COUNTERS) |
| 592 | return -1; | 798 | return -1; |
| 593 | 799 | ||
| 594 | memset(&attr, 0, sizeof(attr)); | 800 | memset(&attr, 0, sizeof(attr)); |
| 595 | if (!parse_event_symbols(&str, &attr)) | 801 | ret = parse_event_symbols(&str, &attr); |
| 802 | if (ret == EVT_FAILED) | ||
| 596 | return -1; | 803 | return -1; |
| 597 | 804 | ||
| 598 | if (!(*str == 0 || *str == ',' || isspace(*str))) | 805 | if (!(*str == 0 || *str == ',' || isspace(*str))) |
| 599 | return -1; | 806 | return -1; |
| 600 | 807 | ||
| 601 | attrs[nr_counters] = attr; | 808 | if (ret != EVT_HANDLED_ALL) { |
| 602 | nr_counters++; | 809 | attrs[nr_counters] = attr; |
| 810 | nr_counters++; | ||
| 811 | } | ||
| 603 | 812 | ||
| 604 | if (*str == 0) | 813 | if (*str == 0) |
| 605 | break; | 814 | break; |
| @@ -612,6 +821,28 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u | |||
| 612 | return 0; | 821 | return 0; |
| 613 | } | 822 | } |
| 614 | 823 | ||
| 824 | int parse_filter(const struct option *opt __used, const char *str, | ||
| 825 | int unset __used) | ||
| 826 | { | ||
| 827 | int i = nr_counters - 1; | ||
| 828 | int len = strlen(str); | ||
| 829 | |||
| 830 | if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) { | ||
| 831 | fprintf(stderr, | ||
| 832 | "-F option should follow a -e tracepoint option\n"); | ||
| 833 | return -1; | ||
| 834 | } | ||
| 835 | |||
| 836 | filters[i] = malloc(len + 1); | ||
| 837 | if (!filters[i]) { | ||
| 838 | fprintf(stderr, "not enough memory to hold filter string\n"); | ||
| 839 | return -1; | ||
| 840 | } | ||
| 841 | strcpy(filters[i], str); | ||
| 842 | |||
| 843 | return 0; | ||
| 844 | } | ||
| 845 | |||
| 615 | static const char * const event_type_descriptors[] = { | 846 | static const char * const event_type_descriptors[] = { |
| 616 | "", | 847 | "", |
| 617 | "Hardware event", | 848 | "Hardware event", |
| @@ -628,38 +859,32 @@ static void print_tracepoint_events(void) | |||
| 628 | { | 859 | { |
| 629 | DIR *sys_dir, *evt_dir; | 860 | DIR *sys_dir, *evt_dir; |
| 630 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | 861 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
| 631 | int sys_dir_fd; | ||
| 632 | char evt_path[MAXPATHLEN]; | 862 | char evt_path[MAXPATHLEN]; |
| 863 | char dir_path[MAXPATHLEN]; | ||
| 633 | 864 | ||
| 634 | if (valid_debugfs_mount(debugfs_path)) | 865 | if (debugfs_valid_mountpoint(debugfs_path)) |
| 635 | return; | 866 | return; |
| 636 | 867 | ||
| 637 | sys_dir = opendir(debugfs_path); | 868 | sys_dir = opendir(debugfs_path); |
| 638 | if (!sys_dir) | 869 | if (!sys_dir) |
| 639 | goto cleanup; | 870 | return; |
| 640 | sys_dir_fd = dirfd(sys_dir); | ||
| 641 | 871 | ||
| 642 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | 872 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
| 643 | int dfd = openat(sys_dir_fd, sys_dirent.d_name, | 873 | |
| 644 | O_RDONLY|O_DIRECTORY), evt_dir_fd; | 874 | snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path, |
| 645 | if (dfd == -1) | 875 | sys_dirent.d_name); |
| 646 | continue; | 876 | evt_dir = opendir(dir_path); |
| 647 | evt_dir = fdopendir(dfd); | 877 | if (!evt_dir) |
| 648 | if (!evt_dir) { | ||
| 649 | close(dfd); | ||
| 650 | continue; | 878 | continue; |
| 651 | } | 879 | |
| 652 | evt_dir_fd = dirfd(evt_dir); | ||
| 653 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { | 880 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
| 654 | snprintf(evt_path, MAXPATHLEN, "%s:%s", | 881 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
| 655 | sys_dirent.d_name, evt_dirent.d_name); | 882 | sys_dirent.d_name, evt_dirent.d_name); |
| 656 | fprintf(stderr, " %-42s [%s]\n", evt_path, | 883 | printf(" %-42s [%s]\n", evt_path, |
| 657 | event_type_descriptors[PERF_TYPE_TRACEPOINT+1]); | 884 | event_type_descriptors[PERF_TYPE_TRACEPOINT+1]); |
| 658 | } | 885 | } |
| 659 | closedir(evt_dir); | 886 | closedir(evt_dir); |
| 660 | } | 887 | } |
| 661 | |||
| 662 | cleanup: | ||
| 663 | closedir(sys_dir); | 888 | closedir(sys_dir); |
| 664 | } | 889 | } |
| 665 | 890 | ||
| @@ -672,8 +897,8 @@ void print_events(void) | |||
| 672 | unsigned int i, type, op, prev_type = -1; | 897 | unsigned int i, type, op, prev_type = -1; |
| 673 | char name[40]; | 898 | char name[40]; |
| 674 | 899 | ||
| 675 | fprintf(stderr, "\n"); | 900 | printf("\n"); |
| 676 | fprintf(stderr, "List of pre-defined events (to be used in -e):\n"); | 901 | printf("List of pre-defined events (to be used in -e):\n"); |
| 677 | 902 | ||
| 678 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) { | 903 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) { |
| 679 | type = syms->type + 1; | 904 | type = syms->type + 1; |
| @@ -681,19 +906,19 @@ void print_events(void) | |||
| 681 | type = 0; | 906 | type = 0; |
| 682 | 907 | ||
| 683 | if (type != prev_type) | 908 | if (type != prev_type) |
| 684 | fprintf(stderr, "\n"); | 909 | printf("\n"); |
| 685 | 910 | ||
| 686 | if (strlen(syms->alias)) | 911 | if (strlen(syms->alias)) |
| 687 | sprintf(name, "%s OR %s", syms->symbol, syms->alias); | 912 | sprintf(name, "%s OR %s", syms->symbol, syms->alias); |
| 688 | else | 913 | else |
| 689 | strcpy(name, syms->symbol); | 914 | strcpy(name, syms->symbol); |
| 690 | fprintf(stderr, " %-42s [%s]\n", name, | 915 | printf(" %-42s [%s]\n", name, |
| 691 | event_type_descriptors[type]); | 916 | event_type_descriptors[type]); |
| 692 | 917 | ||
| 693 | prev_type = type; | 918 | prev_type = type; |
| 694 | } | 919 | } |
| 695 | 920 | ||
| 696 | fprintf(stderr, "\n"); | 921 | printf("\n"); |
| 697 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { | 922 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 698 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { | 923 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 699 | /* skip invalid cache type */ | 924 | /* skip invalid cache type */ |
| @@ -701,17 +926,20 @@ void print_events(void) | |||
| 701 | continue; | 926 | continue; |
| 702 | 927 | ||
| 703 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { | 928 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
| 704 | fprintf(stderr, " %-42s [%s]\n", | 929 | printf(" %-42s [%s]\n", |
| 705 | event_cache_name(type, op, i), | 930 | event_cache_name(type, op, i), |
| 706 | event_type_descriptors[4]); | 931 | event_type_descriptors[4]); |
| 707 | } | 932 | } |
| 708 | } | 933 | } |
| 709 | } | 934 | } |
| 710 | 935 | ||
| 711 | fprintf(stderr, "\n"); | 936 | printf("\n"); |
| 712 | fprintf(stderr, " %-42s [raw hardware event descriptor]\n", | 937 | printf(" %-42s [raw hardware event descriptor]\n", |
| 713 | "rNNN"); | 938 | "rNNN"); |
| 714 | fprintf(stderr, "\n"); | 939 | printf("\n"); |
| 940 | |||
| 941 | printf(" %-42s [hardware breakpoint]\n", "mem:<addr>[:access]"); | ||
| 942 | printf("\n"); | ||
| 715 | 943 | ||
| 716 | print_tracepoint_events(); | 944 | print_tracepoint_events(); |
| 717 | 945 | ||
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h index 60704c15961f..b8c1f64bc935 100644 --- a/tools/perf/util/parse-events.h +++ b/tools/perf/util/parse-events.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef _PARSE_EVENTS_H | 1 | #ifndef __PERF_PARSE_EVENTS_H |
| 2 | #define _PARSE_EVENTS_H | 2 | #define __PERF_PARSE_EVENTS_H |
| 3 | /* | 3 | /* |
| 4 | * Parse symbolic events/counts passed in as options: | 4 | * Parse symbolic events/counts passed in as options: |
| 5 | */ | 5 | */ |
| @@ -16,12 +16,14 @@ extern struct tracepoint_path *tracepoint_id_to_path(u64 config); | |||
| 16 | 16 | ||
| 17 | extern int nr_counters; | 17 | extern int nr_counters; |
| 18 | 18 | ||
| 19 | extern struct perf_counter_attr attrs[MAX_COUNTERS]; | 19 | extern struct perf_event_attr attrs[MAX_COUNTERS]; |
| 20 | extern char *filters[MAX_COUNTERS]; | ||
| 20 | 21 | ||
| 21 | extern const char *event_name(int ctr); | 22 | extern const char *event_name(int ctr); |
| 22 | extern const char *__event_name(int type, u64 config); | 23 | extern const char *__event_name(int type, u64 config); |
| 23 | 24 | ||
| 24 | extern int parse_events(const struct option *opt, const char *str, int unset); | 25 | extern int parse_events(const struct option *opt, const char *str, int unset); |
| 26 | extern int parse_filter(const struct option *opt, const char *str, int unset); | ||
| 25 | 27 | ||
| 26 | #define EVENTS_HELP_MAX (128*1024) | 28 | #define EVENTS_HELP_MAX (128*1024) |
| 27 | 29 | ||
| @@ -31,4 +33,4 @@ extern char debugfs_path[]; | |||
| 31 | extern int valid_debugfs_mount(const char *debugfs); | 33 | extern int valid_debugfs_mount(const char *debugfs); |
| 32 | 34 | ||
| 33 | 35 | ||
| 34 | #endif /* _PARSE_EVENTS_H */ | 36 | #endif /* __PERF_PARSE_EVENTS_H */ |
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h index 8aa3464c7090..948805af43c2 100644 --- a/tools/perf/util/parse-options.h +++ b/tools/perf/util/parse-options.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef PARSE_OPTIONS_H | 1 | #ifndef __PERF_PARSE_OPTIONS_H |
| 2 | #define PARSE_OPTIONS_H | 2 | #define __PERF_PARSE_OPTIONS_H |
| 3 | 3 | ||
| 4 | enum parse_opt_type { | 4 | enum parse_opt_type { |
| 5 | /* special types */ | 5 | /* special types */ |
| @@ -104,6 +104,8 @@ struct option { | |||
| 104 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb } | 104 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb } |
| 105 | #define OPT_CALLBACK(s, l, v, a, h, f) \ | 105 | #define OPT_CALLBACK(s, l, v, a, h, f) \ |
| 106 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f) } | 106 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f) } |
| 107 | #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \ | ||
| 108 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG } | ||
| 107 | #define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \ | 109 | #define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \ |
| 108 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT } | 110 | { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT } |
| 109 | 111 | ||
| @@ -172,4 +174,4 @@ extern int parse_opt_verbosity_cb(const struct option *, const char *, int); | |||
| 172 | 174 | ||
| 173 | extern const char *parse_options_fix_filename(const char *prefix, const char *file); | 175 | extern const char *parse_options_fix_filename(const char *prefix, const char *file); |
| 174 | 176 | ||
| 175 | #endif | 177 | #endif /* __PERF_PARSE_OPTIONS_H */ |
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c new file mode 100644 index 000000000000..cd7fbda5e2a5 --- /dev/null +++ b/tools/perf/util/probe-event.c | |||
| @@ -0,0 +1,484 @@ | |||
| 1 | /* | ||
| 2 | * probe-event.c : perf-probe definition to kprobe_events format converter | ||
| 3 | * | ||
| 4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 19 | * | ||
| 20 | */ | ||
| 21 | |||
| 22 | #define _GNU_SOURCE | ||
| 23 | #include <sys/utsname.h> | ||
| 24 | #include <sys/types.h> | ||
| 25 | #include <sys/stat.h> | ||
| 26 | #include <fcntl.h> | ||
| 27 | #include <errno.h> | ||
| 28 | #include <stdio.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | #include <string.h> | ||
| 32 | #include <stdarg.h> | ||
| 33 | #include <limits.h> | ||
| 34 | |||
| 35 | #undef _GNU_SOURCE | ||
| 36 | #include "event.h" | ||
| 37 | #include "string.h" | ||
| 38 | #include "strlist.h" | ||
| 39 | #include "debug.h" | ||
| 40 | #include "parse-events.h" /* For debugfs_path */ | ||
| 41 | #include "probe-event.h" | ||
| 42 | |||
| 43 | #define MAX_CMDLEN 256 | ||
| 44 | #define MAX_PROBE_ARGS 128 | ||
| 45 | #define PERFPROBE_GROUP "probe" | ||
| 46 | |||
| 47 | #define semantic_error(msg ...) die("Semantic error :" msg) | ||
| 48 | |||
| 49 | /* If there is no space to write, returns -E2BIG. */ | ||
| 50 | static int e_snprintf(char *str, size_t size, const char *format, ...) | ||
| 51 | { | ||
| 52 | int ret; | ||
| 53 | va_list ap; | ||
| 54 | va_start(ap, format); | ||
| 55 | ret = vsnprintf(str, size, format, ap); | ||
| 56 | va_end(ap); | ||
| 57 | if (ret >= (int)size) | ||
| 58 | ret = -E2BIG; | ||
| 59 | return ret; | ||
| 60 | } | ||
| 61 | |||
| 62 | /* Parse probepoint definition. */ | ||
| 63 | static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp) | ||
| 64 | { | ||
| 65 | char *ptr, *tmp; | ||
| 66 | char c, nc = 0; | ||
| 67 | /* | ||
| 68 | * <Syntax> | ||
| 69 | * perf probe SRC:LN | ||
| 70 | * perf probe FUNC[+OFFS|%return][@SRC] | ||
| 71 | */ | ||
| 72 | |||
| 73 | ptr = strpbrk(arg, ":+@%"); | ||
| 74 | if (ptr) { | ||
| 75 | nc = *ptr; | ||
| 76 | *ptr++ = '\0'; | ||
| 77 | } | ||
| 78 | |||
| 79 | /* Check arg is function or file and copy it */ | ||
| 80 | if (strchr(arg, '.')) /* File */ | ||
| 81 | pp->file = strdup(arg); | ||
| 82 | else /* Function */ | ||
| 83 | pp->function = strdup(arg); | ||
| 84 | DIE_IF(pp->file == NULL && pp->function == NULL); | ||
| 85 | |||
| 86 | /* Parse other options */ | ||
| 87 | while (ptr) { | ||
| 88 | arg = ptr; | ||
| 89 | c = nc; | ||
| 90 | ptr = strpbrk(arg, ":+@%"); | ||
| 91 | if (ptr) { | ||
| 92 | nc = *ptr; | ||
| 93 | *ptr++ = '\0'; | ||
| 94 | } | ||
| 95 | switch (c) { | ||
| 96 | case ':': /* Line number */ | ||
| 97 | pp->line = strtoul(arg, &tmp, 0); | ||
| 98 | if (*tmp != '\0') | ||
| 99 | semantic_error("There is non-digit charactor" | ||
| 100 | " in line number."); | ||
| 101 | break; | ||
| 102 | case '+': /* Byte offset from a symbol */ | ||
| 103 | pp->offset = strtoul(arg, &tmp, 0); | ||
| 104 | if (*tmp != '\0') | ||
| 105 | semantic_error("There is non-digit charactor" | ||
| 106 | " in offset."); | ||
| 107 | break; | ||
| 108 | case '@': /* File name */ | ||
| 109 | if (pp->file) | ||
| 110 | semantic_error("SRC@SRC is not allowed."); | ||
| 111 | pp->file = strdup(arg); | ||
| 112 | DIE_IF(pp->file == NULL); | ||
| 113 | if (ptr) | ||
| 114 | semantic_error("@SRC must be the last " | ||
| 115 | "option."); | ||
| 116 | break; | ||
| 117 | case '%': /* Probe places */ | ||
| 118 | if (strcmp(arg, "return") == 0) { | ||
| 119 | pp->retprobe = 1; | ||
| 120 | } else /* Others not supported yet */ | ||
| 121 | semantic_error("%%%s is not supported.", arg); | ||
| 122 | break; | ||
| 123 | default: | ||
| 124 | DIE_IF("Program has a bug."); | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | /* Exclusion check */ | ||
| 130 | if (pp->line && pp->offset) | ||
| 131 | semantic_error("Offset can't be used with line number."); | ||
| 132 | |||
| 133 | if (!pp->line && pp->file && !pp->function) | ||
| 134 | semantic_error("File always requires line number."); | ||
| 135 | |||
| 136 | if (pp->offset && !pp->function) | ||
| 137 | semantic_error("Offset requires an entry function."); | ||
| 138 | |||
| 139 | if (pp->retprobe && !pp->function) | ||
| 140 | semantic_error("Return probe requires an entry function."); | ||
| 141 | |||
| 142 | if ((pp->offset || pp->line) && pp->retprobe) | ||
| 143 | semantic_error("Offset/Line can't be used with return probe."); | ||
| 144 | |||
| 145 | pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n", | ||
| 146 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe); | ||
| 147 | } | ||
| 148 | |||
| 149 | /* Parse perf-probe event definition */ | ||
| 150 | int parse_perf_probe_event(const char *str, struct probe_point *pp) | ||
| 151 | { | ||
| 152 | char **argv; | ||
| 153 | int argc, i, need_dwarf = 0; | ||
| 154 | |||
| 155 | argv = argv_split(str, &argc); | ||
| 156 | if (!argv) | ||
| 157 | die("argv_split failed."); | ||
| 158 | if (argc > MAX_PROBE_ARGS + 1) | ||
| 159 | semantic_error("Too many arguments"); | ||
| 160 | |||
| 161 | /* Parse probe point */ | ||
| 162 | parse_perf_probe_probepoint(argv[0], pp); | ||
| 163 | if (pp->file || pp->line) | ||
| 164 | need_dwarf = 1; | ||
| 165 | |||
| 166 | /* Copy arguments and ensure return probe has no C argument */ | ||
| 167 | pp->nr_args = argc - 1; | ||
| 168 | pp->args = zalloc(sizeof(char *) * pp->nr_args); | ||
| 169 | for (i = 0; i < pp->nr_args; i++) { | ||
| 170 | pp->args[i] = strdup(argv[i + 1]); | ||
| 171 | if (!pp->args[i]) | ||
| 172 | die("Failed to copy argument."); | ||
| 173 | if (is_c_varname(pp->args[i])) { | ||
| 174 | if (pp->retprobe) | ||
| 175 | semantic_error("You can't specify local" | ||
| 176 | " variable for kretprobe"); | ||
| 177 | need_dwarf = 1; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | argv_free(argv); | ||
| 182 | return need_dwarf; | ||
| 183 | } | ||
| 184 | |||
| 185 | /* Parse kprobe_events event into struct probe_point */ | ||
| 186 | void parse_trace_kprobe_event(const char *str, char **group, char **event, | ||
| 187 | struct probe_point *pp) | ||
| 188 | { | ||
| 189 | char pr; | ||
| 190 | char *p; | ||
| 191 | int ret, i, argc; | ||
| 192 | char **argv; | ||
| 193 | |||
| 194 | pr_debug("Parsing kprobe_events: %s\n", str); | ||
| 195 | argv = argv_split(str, &argc); | ||
| 196 | if (!argv) | ||
| 197 | die("argv_split failed."); | ||
| 198 | if (argc < 2) | ||
| 199 | semantic_error("Too less arguments."); | ||
| 200 | |||
| 201 | /* Scan event and group name. */ | ||
| 202 | ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", | ||
| 203 | &pr, (float *)(void *)group, (float *)(void *)event); | ||
| 204 | if (ret != 3) | ||
| 205 | semantic_error("Failed to parse event name: %s", argv[0]); | ||
| 206 | pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr); | ||
| 207 | |||
| 208 | if (!pp) | ||
| 209 | goto end; | ||
| 210 | |||
| 211 | pp->retprobe = (pr == 'r'); | ||
| 212 | |||
| 213 | /* Scan function name and offset */ | ||
| 214 | ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function, &pp->offset); | ||
| 215 | if (ret == 1) | ||
| 216 | pp->offset = 0; | ||
| 217 | |||
| 218 | /* kprobe_events doesn't have this information */ | ||
| 219 | pp->line = 0; | ||
| 220 | pp->file = NULL; | ||
| 221 | |||
| 222 | pp->nr_args = argc - 2; | ||
| 223 | pp->args = zalloc(sizeof(char *) * pp->nr_args); | ||
| 224 | for (i = 0; i < pp->nr_args; i++) { | ||
| 225 | p = strchr(argv[i + 2], '='); | ||
| 226 | if (p) /* We don't need which register is assigned. */ | ||
| 227 | *p = '\0'; | ||
| 228 | pp->args[i] = strdup(argv[i + 2]); | ||
| 229 | if (!pp->args[i]) | ||
| 230 | die("Failed to copy argument."); | ||
| 231 | } | ||
| 232 | |||
| 233 | end: | ||
| 234 | argv_free(argv); | ||
| 235 | } | ||
| 236 | |||
| 237 | int synthesize_perf_probe_event(struct probe_point *pp) | ||
| 238 | { | ||
| 239 | char *buf; | ||
| 240 | char offs[64] = "", line[64] = ""; | ||
| 241 | int i, len, ret; | ||
| 242 | |||
| 243 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); | ||
| 244 | if (!buf) | ||
| 245 | die("Failed to allocate memory by zalloc."); | ||
| 246 | if (pp->offset) { | ||
| 247 | ret = e_snprintf(offs, 64, "+%d", pp->offset); | ||
| 248 | if (ret <= 0) | ||
| 249 | goto error; | ||
| 250 | } | ||
| 251 | if (pp->line) { | ||
| 252 | ret = e_snprintf(line, 64, ":%d", pp->line); | ||
| 253 | if (ret <= 0) | ||
| 254 | goto error; | ||
| 255 | } | ||
| 256 | |||
| 257 | if (pp->function) | ||
| 258 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function, | ||
| 259 | offs, pp->retprobe ? "%return" : "", line); | ||
| 260 | else | ||
| 261 | ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->file, line); | ||
| 262 | if (ret <= 0) | ||
| 263 | goto error; | ||
| 264 | len = ret; | ||
| 265 | |||
| 266 | for (i = 0; i < pp->nr_args; i++) { | ||
| 267 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", | ||
| 268 | pp->args[i]); | ||
| 269 | if (ret <= 0) | ||
| 270 | goto error; | ||
| 271 | len += ret; | ||
| 272 | } | ||
| 273 | pp->found = 1; | ||
| 274 | |||
| 275 | return pp->found; | ||
| 276 | error: | ||
| 277 | free(pp->probes[0]); | ||
| 278 | |||
| 279 | return ret; | ||
| 280 | } | ||
| 281 | |||
| 282 | int synthesize_trace_kprobe_event(struct probe_point *pp) | ||
| 283 | { | ||
| 284 | char *buf; | ||
| 285 | int i, len, ret; | ||
| 286 | |||
| 287 | pp->probes[0] = buf = zalloc(MAX_CMDLEN); | ||
| 288 | if (!buf) | ||
| 289 | die("Failed to allocate memory by zalloc."); | ||
| 290 | ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset); | ||
| 291 | if (ret <= 0) | ||
| 292 | goto error; | ||
| 293 | len = ret; | ||
| 294 | |||
| 295 | for (i = 0; i < pp->nr_args; i++) { | ||
| 296 | ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", | ||
| 297 | pp->args[i]); | ||
| 298 | if (ret <= 0) | ||
| 299 | goto error; | ||
| 300 | len += ret; | ||
| 301 | } | ||
| 302 | pp->found = 1; | ||
| 303 | |||
| 304 | return pp->found; | ||
| 305 | error: | ||
| 306 | free(pp->probes[0]); | ||
| 307 | |||
| 308 | return ret; | ||
| 309 | } | ||
| 310 | |||
| 311 | static int open_kprobe_events(int flags, int mode) | ||
| 312 | { | ||
| 313 | char buf[PATH_MAX]; | ||
| 314 | int ret; | ||
| 315 | |||
| 316 | ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); | ||
| 317 | if (ret < 0) | ||
| 318 | die("Failed to make kprobe_events path."); | ||
| 319 | |||
| 320 | ret = open(buf, flags, mode); | ||
| 321 | if (ret < 0) { | ||
| 322 | if (errno == ENOENT) | ||
| 323 | die("kprobe_events file does not exist -" | ||
| 324 | " please rebuild with CONFIG_KPROBE_TRACER."); | ||
| 325 | else | ||
| 326 | die("Could not open kprobe_events file: %s", | ||
| 327 | strerror(errno)); | ||
| 328 | } | ||
| 329 | return ret; | ||
| 330 | } | ||
| 331 | |||
| 332 | /* Get raw string list of current kprobe_events */ | ||
| 333 | static struct strlist *get_trace_kprobe_event_rawlist(int fd) | ||
| 334 | { | ||
| 335 | int ret, idx; | ||
| 336 | FILE *fp; | ||
| 337 | char buf[MAX_CMDLEN]; | ||
| 338 | char *p; | ||
| 339 | struct strlist *sl; | ||
| 340 | |||
| 341 | sl = strlist__new(true, NULL); | ||
| 342 | |||
| 343 | fp = fdopen(dup(fd), "r"); | ||
| 344 | while (!feof(fp)) { | ||
| 345 | p = fgets(buf, MAX_CMDLEN, fp); | ||
| 346 | if (!p) | ||
| 347 | break; | ||
| 348 | |||
| 349 | idx = strlen(p) - 1; | ||
| 350 | if (p[idx] == '\n') | ||
| 351 | p[idx] = '\0'; | ||
| 352 | ret = strlist__add(sl, buf); | ||
| 353 | if (ret < 0) | ||
| 354 | die("strlist__add failed: %s", strerror(-ret)); | ||
| 355 | } | ||
| 356 | fclose(fp); | ||
| 357 | |||
| 358 | return sl; | ||
| 359 | } | ||
| 360 | |||
| 361 | /* Free and zero clear probe_point */ | ||
| 362 | static void clear_probe_point(struct probe_point *pp) | ||
| 363 | { | ||
| 364 | int i; | ||
| 365 | |||
| 366 | if (pp->function) | ||
| 367 | free(pp->function); | ||
| 368 | if (pp->file) | ||
| 369 | free(pp->file); | ||
| 370 | for (i = 0; i < pp->nr_args; i++) | ||
| 371 | free(pp->args[i]); | ||
| 372 | if (pp->args) | ||
| 373 | free(pp->args); | ||
| 374 | for (i = 0; i < pp->found; i++) | ||
| 375 | free(pp->probes[i]); | ||
| 376 | memset(pp, 0, sizeof(pp)); | ||
| 377 | } | ||
| 378 | |||
| 379 | /* List up current perf-probe events */ | ||
| 380 | void show_perf_probe_events(void) | ||
| 381 | { | ||
| 382 | unsigned int i; | ||
| 383 | int fd; | ||
| 384 | char *group, *event; | ||
| 385 | struct probe_point pp; | ||
| 386 | struct strlist *rawlist; | ||
| 387 | struct str_node *ent; | ||
| 388 | |||
| 389 | fd = open_kprobe_events(O_RDONLY, 0); | ||
| 390 | rawlist = get_trace_kprobe_event_rawlist(fd); | ||
| 391 | close(fd); | ||
| 392 | |||
| 393 | for (i = 0; i < strlist__nr_entries(rawlist); i++) { | ||
| 394 | ent = strlist__entry(rawlist, i); | ||
| 395 | parse_trace_kprobe_event(ent->s, &group, &event, &pp); | ||
| 396 | synthesize_perf_probe_event(&pp); | ||
| 397 | printf("[%s:%s]\t%s\n", group, event, pp.probes[0]); | ||
| 398 | free(group); | ||
| 399 | free(event); | ||
| 400 | clear_probe_point(&pp); | ||
| 401 | } | ||
| 402 | |||
| 403 | strlist__delete(rawlist); | ||
| 404 | } | ||
| 405 | |||
| 406 | /* Get current perf-probe event names */ | ||
| 407 | static struct strlist *get_perf_event_names(int fd) | ||
| 408 | { | ||
| 409 | unsigned int i; | ||
| 410 | char *group, *event; | ||
| 411 | struct strlist *sl, *rawlist; | ||
| 412 | struct str_node *ent; | ||
| 413 | |||
| 414 | rawlist = get_trace_kprobe_event_rawlist(fd); | ||
| 415 | |||
| 416 | sl = strlist__new(false, NULL); | ||
| 417 | for (i = 0; i < strlist__nr_entries(rawlist); i++) { | ||
| 418 | ent = strlist__entry(rawlist, i); | ||
| 419 | parse_trace_kprobe_event(ent->s, &group, &event, NULL); | ||
| 420 | strlist__add(sl, event); | ||
| 421 | free(group); | ||
| 422 | } | ||
| 423 | |||
| 424 | strlist__delete(rawlist); | ||
| 425 | |||
| 426 | return sl; | ||
| 427 | } | ||
| 428 | |||
| 429 | static int write_trace_kprobe_event(int fd, const char *buf) | ||
| 430 | { | ||
| 431 | int ret; | ||
| 432 | |||
| 433 | ret = write(fd, buf, strlen(buf)); | ||
| 434 | if (ret <= 0) | ||
| 435 | die("Failed to create event."); | ||
| 436 | else | ||
| 437 | printf("Added new event: %s\n", buf); | ||
| 438 | |||
| 439 | return ret; | ||
| 440 | } | ||
| 441 | |||
| 442 | static void get_new_event_name(char *buf, size_t len, const char *base, | ||
| 443 | struct strlist *namelist) | ||
| 444 | { | ||
| 445 | int i, ret; | ||
| 446 | for (i = 0; i < MAX_EVENT_INDEX; i++) { | ||
| 447 | ret = e_snprintf(buf, len, "%s_%d", base, i); | ||
| 448 | if (ret < 0) | ||
| 449 | die("snprintf() failed: %s", strerror(-ret)); | ||
| 450 | if (!strlist__has_entry(namelist, buf)) | ||
| 451 | break; | ||
| 452 | } | ||
| 453 | if (i == MAX_EVENT_INDEX) | ||
| 454 | die("Too many events are on the same function."); | ||
| 455 | } | ||
| 456 | |||
| 457 | void add_trace_kprobe_events(struct probe_point *probes, int nr_probes) | ||
| 458 | { | ||
| 459 | int i, j, fd; | ||
| 460 | struct probe_point *pp; | ||
| 461 | char buf[MAX_CMDLEN]; | ||
| 462 | char event[64]; | ||
| 463 | struct strlist *namelist; | ||
| 464 | |||
| 465 | fd = open_kprobe_events(O_RDWR, O_APPEND); | ||
| 466 | /* Get current event names */ | ||
| 467 | namelist = get_perf_event_names(fd); | ||
| 468 | |||
| 469 | for (j = 0; j < nr_probes; j++) { | ||
| 470 | pp = probes + j; | ||
| 471 | for (i = 0; i < pp->found; i++) { | ||
| 472 | /* Get an unused new event name */ | ||
| 473 | get_new_event_name(event, 64, pp->function, namelist); | ||
| 474 | snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n", | ||
| 475 | pp->retprobe ? 'r' : 'p', | ||
| 476 | PERFPROBE_GROUP, event, | ||
| 477 | pp->probes[i]); | ||
| 478 | write_trace_kprobe_event(fd, buf); | ||
| 479 | /* Add added event name to namelist */ | ||
| 480 | strlist__add(namelist, event); | ||
| 481 | } | ||
| 482 | } | ||
| 483 | close(fd); | ||
| 484 | } | ||
diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h new file mode 100644 index 000000000000..0c6fe56fe38a --- /dev/null +++ b/tools/perf/util/probe-event.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef _PROBE_EVENT_H | ||
| 2 | #define _PROBE_EVENT_H | ||
| 3 | |||
| 4 | #include "probe-finder.h" | ||
| 5 | #include "strlist.h" | ||
| 6 | |||
| 7 | extern int parse_perf_probe_event(const char *str, struct probe_point *pp); | ||
| 8 | extern int synthesize_perf_probe_event(struct probe_point *pp); | ||
| 9 | extern void parse_trace_kprobe_event(const char *str, char **group, | ||
| 10 | char **event, struct probe_point *pp); | ||
| 11 | extern int synthesize_trace_kprobe_event(struct probe_point *pp); | ||
| 12 | extern void add_trace_kprobe_events(struct probe_point *probes, int nr_probes); | ||
| 13 | extern void show_perf_probe_events(void); | ||
| 14 | |||
| 15 | /* Maximum index number of event-name postfix */ | ||
| 16 | #define MAX_EVENT_INDEX 1024 | ||
| 17 | |||
| 18 | #endif /*_PROBE_EVENT_H */ | ||
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c new file mode 100644 index 000000000000..293cdfc1b8ca --- /dev/null +++ b/tools/perf/util/probe-finder.c | |||
| @@ -0,0 +1,732 @@ | |||
| 1 | /* | ||
| 2 | * probe-finder.c : C expression to kprobe event converter | ||
| 3 | * | ||
| 4 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 19 | * | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <sys/utsname.h> | ||
| 23 | #include <sys/types.h> | ||
| 24 | #include <sys/stat.h> | ||
| 25 | #include <fcntl.h> | ||
| 26 | #include <errno.h> | ||
| 27 | #include <stdio.h> | ||
| 28 | #include <unistd.h> | ||
| 29 | #include <getopt.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | #include <string.h> | ||
| 32 | #include <stdarg.h> | ||
| 33 | #include <ctype.h> | ||
| 34 | |||
| 35 | #include "event.h" | ||
| 36 | #include "debug.h" | ||
| 37 | #include "util.h" | ||
| 38 | #include "probe-finder.h" | ||
| 39 | |||
| 40 | |||
| 41 | /* Dwarf_Die Linkage to parent Die */ | ||
| 42 | struct die_link { | ||
| 43 | struct die_link *parent; /* Parent die */ | ||
| 44 | Dwarf_Die die; /* Current die */ | ||
| 45 | }; | ||
| 46 | |||
| 47 | static Dwarf_Debug __dw_debug; | ||
| 48 | static Dwarf_Error __dw_error; | ||
| 49 | |||
| 50 | /* | ||
| 51 | * Generic dwarf analysis helpers | ||
| 52 | */ | ||
| 53 | |||
| 54 | #define X86_32_MAX_REGS 8 | ||
| 55 | const char *x86_32_regs_table[X86_32_MAX_REGS] = { | ||
| 56 | "%ax", | ||
| 57 | "%cx", | ||
| 58 | "%dx", | ||
| 59 | "%bx", | ||
| 60 | "$stack", /* Stack address instead of %sp */ | ||
| 61 | "%bp", | ||
| 62 | "%si", | ||
| 63 | "%di", | ||
| 64 | }; | ||
| 65 | |||
| 66 | #define X86_64_MAX_REGS 16 | ||
| 67 | const char *x86_64_regs_table[X86_64_MAX_REGS] = { | ||
| 68 | "%ax", | ||
| 69 | "%dx", | ||
| 70 | "%cx", | ||
| 71 | "%bx", | ||
| 72 | "%si", | ||
| 73 | "%di", | ||
| 74 | "%bp", | ||
| 75 | "%sp", | ||
| 76 | "%r8", | ||
| 77 | "%r9", | ||
| 78 | "%r10", | ||
| 79 | "%r11", | ||
| 80 | "%r12", | ||
| 81 | "%r13", | ||
| 82 | "%r14", | ||
| 83 | "%r15", | ||
| 84 | }; | ||
| 85 | |||
| 86 | /* TODO: switching by dwarf address size */ | ||
| 87 | #ifdef __x86_64__ | ||
| 88 | #define ARCH_MAX_REGS X86_64_MAX_REGS | ||
| 89 | #define arch_regs_table x86_64_regs_table | ||
| 90 | #else | ||
| 91 | #define ARCH_MAX_REGS X86_32_MAX_REGS | ||
| 92 | #define arch_regs_table x86_32_regs_table | ||
| 93 | #endif | ||
| 94 | |||
| 95 | /* Return architecture dependent register string (for kprobe-tracer) */ | ||
| 96 | static const char *get_arch_regstr(unsigned int n) | ||
| 97 | { | ||
| 98 | return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL; | ||
| 99 | } | ||
| 100 | |||
| 101 | /* | ||
| 102 | * Compare the tail of two strings. | ||
| 103 | * Return 0 if whole of either string is same as another's tail part. | ||
| 104 | */ | ||
| 105 | static int strtailcmp(const char *s1, const char *s2) | ||
| 106 | { | ||
| 107 | int i1 = strlen(s1); | ||
| 108 | int i2 = strlen(s2); | ||
| 109 | while (--i1 > 0 && --i2 > 0) { | ||
| 110 | if (s1[i1] != s2[i2]) | ||
| 111 | return s1[i1] - s2[i2]; | ||
| 112 | } | ||
| 113 | return 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | /* Find the fileno of the target file. */ | ||
| 117 | static Dwarf_Unsigned cu_find_fileno(Dwarf_Die cu_die, const char *fname) | ||
| 118 | { | ||
| 119 | Dwarf_Signed cnt, i; | ||
| 120 | Dwarf_Unsigned found = 0; | ||
| 121 | char **srcs; | ||
| 122 | int ret; | ||
| 123 | |||
| 124 | if (!fname) | ||
| 125 | return 0; | ||
| 126 | |||
| 127 | ret = dwarf_srcfiles(cu_die, &srcs, &cnt, &__dw_error); | ||
| 128 | if (ret == DW_DLV_OK) { | ||
| 129 | for (i = 0; i < cnt && !found; i++) { | ||
| 130 | if (strtailcmp(srcs[i], fname) == 0) | ||
| 131 | found = i + 1; | ||
| 132 | dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING); | ||
| 133 | } | ||
| 134 | for (; i < cnt; i++) | ||
| 135 | dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING); | ||
| 136 | dwarf_dealloc(__dw_debug, srcs, DW_DLA_LIST); | ||
| 137 | } | ||
| 138 | if (found) | ||
| 139 | pr_debug("found fno: %d\n", (int)found); | ||
| 140 | return found; | ||
| 141 | } | ||
| 142 | |||
| 143 | /* Compare diename and tname */ | ||
| 144 | static int die_compare_name(Dwarf_Die dw_die, const char *tname) | ||
| 145 | { | ||
| 146 | char *name; | ||
| 147 | int ret; | ||
| 148 | ret = dwarf_diename(dw_die, &name, &__dw_error); | ||
| 149 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 150 | if (ret == DW_DLV_OK) { | ||
| 151 | ret = strcmp(tname, name); | ||
| 152 | dwarf_dealloc(__dw_debug, name, DW_DLA_STRING); | ||
| 153 | } else | ||
| 154 | ret = -1; | ||
| 155 | return ret; | ||
| 156 | } | ||
| 157 | |||
| 158 | /* Check the address is in the subprogram(function). */ | ||
| 159 | static int die_within_subprogram(Dwarf_Die sp_die, Dwarf_Addr addr, | ||
| 160 | Dwarf_Signed *offs) | ||
| 161 | { | ||
| 162 | Dwarf_Addr lopc, hipc; | ||
| 163 | int ret; | ||
| 164 | |||
| 165 | /* TODO: check ranges */ | ||
| 166 | ret = dwarf_lowpc(sp_die, &lopc, &__dw_error); | ||
| 167 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 168 | if (ret == DW_DLV_NO_ENTRY) | ||
| 169 | return 0; | ||
| 170 | ret = dwarf_highpc(sp_die, &hipc, &__dw_error); | ||
| 171 | DIE_IF(ret != DW_DLV_OK); | ||
| 172 | if (lopc <= addr && addr < hipc) { | ||
| 173 | *offs = addr - lopc; | ||
| 174 | return 1; | ||
| 175 | } else | ||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | |||
| 179 | /* Check the die is inlined function */ | ||
| 180 | static Dwarf_Bool die_inlined_subprogram(Dwarf_Die dw_die) | ||
| 181 | { | ||
| 182 | /* TODO: check strictly */ | ||
| 183 | Dwarf_Bool inl; | ||
| 184 | int ret; | ||
| 185 | |||
| 186 | ret = dwarf_hasattr(dw_die, DW_AT_inline, &inl, &__dw_error); | ||
| 187 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 188 | return inl; | ||
| 189 | } | ||
| 190 | |||
| 191 | /* Get the offset of abstruct_origin */ | ||
| 192 | static Dwarf_Off die_get_abstract_origin(Dwarf_Die dw_die) | ||
| 193 | { | ||
| 194 | Dwarf_Attribute attr; | ||
| 195 | Dwarf_Off cu_offs; | ||
| 196 | int ret; | ||
| 197 | |||
| 198 | ret = dwarf_attr(dw_die, DW_AT_abstract_origin, &attr, &__dw_error); | ||
| 199 | DIE_IF(ret != DW_DLV_OK); | ||
| 200 | ret = dwarf_formref(attr, &cu_offs, &__dw_error); | ||
| 201 | DIE_IF(ret != DW_DLV_OK); | ||
| 202 | dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR); | ||
| 203 | return cu_offs; | ||
| 204 | } | ||
| 205 | |||
| 206 | /* Get entry pc(or low pc, 1st entry of ranges) of the die */ | ||
| 207 | static Dwarf_Addr die_get_entrypc(Dwarf_Die dw_die) | ||
| 208 | { | ||
| 209 | Dwarf_Attribute attr; | ||
| 210 | Dwarf_Addr addr; | ||
| 211 | Dwarf_Off offs; | ||
| 212 | Dwarf_Ranges *ranges; | ||
| 213 | Dwarf_Signed cnt; | ||
| 214 | int ret; | ||
| 215 | |||
| 216 | /* Try to get entry pc */ | ||
| 217 | ret = dwarf_attr(dw_die, DW_AT_entry_pc, &attr, &__dw_error); | ||
| 218 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 219 | if (ret == DW_DLV_OK) { | ||
| 220 | ret = dwarf_formaddr(attr, &addr, &__dw_error); | ||
| 221 | DIE_IF(ret != DW_DLV_OK); | ||
| 222 | dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR); | ||
| 223 | return addr; | ||
| 224 | } | ||
| 225 | |||
| 226 | /* Try to get low pc */ | ||
| 227 | ret = dwarf_lowpc(dw_die, &addr, &__dw_error); | ||
| 228 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 229 | if (ret == DW_DLV_OK) | ||
| 230 | return addr; | ||
| 231 | |||
| 232 | /* Try to get ranges */ | ||
| 233 | ret = dwarf_attr(dw_die, DW_AT_ranges, &attr, &__dw_error); | ||
| 234 | DIE_IF(ret != DW_DLV_OK); | ||
| 235 | ret = dwarf_formref(attr, &offs, &__dw_error); | ||
| 236 | DIE_IF(ret != DW_DLV_OK); | ||
| 237 | ret = dwarf_get_ranges(__dw_debug, offs, &ranges, &cnt, NULL, | ||
| 238 | &__dw_error); | ||
| 239 | DIE_IF(ret != DW_DLV_OK); | ||
| 240 | addr = ranges[0].dwr_addr1; | ||
| 241 | dwarf_ranges_dealloc(__dw_debug, ranges, cnt); | ||
| 242 | return addr; | ||
| 243 | } | ||
| 244 | |||
| 245 | /* | ||
| 246 | * Search a Die from Die tree. | ||
| 247 | * Note: cur_link->die should be deallocated in this function. | ||
| 248 | */ | ||
| 249 | static int __search_die_tree(struct die_link *cur_link, | ||
| 250 | int (*die_cb)(struct die_link *, void *), | ||
| 251 | void *data) | ||
| 252 | { | ||
| 253 | Dwarf_Die new_die; | ||
| 254 | struct die_link new_link; | ||
| 255 | int ret; | ||
| 256 | |||
| 257 | if (!die_cb) | ||
| 258 | return 0; | ||
| 259 | |||
| 260 | /* Check current die */ | ||
| 261 | while (!(ret = die_cb(cur_link, data))) { | ||
| 262 | /* Check child die */ | ||
| 263 | ret = dwarf_child(cur_link->die, &new_die, &__dw_error); | ||
| 264 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 265 | if (ret == DW_DLV_OK) { | ||
| 266 | new_link.parent = cur_link; | ||
| 267 | new_link.die = new_die; | ||
| 268 | ret = __search_die_tree(&new_link, die_cb, data); | ||
| 269 | if (ret) | ||
| 270 | break; | ||
| 271 | } | ||
| 272 | |||
| 273 | /* Move to next sibling */ | ||
| 274 | ret = dwarf_siblingof(__dw_debug, cur_link->die, &new_die, | ||
| 275 | &__dw_error); | ||
| 276 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 277 | dwarf_dealloc(__dw_debug, cur_link->die, DW_DLA_DIE); | ||
| 278 | cur_link->die = new_die; | ||
| 279 | if (ret == DW_DLV_NO_ENTRY) | ||
| 280 | return 0; | ||
| 281 | } | ||
| 282 | dwarf_dealloc(__dw_debug, cur_link->die, DW_DLA_DIE); | ||
| 283 | return ret; | ||
| 284 | } | ||
| 285 | |||
| 286 | /* Search a die in its children's die tree */ | ||
| 287 | static int search_die_from_children(Dwarf_Die parent_die, | ||
| 288 | int (*die_cb)(struct die_link *, void *), | ||
| 289 | void *data) | ||
| 290 | { | ||
| 291 | struct die_link new_link; | ||
| 292 | int ret; | ||
| 293 | |||
| 294 | new_link.parent = NULL; | ||
| 295 | ret = dwarf_child(parent_die, &new_link.die, &__dw_error); | ||
| 296 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 297 | if (ret == DW_DLV_OK) | ||
| 298 | return __search_die_tree(&new_link, die_cb, data); | ||
| 299 | else | ||
| 300 | return 0; | ||
| 301 | } | ||
| 302 | |||
| 303 | /* Find a locdesc corresponding to the address */ | ||
| 304 | static int attr_get_locdesc(Dwarf_Attribute attr, Dwarf_Locdesc *desc, | ||
| 305 | Dwarf_Addr addr) | ||
| 306 | { | ||
| 307 | Dwarf_Signed lcnt; | ||
| 308 | Dwarf_Locdesc **llbuf; | ||
| 309 | int ret, i; | ||
| 310 | |||
| 311 | ret = dwarf_loclist_n(attr, &llbuf, &lcnt, &__dw_error); | ||
| 312 | DIE_IF(ret != DW_DLV_OK); | ||
| 313 | ret = DW_DLV_NO_ENTRY; | ||
| 314 | for (i = 0; i < lcnt; ++i) { | ||
| 315 | if (llbuf[i]->ld_lopc <= addr && | ||
| 316 | llbuf[i]->ld_hipc > addr) { | ||
| 317 | memcpy(desc, llbuf[i], sizeof(Dwarf_Locdesc)); | ||
| 318 | desc->ld_s = | ||
| 319 | malloc(sizeof(Dwarf_Loc) * llbuf[i]->ld_cents); | ||
| 320 | DIE_IF(desc->ld_s == NULL); | ||
| 321 | memcpy(desc->ld_s, llbuf[i]->ld_s, | ||
| 322 | sizeof(Dwarf_Loc) * llbuf[i]->ld_cents); | ||
| 323 | ret = DW_DLV_OK; | ||
| 324 | break; | ||
| 325 | } | ||
| 326 | dwarf_dealloc(__dw_debug, llbuf[i]->ld_s, DW_DLA_LOC_BLOCK); | ||
| 327 | dwarf_dealloc(__dw_debug, llbuf[i], DW_DLA_LOCDESC); | ||
| 328 | } | ||
| 329 | /* Releasing loop */ | ||
| 330 | for (; i < lcnt; ++i) { | ||
| 331 | dwarf_dealloc(__dw_debug, llbuf[i]->ld_s, DW_DLA_LOC_BLOCK); | ||
| 332 | dwarf_dealloc(__dw_debug, llbuf[i], DW_DLA_LOCDESC); | ||
| 333 | } | ||
| 334 | dwarf_dealloc(__dw_debug, llbuf, DW_DLA_LIST); | ||
| 335 | return ret; | ||
| 336 | } | ||
| 337 | |||
| 338 | /* Get decl_file attribute value (file number) */ | ||
| 339 | static Dwarf_Unsigned die_get_decl_file(Dwarf_Die sp_die) | ||
| 340 | { | ||
| 341 | Dwarf_Attribute attr; | ||
| 342 | Dwarf_Unsigned fno; | ||
| 343 | int ret; | ||
| 344 | |||
| 345 | ret = dwarf_attr(sp_die, DW_AT_decl_file, &attr, &__dw_error); | ||
| 346 | DIE_IF(ret != DW_DLV_OK); | ||
| 347 | dwarf_formudata(attr, &fno, &__dw_error); | ||
| 348 | DIE_IF(ret != DW_DLV_OK); | ||
| 349 | dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR); | ||
| 350 | return fno; | ||
| 351 | } | ||
| 352 | |||
| 353 | /* Get decl_line attribute value (line number) */ | ||
| 354 | static Dwarf_Unsigned die_get_decl_line(Dwarf_Die sp_die) | ||
| 355 | { | ||
| 356 | Dwarf_Attribute attr; | ||
| 357 | Dwarf_Unsigned lno; | ||
| 358 | int ret; | ||
| 359 | |||
| 360 | ret = dwarf_attr(sp_die, DW_AT_decl_line, &attr, &__dw_error); | ||
| 361 | DIE_IF(ret != DW_DLV_OK); | ||
| 362 | dwarf_formudata(attr, &lno, &__dw_error); | ||
| 363 | DIE_IF(ret != DW_DLV_OK); | ||
| 364 | dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR); | ||
| 365 | return lno; | ||
| 366 | } | ||
| 367 | |||
| 368 | /* | ||
| 369 | * Probe finder related functions | ||
| 370 | */ | ||
| 371 | |||
| 372 | /* Show a location */ | ||
| 373 | static void show_location(Dwarf_Loc *loc, struct probe_finder *pf) | ||
| 374 | { | ||
| 375 | Dwarf_Small op; | ||
| 376 | Dwarf_Unsigned regn; | ||
| 377 | Dwarf_Signed offs; | ||
| 378 | int deref = 0, ret; | ||
| 379 | const char *regs; | ||
| 380 | |||
| 381 | op = loc->lr_atom; | ||
| 382 | |||
| 383 | /* If this is based on frame buffer, set the offset */ | ||
| 384 | if (op == DW_OP_fbreg) { | ||
| 385 | deref = 1; | ||
| 386 | offs = (Dwarf_Signed)loc->lr_number; | ||
| 387 | op = pf->fbloc.ld_s[0].lr_atom; | ||
| 388 | loc = &pf->fbloc.ld_s[0]; | ||
| 389 | } else | ||
| 390 | offs = 0; | ||
| 391 | |||
| 392 | if (op >= DW_OP_breg0 && op <= DW_OP_breg31) { | ||
| 393 | regn = op - DW_OP_breg0; | ||
| 394 | offs += (Dwarf_Signed)loc->lr_number; | ||
| 395 | deref = 1; | ||
| 396 | } else if (op >= DW_OP_reg0 && op <= DW_OP_reg31) { | ||
| 397 | regn = op - DW_OP_reg0; | ||
| 398 | } else if (op == DW_OP_bregx) { | ||
| 399 | regn = loc->lr_number; | ||
| 400 | offs += (Dwarf_Signed)loc->lr_number2; | ||
| 401 | deref = 1; | ||
| 402 | } else if (op == DW_OP_regx) { | ||
| 403 | regn = loc->lr_number; | ||
| 404 | } else | ||
| 405 | die("Dwarf_OP %d is not supported.\n", op); | ||
| 406 | |||
| 407 | regs = get_arch_regstr(regn); | ||
| 408 | if (!regs) | ||
| 409 | die("%lld exceeds max register number.\n", regn); | ||
| 410 | |||
| 411 | if (deref) | ||
| 412 | ret = snprintf(pf->buf, pf->len, | ||
| 413 | " %s=%+lld(%s)", pf->var, offs, regs); | ||
| 414 | else | ||
| 415 | ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs); | ||
| 416 | DIE_IF(ret < 0); | ||
| 417 | DIE_IF(ret >= pf->len); | ||
| 418 | } | ||
| 419 | |||
| 420 | /* Show a variables in kprobe event format */ | ||
| 421 | static void show_variable(Dwarf_Die vr_die, struct probe_finder *pf) | ||
| 422 | { | ||
| 423 | Dwarf_Attribute attr; | ||
| 424 | Dwarf_Locdesc ld; | ||
| 425 | int ret; | ||
| 426 | |||
| 427 | ret = dwarf_attr(vr_die, DW_AT_location, &attr, &__dw_error); | ||
| 428 | if (ret != DW_DLV_OK) | ||
| 429 | goto error; | ||
| 430 | ret = attr_get_locdesc(attr, &ld, (pf->addr - pf->cu_base)); | ||
| 431 | if (ret != DW_DLV_OK) | ||
| 432 | goto error; | ||
| 433 | /* TODO? */ | ||
| 434 | DIE_IF(ld.ld_cents != 1); | ||
| 435 | show_location(&ld.ld_s[0], pf); | ||
| 436 | free(ld.ld_s); | ||
| 437 | dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR); | ||
| 438 | return ; | ||
| 439 | error: | ||
| 440 | die("Failed to find the location of %s at this address.\n" | ||
| 441 | " Perhaps, it has been optimized out.\n", pf->var); | ||
| 442 | } | ||
| 443 | |||
| 444 | static int variable_callback(struct die_link *dlink, void *data) | ||
| 445 | { | ||
| 446 | struct probe_finder *pf = (struct probe_finder *)data; | ||
| 447 | Dwarf_Half tag; | ||
| 448 | int ret; | ||
| 449 | |||
| 450 | ret = dwarf_tag(dlink->die, &tag, &__dw_error); | ||
| 451 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 452 | if ((tag == DW_TAG_formal_parameter || | ||
| 453 | tag == DW_TAG_variable) && | ||
| 454 | (die_compare_name(dlink->die, pf->var) == 0)) { | ||
| 455 | show_variable(dlink->die, pf); | ||
| 456 | return 1; | ||
| 457 | } | ||
| 458 | /* TODO: Support struct members and arrays */ | ||
| 459 | return 0; | ||
| 460 | } | ||
| 461 | |||
| 462 | /* Find a variable in a subprogram die */ | ||
| 463 | static void find_variable(Dwarf_Die sp_die, struct probe_finder *pf) | ||
| 464 | { | ||
| 465 | int ret; | ||
| 466 | |||
| 467 | if (!is_c_varname(pf->var)) { | ||
| 468 | /* Output raw parameters */ | ||
| 469 | ret = snprintf(pf->buf, pf->len, " %s", pf->var); | ||
| 470 | DIE_IF(ret < 0); | ||
| 471 | DIE_IF(ret >= pf->len); | ||
| 472 | return ; | ||
| 473 | } | ||
| 474 | |||
| 475 | pr_debug("Searching '%s' variable in context.\n", pf->var); | ||
| 476 | /* Search child die for local variables and parameters. */ | ||
| 477 | ret = search_die_from_children(sp_die, variable_callback, pf); | ||
| 478 | if (!ret) | ||
| 479 | die("Failed to find '%s' in this function.\n", pf->var); | ||
| 480 | } | ||
| 481 | |||
| 482 | /* Get a frame base on the address */ | ||
| 483 | static void get_current_frame_base(Dwarf_Die sp_die, struct probe_finder *pf) | ||
| 484 | { | ||
| 485 | Dwarf_Attribute attr; | ||
| 486 | int ret; | ||
| 487 | |||
| 488 | ret = dwarf_attr(sp_die, DW_AT_frame_base, &attr, &__dw_error); | ||
| 489 | DIE_IF(ret != DW_DLV_OK); | ||
| 490 | ret = attr_get_locdesc(attr, &pf->fbloc, (pf->addr - pf->cu_base)); | ||
| 491 | DIE_IF(ret != DW_DLV_OK); | ||
| 492 | dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR); | ||
| 493 | } | ||
| 494 | |||
| 495 | static void free_current_frame_base(struct probe_finder *pf) | ||
| 496 | { | ||
| 497 | free(pf->fbloc.ld_s); | ||
| 498 | memset(&pf->fbloc, 0, sizeof(Dwarf_Locdesc)); | ||
| 499 | } | ||
| 500 | |||
| 501 | /* Show a probe point to output buffer */ | ||
| 502 | static void show_probepoint(Dwarf_Die sp_die, Dwarf_Signed offs, | ||
| 503 | struct probe_finder *pf) | ||
| 504 | { | ||
| 505 | struct probe_point *pp = pf->pp; | ||
| 506 | char *name; | ||
| 507 | char tmp[MAX_PROBE_BUFFER]; | ||
| 508 | int ret, i, len; | ||
| 509 | |||
| 510 | /* Output name of probe point */ | ||
| 511 | ret = dwarf_diename(sp_die, &name, &__dw_error); | ||
| 512 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 513 | if (ret == DW_DLV_OK) { | ||
| 514 | ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%u", name, | ||
| 515 | (unsigned int)offs); | ||
| 516 | /* Copy the function name if possible */ | ||
| 517 | if (!pp->function) { | ||
| 518 | pp->function = strdup(name); | ||
| 519 | pp->offset = offs; | ||
| 520 | } | ||
| 521 | dwarf_dealloc(__dw_debug, name, DW_DLA_STRING); | ||
| 522 | } else { | ||
| 523 | /* This function has no name. */ | ||
| 524 | ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%llx", pf->addr); | ||
| 525 | if (!pp->function) { | ||
| 526 | /* TODO: Use _stext */ | ||
| 527 | pp->function = strdup(""); | ||
| 528 | pp->offset = (int)pf->addr; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | DIE_IF(ret < 0); | ||
| 532 | DIE_IF(ret >= MAX_PROBE_BUFFER); | ||
| 533 | len = ret; | ||
| 534 | pr_debug("Probe point found: %s\n", tmp); | ||
| 535 | |||
| 536 | /* Find each argument */ | ||
| 537 | get_current_frame_base(sp_die, pf); | ||
| 538 | for (i = 0; i < pp->nr_args; i++) { | ||
| 539 | pf->var = pp->args[i]; | ||
| 540 | pf->buf = &tmp[len]; | ||
| 541 | pf->len = MAX_PROBE_BUFFER - len; | ||
| 542 | find_variable(sp_die, pf); | ||
| 543 | len += strlen(pf->buf); | ||
| 544 | } | ||
| 545 | free_current_frame_base(pf); | ||
| 546 | |||
| 547 | pp->probes[pp->found] = strdup(tmp); | ||
| 548 | pp->found++; | ||
| 549 | } | ||
| 550 | |||
| 551 | static int probeaddr_callback(struct die_link *dlink, void *data) | ||
| 552 | { | ||
| 553 | struct probe_finder *pf = (struct probe_finder *)data; | ||
| 554 | Dwarf_Half tag; | ||
| 555 | Dwarf_Signed offs; | ||
| 556 | int ret; | ||
| 557 | |||
| 558 | ret = dwarf_tag(dlink->die, &tag, &__dw_error); | ||
| 559 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 560 | /* Check the address is in this subprogram */ | ||
| 561 | if (tag == DW_TAG_subprogram && | ||
| 562 | die_within_subprogram(dlink->die, pf->addr, &offs)) { | ||
| 563 | show_probepoint(dlink->die, offs, pf); | ||
| 564 | return 1; | ||
| 565 | } | ||
| 566 | return 0; | ||
| 567 | } | ||
| 568 | |||
| 569 | /* Find probe point from its line number */ | ||
| 570 | static void find_by_line(struct probe_finder *pf) | ||
| 571 | { | ||
| 572 | Dwarf_Signed cnt, i, clm; | ||
| 573 | Dwarf_Line *lines; | ||
| 574 | Dwarf_Unsigned lineno = 0; | ||
| 575 | Dwarf_Addr addr; | ||
| 576 | Dwarf_Unsigned fno; | ||
| 577 | int ret; | ||
| 578 | |||
| 579 | ret = dwarf_srclines(pf->cu_die, &lines, &cnt, &__dw_error); | ||
| 580 | DIE_IF(ret != DW_DLV_OK); | ||
| 581 | |||
| 582 | for (i = 0; i < cnt; i++) { | ||
| 583 | ret = dwarf_line_srcfileno(lines[i], &fno, &__dw_error); | ||
| 584 | DIE_IF(ret != DW_DLV_OK); | ||
| 585 | if (fno != pf->fno) | ||
| 586 | continue; | ||
| 587 | |||
| 588 | ret = dwarf_lineno(lines[i], &lineno, &__dw_error); | ||
| 589 | DIE_IF(ret != DW_DLV_OK); | ||
| 590 | if (lineno != pf->lno) | ||
| 591 | continue; | ||
| 592 | |||
| 593 | ret = dwarf_lineoff(lines[i], &clm, &__dw_error); | ||
| 594 | DIE_IF(ret != DW_DLV_OK); | ||
| 595 | |||
| 596 | ret = dwarf_lineaddr(lines[i], &addr, &__dw_error); | ||
| 597 | DIE_IF(ret != DW_DLV_OK); | ||
| 598 | pr_debug("Probe line found: line[%d]:%u,%d addr:0x%llx\n", | ||
| 599 | (int)i, (unsigned)lineno, (int)clm, addr); | ||
| 600 | pf->addr = addr; | ||
| 601 | /* Search a real subprogram including this line, */ | ||
| 602 | ret = search_die_from_children(pf->cu_die, | ||
| 603 | probeaddr_callback, pf); | ||
| 604 | if (ret == 0) | ||
| 605 | die("Probe point is not found in subprograms.\n"); | ||
| 606 | /* Continuing, because target line might be inlined. */ | ||
| 607 | } | ||
| 608 | dwarf_srclines_dealloc(__dw_debug, lines, cnt); | ||
| 609 | } | ||
| 610 | |||
| 611 | /* Search function from function name */ | ||
| 612 | static int probefunc_callback(struct die_link *dlink, void *data) | ||
| 613 | { | ||
| 614 | struct probe_finder *pf = (struct probe_finder *)data; | ||
| 615 | struct probe_point *pp = pf->pp; | ||
| 616 | struct die_link *lk; | ||
| 617 | Dwarf_Signed offs; | ||
| 618 | Dwarf_Half tag; | ||
| 619 | int ret; | ||
| 620 | |||
| 621 | ret = dwarf_tag(dlink->die, &tag, &__dw_error); | ||
| 622 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 623 | if (tag == DW_TAG_subprogram) { | ||
| 624 | if (die_compare_name(dlink->die, pp->function) == 0) { | ||
| 625 | if (pp->line) { /* Function relative line */ | ||
| 626 | pf->fno = die_get_decl_file(dlink->die); | ||
| 627 | pf->lno = die_get_decl_line(dlink->die) | ||
| 628 | + pp->line; | ||
| 629 | find_by_line(pf); | ||
| 630 | return 1; | ||
| 631 | } | ||
| 632 | if (die_inlined_subprogram(dlink->die)) { | ||
| 633 | /* Inlined function, save it. */ | ||
| 634 | ret = dwarf_die_CU_offset(dlink->die, | ||
| 635 | &pf->inl_offs, | ||
| 636 | &__dw_error); | ||
| 637 | DIE_IF(ret != DW_DLV_OK); | ||
| 638 | pr_debug("inline definition offset %lld\n", | ||
| 639 | pf->inl_offs); | ||
| 640 | return 0; /* Continue to search */ | ||
| 641 | } | ||
| 642 | /* Get probe address */ | ||
| 643 | pf->addr = die_get_entrypc(dlink->die); | ||
| 644 | pf->addr += pp->offset; | ||
| 645 | /* TODO: Check the address in this function */ | ||
| 646 | show_probepoint(dlink->die, pp->offset, pf); | ||
| 647 | return 1; /* Exit; no same symbol in this CU. */ | ||
| 648 | } | ||
| 649 | } else if (tag == DW_TAG_inlined_subroutine && pf->inl_offs) { | ||
| 650 | if (die_get_abstract_origin(dlink->die) == pf->inl_offs) { | ||
| 651 | /* Get probe address */ | ||
| 652 | pf->addr = die_get_entrypc(dlink->die); | ||
| 653 | pf->addr += pp->offset; | ||
| 654 | pr_debug("found inline addr: 0x%llx\n", pf->addr); | ||
| 655 | /* Inlined function. Get a real subprogram */ | ||
| 656 | for (lk = dlink->parent; lk != NULL; lk = lk->parent) { | ||
| 657 | tag = 0; | ||
| 658 | dwarf_tag(lk->die, &tag, &__dw_error); | ||
| 659 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 660 | if (tag == DW_TAG_subprogram && | ||
| 661 | !die_inlined_subprogram(lk->die)) | ||
| 662 | goto found; | ||
| 663 | } | ||
| 664 | die("Failed to find real subprogram.\n"); | ||
| 665 | found: | ||
| 666 | /* Get offset from subprogram */ | ||
| 667 | ret = die_within_subprogram(lk->die, pf->addr, &offs); | ||
| 668 | DIE_IF(!ret); | ||
| 669 | show_probepoint(lk->die, offs, pf); | ||
| 670 | /* Continue to search */ | ||
| 671 | } | ||
| 672 | } | ||
| 673 | return 0; | ||
| 674 | } | ||
| 675 | |||
| 676 | static void find_by_func(struct probe_finder *pf) | ||
| 677 | { | ||
| 678 | search_die_from_children(pf->cu_die, probefunc_callback, pf); | ||
| 679 | } | ||
| 680 | |||
| 681 | /* Find a probe point */ | ||
| 682 | int find_probepoint(int fd, struct probe_point *pp) | ||
| 683 | { | ||
| 684 | Dwarf_Half addr_size = 0; | ||
| 685 | Dwarf_Unsigned next_cuh = 0; | ||
| 686 | int cu_number = 0, ret; | ||
| 687 | struct probe_finder pf = {.pp = pp}; | ||
| 688 | |||
| 689 | ret = dwarf_init(fd, DW_DLC_READ, 0, 0, &__dw_debug, &__dw_error); | ||
| 690 | if (ret != DW_DLV_OK) { | ||
| 691 | pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO.\n"); | ||
| 692 | return -ENOENT; | ||
| 693 | } | ||
| 694 | |||
| 695 | pp->found = 0; | ||
| 696 | while (++cu_number) { | ||
| 697 | /* Search CU (Compilation Unit) */ | ||
| 698 | ret = dwarf_next_cu_header(__dw_debug, NULL, NULL, NULL, | ||
| 699 | &addr_size, &next_cuh, &__dw_error); | ||
| 700 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 701 | if (ret == DW_DLV_NO_ENTRY) | ||
| 702 | break; | ||
| 703 | |||
| 704 | /* Get the DIE(Debugging Information Entry) of this CU */ | ||
| 705 | ret = dwarf_siblingof(__dw_debug, 0, &pf.cu_die, &__dw_error); | ||
| 706 | DIE_IF(ret != DW_DLV_OK); | ||
| 707 | |||
| 708 | /* Check if target file is included. */ | ||
| 709 | if (pp->file) | ||
| 710 | pf.fno = cu_find_fileno(pf.cu_die, pp->file); | ||
| 711 | |||
| 712 | if (!pp->file || pf.fno) { | ||
| 713 | /* Save CU base address (for frame_base) */ | ||
| 714 | ret = dwarf_lowpc(pf.cu_die, &pf.cu_base, &__dw_error); | ||
| 715 | DIE_IF(ret == DW_DLV_ERROR); | ||
| 716 | if (ret == DW_DLV_NO_ENTRY) | ||
| 717 | pf.cu_base = 0; | ||
| 718 | if (pp->function) | ||
| 719 | find_by_func(&pf); | ||
| 720 | else { | ||
| 721 | pf.lno = pp->line; | ||
| 722 | find_by_line(&pf); | ||
| 723 | } | ||
| 724 | } | ||
| 725 | dwarf_dealloc(__dw_debug, pf.cu_die, DW_DLA_DIE); | ||
| 726 | } | ||
| 727 | ret = dwarf_finish(__dw_debug, &__dw_error); | ||
| 728 | DIE_IF(ret != DW_DLV_OK); | ||
| 729 | |||
| 730 | return pp->found; | ||
| 731 | } | ||
| 732 | |||
diff --git a/tools/perf/util/probe-finder.h b/tools/perf/util/probe-finder.h new file mode 100644 index 000000000000..bdebca6697d2 --- /dev/null +++ b/tools/perf/util/probe-finder.h | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #ifndef _PROBE_FINDER_H | ||
| 2 | #define _PROBE_FINDER_H | ||
| 3 | |||
| 4 | #define MAX_PATH_LEN 256 | ||
| 5 | #define MAX_PROBE_BUFFER 1024 | ||
| 6 | #define MAX_PROBES 128 | ||
| 7 | |||
| 8 | static inline int is_c_varname(const char *name) | ||
| 9 | { | ||
| 10 | /* TODO */ | ||
| 11 | return isalpha(name[0]) || name[0] == '_'; | ||
| 12 | } | ||
| 13 | |||
| 14 | struct probe_point { | ||
| 15 | /* Inputs */ | ||
| 16 | char *file; /* File name */ | ||
| 17 | int line; /* Line number */ | ||
| 18 | |||
| 19 | char *function; /* Function name */ | ||
| 20 | int offset; /* Offset bytes */ | ||
| 21 | |||
| 22 | int nr_args; /* Number of arguments */ | ||
| 23 | char **args; /* Arguments */ | ||
| 24 | |||
| 25 | int retprobe; /* Return probe */ | ||
| 26 | |||
| 27 | /* Output */ | ||
| 28 | int found; /* Number of found probe points */ | ||
| 29 | char *probes[MAX_PROBES]; /* Output buffers (will be allocated)*/ | ||
| 30 | }; | ||
| 31 | |||
| 32 | #ifndef NO_LIBDWARF | ||
| 33 | extern int find_probepoint(int fd, struct probe_point *pp); | ||
| 34 | |||
| 35 | #include <libdwarf/dwarf.h> | ||
| 36 | #include <libdwarf/libdwarf.h> | ||
| 37 | |||
| 38 | struct probe_finder { | ||
| 39 | struct probe_point *pp; /* Target probe point */ | ||
| 40 | |||
| 41 | /* For function searching */ | ||
| 42 | Dwarf_Addr addr; /* Address */ | ||
| 43 | Dwarf_Unsigned fno; /* File number */ | ||
| 44 | Dwarf_Unsigned lno; /* Line number */ | ||
| 45 | Dwarf_Off inl_offs; /* Inline offset */ | ||
| 46 | Dwarf_Die cu_die; /* Current CU */ | ||
| 47 | |||
| 48 | /* For variable searching */ | ||
| 49 | Dwarf_Addr cu_base; /* Current CU base address */ | ||
| 50 | Dwarf_Locdesc fbloc; /* Location of Current Frame Base */ | ||
| 51 | const char *var; /* Current variable name */ | ||
| 52 | char *buf; /* Current output buffer */ | ||
| 53 | int len; /* Length of output buffer */ | ||
| 54 | }; | ||
| 55 | #endif /* NO_LIBDWARF */ | ||
| 56 | |||
| 57 | #endif /*_PROBE_FINDER_H */ | ||
diff --git a/tools/perf/util/quote.h b/tools/perf/util/quote.h index a5454a1d1c13..b6a019733919 100644 --- a/tools/perf/util/quote.h +++ b/tools/perf/util/quote.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef QUOTE_H | 1 | #ifndef __PERF_QUOTE_H |
| 2 | #define QUOTE_H | 2 | #define __PERF_QUOTE_H |
| 3 | 3 | ||
| 4 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| @@ -65,4 +65,4 @@ extern void perl_quote_print(FILE *stream, const char *src); | |||
| 65 | extern void python_quote_print(FILE *stream, const char *src); | 65 | extern void python_quote_print(FILE *stream, const char *src); |
| 66 | extern void tcl_quote_print(FILE *stream, const char *src); | 66 | extern void tcl_quote_print(FILE *stream, const char *src); |
| 67 | 67 | ||
| 68 | #endif | 68 | #endif /* __PERF_QUOTE_H */ |
diff --git a/tools/perf/util/run-command.h b/tools/perf/util/run-command.h index cc1837deba88..d79028727ce2 100644 --- a/tools/perf/util/run-command.h +++ b/tools/perf/util/run-command.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef RUN_COMMAND_H | 1 | #ifndef __PERF_RUN_COMMAND_H |
| 2 | #define RUN_COMMAND_H | 2 | #define __PERF_RUN_COMMAND_H |
| 3 | 3 | ||
| 4 | enum { | 4 | enum { |
| 5 | ERR_RUN_COMMAND_FORK = 10000, | 5 | ERR_RUN_COMMAND_FORK = 10000, |
| @@ -85,4 +85,4 @@ struct async { | |||
| 85 | int start_async(struct async *async); | 85 | int start_async(struct async *async); |
| 86 | int finish_async(struct async *async); | 86 | int finish_async(struct async *async); |
| 87 | 87 | ||
| 88 | #endif | 88 | #endif /* __PERF_RUN_COMMAND_H */ |
diff --git a/tools/perf/util/sigchain.h b/tools/perf/util/sigchain.h index 618083bce0c6..1a53c11265fd 100644 --- a/tools/perf/util/sigchain.h +++ b/tools/perf/util/sigchain.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef SIGCHAIN_H | 1 | #ifndef __PERF_SIGCHAIN_H |
| 2 | #define SIGCHAIN_H | 2 | #define __PERF_SIGCHAIN_H |
| 3 | 3 | ||
| 4 | typedef void (*sigchain_fun)(int); | 4 | typedef void (*sigchain_fun)(int); |
| 5 | 5 | ||
| @@ -8,4 +8,4 @@ int sigchain_pop(int sig); | |||
| 8 | 8 | ||
| 9 | void sigchain_push_common(sigchain_fun f); | 9 | void sigchain_push_common(sigchain_fun f); |
| 10 | 10 | ||
| 11 | #endif /* SIGCHAIN_H */ | 11 | #endif /* __PERF_SIGCHAIN_H */ |
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c new file mode 100644 index 000000000000..b490354d1b23 --- /dev/null +++ b/tools/perf/util/sort.c | |||
| @@ -0,0 +1,290 @@ | |||
| 1 | #include "sort.h" | ||
| 2 | |||
| 3 | regex_t parent_regex; | ||
| 4 | char default_parent_pattern[] = "^sys_|^do_page_fault"; | ||
| 5 | char *parent_pattern = default_parent_pattern; | ||
| 6 | char default_sort_order[] = "comm,dso,symbol"; | ||
| 7 | char *sort_order = default_sort_order; | ||
| 8 | int sort__need_collapse = 0; | ||
| 9 | int sort__has_parent = 0; | ||
| 10 | |||
| 11 | enum sort_type sort__first_dimension; | ||
| 12 | |||
| 13 | unsigned int dsos__col_width; | ||
| 14 | unsigned int comms__col_width; | ||
| 15 | unsigned int threads__col_width; | ||
| 16 | static unsigned int parent_symbol__col_width; | ||
| 17 | char * field_sep; | ||
| 18 | |||
| 19 | LIST_HEAD(hist_entry__sort_list); | ||
| 20 | |||
| 21 | struct sort_entry sort_thread = { | ||
| 22 | .header = "Command: Pid", | ||
| 23 | .cmp = sort__thread_cmp, | ||
| 24 | .print = sort__thread_print, | ||
| 25 | .width = &threads__col_width, | ||
| 26 | }; | ||
| 27 | |||
| 28 | struct sort_entry sort_comm = { | ||
| 29 | .header = "Command", | ||
| 30 | .cmp = sort__comm_cmp, | ||
| 31 | .collapse = sort__comm_collapse, | ||
| 32 | .print = sort__comm_print, | ||
| 33 | .width = &comms__col_width, | ||
| 34 | }; | ||
| 35 | |||
| 36 | struct sort_entry sort_dso = { | ||
| 37 | .header = "Shared Object", | ||
| 38 | .cmp = sort__dso_cmp, | ||
| 39 | .print = sort__dso_print, | ||
| 40 | .width = &dsos__col_width, | ||
| 41 | }; | ||
| 42 | |||
| 43 | struct sort_entry sort_sym = { | ||
| 44 | .header = "Symbol", | ||
| 45 | .cmp = sort__sym_cmp, | ||
| 46 | .print = sort__sym_print, | ||
| 47 | }; | ||
| 48 | |||
| 49 | struct sort_entry sort_parent = { | ||
| 50 | .header = "Parent symbol", | ||
| 51 | .cmp = sort__parent_cmp, | ||
| 52 | .print = sort__parent_print, | ||
| 53 | .width = &parent_symbol__col_width, | ||
| 54 | }; | ||
| 55 | |||
| 56 | struct sort_dimension { | ||
| 57 | const char *name; | ||
| 58 | struct sort_entry *entry; | ||
| 59 | int taken; | ||
| 60 | }; | ||
| 61 | |||
| 62 | static struct sort_dimension sort_dimensions[] = { | ||
| 63 | { .name = "pid", .entry = &sort_thread, }, | ||
| 64 | { .name = "comm", .entry = &sort_comm, }, | ||
| 65 | { .name = "dso", .entry = &sort_dso, }, | ||
| 66 | { .name = "symbol", .entry = &sort_sym, }, | ||
| 67 | { .name = "parent", .entry = &sort_parent, }, | ||
| 68 | }; | ||
| 69 | |||
| 70 | int64_t cmp_null(void *l, void *r) | ||
| 71 | { | ||
| 72 | if (!l && !r) | ||
| 73 | return 0; | ||
| 74 | else if (!l) | ||
| 75 | return -1; | ||
| 76 | else | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | /* --sort pid */ | ||
| 81 | |||
| 82 | int64_t | ||
| 83 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 84 | { | ||
| 85 | return right->thread->pid - left->thread->pid; | ||
| 86 | } | ||
| 87 | |||
| 88 | int repsep_fprintf(FILE *fp, const char *fmt, ...) | ||
| 89 | { | ||
| 90 | int n; | ||
| 91 | va_list ap; | ||
| 92 | |||
| 93 | va_start(ap, fmt); | ||
| 94 | if (!field_sep) | ||
| 95 | n = vfprintf(fp, fmt, ap); | ||
| 96 | else { | ||
| 97 | char *bf = NULL; | ||
| 98 | n = vasprintf(&bf, fmt, ap); | ||
| 99 | if (n > 0) { | ||
| 100 | char *sep = bf; | ||
| 101 | |||
| 102 | while (1) { | ||
| 103 | sep = strchr(sep, *field_sep); | ||
| 104 | if (sep == NULL) | ||
| 105 | break; | ||
| 106 | *sep = '.'; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | fputs(bf, fp); | ||
| 110 | free(bf); | ||
| 111 | } | ||
| 112 | va_end(ap); | ||
| 113 | return n; | ||
| 114 | } | ||
| 115 | |||
| 116 | size_t | ||
| 117 | sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 118 | { | ||
| 119 | return repsep_fprintf(fp, "%*s:%5d", width - 6, | ||
| 120 | self->thread->comm ?: "", self->thread->pid); | ||
| 121 | } | ||
| 122 | |||
| 123 | size_t | ||
| 124 | sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 125 | { | ||
| 126 | return repsep_fprintf(fp, "%*s", width, self->thread->comm); | ||
| 127 | } | ||
| 128 | |||
| 129 | /* --sort dso */ | ||
| 130 | |||
| 131 | int64_t | ||
| 132 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 133 | { | ||
| 134 | struct dso *dso_l = left->map ? left->map->dso : NULL; | ||
| 135 | struct dso *dso_r = right->map ? right->map->dso : NULL; | ||
| 136 | const char *dso_name_l, *dso_name_r; | ||
| 137 | |||
| 138 | if (!dso_l || !dso_r) | ||
| 139 | return cmp_null(dso_l, dso_r); | ||
| 140 | |||
| 141 | if (verbose) { | ||
| 142 | dso_name_l = dso_l->long_name; | ||
| 143 | dso_name_r = dso_r->long_name; | ||
| 144 | } else { | ||
| 145 | dso_name_l = dso_l->short_name; | ||
| 146 | dso_name_r = dso_r->short_name; | ||
| 147 | } | ||
| 148 | |||
| 149 | return strcmp(dso_name_l, dso_name_r); | ||
| 150 | } | ||
| 151 | |||
| 152 | size_t | ||
| 153 | sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 154 | { | ||
| 155 | if (self->map && self->map->dso) { | ||
| 156 | const char *dso_name = !verbose ? self->map->dso->short_name : | ||
| 157 | self->map->dso->long_name; | ||
| 158 | return repsep_fprintf(fp, "%-*s", width, dso_name); | ||
| 159 | } | ||
| 160 | |||
| 161 | return repsep_fprintf(fp, "%*llx", width, (u64)self->ip); | ||
| 162 | } | ||
| 163 | |||
| 164 | /* --sort symbol */ | ||
| 165 | |||
| 166 | int64_t | ||
| 167 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 168 | { | ||
| 169 | u64 ip_l, ip_r; | ||
| 170 | |||
| 171 | if (left->sym == right->sym) | ||
| 172 | return 0; | ||
| 173 | |||
| 174 | ip_l = left->sym ? left->sym->start : left->ip; | ||
| 175 | ip_r = right->sym ? right->sym->start : right->ip; | ||
| 176 | |||
| 177 | return (int64_t)(ip_r - ip_l); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | size_t | ||
| 182 | sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used) | ||
| 183 | { | ||
| 184 | size_t ret = 0; | ||
| 185 | |||
| 186 | if (verbose) { | ||
| 187 | char o = self->map ? dso__symtab_origin(self->map->dso) : '!'; | ||
| 188 | ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip, o); | ||
| 189 | } | ||
| 190 | |||
| 191 | ret += repsep_fprintf(fp, "[%c] ", self->level); | ||
| 192 | if (self->sym) | ||
| 193 | ret += repsep_fprintf(fp, "%s", self->sym->name); | ||
| 194 | else | ||
| 195 | ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip); | ||
| 196 | |||
| 197 | return ret; | ||
| 198 | } | ||
| 199 | |||
| 200 | /* --sort comm */ | ||
| 201 | |||
| 202 | int64_t | ||
| 203 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 204 | { | ||
| 205 | return right->thread->pid - left->thread->pid; | ||
| 206 | } | ||
| 207 | |||
| 208 | int64_t | ||
| 209 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) | ||
| 210 | { | ||
| 211 | char *comm_l = left->thread->comm; | ||
| 212 | char *comm_r = right->thread->comm; | ||
| 213 | |||
| 214 | if (!comm_l || !comm_r) | ||
| 215 | return cmp_null(comm_l, comm_r); | ||
| 216 | |||
| 217 | return strcmp(comm_l, comm_r); | ||
| 218 | } | ||
| 219 | |||
| 220 | /* --sort parent */ | ||
| 221 | |||
| 222 | int64_t | ||
| 223 | sort__parent_cmp(struct hist_entry *left, struct hist_entry *right) | ||
| 224 | { | ||
| 225 | struct symbol *sym_l = left->parent; | ||
| 226 | struct symbol *sym_r = right->parent; | ||
| 227 | |||
| 228 | if (!sym_l || !sym_r) | ||
| 229 | return cmp_null(sym_l, sym_r); | ||
| 230 | |||
| 231 | return strcmp(sym_l->name, sym_r->name); | ||
| 232 | } | ||
| 233 | |||
| 234 | size_t | ||
| 235 | sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width) | ||
| 236 | { | ||
| 237 | return repsep_fprintf(fp, "%-*s", width, | ||
| 238 | self->parent ? self->parent->name : "[other]"); | ||
| 239 | } | ||
| 240 | |||
| 241 | int sort_dimension__add(const char *tok) | ||
| 242 | { | ||
| 243 | unsigned int i; | ||
| 244 | |||
| 245 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { | ||
| 246 | struct sort_dimension *sd = &sort_dimensions[i]; | ||
| 247 | |||
| 248 | if (sd->taken) | ||
| 249 | continue; | ||
| 250 | |||
| 251 | if (strncasecmp(tok, sd->name, strlen(tok))) | ||
| 252 | continue; | ||
| 253 | |||
| 254 | if (sd->entry->collapse) | ||
| 255 | sort__need_collapse = 1; | ||
| 256 | |||
| 257 | if (sd->entry == &sort_parent) { | ||
| 258 | int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED); | ||
| 259 | if (ret) { | ||
| 260 | char err[BUFSIZ]; | ||
| 261 | |||
| 262 | regerror(ret, &parent_regex, err, sizeof(err)); | ||
| 263 | fprintf(stderr, "Invalid regex: %s\n%s", | ||
| 264 | parent_pattern, err); | ||
| 265 | exit(-1); | ||
| 266 | } | ||
| 267 | sort__has_parent = 1; | ||
| 268 | } | ||
| 269 | |||
| 270 | if (list_empty(&hist_entry__sort_list)) { | ||
| 271 | if (!strcmp(sd->name, "pid")) | ||
| 272 | sort__first_dimension = SORT_PID; | ||
| 273 | else if (!strcmp(sd->name, "comm")) | ||
| 274 | sort__first_dimension = SORT_COMM; | ||
| 275 | else if (!strcmp(sd->name, "dso")) | ||
| 276 | sort__first_dimension = SORT_DSO; | ||
| 277 | else if (!strcmp(sd->name, "symbol")) | ||
| 278 | sort__first_dimension = SORT_SYM; | ||
| 279 | else if (!strcmp(sd->name, "parent")) | ||
| 280 | sort__first_dimension = SORT_PARENT; | ||
| 281 | } | ||
| 282 | |||
| 283 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); | ||
| 284 | sd->taken = 1; | ||
| 285 | |||
| 286 | return 0; | ||
| 287 | } | ||
| 288 | |||
| 289 | return -ESRCH; | ||
| 290 | } | ||
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h new file mode 100644 index 000000000000..333e664ff45f --- /dev/null +++ b/tools/perf/util/sort.h | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | #ifndef __PERF_SORT_H | ||
| 2 | #define __PERF_SORT_H | ||
| 3 | #include "../builtin.h" | ||
| 4 | |||
| 5 | #include "util.h" | ||
| 6 | |||
| 7 | #include "color.h" | ||
| 8 | #include <linux/list.h> | ||
| 9 | #include "cache.h" | ||
| 10 | #include <linux/rbtree.h> | ||
| 11 | #include "symbol.h" | ||
| 12 | #include "string.h" | ||
| 13 | #include "callchain.h" | ||
| 14 | #include "strlist.h" | ||
| 15 | #include "values.h" | ||
| 16 | |||
| 17 | #include "../perf.h" | ||
| 18 | #include "debug.h" | ||
| 19 | #include "header.h" | ||
| 20 | |||
| 21 | #include "parse-options.h" | ||
| 22 | #include "parse-events.h" | ||
| 23 | |||
| 24 | #include "thread.h" | ||
| 25 | #include "sort.h" | ||
| 26 | |||
| 27 | extern regex_t parent_regex; | ||
| 28 | extern char *sort_order; | ||
| 29 | extern char default_parent_pattern[]; | ||
| 30 | extern char *parent_pattern; | ||
| 31 | extern char default_sort_order[]; | ||
| 32 | extern int sort__need_collapse; | ||
| 33 | extern int sort__has_parent; | ||
| 34 | extern char *field_sep; | ||
| 35 | extern struct sort_entry sort_comm; | ||
| 36 | extern struct sort_entry sort_dso; | ||
| 37 | extern struct sort_entry sort_sym; | ||
| 38 | extern struct sort_entry sort_parent; | ||
| 39 | extern unsigned int dsos__col_width; | ||
| 40 | extern unsigned int comms__col_width; | ||
| 41 | extern unsigned int threads__col_width; | ||
| 42 | extern enum sort_type sort__first_dimension; | ||
| 43 | |||
| 44 | struct hist_entry { | ||
| 45 | struct rb_node rb_node; | ||
| 46 | u64 count; | ||
| 47 | struct thread *thread; | ||
| 48 | struct map *map; | ||
| 49 | struct symbol *sym; | ||
| 50 | u64 ip; | ||
| 51 | char level; | ||
| 52 | struct symbol *parent; | ||
| 53 | struct callchain_node callchain; | ||
| 54 | struct rb_root sorted_chain; | ||
| 55 | }; | ||
| 56 | |||
| 57 | enum sort_type { | ||
| 58 | SORT_PID, | ||
| 59 | SORT_COMM, | ||
| 60 | SORT_DSO, | ||
| 61 | SORT_SYM, | ||
| 62 | SORT_PARENT | ||
| 63 | }; | ||
| 64 | |||
| 65 | /* | ||
| 66 | * configurable sorting bits | ||
| 67 | */ | ||
| 68 | |||
| 69 | struct sort_entry { | ||
| 70 | struct list_head list; | ||
| 71 | |||
| 72 | const char *header; | ||
| 73 | |||
| 74 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); | ||
| 75 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); | ||
| 76 | size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width); | ||
| 77 | unsigned int *width; | ||
| 78 | bool elide; | ||
| 79 | }; | ||
| 80 | |||
| 81 | extern struct sort_entry sort_thread; | ||
| 82 | extern struct list_head hist_entry__sort_list; | ||
| 83 | |||
| 84 | extern int repsep_fprintf(FILE *fp, const char *fmt, ...); | ||
| 85 | extern size_t sort__thread_print(FILE *, struct hist_entry *, unsigned int); | ||
| 86 | extern size_t sort__comm_print(FILE *, struct hist_entry *, unsigned int); | ||
| 87 | extern size_t sort__dso_print(FILE *, struct hist_entry *, unsigned int); | ||
| 88 | extern size_t sort__sym_print(FILE *, struct hist_entry *, unsigned int __used); | ||
| 89 | extern int64_t cmp_null(void *, void *); | ||
| 90 | extern int64_t sort__thread_cmp(struct hist_entry *, struct hist_entry *); | ||
| 91 | extern int64_t sort__comm_cmp(struct hist_entry *, struct hist_entry *); | ||
| 92 | extern int64_t sort__comm_collapse(struct hist_entry *, struct hist_entry *); | ||
| 93 | extern int64_t sort__dso_cmp(struct hist_entry *, struct hist_entry *); | ||
| 94 | extern int64_t sort__sym_cmp(struct hist_entry *, struct hist_entry *); | ||
| 95 | extern int64_t sort__parent_cmp(struct hist_entry *, struct hist_entry *); | ||
| 96 | extern size_t sort__parent_print(FILE *, struct hist_entry *, unsigned int); | ||
| 97 | extern int sort_dimension__add(const char *); | ||
| 98 | |||
| 99 | #endif /* __PERF_SORT_H */ | ||
diff --git a/tools/perf/util/strbuf.h b/tools/perf/util/strbuf.h index d2aa86c014c1..a3d121d6c83e 100644 --- a/tools/perf/util/strbuf.h +++ b/tools/perf/util/strbuf.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef STRBUF_H | 1 | #ifndef __PERF_STRBUF_H |
| 2 | #define STRBUF_H | 2 | #define __PERF_STRBUF_H |
| 3 | 3 | ||
| 4 | /* | 4 | /* |
| 5 | * Strbuf's can be use in many ways: as a byte array, or to store arbitrary | 5 | * Strbuf's can be use in many ways: as a byte array, or to store arbitrary |
| @@ -134,4 +134,4 @@ extern int launch_editor(const char *path, struct strbuf *buffer, const char *co | |||
| 134 | extern int strbuf_branchname(struct strbuf *sb, const char *name); | 134 | extern int strbuf_branchname(struct strbuf *sb, const char *name); |
| 135 | extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name); | 135 | extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name); |
| 136 | 136 | ||
| 137 | #endif /* STRBUF_H */ | 137 | #endif /* __PERF_STRBUF_H */ |
diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c index c93eca9a7be3..f24a8cc933d5 100644 --- a/tools/perf/util/string.c +++ b/tools/perf/util/string.c | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #include "string.h" | 1 | #include "string.h" |
| 2 | #include "util.h" | ||
| 2 | 3 | ||
| 3 | static int hex(char ch) | 4 | static int hex(char ch) |
| 4 | { | 5 | { |
| @@ -32,3 +33,196 @@ int hex2u64(const char *ptr, u64 *long_val) | |||
| 32 | 33 | ||
| 33 | return p - ptr; | 34 | return p - ptr; |
| 34 | } | 35 | } |
| 36 | |||
| 37 | char *strxfrchar(char *s, char from, char to) | ||
| 38 | { | ||
| 39 | char *p = s; | ||
| 40 | |||
| 41 | while ((p = strchr(p, from)) != NULL) | ||
| 42 | *p++ = to; | ||
| 43 | |||
| 44 | return s; | ||
| 45 | } | ||
| 46 | |||
| 47 | #define K 1024LL | ||
| 48 | /* | ||
| 49 | * perf_atoll() | ||
| 50 | * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB") | ||
| 51 | * and return its numeric value | ||
| 52 | */ | ||
| 53 | s64 perf_atoll(const char *str) | ||
| 54 | { | ||
| 55 | unsigned int i; | ||
| 56 | s64 length = -1, unit = 1; | ||
| 57 | |||
| 58 | if (!isdigit(str[0])) | ||
| 59 | goto out_err; | ||
| 60 | |||
| 61 | for (i = 1; i < strlen(str); i++) { | ||
| 62 | switch (str[i]) { | ||
| 63 | case 'B': | ||
| 64 | case 'b': | ||
| 65 | break; | ||
| 66 | case 'K': | ||
| 67 | if (str[i + 1] != 'B') | ||
| 68 | goto out_err; | ||
| 69 | else | ||
| 70 | goto kilo; | ||
| 71 | case 'k': | ||
| 72 | if (str[i + 1] != 'b') | ||
| 73 | goto out_err; | ||
| 74 | kilo: | ||
| 75 | unit = K; | ||
| 76 | break; | ||
| 77 | case 'M': | ||
| 78 | if (str[i + 1] != 'B') | ||
| 79 | goto out_err; | ||
| 80 | else | ||
| 81 | goto mega; | ||
| 82 | case 'm': | ||
| 83 | if (str[i + 1] != 'b') | ||
| 84 | goto out_err; | ||
| 85 | mega: | ||
| 86 | unit = K * K; | ||
| 87 | break; | ||
| 88 | case 'G': | ||
| 89 | if (str[i + 1] != 'B') | ||
| 90 | goto out_err; | ||
| 91 | else | ||
| 92 | goto giga; | ||
| 93 | case 'g': | ||
| 94 | if (str[i + 1] != 'b') | ||
| 95 | goto out_err; | ||
| 96 | giga: | ||
| 97 | unit = K * K * K; | ||
| 98 | break; | ||
| 99 | case 'T': | ||
| 100 | if (str[i + 1] != 'B') | ||
| 101 | goto out_err; | ||
| 102 | else | ||
| 103 | goto tera; | ||
| 104 | case 't': | ||
| 105 | if (str[i + 1] != 'b') | ||
| 106 | goto out_err; | ||
| 107 | tera: | ||
| 108 | unit = K * K * K * K; | ||
| 109 | break; | ||
| 110 | case '\0': /* only specified figures */ | ||
| 111 | unit = 1; | ||
| 112 | break; | ||
| 113 | default: | ||
| 114 | if (!isdigit(str[i])) | ||
| 115 | goto out_err; | ||
| 116 | break; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | length = atoll(str) * unit; | ||
| 121 | goto out; | ||
| 122 | |||
| 123 | out_err: | ||
| 124 | length = -1; | ||
| 125 | out: | ||
| 126 | return length; | ||
| 127 | } | ||
| 128 | |||
| 129 | /* | ||
| 130 | * Helper function for splitting a string into an argv-like array. | ||
| 131 | * originaly copied from lib/argv_split.c | ||
| 132 | */ | ||
| 133 | static const char *skip_sep(const char *cp) | ||
| 134 | { | ||
| 135 | while (*cp && isspace(*cp)) | ||
| 136 | cp++; | ||
| 137 | |||
| 138 | return cp; | ||
| 139 | } | ||
| 140 | |||
| 141 | static const char *skip_arg(const char *cp) | ||
| 142 | { | ||
| 143 | while (*cp && !isspace(*cp)) | ||
| 144 | cp++; | ||
| 145 | |||
| 146 | return cp; | ||
| 147 | } | ||
| 148 | |||
| 149 | static int count_argc(const char *str) | ||
| 150 | { | ||
| 151 | int count = 0; | ||
| 152 | |||
| 153 | while (*str) { | ||
| 154 | str = skip_sep(str); | ||
| 155 | if (*str) { | ||
| 156 | count++; | ||
| 157 | str = skip_arg(str); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | return count; | ||
| 162 | } | ||
| 163 | |||
| 164 | /** | ||
| 165 | * argv_free - free an argv | ||
| 166 | * @argv - the argument vector to be freed | ||
| 167 | * | ||
| 168 | * Frees an argv and the strings it points to. | ||
| 169 | */ | ||
| 170 | void argv_free(char **argv) | ||
| 171 | { | ||
| 172 | char **p; | ||
| 173 | for (p = argv; *p; p++) | ||
| 174 | free(*p); | ||
| 175 | |||
| 176 | free(argv); | ||
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * argv_split - split a string at whitespace, returning an argv | ||
| 181 | * @str: the string to be split | ||
| 182 | * @argcp: returned argument count | ||
| 183 | * | ||
| 184 | * Returns an array of pointers to strings which are split out from | ||
| 185 | * @str. This is performed by strictly splitting on white-space; no | ||
| 186 | * quote processing is performed. Multiple whitespace characters are | ||
| 187 | * considered to be a single argument separator. The returned array | ||
| 188 | * is always NULL-terminated. Returns NULL on memory allocation | ||
| 189 | * failure. | ||
| 190 | */ | ||
| 191 | char **argv_split(const char *str, int *argcp) | ||
| 192 | { | ||
| 193 | int argc = count_argc(str); | ||
| 194 | char **argv = zalloc(sizeof(*argv) * (argc+1)); | ||
| 195 | char **argvp; | ||
| 196 | |||
| 197 | if (argv == NULL) | ||
| 198 | goto out; | ||
| 199 | |||
| 200 | if (argcp) | ||
| 201 | *argcp = argc; | ||
| 202 | |||
| 203 | argvp = argv; | ||
| 204 | |||
| 205 | while (*str) { | ||
| 206 | str = skip_sep(str); | ||
| 207 | |||
| 208 | if (*str) { | ||
| 209 | const char *p = str; | ||
| 210 | char *t; | ||
| 211 | |||
| 212 | str = skip_arg(str); | ||
| 213 | |||
| 214 | t = strndup(p, str-p); | ||
| 215 | if (t == NULL) | ||
| 216 | goto fail; | ||
| 217 | *argvp++ = t; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | *argvp = NULL; | ||
| 221 | |||
| 222 | out: | ||
| 223 | return argv; | ||
| 224 | |||
| 225 | fail: | ||
| 226 | argv_free(argv); | ||
| 227 | return NULL; | ||
| 228 | } | ||
diff --git a/tools/perf/util/string.h b/tools/perf/util/string.h index bf39dfadfd24..bfecec265a1a 100644 --- a/tools/perf/util/string.h +++ b/tools/perf/util/string.h | |||
| @@ -1,11 +1,15 @@ | |||
| 1 | #ifndef _PERF_STRING_H_ | 1 | #ifndef __PERF_STRING_H_ |
| 2 | #define _PERF_STRING_H_ | 2 | #define __PERF_STRING_H_ |
| 3 | 3 | ||
| 4 | #include "types.h" | 4 | #include "types.h" |
| 5 | 5 | ||
| 6 | int hex2u64(const char *ptr, u64 *val); | 6 | int hex2u64(const char *ptr, u64 *val); |
| 7 | char *strxfrchar(char *s, char from, char to); | ||
| 8 | s64 perf_atoll(const char *str); | ||
| 9 | char **argv_split(const char *str, int *argcp); | ||
| 10 | void argv_free(char **argv); | ||
| 7 | 11 | ||
| 8 | #define _STR(x) #x | 12 | #define _STR(x) #x |
| 9 | #define STR(x) _STR(x) | 13 | #define STR(x) _STR(x) |
| 10 | 14 | ||
| 11 | #endif | 15 | #endif /* __PERF_STRING_H */ |
diff --git a/tools/perf/util/strlist.h b/tools/perf/util/strlist.h index 921818e44a54..cb4659306d7b 100644 --- a/tools/perf/util/strlist.h +++ b/tools/perf/util/strlist.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef STRLIST_H_ | 1 | #ifndef __PERF_STRLIST_H |
| 2 | #define STRLIST_H_ | 2 | #define __PERF_STRLIST_H |
| 3 | 3 | ||
| 4 | #include <linux/rbtree.h> | 4 | #include <linux/rbtree.h> |
| 5 | #include <stdbool.h> | 5 | #include <stdbool.h> |
| @@ -36,4 +36,4 @@ static inline unsigned int strlist__nr_entries(const struct strlist *self) | |||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | int strlist__parse_list(struct strlist *self, const char *s); | 38 | int strlist__parse_list(struct strlist *self, const char *s); |
| 39 | #endif /* STRLIST_H_ */ | 39 | #endif /* __PERF_STRLIST_H */ |
diff --git a/tools/perf/util/svghelper.c b/tools/perf/util/svghelper.c new file mode 100644 index 000000000000..b3637db025a2 --- /dev/null +++ b/tools/perf/util/svghelper.c | |||
| @@ -0,0 +1,500 @@ | |||
| 1 | /* | ||
| 2 | * svghelper.c - helper functions for outputting svg | ||
| 3 | * | ||
| 4 | * (C) Copyright 2009 Intel Corporation | ||
| 5 | * | ||
| 6 | * Authors: | ||
| 7 | * Arjan van de Ven <arjan@linux.intel.com> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU General Public License | ||
| 11 | * as published by the Free Software Foundation; version 2 | ||
| 12 | * of the License. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <stdio.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | #include <unistd.h> | ||
| 18 | #include <string.h> | ||
| 19 | |||
| 20 | #include "svghelper.h" | ||
| 21 | |||
| 22 | static u64 first_time, last_time; | ||
| 23 | static u64 turbo_frequency, max_freq; | ||
| 24 | |||
| 25 | |||
| 26 | #define SLOT_MULT 30.0 | ||
| 27 | #define SLOT_HEIGHT 25.0 | ||
| 28 | |||
| 29 | int svg_page_width = 1000; | ||
| 30 | |||
| 31 | #define MIN_TEXT_SIZE 0.01 | ||
| 32 | |||
| 33 | static u64 total_height; | ||
| 34 | static FILE *svgfile; | ||
| 35 | |||
| 36 | static double cpu2slot(int cpu) | ||
| 37 | { | ||
| 38 | return 2 * cpu + 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | static double cpu2y(int cpu) | ||
| 42 | { | ||
| 43 | return cpu2slot(cpu) * SLOT_MULT; | ||
| 44 | } | ||
| 45 | |||
| 46 | static double time2pixels(u64 time) | ||
| 47 | { | ||
| 48 | double X; | ||
| 49 | |||
| 50 | X = 1.0 * svg_page_width * (time - first_time) / (last_time - first_time); | ||
| 51 | return X; | ||
| 52 | } | ||
| 53 | |||
| 54 | /* | ||
| 55 | * Round text sizes so that the svg viewer only needs a discrete | ||
| 56 | * number of renderings of the font | ||
| 57 | */ | ||
| 58 | static double round_text_size(double size) | ||
| 59 | { | ||
| 60 | int loop = 100; | ||
| 61 | double target = 10.0; | ||
| 62 | |||
| 63 | if (size >= 10.0) | ||
| 64 | return size; | ||
| 65 | while (loop--) { | ||
| 66 | if (size >= target) | ||
| 67 | return target; | ||
| 68 | target = target / 2.0; | ||
| 69 | } | ||
| 70 | return size; | ||
| 71 | } | ||
| 72 | |||
| 73 | void open_svg(const char *filename, int cpus, int rows, u64 start, u64 end) | ||
| 74 | { | ||
| 75 | int new_width; | ||
| 76 | |||
| 77 | svgfile = fopen(filename, "w"); | ||
| 78 | if (!svgfile) { | ||
| 79 | fprintf(stderr, "Cannot open %s for output\n", filename); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | first_time = start; | ||
| 83 | first_time = first_time / 100000000 * 100000000; | ||
| 84 | last_time = end; | ||
| 85 | |||
| 86 | /* | ||
| 87 | * if the recording is short, we default to a width of 1000, but | ||
| 88 | * for longer recordings we want at least 200 units of width per second | ||
| 89 | */ | ||
| 90 | new_width = (last_time - first_time) / 5000000; | ||
| 91 | |||
| 92 | if (new_width > svg_page_width) | ||
| 93 | svg_page_width = new_width; | ||
| 94 | |||
| 95 | total_height = (1 + rows + cpu2slot(cpus)) * SLOT_MULT; | ||
| 96 | fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?> \n"); | ||
| 97 | fprintf(svgfile, "<svg width=\"%i\" height=\"%llu\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", svg_page_width, total_height); | ||
| 98 | |||
| 99 | fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n"); | ||
| 100 | |||
| 101 | fprintf(svgfile, " rect { stroke-width: 1; }\n"); | ||
| 102 | fprintf(svgfile, " rect.process { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1; stroke:rgb( 0, 0, 0); } \n"); | ||
| 103 | fprintf(svgfile, " rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0; stroke:rgb( 0, 0, 0); } \n"); | ||
| 104 | fprintf(svgfile, " rect.sample { fill:rgb( 0, 0,255); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n"); | ||
| 105 | fprintf(svgfile, " rect.blocked { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n"); | ||
| 106 | fprintf(svgfile, " rect.waiting { fill:rgb(224,214, 0); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n"); | ||
| 107 | fprintf(svgfile, " rect.WAITING { fill:rgb(255,214, 48); fill-opacity:0.6; stroke-width:0; stroke:rgb( 0, 0, 0); } \n"); | ||
| 108 | fprintf(svgfile, " rect.cpu { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n"); | ||
| 109 | fprintf(svgfile, " rect.pstate { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n"); | ||
| 110 | fprintf(svgfile, " rect.c1 { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n"); | ||
| 111 | fprintf(svgfile, " rect.c2 { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n"); | ||
| 112 | fprintf(svgfile, " rect.c3 { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n"); | ||
| 113 | fprintf(svgfile, " rect.c4 { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n"); | ||
| 114 | fprintf(svgfile, " rect.c5 { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n"); | ||
| 115 | fprintf(svgfile, " rect.c6 { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; } \n"); | ||
| 116 | fprintf(svgfile, " line.pstate { stroke:rgb(255,255, 0); stroke-opacity:0.8; stroke-width:2; } \n"); | ||
| 117 | |||
| 118 | fprintf(svgfile, " ]]>\n </style>\n</defs>\n"); | ||
| 119 | } | ||
| 120 | |||
| 121 | void svg_box(int Yslot, u64 start, u64 end, const char *type) | ||
| 122 | { | ||
| 123 | if (!svgfile) | ||
| 124 | return; | ||
| 125 | |||
| 126 | fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n", | ||
| 127 | time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type); | ||
| 128 | } | ||
| 129 | |||
| 130 | void svg_sample(int Yslot, int cpu, u64 start, u64 end) | ||
| 131 | { | ||
| 132 | double text_size; | ||
| 133 | if (!svgfile) | ||
| 134 | return; | ||
| 135 | |||
| 136 | fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"sample\"/>\n", | ||
| 137 | time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT); | ||
| 138 | |||
| 139 | text_size = (time2pixels(end)-time2pixels(start)); | ||
| 140 | if (cpu > 9) | ||
| 141 | text_size = text_size/2; | ||
| 142 | if (text_size > 1.25) | ||
| 143 | text_size = 1.25; | ||
| 144 | text_size = round_text_size(text_size); | ||
| 145 | |||
| 146 | if (text_size > MIN_TEXT_SIZE) | ||
| 147 | fprintf(svgfile, "<text x=\"%1.8f\" y=\"%1.8f\" font-size=\"%1.8fpt\">%i</text>\n", | ||
| 148 | time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1); | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | static char *time_to_string(u64 duration) | ||
| 153 | { | ||
| 154 | static char text[80]; | ||
| 155 | |||
| 156 | text[0] = 0; | ||
| 157 | |||
| 158 | if (duration < 1000) /* less than 1 usec */ | ||
| 159 | return text; | ||
| 160 | |||
| 161 | if (duration < 1000 * 1000) { /* less than 1 msec */ | ||
| 162 | sprintf(text, "%4.1f us", duration / 1000.0); | ||
| 163 | return text; | ||
| 164 | } | ||
| 165 | sprintf(text, "%4.1f ms", duration / 1000.0 / 1000); | ||
| 166 | |||
| 167 | return text; | ||
| 168 | } | ||
| 169 | |||
| 170 | void svg_waiting(int Yslot, u64 start, u64 end) | ||
| 171 | { | ||
| 172 | char *text; | ||
| 173 | const char *style; | ||
| 174 | double font_size; | ||
| 175 | |||
| 176 | if (!svgfile) | ||
| 177 | return; | ||
| 178 | |||
| 179 | style = "waiting"; | ||
| 180 | |||
| 181 | if (end-start > 10 * 1000000) /* 10 msec */ | ||
| 182 | style = "WAITING"; | ||
| 183 | |||
| 184 | text = time_to_string(end-start); | ||
| 185 | |||
| 186 | font_size = 1.0 * (time2pixels(end)-time2pixels(start)); | ||
| 187 | |||
| 188 | if (font_size > 3) | ||
| 189 | font_size = 3; | ||
| 190 | |||
| 191 | font_size = round_text_size(font_size); | ||
| 192 | |||
| 193 | fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), Yslot * SLOT_MULT); | ||
| 194 | fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n", | ||
| 195 | time2pixels(end)-time2pixels(start), SLOT_HEIGHT, style); | ||
| 196 | if (font_size > MIN_TEXT_SIZE) | ||
| 197 | fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%1.8fpt\"> %s</text>\n", | ||
| 198 | font_size, text); | ||
| 199 | fprintf(svgfile, "</g>\n"); | ||
| 200 | } | ||
| 201 | |||
| 202 | static char *cpu_model(void) | ||
| 203 | { | ||
| 204 | static char cpu_m[255]; | ||
| 205 | char buf[256]; | ||
| 206 | FILE *file; | ||
| 207 | |||
| 208 | cpu_m[0] = 0; | ||
| 209 | /* CPU type */ | ||
| 210 | file = fopen("/proc/cpuinfo", "r"); | ||
| 211 | if (file) { | ||
| 212 | while (fgets(buf, 255, file)) { | ||
| 213 | if (strstr(buf, "model name")) { | ||
| 214 | strncpy(cpu_m, &buf[13], 255); | ||
| 215 | break; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | fclose(file); | ||
| 219 | } | ||
| 220 | |||
| 221 | /* CPU type */ | ||
| 222 | file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies", "r"); | ||
| 223 | if (file) { | ||
| 224 | while (fgets(buf, 255, file)) { | ||
| 225 | unsigned int freq; | ||
| 226 | freq = strtoull(buf, NULL, 10); | ||
| 227 | if (freq > max_freq) | ||
| 228 | max_freq = freq; | ||
| 229 | } | ||
| 230 | fclose(file); | ||
| 231 | } | ||
| 232 | return cpu_m; | ||
| 233 | } | ||
| 234 | |||
| 235 | void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq) | ||
| 236 | { | ||
| 237 | char cpu_string[80]; | ||
| 238 | if (!svgfile) | ||
| 239 | return; | ||
| 240 | |||
| 241 | max_freq = __max_freq; | ||
| 242 | turbo_frequency = __turbo_freq; | ||
| 243 | |||
| 244 | fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n", | ||
| 245 | time2pixels(first_time), | ||
| 246 | time2pixels(last_time)-time2pixels(first_time), | ||
| 247 | cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT); | ||
| 248 | |||
| 249 | sprintf(cpu_string, "CPU %i", (int)cpu+1); | ||
| 250 | fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n", | ||
| 251 | 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string); | ||
| 252 | |||
| 253 | fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n", | ||
| 254 | 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model()); | ||
| 255 | } | ||
| 256 | |||
| 257 | void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name) | ||
| 258 | { | ||
| 259 | double width; | ||
| 260 | |||
| 261 | if (!svgfile) | ||
| 262 | return; | ||
| 263 | |||
| 264 | |||
| 265 | fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), cpu2y(cpu)); | ||
| 266 | fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n", | ||
| 267 | time2pixels(end)-time2pixels(start), SLOT_MULT+SLOT_HEIGHT, type); | ||
| 268 | width = time2pixels(end)-time2pixels(start); | ||
| 269 | if (width > 6) | ||
| 270 | width = 6; | ||
| 271 | |||
| 272 | width = round_text_size(width); | ||
| 273 | |||
| 274 | if (width > MIN_TEXT_SIZE) | ||
| 275 | fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%3.8fpt\">%s</text>\n", | ||
| 276 | width, name); | ||
| 277 | |||
| 278 | fprintf(svgfile, "</g>\n"); | ||
| 279 | } | ||
| 280 | |||
| 281 | void svg_cstate(int cpu, u64 start, u64 end, int type) | ||
| 282 | { | ||
| 283 | double width; | ||
| 284 | char style[128]; | ||
| 285 | |||
| 286 | if (!svgfile) | ||
| 287 | return; | ||
| 288 | |||
| 289 | |||
| 290 | if (type > 6) | ||
| 291 | type = 6; | ||
| 292 | sprintf(style, "c%i", type); | ||
| 293 | |||
| 294 | fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n", | ||
| 295 | style, | ||
| 296 | time2pixels(start), time2pixels(end)-time2pixels(start), | ||
| 297 | cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT); | ||
| 298 | |||
| 299 | width = (time2pixels(end)-time2pixels(start))/2.0; | ||
| 300 | if (width > 6) | ||
| 301 | width = 6; | ||
| 302 | |||
| 303 | width = round_text_size(width); | ||
| 304 | |||
| 305 | if (width > MIN_TEXT_SIZE) | ||
| 306 | fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"%3.8fpt\">C%i</text>\n", | ||
| 307 | time2pixels(start), cpu2y(cpu)+width, width, type); | ||
| 308 | } | ||
| 309 | |||
| 310 | static char *HzToHuman(unsigned long hz) | ||
| 311 | { | ||
| 312 | static char buffer[1024]; | ||
| 313 | unsigned long long Hz; | ||
| 314 | |||
| 315 | memset(buffer, 0, 1024); | ||
| 316 | |||
| 317 | Hz = hz; | ||
| 318 | |||
| 319 | /* default: just put the Number in */ | ||
| 320 | sprintf(buffer, "%9lli", Hz); | ||
| 321 | |||
| 322 | if (Hz > 1000) | ||
| 323 | sprintf(buffer, " %6lli Mhz", (Hz+500)/1000); | ||
| 324 | |||
| 325 | if (Hz > 1500000) | ||
| 326 | sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000); | ||
| 327 | |||
| 328 | if (Hz == turbo_frequency) | ||
| 329 | sprintf(buffer, "Turbo"); | ||
| 330 | |||
| 331 | return buffer; | ||
| 332 | } | ||
| 333 | |||
| 334 | void svg_pstate(int cpu, u64 start, u64 end, u64 freq) | ||
| 335 | { | ||
| 336 | double height = 0; | ||
| 337 | |||
| 338 | if (!svgfile) | ||
| 339 | return; | ||
| 340 | |||
| 341 | if (max_freq) | ||
| 342 | height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT); | ||
| 343 | height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height; | ||
| 344 | fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n", | ||
| 345 | time2pixels(start), time2pixels(end), height, height); | ||
| 346 | fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"0.25pt\">%s</text>\n", | ||
| 347 | time2pixels(start), height+0.9, HzToHuman(freq)); | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2) | ||
| 353 | { | ||
| 354 | double height; | ||
| 355 | |||
| 356 | if (!svgfile) | ||
| 357 | return; | ||
| 358 | |||
| 359 | |||
| 360 | if (row1 < row2) { | ||
| 361 | if (row1) { | ||
| 362 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n", | ||
| 363 | time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32); | ||
| 364 | if (desc2) | ||
| 365 | fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s ></text></g>\n", | ||
| 366 | time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2); | ||
| 367 | } | ||
| 368 | if (row2) { | ||
| 369 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n", | ||
| 370 | time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT); | ||
| 371 | if (desc1) | ||
| 372 | fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s ></text></g>\n", | ||
| 373 | time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1); | ||
| 374 | } | ||
| 375 | } else { | ||
| 376 | if (row2) { | ||
| 377 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n", | ||
| 378 | time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32); | ||
| 379 | if (desc1) | ||
| 380 | fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s <</text></g>\n", | ||
| 381 | time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1); | ||
| 382 | } | ||
| 383 | if (row1) { | ||
| 384 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n", | ||
| 385 | time2pixels(start), row1 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row1 * SLOT_MULT); | ||
| 386 | if (desc2) | ||
| 387 | fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s <</text></g>\n", | ||
| 388 | time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2); | ||
| 389 | } | ||
| 390 | } | ||
| 391 | height = row1 * SLOT_MULT; | ||
| 392 | if (row2 > row1) | ||
| 393 | height += SLOT_HEIGHT; | ||
| 394 | if (row1) | ||
| 395 | fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n", | ||
| 396 | time2pixels(start), height); | ||
| 397 | } | ||
| 398 | |||
| 399 | void svg_wakeline(u64 start, int row1, int row2) | ||
| 400 | { | ||
| 401 | double height; | ||
| 402 | |||
| 403 | if (!svgfile) | ||
| 404 | return; | ||
| 405 | |||
| 406 | |||
| 407 | if (row1 < row2) | ||
| 408 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n", | ||
| 409 | time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT); | ||
| 410 | else | ||
| 411 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n", | ||
| 412 | time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT); | ||
| 413 | |||
| 414 | height = row1 * SLOT_MULT; | ||
| 415 | if (row2 > row1) | ||
| 416 | height += SLOT_HEIGHT; | ||
| 417 | fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n", | ||
| 418 | time2pixels(start), height); | ||
| 419 | } | ||
| 420 | |||
| 421 | void svg_interrupt(u64 start, int row) | ||
| 422 | { | ||
| 423 | if (!svgfile) | ||
| 424 | return; | ||
| 425 | |||
| 426 | fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n", | ||
| 427 | time2pixels(start), row * SLOT_MULT); | ||
| 428 | fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n", | ||
| 429 | time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT); | ||
| 430 | } | ||
| 431 | |||
| 432 | void svg_text(int Yslot, u64 start, const char *text) | ||
| 433 | { | ||
| 434 | if (!svgfile) | ||
| 435 | return; | ||
| 436 | |||
| 437 | fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n", | ||
| 438 | time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text); | ||
| 439 | } | ||
| 440 | |||
| 441 | static void svg_legenda_box(int X, const char *text, const char *style) | ||
| 442 | { | ||
| 443 | double boxsize; | ||
| 444 | boxsize = SLOT_HEIGHT / 2; | ||
| 445 | |||
| 446 | fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n", | ||
| 447 | X, boxsize, boxsize, style); | ||
| 448 | fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.8fpt\">%s</text>\n", | ||
| 449 | X + boxsize + 5, boxsize, 0.8 * boxsize, text); | ||
| 450 | } | ||
| 451 | |||
| 452 | void svg_legenda(void) | ||
| 453 | { | ||
| 454 | if (!svgfile) | ||
| 455 | return; | ||
| 456 | |||
| 457 | svg_legenda_box(0, "Running", "sample"); | ||
| 458 | svg_legenda_box(100, "Idle","rect.c1"); | ||
| 459 | svg_legenda_box(200, "Deeper Idle", "rect.c3"); | ||
| 460 | svg_legenda_box(350, "Deepest Idle", "rect.c6"); | ||
| 461 | svg_legenda_box(550, "Sleeping", "process2"); | ||
| 462 | svg_legenda_box(650, "Waiting for cpu", "waiting"); | ||
| 463 | svg_legenda_box(800, "Blocked on IO", "blocked"); | ||
| 464 | } | ||
| 465 | |||
| 466 | void svg_time_grid(void) | ||
| 467 | { | ||
| 468 | u64 i; | ||
| 469 | |||
| 470 | if (!svgfile) | ||
| 471 | return; | ||
| 472 | |||
| 473 | i = first_time; | ||
| 474 | while (i < last_time) { | ||
| 475 | int color = 220; | ||
| 476 | double thickness = 0.075; | ||
| 477 | if ((i % 100000000) == 0) { | ||
| 478 | thickness = 0.5; | ||
| 479 | color = 192; | ||
| 480 | } | ||
| 481 | if ((i % 1000000000) == 0) { | ||
| 482 | thickness = 2.0; | ||
| 483 | color = 128; | ||
| 484 | } | ||
| 485 | |||
| 486 | fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%llu\" style=\"stroke:rgb(%i,%i,%i);stroke-width:%1.3f\"/>\n", | ||
| 487 | time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness); | ||
| 488 | |||
| 489 | i += 10000000; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | void svg_close(void) | ||
| 494 | { | ||
| 495 | if (svgfile) { | ||
| 496 | fprintf(svgfile, "</svg>\n"); | ||
| 497 | fclose(svgfile); | ||
| 498 | svgfile = NULL; | ||
| 499 | } | ||
| 500 | } | ||
diff --git a/tools/perf/util/svghelper.h b/tools/perf/util/svghelper.h new file mode 100644 index 000000000000..e0781989cc31 --- /dev/null +++ b/tools/perf/util/svghelper.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #ifndef __PERF_SVGHELPER_H | ||
| 2 | #define __PERF_SVGHELPER_H | ||
| 3 | |||
| 4 | #include "types.h" | ||
| 5 | |||
| 6 | extern void open_svg(const char *filename, int cpus, int rows, u64 start, u64 end); | ||
| 7 | extern void svg_box(int Yslot, u64 start, u64 end, const char *type); | ||
| 8 | extern void svg_sample(int Yslot, int cpu, u64 start, u64 end); | ||
| 9 | extern void svg_waiting(int Yslot, u64 start, u64 end); | ||
| 10 | extern void svg_cpu_box(int cpu, u64 max_frequency, u64 turbo_frequency); | ||
| 11 | |||
| 12 | |||
| 13 | extern void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name); | ||
| 14 | extern void svg_cstate(int cpu, u64 start, u64 end, int type); | ||
| 15 | extern void svg_pstate(int cpu, u64 start, u64 end, u64 freq); | ||
| 16 | |||
| 17 | |||
| 18 | extern void svg_time_grid(void); | ||
| 19 | extern void svg_legenda(void); | ||
| 20 | extern void svg_wakeline(u64 start, int row1, int row2); | ||
| 21 | extern void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2); | ||
| 22 | extern void svg_interrupt(u64 start, int row); | ||
| 23 | extern void svg_text(int Yslot, u64 start, const char *text); | ||
| 24 | extern void svg_close(void); | ||
| 25 | |||
| 26 | extern int svg_page_width; | ||
| 27 | |||
| 28 | #endif /* __PERF_SVGHELPER_H */ | ||
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index fd3d9c8e90fc..fffcb937cdcb 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c | |||
| @@ -2,14 +2,20 @@ | |||
| 2 | #include "../perf.h" | 2 | #include "../perf.h" |
| 3 | #include "string.h" | 3 | #include "string.h" |
| 4 | #include "symbol.h" | 4 | #include "symbol.h" |
| 5 | #include "thread.h" | ||
| 5 | 6 | ||
| 6 | #include "debug.h" | 7 | #include "debug.h" |
| 7 | 8 | ||
| 9 | #include <asm/bug.h> | ||
| 8 | #include <libelf.h> | 10 | #include <libelf.h> |
| 9 | #include <gelf.h> | 11 | #include <gelf.h> |
| 10 | #include <elf.h> | 12 | #include <elf.h> |
| 13 | #include <limits.h> | ||
| 14 | #include <sys/utsname.h> | ||
| 11 | 15 | ||
| 12 | const char *sym_hist_filter; | 16 | #ifndef NT_GNU_BUILD_ID |
| 17 | #define NT_GNU_BUILD_ID 3 | ||
| 18 | #endif | ||
| 13 | 19 | ||
| 14 | enum dso_origin { | 20 | enum dso_origin { |
| 15 | DSO__ORIG_KERNEL = 0, | 21 | DSO__ORIG_KERNEL = 0, |
| @@ -18,94 +24,189 @@ enum dso_origin { | |||
| 18 | DSO__ORIG_UBUNTU, | 24 | DSO__ORIG_UBUNTU, |
| 19 | DSO__ORIG_BUILDID, | 25 | DSO__ORIG_BUILDID, |
| 20 | DSO__ORIG_DSO, | 26 | DSO__ORIG_DSO, |
| 27 | DSO__ORIG_KMODULE, | ||
| 21 | DSO__ORIG_NOT_FOUND, | 28 | DSO__ORIG_NOT_FOUND, |
| 22 | }; | 29 | }; |
| 23 | 30 | ||
| 24 | static struct symbol *symbol__new(u64 start, u64 len, | 31 | static void dsos__add(struct list_head *head, struct dso *dso); |
| 25 | const char *name, unsigned int priv_size, | 32 | static struct map *thread__find_map_by_name(struct thread *self, char *name); |
| 26 | u64 obj_start, int v) | 33 | static struct map *map__new2(u64 start, struct dso *dso, enum map_type type); |
| 34 | struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr); | ||
| 35 | static int dso__load_kernel_sym(struct dso *self, struct map *map, | ||
| 36 | struct thread *thread, symbol_filter_t filter); | ||
| 37 | unsigned int symbol__priv_size; | ||
| 38 | static int vmlinux_path__nr_entries; | ||
| 39 | static char **vmlinux_path; | ||
| 40 | |||
| 41 | static struct symbol_conf symbol_conf__defaults = { | ||
| 42 | .use_modules = true, | ||
| 43 | .try_vmlinux_path = true, | ||
| 44 | }; | ||
| 45 | |||
| 46 | static struct thread kthread_mem; | ||
| 47 | struct thread *kthread = &kthread_mem; | ||
| 48 | |||
| 49 | bool dso__loaded(const struct dso *self, enum map_type type) | ||
| 27 | { | 50 | { |
| 28 | size_t namelen = strlen(name) + 1; | 51 | return self->loaded & (1 << type); |
| 29 | struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen); | 52 | } |
| 30 | 53 | ||
| 31 | if (!self) | 54 | static void dso__set_loaded(struct dso *self, enum map_type type) |
| 32 | return NULL; | 55 | { |
| 56 | self->loaded |= (1 << type); | ||
| 57 | } | ||
| 33 | 58 | ||
| 34 | if (v >= 2) | 59 | static void symbols__fixup_end(struct rb_root *self) |
| 35 | printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n", | 60 | { |
| 36 | (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start); | 61 | struct rb_node *nd, *prevnd = rb_first(self); |
| 62 | struct symbol *curr, *prev; | ||
| 63 | |||
| 64 | if (prevnd == NULL) | ||
| 65 | return; | ||
| 37 | 66 | ||
| 38 | self->obj_start= obj_start; | 67 | curr = rb_entry(prevnd, struct symbol, rb_node); |
| 39 | self->hist = NULL; | ||
| 40 | self->hist_sum = 0; | ||
| 41 | 68 | ||
| 42 | if (sym_hist_filter && !strcmp(name, sym_hist_filter)) | 69 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 43 | self->hist = calloc(sizeof(u64), len); | 70 | prev = curr; |
| 71 | curr = rb_entry(nd, struct symbol, rb_node); | ||
| 44 | 72 | ||
| 45 | if (priv_size) { | 73 | if (prev->end == prev->start) |
| 46 | memset(self, 0, priv_size); | 74 | prev->end = curr->start - 1; |
| 47 | self = ((void *)self) + priv_size; | ||
| 48 | } | 75 | } |
| 76 | |||
| 77 | /* Last entry */ | ||
| 78 | if (curr->end == curr->start) | ||
| 79 | curr->end = roundup(curr->start, 4096); | ||
| 80 | } | ||
| 81 | |||
| 82 | static void __thread__fixup_maps_end(struct thread *self, enum map_type type) | ||
| 83 | { | ||
| 84 | struct map *prev, *curr; | ||
| 85 | struct rb_node *nd, *prevnd = rb_first(&self->maps[type]); | ||
| 86 | |||
| 87 | if (prevnd == NULL) | ||
| 88 | return; | ||
| 89 | |||
| 90 | curr = rb_entry(prevnd, struct map, rb_node); | ||
| 91 | |||
| 92 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { | ||
| 93 | prev = curr; | ||
| 94 | curr = rb_entry(nd, struct map, rb_node); | ||
| 95 | prev->end = curr->start - 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | /* | ||
| 99 | * We still haven't the actual symbols, so guess the | ||
| 100 | * last map final address. | ||
| 101 | */ | ||
| 102 | curr->end = ~0UL; | ||
| 103 | } | ||
| 104 | |||
| 105 | static void thread__fixup_maps_end(struct thread *self) | ||
| 106 | { | ||
| 107 | int i; | ||
| 108 | for (i = 0; i < MAP__NR_TYPES; ++i) | ||
| 109 | __thread__fixup_maps_end(self, i); | ||
| 110 | } | ||
| 111 | |||
| 112 | static struct symbol *symbol__new(u64 start, u64 len, const char *name) | ||
| 113 | { | ||
| 114 | size_t namelen = strlen(name) + 1; | ||
| 115 | struct symbol *self = zalloc(symbol__priv_size + | ||
| 116 | sizeof(*self) + namelen); | ||
| 117 | if (self == NULL) | ||
| 118 | return NULL; | ||
| 119 | |||
| 120 | if (symbol__priv_size) | ||
| 121 | self = ((void *)self) + symbol__priv_size; | ||
| 122 | |||
| 49 | self->start = start; | 123 | self->start = start; |
| 50 | self->end = len ? start + len - 1 : start; | 124 | self->end = len ? start + len - 1 : start; |
| 125 | |||
| 126 | pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end); | ||
| 127 | |||
| 51 | memcpy(self->name, name, namelen); | 128 | memcpy(self->name, name, namelen); |
| 52 | 129 | ||
| 53 | return self; | 130 | return self; |
| 54 | } | 131 | } |
| 55 | 132 | ||
| 56 | static void symbol__delete(struct symbol *self, unsigned int priv_size) | 133 | static void symbol__delete(struct symbol *self) |
| 57 | { | 134 | { |
| 58 | free(((void *)self) - priv_size); | 135 | free(((void *)self) - symbol__priv_size); |
| 59 | } | 136 | } |
| 60 | 137 | ||
| 61 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) | 138 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 62 | { | 139 | { |
| 63 | if (!self->module) | 140 | return fprintf(fp, " %llx-%llx %s\n", |
| 64 | return fprintf(fp, " %llx-%llx %s\n", | ||
| 65 | self->start, self->end, self->name); | 141 | self->start, self->end, self->name); |
| 66 | else | ||
| 67 | return fprintf(fp, " %llx-%llx %s \t[%s]\n", | ||
| 68 | self->start, self->end, self->name, self->module->name); | ||
| 69 | } | 142 | } |
| 70 | 143 | ||
| 71 | struct dso *dso__new(const char *name, unsigned int sym_priv_size) | 144 | static void dso__set_long_name(struct dso *self, char *name) |
| 145 | { | ||
| 146 | if (name == NULL) | ||
| 147 | return; | ||
| 148 | self->long_name = name; | ||
| 149 | self->long_name_len = strlen(name); | ||
| 150 | } | ||
| 151 | |||
| 152 | static void dso__set_basename(struct dso *self) | ||
| 153 | { | ||
| 154 | self->short_name = basename(self->long_name); | ||
| 155 | } | ||
| 156 | |||
| 157 | struct dso *dso__new(const char *name) | ||
| 72 | { | 158 | { |
| 73 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); | 159 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 74 | 160 | ||
| 75 | if (self != NULL) { | 161 | if (self != NULL) { |
| 162 | int i; | ||
| 76 | strcpy(self->name, name); | 163 | strcpy(self->name, name); |
| 77 | self->syms = RB_ROOT; | 164 | dso__set_long_name(self, self->name); |
| 78 | self->sym_priv_size = sym_priv_size; | 165 | self->short_name = self->name; |
| 166 | for (i = 0; i < MAP__NR_TYPES; ++i) | ||
| 167 | self->symbols[i] = RB_ROOT; | ||
| 79 | self->find_symbol = dso__find_symbol; | 168 | self->find_symbol = dso__find_symbol; |
| 80 | self->slen_calculated = 0; | 169 | self->slen_calculated = 0; |
| 81 | self->origin = DSO__ORIG_NOT_FOUND; | 170 | self->origin = DSO__ORIG_NOT_FOUND; |
| 171 | self->loaded = 0; | ||
| 172 | self->has_build_id = 0; | ||
| 82 | } | 173 | } |
| 83 | 174 | ||
| 84 | return self; | 175 | return self; |
| 85 | } | 176 | } |
| 86 | 177 | ||
| 87 | static void dso__delete_symbols(struct dso *self) | 178 | static void symbols__delete(struct rb_root *self) |
| 88 | { | 179 | { |
| 89 | struct symbol *pos; | 180 | struct symbol *pos; |
| 90 | struct rb_node *next = rb_first(&self->syms); | 181 | struct rb_node *next = rb_first(self); |
| 91 | 182 | ||
| 92 | while (next) { | 183 | while (next) { |
| 93 | pos = rb_entry(next, struct symbol, rb_node); | 184 | pos = rb_entry(next, struct symbol, rb_node); |
| 94 | next = rb_next(&pos->rb_node); | 185 | next = rb_next(&pos->rb_node); |
| 95 | rb_erase(&pos->rb_node, &self->syms); | 186 | rb_erase(&pos->rb_node, self); |
| 96 | symbol__delete(pos, self->sym_priv_size); | 187 | symbol__delete(pos); |
| 97 | } | 188 | } |
| 98 | } | 189 | } |
| 99 | 190 | ||
| 100 | void dso__delete(struct dso *self) | 191 | void dso__delete(struct dso *self) |
| 101 | { | 192 | { |
| 102 | dso__delete_symbols(self); | 193 | int i; |
| 194 | for (i = 0; i < MAP__NR_TYPES; ++i) | ||
| 195 | symbols__delete(&self->symbols[i]); | ||
| 196 | if (self->long_name != self->name) | ||
| 197 | free(self->long_name); | ||
| 103 | free(self); | 198 | free(self); |
| 104 | } | 199 | } |
| 105 | 200 | ||
| 106 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) | 201 | void dso__set_build_id(struct dso *self, void *build_id) |
| 107 | { | 202 | { |
| 108 | struct rb_node **p = &self->syms.rb_node; | 203 | memcpy(self->build_id, build_id, sizeof(self->build_id)); |
| 204 | self->has_build_id = 1; | ||
| 205 | } | ||
| 206 | |||
| 207 | static void symbols__insert(struct rb_root *self, struct symbol *sym) | ||
| 208 | { | ||
| 209 | struct rb_node **p = &self->rb_node; | ||
| 109 | struct rb_node *parent = NULL; | 210 | struct rb_node *parent = NULL; |
| 110 | const u64 ip = sym->start; | 211 | const u64 ip = sym->start; |
| 111 | struct symbol *s; | 212 | struct symbol *s; |
| @@ -119,17 +220,17 @@ static void dso__insert_symbol(struct dso *self, struct symbol *sym) | |||
| 119 | p = &(*p)->rb_right; | 220 | p = &(*p)->rb_right; |
| 120 | } | 221 | } |
| 121 | rb_link_node(&sym->rb_node, parent, p); | 222 | rb_link_node(&sym->rb_node, parent, p); |
| 122 | rb_insert_color(&sym->rb_node, &self->syms); | 223 | rb_insert_color(&sym->rb_node, self); |
| 123 | } | 224 | } |
| 124 | 225 | ||
| 125 | struct symbol *dso__find_symbol(struct dso *self, u64 ip) | 226 | static struct symbol *symbols__find(struct rb_root *self, u64 ip) |
| 126 | { | 227 | { |
| 127 | struct rb_node *n; | 228 | struct rb_node *n; |
| 128 | 229 | ||
| 129 | if (self == NULL) | 230 | if (self == NULL) |
| 130 | return NULL; | 231 | return NULL; |
| 131 | 232 | ||
| 132 | n = self->syms.rb_node; | 233 | n = self->rb_node; |
| 133 | 234 | ||
| 134 | while (n) { | 235 | while (n) { |
| 135 | struct symbol *s = rb_entry(n, struct symbol, rb_node); | 236 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| @@ -145,12 +246,42 @@ struct symbol *dso__find_symbol(struct dso *self, u64 ip) | |||
| 145 | return NULL; | 246 | return NULL; |
| 146 | } | 247 | } |
| 147 | 248 | ||
| 148 | size_t dso__fprintf(struct dso *self, FILE *fp) | 249 | struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr) |
| 149 | { | 250 | { |
| 150 | size_t ret = fprintf(fp, "dso: %s\n", self->name); | 251 | return symbols__find(&self->symbols[type], addr); |
| 252 | } | ||
| 253 | |||
| 254 | int build_id__sprintf(u8 *self, int len, char *bf) | ||
| 255 | { | ||
| 256 | char *bid = bf; | ||
| 257 | u8 *raw = self; | ||
| 258 | int i; | ||
| 259 | |||
| 260 | for (i = 0; i < len; ++i) { | ||
| 261 | sprintf(bid, "%02x", *raw); | ||
| 262 | ++raw; | ||
| 263 | bid += 2; | ||
| 264 | } | ||
| 265 | |||
| 266 | return raw - self; | ||
| 267 | } | ||
| 268 | |||
| 269 | size_t dso__fprintf_buildid(struct dso *self, FILE *fp) | ||
| 270 | { | ||
| 271 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | ||
| 272 | |||
| 273 | build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id); | ||
| 274 | return fprintf(fp, "%s", sbuild_id); | ||
| 275 | } | ||
| 151 | 276 | ||
| 277 | size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp) | ||
| 278 | { | ||
| 152 | struct rb_node *nd; | 279 | struct rb_node *nd; |
| 153 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { | 280 | size_t ret = fprintf(fp, "dso: %s (", self->short_name); |
| 281 | |||
| 282 | ret += dso__fprintf_buildid(self, fp); | ||
| 283 | ret += fprintf(fp, ")\n"); | ||
| 284 | for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) { | ||
| 154 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | 285 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 155 | ret += symbol__fprintf(pos, fp); | 286 | ret += symbol__fprintf(pos, fp); |
| 156 | } | 287 | } |
| @@ -158,13 +289,17 @@ size_t dso__fprintf(struct dso *self, FILE *fp) | |||
| 158 | return ret; | 289 | return ret; |
| 159 | } | 290 | } |
| 160 | 291 | ||
| 161 | static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v) | 292 | /* |
| 293 | * Loads the function entries in /proc/kallsyms into kernel_map->dso, | ||
| 294 | * so that we can in the next step set the symbol ->end address and then | ||
| 295 | * call kernel_maps__split_kallsyms. | ||
| 296 | */ | ||
| 297 | static int dso__load_all_kallsyms(struct dso *self, struct map *map) | ||
| 162 | { | 298 | { |
| 163 | struct rb_node *nd, *prevnd; | ||
| 164 | char *line = NULL; | 299 | char *line = NULL; |
| 165 | size_t n; | 300 | size_t n; |
| 301 | struct rb_root *root = &self->symbols[map->type]; | ||
| 166 | FILE *file = fopen("/proc/kallsyms", "r"); | 302 | FILE *file = fopen("/proc/kallsyms", "r"); |
| 167 | int count = 0; | ||
| 168 | 303 | ||
| 169 | if (file == NULL) | 304 | if (file == NULL) |
| 170 | goto out_failure; | 305 | goto out_failure; |
| @@ -174,6 +309,7 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v) | |||
| 174 | struct symbol *sym; | 309 | struct symbol *sym; |
| 175 | int line_len, len; | 310 | int line_len, len; |
| 176 | char symbol_type; | 311 | char symbol_type; |
| 312 | char *symbol_name; | ||
| 177 | 313 | ||
| 178 | line_len = getline(&line, &n, file); | 314 | line_len = getline(&line, &n, file); |
| 179 | if (line_len < 0) | 315 | if (line_len < 0) |
| @@ -196,44 +332,26 @@ static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v) | |||
| 196 | */ | 332 | */ |
| 197 | if (symbol_type != 'T' && symbol_type != 'W') | 333 | if (symbol_type != 'T' && symbol_type != 'W') |
| 198 | continue; | 334 | continue; |
| 335 | |||
| 336 | symbol_name = line + len + 2; | ||
| 199 | /* | 337 | /* |
| 200 | * Well fix up the end later, when we have all sorted. | 338 | * Will fix up the end later, when we have all symbols sorted. |
| 201 | */ | 339 | */ |
| 202 | sym = symbol__new(start, 0xdead, line + len + 2, | 340 | sym = symbol__new(start, 0, symbol_name); |
| 203 | self->sym_priv_size, 0, v); | ||
| 204 | 341 | ||
| 205 | if (sym == NULL) | 342 | if (sym == NULL) |
| 206 | goto out_delete_line; | 343 | goto out_delete_line; |
| 207 | 344 | /* | |
| 208 | if (filter && filter(self, sym)) | 345 | * We will pass the symbols to the filter later, in |
| 209 | symbol__delete(sym, self->sym_priv_size); | 346 | * map__split_kallsyms, when we have split the maps per module |
| 210 | else { | 347 | */ |
| 211 | dso__insert_symbol(self, sym); | 348 | symbols__insert(root, sym); |
| 212 | count++; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | /* | ||
| 217 | * Now that we have all sorted out, just set the ->end of all | ||
| 218 | * symbols | ||
| 219 | */ | ||
| 220 | prevnd = rb_first(&self->syms); | ||
| 221 | |||
| 222 | if (prevnd == NULL) | ||
| 223 | goto out_delete_line; | ||
| 224 | |||
| 225 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { | ||
| 226 | struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node), | ||
| 227 | *curr = rb_entry(nd, struct symbol, rb_node); | ||
| 228 | |||
| 229 | prev->end = curr->start - 1; | ||
| 230 | prevnd = nd; | ||
| 231 | } | 349 | } |
| 232 | 350 | ||
| 233 | free(line); | 351 | free(line); |
| 234 | fclose(file); | 352 | fclose(file); |
| 235 | 353 | ||
| 236 | return count; | 354 | return 0; |
| 237 | 355 | ||
| 238 | out_delete_line: | 356 | out_delete_line: |
| 239 | free(line); | 357 | free(line); |
| @@ -241,14 +359,114 @@ out_failure: | |||
| 241 | return -1; | 359 | return -1; |
| 242 | } | 360 | } |
| 243 | 361 | ||
| 244 | static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int v) | 362 | /* |
| 363 | * Split the symbols into maps, making sure there are no overlaps, i.e. the | ||
| 364 | * kernel range is broken in several maps, named [kernel].N, as we don't have | ||
| 365 | * the original ELF section names vmlinux have. | ||
| 366 | */ | ||
| 367 | static int dso__split_kallsyms(struct dso *self, struct map *map, struct thread *thread, | ||
| 368 | symbol_filter_t filter) | ||
| 369 | { | ||
| 370 | struct map *curr_map = map; | ||
| 371 | struct symbol *pos; | ||
| 372 | int count = 0; | ||
| 373 | struct rb_root *root = &self->symbols[map->type]; | ||
| 374 | struct rb_node *next = rb_first(root); | ||
| 375 | int kernel_range = 0; | ||
| 376 | |||
| 377 | while (next) { | ||
| 378 | char *module; | ||
| 379 | |||
| 380 | pos = rb_entry(next, struct symbol, rb_node); | ||
| 381 | next = rb_next(&pos->rb_node); | ||
| 382 | |||
| 383 | module = strchr(pos->name, '\t'); | ||
| 384 | if (module) { | ||
| 385 | if (!thread->use_modules) | ||
| 386 | goto discard_symbol; | ||
| 387 | |||
| 388 | *module++ = '\0'; | ||
| 389 | |||
| 390 | if (strcmp(self->name, module)) { | ||
| 391 | curr_map = thread__find_map_by_name(thread, module); | ||
| 392 | if (curr_map == NULL) { | ||
| 393 | pr_debug("/proc/{kallsyms,modules} " | ||
| 394 | "inconsistency!\n"); | ||
| 395 | return -1; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | /* | ||
| 399 | * So that we look just like we get from .ko files, | ||
| 400 | * i.e. not prelinked, relative to map->start. | ||
| 401 | */ | ||
| 402 | pos->start = curr_map->map_ip(curr_map, pos->start); | ||
| 403 | pos->end = curr_map->map_ip(curr_map, pos->end); | ||
| 404 | } else if (curr_map != map) { | ||
| 405 | char dso_name[PATH_MAX]; | ||
| 406 | struct dso *dso; | ||
| 407 | |||
| 408 | snprintf(dso_name, sizeof(dso_name), "[kernel].%d", | ||
| 409 | kernel_range++); | ||
| 410 | |||
| 411 | dso = dso__new(dso_name); | ||
| 412 | if (dso == NULL) | ||
| 413 | return -1; | ||
| 414 | |||
| 415 | curr_map = map__new2(pos->start, dso, map->type); | ||
| 416 | if (map == NULL) { | ||
| 417 | dso__delete(dso); | ||
| 418 | return -1; | ||
| 419 | } | ||
| 420 | |||
| 421 | curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; | ||
| 422 | __thread__insert_map(thread, curr_map); | ||
| 423 | ++kernel_range; | ||
| 424 | } | ||
| 425 | |||
| 426 | if (filter && filter(curr_map, pos)) { | ||
| 427 | discard_symbol: rb_erase(&pos->rb_node, root); | ||
| 428 | symbol__delete(pos); | ||
| 429 | } else { | ||
| 430 | if (curr_map != map) { | ||
| 431 | rb_erase(&pos->rb_node, root); | ||
| 432 | symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); | ||
| 433 | } | ||
| 434 | count++; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | return count; | ||
| 439 | } | ||
| 440 | |||
| 441 | |||
| 442 | static int dso__load_kallsyms(struct dso *self, struct map *map, | ||
| 443 | struct thread *thread, symbol_filter_t filter) | ||
| 444 | { | ||
| 445 | if (dso__load_all_kallsyms(self, map) < 0) | ||
| 446 | return -1; | ||
| 447 | |||
| 448 | symbols__fixup_end(&self->symbols[map->type]); | ||
| 449 | self->origin = DSO__ORIG_KERNEL; | ||
| 450 | |||
| 451 | return dso__split_kallsyms(self, map, thread, filter); | ||
| 452 | } | ||
| 453 | |||
| 454 | size_t kernel_maps__fprintf(FILE *fp) | ||
| 455 | { | ||
| 456 | size_t printed = fprintf(fp, "Kernel maps:\n"); | ||
| 457 | printed += thread__fprintf_maps(kthread, fp); | ||
| 458 | return printed + fprintf(fp, "END kernel maps\n"); | ||
| 459 | } | ||
| 460 | |||
| 461 | static int dso__load_perf_map(struct dso *self, struct map *map, | ||
| 462 | symbol_filter_t filter) | ||
| 245 | { | 463 | { |
| 246 | char *line = NULL; | 464 | char *line = NULL; |
| 247 | size_t n; | 465 | size_t n; |
| 248 | FILE *file; | 466 | FILE *file; |
| 249 | int nr_syms = 0; | 467 | int nr_syms = 0; |
| 250 | 468 | ||
| 251 | file = fopen(self->name, "r"); | 469 | file = fopen(self->long_name, "r"); |
| 252 | if (file == NULL) | 470 | if (file == NULL) |
| 253 | goto out_failure; | 471 | goto out_failure; |
| 254 | 472 | ||
| @@ -278,16 +496,15 @@ static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int v) | |||
| 278 | if (len + 2 >= line_len) | 496 | if (len + 2 >= line_len) |
| 279 | continue; | 497 | continue; |
| 280 | 498 | ||
| 281 | sym = symbol__new(start, size, line + len, | 499 | sym = symbol__new(start, size, line + len); |
| 282 | self->sym_priv_size, start, v); | ||
| 283 | 500 | ||
| 284 | if (sym == NULL) | 501 | if (sym == NULL) |
| 285 | goto out_delete_line; | 502 | goto out_delete_line; |
| 286 | 503 | ||
| 287 | if (filter && filter(self, sym)) | 504 | if (filter && filter(map, sym)) |
| 288 | symbol__delete(sym, self->sym_priv_size); | 505 | symbol__delete(sym); |
| 289 | else { | 506 | else { |
| 290 | dso__insert_symbol(self, sym); | 507 | symbols__insert(&self->symbols[map->type], sym); |
| 291 | nr_syms++; | 508 | nr_syms++; |
| 292 | } | 509 | } |
| 293 | } | 510 | } |
| @@ -324,8 +541,7 @@ static inline int elf_sym__is_function(const GElf_Sym *sym) | |||
| 324 | { | 541 | { |
| 325 | return elf_sym__type(sym) == STT_FUNC && | 542 | return elf_sym__type(sym) == STT_FUNC && |
| 326 | sym->st_name != 0 && | 543 | sym->st_name != 0 && |
| 327 | sym->st_shndx != SHN_UNDEF && | 544 | sym->st_shndx != SHN_UNDEF; |
| 328 | sym->st_size != 0; | ||
| 329 | } | 545 | } |
| 330 | 546 | ||
| 331 | static inline int elf_sym__is_label(const GElf_Sym *sym) | 547 | static inline int elf_sym__is_label(const GElf_Sym *sym) |
| @@ -394,7 +610,8 @@ static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, | |||
| 394 | * And always look at the original dso, not at debuginfo packages, that | 610 | * And always look at the original dso, not at debuginfo packages, that |
| 395 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). | 611 | * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). |
| 396 | */ | 612 | */ |
| 397 | static int dso__synthesize_plt_symbols(struct dso *self, int v) | 613 | static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, |
| 614 | symbol_filter_t filter) | ||
| 398 | { | 615 | { |
| 399 | uint32_t nr_rel_entries, idx; | 616 | uint32_t nr_rel_entries, idx; |
| 400 | GElf_Sym sym; | 617 | GElf_Sym sym; |
| @@ -410,11 +627,11 @@ static int dso__synthesize_plt_symbols(struct dso *self, int v) | |||
| 410 | Elf *elf; | 627 | Elf *elf; |
| 411 | int nr = 0, symidx, fd, err = 0; | 628 | int nr = 0, symidx, fd, err = 0; |
| 412 | 629 | ||
| 413 | fd = open(self->name, O_RDONLY); | 630 | fd = open(self->long_name, O_RDONLY); |
| 414 | if (fd < 0) | 631 | if (fd < 0) |
| 415 | goto out; | 632 | goto out; |
| 416 | 633 | ||
| 417 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); | 634 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 418 | if (elf == NULL) | 635 | if (elf == NULL) |
| 419 | goto out_close; | 636 | goto out_close; |
| 420 | 637 | ||
| @@ -478,12 +695,16 @@ static int dso__synthesize_plt_symbols(struct dso *self, int v) | |||
| 478 | "%s@plt", elf_sym__name(&sym, symstrs)); | 695 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 479 | 696 | ||
| 480 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | 697 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 481 | sympltname, self->sym_priv_size, 0, v); | 698 | sympltname); |
| 482 | if (!f) | 699 | if (!f) |
| 483 | goto out_elf_end; | 700 | goto out_elf_end; |
| 484 | 701 | ||
| 485 | dso__insert_symbol(self, f); | 702 | if (filter && filter(map, f)) |
| 486 | ++nr; | 703 | symbol__delete(f); |
| 704 | else { | ||
| 705 | symbols__insert(&self->symbols[map->type], f); | ||
| 706 | ++nr; | ||
| 707 | } | ||
| 487 | } | 708 | } |
| 488 | } else if (shdr_rel_plt.sh_type == SHT_REL) { | 709 | } else if (shdr_rel_plt.sh_type == SHT_REL) { |
| 489 | GElf_Rel pos_mem, *pos; | 710 | GElf_Rel pos_mem, *pos; |
| @@ -496,12 +717,16 @@ static int dso__synthesize_plt_symbols(struct dso *self, int v) | |||
| 496 | "%s@plt", elf_sym__name(&sym, symstrs)); | 717 | "%s@plt", elf_sym__name(&sym, symstrs)); |
| 497 | 718 | ||
| 498 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, | 719 | f = symbol__new(plt_offset, shdr_plt.sh_entsize, |
| 499 | sympltname, self->sym_priv_size, 0, v); | 720 | sympltname); |
| 500 | if (!f) | 721 | if (!f) |
| 501 | goto out_elf_end; | 722 | goto out_elf_end; |
| 502 | 723 | ||
| 503 | dso__insert_symbol(self, f); | 724 | if (filter && filter(map, f)) |
| 504 | ++nr; | 725 | symbol__delete(f); |
| 726 | else { | ||
| 727 | symbols__insert(&self->symbols[map->type], f); | ||
| 728 | ++nr; | ||
| 729 | } | ||
| 505 | } | 730 | } |
| 506 | } | 731 | } |
| 507 | 732 | ||
| @@ -514,14 +739,18 @@ out_close: | |||
| 514 | if (err == 0) | 739 | if (err == 0) |
| 515 | return nr; | 740 | return nr; |
| 516 | out: | 741 | out: |
| 517 | fprintf(stderr, "%s: problems reading %s PLT info.\n", | 742 | pr_warning("%s: problems reading %s PLT info.\n", |
| 518 | __func__, self->name); | 743 | __func__, self->long_name); |
| 519 | return 0; | 744 | return 0; |
| 520 | } | 745 | } |
| 521 | 746 | ||
| 522 | static int dso__load_sym(struct dso *self, int fd, const char *name, | 747 | static int dso__load_sym(struct dso *self, struct map *map, |
| 523 | symbol_filter_t filter, int v, struct module *mod) | 748 | struct thread *thread, const char *name, int fd, |
| 749 | symbol_filter_t filter, int kernel, int kmodule) | ||
| 524 | { | 750 | { |
| 751 | struct map *curr_map = map; | ||
| 752 | struct dso *curr_dso = self; | ||
| 753 | size_t dso_name_len = strlen(self->short_name); | ||
| 525 | Elf_Data *symstrs, *secstrs; | 754 | Elf_Data *symstrs, *secstrs; |
| 526 | uint32_t nr_syms; | 755 | uint32_t nr_syms; |
| 527 | int err = -1; | 756 | int err = -1; |
| @@ -532,19 +761,16 @@ static int dso__load_sym(struct dso *self, int fd, const char *name, | |||
| 532 | GElf_Sym sym; | 761 | GElf_Sym sym; |
| 533 | Elf_Scn *sec, *sec_strndx; | 762 | Elf_Scn *sec, *sec_strndx; |
| 534 | Elf *elf; | 763 | Elf *elf; |
| 535 | int nr = 0, kernel = !strcmp("[kernel]", self->name); | 764 | int nr = 0; |
| 536 | 765 | ||
| 537 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); | 766 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 538 | if (elf == NULL) { | 767 | if (elf == NULL) { |
| 539 | if (v) | 768 | pr_err("%s: cannot read %s ELF file.\n", __func__, name); |
| 540 | fprintf(stderr, "%s: cannot read %s ELF file.\n", | ||
| 541 | __func__, name); | ||
| 542 | goto out_close; | 769 | goto out_close; |
| 543 | } | 770 | } |
| 544 | 771 | ||
| 545 | if (gelf_getehdr(elf, &ehdr) == NULL) { | 772 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 546 | if (v) | 773 | pr_err("%s: cannot get elf header.\n", __func__); |
| 547 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); | ||
| 548 | goto out_elf_end; | 774 | goto out_elf_end; |
| 549 | } | 775 | } |
| 550 | 776 | ||
| @@ -588,9 +814,7 @@ static int dso__load_sym(struct dso *self, int fd, const char *name, | |||
| 588 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { | 814 | elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { |
| 589 | struct symbol *f; | 815 | struct symbol *f; |
| 590 | const char *elf_name; | 816 | const char *elf_name; |
| 591 | char *demangled; | 817 | char *demangled = NULL; |
| 592 | u64 obj_start; | ||
| 593 | struct section *section = NULL; | ||
| 594 | int is_label = elf_sym__is_label(&sym); | 818 | int is_label = elf_sym__is_label(&sym); |
| 595 | const char *section_name; | 819 | const char *section_name; |
| 596 | 820 | ||
| @@ -606,52 +830,85 @@ static int dso__load_sym(struct dso *self, int fd, const char *name, | |||
| 606 | if (is_label && !elf_sec__is_text(&shdr, secstrs)) | 830 | if (is_label && !elf_sec__is_text(&shdr, secstrs)) |
| 607 | continue; | 831 | continue; |
| 608 | 832 | ||
| 833 | elf_name = elf_sym__name(&sym, symstrs); | ||
| 609 | section_name = elf_sec__name(&shdr, secstrs); | 834 | section_name = elf_sec__name(&shdr, secstrs); |
| 610 | obj_start = sym.st_value; | ||
| 611 | 835 | ||
| 612 | if (self->adjust_symbols) { | 836 | if (kernel || kmodule) { |
| 613 | if (v >= 2) | 837 | char dso_name[PATH_MAX]; |
| 614 | printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n", | ||
| 615 | (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset); | ||
| 616 | 838 | ||
| 617 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; | 839 | if (strcmp(section_name, |
| 618 | } | 840 | curr_dso->short_name + dso_name_len) == 0) |
| 841 | goto new_symbol; | ||
| 619 | 842 | ||
| 620 | if (mod) { | 843 | if (strcmp(section_name, ".text") == 0) { |
| 621 | section = mod->sections->find_section(mod->sections, section_name); | 844 | curr_map = map; |
| 622 | if (section) | 845 | curr_dso = self; |
| 623 | sym.st_value += section->vma; | 846 | goto new_symbol; |
| 624 | else { | ||
| 625 | fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n", | ||
| 626 | mod->name, section_name); | ||
| 627 | goto out_elf_end; | ||
| 628 | } | 847 | } |
| 848 | |||
| 849 | snprintf(dso_name, sizeof(dso_name), | ||
| 850 | "%s%s", self->short_name, section_name); | ||
| 851 | |||
| 852 | curr_map = thread__find_map_by_name(thread, dso_name); | ||
| 853 | if (curr_map == NULL) { | ||
| 854 | u64 start = sym.st_value; | ||
| 855 | |||
| 856 | if (kmodule) | ||
| 857 | start += map->start + shdr.sh_offset; | ||
| 858 | |||
| 859 | curr_dso = dso__new(dso_name); | ||
| 860 | if (curr_dso == NULL) | ||
| 861 | goto out_elf_end; | ||
| 862 | curr_map = map__new2(start, curr_dso, | ||
| 863 | MAP__FUNCTION); | ||
| 864 | if (curr_map == NULL) { | ||
| 865 | dso__delete(curr_dso); | ||
| 866 | goto out_elf_end; | ||
| 867 | } | ||
| 868 | curr_map->map_ip = identity__map_ip; | ||
| 869 | curr_map->unmap_ip = identity__map_ip; | ||
| 870 | curr_dso->origin = DSO__ORIG_KERNEL; | ||
| 871 | __thread__insert_map(kthread, curr_map); | ||
| 872 | dsos__add(&dsos__kernel, curr_dso); | ||
| 873 | } else | ||
| 874 | curr_dso = curr_map->dso; | ||
| 875 | |||
| 876 | goto new_symbol; | ||
| 877 | } | ||
| 878 | |||
| 879 | if (curr_dso->adjust_symbols) { | ||
| 880 | pr_debug2("adjusting symbol: st_value: %Lx sh_addr: " | ||
| 881 | "%Lx sh_offset: %Lx\n", (u64)sym.st_value, | ||
| 882 | (u64)shdr.sh_addr, (u64)shdr.sh_offset); | ||
| 883 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; | ||
| 629 | } | 884 | } |
| 630 | /* | 885 | /* |
| 631 | * We need to figure out if the object was created from C++ sources | 886 | * We need to figure out if the object was created from C++ sources |
| 632 | * DWARF DW_compile_unit has this, but we don't always have access | 887 | * DWARF DW_compile_unit has this, but we don't always have access |
| 633 | * to it... | 888 | * to it... |
| 634 | */ | 889 | */ |
| 635 | elf_name = elf_sym__name(&sym, symstrs); | ||
| 636 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); | 890 | demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); |
| 637 | if (demangled != NULL) | 891 | if (demangled != NULL) |
| 638 | elf_name = demangled; | 892 | elf_name = demangled; |
| 639 | 893 | new_symbol: | |
| 640 | f = symbol__new(sym.st_value, sym.st_size, elf_name, | 894 | f = symbol__new(sym.st_value, sym.st_size, elf_name); |
| 641 | self->sym_priv_size, obj_start, v); | ||
| 642 | free(demangled); | 895 | free(demangled); |
| 643 | if (!f) | 896 | if (!f) |
| 644 | goto out_elf_end; | 897 | goto out_elf_end; |
| 645 | 898 | ||
| 646 | if (filter && filter(self, f)) | 899 | if (filter && filter(curr_map, f)) |
| 647 | symbol__delete(f, self->sym_priv_size); | 900 | symbol__delete(f); |
| 648 | else { | 901 | else { |
| 649 | f->module = mod; | 902 | symbols__insert(&curr_dso->symbols[curr_map->type], f); |
| 650 | dso__insert_symbol(self, f); | ||
| 651 | nr++; | 903 | nr++; |
| 652 | } | 904 | } |
| 653 | } | 905 | } |
| 654 | 906 | ||
| 907 | /* | ||
| 908 | * For misannotated, zeroed, ASM function sizes. | ||
| 909 | */ | ||
| 910 | if (nr > 0) | ||
| 911 | symbols__fixup_end(&self->symbols[map->type]); | ||
| 655 | err = nr; | 912 | err = nr; |
| 656 | out_elf_end: | 913 | out_elf_end: |
| 657 | elf_end(elf); | 914 | elf_end(elf); |
| @@ -659,63 +916,153 @@ out_close: | |||
| 659 | return err; | 916 | return err; |
| 660 | } | 917 | } |
| 661 | 918 | ||
| 662 | #define BUILD_ID_SIZE 128 | 919 | static bool dso__build_id_equal(const struct dso *self, u8 *build_id) |
| 920 | { | ||
| 921 | return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0; | ||
| 922 | } | ||
| 663 | 923 | ||
| 664 | static char *dso__read_build_id(struct dso *self, int v) | 924 | static bool __dsos__read_build_ids(struct list_head *head) |
| 665 | { | 925 | { |
| 666 | int i; | 926 | bool have_build_id = false; |
| 927 | struct dso *pos; | ||
| 928 | |||
| 929 | list_for_each_entry(pos, head, node) | ||
| 930 | if (filename__read_build_id(pos->long_name, pos->build_id, | ||
| 931 | sizeof(pos->build_id)) > 0) { | ||
| 932 | have_build_id = true; | ||
| 933 | pos->has_build_id = true; | ||
| 934 | } | ||
| 935 | |||
| 936 | return have_build_id; | ||
| 937 | } | ||
| 938 | |||
| 939 | bool dsos__read_build_ids(void) | ||
| 940 | { | ||
| 941 | return __dsos__read_build_ids(&dsos__kernel) || | ||
| 942 | __dsos__read_build_ids(&dsos__user); | ||
| 943 | } | ||
| 944 | |||
| 945 | /* | ||
| 946 | * Align offset to 4 bytes as needed for note name and descriptor data. | ||
| 947 | */ | ||
| 948 | #define NOTE_ALIGN(n) (((n) + 3) & -4U) | ||
| 949 | |||
| 950 | int filename__read_build_id(const char *filename, void *bf, size_t size) | ||
| 951 | { | ||
| 952 | int fd, err = -1; | ||
| 667 | GElf_Ehdr ehdr; | 953 | GElf_Ehdr ehdr; |
| 668 | GElf_Shdr shdr; | 954 | GElf_Shdr shdr; |
| 669 | Elf_Data *build_id_data; | 955 | Elf_Data *data; |
| 670 | Elf_Scn *sec; | 956 | Elf_Scn *sec; |
| 671 | char *build_id = NULL, *bid; | 957 | Elf_Kind ek; |
| 672 | unsigned char *raw; | 958 | void *ptr; |
| 673 | Elf *elf; | 959 | Elf *elf; |
| 674 | int fd = open(self->name, O_RDONLY); | ||
| 675 | 960 | ||
| 961 | if (size < BUILD_ID_SIZE) | ||
| 962 | goto out; | ||
| 963 | |||
| 964 | fd = open(filename, O_RDONLY); | ||
| 676 | if (fd < 0) | 965 | if (fd < 0) |
| 677 | goto out; | 966 | goto out; |
| 678 | 967 | ||
| 679 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); | 968 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 680 | if (elf == NULL) { | 969 | if (elf == NULL) { |
| 681 | if (v) | 970 | pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); |
| 682 | fprintf(stderr, "%s: cannot read %s ELF file.\n", | ||
| 683 | __func__, self->name); | ||
| 684 | goto out_close; | 971 | goto out_close; |
| 685 | } | 972 | } |
| 686 | 973 | ||
| 974 | ek = elf_kind(elf); | ||
| 975 | if (ek != ELF_K_ELF) | ||
| 976 | goto out_elf_end; | ||
| 977 | |||
| 687 | if (gelf_getehdr(elf, &ehdr) == NULL) { | 978 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 688 | if (v) | 979 | pr_err("%s: cannot get elf header.\n", __func__); |
| 689 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); | ||
| 690 | goto out_elf_end; | 980 | goto out_elf_end; |
| 691 | } | 981 | } |
| 692 | 982 | ||
| 693 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL); | 983 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 694 | if (sec == NULL) | 984 | ".note.gnu.build-id", NULL); |
| 695 | goto out_elf_end; | 985 | if (sec == NULL) { |
| 986 | sec = elf_section_by_name(elf, &ehdr, &shdr, | ||
| 987 | ".notes", NULL); | ||
| 988 | if (sec == NULL) | ||
| 989 | goto out_elf_end; | ||
| 990 | } | ||
| 696 | 991 | ||
| 697 | build_id_data = elf_getdata(sec, NULL); | 992 | data = elf_getdata(sec, NULL); |
| 698 | if (build_id_data == NULL) | 993 | if (data == NULL) |
| 699 | goto out_elf_end; | 994 | goto out_elf_end; |
| 700 | build_id = malloc(BUILD_ID_SIZE); | ||
| 701 | if (build_id == NULL) | ||
| 702 | goto out_elf_end; | ||
| 703 | raw = build_id_data->d_buf + 16; | ||
| 704 | bid = build_id; | ||
| 705 | 995 | ||
| 706 | for (i = 0; i < 20; ++i) { | 996 | ptr = data->d_buf; |
| 707 | sprintf(bid, "%02x", *raw); | 997 | while (ptr < (data->d_buf + data->d_size)) { |
| 708 | ++raw; | 998 | GElf_Nhdr *nhdr = ptr; |
| 709 | bid += 2; | 999 | int namesz = NOTE_ALIGN(nhdr->n_namesz), |
| 1000 | descsz = NOTE_ALIGN(nhdr->n_descsz); | ||
| 1001 | const char *name; | ||
| 1002 | |||
| 1003 | ptr += sizeof(*nhdr); | ||
| 1004 | name = ptr; | ||
| 1005 | ptr += namesz; | ||
| 1006 | if (nhdr->n_type == NT_GNU_BUILD_ID && | ||
| 1007 | nhdr->n_namesz == sizeof("GNU")) { | ||
| 1008 | if (memcmp(name, "GNU", sizeof("GNU")) == 0) { | ||
| 1009 | memcpy(bf, ptr, BUILD_ID_SIZE); | ||
| 1010 | err = BUILD_ID_SIZE; | ||
| 1011 | break; | ||
| 1012 | } | ||
| 1013 | } | ||
| 1014 | ptr += descsz; | ||
| 710 | } | 1015 | } |
| 711 | if (v >= 2) | ||
| 712 | printf("%s(%s): %s\n", __func__, self->name, build_id); | ||
| 713 | out_elf_end: | 1016 | out_elf_end: |
| 714 | elf_end(elf); | 1017 | elf_end(elf); |
| 715 | out_close: | 1018 | out_close: |
| 716 | close(fd); | 1019 | close(fd); |
| 717 | out: | 1020 | out: |
| 718 | return build_id; | 1021 | return err; |
| 1022 | } | ||
| 1023 | |||
| 1024 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) | ||
| 1025 | { | ||
| 1026 | int fd, err = -1; | ||
| 1027 | |||
| 1028 | if (size < BUILD_ID_SIZE) | ||
| 1029 | goto out; | ||
| 1030 | |||
| 1031 | fd = open(filename, O_RDONLY); | ||
| 1032 | if (fd < 0) | ||
| 1033 | goto out; | ||
| 1034 | |||
| 1035 | while (1) { | ||
| 1036 | char bf[BUFSIZ]; | ||
| 1037 | GElf_Nhdr nhdr; | ||
| 1038 | int namesz, descsz; | ||
| 1039 | |||
| 1040 | if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) | ||
| 1041 | break; | ||
| 1042 | |||
| 1043 | namesz = NOTE_ALIGN(nhdr.n_namesz); | ||
| 1044 | descsz = NOTE_ALIGN(nhdr.n_descsz); | ||
| 1045 | if (nhdr.n_type == NT_GNU_BUILD_ID && | ||
| 1046 | nhdr.n_namesz == sizeof("GNU")) { | ||
| 1047 | if (read(fd, bf, namesz) != namesz) | ||
| 1048 | break; | ||
| 1049 | if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { | ||
| 1050 | if (read(fd, build_id, | ||
| 1051 | BUILD_ID_SIZE) == BUILD_ID_SIZE) { | ||
| 1052 | err = 0; | ||
| 1053 | break; | ||
| 1054 | } | ||
| 1055 | } else if (read(fd, bf, descsz) != descsz) | ||
| 1056 | break; | ||
| 1057 | } else { | ||
| 1058 | int n = namesz + descsz; | ||
| 1059 | if (read(fd, bf, n) != n) | ||
| 1060 | break; | ||
| 1061 | } | ||
| 1062 | } | ||
| 1063 | close(fd); | ||
| 1064 | out: | ||
| 1065 | return err; | ||
| 719 | } | 1066 | } |
| 720 | 1067 | ||
| 721 | char dso__symtab_origin(const struct dso *self) | 1068 | char dso__symtab_origin(const struct dso *self) |
| @@ -727,6 +1074,7 @@ char dso__symtab_origin(const struct dso *self) | |||
| 727 | [DSO__ORIG_UBUNTU] = 'u', | 1074 | [DSO__ORIG_UBUNTU] = 'u', |
| 728 | [DSO__ORIG_BUILDID] = 'b', | 1075 | [DSO__ORIG_BUILDID] = 'b', |
| 729 | [DSO__ORIG_DSO] = 'd', | 1076 | [DSO__ORIG_DSO] = 'd', |
| 1077 | [DSO__ORIG_KMODULE] = 'K', | ||
| 730 | }; | 1078 | }; |
| 731 | 1079 | ||
| 732 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) | 1080 | if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND) |
| @@ -734,20 +1082,27 @@ char dso__symtab_origin(const struct dso *self) | |||
| 734 | return origin[self->origin]; | 1082 | return origin[self->origin]; |
| 735 | } | 1083 | } |
| 736 | 1084 | ||
| 737 | int dso__load(struct dso *self, symbol_filter_t filter, int v) | 1085 | int dso__load(struct dso *self, struct map *map, symbol_filter_t filter) |
| 738 | { | 1086 | { |
| 739 | int size = PATH_MAX; | 1087 | int size = PATH_MAX; |
| 740 | char *name = malloc(size), *build_id = NULL; | 1088 | char *name; |
| 1089 | u8 build_id[BUILD_ID_SIZE]; | ||
| 741 | int ret = -1; | 1090 | int ret = -1; |
| 742 | int fd; | 1091 | int fd; |
| 743 | 1092 | ||
| 1093 | dso__set_loaded(self, map->type); | ||
| 1094 | |||
| 1095 | if (self->kernel) | ||
| 1096 | return dso__load_kernel_sym(self, map, kthread, filter); | ||
| 1097 | |||
| 1098 | name = malloc(size); | ||
| 744 | if (!name) | 1099 | if (!name) |
| 745 | return -1; | 1100 | return -1; |
| 746 | 1101 | ||
| 747 | self->adjust_symbols = 0; | 1102 | self->adjust_symbols = 0; |
| 748 | 1103 | ||
| 749 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { | 1104 | if (strncmp(self->name, "/tmp/perf-", 10) == 0) { |
| 750 | ret = dso__load_perf_map(self, filter, v); | 1105 | ret = dso__load_perf_map(self, map, filter); |
| 751 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : | 1106 | self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT : |
| 752 | DSO__ORIG_NOT_FOUND; | 1107 | DSO__ORIG_NOT_FOUND; |
| 753 | return ret; | 1108 | return ret; |
| @@ -760,34 +1115,50 @@ more: | |||
| 760 | self->origin++; | 1115 | self->origin++; |
| 761 | switch (self->origin) { | 1116 | switch (self->origin) { |
| 762 | case DSO__ORIG_FEDORA: | 1117 | case DSO__ORIG_FEDORA: |
| 763 | snprintf(name, size, "/usr/lib/debug%s.debug", self->name); | 1118 | snprintf(name, size, "/usr/lib/debug%s.debug", |
| 1119 | self->long_name); | ||
| 764 | break; | 1120 | break; |
| 765 | case DSO__ORIG_UBUNTU: | 1121 | case DSO__ORIG_UBUNTU: |
| 766 | snprintf(name, size, "/usr/lib/debug%s", self->name); | 1122 | snprintf(name, size, "/usr/lib/debug%s", |
| 1123 | self->long_name); | ||
| 767 | break; | 1124 | break; |
| 768 | case DSO__ORIG_BUILDID: | 1125 | case DSO__ORIG_BUILDID: |
| 769 | build_id = dso__read_build_id(self, v); | 1126 | if (filename__read_build_id(self->long_name, build_id, |
| 770 | if (build_id != NULL) { | 1127 | sizeof(build_id))) { |
| 1128 | char build_id_hex[BUILD_ID_SIZE * 2 + 1]; | ||
| 1129 | |||
| 1130 | build_id__sprintf(build_id, sizeof(build_id), | ||
| 1131 | build_id_hex); | ||
| 771 | snprintf(name, size, | 1132 | snprintf(name, size, |
| 772 | "/usr/lib/debug/.build-id/%.2s/%s.debug", | 1133 | "/usr/lib/debug/.build-id/%.2s/%s.debug", |
| 773 | build_id, build_id + 2); | 1134 | build_id_hex, build_id_hex + 2); |
| 774 | free(build_id); | 1135 | if (self->has_build_id) |
| 1136 | goto compare_build_id; | ||
| 775 | break; | 1137 | break; |
| 776 | } | 1138 | } |
| 777 | self->origin++; | 1139 | self->origin++; |
| 778 | /* Fall thru */ | 1140 | /* Fall thru */ |
| 779 | case DSO__ORIG_DSO: | 1141 | case DSO__ORIG_DSO: |
| 780 | snprintf(name, size, "%s", self->name); | 1142 | snprintf(name, size, "%s", self->long_name); |
| 781 | break; | 1143 | break; |
| 782 | 1144 | ||
| 783 | default: | 1145 | default: |
| 784 | goto out; | 1146 | goto out; |
| 785 | } | 1147 | } |
| 786 | 1148 | ||
| 1149 | if (self->has_build_id) { | ||
| 1150 | if (filename__read_build_id(name, build_id, | ||
| 1151 | sizeof(build_id)) < 0) | ||
| 1152 | goto more; | ||
| 1153 | compare_build_id: | ||
| 1154 | if (!dso__build_id_equal(self, build_id)) | ||
| 1155 | goto more; | ||
| 1156 | } | ||
| 1157 | |||
| 787 | fd = open(name, O_RDONLY); | 1158 | fd = open(name, O_RDONLY); |
| 788 | } while (fd < 0); | 1159 | } while (fd < 0); |
| 789 | 1160 | ||
| 790 | ret = dso__load_sym(self, fd, name, filter, v, NULL); | 1161 | ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0); |
| 791 | close(fd); | 1162 | close(fd); |
| 792 | 1163 | ||
| 793 | /* | 1164 | /* |
| @@ -797,7 +1168,7 @@ more: | |||
| 797 | goto more; | 1168 | goto more; |
| 798 | 1169 | ||
| 799 | if (ret > 0) { | 1170 | if (ret > 0) { |
| 800 | int nr_plt = dso__synthesize_plt_symbols(self, v); | 1171 | int nr_plt = dso__synthesize_plt_symbols(self, map, filter); |
| 801 | if (nr_plt > 0) | 1172 | if (nr_plt > 0) |
| 802 | ret += nr_plt; | 1173 | ret += nr_plt; |
| 803 | } | 1174 | } |
| @@ -808,142 +1179,279 @@ out: | |||
| 808 | return ret; | 1179 | return ret; |
| 809 | } | 1180 | } |
| 810 | 1181 | ||
| 811 | static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name, | 1182 | static struct map *thread__find_map_by_name(struct thread *self, char *name) |
| 812 | symbol_filter_t filter, int v) | ||
| 813 | { | 1183 | { |
| 814 | struct module *mod = mod_dso__find_module(mods, name); | 1184 | struct rb_node *nd; |
| 815 | int err = 0, fd; | ||
| 816 | 1185 | ||
| 817 | if (mod == NULL || !mod->active) | 1186 | for (nd = rb_first(&self->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) { |
| 818 | return err; | 1187 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 819 | 1188 | ||
| 820 | fd = open(mod->path, O_RDONLY); | 1189 | if (map->dso && strcmp(map->dso->name, name) == 0) |
| 1190 | return map; | ||
| 1191 | } | ||
| 821 | 1192 | ||
| 822 | if (fd < 0) | 1193 | return NULL; |
| 823 | return err; | 1194 | } |
| 824 | 1195 | ||
| 825 | err = dso__load_sym(self, fd, name, filter, v, mod); | 1196 | static int dsos__set_modules_path_dir(char *dirname) |
| 826 | close(fd); | 1197 | { |
| 1198 | struct dirent *dent; | ||
| 1199 | DIR *dir = opendir(dirname); | ||
| 827 | 1200 | ||
| 828 | return err; | 1201 | if (!dir) { |
| 1202 | pr_debug("%s: cannot open %s dir\n", __func__, dirname); | ||
| 1203 | return -1; | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | while ((dent = readdir(dir)) != NULL) { | ||
| 1207 | char path[PATH_MAX]; | ||
| 1208 | |||
| 1209 | if (dent->d_type == DT_DIR) { | ||
| 1210 | if (!strcmp(dent->d_name, ".") || | ||
| 1211 | !strcmp(dent->d_name, "..")) | ||
| 1212 | continue; | ||
| 1213 | |||
| 1214 | snprintf(path, sizeof(path), "%s/%s", | ||
| 1215 | dirname, dent->d_name); | ||
| 1216 | if (dsos__set_modules_path_dir(path) < 0) | ||
| 1217 | goto failure; | ||
| 1218 | } else { | ||
| 1219 | char *dot = strrchr(dent->d_name, '.'), | ||
| 1220 | dso_name[PATH_MAX]; | ||
| 1221 | struct map *map; | ||
| 1222 | char *long_name; | ||
| 1223 | |||
| 1224 | if (dot == NULL || strcmp(dot, ".ko")) | ||
| 1225 | continue; | ||
| 1226 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", | ||
| 1227 | (int)(dot - dent->d_name), dent->d_name); | ||
| 1228 | |||
| 1229 | strxfrchar(dso_name, '-', '_'); | ||
| 1230 | map = thread__find_map_by_name(kthread, dso_name); | ||
| 1231 | if (map == NULL) | ||
| 1232 | continue; | ||
| 1233 | |||
| 1234 | snprintf(path, sizeof(path), "%s/%s", | ||
| 1235 | dirname, dent->d_name); | ||
| 1236 | |||
| 1237 | long_name = strdup(path); | ||
| 1238 | if (long_name == NULL) | ||
| 1239 | goto failure; | ||
| 1240 | dso__set_long_name(map->dso, long_name); | ||
| 1241 | } | ||
| 1242 | } | ||
| 1243 | |||
| 1244 | return 0; | ||
| 1245 | failure: | ||
| 1246 | closedir(dir); | ||
| 1247 | return -1; | ||
| 829 | } | 1248 | } |
| 830 | 1249 | ||
| 831 | int dso__load_modules(struct dso *self, symbol_filter_t filter, int v) | 1250 | static int dsos__set_modules_path(void) |
| 832 | { | 1251 | { |
| 833 | struct mod_dso *mods = mod_dso__new_dso("modules"); | 1252 | struct utsname uts; |
| 834 | struct module *pos; | 1253 | char modules_path[PATH_MAX]; |
| 835 | struct rb_node *next; | ||
| 836 | int err; | ||
| 837 | |||
| 838 | err = mod_dso__load_modules(mods); | ||
| 839 | 1254 | ||
| 840 | if (err <= 0) | 1255 | if (uname(&uts) < 0) |
| 841 | return err; | 1256 | return -1; |
| 842 | 1257 | ||
| 843 | /* | 1258 | snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel", |
| 844 | * Iterate over modules, and load active symbols. | 1259 | uts.release); |
| 845 | */ | ||
| 846 | next = rb_first(&mods->mods); | ||
| 847 | while (next) { | ||
| 848 | pos = rb_entry(next, struct module, rb_node); | ||
| 849 | err = dso__load_module(self, mods, pos->name, filter, v); | ||
| 850 | 1260 | ||
| 851 | if (err < 0) | 1261 | return dsos__set_modules_path_dir(modules_path); |
| 852 | break; | 1262 | } |
| 853 | 1263 | ||
| 854 | next = rb_next(&pos->rb_node); | 1264 | /* |
| 855 | } | 1265 | * Constructor variant for modules (where we know from /proc/modules where |
| 1266 | * they are loaded) and for vmlinux, where only after we load all the | ||
| 1267 | * symbols we'll know where it starts and ends. | ||
| 1268 | */ | ||
| 1269 | static struct map *map__new2(u64 start, struct dso *dso, enum map_type type) | ||
| 1270 | { | ||
| 1271 | struct map *self = malloc(sizeof(*self)); | ||
| 856 | 1272 | ||
| 857 | if (err < 0) { | 1273 | if (self != NULL) { |
| 858 | mod_dso__delete_modules(mods); | 1274 | /* |
| 859 | mod_dso__delete_self(mods); | 1275 | * ->end will be filled after we load all the symbols |
| 1276 | */ | ||
| 1277 | map__init(self, type, start, 0, 0, dso); | ||
| 860 | } | 1278 | } |
| 861 | 1279 | ||
| 862 | return err; | 1280 | return self; |
| 863 | } | 1281 | } |
| 864 | 1282 | ||
| 865 | static inline void dso__fill_symbol_holes(struct dso *self) | 1283 | static int thread__create_module_maps(struct thread *self) |
| 866 | { | 1284 | { |
| 867 | struct symbol *prev = NULL; | 1285 | char *line = NULL; |
| 868 | struct rb_node *nd; | 1286 | size_t n; |
| 1287 | FILE *file = fopen("/proc/modules", "r"); | ||
| 1288 | struct map *map; | ||
| 869 | 1289 | ||
| 870 | for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) { | 1290 | if (file == NULL) |
| 871 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | 1291 | return -1; |
| 872 | 1292 | ||
| 873 | if (prev) { | 1293 | while (!feof(file)) { |
| 874 | u64 hole = 0; | 1294 | char name[PATH_MAX]; |
| 875 | int alias = pos->start == prev->start; | 1295 | u64 start; |
| 1296 | struct dso *dso; | ||
| 1297 | char *sep; | ||
| 1298 | int line_len; | ||
| 876 | 1299 | ||
| 877 | if (!alias) | 1300 | line_len = getline(&line, &n, file); |
| 878 | hole = prev->start - pos->end - 1; | 1301 | if (line_len < 0) |
| 1302 | break; | ||
| 879 | 1303 | ||
| 880 | if (hole || alias) { | 1304 | if (!line) |
| 881 | if (alias) | 1305 | goto out_failure; |
| 882 | pos->end = prev->end; | 1306 | |
| 883 | else if (hole) | 1307 | line[--line_len] = '\0'; /* \n */ |
| 884 | pos->end = prev->start - 1; | 1308 | |
| 885 | } | 1309 | sep = strrchr(line, 'x'); |
| 1310 | if (sep == NULL) | ||
| 1311 | continue; | ||
| 1312 | |||
| 1313 | hex2u64(sep + 1, &start); | ||
| 1314 | |||
| 1315 | sep = strchr(line, ' '); | ||
| 1316 | if (sep == NULL) | ||
| 1317 | continue; | ||
| 1318 | |||
| 1319 | *sep = '\0'; | ||
| 1320 | |||
| 1321 | snprintf(name, sizeof(name), "[%s]", line); | ||
| 1322 | dso = dso__new(name); | ||
| 1323 | |||
| 1324 | if (dso == NULL) | ||
| 1325 | goto out_delete_line; | ||
| 1326 | |||
| 1327 | map = map__new2(start, dso, MAP__FUNCTION); | ||
| 1328 | if (map == NULL) { | ||
| 1329 | dso__delete(dso); | ||
| 1330 | goto out_delete_line; | ||
| 886 | } | 1331 | } |
| 887 | prev = pos; | 1332 | |
| 1333 | snprintf(name, sizeof(name), | ||
| 1334 | "/sys/module/%s/notes/.note.gnu.build-id", line); | ||
| 1335 | if (sysfs__read_build_id(name, dso->build_id, | ||
| 1336 | sizeof(dso->build_id)) == 0) | ||
| 1337 | dso->has_build_id = true; | ||
| 1338 | |||
| 1339 | dso->origin = DSO__ORIG_KMODULE; | ||
| 1340 | __thread__insert_map(self, map); | ||
| 1341 | dsos__add(&dsos__kernel, dso); | ||
| 888 | } | 1342 | } |
| 1343 | |||
| 1344 | free(line); | ||
| 1345 | fclose(file); | ||
| 1346 | |||
| 1347 | return dsos__set_modules_path(); | ||
| 1348 | |||
| 1349 | out_delete_line: | ||
| 1350 | free(line); | ||
| 1351 | out_failure: | ||
| 1352 | return -1; | ||
| 889 | } | 1353 | } |
| 890 | 1354 | ||
| 891 | static int dso__load_vmlinux(struct dso *self, const char *vmlinux, | 1355 | static int dso__load_vmlinux(struct dso *self, struct map *map, struct thread *thread, |
| 892 | symbol_filter_t filter, int v) | 1356 | const char *vmlinux, symbol_filter_t filter) |
| 893 | { | 1357 | { |
| 894 | int err, fd = open(vmlinux, O_RDONLY); | 1358 | int err = -1, fd; |
| 895 | 1359 | ||
| 896 | if (fd < 0) | 1360 | if (self->has_build_id) { |
| 897 | return -1; | 1361 | u8 build_id[BUILD_ID_SIZE]; |
| 898 | 1362 | ||
| 899 | err = dso__load_sym(self, fd, vmlinux, filter, v, NULL); | 1363 | if (filename__read_build_id(vmlinux, build_id, |
| 1364 | sizeof(build_id)) < 0) { | ||
| 1365 | pr_debug("No build_id in %s, ignoring it\n", vmlinux); | ||
| 1366 | return -1; | ||
| 1367 | } | ||
| 1368 | if (!dso__build_id_equal(self, build_id)) { | ||
| 1369 | char expected_build_id[BUILD_ID_SIZE * 2 + 1], | ||
| 1370 | vmlinux_build_id[BUILD_ID_SIZE * 2 + 1]; | ||
| 1371 | |||
| 1372 | build_id__sprintf(self->build_id, | ||
| 1373 | sizeof(self->build_id), | ||
| 1374 | expected_build_id); | ||
| 1375 | build_id__sprintf(build_id, sizeof(build_id), | ||
| 1376 | vmlinux_build_id); | ||
| 1377 | pr_debug("build_id in %s is %s while expected is %s, " | ||
| 1378 | "ignoring it\n", vmlinux, vmlinux_build_id, | ||
| 1379 | expected_build_id); | ||
| 1380 | return -1; | ||
| 1381 | } | ||
| 1382 | } | ||
| 900 | 1383 | ||
| 901 | if (err > 0) | 1384 | fd = open(vmlinux, O_RDONLY); |
| 902 | dso__fill_symbol_holes(self); | 1385 | if (fd < 0) |
| 1386 | return -1; | ||
| 903 | 1387 | ||
| 1388 | dso__set_loaded(self, map->type); | ||
| 1389 | err = dso__load_sym(self, map, thread, self->long_name, fd, filter, 1, 0); | ||
| 904 | close(fd); | 1390 | close(fd); |
| 905 | 1391 | ||
| 906 | return err; | 1392 | return err; |
| 907 | } | 1393 | } |
| 908 | 1394 | ||
| 909 | int dso__load_kernel(struct dso *self, const char *vmlinux, | 1395 | static int dso__load_kernel_sym(struct dso *self, struct map *map, |
| 910 | symbol_filter_t filter, int v, int use_modules) | 1396 | struct thread *thread, symbol_filter_t filter) |
| 911 | { | 1397 | { |
| 912 | int err = -1; | 1398 | int err; |
| 913 | 1399 | bool is_kallsyms; | |
| 914 | if (vmlinux) { | 1400 | |
| 915 | err = dso__load_vmlinux(self, vmlinux, filter, v); | 1401 | if (vmlinux_path != NULL) { |
| 916 | if (err > 0 && use_modules) | 1402 | int i; |
| 917 | err = dso__load_modules(self, filter, v); | 1403 | pr_debug("Looking at the vmlinux_path (%d entries long)\n", |
| 1404 | vmlinux_path__nr_entries); | ||
| 1405 | for (i = 0; i < vmlinux_path__nr_entries; ++i) { | ||
| 1406 | err = dso__load_vmlinux(self, map, thread, | ||
| 1407 | vmlinux_path[i], filter); | ||
| 1408 | if (err > 0) { | ||
| 1409 | pr_debug("Using %s for symbols\n", | ||
| 1410 | vmlinux_path[i]); | ||
| 1411 | dso__set_long_name(self, | ||
| 1412 | strdup(vmlinux_path[i])); | ||
| 1413 | goto out_fixup; | ||
| 1414 | } | ||
| 1415 | } | ||
| 918 | } | 1416 | } |
| 919 | 1417 | ||
| 920 | if (err <= 0) | 1418 | is_kallsyms = self->long_name[0] == '['; |
| 921 | err = dso__load_kallsyms(self, filter, v); | 1419 | if (is_kallsyms) |
| 1420 | goto do_kallsyms; | ||
| 922 | 1421 | ||
| 923 | if (err > 0) | 1422 | err = dso__load_vmlinux(self, map, thread, self->long_name, filter); |
| 924 | self->origin = DSO__ORIG_KERNEL; | 1423 | if (err <= 0) { |
| 1424 | pr_info("The file %s cannot be used, " | ||
| 1425 | "trying to use /proc/kallsyms...", self->long_name); | ||
| 1426 | do_kallsyms: | ||
| 1427 | err = dso__load_kallsyms(self, map, thread, filter); | ||
| 1428 | if (err > 0 && !is_kallsyms) | ||
| 1429 | dso__set_long_name(self, strdup("[kernel.kallsyms]")); | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | if (err > 0) { | ||
| 1433 | out_fixup: | ||
| 1434 | map__fixup_start(map); | ||
| 1435 | map__fixup_end(map); | ||
| 1436 | } | ||
| 925 | 1437 | ||
| 926 | return err; | 1438 | return err; |
| 927 | } | 1439 | } |
| 928 | 1440 | ||
| 929 | LIST_HEAD(dsos); | 1441 | LIST_HEAD(dsos__user); |
| 930 | struct dso *kernel_dso; | 1442 | LIST_HEAD(dsos__kernel); |
| 931 | struct dso *vdso; | 1443 | struct dso *vdso; |
| 932 | struct dso *hypervisor_dso; | ||
| 933 | |||
| 934 | const char *vmlinux_name = "vmlinux"; | ||
| 935 | int modules; | ||
| 936 | 1444 | ||
| 937 | static void dsos__add(struct dso *dso) | 1445 | static void dsos__add(struct list_head *head, struct dso *dso) |
| 938 | { | 1446 | { |
| 939 | list_add_tail(&dso->node, &dsos); | 1447 | list_add_tail(&dso->node, head); |
| 940 | } | 1448 | } |
| 941 | 1449 | ||
| 942 | static struct dso *dsos__find(const char *name) | 1450 | static struct dso *dsos__find(struct list_head *head, const char *name) |
| 943 | { | 1451 | { |
| 944 | struct dso *pos; | 1452 | struct dso *pos; |
| 945 | 1453 | ||
| 946 | list_for_each_entry(pos, &dsos, node) | 1454 | list_for_each_entry(pos, head, node) |
| 947 | if (strcmp(pos->name, name) == 0) | 1455 | if (strcmp(pos->name, name) == 0) |
| 948 | return pos; | 1456 | return pos; |
| 949 | return NULL; | 1457 | return NULL; |
| @@ -951,79 +1459,170 @@ static struct dso *dsos__find(const char *name) | |||
| 951 | 1459 | ||
| 952 | struct dso *dsos__findnew(const char *name) | 1460 | struct dso *dsos__findnew(const char *name) |
| 953 | { | 1461 | { |
| 954 | struct dso *dso = dsos__find(name); | 1462 | struct dso *dso = dsos__find(&dsos__user, name); |
| 955 | int nr; | ||
| 956 | |||
| 957 | if (dso) | ||
| 958 | return dso; | ||
| 959 | |||
| 960 | dso = dso__new(name, 0); | ||
| 961 | if (!dso) | ||
| 962 | goto out_delete_dso; | ||
| 963 | 1463 | ||
| 964 | nr = dso__load(dso, NULL, verbose); | 1464 | if (!dso) { |
| 965 | if (nr < 0) { | 1465 | dso = dso__new(name); |
| 966 | eprintf("Failed to open: %s\n", name); | 1466 | if (dso != NULL) { |
| 967 | goto out_delete_dso; | 1467 | dsos__add(&dsos__user, dso); |
| 1468 | dso__set_basename(dso); | ||
| 1469 | } | ||
| 968 | } | 1470 | } |
| 969 | if (!nr) | ||
| 970 | eprintf("No symbols found in: %s, maybe install a debug package?\n", name); | ||
| 971 | |||
| 972 | dsos__add(dso); | ||
| 973 | 1471 | ||
| 974 | return dso; | 1472 | return dso; |
| 1473 | } | ||
| 975 | 1474 | ||
| 976 | out_delete_dso: | 1475 | static void __dsos__fprintf(struct list_head *head, FILE *fp) |
| 977 | dso__delete(dso); | 1476 | { |
| 978 | return NULL; | 1477 | struct dso *pos; |
| 1478 | |||
| 1479 | list_for_each_entry(pos, head, node) { | ||
| 1480 | int i; | ||
| 1481 | for (i = 0; i < MAP__NR_TYPES; ++i) | ||
| 1482 | dso__fprintf(pos, i, fp); | ||
| 1483 | } | ||
| 979 | } | 1484 | } |
| 980 | 1485 | ||
| 981 | void dsos__fprintf(FILE *fp) | 1486 | void dsos__fprintf(FILE *fp) |
| 982 | { | 1487 | { |
| 1488 | __dsos__fprintf(&dsos__kernel, fp); | ||
| 1489 | __dsos__fprintf(&dsos__user, fp); | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp) | ||
| 1493 | { | ||
| 983 | struct dso *pos; | 1494 | struct dso *pos; |
| 1495 | size_t ret = 0; | ||
| 984 | 1496 | ||
| 985 | list_for_each_entry(pos, &dsos, node) | 1497 | list_for_each_entry(pos, head, node) { |
| 986 | dso__fprintf(pos, fp); | 1498 | ret += dso__fprintf_buildid(pos, fp); |
| 1499 | ret += fprintf(fp, " %s\n", pos->long_name); | ||
| 1500 | } | ||
| 1501 | return ret; | ||
| 987 | } | 1502 | } |
| 988 | 1503 | ||
| 989 | static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip) | 1504 | size_t dsos__fprintf_buildid(FILE *fp) |
| 990 | { | 1505 | { |
| 991 | return dso__find_symbol(dso, ip); | 1506 | return (__dsos__fprintf_buildid(&dsos__kernel, fp) + |
| 1507 | __dsos__fprintf_buildid(&dsos__user, fp)); | ||
| 992 | } | 1508 | } |
| 993 | 1509 | ||
| 994 | int load_kernel(void) | 1510 | static int thread__create_kernel_map(struct thread *self, const char *vmlinux) |
| 995 | { | 1511 | { |
| 996 | int err; | 1512 | struct map *kmap; |
| 1513 | struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]"); | ||
| 997 | 1514 | ||
| 998 | kernel_dso = dso__new("[kernel]", 0); | 1515 | if (kernel == NULL) |
| 999 | if (!kernel_dso) | ||
| 1000 | return -1; | 1516 | return -1; |
| 1001 | 1517 | ||
| 1002 | err = dso__load_kernel(kernel_dso, vmlinux_name, NULL, verbose, modules); | 1518 | kmap = map__new2(0, kernel, MAP__FUNCTION); |
| 1003 | if (err <= 0) { | 1519 | if (kmap == NULL) |
| 1004 | dso__delete(kernel_dso); | 1520 | goto out_delete_kernel_dso; |
| 1005 | kernel_dso = NULL; | ||
| 1006 | } else | ||
| 1007 | dsos__add(kernel_dso); | ||
| 1008 | 1521 | ||
| 1009 | vdso = dso__new("[vdso]", 0); | 1522 | kmap->map_ip = kmap->unmap_ip = identity__map_ip; |
| 1010 | if (!vdso) | 1523 | kernel->short_name = "[kernel]"; |
| 1011 | return -1; | 1524 | kernel->kernel = 1; |
| 1012 | 1525 | ||
| 1013 | vdso->find_symbol = vdso__find_symbol; | 1526 | vdso = dso__new("[vdso]"); |
| 1527 | if (vdso == NULL) | ||
| 1528 | goto out_delete_kernel_map; | ||
| 1529 | dso__set_loaded(vdso, MAP__FUNCTION); | ||
| 1014 | 1530 | ||
| 1015 | dsos__add(vdso); | 1531 | if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id, |
| 1532 | sizeof(kernel->build_id)) == 0) | ||
| 1533 | kernel->has_build_id = true; | ||
| 1016 | 1534 | ||
| 1017 | hypervisor_dso = dso__new("[hypervisor]", 0); | 1535 | __thread__insert_map(self, kmap); |
| 1018 | if (!hypervisor_dso) | 1536 | dsos__add(&dsos__kernel, kernel); |
| 1019 | return -1; | 1537 | dsos__add(&dsos__user, vdso); |
| 1020 | dsos__add(hypervisor_dso); | ||
| 1021 | 1538 | ||
| 1022 | return err; | 1539 | return 0; |
| 1540 | |||
| 1541 | out_delete_kernel_map: | ||
| 1542 | map__delete(kmap); | ||
| 1543 | out_delete_kernel_dso: | ||
| 1544 | dso__delete(kernel); | ||
| 1545 | return -1; | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | static void vmlinux_path__exit(void) | ||
| 1549 | { | ||
| 1550 | while (--vmlinux_path__nr_entries >= 0) { | ||
| 1551 | free(vmlinux_path[vmlinux_path__nr_entries]); | ||
| 1552 | vmlinux_path[vmlinux_path__nr_entries] = NULL; | ||
| 1553 | } | ||
| 1554 | |||
| 1555 | free(vmlinux_path); | ||
| 1556 | vmlinux_path = NULL; | ||
| 1023 | } | 1557 | } |
| 1024 | 1558 | ||
| 1559 | static int vmlinux_path__init(void) | ||
| 1560 | { | ||
| 1561 | struct utsname uts; | ||
| 1562 | char bf[PATH_MAX]; | ||
| 1563 | |||
| 1564 | if (uname(&uts) < 0) | ||
| 1565 | return -1; | ||
| 1566 | |||
| 1567 | vmlinux_path = malloc(sizeof(char *) * 5); | ||
| 1568 | if (vmlinux_path == NULL) | ||
| 1569 | return -1; | ||
| 1570 | |||
| 1571 | vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); | ||
| 1572 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | ||
| 1573 | goto out_fail; | ||
| 1574 | ++vmlinux_path__nr_entries; | ||
| 1575 | vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); | ||
| 1576 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | ||
| 1577 | goto out_fail; | ||
| 1578 | ++vmlinux_path__nr_entries; | ||
| 1579 | snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); | ||
| 1580 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | ||
| 1581 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | ||
| 1582 | goto out_fail; | ||
| 1583 | ++vmlinux_path__nr_entries; | ||
| 1584 | snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); | ||
| 1585 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | ||
| 1586 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | ||
| 1587 | goto out_fail; | ||
| 1588 | ++vmlinux_path__nr_entries; | ||
| 1589 | snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", | ||
| 1590 | uts.release); | ||
| 1591 | vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); | ||
| 1592 | if (vmlinux_path[vmlinux_path__nr_entries] == NULL) | ||
| 1593 | goto out_fail; | ||
| 1594 | ++vmlinux_path__nr_entries; | ||
| 1595 | |||
| 1596 | return 0; | ||
| 1597 | |||
| 1598 | out_fail: | ||
| 1599 | vmlinux_path__exit(); | ||
| 1600 | return -1; | ||
| 1601 | } | ||
| 1025 | 1602 | ||
| 1026 | void symbol__init(void) | 1603 | int symbol__init(struct symbol_conf *conf) |
| 1027 | { | 1604 | { |
| 1605 | const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults; | ||
| 1606 | |||
| 1028 | elf_version(EV_CURRENT); | 1607 | elf_version(EV_CURRENT); |
| 1608 | symbol__priv_size = pconf->priv_size; | ||
| 1609 | thread__init(kthread, 0); | ||
| 1610 | |||
| 1611 | if (pconf->try_vmlinux_path && vmlinux_path__init() < 0) | ||
| 1612 | return -1; | ||
| 1613 | |||
| 1614 | if (thread__create_kernel_map(kthread, pconf->vmlinux_name) < 0) { | ||
| 1615 | vmlinux_path__exit(); | ||
| 1616 | return -1; | ||
| 1617 | } | ||
| 1618 | |||
| 1619 | kthread->use_modules = pconf->use_modules; | ||
| 1620 | if (pconf->use_modules && thread__create_module_maps(kthread) < 0) | ||
| 1621 | pr_debug("Failed to load list of modules in use, " | ||
| 1622 | "continuing...\n"); | ||
| 1623 | /* | ||
| 1624 | * Now that we have all the maps created, just set the ->end of them: | ||
| 1625 | */ | ||
| 1626 | thread__fixup_maps_end(kthread); | ||
| 1627 | return 0; | ||
| 1029 | } | 1628 | } |
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 6e8490716408..17003efa0b39 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | #ifndef _PERF_SYMBOL_ | 1 | #ifndef __PERF_SYMBOL |
| 2 | #define _PERF_SYMBOL_ 1 | 2 | #define __PERF_SYMBOL 1 |
| 3 | 3 | ||
| 4 | #include <linux/types.h> | 4 | #include <linux/types.h> |
| 5 | #include <stdbool.h> | ||
| 5 | #include "types.h" | 6 | #include "types.h" |
| 6 | #include <linux/list.h> | 7 | #include <linux/list.h> |
| 7 | #include <linux/rbtree.h> | 8 | #include <linux/rbtree.h> |
| 8 | #include "module.h" | ||
| 9 | #include "event.h" | 9 | #include "event.h" |
| 10 | 10 | ||
| 11 | #ifdef HAVE_CPLUS_DEMANGLE | 11 | #ifdef HAVE_CPLUS_DEMANGLE |
| @@ -27,6 +27,16 @@ static inline char *bfd_demangle(void __used *v, const char __used *c, | |||
| 27 | #endif | 27 | #endif |
| 28 | #endif | 28 | #endif |
| 29 | 29 | ||
| 30 | /* | ||
| 31 | * libelf 0.8.x and earlier do not support ELF_C_READ_MMAP; | ||
| 32 | * for newer versions we can use mmap to reduce memory usage: | ||
| 33 | */ | ||
| 34 | #ifdef LIBELF_NO_MMAP | ||
| 35 | # define PERF_ELF_C_READ_MMAP ELF_C_READ | ||
| 36 | #else | ||
| 37 | # define PERF_ELF_C_READ_MMAP ELF_C_READ_MMAP | ||
| 38 | #endif | ||
| 39 | |||
| 30 | #ifndef DMGL_PARAMS | 40 | #ifndef DMGL_PARAMS |
| 31 | #define DMGL_PARAMS (1 << 0) /* Include function args */ | 41 | #define DMGL_PARAMS (1 << 0) /* Include function args */ |
| 32 | #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ | 42 | #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */ |
| @@ -36,57 +46,75 @@ struct symbol { | |||
| 36 | struct rb_node rb_node; | 46 | struct rb_node rb_node; |
| 37 | u64 start; | 47 | u64 start; |
| 38 | u64 end; | 48 | u64 end; |
| 39 | u64 obj_start; | ||
| 40 | u64 hist_sum; | ||
| 41 | u64 *hist; | ||
| 42 | struct module *module; | ||
| 43 | void *priv; | ||
| 44 | char name[0]; | 49 | char name[0]; |
| 45 | }; | 50 | }; |
| 46 | 51 | ||
| 52 | struct symbol_conf { | ||
| 53 | unsigned short priv_size; | ||
| 54 | bool try_vmlinux_path, | ||
| 55 | use_modules; | ||
| 56 | const char *vmlinux_name; | ||
| 57 | }; | ||
| 58 | |||
| 59 | extern unsigned int symbol__priv_size; | ||
| 60 | |||
| 61 | static inline void *symbol__priv(struct symbol *self) | ||
| 62 | { | ||
| 63 | return ((void *)self) - symbol__priv_size; | ||
| 64 | } | ||
| 65 | |||
| 66 | struct addr_location { | ||
| 67 | struct thread *thread; | ||
| 68 | struct map *map; | ||
| 69 | struct symbol *sym; | ||
| 70 | u64 addr; | ||
| 71 | char level; | ||
| 72 | }; | ||
| 73 | |||
| 47 | struct dso { | 74 | struct dso { |
| 48 | struct list_head node; | 75 | struct list_head node; |
| 49 | struct rb_root syms; | 76 | struct rb_root symbols[MAP__NR_TYPES]; |
| 50 | struct symbol *(*find_symbol)(struct dso *, u64 ip); | 77 | struct symbol *(*find_symbol)(struct dso *self, |
| 51 | unsigned int sym_priv_size; | 78 | enum map_type type, u64 addr); |
| 52 | unsigned char adjust_symbols; | 79 | u8 adjust_symbols:1; |
| 53 | unsigned char slen_calculated; | 80 | u8 slen_calculated:1; |
| 81 | u8 has_build_id:1; | ||
| 82 | u8 kernel:1; | ||
| 54 | unsigned char origin; | 83 | unsigned char origin; |
| 84 | u8 loaded; | ||
| 85 | u8 build_id[BUILD_ID_SIZE]; | ||
| 86 | u16 long_name_len; | ||
| 87 | const char *short_name; | ||
| 88 | char *long_name; | ||
| 55 | char name[0]; | 89 | char name[0]; |
| 56 | }; | 90 | }; |
| 57 | 91 | ||
| 58 | extern const char *sym_hist_filter; | 92 | struct dso *dso__new(const char *name); |
| 59 | |||
| 60 | typedef int (*symbol_filter_t)(struct dso *self, struct symbol *sym); | ||
| 61 | |||
| 62 | struct dso *dso__new(const char *name, unsigned int sym_priv_size); | ||
| 63 | void dso__delete(struct dso *self); | 93 | void dso__delete(struct dso *self); |
| 64 | 94 | ||
| 65 | static inline void *dso__sym_priv(struct dso *self, struct symbol *sym) | 95 | bool dso__loaded(const struct dso *self, enum map_type type); |
| 66 | { | ||
| 67 | return ((void *)sym) - self->sym_priv_size; | ||
| 68 | } | ||
| 69 | 96 | ||
| 70 | struct symbol *dso__find_symbol(struct dso *self, u64 ip); | ||
| 71 | |||
| 72 | int dso__load_kernel(struct dso *self, const char *vmlinux, | ||
| 73 | symbol_filter_t filter, int verbose, int modules); | ||
| 74 | int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose); | ||
| 75 | int dso__load(struct dso *self, symbol_filter_t filter, int verbose); | ||
| 76 | struct dso *dsos__findnew(const char *name); | 97 | struct dso *dsos__findnew(const char *name); |
| 98 | int dso__load(struct dso *self, struct map *map, symbol_filter_t filter); | ||
| 77 | void dsos__fprintf(FILE *fp); | 99 | void dsos__fprintf(FILE *fp); |
| 100 | size_t dsos__fprintf_buildid(FILE *fp); | ||
| 78 | 101 | ||
| 79 | size_t dso__fprintf(struct dso *self, FILE *fp); | 102 | size_t dso__fprintf_buildid(struct dso *self, FILE *fp); |
| 103 | size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp); | ||
| 80 | char dso__symtab_origin(const struct dso *self); | 104 | char dso__symtab_origin(const struct dso *self); |
| 105 | void dso__set_build_id(struct dso *self, void *build_id); | ||
| 106 | |||
| 107 | int filename__read_build_id(const char *filename, void *bf, size_t size); | ||
| 108 | int sysfs__read_build_id(const char *filename, void *bf, size_t size); | ||
| 109 | bool dsos__read_build_ids(void); | ||
| 110 | int build_id__sprintf(u8 *self, int len, char *bf); | ||
| 81 | 111 | ||
| 82 | int load_kernel(void); | 112 | size_t kernel_maps__fprintf(FILE *fp); |
| 83 | 113 | ||
| 84 | void symbol__init(void); | 114 | int symbol__init(struct symbol_conf *conf); |
| 85 | 115 | ||
| 86 | extern struct list_head dsos; | 116 | struct thread; |
| 87 | extern struct dso *kernel_dso; | 117 | struct thread *kthread; |
| 118 | extern struct list_head dsos__user, dsos__kernel; | ||
| 88 | extern struct dso *vdso; | 119 | extern struct dso *vdso; |
| 89 | extern struct dso *hypervisor_dso; | 120 | #endif /* __PERF_SYMBOL */ |
| 90 | extern const char *vmlinux_name; | ||
| 91 | extern int modules; | ||
| 92 | #endif /* _PERF_SYMBOL_ */ | ||
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 7635928ca278..603f5610861b 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c | |||
| @@ -6,16 +6,29 @@ | |||
| 6 | #include "util.h" | 6 | #include "util.h" |
| 7 | #include "debug.h" | 7 | #include "debug.h" |
| 8 | 8 | ||
| 9 | static struct rb_root threads; | ||
| 10 | static struct thread *last_match; | ||
| 11 | |||
| 12 | void thread__init(struct thread *self, pid_t pid) | ||
| 13 | { | ||
| 14 | int i; | ||
| 15 | self->pid = pid; | ||
| 16 | self->comm = NULL; | ||
| 17 | for (i = 0; i < MAP__NR_TYPES; ++i) { | ||
| 18 | self->maps[i] = RB_ROOT; | ||
| 19 | INIT_LIST_HEAD(&self->removed_maps[i]); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 9 | static struct thread *thread__new(pid_t pid) | 23 | static struct thread *thread__new(pid_t pid) |
| 10 | { | 24 | { |
| 11 | struct thread *self = malloc(sizeof(*self)); | 25 | struct thread *self = zalloc(sizeof(*self)); |
| 12 | 26 | ||
| 13 | if (self != NULL) { | 27 | if (self != NULL) { |
| 14 | self->pid = pid; | 28 | thread__init(self, pid); |
| 15 | self->comm = malloc(32); | 29 | self->comm = malloc(32); |
| 16 | if (self->comm) | 30 | if (self->comm) |
| 17 | snprintf(self->comm, 32, ":%d", self->pid); | 31 | snprintf(self->comm, 32, ":%d", self->pid); |
| 18 | INIT_LIST_HEAD(&self->maps); | ||
| 19 | } | 32 | } |
| 20 | 33 | ||
| 21 | return self; | 34 | return self; |
| @@ -29,21 +42,84 @@ int thread__set_comm(struct thread *self, const char *comm) | |||
| 29 | return self->comm ? 0 : -ENOMEM; | 42 | return self->comm ? 0 : -ENOMEM; |
| 30 | } | 43 | } |
| 31 | 44 | ||
| 32 | static size_t thread__fprintf(struct thread *self, FILE *fp) | 45 | int thread__comm_len(struct thread *self) |
| 46 | { | ||
| 47 | if (!self->comm_len) { | ||
| 48 | if (!self->comm) | ||
| 49 | return 0; | ||
| 50 | self->comm_len = strlen(self->comm); | ||
| 51 | } | ||
| 52 | |||
| 53 | return self->comm_len; | ||
| 54 | } | ||
| 55 | |||
| 56 | static const char *map_type__name[MAP__NR_TYPES] = { | ||
| 57 | [MAP__FUNCTION] = "Functions", | ||
| 58 | }; | ||
| 59 | |||
| 60 | static size_t __thread__fprintf_maps(struct thread *self, | ||
| 61 | enum map_type type, FILE *fp) | ||
| 62 | { | ||
| 63 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); | ||
| 64 | struct rb_node *nd; | ||
| 65 | |||
| 66 | for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) { | ||
| 67 | struct map *pos = rb_entry(nd, struct map, rb_node); | ||
| 68 | printed += fprintf(fp, "Map:"); | ||
| 69 | printed += map__fprintf(pos, fp); | ||
| 70 | if (verbose > 1) { | ||
| 71 | printed += dso__fprintf(pos->dso, type, fp); | ||
| 72 | printed += fprintf(fp, "--\n"); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return printed; | ||
| 77 | } | ||
| 78 | |||
| 79 | size_t thread__fprintf_maps(struct thread *self, FILE *fp) | ||
| 80 | { | ||
| 81 | size_t printed = 0, i; | ||
| 82 | for (i = 0; i < MAP__NR_TYPES; ++i) | ||
| 83 | printed += __thread__fprintf_maps(self, i, fp); | ||
| 84 | return printed; | ||
| 85 | } | ||
| 86 | |||
| 87 | static size_t __thread__fprintf_removed_maps(struct thread *self, | ||
| 88 | enum map_type type, FILE *fp) | ||
| 33 | { | 89 | { |
| 34 | struct map *pos; | 90 | struct map *pos; |
| 35 | size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm); | 91 | size_t printed = 0; |
| 92 | |||
| 93 | list_for_each_entry(pos, &self->removed_maps[type], node) { | ||
| 94 | printed += fprintf(fp, "Map:"); | ||
| 95 | printed += map__fprintf(pos, fp); | ||
| 96 | if (verbose > 1) { | ||
| 97 | printed += dso__fprintf(pos->dso, type, fp); | ||
| 98 | printed += fprintf(fp, "--\n"); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | return printed; | ||
| 102 | } | ||
| 36 | 103 | ||
| 37 | list_for_each_entry(pos, &self->maps, node) | 104 | static size_t thread__fprintf_removed_maps(struct thread *self, FILE *fp) |
| 38 | ret += map__fprintf(pos, fp); | 105 | { |
| 106 | size_t printed = 0, i; | ||
| 107 | for (i = 0; i < MAP__NR_TYPES; ++i) | ||
| 108 | printed += __thread__fprintf_removed_maps(self, i, fp); | ||
| 109 | return printed; | ||
| 110 | } | ||
| 39 | 111 | ||
| 40 | return ret; | 112 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 113 | { | ||
| 114 | size_t printed = fprintf(fp, "Thread %d %s\n", self->pid, self->comm); | ||
| 115 | printed += thread__fprintf_removed_maps(self, fp); | ||
| 116 | printed += fprintf(fp, "Removed maps:\n"); | ||
| 117 | return printed + thread__fprintf_removed_maps(self, fp); | ||
| 41 | } | 118 | } |
| 42 | 119 | ||
| 43 | struct thread * | 120 | struct thread *threads__findnew(pid_t pid) |
| 44 | threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match) | ||
| 45 | { | 121 | { |
| 46 | struct rb_node **p = &threads->rb_node; | 122 | struct rb_node **p = &threads.rb_node; |
| 47 | struct rb_node *parent = NULL; | 123 | struct rb_node *parent = NULL; |
| 48 | struct thread *th; | 124 | struct thread *th; |
| 49 | 125 | ||
| @@ -52,15 +128,15 @@ threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match) | |||
| 52 | * so most of the time we dont have to look up | 128 | * so most of the time we dont have to look up |
| 53 | * the full rbtree: | 129 | * the full rbtree: |
| 54 | */ | 130 | */ |
| 55 | if (*last_match && (*last_match)->pid == pid) | 131 | if (last_match && last_match->pid == pid) |
| 56 | return *last_match; | 132 | return last_match; |
| 57 | 133 | ||
| 58 | while (*p != NULL) { | 134 | while (*p != NULL) { |
| 59 | parent = *p; | 135 | parent = *p; |
| 60 | th = rb_entry(parent, struct thread, rb_node); | 136 | th = rb_entry(parent, struct thread, rb_node); |
| 61 | 137 | ||
| 62 | if (th->pid == pid) { | 138 | if (th->pid == pid) { |
| 63 | *last_match = th; | 139 | last_match = th; |
| 64 | return th; | 140 | return th; |
| 65 | } | 141 | } |
| 66 | 142 | ||
| @@ -73,19 +149,18 @@ threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match) | |||
| 73 | th = thread__new(pid); | 149 | th = thread__new(pid); |
| 74 | if (th != NULL) { | 150 | if (th != NULL) { |
| 75 | rb_link_node(&th->rb_node, parent, p); | 151 | rb_link_node(&th->rb_node, parent, p); |
| 76 | rb_insert_color(&th->rb_node, threads); | 152 | rb_insert_color(&th->rb_node, &threads); |
| 77 | *last_match = th; | 153 | last_match = th; |
| 78 | } | 154 | } |
| 79 | 155 | ||
| 80 | return th; | 156 | return th; |
| 81 | } | 157 | } |
| 82 | 158 | ||
| 83 | struct thread * | 159 | struct thread *register_idle_thread(void) |
| 84 | register_idle_thread(struct rb_root *threads, struct thread **last_match) | ||
| 85 | { | 160 | { |
| 86 | struct thread *thread = threads__findnew(0, threads, last_match); | 161 | struct thread *thread = threads__findnew(0); |
| 87 | 162 | ||
| 88 | if (!thread || thread__set_comm(thread, "[init]")) { | 163 | if (!thread || thread__set_comm(thread, "swapper")) { |
| 89 | fprintf(stderr, "problem inserting idle task.\n"); | 164 | fprintf(stderr, "problem inserting idle task.\n"); |
| 90 | exit(-1); | 165 | exit(-1); |
| 91 | } | 166 | } |
| @@ -93,79 +168,116 @@ register_idle_thread(struct rb_root *threads, struct thread **last_match) | |||
| 93 | return thread; | 168 | return thread; |
| 94 | } | 169 | } |
| 95 | 170 | ||
| 96 | void thread__insert_map(struct thread *self, struct map *map) | 171 | static void thread__remove_overlappings(struct thread *self, struct map *map) |
| 97 | { | 172 | { |
| 98 | struct map *pos, *tmp; | 173 | struct rb_root *root = &self->maps[map->type]; |
| 174 | struct rb_node *next = rb_first(root); | ||
| 99 | 175 | ||
| 100 | list_for_each_entry_safe(pos, tmp, &self->maps, node) { | 176 | while (next) { |
| 101 | if (map__overlap(pos, map)) { | 177 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 102 | if (verbose >= 2) { | 178 | next = rb_next(&pos->rb_node); |
| 103 | printf("overlapping maps:\n"); | ||
| 104 | map__fprintf(map, stdout); | ||
| 105 | map__fprintf(pos, stdout); | ||
| 106 | } | ||
| 107 | 179 | ||
| 108 | if (map->start <= pos->start && map->end > pos->start) | 180 | if (!map__overlap(pos, map)) |
| 109 | pos->start = map->end; | 181 | continue; |
| 110 | 182 | ||
| 111 | if (map->end >= pos->end && map->start < pos->end) | 183 | if (verbose >= 2) { |
| 112 | pos->end = map->start; | 184 | fputs("overlapping maps:\n", stderr); |
| 185 | map__fprintf(map, stderr); | ||
| 186 | map__fprintf(pos, stderr); | ||
| 187 | } | ||
| 113 | 188 | ||
| 114 | if (verbose >= 2) { | 189 | rb_erase(&pos->rb_node, root); |
| 115 | printf("after collision:\n"); | 190 | /* |
| 116 | map__fprintf(pos, stdout); | 191 | * We may have references to this map, for instance in some |
| 117 | } | 192 | * hist_entry instances, so just move them to a separate |
| 193 | * list. | ||
| 194 | */ | ||
| 195 | list_add_tail(&pos->node, &self->removed_maps[map->type]); | ||
| 196 | } | ||
| 197 | } | ||
| 118 | 198 | ||
| 119 | if (pos->start >= pos->end) { | 199 | void maps__insert(struct rb_root *maps, struct map *map) |
| 120 | list_del_init(&pos->node); | 200 | { |
| 121 | free(pos); | 201 | struct rb_node **p = &maps->rb_node; |
| 122 | } | 202 | struct rb_node *parent = NULL; |
| 123 | } | 203 | const u64 ip = map->start; |
| 204 | struct map *m; | ||
| 205 | |||
| 206 | while (*p != NULL) { | ||
| 207 | parent = *p; | ||
| 208 | m = rb_entry(parent, struct map, rb_node); | ||
| 209 | if (ip < m->start) | ||
| 210 | p = &(*p)->rb_left; | ||
| 211 | else | ||
| 212 | p = &(*p)->rb_right; | ||
| 124 | } | 213 | } |
| 125 | 214 | ||
| 126 | list_add_tail(&map->node, &self->maps); | 215 | rb_link_node(&map->rb_node, parent, p); |
| 216 | rb_insert_color(&map->rb_node, maps); | ||
| 127 | } | 217 | } |
| 128 | 218 | ||
| 129 | int thread__fork(struct thread *self, struct thread *parent) | 219 | struct map *maps__find(struct rb_root *maps, u64 ip) |
| 130 | { | 220 | { |
| 131 | struct map *map; | 221 | struct rb_node **p = &maps->rb_node; |
| 222 | struct rb_node *parent = NULL; | ||
| 223 | struct map *m; | ||
| 132 | 224 | ||
| 133 | if (self->comm) | 225 | while (*p != NULL) { |
| 134 | free(self->comm); | 226 | parent = *p; |
| 135 | self->comm = strdup(parent->comm); | 227 | m = rb_entry(parent, struct map, rb_node); |
| 136 | if (!self->comm) | 228 | if (ip < m->start) |
| 137 | return -ENOMEM; | 229 | p = &(*p)->rb_left; |
| 230 | else if (ip > m->end) | ||
| 231 | p = &(*p)->rb_right; | ||
| 232 | else | ||
| 233 | return m; | ||
| 234 | } | ||
| 235 | |||
| 236 | return NULL; | ||
| 237 | } | ||
| 238 | |||
| 239 | void thread__insert_map(struct thread *self, struct map *map) | ||
| 240 | { | ||
| 241 | thread__remove_overlappings(self, map); | ||
| 242 | maps__insert(&self->maps[map->type], map); | ||
| 243 | } | ||
| 138 | 244 | ||
| 139 | list_for_each_entry(map, &parent->maps, node) { | 245 | static int thread__clone_maps(struct thread *self, struct thread *parent, |
| 246 | enum map_type type) | ||
| 247 | { | ||
| 248 | struct rb_node *nd; | ||
| 249 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { | ||
| 250 | struct map *map = rb_entry(nd, struct map, rb_node); | ||
| 140 | struct map *new = map__clone(map); | 251 | struct map *new = map__clone(map); |
| 141 | if (!new) | 252 | if (new == NULL) |
| 142 | return -ENOMEM; | 253 | return -ENOMEM; |
| 143 | thread__insert_map(self, new); | 254 | thread__insert_map(self, new); |
| 144 | } | 255 | } |
| 145 | |||
| 146 | return 0; | 256 | return 0; |
| 147 | } | 257 | } |
| 148 | 258 | ||
| 149 | struct map *thread__find_map(struct thread *self, u64 ip) | 259 | int thread__fork(struct thread *self, struct thread *parent) |
| 150 | { | 260 | { |
| 151 | struct map *pos; | 261 | int i; |
| 152 | 262 | ||
| 153 | if (self == NULL) | 263 | if (self->comm) |
| 154 | return NULL; | 264 | free(self->comm); |
| 155 | 265 | self->comm = strdup(parent->comm); | |
| 156 | list_for_each_entry(pos, &self->maps, node) | 266 | if (!self->comm) |
| 157 | if (ip >= pos->start && ip <= pos->end) | 267 | return -ENOMEM; |
| 158 | return pos; | ||
| 159 | 268 | ||
| 160 | return NULL; | 269 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 270 | if (thread__clone_maps(self, parent, i) < 0) | ||
| 271 | return -ENOMEM; | ||
| 272 | return 0; | ||
| 161 | } | 273 | } |
| 162 | 274 | ||
| 163 | size_t threads__fprintf(FILE *fp, struct rb_root *threads) | 275 | size_t threads__fprintf(FILE *fp) |
| 164 | { | 276 | { |
| 165 | size_t ret = 0; | 277 | size_t ret = 0; |
| 166 | struct rb_node *nd; | 278 | struct rb_node *nd; |
| 167 | 279 | ||
| 168 | for (nd = rb_first(threads); nd; nd = rb_next(nd)) { | 280 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { |
| 169 | struct thread *pos = rb_entry(nd, struct thread, rb_node); | 281 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 170 | 282 | ||
| 171 | ret += thread__fprintf(pos, fp); | 283 | ret += thread__fprintf(pos, fp); |
| @@ -173,3 +285,15 @@ size_t threads__fprintf(FILE *fp, struct rb_root *threads) | |||
| 173 | 285 | ||
| 174 | return ret; | 286 | return ret; |
| 175 | } | 287 | } |
| 288 | |||
| 289 | struct symbol *thread__find_symbol(struct thread *self, | ||
| 290 | enum map_type type, u64 addr, | ||
| 291 | symbol_filter_t filter) | ||
| 292 | { | ||
| 293 | struct map *map = thread__find_map(self, type, addr); | ||
| 294 | |||
| 295 | if (map != NULL) | ||
| 296 | return map__find_symbol(map, map->map_ip(map, addr), filter); | ||
| 297 | |||
| 298 | return NULL; | ||
| 299 | } | ||
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 634f2809a342..686d6e914d9e 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h | |||
| @@ -1,21 +1,56 @@ | |||
| 1 | #ifndef __PERF_THREAD_H | ||
| 2 | #define __PERF_THREAD_H | ||
| 3 | |||
| 1 | #include <linux/rbtree.h> | 4 | #include <linux/rbtree.h> |
| 2 | #include <linux/list.h> | ||
| 3 | #include <unistd.h> | 5 | #include <unistd.h> |
| 4 | #include "symbol.h" | 6 | #include "symbol.h" |
| 5 | 7 | ||
| 6 | struct thread { | 8 | struct thread { |
| 7 | struct rb_node rb_node; | 9 | struct rb_node rb_node; |
| 8 | struct list_head maps; | 10 | struct rb_root maps[MAP__NR_TYPES]; |
| 9 | pid_t pid; | 11 | struct list_head removed_maps[MAP__NR_TYPES]; |
| 10 | char *comm; | 12 | pid_t pid; |
| 13 | bool use_modules; | ||
| 14 | char shortname[3]; | ||
| 15 | char *comm; | ||
| 16 | int comm_len; | ||
| 11 | }; | 17 | }; |
| 12 | 18 | ||
| 19 | void thread__init(struct thread *self, pid_t pid); | ||
| 13 | int thread__set_comm(struct thread *self, const char *comm); | 20 | int thread__set_comm(struct thread *self, const char *comm); |
| 14 | struct thread * | 21 | int thread__comm_len(struct thread *self); |
| 15 | threads__findnew(pid_t pid, struct rb_root *threads, struct thread **last_match); | 22 | struct thread *threads__findnew(pid_t pid); |
| 16 | struct thread * | 23 | struct thread *register_idle_thread(void); |
| 17 | register_idle_thread(struct rb_root *threads, struct thread **last_match); | ||
| 18 | void thread__insert_map(struct thread *self, struct map *map); | 24 | void thread__insert_map(struct thread *self, struct map *map); |
| 19 | int thread__fork(struct thread *self, struct thread *parent); | 25 | int thread__fork(struct thread *self, struct thread *parent); |
| 20 | struct map *thread__find_map(struct thread *self, u64 ip); | 26 | size_t thread__fprintf_maps(struct thread *self, FILE *fp); |
| 21 | size_t threads__fprintf(FILE *fp, struct rb_root *threads); | 27 | size_t threads__fprintf(FILE *fp); |
| 28 | |||
| 29 | void maps__insert(struct rb_root *maps, struct map *map); | ||
| 30 | struct map *maps__find(struct rb_root *maps, u64 addr); | ||
| 31 | |||
| 32 | static inline struct map *thread__find_map(struct thread *self, | ||
| 33 | enum map_type type, u64 addr) | ||
| 34 | { | ||
| 35 | return self ? maps__find(&self->maps[type], addr) : NULL; | ||
| 36 | } | ||
| 37 | |||
| 38 | static inline void __thread__insert_map(struct thread *self, struct map *map) | ||
| 39 | { | ||
| 40 | maps__insert(&self->maps[map->type], map); | ||
| 41 | } | ||
| 42 | |||
| 43 | void thread__find_addr_location(struct thread *self, u8 cpumode, | ||
| 44 | enum map_type type, u64 addr, | ||
| 45 | struct addr_location *al, | ||
| 46 | symbol_filter_t filter); | ||
| 47 | struct symbol *thread__find_symbol(struct thread *self, | ||
| 48 | enum map_type type, u64 addr, | ||
| 49 | symbol_filter_t filter); | ||
| 50 | |||
| 51 | static inline struct symbol * | ||
| 52 | thread__find_function(struct thread *self, u64 addr, symbol_filter_t filter) | ||
| 53 | { | ||
| 54 | return thread__find_symbol(self, MAP__FUNCTION, addr, filter); | ||
| 55 | } | ||
| 56 | #endif /* __PERF_THREAD_H */ | ||
diff --git a/tools/perf/util/trace-event-info.c b/tools/perf/util/trace-event-info.c index 6c9302a7274c..cace35595530 100644 --- a/tools/perf/util/trace-event-info.c +++ b/tools/perf/util/trace-event-info.c | |||
| @@ -33,11 +33,11 @@ | |||
| 33 | #include <ctype.h> | 33 | #include <ctype.h> |
| 34 | #include <errno.h> | 34 | #include <errno.h> |
| 35 | #include <stdbool.h> | 35 | #include <stdbool.h> |
| 36 | #include <linux/kernel.h> | ||
| 36 | 37 | ||
| 37 | #include "../perf.h" | 38 | #include "../perf.h" |
| 38 | #include "trace-event.h" | 39 | #include "trace-event.h" |
| 39 | 40 | ||
| 40 | |||
| 41 | #define VERSION "0.5" | 41 | #define VERSION "0.5" |
| 42 | 42 | ||
| 43 | #define _STR(x) #x | 43 | #define _STR(x) #x |
| @@ -458,7 +458,7 @@ static void read_proc_kallsyms(void) | |||
| 458 | static void read_ftrace_printk(void) | 458 | static void read_ftrace_printk(void) |
| 459 | { | 459 | { |
| 460 | unsigned int size, check_size; | 460 | unsigned int size, check_size; |
| 461 | const char *path; | 461 | char *path; |
| 462 | struct stat st; | 462 | struct stat st; |
| 463 | int ret; | 463 | int ret; |
| 464 | 464 | ||
| @@ -468,41 +468,48 @@ static void read_ftrace_printk(void) | |||
| 468 | /* not found */ | 468 | /* not found */ |
| 469 | size = 0; | 469 | size = 0; |
| 470 | write_or_die(&size, 4); | 470 | write_or_die(&size, 4); |
| 471 | return; | 471 | goto out; |
| 472 | } | 472 | } |
| 473 | size = get_size(path); | 473 | size = get_size(path); |
| 474 | write_or_die(&size, 4); | 474 | write_or_die(&size, 4); |
| 475 | check_size = copy_file(path); | 475 | check_size = copy_file(path); |
| 476 | if (size != check_size) | 476 | if (size != check_size) |
| 477 | die("error in size of file '%s'", path); | 477 | die("error in size of file '%s'", path); |
| 478 | 478 | out: | |
| 479 | put_tracing_file(path); | ||
| 479 | } | 480 | } |
| 480 | 481 | ||
| 481 | static struct tracepoint_path * | 482 | static struct tracepoint_path * |
| 482 | get_tracepoints_path(struct perf_counter_attr *pattrs, int nb_counters) | 483 | get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events) |
| 483 | { | 484 | { |
| 484 | struct tracepoint_path path, *ppath = &path; | 485 | struct tracepoint_path path, *ppath = &path; |
| 485 | int i; | 486 | int i, nr_tracepoints = 0; |
| 486 | 487 | ||
| 487 | for (i = 0; i < nb_counters; i++) { | 488 | for (i = 0; i < nb_events; i++) { |
| 488 | if (pattrs[i].type != PERF_TYPE_TRACEPOINT) | 489 | if (pattrs[i].type != PERF_TYPE_TRACEPOINT) |
| 489 | continue; | 490 | continue; |
| 491 | ++nr_tracepoints; | ||
| 490 | ppath->next = tracepoint_id_to_path(pattrs[i].config); | 492 | ppath->next = tracepoint_id_to_path(pattrs[i].config); |
| 491 | if (!ppath->next) | 493 | if (!ppath->next) |
| 492 | die("%s\n", "No memory to alloc tracepoints list"); | 494 | die("%s\n", "No memory to alloc tracepoints list"); |
| 493 | ppath = ppath->next; | 495 | ppath = ppath->next; |
| 494 | } | 496 | } |
| 495 | 497 | ||
| 496 | return path.next; | 498 | return nr_tracepoints > 0 ? path.next : NULL; |
| 497 | } | 499 | } |
| 498 | void read_tracing_data(struct perf_counter_attr *pattrs, int nb_counters) | 500 | |
| 501 | int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events) | ||
| 499 | { | 502 | { |
| 500 | char buf[BUFSIZ]; | 503 | char buf[BUFSIZ]; |
| 501 | struct tracepoint_path *tps; | 504 | struct tracepoint_path *tps = get_tracepoints_path(pattrs, nb_events); |
| 505 | |||
| 506 | /* | ||
| 507 | * What? No tracepoints? No sense writing anything here, bail out. | ||
| 508 | */ | ||
| 509 | if (tps == NULL) | ||
| 510 | return -1; | ||
| 502 | 511 | ||
| 503 | output_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); | 512 | output_fd = fd; |
| 504 | if (output_fd < 0) | ||
| 505 | die("creating file '%s'", output_file); | ||
| 506 | 513 | ||
| 507 | buf[0] = 23; | 514 | buf[0] = 23; |
| 508 | buf[1] = 8; | 515 | buf[1] = 8; |
| @@ -529,11 +536,11 @@ void read_tracing_data(struct perf_counter_attr *pattrs, int nb_counters) | |||
| 529 | page_size = getpagesize(); | 536 | page_size = getpagesize(); |
| 530 | write_or_die(&page_size, 4); | 537 | write_or_die(&page_size, 4); |
| 531 | 538 | ||
| 532 | tps = get_tracepoints_path(pattrs, nb_counters); | ||
| 533 | |||
| 534 | read_header_files(); | 539 | read_header_files(); |
| 535 | read_ftrace_files(tps); | 540 | read_ftrace_files(tps); |
| 536 | read_event_files(tps); | 541 | read_event_files(tps); |
| 537 | read_proc_kallsyms(); | 542 | read_proc_kallsyms(); |
| 538 | read_ftrace_printk(); | 543 | read_ftrace_printk(); |
| 544 | |||
| 545 | return 0; | ||
| 539 | } | 546 | } |
diff --git a/tools/perf/util/trace-event-parse.c b/tools/perf/util/trace-event-parse.c index 629e602d9405..0302405aa2ca 100644 --- a/tools/perf/util/trace-event-parse.c +++ b/tools/perf/util/trace-event-parse.c | |||
| @@ -40,12 +40,19 @@ int header_page_size_size; | |||
| 40 | int header_page_data_offset; | 40 | int header_page_data_offset; |
| 41 | int header_page_data_size; | 41 | int header_page_data_size; |
| 42 | 42 | ||
| 43 | int latency_format; | ||
| 44 | |||
| 43 | static char *input_buf; | 45 | static char *input_buf; |
| 44 | static unsigned long long input_buf_ptr; | 46 | static unsigned long long input_buf_ptr; |
| 45 | static unsigned long long input_buf_siz; | 47 | static unsigned long long input_buf_siz; |
| 46 | 48 | ||
| 47 | static int cpus; | 49 | static int cpus; |
| 48 | static int long_size; | 50 | static int long_size; |
| 51 | static int is_flag_field; | ||
| 52 | static int is_symbolic_field; | ||
| 53 | |||
| 54 | static struct format_field * | ||
| 55 | find_any_field(struct event *event, const char *name); | ||
| 49 | 56 | ||
| 50 | static void init_input_buf(char *buf, unsigned long long size) | 57 | static void init_input_buf(char *buf, unsigned long long size) |
| 51 | { | 58 | { |
| @@ -284,18 +291,19 @@ void parse_ftrace_printk(char *file, unsigned int size __unused) | |||
| 284 | char *line; | 291 | char *line; |
| 285 | char *next = NULL; | 292 | char *next = NULL; |
| 286 | char *addr_str; | 293 | char *addr_str; |
| 287 | int ret; | ||
| 288 | int i; | 294 | int i; |
| 289 | 295 | ||
| 290 | line = strtok_r(file, "\n", &next); | 296 | line = strtok_r(file, "\n", &next); |
| 291 | while (line) { | 297 | while (line) { |
| 298 | addr_str = strsep(&line, ":"); | ||
| 299 | if (!line) { | ||
| 300 | warning("error parsing print strings"); | ||
| 301 | break; | ||
| 302 | } | ||
| 292 | item = malloc_or_die(sizeof(*item)); | 303 | item = malloc_or_die(sizeof(*item)); |
| 293 | ret = sscanf(line, "%as : %as", | ||
| 294 | (float *)(void *)&addr_str, /* workaround gcc warning */ | ||
| 295 | (float *)(void *)&item->printk); | ||
| 296 | item->addr = strtoull(addr_str, NULL, 16); | 304 | item->addr = strtoull(addr_str, NULL, 16); |
| 297 | free(addr_str); | 305 | /* fmt still has a space, skip it */ |
| 298 | 306 | item->printk = strdup(line+1); | |
| 299 | item->next = list; | 307 | item->next = list; |
| 300 | list = item; | 308 | list = item; |
| 301 | line = strtok_r(NULL, "\n", &next); | 309 | line = strtok_r(NULL, "\n", &next); |
| @@ -522,7 +530,10 @@ static enum event_type __read_token(char **tok) | |||
| 522 | last_ch = ch; | 530 | last_ch = ch; |
| 523 | ch = __read_char(); | 531 | ch = __read_char(); |
| 524 | buf[i++] = ch; | 532 | buf[i++] = ch; |
| 525 | } while (ch != quote_ch && last_ch != '\\'); | 533 | /* the '\' '\' will cancel itself */ |
| 534 | if (ch == '\\' && last_ch == '\\') | ||
| 535 | last_ch = 0; | ||
| 536 | } while (ch != quote_ch || last_ch == '\\'); | ||
| 526 | /* remove the last quote */ | 537 | /* remove the last quote */ |
| 527 | i--; | 538 | i--; |
| 528 | goto out; | 539 | goto out; |
| @@ -610,7 +621,7 @@ static enum event_type read_token_item(char **tok) | |||
| 610 | static int test_type(enum event_type type, enum event_type expect) | 621 | static int test_type(enum event_type type, enum event_type expect) |
| 611 | { | 622 | { |
| 612 | if (type != expect) { | 623 | if (type != expect) { |
| 613 | die("Error: expected type %d but read %d", | 624 | warning("Error: expected type %d but read %d", |
| 614 | expect, type); | 625 | expect, type); |
| 615 | return -1; | 626 | return -1; |
| 616 | } | 627 | } |
| @@ -618,16 +629,16 @@ static int test_type(enum event_type type, enum event_type expect) | |||
| 618 | } | 629 | } |
| 619 | 630 | ||
| 620 | static int test_type_token(enum event_type type, char *token, | 631 | static int test_type_token(enum event_type type, char *token, |
| 621 | enum event_type expect, char *expect_tok) | 632 | enum event_type expect, const char *expect_tok) |
| 622 | { | 633 | { |
| 623 | if (type != expect) { | 634 | if (type != expect) { |
| 624 | die("Error: expected type %d but read %d", | 635 | warning("Error: expected type %d but read %d", |
| 625 | expect, type); | 636 | expect, type); |
| 626 | return -1; | 637 | return -1; |
| 627 | } | 638 | } |
| 628 | 639 | ||
| 629 | if (strcmp(token, expect_tok) != 0) { | 640 | if (strcmp(token, expect_tok) != 0) { |
| 630 | die("Error: expected '%s' but read '%s'", | 641 | warning("Error: expected '%s' but read '%s'", |
| 631 | expect_tok, token); | 642 | expect_tok, token); |
| 632 | return -1; | 643 | return -1; |
| 633 | } | 644 | } |
| @@ -650,7 +661,7 @@ static int read_expect_type(enum event_type expect, char **tok) | |||
| 650 | return __read_expect_type(expect, tok, 1); | 661 | return __read_expect_type(expect, tok, 1); |
| 651 | } | 662 | } |
| 652 | 663 | ||
| 653 | static int __read_expected(enum event_type expect, char *str, int newline_ok) | 664 | static int __read_expected(enum event_type expect, const char *str, int newline_ok) |
| 654 | { | 665 | { |
| 655 | enum event_type type; | 666 | enum event_type type; |
| 656 | char *token; | 667 | char *token; |
| @@ -665,15 +676,15 @@ static int __read_expected(enum event_type expect, char *str, int newline_ok) | |||
| 665 | 676 | ||
| 666 | free_token(token); | 677 | free_token(token); |
| 667 | 678 | ||
| 668 | return 0; | 679 | return ret; |
| 669 | } | 680 | } |
| 670 | 681 | ||
| 671 | static int read_expected(enum event_type expect, char *str) | 682 | static int read_expected(enum event_type expect, const char *str) |
| 672 | { | 683 | { |
| 673 | return __read_expected(expect, str, 1); | 684 | return __read_expected(expect, str, 1); |
| 674 | } | 685 | } |
| 675 | 686 | ||
| 676 | static int read_expected_item(enum event_type expect, char *str) | 687 | static int read_expected_item(enum event_type expect, const char *str) |
| 677 | { | 688 | { |
| 678 | return __read_expected(expect, str, 0); | 689 | return __read_expected(expect, str, 0); |
| 679 | } | 690 | } |
| @@ -682,10 +693,10 @@ static char *event_read_name(void) | |||
| 682 | { | 693 | { |
| 683 | char *token; | 694 | char *token; |
| 684 | 695 | ||
| 685 | if (read_expected(EVENT_ITEM, (char *)"name") < 0) | 696 | if (read_expected(EVENT_ITEM, "name") < 0) |
| 686 | return NULL; | 697 | return NULL; |
| 687 | 698 | ||
| 688 | if (read_expected(EVENT_OP, (char *)":") < 0) | 699 | if (read_expected(EVENT_OP, ":") < 0) |
| 689 | return NULL; | 700 | return NULL; |
| 690 | 701 | ||
| 691 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 702 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| @@ -703,10 +714,10 @@ static int event_read_id(void) | |||
| 703 | char *token; | 714 | char *token; |
| 704 | int id; | 715 | int id; |
| 705 | 716 | ||
| 706 | if (read_expected_item(EVENT_ITEM, (char *)"ID") < 0) | 717 | if (read_expected_item(EVENT_ITEM, "ID") < 0) |
| 707 | return -1; | 718 | return -1; |
| 708 | 719 | ||
| 709 | if (read_expected(EVENT_OP, (char *)":") < 0) | 720 | if (read_expected(EVENT_OP, ":") < 0) |
| 710 | return -1; | 721 | return -1; |
| 711 | 722 | ||
| 712 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 723 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| @@ -721,6 +732,24 @@ static int event_read_id(void) | |||
| 721 | return -1; | 732 | return -1; |
| 722 | } | 733 | } |
| 723 | 734 | ||
| 735 | static int field_is_string(struct format_field *field) | ||
| 736 | { | ||
| 737 | if ((field->flags & FIELD_IS_ARRAY) && | ||
| 738 | (!strstr(field->type, "char") || !strstr(field->type, "u8") || | ||
| 739 | !strstr(field->type, "s8"))) | ||
| 740 | return 1; | ||
| 741 | |||
| 742 | return 0; | ||
| 743 | } | ||
| 744 | |||
| 745 | static int field_is_dynamic(struct format_field *field) | ||
| 746 | { | ||
| 747 | if (!strcmp(field->type, "__data_loc")) | ||
| 748 | return 1; | ||
| 749 | |||
| 750 | return 0; | ||
| 751 | } | ||
| 752 | |||
| 724 | static int event_read_fields(struct event *event, struct format_field **fields) | 753 | static int event_read_fields(struct event *event, struct format_field **fields) |
| 725 | { | 754 | { |
| 726 | struct format_field *field = NULL; | 755 | struct format_field *field = NULL; |
| @@ -738,7 +767,7 @@ static int event_read_fields(struct event *event, struct format_field **fields) | |||
| 738 | 767 | ||
| 739 | count++; | 768 | count++; |
| 740 | 769 | ||
| 741 | if (test_type_token(type, token, EVENT_ITEM, (char *)"field")) | 770 | if (test_type_token(type, token, EVENT_ITEM, "field")) |
| 742 | goto fail; | 771 | goto fail; |
| 743 | free_token(token); | 772 | free_token(token); |
| 744 | 773 | ||
| @@ -753,7 +782,7 @@ static int event_read_fields(struct event *event, struct format_field **fields) | |||
| 753 | type = read_token(&token); | 782 | type = read_token(&token); |
| 754 | } | 783 | } |
| 755 | 784 | ||
| 756 | if (test_type_token(type, token, EVENT_OP, (char *)":") < 0) | 785 | if (test_type_token(type, token, EVENT_OP, ":") < 0) |
| 757 | return -1; | 786 | return -1; |
| 758 | 787 | ||
| 759 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 788 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| @@ -865,14 +894,20 @@ static int event_read_fields(struct event *event, struct format_field **fields) | |||
| 865 | free(brackets); | 894 | free(brackets); |
| 866 | } | 895 | } |
| 867 | 896 | ||
| 868 | if (test_type_token(type, token, EVENT_OP, (char *)";")) | 897 | if (field_is_string(field)) { |
| 898 | field->flags |= FIELD_IS_STRING; | ||
| 899 | if (field_is_dynamic(field)) | ||
| 900 | field->flags |= FIELD_IS_DYNAMIC; | ||
| 901 | } | ||
| 902 | |||
| 903 | if (test_type_token(type, token, EVENT_OP, ";")) | ||
| 869 | goto fail; | 904 | goto fail; |
| 870 | free_token(token); | 905 | free_token(token); |
| 871 | 906 | ||
| 872 | if (read_expected(EVENT_ITEM, (char *)"offset") < 0) | 907 | if (read_expected(EVENT_ITEM, "offset") < 0) |
| 873 | goto fail_expect; | 908 | goto fail_expect; |
| 874 | 909 | ||
| 875 | if (read_expected(EVENT_OP, (char *)":") < 0) | 910 | if (read_expected(EVENT_OP, ":") < 0) |
| 876 | goto fail_expect; | 911 | goto fail_expect; |
| 877 | 912 | ||
| 878 | if (read_expect_type(EVENT_ITEM, &token)) | 913 | if (read_expect_type(EVENT_ITEM, &token)) |
| @@ -880,13 +915,13 @@ static int event_read_fields(struct event *event, struct format_field **fields) | |||
| 880 | field->offset = strtoul(token, NULL, 0); | 915 | field->offset = strtoul(token, NULL, 0); |
| 881 | free_token(token); | 916 | free_token(token); |
| 882 | 917 | ||
| 883 | if (read_expected(EVENT_OP, (char *)";") < 0) | 918 | if (read_expected(EVENT_OP, ";") < 0) |
| 884 | goto fail_expect; | 919 | goto fail_expect; |
| 885 | 920 | ||
| 886 | if (read_expected(EVENT_ITEM, (char *)"size") < 0) | 921 | if (read_expected(EVENT_ITEM, "size") < 0) |
| 887 | goto fail_expect; | 922 | goto fail_expect; |
| 888 | 923 | ||
| 889 | if (read_expected(EVENT_OP, (char *)":") < 0) | 924 | if (read_expected(EVENT_OP, ":") < 0) |
| 890 | goto fail_expect; | 925 | goto fail_expect; |
| 891 | 926 | ||
| 892 | if (read_expect_type(EVENT_ITEM, &token)) | 927 | if (read_expect_type(EVENT_ITEM, &token)) |
| @@ -894,11 +929,34 @@ static int event_read_fields(struct event *event, struct format_field **fields) | |||
| 894 | field->size = strtoul(token, NULL, 0); | 929 | field->size = strtoul(token, NULL, 0); |
| 895 | free_token(token); | 930 | free_token(token); |
| 896 | 931 | ||
| 897 | if (read_expected(EVENT_OP, (char *)";") < 0) | 932 | if (read_expected(EVENT_OP, ";") < 0) |
| 898 | goto fail_expect; | 933 | goto fail_expect; |
| 899 | 934 | ||
| 900 | if (read_expect_type(EVENT_NEWLINE, &token) < 0) | 935 | type = read_token(&token); |
| 901 | goto fail; | 936 | if (type != EVENT_NEWLINE) { |
| 937 | /* newer versions of the kernel have a "signed" type */ | ||
| 938 | if (test_type_token(type, token, EVENT_ITEM, "signed")) | ||
| 939 | goto fail; | ||
| 940 | |||
| 941 | free_token(token); | ||
| 942 | |||
| 943 | if (read_expected(EVENT_OP, ":") < 0) | ||
| 944 | goto fail_expect; | ||
| 945 | |||
| 946 | if (read_expect_type(EVENT_ITEM, &token)) | ||
| 947 | goto fail; | ||
| 948 | |||
| 949 | if (strtoul(token, NULL, 0)) | ||
| 950 | field->flags |= FIELD_IS_SIGNED; | ||
| 951 | |||
| 952 | free_token(token); | ||
| 953 | if (read_expected(EVENT_OP, ";") < 0) | ||
| 954 | goto fail_expect; | ||
| 955 | |||
| 956 | if (read_expect_type(EVENT_NEWLINE, &token)) | ||
| 957 | goto fail; | ||
| 958 | } | ||
| 959 | |||
| 902 | free_token(token); | 960 | free_token(token); |
| 903 | 961 | ||
| 904 | *fields = field; | 962 | *fields = field; |
| @@ -921,10 +979,10 @@ static int event_read_format(struct event *event) | |||
| 921 | char *token; | 979 | char *token; |
| 922 | int ret; | 980 | int ret; |
| 923 | 981 | ||
| 924 | if (read_expected_item(EVENT_ITEM, (char *)"format") < 0) | 982 | if (read_expected_item(EVENT_ITEM, "format") < 0) |
| 925 | return -1; | 983 | return -1; |
| 926 | 984 | ||
| 927 | if (read_expected(EVENT_OP, (char *)":") < 0) | 985 | if (read_expected(EVENT_OP, ":") < 0) |
| 928 | return -1; | 986 | return -1; |
| 929 | 987 | ||
| 930 | if (read_expect_type(EVENT_NEWLINE, &token)) | 988 | if (read_expect_type(EVENT_NEWLINE, &token)) |
| @@ -984,7 +1042,7 @@ process_cond(struct event *event, struct print_arg *top, char **tok) | |||
| 984 | 1042 | ||
| 985 | *tok = NULL; | 1043 | *tok = NULL; |
| 986 | type = process_arg(event, left, &token); | 1044 | type = process_arg(event, left, &token); |
| 987 | if (test_type_token(type, token, EVENT_OP, (char *)":")) | 1045 | if (test_type_token(type, token, EVENT_OP, ":")) |
| 988 | goto out_free; | 1046 | goto out_free; |
| 989 | 1047 | ||
| 990 | arg->op.op = token; | 1048 | arg->op.op = token; |
| @@ -1004,6 +1062,35 @@ out_free: | |||
| 1004 | return EVENT_ERROR; | 1062 | return EVENT_ERROR; |
| 1005 | } | 1063 | } |
| 1006 | 1064 | ||
| 1065 | static enum event_type | ||
| 1066 | process_array(struct event *event, struct print_arg *top, char **tok) | ||
| 1067 | { | ||
| 1068 | struct print_arg *arg; | ||
| 1069 | enum event_type type; | ||
| 1070 | char *token = NULL; | ||
| 1071 | |||
| 1072 | arg = malloc_or_die(sizeof(*arg)); | ||
| 1073 | memset(arg, 0, sizeof(*arg)); | ||
| 1074 | |||
| 1075 | *tok = NULL; | ||
| 1076 | type = process_arg(event, arg, &token); | ||
| 1077 | if (test_type_token(type, token, EVENT_OP, "]")) | ||
| 1078 | goto out_free; | ||
| 1079 | |||
| 1080 | top->op.right = arg; | ||
| 1081 | |||
| 1082 | free_token(token); | ||
| 1083 | type = read_token_item(&token); | ||
| 1084 | *tok = token; | ||
| 1085 | |||
| 1086 | return type; | ||
| 1087 | |||
| 1088 | out_free: | ||
| 1089 | free_token(*tok); | ||
| 1090 | free_arg(arg); | ||
| 1091 | return EVENT_ERROR; | ||
| 1092 | } | ||
| 1093 | |||
| 1007 | static int get_op_prio(char *op) | 1094 | static int get_op_prio(char *op) |
| 1008 | { | 1095 | { |
| 1009 | if (!op[1]) { | 1096 | if (!op[1]) { |
| @@ -1128,6 +1215,8 @@ process_op(struct event *event, struct print_arg *arg, char **tok) | |||
| 1128 | strcmp(token, "*") == 0 || | 1215 | strcmp(token, "*") == 0 || |
| 1129 | strcmp(token, "^") == 0 || | 1216 | strcmp(token, "^") == 0 || |
| 1130 | strcmp(token, "/") == 0 || | 1217 | strcmp(token, "/") == 0 || |
| 1218 | strcmp(token, "<") == 0 || | ||
| 1219 | strcmp(token, ">") == 0 || | ||
| 1131 | strcmp(token, "==") == 0 || | 1220 | strcmp(token, "==") == 0 || |
| 1132 | strcmp(token, "!=") == 0) { | 1221 | strcmp(token, "!=") == 0) { |
| 1133 | 1222 | ||
| @@ -1144,17 +1233,46 @@ process_op(struct event *event, struct print_arg *arg, char **tok) | |||
| 1144 | 1233 | ||
| 1145 | right = malloc_or_die(sizeof(*right)); | 1234 | right = malloc_or_die(sizeof(*right)); |
| 1146 | 1235 | ||
| 1147 | type = process_arg(event, right, tok); | 1236 | type = read_token_item(&token); |
| 1237 | *tok = token; | ||
| 1238 | |||
| 1239 | /* could just be a type pointer */ | ||
| 1240 | if ((strcmp(arg->op.op, "*") == 0) && | ||
| 1241 | type == EVENT_DELIM && (strcmp(token, ")") == 0)) { | ||
| 1242 | if (left->type != PRINT_ATOM) | ||
| 1243 | die("bad pointer type"); | ||
| 1244 | left->atom.atom = realloc(left->atom.atom, | ||
| 1245 | sizeof(left->atom.atom) + 3); | ||
| 1246 | strcat(left->atom.atom, " *"); | ||
| 1247 | *arg = *left; | ||
| 1248 | free(arg); | ||
| 1249 | |||
| 1250 | return type; | ||
| 1251 | } | ||
| 1252 | |||
| 1253 | type = process_arg_token(event, right, tok, type); | ||
| 1148 | 1254 | ||
| 1149 | arg->op.right = right; | 1255 | arg->op.right = right; |
| 1150 | 1256 | ||
| 1257 | } else if (strcmp(token, "[") == 0) { | ||
| 1258 | |||
| 1259 | left = malloc_or_die(sizeof(*left)); | ||
| 1260 | *left = *arg; | ||
| 1261 | |||
| 1262 | arg->type = PRINT_OP; | ||
| 1263 | arg->op.op = token; | ||
| 1264 | arg->op.left = left; | ||
| 1265 | |||
| 1266 | arg->op.prio = 0; | ||
| 1267 | type = process_array(event, arg, tok); | ||
| 1268 | |||
| 1151 | } else { | 1269 | } else { |
| 1152 | die("unknown op '%s'", token); | 1270 | warning("unknown op '%s'", token); |
| 1271 | event->flags |= EVENT_FL_FAILED; | ||
| 1153 | /* the arg is now the left side */ | 1272 | /* the arg is now the left side */ |
| 1154 | return EVENT_NONE; | 1273 | return EVENT_NONE; |
| 1155 | } | 1274 | } |
| 1156 | 1275 | ||
| 1157 | |||
| 1158 | if (type == EVENT_OP) { | 1276 | if (type == EVENT_OP) { |
| 1159 | int prio; | 1277 | int prio; |
| 1160 | 1278 | ||
| @@ -1178,7 +1296,7 @@ process_entry(struct event *event __unused, struct print_arg *arg, | |||
| 1178 | char *field; | 1296 | char *field; |
| 1179 | char *token; | 1297 | char *token; |
| 1180 | 1298 | ||
| 1181 | if (read_expected(EVENT_OP, (char *)"->") < 0) | 1299 | if (read_expected(EVENT_OP, "->") < 0) |
| 1182 | return EVENT_ERROR; | 1300 | return EVENT_ERROR; |
| 1183 | 1301 | ||
| 1184 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 1302 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| @@ -1188,6 +1306,16 @@ process_entry(struct event *event __unused, struct print_arg *arg, | |||
| 1188 | arg->type = PRINT_FIELD; | 1306 | arg->type = PRINT_FIELD; |
| 1189 | arg->field.name = field; | 1307 | arg->field.name = field; |
| 1190 | 1308 | ||
| 1309 | if (is_flag_field) { | ||
| 1310 | arg->field.field = find_any_field(event, arg->field.name); | ||
| 1311 | arg->field.field->flags |= FIELD_IS_FLAG; | ||
| 1312 | is_flag_field = 0; | ||
| 1313 | } else if (is_symbolic_field) { | ||
| 1314 | arg->field.field = find_any_field(event, arg->field.name); | ||
| 1315 | arg->field.field->flags |= FIELD_IS_SYMBOLIC; | ||
| 1316 | is_symbolic_field = 0; | ||
| 1317 | } | ||
| 1318 | |||
| 1191 | type = read_token(&token); | 1319 | type = read_token(&token); |
| 1192 | *tok = token; | 1320 | *tok = token; |
| 1193 | 1321 | ||
| @@ -1338,14 +1466,14 @@ process_fields(struct event *event, struct print_flag_sym **list, char **tok) | |||
| 1338 | do { | 1466 | do { |
| 1339 | free_token(token); | 1467 | free_token(token); |
| 1340 | type = read_token_item(&token); | 1468 | type = read_token_item(&token); |
| 1341 | if (test_type_token(type, token, EVENT_OP, (char *)"{")) | 1469 | if (test_type_token(type, token, EVENT_OP, "{")) |
| 1342 | break; | 1470 | break; |
| 1343 | 1471 | ||
| 1344 | arg = malloc_or_die(sizeof(*arg)); | 1472 | arg = malloc_or_die(sizeof(*arg)); |
| 1345 | 1473 | ||
| 1346 | free_token(token); | 1474 | free_token(token); |
| 1347 | type = process_arg(event, arg, &token); | 1475 | type = process_arg(event, arg, &token); |
| 1348 | if (test_type_token(type, token, EVENT_DELIM, (char *)",")) | 1476 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 1349 | goto out_free; | 1477 | goto out_free; |
| 1350 | 1478 | ||
| 1351 | field = malloc_or_die(sizeof(*field)); | 1479 | field = malloc_or_die(sizeof(*field)); |
| @@ -1356,7 +1484,7 @@ process_fields(struct event *event, struct print_flag_sym **list, char **tok) | |||
| 1356 | 1484 | ||
| 1357 | free_token(token); | 1485 | free_token(token); |
| 1358 | type = process_arg(event, arg, &token); | 1486 | type = process_arg(event, arg, &token); |
| 1359 | if (test_type_token(type, token, EVENT_OP, (char *)"}")) | 1487 | if (test_type_token(type, token, EVENT_OP, "}")) |
| 1360 | goto out_free; | 1488 | goto out_free; |
| 1361 | 1489 | ||
| 1362 | value = arg_eval(arg); | 1490 | value = arg_eval(arg); |
| @@ -1391,13 +1519,13 @@ process_flags(struct event *event, struct print_arg *arg, char **tok) | |||
| 1391 | memset(arg, 0, sizeof(*arg)); | 1519 | memset(arg, 0, sizeof(*arg)); |
| 1392 | arg->type = PRINT_FLAGS; | 1520 | arg->type = PRINT_FLAGS; |
| 1393 | 1521 | ||
| 1394 | if (read_expected_item(EVENT_DELIM, (char *)"(") < 0) | 1522 | if (read_expected_item(EVENT_DELIM, "(") < 0) |
| 1395 | return EVENT_ERROR; | 1523 | return EVENT_ERROR; |
| 1396 | 1524 | ||
| 1397 | field = malloc_or_die(sizeof(*field)); | 1525 | field = malloc_or_die(sizeof(*field)); |
| 1398 | 1526 | ||
| 1399 | type = process_arg(event, field, &token); | 1527 | type = process_arg(event, field, &token); |
| 1400 | if (test_type_token(type, token, EVENT_DELIM, (char *)",")) | 1528 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 1401 | goto out_free; | 1529 | goto out_free; |
| 1402 | 1530 | ||
| 1403 | arg->flags.field = field; | 1531 | arg->flags.field = field; |
| @@ -1408,11 +1536,11 @@ process_flags(struct event *event, struct print_arg *arg, char **tok) | |||
| 1408 | type = read_token_item(&token); | 1536 | type = read_token_item(&token); |
| 1409 | } | 1537 | } |
| 1410 | 1538 | ||
| 1411 | if (test_type_token(type, token, EVENT_DELIM, (char *)",")) | 1539 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 1412 | goto out_free; | 1540 | goto out_free; |
| 1413 | 1541 | ||
| 1414 | type = process_fields(event, &arg->flags.flags, &token); | 1542 | type = process_fields(event, &arg->flags.flags, &token); |
| 1415 | if (test_type_token(type, token, EVENT_DELIM, (char *)")")) | 1543 | if (test_type_token(type, token, EVENT_DELIM, ")")) |
| 1416 | goto out_free; | 1544 | goto out_free; |
| 1417 | 1545 | ||
| 1418 | free_token(token); | 1546 | free_token(token); |
| @@ -1434,19 +1562,19 @@ process_symbols(struct event *event, struct print_arg *arg, char **tok) | |||
| 1434 | memset(arg, 0, sizeof(*arg)); | 1562 | memset(arg, 0, sizeof(*arg)); |
| 1435 | arg->type = PRINT_SYMBOL; | 1563 | arg->type = PRINT_SYMBOL; |
| 1436 | 1564 | ||
| 1437 | if (read_expected_item(EVENT_DELIM, (char *)"(") < 0) | 1565 | if (read_expected_item(EVENT_DELIM, "(") < 0) |
| 1438 | return EVENT_ERROR; | 1566 | return EVENT_ERROR; |
| 1439 | 1567 | ||
| 1440 | field = malloc_or_die(sizeof(*field)); | 1568 | field = malloc_or_die(sizeof(*field)); |
| 1441 | 1569 | ||
| 1442 | type = process_arg(event, field, &token); | 1570 | type = process_arg(event, field, &token); |
| 1443 | if (test_type_token(type, token, EVENT_DELIM, (char *)",")) | 1571 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 1444 | goto out_free; | 1572 | goto out_free; |
| 1445 | 1573 | ||
| 1446 | arg->symbol.field = field; | 1574 | arg->symbol.field = field; |
| 1447 | 1575 | ||
| 1448 | type = process_fields(event, &arg->symbol.symbols, &token); | 1576 | type = process_fields(event, &arg->symbol.symbols, &token); |
| 1449 | if (test_type_token(type, token, EVENT_DELIM, (char *)")")) | 1577 | if (test_type_token(type, token, EVENT_DELIM, ")")) |
| 1450 | goto out_free; | 1578 | goto out_free; |
| 1451 | 1579 | ||
| 1452 | free_token(token); | 1580 | free_token(token); |
| @@ -1463,7 +1591,6 @@ process_paren(struct event *event, struct print_arg *arg, char **tok) | |||
| 1463 | { | 1591 | { |
| 1464 | struct print_arg *item_arg; | 1592 | struct print_arg *item_arg; |
| 1465 | enum event_type type; | 1593 | enum event_type type; |
| 1466 | int ptr_cast = 0; | ||
| 1467 | char *token; | 1594 | char *token; |
| 1468 | 1595 | ||
| 1469 | type = process_arg(event, arg, &token); | 1596 | type = process_arg(event, arg, &token); |
| @@ -1471,28 +1598,13 @@ process_paren(struct event *event, struct print_arg *arg, char **tok) | |||
| 1471 | if (type == EVENT_ERROR) | 1598 | if (type == EVENT_ERROR) |
| 1472 | return EVENT_ERROR; | 1599 | return EVENT_ERROR; |
| 1473 | 1600 | ||
| 1474 | if (type == EVENT_OP) { | 1601 | if (type == EVENT_OP) |
| 1475 | /* handle the ptr casts */ | 1602 | type = process_op(event, arg, &token); |
| 1476 | if (!strcmp(token, "*")) { | ||
| 1477 | /* | ||
| 1478 | * FIXME: should we zapp whitespaces before ')' ? | ||
| 1479 | * (may require a peek_token_item()) | ||
| 1480 | */ | ||
| 1481 | if (__peek_char() == ')') { | ||
| 1482 | ptr_cast = 1; | ||
| 1483 | free_token(token); | ||
| 1484 | type = read_token_item(&token); | ||
| 1485 | } | ||
| 1486 | } | ||
| 1487 | if (!ptr_cast) { | ||
| 1488 | type = process_op(event, arg, &token); | ||
| 1489 | 1603 | ||
| 1490 | if (type == EVENT_ERROR) | 1604 | if (type == EVENT_ERROR) |
| 1491 | return EVENT_ERROR; | 1605 | return EVENT_ERROR; |
| 1492 | } | ||
| 1493 | } | ||
| 1494 | 1606 | ||
| 1495 | if (test_type_token(type, token, EVENT_DELIM, (char *)")")) { | 1607 | if (test_type_token(type, token, EVENT_DELIM, ")")) { |
| 1496 | free_token(token); | 1608 | free_token(token); |
| 1497 | return EVENT_ERROR; | 1609 | return EVENT_ERROR; |
| 1498 | } | 1610 | } |
| @@ -1516,13 +1628,6 @@ process_paren(struct event *event, struct print_arg *arg, char **tok) | |||
| 1516 | item_arg = malloc_or_die(sizeof(*item_arg)); | 1628 | item_arg = malloc_or_die(sizeof(*item_arg)); |
| 1517 | 1629 | ||
| 1518 | arg->type = PRINT_TYPE; | 1630 | arg->type = PRINT_TYPE; |
| 1519 | if (ptr_cast) { | ||
| 1520 | char *old = arg->atom.atom; | ||
| 1521 | |||
| 1522 | arg->atom.atom = malloc_or_die(strlen(old + 3)); | ||
| 1523 | sprintf(arg->atom.atom, "%s *", old); | ||
| 1524 | free(old); | ||
| 1525 | } | ||
| 1526 | arg->typecast.type = arg->atom.atom; | 1631 | arg->typecast.type = arg->atom.atom; |
| 1527 | arg->typecast.item = item_arg; | 1632 | arg->typecast.item = item_arg; |
| 1528 | type = process_arg_token(event, item_arg, &token, type); | 1633 | type = process_arg_token(event, item_arg, &token, type); |
| @@ -1540,7 +1645,7 @@ process_str(struct event *event __unused, struct print_arg *arg, char **tok) | |||
| 1540 | enum event_type type; | 1645 | enum event_type type; |
| 1541 | char *token; | 1646 | char *token; |
| 1542 | 1647 | ||
| 1543 | if (read_expected(EVENT_DELIM, (char *)"(") < 0) | 1648 | if (read_expected(EVENT_DELIM, "(") < 0) |
| 1544 | return EVENT_ERROR; | 1649 | return EVENT_ERROR; |
| 1545 | 1650 | ||
| 1546 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 1651 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| @@ -1550,7 +1655,7 @@ process_str(struct event *event __unused, struct print_arg *arg, char **tok) | |||
| 1550 | arg->string.string = token; | 1655 | arg->string.string = token; |
| 1551 | arg->string.offset = -1; | 1656 | arg->string.offset = -1; |
| 1552 | 1657 | ||
| 1553 | if (read_expected(EVENT_DELIM, (char *)")") < 0) | 1658 | if (read_expected(EVENT_DELIM, ")") < 0) |
| 1554 | return EVENT_ERROR; | 1659 | return EVENT_ERROR; |
| 1555 | 1660 | ||
| 1556 | type = read_token(&token); | 1661 | type = read_token(&token); |
| @@ -1578,9 +1683,11 @@ process_arg_token(struct event *event, struct print_arg *arg, | |||
| 1578 | type = process_entry(event, arg, &token); | 1683 | type = process_entry(event, arg, &token); |
| 1579 | } else if (strcmp(token, "__print_flags") == 0) { | 1684 | } else if (strcmp(token, "__print_flags") == 0) { |
| 1580 | free_token(token); | 1685 | free_token(token); |
| 1686 | is_flag_field = 1; | ||
| 1581 | type = process_flags(event, arg, &token); | 1687 | type = process_flags(event, arg, &token); |
| 1582 | } else if (strcmp(token, "__print_symbolic") == 0) { | 1688 | } else if (strcmp(token, "__print_symbolic") == 0) { |
| 1583 | free_token(token); | 1689 | free_token(token); |
| 1690 | is_symbolic_field = 1; | ||
| 1584 | type = process_symbols(event, arg, &token); | 1691 | type = process_symbols(event, arg, &token); |
| 1585 | } else if (strcmp(token, "__get_str") == 0) { | 1692 | } else if (strcmp(token, "__get_str") == 0) { |
| 1586 | free_token(token); | 1693 | free_token(token); |
| @@ -1637,12 +1744,18 @@ process_arg_token(struct event *event, struct print_arg *arg, | |||
| 1637 | 1744 | ||
| 1638 | static int event_read_print_args(struct event *event, struct print_arg **list) | 1745 | static int event_read_print_args(struct event *event, struct print_arg **list) |
| 1639 | { | 1746 | { |
| 1640 | enum event_type type; | 1747 | enum event_type type = EVENT_ERROR; |
| 1641 | struct print_arg *arg; | 1748 | struct print_arg *arg; |
| 1642 | char *token; | 1749 | char *token; |
| 1643 | int args = 0; | 1750 | int args = 0; |
| 1644 | 1751 | ||
| 1645 | do { | 1752 | do { |
| 1753 | if (type == EVENT_NEWLINE) { | ||
| 1754 | free_token(token); | ||
| 1755 | type = read_token_item(&token); | ||
| 1756 | continue; | ||
| 1757 | } | ||
| 1758 | |||
| 1646 | arg = malloc_or_die(sizeof(*arg)); | 1759 | arg = malloc_or_die(sizeof(*arg)); |
| 1647 | memset(arg, 0, sizeof(*arg)); | 1760 | memset(arg, 0, sizeof(*arg)); |
| 1648 | 1761 | ||
| @@ -1683,18 +1796,19 @@ static int event_read_print(struct event *event) | |||
| 1683 | char *token; | 1796 | char *token; |
| 1684 | int ret; | 1797 | int ret; |
| 1685 | 1798 | ||
| 1686 | if (read_expected_item(EVENT_ITEM, (char *)"print") < 0) | 1799 | if (read_expected_item(EVENT_ITEM, "print") < 0) |
| 1687 | return -1; | 1800 | return -1; |
| 1688 | 1801 | ||
| 1689 | if (read_expected(EVENT_ITEM, (char *)"fmt") < 0) | 1802 | if (read_expected(EVENT_ITEM, "fmt") < 0) |
| 1690 | return -1; | 1803 | return -1; |
| 1691 | 1804 | ||
| 1692 | if (read_expected(EVENT_OP, (char *)":") < 0) | 1805 | if (read_expected(EVENT_OP, ":") < 0) |
| 1693 | return -1; | 1806 | return -1; |
| 1694 | 1807 | ||
| 1695 | if (read_expect_type(EVENT_DQUOTE, &token) < 0) | 1808 | if (read_expect_type(EVENT_DQUOTE, &token) < 0) |
| 1696 | goto fail; | 1809 | goto fail; |
| 1697 | 1810 | ||
| 1811 | concat: | ||
| 1698 | event->print_fmt.format = token; | 1812 | event->print_fmt.format = token; |
| 1699 | event->print_fmt.args = NULL; | 1813 | event->print_fmt.args = NULL; |
| 1700 | 1814 | ||
| @@ -1704,7 +1818,22 @@ static int event_read_print(struct event *event) | |||
| 1704 | if (type == EVENT_NONE) | 1818 | if (type == EVENT_NONE) |
| 1705 | return 0; | 1819 | return 0; |
| 1706 | 1820 | ||
| 1707 | if (test_type_token(type, token, EVENT_DELIM, (char *)",")) | 1821 | /* Handle concatination of print lines */ |
| 1822 | if (type == EVENT_DQUOTE) { | ||
| 1823 | char *cat; | ||
| 1824 | |||
| 1825 | cat = malloc_or_die(strlen(event->print_fmt.format) + | ||
| 1826 | strlen(token) + 1); | ||
| 1827 | strcpy(cat, event->print_fmt.format); | ||
| 1828 | strcat(cat, token); | ||
| 1829 | free_token(token); | ||
| 1830 | free_token(event->print_fmt.format); | ||
| 1831 | event->print_fmt.format = NULL; | ||
| 1832 | token = cat; | ||
| 1833 | goto concat; | ||
| 1834 | } | ||
| 1835 | |||
| 1836 | if (test_type_token(type, token, EVENT_DELIM, ",")) | ||
| 1708 | goto fail; | 1837 | goto fail; |
| 1709 | 1838 | ||
| 1710 | free_token(token); | 1839 | free_token(token); |
| @@ -1713,7 +1842,7 @@ static int event_read_print(struct event *event) | |||
| 1713 | if (ret < 0) | 1842 | if (ret < 0) |
| 1714 | return -1; | 1843 | return -1; |
| 1715 | 1844 | ||
| 1716 | return 0; | 1845 | return ret; |
| 1717 | 1846 | ||
| 1718 | fail: | 1847 | fail: |
| 1719 | free_token(token); | 1848 | free_token(token); |
| @@ -1759,7 +1888,7 @@ find_any_field(struct event *event, const char *name) | |||
| 1759 | return find_field(event, name); | 1888 | return find_field(event, name); |
| 1760 | } | 1889 | } |
| 1761 | 1890 | ||
| 1762 | static unsigned long long read_size(void *ptr, int size) | 1891 | unsigned long long read_size(void *ptr, int size) |
| 1763 | { | 1892 | { |
| 1764 | switch (size) { | 1893 | switch (size) { |
| 1765 | case 1: | 1894 | case 1: |
| @@ -1776,6 +1905,29 @@ static unsigned long long read_size(void *ptr, int size) | |||
| 1776 | } | 1905 | } |
| 1777 | } | 1906 | } |
| 1778 | 1907 | ||
| 1908 | unsigned long long | ||
| 1909 | raw_field_value(struct event *event, const char *name, void *data) | ||
| 1910 | { | ||
| 1911 | struct format_field *field; | ||
| 1912 | |||
| 1913 | field = find_any_field(event, name); | ||
| 1914 | if (!field) | ||
| 1915 | return 0ULL; | ||
| 1916 | |||
| 1917 | return read_size(data + field->offset, field->size); | ||
| 1918 | } | ||
| 1919 | |||
| 1920 | void *raw_field_ptr(struct event *event, const char *name, void *data) | ||
| 1921 | { | ||
| 1922 | struct format_field *field; | ||
| 1923 | |||
| 1924 | field = find_any_field(event, name); | ||
| 1925 | if (!field) | ||
| 1926 | return NULL; | ||
| 1927 | |||
| 1928 | return data + field->offset; | ||
| 1929 | } | ||
| 1930 | |||
| 1779 | static int get_common_info(const char *type, int *offset, int *size) | 1931 | static int get_common_info(const char *type, int *offset, int *size) |
| 1780 | { | 1932 | { |
| 1781 | struct event *event; | 1933 | struct event *event; |
| @@ -1799,40 +1951,70 @@ static int get_common_info(const char *type, int *offset, int *size) | |||
| 1799 | return 0; | 1951 | return 0; |
| 1800 | } | 1952 | } |
| 1801 | 1953 | ||
| 1802 | static int parse_common_type(void *data) | 1954 | static int __parse_common(void *data, int *size, int *offset, |
| 1955 | const char *name) | ||
| 1803 | { | 1956 | { |
| 1804 | static int type_offset; | ||
| 1805 | static int type_size; | ||
| 1806 | int ret; | 1957 | int ret; |
| 1807 | 1958 | ||
| 1808 | if (!type_size) { | 1959 | if (!*size) { |
| 1809 | ret = get_common_info("common_type", | 1960 | ret = get_common_info(name, offset, size); |
| 1810 | &type_offset, | ||
| 1811 | &type_size); | ||
| 1812 | if (ret < 0) | 1961 | if (ret < 0) |
| 1813 | return ret; | 1962 | return ret; |
| 1814 | } | 1963 | } |
| 1815 | return read_size(data + type_offset, type_size); | 1964 | return read_size(data + *offset, *size); |
| 1816 | } | 1965 | } |
| 1817 | 1966 | ||
| 1818 | static int parse_common_pid(void *data) | 1967 | int trace_parse_common_type(void *data) |
| 1968 | { | ||
| 1969 | static int type_offset; | ||
| 1970 | static int type_size; | ||
| 1971 | |||
| 1972 | return __parse_common(data, &type_size, &type_offset, | ||
| 1973 | "common_type"); | ||
| 1974 | } | ||
| 1975 | |||
| 1976 | int trace_parse_common_pid(void *data) | ||
| 1819 | { | 1977 | { |
| 1820 | static int pid_offset; | 1978 | static int pid_offset; |
| 1821 | static int pid_size; | 1979 | static int pid_size; |
| 1980 | |||
| 1981 | return __parse_common(data, &pid_size, &pid_offset, | ||
| 1982 | "common_pid"); | ||
| 1983 | } | ||
| 1984 | |||
| 1985 | int parse_common_pc(void *data) | ||
| 1986 | { | ||
| 1987 | static int pc_offset; | ||
| 1988 | static int pc_size; | ||
| 1989 | |||
| 1990 | return __parse_common(data, &pc_size, &pc_offset, | ||
| 1991 | "common_preempt_count"); | ||
| 1992 | } | ||
| 1993 | |||
| 1994 | int parse_common_flags(void *data) | ||
| 1995 | { | ||
| 1996 | static int flags_offset; | ||
| 1997 | static int flags_size; | ||
| 1998 | |||
| 1999 | return __parse_common(data, &flags_size, &flags_offset, | ||
| 2000 | "common_flags"); | ||
| 2001 | } | ||
| 2002 | |||
| 2003 | int parse_common_lock_depth(void *data) | ||
| 2004 | { | ||
| 2005 | static int ld_offset; | ||
| 2006 | static int ld_size; | ||
| 1822 | int ret; | 2007 | int ret; |
| 1823 | 2008 | ||
| 1824 | if (!pid_size) { | 2009 | ret = __parse_common(data, &ld_size, &ld_offset, |
| 1825 | ret = get_common_info("common_pid", | 2010 | "common_lock_depth"); |
| 1826 | &pid_offset, | 2011 | if (ret < 0) |
| 1827 | &pid_size); | 2012 | return -1; |
| 1828 | if (ret < 0) | ||
| 1829 | return ret; | ||
| 1830 | } | ||
| 1831 | 2013 | ||
| 1832 | return read_size(data + pid_offset, pid_size); | 2014 | return ret; |
| 1833 | } | 2015 | } |
| 1834 | 2016 | ||
| 1835 | static struct event *find_event(int id) | 2017 | struct event *trace_find_event(int id) |
| 1836 | { | 2018 | { |
| 1837 | struct event *event; | 2019 | struct event *event; |
| 1838 | 2020 | ||
| @@ -1843,11 +2025,20 @@ static struct event *find_event(int id) | |||
| 1843 | return event; | 2025 | return event; |
| 1844 | } | 2026 | } |
| 1845 | 2027 | ||
| 2028 | struct event *trace_find_next_event(struct event *event) | ||
| 2029 | { | ||
| 2030 | if (!event) | ||
| 2031 | return event_list; | ||
| 2032 | |||
| 2033 | return event->next; | ||
| 2034 | } | ||
| 2035 | |||
| 1846 | static unsigned long long eval_num_arg(void *data, int size, | 2036 | static unsigned long long eval_num_arg(void *data, int size, |
| 1847 | struct event *event, struct print_arg *arg) | 2037 | struct event *event, struct print_arg *arg) |
| 1848 | { | 2038 | { |
| 1849 | unsigned long long val = 0; | 2039 | unsigned long long val = 0; |
| 1850 | unsigned long long left, right; | 2040 | unsigned long long left, right; |
| 2041 | struct print_arg *larg; | ||
| 1851 | 2042 | ||
| 1852 | switch (arg->type) { | 2043 | switch (arg->type) { |
| 1853 | case PRINT_NULL: | 2044 | case PRINT_NULL: |
| @@ -1874,6 +2065,26 @@ static unsigned long long eval_num_arg(void *data, int size, | |||
| 1874 | return 0; | 2065 | return 0; |
| 1875 | break; | 2066 | break; |
| 1876 | case PRINT_OP: | 2067 | case PRINT_OP: |
| 2068 | if (strcmp(arg->op.op, "[") == 0) { | ||
| 2069 | /* | ||
| 2070 | * Arrays are special, since we don't want | ||
| 2071 | * to read the arg as is. | ||
| 2072 | */ | ||
| 2073 | if (arg->op.left->type != PRINT_FIELD) | ||
| 2074 | goto default_op; /* oops, all bets off */ | ||
| 2075 | larg = arg->op.left; | ||
| 2076 | if (!larg->field.field) { | ||
| 2077 | larg->field.field = | ||
| 2078 | find_any_field(event, larg->field.name); | ||
| 2079 | if (!larg->field.field) | ||
| 2080 | die("field %s not found", larg->field.name); | ||
| 2081 | } | ||
| 2082 | right = eval_num_arg(data, size, event, arg->op.right); | ||
| 2083 | val = read_size(data + larg->field.field->offset + | ||
| 2084 | right * long_size, long_size); | ||
| 2085 | break; | ||
| 2086 | } | ||
| 2087 | default_op: | ||
| 1877 | left = eval_num_arg(data, size, event, arg->op.left); | 2088 | left = eval_num_arg(data, size, event, arg->op.left); |
| 1878 | right = eval_num_arg(data, size, event, arg->op.right); | 2089 | right = eval_num_arg(data, size, event, arg->op.right); |
| 1879 | switch (arg->op.op[0]) { | 2090 | switch (arg->op.op[0]) { |
| @@ -1924,6 +2135,12 @@ static unsigned long long eval_num_arg(void *data, int size, | |||
| 1924 | die("unknown op '%s'", arg->op.op); | 2135 | die("unknown op '%s'", arg->op.op); |
| 1925 | val = left == right; | 2136 | val = left == right; |
| 1926 | break; | 2137 | break; |
| 2138 | case '-': | ||
| 2139 | val = left - right; | ||
| 2140 | break; | ||
| 2141 | case '+': | ||
| 2142 | val = left + right; | ||
| 2143 | break; | ||
| 1927 | default: | 2144 | default: |
| 1928 | die("unknown op '%s'", arg->op.op); | 2145 | die("unknown op '%s'", arg->op.op); |
| 1929 | } | 2146 | } |
| @@ -1945,16 +2162,17 @@ static const struct flag flags[] = { | |||
| 1945 | { "NET_TX_SOFTIRQ", 2 }, | 2162 | { "NET_TX_SOFTIRQ", 2 }, |
| 1946 | { "NET_RX_SOFTIRQ", 3 }, | 2163 | { "NET_RX_SOFTIRQ", 3 }, |
| 1947 | { "BLOCK_SOFTIRQ", 4 }, | 2164 | { "BLOCK_SOFTIRQ", 4 }, |
| 1948 | { "TASKLET_SOFTIRQ", 5 }, | 2165 | { "BLOCK_IOPOLL_SOFTIRQ", 5 }, |
| 1949 | { "SCHED_SOFTIRQ", 6 }, | 2166 | { "TASKLET_SOFTIRQ", 6 }, |
| 1950 | { "HRTIMER_SOFTIRQ", 7 }, | 2167 | { "SCHED_SOFTIRQ", 7 }, |
| 1951 | { "RCU_SOFTIRQ", 8 }, | 2168 | { "HRTIMER_SOFTIRQ", 8 }, |
| 2169 | { "RCU_SOFTIRQ", 9 }, | ||
| 1952 | 2170 | ||
| 1953 | { "HRTIMER_NORESTART", 0 }, | 2171 | { "HRTIMER_NORESTART", 0 }, |
| 1954 | { "HRTIMER_RESTART", 1 }, | 2172 | { "HRTIMER_RESTART", 1 }, |
| 1955 | }; | 2173 | }; |
| 1956 | 2174 | ||
| 1957 | static unsigned long long eval_flag(const char *flag) | 2175 | unsigned long long eval_flag(const char *flag) |
| 1958 | { | 2176 | { |
| 1959 | int i; | 2177 | int i; |
| 1960 | 2178 | ||
| @@ -2121,8 +2339,9 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc | |||
| 2121 | case 'u': | 2339 | case 'u': |
| 2122 | case 'x': | 2340 | case 'x': |
| 2123 | case 'i': | 2341 | case 'i': |
| 2124 | bptr = (void *)(((unsigned long)bptr + (long_size - 1)) & | 2342 | /* the pointers are always 4 bytes aligned */ |
| 2125 | ~(long_size - 1)); | 2343 | bptr = (void *)(((unsigned long)bptr + 3) & |
| 2344 | ~3); | ||
| 2126 | switch (ls) { | 2345 | switch (ls) { |
| 2127 | case 0: | 2346 | case 0: |
| 2128 | case 1: | 2347 | case 1: |
| @@ -2246,7 +2465,27 @@ static void pretty_print(void *data, int size, struct event *event) | |||
| 2246 | 2465 | ||
| 2247 | for (; *ptr; ptr++) { | 2466 | for (; *ptr; ptr++) { |
| 2248 | ls = 0; | 2467 | ls = 0; |
| 2249 | if (*ptr == '%') { | 2468 | if (*ptr == '\\') { |
| 2469 | ptr++; | ||
| 2470 | switch (*ptr) { | ||
| 2471 | case 'n': | ||
| 2472 | printf("\n"); | ||
| 2473 | break; | ||
| 2474 | case 't': | ||
| 2475 | printf("\t"); | ||
| 2476 | break; | ||
| 2477 | case 'r': | ||
| 2478 | printf("\r"); | ||
| 2479 | break; | ||
| 2480 | case '\\': | ||
| 2481 | printf("\\"); | ||
| 2482 | break; | ||
| 2483 | default: | ||
| 2484 | printf("%c", *ptr); | ||
| 2485 | break; | ||
| 2486 | } | ||
| 2487 | |||
| 2488 | } else if (*ptr == '%') { | ||
| 2250 | saveptr = ptr; | 2489 | saveptr = ptr; |
| 2251 | show_func = 0; | 2490 | show_func = 0; |
| 2252 | cont_process: | 2491 | cont_process: |
| @@ -2353,6 +2592,41 @@ static inline int log10_cpu(int nb) | |||
| 2353 | return 1; | 2592 | return 1; |
| 2354 | } | 2593 | } |
| 2355 | 2594 | ||
| 2595 | static void print_lat_fmt(void *data, int size __unused) | ||
| 2596 | { | ||
| 2597 | unsigned int lat_flags; | ||
| 2598 | unsigned int pc; | ||
| 2599 | int lock_depth; | ||
| 2600 | int hardirq; | ||
| 2601 | int softirq; | ||
| 2602 | |||
| 2603 | lat_flags = parse_common_flags(data); | ||
| 2604 | pc = parse_common_pc(data); | ||
| 2605 | lock_depth = parse_common_lock_depth(data); | ||
| 2606 | |||
| 2607 | hardirq = lat_flags & TRACE_FLAG_HARDIRQ; | ||
| 2608 | softirq = lat_flags & TRACE_FLAG_SOFTIRQ; | ||
| 2609 | |||
| 2610 | printf("%c%c%c", | ||
| 2611 | (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' : | ||
| 2612 | (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ? | ||
| 2613 | 'X' : '.', | ||
| 2614 | (lat_flags & TRACE_FLAG_NEED_RESCHED) ? | ||
| 2615 | 'N' : '.', | ||
| 2616 | (hardirq && softirq) ? 'H' : | ||
| 2617 | hardirq ? 'h' : softirq ? 's' : '.'); | ||
| 2618 | |||
| 2619 | if (pc) | ||
| 2620 | printf("%x", pc); | ||
| 2621 | else | ||
| 2622 | printf("."); | ||
| 2623 | |||
| 2624 | if (lock_depth < 0) | ||
| 2625 | printf("."); | ||
| 2626 | else | ||
| 2627 | printf("%d", lock_depth); | ||
| 2628 | } | ||
| 2629 | |||
| 2356 | /* taken from Linux, written by Frederic Weisbecker */ | 2630 | /* taken from Linux, written by Frederic Weisbecker */ |
| 2357 | static void print_graph_cpu(int cpu) | 2631 | static void print_graph_cpu(int cpu) |
| 2358 | { | 2632 | { |
| @@ -2420,15 +2694,15 @@ get_return_for_leaf(int cpu, int cur_pid, unsigned long long cur_func, | |||
| 2420 | int type; | 2694 | int type; |
| 2421 | int pid; | 2695 | int pid; |
| 2422 | 2696 | ||
| 2423 | type = parse_common_type(next->data); | 2697 | type = trace_parse_common_type(next->data); |
| 2424 | event = find_event(type); | 2698 | event = trace_find_event(type); |
| 2425 | if (!event) | 2699 | if (!event) |
| 2426 | return NULL; | 2700 | return NULL; |
| 2427 | 2701 | ||
| 2428 | if (!(event->flags & EVENT_FL_ISFUNCRET)) | 2702 | if (!(event->flags & EVENT_FL_ISFUNCRET)) |
| 2429 | return NULL; | 2703 | return NULL; |
| 2430 | 2704 | ||
| 2431 | pid = parse_common_pid(next->data); | 2705 | pid = trace_parse_common_pid(next->data); |
| 2432 | field = find_field(event, "func"); | 2706 | field = find_field(event, "func"); |
| 2433 | if (!field) | 2707 | if (!field) |
| 2434 | die("function return does not have field func"); | 2708 | die("function return does not have field func"); |
| @@ -2502,8 +2776,8 @@ print_graph_entry_leaf(struct event *event, void *data, struct record *ret_rec) | |||
| 2502 | int type; | 2776 | int type; |
| 2503 | int i; | 2777 | int i; |
| 2504 | 2778 | ||
| 2505 | type = parse_common_type(ret_rec->data); | 2779 | type = trace_parse_common_type(ret_rec->data); |
| 2506 | ret_event = find_event(type); | 2780 | ret_event = trace_find_event(type); |
| 2507 | 2781 | ||
| 2508 | field = find_field(ret_event, "rettime"); | 2782 | field = find_field(ret_event, "rettime"); |
| 2509 | if (!field) | 2783 | if (!field) |
| @@ -2596,6 +2870,11 @@ pretty_print_func_ent(void *data, int size, struct event *event, | |||
| 2596 | 2870 | ||
| 2597 | printf(" | "); | 2871 | printf(" | "); |
| 2598 | 2872 | ||
| 2873 | if (latency_format) { | ||
| 2874 | print_lat_fmt(data, size); | ||
| 2875 | printf(" | "); | ||
| 2876 | } | ||
| 2877 | |||
| 2599 | field = find_field(event, "func"); | 2878 | field = find_field(event, "func"); |
| 2600 | if (!field) | 2879 | if (!field) |
| 2601 | die("function entry does not have func field"); | 2880 | die("function entry does not have func field"); |
| @@ -2639,6 +2918,11 @@ pretty_print_func_ret(void *data, int size __unused, struct event *event, | |||
| 2639 | 2918 | ||
| 2640 | printf(" | "); | 2919 | printf(" | "); |
| 2641 | 2920 | ||
| 2921 | if (latency_format) { | ||
| 2922 | print_lat_fmt(data, size); | ||
| 2923 | printf(" | "); | ||
| 2924 | } | ||
| 2925 | |||
| 2642 | field = find_field(event, "rettime"); | 2926 | field = find_field(event, "rettime"); |
| 2643 | if (!field) | 2927 | if (!field) |
| 2644 | die("can't find rettime in return graph"); | 2928 | die("can't find rettime in return graph"); |
| @@ -2696,21 +2980,34 @@ void print_event(int cpu, void *data, int size, unsigned long long nsecs, | |||
| 2696 | nsecs -= secs * NSECS_PER_SEC; | 2980 | nsecs -= secs * NSECS_PER_SEC; |
| 2697 | usecs = nsecs / NSECS_PER_USEC; | 2981 | usecs = nsecs / NSECS_PER_USEC; |
| 2698 | 2982 | ||
| 2699 | type = parse_common_type(data); | 2983 | type = trace_parse_common_type(data); |
| 2700 | 2984 | ||
| 2701 | event = find_event(type); | 2985 | event = trace_find_event(type); |
| 2702 | if (!event) | 2986 | if (!event) { |
| 2703 | die("ug! no event found for type %d", type); | 2987 | warning("ug! no event found for type %d", type); |
| 2988 | return; | ||
| 2989 | } | ||
| 2704 | 2990 | ||
| 2705 | pid = parse_common_pid(data); | 2991 | pid = trace_parse_common_pid(data); |
| 2706 | 2992 | ||
| 2707 | if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET)) | 2993 | if (event->flags & (EVENT_FL_ISFUNCENT | EVENT_FL_ISFUNCRET)) |
| 2708 | return pretty_print_func_graph(data, size, event, cpu, | 2994 | return pretty_print_func_graph(data, size, event, cpu, |
| 2709 | pid, comm, secs, usecs); | 2995 | pid, comm, secs, usecs); |
| 2710 | 2996 | ||
| 2711 | printf("%16s-%-5d [%03d] %5lu.%09Lu: %s: ", | 2997 | if (latency_format) { |
| 2712 | comm, pid, cpu, | 2998 | printf("%8.8s-%-5d %3d", |
| 2713 | secs, nsecs, event->name); | 2999 | comm, pid, cpu); |
| 3000 | print_lat_fmt(data, size); | ||
| 3001 | } else | ||
| 3002 | printf("%16s-%-5d [%03d]", comm, pid, cpu); | ||
| 3003 | |||
| 3004 | printf(" %5lu.%06lu: %s: ", secs, usecs, event->name); | ||
| 3005 | |||
| 3006 | if (event->flags & EVENT_FL_FAILED) { | ||
| 3007 | printf("EVENT '%s' FAILED TO PARSE\n", | ||
| 3008 | event->name); | ||
| 3009 | return; | ||
| 3010 | } | ||
| 2714 | 3011 | ||
| 2715 | pretty_print(data, size, event); | 3012 | pretty_print(data, size, event); |
| 2716 | printf("\n"); | 3013 | printf("\n"); |
| @@ -2781,46 +3078,71 @@ static void print_args(struct print_arg *args) | |||
| 2781 | } | 3078 | } |
| 2782 | } | 3079 | } |
| 2783 | 3080 | ||
| 2784 | static void parse_header_field(char *type, | 3081 | static void parse_header_field(const char *field, |
| 2785 | int *offset, int *size) | 3082 | int *offset, int *size) |
| 2786 | { | 3083 | { |
| 2787 | char *token; | 3084 | char *token; |
| 3085 | int type; | ||
| 2788 | 3086 | ||
| 2789 | if (read_expected(EVENT_ITEM, (char *)"field") < 0) | 3087 | if (read_expected(EVENT_ITEM, "field") < 0) |
| 2790 | return; | 3088 | return; |
| 2791 | if (read_expected(EVENT_OP, (char *)":") < 0) | 3089 | if (read_expected(EVENT_OP, ":") < 0) |
| 2792 | return; | 3090 | return; |
| 3091 | |||
| 2793 | /* type */ | 3092 | /* type */ |
| 2794 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 3093 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 2795 | return; | 3094 | goto fail; |
| 2796 | free_token(token); | 3095 | free_token(token); |
| 2797 | 3096 | ||
| 2798 | if (read_expected(EVENT_ITEM, type) < 0) | 3097 | if (read_expected(EVENT_ITEM, field) < 0) |
| 2799 | return; | 3098 | return; |
| 2800 | if (read_expected(EVENT_OP, (char *)";") < 0) | 3099 | if (read_expected(EVENT_OP, ";") < 0) |
| 2801 | return; | 3100 | return; |
| 2802 | if (read_expected(EVENT_ITEM, (char *)"offset") < 0) | 3101 | if (read_expected(EVENT_ITEM, "offset") < 0) |
| 2803 | return; | 3102 | return; |
| 2804 | if (read_expected(EVENT_OP, (char *)":") < 0) | 3103 | if (read_expected(EVENT_OP, ":") < 0) |
| 2805 | return; | 3104 | return; |
| 2806 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 3105 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 2807 | return; | 3106 | goto fail; |
| 2808 | *offset = atoi(token); | 3107 | *offset = atoi(token); |
| 2809 | free_token(token); | 3108 | free_token(token); |
| 2810 | if (read_expected(EVENT_OP, (char *)";") < 0) | 3109 | if (read_expected(EVENT_OP, ";") < 0) |
| 2811 | return; | 3110 | return; |
| 2812 | if (read_expected(EVENT_ITEM, (char *)"size") < 0) | 3111 | if (read_expected(EVENT_ITEM, "size") < 0) |
| 2813 | return; | 3112 | return; |
| 2814 | if (read_expected(EVENT_OP, (char *)":") < 0) | 3113 | if (read_expected(EVENT_OP, ":") < 0) |
| 2815 | return; | 3114 | return; |
| 2816 | if (read_expect_type(EVENT_ITEM, &token) < 0) | 3115 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 2817 | return; | 3116 | goto fail; |
| 2818 | *size = atoi(token); | 3117 | *size = atoi(token); |
| 2819 | free_token(token); | 3118 | free_token(token); |
| 2820 | if (read_expected(EVENT_OP, (char *)";") < 0) | 3119 | if (read_expected(EVENT_OP, ";") < 0) |
| 2821 | return; | ||
| 2822 | if (read_expect_type(EVENT_NEWLINE, &token) < 0) | ||
| 2823 | return; | 3120 | return; |
| 3121 | type = read_token(&token); | ||
| 3122 | if (type != EVENT_NEWLINE) { | ||
| 3123 | /* newer versions of the kernel have a "signed" type */ | ||
| 3124 | if (type != EVENT_ITEM) | ||
| 3125 | goto fail; | ||
| 3126 | |||
| 3127 | if (strcmp(token, "signed") != 0) | ||
| 3128 | goto fail; | ||
| 3129 | |||
| 3130 | free_token(token); | ||
| 3131 | |||
| 3132 | if (read_expected(EVENT_OP, ":") < 0) | ||
| 3133 | return; | ||
| 3134 | |||
| 3135 | if (read_expect_type(EVENT_ITEM, &token)) | ||
| 3136 | goto fail; | ||
| 3137 | |||
| 3138 | free_token(token); | ||
| 3139 | if (read_expected(EVENT_OP, ";") < 0) | ||
| 3140 | return; | ||
| 3141 | |||
| 3142 | if (read_expect_type(EVENT_NEWLINE, &token)) | ||
| 3143 | goto fail; | ||
| 3144 | } | ||
| 3145 | fail: | ||
| 2824 | free_token(token); | 3146 | free_token(token); |
| 2825 | } | 3147 | } |
| 2826 | 3148 | ||
| @@ -2828,11 +3150,11 @@ int parse_header_page(char *buf, unsigned long size) | |||
| 2828 | { | 3150 | { |
| 2829 | init_input_buf(buf, size); | 3151 | init_input_buf(buf, size); |
| 2830 | 3152 | ||
| 2831 | parse_header_field((char *)"timestamp", &header_page_ts_offset, | 3153 | parse_header_field("timestamp", &header_page_ts_offset, |
| 2832 | &header_page_ts_size); | 3154 | &header_page_ts_size); |
| 2833 | parse_header_field((char *)"commit", &header_page_size_offset, | 3155 | parse_header_field("commit", &header_page_size_offset, |
| 2834 | &header_page_size_size); | 3156 | &header_page_size_size); |
| 2835 | parse_header_field((char *)"data", &header_page_data_offset, | 3157 | parse_header_field("data", &header_page_data_offset, |
| 2836 | &header_page_data_size); | 3158 | &header_page_data_size); |
| 2837 | 3159 | ||
| 2838 | return 0; | 3160 | return 0; |
| @@ -2883,6 +3205,9 @@ int parse_ftrace_file(char *buf, unsigned long size) | |||
| 2883 | if (ret < 0) | 3205 | if (ret < 0) |
| 2884 | die("failed to read ftrace event print fmt"); | 3206 | die("failed to read ftrace event print fmt"); |
| 2885 | 3207 | ||
| 3208 | /* New ftrace handles args */ | ||
| 3209 | if (ret > 0) | ||
| 3210 | return 0; | ||
| 2886 | /* | 3211 | /* |
| 2887 | * The arguments for ftrace files are parsed by the fields. | 3212 | * The arguments for ftrace files are parsed by the fields. |
| 2888 | * Set up the fields as their arguments. | 3213 | * Set up the fields as their arguments. |
| @@ -2900,7 +3225,7 @@ int parse_ftrace_file(char *buf, unsigned long size) | |||
| 2900 | return 0; | 3225 | return 0; |
| 2901 | } | 3226 | } |
| 2902 | 3227 | ||
| 2903 | int parse_event_file(char *buf, unsigned long size, char *system__unused __unused) | 3228 | int parse_event_file(char *buf, unsigned long size, char *sys) |
| 2904 | { | 3229 | { |
| 2905 | struct event *event; | 3230 | struct event *event; |
| 2906 | int ret; | 3231 | int ret; |
| @@ -2920,12 +3245,18 @@ int parse_event_file(char *buf, unsigned long size, char *system__unused __unuse | |||
| 2920 | die("failed to read event id"); | 3245 | die("failed to read event id"); |
| 2921 | 3246 | ||
| 2922 | ret = event_read_format(event); | 3247 | ret = event_read_format(event); |
| 2923 | if (ret < 0) | 3248 | if (ret < 0) { |
| 2924 | die("failed to read event format"); | 3249 | warning("failed to read event format for %s", event->name); |
| 3250 | goto event_failed; | ||
| 3251 | } | ||
| 2925 | 3252 | ||
| 2926 | ret = event_read_print(event); | 3253 | ret = event_read_print(event); |
| 2927 | if (ret < 0) | 3254 | if (ret < 0) { |
| 2928 | die("failed to read event print fmt"); | 3255 | warning("failed to read event print fmt for %s", event->name); |
| 3256 | goto event_failed; | ||
| 3257 | } | ||
| 3258 | |||
| 3259 | event->system = strdup(sys); | ||
| 2929 | 3260 | ||
| 2930 | #define PRINT_ARGS 0 | 3261 | #define PRINT_ARGS 0 |
| 2931 | if (PRINT_ARGS && event->print_fmt.args) | 3262 | if (PRINT_ARGS && event->print_fmt.args) |
| @@ -2933,6 +3264,12 @@ int parse_event_file(char *buf, unsigned long size, char *system__unused __unuse | |||
| 2933 | 3264 | ||
| 2934 | add_event(event); | 3265 | add_event(event); |
| 2935 | return 0; | 3266 | return 0; |
| 3267 | |||
| 3268 | event_failed: | ||
| 3269 | event->flags |= EVENT_FL_FAILED; | ||
| 3270 | /* still add it even if it failed */ | ||
| 3271 | add_event(event); | ||
| 3272 | return -1; | ||
| 2936 | } | 3273 | } |
| 2937 | 3274 | ||
| 2938 | void parse_set_info(int nr_cpus, int long_sz) | 3275 | void parse_set_info(int nr_cpus, int long_sz) |
diff --git a/tools/perf/util/trace-event-perl.c b/tools/perf/util/trace-event-perl.c new file mode 100644 index 000000000000..51e833fd58c3 --- /dev/null +++ b/tools/perf/util/trace-event-perl.c | |||
| @@ -0,0 +1,598 @@ | |||
| 1 | /* | ||
| 2 | * trace-event-perl. Feed perf trace events to an embedded Perl interpreter. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 19 | * | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <stdio.h> | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <string.h> | ||
| 25 | #include <ctype.h> | ||
| 26 | #include <errno.h> | ||
| 27 | |||
| 28 | #include "../perf.h" | ||
| 29 | #include "util.h" | ||
| 30 | #include "trace-event.h" | ||
| 31 | #include "trace-event-perl.h" | ||
| 32 | |||
| 33 | void xs_init(pTHX); | ||
| 34 | |||
| 35 | void boot_Perf__Trace__Context(pTHX_ CV *cv); | ||
| 36 | void boot_DynaLoader(pTHX_ CV *cv); | ||
| 37 | |||
| 38 | void xs_init(pTHX) | ||
| 39 | { | ||
| 40 | const char *file = __FILE__; | ||
| 41 | dXSUB_SYS; | ||
| 42 | |||
| 43 | newXS("Perf::Trace::Context::bootstrap", boot_Perf__Trace__Context, | ||
| 44 | file); | ||
| 45 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); | ||
| 46 | } | ||
| 47 | |||
| 48 | INTERP my_perl; | ||
| 49 | |||
| 50 | #define FTRACE_MAX_EVENT \ | ||
| 51 | ((1 << (sizeof(unsigned short) * 8)) - 1) | ||
| 52 | |||
| 53 | struct event *events[FTRACE_MAX_EVENT]; | ||
| 54 | |||
| 55 | static struct scripting_context *scripting_context; | ||
| 56 | |||
| 57 | static char *cur_field_name; | ||
| 58 | static int zero_flag_atom; | ||
| 59 | |||
| 60 | static void define_symbolic_value(const char *ev_name, | ||
| 61 | const char *field_name, | ||
| 62 | const char *field_value, | ||
| 63 | const char *field_str) | ||
| 64 | { | ||
| 65 | unsigned long long value; | ||
| 66 | dSP; | ||
| 67 | |||
| 68 | value = eval_flag(field_value); | ||
| 69 | |||
| 70 | ENTER; | ||
| 71 | SAVETMPS; | ||
| 72 | PUSHMARK(SP); | ||
| 73 | |||
| 74 | XPUSHs(sv_2mortal(newSVpv(ev_name, 0))); | ||
| 75 | XPUSHs(sv_2mortal(newSVpv(field_name, 0))); | ||
| 76 | XPUSHs(sv_2mortal(newSVuv(value))); | ||
| 77 | XPUSHs(sv_2mortal(newSVpv(field_str, 0))); | ||
| 78 | |||
| 79 | PUTBACK; | ||
| 80 | if (get_cv("main::define_symbolic_value", 0)) | ||
| 81 | call_pv("main::define_symbolic_value", G_SCALAR); | ||
| 82 | SPAGAIN; | ||
| 83 | PUTBACK; | ||
| 84 | FREETMPS; | ||
| 85 | LEAVE; | ||
| 86 | } | ||
| 87 | |||
| 88 | static void define_symbolic_values(struct print_flag_sym *field, | ||
| 89 | const char *ev_name, | ||
| 90 | const char *field_name) | ||
| 91 | { | ||
| 92 | define_symbolic_value(ev_name, field_name, field->value, field->str); | ||
| 93 | if (field->next) | ||
| 94 | define_symbolic_values(field->next, ev_name, field_name); | ||
| 95 | } | ||
| 96 | |||
| 97 | static void define_symbolic_field(const char *ev_name, | ||
| 98 | const char *field_name) | ||
| 99 | { | ||
| 100 | dSP; | ||
| 101 | |||
| 102 | ENTER; | ||
| 103 | SAVETMPS; | ||
| 104 | PUSHMARK(SP); | ||
| 105 | |||
| 106 | XPUSHs(sv_2mortal(newSVpv(ev_name, 0))); | ||
| 107 | XPUSHs(sv_2mortal(newSVpv(field_name, 0))); | ||
| 108 | |||
| 109 | PUTBACK; | ||
| 110 | if (get_cv("main::define_symbolic_field", 0)) | ||
| 111 | call_pv("main::define_symbolic_field", G_SCALAR); | ||
| 112 | SPAGAIN; | ||
| 113 | PUTBACK; | ||
| 114 | FREETMPS; | ||
| 115 | LEAVE; | ||
| 116 | } | ||
| 117 | |||
| 118 | static void define_flag_value(const char *ev_name, | ||
| 119 | const char *field_name, | ||
| 120 | const char *field_value, | ||
| 121 | const char *field_str) | ||
| 122 | { | ||
| 123 | unsigned long long value; | ||
| 124 | dSP; | ||
| 125 | |||
| 126 | value = eval_flag(field_value); | ||
| 127 | |||
| 128 | ENTER; | ||
| 129 | SAVETMPS; | ||
| 130 | PUSHMARK(SP); | ||
| 131 | |||
| 132 | XPUSHs(sv_2mortal(newSVpv(ev_name, 0))); | ||
| 133 | XPUSHs(sv_2mortal(newSVpv(field_name, 0))); | ||
| 134 | XPUSHs(sv_2mortal(newSVuv(value))); | ||
| 135 | XPUSHs(sv_2mortal(newSVpv(field_str, 0))); | ||
| 136 | |||
| 137 | PUTBACK; | ||
| 138 | if (get_cv("main::define_flag_value", 0)) | ||
| 139 | call_pv("main::define_flag_value", G_SCALAR); | ||
| 140 | SPAGAIN; | ||
| 141 | PUTBACK; | ||
| 142 | FREETMPS; | ||
| 143 | LEAVE; | ||
| 144 | } | ||
| 145 | |||
| 146 | static void define_flag_values(struct print_flag_sym *field, | ||
| 147 | const char *ev_name, | ||
| 148 | const char *field_name) | ||
| 149 | { | ||
| 150 | define_flag_value(ev_name, field_name, field->value, field->str); | ||
| 151 | if (field->next) | ||
| 152 | define_flag_values(field->next, ev_name, field_name); | ||
| 153 | } | ||
| 154 | |||
| 155 | static void define_flag_field(const char *ev_name, | ||
| 156 | const char *field_name, | ||
| 157 | const char *delim) | ||
| 158 | { | ||
| 159 | dSP; | ||
| 160 | |||
| 161 | ENTER; | ||
| 162 | SAVETMPS; | ||
| 163 | PUSHMARK(SP); | ||
| 164 | |||
| 165 | XPUSHs(sv_2mortal(newSVpv(ev_name, 0))); | ||
| 166 | XPUSHs(sv_2mortal(newSVpv(field_name, 0))); | ||
| 167 | XPUSHs(sv_2mortal(newSVpv(delim, 0))); | ||
| 168 | |||
| 169 | PUTBACK; | ||
| 170 | if (get_cv("main::define_flag_field", 0)) | ||
| 171 | call_pv("main::define_flag_field", G_SCALAR); | ||
| 172 | SPAGAIN; | ||
| 173 | PUTBACK; | ||
| 174 | FREETMPS; | ||
| 175 | LEAVE; | ||
| 176 | } | ||
| 177 | |||
| 178 | static void define_event_symbols(struct event *event, | ||
| 179 | const char *ev_name, | ||
| 180 | struct print_arg *args) | ||
| 181 | { | ||
| 182 | switch (args->type) { | ||
| 183 | case PRINT_NULL: | ||
| 184 | break; | ||
| 185 | case PRINT_ATOM: | ||
| 186 | define_flag_value(ev_name, cur_field_name, "0", | ||
| 187 | args->atom.atom); | ||
| 188 | zero_flag_atom = 0; | ||
| 189 | break; | ||
| 190 | case PRINT_FIELD: | ||
| 191 | if (cur_field_name) | ||
| 192 | free(cur_field_name); | ||
| 193 | cur_field_name = strdup(args->field.name); | ||
| 194 | break; | ||
| 195 | case PRINT_FLAGS: | ||
| 196 | define_event_symbols(event, ev_name, args->flags.field); | ||
| 197 | define_flag_field(ev_name, cur_field_name, args->flags.delim); | ||
| 198 | define_flag_values(args->flags.flags, ev_name, cur_field_name); | ||
| 199 | break; | ||
| 200 | case PRINT_SYMBOL: | ||
| 201 | define_event_symbols(event, ev_name, args->symbol.field); | ||
| 202 | define_symbolic_field(ev_name, cur_field_name); | ||
| 203 | define_symbolic_values(args->symbol.symbols, ev_name, | ||
| 204 | cur_field_name); | ||
| 205 | break; | ||
| 206 | case PRINT_STRING: | ||
| 207 | break; | ||
| 208 | case PRINT_TYPE: | ||
| 209 | define_event_symbols(event, ev_name, args->typecast.item); | ||
| 210 | break; | ||
| 211 | case PRINT_OP: | ||
| 212 | if (strcmp(args->op.op, ":") == 0) | ||
| 213 | zero_flag_atom = 1; | ||
| 214 | define_event_symbols(event, ev_name, args->op.left); | ||
| 215 | define_event_symbols(event, ev_name, args->op.right); | ||
| 216 | break; | ||
| 217 | default: | ||
| 218 | /* we should warn... */ | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | |||
| 222 | if (args->next) | ||
| 223 | define_event_symbols(event, ev_name, args->next); | ||
| 224 | } | ||
| 225 | |||
| 226 | static inline struct event *find_cache_event(int type) | ||
| 227 | { | ||
| 228 | static char ev_name[256]; | ||
| 229 | struct event *event; | ||
| 230 | |||
| 231 | if (events[type]) | ||
| 232 | return events[type]; | ||
| 233 | |||
| 234 | events[type] = event = trace_find_event(type); | ||
| 235 | if (!event) | ||
| 236 | return NULL; | ||
| 237 | |||
| 238 | sprintf(ev_name, "%s::%s", event->system, event->name); | ||
| 239 | |||
| 240 | define_event_symbols(event, ev_name, event->print_fmt.args); | ||
| 241 | |||
| 242 | return event; | ||
| 243 | } | ||
| 244 | |||
| 245 | int common_pc(struct scripting_context *context) | ||
| 246 | { | ||
| 247 | int pc; | ||
| 248 | |||
| 249 | pc = parse_common_pc(context->event_data); | ||
| 250 | |||
| 251 | return pc; | ||
| 252 | } | ||
| 253 | |||
| 254 | int common_flags(struct scripting_context *context) | ||
| 255 | { | ||
| 256 | int flags; | ||
| 257 | |||
| 258 | flags = parse_common_flags(context->event_data); | ||
| 259 | |||
| 260 | return flags; | ||
| 261 | } | ||
| 262 | |||
| 263 | int common_lock_depth(struct scripting_context *context) | ||
| 264 | { | ||
| 265 | int lock_depth; | ||
| 266 | |||
| 267 | lock_depth = parse_common_lock_depth(context->event_data); | ||
| 268 | |||
| 269 | return lock_depth; | ||
| 270 | } | ||
| 271 | |||
| 272 | static void perl_process_event(int cpu, void *data, | ||
| 273 | int size __attribute((unused)), | ||
| 274 | unsigned long long nsecs, char *comm) | ||
| 275 | { | ||
| 276 | struct format_field *field; | ||
| 277 | static char handler[256]; | ||
| 278 | unsigned long long val; | ||
| 279 | unsigned long s, ns; | ||
| 280 | struct event *event; | ||
| 281 | int type; | ||
| 282 | int pid; | ||
| 283 | |||
| 284 | dSP; | ||
| 285 | |||
| 286 | type = trace_parse_common_type(data); | ||
| 287 | |||
| 288 | event = find_cache_event(type); | ||
| 289 | if (!event) | ||
| 290 | die("ug! no event found for type %d", type); | ||
| 291 | |||
| 292 | pid = trace_parse_common_pid(data); | ||
| 293 | |||
| 294 | sprintf(handler, "%s::%s", event->system, event->name); | ||
| 295 | |||
| 296 | s = nsecs / NSECS_PER_SEC; | ||
| 297 | ns = nsecs - s * NSECS_PER_SEC; | ||
| 298 | |||
| 299 | scripting_context->event_data = data; | ||
| 300 | |||
| 301 | ENTER; | ||
| 302 | SAVETMPS; | ||
| 303 | PUSHMARK(SP); | ||
| 304 | |||
| 305 | XPUSHs(sv_2mortal(newSVpv(handler, 0))); | ||
| 306 | XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context)))); | ||
| 307 | XPUSHs(sv_2mortal(newSVuv(cpu))); | ||
| 308 | XPUSHs(sv_2mortal(newSVuv(s))); | ||
| 309 | XPUSHs(sv_2mortal(newSVuv(ns))); | ||
| 310 | XPUSHs(sv_2mortal(newSViv(pid))); | ||
| 311 | XPUSHs(sv_2mortal(newSVpv(comm, 0))); | ||
| 312 | |||
| 313 | /* common fields other than pid can be accessed via xsub fns */ | ||
| 314 | |||
| 315 | for (field = event->format.fields; field; field = field->next) { | ||
| 316 | if (field->flags & FIELD_IS_STRING) { | ||
| 317 | int offset; | ||
| 318 | if (field->flags & FIELD_IS_DYNAMIC) { | ||
| 319 | offset = *(int *)(data + field->offset); | ||
| 320 | offset &= 0xffff; | ||
| 321 | } else | ||
| 322 | offset = field->offset; | ||
| 323 | XPUSHs(sv_2mortal(newSVpv((char *)data + offset, 0))); | ||
| 324 | } else { /* FIELD_IS_NUMERIC */ | ||
| 325 | val = read_size(data + field->offset, field->size); | ||
| 326 | if (field->flags & FIELD_IS_SIGNED) { | ||
| 327 | XPUSHs(sv_2mortal(newSViv(val))); | ||
| 328 | } else { | ||
| 329 | XPUSHs(sv_2mortal(newSVuv(val))); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | PUTBACK; | ||
| 335 | |||
| 336 | if (get_cv(handler, 0)) | ||
| 337 | call_pv(handler, G_SCALAR); | ||
| 338 | else if (get_cv("main::trace_unhandled", 0)) { | ||
| 339 | XPUSHs(sv_2mortal(newSVpv(handler, 0))); | ||
| 340 | XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context)))); | ||
| 341 | XPUSHs(sv_2mortal(newSVuv(cpu))); | ||
| 342 | XPUSHs(sv_2mortal(newSVuv(nsecs))); | ||
| 343 | XPUSHs(sv_2mortal(newSViv(pid))); | ||
| 344 | XPUSHs(sv_2mortal(newSVpv(comm, 0))); | ||
| 345 | call_pv("main::trace_unhandled", G_SCALAR); | ||
| 346 | } | ||
| 347 | SPAGAIN; | ||
| 348 | PUTBACK; | ||
| 349 | FREETMPS; | ||
| 350 | LEAVE; | ||
| 351 | } | ||
| 352 | |||
| 353 | static void run_start_sub(void) | ||
| 354 | { | ||
| 355 | dSP; /* access to Perl stack */ | ||
| 356 | PUSHMARK(SP); | ||
| 357 | |||
| 358 | if (get_cv("main::trace_begin", 0)) | ||
| 359 | call_pv("main::trace_begin", G_DISCARD | G_NOARGS); | ||
| 360 | } | ||
| 361 | |||
| 362 | /* | ||
| 363 | * Start trace script | ||
| 364 | */ | ||
| 365 | static int perl_start_script(const char *script) | ||
| 366 | { | ||
| 367 | const char *command_line[2] = { "", NULL }; | ||
| 368 | |||
| 369 | command_line[1] = script; | ||
| 370 | |||
| 371 | my_perl = perl_alloc(); | ||
| 372 | perl_construct(my_perl); | ||
| 373 | |||
| 374 | if (perl_parse(my_perl, xs_init, 2, (char **)command_line, | ||
| 375 | (char **)NULL)) | ||
| 376 | return -1; | ||
| 377 | |||
| 378 | perl_run(my_perl); | ||
| 379 | if (SvTRUE(ERRSV)) | ||
| 380 | return -1; | ||
| 381 | |||
| 382 | run_start_sub(); | ||
| 383 | |||
| 384 | fprintf(stderr, "perf trace started with Perl script %s\n\n", script); | ||
| 385 | |||
| 386 | return 0; | ||
| 387 | } | ||
| 388 | |||
| 389 | /* | ||
| 390 | * Stop trace script | ||
| 391 | */ | ||
| 392 | static int perl_stop_script(void) | ||
| 393 | { | ||
| 394 | dSP; /* access to Perl stack */ | ||
| 395 | PUSHMARK(SP); | ||
| 396 | |||
| 397 | if (get_cv("main::trace_end", 0)) | ||
| 398 | call_pv("main::trace_end", G_DISCARD | G_NOARGS); | ||
| 399 | |||
| 400 | perl_destruct(my_perl); | ||
| 401 | perl_free(my_perl); | ||
| 402 | |||
| 403 | fprintf(stderr, "\nperf trace Perl script stopped\n"); | ||
| 404 | |||
| 405 | return 0; | ||
| 406 | } | ||
| 407 | |||
| 408 | static int perl_generate_script(const char *outfile) | ||
| 409 | { | ||
| 410 | struct event *event = NULL; | ||
| 411 | struct format_field *f; | ||
| 412 | char fname[PATH_MAX]; | ||
| 413 | int not_first, count; | ||
| 414 | FILE *ofp; | ||
| 415 | |||
| 416 | sprintf(fname, "%s.pl", outfile); | ||
| 417 | ofp = fopen(fname, "w"); | ||
| 418 | if (ofp == NULL) { | ||
| 419 | fprintf(stderr, "couldn't open %s\n", fname); | ||
| 420 | return -1; | ||
| 421 | } | ||
| 422 | |||
| 423 | fprintf(ofp, "# perf trace event handlers, " | ||
| 424 | "generated by perf trace -g perl\n"); | ||
| 425 | |||
| 426 | fprintf(ofp, "# Licensed under the terms of the GNU GPL" | ||
| 427 | " License version 2\n\n"); | ||
| 428 | |||
| 429 | fprintf(ofp, "# The common_* event handler fields are the most useful " | ||
| 430 | "fields common to\n"); | ||
| 431 | |||
| 432 | fprintf(ofp, "# all events. They don't necessarily correspond to " | ||
| 433 | "the 'common_*' fields\n"); | ||
| 434 | |||
| 435 | fprintf(ofp, "# in the format files. Those fields not available as " | ||
| 436 | "handler params can\n"); | ||
| 437 | |||
| 438 | fprintf(ofp, "# be retrieved using Perl functions of the form " | ||
| 439 | "common_*($context).\n"); | ||
| 440 | |||
| 441 | fprintf(ofp, "# See Context.pm for the list of available " | ||
| 442 | "functions.\n\n"); | ||
| 443 | |||
| 444 | fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/" | ||
| 445 | "Perf-Trace-Util/lib\";\n"); | ||
| 446 | |||
| 447 | fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n"); | ||
| 448 | fprintf(ofp, "use Perf::Trace::Core;\n"); | ||
| 449 | fprintf(ofp, "use Perf::Trace::Context;\n"); | ||
| 450 | fprintf(ofp, "use Perf::Trace::Util;\n\n"); | ||
| 451 | |||
| 452 | fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n"); | ||
| 453 | fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n\n"); | ||
| 454 | |||
| 455 | while ((event = trace_find_next_event(event))) { | ||
| 456 | fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name); | ||
| 457 | fprintf(ofp, "\tmy ("); | ||
| 458 | |||
| 459 | fprintf(ofp, "$event_name, "); | ||
| 460 | fprintf(ofp, "$context, "); | ||
| 461 | fprintf(ofp, "$common_cpu, "); | ||
| 462 | fprintf(ofp, "$common_secs, "); | ||
| 463 | fprintf(ofp, "$common_nsecs,\n"); | ||
| 464 | fprintf(ofp, "\t $common_pid, "); | ||
| 465 | fprintf(ofp, "$common_comm,\n\t "); | ||
| 466 | |||
| 467 | not_first = 0; | ||
| 468 | count = 0; | ||
| 469 | |||
| 470 | for (f = event->format.fields; f; f = f->next) { | ||
| 471 | if (not_first++) | ||
| 472 | fprintf(ofp, ", "); | ||
| 473 | if (++count % 5 == 0) | ||
| 474 | fprintf(ofp, "\n\t "); | ||
| 475 | |||
| 476 | fprintf(ofp, "$%s", f->name); | ||
| 477 | } | ||
| 478 | fprintf(ofp, ") = @_;\n\n"); | ||
| 479 | |||
| 480 | fprintf(ofp, "\tprint_header($event_name, $common_cpu, " | ||
| 481 | "$common_secs, $common_nsecs,\n\t " | ||
| 482 | "$common_pid, $common_comm);\n\n"); | ||
| 483 | |||
| 484 | fprintf(ofp, "\tprintf(\""); | ||
| 485 | |||
| 486 | not_first = 0; | ||
| 487 | count = 0; | ||
| 488 | |||
| 489 | for (f = event->format.fields; f; f = f->next) { | ||
| 490 | if (not_first++) | ||
| 491 | fprintf(ofp, ", "); | ||
| 492 | if (count && count % 4 == 0) { | ||
| 493 | fprintf(ofp, "\".\n\t \""); | ||
| 494 | } | ||
| 495 | count++; | ||
| 496 | |||
| 497 | fprintf(ofp, "%s=", f->name); | ||
| 498 | if (f->flags & FIELD_IS_STRING || | ||
| 499 | f->flags & FIELD_IS_FLAG || | ||
| 500 | f->flags & FIELD_IS_SYMBOLIC) | ||
| 501 | fprintf(ofp, "%%s"); | ||
| 502 | else if (f->flags & FIELD_IS_SIGNED) | ||
| 503 | fprintf(ofp, "%%d"); | ||
| 504 | else | ||
| 505 | fprintf(ofp, "%%u"); | ||
| 506 | } | ||
| 507 | |||
| 508 | fprintf(ofp, "\\n\",\n\t "); | ||
| 509 | |||
| 510 | not_first = 0; | ||
| 511 | count = 0; | ||
| 512 | |||
| 513 | for (f = event->format.fields; f; f = f->next) { | ||
| 514 | if (not_first++) | ||
| 515 | fprintf(ofp, ", "); | ||
| 516 | |||
| 517 | if (++count % 5 == 0) | ||
| 518 | fprintf(ofp, "\n\t "); | ||
| 519 | |||
| 520 | if (f->flags & FIELD_IS_FLAG) { | ||
| 521 | if ((count - 1) % 5 != 0) { | ||
| 522 | fprintf(ofp, "\n\t "); | ||
| 523 | count = 4; | ||
| 524 | } | ||
| 525 | fprintf(ofp, "flag_str(\""); | ||
| 526 | fprintf(ofp, "%s::%s\", ", event->system, | ||
| 527 | event->name); | ||
| 528 | fprintf(ofp, "\"%s\", $%s)", f->name, | ||
| 529 | f->name); | ||
| 530 | } else if (f->flags & FIELD_IS_SYMBOLIC) { | ||
| 531 | if ((count - 1) % 5 != 0) { | ||
| 532 | fprintf(ofp, "\n\t "); | ||
| 533 | count = 4; | ||
| 534 | } | ||
| 535 | fprintf(ofp, "symbol_str(\""); | ||
| 536 | fprintf(ofp, "%s::%s\", ", event->system, | ||
| 537 | event->name); | ||
| 538 | fprintf(ofp, "\"%s\", $%s)", f->name, | ||
| 539 | f->name); | ||
| 540 | } else | ||
| 541 | fprintf(ofp, "$%s", f->name); | ||
| 542 | } | ||
| 543 | |||
| 544 | fprintf(ofp, ");\n"); | ||
| 545 | fprintf(ofp, "}\n\n"); | ||
| 546 | } | ||
| 547 | |||
| 548 | fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, " | ||
| 549 | "$common_cpu, $common_secs, $common_nsecs,\n\t " | ||
| 550 | "$common_pid, $common_comm) = @_;\n\n"); | ||
| 551 | |||
| 552 | fprintf(ofp, "\tprint_header($event_name, $common_cpu, " | ||
| 553 | "$common_secs, $common_nsecs,\n\t $common_pid, " | ||
| 554 | "$common_comm);\n}\n\n"); | ||
| 555 | |||
| 556 | fprintf(ofp, "sub print_header\n{\n" | ||
| 557 | "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n" | ||
| 558 | "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t " | ||
| 559 | "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}"); | ||
| 560 | |||
| 561 | fclose(ofp); | ||
| 562 | |||
| 563 | fprintf(stderr, "generated Perl script: %s\n", fname); | ||
| 564 | |||
| 565 | return 0; | ||
| 566 | } | ||
| 567 | |||
| 568 | struct scripting_ops perl_scripting_ops = { | ||
| 569 | .name = "Perl", | ||
| 570 | .start_script = perl_start_script, | ||
| 571 | .stop_script = perl_stop_script, | ||
| 572 | .process_event = perl_process_event, | ||
| 573 | .generate_script = perl_generate_script, | ||
| 574 | }; | ||
| 575 | |||
| 576 | #ifdef NO_LIBPERL | ||
| 577 | void setup_perl_scripting(void) | ||
| 578 | { | ||
| 579 | fprintf(stderr, "Perl scripting not supported." | ||
| 580 | " Install libperl and rebuild perf to enable it. e.g. " | ||
| 581 | "apt-get install libperl-dev (ubuntu), yum install " | ||
| 582 | "perl-ExtUtils-Embed (Fedora), etc.\n"); | ||
| 583 | } | ||
| 584 | #else | ||
| 585 | void setup_perl_scripting(void) | ||
| 586 | { | ||
| 587 | int err; | ||
| 588 | err = script_spec_register("Perl", &perl_scripting_ops); | ||
| 589 | if (err) | ||
| 590 | die("error registering Perl script extension"); | ||
| 591 | |||
| 592 | err = script_spec_register("pl", &perl_scripting_ops); | ||
| 593 | if (err) | ||
| 594 | die("error registering pl script extension"); | ||
| 595 | |||
| 596 | scripting_context = malloc(sizeof(struct scripting_context)); | ||
| 597 | } | ||
| 598 | #endif | ||
diff --git a/tools/perf/util/trace-event-perl.h b/tools/perf/util/trace-event-perl.h new file mode 100644 index 000000000000..8fe0d866fe1a --- /dev/null +++ b/tools/perf/util/trace-event-perl.h | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #ifndef __PERF_TRACE_EVENT_PERL_H | ||
| 2 | #define __PERF_TRACE_EVENT_PERL_H | ||
| 3 | #ifdef NO_LIBPERL | ||
| 4 | typedef int INTERP; | ||
| 5 | #define dSP | ||
| 6 | #define ENTER | ||
| 7 | #define SAVETMPS | ||
| 8 | #define PUTBACK | ||
| 9 | #define SPAGAIN | ||
| 10 | #define FREETMPS | ||
| 11 | #define LEAVE | ||
| 12 | #define SP | ||
| 13 | #define ERRSV | ||
| 14 | #define G_SCALAR (0) | ||
| 15 | #define G_DISCARD (0) | ||
| 16 | #define G_NOARGS (0) | ||
| 17 | #define PUSHMARK(a) | ||
| 18 | #define SvTRUE(a) (0) | ||
| 19 | #define XPUSHs(s) | ||
| 20 | #define sv_2mortal(a) | ||
| 21 | #define newSVpv(a,b) | ||
| 22 | #define newSVuv(a) | ||
| 23 | #define newSViv(a) | ||
| 24 | #define get_cv(a,b) (0) | ||
| 25 | #define call_pv(a,b) (0) | ||
| 26 | #define perl_alloc() (0) | ||
| 27 | #define perl_construct(a) (0) | ||
| 28 | #define perl_parse(a,b,c,d,e) (0) | ||
| 29 | #define perl_run(a) (0) | ||
| 30 | #define perl_destruct(a) (0) | ||
| 31 | #define perl_free(a) (0) | ||
| 32 | #define pTHX void | ||
| 33 | #define CV void | ||
| 34 | #define dXSUB_SYS | ||
| 35 | #define pTHX_ | ||
| 36 | static inline void newXS(const char *a, void *b, const char *c) {} | ||
| 37 | #else | ||
| 38 | #include <EXTERN.h> | ||
| 39 | #include <perl.h> | ||
| 40 | typedef PerlInterpreter * INTERP; | ||
| 41 | #endif | ||
| 42 | |||
| 43 | struct scripting_context { | ||
| 44 | void *event_data; | ||
| 45 | }; | ||
| 46 | |||
| 47 | int common_pc(struct scripting_context *context); | ||
| 48 | int common_flags(struct scripting_context *context); | ||
| 49 | int common_lock_depth(struct scripting_context *context); | ||
| 50 | |||
| 51 | #endif /* __PERF_TRACE_EVENT_PERL_H */ | ||
diff --git a/tools/perf/util/trace-event-read.c b/tools/perf/util/trace-event-read.c index a1217a10632f..342dfdd43f87 100644 --- a/tools/perf/util/trace-event-read.c +++ b/tools/perf/util/trace-event-read.c | |||
| @@ -458,29 +458,28 @@ struct record *trace_read_data(int cpu) | |||
| 458 | return data; | 458 | return data; |
| 459 | } | 459 | } |
| 460 | 460 | ||
| 461 | void trace_report (void) | 461 | void trace_report(int fd) |
| 462 | { | 462 | { |
| 463 | const char *input_file = "trace.info"; | ||
| 464 | char buf[BUFSIZ]; | 463 | char buf[BUFSIZ]; |
| 465 | char test[] = { 23, 8, 68 }; | 464 | char test[] = { 23, 8, 68 }; |
| 466 | char *version; | 465 | char *version; |
| 466 | int show_version = 0; | ||
| 467 | int show_funcs = 0; | 467 | int show_funcs = 0; |
| 468 | int show_printk = 0; | 468 | int show_printk = 0; |
| 469 | 469 | ||
| 470 | input_fd = open(input_file, O_RDONLY); | 470 | input_fd = fd; |
| 471 | if (input_fd < 0) | ||
| 472 | die("opening '%s'\n", input_file); | ||
| 473 | 471 | ||
| 474 | read_or_die(buf, 3); | 472 | read_or_die(buf, 3); |
| 475 | if (memcmp(buf, test, 3) != 0) | 473 | if (memcmp(buf, test, 3) != 0) |
| 476 | die("not an trace data file"); | 474 | die("no trace data in the file"); |
| 477 | 475 | ||
| 478 | read_or_die(buf, 7); | 476 | read_or_die(buf, 7); |
| 479 | if (memcmp(buf, "tracing", 7) != 0) | 477 | if (memcmp(buf, "tracing", 7) != 0) |
| 480 | die("not a trace file (missing tracing)"); | 478 | die("not a trace file (missing 'tracing' tag)"); |
| 481 | 479 | ||
| 482 | version = read_string(); | 480 | version = read_string(); |
| 483 | printf("version = %s\n", version); | 481 | if (show_version) |
| 482 | printf("version = %s\n", version); | ||
| 484 | free(version); | 483 | free(version); |
| 485 | 484 | ||
| 486 | read_or_die(buf, 1); | 485 | read_or_die(buf, 1); |
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h index 420294a5773e..81698d5e6503 100644 --- a/tools/perf/util/trace-event.h +++ b/tools/perf/util/trace-event.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef _TRACE_EVENTS_H | 1 | #ifndef __PERF_TRACE_EVENTS_H |
| 2 | #define _TRACE_EVENTS_H | 2 | #define __PERF_TRACE_EVENTS_H |
| 3 | 3 | ||
| 4 | #include "parse-events.h" | 4 | #include "parse-events.h" |
| 5 | 5 | ||
| @@ -26,6 +26,11 @@ enum { | |||
| 26 | enum format_flags { | 26 | enum format_flags { |
| 27 | FIELD_IS_ARRAY = 1, | 27 | FIELD_IS_ARRAY = 1, |
| 28 | FIELD_IS_POINTER = 2, | 28 | FIELD_IS_POINTER = 2, |
| 29 | FIELD_IS_SIGNED = 4, | ||
| 30 | FIELD_IS_STRING = 8, | ||
| 31 | FIELD_IS_DYNAMIC = 16, | ||
| 32 | FIELD_IS_FLAG = 32, | ||
| 33 | FIELD_IS_SYMBOLIC = 64, | ||
| 29 | }; | 34 | }; |
| 30 | 35 | ||
| 31 | struct format_field { | 36 | struct format_field { |
| @@ -132,15 +137,18 @@ struct event { | |||
| 132 | int flags; | 137 | int flags; |
| 133 | struct format format; | 138 | struct format format; |
| 134 | struct print_fmt print_fmt; | 139 | struct print_fmt print_fmt; |
| 140 | char *system; | ||
| 135 | }; | 141 | }; |
| 136 | 142 | ||
| 137 | enum { | 143 | enum { |
| 138 | EVENT_FL_ISFTRACE = 1, | 144 | EVENT_FL_ISFTRACE = 0x01, |
| 139 | EVENT_FL_ISPRINT = 2, | 145 | EVENT_FL_ISPRINT = 0x02, |
| 140 | EVENT_FL_ISBPRINT = 4, | 146 | EVENT_FL_ISBPRINT = 0x04, |
| 141 | EVENT_FL_ISFUNC = 8, | 147 | EVENT_FL_ISFUNC = 0x08, |
| 142 | EVENT_FL_ISFUNCENT = 16, | 148 | EVENT_FL_ISFUNCENT = 0x10, |
| 143 | EVENT_FL_ISFUNCRET = 32, | 149 | EVENT_FL_ISFUNCRET = 0x20, |
| 150 | |||
| 151 | EVENT_FL_FAILED = 0x80000000 | ||
| 144 | }; | 152 | }; |
| 145 | 153 | ||
| 146 | struct record { | 154 | struct record { |
| @@ -154,7 +162,7 @@ struct record *trace_read_data(int cpu); | |||
| 154 | 162 | ||
| 155 | void parse_set_info(int nr_cpus, int long_sz); | 163 | void parse_set_info(int nr_cpus, int long_sz); |
| 156 | 164 | ||
| 157 | void trace_report(void); | 165 | void trace_report(int fd); |
| 158 | 166 | ||
| 159 | void *malloc_or_die(unsigned int size); | 167 | void *malloc_or_die(unsigned int size); |
| 160 | 168 | ||
| @@ -166,7 +174,7 @@ void print_funcs(void); | |||
| 166 | void print_printk(void); | 174 | void print_printk(void); |
| 167 | 175 | ||
| 168 | int parse_ftrace_file(char *buf, unsigned long size); | 176 | int parse_ftrace_file(char *buf, unsigned long size); |
| 169 | int parse_event_file(char *buf, unsigned long size, char *system); | 177 | int parse_event_file(char *buf, unsigned long size, char *sys); |
| 170 | void print_event(int cpu, void *data, int size, unsigned long long nsecs, | 178 | void print_event(int cpu, void *data, int size, unsigned long long nsecs, |
| 171 | char *comm); | 179 | char *comm); |
| 172 | 180 | ||
| @@ -233,8 +241,45 @@ extern int header_page_size_size; | |||
| 233 | extern int header_page_data_offset; | 241 | extern int header_page_data_offset; |
| 234 | extern int header_page_data_size; | 242 | extern int header_page_data_size; |
| 235 | 243 | ||
| 244 | extern int latency_format; | ||
| 245 | |||
| 236 | int parse_header_page(char *buf, unsigned long size); | 246 | int parse_header_page(char *buf, unsigned long size); |
| 247 | int trace_parse_common_type(void *data); | ||
| 248 | int trace_parse_common_pid(void *data); | ||
| 249 | int parse_common_pc(void *data); | ||
| 250 | int parse_common_flags(void *data); | ||
| 251 | int parse_common_lock_depth(void *data); | ||
| 252 | struct event *trace_find_event(int id); | ||
| 253 | struct event *trace_find_next_event(struct event *event); | ||
| 254 | unsigned long long read_size(void *ptr, int size); | ||
| 255 | unsigned long long | ||
| 256 | raw_field_value(struct event *event, const char *name, void *data); | ||
| 257 | void *raw_field_ptr(struct event *event, const char *name, void *data); | ||
| 258 | unsigned long long eval_flag(const char *flag); | ||
| 259 | |||
| 260 | int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events); | ||
| 261 | |||
| 262 | /* taken from kernel/trace/trace.h */ | ||
| 263 | enum trace_flag_type { | ||
| 264 | TRACE_FLAG_IRQS_OFF = 0x01, | ||
| 265 | TRACE_FLAG_IRQS_NOSUPPORT = 0x02, | ||
| 266 | TRACE_FLAG_NEED_RESCHED = 0x04, | ||
| 267 | TRACE_FLAG_HARDIRQ = 0x08, | ||
| 268 | TRACE_FLAG_SOFTIRQ = 0x10, | ||
| 269 | }; | ||
| 270 | |||
| 271 | struct scripting_ops { | ||
| 272 | const char *name; | ||
| 273 | int (*start_script) (const char *); | ||
| 274 | int (*stop_script) (void); | ||
| 275 | void (*process_event) (int cpu, void *data, int size, | ||
| 276 | unsigned long long nsecs, char *comm); | ||
| 277 | int (*generate_script) (const char *outfile); | ||
| 278 | }; | ||
| 279 | |||
| 280 | int script_spec_register(const char *spec, struct scripting_ops *ops); | ||
| 237 | 281 | ||
| 238 | void read_tracing_data(struct perf_counter_attr *pattrs, int nb_counters); | 282 | extern struct scripting_ops perl_scripting_ops; |
| 283 | void setup_perl_scripting(void); | ||
| 239 | 284 | ||
| 240 | #endif /* _TRACE_EVENTS_H */ | 285 | #endif /* __PERF_TRACE_EVENTS_H */ |
diff --git a/tools/perf/util/types.h b/tools/perf/util/types.h index 5e75f9005940..7d6b8331f898 100644 --- a/tools/perf/util/types.h +++ b/tools/perf/util/types.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef _PERF_TYPES_H | 1 | #ifndef __PERF_TYPES_H |
| 2 | #define _PERF_TYPES_H | 2 | #define __PERF_TYPES_H |
| 3 | 3 | ||
| 4 | /* | 4 | /* |
| 5 | * We define u64 as unsigned long long for every architecture | 5 | * We define u64 as unsigned long long for every architecture |
| @@ -14,4 +14,4 @@ typedef signed short s16; | |||
| 14 | typedef unsigned char u8; | 14 | typedef unsigned char u8; |
| 15 | typedef signed char s8; | 15 | typedef signed char s8; |
| 16 | 16 | ||
| 17 | #endif /* _PERF_TYPES_H */ | 17 | #endif /* __PERF_TYPES_H */ |
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 9de2329dd44d..c673d8825883 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h | |||
| @@ -84,6 +84,9 @@ | |||
| 84 | #include <iconv.h> | 84 | #include <iconv.h> |
| 85 | #endif | 85 | #endif |
| 86 | 86 | ||
| 87 | extern const char *graph_line; | ||
| 88 | extern const char *graph_dotted_line; | ||
| 89 | |||
| 87 | /* On most systems <limits.h> would have given us this, but | 90 | /* On most systems <limits.h> would have given us this, but |
| 88 | * not on some systems (e.g. GNU/Hurd). | 91 | * not on some systems (e.g. GNU/Hurd). |
| 89 | */ | 92 | */ |
| @@ -134,6 +137,15 @@ extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, | |||
| 134 | extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); | 137 | extern int error(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 135 | extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); | 138 | extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); |
| 136 | 139 | ||
| 140 | #include "../../../include/linux/stringify.h" | ||
| 141 | |||
| 142 | #define DIE_IF(cnd) \ | ||
| 143 | do { if (cnd) \ | ||
| 144 | die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \ | ||
| 145 | __stringify(cnd) "\n"); \ | ||
| 146 | } while (0) | ||
| 147 | |||
| 148 | |||
| 137 | extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); | 149 | extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); |
| 138 | 150 | ||
| 139 | extern int prefixcmp(const char *str, const char *prefix); | 151 | extern int prefixcmp(const char *str, const char *prefix); |
| @@ -278,17 +290,15 @@ static inline char *gitstrchrnul(const char *s, int c) | |||
| 278 | * Wrappers: | 290 | * Wrappers: |
| 279 | */ | 291 | */ |
| 280 | extern char *xstrdup(const char *str); | 292 | extern char *xstrdup(const char *str); |
| 281 | extern void *xmalloc(size_t size); | 293 | extern void *xmalloc(size_t size) __attribute__((weak)); |
| 282 | extern void *xmemdupz(const void *data, size_t len); | 294 | extern void *xmemdupz(const void *data, size_t len); |
| 283 | extern char *xstrndup(const char *str, size_t len); | 295 | extern char *xstrndup(const char *str, size_t len); |
| 284 | extern void *xrealloc(void *ptr, size_t size); | 296 | extern void *xrealloc(void *ptr, size_t size) __attribute__((weak)); |
| 285 | extern void *xcalloc(size_t nmemb, size_t size); | 297 | |
| 286 | extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); | 298 | static inline void *zalloc(size_t size) |
| 287 | extern ssize_t xread(int fd, void *buf, size_t len); | 299 | { |
| 288 | extern ssize_t xwrite(int fd, const void *buf, size_t len); | 300 | return calloc(1, size); |
| 289 | extern int xdup(int fd); | 301 | } |
| 290 | extern FILE *xfdopen(int fd, const char *mode); | ||
| 291 | extern int xmkstemp(char *template); | ||
| 292 | 302 | ||
| 293 | static inline size_t xsize_t(off_t len) | 303 | static inline size_t xsize_t(off_t len) |
| 294 | { | 304 | { |
| @@ -306,6 +316,7 @@ static inline int has_extension(const char *filename, const char *ext) | |||
| 306 | #undef isascii | 316 | #undef isascii |
| 307 | #undef isspace | 317 | #undef isspace |
| 308 | #undef isdigit | 318 | #undef isdigit |
| 319 | #undef isxdigit | ||
| 309 | #undef isalpha | 320 | #undef isalpha |
| 310 | #undef isprint | 321 | #undef isprint |
| 311 | #undef isalnum | 322 | #undef isalnum |
| @@ -323,6 +334,8 @@ extern unsigned char sane_ctype[256]; | |||
| 323 | #define isascii(x) (((x) & ~0x7f) == 0) | 334 | #define isascii(x) (((x) & ~0x7f) == 0) |
| 324 | #define isspace(x) sane_istest(x,GIT_SPACE) | 335 | #define isspace(x) sane_istest(x,GIT_SPACE) |
| 325 | #define isdigit(x) sane_istest(x,GIT_DIGIT) | 336 | #define isdigit(x) sane_istest(x,GIT_DIGIT) |
| 337 | #define isxdigit(x) \ | ||
| 338 | (sane_istest(toupper(x), GIT_ALPHA | GIT_DIGIT) && toupper(x) < 'G') | ||
| 326 | #define isalpha(x) sane_istest(x,GIT_ALPHA) | 339 | #define isalpha(x) sane_istest(x,GIT_ALPHA) |
| 327 | #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) | 340 | #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) |
| 328 | #define isprint(x) sane_istest(x,GIT_PRINT) | 341 | #define isprint(x) sane_istest(x,GIT_PRINT) |
diff --git a/tools/perf/util/values.h b/tools/perf/util/values.h index cadf8cf2a590..2fa967e1a88a 100644 --- a/tools/perf/util/values.h +++ b/tools/perf/util/values.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #ifndef _PERF_VALUES_H | 1 | #ifndef __PERF_VALUES_H |
| 2 | #define _PERF_VALUES_H | 2 | #define __PERF_VALUES_H |
| 3 | 3 | ||
| 4 | #include "types.h" | 4 | #include "types.h" |
| 5 | 5 | ||
| @@ -24,4 +24,4 @@ void perf_read_values_add_value(struct perf_read_values *values, | |||
| 24 | void perf_read_values_display(FILE *fp, struct perf_read_values *values, | 24 | void perf_read_values_display(FILE *fp, struct perf_read_values *values, |
| 25 | int raw); | 25 | int raw); |
| 26 | 26 | ||
| 27 | #endif /* _PERF_VALUES_H */ | 27 | #endif /* __PERF_VALUES_H */ |
diff --git a/tools/perf/util/wrapper.c b/tools/perf/util/wrapper.c index 4574ac28396f..bf44ca85d23b 100644 --- a/tools/perf/util/wrapper.c +++ b/tools/perf/util/wrapper.c | |||
| @@ -79,43 +79,12 @@ void *xrealloc(void *ptr, size_t size) | |||
| 79 | return ret; | 79 | return ret; |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | void *xcalloc(size_t nmemb, size_t size) | ||
| 83 | { | ||
| 84 | void *ret = calloc(nmemb, size); | ||
| 85 | if (!ret && (!nmemb || !size)) | ||
| 86 | ret = calloc(1, 1); | ||
| 87 | if (!ret) { | ||
| 88 | release_pack_memory(nmemb * size, -1); | ||
| 89 | ret = calloc(nmemb, size); | ||
| 90 | if (!ret && (!nmemb || !size)) | ||
| 91 | ret = calloc(1, 1); | ||
| 92 | if (!ret) | ||
| 93 | die("Out of memory, calloc failed"); | ||
| 94 | } | ||
| 95 | return ret; | ||
| 96 | } | ||
| 97 | |||
| 98 | void *xmmap(void *start, size_t length, | ||
| 99 | int prot, int flags, int fd, off_t offset) | ||
| 100 | { | ||
| 101 | void *ret = mmap(start, length, prot, flags, fd, offset); | ||
| 102 | if (ret == MAP_FAILED) { | ||
| 103 | if (!length) | ||
| 104 | return NULL; | ||
| 105 | release_pack_memory(length, fd); | ||
| 106 | ret = mmap(start, length, prot, flags, fd, offset); | ||
| 107 | if (ret == MAP_FAILED) | ||
| 108 | die("Out of memory? mmap failed: %s", strerror(errno)); | ||
| 109 | } | ||
| 110 | return ret; | ||
| 111 | } | ||
| 112 | |||
| 113 | /* | 82 | /* |
| 114 | * xread() is the same a read(), but it automatically restarts read() | 83 | * xread() is the same a read(), but it automatically restarts read() |
| 115 | * operations with a recoverable error (EAGAIN and EINTR). xread() | 84 | * operations with a recoverable error (EAGAIN and EINTR). xread() |
| 116 | * DOES NOT GUARANTEE that "len" bytes is read even if the data is available. | 85 | * DOES NOT GUARANTEE that "len" bytes is read even if the data is available. |
| 117 | */ | 86 | */ |
| 118 | ssize_t xread(int fd, void *buf, size_t len) | 87 | static ssize_t xread(int fd, void *buf, size_t len) |
| 119 | { | 88 | { |
| 120 | ssize_t nr; | 89 | ssize_t nr; |
| 121 | while (1) { | 90 | while (1) { |
| @@ -131,7 +100,7 @@ ssize_t xread(int fd, void *buf, size_t len) | |||
| 131 | * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT | 100 | * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT |
| 132 | * GUARANTEE that "len" bytes is written even if the operation is successful. | 101 | * GUARANTEE that "len" bytes is written even if the operation is successful. |
| 133 | */ | 102 | */ |
| 134 | ssize_t xwrite(int fd, const void *buf, size_t len) | 103 | static ssize_t xwrite(int fd, const void *buf, size_t len) |
| 135 | { | 104 | { |
| 136 | ssize_t nr; | 105 | ssize_t nr; |
| 137 | while (1) { | 106 | while (1) { |
| @@ -179,29 +148,3 @@ ssize_t write_in_full(int fd, const void *buf, size_t count) | |||
| 179 | 148 | ||
| 180 | return total; | 149 | return total; |
| 181 | } | 150 | } |
| 182 | |||
| 183 | int xdup(int fd) | ||
| 184 | { | ||
| 185 | int ret = dup(fd); | ||
| 186 | if (ret < 0) | ||
| 187 | die("dup failed: %s", strerror(errno)); | ||
| 188 | return ret; | ||
| 189 | } | ||
| 190 | |||
| 191 | FILE *xfdopen(int fd, const char *mode) | ||
| 192 | { | ||
| 193 | FILE *stream = fdopen(fd, mode); | ||
| 194 | if (stream == NULL) | ||
| 195 | die("Out of memory? fdopen failed: %s", strerror(errno)); | ||
| 196 | return stream; | ||
| 197 | } | ||
| 198 | |||
| 199 | int xmkstemp(char *template) | ||
| 200 | { | ||
| 201 | int fd; | ||
| 202 | |||
| 203 | fd = mkstemp(template); | ||
| 204 | if (fd < 0) | ||
| 205 | die("Unable to create temporary file: %s", strerror(errno)); | ||
| 206 | return fd; | ||
| 207 | } | ||
