aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2015-04-14 17:37:47 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2015-04-14 17:37:47 -0400
commit6c8a53c9e6a151fffb07f8b4c34bd1e33dddd467 (patch)
tree791caf826ef136c521a97b7878f226b6ba1c1d75
parente95e7f627062be5e6ce971ce873e6234c91ffc50 (diff)
parent066450be419fa48007a9f29e19828f2a86198754 (diff)
Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull perf changes from Ingo Molnar: "Core kernel changes: - One of the more interesting features in this cycle is the ability to attach eBPF programs (user-defined, sandboxed bytecode executed by the kernel) to kprobes. This allows user-defined instrumentation on a live kernel image that can never crash, hang or interfere with the kernel negatively. (Right now it's limited to root-only, but in the future we might allow unprivileged use as well.) (Alexei Starovoitov) - Another non-trivial feature is per event clockid support: this allows, amongst other things, the selection of different clock sources for event timestamps traced via perf. This feature is sought by people who'd like to merge perf generated events with external events that were measured with different clocks: - cluster wide profiling - for system wide tracing with user-space events, - JIT profiling events etc. Matching perf tooling support is added as well, available via the -k, --clockid <clockid> parameter to perf record et al. (Peter Zijlstra) Hardware enablement kernel changes: - x86 Intel Processor Trace (PT) support: which is a hardware tracer on steroids, available on Broadwell CPUs. The hardware trace stream is directly output into the user-space ring-buffer, using the 'AUX' data format extension that was added to the perf core to support hardware constraints such as the necessity to have the tracing buffer physically contiguous. This patch-set was developed for two years and this is the result. A simple way to make use of this is to use BTS tracing, the PT driver emulates BTS output - available via the 'intel_bts' PMU. More explicit PT specific tooling support is in the works as well - will probably be ready by 4.2. (Alexander Shishkin, Peter Zijlstra) - x86 Intel Cache QoS Monitoring (CQM) support: this is a hardware feature of Intel Xeon CPUs that allows the measurement and allocation/partitioning of caches to individual workloads. These kernel changes expose the measurement side as a new PMU driver, which exposes various QoS related PMU events. (The partitioning change is work in progress and is planned to be merged as a cgroup extension.) (Matt Fleming, Peter Zijlstra; CPU feature detection by Peter P Waskiewicz Jr) - x86 Intel Haswell LBR call stack support: this is a new Haswell feature that allows the hardware recording of call chains, plus tooling support. To activate this feature you have to enable it via the new 'lbr' call-graph recording option: perf record --call-graph lbr perf report or: perf top --call-graph lbr This hardware feature is a lot faster than stack walk or dwarf based unwinding, but has some limitations: - It reuses the current LBR facility, so LBR call stack and branch record can not be enabled at the same time. - It is only available for user-space callchains. (Yan, Zheng) - x86 Intel Broadwell CPU support and various event constraints and event table fixes for earlier models. (Andi Kleen) - x86 Intel HT CPUs event scheduling workarounds. This is a complex CPU bug affecting the SNB,IVB,HSW families that results in counter value corruption. The mitigation code is automatically enabled and is transparent. (Maria Dimakopoulou, Stephane Eranian) The perf tooling side had a ton of changes in this cycle as well, so I'm only able to list the user visible changes here, in addition to the tooling changes outlined above: User visible changes affecting all tools: - Improve support of compressed kernel modules (Jiri Olsa) - Save DSO loading errno to better report errors (Arnaldo Carvalho de Melo) - Bash completion for subcommands (Yunlong Song) - Add 'I' event modifier for perf_event_attr.exclude_idle bit (Jiri Olsa) - Support missing -f to override perf.data file ownership. (Yunlong Song) - Show the first event with an invalid filter (David Ahern, Arnaldo Carvalho de Melo) User visible changes in individual tools: 'perf data': New tool for converting perf.data to other formats, initially for the CTF (Common Trace Format) from LTTng (Jiri Olsa, Sebastian Siewior) 'perf diff': Add --kallsyms option (David Ahern) 'perf list': Allow listing events with 'tracepoint' prefix (Yunlong Song) Sort the output of the command (Yunlong Song) 'perf kmem': Respect -i option (Jiri Olsa) Print big numbers using thousands' group (Namhyung Kim) Allow -v option (Namhyung Kim) Fix alignment of slab result table (Namhyung Kim) 'perf probe': Support multiple probes on different binaries on the same command line (Masami Hiramatsu) Support unnamed union/structure members data collection. (Masami Hiramatsu) Check kprobes blacklist when adding new events. (Masami Hiramatsu) 'perf record': Teach 'perf record' about perf_event_attr.clockid (Peter Zijlstra) Support recording running/enabled time (Andi Kleen) 'perf sched': Improve the performance of 'perf sched replay' on high CPU core count machines (Yunlong Song) 'perf report' and 'perf top': Allow annotating entries in callchains in the hists browser (Arnaldo Carvalho de Melo) Indicate which callchain entries are annotated in the TUI hists browser (Arnaldo Carvalho de Melo) Add pid/tid filtering to 'report' and 'script' commands (David Ahern) Consider PERF_RECORD_ events with cpumode == 0 in 'perf top', removing one cause of long term memory usage buildup, i.e. not processing PERF_RECORD_EXIT events (Arnaldo Carvalho de Melo) 'perf stat': Report unsupported events properly (Suzuki K. Poulose) Output running time and run/enabled ratio in CSV mode (Andi Kleen) 'perf trace': Handle legacy syscalls tracepoints (David Ahern, Arnaldo Carvalho de Melo) Only insert blank duration bracket when tracing syscalls (Arnaldo Carvalho de Melo) Filter out the trace pid when no threads are specified (Arnaldo Carvalho de Melo) Dump stack on segfaults (Arnaldo Carvalho de Melo) No need to explicitely enable evsels for workload started from perf, let it be enabled via perf_event_attr.enable_on_exec, removing some events that take place in the 'perf trace' before a workload is really started by it. (Arnaldo Carvalho de Melo) Allow mixing with tracepoints and suppressing plain syscalls. (Arnaldo Carvalho de Melo) There's also been a ton of infrastructure work done, such as the split-out of perf's build system into tools/build/ and other changes - see the shortlog and changelog for details" * 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (358 commits) perf/x86/intel/pt: Clean up the control flow in pt_pmu_hw_init() perf evlist: Fix type for references to data_head/tail perf probe: Check the orphaned -x option perf probe: Support multiple probes on different binaries perf buildid-list: Fix segfault when show DSOs with hits perf tools: Fix cross-endian analysis perf tools: Fix error path to do closedir() when synthesizing threads perf tools: Fix synthesizing fork_event.ppid for non-main thread perf tools: Add 'I' event modifier for exclude_idle bit perf report: Don't call map__kmap if map is NULL. perf tests: Fix attr tests perf probe: Fix ARM 32 building error perf tools: Merge all perf_event_attr print functions perf record: Add clockid parameter perf sched replay: Use replay_repeat to calculate the runavg of cpu usage instead of the default value 10 perf sched replay: Support using -f to override perf.data file ownership perf sched replay: Fix the EMFILE error caused by the limitation of the maximum open files perf sched replay: Handle the dead halt of sem_wait when create_tasks() fails for any task perf sched replay: Fix the segmentation fault problem caused by pr_err in threads perf sched replay: Realloc the memory of pid_to_task stepwise to adapt to the different pid_max configurations ...
-rw-r--r--arch/arm/kernel/hw_breakpoint.c2
-rw-r--r--arch/arm64/kernel/hw_breakpoint.c2
-rw-r--r--arch/powerpc/perf/core-book3s.c13
-rw-r--r--arch/x86/include/asm/cpufeature.h10
-rw-r--r--arch/x86/include/asm/processor.h3
-rw-r--r--arch/x86/include/uapi/asm/msr-index.h18
-rw-r--r--arch/x86/kernel/cpu/Makefile3
-rw-r--r--arch/x86/kernel/cpu/common.c39
-rw-r--r--arch/x86/kernel/cpu/intel_pt.h131
-rw-r--r--arch/x86/kernel/cpu/perf_event.c205
-rw-r--r--arch/x86/kernel/cpu/perf_event.h167
-rw-r--r--arch/x86/kernel/cpu/perf_event_amd.c9
-rw-r--r--arch/x86/kernel/cpu/perf_event_amd_ibs.c12
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel.c908
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel_bts.c525
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel_cqm.c1379
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel_ds.c31
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel_lbr.c321
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel_pt.c1103
-rw-r--r--arch/x86/kernel/cpu/perf_event_intel_uncore_snbep.c3
-rw-r--r--arch/x86/kernel/cpu/scattered.c1
-rw-r--r--arch/x86/kernel/kprobes/core.c9
-rw-r--r--include/linux/bpf.h20
-rw-r--r--include/linux/ftrace_event.h14
-rw-r--r--include/linux/perf_event.h121
-rw-r--r--include/linux/watchdog.h8
-rw-r--r--include/uapi/linux/bpf.h5
-rw-r--r--include/uapi/linux/perf_event.h115
-rw-r--r--init/Kconfig2
-rw-r--r--kernel/bpf/syscall.c7
-rw-r--r--kernel/events/core.c752
-rw-r--r--kernel/events/hw_breakpoint.c8
-rw-r--r--kernel/events/internal.h33
-rw-r--r--kernel/events/ring_buffer.c327
-rw-r--r--kernel/trace/Kconfig8
-rw-r--r--kernel/trace/Makefile1
-rw-r--r--kernel/trace/bpf_trace.c222
-rw-r--r--kernel/trace/trace_kprobe.c10
-rw-r--r--kernel/trace/trace_uprobe.c10
-rw-r--r--kernel/watchdog.c28
-rw-r--r--samples/bpf/Makefile16
-rw-r--r--samples/bpf/bpf_helpers.h6
-rw-r--r--samples/bpf/bpf_load.c125
-rw-r--r--samples/bpf/bpf_load.h3
-rw-r--r--samples/bpf/libbpf.c14
-rw-r--r--samples/bpf/libbpf.h5
-rw-r--r--samples/bpf/sock_example.c2
-rw-r--r--samples/bpf/test_verifier.c2
-rw-r--r--samples/bpf/tracex1_kern.c50
-rw-r--r--samples/bpf/tracex1_user.c25
-rw-r--r--samples/bpf/tracex2_kern.c86
-rw-r--r--samples/bpf/tracex2_user.c95
-rw-r--r--samples/bpf/tracex3_kern.c89
-rw-r--r--samples/bpf/tracex3_user.c150
-rw-r--r--samples/bpf/tracex4_kern.c54
-rw-r--r--samples/bpf/tracex4_user.c69
-rw-r--r--tools/build/Build.include81
-rw-r--r--tools/build/Documentation/Build.txt139
-rw-r--r--tools/build/Makefile.build130
-rw-r--r--tools/build/Makefile.feature171
-rw-r--r--tools/build/feature/.gitignore (renamed from tools/perf/config/feature-checks/.gitignore)1
-rw-r--r--tools/build/feature/Makefile (renamed from tools/perf/config/feature-checks/Makefile)31
-rw-r--r--tools/build/feature/test-all.c (renamed from tools/perf/config/feature-checks/test-all.c)19
-rw-r--r--tools/build/feature/test-backtrace.c (renamed from tools/perf/config/feature-checks/test-backtrace.c)0
-rw-r--r--tools/build/feature/test-bionic.c (renamed from tools/perf/config/feature-checks/test-bionic.c)0
-rw-r--r--tools/build/feature/test-compile.c (renamed from tools/perf/config/feature-checks/test-compile.c)0
-rw-r--r--tools/build/feature/test-cplus-demangle.c (renamed from tools/perf/config/feature-checks/test-cplus-demangle.c)0
-rw-r--r--tools/build/feature/test-dwarf.c (renamed from tools/perf/config/feature-checks/test-dwarf.c)0
-rw-r--r--tools/build/feature/test-fortify-source.c (renamed from tools/perf/config/feature-checks/test-fortify-source.c)0
-rw-r--r--tools/build/feature/test-glibc.c (renamed from tools/perf/config/feature-checks/test-glibc.c)0
-rw-r--r--tools/build/feature/test-gtk2-infobar.c (renamed from tools/perf/config/feature-checks/test-gtk2-infobar.c)0
-rw-r--r--tools/build/feature/test-gtk2.c (renamed from tools/perf/config/feature-checks/test-gtk2.c)0
-rw-r--r--tools/build/feature/test-hello.c (renamed from tools/perf/config/feature-checks/test-hello.c)0
-rw-r--r--tools/build/feature/test-libaudit.c (renamed from tools/perf/config/feature-checks/test-libaudit.c)0
-rw-r--r--tools/build/feature/test-libbabeltrace.c9
-rw-r--r--tools/build/feature/test-libbfd.c (renamed from tools/perf/config/feature-checks/test-libbfd.c)0
-rw-r--r--tools/build/feature/test-libdw-dwarf-unwind.c (renamed from tools/perf/config/feature-checks/test-libdw-dwarf-unwind.c)0
-rw-r--r--tools/build/feature/test-libelf-getphdrnum.c (renamed from tools/perf/config/feature-checks/test-libelf-getphdrnum.c)0
-rw-r--r--tools/build/feature/test-libelf-mmap.c (renamed from tools/perf/config/feature-checks/test-libelf-mmap.c)0
-rw-r--r--tools/build/feature/test-libelf.c (renamed from tools/perf/config/feature-checks/test-libelf.c)0
-rw-r--r--tools/build/feature/test-libnuma.c (renamed from tools/perf/config/feature-checks/test-libnuma.c)0
-rw-r--r--tools/build/feature/test-libperl.c (renamed from tools/perf/config/feature-checks/test-libperl.c)0
-rw-r--r--tools/build/feature/test-libpython-version.c (renamed from tools/perf/config/feature-checks/test-libpython-version.c)0
-rw-r--r--tools/build/feature/test-libpython.c (renamed from tools/perf/config/feature-checks/test-libpython.c)0
-rw-r--r--tools/build/feature/test-libslang.c (renamed from tools/perf/config/feature-checks/test-libslang.c)0
-rw-r--r--tools/build/feature/test-libunwind-debug-frame.c (renamed from tools/perf/config/feature-checks/test-libunwind-debug-frame.c)0
-rw-r--r--tools/build/feature/test-libunwind.c (renamed from tools/perf/config/feature-checks/test-libunwind.c)0
-rw-r--r--tools/build/feature/test-lzma.c10
-rw-r--r--tools/build/feature/test-pthread-attr-setaffinity-np.c (renamed from tools/perf/config/feature-checks/test-pthread-attr-setaffinity-np.c)4
-rw-r--r--tools/build/feature/test-stackprotector-all.c (renamed from tools/perf/config/feature-checks/test-stackprotector-all.c)0
-rw-r--r--tools/build/feature/test-sync-compare-and-swap.c (renamed from tools/perf/config/feature-checks/test-sync-compare-and-swap.c)0
-rw-r--r--tools/build/feature/test-timerfd.c (renamed from tools/perf/config/feature-checks/test-timerfd.c)0
-rw-r--r--tools/build/feature/test-zlib.c (renamed from tools/perf/config/feature-checks/test-zlib.c)0
-rw-r--r--tools/build/tests/ex/Build8
-rw-r--r--tools/build/tests/ex/Makefile23
-rw-r--r--tools/build/tests/ex/a.c5
-rw-r--r--tools/build/tests/ex/arch/Build2
-rw-r--r--tools/build/tests/ex/arch/e.c5
-rw-r--r--tools/build/tests/ex/arch/f.c5
-rw-r--r--tools/build/tests/ex/b.c5
-rw-r--r--tools/build/tests/ex/c.c5
-rw-r--r--tools/build/tests/ex/d.c5
-rw-r--r--tools/build/tests/ex/empty/Build0
-rw-r--r--tools/build/tests/ex/ex.c19
-rwxr-xr-xtools/build/tests/run.sh42
-rw-r--r--tools/lib/api/Build2
-rw-r--r--tools/lib/api/Makefile58
-rw-r--r--tools/lib/api/fd/Build1
-rw-r--r--tools/lib/api/fs/Build4
-rw-r--r--tools/lib/api/fs/debugfs.c69
-rw-r--r--tools/lib/api/fs/debugfs.h13
-rw-r--r--tools/lib/api/fs/findfs.c63
-rw-r--r--tools/lib/api/fs/findfs.h23
-rw-r--r--tools/lib/api/fs/tracefs.c78
-rw-r--r--tools/lib/api/fs/tracefs.h21
-rw-r--r--tools/lib/lockdep/Build1
-rw-r--r--tools/lib/lockdep/Makefile132
-rw-r--r--tools/lib/traceevent/Build17
-rw-r--r--tools/lib/traceevent/Makefile169
-rw-r--r--tools/lib/traceevent/event-parse.c305
-rw-r--r--tools/lib/traceevent/event-parse.h24
-rw-r--r--tools/lib/traceevent/event-plugin.c60
-rw-r--r--tools/lib/traceevent/kbuffer-parse.c12
-rw-r--r--tools/lib/traceevent/kbuffer.h1
-rw-r--r--tools/lib/traceevent/parse-filter.c2
-rw-r--r--tools/lib/traceevent/trace-seq.c13
-rw-r--r--tools/perf/.gitignore1
-rw-r--r--tools/perf/Build44
-rw-r--r--tools/perf/Documentation/Build.txt49
-rw-r--r--tools/perf/Documentation/perf-buildid-cache.txt24
-rw-r--r--tools/perf/Documentation/perf-data.txt40
-rw-r--r--tools/perf/Documentation/perf-diff.txt8
-rw-r--r--tools/perf/Documentation/perf-kmem.txt4
-rw-r--r--tools/perf/Documentation/perf-list.txt7
-rw-r--r--tools/perf/Documentation/perf-probe.txt16
-rw-r--r--tools/perf/Documentation/perf-record.txt30
-rw-r--r--tools/perf/Documentation/perf-report.txt5
-rw-r--r--tools/perf/Documentation/perf-script.txt6
-rw-r--r--tools/perf/Documentation/perf-trace.txt6
-rw-r--r--tools/perf/Documentation/perf.txt7
-rw-r--r--tools/perf/MANIFEST1
-rw-r--r--tools/perf/Makefile4
-rw-r--r--tools/perf/Makefile.perf622
-rw-r--r--tools/perf/arch/Build2
-rw-r--r--tools/perf/arch/arm/Build2
-rw-r--r--tools/perf/arch/arm/Makefile11
-rw-r--r--tools/perf/arch/arm/tests/Build2
-rw-r--r--tools/perf/arch/arm/util/Build4
-rw-r--r--tools/perf/arch/arm64/Build1
-rw-r--r--tools/perf/arch/arm64/Makefile4
-rw-r--r--tools/perf/arch/arm64/util/Build2
-rw-r--r--tools/perf/arch/powerpc/Build1
-rw-r--r--tools/perf/arch/powerpc/Makefile3
-rw-r--r--tools/perf/arch/powerpc/util/Build4
-rw-r--r--tools/perf/arch/s390/Build1
-rw-r--r--tools/perf/arch/s390/Makefile3
-rw-r--r--tools/perf/arch/s390/util/Build4
-rw-r--r--tools/perf/arch/sh/Build1
-rw-r--r--tools/perf/arch/sh/Makefile1
-rw-r--r--tools/perf/arch/sh/util/Build1
-rw-r--r--tools/perf/arch/sparc/Build1
-rw-r--r--tools/perf/arch/sparc/Makefile1
-rw-r--r--tools/perf/arch/sparc/util/Build1
-rw-r--r--tools/perf/arch/x86/Build2
-rw-r--r--tools/perf/arch/x86/Makefile15
-rw-r--r--tools/perf/arch/x86/tests/Build2
-rw-r--r--tools/perf/arch/x86/util/Build8
-rw-r--r--tools/perf/bench/Build11
-rw-r--r--tools/perf/builtin-annotate.c2
-rw-r--r--tools/perf/builtin-buildid-cache.c107
-rw-r--r--tools/perf/builtin-buildid-list.c2
-rw-r--r--tools/perf/builtin-data.c123
-rw-r--r--tools/perf/builtin-diff.c6
-rw-r--r--tools/perf/builtin-evlist.c2
-rw-r--r--tools/perf/builtin-help.c17
-rw-r--r--tools/perf/builtin-inject.c16
-rw-r--r--tools/perf/builtin-kmem.c38
-rw-r--r--tools/perf/builtin-kvm.c15
-rw-r--r--tools/perf/builtin-list.c28
-rw-r--r--tools/perf/builtin-lock.c7
-rw-r--r--tools/perf/builtin-mem.c7
-rw-r--r--tools/perf/builtin-probe.c19
-rw-r--r--tools/perf/builtin-record.c108
-rw-r--r--tools/perf/builtin-report.c16
-rw-r--r--tools/perf/builtin-sched.c101
-rw-r--r--tools/perf/builtin-script.c34
-rw-r--r--tools/perf/builtin-stat.c98
-rw-r--r--tools/perf/builtin-timechart.c12
-rw-r--r--tools/perf/builtin-top.c10
-rw-r--r--tools/perf/builtin-trace.c281
-rw-r--r--tools/perf/builtin.h1
-rw-r--r--tools/perf/command-list.txt1
-rw-r--r--tools/perf/config/Makefile293
-rw-r--r--tools/perf/config/Makefile.arch29
-rw-r--r--tools/perf/config/utilities.mak3
-rw-r--r--tools/perf/perf-completion.sh94
-rw-r--r--tools/perf/perf.c29
-rw-r--r--tools/perf/perf.h5
-rw-r--r--tools/perf/scripts/Build2
-rw-r--r--tools/perf/scripts/perl/Perf-Trace-Util/Build3
-rw-r--r--tools/perf/scripts/python/Perf-Trace-Util/Build3
-rw-r--r--tools/perf/tests/Build43
-rw-r--r--tools/perf/tests/attr/base-record2
-rw-r--r--tools/perf/tests/attr/base-stat2
-rw-r--r--tools/perf/tests/builtin-test.c9
-rw-r--r--tools/perf/tests/dso-data.c22
-rw-r--r--tools/perf/tests/kmod-path.c73
-rw-r--r--tools/perf/tests/make2
-rw-r--r--tools/perf/tests/open-syscall-all-cpus.c7
-rw-r--r--tools/perf/tests/open-syscall.c7
-rw-r--r--tools/perf/tests/parse-events.c53
-rw-r--r--tools/perf/tests/tests.h1
-rw-r--r--tools/perf/ui/Build14
-rw-r--r--tools/perf/ui/browsers/Build10
-rw-r--r--tools/perf/ui/browsers/annotate.c8
-rw-r--r--tools/perf/ui/browsers/hists.c88
-rw-r--r--tools/perf/ui/gtk/Build9
-rw-r--r--tools/perf/ui/tui/Build4
-rw-r--r--tools/perf/util/Build145
-rw-r--r--tools/perf/util/annotate.c32
-rw-r--r--tools/perf/util/build-id.c209
-rw-r--r--tools/perf/util/build-id.h8
-rw-r--r--tools/perf/util/cache.h1
-rw-r--r--tools/perf/util/callchain.c8
-rw-r--r--tools/perf/util/callchain.h1
-rw-r--r--tools/perf/util/cloexec.c6
-rw-r--r--tools/perf/util/cloexec.h6
-rw-r--r--tools/perf/util/data-convert-bt.c857
-rw-r--r--tools/perf/util/data-convert-bt.h8
-rw-r--r--tools/perf/util/db-export.c4
-rw-r--r--tools/perf/util/db-export.h3
-rw-r--r--tools/perf/util/debug.c2
-rw-r--r--tools/perf/util/debug.h1
-rw-r--r--tools/perf/util/dso.c159
-rw-r--r--tools/perf/util/dso.h49
-rw-r--r--tools/perf/util/dwarf-aux.c29
-rw-r--r--tools/perf/util/dwarf-aux.h3
-rw-r--r--tools/perf/util/event.c190
-rw-r--r--tools/perf/util/event.h1
-rw-r--r--tools/perf/util/evlist.c49
-rw-r--r--tools/perf/util/evlist.h14
-rw-r--r--tools/perf/util/evsel.c354
-rw-r--r--tools/perf/util/evsel.h11
-rw-r--r--tools/perf/util/header.c35
-rw-r--r--tools/perf/util/hist.c11
-rw-r--r--tools/perf/util/hist.h11
-rw-r--r--tools/perf/util/kvm-stat.h1
-rw-r--r--tools/perf/util/lzma.c95
-rw-r--r--tools/perf/util/machine.c332
-rw-r--r--tools/perf/util/machine.h2
-rw-r--r--tools/perf/util/map.c20
-rw-r--r--tools/perf/util/map.h6
-rw-r--r--tools/perf/util/ordered-events.c65
-rw-r--r--tools/perf/util/ordered-events.h19
-rw-r--r--tools/perf/util/parse-events.c242
-rw-r--r--tools/perf/util/parse-events.h13
-rw-r--r--tools/perf/util/parse-events.l2
-rw-r--r--tools/perf/util/parse-options.c32
-rw-r--r--tools/perf/util/parse-options.h2
-rw-r--r--tools/perf/util/probe-event.c373
-rw-r--r--tools/perf/util/probe-event.h6
-rw-r--r--tools/perf/util/probe-finder.c25
-rw-r--r--tools/perf/util/python-ext-sources1
-rw-r--r--tools/perf/util/scripting-engines/Build6
-rw-r--r--tools/perf/util/scripting-engines/trace-event-perl.c10
-rw-r--r--tools/perf/util/scripting-engines/trace-event-python.c21
-rw-r--r--tools/perf/util/session.c320
-rw-r--r--tools/perf/util/session.h18
-rw-r--r--tools/perf/util/setup.py2
-rw-r--r--tools/perf/util/sort.c9
-rw-r--r--tools/perf/util/sort.h2
-rw-r--r--tools/perf/util/symbol-elf.c87
-rw-r--r--tools/perf/util/symbol-minimal.c7
-rw-r--r--tools/perf/util/symbol.c65
-rw-r--r--tools/perf/util/symbol.h8
-rw-r--r--tools/perf/util/target.c7
-rw-r--r--tools/perf/util/thread.c15
-rw-r--r--tools/perf/util/thread.h24
-rw-r--r--tools/perf/util/tool.h8
-rw-r--r--tools/perf/util/trace-event-parse.c12
-rw-r--r--tools/perf/util/trace-event-scripting.c1
-rw-r--r--tools/perf/util/trace-event.h6
-rw-r--r--tools/perf/util/unwind-libunwind.c8
-rw-r--r--tools/perf/util/util.c67
-rw-r--r--tools/perf/util/util.h6
285 files changed, 13237 insertions, 2906 deletions
diff --git a/arch/arm/kernel/hw_breakpoint.c b/arch/arm/kernel/hw_breakpoint.c
index 7fc70ae21185..dc7d0a95bd36 100644
--- a/arch/arm/kernel/hw_breakpoint.c
+++ b/arch/arm/kernel/hw_breakpoint.c
@@ -648,7 +648,7 @@ int arch_validate_hwbkpt_settings(struct perf_event *bp)
648 * Per-cpu breakpoints are not supported by our stepping 648 * Per-cpu breakpoints are not supported by our stepping
649 * mechanism. 649 * mechanism.
650 */ 650 */
651 if (!bp->hw.bp_target) 651 if (!bp->hw.target)
652 return -EINVAL; 652 return -EINVAL;
653 653
654 /* 654 /*
diff --git a/arch/arm64/kernel/hw_breakpoint.c b/arch/arm64/kernel/hw_breakpoint.c
index 98bbe06e469c..e7d934d3afe0 100644
--- a/arch/arm64/kernel/hw_breakpoint.c
+++ b/arch/arm64/kernel/hw_breakpoint.c
@@ -527,7 +527,7 @@ int arch_validate_hwbkpt_settings(struct perf_event *bp)
527 * Disallow per-task kernel breakpoints since these would 527 * Disallow per-task kernel breakpoints since these would
528 * complicate the stepping code. 528 * complicate the stepping code.
529 */ 529 */
530 if (info->ctrl.privilege == AARCH64_BREAKPOINT_EL1 && bp->hw.bp_target) 530 if (info->ctrl.privilege == AARCH64_BREAKPOINT_EL1 && bp->hw.target)
531 return -EINVAL; 531 return -EINVAL;
532 532
533 return 0; 533 return 0;
diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c
index 7c4f6690533a..7fd60dcb2cb0 100644
--- a/arch/powerpc/perf/core-book3s.c
+++ b/arch/powerpc/perf/core-book3s.c
@@ -124,7 +124,7 @@ static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
124 124
125static inline void power_pmu_bhrb_enable(struct perf_event *event) {} 125static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
126static inline void power_pmu_bhrb_disable(struct perf_event *event) {} 126static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
127static void power_pmu_flush_branch_stack(void) {} 127static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
128static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {} 128static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
129static void pmao_restore_workaround(bool ebb) { } 129static void pmao_restore_workaround(bool ebb) { }
130#endif /* CONFIG_PPC32 */ 130#endif /* CONFIG_PPC32 */
@@ -350,6 +350,7 @@ static void power_pmu_bhrb_enable(struct perf_event *event)
350 cpuhw->bhrb_context = event->ctx; 350 cpuhw->bhrb_context = event->ctx;
351 } 351 }
352 cpuhw->bhrb_users++; 352 cpuhw->bhrb_users++;
353 perf_sched_cb_inc(event->ctx->pmu);
353} 354}
354 355
355static void power_pmu_bhrb_disable(struct perf_event *event) 356static void power_pmu_bhrb_disable(struct perf_event *event)
@@ -361,6 +362,7 @@ static void power_pmu_bhrb_disable(struct perf_event *event)
361 362
362 cpuhw->bhrb_users--; 363 cpuhw->bhrb_users--;
363 WARN_ON_ONCE(cpuhw->bhrb_users < 0); 364 WARN_ON_ONCE(cpuhw->bhrb_users < 0);
365 perf_sched_cb_dec(event->ctx->pmu);
364 366
365 if (!cpuhw->disabled && !cpuhw->bhrb_users) { 367 if (!cpuhw->disabled && !cpuhw->bhrb_users) {
366 /* BHRB cannot be turned off when other 368 /* BHRB cannot be turned off when other
@@ -375,9 +377,12 @@ static void power_pmu_bhrb_disable(struct perf_event *event)
375/* Called from ctxsw to prevent one process's branch entries to 377/* Called from ctxsw to prevent one process's branch entries to
376 * mingle with the other process's entries during context switch. 378 * mingle with the other process's entries during context switch.
377 */ 379 */
378static void power_pmu_flush_branch_stack(void) 380static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
379{ 381{
380 if (ppmu->bhrb_nr) 382 if (!ppmu->bhrb_nr)
383 return;
384
385 if (sched_in)
381 power_pmu_bhrb_reset(); 386 power_pmu_bhrb_reset();
382} 387}
383/* Calculate the to address for a branch */ 388/* Calculate the to address for a branch */
@@ -1901,7 +1906,7 @@ static struct pmu power_pmu = {
1901 .cancel_txn = power_pmu_cancel_txn, 1906 .cancel_txn = power_pmu_cancel_txn,
1902 .commit_txn = power_pmu_commit_txn, 1907 .commit_txn = power_pmu_commit_txn,
1903 .event_idx = power_pmu_event_idx, 1908 .event_idx = power_pmu_event_idx,
1904 .flush_branch_stack = power_pmu_flush_branch_stack, 1909 .sched_task = power_pmu_sched_task,
1905}; 1910};
1906 1911
1907/* 1912/*
diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index 854c04b3c9c2..7ee9b94d9921 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -12,7 +12,7 @@
12#include <asm/disabled-features.h> 12#include <asm/disabled-features.h>
13#endif 13#endif
14 14
15#define NCAPINTS 11 /* N 32-bit words worth of info */ 15#define NCAPINTS 13 /* N 32-bit words worth of info */
16#define NBUGINTS 1 /* N 32-bit bug flags */ 16#define NBUGINTS 1 /* N 32-bit bug flags */
17 17
18/* 18/*
@@ -195,6 +195,7 @@
195#define X86_FEATURE_HWP_ACT_WINDOW ( 7*32+ 12) /* Intel HWP_ACT_WINDOW */ 195#define X86_FEATURE_HWP_ACT_WINDOW ( 7*32+ 12) /* Intel HWP_ACT_WINDOW */
196#define X86_FEATURE_HWP_EPP ( 7*32+13) /* Intel HWP_EPP */ 196#define X86_FEATURE_HWP_EPP ( 7*32+13) /* Intel HWP_EPP */
197#define X86_FEATURE_HWP_PKG_REQ ( 7*32+14) /* Intel HWP_PKG_REQ */ 197#define X86_FEATURE_HWP_PKG_REQ ( 7*32+14) /* Intel HWP_PKG_REQ */
198#define X86_FEATURE_INTEL_PT ( 7*32+15) /* Intel Processor Trace */
198 199
199/* Virtualization flags: Linux defined, word 8 */ 200/* Virtualization flags: Linux defined, word 8 */
200#define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */ 201#define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
@@ -226,6 +227,7 @@
226#define X86_FEATURE_ERMS ( 9*32+ 9) /* Enhanced REP MOVSB/STOSB */ 227#define X86_FEATURE_ERMS ( 9*32+ 9) /* Enhanced REP MOVSB/STOSB */
227#define X86_FEATURE_INVPCID ( 9*32+10) /* Invalidate Processor Context ID */ 228#define X86_FEATURE_INVPCID ( 9*32+10) /* Invalidate Processor Context ID */
228#define X86_FEATURE_RTM ( 9*32+11) /* Restricted Transactional Memory */ 229#define X86_FEATURE_RTM ( 9*32+11) /* Restricted Transactional Memory */
230#define X86_FEATURE_CQM ( 9*32+12) /* Cache QoS Monitoring */
229#define X86_FEATURE_MPX ( 9*32+14) /* Memory Protection Extension */ 231#define X86_FEATURE_MPX ( 9*32+14) /* Memory Protection Extension */
230#define X86_FEATURE_AVX512F ( 9*32+16) /* AVX-512 Foundation */ 232#define X86_FEATURE_AVX512F ( 9*32+16) /* AVX-512 Foundation */
231#define X86_FEATURE_RDSEED ( 9*32+18) /* The RDSEED instruction */ 233#define X86_FEATURE_RDSEED ( 9*32+18) /* The RDSEED instruction */
@@ -244,6 +246,12 @@
244#define X86_FEATURE_XGETBV1 (10*32+ 2) /* XGETBV with ECX = 1 */ 246#define X86_FEATURE_XGETBV1 (10*32+ 2) /* XGETBV with ECX = 1 */
245#define X86_FEATURE_XSAVES (10*32+ 3) /* XSAVES/XRSTORS */ 247#define X86_FEATURE_XSAVES (10*32+ 3) /* XSAVES/XRSTORS */
246 248
249/* Intel-defined CPU QoS Sub-leaf, CPUID level 0x0000000F:0 (edx), word 11 */
250#define X86_FEATURE_CQM_LLC (11*32+ 1) /* LLC QoS if 1 */
251
252/* Intel-defined CPU QoS Sub-leaf, CPUID level 0x0000000F:1 (edx), word 12 */
253#define X86_FEATURE_CQM_OCCUP_LLC (12*32+ 0) /* LLC occupancy monitoring if 1 */
254
247/* 255/*
248 * BUG word(s) 256 * BUG word(s)
249 */ 257 */
diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index d2203b5d9538..23ba6765b718 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -109,6 +109,9 @@ struct cpuinfo_x86 {
109 /* in KB - valid for CPUS which support this call: */ 109 /* in KB - valid for CPUS which support this call: */
110 int x86_cache_size; 110 int x86_cache_size;
111 int x86_cache_alignment; /* In bytes */ 111 int x86_cache_alignment; /* In bytes */
112 /* Cache QoS architectural values: */
113 int x86_cache_max_rmid; /* max index */
114 int x86_cache_occ_scale; /* scale to bytes */
112 int x86_power; 115 int x86_power;
113 unsigned long loops_per_jiffy; 116 unsigned long loops_per_jiffy;
114 /* cpuid returned max cores value: */ 117 /* cpuid returned max cores value: */
diff --git a/arch/x86/include/uapi/asm/msr-index.h b/arch/x86/include/uapi/asm/msr-index.h
index 3ce079136c11..1a4eae695ca8 100644
--- a/arch/x86/include/uapi/asm/msr-index.h
+++ b/arch/x86/include/uapi/asm/msr-index.h
@@ -74,6 +74,24 @@
74#define MSR_IA32_PERF_CAPABILITIES 0x00000345 74#define MSR_IA32_PERF_CAPABILITIES 0x00000345
75#define MSR_PEBS_LD_LAT_THRESHOLD 0x000003f6 75#define MSR_PEBS_LD_LAT_THRESHOLD 0x000003f6
76 76
77#define MSR_IA32_RTIT_CTL 0x00000570
78#define RTIT_CTL_TRACEEN BIT(0)
79#define RTIT_CTL_OS BIT(2)
80#define RTIT_CTL_USR BIT(3)
81#define RTIT_CTL_CR3EN BIT(7)
82#define RTIT_CTL_TOPA BIT(8)
83#define RTIT_CTL_TSC_EN BIT(10)
84#define RTIT_CTL_DISRETC BIT(11)
85#define RTIT_CTL_BRANCH_EN BIT(13)
86#define MSR_IA32_RTIT_STATUS 0x00000571
87#define RTIT_STATUS_CONTEXTEN BIT(1)
88#define RTIT_STATUS_TRIGGEREN BIT(2)
89#define RTIT_STATUS_ERROR BIT(4)
90#define RTIT_STATUS_STOPPED BIT(5)
91#define MSR_IA32_RTIT_CR3_MATCH 0x00000572
92#define MSR_IA32_RTIT_OUTPUT_BASE 0x00000560
93#define MSR_IA32_RTIT_OUTPUT_MASK 0x00000561
94
77#define MSR_MTRRfix64K_00000 0x00000250 95#define MSR_MTRRfix64K_00000 0x00000250
78#define MSR_MTRRfix16K_80000 0x00000258 96#define MSR_MTRRfix16K_80000 0x00000258
79#define MSR_MTRRfix16K_A0000 0x00000259 97#define MSR_MTRRfix16K_A0000 0x00000259
diff --git a/arch/x86/kernel/cpu/Makefile b/arch/x86/kernel/cpu/Makefile
index 80091ae54c2b..9bff68798836 100644
--- a/arch/x86/kernel/cpu/Makefile
+++ b/arch/x86/kernel/cpu/Makefile
@@ -39,7 +39,8 @@ obj-$(CONFIG_CPU_SUP_AMD) += perf_event_amd_iommu.o
39endif 39endif
40obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_p6.o perf_event_knc.o perf_event_p4.o 40obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_p6.o perf_event_knc.o perf_event_p4.o
41obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o 41obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o
42obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_rapl.o 42obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_rapl.o perf_event_intel_cqm.o
43obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_pt.o perf_event_intel_bts.o
43 44
44obj-$(CONFIG_PERF_EVENTS_INTEL_UNCORE) += perf_event_intel_uncore.o \ 45obj-$(CONFIG_PERF_EVENTS_INTEL_UNCORE) += perf_event_intel_uncore.o \
45 perf_event_intel_uncore_snb.o \ 46 perf_event_intel_uncore_snb.o \
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 3f70538012e2..a62cf04dac8a 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -646,6 +646,30 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
646 c->x86_capability[10] = eax; 646 c->x86_capability[10] = eax;
647 } 647 }
648 648
649 /* Additional Intel-defined flags: level 0x0000000F */
650 if (c->cpuid_level >= 0x0000000F) {
651 u32 eax, ebx, ecx, edx;
652
653 /* QoS sub-leaf, EAX=0Fh, ECX=0 */
654 cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx);
655 c->x86_capability[11] = edx;
656 if (cpu_has(c, X86_FEATURE_CQM_LLC)) {
657 /* will be overridden if occupancy monitoring exists */
658 c->x86_cache_max_rmid = ebx;
659
660 /* QoS sub-leaf, EAX=0Fh, ECX=1 */
661 cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx);
662 c->x86_capability[12] = edx;
663 if (cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) {
664 c->x86_cache_max_rmid = ecx;
665 c->x86_cache_occ_scale = ebx;
666 }
667 } else {
668 c->x86_cache_max_rmid = -1;
669 c->x86_cache_occ_scale = -1;
670 }
671 }
672
649 /* AMD-defined flags: level 0x80000001 */ 673 /* AMD-defined flags: level 0x80000001 */
650 xlvl = cpuid_eax(0x80000000); 674 xlvl = cpuid_eax(0x80000000);
651 c->extended_cpuid_level = xlvl; 675 c->extended_cpuid_level = xlvl;
@@ -834,6 +858,20 @@ static void generic_identify(struct cpuinfo_x86 *c)
834 detect_nopl(c); 858 detect_nopl(c);
835} 859}
836 860
861static void x86_init_cache_qos(struct cpuinfo_x86 *c)
862{
863 /*
864 * The heavy lifting of max_rmid and cache_occ_scale are handled
865 * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu
866 * in case CQM bits really aren't there in this CPU.
867 */
868 if (c != &boot_cpu_data) {
869 boot_cpu_data.x86_cache_max_rmid =
870 min(boot_cpu_data.x86_cache_max_rmid,
871 c->x86_cache_max_rmid);
872 }
873}
874
837/* 875/*
838 * This does the hard work of actually picking apart the CPU stuff... 876 * This does the hard work of actually picking apart the CPU stuff...
839 */ 877 */
@@ -923,6 +961,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
923 961
924 init_hypervisor(c); 962 init_hypervisor(c);
925 x86_init_rdrand(c); 963 x86_init_rdrand(c);
964 x86_init_cache_qos(c);
926 965
927 /* 966 /*
928 * Clear/Set all flags overriden by options, need do it 967 * Clear/Set all flags overriden by options, need do it
diff --git a/arch/x86/kernel/cpu/intel_pt.h b/arch/x86/kernel/cpu/intel_pt.h
new file mode 100644
index 000000000000..1c338b0eba05
--- /dev/null
+++ b/arch/x86/kernel/cpu/intel_pt.h
@@ -0,0 +1,131 @@
1/*
2 * Intel(R) Processor Trace PMU driver for perf
3 * Copyright (c) 2013-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * Intel PT is specified in the Intel Architecture Instruction Set Extensions
15 * Programming Reference:
16 * http://software.intel.com/en-us/intel-isa-extensions
17 */
18
19#ifndef __INTEL_PT_H__
20#define __INTEL_PT_H__
21
22/*
23 * Single-entry ToPA: when this close to region boundary, switch
24 * buffers to avoid losing data.
25 */
26#define TOPA_PMI_MARGIN 512
27
28/*
29 * Table of Physical Addresses bits
30 */
31enum topa_sz {
32 TOPA_4K = 0,
33 TOPA_8K,
34 TOPA_16K,
35 TOPA_32K,
36 TOPA_64K,
37 TOPA_128K,
38 TOPA_256K,
39 TOPA_512K,
40 TOPA_1MB,
41 TOPA_2MB,
42 TOPA_4MB,
43 TOPA_8MB,
44 TOPA_16MB,
45 TOPA_32MB,
46 TOPA_64MB,
47 TOPA_128MB,
48 TOPA_SZ_END,
49};
50
51static inline unsigned int sizes(enum topa_sz tsz)
52{
53 return 1 << (tsz + 12);
54};
55
56struct topa_entry {
57 u64 end : 1;
58 u64 rsvd0 : 1;
59 u64 intr : 1;
60 u64 rsvd1 : 1;
61 u64 stop : 1;
62 u64 rsvd2 : 1;
63 u64 size : 4;
64 u64 rsvd3 : 2;
65 u64 base : 36;
66 u64 rsvd4 : 16;
67};
68
69#define TOPA_SHIFT 12
70#define PT_CPUID_LEAVES 2
71
72enum pt_capabilities {
73 PT_CAP_max_subleaf = 0,
74 PT_CAP_cr3_filtering,
75 PT_CAP_topa_output,
76 PT_CAP_topa_multiple_entries,
77 PT_CAP_payloads_lip,
78};
79
80struct pt_pmu {
81 struct pmu pmu;
82 u32 caps[4 * PT_CPUID_LEAVES];
83};
84
85/**
86 * struct pt_buffer - buffer configuration; one buffer per task_struct or
87 * cpu, depending on perf event configuration
88 * @cpu: cpu for per-cpu allocation
89 * @tables: list of ToPA tables in this buffer
90 * @first: shorthand for first topa table
91 * @last: shorthand for last topa table
92 * @cur: current topa table
93 * @nr_pages: buffer size in pages
94 * @cur_idx: current output region's index within @cur table
95 * @output_off: offset within the current output region
96 * @data_size: running total of the amount of data in this buffer
97 * @lost: if data was lost/truncated
98 * @head: logical write offset inside the buffer
99 * @snapshot: if this is for a snapshot/overwrite counter
100 * @stop_pos: STOP topa entry in the buffer
101 * @intr_pos: INT topa entry in the buffer
102 * @data_pages: array of pages from perf
103 * @topa_index: table of topa entries indexed by page offset
104 */
105struct pt_buffer {
106 int cpu;
107 struct list_head tables;
108 struct topa *first, *last, *cur;
109 unsigned int cur_idx;
110 size_t output_off;
111 unsigned long nr_pages;
112 local_t data_size;
113 local_t lost;
114 local64_t head;
115 bool snapshot;
116 unsigned long stop_pos, intr_pos;
117 void **data_pages;
118 struct topa_entry *topa_index[0];
119};
120
121/**
122 * struct pt - per-cpu pt context
123 * @handle: perf output handle
124 * @handle_nmi: do handle PT PMI on this cpu, there's an active event
125 */
126struct pt {
127 struct perf_output_handle handle;
128 int handle_nmi;
129};
130
131#endif /* __INTEL_PT_H__ */
diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c
index e2888a3ad1e3..87848ebe2bb7 100644
--- a/arch/x86/kernel/cpu/perf_event.c
+++ b/arch/x86/kernel/cpu/perf_event.c
@@ -263,6 +263,14 @@ static void hw_perf_event_destroy(struct perf_event *event)
263 } 263 }
264} 264}
265 265
266void hw_perf_lbr_event_destroy(struct perf_event *event)
267{
268 hw_perf_event_destroy(event);
269
270 /* undo the lbr/bts event accounting */
271 x86_del_exclusive(x86_lbr_exclusive_lbr);
272}
273
266static inline int x86_pmu_initialized(void) 274static inline int x86_pmu_initialized(void)
267{ 275{
268 return x86_pmu.handle_irq != NULL; 276 return x86_pmu.handle_irq != NULL;
@@ -302,6 +310,35 @@ set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
302 return x86_pmu_extra_regs(val, event); 310 return x86_pmu_extra_regs(val, event);
303} 311}
304 312
313/*
314 * Check if we can create event of a certain type (that no conflicting events
315 * are present).
316 */
317int x86_add_exclusive(unsigned int what)
318{
319 int ret = -EBUSY, i;
320
321 if (atomic_inc_not_zero(&x86_pmu.lbr_exclusive[what]))
322 return 0;
323
324 mutex_lock(&pmc_reserve_mutex);
325 for (i = 0; i < ARRAY_SIZE(x86_pmu.lbr_exclusive); i++)
326 if (i != what && atomic_read(&x86_pmu.lbr_exclusive[i]))
327 goto out;
328
329 atomic_inc(&x86_pmu.lbr_exclusive[what]);
330 ret = 0;
331
332out:
333 mutex_unlock(&pmc_reserve_mutex);
334 return ret;
335}
336
337void x86_del_exclusive(unsigned int what)
338{
339 atomic_dec(&x86_pmu.lbr_exclusive[what]);
340}
341
305int x86_setup_perfctr(struct perf_event *event) 342int x86_setup_perfctr(struct perf_event *event)
306{ 343{
307 struct perf_event_attr *attr = &event->attr; 344 struct perf_event_attr *attr = &event->attr;
@@ -346,6 +383,12 @@ int x86_setup_perfctr(struct perf_event *event)
346 /* BTS is currently only allowed for user-mode. */ 383 /* BTS is currently only allowed for user-mode. */
347 if (!attr->exclude_kernel) 384 if (!attr->exclude_kernel)
348 return -EOPNOTSUPP; 385 return -EOPNOTSUPP;
386
387 /* disallow bts if conflicting events are present */
388 if (x86_add_exclusive(x86_lbr_exclusive_lbr))
389 return -EBUSY;
390
391 event->destroy = hw_perf_lbr_event_destroy;
349 } 392 }
350 393
351 hwc->config |= config; 394 hwc->config |= config;
@@ -399,39 +442,41 @@ int x86_pmu_hw_config(struct perf_event *event)
399 442
400 if (event->attr.precise_ip > precise) 443 if (event->attr.precise_ip > precise)
401 return -EOPNOTSUPP; 444 return -EOPNOTSUPP;
402 /* 445 }
403 * check that PEBS LBR correction does not conflict with 446 /*
404 * whatever the user is asking with attr->branch_sample_type 447 * check that PEBS LBR correction does not conflict with
405 */ 448 * whatever the user is asking with attr->branch_sample_type
406 if (event->attr.precise_ip > 1 && 449 */
407 x86_pmu.intel_cap.pebs_format < 2) { 450 if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format < 2) {
408 u64 *br_type = &event->attr.branch_sample_type; 451 u64 *br_type = &event->attr.branch_sample_type;
409 452
410 if (has_branch_stack(event)) { 453 if (has_branch_stack(event)) {
411 if (!precise_br_compat(event)) 454 if (!precise_br_compat(event))
412 return -EOPNOTSUPP; 455 return -EOPNOTSUPP;
413 456
414 /* branch_sample_type is compatible */ 457 /* branch_sample_type is compatible */
415 458
416 } else { 459 } else {
417 /* 460 /*
418 * user did not specify branch_sample_type 461 * user did not specify branch_sample_type
419 * 462 *
420 * For PEBS fixups, we capture all 463 * For PEBS fixups, we capture all
421 * the branches at the priv level of the 464 * the branches at the priv level of the
422 * event. 465 * event.
423 */ 466 */
424 *br_type = PERF_SAMPLE_BRANCH_ANY; 467 *br_type = PERF_SAMPLE_BRANCH_ANY;
425 468
426 if (!event->attr.exclude_user) 469 if (!event->attr.exclude_user)
427 *br_type |= PERF_SAMPLE_BRANCH_USER; 470 *br_type |= PERF_SAMPLE_BRANCH_USER;
428 471
429 if (!event->attr.exclude_kernel) 472 if (!event->attr.exclude_kernel)
430 *br_type |= PERF_SAMPLE_BRANCH_KERNEL; 473 *br_type |= PERF_SAMPLE_BRANCH_KERNEL;
431 }
432 } 474 }
433 } 475 }
434 476
477 if (event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK)
478 event->attach_state |= PERF_ATTACH_TASK_DATA;
479
435 /* 480 /*
436 * Generate PMC IRQs: 481 * Generate PMC IRQs:
437 * (keep 'enabled' bit clear for now) 482 * (keep 'enabled' bit clear for now)
@@ -449,6 +494,12 @@ int x86_pmu_hw_config(struct perf_event *event)
449 if (event->attr.type == PERF_TYPE_RAW) 494 if (event->attr.type == PERF_TYPE_RAW)
450 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK; 495 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
451 496
497 if (event->attr.sample_period && x86_pmu.limit_period) {
498 if (x86_pmu.limit_period(event, event->attr.sample_period) >
499 event->attr.sample_period)
500 return -EINVAL;
501 }
502
452 return x86_setup_perfctr(event); 503 return x86_setup_perfctr(event);
453} 504}
454 505
@@ -728,14 +779,17 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
728 struct event_constraint *c; 779 struct event_constraint *c;
729 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; 780 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
730 struct perf_event *e; 781 struct perf_event *e;
731 int i, wmin, wmax, num = 0; 782 int i, wmin, wmax, unsched = 0;
732 struct hw_perf_event *hwc; 783 struct hw_perf_event *hwc;
733 784
734 bitmap_zero(used_mask, X86_PMC_IDX_MAX); 785 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
735 786
787 if (x86_pmu.start_scheduling)
788 x86_pmu.start_scheduling(cpuc);
789
736 for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) { 790 for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) {
737 hwc = &cpuc->event_list[i]->hw; 791 hwc = &cpuc->event_list[i]->hw;
738 c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); 792 c = x86_pmu.get_event_constraints(cpuc, i, cpuc->event_list[i]);
739 hwc->constraint = c; 793 hwc->constraint = c;
740 794
741 wmin = min(wmin, c->weight); 795 wmin = min(wmin, c->weight);
@@ -768,24 +822,30 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
768 822
769 /* slow path */ 823 /* slow path */
770 if (i != n) 824 if (i != n)
771 num = perf_assign_events(cpuc->event_list, n, wmin, 825 unsched = perf_assign_events(cpuc->event_list, n, wmin,
772 wmax, assign); 826 wmax, assign);
773 827
774 /* 828 /*
775 * Mark the event as committed, so we do not put_constraint() 829 * In case of success (unsched = 0), mark events as committed,
776 * in case new events are added and fail scheduling. 830 * so we do not put_constraint() in case new events are added
831 * and fail to be scheduled
832 *
833 * We invoke the lower level commit callback to lock the resource
834 *
835 * We do not need to do all of this in case we are called to
836 * validate an event group (assign == NULL)
777 */ 837 */
778 if (!num && assign) { 838 if (!unsched && assign) {
779 for (i = 0; i < n; i++) { 839 for (i = 0; i < n; i++) {
780 e = cpuc->event_list[i]; 840 e = cpuc->event_list[i];
781 e->hw.flags |= PERF_X86_EVENT_COMMITTED; 841 e->hw.flags |= PERF_X86_EVENT_COMMITTED;
842 if (x86_pmu.commit_scheduling)
843 x86_pmu.commit_scheduling(cpuc, e, assign[i]);
782 } 844 }
783 } 845 }
784 /* 846
785 * scheduling failed or is just a simulation, 847 if (!assign || unsched) {
786 * free resources if necessary 848
787 */
788 if (!assign || num) {
789 for (i = 0; i < n; i++) { 849 for (i = 0; i < n; i++) {
790 e = cpuc->event_list[i]; 850 e = cpuc->event_list[i];
791 /* 851 /*
@@ -795,11 +855,18 @@ int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
795 if ((e->hw.flags & PERF_X86_EVENT_COMMITTED)) 855 if ((e->hw.flags & PERF_X86_EVENT_COMMITTED))
796 continue; 856 continue;
797 857
858 /*
859 * release events that failed scheduling
860 */
798 if (x86_pmu.put_event_constraints) 861 if (x86_pmu.put_event_constraints)
799 x86_pmu.put_event_constraints(cpuc, e); 862 x86_pmu.put_event_constraints(cpuc, e);
800 } 863 }
801 } 864 }
802 return num ? -EINVAL : 0; 865
866 if (x86_pmu.stop_scheduling)
867 x86_pmu.stop_scheduling(cpuc);
868
869 return unsched ? -EINVAL : 0;
803} 870}
804 871
805/* 872/*
@@ -986,6 +1053,9 @@ int x86_perf_event_set_period(struct perf_event *event)
986 if (left > x86_pmu.max_period) 1053 if (left > x86_pmu.max_period)
987 left = x86_pmu.max_period; 1054 left = x86_pmu.max_period;
988 1055
1056 if (x86_pmu.limit_period)
1057 left = x86_pmu.limit_period(event, left);
1058
989 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; 1059 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
990 1060
991 /* 1061 /*
@@ -1033,7 +1103,6 @@ static int x86_pmu_add(struct perf_event *event, int flags)
1033 1103
1034 hwc = &event->hw; 1104 hwc = &event->hw;
1035 1105
1036 perf_pmu_disable(event->pmu);
1037 n0 = cpuc->n_events; 1106 n0 = cpuc->n_events;
1038 ret = n = collect_events(cpuc, event, false); 1107 ret = n = collect_events(cpuc, event, false);
1039 if (ret < 0) 1108 if (ret < 0)
@@ -1071,7 +1140,6 @@ done_collect:
1071 1140
1072 ret = 0; 1141 ret = 0;
1073out: 1142out:
1074 perf_pmu_enable(event->pmu);
1075 return ret; 1143 return ret;
1076} 1144}
1077 1145
@@ -1103,7 +1171,7 @@ static void x86_pmu_start(struct perf_event *event, int flags)
1103void perf_event_print_debug(void) 1171void perf_event_print_debug(void)
1104{ 1172{
1105 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; 1173 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
1106 u64 pebs; 1174 u64 pebs, debugctl;
1107 struct cpu_hw_events *cpuc; 1175 struct cpu_hw_events *cpuc;
1108 unsigned long flags; 1176 unsigned long flags;
1109 int cpu, idx; 1177 int cpu, idx;
@@ -1121,14 +1189,20 @@ void perf_event_print_debug(void)
1121 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); 1189 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1122 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); 1190 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1123 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); 1191 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
1124 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
1125 1192
1126 pr_info("\n"); 1193 pr_info("\n");
1127 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); 1194 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1128 pr_info("CPU#%d: status: %016llx\n", cpu, status); 1195 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1129 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); 1196 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1130 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); 1197 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
1131 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); 1198 if (x86_pmu.pebs_constraints) {
1199 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
1200 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
1201 }
1202 if (x86_pmu.lbr_nr) {
1203 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
1204 pr_info("CPU#%d: debugctl: %016llx\n", cpu, debugctl);
1205 }
1132 } 1206 }
1133 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); 1207 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
1134 1208
@@ -1321,11 +1395,12 @@ x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1321{ 1395{
1322 unsigned int cpu = (long)hcpu; 1396 unsigned int cpu = (long)hcpu;
1323 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); 1397 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
1324 int ret = NOTIFY_OK; 1398 int i, ret = NOTIFY_OK;
1325 1399
1326 switch (action & ~CPU_TASKS_FROZEN) { 1400 switch (action & ~CPU_TASKS_FROZEN) {
1327 case CPU_UP_PREPARE: 1401 case CPU_UP_PREPARE:
1328 cpuc->kfree_on_online = NULL; 1402 for (i = 0 ; i < X86_PERF_KFREE_MAX; i++)
1403 cpuc->kfree_on_online[i] = NULL;
1329 if (x86_pmu.cpu_prepare) 1404 if (x86_pmu.cpu_prepare)
1330 ret = x86_pmu.cpu_prepare(cpu); 1405 ret = x86_pmu.cpu_prepare(cpu);
1331 break; 1406 break;
@@ -1336,7 +1411,10 @@ x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1336 break; 1411 break;
1337 1412
1338 case CPU_ONLINE: 1413 case CPU_ONLINE:
1339 kfree(cpuc->kfree_on_online); 1414 for (i = 0 ; i < X86_PERF_KFREE_MAX; i++) {
1415 kfree(cpuc->kfree_on_online[i]);
1416 cpuc->kfree_on_online[i] = NULL;
1417 }
1340 break; 1418 break;
1341 1419
1342 case CPU_DYING: 1420 case CPU_DYING:
@@ -1712,7 +1790,7 @@ static int validate_event(struct perf_event *event)
1712 if (IS_ERR(fake_cpuc)) 1790 if (IS_ERR(fake_cpuc))
1713 return PTR_ERR(fake_cpuc); 1791 return PTR_ERR(fake_cpuc);
1714 1792
1715 c = x86_pmu.get_event_constraints(fake_cpuc, event); 1793 c = x86_pmu.get_event_constraints(fake_cpuc, -1, event);
1716 1794
1717 if (!c || !c->weight) 1795 if (!c || !c->weight)
1718 ret = -EINVAL; 1796 ret = -EINVAL;
@@ -1914,10 +1992,10 @@ static const struct attribute_group *x86_pmu_attr_groups[] = {
1914 NULL, 1992 NULL,
1915}; 1993};
1916 1994
1917static void x86_pmu_flush_branch_stack(void) 1995static void x86_pmu_sched_task(struct perf_event_context *ctx, bool sched_in)
1918{ 1996{
1919 if (x86_pmu.flush_branch_stack) 1997 if (x86_pmu.sched_task)
1920 x86_pmu.flush_branch_stack(); 1998 x86_pmu.sched_task(ctx, sched_in);
1921} 1999}
1922 2000
1923void perf_check_microcode(void) 2001void perf_check_microcode(void)
@@ -1949,7 +2027,8 @@ static struct pmu pmu = {
1949 .commit_txn = x86_pmu_commit_txn, 2027 .commit_txn = x86_pmu_commit_txn,
1950 2028
1951 .event_idx = x86_pmu_event_idx, 2029 .event_idx = x86_pmu_event_idx,
1952 .flush_branch_stack = x86_pmu_flush_branch_stack, 2030 .sched_task = x86_pmu_sched_task,
2031 .task_ctx_size = sizeof(struct x86_perf_task_context),
1953}; 2032};
1954 2033
1955void arch_perf_update_userpage(struct perf_event *event, 2034void arch_perf_update_userpage(struct perf_event *event,
@@ -1968,13 +2047,23 @@ void arch_perf_update_userpage(struct perf_event *event,
1968 2047
1969 data = cyc2ns_read_begin(); 2048 data = cyc2ns_read_begin();
1970 2049
2050 /*
2051 * Internal timekeeping for enabled/running/stopped times
2052 * is always in the local_clock domain.
2053 */
1971 userpg->cap_user_time = 1; 2054 userpg->cap_user_time = 1;
1972 userpg->time_mult = data->cyc2ns_mul; 2055 userpg->time_mult = data->cyc2ns_mul;
1973 userpg->time_shift = data->cyc2ns_shift; 2056 userpg->time_shift = data->cyc2ns_shift;
1974 userpg->time_offset = data->cyc2ns_offset - now; 2057 userpg->time_offset = data->cyc2ns_offset - now;
1975 2058
1976 userpg->cap_user_time_zero = 1; 2059 /*
1977 userpg->time_zero = data->cyc2ns_offset; 2060 * cap_user_time_zero doesn't make sense when we're using a different
2061 * time base for the records.
2062 */
2063 if (event->clock == &local_clock) {
2064 userpg->cap_user_time_zero = 1;
2065 userpg->time_zero = data->cyc2ns_offset;
2066 }
1978 2067
1979 cyc2ns_read_end(data); 2068 cyc2ns_read_end(data);
1980} 2069}
diff --git a/arch/x86/kernel/cpu/perf_event.h b/arch/x86/kernel/cpu/perf_event.h
index df525d2be1e8..329f0356ad4a 100644
--- a/arch/x86/kernel/cpu/perf_event.h
+++ b/arch/x86/kernel/cpu/perf_event.h
@@ -71,6 +71,8 @@ struct event_constraint {
71#define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */ 71#define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */
72#define PERF_X86_EVENT_PEBS_LD_HSW 0x10 /* haswell style datala, load */ 72#define PERF_X86_EVENT_PEBS_LD_HSW 0x10 /* haswell style datala, load */
73#define PERF_X86_EVENT_PEBS_NA_HSW 0x20 /* haswell style datala, unknown */ 73#define PERF_X86_EVENT_PEBS_NA_HSW 0x20 /* haswell style datala, unknown */
74#define PERF_X86_EVENT_EXCL 0x40 /* HT exclusivity on counter */
75#define PERF_X86_EVENT_DYNAMIC 0x80 /* dynamic alloc'd constraint */
74#define PERF_X86_EVENT_RDPMC_ALLOWED 0x40 /* grant rdpmc permission */ 76#define PERF_X86_EVENT_RDPMC_ALLOWED 0x40 /* grant rdpmc permission */
75 77
76 78
@@ -123,8 +125,37 @@ struct intel_shared_regs {
123 unsigned core_id; /* per-core: core id */ 125 unsigned core_id; /* per-core: core id */
124}; 126};
125 127
128enum intel_excl_state_type {
129 INTEL_EXCL_UNUSED = 0, /* counter is unused */
130 INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */
131 INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */
132};
133
134struct intel_excl_states {
135 enum intel_excl_state_type init_state[X86_PMC_IDX_MAX];
136 enum intel_excl_state_type state[X86_PMC_IDX_MAX];
137 int num_alloc_cntrs;/* #counters allocated */
138 int max_alloc_cntrs;/* max #counters allowed */
139 bool sched_started; /* true if scheduling has started */
140};
141
142struct intel_excl_cntrs {
143 raw_spinlock_t lock;
144
145 struct intel_excl_states states[2];
146
147 int refcnt; /* per-core: #HT threads */
148 unsigned core_id; /* per-core: core id */
149};
150
126#define MAX_LBR_ENTRIES 16 151#define MAX_LBR_ENTRIES 16
127 152
153enum {
154 X86_PERF_KFREE_SHARED = 0,
155 X86_PERF_KFREE_EXCL = 1,
156 X86_PERF_KFREE_MAX
157};
158
128struct cpu_hw_events { 159struct cpu_hw_events {
129 /* 160 /*
130 * Generic x86 PMC bits 161 * Generic x86 PMC bits
@@ -179,6 +210,12 @@ struct cpu_hw_events {
179 * used on Intel NHM/WSM/SNB 210 * used on Intel NHM/WSM/SNB
180 */ 211 */
181 struct intel_shared_regs *shared_regs; 212 struct intel_shared_regs *shared_regs;
213 /*
214 * manage exclusive counter access between hyperthread
215 */
216 struct event_constraint *constraint_list; /* in enable order */
217 struct intel_excl_cntrs *excl_cntrs;
218 int excl_thread_id; /* 0 or 1 */
182 219
183 /* 220 /*
184 * AMD specific bits 221 * AMD specific bits
@@ -187,7 +224,7 @@ struct cpu_hw_events {
187 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */ 224 /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
188 u64 perf_ctr_virt_mask; 225 u64 perf_ctr_virt_mask;
189 226
190 void *kfree_on_online; 227 void *kfree_on_online[X86_PERF_KFREE_MAX];
191}; 228};
192 229
193#define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\ 230#define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\
@@ -202,6 +239,10 @@ struct cpu_hw_events {
202#define EVENT_CONSTRAINT(c, n, m) \ 239#define EVENT_CONSTRAINT(c, n, m) \
203 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0) 240 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0)
204 241
242#define INTEL_EXCLEVT_CONSTRAINT(c, n) \
243 __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\
244 0, PERF_X86_EVENT_EXCL)
245
205/* 246/*
206 * The overlap flag marks event constraints with overlapping counter 247 * The overlap flag marks event constraints with overlapping counter
207 * masks. This is the case if the counter mask of such an event is not 248 * masks. This is the case if the counter mask of such an event is not
@@ -259,6 +300,10 @@ struct cpu_hw_events {
259#define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \ 300#define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \
260 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS) 301 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS)
261 302
303#define INTEL_EXCLUEVT_CONSTRAINT(c, n) \
304 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \
305 HWEIGHT(n), 0, PERF_X86_EVENT_EXCL)
306
262#define INTEL_PLD_CONSTRAINT(c, n) \ 307#define INTEL_PLD_CONSTRAINT(c, n) \
263 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 308 __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
264 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT) 309 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT)
@@ -283,22 +328,40 @@ struct cpu_hw_events {
283 328
284/* Check flags and event code, and set the HSW load flag */ 329/* Check flags and event code, and set the HSW load flag */
285#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \ 330#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \
286 __EVENT_CONSTRAINT(code, n, \ 331 __EVENT_CONSTRAINT(code, n, \
287 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \ 332 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
288 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW) 333 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
289 334
335#define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(code, n) \
336 __EVENT_CONSTRAINT(code, n, \
337 ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \
338 HWEIGHT(n), 0, \
339 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
340
290/* Check flags and event code/umask, and set the HSW store flag */ 341/* Check flags and event code/umask, and set the HSW store flag */
291#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \ 342#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \
292 __EVENT_CONSTRAINT(code, n, \ 343 __EVENT_CONSTRAINT(code, n, \
293 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 344 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
294 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW) 345 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW)
295 346
347#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(code, n) \
348 __EVENT_CONSTRAINT(code, n, \
349 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
350 HWEIGHT(n), 0, \
351 PERF_X86_EVENT_PEBS_ST_HSW|PERF_X86_EVENT_EXCL)
352
296/* Check flags and event code/umask, and set the HSW load flag */ 353/* Check flags and event code/umask, and set the HSW load flag */
297#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \ 354#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \
298 __EVENT_CONSTRAINT(code, n, \ 355 __EVENT_CONSTRAINT(code, n, \
299 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ 356 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
300 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW) 357 HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW)
301 358
359#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(code, n) \
360 __EVENT_CONSTRAINT(code, n, \
361 INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \
362 HWEIGHT(n), 0, \
363 PERF_X86_EVENT_PEBS_LD_HSW|PERF_X86_EVENT_EXCL)
364
302/* Check flags and event code/umask, and set the HSW N/A flag */ 365/* Check flags and event code/umask, and set the HSW N/A flag */
303#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \ 366#define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \
304 __EVENT_CONSTRAINT(code, n, \ 367 __EVENT_CONSTRAINT(code, n, \
@@ -408,6 +471,13 @@ union x86_pmu_config {
408 471
409#define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value 472#define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value
410 473
474enum {
475 x86_lbr_exclusive_lbr,
476 x86_lbr_exclusive_bts,
477 x86_lbr_exclusive_pt,
478 x86_lbr_exclusive_max,
479};
480
411/* 481/*
412 * struct x86_pmu - generic x86 pmu 482 * struct x86_pmu - generic x86 pmu
413 */ 483 */
@@ -443,14 +513,25 @@ struct x86_pmu {
443 u64 max_period; 513 u64 max_period;
444 struct event_constraint * 514 struct event_constraint *
445 (*get_event_constraints)(struct cpu_hw_events *cpuc, 515 (*get_event_constraints)(struct cpu_hw_events *cpuc,
516 int idx,
446 struct perf_event *event); 517 struct perf_event *event);
447 518
448 void (*put_event_constraints)(struct cpu_hw_events *cpuc, 519 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
449 struct perf_event *event); 520 struct perf_event *event);
521
522 void (*commit_scheduling)(struct cpu_hw_events *cpuc,
523 struct perf_event *event,
524 int cntr);
525
526 void (*start_scheduling)(struct cpu_hw_events *cpuc);
527
528 void (*stop_scheduling)(struct cpu_hw_events *cpuc);
529
450 struct event_constraint *event_constraints; 530 struct event_constraint *event_constraints;
451 struct x86_pmu_quirk *quirks; 531 struct x86_pmu_quirk *quirks;
452 int perfctr_second_write; 532 int perfctr_second_write;
453 bool late_ack; 533 bool late_ack;
534 unsigned (*limit_period)(struct perf_event *event, unsigned l);
454 535
455 /* 536 /*
456 * sysfs attrs 537 * sysfs attrs
@@ -472,7 +553,8 @@ struct x86_pmu {
472 void (*cpu_dead)(int cpu); 553 void (*cpu_dead)(int cpu);
473 554
474 void (*check_microcode)(void); 555 void (*check_microcode)(void);
475 void (*flush_branch_stack)(void); 556 void (*sched_task)(struct perf_event_context *ctx,
557 bool sched_in);
476 558
477 /* 559 /*
478 * Intel Arch Perfmon v2+ 560 * Intel Arch Perfmon v2+
@@ -504,10 +586,15 @@ struct x86_pmu {
504 bool lbr_double_abort; /* duplicated lbr aborts */ 586 bool lbr_double_abort; /* duplicated lbr aborts */
505 587
506 /* 588 /*
589 * Intel PT/LBR/BTS are exclusive
590 */
591 atomic_t lbr_exclusive[x86_lbr_exclusive_max];
592
593 /*
507 * Extra registers for events 594 * Extra registers for events
508 */ 595 */
509 struct extra_reg *extra_regs; 596 struct extra_reg *extra_regs;
510 unsigned int er_flags; 597 unsigned int flags;
511 598
512 /* 599 /*
513 * Intel host/guest support (KVM) 600 * Intel host/guest support (KVM)
@@ -515,6 +602,13 @@ struct x86_pmu {
515 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr); 602 struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
516}; 603};
517 604
605struct x86_perf_task_context {
606 u64 lbr_from[MAX_LBR_ENTRIES];
607 u64 lbr_to[MAX_LBR_ENTRIES];
608 int lbr_callstack_users;
609 int lbr_stack_state;
610};
611
518#define x86_add_quirk(func_) \ 612#define x86_add_quirk(func_) \
519do { \ 613do { \
520 static struct x86_pmu_quirk __quirk __initdata = { \ 614 static struct x86_pmu_quirk __quirk __initdata = { \
@@ -524,8 +618,13 @@ do { \
524 x86_pmu.quirks = &__quirk; \ 618 x86_pmu.quirks = &__quirk; \
525} while (0) 619} while (0)
526 620
527#define ERF_NO_HT_SHARING 1 621/*
528#define ERF_HAS_RSP_1 2 622 * x86_pmu flags
623 */
624#define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */
625#define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */
626#define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */
627#define PMU_FL_EXCL_ENABLED 0x8 /* exclusive counter active */
529 628
530#define EVENT_VAR(_id) event_attr_##_id 629#define EVENT_VAR(_id) event_attr_##_id
531#define EVENT_PTR(_id) &event_attr_##_id.attr.attr 630#define EVENT_PTR(_id) &event_attr_##_id.attr.attr
@@ -546,6 +645,12 @@ static struct perf_pmu_events_attr event_attr_##v = { \
546 645
547extern struct x86_pmu x86_pmu __read_mostly; 646extern struct x86_pmu x86_pmu __read_mostly;
548 647
648static inline bool x86_pmu_has_lbr_callstack(void)
649{
650 return x86_pmu.lbr_sel_map &&
651 x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0;
652}
653
549DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events); 654DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
550 655
551int x86_perf_event_set_period(struct perf_event *event); 656int x86_perf_event_set_period(struct perf_event *event);
@@ -588,6 +693,12 @@ static inline int x86_pmu_rdpmc_index(int index)
588 return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index; 693 return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index;
589} 694}
590 695
696int x86_add_exclusive(unsigned int what);
697
698void x86_del_exclusive(unsigned int what);
699
700void hw_perf_lbr_event_destroy(struct perf_event *event);
701
591int x86_setup_perfctr(struct perf_event *event); 702int x86_setup_perfctr(struct perf_event *event);
592 703
593int x86_pmu_hw_config(struct perf_event *event); 704int x86_pmu_hw_config(struct perf_event *event);
@@ -674,10 +785,34 @@ static inline int amd_pmu_init(void)
674 785
675#ifdef CONFIG_CPU_SUP_INTEL 786#ifdef CONFIG_CPU_SUP_INTEL
676 787
788static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
789{
790 /* user explicitly requested branch sampling */
791 if (has_branch_stack(event))
792 return true;
793
794 /* implicit branch sampling to correct PEBS skid */
795 if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
796 x86_pmu.intel_cap.pebs_format < 2)
797 return true;
798
799 return false;
800}
801
802static inline bool intel_pmu_has_bts(struct perf_event *event)
803{
804 if (event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS &&
805 !event->attr.freq && event->hw.sample_period == 1)
806 return true;
807
808 return false;
809}
810
677int intel_pmu_save_and_restart(struct perf_event *event); 811int intel_pmu_save_and_restart(struct perf_event *event);
678 812
679struct event_constraint * 813struct event_constraint *
680x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event); 814x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
815 struct perf_event *event);
681 816
682struct intel_shared_regs *allocate_shared_regs(int cpu); 817struct intel_shared_regs *allocate_shared_regs(int cpu);
683 818
@@ -727,13 +862,15 @@ void intel_pmu_pebs_disable_all(void);
727 862
728void intel_ds_init(void); 863void intel_ds_init(void);
729 864
865void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in);
866
730void intel_pmu_lbr_reset(void); 867void intel_pmu_lbr_reset(void);
731 868
732void intel_pmu_lbr_enable(struct perf_event *event); 869void intel_pmu_lbr_enable(struct perf_event *event);
733 870
734void intel_pmu_lbr_disable(struct perf_event *event); 871void intel_pmu_lbr_disable(struct perf_event *event);
735 872
736void intel_pmu_lbr_enable_all(void); 873void intel_pmu_lbr_enable_all(bool pmi);
737 874
738void intel_pmu_lbr_disable_all(void); 875void intel_pmu_lbr_disable_all(void);
739 876
@@ -747,8 +884,18 @@ void intel_pmu_lbr_init_atom(void);
747 884
748void intel_pmu_lbr_init_snb(void); 885void intel_pmu_lbr_init_snb(void);
749 886
887void intel_pmu_lbr_init_hsw(void);
888
750int intel_pmu_setup_lbr_filter(struct perf_event *event); 889int intel_pmu_setup_lbr_filter(struct perf_event *event);
751 890
891void intel_pt_interrupt(void);
892
893int intel_bts_interrupt(void);
894
895void intel_bts_enable_local(void);
896
897void intel_bts_disable_local(void);
898
752int p4_pmu_init(void); 899int p4_pmu_init(void);
753 900
754int p6_pmu_init(void); 901int p6_pmu_init(void);
@@ -758,6 +905,10 @@ int knc_pmu_init(void);
758ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, 905ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
759 char *page); 906 char *page);
760 907
908static inline int is_ht_workaround_enabled(void)
909{
910 return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);
911}
761#else /* CONFIG_CPU_SUP_INTEL */ 912#else /* CONFIG_CPU_SUP_INTEL */
762 913
763static inline void reserve_ds_buffers(void) 914static inline void reserve_ds_buffers(void)
diff --git a/arch/x86/kernel/cpu/perf_event_amd.c b/arch/x86/kernel/cpu/perf_event_amd.c
index 28926311aac1..1cee5d2d7ece 100644
--- a/arch/x86/kernel/cpu/perf_event_amd.c
+++ b/arch/x86/kernel/cpu/perf_event_amd.c
@@ -382,6 +382,7 @@ static int amd_pmu_cpu_prepare(int cpu)
382static void amd_pmu_cpu_starting(int cpu) 382static void amd_pmu_cpu_starting(int cpu)
383{ 383{
384 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); 384 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
385 void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
385 struct amd_nb *nb; 386 struct amd_nb *nb;
386 int i, nb_id; 387 int i, nb_id;
387 388
@@ -399,7 +400,7 @@ static void amd_pmu_cpu_starting(int cpu)
399 continue; 400 continue;
400 401
401 if (nb->nb_id == nb_id) { 402 if (nb->nb_id == nb_id) {
402 cpuc->kfree_on_online = cpuc->amd_nb; 403 *onln = cpuc->amd_nb;
403 cpuc->amd_nb = nb; 404 cpuc->amd_nb = nb;
404 break; 405 break;
405 } 406 }
@@ -429,7 +430,8 @@ static void amd_pmu_cpu_dead(int cpu)
429} 430}
430 431
431static struct event_constraint * 432static struct event_constraint *
432amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) 433amd_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
434 struct perf_event *event)
433{ 435{
434 /* 436 /*
435 * if not NB event or no NB, then no constraints 437 * if not NB event or no NB, then no constraints
@@ -537,7 +539,8 @@ static struct event_constraint amd_f15_PMC50 = EVENT_CONSTRAINT(0, 0x3F, 0);
537static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0); 539static struct event_constraint amd_f15_PMC53 = EVENT_CONSTRAINT(0, 0x38, 0);
538 540
539static struct event_constraint * 541static struct event_constraint *
540amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, struct perf_event *event) 542amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, int idx,
543 struct perf_event *event)
541{ 544{
542 struct hw_perf_event *hwc = &event->hw; 545 struct hw_perf_event *hwc = &event->hw;
543 unsigned int event_code = amd_get_event_code(hwc); 546 unsigned int event_code = amd_get_event_code(hwc);
diff --git a/arch/x86/kernel/cpu/perf_event_amd_ibs.c b/arch/x86/kernel/cpu/perf_event_amd_ibs.c
index a61f5c6911da..989d3c215d2b 100644
--- a/arch/x86/kernel/cpu/perf_event_amd_ibs.c
+++ b/arch/x86/kernel/cpu/perf_event_amd_ibs.c
@@ -796,7 +796,7 @@ static int setup_ibs_ctl(int ibs_eilvt_off)
796 * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that 796 * the IBS interrupt vector is handled by perf_ibs_cpu_notifier that
797 * is using the new offset. 797 * is using the new offset.
798 */ 798 */
799static int force_ibs_eilvt_setup(void) 799static void force_ibs_eilvt_setup(void)
800{ 800{
801 int offset; 801 int offset;
802 int ret; 802 int ret;
@@ -811,26 +811,24 @@ static int force_ibs_eilvt_setup(void)
811 811
812 if (offset == APIC_EILVT_NR_MAX) { 812 if (offset == APIC_EILVT_NR_MAX) {
813 printk(KERN_DEBUG "No EILVT entry available\n"); 813 printk(KERN_DEBUG "No EILVT entry available\n");
814 return -EBUSY; 814 return;
815 } 815 }
816 816
817 ret = setup_ibs_ctl(offset); 817 ret = setup_ibs_ctl(offset);
818 if (ret) 818 if (ret)
819 goto out; 819 goto out;
820 820
821 if (!ibs_eilvt_valid()) { 821 if (!ibs_eilvt_valid())
822 ret = -EFAULT;
823 goto out; 822 goto out;
824 }
825 823
826 pr_info("IBS: LVT offset %d assigned\n", offset); 824 pr_info("IBS: LVT offset %d assigned\n", offset);
827 825
828 return 0; 826 return;
829out: 827out:
830 preempt_disable(); 828 preempt_disable();
831 put_eilvt(offset); 829 put_eilvt(offset);
832 preempt_enable(); 830 preempt_enable();
833 return ret; 831 return;
834} 832}
835 833
836static void ibs_eilvt_setup(void) 834static void ibs_eilvt_setup(void)
diff --git a/arch/x86/kernel/cpu/perf_event_intel.c b/arch/x86/kernel/cpu/perf_event_intel.c
index 258990688a5e..9da2400c2ec3 100644
--- a/arch/x86/kernel/cpu/perf_event_intel.c
+++ b/arch/x86/kernel/cpu/perf_event_intel.c
@@ -12,6 +12,7 @@
12#include <linux/init.h> 12#include <linux/init.h>
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/export.h> 14#include <linux/export.h>
15#include <linux/watchdog.h>
15 16
16#include <asm/cpufeature.h> 17#include <asm/cpufeature.h>
17#include <asm/hardirq.h> 18#include <asm/hardirq.h>
@@ -113,6 +114,12 @@ static struct event_constraint intel_snb_event_constraints[] __read_mostly =
113 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */ 114 INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
114 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */ 115 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
115 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */ 116 INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
117
118 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
119 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
120 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
121 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
122
116 EVENT_CONSTRAINT_END 123 EVENT_CONSTRAINT_END
117}; 124};
118 125
@@ -131,15 +138,12 @@ static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
131 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */ 138 INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
132 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */ 139 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
133 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */ 140 INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
134 /* 141
135 * Errata BV98 -- MEM_*_RETIRED events can leak between counters of SMT 142 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
136 * siblings; disable these events because they can corrupt unrelated 143 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
137 * counters. 144 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
138 */ 145 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
139 INTEL_EVENT_CONSTRAINT(0xd0, 0x0), /* MEM_UOPS_RETIRED.* */ 146
140 INTEL_EVENT_CONSTRAINT(0xd1, 0x0), /* MEM_LOAD_UOPS_RETIRED.* */
141 INTEL_EVENT_CONSTRAINT(0xd2, 0x0), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
142 INTEL_EVENT_CONSTRAINT(0xd3, 0x0), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
143 EVENT_CONSTRAINT_END 147 EVENT_CONSTRAINT_END
144}; 148};
145 149
@@ -217,6 +221,21 @@ static struct event_constraint intel_hsw_event_constraints[] = {
217 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), 221 INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4),
218 /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */ 222 /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
219 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), 223 INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf),
224
225 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
226 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
227 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
228 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
229
230 EVENT_CONSTRAINT_END
231};
232
233struct event_constraint intel_bdw_event_constraints[] = {
234 FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
235 FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
236 FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
237 INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
238 INTEL_EVENT_CONSTRAINT(0xa3, 0x4), /* CYCLE_ACTIVITY.* */
220 EVENT_CONSTRAINT_END 239 EVENT_CONSTRAINT_END
221}; 240};
222 241
@@ -415,6 +434,202 @@ static __initconst const u64 snb_hw_cache_event_ids
415 434
416}; 435};
417 436
437/*
438 * Notes on the events:
439 * - data reads do not include code reads (comparable to earlier tables)
440 * - data counts include speculative execution (except L1 write, dtlb, bpu)
441 * - remote node access includes remote memory, remote cache, remote mmio.
442 * - prefetches are not included in the counts because they are not
443 * reliably counted.
444 */
445
446#define HSW_DEMAND_DATA_RD BIT_ULL(0)
447#define HSW_DEMAND_RFO BIT_ULL(1)
448#define HSW_ANY_RESPONSE BIT_ULL(16)
449#define HSW_SUPPLIER_NONE BIT_ULL(17)
450#define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22)
451#define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27)
452#define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28)
453#define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29)
454#define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \
455 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
456 HSW_L3_MISS_REMOTE_HOP2P)
457#define HSW_SNOOP_NONE BIT_ULL(31)
458#define HSW_SNOOP_NOT_NEEDED BIT_ULL(32)
459#define HSW_SNOOP_MISS BIT_ULL(33)
460#define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34)
461#define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35)
462#define HSW_SNOOP_HITM BIT_ULL(36)
463#define HSW_SNOOP_NON_DRAM BIT_ULL(37)
464#define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \
465 HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \
466 HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \
467 HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM)
468#define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM)
469#define HSW_DEMAND_READ HSW_DEMAND_DATA_RD
470#define HSW_DEMAND_WRITE HSW_DEMAND_RFO
471#define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\
472 HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P)
473#define HSW_LLC_ACCESS HSW_ANY_RESPONSE
474
475#define BDW_L3_MISS_LOCAL BIT(26)
476#define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \
477 HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
478 HSW_L3_MISS_REMOTE_HOP2P)
479
480
481static __initconst const u64 hsw_hw_cache_event_ids
482 [PERF_COUNT_HW_CACHE_MAX]
483 [PERF_COUNT_HW_CACHE_OP_MAX]
484 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
485{
486 [ C(L1D ) ] = {
487 [ C(OP_READ) ] = {
488 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
489 [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
490 },
491 [ C(OP_WRITE) ] = {
492 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
493 [ C(RESULT_MISS) ] = 0x0,
494 },
495 [ C(OP_PREFETCH) ] = {
496 [ C(RESULT_ACCESS) ] = 0x0,
497 [ C(RESULT_MISS) ] = 0x0,
498 },
499 },
500 [ C(L1I ) ] = {
501 [ C(OP_READ) ] = {
502 [ C(RESULT_ACCESS) ] = 0x0,
503 [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */
504 },
505 [ C(OP_WRITE) ] = {
506 [ C(RESULT_ACCESS) ] = -1,
507 [ C(RESULT_MISS) ] = -1,
508 },
509 [ C(OP_PREFETCH) ] = {
510 [ C(RESULT_ACCESS) ] = 0x0,
511 [ C(RESULT_MISS) ] = 0x0,
512 },
513 },
514 [ C(LL ) ] = {
515 [ C(OP_READ) ] = {
516 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
517 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
518 },
519 [ C(OP_WRITE) ] = {
520 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
521 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
522 },
523 [ C(OP_PREFETCH) ] = {
524 [ C(RESULT_ACCESS) ] = 0x0,
525 [ C(RESULT_MISS) ] = 0x0,
526 },
527 },
528 [ C(DTLB) ] = {
529 [ C(OP_READ) ] = {
530 [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
531 [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */
532 },
533 [ C(OP_WRITE) ] = {
534 [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
535 [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
536 },
537 [ C(OP_PREFETCH) ] = {
538 [ C(RESULT_ACCESS) ] = 0x0,
539 [ C(RESULT_MISS) ] = 0x0,
540 },
541 },
542 [ C(ITLB) ] = {
543 [ C(OP_READ) ] = {
544 [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */
545 [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */
546 },
547 [ C(OP_WRITE) ] = {
548 [ C(RESULT_ACCESS) ] = -1,
549 [ C(RESULT_MISS) ] = -1,
550 },
551 [ C(OP_PREFETCH) ] = {
552 [ C(RESULT_ACCESS) ] = -1,
553 [ C(RESULT_MISS) ] = -1,
554 },
555 },
556 [ C(BPU ) ] = {
557 [ C(OP_READ) ] = {
558 [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
559 [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
560 },
561 [ C(OP_WRITE) ] = {
562 [ C(RESULT_ACCESS) ] = -1,
563 [ C(RESULT_MISS) ] = -1,
564 },
565 [ C(OP_PREFETCH) ] = {
566 [ C(RESULT_ACCESS) ] = -1,
567 [ C(RESULT_MISS) ] = -1,
568 },
569 },
570 [ C(NODE) ] = {
571 [ C(OP_READ) ] = {
572 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
573 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
574 },
575 [ C(OP_WRITE) ] = {
576 [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
577 [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
578 },
579 [ C(OP_PREFETCH) ] = {
580 [ C(RESULT_ACCESS) ] = 0x0,
581 [ C(RESULT_MISS) ] = 0x0,
582 },
583 },
584};
585
586static __initconst const u64 hsw_hw_cache_extra_regs
587 [PERF_COUNT_HW_CACHE_MAX]
588 [PERF_COUNT_HW_CACHE_OP_MAX]
589 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
590{
591 [ C(LL ) ] = {
592 [ C(OP_READ) ] = {
593 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
594 HSW_LLC_ACCESS,
595 [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
596 HSW_L3_MISS|HSW_ANY_SNOOP,
597 },
598 [ C(OP_WRITE) ] = {
599 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
600 HSW_LLC_ACCESS,
601 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
602 HSW_L3_MISS|HSW_ANY_SNOOP,
603 },
604 [ C(OP_PREFETCH) ] = {
605 [ C(RESULT_ACCESS) ] = 0x0,
606 [ C(RESULT_MISS) ] = 0x0,
607 },
608 },
609 [ C(NODE) ] = {
610 [ C(OP_READ) ] = {
611 [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
612 HSW_L3_MISS_LOCAL_DRAM|
613 HSW_SNOOP_DRAM,
614 [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
615 HSW_L3_MISS_REMOTE|
616 HSW_SNOOP_DRAM,
617 },
618 [ C(OP_WRITE) ] = {
619 [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
620 HSW_L3_MISS_LOCAL_DRAM|
621 HSW_SNOOP_DRAM,
622 [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
623 HSW_L3_MISS_REMOTE|
624 HSW_SNOOP_DRAM,
625 },
626 [ C(OP_PREFETCH) ] = {
627 [ C(RESULT_ACCESS) ] = 0x0,
628 [ C(RESULT_MISS) ] = 0x0,
629 },
630 },
631};
632
418static __initconst const u64 westmere_hw_cache_event_ids 633static __initconst const u64 westmere_hw_cache_event_ids
419 [PERF_COUNT_HW_CACHE_MAX] 634 [PERF_COUNT_HW_CACHE_MAX]
420 [PERF_COUNT_HW_CACHE_OP_MAX] 635 [PERF_COUNT_HW_CACHE_OP_MAX]
@@ -1029,21 +1244,10 @@ static __initconst const u64 slm_hw_cache_event_ids
1029 }, 1244 },
1030}; 1245};
1031 1246
1032static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event) 1247/*
1033{ 1248 * Use from PMIs where the LBRs are already disabled.
1034 /* user explicitly requested branch sampling */ 1249 */
1035 if (has_branch_stack(event)) 1250static void __intel_pmu_disable_all(void)
1036 return true;
1037
1038 /* implicit branch sampling to correct PEBS skid */
1039 if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
1040 x86_pmu.intel_cap.pebs_format < 2)
1041 return true;
1042
1043 return false;
1044}
1045
1046static void intel_pmu_disable_all(void)
1047{ 1251{
1048 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1252 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1049 1253
@@ -1051,17 +1255,24 @@ static void intel_pmu_disable_all(void)
1051 1255
1052 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) 1256 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
1053 intel_pmu_disable_bts(); 1257 intel_pmu_disable_bts();
1258 else
1259 intel_bts_disable_local();
1054 1260
1055 intel_pmu_pebs_disable_all(); 1261 intel_pmu_pebs_disable_all();
1262}
1263
1264static void intel_pmu_disable_all(void)
1265{
1266 __intel_pmu_disable_all();
1056 intel_pmu_lbr_disable_all(); 1267 intel_pmu_lbr_disable_all();
1057} 1268}
1058 1269
1059static void intel_pmu_enable_all(int added) 1270static void __intel_pmu_enable_all(int added, bool pmi)
1060{ 1271{
1061 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 1272 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1062 1273
1063 intel_pmu_pebs_enable_all(); 1274 intel_pmu_pebs_enable_all();
1064 intel_pmu_lbr_enable_all(); 1275 intel_pmu_lbr_enable_all(pmi);
1065 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 1276 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
1066 x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask); 1277 x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
1067 1278
@@ -1073,7 +1284,13 @@ static void intel_pmu_enable_all(int added)
1073 return; 1284 return;
1074 1285
1075 intel_pmu_enable_bts(event->hw.config); 1286 intel_pmu_enable_bts(event->hw.config);
1076 } 1287 } else
1288 intel_bts_enable_local();
1289}
1290
1291static void intel_pmu_enable_all(int added)
1292{
1293 __intel_pmu_enable_all(added, false);
1077} 1294}
1078 1295
1079/* 1296/*
@@ -1207,7 +1424,7 @@ static void intel_pmu_disable_event(struct perf_event *event)
1207 * must disable before any actual event 1424 * must disable before any actual event
1208 * because any event may be combined with LBR 1425 * because any event may be combined with LBR
1209 */ 1426 */
1210 if (intel_pmu_needs_lbr_smpl(event)) 1427 if (needs_branch_stack(event))
1211 intel_pmu_lbr_disable(event); 1428 intel_pmu_lbr_disable(event);
1212 1429
1213 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { 1430 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
@@ -1268,7 +1485,7 @@ static void intel_pmu_enable_event(struct perf_event *event)
1268 * must enabled before any actual event 1485 * must enabled before any actual event
1269 * because any event may be combined with LBR 1486 * because any event may be combined with LBR
1270 */ 1487 */
1271 if (intel_pmu_needs_lbr_smpl(event)) 1488 if (needs_branch_stack(event))
1272 intel_pmu_lbr_enable(event); 1489 intel_pmu_lbr_enable(event);
1273 1490
1274 if (event->attr.exclude_host) 1491 if (event->attr.exclude_host)
@@ -1334,6 +1551,18 @@ static void intel_pmu_reset(void)
1334 if (ds) 1551 if (ds)
1335 ds->bts_index = ds->bts_buffer_base; 1552 ds->bts_index = ds->bts_buffer_base;
1336 1553
1554 /* Ack all overflows and disable fixed counters */
1555 if (x86_pmu.version >= 2) {
1556 intel_pmu_ack_status(intel_pmu_get_status());
1557 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
1558 }
1559
1560 /* Reset LBRs and LBR freezing */
1561 if (x86_pmu.lbr_nr) {
1562 update_debugctlmsr(get_debugctlmsr() &
1563 ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR));
1564 }
1565
1337 local_irq_restore(flags); 1566 local_irq_restore(flags);
1338} 1567}
1339 1568
@@ -1357,8 +1586,9 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
1357 */ 1586 */
1358 if (!x86_pmu.late_ack) 1587 if (!x86_pmu.late_ack)
1359 apic_write(APIC_LVTPC, APIC_DM_NMI); 1588 apic_write(APIC_LVTPC, APIC_DM_NMI);
1360 intel_pmu_disable_all(); 1589 __intel_pmu_disable_all();
1361 handled = intel_pmu_drain_bts_buffer(); 1590 handled = intel_pmu_drain_bts_buffer();
1591 handled += intel_bts_interrupt();
1362 status = intel_pmu_get_status(); 1592 status = intel_pmu_get_status();
1363 if (!status) 1593 if (!status)
1364 goto done; 1594 goto done;
@@ -1399,6 +1629,14 @@ again:
1399 } 1629 }
1400 1630
1401 /* 1631 /*
1632 * Intel PT
1633 */
1634 if (__test_and_clear_bit(55, (unsigned long *)&status)) {
1635 handled++;
1636 intel_pt_interrupt();
1637 }
1638
1639 /*
1402 * Checkpointed counters can lead to 'spurious' PMIs because the 1640 * Checkpointed counters can lead to 'spurious' PMIs because the
1403 * rollback caused by the PMI will have cleared the overflow status 1641 * rollback caused by the PMI will have cleared the overflow status
1404 * bit. Therefore always force probe these counters. 1642 * bit. Therefore always force probe these counters.
@@ -1433,7 +1671,7 @@ again:
1433 goto again; 1671 goto again;
1434 1672
1435done: 1673done:
1436 intel_pmu_enable_all(0); 1674 __intel_pmu_enable_all(0, true);
1437 /* 1675 /*
1438 * Only unmask the NMI after the overflow counters 1676 * Only unmask the NMI after the overflow counters
1439 * have been reset. This avoids spurious NMIs on 1677 * have been reset. This avoids spurious NMIs on
@@ -1464,7 +1702,7 @@ intel_bts_constraints(struct perf_event *event)
1464 1702
1465static int intel_alt_er(int idx) 1703static int intel_alt_er(int idx)
1466{ 1704{
1467 if (!(x86_pmu.er_flags & ERF_HAS_RSP_1)) 1705 if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1))
1468 return idx; 1706 return idx;
1469 1707
1470 if (idx == EXTRA_REG_RSP_0) 1708 if (idx == EXTRA_REG_RSP_0)
@@ -1624,7 +1862,8 @@ intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
1624} 1862}
1625 1863
1626struct event_constraint * 1864struct event_constraint *
1627x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) 1865x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
1866 struct perf_event *event)
1628{ 1867{
1629 struct event_constraint *c; 1868 struct event_constraint *c;
1630 1869
@@ -1641,7 +1880,8 @@ x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
1641} 1880}
1642 1881
1643static struct event_constraint * 1882static struct event_constraint *
1644intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) 1883__intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
1884 struct perf_event *event)
1645{ 1885{
1646 struct event_constraint *c; 1886 struct event_constraint *c;
1647 1887
@@ -1657,7 +1897,278 @@ intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event
1657 if (c) 1897 if (c)
1658 return c; 1898 return c;
1659 1899
1660 return x86_get_event_constraints(cpuc, event); 1900 return x86_get_event_constraints(cpuc, idx, event);
1901}
1902
1903static void
1904intel_start_scheduling(struct cpu_hw_events *cpuc)
1905{
1906 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
1907 struct intel_excl_states *xl, *xlo;
1908 int tid = cpuc->excl_thread_id;
1909 int o_tid = 1 - tid; /* sibling thread */
1910
1911 /*
1912 * nothing needed if in group validation mode
1913 */
1914 if (cpuc->is_fake || !is_ht_workaround_enabled())
1915 return;
1916
1917 /*
1918 * no exclusion needed
1919 */
1920 if (!excl_cntrs)
1921 return;
1922
1923 xlo = &excl_cntrs->states[o_tid];
1924 xl = &excl_cntrs->states[tid];
1925
1926 xl->sched_started = true;
1927 xl->num_alloc_cntrs = 0;
1928 /*
1929 * lock shared state until we are done scheduling
1930 * in stop_event_scheduling()
1931 * makes scheduling appear as a transaction
1932 */
1933 WARN_ON_ONCE(!irqs_disabled());
1934 raw_spin_lock(&excl_cntrs->lock);
1935
1936 /*
1937 * save initial state of sibling thread
1938 */
1939 memcpy(xlo->init_state, xlo->state, sizeof(xlo->init_state));
1940}
1941
1942static void
1943intel_stop_scheduling(struct cpu_hw_events *cpuc)
1944{
1945 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
1946 struct intel_excl_states *xl, *xlo;
1947 int tid = cpuc->excl_thread_id;
1948 int o_tid = 1 - tid; /* sibling thread */
1949
1950 /*
1951 * nothing needed if in group validation mode
1952 */
1953 if (cpuc->is_fake || !is_ht_workaround_enabled())
1954 return;
1955 /*
1956 * no exclusion needed
1957 */
1958 if (!excl_cntrs)
1959 return;
1960
1961 xlo = &excl_cntrs->states[o_tid];
1962 xl = &excl_cntrs->states[tid];
1963
1964 /*
1965 * make new sibling thread state visible
1966 */
1967 memcpy(xlo->state, xlo->init_state, sizeof(xlo->state));
1968
1969 xl->sched_started = false;
1970 /*
1971 * release shared state lock (acquired in intel_start_scheduling())
1972 */
1973 raw_spin_unlock(&excl_cntrs->lock);
1974}
1975
1976static struct event_constraint *
1977intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
1978 int idx, struct event_constraint *c)
1979{
1980 struct event_constraint *cx;
1981 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
1982 struct intel_excl_states *xl, *xlo;
1983 int is_excl, i;
1984 int tid = cpuc->excl_thread_id;
1985 int o_tid = 1 - tid; /* alternate */
1986
1987 /*
1988 * validating a group does not require
1989 * enforcing cross-thread exclusion
1990 */
1991 if (cpuc->is_fake || !is_ht_workaround_enabled())
1992 return c;
1993
1994 /*
1995 * no exclusion needed
1996 */
1997 if (!excl_cntrs)
1998 return c;
1999 /*
2000 * event requires exclusive counter access
2001 * across HT threads
2002 */
2003 is_excl = c->flags & PERF_X86_EVENT_EXCL;
2004
2005 /*
2006 * xl = state of current HT
2007 * xlo = state of sibling HT
2008 */
2009 xl = &excl_cntrs->states[tid];
2010 xlo = &excl_cntrs->states[o_tid];
2011
2012 /*
2013 * do not allow scheduling of more than max_alloc_cntrs
2014 * which is set to half the available generic counters.
2015 * this helps avoid counter starvation of sibling thread
2016 * by ensuring at most half the counters cannot be in
2017 * exclusive mode. There is not designated counters for the
2018 * limits. Any N/2 counters can be used. This helps with
2019 * events with specifix counter constraints
2020 */
2021 if (xl->num_alloc_cntrs++ == xl->max_alloc_cntrs)
2022 return &emptyconstraint;
2023
2024 cx = c;
2025
2026 /*
2027 * because we modify the constraint, we need
2028 * to make a copy. Static constraints come
2029 * from static const tables.
2030 *
2031 * only needed when constraint has not yet
2032 * been cloned (marked dynamic)
2033 */
2034 if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) {
2035
2036 /* sanity check */
2037 if (idx < 0)
2038 return &emptyconstraint;
2039
2040 /*
2041 * grab pre-allocated constraint entry
2042 */
2043 cx = &cpuc->constraint_list[idx];
2044
2045 /*
2046 * initialize dynamic constraint
2047 * with static constraint
2048 */
2049 memcpy(cx, c, sizeof(*cx));
2050
2051 /*
2052 * mark constraint as dynamic, so we
2053 * can free it later on
2054 */
2055 cx->flags |= PERF_X86_EVENT_DYNAMIC;
2056 }
2057
2058 /*
2059 * From here on, the constraint is dynamic.
2060 * Either it was just allocated above, or it
2061 * was allocated during a earlier invocation
2062 * of this function
2063 */
2064
2065 /*
2066 * Modify static constraint with current dynamic
2067 * state of thread
2068 *
2069 * EXCLUSIVE: sibling counter measuring exclusive event
2070 * SHARED : sibling counter measuring non-exclusive event
2071 * UNUSED : sibling counter unused
2072 */
2073 for_each_set_bit(i, cx->idxmsk, X86_PMC_IDX_MAX) {
2074 /*
2075 * exclusive event in sibling counter
2076 * our corresponding counter cannot be used
2077 * regardless of our event
2078 */
2079 if (xl->state[i] == INTEL_EXCL_EXCLUSIVE)
2080 __clear_bit(i, cx->idxmsk);
2081 /*
2082 * if measuring an exclusive event, sibling
2083 * measuring non-exclusive, then counter cannot
2084 * be used
2085 */
2086 if (is_excl && xl->state[i] == INTEL_EXCL_SHARED)
2087 __clear_bit(i, cx->idxmsk);
2088 }
2089
2090 /*
2091 * recompute actual bit weight for scheduling algorithm
2092 */
2093 cx->weight = hweight64(cx->idxmsk64);
2094
2095 /*
2096 * if we return an empty mask, then switch
2097 * back to static empty constraint to avoid
2098 * the cost of freeing later on
2099 */
2100 if (cx->weight == 0)
2101 cx = &emptyconstraint;
2102
2103 return cx;
2104}
2105
2106static struct event_constraint *
2107intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2108 struct perf_event *event)
2109{
2110 struct event_constraint *c1 = event->hw.constraint;
2111 struct event_constraint *c2;
2112
2113 /*
2114 * first time only
2115 * - static constraint: no change across incremental scheduling calls
2116 * - dynamic constraint: handled by intel_get_excl_constraints()
2117 */
2118 c2 = __intel_get_event_constraints(cpuc, idx, event);
2119 if (c1 && (c1->flags & PERF_X86_EVENT_DYNAMIC)) {
2120 bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX);
2121 c1->weight = c2->weight;
2122 c2 = c1;
2123 }
2124
2125 if (cpuc->excl_cntrs)
2126 return intel_get_excl_constraints(cpuc, event, idx, c2);
2127
2128 return c2;
2129}
2130
2131static void intel_put_excl_constraints(struct cpu_hw_events *cpuc,
2132 struct perf_event *event)
2133{
2134 struct hw_perf_event *hwc = &event->hw;
2135 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2136 struct intel_excl_states *xlo, *xl;
2137 unsigned long flags = 0; /* keep compiler happy */
2138 int tid = cpuc->excl_thread_id;
2139 int o_tid = 1 - tid;
2140
2141 /*
2142 * nothing needed if in group validation mode
2143 */
2144 if (cpuc->is_fake)
2145 return;
2146
2147 WARN_ON_ONCE(!excl_cntrs);
2148
2149 if (!excl_cntrs)
2150 return;
2151
2152 xl = &excl_cntrs->states[tid];
2153 xlo = &excl_cntrs->states[o_tid];
2154
2155 /*
2156 * put_constraint may be called from x86_schedule_events()
2157 * which already has the lock held so here make locking
2158 * conditional
2159 */
2160 if (!xl->sched_started)
2161 raw_spin_lock_irqsave(&excl_cntrs->lock, flags);
2162
2163 /*
2164 * if event was actually assigned, then mark the
2165 * counter state as unused now
2166 */
2167 if (hwc->idx >= 0)
2168 xlo->state[hwc->idx] = INTEL_EXCL_UNUSED;
2169
2170 if (!xl->sched_started)
2171 raw_spin_unlock_irqrestore(&excl_cntrs->lock, flags);
1661} 2172}
1662 2173
1663static void 2174static void
@@ -1678,7 +2189,57 @@ intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
1678static void intel_put_event_constraints(struct cpu_hw_events *cpuc, 2189static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
1679 struct perf_event *event) 2190 struct perf_event *event)
1680{ 2191{
2192 struct event_constraint *c = event->hw.constraint;
2193
1681 intel_put_shared_regs_event_constraints(cpuc, event); 2194 intel_put_shared_regs_event_constraints(cpuc, event);
2195
2196 /*
2197 * is PMU has exclusive counter restrictions, then
2198 * all events are subject to and must call the
2199 * put_excl_constraints() routine
2200 */
2201 if (c && cpuc->excl_cntrs)
2202 intel_put_excl_constraints(cpuc, event);
2203
2204 /* cleanup dynamic constraint */
2205 if (c && (c->flags & PERF_X86_EVENT_DYNAMIC))
2206 event->hw.constraint = NULL;
2207}
2208
2209static void intel_commit_scheduling(struct cpu_hw_events *cpuc,
2210 struct perf_event *event, int cntr)
2211{
2212 struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
2213 struct event_constraint *c = event->hw.constraint;
2214 struct intel_excl_states *xlo, *xl;
2215 int tid = cpuc->excl_thread_id;
2216 int o_tid = 1 - tid;
2217 int is_excl;
2218
2219 if (cpuc->is_fake || !c)
2220 return;
2221
2222 is_excl = c->flags & PERF_X86_EVENT_EXCL;
2223
2224 if (!(c->flags & PERF_X86_EVENT_DYNAMIC))
2225 return;
2226
2227 WARN_ON_ONCE(!excl_cntrs);
2228
2229 if (!excl_cntrs)
2230 return;
2231
2232 xl = &excl_cntrs->states[tid];
2233 xlo = &excl_cntrs->states[o_tid];
2234
2235 WARN_ON_ONCE(!raw_spin_is_locked(&excl_cntrs->lock));
2236
2237 if (cntr >= 0) {
2238 if (is_excl)
2239 xlo->init_state[cntr] = INTEL_EXCL_EXCLUSIVE;
2240 else
2241 xlo->init_state[cntr] = INTEL_EXCL_SHARED;
2242 }
1682} 2243}
1683 2244
1684static void intel_pebs_aliases_core2(struct perf_event *event) 2245static void intel_pebs_aliases_core2(struct perf_event *event)
@@ -1747,10 +2308,21 @@ static int intel_pmu_hw_config(struct perf_event *event)
1747 if (event->attr.precise_ip && x86_pmu.pebs_aliases) 2308 if (event->attr.precise_ip && x86_pmu.pebs_aliases)
1748 x86_pmu.pebs_aliases(event); 2309 x86_pmu.pebs_aliases(event);
1749 2310
1750 if (intel_pmu_needs_lbr_smpl(event)) { 2311 if (needs_branch_stack(event)) {
1751 ret = intel_pmu_setup_lbr_filter(event); 2312 ret = intel_pmu_setup_lbr_filter(event);
1752 if (ret) 2313 if (ret)
1753 return ret; 2314 return ret;
2315
2316 /*
2317 * BTS is set up earlier in this path, so don't account twice
2318 */
2319 if (!intel_pmu_has_bts(event)) {
2320 /* disallow lbr if conflicting events are present */
2321 if (x86_add_exclusive(x86_lbr_exclusive_lbr))
2322 return -EBUSY;
2323
2324 event->destroy = hw_perf_lbr_event_destroy;
2325 }
1754 } 2326 }
1755 2327
1756 if (event->attr.type != PERF_TYPE_RAW) 2328 if (event->attr.type != PERF_TYPE_RAW)
@@ -1891,9 +2463,12 @@ static struct event_constraint counter2_constraint =
1891 EVENT_CONSTRAINT(0, 0x4, 0); 2463 EVENT_CONSTRAINT(0, 0x4, 0);
1892 2464
1893static struct event_constraint * 2465static struct event_constraint *
1894hsw_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) 2466hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
2467 struct perf_event *event)
1895{ 2468{
1896 struct event_constraint *c = intel_get_event_constraints(cpuc, event); 2469 struct event_constraint *c;
2470
2471 c = intel_get_event_constraints(cpuc, idx, event);
1897 2472
1898 /* Handle special quirk on in_tx_checkpointed only in counter 2 */ 2473 /* Handle special quirk on in_tx_checkpointed only in counter 2 */
1899 if (event->hw.config & HSW_IN_TX_CHECKPOINTED) { 2474 if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
@@ -1905,6 +2480,32 @@ hsw_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
1905 return c; 2480 return c;
1906} 2481}
1907 2482
2483/*
2484 * Broadwell:
2485 *
2486 * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared
2487 * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine
2488 * the two to enforce a minimum period of 128 (the smallest value that has bits
2489 * 0-5 cleared and >= 100).
2490 *
2491 * Because of how the code in x86_perf_event_set_period() works, the truncation
2492 * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period
2493 * to make up for the 'lost' events due to carrying the 'error' in period_left.
2494 *
2495 * Therefore the effective (average) period matches the requested period,
2496 * despite coarser hardware granularity.
2497 */
2498static unsigned bdw_limit_period(struct perf_event *event, unsigned left)
2499{
2500 if ((event->hw.config & INTEL_ARCH_EVENT_MASK) ==
2501 X86_CONFIG(.event=0xc0, .umask=0x01)) {
2502 if (left < 128)
2503 left = 128;
2504 left &= ~0x3fu;
2505 }
2506 return left;
2507}
2508
1908PMU_FORMAT_ATTR(event, "config:0-7" ); 2509PMU_FORMAT_ATTR(event, "config:0-7" );
1909PMU_FORMAT_ATTR(umask, "config:8-15" ); 2510PMU_FORMAT_ATTR(umask, "config:8-15" );
1910PMU_FORMAT_ATTR(edge, "config:18" ); 2511PMU_FORMAT_ATTR(edge, "config:18" );
@@ -1979,16 +2580,52 @@ struct intel_shared_regs *allocate_shared_regs(int cpu)
1979 return regs; 2580 return regs;
1980} 2581}
1981 2582
2583static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu)
2584{
2585 struct intel_excl_cntrs *c;
2586 int i;
2587
2588 c = kzalloc_node(sizeof(struct intel_excl_cntrs),
2589 GFP_KERNEL, cpu_to_node(cpu));
2590 if (c) {
2591 raw_spin_lock_init(&c->lock);
2592 for (i = 0; i < X86_PMC_IDX_MAX; i++) {
2593 c->states[0].state[i] = INTEL_EXCL_UNUSED;
2594 c->states[0].init_state[i] = INTEL_EXCL_UNUSED;
2595
2596 c->states[1].state[i] = INTEL_EXCL_UNUSED;
2597 c->states[1].init_state[i] = INTEL_EXCL_UNUSED;
2598 }
2599 c->core_id = -1;
2600 }
2601 return c;
2602}
2603
1982static int intel_pmu_cpu_prepare(int cpu) 2604static int intel_pmu_cpu_prepare(int cpu)
1983{ 2605{
1984 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); 2606 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
1985 2607
1986 if (!(x86_pmu.extra_regs || x86_pmu.lbr_sel_map)) 2608 if (x86_pmu.extra_regs || x86_pmu.lbr_sel_map) {
1987 return NOTIFY_OK; 2609 cpuc->shared_regs = allocate_shared_regs(cpu);
2610 if (!cpuc->shared_regs)
2611 return NOTIFY_BAD;
2612 }
1988 2613
1989 cpuc->shared_regs = allocate_shared_regs(cpu); 2614 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
1990 if (!cpuc->shared_regs) 2615 size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
1991 return NOTIFY_BAD; 2616
2617 cpuc->constraint_list = kzalloc(sz, GFP_KERNEL);
2618 if (!cpuc->constraint_list)
2619 return NOTIFY_BAD;
2620
2621 cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
2622 if (!cpuc->excl_cntrs) {
2623 kfree(cpuc->constraint_list);
2624 kfree(cpuc->shared_regs);
2625 return NOTIFY_BAD;
2626 }
2627 cpuc->excl_thread_id = 0;
2628 }
1992 2629
1993 return NOTIFY_OK; 2630 return NOTIFY_OK;
1994} 2631}
@@ -2010,13 +2647,15 @@ static void intel_pmu_cpu_starting(int cpu)
2010 if (!cpuc->shared_regs) 2647 if (!cpuc->shared_regs)
2011 return; 2648 return;
2012 2649
2013 if (!(x86_pmu.er_flags & ERF_NO_HT_SHARING)) { 2650 if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) {
2651 void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
2652
2014 for_each_cpu(i, topology_thread_cpumask(cpu)) { 2653 for_each_cpu(i, topology_thread_cpumask(cpu)) {
2015 struct intel_shared_regs *pc; 2654 struct intel_shared_regs *pc;
2016 2655
2017 pc = per_cpu(cpu_hw_events, i).shared_regs; 2656 pc = per_cpu(cpu_hw_events, i).shared_regs;
2018 if (pc && pc->core_id == core_id) { 2657 if (pc && pc->core_id == core_id) {
2019 cpuc->kfree_on_online = cpuc->shared_regs; 2658 *onln = cpuc->shared_regs;
2020 cpuc->shared_regs = pc; 2659 cpuc->shared_regs = pc;
2021 break; 2660 break;
2022 } 2661 }
@@ -2027,6 +2666,44 @@ static void intel_pmu_cpu_starting(int cpu)
2027 2666
2028 if (x86_pmu.lbr_sel_map) 2667 if (x86_pmu.lbr_sel_map)
2029 cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR]; 2668 cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
2669
2670 if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
2671 int h = x86_pmu.num_counters >> 1;
2672
2673 for_each_cpu(i, topology_thread_cpumask(cpu)) {
2674 struct intel_excl_cntrs *c;
2675
2676 c = per_cpu(cpu_hw_events, i).excl_cntrs;
2677 if (c && c->core_id == core_id) {
2678 cpuc->kfree_on_online[1] = cpuc->excl_cntrs;
2679 cpuc->excl_cntrs = c;
2680 cpuc->excl_thread_id = 1;
2681 break;
2682 }
2683 }
2684 cpuc->excl_cntrs->core_id = core_id;
2685 cpuc->excl_cntrs->refcnt++;
2686 /*
2687 * set hard limit to half the number of generic counters
2688 */
2689 cpuc->excl_cntrs->states[0].max_alloc_cntrs = h;
2690 cpuc->excl_cntrs->states[1].max_alloc_cntrs = h;
2691 }
2692}
2693
2694static void free_excl_cntrs(int cpu)
2695{
2696 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2697 struct intel_excl_cntrs *c;
2698
2699 c = cpuc->excl_cntrs;
2700 if (c) {
2701 if (c->core_id == -1 || --c->refcnt == 0)
2702 kfree(c);
2703 cpuc->excl_cntrs = NULL;
2704 kfree(cpuc->constraint_list);
2705 cpuc->constraint_list = NULL;
2706 }
2030} 2707}
2031 2708
2032static void intel_pmu_cpu_dying(int cpu) 2709static void intel_pmu_cpu_dying(int cpu)
@@ -2041,19 +2718,9 @@ static void intel_pmu_cpu_dying(int cpu)
2041 cpuc->shared_regs = NULL; 2718 cpuc->shared_regs = NULL;
2042 } 2719 }
2043 2720
2044 fini_debug_store_on_cpu(cpu); 2721 free_excl_cntrs(cpu);
2045}
2046 2722
2047static void intel_pmu_flush_branch_stack(void) 2723 fini_debug_store_on_cpu(cpu);
2048{
2049 /*
2050 * Intel LBR does not tag entries with the
2051 * PID of the current task, then we need to
2052 * flush it on ctxsw
2053 * For now, we simply reset it
2054 */
2055 if (x86_pmu.lbr_nr)
2056 intel_pmu_lbr_reset();
2057} 2724}
2058 2725
2059PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63"); 2726PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
@@ -2107,7 +2774,7 @@ static __initconst const struct x86_pmu intel_pmu = {
2107 .cpu_starting = intel_pmu_cpu_starting, 2774 .cpu_starting = intel_pmu_cpu_starting,
2108 .cpu_dying = intel_pmu_cpu_dying, 2775 .cpu_dying = intel_pmu_cpu_dying,
2109 .guest_get_msrs = intel_guest_get_msrs, 2776 .guest_get_msrs = intel_guest_get_msrs,
2110 .flush_branch_stack = intel_pmu_flush_branch_stack, 2777 .sched_task = intel_pmu_lbr_sched_task,
2111}; 2778};
2112 2779
2113static __init void intel_clovertown_quirk(void) 2780static __init void intel_clovertown_quirk(void)
@@ -2264,6 +2931,27 @@ static __init void intel_nehalem_quirk(void)
2264 } 2931 }
2265} 2932}
2266 2933
2934/*
2935 * enable software workaround for errata:
2936 * SNB: BJ122
2937 * IVB: BV98
2938 * HSW: HSD29
2939 *
2940 * Only needed when HT is enabled. However detecting
2941 * if HT is enabled is difficult (model specific). So instead,
2942 * we enable the workaround in the early boot, and verify if
2943 * it is needed in a later initcall phase once we have valid
2944 * topology information to check if HT is actually enabled
2945 */
2946static __init void intel_ht_bug(void)
2947{
2948 x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED;
2949
2950 x86_pmu.commit_scheduling = intel_commit_scheduling;
2951 x86_pmu.start_scheduling = intel_start_scheduling;
2952 x86_pmu.stop_scheduling = intel_stop_scheduling;
2953}
2954
2267EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3"); 2955EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3");
2268EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82") 2956EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82")
2269 2957
@@ -2443,7 +3131,7 @@ __init int intel_pmu_init(void)
2443 x86_pmu.event_constraints = intel_slm_event_constraints; 3131 x86_pmu.event_constraints = intel_slm_event_constraints;
2444 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints; 3132 x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
2445 x86_pmu.extra_regs = intel_slm_extra_regs; 3133 x86_pmu.extra_regs = intel_slm_extra_regs;
2446 x86_pmu.er_flags |= ERF_HAS_RSP_1; 3134 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
2447 pr_cont("Silvermont events, "); 3135 pr_cont("Silvermont events, ");
2448 break; 3136 break;
2449 3137
@@ -2461,7 +3149,7 @@ __init int intel_pmu_init(void)
2461 x86_pmu.enable_all = intel_pmu_nhm_enable_all; 3149 x86_pmu.enable_all = intel_pmu_nhm_enable_all;
2462 x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints; 3150 x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
2463 x86_pmu.extra_regs = intel_westmere_extra_regs; 3151 x86_pmu.extra_regs = intel_westmere_extra_regs;
2464 x86_pmu.er_flags |= ERF_HAS_RSP_1; 3152 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
2465 3153
2466 x86_pmu.cpu_events = nhm_events_attrs; 3154 x86_pmu.cpu_events = nhm_events_attrs;
2467 3155
@@ -2478,6 +3166,7 @@ __init int intel_pmu_init(void)
2478 case 42: /* 32nm SandyBridge */ 3166 case 42: /* 32nm SandyBridge */
2479 case 45: /* 32nm SandyBridge-E/EN/EP */ 3167 case 45: /* 32nm SandyBridge-E/EN/EP */
2480 x86_add_quirk(intel_sandybridge_quirk); 3168 x86_add_quirk(intel_sandybridge_quirk);
3169 x86_add_quirk(intel_ht_bug);
2481 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids, 3170 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
2482 sizeof(hw_cache_event_ids)); 3171 sizeof(hw_cache_event_ids));
2483 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs, 3172 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
@@ -2492,9 +3181,11 @@ __init int intel_pmu_init(void)
2492 x86_pmu.extra_regs = intel_snbep_extra_regs; 3181 x86_pmu.extra_regs = intel_snbep_extra_regs;
2493 else 3182 else
2494 x86_pmu.extra_regs = intel_snb_extra_regs; 3183 x86_pmu.extra_regs = intel_snb_extra_regs;
3184
3185
2495 /* all extra regs are per-cpu when HT is on */ 3186 /* all extra regs are per-cpu when HT is on */
2496 x86_pmu.er_flags |= ERF_HAS_RSP_1; 3187 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
2497 x86_pmu.er_flags |= ERF_NO_HT_SHARING; 3188 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
2498 3189
2499 x86_pmu.cpu_events = snb_events_attrs; 3190 x86_pmu.cpu_events = snb_events_attrs;
2500 3191
@@ -2510,6 +3201,7 @@ __init int intel_pmu_init(void)
2510 3201
2511 case 58: /* 22nm IvyBridge */ 3202 case 58: /* 22nm IvyBridge */
2512 case 62: /* 22nm IvyBridge-EP/EX */ 3203 case 62: /* 22nm IvyBridge-EP/EX */
3204 x86_add_quirk(intel_ht_bug);
2513 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids, 3205 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
2514 sizeof(hw_cache_event_ids)); 3206 sizeof(hw_cache_event_ids));
2515 /* dTLB-load-misses on IVB is different than SNB */ 3207 /* dTLB-load-misses on IVB is different than SNB */
@@ -2528,8 +3220,8 @@ __init int intel_pmu_init(void)
2528 else 3220 else
2529 x86_pmu.extra_regs = intel_snb_extra_regs; 3221 x86_pmu.extra_regs = intel_snb_extra_regs;
2530 /* all extra regs are per-cpu when HT is on */ 3222 /* all extra regs are per-cpu when HT is on */
2531 x86_pmu.er_flags |= ERF_HAS_RSP_1; 3223 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
2532 x86_pmu.er_flags |= ERF_NO_HT_SHARING; 3224 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
2533 3225
2534 x86_pmu.cpu_events = snb_events_attrs; 3226 x86_pmu.cpu_events = snb_events_attrs;
2535 3227
@@ -2545,19 +3237,20 @@ __init int intel_pmu_init(void)
2545 case 63: /* 22nm Haswell Server */ 3237 case 63: /* 22nm Haswell Server */
2546 case 69: /* 22nm Haswell ULT */ 3238 case 69: /* 22nm Haswell ULT */
2547 case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */ 3239 case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
3240 x86_add_quirk(intel_ht_bug);
2548 x86_pmu.late_ack = true; 3241 x86_pmu.late_ack = true;
2549 memcpy(hw_cache_event_ids, snb_hw_cache_event_ids, sizeof(hw_cache_event_ids)); 3242 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
2550 memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs, sizeof(hw_cache_extra_regs)); 3243 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
2551 3244
2552 intel_pmu_lbr_init_snb(); 3245 intel_pmu_lbr_init_hsw();
2553 3246
2554 x86_pmu.event_constraints = intel_hsw_event_constraints; 3247 x86_pmu.event_constraints = intel_hsw_event_constraints;
2555 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints; 3248 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
2556 x86_pmu.extra_regs = intel_snbep_extra_regs; 3249 x86_pmu.extra_regs = intel_snbep_extra_regs;
2557 x86_pmu.pebs_aliases = intel_pebs_aliases_snb; 3250 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
2558 /* all extra regs are per-cpu when HT is on */ 3251 /* all extra regs are per-cpu when HT is on */
2559 x86_pmu.er_flags |= ERF_HAS_RSP_1; 3252 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
2560 x86_pmu.er_flags |= ERF_NO_HT_SHARING; 3253 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
2561 3254
2562 x86_pmu.hw_config = hsw_hw_config; 3255 x86_pmu.hw_config = hsw_hw_config;
2563 x86_pmu.get_event_constraints = hsw_get_event_constraints; 3256 x86_pmu.get_event_constraints = hsw_get_event_constraints;
@@ -2566,6 +3259,39 @@ __init int intel_pmu_init(void)
2566 pr_cont("Haswell events, "); 3259 pr_cont("Haswell events, ");
2567 break; 3260 break;
2568 3261
3262 case 61: /* 14nm Broadwell Core-M */
3263 case 86: /* 14nm Broadwell Xeon D */
3264 x86_pmu.late_ack = true;
3265 memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
3266 memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
3267
3268 /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */
3269 hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ |
3270 BDW_L3_MISS|HSW_SNOOP_DRAM;
3271 hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS|
3272 HSW_SNOOP_DRAM;
3273 hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ|
3274 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
3275 hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE|
3276 BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
3277
3278 intel_pmu_lbr_init_snb();
3279
3280 x86_pmu.event_constraints = intel_bdw_event_constraints;
3281 x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
3282 x86_pmu.extra_regs = intel_snbep_extra_regs;
3283 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
3284 /* all extra regs are per-cpu when HT is on */
3285 x86_pmu.flags |= PMU_FL_HAS_RSP_1;
3286 x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
3287
3288 x86_pmu.hw_config = hsw_hw_config;
3289 x86_pmu.get_event_constraints = hsw_get_event_constraints;
3290 x86_pmu.cpu_events = hsw_events_attrs;
3291 x86_pmu.limit_period = bdw_limit_period;
3292 pr_cont("Broadwell events, ");
3293 break;
3294
2569 default: 3295 default:
2570 switch (x86_pmu.version) { 3296 switch (x86_pmu.version) {
2571 case 1: 3297 case 1:
@@ -2651,3 +3377,47 @@ __init int intel_pmu_init(void)
2651 3377
2652 return 0; 3378 return 0;
2653} 3379}
3380
3381/*
3382 * HT bug: phase 2 init
3383 * Called once we have valid topology information to check
3384 * whether or not HT is enabled
3385 * If HT is off, then we disable the workaround
3386 */
3387static __init int fixup_ht_bug(void)
3388{
3389 int cpu = smp_processor_id();
3390 int w, c;
3391 /*
3392 * problem not present on this CPU model, nothing to do
3393 */
3394 if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED))
3395 return 0;
3396
3397 w = cpumask_weight(topology_thread_cpumask(cpu));
3398 if (w > 1) {
3399 pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n");
3400 return 0;
3401 }
3402
3403 watchdog_nmi_disable_all();
3404
3405 x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED);
3406
3407 x86_pmu.commit_scheduling = NULL;
3408 x86_pmu.start_scheduling = NULL;
3409 x86_pmu.stop_scheduling = NULL;
3410
3411 watchdog_nmi_enable_all();
3412
3413 get_online_cpus();
3414
3415 for_each_online_cpu(c) {
3416 free_excl_cntrs(c);
3417 }
3418
3419 put_online_cpus();
3420 pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n");
3421 return 0;
3422}
3423subsys_initcall(fixup_ht_bug)
diff --git a/arch/x86/kernel/cpu/perf_event_intel_bts.c b/arch/x86/kernel/cpu/perf_event_intel_bts.c
new file mode 100644
index 000000000000..ac1f0c55f379
--- /dev/null
+++ b/arch/x86/kernel/cpu/perf_event_intel_bts.c
@@ -0,0 +1,525 @@
1/*
2 * BTS PMU driver for perf
3 * Copyright (c) 2013-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#undef DEBUG
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/bitops.h>
20#include <linux/types.h>
21#include <linux/slab.h>
22#include <linux/debugfs.h>
23#include <linux/device.h>
24#include <linux/coredump.h>
25
26#include <asm-generic/sizes.h>
27#include <asm/perf_event.h>
28
29#include "perf_event.h"
30
31struct bts_ctx {
32 struct perf_output_handle handle;
33 struct debug_store ds_back;
34 int started;
35};
36
37static DEFINE_PER_CPU(struct bts_ctx, bts_ctx);
38
39#define BTS_RECORD_SIZE 24
40#define BTS_SAFETY_MARGIN 4080
41
42struct bts_phys {
43 struct page *page;
44 unsigned long size;
45 unsigned long offset;
46 unsigned long displacement;
47};
48
49struct bts_buffer {
50 size_t real_size; /* multiple of BTS_RECORD_SIZE */
51 unsigned int nr_pages;
52 unsigned int nr_bufs;
53 unsigned int cur_buf;
54 bool snapshot;
55 local_t data_size;
56 local_t lost;
57 local_t head;
58 unsigned long end;
59 void **data_pages;
60 struct bts_phys buf[0];
61};
62
63struct pmu bts_pmu;
64
65void intel_pmu_enable_bts(u64 config);
66void intel_pmu_disable_bts(void);
67
68static size_t buf_size(struct page *page)
69{
70 return 1 << (PAGE_SHIFT + page_private(page));
71}
72
73static void *
74bts_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool overwrite)
75{
76 struct bts_buffer *buf;
77 struct page *page;
78 int node = (cpu == -1) ? cpu : cpu_to_node(cpu);
79 unsigned long offset;
80 size_t size = nr_pages << PAGE_SHIFT;
81 int pg, nbuf, pad;
82
83 /* count all the high order buffers */
84 for (pg = 0, nbuf = 0; pg < nr_pages;) {
85 page = virt_to_page(pages[pg]);
86 if (WARN_ON_ONCE(!PagePrivate(page) && nr_pages > 1))
87 return NULL;
88 pg += 1 << page_private(page);
89 nbuf++;
90 }
91
92 /*
93 * to avoid interrupts in overwrite mode, only allow one physical
94 */
95 if (overwrite && nbuf > 1)
96 return NULL;
97
98 buf = kzalloc_node(offsetof(struct bts_buffer, buf[nbuf]), GFP_KERNEL, node);
99 if (!buf)
100 return NULL;
101
102 buf->nr_pages = nr_pages;
103 buf->nr_bufs = nbuf;
104 buf->snapshot = overwrite;
105 buf->data_pages = pages;
106 buf->real_size = size - size % BTS_RECORD_SIZE;
107
108 for (pg = 0, nbuf = 0, offset = 0, pad = 0; nbuf < buf->nr_bufs; nbuf++) {
109 unsigned int __nr_pages;
110
111 page = virt_to_page(pages[pg]);
112 __nr_pages = PagePrivate(page) ? 1 << page_private(page) : 1;
113 buf->buf[nbuf].page = page;
114 buf->buf[nbuf].offset = offset;
115 buf->buf[nbuf].displacement = (pad ? BTS_RECORD_SIZE - pad : 0);
116 buf->buf[nbuf].size = buf_size(page) - buf->buf[nbuf].displacement;
117 pad = buf->buf[nbuf].size % BTS_RECORD_SIZE;
118 buf->buf[nbuf].size -= pad;
119
120 pg += __nr_pages;
121 offset += __nr_pages << PAGE_SHIFT;
122 }
123
124 return buf;
125}
126
127static void bts_buffer_free_aux(void *data)
128{
129 kfree(data);
130}
131
132static unsigned long bts_buffer_offset(struct bts_buffer *buf, unsigned int idx)
133{
134 return buf->buf[idx].offset + buf->buf[idx].displacement;
135}
136
137static void
138bts_config_buffer(struct bts_buffer *buf)
139{
140 int cpu = raw_smp_processor_id();
141 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
142 struct bts_phys *phys = &buf->buf[buf->cur_buf];
143 unsigned long index, thresh = 0, end = phys->size;
144 struct page *page = phys->page;
145
146 index = local_read(&buf->head);
147
148 if (!buf->snapshot) {
149 if (buf->end < phys->offset + buf_size(page))
150 end = buf->end - phys->offset - phys->displacement;
151
152 index -= phys->offset + phys->displacement;
153
154 if (end - index > BTS_SAFETY_MARGIN)
155 thresh = end - BTS_SAFETY_MARGIN;
156 else if (end - index > BTS_RECORD_SIZE)
157 thresh = end - BTS_RECORD_SIZE;
158 else
159 thresh = end;
160 }
161
162 ds->bts_buffer_base = (u64)(long)page_address(page) + phys->displacement;
163 ds->bts_index = ds->bts_buffer_base + index;
164 ds->bts_absolute_maximum = ds->bts_buffer_base + end;
165 ds->bts_interrupt_threshold = !buf->snapshot
166 ? ds->bts_buffer_base + thresh
167 : ds->bts_absolute_maximum + BTS_RECORD_SIZE;
168}
169
170static void bts_buffer_pad_out(struct bts_phys *phys, unsigned long head)
171{
172 unsigned long index = head - phys->offset;
173
174 memset(page_address(phys->page) + index, 0, phys->size - index);
175}
176
177static bool bts_buffer_is_full(struct bts_buffer *buf, struct bts_ctx *bts)
178{
179 if (buf->snapshot)
180 return false;
181
182 if (local_read(&buf->data_size) >= bts->handle.size ||
183 bts->handle.size - local_read(&buf->data_size) < BTS_RECORD_SIZE)
184 return true;
185
186 return false;
187}
188
189static void bts_update(struct bts_ctx *bts)
190{
191 int cpu = raw_smp_processor_id();
192 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
193 struct bts_buffer *buf = perf_get_aux(&bts->handle);
194 unsigned long index = ds->bts_index - ds->bts_buffer_base, old, head;
195
196 if (!buf)
197 return;
198
199 head = index + bts_buffer_offset(buf, buf->cur_buf);
200 old = local_xchg(&buf->head, head);
201
202 if (!buf->snapshot) {
203 if (old == head)
204 return;
205
206 if (ds->bts_index >= ds->bts_absolute_maximum)
207 local_inc(&buf->lost);
208
209 /*
210 * old and head are always in the same physical buffer, so we
211 * can subtract them to get the data size.
212 */
213 local_add(head - old, &buf->data_size);
214 } else {
215 local_set(&buf->data_size, head);
216 }
217}
218
219static void __bts_event_start(struct perf_event *event)
220{
221 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
222 struct bts_buffer *buf = perf_get_aux(&bts->handle);
223 u64 config = 0;
224
225 if (!buf || bts_buffer_is_full(buf, bts))
226 return;
227
228 event->hw.state = 0;
229
230 if (!buf->snapshot)
231 config |= ARCH_PERFMON_EVENTSEL_INT;
232 if (!event->attr.exclude_kernel)
233 config |= ARCH_PERFMON_EVENTSEL_OS;
234 if (!event->attr.exclude_user)
235 config |= ARCH_PERFMON_EVENTSEL_USR;
236
237 bts_config_buffer(buf);
238
239 /*
240 * local barrier to make sure that ds configuration made it
241 * before we enable BTS
242 */
243 wmb();
244
245 intel_pmu_enable_bts(config);
246}
247
248static void bts_event_start(struct perf_event *event, int flags)
249{
250 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
251
252 __bts_event_start(event);
253
254 /* PMI handler: this counter is running and likely generating PMIs */
255 ACCESS_ONCE(bts->started) = 1;
256}
257
258static void __bts_event_stop(struct perf_event *event)
259{
260 /*
261 * No extra synchronization is mandated by the documentation to have
262 * BTS data stores globally visible.
263 */
264 intel_pmu_disable_bts();
265
266 if (event->hw.state & PERF_HES_STOPPED)
267 return;
268
269 ACCESS_ONCE(event->hw.state) |= PERF_HES_STOPPED;
270}
271
272static void bts_event_stop(struct perf_event *event, int flags)
273{
274 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
275
276 /* PMI handler: don't restart this counter */
277 ACCESS_ONCE(bts->started) = 0;
278
279 __bts_event_stop(event);
280
281 if (flags & PERF_EF_UPDATE)
282 bts_update(bts);
283}
284
285void intel_bts_enable_local(void)
286{
287 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
288
289 if (bts->handle.event && bts->started)
290 __bts_event_start(bts->handle.event);
291}
292
293void intel_bts_disable_local(void)
294{
295 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
296
297 if (bts->handle.event)
298 __bts_event_stop(bts->handle.event);
299}
300
301static int
302bts_buffer_reset(struct bts_buffer *buf, struct perf_output_handle *handle)
303{
304 unsigned long head, space, next_space, pad, gap, skip, wakeup;
305 unsigned int next_buf;
306 struct bts_phys *phys, *next_phys;
307 int ret;
308
309 if (buf->snapshot)
310 return 0;
311
312 head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
313 if (WARN_ON_ONCE(head != local_read(&buf->head)))
314 return -EINVAL;
315
316 phys = &buf->buf[buf->cur_buf];
317 space = phys->offset + phys->displacement + phys->size - head;
318 pad = space;
319 if (space > handle->size) {
320 space = handle->size;
321 space -= space % BTS_RECORD_SIZE;
322 }
323 if (space <= BTS_SAFETY_MARGIN) {
324 /* See if next phys buffer has more space */
325 next_buf = buf->cur_buf + 1;
326 if (next_buf >= buf->nr_bufs)
327 next_buf = 0;
328 next_phys = &buf->buf[next_buf];
329 gap = buf_size(phys->page) - phys->displacement - phys->size +
330 next_phys->displacement;
331 skip = pad + gap;
332 if (handle->size >= skip) {
333 next_space = next_phys->size;
334 if (next_space + skip > handle->size) {
335 next_space = handle->size - skip;
336 next_space -= next_space % BTS_RECORD_SIZE;
337 }
338 if (next_space > space || !space) {
339 if (pad)
340 bts_buffer_pad_out(phys, head);
341 ret = perf_aux_output_skip(handle, skip);
342 if (ret)
343 return ret;
344 /* Advance to next phys buffer */
345 phys = next_phys;
346 space = next_space;
347 head = phys->offset + phys->displacement;
348 /*
349 * After this, cur_buf and head won't match ds
350 * anymore, so we must not be racing with
351 * bts_update().
352 */
353 buf->cur_buf = next_buf;
354 local_set(&buf->head, head);
355 }
356 }
357 }
358
359 /* Don't go far beyond wakeup watermark */
360 wakeup = BTS_SAFETY_MARGIN + BTS_RECORD_SIZE + handle->wakeup -
361 handle->head;
362 if (space > wakeup) {
363 space = wakeup;
364 space -= space % BTS_RECORD_SIZE;
365 }
366
367 buf->end = head + space;
368
369 /*
370 * If we have no space, the lost notification would have been sent when
371 * we hit absolute_maximum - see bts_update()
372 */
373 if (!space)
374 return -ENOSPC;
375
376 return 0;
377}
378
379int intel_bts_interrupt(void)
380{
381 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
382 struct perf_event *event = bts->handle.event;
383 struct bts_buffer *buf;
384 s64 old_head;
385 int err;
386
387 if (!event || !bts->started)
388 return 0;
389
390 buf = perf_get_aux(&bts->handle);
391 /*
392 * Skip snapshot counters: they don't use the interrupt, but
393 * there's no other way of telling, because the pointer will
394 * keep moving
395 */
396 if (!buf || buf->snapshot)
397 return 0;
398
399 old_head = local_read(&buf->head);
400 bts_update(bts);
401
402 /* no new data */
403 if (old_head == local_read(&buf->head))
404 return 0;
405
406 perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0),
407 !!local_xchg(&buf->lost, 0));
408
409 buf = perf_aux_output_begin(&bts->handle, event);
410 if (!buf)
411 return 1;
412
413 err = bts_buffer_reset(buf, &bts->handle);
414 if (err)
415 perf_aux_output_end(&bts->handle, 0, false);
416
417 return 1;
418}
419
420static void bts_event_del(struct perf_event *event, int mode)
421{
422 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
423 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
424 struct bts_buffer *buf = perf_get_aux(&bts->handle);
425
426 bts_event_stop(event, PERF_EF_UPDATE);
427
428 if (buf) {
429 if (buf->snapshot)
430 bts->handle.head =
431 local_xchg(&buf->data_size,
432 buf->nr_pages << PAGE_SHIFT);
433 perf_aux_output_end(&bts->handle, local_xchg(&buf->data_size, 0),
434 !!local_xchg(&buf->lost, 0));
435 }
436
437 cpuc->ds->bts_index = bts->ds_back.bts_buffer_base;
438 cpuc->ds->bts_buffer_base = bts->ds_back.bts_buffer_base;
439 cpuc->ds->bts_absolute_maximum = bts->ds_back.bts_absolute_maximum;
440 cpuc->ds->bts_interrupt_threshold = bts->ds_back.bts_interrupt_threshold;
441}
442
443static int bts_event_add(struct perf_event *event, int mode)
444{
445 struct bts_buffer *buf;
446 struct bts_ctx *bts = this_cpu_ptr(&bts_ctx);
447 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
448 struct hw_perf_event *hwc = &event->hw;
449 int ret = -EBUSY;
450
451 event->hw.state = PERF_HES_STOPPED;
452
453 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
454 return -EBUSY;
455
456 if (bts->handle.event)
457 return -EBUSY;
458
459 buf = perf_aux_output_begin(&bts->handle, event);
460 if (!buf)
461 return -EINVAL;
462
463 ret = bts_buffer_reset(buf, &bts->handle);
464 if (ret) {
465 perf_aux_output_end(&bts->handle, 0, false);
466 return ret;
467 }
468
469 bts->ds_back.bts_buffer_base = cpuc->ds->bts_buffer_base;
470 bts->ds_back.bts_absolute_maximum = cpuc->ds->bts_absolute_maximum;
471 bts->ds_back.bts_interrupt_threshold = cpuc->ds->bts_interrupt_threshold;
472
473 if (mode & PERF_EF_START) {
474 bts_event_start(event, 0);
475 if (hwc->state & PERF_HES_STOPPED) {
476 bts_event_del(event, 0);
477 return -EBUSY;
478 }
479 }
480
481 return 0;
482}
483
484static void bts_event_destroy(struct perf_event *event)
485{
486 x86_del_exclusive(x86_lbr_exclusive_bts);
487}
488
489static int bts_event_init(struct perf_event *event)
490{
491 if (event->attr.type != bts_pmu.type)
492 return -ENOENT;
493
494 if (x86_add_exclusive(x86_lbr_exclusive_bts))
495 return -EBUSY;
496
497 event->destroy = bts_event_destroy;
498
499 return 0;
500}
501
502static void bts_event_read(struct perf_event *event)
503{
504}
505
506static __init int bts_init(void)
507{
508 if (!boot_cpu_has(X86_FEATURE_DTES64) || !x86_pmu.bts)
509 return -ENODEV;
510
511 bts_pmu.capabilities = PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_ITRACE;
512 bts_pmu.task_ctx_nr = perf_sw_context;
513 bts_pmu.event_init = bts_event_init;
514 bts_pmu.add = bts_event_add;
515 bts_pmu.del = bts_event_del;
516 bts_pmu.start = bts_event_start;
517 bts_pmu.stop = bts_event_stop;
518 bts_pmu.read = bts_event_read;
519 bts_pmu.setup_aux = bts_buffer_setup_aux;
520 bts_pmu.free_aux = bts_buffer_free_aux;
521
522 return perf_pmu_register(&bts_pmu, "intel_bts", -1);
523}
524
525module_init(bts_init);
diff --git a/arch/x86/kernel/cpu/perf_event_intel_cqm.c b/arch/x86/kernel/cpu/perf_event_intel_cqm.c
new file mode 100644
index 000000000000..e4d1b8b738fa
--- /dev/null
+++ b/arch/x86/kernel/cpu/perf_event_intel_cqm.c
@@ -0,0 +1,1379 @@
1/*
2 * Intel Cache Quality-of-Service Monitoring (CQM) support.
3 *
4 * Based very, very heavily on work by Peter Zijlstra.
5 */
6
7#include <linux/perf_event.h>
8#include <linux/slab.h>
9#include <asm/cpu_device_id.h>
10#include "perf_event.h"
11
12#define MSR_IA32_PQR_ASSOC 0x0c8f
13#define MSR_IA32_QM_CTR 0x0c8e
14#define MSR_IA32_QM_EVTSEL 0x0c8d
15
16static unsigned int cqm_max_rmid = -1;
17static unsigned int cqm_l3_scale; /* supposedly cacheline size */
18
19struct intel_cqm_state {
20 raw_spinlock_t lock;
21 int rmid;
22 int cnt;
23};
24
25static DEFINE_PER_CPU(struct intel_cqm_state, cqm_state);
26
27/*
28 * Protects cache_cgroups and cqm_rmid_free_lru and cqm_rmid_limbo_lru.
29 * Also protects event->hw.cqm_rmid
30 *
31 * Hold either for stability, both for modification of ->hw.cqm_rmid.
32 */
33static DEFINE_MUTEX(cache_mutex);
34static DEFINE_RAW_SPINLOCK(cache_lock);
35
36/*
37 * Groups of events that have the same target(s), one RMID per group.
38 */
39static LIST_HEAD(cache_groups);
40
41/*
42 * Mask of CPUs for reading CQM values. We only need one per-socket.
43 */
44static cpumask_t cqm_cpumask;
45
46#define RMID_VAL_ERROR (1ULL << 63)
47#define RMID_VAL_UNAVAIL (1ULL << 62)
48
49#define QOS_L3_OCCUP_EVENT_ID (1 << 0)
50
51#define QOS_EVENT_MASK QOS_L3_OCCUP_EVENT_ID
52
53/*
54 * This is central to the rotation algorithm in __intel_cqm_rmid_rotate().
55 *
56 * This rmid is always free and is guaranteed to have an associated
57 * near-zero occupancy value, i.e. no cachelines are tagged with this
58 * RMID, once __intel_cqm_rmid_rotate() returns.
59 */
60static unsigned int intel_cqm_rotation_rmid;
61
62#define INVALID_RMID (-1)
63
64/*
65 * Is @rmid valid for programming the hardware?
66 *
67 * rmid 0 is reserved by the hardware for all non-monitored tasks, which
68 * means that we should never come across an rmid with that value.
69 * Likewise, an rmid value of -1 is used to indicate "no rmid currently
70 * assigned" and is used as part of the rotation code.
71 */
72static inline bool __rmid_valid(unsigned int rmid)
73{
74 if (!rmid || rmid == INVALID_RMID)
75 return false;
76
77 return true;
78}
79
80static u64 __rmid_read(unsigned int rmid)
81{
82 u64 val;
83
84 /*
85 * Ignore the SDM, this thing is _NOTHING_ like a regular perfcnt,
86 * it just says that to increase confusion.
87 */
88 wrmsr(MSR_IA32_QM_EVTSEL, QOS_L3_OCCUP_EVENT_ID, rmid);
89 rdmsrl(MSR_IA32_QM_CTR, val);
90
91 /*
92 * Aside from the ERROR and UNAVAIL bits, assume this thing returns
93 * the number of cachelines tagged with @rmid.
94 */
95 return val;
96}
97
98enum rmid_recycle_state {
99 RMID_YOUNG = 0,
100 RMID_AVAILABLE,
101 RMID_DIRTY,
102};
103
104struct cqm_rmid_entry {
105 unsigned int rmid;
106 enum rmid_recycle_state state;
107 struct list_head list;
108 unsigned long queue_time;
109};
110
111/*
112 * cqm_rmid_free_lru - A least recently used list of RMIDs.
113 *
114 * Oldest entry at the head, newest (most recently used) entry at the
115 * tail. This list is never traversed, it's only used to keep track of
116 * the lru order. That is, we only pick entries of the head or insert
117 * them on the tail.
118 *
119 * All entries on the list are 'free', and their RMIDs are not currently
120 * in use. To mark an RMID as in use, remove its entry from the lru
121 * list.
122 *
123 *
124 * cqm_rmid_limbo_lru - list of currently unused but (potentially) dirty RMIDs.
125 *
126 * This list is contains RMIDs that no one is currently using but that
127 * may have a non-zero occupancy value associated with them. The
128 * rotation worker moves RMIDs from the limbo list to the free list once
129 * the occupancy value drops below __intel_cqm_threshold.
130 *
131 * Both lists are protected by cache_mutex.
132 */
133static LIST_HEAD(cqm_rmid_free_lru);
134static LIST_HEAD(cqm_rmid_limbo_lru);
135
136/*
137 * We use a simple array of pointers so that we can lookup a struct
138 * cqm_rmid_entry in O(1). This alleviates the callers of __get_rmid()
139 * and __put_rmid() from having to worry about dealing with struct
140 * cqm_rmid_entry - they just deal with rmids, i.e. integers.
141 *
142 * Once this array is initialized it is read-only. No locks are required
143 * to access it.
144 *
145 * All entries for all RMIDs can be looked up in the this array at all
146 * times.
147 */
148static struct cqm_rmid_entry **cqm_rmid_ptrs;
149
150static inline struct cqm_rmid_entry *__rmid_entry(int rmid)
151{
152 struct cqm_rmid_entry *entry;
153
154 entry = cqm_rmid_ptrs[rmid];
155 WARN_ON(entry->rmid != rmid);
156
157 return entry;
158}
159
160/*
161 * Returns < 0 on fail.
162 *
163 * We expect to be called with cache_mutex held.
164 */
165static int __get_rmid(void)
166{
167 struct cqm_rmid_entry *entry;
168
169 lockdep_assert_held(&cache_mutex);
170
171 if (list_empty(&cqm_rmid_free_lru))
172 return INVALID_RMID;
173
174 entry = list_first_entry(&cqm_rmid_free_lru, struct cqm_rmid_entry, list);
175 list_del(&entry->list);
176
177 return entry->rmid;
178}
179
180static void __put_rmid(unsigned int rmid)
181{
182 struct cqm_rmid_entry *entry;
183
184 lockdep_assert_held(&cache_mutex);
185
186 WARN_ON(!__rmid_valid(rmid));
187 entry = __rmid_entry(rmid);
188
189 entry->queue_time = jiffies;
190 entry->state = RMID_YOUNG;
191
192 list_add_tail(&entry->list, &cqm_rmid_limbo_lru);
193}
194
195static int intel_cqm_setup_rmid_cache(void)
196{
197 struct cqm_rmid_entry *entry;
198 unsigned int nr_rmids;
199 int r = 0;
200
201 nr_rmids = cqm_max_rmid + 1;
202 cqm_rmid_ptrs = kmalloc(sizeof(struct cqm_rmid_entry *) *
203 nr_rmids, GFP_KERNEL);
204 if (!cqm_rmid_ptrs)
205 return -ENOMEM;
206
207 for (; r <= cqm_max_rmid; r++) {
208 struct cqm_rmid_entry *entry;
209
210 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
211 if (!entry)
212 goto fail;
213
214 INIT_LIST_HEAD(&entry->list);
215 entry->rmid = r;
216 cqm_rmid_ptrs[r] = entry;
217
218 list_add_tail(&entry->list, &cqm_rmid_free_lru);
219 }
220
221 /*
222 * RMID 0 is special and is always allocated. It's used for all
223 * tasks that are not monitored.
224 */
225 entry = __rmid_entry(0);
226 list_del(&entry->list);
227
228 mutex_lock(&cache_mutex);
229 intel_cqm_rotation_rmid = __get_rmid();
230 mutex_unlock(&cache_mutex);
231
232 return 0;
233fail:
234 while (r--)
235 kfree(cqm_rmid_ptrs[r]);
236
237 kfree(cqm_rmid_ptrs);
238 return -ENOMEM;
239}
240
241/*
242 * Determine if @a and @b measure the same set of tasks.
243 *
244 * If @a and @b measure the same set of tasks then we want to share a
245 * single RMID.
246 */
247static bool __match_event(struct perf_event *a, struct perf_event *b)
248{
249 /* Per-cpu and task events don't mix */
250 if ((a->attach_state & PERF_ATTACH_TASK) !=
251 (b->attach_state & PERF_ATTACH_TASK))
252 return false;
253
254#ifdef CONFIG_CGROUP_PERF
255 if (a->cgrp != b->cgrp)
256 return false;
257#endif
258
259 /* If not task event, we're machine wide */
260 if (!(b->attach_state & PERF_ATTACH_TASK))
261 return true;
262
263 /*
264 * Events that target same task are placed into the same cache group.
265 */
266 if (a->hw.target == b->hw.target)
267 return true;
268
269 /*
270 * Are we an inherited event?
271 */
272 if (b->parent == a)
273 return true;
274
275 return false;
276}
277
278#ifdef CONFIG_CGROUP_PERF
279static inline struct perf_cgroup *event_to_cgroup(struct perf_event *event)
280{
281 if (event->attach_state & PERF_ATTACH_TASK)
282 return perf_cgroup_from_task(event->hw.target);
283
284 return event->cgrp;
285}
286#endif
287
288/*
289 * Determine if @a's tasks intersect with @b's tasks
290 *
291 * There are combinations of events that we explicitly prohibit,
292 *
293 * PROHIBITS
294 * system-wide -> cgroup and task
295 * cgroup -> system-wide
296 * -> task in cgroup
297 * task -> system-wide
298 * -> task in cgroup
299 *
300 * Call this function before allocating an RMID.
301 */
302static bool __conflict_event(struct perf_event *a, struct perf_event *b)
303{
304#ifdef CONFIG_CGROUP_PERF
305 /*
306 * We can have any number of cgroups but only one system-wide
307 * event at a time.
308 */
309 if (a->cgrp && b->cgrp) {
310 struct perf_cgroup *ac = a->cgrp;
311 struct perf_cgroup *bc = b->cgrp;
312
313 /*
314 * This condition should have been caught in
315 * __match_event() and we should be sharing an RMID.
316 */
317 WARN_ON_ONCE(ac == bc);
318
319 if (cgroup_is_descendant(ac->css.cgroup, bc->css.cgroup) ||
320 cgroup_is_descendant(bc->css.cgroup, ac->css.cgroup))
321 return true;
322
323 return false;
324 }
325
326 if (a->cgrp || b->cgrp) {
327 struct perf_cgroup *ac, *bc;
328
329 /*
330 * cgroup and system-wide events are mutually exclusive
331 */
332 if ((a->cgrp && !(b->attach_state & PERF_ATTACH_TASK)) ||
333 (b->cgrp && !(a->attach_state & PERF_ATTACH_TASK)))
334 return true;
335
336 /*
337 * Ensure neither event is part of the other's cgroup
338 */
339 ac = event_to_cgroup(a);
340 bc = event_to_cgroup(b);
341 if (ac == bc)
342 return true;
343
344 /*
345 * Must have cgroup and non-intersecting task events.
346 */
347 if (!ac || !bc)
348 return false;
349
350 /*
351 * We have cgroup and task events, and the task belongs
352 * to a cgroup. Check for for overlap.
353 */
354 if (cgroup_is_descendant(ac->css.cgroup, bc->css.cgroup) ||
355 cgroup_is_descendant(bc->css.cgroup, ac->css.cgroup))
356 return true;
357
358 return false;
359 }
360#endif
361 /*
362 * If one of them is not a task, same story as above with cgroups.
363 */
364 if (!(a->attach_state & PERF_ATTACH_TASK) ||
365 !(b->attach_state & PERF_ATTACH_TASK))
366 return true;
367
368 /*
369 * Must be non-overlapping.
370 */
371 return false;
372}
373
374struct rmid_read {
375 unsigned int rmid;
376 atomic64_t value;
377};
378
379static void __intel_cqm_event_count(void *info);
380
381/*
382 * Exchange the RMID of a group of events.
383 */
384static unsigned int
385intel_cqm_xchg_rmid(struct perf_event *group, unsigned int rmid)
386{
387 struct perf_event *event;
388 unsigned int old_rmid = group->hw.cqm_rmid;
389 struct list_head *head = &group->hw.cqm_group_entry;
390
391 lockdep_assert_held(&cache_mutex);
392
393 /*
394 * If our RMID is being deallocated, perform a read now.
395 */
396 if (__rmid_valid(old_rmid) && !__rmid_valid(rmid)) {
397 struct rmid_read rr = {
398 .value = ATOMIC64_INIT(0),
399 .rmid = old_rmid,
400 };
401
402 on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count,
403 &rr, 1);
404 local64_set(&group->count, atomic64_read(&rr.value));
405 }
406
407 raw_spin_lock_irq(&cache_lock);
408
409 group->hw.cqm_rmid = rmid;
410 list_for_each_entry(event, head, hw.cqm_group_entry)
411 event->hw.cqm_rmid = rmid;
412
413 raw_spin_unlock_irq(&cache_lock);
414
415 return old_rmid;
416}
417
418/*
419 * If we fail to assign a new RMID for intel_cqm_rotation_rmid because
420 * cachelines are still tagged with RMIDs in limbo, we progressively
421 * increment the threshold until we find an RMID in limbo with <=
422 * __intel_cqm_threshold lines tagged. This is designed to mitigate the
423 * problem where cachelines tagged with an RMID are not steadily being
424 * evicted.
425 *
426 * On successful rotations we decrease the threshold back towards zero.
427 *
428 * __intel_cqm_max_threshold provides an upper bound on the threshold,
429 * and is measured in bytes because it's exposed to userland.
430 */
431static unsigned int __intel_cqm_threshold;
432static unsigned int __intel_cqm_max_threshold;
433
434/*
435 * Test whether an RMID has a zero occupancy value on this cpu.
436 */
437static void intel_cqm_stable(void *arg)
438{
439 struct cqm_rmid_entry *entry;
440
441 list_for_each_entry(entry, &cqm_rmid_limbo_lru, list) {
442 if (entry->state != RMID_AVAILABLE)
443 break;
444
445 if (__rmid_read(entry->rmid) > __intel_cqm_threshold)
446 entry->state = RMID_DIRTY;
447 }
448}
449
450/*
451 * If we have group events waiting for an RMID that don't conflict with
452 * events already running, assign @rmid.
453 */
454static bool intel_cqm_sched_in_event(unsigned int rmid)
455{
456 struct perf_event *leader, *event;
457
458 lockdep_assert_held(&cache_mutex);
459
460 leader = list_first_entry(&cache_groups, struct perf_event,
461 hw.cqm_groups_entry);
462 event = leader;
463
464 list_for_each_entry_continue(event, &cache_groups,
465 hw.cqm_groups_entry) {
466 if (__rmid_valid(event->hw.cqm_rmid))
467 continue;
468
469 if (__conflict_event(event, leader))
470 continue;
471
472 intel_cqm_xchg_rmid(event, rmid);
473 return true;
474 }
475
476 return false;
477}
478
479/*
480 * Initially use this constant for both the limbo queue time and the
481 * rotation timer interval, pmu::hrtimer_interval_ms.
482 *
483 * They don't need to be the same, but the two are related since if you
484 * rotate faster than you recycle RMIDs, you may run out of available
485 * RMIDs.
486 */
487#define RMID_DEFAULT_QUEUE_TIME 250 /* ms */
488
489static unsigned int __rmid_queue_time_ms = RMID_DEFAULT_QUEUE_TIME;
490
491/*
492 * intel_cqm_rmid_stabilize - move RMIDs from limbo to free list
493 * @nr_available: number of freeable RMIDs on the limbo list
494 *
495 * Quiescent state; wait for all 'freed' RMIDs to become unused, i.e. no
496 * cachelines are tagged with those RMIDs. After this we can reuse them
497 * and know that the current set of active RMIDs is stable.
498 *
499 * Return %true or %false depending on whether stabilization needs to be
500 * reattempted.
501 *
502 * If we return %true then @nr_available is updated to indicate the
503 * number of RMIDs on the limbo list that have been queued for the
504 * minimum queue time (RMID_AVAILABLE), but whose data occupancy values
505 * are above __intel_cqm_threshold.
506 */
507static bool intel_cqm_rmid_stabilize(unsigned int *available)
508{
509 struct cqm_rmid_entry *entry, *tmp;
510
511 lockdep_assert_held(&cache_mutex);
512
513 *available = 0;
514 list_for_each_entry(entry, &cqm_rmid_limbo_lru, list) {
515 unsigned long min_queue_time;
516 unsigned long now = jiffies;
517
518 /*
519 * We hold RMIDs placed into limbo for a minimum queue
520 * time. Before the minimum queue time has elapsed we do
521 * not recycle RMIDs.
522 *
523 * The reasoning is that until a sufficient time has
524 * passed since we stopped using an RMID, any RMID
525 * placed onto the limbo list will likely still have
526 * data tagged in the cache, which means we'll probably
527 * fail to recycle it anyway.
528 *
529 * We can save ourselves an expensive IPI by skipping
530 * any RMIDs that have not been queued for the minimum
531 * time.
532 */
533 min_queue_time = entry->queue_time +
534 msecs_to_jiffies(__rmid_queue_time_ms);
535
536 if (time_after(min_queue_time, now))
537 break;
538
539 entry->state = RMID_AVAILABLE;
540 (*available)++;
541 }
542
543 /*
544 * Fast return if none of the RMIDs on the limbo list have been
545 * sitting on the queue for the minimum queue time.
546 */
547 if (!*available)
548 return false;
549
550 /*
551 * Test whether an RMID is free for each package.
552 */
553 on_each_cpu_mask(&cqm_cpumask, intel_cqm_stable, NULL, true);
554
555 list_for_each_entry_safe(entry, tmp, &cqm_rmid_limbo_lru, list) {
556 /*
557 * Exhausted all RMIDs that have waited min queue time.
558 */
559 if (entry->state == RMID_YOUNG)
560 break;
561
562 if (entry->state == RMID_DIRTY)
563 continue;
564
565 list_del(&entry->list); /* remove from limbo */
566
567 /*
568 * The rotation RMID gets priority if it's
569 * currently invalid. In which case, skip adding
570 * the RMID to the the free lru.
571 */
572 if (!__rmid_valid(intel_cqm_rotation_rmid)) {
573 intel_cqm_rotation_rmid = entry->rmid;
574 continue;
575 }
576
577 /*
578 * If we have groups waiting for RMIDs, hand
579 * them one now provided they don't conflict.
580 */
581 if (intel_cqm_sched_in_event(entry->rmid))
582 continue;
583
584 /*
585 * Otherwise place it onto the free list.
586 */
587 list_add_tail(&entry->list, &cqm_rmid_free_lru);
588 }
589
590
591 return __rmid_valid(intel_cqm_rotation_rmid);
592}
593
594/*
595 * Pick a victim group and move it to the tail of the group list.
596 * @next: The first group without an RMID
597 */
598static void __intel_cqm_pick_and_rotate(struct perf_event *next)
599{
600 struct perf_event *rotor;
601 unsigned int rmid;
602
603 lockdep_assert_held(&cache_mutex);
604
605 rotor = list_first_entry(&cache_groups, struct perf_event,
606 hw.cqm_groups_entry);
607
608 /*
609 * The group at the front of the list should always have a valid
610 * RMID. If it doesn't then no groups have RMIDs assigned and we
611 * don't need to rotate the list.
612 */
613 if (next == rotor)
614 return;
615
616 rmid = intel_cqm_xchg_rmid(rotor, INVALID_RMID);
617 __put_rmid(rmid);
618
619 list_rotate_left(&cache_groups);
620}
621
622/*
623 * Deallocate the RMIDs from any events that conflict with @event, and
624 * place them on the back of the group list.
625 */
626static void intel_cqm_sched_out_conflicting_events(struct perf_event *event)
627{
628 struct perf_event *group, *g;
629 unsigned int rmid;
630
631 lockdep_assert_held(&cache_mutex);
632
633 list_for_each_entry_safe(group, g, &cache_groups, hw.cqm_groups_entry) {
634 if (group == event)
635 continue;
636
637 rmid = group->hw.cqm_rmid;
638
639 /*
640 * Skip events that don't have a valid RMID.
641 */
642 if (!__rmid_valid(rmid))
643 continue;
644
645 /*
646 * No conflict? No problem! Leave the event alone.
647 */
648 if (!__conflict_event(group, event))
649 continue;
650
651 intel_cqm_xchg_rmid(group, INVALID_RMID);
652 __put_rmid(rmid);
653 }
654}
655
656/*
657 * Attempt to rotate the groups and assign new RMIDs.
658 *
659 * We rotate for two reasons,
660 * 1. To handle the scheduling of conflicting events
661 * 2. To recycle RMIDs
662 *
663 * Rotating RMIDs is complicated because the hardware doesn't give us
664 * any clues.
665 *
666 * There's problems with the hardware interface; when you change the
667 * task:RMID map cachelines retain their 'old' tags, giving a skewed
668 * picture. In order to work around this, we must always keep one free
669 * RMID - intel_cqm_rotation_rmid.
670 *
671 * Rotation works by taking away an RMID from a group (the old RMID),
672 * and assigning the free RMID to another group (the new RMID). We must
673 * then wait for the old RMID to not be used (no cachelines tagged).
674 * This ensure that all cachelines are tagged with 'active' RMIDs. At
675 * this point we can start reading values for the new RMID and treat the
676 * old RMID as the free RMID for the next rotation.
677 *
678 * Return %true or %false depending on whether we did any rotating.
679 */
680static bool __intel_cqm_rmid_rotate(void)
681{
682 struct perf_event *group, *start = NULL;
683 unsigned int threshold_limit;
684 unsigned int nr_needed = 0;
685 unsigned int nr_available;
686 bool rotated = false;
687
688 mutex_lock(&cache_mutex);
689
690again:
691 /*
692 * Fast path through this function if there are no groups and no
693 * RMIDs that need cleaning.
694 */
695 if (list_empty(&cache_groups) && list_empty(&cqm_rmid_limbo_lru))
696 goto out;
697
698 list_for_each_entry(group, &cache_groups, hw.cqm_groups_entry) {
699 if (!__rmid_valid(group->hw.cqm_rmid)) {
700 if (!start)
701 start = group;
702 nr_needed++;
703 }
704 }
705
706 /*
707 * We have some event groups, but they all have RMIDs assigned
708 * and no RMIDs need cleaning.
709 */
710 if (!nr_needed && list_empty(&cqm_rmid_limbo_lru))
711 goto out;
712
713 if (!nr_needed)
714 goto stabilize;
715
716 /*
717 * We have more event groups without RMIDs than available RMIDs,
718 * or we have event groups that conflict with the ones currently
719 * scheduled.
720 *
721 * We force deallocate the rmid of the group at the head of
722 * cache_groups. The first event group without an RMID then gets
723 * assigned intel_cqm_rotation_rmid. This ensures we always make
724 * forward progress.
725 *
726 * Rotate the cache_groups list so the previous head is now the
727 * tail.
728 */
729 __intel_cqm_pick_and_rotate(start);
730
731 /*
732 * If the rotation is going to succeed, reduce the threshold so
733 * that we don't needlessly reuse dirty RMIDs.
734 */
735 if (__rmid_valid(intel_cqm_rotation_rmid)) {
736 intel_cqm_xchg_rmid(start, intel_cqm_rotation_rmid);
737 intel_cqm_rotation_rmid = __get_rmid();
738
739 intel_cqm_sched_out_conflicting_events(start);
740
741 if (__intel_cqm_threshold)
742 __intel_cqm_threshold--;
743 }
744
745 rotated = true;
746
747stabilize:
748 /*
749 * We now need to stablize the RMID we freed above (if any) to
750 * ensure that the next time we rotate we have an RMID with zero
751 * occupancy value.
752 *
753 * Alternatively, if we didn't need to perform any rotation,
754 * we'll have a bunch of RMIDs in limbo that need stabilizing.
755 */
756 threshold_limit = __intel_cqm_max_threshold / cqm_l3_scale;
757
758 while (intel_cqm_rmid_stabilize(&nr_available) &&
759 __intel_cqm_threshold < threshold_limit) {
760 unsigned int steal_limit;
761
762 /*
763 * Don't spin if nobody is actively waiting for an RMID,
764 * the rotation worker will be kicked as soon as an
765 * event needs an RMID anyway.
766 */
767 if (!nr_needed)
768 break;
769
770 /* Allow max 25% of RMIDs to be in limbo. */
771 steal_limit = (cqm_max_rmid + 1) / 4;
772
773 /*
774 * We failed to stabilize any RMIDs so our rotation
775 * logic is now stuck. In order to make forward progress
776 * we have a few options:
777 *
778 * 1. rotate ("steal") another RMID
779 * 2. increase the threshold
780 * 3. do nothing
781 *
782 * We do both of 1. and 2. until we hit the steal limit.
783 *
784 * The steal limit prevents all RMIDs ending up on the
785 * limbo list. This can happen if every RMID has a
786 * non-zero occupancy above threshold_limit, and the
787 * occupancy values aren't dropping fast enough.
788 *
789 * Note that there is prioritisation at work here - we'd
790 * rather increase the number of RMIDs on the limbo list
791 * than increase the threshold, because increasing the
792 * threshold skews the event data (because we reuse
793 * dirty RMIDs) - threshold bumps are a last resort.
794 */
795 if (nr_available < steal_limit)
796 goto again;
797
798 __intel_cqm_threshold++;
799 }
800
801out:
802 mutex_unlock(&cache_mutex);
803 return rotated;
804}
805
806static void intel_cqm_rmid_rotate(struct work_struct *work);
807
808static DECLARE_DELAYED_WORK(intel_cqm_rmid_work, intel_cqm_rmid_rotate);
809
810static struct pmu intel_cqm_pmu;
811
812static void intel_cqm_rmid_rotate(struct work_struct *work)
813{
814 unsigned long delay;
815
816 __intel_cqm_rmid_rotate();
817
818 delay = msecs_to_jiffies(intel_cqm_pmu.hrtimer_interval_ms);
819 schedule_delayed_work(&intel_cqm_rmid_work, delay);
820}
821
822/*
823 * Find a group and setup RMID.
824 *
825 * If we're part of a group, we use the group's RMID.
826 */
827static void intel_cqm_setup_event(struct perf_event *event,
828 struct perf_event **group)
829{
830 struct perf_event *iter;
831 unsigned int rmid;
832 bool conflict = false;
833
834 list_for_each_entry(iter, &cache_groups, hw.cqm_groups_entry) {
835 rmid = iter->hw.cqm_rmid;
836
837 if (__match_event(iter, event)) {
838 /* All tasks in a group share an RMID */
839 event->hw.cqm_rmid = rmid;
840 *group = iter;
841 return;
842 }
843
844 /*
845 * We only care about conflicts for events that are
846 * actually scheduled in (and hence have a valid RMID).
847 */
848 if (__conflict_event(iter, event) && __rmid_valid(rmid))
849 conflict = true;
850 }
851
852 if (conflict)
853 rmid = INVALID_RMID;
854 else
855 rmid = __get_rmid();
856
857 event->hw.cqm_rmid = rmid;
858}
859
860static void intel_cqm_event_read(struct perf_event *event)
861{
862 unsigned long flags;
863 unsigned int rmid;
864 u64 val;
865
866 /*
867 * Task events are handled by intel_cqm_event_count().
868 */
869 if (event->cpu == -1)
870 return;
871
872 raw_spin_lock_irqsave(&cache_lock, flags);
873 rmid = event->hw.cqm_rmid;
874
875 if (!__rmid_valid(rmid))
876 goto out;
877
878 val = __rmid_read(rmid);
879
880 /*
881 * Ignore this reading on error states and do not update the value.
882 */
883 if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
884 goto out;
885
886 local64_set(&event->count, val);
887out:
888 raw_spin_unlock_irqrestore(&cache_lock, flags);
889}
890
891static void __intel_cqm_event_count(void *info)
892{
893 struct rmid_read *rr = info;
894 u64 val;
895
896 val = __rmid_read(rr->rmid);
897
898 if (val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL))
899 return;
900
901 atomic64_add(val, &rr->value);
902}
903
904static inline bool cqm_group_leader(struct perf_event *event)
905{
906 return !list_empty(&event->hw.cqm_groups_entry);
907}
908
909static u64 intel_cqm_event_count(struct perf_event *event)
910{
911 unsigned long flags;
912 struct rmid_read rr = {
913 .value = ATOMIC64_INIT(0),
914 };
915
916 /*
917 * We only need to worry about task events. System-wide events
918 * are handled like usual, i.e. entirely with
919 * intel_cqm_event_read().
920 */
921 if (event->cpu != -1)
922 return __perf_event_count(event);
923
924 /*
925 * Only the group leader gets to report values. This stops us
926 * reporting duplicate values to userspace, and gives us a clear
927 * rule for which task gets to report the values.
928 *
929 * Note that it is impossible to attribute these values to
930 * specific packages - we forfeit that ability when we create
931 * task events.
932 */
933 if (!cqm_group_leader(event))
934 return 0;
935
936 /*
937 * Notice that we don't perform the reading of an RMID
938 * atomically, because we can't hold a spin lock across the
939 * IPIs.
940 *
941 * Speculatively perform the read, since @event might be
942 * assigned a different (possibly invalid) RMID while we're
943 * busying performing the IPI calls. It's therefore necessary to
944 * check @event's RMID afterwards, and if it has changed,
945 * discard the result of the read.
946 */
947 rr.rmid = ACCESS_ONCE(event->hw.cqm_rmid);
948
949 if (!__rmid_valid(rr.rmid))
950 goto out;
951
952 on_each_cpu_mask(&cqm_cpumask, __intel_cqm_event_count, &rr, 1);
953
954 raw_spin_lock_irqsave(&cache_lock, flags);
955 if (event->hw.cqm_rmid == rr.rmid)
956 local64_set(&event->count, atomic64_read(&rr.value));
957 raw_spin_unlock_irqrestore(&cache_lock, flags);
958out:
959 return __perf_event_count(event);
960}
961
962static void intel_cqm_event_start(struct perf_event *event, int mode)
963{
964 struct intel_cqm_state *state = this_cpu_ptr(&cqm_state);
965 unsigned int rmid = event->hw.cqm_rmid;
966 unsigned long flags;
967
968 if (!(event->hw.cqm_state & PERF_HES_STOPPED))
969 return;
970
971 event->hw.cqm_state &= ~PERF_HES_STOPPED;
972
973 raw_spin_lock_irqsave(&state->lock, flags);
974
975 if (state->cnt++)
976 WARN_ON_ONCE(state->rmid != rmid);
977 else
978 WARN_ON_ONCE(state->rmid);
979
980 state->rmid = rmid;
981 wrmsrl(MSR_IA32_PQR_ASSOC, state->rmid);
982
983 raw_spin_unlock_irqrestore(&state->lock, flags);
984}
985
986static void intel_cqm_event_stop(struct perf_event *event, int mode)
987{
988 struct intel_cqm_state *state = this_cpu_ptr(&cqm_state);
989 unsigned long flags;
990
991 if (event->hw.cqm_state & PERF_HES_STOPPED)
992 return;
993
994 event->hw.cqm_state |= PERF_HES_STOPPED;
995
996 raw_spin_lock_irqsave(&state->lock, flags);
997 intel_cqm_event_read(event);
998
999 if (!--state->cnt) {
1000 state->rmid = 0;
1001 wrmsrl(MSR_IA32_PQR_ASSOC, 0);
1002 } else {
1003 WARN_ON_ONCE(!state->rmid);
1004 }
1005
1006 raw_spin_unlock_irqrestore(&state->lock, flags);
1007}
1008
1009static int intel_cqm_event_add(struct perf_event *event, int mode)
1010{
1011 unsigned long flags;
1012 unsigned int rmid;
1013
1014 raw_spin_lock_irqsave(&cache_lock, flags);
1015
1016 event->hw.cqm_state = PERF_HES_STOPPED;
1017 rmid = event->hw.cqm_rmid;
1018
1019 if (__rmid_valid(rmid) && (mode & PERF_EF_START))
1020 intel_cqm_event_start(event, mode);
1021
1022 raw_spin_unlock_irqrestore(&cache_lock, flags);
1023
1024 return 0;
1025}
1026
1027static void intel_cqm_event_del(struct perf_event *event, int mode)
1028{
1029 intel_cqm_event_stop(event, mode);
1030}
1031
1032static void intel_cqm_event_destroy(struct perf_event *event)
1033{
1034 struct perf_event *group_other = NULL;
1035
1036 mutex_lock(&cache_mutex);
1037
1038 /*
1039 * If there's another event in this group...
1040 */
1041 if (!list_empty(&event->hw.cqm_group_entry)) {
1042 group_other = list_first_entry(&event->hw.cqm_group_entry,
1043 struct perf_event,
1044 hw.cqm_group_entry);
1045 list_del(&event->hw.cqm_group_entry);
1046 }
1047
1048 /*
1049 * And we're the group leader..
1050 */
1051 if (cqm_group_leader(event)) {
1052 /*
1053 * If there was a group_other, make that leader, otherwise
1054 * destroy the group and return the RMID.
1055 */
1056 if (group_other) {
1057 list_replace(&event->hw.cqm_groups_entry,
1058 &group_other->hw.cqm_groups_entry);
1059 } else {
1060 unsigned int rmid = event->hw.cqm_rmid;
1061
1062 if (__rmid_valid(rmid))
1063 __put_rmid(rmid);
1064 list_del(&event->hw.cqm_groups_entry);
1065 }
1066 }
1067
1068 mutex_unlock(&cache_mutex);
1069}
1070
1071static int intel_cqm_event_init(struct perf_event *event)
1072{
1073 struct perf_event *group = NULL;
1074 bool rotate = false;
1075
1076 if (event->attr.type != intel_cqm_pmu.type)
1077 return -ENOENT;
1078
1079 if (event->attr.config & ~QOS_EVENT_MASK)
1080 return -EINVAL;
1081
1082 /* unsupported modes and filters */
1083 if (event->attr.exclude_user ||
1084 event->attr.exclude_kernel ||
1085 event->attr.exclude_hv ||
1086 event->attr.exclude_idle ||
1087 event->attr.exclude_host ||
1088 event->attr.exclude_guest ||
1089 event->attr.sample_period) /* no sampling */
1090 return -EINVAL;
1091
1092 INIT_LIST_HEAD(&event->hw.cqm_group_entry);
1093 INIT_LIST_HEAD(&event->hw.cqm_groups_entry);
1094
1095 event->destroy = intel_cqm_event_destroy;
1096
1097 mutex_lock(&cache_mutex);
1098
1099 /* Will also set rmid */
1100 intel_cqm_setup_event(event, &group);
1101
1102 if (group) {
1103 list_add_tail(&event->hw.cqm_group_entry,
1104 &group->hw.cqm_group_entry);
1105 } else {
1106 list_add_tail(&event->hw.cqm_groups_entry,
1107 &cache_groups);
1108
1109 /*
1110 * All RMIDs are either in use or have recently been
1111 * used. Kick the rotation worker to clean/free some.
1112 *
1113 * We only do this for the group leader, rather than for
1114 * every event in a group to save on needless work.
1115 */
1116 if (!__rmid_valid(event->hw.cqm_rmid))
1117 rotate = true;
1118 }
1119
1120 mutex_unlock(&cache_mutex);
1121
1122 if (rotate)
1123 schedule_delayed_work(&intel_cqm_rmid_work, 0);
1124
1125 return 0;
1126}
1127
1128EVENT_ATTR_STR(llc_occupancy, intel_cqm_llc, "event=0x01");
1129EVENT_ATTR_STR(llc_occupancy.per-pkg, intel_cqm_llc_pkg, "1");
1130EVENT_ATTR_STR(llc_occupancy.unit, intel_cqm_llc_unit, "Bytes");
1131EVENT_ATTR_STR(llc_occupancy.scale, intel_cqm_llc_scale, NULL);
1132EVENT_ATTR_STR(llc_occupancy.snapshot, intel_cqm_llc_snapshot, "1");
1133
1134static struct attribute *intel_cqm_events_attr[] = {
1135 EVENT_PTR(intel_cqm_llc),
1136 EVENT_PTR(intel_cqm_llc_pkg),
1137 EVENT_PTR(intel_cqm_llc_unit),
1138 EVENT_PTR(intel_cqm_llc_scale),
1139 EVENT_PTR(intel_cqm_llc_snapshot),
1140 NULL,
1141};
1142
1143static struct attribute_group intel_cqm_events_group = {
1144 .name = "events",
1145 .attrs = intel_cqm_events_attr,
1146};
1147
1148PMU_FORMAT_ATTR(event, "config:0-7");
1149static struct attribute *intel_cqm_formats_attr[] = {
1150 &format_attr_event.attr,
1151 NULL,
1152};
1153
1154static struct attribute_group intel_cqm_format_group = {
1155 .name = "format",
1156 .attrs = intel_cqm_formats_attr,
1157};
1158
1159static ssize_t
1160max_recycle_threshold_show(struct device *dev, struct device_attribute *attr,
1161 char *page)
1162{
1163 ssize_t rv;
1164
1165 mutex_lock(&cache_mutex);
1166 rv = snprintf(page, PAGE_SIZE-1, "%u\n", __intel_cqm_max_threshold);
1167 mutex_unlock(&cache_mutex);
1168
1169 return rv;
1170}
1171
1172static ssize_t
1173max_recycle_threshold_store(struct device *dev,
1174 struct device_attribute *attr,
1175 const char *buf, size_t count)
1176{
1177 unsigned int bytes, cachelines;
1178 int ret;
1179
1180 ret = kstrtouint(buf, 0, &bytes);
1181 if (ret)
1182 return ret;
1183
1184 mutex_lock(&cache_mutex);
1185
1186 __intel_cqm_max_threshold = bytes;
1187 cachelines = bytes / cqm_l3_scale;
1188
1189 /*
1190 * The new maximum takes effect immediately.
1191 */
1192 if (__intel_cqm_threshold > cachelines)
1193 __intel_cqm_threshold = cachelines;
1194
1195 mutex_unlock(&cache_mutex);
1196
1197 return count;
1198}
1199
1200static DEVICE_ATTR_RW(max_recycle_threshold);
1201
1202static struct attribute *intel_cqm_attrs[] = {
1203 &dev_attr_max_recycle_threshold.attr,
1204 NULL,
1205};
1206
1207static const struct attribute_group intel_cqm_group = {
1208 .attrs = intel_cqm_attrs,
1209};
1210
1211static const struct attribute_group *intel_cqm_attr_groups[] = {
1212 &intel_cqm_events_group,
1213 &intel_cqm_format_group,
1214 &intel_cqm_group,
1215 NULL,
1216};
1217
1218static struct pmu intel_cqm_pmu = {
1219 .hrtimer_interval_ms = RMID_DEFAULT_QUEUE_TIME,
1220 .attr_groups = intel_cqm_attr_groups,
1221 .task_ctx_nr = perf_sw_context,
1222 .event_init = intel_cqm_event_init,
1223 .add = intel_cqm_event_add,
1224 .del = intel_cqm_event_del,
1225 .start = intel_cqm_event_start,
1226 .stop = intel_cqm_event_stop,
1227 .read = intel_cqm_event_read,
1228 .count = intel_cqm_event_count,
1229};
1230
1231static inline void cqm_pick_event_reader(int cpu)
1232{
1233 int phys_id = topology_physical_package_id(cpu);
1234 int i;
1235
1236 for_each_cpu(i, &cqm_cpumask) {
1237 if (phys_id == topology_physical_package_id(i))
1238 return; /* already got reader for this socket */
1239 }
1240
1241 cpumask_set_cpu(cpu, &cqm_cpumask);
1242}
1243
1244static void intel_cqm_cpu_prepare(unsigned int cpu)
1245{
1246 struct intel_cqm_state *state = &per_cpu(cqm_state, cpu);
1247 struct cpuinfo_x86 *c = &cpu_data(cpu);
1248
1249 raw_spin_lock_init(&state->lock);
1250 state->rmid = 0;
1251 state->cnt = 0;
1252
1253 WARN_ON(c->x86_cache_max_rmid != cqm_max_rmid);
1254 WARN_ON(c->x86_cache_occ_scale != cqm_l3_scale);
1255}
1256
1257static void intel_cqm_cpu_exit(unsigned int cpu)
1258{
1259 int phys_id = topology_physical_package_id(cpu);
1260 int i;
1261
1262 /*
1263 * Is @cpu a designated cqm reader?
1264 */
1265 if (!cpumask_test_and_clear_cpu(cpu, &cqm_cpumask))
1266 return;
1267
1268 for_each_online_cpu(i) {
1269 if (i == cpu)
1270 continue;
1271
1272 if (phys_id == topology_physical_package_id(i)) {
1273 cpumask_set_cpu(i, &cqm_cpumask);
1274 break;
1275 }
1276 }
1277}
1278
1279static int intel_cqm_cpu_notifier(struct notifier_block *nb,
1280 unsigned long action, void *hcpu)
1281{
1282 unsigned int cpu = (unsigned long)hcpu;
1283
1284 switch (action & ~CPU_TASKS_FROZEN) {
1285 case CPU_UP_PREPARE:
1286 intel_cqm_cpu_prepare(cpu);
1287 break;
1288 case CPU_DOWN_PREPARE:
1289 intel_cqm_cpu_exit(cpu);
1290 break;
1291 case CPU_STARTING:
1292 cqm_pick_event_reader(cpu);
1293 break;
1294 }
1295
1296 return NOTIFY_OK;
1297}
1298
1299static const struct x86_cpu_id intel_cqm_match[] = {
1300 { .vendor = X86_VENDOR_INTEL, .feature = X86_FEATURE_CQM_OCCUP_LLC },
1301 {}
1302};
1303
1304static int __init intel_cqm_init(void)
1305{
1306 char *str, scale[20];
1307 int i, cpu, ret;
1308
1309 if (!x86_match_cpu(intel_cqm_match))
1310 return -ENODEV;
1311
1312 cqm_l3_scale = boot_cpu_data.x86_cache_occ_scale;
1313
1314 /*
1315 * It's possible that not all resources support the same number
1316 * of RMIDs. Instead of making scheduling much more complicated
1317 * (where we have to match a task's RMID to a cpu that supports
1318 * that many RMIDs) just find the minimum RMIDs supported across
1319 * all cpus.
1320 *
1321 * Also, check that the scales match on all cpus.
1322 */
1323 cpu_notifier_register_begin();
1324
1325 for_each_online_cpu(cpu) {
1326 struct cpuinfo_x86 *c = &cpu_data(cpu);
1327
1328 if (c->x86_cache_max_rmid < cqm_max_rmid)
1329 cqm_max_rmid = c->x86_cache_max_rmid;
1330
1331 if (c->x86_cache_occ_scale != cqm_l3_scale) {
1332 pr_err("Multiple LLC scale values, disabling\n");
1333 ret = -EINVAL;
1334 goto out;
1335 }
1336 }
1337
1338 /*
1339 * A reasonable upper limit on the max threshold is the number
1340 * of lines tagged per RMID if all RMIDs have the same number of
1341 * lines tagged in the LLC.
1342 *
1343 * For a 35MB LLC and 56 RMIDs, this is ~1.8% of the LLC.
1344 */
1345 __intel_cqm_max_threshold =
1346 boot_cpu_data.x86_cache_size * 1024 / (cqm_max_rmid + 1);
1347
1348 snprintf(scale, sizeof(scale), "%u", cqm_l3_scale);
1349 str = kstrdup(scale, GFP_KERNEL);
1350 if (!str) {
1351 ret = -ENOMEM;
1352 goto out;
1353 }
1354
1355 event_attr_intel_cqm_llc_scale.event_str = str;
1356
1357 ret = intel_cqm_setup_rmid_cache();
1358 if (ret)
1359 goto out;
1360
1361 for_each_online_cpu(i) {
1362 intel_cqm_cpu_prepare(i);
1363 cqm_pick_event_reader(i);
1364 }
1365
1366 __perf_cpu_notifier(intel_cqm_cpu_notifier);
1367
1368 ret = perf_pmu_register(&intel_cqm_pmu, "intel_cqm", -1);
1369 if (ret)
1370 pr_err("Intel CQM perf registration failed: %d\n", ret);
1371 else
1372 pr_info("Intel CQM monitoring enabled\n");
1373
1374out:
1375 cpu_notifier_register_done();
1376
1377 return ret;
1378}
1379device_initcall(intel_cqm_init);
diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c
index 073983398364..ca69ea56c712 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_ds.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c
@@ -461,7 +461,8 @@ void intel_pmu_enable_bts(u64 config)
461 461
462 debugctlmsr |= DEBUGCTLMSR_TR; 462 debugctlmsr |= DEBUGCTLMSR_TR;
463 debugctlmsr |= DEBUGCTLMSR_BTS; 463 debugctlmsr |= DEBUGCTLMSR_BTS;
464 debugctlmsr |= DEBUGCTLMSR_BTINT; 464 if (config & ARCH_PERFMON_EVENTSEL_INT)
465 debugctlmsr |= DEBUGCTLMSR_BTINT;
465 466
466 if (!(config & ARCH_PERFMON_EVENTSEL_OS)) 467 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
467 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; 468 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
@@ -611,6 +612,10 @@ struct event_constraint intel_snb_pebs_event_constraints[] = {
611 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ 612 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
612 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 613 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
613 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 614 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
615 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
616 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
617 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
618 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
614 /* Allow all events as PEBS with no flags */ 619 /* Allow all events as PEBS with no flags */
615 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 620 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
616 EVENT_CONSTRAINT_END 621 EVENT_CONSTRAINT_END
@@ -622,6 +627,10 @@ struct event_constraint intel_ivb_pebs_event_constraints[] = {
622 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ 627 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
623 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 628 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
624 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 629 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
630 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
631 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
632 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
633 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
625 /* Allow all events as PEBS with no flags */ 634 /* Allow all events as PEBS with no flags */
626 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 635 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
627 EVENT_CONSTRAINT_END 636 EVENT_CONSTRAINT_END
@@ -633,16 +642,16 @@ struct event_constraint intel_hsw_pebs_event_constraints[] = {
633 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ 642 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
634 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), 643 INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
635 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ 644 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
636 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ 645 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
637 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ 646 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
638 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ 647 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
639 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ 648 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
640 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ 649 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
641 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ 650 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
642 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ 651 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
643 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ 652 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
644 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ 653 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
645 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ 654 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
646 /* Allow all events as PEBS with no flags */ 655 /* Allow all events as PEBS with no flags */
647 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), 656 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
648 EVENT_CONSTRAINT_END 657 EVENT_CONSTRAINT_END
diff --git a/arch/x86/kernel/cpu/perf_event_intel_lbr.c b/arch/x86/kernel/cpu/perf_event_intel_lbr.c
index 58f1a94beaf0..94e5b506caa6 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_lbr.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_lbr.c
@@ -39,6 +39,7 @@ static enum {
39#define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */ 39#define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
40#define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */ 40#define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
41#define LBR_FAR_BIT 8 /* do not capture far branches */ 41#define LBR_FAR_BIT 8 /* do not capture far branches */
42#define LBR_CALL_STACK_BIT 9 /* enable call stack */
42 43
43#define LBR_KERNEL (1 << LBR_KERNEL_BIT) 44#define LBR_KERNEL (1 << LBR_KERNEL_BIT)
44#define LBR_USER (1 << LBR_USER_BIT) 45#define LBR_USER (1 << LBR_USER_BIT)
@@ -49,6 +50,7 @@ static enum {
49#define LBR_REL_JMP (1 << LBR_REL_JMP_BIT) 50#define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
50#define LBR_IND_JMP (1 << LBR_IND_JMP_BIT) 51#define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
51#define LBR_FAR (1 << LBR_FAR_BIT) 52#define LBR_FAR (1 << LBR_FAR_BIT)
53#define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
52 54
53#define LBR_PLM (LBR_KERNEL | LBR_USER) 55#define LBR_PLM (LBR_KERNEL | LBR_USER)
54 56
@@ -69,33 +71,31 @@ static enum {
69#define LBR_FROM_FLAG_IN_TX (1ULL << 62) 71#define LBR_FROM_FLAG_IN_TX (1ULL << 62)
70#define LBR_FROM_FLAG_ABORT (1ULL << 61) 72#define LBR_FROM_FLAG_ABORT (1ULL << 61)
71 73
72#define for_each_branch_sample_type(x) \
73 for ((x) = PERF_SAMPLE_BRANCH_USER; \
74 (x) < PERF_SAMPLE_BRANCH_MAX; (x) <<= 1)
75
76/* 74/*
77 * x86control flow change classification 75 * x86control flow change classification
78 * x86control flow changes include branches, interrupts, traps, faults 76 * x86control flow changes include branches, interrupts, traps, faults
79 */ 77 */
80enum { 78enum {
81 X86_BR_NONE = 0, /* unknown */ 79 X86_BR_NONE = 0, /* unknown */
82 80
83 X86_BR_USER = 1 << 0, /* branch target is user */ 81 X86_BR_USER = 1 << 0, /* branch target is user */
84 X86_BR_KERNEL = 1 << 1, /* branch target is kernel */ 82 X86_BR_KERNEL = 1 << 1, /* branch target is kernel */
85 83
86 X86_BR_CALL = 1 << 2, /* call */ 84 X86_BR_CALL = 1 << 2, /* call */
87 X86_BR_RET = 1 << 3, /* return */ 85 X86_BR_RET = 1 << 3, /* return */
88 X86_BR_SYSCALL = 1 << 4, /* syscall */ 86 X86_BR_SYSCALL = 1 << 4, /* syscall */
89 X86_BR_SYSRET = 1 << 5, /* syscall return */ 87 X86_BR_SYSRET = 1 << 5, /* syscall return */
90 X86_BR_INT = 1 << 6, /* sw interrupt */ 88 X86_BR_INT = 1 << 6, /* sw interrupt */
91 X86_BR_IRET = 1 << 7, /* return from interrupt */ 89 X86_BR_IRET = 1 << 7, /* return from interrupt */
92 X86_BR_JCC = 1 << 8, /* conditional */ 90 X86_BR_JCC = 1 << 8, /* conditional */
93 X86_BR_JMP = 1 << 9, /* jump */ 91 X86_BR_JMP = 1 << 9, /* jump */
94 X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */ 92 X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */
95 X86_BR_IND_CALL = 1 << 11,/* indirect calls */ 93 X86_BR_IND_CALL = 1 << 11,/* indirect calls */
96 X86_BR_ABORT = 1 << 12,/* transaction abort */ 94 X86_BR_ABORT = 1 << 12,/* transaction abort */
97 X86_BR_IN_TX = 1 << 13,/* in transaction */ 95 X86_BR_IN_TX = 1 << 13,/* in transaction */
98 X86_BR_NO_TX = 1 << 14,/* not in transaction */ 96 X86_BR_NO_TX = 1 << 14,/* not in transaction */
97 X86_BR_ZERO_CALL = 1 << 15,/* zero length call */
98 X86_BR_CALL_STACK = 1 << 16,/* call stack */
99}; 99};
100 100
101#define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL) 101#define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
@@ -112,13 +112,15 @@ enum {
112 X86_BR_JMP |\ 112 X86_BR_JMP |\
113 X86_BR_IRQ |\ 113 X86_BR_IRQ |\
114 X86_BR_ABORT |\ 114 X86_BR_ABORT |\
115 X86_BR_IND_CALL) 115 X86_BR_IND_CALL |\
116 X86_BR_ZERO_CALL)
116 117
117#define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY) 118#define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
118 119
119#define X86_BR_ANY_CALL \ 120#define X86_BR_ANY_CALL \
120 (X86_BR_CALL |\ 121 (X86_BR_CALL |\
121 X86_BR_IND_CALL |\ 122 X86_BR_IND_CALL |\
123 X86_BR_ZERO_CALL |\
122 X86_BR_SYSCALL |\ 124 X86_BR_SYSCALL |\
123 X86_BR_IRQ |\ 125 X86_BR_IRQ |\
124 X86_BR_INT) 126 X86_BR_INT)
@@ -130,17 +132,32 @@ static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
130 * otherwise it becomes near impossible to get a reliable stack. 132 * otherwise it becomes near impossible to get a reliable stack.
131 */ 133 */
132 134
133static void __intel_pmu_lbr_enable(void) 135static void __intel_pmu_lbr_enable(bool pmi)
134{ 136{
135 u64 debugctl;
136 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 137 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
138 u64 debugctl, lbr_select = 0, orig_debugctl;
137 139
138 if (cpuc->lbr_sel) 140 /*
139 wrmsrl(MSR_LBR_SELECT, cpuc->lbr_sel->config); 141 * No need to reprogram LBR_SELECT in a PMI, as it
142 * did not change.
143 */
144 if (cpuc->lbr_sel && !pmi) {
145 lbr_select = cpuc->lbr_sel->config;
146 wrmsrl(MSR_LBR_SELECT, lbr_select);
147 }
140 148
141 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); 149 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
142 debugctl |= (DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI); 150 orig_debugctl = debugctl;
143 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); 151 debugctl |= DEBUGCTLMSR_LBR;
152 /*
153 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
154 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
155 * may cause superfluous increase/decrease of LBR_TOS.
156 */
157 if (!(lbr_select & LBR_CALL_STACK))
158 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
159 if (orig_debugctl != debugctl)
160 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
144} 161}
145 162
146static void __intel_pmu_lbr_disable(void) 163static void __intel_pmu_lbr_disable(void)
@@ -181,9 +198,116 @@ void intel_pmu_lbr_reset(void)
181 intel_pmu_lbr_reset_64(); 198 intel_pmu_lbr_reset_64();
182} 199}
183 200
201/*
202 * TOS = most recently recorded branch
203 */
204static inline u64 intel_pmu_lbr_tos(void)
205{
206 u64 tos;
207
208 rdmsrl(x86_pmu.lbr_tos, tos);
209 return tos;
210}
211
212enum {
213 LBR_NONE,
214 LBR_VALID,
215};
216
217static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
218{
219 int i;
220 unsigned lbr_idx, mask;
221 u64 tos;
222
223 if (task_ctx->lbr_callstack_users == 0 ||
224 task_ctx->lbr_stack_state == LBR_NONE) {
225 intel_pmu_lbr_reset();
226 return;
227 }
228
229 mask = x86_pmu.lbr_nr - 1;
230 tos = intel_pmu_lbr_tos();
231 for (i = 0; i < x86_pmu.lbr_nr; i++) {
232 lbr_idx = (tos - i) & mask;
233 wrmsrl(x86_pmu.lbr_from + lbr_idx, task_ctx->lbr_from[i]);
234 wrmsrl(x86_pmu.lbr_to + lbr_idx, task_ctx->lbr_to[i]);
235 }
236 task_ctx->lbr_stack_state = LBR_NONE;
237}
238
239static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
240{
241 int i;
242 unsigned lbr_idx, mask;
243 u64 tos;
244
245 if (task_ctx->lbr_callstack_users == 0) {
246 task_ctx->lbr_stack_state = LBR_NONE;
247 return;
248 }
249
250 mask = x86_pmu.lbr_nr - 1;
251 tos = intel_pmu_lbr_tos();
252 for (i = 0; i < x86_pmu.lbr_nr; i++) {
253 lbr_idx = (tos - i) & mask;
254 rdmsrl(x86_pmu.lbr_from + lbr_idx, task_ctx->lbr_from[i]);
255 rdmsrl(x86_pmu.lbr_to + lbr_idx, task_ctx->lbr_to[i]);
256 }
257 task_ctx->lbr_stack_state = LBR_VALID;
258}
259
260void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
261{
262 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
263 struct x86_perf_task_context *task_ctx;
264
265 if (!x86_pmu.lbr_nr)
266 return;
267
268 /*
269 * If LBR callstack feature is enabled and the stack was saved when
270 * the task was scheduled out, restore the stack. Otherwise flush
271 * the LBR stack.
272 */
273 task_ctx = ctx ? ctx->task_ctx_data : NULL;
274 if (task_ctx) {
275 if (sched_in) {
276 __intel_pmu_lbr_restore(task_ctx);
277 cpuc->lbr_context = ctx;
278 } else {
279 __intel_pmu_lbr_save(task_ctx);
280 }
281 return;
282 }
283
284 /*
285 * When sampling the branck stack in system-wide, it may be
286 * necessary to flush the stack on context switch. This happens
287 * when the branch stack does not tag its entries with the pid
288 * of the current task. Otherwise it becomes impossible to
289 * associate a branch entry with a task. This ambiguity is more
290 * likely to appear when the branch stack supports priv level
291 * filtering and the user sets it to monitor only at the user
292 * level (which could be a useful measurement in system-wide
293 * mode). In that case, the risk is high of having a branch
294 * stack with branch from multiple tasks.
295 */
296 if (sched_in) {
297 intel_pmu_lbr_reset();
298 cpuc->lbr_context = ctx;
299 }
300}
301
302static inline bool branch_user_callstack(unsigned br_sel)
303{
304 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
305}
306
184void intel_pmu_lbr_enable(struct perf_event *event) 307void intel_pmu_lbr_enable(struct perf_event *event)
185{ 308{
186 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 309 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
310 struct x86_perf_task_context *task_ctx;
187 311
188 if (!x86_pmu.lbr_nr) 312 if (!x86_pmu.lbr_nr)
189 return; 313 return;
@@ -198,18 +322,33 @@ void intel_pmu_lbr_enable(struct perf_event *event)
198 } 322 }
199 cpuc->br_sel = event->hw.branch_reg.reg; 323 cpuc->br_sel = event->hw.branch_reg.reg;
200 324
325 if (branch_user_callstack(cpuc->br_sel) && event->ctx &&
326 event->ctx->task_ctx_data) {
327 task_ctx = event->ctx->task_ctx_data;
328 task_ctx->lbr_callstack_users++;
329 }
330
201 cpuc->lbr_users++; 331 cpuc->lbr_users++;
332 perf_sched_cb_inc(event->ctx->pmu);
202} 333}
203 334
204void intel_pmu_lbr_disable(struct perf_event *event) 335void intel_pmu_lbr_disable(struct perf_event *event)
205{ 336{
206 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 337 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
338 struct x86_perf_task_context *task_ctx;
207 339
208 if (!x86_pmu.lbr_nr) 340 if (!x86_pmu.lbr_nr)
209 return; 341 return;
210 342
343 if (branch_user_callstack(cpuc->br_sel) && event->ctx &&
344 event->ctx->task_ctx_data) {
345 task_ctx = event->ctx->task_ctx_data;
346 task_ctx->lbr_callstack_users--;
347 }
348
211 cpuc->lbr_users--; 349 cpuc->lbr_users--;
212 WARN_ON_ONCE(cpuc->lbr_users < 0); 350 WARN_ON_ONCE(cpuc->lbr_users < 0);
351 perf_sched_cb_dec(event->ctx->pmu);
213 352
214 if (cpuc->enabled && !cpuc->lbr_users) { 353 if (cpuc->enabled && !cpuc->lbr_users) {
215 __intel_pmu_lbr_disable(); 354 __intel_pmu_lbr_disable();
@@ -218,12 +357,12 @@ void intel_pmu_lbr_disable(struct perf_event *event)
218 } 357 }
219} 358}
220 359
221void intel_pmu_lbr_enable_all(void) 360void intel_pmu_lbr_enable_all(bool pmi)
222{ 361{
223 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); 362 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
224 363
225 if (cpuc->lbr_users) 364 if (cpuc->lbr_users)
226 __intel_pmu_lbr_enable(); 365 __intel_pmu_lbr_enable(pmi);
227} 366}
228 367
229void intel_pmu_lbr_disable_all(void) 368void intel_pmu_lbr_disable_all(void)
@@ -234,18 +373,6 @@ void intel_pmu_lbr_disable_all(void)
234 __intel_pmu_lbr_disable(); 373 __intel_pmu_lbr_disable();
235} 374}
236 375
237/*
238 * TOS = most recently recorded branch
239 */
240static inline u64 intel_pmu_lbr_tos(void)
241{
242 u64 tos;
243
244 rdmsrl(x86_pmu.lbr_tos, tos);
245
246 return tos;
247}
248
249static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc) 376static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
250{ 377{
251 unsigned long mask = x86_pmu.lbr_nr - 1; 378 unsigned long mask = x86_pmu.lbr_nr - 1;
@@ -350,7 +477,7 @@ void intel_pmu_lbr_read(void)
350 * - in case there is no HW filter 477 * - in case there is no HW filter
351 * - in case the HW filter has errata or limitations 478 * - in case the HW filter has errata or limitations
352 */ 479 */
353static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event) 480static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
354{ 481{
355 u64 br_type = event->attr.branch_sample_type; 482 u64 br_type = event->attr.branch_sample_type;
356 int mask = 0; 483 int mask = 0;
@@ -387,11 +514,21 @@ static void intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
387 if (br_type & PERF_SAMPLE_BRANCH_COND) 514 if (br_type & PERF_SAMPLE_BRANCH_COND)
388 mask |= X86_BR_JCC; 515 mask |= X86_BR_JCC;
389 516
517 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
518 if (!x86_pmu_has_lbr_callstack())
519 return -EOPNOTSUPP;
520 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
521 return -EINVAL;
522 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
523 X86_BR_CALL_STACK;
524 }
525
390 /* 526 /*
391 * stash actual user request into reg, it may 527 * stash actual user request into reg, it may
392 * be used by fixup code for some CPU 528 * be used by fixup code for some CPU
393 */ 529 */
394 event->hw.branch_reg.reg = mask; 530 event->hw.branch_reg.reg = mask;
531 return 0;
395} 532}
396 533
397/* 534/*
@@ -403,14 +540,14 @@ static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
403{ 540{
404 struct hw_perf_event_extra *reg; 541 struct hw_perf_event_extra *reg;
405 u64 br_type = event->attr.branch_sample_type; 542 u64 br_type = event->attr.branch_sample_type;
406 u64 mask = 0, m; 543 u64 mask = 0, v;
407 u64 v; 544 int i;
408 545
409 for_each_branch_sample_type(m) { 546 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
410 if (!(br_type & m)) 547 if (!(br_type & (1ULL << i)))
411 continue; 548 continue;
412 549
413 v = x86_pmu.lbr_sel_map[m]; 550 v = x86_pmu.lbr_sel_map[i];
414 if (v == LBR_NOT_SUPP) 551 if (v == LBR_NOT_SUPP)
415 return -EOPNOTSUPP; 552 return -EOPNOTSUPP;
416 553
@@ -420,8 +557,12 @@ static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
420 reg = &event->hw.branch_reg; 557 reg = &event->hw.branch_reg;
421 reg->idx = EXTRA_REG_LBR; 558 reg->idx = EXTRA_REG_LBR;
422 559
423 /* LBR_SELECT operates in suppress mode so invert mask */ 560 /*
424 reg->config = ~mask & x86_pmu.lbr_sel_mask; 561 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
562 * in suppress mode. So LBR_SELECT should be set to
563 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
564 */
565 reg->config = mask ^ x86_pmu.lbr_sel_mask;
425 566
426 return 0; 567 return 0;
427} 568}
@@ -439,7 +580,9 @@ int intel_pmu_setup_lbr_filter(struct perf_event *event)
439 /* 580 /*
440 * setup SW LBR filter 581 * setup SW LBR filter
441 */ 582 */
442 intel_pmu_setup_sw_lbr_filter(event); 583 ret = intel_pmu_setup_sw_lbr_filter(event);
584 if (ret)
585 return ret;
443 586
444 /* 587 /*
445 * setup HW LBR filter, if any 588 * setup HW LBR filter, if any
@@ -568,6 +711,12 @@ static int branch_type(unsigned long from, unsigned long to, int abort)
568 ret = X86_BR_INT; 711 ret = X86_BR_INT;
569 break; 712 break;
570 case 0xe8: /* call near rel */ 713 case 0xe8: /* call near rel */
714 insn_get_immediate(&insn);
715 if (insn.immediate1.value == 0) {
716 /* zero length call */
717 ret = X86_BR_ZERO_CALL;
718 break;
719 }
571 case 0x9a: /* call far absolute */ 720 case 0x9a: /* call far absolute */
572 ret = X86_BR_CALL; 721 ret = X86_BR_CALL;
573 break; 722 break;
@@ -678,35 +827,49 @@ intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
678/* 827/*
679 * Map interface branch filters onto LBR filters 828 * Map interface branch filters onto LBR filters
680 */ 829 */
681static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX] = { 830static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
682 [PERF_SAMPLE_BRANCH_ANY] = LBR_ANY, 831 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
683 [PERF_SAMPLE_BRANCH_USER] = LBR_USER, 832 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
684 [PERF_SAMPLE_BRANCH_KERNEL] = LBR_KERNEL, 833 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
685 [PERF_SAMPLE_BRANCH_HV] = LBR_IGN, 834 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
686 [PERF_SAMPLE_BRANCH_ANY_RETURN] = LBR_RETURN | LBR_REL_JMP 835 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
687 | LBR_IND_JMP | LBR_FAR, 836 | LBR_IND_JMP | LBR_FAR,
688 /* 837 /*
689 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches 838 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
690 */ 839 */
691 [PERF_SAMPLE_BRANCH_ANY_CALL] = 840 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
692 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR, 841 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
693 /* 842 /*
694 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL 843 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
695 */ 844 */
696 [PERF_SAMPLE_BRANCH_IND_CALL] = LBR_IND_CALL | LBR_IND_JMP, 845 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
697 [PERF_SAMPLE_BRANCH_COND] = LBR_JCC, 846 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
698}; 847};
699 848
700static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX] = { 849static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
701 [PERF_SAMPLE_BRANCH_ANY] = LBR_ANY, 850 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
702 [PERF_SAMPLE_BRANCH_USER] = LBR_USER, 851 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
703 [PERF_SAMPLE_BRANCH_KERNEL] = LBR_KERNEL, 852 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
704 [PERF_SAMPLE_BRANCH_HV] = LBR_IGN, 853 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
705 [PERF_SAMPLE_BRANCH_ANY_RETURN] = LBR_RETURN | LBR_FAR, 854 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
706 [PERF_SAMPLE_BRANCH_ANY_CALL] = LBR_REL_CALL | LBR_IND_CALL 855 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
707 | LBR_FAR, 856 | LBR_FAR,
708 [PERF_SAMPLE_BRANCH_IND_CALL] = LBR_IND_CALL, 857 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
709 [PERF_SAMPLE_BRANCH_COND] = LBR_JCC, 858 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
859};
860
861static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
862 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
863 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
864 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
865 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
866 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
867 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
868 | LBR_FAR,
869 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
870 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
871 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
872 | LBR_RETURN | LBR_CALL_STACK,
710}; 873};
711 874
712/* core */ 875/* core */
@@ -765,6 +928,20 @@ void __init intel_pmu_lbr_init_snb(void)
765 pr_cont("16-deep LBR, "); 928 pr_cont("16-deep LBR, ");
766} 929}
767 930
931/* haswell */
932void intel_pmu_lbr_init_hsw(void)
933{
934 x86_pmu.lbr_nr = 16;
935 x86_pmu.lbr_tos = MSR_LBR_TOS;
936 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
937 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
938
939 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
940 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
941
942 pr_cont("16-deep LBR, ");
943}
944
768/* atom */ 945/* atom */
769void __init intel_pmu_lbr_init_atom(void) 946void __init intel_pmu_lbr_init_atom(void)
770{ 947{
diff --git a/arch/x86/kernel/cpu/perf_event_intel_pt.c b/arch/x86/kernel/cpu/perf_event_intel_pt.c
new file mode 100644
index 000000000000..f2770641c0fd
--- /dev/null
+++ b/arch/x86/kernel/cpu/perf_event_intel_pt.c
@@ -0,0 +1,1103 @@
1/*
2 * Intel(R) Processor Trace PMU driver for perf
3 * Copyright (c) 2013-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * Intel PT is specified in the Intel Architecture Instruction Set Extensions
15 * Programming Reference:
16 * http://software.intel.com/en-us/intel-isa-extensions
17 */
18
19#undef DEBUG
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/types.h>
24#include <linux/slab.h>
25#include <linux/device.h>
26
27#include <asm/perf_event.h>
28#include <asm/insn.h>
29#include <asm/io.h>
30
31#include "perf_event.h"
32#include "intel_pt.h"
33
34static DEFINE_PER_CPU(struct pt, pt_ctx);
35
36static struct pt_pmu pt_pmu;
37
38enum cpuid_regs {
39 CR_EAX = 0,
40 CR_ECX,
41 CR_EDX,
42 CR_EBX
43};
44
45/*
46 * Capabilities of Intel PT hardware, such as number of address bits or
47 * supported output schemes, are cached and exported to userspace as "caps"
48 * attribute group of pt pmu device
49 * (/sys/bus/event_source/devices/intel_pt/caps/) so that userspace can store
50 * relevant bits together with intel_pt traces.
51 *
52 * These are necessary for both trace decoding (payloads_lip, contains address
53 * width encoded in IP-related packets), and event configuration (bitmasks with
54 * permitted values for certain bit fields).
55 */
56#define PT_CAP(_n, _l, _r, _m) \
57 [PT_CAP_ ## _n] = { .name = __stringify(_n), .leaf = _l, \
58 .reg = _r, .mask = _m }
59
60static struct pt_cap_desc {
61 const char *name;
62 u32 leaf;
63 u8 reg;
64 u32 mask;
65} pt_caps[] = {
66 PT_CAP(max_subleaf, 0, CR_EAX, 0xffffffff),
67 PT_CAP(cr3_filtering, 0, CR_EBX, BIT(0)),
68 PT_CAP(topa_output, 0, CR_ECX, BIT(0)),
69 PT_CAP(topa_multiple_entries, 0, CR_ECX, BIT(1)),
70 PT_CAP(payloads_lip, 0, CR_ECX, BIT(31)),
71};
72
73static u32 pt_cap_get(enum pt_capabilities cap)
74{
75 struct pt_cap_desc *cd = &pt_caps[cap];
76 u32 c = pt_pmu.caps[cd->leaf * 4 + cd->reg];
77 unsigned int shift = __ffs(cd->mask);
78
79 return (c & cd->mask) >> shift;
80}
81
82static ssize_t pt_cap_show(struct device *cdev,
83 struct device_attribute *attr,
84 char *buf)
85{
86 struct dev_ext_attribute *ea =
87 container_of(attr, struct dev_ext_attribute, attr);
88 enum pt_capabilities cap = (long)ea->var;
89
90 return snprintf(buf, PAGE_SIZE, "%x\n", pt_cap_get(cap));
91}
92
93static struct attribute_group pt_cap_group = {
94 .name = "caps",
95};
96
97PMU_FORMAT_ATTR(tsc, "config:10" );
98PMU_FORMAT_ATTR(noretcomp, "config:11" );
99
100static struct attribute *pt_formats_attr[] = {
101 &format_attr_tsc.attr,
102 &format_attr_noretcomp.attr,
103 NULL,
104};
105
106static struct attribute_group pt_format_group = {
107 .name = "format",
108 .attrs = pt_formats_attr,
109};
110
111static const struct attribute_group *pt_attr_groups[] = {
112 &pt_cap_group,
113 &pt_format_group,
114 NULL,
115};
116
117static int __init pt_pmu_hw_init(void)
118{
119 struct dev_ext_attribute *de_attrs;
120 struct attribute **attrs;
121 size_t size;
122 int ret;
123 long i;
124
125 attrs = NULL;
126 ret = -ENODEV;
127 if (!test_cpu_cap(&boot_cpu_data, X86_FEATURE_INTEL_PT))
128 goto fail;
129
130 for (i = 0; i < PT_CPUID_LEAVES; i++) {
131 cpuid_count(20, i,
132 &pt_pmu.caps[CR_EAX + i*4],
133 &pt_pmu.caps[CR_EBX + i*4],
134 &pt_pmu.caps[CR_ECX + i*4],
135 &pt_pmu.caps[CR_EDX + i*4]);
136 }
137
138 ret = -ENOMEM;
139 size = sizeof(struct attribute *) * (ARRAY_SIZE(pt_caps)+1);
140 attrs = kzalloc(size, GFP_KERNEL);
141 if (!attrs)
142 goto fail;
143
144 size = sizeof(struct dev_ext_attribute) * (ARRAY_SIZE(pt_caps)+1);
145 de_attrs = kzalloc(size, GFP_KERNEL);
146 if (!de_attrs)
147 goto fail;
148
149 for (i = 0; i < ARRAY_SIZE(pt_caps); i++) {
150 struct dev_ext_attribute *de_attr = de_attrs + i;
151
152 de_attr->attr.attr.name = pt_caps[i].name;
153
154 sysfs_attr_init(&de_attrs->attr.attr);
155
156 de_attr->attr.attr.mode = S_IRUGO;
157 de_attr->attr.show = pt_cap_show;
158 de_attr->var = (void *)i;
159
160 attrs[i] = &de_attr->attr.attr;
161 }
162
163 pt_cap_group.attrs = attrs;
164
165 return 0;
166
167fail:
168 kfree(attrs);
169
170 return ret;
171}
172
173#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC)
174
175static bool pt_event_valid(struct perf_event *event)
176{
177 u64 config = event->attr.config;
178
179 if ((config & PT_CONFIG_MASK) != config)
180 return false;
181
182 return true;
183}
184
185/*
186 * PT configuration helpers
187 * These all are cpu affine and operate on a local PT
188 */
189
190static bool pt_is_running(void)
191{
192 u64 ctl;
193
194 rdmsrl(MSR_IA32_RTIT_CTL, ctl);
195
196 return !!(ctl & RTIT_CTL_TRACEEN);
197}
198
199static void pt_config(struct perf_event *event)
200{
201 u64 reg;
202
203 reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
204
205 if (!event->attr.exclude_kernel)
206 reg |= RTIT_CTL_OS;
207 if (!event->attr.exclude_user)
208 reg |= RTIT_CTL_USR;
209
210 reg |= (event->attr.config & PT_CONFIG_MASK);
211
212 wrmsrl(MSR_IA32_RTIT_CTL, reg);
213}
214
215static void pt_config_start(bool start)
216{
217 u64 ctl;
218
219 rdmsrl(MSR_IA32_RTIT_CTL, ctl);
220 if (start)
221 ctl |= RTIT_CTL_TRACEEN;
222 else
223 ctl &= ~RTIT_CTL_TRACEEN;
224 wrmsrl(MSR_IA32_RTIT_CTL, ctl);
225
226 /*
227 * A wrmsr that disables trace generation serializes other PT
228 * registers and causes all data packets to be written to memory,
229 * but a fence is required for the data to become globally visible.
230 *
231 * The below WMB, separating data store and aux_head store matches
232 * the consumer's RMB that separates aux_head load and data load.
233 */
234 if (!start)
235 wmb();
236}
237
238static void pt_config_buffer(void *buf, unsigned int topa_idx,
239 unsigned int output_off)
240{
241 u64 reg;
242
243 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, virt_to_phys(buf));
244
245 reg = 0x7f | ((u64)topa_idx << 7) | ((u64)output_off << 32);
246
247 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, reg);
248}
249
250/*
251 * Keep ToPA table-related metadata on the same page as the actual table,
252 * taking up a few words from the top
253 */
254
255#define TENTS_PER_PAGE (((PAGE_SIZE - 40) / sizeof(struct topa_entry)) - 1)
256
257/**
258 * struct topa - page-sized ToPA table with metadata at the top
259 * @table: actual ToPA table entries, as understood by PT hardware
260 * @list: linkage to struct pt_buffer's list of tables
261 * @phys: physical address of this page
262 * @offset: offset of the first entry in this table in the buffer
263 * @size: total size of all entries in this table
264 * @last: index of the last initialized entry in this table
265 */
266struct topa {
267 struct topa_entry table[TENTS_PER_PAGE];
268 struct list_head list;
269 u64 phys;
270 u64 offset;
271 size_t size;
272 int last;
273};
274
275/* make -1 stand for the last table entry */
276#define TOPA_ENTRY(t, i) ((i) == -1 ? &(t)->table[(t)->last] : &(t)->table[(i)])
277
278/**
279 * topa_alloc() - allocate page-sized ToPA table
280 * @cpu: CPU on which to allocate.
281 * @gfp: Allocation flags.
282 *
283 * Return: On success, return the pointer to ToPA table page.
284 */
285static struct topa *topa_alloc(int cpu, gfp_t gfp)
286{
287 int node = cpu_to_node(cpu);
288 struct topa *topa;
289 struct page *p;
290
291 p = alloc_pages_node(node, gfp | __GFP_ZERO, 0);
292 if (!p)
293 return NULL;
294
295 topa = page_address(p);
296 topa->last = 0;
297 topa->phys = page_to_phys(p);
298
299 /*
300 * In case of singe-entry ToPA, always put the self-referencing END
301 * link as the 2nd entry in the table
302 */
303 if (!pt_cap_get(PT_CAP_topa_multiple_entries)) {
304 TOPA_ENTRY(topa, 1)->base = topa->phys >> TOPA_SHIFT;
305 TOPA_ENTRY(topa, 1)->end = 1;
306 }
307
308 return topa;
309}
310
311/**
312 * topa_free() - free a page-sized ToPA table
313 * @topa: Table to deallocate.
314 */
315static void topa_free(struct topa *topa)
316{
317 free_page((unsigned long)topa);
318}
319
320/**
321 * topa_insert_table() - insert a ToPA table into a buffer
322 * @buf: PT buffer that's being extended.
323 * @topa: New topa table to be inserted.
324 *
325 * If it's the first table in this buffer, set up buffer's pointers
326 * accordingly; otherwise, add a END=1 link entry to @topa to the current
327 * "last" table and adjust the last table pointer to @topa.
328 */
329static void topa_insert_table(struct pt_buffer *buf, struct topa *topa)
330{
331 struct topa *last = buf->last;
332
333 list_add_tail(&topa->list, &buf->tables);
334
335 if (!buf->first) {
336 buf->first = buf->last = buf->cur = topa;
337 return;
338 }
339
340 topa->offset = last->offset + last->size;
341 buf->last = topa;
342
343 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
344 return;
345
346 BUG_ON(last->last != TENTS_PER_PAGE - 1);
347
348 TOPA_ENTRY(last, -1)->base = topa->phys >> TOPA_SHIFT;
349 TOPA_ENTRY(last, -1)->end = 1;
350}
351
352/**
353 * topa_table_full() - check if a ToPA table is filled up
354 * @topa: ToPA table.
355 */
356static bool topa_table_full(struct topa *topa)
357{
358 /* single-entry ToPA is a special case */
359 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
360 return !!topa->last;
361
362 return topa->last == TENTS_PER_PAGE - 1;
363}
364
365/**
366 * topa_insert_pages() - create a list of ToPA tables
367 * @buf: PT buffer being initialized.
368 * @gfp: Allocation flags.
369 *
370 * This initializes a list of ToPA tables with entries from
371 * the data_pages provided by rb_alloc_aux().
372 *
373 * Return: 0 on success or error code.
374 */
375static int topa_insert_pages(struct pt_buffer *buf, gfp_t gfp)
376{
377 struct topa *topa = buf->last;
378 int order = 0;
379 struct page *p;
380
381 p = virt_to_page(buf->data_pages[buf->nr_pages]);
382 if (PagePrivate(p))
383 order = page_private(p);
384
385 if (topa_table_full(topa)) {
386 topa = topa_alloc(buf->cpu, gfp);
387 if (!topa)
388 return -ENOMEM;
389
390 topa_insert_table(buf, topa);
391 }
392
393 TOPA_ENTRY(topa, -1)->base = page_to_phys(p) >> TOPA_SHIFT;
394 TOPA_ENTRY(topa, -1)->size = order;
395 if (!buf->snapshot && !pt_cap_get(PT_CAP_topa_multiple_entries)) {
396 TOPA_ENTRY(topa, -1)->intr = 1;
397 TOPA_ENTRY(topa, -1)->stop = 1;
398 }
399
400 topa->last++;
401 topa->size += sizes(order);
402
403 buf->nr_pages += 1ul << order;
404
405 return 0;
406}
407
408/**
409 * pt_topa_dump() - print ToPA tables and their entries
410 * @buf: PT buffer.
411 */
412static void pt_topa_dump(struct pt_buffer *buf)
413{
414 struct topa *topa;
415
416 list_for_each_entry(topa, &buf->tables, list) {
417 int i;
418
419 pr_debug("# table @%p (%016Lx), off %llx size %zx\n", topa->table,
420 topa->phys, topa->offset, topa->size);
421 for (i = 0; i < TENTS_PER_PAGE; i++) {
422 pr_debug("# entry @%p (%lx sz %u %c%c%c) raw=%16llx\n",
423 &topa->table[i],
424 (unsigned long)topa->table[i].base << TOPA_SHIFT,
425 sizes(topa->table[i].size),
426 topa->table[i].end ? 'E' : ' ',
427 topa->table[i].intr ? 'I' : ' ',
428 topa->table[i].stop ? 'S' : ' ',
429 *(u64 *)&topa->table[i]);
430 if ((pt_cap_get(PT_CAP_topa_multiple_entries) &&
431 topa->table[i].stop) ||
432 topa->table[i].end)
433 break;
434 }
435 }
436}
437
438/**
439 * pt_buffer_advance() - advance to the next output region
440 * @buf: PT buffer.
441 *
442 * Advance the current pointers in the buffer to the next ToPA entry.
443 */
444static void pt_buffer_advance(struct pt_buffer *buf)
445{
446 buf->output_off = 0;
447 buf->cur_idx++;
448
449 if (buf->cur_idx == buf->cur->last) {
450 if (buf->cur == buf->last)
451 buf->cur = buf->first;
452 else
453 buf->cur = list_entry(buf->cur->list.next, struct topa,
454 list);
455 buf->cur_idx = 0;
456 }
457}
458
459/**
460 * pt_update_head() - calculate current offsets and sizes
461 * @pt: Per-cpu pt context.
462 *
463 * Update buffer's current write pointer position and data size.
464 */
465static void pt_update_head(struct pt *pt)
466{
467 struct pt_buffer *buf = perf_get_aux(&pt->handle);
468 u64 topa_idx, base, old;
469
470 /* offset of the first region in this table from the beginning of buf */
471 base = buf->cur->offset + buf->output_off;
472
473 /* offset of the current output region within this table */
474 for (topa_idx = 0; topa_idx < buf->cur_idx; topa_idx++)
475 base += sizes(buf->cur->table[topa_idx].size);
476
477 if (buf->snapshot) {
478 local_set(&buf->data_size, base);
479 } else {
480 old = (local64_xchg(&buf->head, base) &
481 ((buf->nr_pages << PAGE_SHIFT) - 1));
482 if (base < old)
483 base += buf->nr_pages << PAGE_SHIFT;
484
485 local_add(base - old, &buf->data_size);
486 }
487}
488
489/**
490 * pt_buffer_region() - obtain current output region's address
491 * @buf: PT buffer.
492 */
493static void *pt_buffer_region(struct pt_buffer *buf)
494{
495 return phys_to_virt(buf->cur->table[buf->cur_idx].base << TOPA_SHIFT);
496}
497
498/**
499 * pt_buffer_region_size() - obtain current output region's size
500 * @buf: PT buffer.
501 */
502static size_t pt_buffer_region_size(struct pt_buffer *buf)
503{
504 return sizes(buf->cur->table[buf->cur_idx].size);
505}
506
507/**
508 * pt_handle_status() - take care of possible status conditions
509 * @pt: Per-cpu pt context.
510 */
511static void pt_handle_status(struct pt *pt)
512{
513 struct pt_buffer *buf = perf_get_aux(&pt->handle);
514 int advance = 0;
515 u64 status;
516
517 rdmsrl(MSR_IA32_RTIT_STATUS, status);
518
519 if (status & RTIT_STATUS_ERROR) {
520 pr_err_ratelimited("ToPA ERROR encountered, trying to recover\n");
521 pt_topa_dump(buf);
522 status &= ~RTIT_STATUS_ERROR;
523 }
524
525 if (status & RTIT_STATUS_STOPPED) {
526 status &= ~RTIT_STATUS_STOPPED;
527
528 /*
529 * On systems that only do single-entry ToPA, hitting STOP
530 * means we are already losing data; need to let the decoder
531 * know.
532 */
533 if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
534 buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
535 local_inc(&buf->lost);
536 advance++;
537 }
538 }
539
540 /*
541 * Also on single-entry ToPA implementations, interrupt will come
542 * before the output reaches its output region's boundary.
543 */
544 if (!pt_cap_get(PT_CAP_topa_multiple_entries) && !buf->snapshot &&
545 pt_buffer_region_size(buf) - buf->output_off <= TOPA_PMI_MARGIN) {
546 void *head = pt_buffer_region(buf);
547
548 /* everything within this margin needs to be zeroed out */
549 memset(head + buf->output_off, 0,
550 pt_buffer_region_size(buf) -
551 buf->output_off);
552 advance++;
553 }
554
555 if (advance)
556 pt_buffer_advance(buf);
557
558 wrmsrl(MSR_IA32_RTIT_STATUS, status);
559}
560
561/**
562 * pt_read_offset() - translate registers into buffer pointers
563 * @buf: PT buffer.
564 *
565 * Set buffer's output pointers from MSR values.
566 */
567static void pt_read_offset(struct pt_buffer *buf)
568{
569 u64 offset, base_topa;
570
571 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, base_topa);
572 buf->cur = phys_to_virt(base_topa);
573
574 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, offset);
575 /* offset within current output region */
576 buf->output_off = offset >> 32;
577 /* index of current output region within this table */
578 buf->cur_idx = (offset & 0xffffff80) >> 7;
579}
580
581/**
582 * pt_topa_next_entry() - obtain index of the first page in the next ToPA entry
583 * @buf: PT buffer.
584 * @pg: Page offset in the buffer.
585 *
586 * When advancing to the next output region (ToPA entry), given a page offset
587 * into the buffer, we need to find the offset of the first page in the next
588 * region.
589 */
590static unsigned int pt_topa_next_entry(struct pt_buffer *buf, unsigned int pg)
591{
592 struct topa_entry *te = buf->topa_index[pg];
593
594 /* one region */
595 if (buf->first == buf->last && buf->first->last == 1)
596 return pg;
597
598 do {
599 pg++;
600 pg &= buf->nr_pages - 1;
601 } while (buf->topa_index[pg] == te);
602
603 return pg;
604}
605
606/**
607 * pt_buffer_reset_markers() - place interrupt and stop bits in the buffer
608 * @buf: PT buffer.
609 * @handle: Current output handle.
610 *
611 * Place INT and STOP marks to prevent overwriting old data that the consumer
612 * hasn't yet collected.
613 */
614static int pt_buffer_reset_markers(struct pt_buffer *buf,
615 struct perf_output_handle *handle)
616
617{
618 unsigned long idx, npages, end;
619
620 if (buf->snapshot)
621 return 0;
622
623 /* can't stop in the middle of an output region */
624 if (buf->output_off + handle->size + 1 <
625 sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size))
626 return -EINVAL;
627
628
629 /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
630 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
631 return 0;
632
633 /* clear STOP and INT from current entry */
634 buf->topa_index[buf->stop_pos]->stop = 0;
635 buf->topa_index[buf->intr_pos]->intr = 0;
636
637 if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
638 npages = (handle->size + 1) >> PAGE_SHIFT;
639 end = (local64_read(&buf->head) >> PAGE_SHIFT) + npages;
640 /*if (end > handle->wakeup >> PAGE_SHIFT)
641 end = handle->wakeup >> PAGE_SHIFT;*/
642 idx = end & (buf->nr_pages - 1);
643 buf->stop_pos = idx;
644 idx = (local64_read(&buf->head) >> PAGE_SHIFT) + npages - 1;
645 idx &= buf->nr_pages - 1;
646 buf->intr_pos = idx;
647 }
648
649 buf->topa_index[buf->stop_pos]->stop = 1;
650 buf->topa_index[buf->intr_pos]->intr = 1;
651
652 return 0;
653}
654
655/**
656 * pt_buffer_setup_topa_index() - build topa_index[] table of regions
657 * @buf: PT buffer.
658 *
659 * topa_index[] references output regions indexed by offset into the
660 * buffer for purposes of quick reverse lookup.
661 */
662static void pt_buffer_setup_topa_index(struct pt_buffer *buf)
663{
664 struct topa *cur = buf->first, *prev = buf->last;
665 struct topa_entry *te_cur = TOPA_ENTRY(cur, 0),
666 *te_prev = TOPA_ENTRY(prev, prev->last - 1);
667 int pg = 0, idx = 0, ntopa = 0;
668
669 while (pg < buf->nr_pages) {
670 int tidx;
671
672 /* pages within one topa entry */
673 for (tidx = 0; tidx < 1 << te_cur->size; tidx++, pg++)
674 buf->topa_index[pg] = te_prev;
675
676 te_prev = te_cur;
677
678 if (idx == cur->last - 1) {
679 /* advance to next topa table */
680 idx = 0;
681 cur = list_entry(cur->list.next, struct topa, list);
682 ntopa++;
683 } else
684 idx++;
685 te_cur = TOPA_ENTRY(cur, idx);
686 }
687
688}
689
690/**
691 * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
692 * @buf: PT buffer.
693 * @head: Write pointer (aux_head) from AUX buffer.
694 *
695 * Find the ToPA table and entry corresponding to given @head and set buffer's
696 * "current" pointers accordingly.
697 */
698static void pt_buffer_reset_offsets(struct pt_buffer *buf, unsigned long head)
699{
700 int pg;
701
702 if (buf->snapshot)
703 head &= (buf->nr_pages << PAGE_SHIFT) - 1;
704
705 pg = (head >> PAGE_SHIFT) & (buf->nr_pages - 1);
706 pg = pt_topa_next_entry(buf, pg);
707
708 buf->cur = (struct topa *)((unsigned long)buf->topa_index[pg] & PAGE_MASK);
709 buf->cur_idx = ((unsigned long)buf->topa_index[pg] -
710 (unsigned long)buf->cur) / sizeof(struct topa_entry);
711 buf->output_off = head & (sizes(buf->cur->table[buf->cur_idx].size) - 1);
712
713 local64_set(&buf->head, head);
714 local_set(&buf->data_size, 0);
715}
716
717/**
718 * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
719 * @buf: PT buffer.
720 */
721static void pt_buffer_fini_topa(struct pt_buffer *buf)
722{
723 struct topa *topa, *iter;
724
725 list_for_each_entry_safe(topa, iter, &buf->tables, list) {
726 /*
727 * right now, this is in free_aux() path only, so
728 * no need to unlink this table from the list
729 */
730 topa_free(topa);
731 }
732}
733
734/**
735 * pt_buffer_init_topa() - initialize ToPA table for pt buffer
736 * @buf: PT buffer.
737 * @size: Total size of all regions within this ToPA.
738 * @gfp: Allocation flags.
739 */
740static int pt_buffer_init_topa(struct pt_buffer *buf, unsigned long nr_pages,
741 gfp_t gfp)
742{
743 struct topa *topa;
744 int err;
745
746 topa = topa_alloc(buf->cpu, gfp);
747 if (!topa)
748 return -ENOMEM;
749
750 topa_insert_table(buf, topa);
751
752 while (buf->nr_pages < nr_pages) {
753 err = topa_insert_pages(buf, gfp);
754 if (err) {
755 pt_buffer_fini_topa(buf);
756 return -ENOMEM;
757 }
758 }
759
760 pt_buffer_setup_topa_index(buf);
761
762 /* link last table to the first one, unless we're double buffering */
763 if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
764 TOPA_ENTRY(buf->last, -1)->base = buf->first->phys >> TOPA_SHIFT;
765 TOPA_ENTRY(buf->last, -1)->end = 1;
766 }
767
768 pt_topa_dump(buf);
769 return 0;
770}
771
772/**
773 * pt_buffer_setup_aux() - set up topa tables for a PT buffer
774 * @cpu: Cpu on which to allocate, -1 means current.
775 * @pages: Array of pointers to buffer pages passed from perf core.
776 * @nr_pages: Number of pages in the buffer.
777 * @snapshot: If this is a snapshot/overwrite counter.
778 *
779 * This is a pmu::setup_aux callback that sets up ToPA tables and all the
780 * bookkeeping for an AUX buffer.
781 *
782 * Return: Our private PT buffer structure.
783 */
784static void *
785pt_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool snapshot)
786{
787 struct pt_buffer *buf;
788 int node, ret;
789
790 if (!nr_pages)
791 return NULL;
792
793 if (cpu == -1)
794 cpu = raw_smp_processor_id();
795 node = cpu_to_node(cpu);
796
797 buf = kzalloc_node(offsetof(struct pt_buffer, topa_index[nr_pages]),
798 GFP_KERNEL, node);
799 if (!buf)
800 return NULL;
801
802 buf->cpu = cpu;
803 buf->snapshot = snapshot;
804 buf->data_pages = pages;
805
806 INIT_LIST_HEAD(&buf->tables);
807
808 ret = pt_buffer_init_topa(buf, nr_pages, GFP_KERNEL);
809 if (ret) {
810 kfree(buf);
811 return NULL;
812 }
813
814 return buf;
815}
816
817/**
818 * pt_buffer_free_aux() - perf AUX deallocation path callback
819 * @data: PT buffer.
820 */
821static void pt_buffer_free_aux(void *data)
822{
823 struct pt_buffer *buf = data;
824
825 pt_buffer_fini_topa(buf);
826 kfree(buf);
827}
828
829/**
830 * pt_buffer_is_full() - check if the buffer is full
831 * @buf: PT buffer.
832 * @pt: Per-cpu pt handle.
833 *
834 * If the user hasn't read data from the output region that aux_head
835 * points to, the buffer is considered full: the user needs to read at
836 * least this region and update aux_tail to point past it.
837 */
838static bool pt_buffer_is_full(struct pt_buffer *buf, struct pt *pt)
839{
840 if (buf->snapshot)
841 return false;
842
843 if (local_read(&buf->data_size) >= pt->handle.size)
844 return true;
845
846 return false;
847}
848
849/**
850 * intel_pt_interrupt() - PT PMI handler
851 */
852void intel_pt_interrupt(void)
853{
854 struct pt *pt = this_cpu_ptr(&pt_ctx);
855 struct pt_buffer *buf;
856 struct perf_event *event = pt->handle.event;
857
858 /*
859 * There may be a dangling PT bit in the interrupt status register
860 * after PT has been disabled by pt_event_stop(). Make sure we don't
861 * do anything (particularly, re-enable) for this event here.
862 */
863 if (!ACCESS_ONCE(pt->handle_nmi))
864 return;
865
866 pt_config_start(false);
867
868 if (!event)
869 return;
870
871 buf = perf_get_aux(&pt->handle);
872 if (!buf)
873 return;
874
875 pt_read_offset(buf);
876
877 pt_handle_status(pt);
878
879 pt_update_head(pt);
880
881 perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
882 local_xchg(&buf->lost, 0));
883
884 if (!event->hw.state) {
885 int ret;
886
887 buf = perf_aux_output_begin(&pt->handle, event);
888 if (!buf) {
889 event->hw.state = PERF_HES_STOPPED;
890 return;
891 }
892
893 pt_buffer_reset_offsets(buf, pt->handle.head);
894 ret = pt_buffer_reset_markers(buf, &pt->handle);
895 if (ret) {
896 perf_aux_output_end(&pt->handle, 0, true);
897 return;
898 }
899
900 pt_config_buffer(buf->cur->table, buf->cur_idx,
901 buf->output_off);
902 wrmsrl(MSR_IA32_RTIT_STATUS, 0);
903 pt_config(event);
904 }
905}
906
907/*
908 * PMU callbacks
909 */
910
911static void pt_event_start(struct perf_event *event, int mode)
912{
913 struct pt *pt = this_cpu_ptr(&pt_ctx);
914 struct pt_buffer *buf = perf_get_aux(&pt->handle);
915
916 if (pt_is_running() || !buf || pt_buffer_is_full(buf, pt)) {
917 event->hw.state = PERF_HES_STOPPED;
918 return;
919 }
920
921 ACCESS_ONCE(pt->handle_nmi) = 1;
922 event->hw.state = 0;
923
924 pt_config_buffer(buf->cur->table, buf->cur_idx,
925 buf->output_off);
926 wrmsrl(MSR_IA32_RTIT_STATUS, 0);
927 pt_config(event);
928}
929
930static void pt_event_stop(struct perf_event *event, int mode)
931{
932 struct pt *pt = this_cpu_ptr(&pt_ctx);
933
934 /*
935 * Protect against the PMI racing with disabling wrmsr,
936 * see comment in intel_pt_interrupt().
937 */
938 ACCESS_ONCE(pt->handle_nmi) = 0;
939 pt_config_start(false);
940
941 if (event->hw.state == PERF_HES_STOPPED)
942 return;
943
944 event->hw.state = PERF_HES_STOPPED;
945
946 if (mode & PERF_EF_UPDATE) {
947 struct pt *pt = this_cpu_ptr(&pt_ctx);
948 struct pt_buffer *buf = perf_get_aux(&pt->handle);
949
950 if (!buf)
951 return;
952
953 if (WARN_ON_ONCE(pt->handle.event != event))
954 return;
955
956 pt_read_offset(buf);
957
958 pt_handle_status(pt);
959
960 pt_update_head(pt);
961 }
962}
963
964static void pt_event_del(struct perf_event *event, int mode)
965{
966 struct pt *pt = this_cpu_ptr(&pt_ctx);
967 struct pt_buffer *buf;
968
969 pt_event_stop(event, PERF_EF_UPDATE);
970
971 buf = perf_get_aux(&pt->handle);
972
973 if (buf) {
974 if (buf->snapshot)
975 pt->handle.head =
976 local_xchg(&buf->data_size,
977 buf->nr_pages << PAGE_SHIFT);
978 perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
979 local_xchg(&buf->lost, 0));
980 }
981}
982
983static int pt_event_add(struct perf_event *event, int mode)
984{
985 struct pt_buffer *buf;
986 struct pt *pt = this_cpu_ptr(&pt_ctx);
987 struct hw_perf_event *hwc = &event->hw;
988 int ret = -EBUSY;
989
990 if (pt->handle.event)
991 goto out;
992
993 buf = perf_aux_output_begin(&pt->handle, event);
994 if (!buf) {
995 ret = -EINVAL;
996 goto out;
997 }
998
999 pt_buffer_reset_offsets(buf, pt->handle.head);
1000 if (!buf->snapshot) {
1001 ret = pt_buffer_reset_markers(buf, &pt->handle);
1002 if (ret) {
1003 perf_aux_output_end(&pt->handle, 0, true);
1004 goto out;
1005 }
1006 }
1007
1008 if (mode & PERF_EF_START) {
1009 pt_event_start(event, 0);
1010 if (hwc->state == PERF_HES_STOPPED) {
1011 pt_event_del(event, 0);
1012 ret = -EBUSY;
1013 }
1014 } else {
1015 hwc->state = PERF_HES_STOPPED;
1016 }
1017
1018 ret = 0;
1019out:
1020
1021 if (ret)
1022 hwc->state = PERF_HES_STOPPED;
1023
1024 return ret;
1025}
1026
1027static void pt_event_read(struct perf_event *event)
1028{
1029}
1030
1031static void pt_event_destroy(struct perf_event *event)
1032{
1033 x86_del_exclusive(x86_lbr_exclusive_pt);
1034}
1035
1036static int pt_event_init(struct perf_event *event)
1037{
1038 if (event->attr.type != pt_pmu.pmu.type)
1039 return -ENOENT;
1040
1041 if (!pt_event_valid(event))
1042 return -EINVAL;
1043
1044 if (x86_add_exclusive(x86_lbr_exclusive_pt))
1045 return -EBUSY;
1046
1047 event->destroy = pt_event_destroy;
1048
1049 return 0;
1050}
1051
1052static __init int pt_init(void)
1053{
1054 int ret, cpu, prior_warn = 0;
1055
1056 BUILD_BUG_ON(sizeof(struct topa) > PAGE_SIZE);
1057 get_online_cpus();
1058 for_each_online_cpu(cpu) {
1059 u64 ctl;
1060
1061 ret = rdmsrl_safe_on_cpu(cpu, MSR_IA32_RTIT_CTL, &ctl);
1062 if (!ret && (ctl & RTIT_CTL_TRACEEN))
1063 prior_warn++;
1064 }
1065 put_online_cpus();
1066
1067 if (prior_warn) {
1068 x86_add_exclusive(x86_lbr_exclusive_pt);
1069 pr_warn("PT is enabled at boot time, doing nothing\n");
1070
1071 return -EBUSY;
1072 }
1073
1074 ret = pt_pmu_hw_init();
1075 if (ret)
1076 return ret;
1077
1078 if (!pt_cap_get(PT_CAP_topa_output)) {
1079 pr_warn("ToPA output is not supported on this CPU\n");
1080 return -ENODEV;
1081 }
1082
1083 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
1084 pt_pmu.pmu.capabilities =
1085 PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_SW_DOUBLEBUF;
1086
1087 pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE;
1088 pt_pmu.pmu.attr_groups = pt_attr_groups;
1089 pt_pmu.pmu.task_ctx_nr = perf_sw_context;
1090 pt_pmu.pmu.event_init = pt_event_init;
1091 pt_pmu.pmu.add = pt_event_add;
1092 pt_pmu.pmu.del = pt_event_del;
1093 pt_pmu.pmu.start = pt_event_start;
1094 pt_pmu.pmu.stop = pt_event_stop;
1095 pt_pmu.pmu.read = pt_event_read;
1096 pt_pmu.pmu.setup_aux = pt_buffer_setup_aux;
1097 pt_pmu.pmu.free_aux = pt_buffer_free_aux;
1098 ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
1099
1100 return ret;
1101}
1102
1103module_init(pt_init);
diff --git a/arch/x86/kernel/cpu/perf_event_intel_uncore_snbep.c b/arch/x86/kernel/cpu/perf_event_intel_uncore_snbep.c
index 21af6149edf2..12d9548457e7 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_uncore_snbep.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_uncore_snbep.c
@@ -1132,8 +1132,7 @@ static int snbep_pci2phy_map_init(int devid)
1132 } 1132 }
1133 } 1133 }
1134 1134
1135 if (ubox_dev) 1135 pci_dev_put(ubox_dev);
1136 pci_dev_put(ubox_dev);
1137 1136
1138 return err ? pcibios_err_to_errno(err) : 0; 1137 return err ? pcibios_err_to_errno(err) : 0;
1139} 1138}
diff --git a/arch/x86/kernel/cpu/scattered.c b/arch/x86/kernel/cpu/scattered.c
index 60639093d536..3d423a101fae 100644
--- a/arch/x86/kernel/cpu/scattered.c
+++ b/arch/x86/kernel/cpu/scattered.c
@@ -41,6 +41,7 @@ void init_scattered_cpuid_features(struct cpuinfo_x86 *c)
41 { X86_FEATURE_HWP_ACT_WINDOW, CR_EAX, 9, 0x00000006, 0 }, 41 { X86_FEATURE_HWP_ACT_WINDOW, CR_EAX, 9, 0x00000006, 0 },
42 { X86_FEATURE_HWP_EPP, CR_EAX,10, 0x00000006, 0 }, 42 { X86_FEATURE_HWP_EPP, CR_EAX,10, 0x00000006, 0 },
43 { X86_FEATURE_HWP_PKG_REQ, CR_EAX,11, 0x00000006, 0 }, 43 { X86_FEATURE_HWP_PKG_REQ, CR_EAX,11, 0x00000006, 0 },
44 { X86_FEATURE_INTEL_PT, CR_EBX,25, 0x00000007, 0 },
44 { X86_FEATURE_APERFMPERF, CR_ECX, 0, 0x00000006, 0 }, 45 { X86_FEATURE_APERFMPERF, CR_ECX, 0, 0x00000006, 0 },
45 { X86_FEATURE_EPB, CR_ECX, 3, 0x00000006, 0 }, 46 { X86_FEATURE_EPB, CR_ECX, 3, 0x00000006, 0 },
46 { X86_FEATURE_HW_PSTATE, CR_EDX, 7, 0x80000007, 0 }, 47 { X86_FEATURE_HW_PSTATE, CR_EDX, 7, 0x80000007, 0 },
diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 24d079604fd5..1deffe6cc873 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -354,6 +354,7 @@ int __copy_instruction(u8 *dest, u8 *src)
354{ 354{
355 struct insn insn; 355 struct insn insn;
356 kprobe_opcode_t buf[MAX_INSN_SIZE]; 356 kprobe_opcode_t buf[MAX_INSN_SIZE];
357 int length;
357 unsigned long recovered_insn = 358 unsigned long recovered_insn =
358 recover_probed_instruction(buf, (unsigned long)src); 359 recover_probed_instruction(buf, (unsigned long)src);
359 360
@@ -361,16 +362,18 @@ int __copy_instruction(u8 *dest, u8 *src)
361 return 0; 362 return 0;
362 kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE); 363 kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
363 insn_get_length(&insn); 364 insn_get_length(&insn);
365 length = insn.length;
366
364 /* Another subsystem puts a breakpoint, failed to recover */ 367 /* Another subsystem puts a breakpoint, failed to recover */
365 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) 368 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
366 return 0; 369 return 0;
367 memcpy(dest, insn.kaddr, insn.length); 370 memcpy(dest, insn.kaddr, length);
368 371
369#ifdef CONFIG_X86_64 372#ifdef CONFIG_X86_64
370 if (insn_rip_relative(&insn)) { 373 if (insn_rip_relative(&insn)) {
371 s64 newdisp; 374 s64 newdisp;
372 u8 *disp; 375 u8 *disp;
373 kernel_insn_init(&insn, dest, insn.length); 376 kernel_insn_init(&insn, dest, length);
374 insn_get_displacement(&insn); 377 insn_get_displacement(&insn);
375 /* 378 /*
376 * The copied instruction uses the %rip-relative addressing 379 * The copied instruction uses the %rip-relative addressing
@@ -394,7 +397,7 @@ int __copy_instruction(u8 *dest, u8 *src)
394 *(s32 *) disp = (s32) newdisp; 397 *(s32 *) disp = (s32) newdisp;
395 } 398 }
396#endif 399#endif
397 return insn.length; 400 return length;
398} 401}
399 402
400static int arch_copy_kprobe(struct kprobe *p) 403static int arch_copy_kprobe(struct kprobe *p)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index bbfceb756452..c2e21113ecc0 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -113,8 +113,6 @@ struct bpf_prog_type_list {
113 enum bpf_prog_type type; 113 enum bpf_prog_type type;
114}; 114};
115 115
116void bpf_register_prog_type(struct bpf_prog_type_list *tl);
117
118struct bpf_prog; 116struct bpf_prog;
119 117
120struct bpf_prog_aux { 118struct bpf_prog_aux {
@@ -129,11 +127,25 @@ struct bpf_prog_aux {
129}; 127};
130 128
131#ifdef CONFIG_BPF_SYSCALL 129#ifdef CONFIG_BPF_SYSCALL
130void bpf_register_prog_type(struct bpf_prog_type_list *tl);
131
132void bpf_prog_put(struct bpf_prog *prog); 132void bpf_prog_put(struct bpf_prog *prog);
133struct bpf_prog *bpf_prog_get(u32 ufd);
133#else 134#else
134static inline void bpf_prog_put(struct bpf_prog *prog) {} 135static inline void bpf_register_prog_type(struct bpf_prog_type_list *tl)
136{
137}
138
139static inline struct bpf_prog *bpf_prog_get(u32 ufd)
140{
141 return ERR_PTR(-EOPNOTSUPP);
142}
143
144static inline void bpf_prog_put(struct bpf_prog *prog)
145{
146}
135#endif 147#endif
136struct bpf_prog *bpf_prog_get(u32 ufd); 148
137/* verify correctness of eBPF program */ 149/* verify correctness of eBPF program */
138int bpf_check(struct bpf_prog *fp, union bpf_attr *attr); 150int bpf_check(struct bpf_prog *fp, union bpf_attr *attr);
139 151
diff --git a/include/linux/ftrace_event.h b/include/linux/ftrace_event.h
index 112cf49d9576..46e83c2156c6 100644
--- a/include/linux/ftrace_event.h
+++ b/include/linux/ftrace_event.h
@@ -13,6 +13,7 @@ struct trace_array;
13struct trace_buffer; 13struct trace_buffer;
14struct tracer; 14struct tracer;
15struct dentry; 15struct dentry;
16struct bpf_prog;
16 17
17struct trace_print_flags { 18struct trace_print_flags {
18 unsigned long mask; 19 unsigned long mask;
@@ -252,6 +253,7 @@ enum {
252 TRACE_EVENT_FL_WAS_ENABLED_BIT, 253 TRACE_EVENT_FL_WAS_ENABLED_BIT,
253 TRACE_EVENT_FL_USE_CALL_FILTER_BIT, 254 TRACE_EVENT_FL_USE_CALL_FILTER_BIT,
254 TRACE_EVENT_FL_TRACEPOINT_BIT, 255 TRACE_EVENT_FL_TRACEPOINT_BIT,
256 TRACE_EVENT_FL_KPROBE_BIT,
255}; 257};
256 258
257/* 259/*
@@ -265,6 +267,7 @@ enum {
265 * it is best to clear the buffers that used it). 267 * it is best to clear the buffers that used it).
266 * USE_CALL_FILTER - For ftrace internal events, don't use file filter 268 * USE_CALL_FILTER - For ftrace internal events, don't use file filter
267 * TRACEPOINT - Event is a tracepoint 269 * TRACEPOINT - Event is a tracepoint
270 * KPROBE - Event is a kprobe
268 */ 271 */
269enum { 272enum {
270 TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT), 273 TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
@@ -274,6 +277,7 @@ enum {
274 TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT), 277 TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
275 TRACE_EVENT_FL_USE_CALL_FILTER = (1 << TRACE_EVENT_FL_USE_CALL_FILTER_BIT), 278 TRACE_EVENT_FL_USE_CALL_FILTER = (1 << TRACE_EVENT_FL_USE_CALL_FILTER_BIT),
276 TRACE_EVENT_FL_TRACEPOINT = (1 << TRACE_EVENT_FL_TRACEPOINT_BIT), 279 TRACE_EVENT_FL_TRACEPOINT = (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
280 TRACE_EVENT_FL_KPROBE = (1 << TRACE_EVENT_FL_KPROBE_BIT),
277}; 281};
278 282
279struct ftrace_event_call { 283struct ftrace_event_call {
@@ -303,6 +307,7 @@ struct ftrace_event_call {
303#ifdef CONFIG_PERF_EVENTS 307#ifdef CONFIG_PERF_EVENTS
304 int perf_refcount; 308 int perf_refcount;
305 struct hlist_head __percpu *perf_events; 309 struct hlist_head __percpu *perf_events;
310 struct bpf_prog *prog;
306 311
307 int (*perf_perm)(struct ftrace_event_call *, 312 int (*perf_perm)(struct ftrace_event_call *,
308 struct perf_event *); 313 struct perf_event *);
@@ -548,6 +553,15 @@ event_trigger_unlock_commit_regs(struct ftrace_event_file *file,
548 event_triggers_post_call(file, tt); 553 event_triggers_post_call(file, tt);
549} 554}
550 555
556#ifdef CONFIG_BPF_SYSCALL
557unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx);
558#else
559static inline unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
560{
561 return 1;
562}
563#endif
564
551enum { 565enum {
552 FILTER_OTHER = 0, 566 FILTER_OTHER = 0,
553 FILTER_STATIC_STRING, 567 FILTER_STATIC_STRING,
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 2b621982938d..61992cf2e977 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -53,6 +53,7 @@ struct perf_guest_info_callbacks {
53#include <linux/sysfs.h> 53#include <linux/sysfs.h>
54#include <linux/perf_regs.h> 54#include <linux/perf_regs.h>
55#include <linux/workqueue.h> 55#include <linux/workqueue.h>
56#include <linux/cgroup.h>
56#include <asm/local.h> 57#include <asm/local.h>
57 58
58struct perf_callchain_entry { 59struct perf_callchain_entry {
@@ -118,10 +119,19 @@ struct hw_perf_event {
118 struct hrtimer hrtimer; 119 struct hrtimer hrtimer;
119 }; 120 };
120 struct { /* tracepoint */ 121 struct { /* tracepoint */
121 struct task_struct *tp_target;
122 /* for tp_event->class */ 122 /* for tp_event->class */
123 struct list_head tp_list; 123 struct list_head tp_list;
124 }; 124 };
125 struct { /* intel_cqm */
126 int cqm_state;
127 int cqm_rmid;
128 struct list_head cqm_events_entry;
129 struct list_head cqm_groups_entry;
130 struct list_head cqm_group_entry;
131 };
132 struct { /* itrace */
133 int itrace_started;
134 };
125#ifdef CONFIG_HAVE_HW_BREAKPOINT 135#ifdef CONFIG_HAVE_HW_BREAKPOINT
126 struct { /* breakpoint */ 136 struct { /* breakpoint */
127 /* 137 /*
@@ -129,12 +139,12 @@ struct hw_perf_event {
129 * problem hw_breakpoint has with context 139 * problem hw_breakpoint has with context
130 * creation and event initalization. 140 * creation and event initalization.
131 */ 141 */
132 struct task_struct *bp_target;
133 struct arch_hw_breakpoint info; 142 struct arch_hw_breakpoint info;
134 struct list_head bp_list; 143 struct list_head bp_list;
135 }; 144 };
136#endif 145#endif
137 }; 146 };
147 struct task_struct *target;
138 int state; 148 int state;
139 local64_t prev_count; 149 local64_t prev_count;
140 u64 sample_period; 150 u64 sample_period;
@@ -166,6 +176,11 @@ struct perf_event;
166 * pmu::capabilities flags 176 * pmu::capabilities flags
167 */ 177 */
168#define PERF_PMU_CAP_NO_INTERRUPT 0x01 178#define PERF_PMU_CAP_NO_INTERRUPT 0x01
179#define PERF_PMU_CAP_NO_NMI 0x02
180#define PERF_PMU_CAP_AUX_NO_SG 0x04
181#define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08
182#define PERF_PMU_CAP_EXCLUSIVE 0x10
183#define PERF_PMU_CAP_ITRACE 0x20
169 184
170/** 185/**
171 * struct pmu - generic performance monitoring unit 186 * struct pmu - generic performance monitoring unit
@@ -186,6 +201,7 @@ struct pmu {
186 201
187 int * __percpu pmu_disable_count; 202 int * __percpu pmu_disable_count;
188 struct perf_cpu_context * __percpu pmu_cpu_context; 203 struct perf_cpu_context * __percpu pmu_cpu_context;
204 atomic_t exclusive_cnt; /* < 0: cpu; > 0: tsk */
189 int task_ctx_nr; 205 int task_ctx_nr;
190 int hrtimer_interval_ms; 206 int hrtimer_interval_ms;
191 207
@@ -262,9 +278,32 @@ struct pmu {
262 int (*event_idx) (struct perf_event *event); /*optional */ 278 int (*event_idx) (struct perf_event *event); /*optional */
263 279
264 /* 280 /*
265 * flush branch stack on context-switches (needed in cpu-wide mode) 281 * context-switches callback
282 */
283 void (*sched_task) (struct perf_event_context *ctx,
284 bool sched_in);
285 /*
286 * PMU specific data size
287 */
288 size_t task_ctx_size;
289
290
291 /*
292 * Return the count value for a counter.
293 */
294 u64 (*count) (struct perf_event *event); /*optional*/
295
296 /*
297 * Set up pmu-private data structures for an AUX area
266 */ 298 */
267 void (*flush_branch_stack) (void); 299 void *(*setup_aux) (int cpu, void **pages,
300 int nr_pages, bool overwrite);
301 /* optional */
302
303 /*
304 * Free pmu-private AUX data structures
305 */
306 void (*free_aux) (void *aux); /* optional */
268}; 307};
269 308
270/** 309/**
@@ -300,6 +339,7 @@ struct swevent_hlist {
300#define PERF_ATTACH_CONTEXT 0x01 339#define PERF_ATTACH_CONTEXT 0x01
301#define PERF_ATTACH_GROUP 0x02 340#define PERF_ATTACH_GROUP 0x02
302#define PERF_ATTACH_TASK 0x04 341#define PERF_ATTACH_TASK 0x04
342#define PERF_ATTACH_TASK_DATA 0x08
303 343
304struct perf_cgroup; 344struct perf_cgroup;
305struct ring_buffer; 345struct ring_buffer;
@@ -438,6 +478,7 @@ struct perf_event {
438 struct pid_namespace *ns; 478 struct pid_namespace *ns;
439 u64 id; 479 u64 id;
440 480
481 u64 (*clock)(void);
441 perf_overflow_handler_t overflow_handler; 482 perf_overflow_handler_t overflow_handler;
442 void *overflow_handler_context; 483 void *overflow_handler_context;
443 484
@@ -504,7 +545,7 @@ struct perf_event_context {
504 u64 generation; 545 u64 generation;
505 int pin_count; 546 int pin_count;
506 int nr_cgroups; /* cgroup evts */ 547 int nr_cgroups; /* cgroup evts */
507 int nr_branch_stack; /* branch_stack evt */ 548 void *task_ctx_data; /* pmu specific data */
508 struct rcu_head rcu_head; 549 struct rcu_head rcu_head;
509 550
510 struct delayed_work orphans_remove; 551 struct delayed_work orphans_remove;
@@ -536,12 +577,52 @@ struct perf_output_handle {
536 struct ring_buffer *rb; 577 struct ring_buffer *rb;
537 unsigned long wakeup; 578 unsigned long wakeup;
538 unsigned long size; 579 unsigned long size;
539 void *addr; 580 union {
581 void *addr;
582 unsigned long head;
583 };
540 int page; 584 int page;
541}; 585};
542 586
587#ifdef CONFIG_CGROUP_PERF
588
589/*
590 * perf_cgroup_info keeps track of time_enabled for a cgroup.
591 * This is a per-cpu dynamically allocated data structure.
592 */
593struct perf_cgroup_info {
594 u64 time;
595 u64 timestamp;
596};
597
598struct perf_cgroup {
599 struct cgroup_subsys_state css;
600 struct perf_cgroup_info __percpu *info;
601};
602
603/*
604 * Must ensure cgroup is pinned (css_get) before calling
605 * this function. In other words, we cannot call this function
606 * if there is no cgroup event for the current CPU context.
607 */
608static inline struct perf_cgroup *
609perf_cgroup_from_task(struct task_struct *task)
610{
611 return container_of(task_css(task, perf_event_cgrp_id),
612 struct perf_cgroup, css);
613}
614#endif /* CONFIG_CGROUP_PERF */
615
543#ifdef CONFIG_PERF_EVENTS 616#ifdef CONFIG_PERF_EVENTS
544 617
618extern void *perf_aux_output_begin(struct perf_output_handle *handle,
619 struct perf_event *event);
620extern void perf_aux_output_end(struct perf_output_handle *handle,
621 unsigned long size, bool truncated);
622extern int perf_aux_output_skip(struct perf_output_handle *handle,
623 unsigned long size);
624extern void *perf_get_aux(struct perf_output_handle *handle);
625
545extern int perf_pmu_register(struct pmu *pmu, const char *name, int type); 626extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
546extern void perf_pmu_unregister(struct pmu *pmu); 627extern void perf_pmu_unregister(struct pmu *pmu);
547 628
@@ -558,6 +639,8 @@ extern void perf_event_delayed_put(struct task_struct *task);
558extern void perf_event_print_debug(void); 639extern void perf_event_print_debug(void);
559extern void perf_pmu_disable(struct pmu *pmu); 640extern void perf_pmu_disable(struct pmu *pmu);
560extern void perf_pmu_enable(struct pmu *pmu); 641extern void perf_pmu_enable(struct pmu *pmu);
642extern void perf_sched_cb_dec(struct pmu *pmu);
643extern void perf_sched_cb_inc(struct pmu *pmu);
561extern int perf_event_task_disable(void); 644extern int perf_event_task_disable(void);
562extern int perf_event_task_enable(void); 645extern int perf_event_task_enable(void);
563extern int perf_event_refresh(struct perf_event *event, int refresh); 646extern int perf_event_refresh(struct perf_event *event, int refresh);
@@ -731,6 +814,11 @@ static inline void perf_event_task_sched_out(struct task_struct *prev,
731 __perf_event_task_sched_out(prev, next); 814 __perf_event_task_sched_out(prev, next);
732} 815}
733 816
817static inline u64 __perf_event_count(struct perf_event *event)
818{
819 return local64_read(&event->count) + atomic64_read(&event->child_count);
820}
821
734extern void perf_event_mmap(struct vm_area_struct *vma); 822extern void perf_event_mmap(struct vm_area_struct *vma);
735extern struct perf_guest_info_callbacks *perf_guest_cbs; 823extern struct perf_guest_info_callbacks *perf_guest_cbs;
736extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks); 824extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
@@ -800,6 +888,16 @@ static inline bool has_branch_stack(struct perf_event *event)
800 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK; 888 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
801} 889}
802 890
891static inline bool needs_branch_stack(struct perf_event *event)
892{
893 return event->attr.branch_sample_type != 0;
894}
895
896static inline bool has_aux(struct perf_event *event)
897{
898 return event->pmu->setup_aux;
899}
900
803extern int perf_output_begin(struct perf_output_handle *handle, 901extern int perf_output_begin(struct perf_output_handle *handle,
804 struct perf_event *event, unsigned int size); 902 struct perf_event *event, unsigned int size);
805extern void perf_output_end(struct perf_output_handle *handle); 903extern void perf_output_end(struct perf_output_handle *handle);
@@ -815,6 +913,17 @@ extern void perf_event_disable(struct perf_event *event);
815extern int __perf_event_disable(void *info); 913extern int __perf_event_disable(void *info);
816extern void perf_event_task_tick(void); 914extern void perf_event_task_tick(void);
817#else /* !CONFIG_PERF_EVENTS: */ 915#else /* !CONFIG_PERF_EVENTS: */
916static inline void *
917perf_aux_output_begin(struct perf_output_handle *handle,
918 struct perf_event *event) { return NULL; }
919static inline void
920perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
921 bool truncated) { }
922static inline int
923perf_aux_output_skip(struct perf_output_handle *handle,
924 unsigned long size) { return -EINVAL; }
925static inline void *
926perf_get_aux(struct perf_output_handle *handle) { return NULL; }
818static inline void 927static inline void
819perf_event_task_sched_in(struct task_struct *prev, 928perf_event_task_sched_in(struct task_struct *prev,
820 struct task_struct *task) { } 929 struct task_struct *task) { }
diff --git a/include/linux/watchdog.h b/include/linux/watchdog.h
index 395b70e0eccf..a746bf5216f8 100644
--- a/include/linux/watchdog.h
+++ b/include/linux/watchdog.h
@@ -137,4 +137,12 @@ extern int watchdog_init_timeout(struct watchdog_device *wdd,
137extern int watchdog_register_device(struct watchdog_device *); 137extern int watchdog_register_device(struct watchdog_device *);
138extern void watchdog_unregister_device(struct watchdog_device *); 138extern void watchdog_unregister_device(struct watchdog_device *);
139 139
140#ifdef CONFIG_HARDLOCKUP_DETECTOR
141void watchdog_nmi_disable_all(void);
142void watchdog_nmi_enable_all(void);
143#else
144static inline void watchdog_nmi_disable_all(void) {}
145static inline void watchdog_nmi_enable_all(void) {}
146#endif
147
140#endif /* ifndef _LINUX_WATCHDOG_H */ 148#endif /* ifndef _LINUX_WATCHDOG_H */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 45da7ec7d274..cc47ef41076a 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -118,6 +118,7 @@ enum bpf_map_type {
118enum bpf_prog_type { 118enum bpf_prog_type {
119 BPF_PROG_TYPE_UNSPEC, 119 BPF_PROG_TYPE_UNSPEC,
120 BPF_PROG_TYPE_SOCKET_FILTER, 120 BPF_PROG_TYPE_SOCKET_FILTER,
121 BPF_PROG_TYPE_KPROBE,
121}; 122};
122 123
123/* flags for BPF_MAP_UPDATE_ELEM command */ 124/* flags for BPF_MAP_UPDATE_ELEM command */
@@ -151,6 +152,7 @@ union bpf_attr {
151 __u32 log_level; /* verbosity level of verifier */ 152 __u32 log_level; /* verbosity level of verifier */
152 __u32 log_size; /* size of user buffer */ 153 __u32 log_size; /* size of user buffer */
153 __aligned_u64 log_buf; /* user supplied buffer */ 154 __aligned_u64 log_buf; /* user supplied buffer */
155 __u32 kern_version; /* checked when prog_type=kprobe */
154 }; 156 };
155} __attribute__((aligned(8))); 157} __attribute__((aligned(8)));
156 158
@@ -162,6 +164,9 @@ enum bpf_func_id {
162 BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */ 164 BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
163 BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */ 165 BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
164 BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */ 166 BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
167 BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */
168 BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */
169 BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */
165 __BPF_FUNC_MAX_ID, 170 __BPF_FUNC_MAX_ID,
166}; 171};
167 172
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 9b79abbd1ab8..309211b3eb67 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -152,21 +152,42 @@ enum perf_event_sample_format {
152 * The branch types can be combined, however BRANCH_ANY covers all types 152 * The branch types can be combined, however BRANCH_ANY covers all types
153 * of branches and therefore it supersedes all the other types. 153 * of branches and therefore it supersedes all the other types.
154 */ 154 */
155enum perf_branch_sample_type_shift {
156 PERF_SAMPLE_BRANCH_USER_SHIFT = 0, /* user branches */
157 PERF_SAMPLE_BRANCH_KERNEL_SHIFT = 1, /* kernel branches */
158 PERF_SAMPLE_BRANCH_HV_SHIFT = 2, /* hypervisor branches */
159
160 PERF_SAMPLE_BRANCH_ANY_SHIFT = 3, /* any branch types */
161 PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT = 4, /* any call branch */
162 PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT = 5, /* any return branch */
163 PERF_SAMPLE_BRANCH_IND_CALL_SHIFT = 6, /* indirect calls */
164 PERF_SAMPLE_BRANCH_ABORT_TX_SHIFT = 7, /* transaction aborts */
165 PERF_SAMPLE_BRANCH_IN_TX_SHIFT = 8, /* in transaction */
166 PERF_SAMPLE_BRANCH_NO_TX_SHIFT = 9, /* not in transaction */
167 PERF_SAMPLE_BRANCH_COND_SHIFT = 10, /* conditional branches */
168
169 PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT = 11, /* call/ret stack */
170
171 PERF_SAMPLE_BRANCH_MAX_SHIFT /* non-ABI */
172};
173
155enum perf_branch_sample_type { 174enum perf_branch_sample_type {
156 PERF_SAMPLE_BRANCH_USER = 1U << 0, /* user branches */ 175 PERF_SAMPLE_BRANCH_USER = 1U << PERF_SAMPLE_BRANCH_USER_SHIFT,
157 PERF_SAMPLE_BRANCH_KERNEL = 1U << 1, /* kernel branches */ 176 PERF_SAMPLE_BRANCH_KERNEL = 1U << PERF_SAMPLE_BRANCH_KERNEL_SHIFT,
158 PERF_SAMPLE_BRANCH_HV = 1U << 2, /* hypervisor branches */ 177 PERF_SAMPLE_BRANCH_HV = 1U << PERF_SAMPLE_BRANCH_HV_SHIFT,
159 178
160 PERF_SAMPLE_BRANCH_ANY = 1U << 3, /* any branch types */ 179 PERF_SAMPLE_BRANCH_ANY = 1U << PERF_SAMPLE_BRANCH_ANY_SHIFT,
161 PERF_SAMPLE_BRANCH_ANY_CALL = 1U << 4, /* any call branch */ 180 PERF_SAMPLE_BRANCH_ANY_CALL = 1U << PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT,
162 PERF_SAMPLE_BRANCH_ANY_RETURN = 1U << 5, /* any return branch */ 181 PERF_SAMPLE_BRANCH_ANY_RETURN = 1U << PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT,
163 PERF_SAMPLE_BRANCH_IND_CALL = 1U << 6, /* indirect calls */ 182 PERF_SAMPLE_BRANCH_IND_CALL = 1U << PERF_SAMPLE_BRANCH_IND_CALL_SHIFT,
164 PERF_SAMPLE_BRANCH_ABORT_TX = 1U << 7, /* transaction aborts */ 183 PERF_SAMPLE_BRANCH_ABORT_TX = 1U << PERF_SAMPLE_BRANCH_ABORT_TX_SHIFT,
165 PERF_SAMPLE_BRANCH_IN_TX = 1U << 8, /* in transaction */ 184 PERF_SAMPLE_BRANCH_IN_TX = 1U << PERF_SAMPLE_BRANCH_IN_TX_SHIFT,
166 PERF_SAMPLE_BRANCH_NO_TX = 1U << 9, /* not in transaction */ 185 PERF_SAMPLE_BRANCH_NO_TX = 1U << PERF_SAMPLE_BRANCH_NO_TX_SHIFT,
167 PERF_SAMPLE_BRANCH_COND = 1U << 10, /* conditional branches */ 186 PERF_SAMPLE_BRANCH_COND = 1U << PERF_SAMPLE_BRANCH_COND_SHIFT,
168 187
169 PERF_SAMPLE_BRANCH_MAX = 1U << 11, /* non-ABI */ 188 PERF_SAMPLE_BRANCH_CALL_STACK = 1U << PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT,
189
190 PERF_SAMPLE_BRANCH_MAX = 1U << PERF_SAMPLE_BRANCH_MAX_SHIFT,
170}; 191};
171 192
172#define PERF_SAMPLE_BRANCH_PLM_ALL \ 193#define PERF_SAMPLE_BRANCH_PLM_ALL \
@@ -240,6 +261,7 @@ enum perf_event_read_format {
240#define PERF_ATTR_SIZE_VER3 96 /* add: sample_regs_user */ 261#define PERF_ATTR_SIZE_VER3 96 /* add: sample_regs_user */
241 /* add: sample_stack_user */ 262 /* add: sample_stack_user */
242#define PERF_ATTR_SIZE_VER4 104 /* add: sample_regs_intr */ 263#define PERF_ATTR_SIZE_VER4 104 /* add: sample_regs_intr */
264#define PERF_ATTR_SIZE_VER5 112 /* add: aux_watermark */
243 265
244/* 266/*
245 * Hardware event_id to monitor via a performance monitoring event: 267 * Hardware event_id to monitor via a performance monitoring event:
@@ -305,7 +327,8 @@ struct perf_event_attr {
305 exclude_callchain_user : 1, /* exclude user callchains */ 327 exclude_callchain_user : 1, /* exclude user callchains */
306 mmap2 : 1, /* include mmap with inode data */ 328 mmap2 : 1, /* include mmap with inode data */
307 comm_exec : 1, /* flag comm events that are due to an exec */ 329 comm_exec : 1, /* flag comm events that are due to an exec */
308 __reserved_1 : 39; 330 use_clockid : 1, /* use @clockid for time fields */
331 __reserved_1 : 38;
309 332
310 union { 333 union {
311 __u32 wakeup_events; /* wakeup every n events */ 334 __u32 wakeup_events; /* wakeup every n events */
@@ -334,8 +357,7 @@ struct perf_event_attr {
334 */ 357 */
335 __u32 sample_stack_user; 358 __u32 sample_stack_user;
336 359
337 /* Align to u64. */ 360 __s32 clockid;
338 __u32 __reserved_2;
339 /* 361 /*
340 * Defines set of regs to dump for each sample 362 * Defines set of regs to dump for each sample
341 * state captured on: 363 * state captured on:
@@ -345,6 +367,12 @@ struct perf_event_attr {
345 * See asm/perf_regs.h for details. 367 * See asm/perf_regs.h for details.
346 */ 368 */
347 __u64 sample_regs_intr; 369 __u64 sample_regs_intr;
370
371 /*
372 * Wakeup watermark for AUX area
373 */
374 __u32 aux_watermark;
375 __u32 __reserved_2; /* align to __u64 */
348}; 376};
349 377
350#define perf_flags(attr) (*(&(attr)->read_format + 1)) 378#define perf_flags(attr) (*(&(attr)->read_format + 1))
@@ -360,6 +388,7 @@ struct perf_event_attr {
360#define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5) 388#define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5)
361#define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *) 389#define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *)
362#define PERF_EVENT_IOC_ID _IOR('$', 7, __u64 *) 390#define PERF_EVENT_IOC_ID _IOR('$', 7, __u64 *)
391#define PERF_EVENT_IOC_SET_BPF _IOW('$', 8, __u32)
363 392
364enum perf_event_ioc_flags { 393enum perf_event_ioc_flags {
365 PERF_IOC_FLAG_GROUP = 1U << 0, 394 PERF_IOC_FLAG_GROUP = 1U << 0,
@@ -500,9 +529,30 @@ struct perf_event_mmap_page {
500 * In this case the kernel will not over-write unread data. 529 * In this case the kernel will not over-write unread data.
501 * 530 *
502 * See perf_output_put_handle() for the data ordering. 531 * See perf_output_put_handle() for the data ordering.
532 *
533 * data_{offset,size} indicate the location and size of the perf record
534 * buffer within the mmapped area.
503 */ 535 */
504 __u64 data_head; /* head in the data section */ 536 __u64 data_head; /* head in the data section */
505 __u64 data_tail; /* user-space written tail */ 537 __u64 data_tail; /* user-space written tail */
538 __u64 data_offset; /* where the buffer starts */
539 __u64 data_size; /* data buffer size */
540
541 /*
542 * AUX area is defined by aux_{offset,size} fields that should be set
543 * by the userspace, so that
544 *
545 * aux_offset >= data_offset + data_size
546 *
547 * prior to mmap()ing it. Size of the mmap()ed area should be aux_size.
548 *
549 * Ring buffer pointers aux_{head,tail} have the same semantics as
550 * data_{head,tail} and same ordering rules apply.
551 */
552 __u64 aux_head;
553 __u64 aux_tail;
554 __u64 aux_offset;
555 __u64 aux_size;
506}; 556};
507 557
508#define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0) 558#define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0)
@@ -725,6 +775,31 @@ enum perf_event_type {
725 */ 775 */
726 PERF_RECORD_MMAP2 = 10, 776 PERF_RECORD_MMAP2 = 10,
727 777
778 /*
779 * Records that new data landed in the AUX buffer part.
780 *
781 * struct {
782 * struct perf_event_header header;
783 *
784 * u64 aux_offset;
785 * u64 aux_size;
786 * u64 flags;
787 * struct sample_id sample_id;
788 * };
789 */
790 PERF_RECORD_AUX = 11,
791
792 /*
793 * Indicates that instruction trace has started
794 *
795 * struct {
796 * struct perf_event_header header;
797 * u32 pid;
798 * u32 tid;
799 * };
800 */
801 PERF_RECORD_ITRACE_START = 12,
802
728 PERF_RECORD_MAX, /* non-ABI */ 803 PERF_RECORD_MAX, /* non-ABI */
729}; 804};
730 805
@@ -742,6 +817,12 @@ enum perf_callchain_context {
742 PERF_CONTEXT_MAX = (__u64)-4095, 817 PERF_CONTEXT_MAX = (__u64)-4095,
743}; 818};
744 819
820/**
821 * PERF_RECORD_AUX::flags bits
822 */
823#define PERF_AUX_FLAG_TRUNCATED 0x01 /* record was truncated to fit */
824#define PERF_AUX_FLAG_OVERWRITE 0x02 /* snapshot from overwrite mode */
825
745#define PERF_FLAG_FD_NO_GROUP (1UL << 0) 826#define PERF_FLAG_FD_NO_GROUP (1UL << 0)
746#define PERF_FLAG_FD_OUTPUT (1UL << 1) 827#define PERF_FLAG_FD_OUTPUT (1UL << 1)
747#define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */ 828#define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */
diff --git a/init/Kconfig b/init/Kconfig
index 9a0592516f48..a905b7301e10 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1526,7 +1526,7 @@ config EVENTFD
1526 1526
1527# syscall, maps, verifier 1527# syscall, maps, verifier
1528config BPF_SYSCALL 1528config BPF_SYSCALL
1529 bool "Enable bpf() system call" if EXPERT 1529 bool "Enable bpf() system call"
1530 select ANON_INODES 1530 select ANON_INODES
1531 select BPF 1531 select BPF
1532 default n 1532 default n
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 536edc2be307..504c10b990ef 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -16,6 +16,7 @@
16#include <linux/file.h> 16#include <linux/file.h>
17#include <linux/license.h> 17#include <linux/license.h>
18#include <linux/filter.h> 18#include <linux/filter.h>
19#include <linux/version.h>
19 20
20static LIST_HEAD(bpf_map_types); 21static LIST_HEAD(bpf_map_types);
21 22
@@ -467,7 +468,7 @@ struct bpf_prog *bpf_prog_get(u32 ufd)
467} 468}
468 469
469/* last field in 'union bpf_attr' used by this command */ 470/* last field in 'union bpf_attr' used by this command */
470#define BPF_PROG_LOAD_LAST_FIELD log_buf 471#define BPF_PROG_LOAD_LAST_FIELD kern_version
471 472
472static int bpf_prog_load(union bpf_attr *attr) 473static int bpf_prog_load(union bpf_attr *attr)
473{ 474{
@@ -492,6 +493,10 @@ static int bpf_prog_load(union bpf_attr *attr)
492 if (attr->insn_cnt >= BPF_MAXINSNS) 493 if (attr->insn_cnt >= BPF_MAXINSNS)
493 return -EINVAL; 494 return -EINVAL;
494 495
496 if (type == BPF_PROG_TYPE_KPROBE &&
497 attr->kern_version != LINUX_VERSION_CODE)
498 return -EINVAL;
499
495 /* plain bpf_prog allocation */ 500 /* plain bpf_prog allocation */
496 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); 501 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
497 if (!prog) 502 if (!prog)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 2fabc0627165..06917d537302 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -34,14 +34,16 @@
34#include <linux/syscalls.h> 34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h> 35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h> 36#include <linux/kernel_stat.h>
37#include <linux/cgroup.h>
37#include <linux/perf_event.h> 38#include <linux/perf_event.h>
38#include <linux/ftrace_event.h> 39#include <linux/ftrace_event.h>
39#include <linux/hw_breakpoint.h> 40#include <linux/hw_breakpoint.h>
40#include <linux/mm_types.h> 41#include <linux/mm_types.h>
41#include <linux/cgroup.h>
42#include <linux/module.h> 42#include <linux/module.h>
43#include <linux/mman.h> 43#include <linux/mman.h>
44#include <linux/compat.h> 44#include <linux/compat.h>
45#include <linux/bpf.h>
46#include <linux/filter.h>
45 47
46#include "internal.h" 48#include "internal.h"
47 49
@@ -153,7 +155,7 @@ enum event_type_t {
153 */ 155 */
154struct static_key_deferred perf_sched_events __read_mostly; 156struct static_key_deferred perf_sched_events __read_mostly;
155static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); 157static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
156static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events); 158static DEFINE_PER_CPU(int, perf_sched_cb_usages);
157 159
158static atomic_t nr_mmap_events __read_mostly; 160static atomic_t nr_mmap_events __read_mostly;
159static atomic_t nr_comm_events __read_mostly; 161static atomic_t nr_comm_events __read_mostly;
@@ -327,6 +329,11 @@ static inline u64 perf_clock(void)
327 return local_clock(); 329 return local_clock();
328} 330}
329 331
332static inline u64 perf_event_clock(struct perf_event *event)
333{
334 return event->clock();
335}
336
330static inline struct perf_cpu_context * 337static inline struct perf_cpu_context *
331__get_cpu_context(struct perf_event_context *ctx) 338__get_cpu_context(struct perf_event_context *ctx)
332{ 339{
@@ -351,32 +358,6 @@ static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
351 358
352#ifdef CONFIG_CGROUP_PERF 359#ifdef CONFIG_CGROUP_PERF
353 360
354/*
355 * perf_cgroup_info keeps track of time_enabled for a cgroup.
356 * This is a per-cpu dynamically allocated data structure.
357 */
358struct perf_cgroup_info {
359 u64 time;
360 u64 timestamp;
361};
362
363struct perf_cgroup {
364 struct cgroup_subsys_state css;
365 struct perf_cgroup_info __percpu *info;
366};
367
368/*
369 * Must ensure cgroup is pinned (css_get) before calling
370 * this function. In other words, we cannot call this function
371 * if there is no cgroup event for the current CPU context.
372 */
373static inline struct perf_cgroup *
374perf_cgroup_from_task(struct task_struct *task)
375{
376 return container_of(task_css(task, perf_event_cgrp_id),
377 struct perf_cgroup, css);
378}
379
380static inline bool 361static inline bool
381perf_cgroup_match(struct perf_event *event) 362perf_cgroup_match(struct perf_event *event)
382{ 363{
@@ -905,6 +886,15 @@ static void get_ctx(struct perf_event_context *ctx)
905 WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); 886 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
906} 887}
907 888
889static void free_ctx(struct rcu_head *head)
890{
891 struct perf_event_context *ctx;
892
893 ctx = container_of(head, struct perf_event_context, rcu_head);
894 kfree(ctx->task_ctx_data);
895 kfree(ctx);
896}
897
908static void put_ctx(struct perf_event_context *ctx) 898static void put_ctx(struct perf_event_context *ctx)
909{ 899{
910 if (atomic_dec_and_test(&ctx->refcount)) { 900 if (atomic_dec_and_test(&ctx->refcount)) {
@@ -912,7 +902,7 @@ static void put_ctx(struct perf_event_context *ctx)
912 put_ctx(ctx->parent_ctx); 902 put_ctx(ctx->parent_ctx);
913 if (ctx->task) 903 if (ctx->task)
914 put_task_struct(ctx->task); 904 put_task_struct(ctx->task);
915 kfree_rcu(ctx, rcu_head); 905 call_rcu(&ctx->rcu_head, free_ctx);
916 } 906 }
917} 907}
918 908
@@ -1239,9 +1229,6 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1239 if (is_cgroup_event(event)) 1229 if (is_cgroup_event(event))
1240 ctx->nr_cgroups++; 1230 ctx->nr_cgroups++;
1241 1231
1242 if (has_branch_stack(event))
1243 ctx->nr_branch_stack++;
1244
1245 list_add_rcu(&event->event_entry, &ctx->event_list); 1232 list_add_rcu(&event->event_entry, &ctx->event_list);
1246 ctx->nr_events++; 1233 ctx->nr_events++;
1247 if (event->attr.inherit_stat) 1234 if (event->attr.inherit_stat)
@@ -1408,9 +1395,6 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1408 cpuctx->cgrp = NULL; 1395 cpuctx->cgrp = NULL;
1409 } 1396 }
1410 1397
1411 if (has_branch_stack(event))
1412 ctx->nr_branch_stack--;
1413
1414 ctx->nr_events--; 1398 ctx->nr_events--;
1415 if (event->attr.inherit_stat) 1399 if (event->attr.inherit_stat)
1416 ctx->nr_stat--; 1400 ctx->nr_stat--;
@@ -1847,6 +1831,7 @@ static void perf_set_shadow_time(struct perf_event *event,
1847#define MAX_INTERRUPTS (~0ULL) 1831#define MAX_INTERRUPTS (~0ULL)
1848 1832
1849static void perf_log_throttle(struct perf_event *event, int enable); 1833static void perf_log_throttle(struct perf_event *event, int enable);
1834static void perf_log_itrace_start(struct perf_event *event);
1850 1835
1851static int 1836static int
1852event_sched_in(struct perf_event *event, 1837event_sched_in(struct perf_event *event,
@@ -1881,6 +1866,12 @@ event_sched_in(struct perf_event *event,
1881 1866
1882 perf_pmu_disable(event->pmu); 1867 perf_pmu_disable(event->pmu);
1883 1868
1869 event->tstamp_running += tstamp - event->tstamp_stopped;
1870
1871 perf_set_shadow_time(event, ctx, tstamp);
1872
1873 perf_log_itrace_start(event);
1874
1884 if (event->pmu->add(event, PERF_EF_START)) { 1875 if (event->pmu->add(event, PERF_EF_START)) {
1885 event->state = PERF_EVENT_STATE_INACTIVE; 1876 event->state = PERF_EVENT_STATE_INACTIVE;
1886 event->oncpu = -1; 1877 event->oncpu = -1;
@@ -1888,10 +1879,6 @@ event_sched_in(struct perf_event *event,
1888 goto out; 1879 goto out;
1889 } 1880 }
1890 1881
1891 event->tstamp_running += tstamp - event->tstamp_stopped;
1892
1893 perf_set_shadow_time(event, ctx, tstamp);
1894
1895 if (!is_software_event(event)) 1882 if (!is_software_event(event))
1896 cpuctx->active_oncpu++; 1883 cpuctx->active_oncpu++;
1897 if (!ctx->nr_active++) 1884 if (!ctx->nr_active++)
@@ -2559,6 +2546,9 @@ static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2559 next->perf_event_ctxp[ctxn] = ctx; 2546 next->perf_event_ctxp[ctxn] = ctx;
2560 ctx->task = next; 2547 ctx->task = next;
2561 next_ctx->task = task; 2548 next_ctx->task = task;
2549
2550 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2551
2562 do_switch = 0; 2552 do_switch = 0;
2563 2553
2564 perf_event_sync_stat(ctx, next_ctx); 2554 perf_event_sync_stat(ctx, next_ctx);
@@ -2577,6 +2567,56 @@ unlock:
2577 } 2567 }
2578} 2568}
2579 2569
2570void perf_sched_cb_dec(struct pmu *pmu)
2571{
2572 this_cpu_dec(perf_sched_cb_usages);
2573}
2574
2575void perf_sched_cb_inc(struct pmu *pmu)
2576{
2577 this_cpu_inc(perf_sched_cb_usages);
2578}
2579
2580/*
2581 * This function provides the context switch callback to the lower code
2582 * layer. It is invoked ONLY when the context switch callback is enabled.
2583 */
2584static void perf_pmu_sched_task(struct task_struct *prev,
2585 struct task_struct *next,
2586 bool sched_in)
2587{
2588 struct perf_cpu_context *cpuctx;
2589 struct pmu *pmu;
2590 unsigned long flags;
2591
2592 if (prev == next)
2593 return;
2594
2595 local_irq_save(flags);
2596
2597 rcu_read_lock();
2598
2599 list_for_each_entry_rcu(pmu, &pmus, entry) {
2600 if (pmu->sched_task) {
2601 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2602
2603 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2604
2605 perf_pmu_disable(pmu);
2606
2607 pmu->sched_task(cpuctx->task_ctx, sched_in);
2608
2609 perf_pmu_enable(pmu);
2610
2611 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2612 }
2613 }
2614
2615 rcu_read_unlock();
2616
2617 local_irq_restore(flags);
2618}
2619
2580#define for_each_task_context_nr(ctxn) \ 2620#define for_each_task_context_nr(ctxn) \
2581 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) 2621 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2582 2622
@@ -2596,6 +2636,9 @@ void __perf_event_task_sched_out(struct task_struct *task,
2596{ 2636{
2597 int ctxn; 2637 int ctxn;
2598 2638
2639 if (__this_cpu_read(perf_sched_cb_usages))
2640 perf_pmu_sched_task(task, next, false);
2641
2599 for_each_task_context_nr(ctxn) 2642 for_each_task_context_nr(ctxn)
2600 perf_event_context_sched_out(task, ctxn, next); 2643 perf_event_context_sched_out(task, ctxn, next);
2601 2644
@@ -2755,64 +2798,6 @@ static void perf_event_context_sched_in(struct perf_event_context *ctx,
2755} 2798}
2756 2799
2757/* 2800/*
2758 * When sampling the branck stack in system-wide, it may be necessary
2759 * to flush the stack on context switch. This happens when the branch
2760 * stack does not tag its entries with the pid of the current task.
2761 * Otherwise it becomes impossible to associate a branch entry with a
2762 * task. This ambiguity is more likely to appear when the branch stack
2763 * supports priv level filtering and the user sets it to monitor only
2764 * at the user level (which could be a useful measurement in system-wide
2765 * mode). In that case, the risk is high of having a branch stack with
2766 * branch from multiple tasks. Flushing may mean dropping the existing
2767 * entries or stashing them somewhere in the PMU specific code layer.
2768 *
2769 * This function provides the context switch callback to the lower code
2770 * layer. It is invoked ONLY when there is at least one system-wide context
2771 * with at least one active event using taken branch sampling.
2772 */
2773static void perf_branch_stack_sched_in(struct task_struct *prev,
2774 struct task_struct *task)
2775{
2776 struct perf_cpu_context *cpuctx;
2777 struct pmu *pmu;
2778 unsigned long flags;
2779
2780 /* no need to flush branch stack if not changing task */
2781 if (prev == task)
2782 return;
2783
2784 local_irq_save(flags);
2785
2786 rcu_read_lock();
2787
2788 list_for_each_entry_rcu(pmu, &pmus, entry) {
2789 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2790
2791 /*
2792 * check if the context has at least one
2793 * event using PERF_SAMPLE_BRANCH_STACK
2794 */
2795 if (cpuctx->ctx.nr_branch_stack > 0
2796 && pmu->flush_branch_stack) {
2797
2798 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2799
2800 perf_pmu_disable(pmu);
2801
2802 pmu->flush_branch_stack();
2803
2804 perf_pmu_enable(pmu);
2805
2806 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2807 }
2808 }
2809
2810 rcu_read_unlock();
2811
2812 local_irq_restore(flags);
2813}
2814
2815/*
2816 * Called from scheduler to add the events of the current task 2801 * Called from scheduler to add the events of the current task
2817 * with interrupts disabled. 2802 * with interrupts disabled.
2818 * 2803 *
@@ -2844,9 +2829,8 @@ void __perf_event_task_sched_in(struct task_struct *prev,
2844 if (atomic_read(this_cpu_ptr(&perf_cgroup_events))) 2829 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2845 perf_cgroup_sched_in(prev, task); 2830 perf_cgroup_sched_in(prev, task);
2846 2831
2847 /* check for system-wide branch_stack events */ 2832 if (__this_cpu_read(perf_sched_cb_usages))
2848 if (atomic_read(this_cpu_ptr(&perf_branch_stack_events))) 2833 perf_pmu_sched_task(prev, task, true);
2849 perf_branch_stack_sched_in(prev, task);
2850} 2834}
2851 2835
2852static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) 2836static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
@@ -3220,7 +3204,10 @@ static void __perf_event_read(void *info)
3220 3204
3221static inline u64 perf_event_count(struct perf_event *event) 3205static inline u64 perf_event_count(struct perf_event *event)
3222{ 3206{
3223 return local64_read(&event->count) + atomic64_read(&event->child_count); 3207 if (event->pmu->count)
3208 return event->pmu->count(event);
3209
3210 return __perf_event_count(event);
3224} 3211}
3225 3212
3226static u64 perf_event_read(struct perf_event *event) 3213static u64 perf_event_read(struct perf_event *event)
@@ -3321,12 +3308,15 @@ errout:
3321 * Returns a matching context with refcount and pincount. 3308 * Returns a matching context with refcount and pincount.
3322 */ 3309 */
3323static struct perf_event_context * 3310static struct perf_event_context *
3324find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) 3311find_get_context(struct pmu *pmu, struct task_struct *task,
3312 struct perf_event *event)
3325{ 3313{
3326 struct perf_event_context *ctx, *clone_ctx = NULL; 3314 struct perf_event_context *ctx, *clone_ctx = NULL;
3327 struct perf_cpu_context *cpuctx; 3315 struct perf_cpu_context *cpuctx;
3316 void *task_ctx_data = NULL;
3328 unsigned long flags; 3317 unsigned long flags;
3329 int ctxn, err; 3318 int ctxn, err;
3319 int cpu = event->cpu;
3330 3320
3331 if (!task) { 3321 if (!task) {
3332 /* Must be root to operate on a CPU event: */ 3322 /* Must be root to operate on a CPU event: */
@@ -3354,11 +3344,24 @@ find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
3354 if (ctxn < 0) 3344 if (ctxn < 0)
3355 goto errout; 3345 goto errout;
3356 3346
3347 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3348 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3349 if (!task_ctx_data) {
3350 err = -ENOMEM;
3351 goto errout;
3352 }
3353 }
3354
3357retry: 3355retry:
3358 ctx = perf_lock_task_context(task, ctxn, &flags); 3356 ctx = perf_lock_task_context(task, ctxn, &flags);
3359 if (ctx) { 3357 if (ctx) {
3360 clone_ctx = unclone_ctx(ctx); 3358 clone_ctx = unclone_ctx(ctx);
3361 ++ctx->pin_count; 3359 ++ctx->pin_count;
3360
3361 if (task_ctx_data && !ctx->task_ctx_data) {
3362 ctx->task_ctx_data = task_ctx_data;
3363 task_ctx_data = NULL;
3364 }
3362 raw_spin_unlock_irqrestore(&ctx->lock, flags); 3365 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3363 3366
3364 if (clone_ctx) 3367 if (clone_ctx)
@@ -3369,6 +3372,11 @@ retry:
3369 if (!ctx) 3372 if (!ctx)
3370 goto errout; 3373 goto errout;
3371 3374
3375 if (task_ctx_data) {
3376 ctx->task_ctx_data = task_ctx_data;
3377 task_ctx_data = NULL;
3378 }
3379
3372 err = 0; 3380 err = 0;
3373 mutex_lock(&task->perf_event_mutex); 3381 mutex_lock(&task->perf_event_mutex);
3374 /* 3382 /*
@@ -3395,13 +3403,16 @@ retry:
3395 } 3403 }
3396 } 3404 }
3397 3405
3406 kfree(task_ctx_data);
3398 return ctx; 3407 return ctx;
3399 3408
3400errout: 3409errout:
3410 kfree(task_ctx_data);
3401 return ERR_PTR(err); 3411 return ERR_PTR(err);
3402} 3412}
3403 3413
3404static void perf_event_free_filter(struct perf_event *event); 3414static void perf_event_free_filter(struct perf_event *event);
3415static void perf_event_free_bpf_prog(struct perf_event *event);
3405 3416
3406static void free_event_rcu(struct rcu_head *head) 3417static void free_event_rcu(struct rcu_head *head)
3407{ 3418{
@@ -3411,10 +3422,10 @@ static void free_event_rcu(struct rcu_head *head)
3411 if (event->ns) 3422 if (event->ns)
3412 put_pid_ns(event->ns); 3423 put_pid_ns(event->ns);
3413 perf_event_free_filter(event); 3424 perf_event_free_filter(event);
3425 perf_event_free_bpf_prog(event);
3414 kfree(event); 3426 kfree(event);
3415} 3427}
3416 3428
3417static void ring_buffer_put(struct ring_buffer *rb);
3418static void ring_buffer_attach(struct perf_event *event, 3429static void ring_buffer_attach(struct perf_event *event,
3419 struct ring_buffer *rb); 3430 struct ring_buffer *rb);
3420 3431
@@ -3423,10 +3434,6 @@ static void unaccount_event_cpu(struct perf_event *event, int cpu)
3423 if (event->parent) 3434 if (event->parent)
3424 return; 3435 return;
3425 3436
3426 if (has_branch_stack(event)) {
3427 if (!(event->attach_state & PERF_ATTACH_TASK))
3428 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3429 }
3430 if (is_cgroup_event(event)) 3437 if (is_cgroup_event(event))
3431 atomic_dec(&per_cpu(perf_cgroup_events, cpu)); 3438 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3432} 3439}
@@ -3454,6 +3461,91 @@ static void unaccount_event(struct perf_event *event)
3454 unaccount_event_cpu(event, event->cpu); 3461 unaccount_event_cpu(event, event->cpu);
3455} 3462}
3456 3463
3464/*
3465 * The following implement mutual exclusion of events on "exclusive" pmus
3466 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3467 * at a time, so we disallow creating events that might conflict, namely:
3468 *
3469 * 1) cpu-wide events in the presence of per-task events,
3470 * 2) per-task events in the presence of cpu-wide events,
3471 * 3) two matching events on the same context.
3472 *
3473 * The former two cases are handled in the allocation path (perf_event_alloc(),
3474 * __free_event()), the latter -- before the first perf_install_in_context().
3475 */
3476static int exclusive_event_init(struct perf_event *event)
3477{
3478 struct pmu *pmu = event->pmu;
3479
3480 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3481 return 0;
3482
3483 /*
3484 * Prevent co-existence of per-task and cpu-wide events on the
3485 * same exclusive pmu.
3486 *
3487 * Negative pmu::exclusive_cnt means there are cpu-wide
3488 * events on this "exclusive" pmu, positive means there are
3489 * per-task events.
3490 *
3491 * Since this is called in perf_event_alloc() path, event::ctx
3492 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3493 * to mean "per-task event", because unlike other attach states it
3494 * never gets cleared.
3495 */
3496 if (event->attach_state & PERF_ATTACH_TASK) {
3497 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3498 return -EBUSY;
3499 } else {
3500 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3501 return -EBUSY;
3502 }
3503
3504 return 0;
3505}
3506
3507static void exclusive_event_destroy(struct perf_event *event)
3508{
3509 struct pmu *pmu = event->pmu;
3510
3511 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3512 return;
3513
3514 /* see comment in exclusive_event_init() */
3515 if (event->attach_state & PERF_ATTACH_TASK)
3516 atomic_dec(&pmu->exclusive_cnt);
3517 else
3518 atomic_inc(&pmu->exclusive_cnt);
3519}
3520
3521static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3522{
3523 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3524 (e1->cpu == e2->cpu ||
3525 e1->cpu == -1 ||
3526 e2->cpu == -1))
3527 return true;
3528 return false;
3529}
3530
3531/* Called under the same ctx::mutex as perf_install_in_context() */
3532static bool exclusive_event_installable(struct perf_event *event,
3533 struct perf_event_context *ctx)
3534{
3535 struct perf_event *iter_event;
3536 struct pmu *pmu = event->pmu;
3537
3538 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3539 return true;
3540
3541 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3542 if (exclusive_event_match(iter_event, event))
3543 return false;
3544 }
3545
3546 return true;
3547}
3548
3457static void __free_event(struct perf_event *event) 3549static void __free_event(struct perf_event *event)
3458{ 3550{
3459 if (!event->parent) { 3551 if (!event->parent) {
@@ -3467,8 +3559,10 @@ static void __free_event(struct perf_event *event)
3467 if (event->ctx) 3559 if (event->ctx)
3468 put_ctx(event->ctx); 3560 put_ctx(event->ctx);
3469 3561
3470 if (event->pmu) 3562 if (event->pmu) {
3563 exclusive_event_destroy(event);
3471 module_put(event->pmu->module); 3564 module_put(event->pmu->module);
3565 }
3472 3566
3473 call_rcu(&event->rcu_head, free_event_rcu); 3567 call_rcu(&event->rcu_head, free_event_rcu);
3474} 3568}
@@ -3927,6 +4021,7 @@ static inline int perf_fget_light(int fd, struct fd *p)
3927static int perf_event_set_output(struct perf_event *event, 4021static int perf_event_set_output(struct perf_event *event,
3928 struct perf_event *output_event); 4022 struct perf_event *output_event);
3929static int perf_event_set_filter(struct perf_event *event, void __user *arg); 4023static int perf_event_set_filter(struct perf_event *event, void __user *arg);
4024static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
3930 4025
3931static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) 4026static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
3932{ 4027{
@@ -3980,6 +4075,9 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
3980 case PERF_EVENT_IOC_SET_FILTER: 4075 case PERF_EVENT_IOC_SET_FILTER:
3981 return perf_event_set_filter(event, (void __user *)arg); 4076 return perf_event_set_filter(event, (void __user *)arg);
3982 4077
4078 case PERF_EVENT_IOC_SET_BPF:
4079 return perf_event_set_bpf_prog(event, arg);
4080
3983 default: 4081 default:
3984 return -ENOTTY; 4082 return -ENOTTY;
3985 } 4083 }
@@ -4096,6 +4194,8 @@ static void perf_event_init_userpage(struct perf_event *event)
4096 /* Allow new userspace to detect that bit 0 is deprecated */ 4194 /* Allow new userspace to detect that bit 0 is deprecated */
4097 userpg->cap_bit0_is_deprecated = 1; 4195 userpg->cap_bit0_is_deprecated = 1;
4098 userpg->size = offsetof(struct perf_event_mmap_page, __reserved); 4196 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
4197 userpg->data_offset = PAGE_SIZE;
4198 userpg->data_size = perf_data_size(rb);
4099 4199
4100unlock: 4200unlock:
4101 rcu_read_unlock(); 4201 rcu_read_unlock();
@@ -4263,7 +4363,7 @@ static void rb_free_rcu(struct rcu_head *rcu_head)
4263 rb_free(rb); 4363 rb_free(rb);
4264} 4364}
4265 4365
4266static struct ring_buffer *ring_buffer_get(struct perf_event *event) 4366struct ring_buffer *ring_buffer_get(struct perf_event *event)
4267{ 4367{
4268 struct ring_buffer *rb; 4368 struct ring_buffer *rb;
4269 4369
@@ -4278,7 +4378,7 @@ static struct ring_buffer *ring_buffer_get(struct perf_event *event)
4278 return rb; 4378 return rb;
4279} 4379}
4280 4380
4281static void ring_buffer_put(struct ring_buffer *rb) 4381void ring_buffer_put(struct ring_buffer *rb)
4282{ 4382{
4283 if (!atomic_dec_and_test(&rb->refcount)) 4383 if (!atomic_dec_and_test(&rb->refcount))
4284 return; 4384 return;
@@ -4295,6 +4395,9 @@ static void perf_mmap_open(struct vm_area_struct *vma)
4295 atomic_inc(&event->mmap_count); 4395 atomic_inc(&event->mmap_count);
4296 atomic_inc(&event->rb->mmap_count); 4396 atomic_inc(&event->rb->mmap_count);
4297 4397
4398 if (vma->vm_pgoff)
4399 atomic_inc(&event->rb->aux_mmap_count);
4400
4298 if (event->pmu->event_mapped) 4401 if (event->pmu->event_mapped)
4299 event->pmu->event_mapped(event); 4402 event->pmu->event_mapped(event);
4300} 4403}
@@ -4319,6 +4422,20 @@ static void perf_mmap_close(struct vm_area_struct *vma)
4319 if (event->pmu->event_unmapped) 4422 if (event->pmu->event_unmapped)
4320 event->pmu->event_unmapped(event); 4423 event->pmu->event_unmapped(event);
4321 4424
4425 /*
4426 * rb->aux_mmap_count will always drop before rb->mmap_count and
4427 * event->mmap_count, so it is ok to use event->mmap_mutex to
4428 * serialize with perf_mmap here.
4429 */
4430 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4431 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
4432 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4433 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4434
4435 rb_free_aux(rb);
4436 mutex_unlock(&event->mmap_mutex);
4437 }
4438
4322 atomic_dec(&rb->mmap_count); 4439 atomic_dec(&rb->mmap_count);
4323 4440
4324 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) 4441 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
@@ -4392,7 +4509,7 @@ out_put:
4392 4509
4393static const struct vm_operations_struct perf_mmap_vmops = { 4510static const struct vm_operations_struct perf_mmap_vmops = {
4394 .open = perf_mmap_open, 4511 .open = perf_mmap_open,
4395 .close = perf_mmap_close, 4512 .close = perf_mmap_close, /* non mergable */
4396 .fault = perf_mmap_fault, 4513 .fault = perf_mmap_fault,
4397 .page_mkwrite = perf_mmap_fault, 4514 .page_mkwrite = perf_mmap_fault,
4398}; 4515};
@@ -4403,10 +4520,10 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4403 unsigned long user_locked, user_lock_limit; 4520 unsigned long user_locked, user_lock_limit;
4404 struct user_struct *user = current_user(); 4521 struct user_struct *user = current_user();
4405 unsigned long locked, lock_limit; 4522 unsigned long locked, lock_limit;
4406 struct ring_buffer *rb; 4523 struct ring_buffer *rb = NULL;
4407 unsigned long vma_size; 4524 unsigned long vma_size;
4408 unsigned long nr_pages; 4525 unsigned long nr_pages;
4409 long user_extra, extra; 4526 long user_extra = 0, extra = 0;
4410 int ret = 0, flags = 0; 4527 int ret = 0, flags = 0;
4411 4528
4412 /* 4529 /*
@@ -4421,7 +4538,66 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4421 return -EINVAL; 4538 return -EINVAL;
4422 4539
4423 vma_size = vma->vm_end - vma->vm_start; 4540 vma_size = vma->vm_end - vma->vm_start;
4424 nr_pages = (vma_size / PAGE_SIZE) - 1; 4541
4542 if (vma->vm_pgoff == 0) {
4543 nr_pages = (vma_size / PAGE_SIZE) - 1;
4544 } else {
4545 /*
4546 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4547 * mapped, all subsequent mappings should have the same size
4548 * and offset. Must be above the normal perf buffer.
4549 */
4550 u64 aux_offset, aux_size;
4551
4552 if (!event->rb)
4553 return -EINVAL;
4554
4555 nr_pages = vma_size / PAGE_SIZE;
4556
4557 mutex_lock(&event->mmap_mutex);
4558 ret = -EINVAL;
4559
4560 rb = event->rb;
4561 if (!rb)
4562 goto aux_unlock;
4563
4564 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4565 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4566
4567 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4568 goto aux_unlock;
4569
4570 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4571 goto aux_unlock;
4572
4573 /* already mapped with a different offset */
4574 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4575 goto aux_unlock;
4576
4577 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4578 goto aux_unlock;
4579
4580 /* already mapped with a different size */
4581 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4582 goto aux_unlock;
4583
4584 if (!is_power_of_2(nr_pages))
4585 goto aux_unlock;
4586
4587 if (!atomic_inc_not_zero(&rb->mmap_count))
4588 goto aux_unlock;
4589
4590 if (rb_has_aux(rb)) {
4591 atomic_inc(&rb->aux_mmap_count);
4592 ret = 0;
4593 goto unlock;
4594 }
4595
4596 atomic_set(&rb->aux_mmap_count, 1);
4597 user_extra = nr_pages;
4598
4599 goto accounting;
4600 }
4425 4601
4426 /* 4602 /*
4427 * If we have rb pages ensure they're a power-of-two number, so we 4603 * If we have rb pages ensure they're a power-of-two number, so we
@@ -4433,9 +4609,6 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4433 if (vma_size != PAGE_SIZE * (1 + nr_pages)) 4609 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4434 return -EINVAL; 4610 return -EINVAL;
4435 4611
4436 if (vma->vm_pgoff != 0)
4437 return -EINVAL;
4438
4439 WARN_ON_ONCE(event->ctx->parent_ctx); 4612 WARN_ON_ONCE(event->ctx->parent_ctx);
4440again: 4613again:
4441 mutex_lock(&event->mmap_mutex); 4614 mutex_lock(&event->mmap_mutex);
@@ -4459,6 +4632,8 @@ again:
4459 } 4632 }
4460 4633
4461 user_extra = nr_pages + 1; 4634 user_extra = nr_pages + 1;
4635
4636accounting:
4462 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); 4637 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4463 4638
4464 /* 4639 /*
@@ -4468,7 +4643,6 @@ again:
4468 4643
4469 user_locked = atomic_long_read(&user->locked_vm) + user_extra; 4644 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4470 4645
4471 extra = 0;
4472 if (user_locked > user_lock_limit) 4646 if (user_locked > user_lock_limit)
4473 extra = user_locked - user_lock_limit; 4647 extra = user_locked - user_lock_limit;
4474 4648
@@ -4482,35 +4656,46 @@ again:
4482 goto unlock; 4656 goto unlock;
4483 } 4657 }
4484 4658
4485 WARN_ON(event->rb); 4659 WARN_ON(!rb && event->rb);
4486 4660
4487 if (vma->vm_flags & VM_WRITE) 4661 if (vma->vm_flags & VM_WRITE)
4488 flags |= RING_BUFFER_WRITABLE; 4662 flags |= RING_BUFFER_WRITABLE;
4489 4663
4490 rb = rb_alloc(nr_pages,
4491 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4492 event->cpu, flags);
4493
4494 if (!rb) { 4664 if (!rb) {
4495 ret = -ENOMEM; 4665 rb = rb_alloc(nr_pages,
4496 goto unlock; 4666 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4497 } 4667 event->cpu, flags);
4498 4668
4499 atomic_set(&rb->mmap_count, 1); 4669 if (!rb) {
4500 rb->mmap_locked = extra; 4670 ret = -ENOMEM;
4501 rb->mmap_user = get_current_user(); 4671 goto unlock;
4672 }
4502 4673
4503 atomic_long_add(user_extra, &user->locked_vm); 4674 atomic_set(&rb->mmap_count, 1);
4504 vma->vm_mm->pinned_vm += extra; 4675 rb->mmap_user = get_current_user();
4676 rb->mmap_locked = extra;
4505 4677
4506 ring_buffer_attach(event, rb); 4678 ring_buffer_attach(event, rb);
4507 4679
4508 perf_event_init_userpage(event); 4680 perf_event_init_userpage(event);
4509 perf_event_update_userpage(event); 4681 perf_event_update_userpage(event);
4682 } else {
4683 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
4684 event->attr.aux_watermark, flags);
4685 if (!ret)
4686 rb->aux_mmap_locked = extra;
4687 }
4510 4688
4511unlock: 4689unlock:
4512 if (!ret) 4690 if (!ret) {
4691 atomic_long_add(user_extra, &user->locked_vm);
4692 vma->vm_mm->pinned_vm += extra;
4693
4513 atomic_inc(&event->mmap_count); 4694 atomic_inc(&event->mmap_count);
4695 } else if (rb) {
4696 atomic_dec(&rb->mmap_count);
4697 }
4698aux_unlock:
4514 mutex_unlock(&event->mmap_mutex); 4699 mutex_unlock(&event->mmap_mutex);
4515 4700
4516 /* 4701 /*
@@ -4766,7 +4951,7 @@ static void __perf_event_header__init_id(struct perf_event_header *header,
4766 } 4951 }
4767 4952
4768 if (sample_type & PERF_SAMPLE_TIME) 4953 if (sample_type & PERF_SAMPLE_TIME)
4769 data->time = perf_clock(); 4954 data->time = perf_event_clock(event);
4770 4955
4771 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) 4956 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
4772 data->id = primary_event_id(event); 4957 data->id = primary_event_id(event);
@@ -5344,6 +5529,8 @@ static void perf_event_task_output(struct perf_event *event,
5344 task_event->event_id.tid = perf_event_tid(event, task); 5529 task_event->event_id.tid = perf_event_tid(event, task);
5345 task_event->event_id.ptid = perf_event_tid(event, current); 5530 task_event->event_id.ptid = perf_event_tid(event, current);
5346 5531
5532 task_event->event_id.time = perf_event_clock(event);
5533
5347 perf_output_put(&handle, task_event->event_id); 5534 perf_output_put(&handle, task_event->event_id);
5348 5535
5349 perf_event__output_id_sample(event, &handle, &sample); 5536 perf_event__output_id_sample(event, &handle, &sample);
@@ -5377,7 +5564,7 @@ static void perf_event_task(struct task_struct *task,
5377 /* .ppid */ 5564 /* .ppid */
5378 /* .tid */ 5565 /* .tid */
5379 /* .ptid */ 5566 /* .ptid */
5380 .time = perf_clock(), 5567 /* .time */
5381 }, 5568 },
5382 }; 5569 };
5383 5570
@@ -5732,6 +5919,40 @@ void perf_event_mmap(struct vm_area_struct *vma)
5732 perf_event_mmap_event(&mmap_event); 5919 perf_event_mmap_event(&mmap_event);
5733} 5920}
5734 5921
5922void perf_event_aux_event(struct perf_event *event, unsigned long head,
5923 unsigned long size, u64 flags)
5924{
5925 struct perf_output_handle handle;
5926 struct perf_sample_data sample;
5927 struct perf_aux_event {
5928 struct perf_event_header header;
5929 u64 offset;
5930 u64 size;
5931 u64 flags;
5932 } rec = {
5933 .header = {
5934 .type = PERF_RECORD_AUX,
5935 .misc = 0,
5936 .size = sizeof(rec),
5937 },
5938 .offset = head,
5939 .size = size,
5940 .flags = flags,
5941 };
5942 int ret;
5943
5944 perf_event_header__init_id(&rec.header, &sample, event);
5945 ret = perf_output_begin(&handle, event, rec.header.size);
5946
5947 if (ret)
5948 return;
5949
5950 perf_output_put(&handle, rec);
5951 perf_event__output_id_sample(event, &handle, &sample);
5952
5953 perf_output_end(&handle);
5954}
5955
5735/* 5956/*
5736 * IRQ throttle logging 5957 * IRQ throttle logging
5737 */ 5958 */
@@ -5753,7 +5974,7 @@ static void perf_log_throttle(struct perf_event *event, int enable)
5753 .misc = 0, 5974 .misc = 0,
5754 .size = sizeof(throttle_event), 5975 .size = sizeof(throttle_event),
5755 }, 5976 },
5756 .time = perf_clock(), 5977 .time = perf_event_clock(event),
5757 .id = primary_event_id(event), 5978 .id = primary_event_id(event),
5758 .stream_id = event->id, 5979 .stream_id = event->id,
5759 }; 5980 };
@@ -5773,6 +5994,44 @@ static void perf_log_throttle(struct perf_event *event, int enable)
5773 perf_output_end(&handle); 5994 perf_output_end(&handle);
5774} 5995}
5775 5996
5997static void perf_log_itrace_start(struct perf_event *event)
5998{
5999 struct perf_output_handle handle;
6000 struct perf_sample_data sample;
6001 struct perf_aux_event {
6002 struct perf_event_header header;
6003 u32 pid;
6004 u32 tid;
6005 } rec;
6006 int ret;
6007
6008 if (event->parent)
6009 event = event->parent;
6010
6011 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6012 event->hw.itrace_started)
6013 return;
6014
6015 event->hw.itrace_started = 1;
6016
6017 rec.header.type = PERF_RECORD_ITRACE_START;
6018 rec.header.misc = 0;
6019 rec.header.size = sizeof(rec);
6020 rec.pid = perf_event_pid(event, current);
6021 rec.tid = perf_event_tid(event, current);
6022
6023 perf_event_header__init_id(&rec.header, &sample, event);
6024 ret = perf_output_begin(&handle, event, rec.header.size);
6025
6026 if (ret)
6027 return;
6028
6029 perf_output_put(&handle, rec);
6030 perf_event__output_id_sample(event, &handle, &sample);
6031
6032 perf_output_end(&handle);
6033}
6034
5776/* 6035/*
5777 * Generic event overflow handling, sampling. 6036 * Generic event overflow handling, sampling.
5778 */ 6037 */
@@ -6133,6 +6392,7 @@ static int perf_swevent_add(struct perf_event *event, int flags)
6133 } 6392 }
6134 6393
6135 hlist_add_head_rcu(&event->hlist_entry, head); 6394 hlist_add_head_rcu(&event->hlist_entry, head);
6395 perf_event_update_userpage(event);
6136 6396
6137 return 0; 6397 return 0;
6138} 6398}
@@ -6296,6 +6556,8 @@ static int perf_swevent_init(struct perf_event *event)
6296static struct pmu perf_swevent = { 6556static struct pmu perf_swevent = {
6297 .task_ctx_nr = perf_sw_context, 6557 .task_ctx_nr = perf_sw_context,
6298 6558
6559 .capabilities = PERF_PMU_CAP_NO_NMI,
6560
6299 .event_init = perf_swevent_init, 6561 .event_init = perf_swevent_init,
6300 .add = perf_swevent_add, 6562 .add = perf_swevent_add,
6301 .del = perf_swevent_del, 6563 .del = perf_swevent_del,
@@ -6449,6 +6711,49 @@ static void perf_event_free_filter(struct perf_event *event)
6449 ftrace_profile_free_filter(event); 6711 ftrace_profile_free_filter(event);
6450} 6712}
6451 6713
6714static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
6715{
6716 struct bpf_prog *prog;
6717
6718 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6719 return -EINVAL;
6720
6721 if (event->tp_event->prog)
6722 return -EEXIST;
6723
6724 if (!(event->tp_event->flags & TRACE_EVENT_FL_KPROBE))
6725 /* bpf programs can only be attached to kprobes */
6726 return -EINVAL;
6727
6728 prog = bpf_prog_get(prog_fd);
6729 if (IS_ERR(prog))
6730 return PTR_ERR(prog);
6731
6732 if (prog->aux->prog_type != BPF_PROG_TYPE_KPROBE) {
6733 /* valid fd, but invalid bpf program type */
6734 bpf_prog_put(prog);
6735 return -EINVAL;
6736 }
6737
6738 event->tp_event->prog = prog;
6739
6740 return 0;
6741}
6742
6743static void perf_event_free_bpf_prog(struct perf_event *event)
6744{
6745 struct bpf_prog *prog;
6746
6747 if (!event->tp_event)
6748 return;
6749
6750 prog = event->tp_event->prog;
6751 if (prog) {
6752 event->tp_event->prog = NULL;
6753 bpf_prog_put(prog);
6754 }
6755}
6756
6452#else 6757#else
6453 6758
6454static inline void perf_tp_register(void) 6759static inline void perf_tp_register(void)
@@ -6464,6 +6769,14 @@ static void perf_event_free_filter(struct perf_event *event)
6464{ 6769{
6465} 6770}
6466 6771
6772static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
6773{
6774 return -ENOENT;
6775}
6776
6777static void perf_event_free_bpf_prog(struct perf_event *event)
6778{
6779}
6467#endif /* CONFIG_EVENT_TRACING */ 6780#endif /* CONFIG_EVENT_TRACING */
6468 6781
6469#ifdef CONFIG_HAVE_HW_BREAKPOINT 6782#ifdef CONFIG_HAVE_HW_BREAKPOINT
@@ -6602,6 +6915,7 @@ static int cpu_clock_event_add(struct perf_event *event, int flags)
6602{ 6915{
6603 if (flags & PERF_EF_START) 6916 if (flags & PERF_EF_START)
6604 cpu_clock_event_start(event, flags); 6917 cpu_clock_event_start(event, flags);
6918 perf_event_update_userpage(event);
6605 6919
6606 return 0; 6920 return 0;
6607} 6921}
@@ -6638,6 +6952,8 @@ static int cpu_clock_event_init(struct perf_event *event)
6638static struct pmu perf_cpu_clock = { 6952static struct pmu perf_cpu_clock = {
6639 .task_ctx_nr = perf_sw_context, 6953 .task_ctx_nr = perf_sw_context,
6640 6954
6955 .capabilities = PERF_PMU_CAP_NO_NMI,
6956
6641 .event_init = cpu_clock_event_init, 6957 .event_init = cpu_clock_event_init,
6642 .add = cpu_clock_event_add, 6958 .add = cpu_clock_event_add,
6643 .del = cpu_clock_event_del, 6959 .del = cpu_clock_event_del,
@@ -6676,6 +6992,7 @@ static int task_clock_event_add(struct perf_event *event, int flags)
6676{ 6992{
6677 if (flags & PERF_EF_START) 6993 if (flags & PERF_EF_START)
6678 task_clock_event_start(event, flags); 6994 task_clock_event_start(event, flags);
6995 perf_event_update_userpage(event);
6679 6996
6680 return 0; 6997 return 0;
6681} 6998}
@@ -6716,6 +7033,8 @@ static int task_clock_event_init(struct perf_event *event)
6716static struct pmu perf_task_clock = { 7033static struct pmu perf_task_clock = {
6717 .task_ctx_nr = perf_sw_context, 7034 .task_ctx_nr = perf_sw_context,
6718 7035
7036 .capabilities = PERF_PMU_CAP_NO_NMI,
7037
6719 .event_init = task_clock_event_init, 7038 .event_init = task_clock_event_init,
6720 .add = task_clock_event_add, 7039 .add = task_clock_event_add,
6721 .del = task_clock_event_del, 7040 .del = task_clock_event_del,
@@ -6993,6 +7312,7 @@ got_cpu_context:
6993 pmu->event_idx = perf_event_idx_default; 7312 pmu->event_idx = perf_event_idx_default;
6994 7313
6995 list_add_rcu(&pmu->entry, &pmus); 7314 list_add_rcu(&pmu->entry, &pmus);
7315 atomic_set(&pmu->exclusive_cnt, 0);
6996 ret = 0; 7316 ret = 0;
6997unlock: 7317unlock:
6998 mutex_unlock(&pmus_lock); 7318 mutex_unlock(&pmus_lock);
@@ -7037,12 +7357,23 @@ EXPORT_SYMBOL_GPL(perf_pmu_unregister);
7037 7357
7038static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) 7358static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7039{ 7359{
7360 struct perf_event_context *ctx = NULL;
7040 int ret; 7361 int ret;
7041 7362
7042 if (!try_module_get(pmu->module)) 7363 if (!try_module_get(pmu->module))
7043 return -ENODEV; 7364 return -ENODEV;
7365
7366 if (event->group_leader != event) {
7367 ctx = perf_event_ctx_lock(event->group_leader);
7368 BUG_ON(!ctx);
7369 }
7370
7044 event->pmu = pmu; 7371 event->pmu = pmu;
7045 ret = pmu->event_init(event); 7372 ret = pmu->event_init(event);
7373
7374 if (ctx)
7375 perf_event_ctx_unlock(event->group_leader, ctx);
7376
7046 if (ret) 7377 if (ret)
7047 module_put(pmu->module); 7378 module_put(pmu->module);
7048 7379
@@ -7089,10 +7420,6 @@ static void account_event_cpu(struct perf_event *event, int cpu)
7089 if (event->parent) 7420 if (event->parent)
7090 return; 7421 return;
7091 7422
7092 if (has_branch_stack(event)) {
7093 if (!(event->attach_state & PERF_ATTACH_TASK))
7094 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
7095 }
7096 if (is_cgroup_event(event)) 7423 if (is_cgroup_event(event))
7097 atomic_inc(&per_cpu(perf_cgroup_events, cpu)); 7424 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7098} 7425}
@@ -7131,7 +7458,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
7131 struct perf_event *group_leader, 7458 struct perf_event *group_leader,
7132 struct perf_event *parent_event, 7459 struct perf_event *parent_event,
7133 perf_overflow_handler_t overflow_handler, 7460 perf_overflow_handler_t overflow_handler,
7134 void *context) 7461 void *context, int cgroup_fd)
7135{ 7462{
7136 struct pmu *pmu; 7463 struct pmu *pmu;
7137 struct perf_event *event; 7464 struct perf_event *event;
@@ -7186,18 +7513,18 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
7186 7513
7187 if (task) { 7514 if (task) {
7188 event->attach_state = PERF_ATTACH_TASK; 7515 event->attach_state = PERF_ATTACH_TASK;
7189
7190 if (attr->type == PERF_TYPE_TRACEPOINT)
7191 event->hw.tp_target = task;
7192#ifdef CONFIG_HAVE_HW_BREAKPOINT
7193 /* 7516 /*
7194 * hw_breakpoint is a bit difficult here.. 7517 * XXX pmu::event_init needs to know what task to account to
7518 * and we cannot use the ctx information because we need the
7519 * pmu before we get a ctx.
7195 */ 7520 */
7196 else if (attr->type == PERF_TYPE_BREAKPOINT) 7521 event->hw.target = task;
7197 event->hw.bp_target = task;
7198#endif
7199 } 7522 }
7200 7523
7524 event->clock = &local_clock;
7525 if (parent_event)
7526 event->clock = parent_event->clock;
7527
7201 if (!overflow_handler && parent_event) { 7528 if (!overflow_handler && parent_event) {
7202 overflow_handler = parent_event->overflow_handler; 7529 overflow_handler = parent_event->overflow_handler;
7203 context = parent_event->overflow_handler_context; 7530 context = parent_event->overflow_handler_context;
@@ -7224,6 +7551,15 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
7224 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) 7551 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
7225 goto err_ns; 7552 goto err_ns;
7226 7553
7554 if (!has_branch_stack(event))
7555 event->attr.branch_sample_type = 0;
7556
7557 if (cgroup_fd != -1) {
7558 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
7559 if (err)
7560 goto err_ns;
7561 }
7562
7227 pmu = perf_init_event(event); 7563 pmu = perf_init_event(event);
7228 if (!pmu) 7564 if (!pmu)
7229 goto err_ns; 7565 goto err_ns;
@@ -7232,21 +7568,30 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
7232 goto err_ns; 7568 goto err_ns;
7233 } 7569 }
7234 7570
7571 err = exclusive_event_init(event);
7572 if (err)
7573 goto err_pmu;
7574
7235 if (!event->parent) { 7575 if (!event->parent) {
7236 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { 7576 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7237 err = get_callchain_buffers(); 7577 err = get_callchain_buffers();
7238 if (err) 7578 if (err)
7239 goto err_pmu; 7579 goto err_per_task;
7240 } 7580 }
7241 } 7581 }
7242 7582
7243 return event; 7583 return event;
7244 7584
7585err_per_task:
7586 exclusive_event_destroy(event);
7587
7245err_pmu: 7588err_pmu:
7246 if (event->destroy) 7589 if (event->destroy)
7247 event->destroy(event); 7590 event->destroy(event);
7248 module_put(pmu->module); 7591 module_put(pmu->module);
7249err_ns: 7592err_ns:
7593 if (is_cgroup_event(event))
7594 perf_detach_cgroup(event);
7250 if (event->ns) 7595 if (event->ns)
7251 put_pid_ns(event->ns); 7596 put_pid_ns(event->ns);
7252 kfree(event); 7597 kfree(event);
@@ -7409,6 +7754,19 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
7409 if (output_event->cpu == -1 && output_event->ctx != event->ctx) 7754 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7410 goto out; 7755 goto out;
7411 7756
7757 /*
7758 * Mixing clocks in the same buffer is trouble you don't need.
7759 */
7760 if (output_event->clock != event->clock)
7761 goto out;
7762
7763 /*
7764 * If both events generate aux data, they must be on the same PMU
7765 */
7766 if (has_aux(event) && has_aux(output_event) &&
7767 event->pmu != output_event->pmu)
7768 goto out;
7769
7412set: 7770set:
7413 mutex_lock(&event->mmap_mutex); 7771 mutex_lock(&event->mmap_mutex);
7414 /* Can't redirect output if we've got an active mmap() */ 7772 /* Can't redirect output if we've got an active mmap() */
@@ -7441,6 +7799,43 @@ static void mutex_lock_double(struct mutex *a, struct mutex *b)
7441 mutex_lock_nested(b, SINGLE_DEPTH_NESTING); 7799 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
7442} 7800}
7443 7801
7802static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
7803{
7804 bool nmi_safe = false;
7805
7806 switch (clk_id) {
7807 case CLOCK_MONOTONIC:
7808 event->clock = &ktime_get_mono_fast_ns;
7809 nmi_safe = true;
7810 break;
7811
7812 case CLOCK_MONOTONIC_RAW:
7813 event->clock = &ktime_get_raw_fast_ns;
7814 nmi_safe = true;
7815 break;
7816
7817 case CLOCK_REALTIME:
7818 event->clock = &ktime_get_real_ns;
7819 break;
7820
7821 case CLOCK_BOOTTIME:
7822 event->clock = &ktime_get_boot_ns;
7823 break;
7824
7825 case CLOCK_TAI:
7826 event->clock = &ktime_get_tai_ns;
7827 break;
7828
7829 default:
7830 return -EINVAL;
7831 }
7832
7833 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
7834 return -EINVAL;
7835
7836 return 0;
7837}
7838
7444/** 7839/**
7445 * sys_perf_event_open - open a performance event, associate it to a task/cpu 7840 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7446 * 7841 *
@@ -7465,6 +7860,7 @@ SYSCALL_DEFINE5(perf_event_open,
7465 int move_group = 0; 7860 int move_group = 0;
7466 int err; 7861 int err;
7467 int f_flags = O_RDWR; 7862 int f_flags = O_RDWR;
7863 int cgroup_fd = -1;
7468 7864
7469 /* for future expandability... */ 7865 /* for future expandability... */
7470 if (flags & ~PERF_FLAG_ALL) 7866 if (flags & ~PERF_FLAG_ALL)
@@ -7530,21 +7926,16 @@ SYSCALL_DEFINE5(perf_event_open,
7530 7926
7531 get_online_cpus(); 7927 get_online_cpus();
7532 7928
7929 if (flags & PERF_FLAG_PID_CGROUP)
7930 cgroup_fd = pid;
7931
7533 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, 7932 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7534 NULL, NULL); 7933 NULL, NULL, cgroup_fd);
7535 if (IS_ERR(event)) { 7934 if (IS_ERR(event)) {
7536 err = PTR_ERR(event); 7935 err = PTR_ERR(event);
7537 goto err_cpus; 7936 goto err_cpus;
7538 } 7937 }
7539 7938
7540 if (flags & PERF_FLAG_PID_CGROUP) {
7541 err = perf_cgroup_connect(pid, event, &attr, group_leader);
7542 if (err) {
7543 __free_event(event);
7544 goto err_cpus;
7545 }
7546 }
7547
7548 if (is_sampling_event(event)) { 7939 if (is_sampling_event(event)) {
7549 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { 7940 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7550 err = -ENOTSUPP; 7941 err = -ENOTSUPP;
@@ -7560,6 +7951,12 @@ SYSCALL_DEFINE5(perf_event_open,
7560 */ 7951 */
7561 pmu = event->pmu; 7952 pmu = event->pmu;
7562 7953
7954 if (attr.use_clockid) {
7955 err = perf_event_set_clock(event, attr.clockid);
7956 if (err)
7957 goto err_alloc;
7958 }
7959
7563 if (group_leader && 7960 if (group_leader &&
7564 (is_software_event(event) != is_software_event(group_leader))) { 7961 (is_software_event(event) != is_software_event(group_leader))) {
7565 if (is_software_event(event)) { 7962 if (is_software_event(event)) {
@@ -7586,12 +7983,17 @@ SYSCALL_DEFINE5(perf_event_open,
7586 /* 7983 /*
7587 * Get the target context (task or percpu): 7984 * Get the target context (task or percpu):
7588 */ 7985 */
7589 ctx = find_get_context(pmu, task, event->cpu); 7986 ctx = find_get_context(pmu, task, event);
7590 if (IS_ERR(ctx)) { 7987 if (IS_ERR(ctx)) {
7591 err = PTR_ERR(ctx); 7988 err = PTR_ERR(ctx);
7592 goto err_alloc; 7989 goto err_alloc;
7593 } 7990 }
7594 7991
7992 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
7993 err = -EBUSY;
7994 goto err_context;
7995 }
7996
7595 if (task) { 7997 if (task) {
7596 put_task_struct(task); 7998 put_task_struct(task);
7597 task = NULL; 7999 task = NULL;
@@ -7609,6 +8011,11 @@ SYSCALL_DEFINE5(perf_event_open,
7609 */ 8011 */
7610 if (group_leader->group_leader != group_leader) 8012 if (group_leader->group_leader != group_leader)
7611 goto err_context; 8013 goto err_context;
8014
8015 /* All events in a group should have the same clock */
8016 if (group_leader->clock != event->clock)
8017 goto err_context;
8018
7612 /* 8019 /*
7613 * Do not allow to attach to a group in a different 8020 * Do not allow to attach to a group in a different
7614 * task or CPU context: 8021 * task or CPU context:
@@ -7709,6 +8116,13 @@ SYSCALL_DEFINE5(perf_event_open,
7709 get_ctx(ctx); 8116 get_ctx(ctx);
7710 } 8117 }
7711 8118
8119 if (!exclusive_event_installable(event, ctx)) {
8120 err = -EBUSY;
8121 mutex_unlock(&ctx->mutex);
8122 fput(event_file);
8123 goto err_context;
8124 }
8125
7712 perf_install_in_context(ctx, event, event->cpu); 8126 perf_install_in_context(ctx, event, event->cpu);
7713 perf_unpin_context(ctx); 8127 perf_unpin_context(ctx);
7714 8128
@@ -7781,7 +8195,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7781 */ 8195 */
7782 8196
7783 event = perf_event_alloc(attr, cpu, task, NULL, NULL, 8197 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7784 overflow_handler, context); 8198 overflow_handler, context, -1);
7785 if (IS_ERR(event)) { 8199 if (IS_ERR(event)) {
7786 err = PTR_ERR(event); 8200 err = PTR_ERR(event);
7787 goto err; 8201 goto err;
@@ -7792,7 +8206,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7792 8206
7793 account_event(event); 8207 account_event(event);
7794 8208
7795 ctx = find_get_context(event->pmu, task, cpu); 8209 ctx = find_get_context(event->pmu, task, event);
7796 if (IS_ERR(ctx)) { 8210 if (IS_ERR(ctx)) {
7797 err = PTR_ERR(ctx); 8211 err = PTR_ERR(ctx);
7798 goto err_free; 8212 goto err_free;
@@ -7800,6 +8214,14 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7800 8214
7801 WARN_ON_ONCE(ctx->parent_ctx); 8215 WARN_ON_ONCE(ctx->parent_ctx);
7802 mutex_lock(&ctx->mutex); 8216 mutex_lock(&ctx->mutex);
8217 if (!exclusive_event_installable(event, ctx)) {
8218 mutex_unlock(&ctx->mutex);
8219 perf_unpin_context(ctx);
8220 put_ctx(ctx);
8221 err = -EBUSY;
8222 goto err_free;
8223 }
8224
7803 perf_install_in_context(ctx, event, cpu); 8225 perf_install_in_context(ctx, event, cpu);
7804 perf_unpin_context(ctx); 8226 perf_unpin_context(ctx);
7805 mutex_unlock(&ctx->mutex); 8227 mutex_unlock(&ctx->mutex);
@@ -8142,7 +8564,7 @@ inherit_event(struct perf_event *parent_event,
8142 parent_event->cpu, 8564 parent_event->cpu,
8143 child, 8565 child,
8144 group_leader, parent_event, 8566 group_leader, parent_event,
8145 NULL, NULL); 8567 NULL, NULL, -1);
8146 if (IS_ERR(child_event)) 8568 if (IS_ERR(child_event))
8147 return child_event; 8569 return child_event;
8148 8570
diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c
index 9803a6600d49..92ce5f4ccc26 100644
--- a/kernel/events/hw_breakpoint.c
+++ b/kernel/events/hw_breakpoint.c
@@ -116,12 +116,12 @@ static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type)
116 */ 116 */
117static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type) 117static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type)
118{ 118{
119 struct task_struct *tsk = bp->hw.bp_target; 119 struct task_struct *tsk = bp->hw.target;
120 struct perf_event *iter; 120 struct perf_event *iter;
121 int count = 0; 121 int count = 0;
122 122
123 list_for_each_entry(iter, &bp_task_head, hw.bp_list) { 123 list_for_each_entry(iter, &bp_task_head, hw.bp_list) {
124 if (iter->hw.bp_target == tsk && 124 if (iter->hw.target == tsk &&
125 find_slot_idx(iter) == type && 125 find_slot_idx(iter) == type &&
126 (iter->cpu < 0 || cpu == iter->cpu)) 126 (iter->cpu < 0 || cpu == iter->cpu))
127 count += hw_breakpoint_weight(iter); 127 count += hw_breakpoint_weight(iter);
@@ -153,7 +153,7 @@ fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp,
153 int nr; 153 int nr;
154 154
155 nr = info->cpu_pinned; 155 nr = info->cpu_pinned;
156 if (!bp->hw.bp_target) 156 if (!bp->hw.target)
157 nr += max_task_bp_pinned(cpu, type); 157 nr += max_task_bp_pinned(cpu, type);
158 else 158 else
159 nr += task_bp_pinned(cpu, bp, type); 159 nr += task_bp_pinned(cpu, bp, type);
@@ -210,7 +210,7 @@ toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type,
210 weight = -weight; 210 weight = -weight;
211 211
212 /* Pinned counter cpu profiling */ 212 /* Pinned counter cpu profiling */
213 if (!bp->hw.bp_target) { 213 if (!bp->hw.target) {
214 get_bp_info(bp->cpu, type)->cpu_pinned += weight; 214 get_bp_info(bp->cpu, type)->cpu_pinned += weight;
215 return; 215 return;
216 } 216 }
diff --git a/kernel/events/internal.h b/kernel/events/internal.h
index 569b218782ad..9f6ce9ba4a04 100644
--- a/kernel/events/internal.h
+++ b/kernel/events/internal.h
@@ -27,6 +27,7 @@ struct ring_buffer {
27 local_t lost; /* nr records lost */ 27 local_t lost; /* nr records lost */
28 28
29 long watermark; /* wakeup watermark */ 29 long watermark; /* wakeup watermark */
30 long aux_watermark;
30 /* poll crap */ 31 /* poll crap */
31 spinlock_t event_lock; 32 spinlock_t event_lock;
32 struct list_head event_list; 33 struct list_head event_list;
@@ -35,6 +36,20 @@ struct ring_buffer {
35 unsigned long mmap_locked; 36 unsigned long mmap_locked;
36 struct user_struct *mmap_user; 37 struct user_struct *mmap_user;
37 38
39 /* AUX area */
40 local_t aux_head;
41 local_t aux_nest;
42 local_t aux_wakeup;
43 unsigned long aux_pgoff;
44 int aux_nr_pages;
45 int aux_overwrite;
46 atomic_t aux_mmap_count;
47 unsigned long aux_mmap_locked;
48 void (*free_aux)(void *);
49 atomic_t aux_refcount;
50 void **aux_pages;
51 void *aux_priv;
52
38 struct perf_event_mmap_page *user_page; 53 struct perf_event_mmap_page *user_page;
39 void *data_pages[0]; 54 void *data_pages[0];
40}; 55};
@@ -43,6 +58,19 @@ extern void rb_free(struct ring_buffer *rb);
43extern struct ring_buffer * 58extern struct ring_buffer *
44rb_alloc(int nr_pages, long watermark, int cpu, int flags); 59rb_alloc(int nr_pages, long watermark, int cpu, int flags);
45extern void perf_event_wakeup(struct perf_event *event); 60extern void perf_event_wakeup(struct perf_event *event);
61extern int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
62 pgoff_t pgoff, int nr_pages, long watermark, int flags);
63extern void rb_free_aux(struct ring_buffer *rb);
64extern struct ring_buffer *ring_buffer_get(struct perf_event *event);
65extern void ring_buffer_put(struct ring_buffer *rb);
66
67static inline bool rb_has_aux(struct ring_buffer *rb)
68{
69 return !!rb->aux_nr_pages;
70}
71
72void perf_event_aux_event(struct perf_event *event, unsigned long head,
73 unsigned long size, u64 flags);
46 74
47extern void 75extern void
48perf_event_header__init_id(struct perf_event_header *header, 76perf_event_header__init_id(struct perf_event_header *header,
@@ -81,6 +109,11 @@ static inline unsigned long perf_data_size(struct ring_buffer *rb)
81 return rb->nr_pages << (PAGE_SHIFT + page_order(rb)); 109 return rb->nr_pages << (PAGE_SHIFT + page_order(rb));
82} 110}
83 111
112static inline unsigned long perf_aux_size(struct ring_buffer *rb)
113{
114 return rb->aux_nr_pages << PAGE_SHIFT;
115}
116
84#define DEFINE_OUTPUT_COPY(func_name, memcpy_func) \ 117#define DEFINE_OUTPUT_COPY(func_name, memcpy_func) \
85static inline unsigned long \ 118static inline unsigned long \
86func_name(struct perf_output_handle *handle, \ 119func_name(struct perf_output_handle *handle, \
diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c
index eadb95ce7aac..232f00f273cb 100644
--- a/kernel/events/ring_buffer.c
+++ b/kernel/events/ring_buffer.c
@@ -243,14 +243,317 @@ ring_buffer_init(struct ring_buffer *rb, long watermark, int flags)
243 spin_lock_init(&rb->event_lock); 243 spin_lock_init(&rb->event_lock);
244} 244}
245 245
246/*
247 * This is called before hardware starts writing to the AUX area to
248 * obtain an output handle and make sure there's room in the buffer.
249 * When the capture completes, call perf_aux_output_end() to commit
250 * the recorded data to the buffer.
251 *
252 * The ordering is similar to that of perf_output_{begin,end}, with
253 * the exception of (B), which should be taken care of by the pmu
254 * driver, since ordering rules will differ depending on hardware.
255 */
256void *perf_aux_output_begin(struct perf_output_handle *handle,
257 struct perf_event *event)
258{
259 struct perf_event *output_event = event;
260 unsigned long aux_head, aux_tail;
261 struct ring_buffer *rb;
262
263 if (output_event->parent)
264 output_event = output_event->parent;
265
266 /*
267 * Since this will typically be open across pmu::add/pmu::del, we
268 * grab ring_buffer's refcount instead of holding rcu read lock
269 * to make sure it doesn't disappear under us.
270 */
271 rb = ring_buffer_get(output_event);
272 if (!rb)
273 return NULL;
274
275 if (!rb_has_aux(rb) || !atomic_inc_not_zero(&rb->aux_refcount))
276 goto err;
277
278 /*
279 * Nesting is not supported for AUX area, make sure nested
280 * writers are caught early
281 */
282 if (WARN_ON_ONCE(local_xchg(&rb->aux_nest, 1)))
283 goto err_put;
284
285 aux_head = local_read(&rb->aux_head);
286
287 handle->rb = rb;
288 handle->event = event;
289 handle->head = aux_head;
290 handle->size = 0;
291
292 /*
293 * In overwrite mode, AUX data stores do not depend on aux_tail,
294 * therefore (A) control dependency barrier does not exist. The
295 * (B) <-> (C) ordering is still observed by the pmu driver.
296 */
297 if (!rb->aux_overwrite) {
298 aux_tail = ACCESS_ONCE(rb->user_page->aux_tail);
299 handle->wakeup = local_read(&rb->aux_wakeup) + rb->aux_watermark;
300 if (aux_head - aux_tail < perf_aux_size(rb))
301 handle->size = CIRC_SPACE(aux_head, aux_tail, perf_aux_size(rb));
302
303 /*
304 * handle->size computation depends on aux_tail load; this forms a
305 * control dependency barrier separating aux_tail load from aux data
306 * store that will be enabled on successful return
307 */
308 if (!handle->size) { /* A, matches D */
309 event->pending_disable = 1;
310 perf_output_wakeup(handle);
311 local_set(&rb->aux_nest, 0);
312 goto err_put;
313 }
314 }
315
316 return handle->rb->aux_priv;
317
318err_put:
319 rb_free_aux(rb);
320
321err:
322 ring_buffer_put(rb);
323 handle->event = NULL;
324
325 return NULL;
326}
327
328/*
329 * Commit the data written by hardware into the ring buffer by adjusting
330 * aux_head and posting a PERF_RECORD_AUX into the perf buffer. It is the
331 * pmu driver's responsibility to observe ordering rules of the hardware,
332 * so that all the data is externally visible before this is called.
333 */
334void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
335 bool truncated)
336{
337 struct ring_buffer *rb = handle->rb;
338 unsigned long aux_head;
339 u64 flags = 0;
340
341 if (truncated)
342 flags |= PERF_AUX_FLAG_TRUNCATED;
343
344 /* in overwrite mode, driver provides aux_head via handle */
345 if (rb->aux_overwrite) {
346 flags |= PERF_AUX_FLAG_OVERWRITE;
347
348 aux_head = handle->head;
349 local_set(&rb->aux_head, aux_head);
350 } else {
351 aux_head = local_read(&rb->aux_head);
352 local_add(size, &rb->aux_head);
353 }
354
355 if (size || flags) {
356 /*
357 * Only send RECORD_AUX if we have something useful to communicate
358 */
359
360 perf_event_aux_event(handle->event, aux_head, size, flags);
361 }
362
363 aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
364
365 if (aux_head - local_read(&rb->aux_wakeup) >= rb->aux_watermark) {
366 perf_output_wakeup(handle);
367 local_add(rb->aux_watermark, &rb->aux_wakeup);
368 }
369 handle->event = NULL;
370
371 local_set(&rb->aux_nest, 0);
372 rb_free_aux(rb);
373 ring_buffer_put(rb);
374}
375
376/*
377 * Skip over a given number of bytes in the AUX buffer, due to, for example,
378 * hardware's alignment constraints.
379 */
380int perf_aux_output_skip(struct perf_output_handle *handle, unsigned long size)
381{
382 struct ring_buffer *rb = handle->rb;
383 unsigned long aux_head;
384
385 if (size > handle->size)
386 return -ENOSPC;
387
388 local_add(size, &rb->aux_head);
389
390 aux_head = rb->user_page->aux_head = local_read(&rb->aux_head);
391 if (aux_head - local_read(&rb->aux_wakeup) >= rb->aux_watermark) {
392 perf_output_wakeup(handle);
393 local_add(rb->aux_watermark, &rb->aux_wakeup);
394 handle->wakeup = local_read(&rb->aux_wakeup) +
395 rb->aux_watermark;
396 }
397
398 handle->head = aux_head;
399 handle->size -= size;
400
401 return 0;
402}
403
404void *perf_get_aux(struct perf_output_handle *handle)
405{
406 /* this is only valid between perf_aux_output_begin and *_end */
407 if (!handle->event)
408 return NULL;
409
410 return handle->rb->aux_priv;
411}
412
413#define PERF_AUX_GFP (GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY)
414
415static struct page *rb_alloc_aux_page(int node, int order)
416{
417 struct page *page;
418
419 if (order > MAX_ORDER)
420 order = MAX_ORDER;
421
422 do {
423 page = alloc_pages_node(node, PERF_AUX_GFP, order);
424 } while (!page && order--);
425
426 if (page && order) {
427 /*
428 * Communicate the allocation size to the driver
429 */
430 split_page(page, order);
431 SetPagePrivate(page);
432 set_page_private(page, order);
433 }
434
435 return page;
436}
437
438static void rb_free_aux_page(struct ring_buffer *rb, int idx)
439{
440 struct page *page = virt_to_page(rb->aux_pages[idx]);
441
442 ClearPagePrivate(page);
443 page->mapping = NULL;
444 __free_page(page);
445}
446
447int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
448 pgoff_t pgoff, int nr_pages, long watermark, int flags)
449{
450 bool overwrite = !(flags & RING_BUFFER_WRITABLE);
451 int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu);
452 int ret = -ENOMEM, max_order = 0;
453
454 if (!has_aux(event))
455 return -ENOTSUPP;
456
457 if (event->pmu->capabilities & PERF_PMU_CAP_AUX_NO_SG) {
458 /*
459 * We need to start with the max_order that fits in nr_pages,
460 * not the other way around, hence ilog2() and not get_order.
461 */
462 max_order = ilog2(nr_pages);
463
464 /*
465 * PMU requests more than one contiguous chunks of memory
466 * for SW double buffering
467 */
468 if ((event->pmu->capabilities & PERF_PMU_CAP_AUX_SW_DOUBLEBUF) &&
469 !overwrite) {
470 if (!max_order)
471 return -EINVAL;
472
473 max_order--;
474 }
475 }
476
477 rb->aux_pages = kzalloc_node(nr_pages * sizeof(void *), GFP_KERNEL, node);
478 if (!rb->aux_pages)
479 return -ENOMEM;
480
481 rb->free_aux = event->pmu->free_aux;
482 for (rb->aux_nr_pages = 0; rb->aux_nr_pages < nr_pages;) {
483 struct page *page;
484 int last, order;
485
486 order = min(max_order, ilog2(nr_pages - rb->aux_nr_pages));
487 page = rb_alloc_aux_page(node, order);
488 if (!page)
489 goto out;
490
491 for (last = rb->aux_nr_pages + (1 << page_private(page));
492 last > rb->aux_nr_pages; rb->aux_nr_pages++)
493 rb->aux_pages[rb->aux_nr_pages] = page_address(page++);
494 }
495
496 rb->aux_priv = event->pmu->setup_aux(event->cpu, rb->aux_pages, nr_pages,
497 overwrite);
498 if (!rb->aux_priv)
499 goto out;
500
501 ret = 0;
502
503 /*
504 * aux_pages (and pmu driver's private data, aux_priv) will be
505 * referenced in both producer's and consumer's contexts, thus
506 * we keep a refcount here to make sure either of the two can
507 * reference them safely.
508 */
509 atomic_set(&rb->aux_refcount, 1);
510
511 rb->aux_overwrite = overwrite;
512 rb->aux_watermark = watermark;
513
514 if (!rb->aux_watermark && !rb->aux_overwrite)
515 rb->aux_watermark = nr_pages << (PAGE_SHIFT - 1);
516
517out:
518 if (!ret)
519 rb->aux_pgoff = pgoff;
520 else
521 rb_free_aux(rb);
522
523 return ret;
524}
525
526static void __rb_free_aux(struct ring_buffer *rb)
527{
528 int pg;
529
530 if (rb->aux_priv) {
531 rb->free_aux(rb->aux_priv);
532 rb->free_aux = NULL;
533 rb->aux_priv = NULL;
534 }
535
536 for (pg = 0; pg < rb->aux_nr_pages; pg++)
537 rb_free_aux_page(rb, pg);
538
539 kfree(rb->aux_pages);
540 rb->aux_nr_pages = 0;
541}
542
543void rb_free_aux(struct ring_buffer *rb)
544{
545 if (atomic_dec_and_test(&rb->aux_refcount))
546 __rb_free_aux(rb);
547}
548
246#ifndef CONFIG_PERF_USE_VMALLOC 549#ifndef CONFIG_PERF_USE_VMALLOC
247 550
248/* 551/*
249 * Back perf_mmap() with regular GFP_KERNEL-0 pages. 552 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
250 */ 553 */
251 554
252struct page * 555static struct page *
253perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff) 556__perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
254{ 557{
255 if (pgoff > rb->nr_pages) 558 if (pgoff > rb->nr_pages)
256 return NULL; 559 return NULL;
@@ -340,8 +643,8 @@ static int data_page_nr(struct ring_buffer *rb)
340 return rb->nr_pages << page_order(rb); 643 return rb->nr_pages << page_order(rb);
341} 644}
342 645
343struct page * 646static struct page *
344perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff) 647__perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
345{ 648{
346 /* The '>' counts in the user page. */ 649 /* The '>' counts in the user page. */
347 if (pgoff > data_page_nr(rb)) 650 if (pgoff > data_page_nr(rb))
@@ -416,3 +719,19 @@ fail:
416} 719}
417 720
418#endif 721#endif
722
723struct page *
724perf_mmap_to_page(struct ring_buffer *rb, unsigned long pgoff)
725{
726 if (rb->aux_nr_pages) {
727 /* above AUX space */
728 if (pgoff > rb->aux_pgoff + rb->aux_nr_pages)
729 return NULL;
730
731 /* AUX space */
732 if (pgoff >= rb->aux_pgoff)
733 return virt_to_page(rb->aux_pages[pgoff - rb->aux_pgoff]);
734 }
735
736 return __perf_mmap_to_page(rb, pgoff);
737}
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index fedbdd7d5d1e..3b9a48ae153a 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -432,6 +432,14 @@ config UPROBE_EVENT
432 This option is required if you plan to use perf-probe subcommand 432 This option is required if you plan to use perf-probe subcommand
433 of perf tools on user space applications. 433 of perf tools on user space applications.
434 434
435config BPF_EVENTS
436 depends on BPF_SYSCALL
437 depends on KPROBE_EVENT
438 bool
439 default y
440 help
441 This allows the user to attach BPF programs to kprobe events.
442
435config PROBE_EVENTS 443config PROBE_EVENTS
436 def_bool n 444 def_bool n
437 445
diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
index 98f26588255e..9b1044e936a6 100644
--- a/kernel/trace/Makefile
+++ b/kernel/trace/Makefile
@@ -53,6 +53,7 @@ obj-$(CONFIG_EVENT_TRACING) += trace_event_perf.o
53endif 53endif
54obj-$(CONFIG_EVENT_TRACING) += trace_events_filter.o 54obj-$(CONFIG_EVENT_TRACING) += trace_events_filter.o
55obj-$(CONFIG_EVENT_TRACING) += trace_events_trigger.o 55obj-$(CONFIG_EVENT_TRACING) += trace_events_trigger.o
56obj-$(CONFIG_BPF_EVENTS) += bpf_trace.o
56obj-$(CONFIG_KPROBE_EVENT) += trace_kprobe.o 57obj-$(CONFIG_KPROBE_EVENT) += trace_kprobe.o
57obj-$(CONFIG_TRACEPOINTS) += power-traces.o 58obj-$(CONFIG_TRACEPOINTS) += power-traces.o
58ifeq ($(CONFIG_PM),y) 59ifeq ($(CONFIG_PM),y)
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
new file mode 100644
index 000000000000..2d56ce501632
--- /dev/null
+++ b/kernel/trace/bpf_trace.c
@@ -0,0 +1,222 @@
1/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/slab.h>
10#include <linux/bpf.h>
11#include <linux/filter.h>
12#include <linux/uaccess.h>
13#include <linux/ctype.h>
14#include "trace.h"
15
16static DEFINE_PER_CPU(int, bpf_prog_active);
17
18/**
19 * trace_call_bpf - invoke BPF program
20 * @prog: BPF program
21 * @ctx: opaque context pointer
22 *
23 * kprobe handlers execute BPF programs via this helper.
24 * Can be used from static tracepoints in the future.
25 *
26 * Return: BPF programs always return an integer which is interpreted by
27 * kprobe handler as:
28 * 0 - return from kprobe (event is filtered out)
29 * 1 - store kprobe event into ring buffer
30 * Other values are reserved and currently alias to 1
31 */
32unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
33{
34 unsigned int ret;
35
36 if (in_nmi()) /* not supported yet */
37 return 1;
38
39 preempt_disable();
40
41 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) {
42 /*
43 * since some bpf program is already running on this cpu,
44 * don't call into another bpf program (same or different)
45 * and don't send kprobe event into ring-buffer,
46 * so return zero here
47 */
48 ret = 0;
49 goto out;
50 }
51
52 rcu_read_lock();
53 ret = BPF_PROG_RUN(prog, ctx);
54 rcu_read_unlock();
55
56 out:
57 __this_cpu_dec(bpf_prog_active);
58 preempt_enable();
59
60 return ret;
61}
62EXPORT_SYMBOL_GPL(trace_call_bpf);
63
64static u64 bpf_probe_read(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
65{
66 void *dst = (void *) (long) r1;
67 int size = (int) r2;
68 void *unsafe_ptr = (void *) (long) r3;
69
70 return probe_kernel_read(dst, unsafe_ptr, size);
71}
72
73static const struct bpf_func_proto bpf_probe_read_proto = {
74 .func = bpf_probe_read,
75 .gpl_only = true,
76 .ret_type = RET_INTEGER,
77 .arg1_type = ARG_PTR_TO_STACK,
78 .arg2_type = ARG_CONST_STACK_SIZE,
79 .arg3_type = ARG_ANYTHING,
80};
81
82static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
83{
84 /* NMI safe access to clock monotonic */
85 return ktime_get_mono_fast_ns();
86}
87
88static const struct bpf_func_proto bpf_ktime_get_ns_proto = {
89 .func = bpf_ktime_get_ns,
90 .gpl_only = true,
91 .ret_type = RET_INTEGER,
92};
93
94/*
95 * limited trace_printk()
96 * only %d %u %x %ld %lu %lx %lld %llu %llx %p conversion specifiers allowed
97 */
98static u64 bpf_trace_printk(u64 r1, u64 fmt_size, u64 r3, u64 r4, u64 r5)
99{
100 char *fmt = (char *) (long) r1;
101 int mod[3] = {};
102 int fmt_cnt = 0;
103 int i;
104
105 /*
106 * bpf_check()->check_func_arg()->check_stack_boundary()
107 * guarantees that fmt points to bpf program stack,
108 * fmt_size bytes of it were initialized and fmt_size > 0
109 */
110 if (fmt[--fmt_size] != 0)
111 return -EINVAL;
112
113 /* check format string for allowed specifiers */
114 for (i = 0; i < fmt_size; i++) {
115 if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
116 return -EINVAL;
117
118 if (fmt[i] != '%')
119 continue;
120
121 if (fmt_cnt >= 3)
122 return -EINVAL;
123
124 /* fmt[i] != 0 && fmt[last] == 0, so we can access fmt[i + 1] */
125 i++;
126 if (fmt[i] == 'l') {
127 mod[fmt_cnt]++;
128 i++;
129 } else if (fmt[i] == 'p') {
130 mod[fmt_cnt]++;
131 i++;
132 if (!isspace(fmt[i]) && !ispunct(fmt[i]) && fmt[i] != 0)
133 return -EINVAL;
134 fmt_cnt++;
135 continue;
136 }
137
138 if (fmt[i] == 'l') {
139 mod[fmt_cnt]++;
140 i++;
141 }
142
143 if (fmt[i] != 'd' && fmt[i] != 'u' && fmt[i] != 'x')
144 return -EINVAL;
145 fmt_cnt++;
146 }
147
148 return __trace_printk(1/* fake ip will not be printed */, fmt,
149 mod[0] == 2 ? r3 : mod[0] == 1 ? (long) r3 : (u32) r3,
150 mod[1] == 2 ? r4 : mod[1] == 1 ? (long) r4 : (u32) r4,
151 mod[2] == 2 ? r5 : mod[2] == 1 ? (long) r5 : (u32) r5);
152}
153
154static const struct bpf_func_proto bpf_trace_printk_proto = {
155 .func = bpf_trace_printk,
156 .gpl_only = true,
157 .ret_type = RET_INTEGER,
158 .arg1_type = ARG_PTR_TO_STACK,
159 .arg2_type = ARG_CONST_STACK_SIZE,
160};
161
162static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
163{
164 switch (func_id) {
165 case BPF_FUNC_map_lookup_elem:
166 return &bpf_map_lookup_elem_proto;
167 case BPF_FUNC_map_update_elem:
168 return &bpf_map_update_elem_proto;
169 case BPF_FUNC_map_delete_elem:
170 return &bpf_map_delete_elem_proto;
171 case BPF_FUNC_probe_read:
172 return &bpf_probe_read_proto;
173 case BPF_FUNC_ktime_get_ns:
174 return &bpf_ktime_get_ns_proto;
175
176 case BPF_FUNC_trace_printk:
177 /*
178 * this program might be calling bpf_trace_printk,
179 * so allocate per-cpu printk buffers
180 */
181 trace_printk_init_buffers();
182
183 return &bpf_trace_printk_proto;
184 default:
185 return NULL;
186 }
187}
188
189/* bpf+kprobe programs can access fields of 'struct pt_regs' */
190static bool kprobe_prog_is_valid_access(int off, int size, enum bpf_access_type type)
191{
192 /* check bounds */
193 if (off < 0 || off >= sizeof(struct pt_regs))
194 return false;
195
196 /* only read is allowed */
197 if (type != BPF_READ)
198 return false;
199
200 /* disallow misaligned access */
201 if (off % size != 0)
202 return false;
203
204 return true;
205}
206
207static struct bpf_verifier_ops kprobe_prog_ops = {
208 .get_func_proto = kprobe_prog_func_proto,
209 .is_valid_access = kprobe_prog_is_valid_access,
210};
211
212static struct bpf_prog_type_list kprobe_tl = {
213 .ops = &kprobe_prog_ops,
214 .type = BPF_PROG_TYPE_KPROBE,
215};
216
217static int __init register_kprobe_prog_ops(void)
218{
219 bpf_register_prog_type(&kprobe_tl);
220 return 0;
221}
222late_initcall(register_kprobe_prog_ops);
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 9ba3f43f580e..d0ce590f06e1 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -1135,11 +1135,15 @@ static void
1135kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) 1135kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1136{ 1136{
1137 struct ftrace_event_call *call = &tk->tp.call; 1137 struct ftrace_event_call *call = &tk->tp.call;
1138 struct bpf_prog *prog = call->prog;
1138 struct kprobe_trace_entry_head *entry; 1139 struct kprobe_trace_entry_head *entry;
1139 struct hlist_head *head; 1140 struct hlist_head *head;
1140 int size, __size, dsize; 1141 int size, __size, dsize;
1141 int rctx; 1142 int rctx;
1142 1143
1144 if (prog && !trace_call_bpf(prog, regs))
1145 return;
1146
1143 head = this_cpu_ptr(call->perf_events); 1147 head = this_cpu_ptr(call->perf_events);
1144 if (hlist_empty(head)) 1148 if (hlist_empty(head))
1145 return; 1149 return;
@@ -1166,11 +1170,15 @@ kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1166 struct pt_regs *regs) 1170 struct pt_regs *regs)
1167{ 1171{
1168 struct ftrace_event_call *call = &tk->tp.call; 1172 struct ftrace_event_call *call = &tk->tp.call;
1173 struct bpf_prog *prog = call->prog;
1169 struct kretprobe_trace_entry_head *entry; 1174 struct kretprobe_trace_entry_head *entry;
1170 struct hlist_head *head; 1175 struct hlist_head *head;
1171 int size, __size, dsize; 1176 int size, __size, dsize;
1172 int rctx; 1177 int rctx;
1173 1178
1179 if (prog && !trace_call_bpf(prog, regs))
1180 return;
1181
1174 head = this_cpu_ptr(call->perf_events); 1182 head = this_cpu_ptr(call->perf_events);
1175 if (hlist_empty(head)) 1183 if (hlist_empty(head))
1176 return; 1184 return;
@@ -1287,7 +1295,7 @@ static int register_kprobe_event(struct trace_kprobe *tk)
1287 kfree(call->print_fmt); 1295 kfree(call->print_fmt);
1288 return -ENODEV; 1296 return -ENODEV;
1289 } 1297 }
1290 call->flags = 0; 1298 call->flags = TRACE_EVENT_FL_KPROBE;
1291 call->class->reg = kprobe_register; 1299 call->class->reg = kprobe_register;
1292 call->data = tk; 1300 call->data = tk;
1293 ret = trace_add_event_call(call); 1301 ret = trace_add_event_call(call);
diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c
index 74865465e0b7..d60fe62ec4fa 100644
--- a/kernel/trace/trace_uprobe.c
+++ b/kernel/trace/trace_uprobe.c
@@ -1006,7 +1006,7 @@ __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1006 return true; 1006 return true;
1007 1007
1008 list_for_each_entry(event, &filter->perf_events, hw.tp_list) { 1008 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1009 if (event->hw.tp_target->mm == mm) 1009 if (event->hw.target->mm == mm)
1010 return true; 1010 return true;
1011 } 1011 }
1012 1012
@@ -1016,7 +1016,7 @@ __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1016static inline bool 1016static inline bool
1017uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event) 1017uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1018{ 1018{
1019 return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm); 1019 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1020} 1020}
1021 1021
1022static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) 1022static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
@@ -1024,10 +1024,10 @@ static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1024 bool done; 1024 bool done;
1025 1025
1026 write_lock(&tu->filter.rwlock); 1026 write_lock(&tu->filter.rwlock);
1027 if (event->hw.tp_target) { 1027 if (event->hw.target) {
1028 list_del(&event->hw.tp_list); 1028 list_del(&event->hw.tp_list);
1029 done = tu->filter.nr_systemwide || 1029 done = tu->filter.nr_systemwide ||
1030 (event->hw.tp_target->flags & PF_EXITING) || 1030 (event->hw.target->flags & PF_EXITING) ||
1031 uprobe_filter_event(tu, event); 1031 uprobe_filter_event(tu, event);
1032 } else { 1032 } else {
1033 tu->filter.nr_systemwide--; 1033 tu->filter.nr_systemwide--;
@@ -1047,7 +1047,7 @@ static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1047 int err; 1047 int err;
1048 1048
1049 write_lock(&tu->filter.rwlock); 1049 write_lock(&tu->filter.rwlock);
1050 if (event->hw.tp_target) { 1050 if (event->hw.target) {
1051 /* 1051 /*
1052 * event->parent != NULL means copy_process(), we can avoid 1052 * event->parent != NULL means copy_process(), we can avoid
1053 * uprobe_apply(). current->mm must be probed and we can rely 1053 * uprobe_apply(). current->mm must be probed and we can rely
diff --git a/kernel/watchdog.c b/kernel/watchdog.c
index 3174bf8e3538..9a056f5bc02c 100644
--- a/kernel/watchdog.c
+++ b/kernel/watchdog.c
@@ -567,9 +567,37 @@ static void watchdog_nmi_disable(unsigned int cpu)
567 cpu0_err = 0; 567 cpu0_err = 0;
568 } 568 }
569} 569}
570
571void watchdog_nmi_enable_all(void)
572{
573 int cpu;
574
575 if (!watchdog_user_enabled)
576 return;
577
578 get_online_cpus();
579 for_each_online_cpu(cpu)
580 watchdog_nmi_enable(cpu);
581 put_online_cpus();
582}
583
584void watchdog_nmi_disable_all(void)
585{
586 int cpu;
587
588 if (!watchdog_running)
589 return;
590
591 get_online_cpus();
592 for_each_online_cpu(cpu)
593 watchdog_nmi_disable(cpu);
594 put_online_cpus();
595}
570#else 596#else
571static int watchdog_nmi_enable(unsigned int cpu) { return 0; } 597static int watchdog_nmi_enable(unsigned int cpu) { return 0; }
572static void watchdog_nmi_disable(unsigned int cpu) { return; } 598static void watchdog_nmi_disable(unsigned int cpu) { return; }
599void watchdog_nmi_enable_all(void) {}
600void watchdog_nmi_disable_all(void) {}
573#endif /* CONFIG_HARDLOCKUP_DETECTOR */ 601#endif /* CONFIG_HARDLOCKUP_DETECTOR */
574 602
575static struct smp_hotplug_thread watchdog_threads = { 603static struct smp_hotplug_thread watchdog_threads = {
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index b5b3600dcdf5..fe98fb226e6e 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -6,23 +6,39 @@ hostprogs-y := test_verifier test_maps
6hostprogs-y += sock_example 6hostprogs-y += sock_example
7hostprogs-y += sockex1 7hostprogs-y += sockex1
8hostprogs-y += sockex2 8hostprogs-y += sockex2
9hostprogs-y += tracex1
10hostprogs-y += tracex2
11hostprogs-y += tracex3
12hostprogs-y += tracex4
9 13
10test_verifier-objs := test_verifier.o libbpf.o 14test_verifier-objs := test_verifier.o libbpf.o
11test_maps-objs := test_maps.o libbpf.o 15test_maps-objs := test_maps.o libbpf.o
12sock_example-objs := sock_example.o libbpf.o 16sock_example-objs := sock_example.o libbpf.o
13sockex1-objs := bpf_load.o libbpf.o sockex1_user.o 17sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
14sockex2-objs := bpf_load.o libbpf.o sockex2_user.o 18sockex2-objs := bpf_load.o libbpf.o sockex2_user.o
19tracex1-objs := bpf_load.o libbpf.o tracex1_user.o
20tracex2-objs := bpf_load.o libbpf.o tracex2_user.o
21tracex3-objs := bpf_load.o libbpf.o tracex3_user.o
22tracex4-objs := bpf_load.o libbpf.o tracex4_user.o
15 23
16# Tell kbuild to always build the programs 24# Tell kbuild to always build the programs
17always := $(hostprogs-y) 25always := $(hostprogs-y)
18always += sockex1_kern.o 26always += sockex1_kern.o
19always += sockex2_kern.o 27always += sockex2_kern.o
28always += tracex1_kern.o
29always += tracex2_kern.o
30always += tracex3_kern.o
31always += tracex4_kern.o
20 32
21HOSTCFLAGS += -I$(objtree)/usr/include 33HOSTCFLAGS += -I$(objtree)/usr/include
22 34
23HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable 35HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable
24HOSTLOADLIBES_sockex1 += -lelf 36HOSTLOADLIBES_sockex1 += -lelf
25HOSTLOADLIBES_sockex2 += -lelf 37HOSTLOADLIBES_sockex2 += -lelf
38HOSTLOADLIBES_tracex1 += -lelf
39HOSTLOADLIBES_tracex2 += -lelf
40HOSTLOADLIBES_tracex3 += -lelf
41HOSTLOADLIBES_tracex4 += -lelf -lrt
26 42
27# point this to your LLVM backend with bpf support 43# point this to your LLVM backend with bpf support
28LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc 44LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
diff --git a/samples/bpf/bpf_helpers.h b/samples/bpf/bpf_helpers.h
index ca0333146006..1c872bcf5a80 100644
--- a/samples/bpf/bpf_helpers.h
+++ b/samples/bpf/bpf_helpers.h
@@ -15,6 +15,12 @@ static int (*bpf_map_update_elem)(void *map, void *key, void *value,
15 (void *) BPF_FUNC_map_update_elem; 15 (void *) BPF_FUNC_map_update_elem;
16static int (*bpf_map_delete_elem)(void *map, void *key) = 16static int (*bpf_map_delete_elem)(void *map, void *key) =
17 (void *) BPF_FUNC_map_delete_elem; 17 (void *) BPF_FUNC_map_delete_elem;
18static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
19 (void *) BPF_FUNC_probe_read;
20static unsigned long long (*bpf_ktime_get_ns)(void) =
21 (void *) BPF_FUNC_ktime_get_ns;
22static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
23 (void *) BPF_FUNC_trace_printk;
18 24
19/* llvm builtin functions that eBPF C program may use to 25/* llvm builtin functions that eBPF C program may use to
20 * emit BPF_LD_ABS and BPF_LD_IND instructions 26 * emit BPF_LD_ABS and BPF_LD_IND instructions
diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c
index 1831d236382b..38dac5a53b51 100644
--- a/samples/bpf/bpf_load.c
+++ b/samples/bpf/bpf_load.c
@@ -8,29 +8,70 @@
8#include <unistd.h> 8#include <unistd.h>
9#include <string.h> 9#include <string.h>
10#include <stdbool.h> 10#include <stdbool.h>
11#include <stdlib.h>
11#include <linux/bpf.h> 12#include <linux/bpf.h>
12#include <linux/filter.h> 13#include <linux/filter.h>
14#include <linux/perf_event.h>
15#include <sys/syscall.h>
16#include <sys/ioctl.h>
17#include <sys/mman.h>
18#include <poll.h>
13#include "libbpf.h" 19#include "libbpf.h"
14#include "bpf_helpers.h" 20#include "bpf_helpers.h"
15#include "bpf_load.h" 21#include "bpf_load.h"
16 22
23#define DEBUGFS "/sys/kernel/debug/tracing/"
24
17static char license[128]; 25static char license[128];
26static int kern_version;
18static bool processed_sec[128]; 27static bool processed_sec[128];
19int map_fd[MAX_MAPS]; 28int map_fd[MAX_MAPS];
20int prog_fd[MAX_PROGS]; 29int prog_fd[MAX_PROGS];
30int event_fd[MAX_PROGS];
21int prog_cnt; 31int prog_cnt;
22 32
23static int load_and_attach(const char *event, struct bpf_insn *prog, int size) 33static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
24{ 34{
25 int fd;
26 bool is_socket = strncmp(event, "socket", 6) == 0; 35 bool is_socket = strncmp(event, "socket", 6) == 0;
27 36 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
28 if (!is_socket) 37 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
29 /* tracing events tbd */ 38 enum bpf_prog_type prog_type;
39 char buf[256];
40 int fd, efd, err, id;
41 struct perf_event_attr attr = {};
42
43 attr.type = PERF_TYPE_TRACEPOINT;
44 attr.sample_type = PERF_SAMPLE_RAW;
45 attr.sample_period = 1;
46 attr.wakeup_events = 1;
47
48 if (is_socket) {
49 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
50 } else if (is_kprobe || is_kretprobe) {
51 prog_type = BPF_PROG_TYPE_KPROBE;
52 } else {
53 printf("Unknown event '%s'\n", event);
30 return -1; 54 return -1;
55 }
56
57 if (is_kprobe || is_kretprobe) {
58 if (is_kprobe)
59 event += 7;
60 else
61 event += 10;
62
63 snprintf(buf, sizeof(buf),
64 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
65 is_kprobe ? 'p' : 'r', event, event);
66 err = system(buf);
67 if (err < 0) {
68 printf("failed to create kprobe '%s' error '%s'\n",
69 event, strerror(errno));
70 return -1;
71 }
72 }
31 73
32 fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, 74 fd = bpf_prog_load(prog_type, prog, size, license, kern_version);
33 prog, size, license);
34 75
35 if (fd < 0) { 76 if (fd < 0) {
36 printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf); 77 printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf);
@@ -39,6 +80,41 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
39 80
40 prog_fd[prog_cnt++] = fd; 81 prog_fd[prog_cnt++] = fd;
41 82
83 if (is_socket)
84 return 0;
85
86 strcpy(buf, DEBUGFS);
87 strcat(buf, "events/kprobes/");
88 strcat(buf, event);
89 strcat(buf, "/id");
90
91 efd = open(buf, O_RDONLY, 0);
92 if (efd < 0) {
93 printf("failed to open event %s\n", event);
94 return -1;
95 }
96
97 err = read(efd, buf, sizeof(buf));
98 if (err < 0 || err >= sizeof(buf)) {
99 printf("read from '%s' failed '%s'\n", event, strerror(errno));
100 return -1;
101 }
102
103 close(efd);
104
105 buf[err] = 0;
106 id = atoi(buf);
107 attr.config = id;
108
109 efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
110 if (efd < 0) {
111 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
112 return -1;
113 }
114 event_fd[prog_cnt - 1] = efd;
115 ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
116 ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
117
42 return 0; 118 return 0;
43} 119}
44 120
@@ -135,6 +211,9 @@ int load_bpf_file(char *path)
135 if (gelf_getehdr(elf, &ehdr) != &ehdr) 211 if (gelf_getehdr(elf, &ehdr) != &ehdr)
136 return 1; 212 return 1;
137 213
214 /* clear all kprobes */
215 i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
216
138 /* scan over all elf sections to get license and map info */ 217 /* scan over all elf sections to get license and map info */
139 for (i = 1; i < ehdr.e_shnum; i++) { 218 for (i = 1; i < ehdr.e_shnum; i++) {
140 219
@@ -149,6 +228,14 @@ int load_bpf_file(char *path)
149 if (strcmp(shname, "license") == 0) { 228 if (strcmp(shname, "license") == 0) {
150 processed_sec[i] = true; 229 processed_sec[i] = true;
151 memcpy(license, data->d_buf, data->d_size); 230 memcpy(license, data->d_buf, data->d_size);
231 } else if (strcmp(shname, "version") == 0) {
232 processed_sec[i] = true;
233 if (data->d_size != sizeof(int)) {
234 printf("invalid size of version section %zd\n",
235 data->d_size);
236 return 1;
237 }
238 memcpy(&kern_version, data->d_buf, sizeof(int));
152 } else if (strcmp(shname, "maps") == 0) { 239 } else if (strcmp(shname, "maps") == 0) {
153 processed_sec[i] = true; 240 processed_sec[i] = true;
154 if (load_maps(data->d_buf, data->d_size)) 241 if (load_maps(data->d_buf, data->d_size))
@@ -178,7 +265,8 @@ int load_bpf_file(char *path)
178 if (parse_relo_and_apply(data, symbols, &shdr, insns)) 265 if (parse_relo_and_apply(data, symbols, &shdr, insns))
179 continue; 266 continue;
180 267
181 if (memcmp(shname_prog, "events/", 7) == 0 || 268 if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
269 memcmp(shname_prog, "kretprobe/", 10) == 0 ||
182 memcmp(shname_prog, "socket", 6) == 0) 270 memcmp(shname_prog, "socket", 6) == 0)
183 load_and_attach(shname_prog, insns, data_prog->d_size); 271 load_and_attach(shname_prog, insns, data_prog->d_size);
184 } 272 }
@@ -193,7 +281,8 @@ int load_bpf_file(char *path)
193 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) 281 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
194 continue; 282 continue;
195 283
196 if (memcmp(shname, "events/", 7) == 0 || 284 if (memcmp(shname, "kprobe/", 7) == 0 ||
285 memcmp(shname, "kretprobe/", 10) == 0 ||
197 memcmp(shname, "socket", 6) == 0) 286 memcmp(shname, "socket", 6) == 0)
198 load_and_attach(shname, data->d_buf, data->d_size); 287 load_and_attach(shname, data->d_buf, data->d_size);
199 } 288 }
@@ -201,3 +290,23 @@ int load_bpf_file(char *path)
201 close(fd); 290 close(fd);
202 return 0; 291 return 0;
203} 292}
293
294void read_trace_pipe(void)
295{
296 int trace_fd;
297
298 trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
299 if (trace_fd < 0)
300 return;
301
302 while (1) {
303 static char buf[4096];
304 ssize_t sz;
305
306 sz = read(trace_fd, buf, sizeof(buf));
307 if (sz > 0) {
308 buf[sz] = 0;
309 puts(buf);
310 }
311 }
312}
diff --git a/samples/bpf/bpf_load.h b/samples/bpf/bpf_load.h
index 27789a34f5e6..cbd7c2b532b9 100644
--- a/samples/bpf/bpf_load.h
+++ b/samples/bpf/bpf_load.h
@@ -6,6 +6,7 @@
6 6
7extern int map_fd[MAX_MAPS]; 7extern int map_fd[MAX_MAPS];
8extern int prog_fd[MAX_PROGS]; 8extern int prog_fd[MAX_PROGS];
9extern int event_fd[MAX_PROGS];
9 10
10/* parses elf file compiled by llvm .c->.o 11/* parses elf file compiled by llvm .c->.o
11 * . parses 'maps' section and creates maps via BPF syscall 12 * . parses 'maps' section and creates maps via BPF syscall
@@ -21,4 +22,6 @@ extern int prog_fd[MAX_PROGS];
21 */ 22 */
22int load_bpf_file(char *path); 23int load_bpf_file(char *path);
23 24
25void read_trace_pipe(void);
26
24#endif 27#endif
diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c
index 46d50b7ddf79..7e1efa7e2ed7 100644
--- a/samples/bpf/libbpf.c
+++ b/samples/bpf/libbpf.c
@@ -81,7 +81,7 @@ char bpf_log_buf[LOG_BUF_SIZE];
81 81
82int bpf_prog_load(enum bpf_prog_type prog_type, 82int bpf_prog_load(enum bpf_prog_type prog_type,
83 const struct bpf_insn *insns, int prog_len, 83 const struct bpf_insn *insns, int prog_len,
84 const char *license) 84 const char *license, int kern_version)
85{ 85{
86 union bpf_attr attr = { 86 union bpf_attr attr = {
87 .prog_type = prog_type, 87 .prog_type = prog_type,
@@ -93,6 +93,11 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
93 .log_level = 1, 93 .log_level = 1,
94 }; 94 };
95 95
96 /* assign one field outside of struct init to make sure any
97 * padding is zero initialized
98 */
99 attr.kern_version = kern_version;
100
96 bpf_log_buf[0] = 0; 101 bpf_log_buf[0] = 0;
97 102
98 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); 103 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
@@ -121,3 +126,10 @@ int open_raw_sock(const char *name)
121 126
122 return sock; 127 return sock;
123} 128}
129
130int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
131 int group_fd, unsigned long flags)
132{
133 return syscall(__NR_perf_event_open, attr, pid, cpu,
134 group_fd, flags);
135}
diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h
index 58c5fe1bdba1..ac7b09672b46 100644
--- a/samples/bpf/libbpf.h
+++ b/samples/bpf/libbpf.h
@@ -13,7 +13,7 @@ int bpf_get_next_key(int fd, void *key, void *next_key);
13 13
14int bpf_prog_load(enum bpf_prog_type prog_type, 14int bpf_prog_load(enum bpf_prog_type prog_type,
15 const struct bpf_insn *insns, int insn_len, 15 const struct bpf_insn *insns, int insn_len,
16 const char *license); 16 const char *license, int kern_version);
17 17
18#define LOG_BUF_SIZE 65536 18#define LOG_BUF_SIZE 65536
19extern char bpf_log_buf[LOG_BUF_SIZE]; 19extern char bpf_log_buf[LOG_BUF_SIZE];
@@ -182,4 +182,7 @@ extern char bpf_log_buf[LOG_BUF_SIZE];
182/* create RAW socket and bind to interface 'name' */ 182/* create RAW socket and bind to interface 'name' */
183int open_raw_sock(const char *name); 183int open_raw_sock(const char *name);
184 184
185struct perf_event_attr;
186int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
187 int group_fd, unsigned long flags);
185#endif 188#endif
diff --git a/samples/bpf/sock_example.c b/samples/bpf/sock_example.c
index c8ad0404416f..a0ce251c5390 100644
--- a/samples/bpf/sock_example.c
+++ b/samples/bpf/sock_example.c
@@ -56,7 +56,7 @@ static int test_sock(void)
56 }; 56 };
57 57
58 prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog), 58 prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog),
59 "GPL"); 59 "GPL", 0);
60 if (prog_fd < 0) { 60 if (prog_fd < 0) {
61 printf("failed to load prog '%s'\n", strerror(errno)); 61 printf("failed to load prog '%s'\n", strerror(errno));
62 goto cleanup; 62 goto cleanup;
diff --git a/samples/bpf/test_verifier.c b/samples/bpf/test_verifier.c
index b96175e90363..740ce97cda5e 100644
--- a/samples/bpf/test_verifier.c
+++ b/samples/bpf/test_verifier.c
@@ -689,7 +689,7 @@ static int test(void)
689 689
690 prog_fd = bpf_prog_load(BPF_PROG_TYPE_UNSPEC, prog, 690 prog_fd = bpf_prog_load(BPF_PROG_TYPE_UNSPEC, prog,
691 prog_len * sizeof(struct bpf_insn), 691 prog_len * sizeof(struct bpf_insn),
692 "GPL"); 692 "GPL", 0);
693 693
694 if (tests[i].result == ACCEPT) { 694 if (tests[i].result == ACCEPT) {
695 if (prog_fd < 0) { 695 if (prog_fd < 0) {
diff --git a/samples/bpf/tracex1_kern.c b/samples/bpf/tracex1_kern.c
new file mode 100644
index 000000000000..31620463701a
--- /dev/null
+++ b/samples/bpf/tracex1_kern.c
@@ -0,0 +1,50 @@
1/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/skbuff.h>
8#include <linux/netdevice.h>
9#include <uapi/linux/bpf.h>
10#include <linux/version.h>
11#include "bpf_helpers.h"
12
13#define _(P) ({typeof(P) val = 0; bpf_probe_read(&val, sizeof(val), &P); val;})
14
15/* kprobe is NOT a stable ABI
16 * kernel functions can be removed, renamed or completely change semantics.
17 * Number of arguments and their positions can change, etc.
18 * In such case this bpf+kprobe example will no longer be meaningful
19 */
20SEC("kprobe/__netif_receive_skb_core")
21int bpf_prog1(struct pt_regs *ctx)
22{
23 /* attaches to kprobe netif_receive_skb,
24 * looks for packets on loobpack device and prints them
25 */
26 char devname[IFNAMSIZ] = {};
27 struct net_device *dev;
28 struct sk_buff *skb;
29 int len;
30
31 /* non-portable! works for the given kernel only */
32 skb = (struct sk_buff *) ctx->di;
33
34 dev = _(skb->dev);
35
36 len = _(skb->len);
37
38 bpf_probe_read(devname, sizeof(devname), dev->name);
39
40 if (devname[0] == 'l' && devname[1] == 'o') {
41 char fmt[] = "skb %p len %d\n";
42 /* using bpf_trace_printk() for DEBUG ONLY */
43 bpf_trace_printk(fmt, sizeof(fmt), skb, len);
44 }
45
46 return 0;
47}
48
49char _license[] SEC("license") = "GPL";
50u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/tracex1_user.c b/samples/bpf/tracex1_user.c
new file mode 100644
index 000000000000..31a48183beea
--- /dev/null
+++ b/samples/bpf/tracex1_user.c
@@ -0,0 +1,25 @@
1#include <stdio.h>
2#include <linux/bpf.h>
3#include <unistd.h>
4#include "libbpf.h"
5#include "bpf_load.h"
6
7int main(int ac, char **argv)
8{
9 FILE *f;
10 char filename[256];
11
12 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
13
14 if (load_bpf_file(filename)) {
15 printf("%s", bpf_log_buf);
16 return 1;
17 }
18
19 f = popen("taskset 1 ping -c5 localhost", "r");
20 (void) f;
21
22 read_trace_pipe();
23
24 return 0;
25}
diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
new file mode 100644
index 000000000000..19ec1cfc45db
--- /dev/null
+++ b/samples/bpf/tracex2_kern.c
@@ -0,0 +1,86 @@
1/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/skbuff.h>
8#include <linux/netdevice.h>
9#include <linux/version.h>
10#include <uapi/linux/bpf.h>
11#include "bpf_helpers.h"
12
13struct bpf_map_def SEC("maps") my_map = {
14 .type = BPF_MAP_TYPE_HASH,
15 .key_size = sizeof(long),
16 .value_size = sizeof(long),
17 .max_entries = 1024,
18};
19
20/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
21 * example will no longer be meaningful
22 */
23SEC("kprobe/kfree_skb")
24int bpf_prog2(struct pt_regs *ctx)
25{
26 long loc = 0;
27 long init_val = 1;
28 long *value;
29
30 /* x64 specific: read ip of kfree_skb caller.
31 * non-portable version of __builtin_return_address(0)
32 */
33 bpf_probe_read(&loc, sizeof(loc), (void *)ctx->sp);
34
35 value = bpf_map_lookup_elem(&my_map, &loc);
36 if (value)
37 *value += 1;
38 else
39 bpf_map_update_elem(&my_map, &loc, &init_val, BPF_ANY);
40 return 0;
41}
42
43static unsigned int log2(unsigned int v)
44{
45 unsigned int r;
46 unsigned int shift;
47
48 r = (v > 0xFFFF) << 4; v >>= r;
49 shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
50 shift = (v > 0xF) << 2; v >>= shift; r |= shift;
51 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
52 r |= (v >> 1);
53 return r;
54}
55
56static unsigned int log2l(unsigned long v)
57{
58 unsigned int hi = v >> 32;
59 if (hi)
60 return log2(hi) + 32;
61 else
62 return log2(v);
63}
64
65struct bpf_map_def SEC("maps") my_hist_map = {
66 .type = BPF_MAP_TYPE_ARRAY,
67 .key_size = sizeof(u32),
68 .value_size = sizeof(long),
69 .max_entries = 64,
70};
71
72SEC("kprobe/sys_write")
73int bpf_prog3(struct pt_regs *ctx)
74{
75 long write_size = ctx->dx; /* arg3 */
76 long init_val = 1;
77 long *value;
78 u32 index = log2l(write_size);
79
80 value = bpf_map_lookup_elem(&my_hist_map, &index);
81 if (value)
82 __sync_fetch_and_add(value, 1);
83 return 0;
84}
85char _license[] SEC("license") = "GPL";
86u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/tracex2_user.c b/samples/bpf/tracex2_user.c
new file mode 100644
index 000000000000..91b8d0896fbb
--- /dev/null
+++ b/samples/bpf/tracex2_user.c
@@ -0,0 +1,95 @@
1#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <signal.h>
5#include <linux/bpf.h>
6#include "libbpf.h"
7#include "bpf_load.h"
8
9#define MAX_INDEX 64
10#define MAX_STARS 38
11
12static void stars(char *str, long val, long max, int width)
13{
14 int i;
15
16 for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
17 str[i] = '*';
18 if (val > max)
19 str[i - 1] = '+';
20 str[i] = '\0';
21}
22
23static void print_hist(int fd)
24{
25 int key;
26 long value;
27 long data[MAX_INDEX] = {};
28 char starstr[MAX_STARS];
29 int i;
30 int max_ind = -1;
31 long max_value = 0;
32
33 for (key = 0; key < MAX_INDEX; key++) {
34 bpf_lookup_elem(fd, &key, &value);
35 data[key] = value;
36 if (value && key > max_ind)
37 max_ind = key;
38 if (value > max_value)
39 max_value = value;
40 }
41
42 printf(" syscall write() stats\n");
43 printf(" byte_size : count distribution\n");
44 for (i = 1; i <= max_ind + 1; i++) {
45 stars(starstr, data[i - 1], max_value, MAX_STARS);
46 printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
47 (1l << i) >> 1, (1l << i) - 1, data[i - 1],
48 MAX_STARS, starstr);
49 }
50}
51static void int_exit(int sig)
52{
53 print_hist(map_fd[1]);
54 exit(0);
55}
56
57int main(int ac, char **argv)
58{
59 char filename[256];
60 long key, next_key, value;
61 FILE *f;
62 int i;
63
64 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
65
66 signal(SIGINT, int_exit);
67
68 /* start 'ping' in the background to have some kfree_skb events */
69 f = popen("ping -c5 localhost", "r");
70 (void) f;
71
72 /* start 'dd' in the background to have plenty of 'write' syscalls */
73 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
74 (void) f;
75
76 if (load_bpf_file(filename)) {
77 printf("%s", bpf_log_buf);
78 return 1;
79 }
80
81 for (i = 0; i < 5; i++) {
82 key = 0;
83 while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
84 bpf_lookup_elem(map_fd[0], &next_key, &value);
85 printf("location 0x%lx count %ld\n", next_key, value);
86 key = next_key;
87 }
88 if (key)
89 printf("\n");
90 sleep(1);
91 }
92 print_hist(map_fd[1]);
93
94 return 0;
95}
diff --git a/samples/bpf/tracex3_kern.c b/samples/bpf/tracex3_kern.c
new file mode 100644
index 000000000000..255ff2792366
--- /dev/null
+++ b/samples/bpf/tracex3_kern.c
@@ -0,0 +1,89 @@
1/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/skbuff.h>
8#include <linux/netdevice.h>
9#include <linux/version.h>
10#include <uapi/linux/bpf.h>
11#include "bpf_helpers.h"
12
13struct bpf_map_def SEC("maps") my_map = {
14 .type = BPF_MAP_TYPE_HASH,
15 .key_size = sizeof(long),
16 .value_size = sizeof(u64),
17 .max_entries = 4096,
18};
19
20/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
21 * example will no longer be meaningful
22 */
23SEC("kprobe/blk_mq_start_request")
24int bpf_prog1(struct pt_regs *ctx)
25{
26 long rq = ctx->di;
27 u64 val = bpf_ktime_get_ns();
28
29 bpf_map_update_elem(&my_map, &rq, &val, BPF_ANY);
30 return 0;
31}
32
33static unsigned int log2l(unsigned long long n)
34{
35#define S(k) if (n >= (1ull << k)) { i += k; n >>= k; }
36 int i = -(n == 0);
37 S(32); S(16); S(8); S(4); S(2); S(1);
38 return i;
39#undef S
40}
41
42#define SLOTS 100
43
44struct bpf_map_def SEC("maps") lat_map = {
45 .type = BPF_MAP_TYPE_ARRAY,
46 .key_size = sizeof(u32),
47 .value_size = sizeof(u64),
48 .max_entries = SLOTS,
49};
50
51SEC("kprobe/blk_update_request")
52int bpf_prog2(struct pt_regs *ctx)
53{
54 long rq = ctx->di;
55 u64 *value, l, base;
56 u32 index;
57
58 value = bpf_map_lookup_elem(&my_map, &rq);
59 if (!value)
60 return 0;
61
62 u64 cur_time = bpf_ktime_get_ns();
63 u64 delta = cur_time - *value;
64
65 bpf_map_delete_elem(&my_map, &rq);
66
67 /* the lines below are computing index = log10(delta)*10
68 * using integer arithmetic
69 * index = 29 ~ 1 usec
70 * index = 59 ~ 1 msec
71 * index = 89 ~ 1 sec
72 * index = 99 ~ 10sec or more
73 * log10(x)*10 = log2(x)*10/log2(10) = log2(x)*3
74 */
75 l = log2l(delta);
76 base = 1ll << l;
77 index = (l * 64 + (delta - base) * 64 / base) * 3 / 64;
78
79 if (index >= SLOTS)
80 index = SLOTS - 1;
81
82 value = bpf_map_lookup_elem(&lat_map, &index);
83 if (value)
84 __sync_fetch_and_add((long *)value, 1);
85
86 return 0;
87}
88char _license[] SEC("license") = "GPL";
89u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/tracex3_user.c b/samples/bpf/tracex3_user.c
new file mode 100644
index 000000000000..0aaa933ab938
--- /dev/null
+++ b/samples/bpf/tracex3_user.c
@@ -0,0 +1,150 @@
1/* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <signal.h>
10#include <unistd.h>
11#include <stdbool.h>
12#include <string.h>
13#include <linux/bpf.h>
14#include "libbpf.h"
15#include "bpf_load.h"
16
17#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
18
19#define SLOTS 100
20
21static void clear_stats(int fd)
22{
23 __u32 key;
24 __u64 value = 0;
25
26 for (key = 0; key < SLOTS; key++)
27 bpf_update_elem(fd, &key, &value, BPF_ANY);
28}
29
30const char *color[] = {
31 "\033[48;5;255m",
32 "\033[48;5;252m",
33 "\033[48;5;250m",
34 "\033[48;5;248m",
35 "\033[48;5;246m",
36 "\033[48;5;244m",
37 "\033[48;5;242m",
38 "\033[48;5;240m",
39 "\033[48;5;238m",
40 "\033[48;5;236m",
41 "\033[48;5;234m",
42 "\033[48;5;232m",
43};
44const int num_colors = ARRAY_SIZE(color);
45
46const char nocolor[] = "\033[00m";
47
48const char *sym[] = {
49 " ",
50 " ",
51 ".",
52 ".",
53 "*",
54 "*",
55 "o",
56 "o",
57 "O",
58 "O",
59 "#",
60 "#",
61};
62
63bool full_range = false;
64bool text_only = false;
65
66static void print_banner(void)
67{
68 if (full_range)
69 printf("|1ns |10ns |100ns |1us |10us |100us"
70 " |1ms |10ms |100ms |1s |10s\n");
71 else
72 printf("|1us |10us |100us |1ms |10ms "
73 "|100ms |1s |10s\n");
74}
75
76static void print_hist(int fd)
77{
78 __u32 key;
79 __u64 value;
80 __u64 cnt[SLOTS];
81 __u64 max_cnt = 0;
82 __u64 total_events = 0;
83
84 for (key = 0; key < SLOTS; key++) {
85 value = 0;
86 bpf_lookup_elem(fd, &key, &value);
87 cnt[key] = value;
88 total_events += value;
89 if (value > max_cnt)
90 max_cnt = value;
91 }
92 clear_stats(fd);
93 for (key = full_range ? 0 : 29; key < SLOTS; key++) {
94 int c = num_colors * cnt[key] / (max_cnt + 1);
95
96 if (text_only)
97 printf("%s", sym[c]);
98 else
99 printf("%s %s", color[c], nocolor);
100 }
101 printf(" # %lld\n", total_events);
102}
103
104int main(int ac, char **argv)
105{
106 char filename[256];
107 int i;
108
109 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
110
111 if (load_bpf_file(filename)) {
112 printf("%s", bpf_log_buf);
113 return 1;
114 }
115
116 for (i = 1; i < ac; i++) {
117 if (strcmp(argv[i], "-a") == 0) {
118 full_range = true;
119 } else if (strcmp(argv[i], "-t") == 0) {
120 text_only = true;
121 } else if (strcmp(argv[i], "-h") == 0) {
122 printf("Usage:\n"
123 " -a display wider latency range\n"
124 " -t text only\n");
125 return 1;
126 }
127 }
128
129 printf(" heatmap of IO latency\n");
130 if (text_only)
131 printf(" %s", sym[num_colors - 1]);
132 else
133 printf(" %s %s", color[num_colors - 1], nocolor);
134 printf(" - many events with this latency\n");
135
136 if (text_only)
137 printf(" %s", sym[0]);
138 else
139 printf(" %s %s", color[0], nocolor);
140 printf(" - few events\n");
141
142 for (i = 0; ; i++) {
143 if (i % 20 == 0)
144 print_banner();
145 print_hist(map_fd[1]);
146 sleep(2);
147 }
148
149 return 0;
150}
diff --git a/samples/bpf/tracex4_kern.c b/samples/bpf/tracex4_kern.c
new file mode 100644
index 000000000000..126b80512228
--- /dev/null
+++ b/samples/bpf/tracex4_kern.c
@@ -0,0 +1,54 @@
1/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/ptrace.h>
8#include <linux/version.h>
9#include <uapi/linux/bpf.h>
10#include "bpf_helpers.h"
11
12struct pair {
13 u64 val;
14 u64 ip;
15};
16
17struct bpf_map_def SEC("maps") my_map = {
18 .type = BPF_MAP_TYPE_HASH,
19 .key_size = sizeof(long),
20 .value_size = sizeof(struct pair),
21 .max_entries = 1000000,
22};
23
24/* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
25 * example will no longer be meaningful
26 */
27SEC("kprobe/kmem_cache_free")
28int bpf_prog1(struct pt_regs *ctx)
29{
30 long ptr = ctx->si;
31
32 bpf_map_delete_elem(&my_map, &ptr);
33 return 0;
34}
35
36SEC("kretprobe/kmem_cache_alloc_node")
37int bpf_prog2(struct pt_regs *ctx)
38{
39 long ptr = ctx->ax;
40 long ip = 0;
41
42 /* get ip address of kmem_cache_alloc_node() caller */
43 bpf_probe_read(&ip, sizeof(ip), (void *)(ctx->bp + sizeof(ip)));
44
45 struct pair v = {
46 .val = bpf_ktime_get_ns(),
47 .ip = ip,
48 };
49
50 bpf_map_update_elem(&my_map, &ptr, &v, BPF_ANY);
51 return 0;
52}
53char _license[] SEC("license") = "GPL";
54u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/tracex4_user.c b/samples/bpf/tracex4_user.c
new file mode 100644
index 000000000000..bc4a3bdea6ed
--- /dev/null
+++ b/samples/bpf/tracex4_user.c
@@ -0,0 +1,69 @@
1/* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <signal.h>
10#include <unistd.h>
11#include <stdbool.h>
12#include <string.h>
13#include <time.h>
14#include <linux/bpf.h>
15#include "libbpf.h"
16#include "bpf_load.h"
17
18struct pair {
19 long long val;
20 __u64 ip;
21};
22
23static __u64 time_get_ns(void)
24{
25 struct timespec ts;
26
27 clock_gettime(CLOCK_MONOTONIC, &ts);
28 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
29}
30
31static void print_old_objects(int fd)
32{
33 long long val = time_get_ns();
34 __u64 key, next_key;
35 struct pair v;
36
37 key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
38
39 key = -1;
40 while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
41 bpf_lookup_elem(map_fd[0], &next_key, &v);
42 key = next_key;
43 if (val - v.val < 1000000000ll)
44 /* object was allocated more then 1 sec ago */
45 continue;
46 printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
47 next_key, (val - v.val) / 1000000000ll, v.ip);
48 }
49}
50
51int main(int ac, char **argv)
52{
53 char filename[256];
54 int i;
55
56 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
57
58 if (load_bpf_file(filename)) {
59 printf("%s", bpf_log_buf);
60 return 1;
61 }
62
63 for (i = 0; ; i++) {
64 print_old_objects(map_fd[1]);
65 sleep(1);
66 }
67
68 return 0;
69}
diff --git a/tools/build/Build.include b/tools/build/Build.include
new file mode 100644
index 000000000000..4c8daaccb82a
--- /dev/null
+++ b/tools/build/Build.include
@@ -0,0 +1,81 @@
1###
2# build: Generic definitions
3#
4# Lots of this code have been borrowed or heavily inspired from parts
5# of kbuild code, which is not credited, but mostly developed by:
6#
7# Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
8# Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
9#
10
11###
12# Convenient variables
13comma := ,
14squote := '
15
16###
17# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
18dot-target = $(dir $@).$(notdir $@)
19
20###
21# filename of target with directory and extension stripped
22basetarget = $(basename $(notdir $@))
23
24###
25# The temporary file to save gcc -MD generated dependencies must not
26# contain a comma
27depfile = $(subst $(comma),_,$(dot-target).d)
28
29###
30# Check if both arguments has same arguments. Result is empty string if equal.
31arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
32 $(filter-out $(cmd_$@), $(cmd_$(1))) )
33
34###
35# Escape single quote for use in echo statements
36escsq = $(subst $(squote),'\$(squote)',$1)
37
38# Echo command
39# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
40echo-cmd = $(if $($(quiet)cmd_$(1)),\
41 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
42
43###
44# Replace >$< with >$$< to preserve $ when reloading the .cmd file
45# (needed for make)
46# Replace >#< with >\#< to avoid starting a comment in the .cmd file
47# (needed for make)
48# Replace >'< with >'\''< to be able to enclose the whole string in '...'
49# (needed for the shell)
50make-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
51
52###
53# Find any prerequisites that is newer than target or that does not exist.
54# PHONY targets skipped in both cases.
55any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
56
57###
58# if_changed_dep - execute command if any prerequisite is newer than
59# target, or command line has changed and update
60# dependencies in the cmd file
61if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
62 @set -e; \
63 $(echo-cmd) $(cmd_$(1)); \
64 cat $(depfile) > $(dot-target).cmd; \
65 printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
66
67# if_changed - execute command if any prerequisite is newer than
68# target, or command line has changed
69if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
70 @set -e; \
71 $(echo-cmd) $(cmd_$(1)); \
72 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
73
74###
75# C flags to be used in rule definitions, includes:
76# - depfile generation
77# - global $(CFLAGS)
78# - per target C flags
79# - per object C flags
80# - BUILD_STR macro to allow '-D"$(variable)"' constructs
81c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
diff --git a/tools/build/Documentation/Build.txt b/tools/build/Documentation/Build.txt
new file mode 100644
index 000000000000..00ad2d608727
--- /dev/null
+++ b/tools/build/Documentation/Build.txt
@@ -0,0 +1,139 @@
1Build Framework
2===============
3
4The perf build framework was adopted from the kernel build system, hence the
5idea and the way how objects are built is the same.
6
7Basically the user provides set of 'Build' files that list objects and
8directories to nest for specific target to be build.
9
10Unlike the kernel we don't have a single build object 'obj-y' list that where
11we setup source objects, but we support more. This allows one 'Build' file to
12carry a sources list for multiple build objects.
13
14a) Build framework makefiles
15----------------------------
16
17The build framework consists of 2 Makefiles:
18
19 Build.include
20 Makefile.build
21
22While the 'Build.include' file contains just some generic definitions, the
23'Makefile.build' file is the makefile used from the outside. It's
24interface/usage is following:
25
26 $ make -f tools/build/Makefile srctree=$(KSRC) dir=$(DIR) obj=$(OBJECT)
27
28where:
29
30 KSRC - is the path to kernel sources
31 DIR - is the path to the project to be built
32 OBJECT - is the name of the build object
33
34When succefully finished the $(DIR) directory contains the final object file
35called $(OBJECT)-in.o:
36
37 $ ls $(DIR)/$(OBJECT)-in.o
38
39which includes all compiled sources described in 'Build' makefiles.
40
41a) Build makefiles
42------------------
43
44The user supplies 'Build' makefiles that contains a objects list, and connects
45the build to nested directories.
46
47Assume we have the following project structure:
48
49 ex/a.c
50 /b.c
51 /c.c
52 /d.c
53 /arch/e.c
54 /arch/f.c
55
56Out of which you build the 'ex' binary ' and the 'libex.a' library:
57
58 'ex' - consists of 'a.o', 'b.o' and libex.a
59 'libex.a' - consists of 'c.o', 'd.o', 'e.o' and 'f.o'
60
61The build framework does not create the 'ex' and 'libex.a' binaries for you, it
62only prepares proper objects to be compiled and grouped together.
63
64To follow the above example, the user provides following 'Build' files:
65
66 ex/Build:
67 ex-y += a.o
68 ex-y += b.o
69
70 libex-y += c.o
71 libex-y += d.o
72 libex-y += arch/
73
74 ex/arch/Build:
75 libex-y += e.o
76 libex-y += f.o
77
78and runs:
79
80 $ make -f tools/build/Makefile.build dir=. obj=ex
81 $ make -f tools/build/Makefile.build dir=. obj=libex
82
83which creates the following objects:
84
85 ex/ex-in.o
86 ex/libex-in.o
87
88that contain request objects names in Build files.
89
90It's only a matter of 2 single commands to create the final binaries:
91
92 $ ar rcs libex.a libex-in.o
93 $ gcc -o ex ex-in.o libex.a
94
95You can check the 'ex' example in 'tools/build/tests/ex' for more details.
96
97b) Rules
98--------
99
100The build framework provides standard compilation rules to handle .S and .c
101compilation.
102
103It's possible to include special rule if needed (like we do for flex or bison
104code generation).
105
106c) CFLAGS
107---------
108
109It's possible to alter the standard object C flags in the following way:
110
111 CFLAGS_perf.o += '...' - alters CFLAGS for perf.o object
112 CFLAGS_gtk += '...' - alters CFLAGS for gtk build object
113
114This C flags changes has the scope of the Build makefile they are defined in.
115
116
117d) Dependencies
118---------------
119
120For each built object file 'a.o' the '.a.cmd' is created and holds:
121
122 - Command line used to built that object
123 (for each object)
124
125 - Dependency rules generated by 'gcc -Wp,-MD,...'
126 (for compiled object)
127
128All existing '.cmd' files are included in the Build process to follow properly
129the dependencies and trigger a rebuild when necessary.
130
131
132e) Single rules
133---------------
134
135It's possible to build single object file by choice, like:
136
137 $ make util/map.o # objects
138 $ make util/map.i # preprocessor
139 $ make util/map.s # assembly
diff --git a/tools/build/Makefile.build b/tools/build/Makefile.build
new file mode 100644
index 000000000000..10df57237a66
--- /dev/null
+++ b/tools/build/Makefile.build
@@ -0,0 +1,130 @@
1###
2# Main build makefile.
3#
4# Lots of this code have been borrowed or heavily inspired from parts
5# of kbuild code, which is not credited, but mostly developed by:
6#
7# Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
8# Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
9#
10
11PHONY := __build
12__build:
13
14ifeq ($(V),1)
15 quiet =
16 Q =
17else
18 quiet=quiet_
19 Q=@
20endif
21
22build-dir := $(srctree)/tools/build
23
24# Generic definitions
25include $(build-dir)/Build.include
26
27# do not force detected configuration
28-include .config-detected
29
30# Init all relevant variables used in build files so
31# 1) they have correct type
32# 2) they do not inherit any value from the environment
33subdir-y :=
34obj-y :=
35subdir-y :=
36subdir-obj-y :=
37
38# Build definitions
39build-file := $(dir)/Build
40include $(build-file)
41
42quiet_cmd_flex = FLEX $@
43quiet_cmd_bison = BISON $@
44
45# Create directory unless it exists
46quiet_cmd_mkdir = MKDIR $(dir $@)
47 cmd_mkdir = mkdir -p $(dir $@)
48 rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
49
50# Compile command
51quiet_cmd_cc_o_c = CC $@
52 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
53
54quiet_cmd_cc_i_c = CPP $@
55 cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
56
57quiet_cmd_cc_s_c = AS $@
58 cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
59
60# Link agregate command
61# If there's nothing to link, create empty $@ object.
62quiet_cmd_ld_multi = LD $@
63 cmd_ld_multi = $(if $(strip $(obj-y)),\
64 $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@)
65
66# Build rules
67$(OUTPUT)%.o: %.c FORCE
68 $(call rule_mkdir)
69 $(call if_changed_dep,cc_o_c)
70
71$(OUTPUT)%.o: %.S FORCE
72 $(call rule_mkdir)
73 $(call if_changed_dep,cc_o_c)
74
75$(OUTPUT)%.i: %.c FORCE
76 $(call rule_mkdir)
77 $(call if_changed_dep,cc_i_c)
78
79$(OUTPUT)%.i: %.S FORCE
80 $(call rule_mkdir)
81 $(call if_changed_dep,cc_i_c)
82
83$(OUTPUT)%.s: %.c FORCE
84 $(call rule_mkdir)
85 $(call if_changed_dep,cc_s_c)
86
87# Gather build data:
88# obj-y - list of build objects
89# subdir-y - list of directories to nest
90# subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
91obj-y := $($(obj)-y)
92subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
93obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
94subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
95
96# '$(OUTPUT)/dir' prefix to all objects
97prefix := $(subst ./,,$(OUTPUT)$(dir)/)
98obj-y := $(addprefix $(prefix),$(obj-y))
99subdir-obj-y := $(addprefix $(prefix),$(subdir-obj-y))
100
101# Final '$(obj)-in.o' object
102in-target := $(prefix)$(obj)-in.o
103
104PHONY += $(subdir-y)
105
106$(subdir-y):
107 $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
108
109$(sort $(subdir-obj-y)): $(subdir-y) ;
110
111$(in-target): $(obj-y) FORCE
112 $(call rule_mkdir)
113 $(call if_changed,ld_multi)
114
115__build: $(in-target)
116 @:
117
118PHONY += FORCE
119FORCE:
120
121# Include all cmd files to get all the dependency rules
122# for all objects included
123targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
124cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
125
126ifneq ($(cmd_files),)
127 include $(cmd_files)
128endif
129
130.PHONY: $(PHONY)
diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature
new file mode 100644
index 000000000000..3a0b0ca2a28c
--- /dev/null
+++ b/tools/build/Makefile.feature
@@ -0,0 +1,171 @@
1feature_dir := $(srctree)/tools/build/feature
2
3ifneq ($(OUTPUT),)
4 OUTPUT_FEATURES = $(OUTPUT)feature/
5 $(shell mkdir -p $(OUTPUT_FEATURES))
6endif
7
8feature_check = $(eval $(feature_check_code))
9define feature_check_code
10 feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
11endef
12
13feature_set = $(eval $(feature_set_code))
14define feature_set_code
15 feature-$(1) := 1
16endef
17
18#
19# Build the feature check binaries in parallel, ignore errors, ignore return value and suppress output:
20#
21
22#
23# Note that this is not a complete list of all feature tests, just
24# those that are typically built on a fully configured system.
25#
26# [ Feature tests not mentioned here have to be built explicitly in
27# the rule that uses them - an example for that is the 'bionic'
28# feature check. ]
29#
30FEATURE_TESTS = \
31 backtrace \
32 dwarf \
33 fortify-source \
34 sync-compare-and-swap \
35 glibc \
36 gtk2 \
37 gtk2-infobar \
38 libaudit \
39 libbfd \
40 libelf \
41 libelf-getphdrnum \
42 libelf-mmap \
43 libnuma \
44 libperl \
45 libpython \
46 libpython-version \
47 libslang \
48 libunwind \
49 pthread-attr-setaffinity-np \
50 stackprotector-all \
51 timerfd \
52 libdw-dwarf-unwind \
53 zlib \
54 lzma
55
56FEATURE_DISPLAY = \
57 dwarf \
58 glibc \
59 gtk2 \
60 libaudit \
61 libbfd \
62 libelf \
63 libnuma \
64 libperl \
65 libpython \
66 libslang \
67 libunwind \
68 libdw-dwarf-unwind \
69 zlib \
70 lzma
71
72# Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features.
73# If in the future we need per-feature checks/flags for features not
74# mentioned in this list we need to refactor this ;-).
75set_test_all_flags = $(eval $(set_test_all_flags_code))
76define set_test_all_flags_code
77 FEATURE_CHECK_CFLAGS-all += $(FEATURE_CHECK_CFLAGS-$(1))
78 FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1))
79endef
80
81$(foreach feat,$(FEATURE_TESTS),$(call set_test_all_flags,$(feat)))
82
83#
84# Special fast-path for the 'all features are available' case:
85#
86$(call feature_check,all,$(MSG))
87
88#
89# Just in case the build freshly failed, make sure we print the
90# feature matrix:
91#
92ifeq ($(feature-all), 1)
93 #
94 # test-all.c passed - just set all the core feature flags to 1:
95 #
96 $(foreach feat,$(FEATURE_TESTS),$(call feature_set,$(feat)))
97else
98 $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CFLAGS="$(EXTRA_CFLAGS)" LDFLAGS=$(LDFLAGS) -i -j -C $(feature_dir) $(addsuffix .bin,$(FEATURE_TESTS)) >/dev/null 2>&1)
99 $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat)))
100endif
101
102#
103# Print the result of the feature test:
104#
105feature_print_status = $(eval $(feature_print_status_code)) $(info $(MSG))
106
107define feature_print_status_code
108 ifeq ($(feature-$(1)), 1)
109 MSG = $(shell printf '...%30s: [ \033[32mon\033[m ]' $(1))
110 else
111 MSG = $(shell printf '...%30s: [ \033[31mOFF\033[m ]' $(1))
112 endif
113endef
114
115feature_print_text = $(eval $(feature_print_text_code)) $(info $(MSG))
116define feature_print_text_code
117 MSG = $(shell printf '...%30s: %s' $(1) $(2))
118endef
119
120FEATURE_DUMP := $(foreach feat,$(FEATURE_DISPLAY),feature-$(feat)($(feature-$(feat))))
121FEATURE_DUMP_FILE := $(shell touch $(OUTPUT)FEATURE-DUMP; cat $(OUTPUT)FEATURE-DUMP)
122
123ifeq ($(dwarf-post-unwind),1)
124 FEATURE_DUMP += dwarf-post-unwind($(dwarf-post-unwind-text))
125endif
126
127# The $(feature_display) controls the default detection message
128# output. It's set if:
129# - detected features differes from stored features from
130# last build (in FEATURE-DUMP file)
131# - one of the $(FEATURE_DISPLAY) is not detected
132# - VF is enabled
133
134ifneq ("$(FEATURE_DUMP)","$(FEATURE_DUMP_FILE)")
135 $(shell echo "$(FEATURE_DUMP)" > $(OUTPUT)FEATURE-DUMP)
136 feature_display := 1
137endif
138
139feature_display_check = $(eval $(feature_check_code))
140define feature_display_check_code
141 ifneq ($(feature-$(1)), 1)
142 feature_display := 1
143 endif
144endef
145
146$(foreach feat,$(FEATURE_DISPLAY),$(call feature_display_check,$(feat)))
147
148ifeq ($(VF),1)
149 feature_display := 1
150 feature_verbose := 1
151endif
152
153ifeq ($(feature_display),1)
154 $(info )
155 $(info Auto-detecting system features:)
156 $(foreach feat,$(FEATURE_DISPLAY),$(call feature_print_status,$(feat),))
157
158 ifeq ($(dwarf-post-unwind),1)
159 $(call feature_print_text,"DWARF post unwind library", $(dwarf-post-unwind-text))
160 endif
161
162 ifneq ($(feature_verbose),1)
163 $(info )
164 endif
165endif
166
167ifeq ($(feature_verbose),1)
168 TMP := $(filter-out $(FEATURE_DISPLAY),$(FEATURE_TESTS))
169 $(foreach feat,$(TMP),$(call feature_print_status,$(feat),))
170 $(info )
171endif
diff --git a/tools/perf/config/feature-checks/.gitignore b/tools/build/feature/.gitignore
index 80f3da0c3515..09b335b98842 100644
--- a/tools/perf/config/feature-checks/.gitignore
+++ b/tools/build/feature/.gitignore
@@ -1,2 +1,3 @@
1*.d 1*.d
2*.bin 2*.bin
3*.output
diff --git a/tools/perf/config/feature-checks/Makefile b/tools/build/feature/Makefile
index b32ff3372514..463ed8f2a267 100644
--- a/tools/perf/config/feature-checks/Makefile
+++ b/tools/build/feature/Makefile
@@ -29,33 +29,36 @@ FILES= \
29 test-stackprotector-all.bin \ 29 test-stackprotector-all.bin \
30 test-timerfd.bin \ 30 test-timerfd.bin \
31 test-libdw-dwarf-unwind.bin \ 31 test-libdw-dwarf-unwind.bin \
32 test-libbabeltrace.bin \
32 test-compile-32.bin \ 33 test-compile-32.bin \
33 test-compile-x32.bin \ 34 test-compile-x32.bin \
34 test-zlib.bin 35 test-zlib.bin \
36 test-lzma.bin
35 37
36CC := $(CROSS_COMPILE)gcc -MD 38CC := $(CROSS_COMPILE)gcc -MD
37PKG_CONFIG := $(CROSS_COMPILE)pkg-config 39PKG_CONFIG := $(CROSS_COMPILE)pkg-config
38 40
39all: $(FILES) 41all: $(FILES)
40 42
41BUILD = $(CC) $(CFLAGS) -o $(OUTPUT)$@ $(patsubst %.bin,%.c,$@) $(LDFLAGS) 43__BUILD = $(CC) $(CFLAGS) -Wall -Werror -o $(OUTPUT)$@ $(patsubst %.bin,%.c,$@) $(LDFLAGS)
44 BUILD = $(__BUILD) > $(OUTPUT)$(@:.bin=.make.output) 2>&1
42 45
43############################### 46###############################
44 47
45test-all.bin: 48test-all.bin:
46 $(BUILD) -Werror -fstack-protector-all -O2 -Werror -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -laudit -I/usr/include/slang -lslang $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -DPACKAGE='"perf"' -lbfd -ldl -lz 49 $(BUILD) -fstack-protector-all -O2 -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -laudit -I/usr/include/slang -lslang $(shell $(PKG_CONFIG) --libs --cflags gtk+-2.0 2>/dev/null) $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -DPACKAGE='"perf"' -lbfd -ldl -lz -llzma
47 50
48test-hello.bin: 51test-hello.bin:
49 $(BUILD) 52 $(BUILD)
50 53
51test-pthread-attr-setaffinity-np.bin: 54test-pthread-attr-setaffinity-np.bin:
52 $(BUILD) -D_GNU_SOURCE -Werror -lpthread 55 $(BUILD) -D_GNU_SOURCE -lpthread
53 56
54test-stackprotector-all.bin: 57test-stackprotector-all.bin:
55 $(BUILD) -Werror -fstack-protector-all 58 $(BUILD) -fstack-protector-all
56 59
57test-fortify-source.bin: 60test-fortify-source.bin:
58 $(BUILD) -O2 -Werror -D_FORTIFY_SOURCE=2 61 $(BUILD) -O2 -D_FORTIFY_SOURCE=2
59 62
60test-bionic.bin: 63test-bionic.bin:
61 $(BUILD) 64 $(BUILD)
@@ -118,10 +121,10 @@ test-libbfd.bin:
118 $(BUILD) -DPACKAGE='"perf"' -lbfd -lz -liberty -ldl 121 $(BUILD) -DPACKAGE='"perf"' -lbfd -lz -liberty -ldl
119 122
120test-liberty.bin: 123test-liberty.bin:
121 $(CC) -o $(OUTPUT)$@ test-libbfd.c -DPACKAGE='"perf"' -lbfd -ldl -liberty 124 $(CC) -Wall -Werror -o $(OUTPUT)$@ test-libbfd.c -DPACKAGE='"perf"' -lbfd -ldl -liberty
122 125
123test-liberty-z.bin: 126test-liberty-z.bin:
124 $(CC) -o $(OUTPUT)$@ test-libbfd.c -DPACKAGE='"perf"' -lbfd -ldl -liberty -lz 127 $(CC) -Wall -Werror -o $(OUTPUT)$@ test-libbfd.c -DPACKAGE='"perf"' -lbfd -ldl -liberty -lz
125 128
126test-cplus-demangle.bin: 129test-cplus-demangle.bin:
127 $(BUILD) -liberty 130 $(BUILD) -liberty
@@ -133,10 +136,13 @@ test-timerfd.bin:
133 $(BUILD) 136 $(BUILD)
134 137
135test-libdw-dwarf-unwind.bin: 138test-libdw-dwarf-unwind.bin:
136 $(BUILD) 139 $(BUILD) # -ldw provided by $(FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind)
140
141test-libbabeltrace.bin:
142 $(BUILD) # -lbabeltrace provided by $(FEATURE_CHECK_LDFLAGS-libbabeltrace)
137 143
138test-sync-compare-and-swap.bin: 144test-sync-compare-and-swap.bin:
139 $(BUILD) -Werror 145 $(BUILD)
140 146
141test-compile-32.bin: 147test-compile-32.bin:
142 $(CC) -m32 -o $(OUTPUT)$@ test-compile.c 148 $(CC) -m32 -o $(OUTPUT)$@ test-compile.c
@@ -147,9 +153,12 @@ test-compile-x32.bin:
147test-zlib.bin: 153test-zlib.bin:
148 $(BUILD) -lz 154 $(BUILD) -lz
149 155
156test-lzma.bin:
157 $(BUILD) -llzma
158
150-include *.d 159-include *.d
151 160
152############################### 161###############################
153 162
154clean: 163clean:
155 rm -f $(FILES) *.d 164 rm -f $(FILES) *.d $(FILES:.bin=.make.output)
diff --git a/tools/perf/config/feature-checks/test-all.c b/tools/build/feature/test-all.c
index 6d4d09323922..84689a67814a 100644
--- a/tools/perf/config/feature-checks/test-all.c
+++ b/tools/build/feature/test-all.c
@@ -98,7 +98,23 @@
98#undef main 98#undef main
99 99
100#define main main_test_pthread_attr_setaffinity_np 100#define main main_test_pthread_attr_setaffinity_np
101# include "test-pthread_attr_setaffinity_np.c" 101# include "test-pthread-attr-setaffinity-np.c"
102#undef main
103
104# if 0
105/*
106 * Disable libbabeltrace check for test-all, because the requested
107 * library version is not released yet in most distributions. Will
108 * reenable later.
109 */
110
111#define main main_test_libbabeltrace
112# include "test-libbabeltrace.c"
113#undef main
114#endif
115
116#define main main_test_lzma
117# include "test-lzma.c"
102#undef main 118#undef main
103 119
104int main(int argc, char *argv[]) 120int main(int argc, char *argv[])
@@ -126,6 +142,7 @@ int main(int argc, char *argv[])
126 main_test_sync_compare_and_swap(argc, argv); 142 main_test_sync_compare_and_swap(argc, argv);
127 main_test_zlib(); 143 main_test_zlib();
128 main_test_pthread_attr_setaffinity_np(); 144 main_test_pthread_attr_setaffinity_np();
145 main_test_lzma();
129 146
130 return 0; 147 return 0;
131} 148}
diff --git a/tools/perf/config/feature-checks/test-backtrace.c b/tools/build/feature/test-backtrace.c
index 7124aa1dc8fb..7124aa1dc8fb 100644
--- a/tools/perf/config/feature-checks/test-backtrace.c
+++ b/tools/build/feature/test-backtrace.c
diff --git a/tools/perf/config/feature-checks/test-bionic.c b/tools/build/feature/test-bionic.c
index eac24e9513eb..eac24e9513eb 100644
--- a/tools/perf/config/feature-checks/test-bionic.c
+++ b/tools/build/feature/test-bionic.c
diff --git a/tools/perf/config/feature-checks/test-compile.c b/tools/build/feature/test-compile.c
index 31dbf45bf99c..31dbf45bf99c 100644
--- a/tools/perf/config/feature-checks/test-compile.c
+++ b/tools/build/feature/test-compile.c
diff --git a/tools/perf/config/feature-checks/test-cplus-demangle.c b/tools/build/feature/test-cplus-demangle.c
index 610c686e0009..610c686e0009 100644
--- a/tools/perf/config/feature-checks/test-cplus-demangle.c
+++ b/tools/build/feature/test-cplus-demangle.c
diff --git a/tools/perf/config/feature-checks/test-dwarf.c b/tools/build/feature/test-dwarf.c
index 3fc1801ce4a9..3fc1801ce4a9 100644
--- a/tools/perf/config/feature-checks/test-dwarf.c
+++ b/tools/build/feature/test-dwarf.c
diff --git a/tools/perf/config/feature-checks/test-fortify-source.c b/tools/build/feature/test-fortify-source.c
index c9f398d87868..c9f398d87868 100644
--- a/tools/perf/config/feature-checks/test-fortify-source.c
+++ b/tools/build/feature/test-fortify-source.c
diff --git a/tools/perf/config/feature-checks/test-glibc.c b/tools/build/feature/test-glibc.c
index b0820345cd98..b0820345cd98 100644
--- a/tools/perf/config/feature-checks/test-glibc.c
+++ b/tools/build/feature/test-glibc.c
diff --git a/tools/perf/config/feature-checks/test-gtk2-infobar.c b/tools/build/feature/test-gtk2-infobar.c
index 397b4646d066..397b4646d066 100644
--- a/tools/perf/config/feature-checks/test-gtk2-infobar.c
+++ b/tools/build/feature/test-gtk2-infobar.c
diff --git a/tools/perf/config/feature-checks/test-gtk2.c b/tools/build/feature/test-gtk2.c
index 6bd80e509439..6bd80e509439 100644
--- a/tools/perf/config/feature-checks/test-gtk2.c
+++ b/tools/build/feature/test-gtk2.c
diff --git a/tools/perf/config/feature-checks/test-hello.c b/tools/build/feature/test-hello.c
index c9f398d87868..c9f398d87868 100644
--- a/tools/perf/config/feature-checks/test-hello.c
+++ b/tools/build/feature/test-hello.c
diff --git a/tools/perf/config/feature-checks/test-libaudit.c b/tools/build/feature/test-libaudit.c
index afc019f08641..afc019f08641 100644
--- a/tools/perf/config/feature-checks/test-libaudit.c
+++ b/tools/build/feature/test-libaudit.c
diff --git a/tools/build/feature/test-libbabeltrace.c b/tools/build/feature/test-libbabeltrace.c
new file mode 100644
index 000000000000..9cf802a04885
--- /dev/null
+++ b/tools/build/feature/test-libbabeltrace.c
@@ -0,0 +1,9 @@
1
2#include <babeltrace/ctf-writer/writer.h>
3#include <babeltrace/ctf-ir/stream-class.h>
4
5int main(void)
6{
7 bt_ctf_stream_class_get_packet_context_type((void *) 0);
8 return 0;
9}
diff --git a/tools/perf/config/feature-checks/test-libbfd.c b/tools/build/feature/test-libbfd.c
index 24059907e990..24059907e990 100644
--- a/tools/perf/config/feature-checks/test-libbfd.c
+++ b/tools/build/feature/test-libbfd.c
diff --git a/tools/perf/config/feature-checks/test-libdw-dwarf-unwind.c b/tools/build/feature/test-libdw-dwarf-unwind.c
index f676a3ff442a..f676a3ff442a 100644
--- a/tools/perf/config/feature-checks/test-libdw-dwarf-unwind.c
+++ b/tools/build/feature/test-libdw-dwarf-unwind.c
diff --git a/tools/perf/config/feature-checks/test-libelf-getphdrnum.c b/tools/build/feature/test-libelf-getphdrnum.c
index d710459306c3..d710459306c3 100644
--- a/tools/perf/config/feature-checks/test-libelf-getphdrnum.c
+++ b/tools/build/feature/test-libelf-getphdrnum.c
diff --git a/tools/perf/config/feature-checks/test-libelf-mmap.c b/tools/build/feature/test-libelf-mmap.c
index 564427d7ef18..564427d7ef18 100644
--- a/tools/perf/config/feature-checks/test-libelf-mmap.c
+++ b/tools/build/feature/test-libelf-mmap.c
diff --git a/tools/perf/config/feature-checks/test-libelf.c b/tools/build/feature/test-libelf.c
index 08db322d8957..08db322d8957 100644
--- a/tools/perf/config/feature-checks/test-libelf.c
+++ b/tools/build/feature/test-libelf.c
diff --git a/tools/perf/config/feature-checks/test-libnuma.c b/tools/build/feature/test-libnuma.c
index 4763d9cd587d..4763d9cd587d 100644
--- a/tools/perf/config/feature-checks/test-libnuma.c
+++ b/tools/build/feature/test-libnuma.c
diff --git a/tools/perf/config/feature-checks/test-libperl.c b/tools/build/feature/test-libperl.c
index 8871f6a0fdb4..8871f6a0fdb4 100644
--- a/tools/perf/config/feature-checks/test-libperl.c
+++ b/tools/build/feature/test-libperl.c
diff --git a/tools/perf/config/feature-checks/test-libpython-version.c b/tools/build/feature/test-libpython-version.c
index facea122d812..facea122d812 100644
--- a/tools/perf/config/feature-checks/test-libpython-version.c
+++ b/tools/build/feature/test-libpython-version.c
diff --git a/tools/perf/config/feature-checks/test-libpython.c b/tools/build/feature/test-libpython.c
index b24b28ad6324..b24b28ad6324 100644
--- a/tools/perf/config/feature-checks/test-libpython.c
+++ b/tools/build/feature/test-libpython.c
diff --git a/tools/perf/config/feature-checks/test-libslang.c b/tools/build/feature/test-libslang.c
index 22ff22ed94d1..22ff22ed94d1 100644
--- a/tools/perf/config/feature-checks/test-libslang.c
+++ b/tools/build/feature/test-libslang.c
diff --git a/tools/perf/config/feature-checks/test-libunwind-debug-frame.c b/tools/build/feature/test-libunwind-debug-frame.c
index 0ef8087a104a..0ef8087a104a 100644
--- a/tools/perf/config/feature-checks/test-libunwind-debug-frame.c
+++ b/tools/build/feature/test-libunwind-debug-frame.c
diff --git a/tools/perf/config/feature-checks/test-libunwind.c b/tools/build/feature/test-libunwind.c
index 43b9369bcab7..43b9369bcab7 100644
--- a/tools/perf/config/feature-checks/test-libunwind.c
+++ b/tools/build/feature/test-libunwind.c
diff --git a/tools/build/feature/test-lzma.c b/tools/build/feature/test-lzma.c
new file mode 100644
index 000000000000..95adc8ced3dd
--- /dev/null
+++ b/tools/build/feature/test-lzma.c
@@ -0,0 +1,10 @@
1#include <lzma.h>
2
3int main(void)
4{
5 lzma_stream strm = LZMA_STREAM_INIT;
6 int ret;
7
8 ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
9 return ret ? -1 : 0;
10}
diff --git a/tools/perf/config/feature-checks/test-pthread-attr-setaffinity-np.c b/tools/build/feature/test-pthread-attr-setaffinity-np.c
index 2b81b72eca23..fdada5e8d454 100644
--- a/tools/perf/config/feature-checks/test-pthread-attr-setaffinity-np.c
+++ b/tools/build/feature/test-pthread-attr-setaffinity-np.c
@@ -1,5 +1,6 @@
1#include <stdint.h> 1#include <stdint.h>
2#include <pthread.h> 2#include <pthread.h>
3#include <sched.h>
3 4
4int main(void) 5int main(void)
5{ 6{
@@ -8,7 +9,8 @@ int main(void)
8 cpu_set_t cs; 9 cpu_set_t cs;
9 10
10 pthread_attr_init(&thread_attr); 11 pthread_attr_init(&thread_attr);
11 /* don't care abt exact args, just the API itself in libpthread */ 12 CPU_ZERO(&cs);
13
12 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cs), &cs); 14 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cs), &cs);
13 15
14 return ret; 16 return ret;
diff --git a/tools/perf/config/feature-checks/test-stackprotector-all.c b/tools/build/feature/test-stackprotector-all.c
index c9f398d87868..c9f398d87868 100644
--- a/tools/perf/config/feature-checks/test-stackprotector-all.c
+++ b/tools/build/feature/test-stackprotector-all.c
diff --git a/tools/perf/config/feature-checks/test-sync-compare-and-swap.c b/tools/build/feature/test-sync-compare-and-swap.c
index c34d4ca4af56..c34d4ca4af56 100644
--- a/tools/perf/config/feature-checks/test-sync-compare-and-swap.c
+++ b/tools/build/feature/test-sync-compare-and-swap.c
diff --git a/tools/perf/config/feature-checks/test-timerfd.c b/tools/build/feature/test-timerfd.c
index 8c5c083b4d3c..8c5c083b4d3c 100644
--- a/tools/perf/config/feature-checks/test-timerfd.c
+++ b/tools/build/feature/test-timerfd.c
diff --git a/tools/perf/config/feature-checks/test-zlib.c b/tools/build/feature/test-zlib.c
index e111fff6240e..e111fff6240e 100644
--- a/tools/perf/config/feature-checks/test-zlib.c
+++ b/tools/build/feature/test-zlib.c
diff --git a/tools/build/tests/ex/Build b/tools/build/tests/ex/Build
new file mode 100644
index 000000000000..0e6c3e6767e6
--- /dev/null
+++ b/tools/build/tests/ex/Build
@@ -0,0 +1,8 @@
1ex-y += ex.o
2ex-y += a.o
3ex-y += b.o
4ex-y += empty/
5
6libex-y += c.o
7libex-y += d.o
8libex-y += arch/
diff --git a/tools/build/tests/ex/Makefile b/tools/build/tests/ex/Makefile
new file mode 100644
index 000000000000..52d2476073a3
--- /dev/null
+++ b/tools/build/tests/ex/Makefile
@@ -0,0 +1,23 @@
1export srctree := ../../../..
2export CC := gcc
3export LD := ld
4export AR := ar
5
6build := -f $(srctree)/tools/build/Makefile.build dir=. obj
7ex: ex-in.o libex-in.o
8 gcc -o $@ $^
9
10ex.%: FORCE
11 make -f $(srctree)/tools/build/Makefile.build dir=. $@
12
13ex-in.o: FORCE
14 make $(build)=ex
15
16libex-in.o: FORCE
17 make $(build)=libex
18
19clean:
20 find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
21 rm -f ex ex.i ex.s
22
23.PHONY: FORCE
diff --git a/tools/build/tests/ex/a.c b/tools/build/tests/ex/a.c
new file mode 100644
index 000000000000..851762798c83
--- /dev/null
+++ b/tools/build/tests/ex/a.c
@@ -0,0 +1,5 @@
1
2int a(void)
3{
4 return 0;
5}
diff --git a/tools/build/tests/ex/arch/Build b/tools/build/tests/ex/arch/Build
new file mode 100644
index 000000000000..55506189efae
--- /dev/null
+++ b/tools/build/tests/ex/arch/Build
@@ -0,0 +1,2 @@
1libex-y += e.o
2libex-y += f.o
diff --git a/tools/build/tests/ex/arch/e.c b/tools/build/tests/ex/arch/e.c
new file mode 100644
index 000000000000..beaa4a1d7ba8
--- /dev/null
+++ b/tools/build/tests/ex/arch/e.c
@@ -0,0 +1,5 @@
1
2int e(void)
3{
4 return 0;
5}
diff --git a/tools/build/tests/ex/arch/f.c b/tools/build/tests/ex/arch/f.c
new file mode 100644
index 000000000000..7c3e9e9da5b7
--- /dev/null
+++ b/tools/build/tests/ex/arch/f.c
@@ -0,0 +1,5 @@
1
2int f(void)
3{
4 return 0;
5}
diff --git a/tools/build/tests/ex/b.c b/tools/build/tests/ex/b.c
new file mode 100644
index 000000000000..c24ff9ca9a97
--- /dev/null
+++ b/tools/build/tests/ex/b.c
@@ -0,0 +1,5 @@
1
2int b(void)
3{
4 return 0;
5}
diff --git a/tools/build/tests/ex/c.c b/tools/build/tests/ex/c.c
new file mode 100644
index 000000000000..e216d0217499
--- /dev/null
+++ b/tools/build/tests/ex/c.c
@@ -0,0 +1,5 @@
1
2int c(void)
3{
4 return 0;
5}
diff --git a/tools/build/tests/ex/d.c b/tools/build/tests/ex/d.c
new file mode 100644
index 000000000000..80dc0f06151b
--- /dev/null
+++ b/tools/build/tests/ex/d.c
@@ -0,0 +1,5 @@
1
2int d(void)
3{
4 return 0;
5}
diff --git a/tools/build/tests/ex/empty/Build b/tools/build/tests/ex/empty/Build
new file mode 100644
index 000000000000..e69de29bb2d1
--- /dev/null
+++ b/tools/build/tests/ex/empty/Build
diff --git a/tools/build/tests/ex/ex.c b/tools/build/tests/ex/ex.c
new file mode 100644
index 000000000000..dc42eb2e1a67
--- /dev/null
+++ b/tools/build/tests/ex/ex.c
@@ -0,0 +1,19 @@
1
2int a(void);
3int b(void);
4int c(void);
5int d(void);
6int e(void);
7int f(void);
8
9int main(void)
10{
11 a();
12 b();
13 c();
14 d();
15 e();
16 f();
17
18 return 0;
19}
diff --git a/tools/build/tests/run.sh b/tools/build/tests/run.sh
new file mode 100755
index 000000000000..5494f8ea7567
--- /dev/null
+++ b/tools/build/tests/run.sh
@@ -0,0 +1,42 @@
1#!/bin/sh
2
3function test_ex {
4 make -C ex V=1 clean > ex.out 2>&1
5 make -C ex V=1 >> ex.out 2>&1
6
7 if [ ! -x ./ex/ex ]; then
8 echo FAILED
9 exit -1
10 fi
11
12 make -C ex V=1 clean > /dev/null 2>&1
13 rm -f ex.out
14}
15
16function test_ex_suffix {
17 make -C ex V=1 clean > ex.out 2>&1
18
19 # use -rR to disable make's builtin rules
20 make -rR -C ex V=1 ex.o >> ex.out 2>&1
21 make -rR -C ex V=1 ex.i >> ex.out 2>&1
22 make -rR -C ex V=1 ex.s >> ex.out 2>&1
23
24 if [ -x ./ex/ex ]; then
25 echo FAILED
26 exit -1
27 fi
28
29 if [ ! -f ./ex/ex.o -o ! -f ./ex/ex.i -o ! -f ./ex/ex.s ]; then
30 echo FAILED
31 exit -1
32 fi
33
34 make -C ex V=1 clean > /dev/null 2>&1
35 rm -f ex.out
36}
37echo -n Testing..
38
39test_ex
40test_ex_suffix
41
42echo OK
diff --git a/tools/lib/api/Build b/tools/lib/api/Build
new file mode 100644
index 000000000000..3653965cf481
--- /dev/null
+++ b/tools/lib/api/Build
@@ -0,0 +1,2 @@
1libapi-y += fd/
2libapi-y += fs/
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile
index 36c08b1f4afb..d8fe29fc19a4 100644
--- a/tools/lib/api/Makefile
+++ b/tools/lib/api/Makefile
@@ -1,49 +1,43 @@
1include ../../scripts/Makefile.include 1include ../../scripts/Makefile.include
2include ../../perf/config/utilities.mak # QUIET_CLEAN 2include ../../perf/config/utilities.mak # QUIET_CLEAN
3 3
4ifeq ($(srctree),)
5srctree := $(patsubst %/,%,$(dir $(shell pwd)))
6srctree := $(patsubst %/,%,$(dir $(srctree)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8#$(info Determined 'srctree' to be $(srctree))
9endif
10
4CC = $(CROSS_COMPILE)gcc 11CC = $(CROSS_COMPILE)gcc
5AR = $(CROSS_COMPILE)ar 12AR = $(CROSS_COMPILE)ar
6 13
7# guard against environment variables 14MAKEFLAGS += --no-print-directory
8LIB_H=
9LIB_OBJS=
10
11LIB_H += fs/debugfs.h
12LIB_H += fs/fs.h
13# See comment below about piggybacking...
14LIB_H += fd/array.h
15
16LIB_OBJS += $(OUTPUT)fs/debugfs.o
17LIB_OBJS += $(OUTPUT)fs/fs.o
18# XXX piggybacking here, need to introduce libapikfd, or rename this
19# to plain libapik.a and make it have it all api goodies
20LIB_OBJS += $(OUTPUT)fd/array.o
21 15
22LIBFILE = libapikfs.a 16LIBFILE = $(OUTPUT)libapi.a
23 17
24CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) -fPIC 18CFLAGS := $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
25EXTLIBS = -lelf -lpthread -lrt -lm 19CFLAGS += -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 -fPIC
26ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 20CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
27ALL_LDFLAGS = $(LDFLAGS)
28 21
29RM = rm -f 22RM = rm -f
30 23
31$(LIBFILE): $(LIB_OBJS) 24build := -f $(srctree)/tools/build/Makefile.build dir=. obj
32 $(QUIET_AR)$(RM) $@ && $(AR) rcs $(OUTPUT)$@ $(LIB_OBJS) 25API_IN := $(OUTPUT)libapi-in.o
33 26
34$(LIB_OBJS): $(LIB_H) 27export srctree OUTPUT CC LD CFLAGS V
35 28
36libapi_dirs: 29all: $(LIBFILE)
37 $(QUIET_MKDIR)mkdir -p $(OUTPUT)fd $(OUTPUT)fs
38 30
39$(OUTPUT)%.o: %.c libapi_dirs 31$(API_IN): FORCE
40 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< 32 @$(MAKE) $(build)=libapi
41$(OUTPUT)%.s: %.c libapi_dirs 33
42 $(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $< 34$(LIBFILE): $(API_IN)
43$(OUTPUT)%.o: %.S libapi_dirs 35 $(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(API_IN)
44 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<
45 36
46clean: 37clean:
47 $(call QUIET_CLEAN, libapi) $(RM) $(LIB_OBJS) $(LIBFILE) 38 $(call QUIET_CLEAN, libapi) $(RM) $(LIBFILE); \
39 find $(if $(OUTPUT),$(OUTPUT),.) -name \*.o | xargs $(RM)
40
41FORCE:
48 42
49.PHONY: clean 43.PHONY: clean FORCE
diff --git a/tools/lib/api/fd/Build b/tools/lib/api/fd/Build
new file mode 100644
index 000000000000..605d99f6d71a
--- /dev/null
+++ b/tools/lib/api/fd/Build
@@ -0,0 +1 @@
libapi-y += array.o
diff --git a/tools/lib/api/fs/Build b/tools/lib/api/fs/Build
new file mode 100644
index 000000000000..6de5a4f0b501
--- /dev/null
+++ b/tools/lib/api/fs/Build
@@ -0,0 +1,4 @@
1libapi-y += fs.o
2libapi-y += debugfs.o
3libapi-y += findfs.o
4libapi-y += tracefs.o
diff --git a/tools/lib/api/fs/debugfs.c b/tools/lib/api/fs/debugfs.c
index d2b18e887071..8305b3e9d48e 100644
--- a/tools/lib/api/fs/debugfs.c
+++ b/tools/lib/api/fs/debugfs.c
@@ -3,75 +3,50 @@
3#include <stdio.h> 3#include <stdio.h>
4#include <stdlib.h> 4#include <stdlib.h>
5#include <string.h> 5#include <string.h>
6#include <unistd.h>
6#include <stdbool.h> 7#include <stdbool.h>
7#include <sys/vfs.h> 8#include <sys/vfs.h>
9#include <sys/types.h>
10#include <sys/stat.h>
8#include <sys/mount.h> 11#include <sys/mount.h>
9#include <linux/kernel.h> 12#include <linux/kernel.h>
10 13
11#include "debugfs.h" 14#include "debugfs.h"
12 15
13char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; 16#ifndef DEBUGFS_DEFAULT_PATH
17#define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
18#endif
19
20char debugfs_mountpoint[PATH_MAX + 1] = DEBUGFS_DEFAULT_PATH;
14 21
15static const char * const debugfs_known_mountpoints[] = { 22static const char * const debugfs_known_mountpoints[] = {
16 "/sys/kernel/debug", 23 DEBUGFS_DEFAULT_PATH,
17 "/debug", 24 "/debug",
18 0, 25 0,
19}; 26};
20 27
21static bool debugfs_found; 28static bool debugfs_found;
22 29
30bool debugfs_configured(void)
31{
32 return debugfs_find_mountpoint() != NULL;
33}
34
23/* find the path to the mounted debugfs */ 35/* find the path to the mounted debugfs */
24const char *debugfs_find_mountpoint(void) 36const char *debugfs_find_mountpoint(void)
25{ 37{
26 const char * const *ptr; 38 const char *ret;
27 char type[100];
28 FILE *fp;
29 39
30 if (debugfs_found) 40 if (debugfs_found)
31 return (const char *)debugfs_mountpoint; 41 return (const char *)debugfs_mountpoint;
32 42
33 ptr = debugfs_known_mountpoints; 43 ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC,
34 while (*ptr) { 44 debugfs_mountpoint, PATH_MAX + 1,
35 if (debugfs_valid_mountpoint(*ptr) == 0) { 45 debugfs_known_mountpoints);
36 debugfs_found = true; 46 if (ret)
37 strcpy(debugfs_mountpoint, *ptr); 47 debugfs_found = true;
38 return debugfs_mountpoint;
39 }
40 ptr++;
41 }
42
43 /* give up and parse /proc/mounts */
44 fp = fopen("/proc/mounts", "r");
45 if (fp == NULL)
46 return NULL;
47
48 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
49 debugfs_mountpoint, type) == 2) {
50 if (strcmp(type, "debugfs") == 0)
51 break;
52 }
53 fclose(fp);
54 48
55 if (strcmp(type, "debugfs") != 0) 49 return ret;
56 return NULL;
57
58 debugfs_found = true;
59
60 return debugfs_mountpoint;
61}
62
63/* verify that a mountpoint is actually a debugfs instance */
64
65int debugfs_valid_mountpoint(const char *debugfs)
66{
67 struct statfs st_fs;
68
69 if (statfs(debugfs, &st_fs) < 0)
70 return -ENOENT;
71 else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC)
72 return -ENOENT;
73
74 return 0;
75} 50}
76 51
77/* mount the debugfs somewhere if it's not mounted */ 52/* mount the debugfs somewhere if it's not mounted */
@@ -87,7 +62,7 @@ char *debugfs_mount(const char *mountpoint)
87 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT); 62 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
88 /* if no environment variable, use default */ 63 /* if no environment variable, use default */
89 if (mountpoint == NULL) 64 if (mountpoint == NULL)
90 mountpoint = "/sys/kernel/debug"; 65 mountpoint = DEBUGFS_DEFAULT_PATH;
91 } 66 }
92 67
93 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) 68 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
diff --git a/tools/lib/api/fs/debugfs.h b/tools/lib/api/fs/debugfs.h
index 0739881a9897..455023698d2b 100644
--- a/tools/lib/api/fs/debugfs.h
+++ b/tools/lib/api/fs/debugfs.h
@@ -1,16 +1,7 @@
1#ifndef __API_DEBUGFS_H__ 1#ifndef __API_DEBUGFS_H__
2#define __API_DEBUGFS_H__ 2#define __API_DEBUGFS_H__
3 3
4#define _STR(x) #x 4#include "findfs.h"
5#define STR(x) _STR(x)
6
7/*
8 * On most systems <limits.h> would have given us this, but not on some systems
9 * (e.g. GNU/Hurd).
10 */
11#ifndef PATH_MAX
12#define PATH_MAX 4096
13#endif
14 5
15#ifndef DEBUGFS_MAGIC 6#ifndef DEBUGFS_MAGIC
16#define DEBUGFS_MAGIC 0x64626720 7#define DEBUGFS_MAGIC 0x64626720
@@ -20,8 +11,8 @@
20#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR" 11#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
21#endif 12#endif
22 13
14bool debugfs_configured(void);
23const char *debugfs_find_mountpoint(void); 15const char *debugfs_find_mountpoint(void);
24int debugfs_valid_mountpoint(const char *debugfs);
25char *debugfs_mount(const char *mountpoint); 16char *debugfs_mount(const char *mountpoint);
26 17
27extern char debugfs_mountpoint[]; 18extern char debugfs_mountpoint[];
diff --git a/tools/lib/api/fs/findfs.c b/tools/lib/api/fs/findfs.c
new file mode 100644
index 000000000000..49946cb6d7af
--- /dev/null
+++ b/tools/lib/api/fs/findfs.c
@@ -0,0 +1,63 @@
1#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <sys/vfs.h>
7
8#include "findfs.h"
9
10/* verify that a mountpoint is actually the type we want */
11
12int valid_mountpoint(const char *mount, long magic)
13{
14 struct statfs st_fs;
15
16 if (statfs(mount, &st_fs) < 0)
17 return -ENOENT;
18 else if ((long)st_fs.f_type != magic)
19 return -ENOENT;
20
21 return 0;
22}
23
24/* find the path to a mounted file system */
25const char *find_mountpoint(const char *fstype, long magic,
26 char *mountpoint, int len,
27 const char * const *known_mountpoints)
28{
29 const char * const *ptr;
30 char format[128];
31 char type[100];
32 FILE *fp;
33
34 if (known_mountpoints) {
35 ptr = known_mountpoints;
36 while (*ptr) {
37 if (valid_mountpoint(*ptr, magic) == 0) {
38 strncpy(mountpoint, *ptr, len - 1);
39 mountpoint[len-1] = 0;
40 return mountpoint;
41 }
42 ptr++;
43 }
44 }
45
46 /* give up and parse /proc/mounts */
47 fp = fopen("/proc/mounts", "r");
48 if (fp == NULL)
49 return NULL;
50
51 snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len);
52
53 while (fscanf(fp, format, mountpoint, type) == 2) {
54 if (strcmp(type, fstype) == 0)
55 break;
56 }
57 fclose(fp);
58
59 if (strcmp(type, fstype) != 0)
60 return NULL;
61
62 return mountpoint;
63}
diff --git a/tools/lib/api/fs/findfs.h b/tools/lib/api/fs/findfs.h
new file mode 100644
index 000000000000..b6f5d05acc42
--- /dev/null
+++ b/tools/lib/api/fs/findfs.h
@@ -0,0 +1,23 @@
1#ifndef __API_FINDFS_H__
2#define __API_FINDFS_H__
3
4#include <stdbool.h>
5
6#define _STR(x) #x
7#define STR(x) _STR(x)
8
9/*
10 * On most systems <limits.h> would have given us this, but not on some systems
11 * (e.g. GNU/Hurd).
12 */
13#ifndef PATH_MAX
14#define PATH_MAX 4096
15#endif
16
17const char *find_mountpoint(const char *fstype, long magic,
18 char *mountpoint, int len,
19 const char * const *known_mountpoints);
20
21int valid_mountpoint(const char *mount, long magic);
22
23#endif /* __API_FINDFS_H__ */
diff --git a/tools/lib/api/fs/tracefs.c b/tools/lib/api/fs/tracefs.c
new file mode 100644
index 000000000000..e4aa9688b71e
--- /dev/null
+++ b/tools/lib/api/fs/tracefs.c
@@ -0,0 +1,78 @@
1#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <stdbool.h>
7#include <sys/vfs.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <sys/mount.h>
11#include <linux/kernel.h>
12
13#include "tracefs.h"
14
15#ifndef TRACEFS_DEFAULT_PATH
16#define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
17#endif
18
19char tracefs_mountpoint[PATH_MAX + 1] = TRACEFS_DEFAULT_PATH;
20
21static const char * const tracefs_known_mountpoints[] = {
22 TRACEFS_DEFAULT_PATH,
23 "/sys/kernel/debug/tracing",
24 "/tracing",
25 "/trace",
26 0,
27};
28
29static bool tracefs_found;
30
31bool tracefs_configured(void)
32{
33 return tracefs_find_mountpoint() != NULL;
34}
35
36/* find the path to the mounted tracefs */
37const char *tracefs_find_mountpoint(void)
38{
39 const char *ret;
40
41 if (tracefs_found)
42 return (const char *)tracefs_mountpoint;
43
44 ret = find_mountpoint("tracefs", (long) TRACEFS_MAGIC,
45 tracefs_mountpoint, PATH_MAX + 1,
46 tracefs_known_mountpoints);
47
48 if (ret)
49 tracefs_found = true;
50
51 return ret;
52}
53
54/* mount the tracefs somewhere if it's not mounted */
55char *tracefs_mount(const char *mountpoint)
56{
57 /* see if it's already mounted */
58 if (tracefs_find_mountpoint())
59 goto out;
60
61 /* if not mounted and no argument */
62 if (mountpoint == NULL) {
63 /* see if environment variable set */
64 mountpoint = getenv(PERF_TRACEFS_ENVIRONMENT);
65 /* if no environment variable, use default */
66 if (mountpoint == NULL)
67 mountpoint = TRACEFS_DEFAULT_PATH;
68 }
69
70 if (mount(NULL, mountpoint, "tracefs", 0, NULL) < 0)
71 return NULL;
72
73 /* save the mountpoint */
74 tracefs_found = true;
75 strncpy(tracefs_mountpoint, mountpoint, sizeof(tracefs_mountpoint));
76out:
77 return tracefs_mountpoint;
78}
diff --git a/tools/lib/api/fs/tracefs.h b/tools/lib/api/fs/tracefs.h
new file mode 100644
index 000000000000..da780ac49acb
--- /dev/null
+++ b/tools/lib/api/fs/tracefs.h
@@ -0,0 +1,21 @@
1#ifndef __API_TRACEFS_H__
2#define __API_TRACEFS_H__
3
4#include "findfs.h"
5
6#ifndef TRACEFS_MAGIC
7#define TRACEFS_MAGIC 0x74726163
8#endif
9
10#ifndef PERF_TRACEFS_ENVIRONMENT
11#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
12#endif
13
14bool tracefs_configured(void);
15const char *tracefs_find_mountpoint(void);
16int tracefs_valid_mountpoint(const char *debugfs);
17char *tracefs_mount(const char *mountpoint);
18
19extern char tracefs_mountpoint[];
20
21#endif /* __API_DEBUGFS_H__ */
diff --git a/tools/lib/lockdep/Build b/tools/lib/lockdep/Build
new file mode 100644
index 000000000000..6f667355b068
--- /dev/null
+++ b/tools/lib/lockdep/Build
@@ -0,0 +1 @@
liblockdep-y += common.o lockdep.o preload.o rbtree.o
diff --git a/tools/lib/lockdep/Makefile b/tools/lib/lockdep/Makefile
index 4b866c54f624..0c356fb65022 100644
--- a/tools/lib/lockdep/Makefile
+++ b/tools/lib/lockdep/Makefile
@@ -35,6 +35,10 @@ bindir = $(prefix)/$(bindir_relative)
35 35
36export DESTDIR DESTDIR_SQ INSTALL 36export DESTDIR DESTDIR_SQ INSTALL
37 37
38MAKEFLAGS += --no-print-directory
39
40include ../../scripts/Makefile.include
41
38# copy a bit from Linux kbuild 42# copy a bit from Linux kbuild
39 43
40ifeq ("$(origin V)", "command line") 44ifeq ("$(origin V)", "command line")
@@ -44,56 +48,21 @@ ifndef VERBOSE
44 VERBOSE = 0 48 VERBOSE = 0
45endif 49endif
46 50
47ifeq ("$(origin O)", "command line") 51ifeq ($(srctree),)
48 BUILD_OUTPUT := $(O) 52srctree := $(patsubst %/,%,$(dir $(shell pwd)))
53srctree := $(patsubst %/,%,$(dir $(srctree)))
54srctree := $(patsubst %/,%,$(dir $(srctree)))
55#$(info Determined 'srctree' to be $(srctree))
49endif 56endif
50 57
51ifeq ($(BUILD_SRC),)
52ifneq ($(BUILD_OUTPUT),)
53
54define build_output
55 $(if $(VERBOSE:1=),@)$(MAKE) -C $(BUILD_OUTPUT) \
56 BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1
57endef
58
59saved-output := $(BUILD_OUTPUT)
60BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd)
61$(if $(BUILD_OUTPUT),, \
62 $(error output directory "$(saved-output)" does not exist))
63
64all: sub-make
65
66gui: force
67 $(call build_output, all_cmd)
68
69$(filter-out gui,$(MAKECMDGOALS)): sub-make
70
71sub-make: force
72 $(call build_output, $(MAKECMDGOALS))
73
74
75# Leave processing to above invocation of make
76skip-makefile := 1
77
78endif # BUILD_OUTPUT
79endif # BUILD_SRC
80
81# We process the rest of the Makefile if this is the final invocation of make
82ifeq ($(skip-makefile),)
83
84srctree := $(realpath $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR)))
85objtree := $(realpath $(CURDIR))
86src := $(srctree)
87obj := $(objtree)
88
89export prefix libdir bindir src obj
90
91# Shell quotes 58# Shell quotes
92libdir_SQ = $(subst ','\'',$(libdir)) 59libdir_SQ = $(subst ','\'',$(libdir))
93bindir_SQ = $(subst ','\'',$(bindir)) 60bindir_SQ = $(subst ','\'',$(bindir))
94 61
95LIB_FILE = liblockdep.a liblockdep.so.$(LIBLOCKDEP_VERSION) 62LIB_IN := $(OUTPUT)liblockdep-in.o
63
96BIN_FILE = lockdep 64BIN_FILE = lockdep
65LIB_FILE = $(OUTPUT)liblockdep.a $(OUTPUT)liblockdep.so.$(LIBLOCKDEP_VERSION)
97 66
98CONFIG_INCLUDES = 67CONFIG_INCLUDES =
99CONFIG_LIBS = 68CONFIG_LIBS =
@@ -108,33 +77,23 @@ INCLUDES = -I. -I./uinclude -I./include -I../../include $(CONFIG_INCLUDES)
108 77
109# Set compile option CFLAGS if not set elsewhere 78# Set compile option CFLAGS if not set elsewhere
110CFLAGS ?= -g -DCONFIG_LOCKDEP -DCONFIG_STACKTRACE -DCONFIG_PROVE_LOCKING -DBITS_PER_LONG=__WORDSIZE -DLIBLOCKDEP_VERSION='"$(LIBLOCKDEP_VERSION)"' -rdynamic -O0 -g 79CFLAGS ?= -g -DCONFIG_LOCKDEP -DCONFIG_STACKTRACE -DCONFIG_PROVE_LOCKING -DBITS_PER_LONG=__WORDSIZE -DLIBLOCKDEP_VERSION='"$(LIBLOCKDEP_VERSION)"' -rdynamic -O0 -g
80CFLAGS += -fPIC
111 81
112override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) 82override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
113 83
114ifeq ($(VERBOSE),1) 84ifeq ($(VERBOSE),1)
115 Q = 85 Q =
116 print_compile =
117 print_app_build =
118 print_fpic_compile =
119 print_shared_lib_compile = 86 print_shared_lib_compile =
120 print_install = 87 print_install =
121else 88else
122 Q = @ 89 Q = @
123 print_compile = echo ' CC '$(OBJ); 90 print_shared_lib_compile = echo ' LD '$(OBJ);
124 print_app_build = echo ' BUILD '$(OBJ); 91 print_static_lib_build = echo ' LD '$(OBJ);
125 print_fpic_compile = echo ' CC FPIC '$(OBJ); 92 print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
126 print_shared_lib_compile = echo ' BUILD SHARED LIB '$(OBJ);
127 print_static_lib_build = echo ' BUILD STATIC LIB '$(OBJ);
128 print_install = echo ' INSTALL '$1' to $(DESTDIR_SQ)$2';
129endif 93endif
130 94
131do_fpic_compile = \ 95export srctree OUTPUT CC LD CFLAGS V
132 ($(print_fpic_compile) \ 96build := -f $(srctree)/tools/build/Makefile.build dir=. obj
133 $(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@)
134
135do_app_build = \
136 ($(print_app_build) \
137 $(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS))
138 97
139do_compile_shared_library = \ 98do_compile_shared_library = \
140 ($(print_shared_lib_compile) \ 99 ($(print_shared_lib_compile) \
@@ -144,22 +103,6 @@ do_build_static_lib = \
144 ($(print_static_lib_build) \ 103 ($(print_static_lib_build) \
145 $(RM) $@; $(AR) rcs $@ $^) 104 $(RM) $@; $(AR) rcs $@ $^)
146 105
147
148define do_compile
149 $(print_compile) \
150 $(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
151endef
152
153$(obj)/%.o: $(src)/%.c
154 $(Q)$(call do_compile)
155
156%.o: $(src)/%.c
157 $(Q)$(call do_compile)
158
159PEVENT_LIB_OBJS = common.o lockdep.o preload.o rbtree.o
160
161ALL_OBJS = $(PEVENT_LIB_OBJS)
162
163CMD_TARGETS = $(LIB_FILE) 106CMD_TARGETS = $(LIB_FILE)
164 107
165TARGETS = $(CMD_TARGETS) 108TARGETS = $(CMD_TARGETS)
@@ -169,42 +112,15 @@ all: all_cmd
169 112
170all_cmd: $(CMD_TARGETS) 113all_cmd: $(CMD_TARGETS)
171 114
172liblockdep.so.$(LIBLOCKDEP_VERSION): $(PEVENT_LIB_OBJS) 115$(LIB_IN): force
116 $(Q)$(MAKE) $(build)=liblockdep
117
118liblockdep.so.$(LIBLOCKDEP_VERSION): $(LIB_IN)
173 $(Q)$(do_compile_shared_library) 119 $(Q)$(do_compile_shared_library)
174 120
175liblockdep.a: $(PEVENT_LIB_OBJS) 121liblockdep.a: $(LIB_IN)
176 $(Q)$(do_build_static_lib) 122 $(Q)$(do_build_static_lib)
177 123
178$(PEVENT_LIB_OBJS): %.o: $(src)/%.c
179 $(Q)$(do_fpic_compile)
180
181## make deps
182
183all_objs := $(sort $(ALL_OBJS))
184all_deps := $(all_objs:%.o=.%.d)
185
186# let .d file also depends on the source and header files
187define check_deps
188 @set -e; $(RM) $@; \
189 $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
190 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
191 $(RM) $@.$$$$
192endef
193
194$(all_deps): .%.d: $(src)/%.c
195 $(Q)$(call check_deps)
196
197$(all_objs) : %.o : .%.d
198
199dep_includes := $(wildcard $(all_deps))
200
201ifneq ($(dep_includes),)
202 include $(dep_includes)
203endif
204
205### Detect environment changes
206TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
207
208tags: force 124tags: force
209 $(RM) tags 125 $(RM) tags
210 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \ 126 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
@@ -233,8 +149,6 @@ clean:
233 $(RM) *.o *~ $(TARGETS) *.a *liblockdep*.so* $(VERSION_FILES) .*.d 149 $(RM) *.o *~ $(TARGETS) *.a *liblockdep*.so* $(VERSION_FILES) .*.d
234 $(RM) tags TAGS 150 $(RM) tags TAGS
235 151
236endif # skip-makefile
237
238PHONY += force 152PHONY += force
239force: 153force:
240 154
diff --git a/tools/lib/traceevent/Build b/tools/lib/traceevent/Build
new file mode 100644
index 000000000000..c681d0575d16
--- /dev/null
+++ b/tools/lib/traceevent/Build
@@ -0,0 +1,17 @@
1libtraceevent-y += event-parse.o
2libtraceevent-y += event-plugin.o
3libtraceevent-y += trace-seq.o
4libtraceevent-y += parse-filter.o
5libtraceevent-y += parse-utils.o
6libtraceevent-y += kbuffer-parse.o
7
8plugin_jbd2-y += plugin_jbd2.o
9plugin_hrtimer-y += plugin_hrtimer.o
10plugin_kmem-y += plugin_kmem.o
11plugin_kvm-y += plugin_kvm.o
12plugin_mac80211-y += plugin_mac80211.o
13plugin_sched_switch-y += plugin_sched_switch.o
14plugin_function-y += plugin_function.o
15plugin_xen-y += plugin_xen.o
16plugin_scsi-y += plugin_scsi.o
17plugin_cfg80211-y += plugin_cfg80211.o
diff --git a/tools/lib/traceevent/Makefile b/tools/lib/traceevent/Makefile
index 005c9cc06935..d410da335e3d 100644
--- a/tools/lib/traceevent/Makefile
+++ b/tools/lib/traceevent/Makefile
@@ -67,7 +67,7 @@ PLUGIN_DIR = -DPLUGIN_DIR="$(plugin_dir)"
67PLUGIN_DIR_SQ = '$(subst ','\'',$(PLUGIN_DIR))' 67PLUGIN_DIR_SQ = '$(subst ','\'',$(PLUGIN_DIR))'
68endif 68endif
69 69
70include $(if $(BUILD_SRC),$(BUILD_SRC)/)../../scripts/Makefile.include 70include ../../scripts/Makefile.include
71 71
72# copy a bit from Linux kbuild 72# copy a bit from Linux kbuild
73 73
@@ -78,40 +78,13 @@ ifndef VERBOSE
78 VERBOSE = 0 78 VERBOSE = 0
79endif 79endif
80 80
81ifeq ("$(origin O)", "command line") 81ifeq ($(srctree),)
82 BUILD_OUTPUT := $(O) 82srctree := $(patsubst %/,%,$(dir $(shell pwd)))
83srctree := $(patsubst %/,%,$(dir $(srctree)))
84srctree := $(patsubst %/,%,$(dir $(srctree)))
85#$(info Determined 'srctree' to be $(srctree))
83endif 86endif
84 87
85ifeq ($(BUILD_SRC),)
86ifneq ($(OUTPUT),)
87
88define build_output
89 $(if $(VERBOSE:1=),@)+$(MAKE) -C $(OUTPUT) \
90 BUILD_SRC=$(CURDIR)/ -f $(CURDIR)/Makefile $1
91endef
92
93all: sub-make
94
95$(MAKECMDGOALS): sub-make
96
97sub-make: force
98 $(call build_output, $(MAKECMDGOALS))
99
100
101# Leave processing to above invocation of make
102skip-makefile := 1
103
104endif # OUTPUT
105endif # BUILD_SRC
106
107# We process the rest of the Makefile if this is the final invocation of make
108ifeq ($(skip-makefile),)
109
110srctree := $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR))
111objtree := $(CURDIR)
112src := $(srctree)
113obj := $(objtree)
114
115export prefix bindir src obj 88export prefix bindir src obj
116 89
117# Shell quotes 90# Shell quotes
@@ -132,16 +105,19 @@ EXTRAVERSION = $(EP_EXTRAVERSION)
132OBJ = $@ 105OBJ = $@
133N = 106N =
134 107
135export Q VERBOSE
136
137EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION) 108EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION)
138 109
139INCLUDES = -I. -I $(srctree)/../../include $(CONFIG_INCLUDES) 110INCLUDES = -I. -I $(srctree)/tools/include $(CONFIG_INCLUDES)
140 111
141# Set compile option CFLAGS if not set elsewhere 112# Set compile option CFLAGS
142CFLAGS ?= -g -Wall 113ifdef EXTRA_CFLAGS
114 CFLAGS := $(EXTRA_CFLAGS)
115else
116 CFLAGS := -g -Wall
117endif
143 118
144# Append required CFLAGS 119# Append required CFLAGS
120override CFLAGS += -fPIC
145override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) 121override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ)
146override CFLAGS += $(udis86-flags) -D_GNU_SOURCE 122override CFLAGS += $(udis86-flags) -D_GNU_SOURCE
147 123
@@ -151,74 +127,58 @@ else
151 Q = @ 127 Q = @
152endif 128endif
153 129
154do_compile_shared_library = \ 130# Disable command line variables (CFLAGS) overide from top
155 ($(print_shared_lib_compile) \ 131# level Makefile (perf), otherwise build Makefile will get
156 $(CC) --shared $^ -o $@) 132# the same command line setup.
157 133MAKEOVERRIDES=
158do_plugin_build = \
159 ($(print_plugin_build) \
160 $(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<)
161
162do_build_static_lib = \
163 ($(print_static_lib_build) \
164 $(RM) $@; $(AR) rcs $@ $^)
165
166
167do_compile = $(QUIET_CC)$(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;
168 134
169$(obj)/%.o: $(src)/%.c 135export srctree OUTPUT CC LD CFLAGS V
170 $(call do_compile) 136build := -f $(srctree)/tools/build/Makefile.build dir=. obj
171 137
172%.o: $(src)/%.c 138PLUGINS = plugin_jbd2.so
173 $(call do_compile) 139PLUGINS += plugin_hrtimer.so
140PLUGINS += plugin_kmem.so
141PLUGINS += plugin_kvm.so
142PLUGINS += plugin_mac80211.so
143PLUGINS += plugin_sched_switch.so
144PLUGINS += plugin_function.so
145PLUGINS += plugin_xen.so
146PLUGINS += plugin_scsi.so
147PLUGINS += plugin_cfg80211.so
174 148
175PEVENT_LIB_OBJS = event-parse.o 149PLUGINS := $(addprefix $(OUTPUT),$(PLUGINS))
176PEVENT_LIB_OBJS += event-plugin.o 150PLUGINS_IN := $(PLUGINS:.so=-in.o)
177PEVENT_LIB_OBJS += trace-seq.o
178PEVENT_LIB_OBJS += parse-filter.o
179PEVENT_LIB_OBJS += parse-utils.o
180PEVENT_LIB_OBJS += kbuffer-parse.o
181 151
182PLUGIN_OBJS = plugin_jbd2.o 152TE_IN := $(OUTPUT)libtraceevent-in.o
183PLUGIN_OBJS += plugin_hrtimer.o 153LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE))
184PLUGIN_OBJS += plugin_kmem.o
185PLUGIN_OBJS += plugin_kvm.o
186PLUGIN_OBJS += plugin_mac80211.o
187PLUGIN_OBJS += plugin_sched_switch.o
188PLUGIN_OBJS += plugin_function.o
189PLUGIN_OBJS += plugin_xen.o
190PLUGIN_OBJS += plugin_scsi.o
191PLUGIN_OBJS += plugin_cfg80211.o
192
193PLUGINS := $(PLUGIN_OBJS:.o=.so)
194
195ALL_OBJS = $(PEVENT_LIB_OBJS) $(PLUGIN_OBJS)
196 154
197CMD_TARGETS = $(LIB_FILE) $(PLUGINS) 155CMD_TARGETS = $(LIB_FILE) $(PLUGINS)
198 156
199TARGETS = $(CMD_TARGETS) 157TARGETS = $(CMD_TARGETS)
200 158
201
202all: all_cmd 159all: all_cmd
203 160
204all_cmd: $(CMD_TARGETS) 161all_cmd: $(CMD_TARGETS)
205 162
206libtraceevent.so: $(PEVENT_LIB_OBJS) 163$(TE_IN): force
164 $(Q)$(MAKE) $(build)=libtraceevent
165
166$(OUTPUT)libtraceevent.so: $(TE_IN)
207 $(QUIET_LINK)$(CC) --shared $^ -o $@ 167 $(QUIET_LINK)$(CC) --shared $^ -o $@
208 168
209libtraceevent.a: $(PEVENT_LIB_OBJS) 169$(OUTPUT)libtraceevent.a: $(TE_IN)
210 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 170 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^
211 171
212plugins: $(PLUGINS) 172plugins: $(PLUGINS)
213 173
214$(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS 174__plugin_obj = $(notdir $@)
215 $(QUIET_CC_FPIC)$(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@ 175 plugin_obj = $(__plugin_obj:-in.o=)
216 176
217$(PLUGIN_OBJS): %.o : $(src)/%.c 177$(PLUGINS_IN): force
218 $(QUIET_CC_FPIC)$(CC) -c $(CFLAGS) -fPIC -o $@ $< 178 $(Q)$(MAKE) $(build)=$(plugin_obj)
219 179
220$(PLUGINS): %.so: %.o 180$(OUTPUT)%.so: $(OUTPUT)%-in.o
221 $(QUIET_LINK)$(CC) $(CFLAGS) -shared -nostartfiles -o $@ $< 181 $(QUIET_LINK)$(CC) $(CFLAGS) -shared -nostartfiles -o $@ $^
222 182
223define make_version.h 183define make_version.h
224 (echo '/* This file is automatically generated. Do not modify. */'; \ 184 (echo '/* This file is automatically generated. Do not modify. */'; \
@@ -255,40 +215,6 @@ define update_dir
255 fi); 215 fi);
256endef 216endef
257 217
258## make deps
259
260all_objs := $(sort $(ALL_OBJS))
261all_deps := $(all_objs:%.o=.%.d)
262
263# let .d file also depends on the source and header files
264define check_deps
265 @set -e; $(RM) $@; \
266 $(CC) -MM $(CFLAGS) $< > $@.$$$$; \
267 sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
268 $(RM) $@.$$$$
269endef
270
271$(all_deps): .%.d: $(src)/%.c
272 $(Q)$(call check_deps)
273
274$(all_objs) : %.o : .%.d
275
276dep_includes := $(wildcard $(all_deps))
277
278ifneq ($(dep_includes),)
279 include $(dep_includes)
280endif
281
282### Detect environment changes
283TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
284
285TRACEEVENT-CFLAGS: force
286 @FLAGS='$(TRACK_CFLAGS)'; \
287 if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \
288 echo 1>&2 " FLAGS: * new build flags or cross compiler"; \
289 echo "$$FLAGS" >TRACEEVENT-CFLAGS; \
290 fi
291
292tags: force 218tags: force
293 $(RM) tags 219 $(RM) tags
294 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \ 220 find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \
@@ -327,14 +253,9 @@ clean:
327 $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \ 253 $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \
328 $(RM) TRACEEVENT-CFLAGS tags TAGS 254 $(RM) TRACEEVENT-CFLAGS tags TAGS
329 255
330endif # skip-makefile
331
332PHONY += force plugins 256PHONY += force plugins
333force: 257force:
334 258
335plugins:
336 @echo > /dev/null
337
338# Declare the contents of the .PHONY variable as phony. We keep that 259# Declare the contents of the .PHONY variable as phony. We keep that
339# information in a variable so we can use it in if_changed and friends. 260# information in a variable so we can use it in if_changed and friends.
340.PHONY: $(PHONY) 261.PHONY: $(PHONY)
diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c
index 2c0bd8f2aad0..e0917c0f5d9f 100644
--- a/tools/lib/traceevent/event-parse.c
+++ b/tools/lib/traceevent/event-parse.c
@@ -304,7 +304,10 @@ int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
304 if (!item) 304 if (!item)
305 return -1; 305 return -1;
306 306
307 item->comm = strdup(comm); 307 if (comm)
308 item->comm = strdup(comm);
309 else
310 item->comm = strdup("<...>");
308 if (!item->comm) { 311 if (!item->comm) {
309 free(item); 312 free(item);
310 return -1; 313 return -1;
@@ -318,9 +321,14 @@ int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
318 return 0; 321 return 0;
319} 322}
320 323
321void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock) 324int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
322{ 325{
323 pevent->trace_clock = trace_clock; 326 pevent->trace_clock = strdup(trace_clock);
327 if (!pevent->trace_clock) {
328 errno = ENOMEM;
329 return -1;
330 }
331 return 0;
324} 332}
325 333
326struct func_map { 334struct func_map {
@@ -758,6 +766,11 @@ static void free_arg(struct print_arg *arg)
758 free_arg(arg->hex.field); 766 free_arg(arg->hex.field);
759 free_arg(arg->hex.size); 767 free_arg(arg->hex.size);
760 break; 768 break;
769 case PRINT_INT_ARRAY:
770 free_arg(arg->int_array.field);
771 free_arg(arg->int_array.count);
772 free_arg(arg->int_array.el_size);
773 break;
761 case PRINT_TYPE: 774 case PRINT_TYPE:
762 free(arg->typecast.type); 775 free(arg->typecast.type);
763 free_arg(arg->typecast.item); 776 free_arg(arg->typecast.item);
@@ -1926,7 +1939,22 @@ process_op(struct event_format *event, struct print_arg *arg, char **tok)
1926 goto out_warn_free; 1939 goto out_warn_free;
1927 1940
1928 type = process_arg_token(event, right, tok, type); 1941 type = process_arg_token(event, right, tok, type);
1929 arg->op.right = right; 1942
1943 if (right->type == PRINT_OP &&
1944 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
1945 struct print_arg tmp;
1946
1947 /* rotate ops according to the priority */
1948 arg->op.right = right->op.left;
1949
1950 tmp = *arg;
1951 *arg = *right;
1952 *right = tmp;
1953
1954 arg->op.left = right;
1955 } else {
1956 arg->op.right = right;
1957 }
1930 1958
1931 } else if (strcmp(token, "[") == 0) { 1959 } else if (strcmp(token, "[") == 0) {
1932 1960
@@ -2014,6 +2042,38 @@ process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2014 return EVENT_ERROR; 2042 return EVENT_ERROR;
2015} 2043}
2016 2044
2045static int alloc_and_process_delim(struct event_format *event, char *next_token,
2046 struct print_arg **print_arg)
2047{
2048 struct print_arg *field;
2049 enum event_type type;
2050 char *token;
2051 int ret = 0;
2052
2053 field = alloc_arg();
2054 if (!field) {
2055 do_warning_event(event, "%s: not enough memory!", __func__);
2056 errno = ENOMEM;
2057 return -1;
2058 }
2059
2060 type = process_arg(event, field, &token);
2061
2062 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2063 errno = EINVAL;
2064 ret = -1;
2065 free_arg(field);
2066 goto out_free_token;
2067 }
2068
2069 *print_arg = field;
2070
2071out_free_token:
2072 free_token(token);
2073
2074 return ret;
2075}
2076
2017static char *arg_eval (struct print_arg *arg); 2077static char *arg_eval (struct print_arg *arg);
2018 2078
2019static unsigned long long 2079static unsigned long long
@@ -2486,49 +2546,46 @@ out_free:
2486static enum event_type 2546static enum event_type
2487process_hex(struct event_format *event, struct print_arg *arg, char **tok) 2547process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2488{ 2548{
2489 struct print_arg *field;
2490 enum event_type type;
2491 char *token = NULL;
2492
2493 memset(arg, 0, sizeof(*arg)); 2549 memset(arg, 0, sizeof(*arg));
2494 arg->type = PRINT_HEX; 2550 arg->type = PRINT_HEX;
2495 2551
2496 field = alloc_arg(); 2552 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2497 if (!field) { 2553 goto out;
2498 do_warning_event(event, "%s: not enough memory!", __func__);
2499 goto out_free;
2500 }
2501 2554
2502 type = process_arg(event, field, &token); 2555 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2556 goto free_field;
2503 2557
2504 if (test_type_token(type, token, EVENT_DELIM, ",")) 2558 return read_token_item(tok);
2505 goto out_free;
2506
2507 arg->hex.field = field;
2508 2559
2509 free_token(token); 2560free_field:
2561 free_arg(arg->hex.field);
2562out:
2563 *tok = NULL;
2564 return EVENT_ERROR;
2565}
2510 2566
2511 field = alloc_arg(); 2567static enum event_type
2512 if (!field) { 2568process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2513 do_warning_event(event, "%s: not enough memory!", __func__); 2569{
2514 *tok = NULL; 2570 memset(arg, 0, sizeof(*arg));
2515 return EVENT_ERROR; 2571 arg->type = PRINT_INT_ARRAY;
2516 }
2517 2572
2518 type = process_arg(event, field, &token); 2573 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2574 goto out;
2519 2575
2520 if (test_type_token(type, token, EVENT_DELIM, ")")) 2576 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2521 goto out_free; 2577 goto free_field;
2522 2578
2523 arg->hex.size = field; 2579 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2580 goto free_size;
2524 2581
2525 free_token(token); 2582 return read_token_item(tok);
2526 type = read_token_item(tok);
2527 return type;
2528 2583
2529 out_free: 2584free_size:
2530 free_arg(field); 2585 free_arg(arg->int_array.count);
2531 free_token(token); 2586free_field:
2587 free_arg(arg->int_array.field);
2588out:
2532 *tok = NULL; 2589 *tok = NULL;
2533 return EVENT_ERROR; 2590 return EVENT_ERROR;
2534} 2591}
@@ -2828,6 +2885,10 @@ process_function(struct event_format *event, struct print_arg *arg,
2828 free_token(token); 2885 free_token(token);
2829 return process_hex(event, arg, tok); 2886 return process_hex(event, arg, tok);
2830 } 2887 }
2888 if (strcmp(token, "__print_array") == 0) {
2889 free_token(token);
2890 return process_int_array(event, arg, tok);
2891 }
2831 if (strcmp(token, "__get_str") == 0) { 2892 if (strcmp(token, "__get_str") == 0) {
2832 free_token(token); 2893 free_token(token);
2833 return process_str(event, arg, tok); 2894 return process_str(event, arg, tok);
@@ -3356,6 +3417,7 @@ eval_num_arg(void *data, int size, struct event_format *event, struct print_arg
3356 break; 3417 break;
3357 case PRINT_FLAGS: 3418 case PRINT_FLAGS:
3358 case PRINT_SYMBOL: 3419 case PRINT_SYMBOL:
3420 case PRINT_INT_ARRAY:
3359 case PRINT_HEX: 3421 case PRINT_HEX:
3360 break; 3422 break;
3361 case PRINT_TYPE: 3423 case PRINT_TYPE:
@@ -3568,7 +3630,7 @@ static const struct flag flags[] = {
3568 { "HRTIMER_RESTART", 1 }, 3630 { "HRTIMER_RESTART", 1 },
3569}; 3631};
3570 3632
3571static unsigned long long eval_flag(const char *flag) 3633static long long eval_flag(const char *flag)
3572{ 3634{
3573 int i; 3635 int i;
3574 3636
@@ -3584,7 +3646,7 @@ static unsigned long long eval_flag(const char *flag)
3584 if (strcmp(flags[i].name, flag) == 0) 3646 if (strcmp(flags[i].name, flag) == 0)
3585 return flags[i].value; 3647 return flags[i].value;
3586 3648
3587 return 0; 3649 return -1LL;
3588} 3650}
3589 3651
3590static void print_str_to_seq(struct trace_seq *s, const char *format, 3652static void print_str_to_seq(struct trace_seq *s, const char *format,
@@ -3658,7 +3720,7 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
3658 struct print_flag_sym *flag; 3720 struct print_flag_sym *flag;
3659 struct format_field *field; 3721 struct format_field *field;
3660 struct printk_map *printk; 3722 struct printk_map *printk;
3661 unsigned long long val, fval; 3723 long long val, fval;
3662 unsigned long addr; 3724 unsigned long addr;
3663 char *str; 3725 char *str;
3664 unsigned char *hex; 3726 unsigned char *hex;
@@ -3717,11 +3779,11 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
3717 print = 0; 3779 print = 0;
3718 for (flag = arg->flags.flags; flag; flag = flag->next) { 3780 for (flag = arg->flags.flags; flag; flag = flag->next) {
3719 fval = eval_flag(flag->value); 3781 fval = eval_flag(flag->value);
3720 if (!val && !fval) { 3782 if (!val && fval < 0) {
3721 print_str_to_seq(s, format, len_arg, flag->str); 3783 print_str_to_seq(s, format, len_arg, flag->str);
3722 break; 3784 break;
3723 } 3785 }
3724 if (fval && (val & fval) == fval) { 3786 if (fval > 0 && (val & fval) == fval) {
3725 if (print && arg->flags.delim) 3787 if (print && arg->flags.delim)
3726 trace_seq_puts(s, arg->flags.delim); 3788 trace_seq_puts(s, arg->flags.delim);
3727 print_str_to_seq(s, format, len_arg, flag->str); 3789 print_str_to_seq(s, format, len_arg, flag->str);
@@ -3766,6 +3828,54 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
3766 } 3828 }
3767 break; 3829 break;
3768 3830
3831 case PRINT_INT_ARRAY: {
3832 void *num;
3833 int el_size;
3834
3835 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3836 unsigned long offset;
3837 struct format_field *field =
3838 arg->int_array.field->dynarray.field;
3839 offset = pevent_read_number(pevent,
3840 data + field->offset,
3841 field->size);
3842 num = data + (offset & 0xffff);
3843 } else {
3844 field = arg->int_array.field->field.field;
3845 if (!field) {
3846 str = arg->int_array.field->field.name;
3847 field = pevent_find_any_field(event, str);
3848 if (!field)
3849 goto out_warning_field;
3850 arg->int_array.field->field.field = field;
3851 }
3852 num = data + field->offset;
3853 }
3854 len = eval_num_arg(data, size, event, arg->int_array.count);
3855 el_size = eval_num_arg(data, size, event,
3856 arg->int_array.el_size);
3857 for (i = 0; i < len; i++) {
3858 if (i)
3859 trace_seq_putc(s, ' ');
3860
3861 if (el_size == 1) {
3862 trace_seq_printf(s, "%u", *(uint8_t *)num);
3863 } else if (el_size == 2) {
3864 trace_seq_printf(s, "%u", *(uint16_t *)num);
3865 } else if (el_size == 4) {
3866 trace_seq_printf(s, "%u", *(uint32_t *)num);
3867 } else if (el_size == 8) {
3868 trace_seq_printf(s, "%lu", *(uint64_t *)num);
3869 } else {
3870 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
3871 el_size, *(uint8_t *)num);
3872 el_size = 1;
3873 }
3874
3875 num += el_size;
3876 }
3877 break;
3878 }
3769 case PRINT_TYPE: 3879 case PRINT_TYPE:
3770 break; 3880 break;
3771 case PRINT_STRING: { 3881 case PRINT_STRING: {
@@ -3997,6 +4107,10 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc
3997 goto process_again; 4107 goto process_again;
3998 case '.': 4108 case '.':
3999 goto process_again; 4109 goto process_again;
4110 case 'z':
4111 case 'Z':
4112 ls = 1;
4113 goto process_again;
4000 case 'p': 4114 case 'p':
4001 ls = 1; 4115 ls = 1;
4002 /* fall through */ 4116 /* fall through */
@@ -4939,6 +5053,96 @@ const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4939 return comm; 5053 return comm;
4940} 5054}
4941 5055
5056static struct cmdline *
5057pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5058{
5059 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5060
5061 if (cmdlist)
5062 cmdlist = cmdlist->next;
5063 else
5064 cmdlist = pevent->cmdlist;
5065
5066 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5067 cmdlist = cmdlist->next;
5068
5069 return (struct cmdline *)cmdlist;
5070}
5071
5072/**
5073 * pevent_data_pid_from_comm - return the pid from a given comm
5074 * @pevent: a handle to the pevent
5075 * @comm: the cmdline to find the pid from
5076 * @next: the cmdline structure to find the next comm
5077 *
5078 * This returns the cmdline structure that holds a pid for a given
5079 * comm, or NULL if none found. As there may be more than one pid for
5080 * a given comm, the result of this call can be passed back into
5081 * a recurring call in the @next paramater, and then it will find the
5082 * next pid.
5083 * Also, it does a linear seach, so it may be slow.
5084 */
5085struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5086 struct cmdline *next)
5087{
5088 struct cmdline *cmdline;
5089
5090 /*
5091 * If the cmdlines have not been converted yet, then use
5092 * the list.
5093 */
5094 if (!pevent->cmdlines)
5095 return pid_from_cmdlist(pevent, comm, next);
5096
5097 if (next) {
5098 /*
5099 * The next pointer could have been still from
5100 * a previous call before cmdlines were created
5101 */
5102 if (next < pevent->cmdlines ||
5103 next >= pevent->cmdlines + pevent->cmdline_count)
5104 next = NULL;
5105 else
5106 cmdline = next++;
5107 }
5108
5109 if (!next)
5110 cmdline = pevent->cmdlines;
5111
5112 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5113 if (strcmp(cmdline->comm, comm) == 0)
5114 return cmdline;
5115 cmdline++;
5116 }
5117 return NULL;
5118}
5119
5120/**
5121 * pevent_cmdline_pid - return the pid associated to a given cmdline
5122 * @cmdline: The cmdline structure to get the pid from
5123 *
5124 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5125 * -1 is returned.
5126 */
5127int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5128{
5129 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5130
5131 if (!cmdline)
5132 return -1;
5133
5134 /*
5135 * If cmdlines have not been created yet, or cmdline is
5136 * not part of the array, then treat it as a cmdlist instead.
5137 */
5138 if (!pevent->cmdlines ||
5139 cmdline < pevent->cmdlines ||
5140 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5141 return cmdlist->pid;
5142
5143 return cmdline->pid;
5144}
5145
4942/** 5146/**
4943 * pevent_data_comm_from_pid - parse the data into the print format 5147 * pevent_data_comm_from_pid - parse the data into the print format
4944 * @s: the trace_seq to write to 5148 * @s: the trace_seq to write to
@@ -5256,6 +5460,15 @@ static void print_args(struct print_arg *args)
5256 print_args(args->hex.size); 5460 print_args(args->hex.size);
5257 printf(")"); 5461 printf(")");
5258 break; 5462 break;
5463 case PRINT_INT_ARRAY:
5464 printf("__print_array(");
5465 print_args(args->int_array.field);
5466 printf(", ");
5467 print_args(args->int_array.count);
5468 printf(", ");
5469 print_args(args->int_array.el_size);
5470 printf(")");
5471 break;
5259 case PRINT_STRING: 5472 case PRINT_STRING:
5260 case PRINT_BSTRING: 5473 case PRINT_BSTRING:
5261 printf("__get_str(%s)", args->string.string); 5474 printf("__get_str(%s)", args->string.string);
@@ -6228,15 +6441,20 @@ void pevent_ref(struct pevent *pevent)
6228 pevent->ref_count++; 6441 pevent->ref_count++;
6229} 6442}
6230 6443
6444void pevent_free_format_field(struct format_field *field)
6445{
6446 free(field->type);
6447 free(field->name);
6448 free(field);
6449}
6450
6231static void free_format_fields(struct format_field *field) 6451static void free_format_fields(struct format_field *field)
6232{ 6452{
6233 struct format_field *next; 6453 struct format_field *next;
6234 6454
6235 while (field) { 6455 while (field) {
6236 next = field->next; 6456 next = field->next;
6237 free(field->type); 6457 pevent_free_format_field(field);
6238 free(field->name);
6239 free(field);
6240 field = next; 6458 field = next;
6241 } 6459 }
6242} 6460}
@@ -6341,6 +6559,7 @@ void pevent_free(struct pevent *pevent)
6341 free_handler(handle); 6559 free_handler(handle);
6342 } 6560 }
6343 6561
6562 free(pevent->trace_clock);
6344 free(pevent->events); 6563 free(pevent->events);
6345 free(pevent->sort_events); 6564 free(pevent->sort_events);
6346 6565
diff --git a/tools/lib/traceevent/event-parse.h b/tools/lib/traceevent/event-parse.h
index 7a3873ff9a4f..86a5839fb048 100644
--- a/tools/lib/traceevent/event-parse.h
+++ b/tools/lib/traceevent/event-parse.h
@@ -22,6 +22,7 @@
22 22
23#include <stdbool.h> 23#include <stdbool.h>
24#include <stdarg.h> 24#include <stdarg.h>
25#include <stdio.h>
25#include <regex.h> 26#include <regex.h>
26#include <string.h> 27#include <string.h>
27 28
@@ -91,6 +92,7 @@ extern int trace_seq_putc(struct trace_seq *s, unsigned char c);
91 92
92extern void trace_seq_terminate(struct trace_seq *s); 93extern void trace_seq_terminate(struct trace_seq *s);
93 94
95extern int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp);
94extern int trace_seq_do_printf(struct trace_seq *s); 96extern int trace_seq_do_printf(struct trace_seq *s);
95 97
96 98
@@ -114,7 +116,7 @@ struct pevent_plugin_option {
114 char *name; 116 char *name;
115 char *plugin_alias; 117 char *plugin_alias;
116 char *description; 118 char *description;
117 char *value; 119 const char *value;
118 void *priv; 120 void *priv;
119 int set; 121 int set;
120}; 122};
@@ -152,6 +154,10 @@ struct pevent_plugin_option {
152 * .plugin_alias is used to give a shorter name to access 154 * .plugin_alias is used to give a shorter name to access
153 * the vairable. Useful if a plugin handles more than one event. 155 * the vairable. Useful if a plugin handles more than one event.
154 * 156 *
157 * If .value is not set, then it is considered a boolean and only
158 * .set will be processed. If .value is defined, then it is considered
159 * a string option and .set will be ignored.
160 *
155 * PEVENT_PLUGIN_ALIAS: (optional) 161 * PEVENT_PLUGIN_ALIAS: (optional)
156 * The name to use for finding options (uses filename if not defined) 162 * The name to use for finding options (uses filename if not defined)
157 */ 163 */
@@ -245,6 +251,12 @@ struct print_arg_hex {
245 struct print_arg *size; 251 struct print_arg *size;
246}; 252};
247 253
254struct print_arg_int_array {
255 struct print_arg *field;
256 struct print_arg *count;
257 struct print_arg *el_size;
258};
259
248struct print_arg_dynarray { 260struct print_arg_dynarray {
249 struct format_field *field; 261 struct format_field *field;
250 struct print_arg *index; 262 struct print_arg *index;
@@ -273,6 +285,7 @@ enum print_arg_type {
273 PRINT_FLAGS, 285 PRINT_FLAGS,
274 PRINT_SYMBOL, 286 PRINT_SYMBOL,
275 PRINT_HEX, 287 PRINT_HEX,
288 PRINT_INT_ARRAY,
276 PRINT_TYPE, 289 PRINT_TYPE,
277 PRINT_STRING, 290 PRINT_STRING,
278 PRINT_BSTRING, 291 PRINT_BSTRING,
@@ -292,6 +305,7 @@ struct print_arg {
292 struct print_arg_flags flags; 305 struct print_arg_flags flags;
293 struct print_arg_symbol symbol; 306 struct print_arg_symbol symbol;
294 struct print_arg_hex hex; 307 struct print_arg_hex hex;
308 struct print_arg_int_array int_array;
295 struct print_arg_func func; 309 struct print_arg_func func;
296 struct print_arg_string string; 310 struct print_arg_string string;
297 struct print_arg_bitmask bitmask; 311 struct print_arg_bitmask bitmask;
@@ -597,7 +611,7 @@ enum trace_flag_type {
597}; 611};
598 612
599int pevent_register_comm(struct pevent *pevent, const char *comm, int pid); 613int pevent_register_comm(struct pevent *pevent, const char *comm, int pid);
600void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock); 614int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock);
601int pevent_register_function(struct pevent *pevent, char *name, 615int pevent_register_function(struct pevent *pevent, char *name,
602 unsigned long long addr, char *mod); 616 unsigned long long addr, char *mod);
603int pevent_register_print_string(struct pevent *pevent, const char *fmt, 617int pevent_register_print_string(struct pevent *pevent, const char *fmt,
@@ -617,6 +631,7 @@ enum pevent_errno pevent_parse_format(struct pevent *pevent,
617 const char *buf, 631 const char *buf,
618 unsigned long size, const char *sys); 632 unsigned long size, const char *sys);
619void pevent_free_format(struct event_format *event); 633void pevent_free_format(struct event_format *event);
634void pevent_free_format_field(struct format_field *field);
620 635
621void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event, 636void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
622 const char *name, struct pevent_record *record, 637 const char *name, struct pevent_record *record,
@@ -675,6 +690,11 @@ int pevent_data_type(struct pevent *pevent, struct pevent_record *rec);
675struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type); 690struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type);
676int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec); 691int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec);
677const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid); 692const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid);
693struct cmdline;
694struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
695 struct cmdline *next);
696int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline);
697
678void pevent_event_info(struct trace_seq *s, struct event_format *event, 698void pevent_event_info(struct trace_seq *s, struct event_format *event,
679 struct pevent_record *record); 699 struct pevent_record *record);
680int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum, 700int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
diff --git a/tools/lib/traceevent/event-plugin.c b/tools/lib/traceevent/event-plugin.c
index 136162c03af1..a16756ae3526 100644
--- a/tools/lib/traceevent/event-plugin.c
+++ b/tools/lib/traceevent/event-plugin.c
@@ -18,6 +18,7 @@
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */ 19 */
20 20
21#include <ctype.h>
21#include <stdio.h> 22#include <stdio.h>
22#include <string.h> 23#include <string.h>
23#include <dlfcn.h> 24#include <dlfcn.h>
@@ -49,6 +50,52 @@ struct plugin_list {
49 void *handle; 50 void *handle;
50}; 51};
51 52
53static void lower_case(char *str)
54{
55 if (!str)
56 return;
57 for (; *str; str++)
58 *str = tolower(*str);
59}
60
61static int update_option_value(struct pevent_plugin_option *op, const char *val)
62{
63 char *op_val;
64
65 if (!val) {
66 /* toggle, only if option is boolean */
67 if (op->value)
68 /* Warn? */
69 return 0;
70 op->set ^= 1;
71 return 0;
72 }
73
74 /*
75 * If the option has a value then it takes a string
76 * otherwise the option is a boolean.
77 */
78 if (op->value) {
79 op->value = val;
80 return 0;
81 }
82
83 /* Option is boolean, must be either "1", "0", "true" or "false" */
84
85 op_val = strdup(val);
86 if (!op_val)
87 return -1;
88 lower_case(op_val);
89
90 if (strcmp(val, "1") == 0 || strcmp(val, "true") == 0)
91 op->set = 1;
92 else if (strcmp(val, "0") == 0 || strcmp(val, "false") == 0)
93 op->set = 0;
94 free(op_val);
95
96 return 0;
97}
98
52/** 99/**
53 * traceevent_plugin_list_options - get list of plugin options 100 * traceevent_plugin_list_options - get list of plugin options
54 * 101 *
@@ -120,6 +167,7 @@ update_option(const char *file, struct pevent_plugin_option *option)
120{ 167{
121 struct trace_plugin_options *op; 168 struct trace_plugin_options *op;
122 char *plugin; 169 char *plugin;
170 int ret = 0;
123 171
124 if (option->plugin_alias) { 172 if (option->plugin_alias) {
125 plugin = strdup(option->plugin_alias); 173 plugin = strdup(option->plugin_alias);
@@ -144,9 +192,10 @@ update_option(const char *file, struct pevent_plugin_option *option)
144 if (strcmp(op->option, option->name) != 0) 192 if (strcmp(op->option, option->name) != 0)
145 continue; 193 continue;
146 194
147 option->value = op->value; 195 ret = update_option_value(option, op->value);
148 option->set ^= 1; 196 if (ret)
149 goto out; 197 goto out;
198 break;
150 } 199 }
151 200
152 /* first look for unnamed options */ 201 /* first look for unnamed options */
@@ -156,14 +205,13 @@ update_option(const char *file, struct pevent_plugin_option *option)
156 if (strcmp(op->option, option->name) != 0) 205 if (strcmp(op->option, option->name) != 0)
157 continue; 206 continue;
158 207
159 option->value = op->value; 208 ret = update_option_value(option, op->value);
160 option->set ^= 1;
161 break; 209 break;
162 } 210 }
163 211
164 out: 212 out:
165 free(plugin); 213 free(plugin);
166 return 0; 214 return ret;
167} 215}
168 216
169/** 217/**
diff --git a/tools/lib/traceevent/kbuffer-parse.c b/tools/lib/traceevent/kbuffer-parse.c
index dcc665228c71..3bcada3ae05a 100644
--- a/tools/lib/traceevent/kbuffer-parse.c
+++ b/tools/lib/traceevent/kbuffer-parse.c
@@ -372,7 +372,6 @@ translate_data(struct kbuffer *kbuf, void *data, void **rptr,
372 switch (type_len) { 372 switch (type_len) {
373 case KBUFFER_TYPE_PADDING: 373 case KBUFFER_TYPE_PADDING:
374 *length = read_4(kbuf, data); 374 *length = read_4(kbuf, data);
375 data += *length;
376 break; 375 break;
377 376
378 case KBUFFER_TYPE_TIME_EXTEND: 377 case KBUFFER_TYPE_TIME_EXTEND:
@@ -730,3 +729,14 @@ void kbuffer_set_old_format(struct kbuffer *kbuf)
730 729
731 kbuf->next_event = __old_next_event; 730 kbuf->next_event = __old_next_event;
732} 731}
732
733/**
734 * kbuffer_start_of_data - return offset of where data starts on subbuffer
735 * @kbuf: The kbuffer
736 *
737 * Returns the location on the subbuffer where the data starts.
738 */
739int kbuffer_start_of_data(struct kbuffer *kbuf)
740{
741 return kbuf->start;
742}
diff --git a/tools/lib/traceevent/kbuffer.h b/tools/lib/traceevent/kbuffer.h
index c831f64b17a0..03dce757553f 100644
--- a/tools/lib/traceevent/kbuffer.h
+++ b/tools/lib/traceevent/kbuffer.h
@@ -63,5 +63,6 @@ int kbuffer_missed_events(struct kbuffer *kbuf);
63int kbuffer_subbuffer_size(struct kbuffer *kbuf); 63int kbuffer_subbuffer_size(struct kbuffer *kbuf);
64 64
65void kbuffer_set_old_format(struct kbuffer *kbuf); 65void kbuffer_set_old_format(struct kbuffer *kbuf);
66int kbuffer_start_of_data(struct kbuffer *kbuf);
66 67
67#endif /* _K_BUFFER_H */ 68#endif /* _K_BUFFER_H */
diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c
index b50234402fc2..0144b3d1bb77 100644
--- a/tools/lib/traceevent/parse-filter.c
+++ b/tools/lib/traceevent/parse-filter.c
@@ -1058,6 +1058,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,
1058 *parg = current_op; 1058 *parg = current_op;
1059 else 1059 else
1060 *parg = current_exp; 1060 *parg = current_exp;
1061 free(token);
1061 return PEVENT_ERRNO__UNBALANCED_PAREN; 1062 return PEVENT_ERRNO__UNBALANCED_PAREN;
1062 } 1063 }
1063 break; 1064 break;
@@ -1168,6 +1169,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,
1168 1169
1169 *parg = current_op; 1170 *parg = current_op;
1170 1171
1172 free(token);
1171 return 0; 1173 return 0;
1172 1174
1173 fail_alloc: 1175 fail_alloc:
diff --git a/tools/lib/traceevent/trace-seq.c b/tools/lib/traceevent/trace-seq.c
index ec3bd16a5488..292dc9f1d233 100644
--- a/tools/lib/traceevent/trace-seq.c
+++ b/tools/lib/traceevent/trace-seq.c
@@ -231,19 +231,24 @@ void trace_seq_terminate(struct trace_seq *s)
231 s->buffer[s->len] = 0; 231 s->buffer[s->len] = 0;
232} 232}
233 233
234int trace_seq_do_printf(struct trace_seq *s) 234int trace_seq_do_fprintf(struct trace_seq *s, FILE *fp)
235{ 235{
236 TRACE_SEQ_CHECK(s); 236 TRACE_SEQ_CHECK(s);
237 237
238 switch (s->state) { 238 switch (s->state) {
239 case TRACE_SEQ__GOOD: 239 case TRACE_SEQ__GOOD:
240 return printf("%.*s", s->len, s->buffer); 240 return fprintf(fp, "%.*s", s->len, s->buffer);
241 case TRACE_SEQ__BUFFER_POISONED: 241 case TRACE_SEQ__BUFFER_POISONED:
242 puts("Usage of trace_seq after it was destroyed"); 242 fprintf(fp, "%s\n", "Usage of trace_seq after it was destroyed");
243 break; 243 break;
244 case TRACE_SEQ__MEM_ALLOC_FAILED: 244 case TRACE_SEQ__MEM_ALLOC_FAILED:
245 puts("Can't allocate trace_seq buffer memory"); 245 fprintf(fp, "%s\n", "Can't allocate trace_seq buffer memory");
246 break; 246 break;
247 } 247 }
248 return -1; 248 return -1;
249} 249}
250
251int trace_seq_do_printf(struct trace_seq *s)
252{
253 return trace_seq_do_fprintf(s, stdout);
254}
diff --git a/tools/perf/.gitignore b/tools/perf/.gitignore
index 40399c3d97d6..812f904193e8 100644
--- a/tools/perf/.gitignore
+++ b/tools/perf/.gitignore
@@ -1,6 +1,7 @@
1PERF-CFLAGS 1PERF-CFLAGS
2PERF-GUI-VARS 2PERF-GUI-VARS
3PERF-VERSION-FILE 3PERF-VERSION-FILE
4FEATURE-DUMP
4perf 5perf
5perf-read-vdso32 6perf-read-vdso32
6perf-read-vdsox32 7perf-read-vdsox32
diff --git a/tools/perf/Build b/tools/perf/Build
new file mode 100644
index 000000000000..b77370ef7005
--- /dev/null
+++ b/tools/perf/Build
@@ -0,0 +1,44 @@
1perf-y += builtin-bench.o
2perf-y += builtin-annotate.o
3perf-y += builtin-diff.o
4perf-y += builtin-evlist.o
5perf-y += builtin-help.o
6perf-y += builtin-sched.o
7perf-y += builtin-buildid-list.o
8perf-y += builtin-buildid-cache.o
9perf-y += builtin-list.o
10perf-y += builtin-record.o
11perf-y += builtin-report.o
12perf-y += builtin-stat.o
13perf-y += builtin-timechart.o
14perf-y += builtin-top.o
15perf-y += builtin-script.o
16perf-y += builtin-kmem.o
17perf-y += builtin-lock.o
18perf-y += builtin-kvm.o
19perf-y += builtin-inject.o
20perf-y += builtin-mem.o
21perf-y += builtin-data.o
22
23perf-$(CONFIG_AUDIT) += builtin-trace.o
24perf-$(CONFIG_LIBELF) += builtin-probe.o
25
26perf-y += bench/
27perf-y += tests/
28
29perf-y += perf.o
30
31paths += -DPERF_HTML_PATH="BUILD_STR($(htmldir_SQ))"
32paths += -DPERF_INFO_PATH="BUILD_STR($(infodir_SQ))"
33paths += -DPERF_MAN_PATH="BUILD_STR($(mandir_SQ))"
34
35CFLAGS_builtin-help.o += $(paths)
36CFLAGS_builtin-timechart.o += $(paths)
37CFLAGS_perf.o += -DPERF_HTML_PATH="BUILD_STR($(htmldir_SQ))" -include $(OUTPUT)PERF-VERSION-FILE
38
39libperf-y += util/
40libperf-y += arch/
41libperf-y += ui/
42libperf-y += scripts/
43
44gtk-y += ui/gtk/
diff --git a/tools/perf/Documentation/Build.txt b/tools/perf/Documentation/Build.txt
new file mode 100644
index 000000000000..f6fc6507ba55
--- /dev/null
+++ b/tools/perf/Documentation/Build.txt
@@ -0,0 +1,49 @@
1
21) perf build
3=============
4The perf build process consists of several separated building blocks,
5which are linked together to form the perf binary:
6 - libperf library (static)
7 - perf builtin commands
8 - traceevent library (static)
9 - GTK ui library
10
11Several makefiles govern the perf build:
12
13 - Makefile
14 top level Makefile working as a wrapper that calls the main
15 Makefile.perf with a -j option to do parallel builds.
16
17 - Makefile.perf
18 main makefile that triggers build of all perf objects including
19 installation and documentation processing.
20
21 - tools/build/Makefile.build
22 main makefile of the build framework
23
24 - tools/build/Build.include
25 build framework generic definitions
26
27 - Build makefiles
28 makefiles that defines build objects
29
30Please refer to tools/build/Documentation/Build.txt for more
31information about build framework.
32
33
342) perf build
35=============
36The Makefile.perf triggers the build framework for build objects:
37 perf, libperf, gtk
38
39resulting in following objects:
40 $ ls *-in.o
41 gtk-in.o libperf-in.o perf-in.o
42
43Those objects are then used in final linking:
44 libperf-gtk.so <- gtk-in.o libperf-in.o
45 perf <- perf-in.o libperf-in.o
46
47
48NOTE this description is omitting other libraries involved, only
49 focusing on build framework outcomes
diff --git a/tools/perf/Documentation/perf-buildid-cache.txt b/tools/perf/Documentation/perf-buildid-cache.txt
index 0294c57b1f5e..dd07b55f58d8 100644
--- a/tools/perf/Documentation/perf-buildid-cache.txt
+++ b/tools/perf/Documentation/perf-buildid-cache.txt
@@ -12,9 +12,9 @@ SYNOPSIS
12 12
13DESCRIPTION 13DESCRIPTION
14----------- 14-----------
15This command manages the build-id cache. It can add and remove files to/from 15This command manages the build-id cache. It can add, remove, update and purge
16the cache. In the future it should as well purge older entries, set upper 16files to/from the cache. In the future it should as well set upper limits for
17limits for the space used by the cache, etc. 17the space used by the cache, etc.
18 18
19OPTIONS 19OPTIONS
20------- 20-------
@@ -36,14 +36,24 @@ OPTIONS
36 actually made. 36 actually made.
37-r:: 37-r::
38--remove=:: 38--remove=::
39 Remove specified file from the cache. 39 Remove a cached binary which has same build-id of specified file
40 from the cache.
41-p::
42--purge=::
43 Purge all cached binaries including older caches which have specified
44 path from the cache.
40-M:: 45-M::
41--missing=:: 46--missing=::
42 List missing build ids in the cache for the specified file. 47 List missing build ids in the cache for the specified file.
43-u:: 48-u::
44--update:: 49--update=::
45 Update specified file of the cache. It can be used to update kallsyms 50 Update specified file of the cache. Note that this doesn't remove
46 kernel dso to vmlinux in order to support annotation. 51 older entires since those may be still needed for annotating old
52 (or remote) perf.data. Only if there is already a cache which has
53 exactly same build-id, that is replaced by new one. It can be used
54 to update kallsyms and kernel dso to vmlinux in order to support
55 annotation.
56
47-v:: 57-v::
48--verbose:: 58--verbose::
49 Be more verbose. 59 Be more verbose.
diff --git a/tools/perf/Documentation/perf-data.txt b/tools/perf/Documentation/perf-data.txt
new file mode 100644
index 000000000000..be8fa1a0a97e
--- /dev/null
+++ b/tools/perf/Documentation/perf-data.txt
@@ -0,0 +1,40 @@
1perf-data(1)
2==============
3
4NAME
5----
6perf-data - Data file related processing
7
8SYNOPSIS
9--------
10[verse]
11'perf data' [<common options>] <command> [<options>]",
12
13DESCRIPTION
14-----------
15Data file related processing.
16
17COMMANDS
18--------
19convert::
20 Converts perf data file into another format (only CTF [1] format is support by now).
21 It's possible to set data-convert debug variable to get debug messages from conversion,
22 like:
23 perf --debug data-convert data convert ...
24
25OPTIONS for 'convert'
26---------------------
27--to-ctf::
28 Triggers the CTF conversion, specify the path of CTF data directory.
29
30-i::
31 Specify input perf data file path.
32
33-v::
34--verbose::
35 Be more verbose (show counter open errors, etc).
36
37SEE ALSO
38--------
39linkperf:perf[1]
40[1] Common Trace Format - http://www.efficios.com/ctf
diff --git a/tools/perf/Documentation/perf-diff.txt b/tools/perf/Documentation/perf-diff.txt
index e463caa3eb49..d1deb573877f 100644
--- a/tools/perf/Documentation/perf-diff.txt
+++ b/tools/perf/Documentation/perf-diff.txt
@@ -20,12 +20,20 @@ If no parameters are passed it will assume perf.data.old and perf.data.
20The differential profile is displayed only for events matching both 20The differential profile is displayed only for events matching both
21specified perf.data files. 21specified perf.data files.
22 22
23If no parameters are passed the samples will be sorted by dso and symbol.
24As the perf.data files could come from different binaries, the symbols addresses
25could vary. So perf diff is based on the comparison of the files and
26symbols name.
27
23OPTIONS 28OPTIONS
24------- 29-------
25-D:: 30-D::
26--dump-raw-trace:: 31--dump-raw-trace::
27 Dump raw trace in ASCII. 32 Dump raw trace in ASCII.
28 33
34--kallsyms=<file>::
35 kallsyms pathname
36
29-m:: 37-m::
30--modules:: 38--modules::
31 Load module symbols. WARNING: use only with -k and LIVE kernel 39 Load module symbols. WARNING: use only with -k and LIVE kernel
diff --git a/tools/perf/Documentation/perf-kmem.txt b/tools/perf/Documentation/perf-kmem.txt
index 7c8fbbf3f61c..150253cc3c97 100644
--- a/tools/perf/Documentation/perf-kmem.txt
+++ b/tools/perf/Documentation/perf-kmem.txt
@@ -25,6 +25,10 @@ OPTIONS
25--input=<file>:: 25--input=<file>::
26 Select the input file (default: perf.data unless stdin is a fifo) 26 Select the input file (default: perf.data unless stdin is a fifo)
27 27
28-v::
29--verbose::
30 Be more verbose. (show symbol address, etc)
31
28--caller:: 32--caller::
29 Show per-callsite statistics 33 Show per-callsite statistics
30 34
diff --git a/tools/perf/Documentation/perf-list.txt b/tools/perf/Documentation/perf-list.txt
index 3e2aec94f806..bada8933fdd4 100644
--- a/tools/perf/Documentation/perf-list.txt
+++ b/tools/perf/Documentation/perf-list.txt
@@ -26,6 +26,7 @@ counted. The following modifiers exist:
26 u - user-space counting 26 u - user-space counting
27 k - kernel counting 27 k - kernel counting
28 h - hypervisor counting 28 h - hypervisor counting
29 I - non idle counting
29 G - guest counting (in KVM guests) 30 G - guest counting (in KVM guests)
30 H - host counting (not in KVM guests) 31 H - host counting (not in KVM guests)
31 p - precise level 32 p - precise level
@@ -127,6 +128,12 @@ To limit the list use:
127One or more types can be used at the same time, listing the events for the 128One or more types can be used at the same time, listing the events for the
128types specified. 129types specified.
129 130
131Support raw format:
132
133. '--raw-dump', shows the raw-dump of all the events.
134. '--raw-dump [hw|sw|cache|tracepoint|pmu|event_glob]', shows the raw-dump of
135 a certain kind of events.
136
130SEE ALSO 137SEE ALSO
131-------- 138--------
132linkperf:perf-stat[1], linkperf:perf-top[1], 139linkperf:perf-stat[1], linkperf:perf-top[1],
diff --git a/tools/perf/Documentation/perf-probe.txt b/tools/perf/Documentation/perf-probe.txt
index aaa869be3dc1..239609c09f83 100644
--- a/tools/perf/Documentation/perf-probe.txt
+++ b/tools/perf/Documentation/perf-probe.txt
@@ -47,6 +47,12 @@ OPTIONS
47-v:: 47-v::
48--verbose:: 48--verbose::
49 Be more verbose (show parsed arguments, etc). 49 Be more verbose (show parsed arguments, etc).
50 Can not use with -q.
51
52-q::
53--quiet::
54 Be quiet (do not show any messages including errors).
55 Can not use with -v.
50 56
51-a:: 57-a::
52--add=:: 58--add=::
@@ -96,7 +102,7 @@ OPTIONS
96 Dry run. With this option, --add and --del doesn't execute actual 102 Dry run. With this option, --add and --del doesn't execute actual
97 adding and removal operations. 103 adding and removal operations.
98 104
99--max-probes:: 105--max-probes=NUM::
100 Set the maximum number of probe points for an event. Default is 128. 106 Set the maximum number of probe points for an event. Default is 128.
101 107
102-x:: 108-x::
@@ -104,8 +110,13 @@ OPTIONS
104 Specify path to the executable or shared library file for user 110 Specify path to the executable or shared library file for user
105 space tracing. Can also be used with --funcs option. 111 space tracing. Can also be used with --funcs option.
106 112
113--demangle::
114 Demangle application symbols. --no-demangle is also available
115 for disabling demangling.
116
107--demangle-kernel:: 117--demangle-kernel::
108 Demangle kernel symbols. 118 Demangle kernel symbols. --no-demangle-kernel is also available
119 for disabling kernel demangling.
109 120
110In absence of -m/-x options, perf probe checks if the first argument after 121In absence of -m/-x options, perf probe checks if the first argument after
111the options is an absolute path name. If its an absolute path, perf probe 122the options is an absolute path name. If its an absolute path, perf probe
@@ -137,6 +148,7 @@ Each probe argument follows below syntax.
137 [NAME=]LOCALVAR|$retval|%REG|@SYMBOL[:TYPE] 148 [NAME=]LOCALVAR|$retval|%REG|@SYMBOL[:TYPE]
138 149
139'NAME' specifies the name of this argument (optional). You can use the name of local variable, local data structure member (e.g. var->field, var.field2), local array with fixed index (e.g. array[1], var->array[0], var->pointer[2]), or kprobe-tracer argument format (e.g. $retval, %ax, etc). Note that the name of this argument will be set as the last member name if you specify a local data structure member (e.g. field2 for 'var->field1.field2'.) 150'NAME' specifies the name of this argument (optional). You can use the name of local variable, local data structure member (e.g. var->field, var.field2), local array with fixed index (e.g. array[1], var->array[0], var->pointer[2]), or kprobe-tracer argument format (e.g. $retval, %ax, etc). Note that the name of this argument will be set as the last member name if you specify a local data structure member (e.g. field2 for 'var->field1.field2'.)
151'$vars' special argument is also available for NAME, it is expanded to the local variables which can access at given probe point.
140'TYPE' casts the type of this argument (optional). If omitted, perf probe automatically set the type based on debuginfo. You can specify 'string' type only for the local variable or structure member which is an array of or a pointer to 'char' or 'unsigned char' type. 152'TYPE' casts the type of this argument (optional). If omitted, perf probe automatically set the type based on debuginfo. You can specify 'string' type only for the local variable or structure member which is an array of or a pointer to 'char' or 'unsigned char' type.
141 153
142On x86 systems %REG is always the short form of the register: for example %AX. %RAX or %EAX is not valid. 154On x86 systems %REG is always the short form of the register: for example %AX. %RAX or %EAX is not valid.
diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 31e977459c51..4847a793de65 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -55,6 +55,11 @@ OPTIONS
55 If you want to profile write accesses in [0x1000~1008), just set 55 If you want to profile write accesses in [0x1000~1008), just set
56 'mem:0x1000/8:w'. 56 'mem:0x1000/8:w'.
57 57
58 - a group of events surrounded by a pair of brace ("{event1,event2,...}").
59 Each event is separated by commas and the group should be quoted to
60 prevent the shell interpretation. You also need to use --group on
61 "perf report" to view group events together.
62
58--filter=<filter>:: 63--filter=<filter>::
59 Event filter. 64 Event filter.
60 65
@@ -62,9 +67,6 @@ OPTIONS
62--all-cpus:: 67--all-cpus::
63 System-wide collection from all CPUs. 68 System-wide collection from all CPUs.
64 69
65-l::
66 Scale counter values.
67
68-p:: 70-p::
69--pid=:: 71--pid=::
70 Record events on existing process ID (comma separated list). 72 Record events on existing process ID (comma separated list).
@@ -107,6 +109,10 @@ OPTIONS
107 specification with appended unit character - B/K/M/G. The 109 specification with appended unit character - B/K/M/G. The
108 size is rounded up to have nearest pages power of two value. 110 size is rounded up to have nearest pages power of two value.
109 111
112--group::
113 Put all events in a single event group. This precedes the --event
114 option and remains only for backward compatibility. See --event.
115
110-g:: 116-g::
111 Enables call-graph (stack chain/backtrace) recording. 117 Enables call-graph (stack chain/backtrace) recording.
112 118
@@ -115,13 +121,19 @@ OPTIONS
115 implies -g. 121 implies -g.
116 122
117 Allows specifying "fp" (frame pointer) or "dwarf" 123 Allows specifying "fp" (frame pointer) or "dwarf"
118 (DWARF's CFI - Call Frame Information) as the method to collect 124 (DWARF's CFI - Call Frame Information) or "lbr"
125 (Hardware Last Branch Record facility) as the method to collect
119 the information used to show the call graphs. 126 the information used to show the call graphs.
120 127
121 In some systems, where binaries are build with gcc 128 In some systems, where binaries are build with gcc
122 --fomit-frame-pointer, using the "fp" method will produce bogus 129 --fomit-frame-pointer, using the "fp" method will produce bogus
123 call graphs, using "dwarf", if available (perf tools linked to 130 call graphs, using "dwarf", if available (perf tools linked to
124 the libunwind library) should be used instead. 131 the libunwind library) should be used instead.
132 Using the "lbr" method doesn't require any compiler options. It
133 will produce call graphs from the hardware LBR registers. The
134 main limition is that it is only available on new Intel
135 platforms, such as Haswell. It can only get user call chain. It
136 doesn't work with branch stack sampling at the same time.
125 137
126-q:: 138-q::
127--quiet:: 139--quiet::
@@ -235,6 +247,16 @@ Capture machine state (registers) at interrupt, i.e., on counter overflows for
235each sample. List of captured registers depends on the architecture. This option 247each sample. List of captured registers depends on the architecture. This option
236is off by default. 248is off by default.
237 249
250--running-time::
251Record running and enabled time for read events (:S)
252
253-k::
254--clockid::
255Sets the clock id to use for the various time fields in the perf_event_type
256records. See clock_gettime(). In particular CLOCK_MONOTONIC and
257CLOCK_MONOTONIC_RAW are supported, some events might also allow
258CLOCK_BOOTTIME, CLOCK_REALTIME and CLOCK_TAI.
259
238SEE ALSO 260SEE ALSO
239-------- 261--------
240linkperf:perf-stat[1], linkperf:perf-list[1] 262linkperf:perf-stat[1], linkperf:perf-list[1]
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
index dd7cccdde498..4879cf638824 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -40,6 +40,11 @@ OPTIONS
40 Only consider symbols in these comms. CSV that understands 40 Only consider symbols in these comms. CSV that understands
41 file://filename entries. This option will affect the percentage of 41 file://filename entries. This option will affect the percentage of
42 the overhead column. See --percentage for more info. 42 the overhead column. See --percentage for more info.
43--pid=::
44 Only show events for given process ID (comma separated list).
45
46--tid=::
47 Only show events for given thread ID (comma separated list).
43-d:: 48-d::
44--dsos=:: 49--dsos=::
45 Only consider symbols in these dsos. CSV that understands 50 Only consider symbols in these dsos. CSV that understands
diff --git a/tools/perf/Documentation/perf-script.txt b/tools/perf/Documentation/perf-script.txt
index a21eec05bc42..79445750fcb3 100644
--- a/tools/perf/Documentation/perf-script.txt
+++ b/tools/perf/Documentation/perf-script.txt
@@ -193,6 +193,12 @@ OPTIONS
193 Only display events for these comms. CSV that understands 193 Only display events for these comms. CSV that understands
194 file://filename entries. 194 file://filename entries.
195 195
196--pid=::
197 Only show events for given process ID (comma separated list).
198
199--tid=::
200 Only show events for given thread ID (comma separated list).
201
196-I:: 202-I::
197--show-info:: 203--show-info::
198 Display extended information about the perf.data file. This adds 204 Display extended information about the perf.data file. This adds
diff --git a/tools/perf/Documentation/perf-trace.txt b/tools/perf/Documentation/perf-trace.txt
index 7e1b1f2bb83c..ba03fd5d1a54 100644
--- a/tools/perf/Documentation/perf-trace.txt
+++ b/tools/perf/Documentation/perf-trace.txt
@@ -55,6 +55,9 @@ OPTIONS
55--uid=:: 55--uid=::
56 Record events in threads owned by uid. Name or number. 56 Record events in threads owned by uid. Name or number.
57 57
58--filter-pids=::
59 Filter out events for these pids and for 'trace' itself (comma separated list).
60
58-v:: 61-v::
59--verbose=:: 62--verbose=::
60 Verbosity level. 63 Verbosity level.
@@ -115,6 +118,9 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
115--syscalls:: 118--syscalls::
116 Trace system calls. This options is enabled by default. 119 Trace system calls. This options is enabled by default.
117 120
121--event::
122 Trace other events, see 'perf list' for a complete list.
123
118PAGEFAULTS 124PAGEFAULTS
119---------- 125----------
120 126
diff --git a/tools/perf/Documentation/perf.txt b/tools/perf/Documentation/perf.txt
index 1e8e400b4493..2b131776363e 100644
--- a/tools/perf/Documentation/perf.txt
+++ b/tools/perf/Documentation/perf.txt
@@ -13,11 +13,16 @@ SYNOPSIS
13OPTIONS 13OPTIONS
14------- 14-------
15--debug:: 15--debug::
16 Setup debug variable (just verbose for now) in value 16 Setup debug variable (see list below) in value
17 range (0, 10). Use like: 17 range (0, 10). Use like:
18 --debug verbose # sets verbose = 1 18 --debug verbose # sets verbose = 1
19 --debug verbose=2 # sets verbose = 2 19 --debug verbose=2 # sets verbose = 2
20 20
21 List of debug variables allowed to set:
22 verbose - general debug messages
23 ordered-events - ordered events object debug messages
24 data-convert - data convert command debug messages
25
21--buildid-dir:: 26--buildid-dir::
22 Setup buildid cache directory. It has higher priority than 27 Setup buildid cache directory. It has higher priority than
23 buildid.dir config file option. 28 buildid.dir config file option.
diff --git a/tools/perf/MANIFEST b/tools/perf/MANIFEST
index fbbfdc39271d..11ccbb22ea2b 100644
--- a/tools/perf/MANIFEST
+++ b/tools/perf/MANIFEST
@@ -1,5 +1,6 @@
1tools/perf 1tools/perf
2tools/scripts 2tools/scripts
3tools/build
3tools/lib/traceevent 4tools/lib/traceevent
4tools/lib/api 5tools/lib/api
5tools/lib/symbol/kallsyms.c 6tools/lib/symbol/kallsyms.c
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index cb2e5868c8e8..c699dc35eef9 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -24,8 +24,8 @@ unexport MAKEFLAGS
24# (To override it, run 'make JOBS=1' and similar.) 24# (To override it, run 'make JOBS=1' and similar.)
25# 25#
26ifeq ($(JOBS),) 26ifeq ($(JOBS),)
27 JOBS := $(shell grep -c ^processor /proc/cpuinfo 2>/dev/null) 27 JOBS := $(shell egrep -c '^processor|^CPU' /proc/cpuinfo 2>/dev/null)
28 ifeq ($(JOBS),) 28 ifeq ($(JOBS),0)
29 JOBS := 1 29 JOBS := 1
30 endif 30 endif
31endif 31endif
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index aa6a50447c32..c43a20517591 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -68,7 +68,11 @@ include config/utilities.mak
68# for reading the x32 mode 32-bit compatibility VDSO in 64-bit mode 68# for reading the x32 mode 32-bit compatibility VDSO in 64-bit mode
69# 69#
70# Define NO_ZLIB if you do not want to support compressed kernel modules 70# Define NO_ZLIB if you do not want to support compressed kernel modules
71 71#
72# Define LIBBABELTRACE if you DO want libbabeltrace support
73# for CTF data format.
74#
75# Define NO_LZMA if you do not want to support compressed (xz) kernel modules
72 76
73ifeq ($(srctree),) 77ifeq ($(srctree),)
74srctree := $(patsubst %/,%,$(dir $(shell pwd))) 78srctree := $(patsubst %/,%,$(dir $(shell pwd)))
@@ -82,13 +86,29 @@ endif
82 86
83ifneq ($(OUTPUT),) 87ifneq ($(OUTPUT),)
84#$(info Determined 'OUTPUT' to be $(OUTPUT)) 88#$(info Determined 'OUTPUT' to be $(OUTPUT))
89# Adding $(OUTPUT) as a directory to look for source files,
90# because use generated output files as sources dependency
91# for flex/bison parsers.
92VPATH += $(OUTPUT)
93export VPATH
85endif 94endif
86 95
96ifeq ($(V),1)
97 Q =
98else
99 Q = @
100endif
101
102# Do not use make's built-in rules
103# (this improves performance and avoids hard-to-debug behaviour);
104MAKEFLAGS += -r
105
87$(OUTPUT)PERF-VERSION-FILE: ../../.git/HEAD 106$(OUTPUT)PERF-VERSION-FILE: ../../.git/HEAD
88 @$(SHELL_PATH) util/PERF-VERSION-GEN $(OUTPUT) 107 $(Q)$(SHELL_PATH) util/PERF-VERSION-GEN $(OUTPUT)
89 @touch $(OUTPUT)PERF-VERSION-FILE 108 $(Q)touch $(OUTPUT)PERF-VERSION-FILE
90 109
91CC = $(CROSS_COMPILE)gcc 110CC = $(CROSS_COMPILE)gcc
111LD = $(CROSS_COMPILE)ld
92AR = $(CROSS_COMPILE)ar 112AR = $(CROSS_COMPILE)ar
93PKG_CONFIG = $(CROSS_COMPILE)pkg-config 113PKG_CONFIG = $(CROSS_COMPILE)pkg-config
94 114
@@ -127,10 +147,6 @@ export prefix bindir sharedir sysconfdir DESTDIR
127SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__ 147SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
128 148
129# Guard against environment variables 149# Guard against environment variables
130BUILTIN_OBJS =
131LIB_H =
132LIB_OBJS =
133GTK_OBJS =
134PYRF_OBJS = 150PYRF_OBJS =
135SCRIPT_SH = 151SCRIPT_SH =
136 152
@@ -155,8 +171,8 @@ endif
155LIBTRACEEVENT = $(TE_PATH)libtraceevent.a 171LIBTRACEEVENT = $(TE_PATH)libtraceevent.a
156export LIBTRACEEVENT 172export LIBTRACEEVENT
157 173
158LIBAPIKFS = $(LIB_PATH)libapikfs.a 174LIBAPI = $(LIB_PATH)libapi.a
159export LIBAPIKFS 175export LIBAPI
160 176
161# python extension build directories 177# python extension build directories
162PYTHON_EXTBUILD := $(OUTPUT)python_ext_build/ 178PYTHON_EXTBUILD := $(OUTPUT)python_ext_build/
@@ -167,7 +183,7 @@ export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP
167python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf.so 183python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf.so
168 184
169PYTHON_EXT_SRCS := $(shell grep -v ^\# util/python-ext-sources) 185PYTHON_EXT_SRCS := $(shell grep -v ^\# util/python-ext-sources)
170PYTHON_EXT_DEPS := util/python-ext-sources util/setup.py $(LIBTRACEEVENT) $(LIBAPIKFS) 186PYTHON_EXT_DEPS := util/python-ext-sources util/setup.py $(LIBTRACEEVENT) $(LIBAPI)
171 187
172$(OUTPUT)python/perf.so: $(PYTHON_EXT_SRCS) $(PYTHON_EXT_DEPS) 188$(OUTPUT)python/perf.so: $(PYTHON_EXT_SRCS) $(PYTHON_EXT_DEPS)
173 $(QUIET_GEN)CFLAGS='$(CFLAGS)' $(PYTHON_WORD) util/setup.py \ 189 $(QUIET_GEN)CFLAGS='$(CFLAGS)' $(PYTHON_WORD) util/setup.py \
@@ -206,297 +222,9 @@ endif
206 222
207export PERL_PATH 223export PERL_PATH
208 224
209$(OUTPUT)util/parse-events-flex.c: util/parse-events.l $(OUTPUT)util/parse-events-bison.c
210 $(QUIET_FLEX)$(FLEX) -o $@ --header-file=$(OUTPUT)util/parse-events-flex.h $(PARSER_DEBUG_FLEX) util/parse-events.l
211
212$(OUTPUT)util/parse-events-bison.c: util/parse-events.y
213 $(QUIET_BISON)$(BISON) -v util/parse-events.y -d $(PARSER_DEBUG_BISON) -o $(OUTPUT)util/parse-events-bison.c -p parse_events_
214
215$(OUTPUT)util/pmu-flex.c: util/pmu.l $(OUTPUT)util/pmu-bison.c
216 $(QUIET_FLEX)$(FLEX) -o $@ --header-file=$(OUTPUT)util/pmu-flex.h util/pmu.l
217
218$(OUTPUT)util/pmu-bison.c: util/pmu.y
219 $(QUIET_BISON)$(BISON) -v util/pmu.y -d -o $(OUTPUT)util/pmu-bison.c -p perf_pmu_
220
221$(OUTPUT)util/parse-events.o: $(OUTPUT)util/parse-events-flex.c $(OUTPUT)util/parse-events-bison.c
222$(OUTPUT)util/pmu.o: $(OUTPUT)util/pmu-flex.c $(OUTPUT)util/pmu-bison.c
223
224LIB_FILE=$(OUTPUT)libperf.a 225LIB_FILE=$(OUTPUT)libperf.a
225 226
226LIB_H += ../lib/symbol/kallsyms.h 227PERFLIBS = $(LIB_FILE) $(LIBAPI) $(LIBTRACEEVENT)
227LIB_H += ../../include/uapi/linux/perf_event.h
228LIB_H += ../../include/linux/rbtree.h
229LIB_H += ../../include/linux/list.h
230LIB_H += ../../include/uapi/linux/const.h
231LIB_H += ../include/linux/hash.h
232LIB_H += ../../include/linux/stringify.h
233LIB_H += util/include/linux/bitmap.h
234LIB_H += ../include/linux/bitops.h
235LIB_H += ../include/asm-generic/bitops/arch_hweight.h
236LIB_H += ../include/asm-generic/bitops/atomic.h
237LIB_H += ../include/asm-generic/bitops/const_hweight.h
238LIB_H += ../include/asm-generic/bitops/find.h
239LIB_H += ../include/asm-generic/bitops/fls64.h
240LIB_H += ../include/asm-generic/bitops/fls.h
241LIB_H += ../include/asm-generic/bitops/__ffs.h
242LIB_H += ../include/asm-generic/bitops/__fls.h
243LIB_H += ../include/asm-generic/bitops/hweight.h
244LIB_H += ../include/asm-generic/bitops.h
245LIB_H += ../include/linux/compiler.h
246LIB_H += ../include/linux/log2.h
247LIB_H += util/include/linux/const.h
248LIB_H += util/include/linux/ctype.h
249LIB_H += util/include/linux/kernel.h
250LIB_H += util/include/linux/list.h
251LIB_H += ../include/linux/export.h
252LIB_H += util/include/linux/poison.h
253LIB_H += util/include/linux/rbtree.h
254LIB_H += util/include/linux/rbtree_augmented.h
255LIB_H += util/include/linux/string.h
256LIB_H += ../include/linux/types.h
257LIB_H += util/include/linux/linkage.h
258LIB_H += util/include/asm/asm-offsets.h
259LIB_H += ../include/asm/bug.h
260LIB_H += util/include/asm/byteorder.h
261LIB_H += util/include/asm/swab.h
262LIB_H += util/include/asm/system.h
263LIB_H += util/include/asm/uaccess.h
264LIB_H += util/include/dwarf-regs.h
265LIB_H += util/include/asm/dwarf2.h
266LIB_H += util/include/asm/cpufeature.h
267LIB_H += util/include/asm/unistd_32.h
268LIB_H += util/include/asm/unistd_64.h
269LIB_H += perf.h
270LIB_H += util/annotate.h
271LIB_H += util/cache.h
272LIB_H += util/callchain.h
273LIB_H += util/build-id.h
274LIB_H += util/db-export.h
275LIB_H += util/debug.h
276LIB_H += util/pmu.h
277LIB_H += util/event.h
278LIB_H += util/evsel.h
279LIB_H += util/evlist.h
280LIB_H += util/exec_cmd.h
281LIB_H += util/find-vdso-map.c
282LIB_H += util/levenshtein.h
283LIB_H += util/machine.h
284LIB_H += util/map.h
285LIB_H += util/parse-options.h
286LIB_H += util/parse-events.h
287LIB_H += util/quote.h
288LIB_H += util/util.h
289LIB_H += util/xyarray.h
290LIB_H += util/header.h
291LIB_H += util/help.h
292LIB_H += util/session.h
293LIB_H += util/ordered-events.h
294LIB_H += util/strbuf.h
295LIB_H += util/strlist.h
296LIB_H += util/strfilter.h
297LIB_H += util/svghelper.h
298LIB_H += util/tool.h
299LIB_H += util/run-command.h
300LIB_H += util/sigchain.h
301LIB_H += util/dso.h
302LIB_H += util/symbol.h
303LIB_H += util/color.h
304LIB_H += util/values.h
305LIB_H += util/sort.h
306LIB_H += util/hist.h
307LIB_H += util/comm.h
308LIB_H += util/thread.h
309LIB_H += util/thread_map.h
310LIB_H += util/trace-event.h
311LIB_H += util/probe-finder.h
312LIB_H += util/dwarf-aux.h
313LIB_H += util/probe-event.h
314LIB_H += util/pstack.h
315LIB_H += util/cpumap.h
316LIB_H += util/top.h
317LIB_H += $(ARCH_INCLUDE)
318LIB_H += util/cgroup.h
319LIB_H += $(LIB_INCLUDE)traceevent/event-parse.h
320LIB_H += util/target.h
321LIB_H += util/rblist.h
322LIB_H += util/intlist.h
323LIB_H += util/perf_regs.h
324LIB_H += util/unwind.h
325LIB_H += util/vdso.h
326LIB_H += util/tsc.h
327LIB_H += ui/helpline.h
328LIB_H += ui/progress.h
329LIB_H += ui/util.h
330LIB_H += ui/ui.h
331LIB_H += util/data.h
332LIB_H += util/kvm-stat.h
333LIB_H += util/thread-stack.h
334
335LIB_OBJS += $(OUTPUT)util/abspath.o
336LIB_OBJS += $(OUTPUT)util/alias.o
337LIB_OBJS += $(OUTPUT)util/annotate.o
338LIB_OBJS += $(OUTPUT)util/build-id.o
339LIB_OBJS += $(OUTPUT)util/config.o
340LIB_OBJS += $(OUTPUT)util/ctype.o
341LIB_OBJS += $(OUTPUT)util/db-export.o
342LIB_OBJS += $(OUTPUT)util/pmu.o
343LIB_OBJS += $(OUTPUT)util/environment.o
344LIB_OBJS += $(OUTPUT)util/event.o
345LIB_OBJS += $(OUTPUT)util/evlist.o
346LIB_OBJS += $(OUTPUT)util/evsel.o
347LIB_OBJS += $(OUTPUT)util/exec_cmd.o
348LIB_OBJS += $(OUTPUT)util/find_next_bit.o
349LIB_OBJS += $(OUTPUT)util/help.o
350LIB_OBJS += $(OUTPUT)util/kallsyms.o
351LIB_OBJS += $(OUTPUT)util/levenshtein.o
352LIB_OBJS += $(OUTPUT)util/parse-options.o
353LIB_OBJS += $(OUTPUT)util/parse-events.o
354LIB_OBJS += $(OUTPUT)util/path.o
355LIB_OBJS += $(OUTPUT)util/rbtree.o
356LIB_OBJS += $(OUTPUT)util/bitmap.o
357LIB_OBJS += $(OUTPUT)util/hweight.o
358LIB_OBJS += $(OUTPUT)util/run-command.o
359LIB_OBJS += $(OUTPUT)util/quote.o
360LIB_OBJS += $(OUTPUT)util/strbuf.o
361LIB_OBJS += $(OUTPUT)util/string.o
362LIB_OBJS += $(OUTPUT)util/strlist.o
363LIB_OBJS += $(OUTPUT)util/strfilter.o
364LIB_OBJS += $(OUTPUT)util/top.o
365LIB_OBJS += $(OUTPUT)util/usage.o
366LIB_OBJS += $(OUTPUT)util/wrapper.o
367LIB_OBJS += $(OUTPUT)util/sigchain.o
368LIB_OBJS += $(OUTPUT)util/dso.o
369LIB_OBJS += $(OUTPUT)util/symbol.o
370LIB_OBJS += $(OUTPUT)util/symbol-elf.o
371LIB_OBJS += $(OUTPUT)util/color.o
372LIB_OBJS += $(OUTPUT)util/pager.o
373LIB_OBJS += $(OUTPUT)util/header.o
374LIB_OBJS += $(OUTPUT)util/callchain.o
375LIB_OBJS += $(OUTPUT)util/values.o
376LIB_OBJS += $(OUTPUT)util/debug.o
377LIB_OBJS += $(OUTPUT)util/machine.o
378LIB_OBJS += $(OUTPUT)util/map.o
379LIB_OBJS += $(OUTPUT)util/pstack.o
380LIB_OBJS += $(OUTPUT)util/session.o
381LIB_OBJS += $(OUTPUT)util/ordered-events.o
382LIB_OBJS += $(OUTPUT)util/comm.o
383LIB_OBJS += $(OUTPUT)util/thread.o
384LIB_OBJS += $(OUTPUT)util/thread_map.o
385LIB_OBJS += $(OUTPUT)util/trace-event-parse.o
386LIB_OBJS += $(OUTPUT)util/parse-events-flex.o
387LIB_OBJS += $(OUTPUT)util/parse-events-bison.o
388LIB_OBJS += $(OUTPUT)util/pmu-flex.o
389LIB_OBJS += $(OUTPUT)util/pmu-bison.o
390LIB_OBJS += $(OUTPUT)util/trace-event-read.o
391LIB_OBJS += $(OUTPUT)util/trace-event-info.o
392LIB_OBJS += $(OUTPUT)util/trace-event-scripting.o
393LIB_OBJS += $(OUTPUT)util/trace-event.o
394LIB_OBJS += $(OUTPUT)util/svghelper.o
395LIB_OBJS += $(OUTPUT)util/sort.o
396LIB_OBJS += $(OUTPUT)util/hist.o
397LIB_OBJS += $(OUTPUT)util/probe-event.o
398LIB_OBJS += $(OUTPUT)util/util.o
399LIB_OBJS += $(OUTPUT)util/xyarray.o
400LIB_OBJS += $(OUTPUT)util/cpumap.o
401LIB_OBJS += $(OUTPUT)util/cgroup.o
402LIB_OBJS += $(OUTPUT)util/target.o
403LIB_OBJS += $(OUTPUT)util/rblist.o
404LIB_OBJS += $(OUTPUT)util/intlist.o
405LIB_OBJS += $(OUTPUT)util/vdso.o
406LIB_OBJS += $(OUTPUT)util/stat.o
407LIB_OBJS += $(OUTPUT)util/record.o
408LIB_OBJS += $(OUTPUT)util/srcline.o
409LIB_OBJS += $(OUTPUT)util/data.o
410LIB_OBJS += $(OUTPUT)util/tsc.o
411LIB_OBJS += $(OUTPUT)util/cloexec.o
412LIB_OBJS += $(OUTPUT)util/thread-stack.o
413
414LIB_OBJS += $(OUTPUT)ui/setup.o
415LIB_OBJS += $(OUTPUT)ui/helpline.o
416LIB_OBJS += $(OUTPUT)ui/progress.o
417LIB_OBJS += $(OUTPUT)ui/util.o
418LIB_OBJS += $(OUTPUT)ui/hist.o
419LIB_OBJS += $(OUTPUT)ui/stdio/hist.o
420
421LIB_OBJS += $(OUTPUT)arch/common.o
422
423LIB_OBJS += $(OUTPUT)tests/parse-events.o
424LIB_OBJS += $(OUTPUT)tests/dso-data.o
425LIB_OBJS += $(OUTPUT)tests/attr.o
426LIB_OBJS += $(OUTPUT)tests/vmlinux-kallsyms.o
427LIB_OBJS += $(OUTPUT)tests/open-syscall.o
428LIB_OBJS += $(OUTPUT)tests/open-syscall-all-cpus.o
429LIB_OBJS += $(OUTPUT)tests/open-syscall-tp-fields.o
430LIB_OBJS += $(OUTPUT)tests/mmap-basic.o
431LIB_OBJS += $(OUTPUT)tests/perf-record.o
432LIB_OBJS += $(OUTPUT)tests/rdpmc.o
433LIB_OBJS += $(OUTPUT)tests/evsel-roundtrip-name.o
434LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o
435LIB_OBJS += $(OUTPUT)tests/fdarray.o
436LIB_OBJS += $(OUTPUT)tests/pmu.o
437LIB_OBJS += $(OUTPUT)tests/hists_common.o
438LIB_OBJS += $(OUTPUT)tests/hists_link.o
439LIB_OBJS += $(OUTPUT)tests/hists_filter.o
440LIB_OBJS += $(OUTPUT)tests/hists_output.o
441LIB_OBJS += $(OUTPUT)tests/hists_cumulate.o
442LIB_OBJS += $(OUTPUT)tests/python-use.o
443LIB_OBJS += $(OUTPUT)tests/bp_signal.o
444LIB_OBJS += $(OUTPUT)tests/bp_signal_overflow.o
445LIB_OBJS += $(OUTPUT)tests/task-exit.o
446LIB_OBJS += $(OUTPUT)tests/sw-clock.o
447ifeq ($(ARCH),x86)
448LIB_OBJS += $(OUTPUT)tests/perf-time-to-tsc.o
449endif
450LIB_OBJS += $(OUTPUT)tests/code-reading.o
451LIB_OBJS += $(OUTPUT)tests/sample-parsing.o
452LIB_OBJS += $(OUTPUT)tests/parse-no-sample-id-all.o
453ifndef NO_DWARF_UNWIND
454ifeq ($(ARCH),$(filter $(ARCH),x86 arm))
455LIB_OBJS += $(OUTPUT)tests/dwarf-unwind.o
456endif
457endif
458LIB_OBJS += $(OUTPUT)tests/mmap-thread-lookup.o
459LIB_OBJS += $(OUTPUT)tests/thread-mg-share.o
460LIB_OBJS += $(OUTPUT)tests/switch-tracking.o
461
462BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
463BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
464# Benchmark modules
465BUILTIN_OBJS += $(OUTPUT)bench/sched-messaging.o
466BUILTIN_OBJS += $(OUTPUT)bench/sched-pipe.o
467ifeq ($(ARCH), x86)
468ifeq ($(IS_64_BIT), 1)
469BUILTIN_OBJS += $(OUTPUT)bench/mem-memcpy-x86-64-asm.o
470BUILTIN_OBJS += $(OUTPUT)bench/mem-memset-x86-64-asm.o
471endif
472endif
473BUILTIN_OBJS += $(OUTPUT)bench/mem-memcpy.o
474BUILTIN_OBJS += $(OUTPUT)bench/futex-hash.o
475BUILTIN_OBJS += $(OUTPUT)bench/futex-wake.o
476BUILTIN_OBJS += $(OUTPUT)bench/futex-requeue.o
477
478BUILTIN_OBJS += $(OUTPUT)builtin-diff.o
479BUILTIN_OBJS += $(OUTPUT)builtin-evlist.o
480BUILTIN_OBJS += $(OUTPUT)builtin-help.o
481BUILTIN_OBJS += $(OUTPUT)builtin-sched.o
482BUILTIN_OBJS += $(OUTPUT)builtin-buildid-list.o
483BUILTIN_OBJS += $(OUTPUT)builtin-buildid-cache.o
484BUILTIN_OBJS += $(OUTPUT)builtin-list.o
485BUILTIN_OBJS += $(OUTPUT)builtin-record.o
486BUILTIN_OBJS += $(OUTPUT)builtin-report.o
487BUILTIN_OBJS += $(OUTPUT)builtin-stat.o
488BUILTIN_OBJS += $(OUTPUT)builtin-timechart.o
489BUILTIN_OBJS += $(OUTPUT)builtin-top.o
490BUILTIN_OBJS += $(OUTPUT)builtin-script.o
491BUILTIN_OBJS += $(OUTPUT)builtin-probe.o
492BUILTIN_OBJS += $(OUTPUT)builtin-kmem.o
493BUILTIN_OBJS += $(OUTPUT)builtin-lock.o
494BUILTIN_OBJS += $(OUTPUT)builtin-kvm.o
495BUILTIN_OBJS += $(OUTPUT)builtin-inject.o
496BUILTIN_OBJS += $(OUTPUT)tests/builtin-test.o
497BUILTIN_OBJS += $(OUTPUT)builtin-mem.o
498
499PERFLIBS = $(LIB_FILE) $(LIBAPIKFS) $(LIBTRACEEVENT)
500 228
501# We choose to avoid "if .. else if .. else .. endif endif" 229# We choose to avoid "if .. else if .. else .. endif endif"
502# because maintaining the nesting to match is a pain. If 230# because maintaining the nesting to match is a pain. If
@@ -508,67 +236,9 @@ ifneq ($(OUTPUT),)
508 CFLAGS += -I$(OUTPUT) 236 CFLAGS += -I$(OUTPUT)
509endif 237endif
510 238
511ifdef NO_LIBELF
512# Remove ELF/DWARF dependent codes
513LIB_OBJS := $(filter-out $(OUTPUT)util/symbol-elf.o,$(LIB_OBJS))
514LIB_OBJS := $(filter-out $(OUTPUT)util/dwarf-aux.o,$(LIB_OBJS))
515LIB_OBJS := $(filter-out $(OUTPUT)util/probe-event.o,$(LIB_OBJS))
516LIB_OBJS := $(filter-out $(OUTPUT)util/probe-finder.o,$(LIB_OBJS))
517
518BUILTIN_OBJS := $(filter-out $(OUTPUT)builtin-probe.o,$(BUILTIN_OBJS))
519
520# Use minimal symbol handling
521LIB_OBJS += $(OUTPUT)util/symbol-minimal.o
522
523else # NO_LIBELF
524ifndef NO_DWARF
525 LIB_OBJS += $(OUTPUT)util/probe-finder.o
526 LIB_OBJS += $(OUTPUT)util/dwarf-aux.o
527endif # NO_DWARF
528endif # NO_LIBELF
529
530ifndef NO_LIBDW_DWARF_UNWIND
531 LIB_OBJS += $(OUTPUT)util/unwind-libdw.o
532 LIB_H += util/unwind-libdw.h
533endif
534
535ifndef NO_LIBUNWIND
536 LIB_OBJS += $(OUTPUT)util/unwind-libunwind.o
537endif
538LIB_OBJS += $(OUTPUT)tests/keep-tracking.o
539
540ifndef NO_LIBAUDIT
541 BUILTIN_OBJS += $(OUTPUT)builtin-trace.o
542endif
543
544ifndef NO_SLANG
545 LIB_OBJS += $(OUTPUT)ui/browser.o
546 LIB_OBJS += $(OUTPUT)ui/browsers/annotate.o
547 LIB_OBJS += $(OUTPUT)ui/browsers/hists.o
548 LIB_OBJS += $(OUTPUT)ui/browsers/map.o
549 LIB_OBJS += $(OUTPUT)ui/browsers/scripts.o
550 LIB_OBJS += $(OUTPUT)ui/browsers/header.o
551 LIB_OBJS += $(OUTPUT)ui/tui/setup.o
552 LIB_OBJS += $(OUTPUT)ui/tui/util.o
553 LIB_OBJS += $(OUTPUT)ui/tui/helpline.o
554 LIB_OBJS += $(OUTPUT)ui/tui/progress.o
555 LIB_H += ui/tui/tui.h
556 LIB_H += ui/browser.h
557 LIB_H += ui/browsers/map.h
558 LIB_H += ui/keysyms.h
559 LIB_H += ui/libslang.h
560endif
561
562ifndef NO_GTK2 239ifndef NO_GTK2
563 ALL_PROGRAMS += $(OUTPUT)libperf-gtk.so 240 ALL_PROGRAMS += $(OUTPUT)libperf-gtk.so
564 241 GTK_IN := $(OUTPUT)gtk-in.o
565 GTK_OBJS += $(OUTPUT)ui/gtk/browser.o
566 GTK_OBJS += $(OUTPUT)ui/gtk/hists.o
567 GTK_OBJS += $(OUTPUT)ui/gtk/setup.o
568 GTK_OBJS += $(OUTPUT)ui/gtk/util.o
569 GTK_OBJS += $(OUTPUT)ui/gtk/helpline.o
570 GTK_OBJS += $(OUTPUT)ui/gtk/progress.o
571 GTK_OBJS += $(OUTPUT)ui/gtk/annotate.o
572 242
573install-gtk: $(OUTPUT)libperf-gtk.so 243install-gtk: $(OUTPUT)libperf-gtk.so
574 $(call QUIET_INSTALL, 'GTK UI') \ 244 $(call QUIET_INSTALL, 'GTK UI') \
@@ -576,31 +246,6 @@ install-gtk: $(OUTPUT)libperf-gtk.so
576 $(INSTALL) $(OUTPUT)libperf-gtk.so '$(DESTDIR_SQ)$(libdir_SQ)' 246 $(INSTALL) $(OUTPUT)libperf-gtk.so '$(DESTDIR_SQ)$(libdir_SQ)'
577endif 247endif
578 248
579ifndef NO_LIBPERL
580 LIB_OBJS += $(OUTPUT)util/scripting-engines/trace-event-perl.o
581 LIB_OBJS += $(OUTPUT)scripts/perl/Perf-Trace-Util/Context.o
582endif
583
584ifndef NO_LIBPYTHON
585 LIB_OBJS += $(OUTPUT)util/scripting-engines/trace-event-python.o
586 LIB_OBJS += $(OUTPUT)scripts/python/Perf-Trace-Util/Context.o
587endif
588
589ifeq ($(NO_PERF_REGS),0)
590 ifeq ($(ARCH),x86)
591 LIB_H += arch/x86/include/perf_regs.h
592 endif
593 LIB_OBJS += $(OUTPUT)util/perf_regs.o
594endif
595
596ifndef NO_LIBNUMA
597 BUILTIN_OBJS += $(OUTPUT)bench/numa.o
598endif
599
600ifndef NO_ZLIB
601 LIB_OBJS += $(OUTPUT)util/zlib.o
602endif
603
604ifdef ASCIIDOC8 249ifdef ASCIIDOC8
605 export ASCIIDOC8 250 export ASCIIDOC8
606endif 251endif
@@ -616,39 +261,29 @@ SHELL = $(SHELL_PATH)
616all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS) 261all: shell_compatibility_test $(ALL_PROGRAMS) $(LANG_BINDINGS) $(OTHER_PROGRAMS)
617 262
618please_set_SHELL_PATH_to_a_more_modern_shell: 263please_set_SHELL_PATH_to_a_more_modern_shell:
619 @$$(:) 264 $(Q)$$(:)
620 265
621shell_compatibility_test: please_set_SHELL_PATH_to_a_more_modern_shell 266shell_compatibility_test: please_set_SHELL_PATH_to_a_more_modern_shell
622 267
623strip: $(PROGRAMS) $(OUTPUT)perf 268strip: $(PROGRAMS) $(OUTPUT)perf
624 $(STRIP) $(STRIP_OPTS) $(PROGRAMS) $(OUTPUT)perf 269 $(STRIP) $(STRIP_OPTS) $(PROGRAMS) $(OUTPUT)perf
625 270
626$(OUTPUT)perf.o: perf.c $(OUTPUT)common-cmds.h $(OUTPUT)PERF-CFLAGS 271PERF_IN := $(OUTPUT)perf-in.o
627 $(QUIET_CC)$(CC) -include $(OUTPUT)PERF-VERSION-FILE \
628 '-DPERF_HTML_PATH="$(htmldir_SQ)"' \
629 $(CFLAGS) -c $(filter %.c,$^) -o $@
630 272
631$(OUTPUT)perf: $(OUTPUT)perf.o $(BUILTIN_OBJS) $(PERFLIBS) 273export srctree OUTPUT RM CC LD AR CFLAGS V BISON FLEX
632 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OUTPUT)perf.o \ 274build := -f $(srctree)/tools/build/Makefile.build dir=. obj
633 $(BUILTIN_OBJS) $(LIBS) -o $@
634 275
635$(GTK_OBJS): $(OUTPUT)%.o: %.c $(LIB_H) 276$(PERF_IN): $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h FORCE
636 $(QUIET_CC)$(CC) -o $@ -c -fPIC $(CFLAGS) $(GTK_CFLAGS) $< 277 $(Q)$(MAKE) $(build)=perf
637 278
638$(OUTPUT)libperf-gtk.so: $(GTK_OBJS) $(PERFLIBS) 279$(OUTPUT)perf: $(PERFLIBS) $(PERF_IN)
639 $(QUIET_LINK)$(CC) -o $@ -shared $(LDFLAGS) $(filter %.o,$^) $(GTK_LIBS) 280 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(PERF_IN) $(LIBS) -o $@
640 281
641$(OUTPUT)builtin-help.o: builtin-help.c $(OUTPUT)common-cmds.h $(OUTPUT)PERF-CFLAGS 282$(GTK_IN): FORCE
642 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) \ 283 $(Q)$(MAKE) $(build)=gtk
643 '-DPERF_HTML_PATH="$(htmldir_SQ)"' \
644 '-DPERF_MAN_PATH="$(mandir_SQ)"' \
645 '-DPERF_INFO_PATH="$(infodir_SQ)"' $<
646 284
647$(OUTPUT)builtin-timechart.o: builtin-timechart.c $(OUTPUT)common-cmds.h $(OUTPUT)PERF-CFLAGS 285$(OUTPUT)libperf-gtk.so: $(GTK_IN) $(PERFLIBS)
648 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) \ 286 $(QUIET_LINK)$(CC) -o $@ -shared $(LDFLAGS) $(filter %.o,$^) $(GTK_LIBS)
649 '-DPERF_HTML_PATH="$(htmldir_SQ)"' \
650 '-DPERF_MAN_PATH="$(mandir_SQ)"' \
651 '-DPERF_INFO_PATH="$(infodir_SQ)"' $<
652 287
653$(OUTPUT)common-cmds.h: util/generate-cmdlist.sh command-list.txt 288$(OUTPUT)common-cmds.h: util/generate-cmdlist.sh command-list.txt
654 289
@@ -659,8 +294,7 @@ $(SCRIPTS) : % : %.sh
659 $(QUIET_GEN)$(INSTALL) '$@.sh' '$(OUTPUT)$@' 294 $(QUIET_GEN)$(INSTALL) '$@.sh' '$(OUTPUT)$@'
660 295
661# These can record PERF_VERSION 296# These can record PERF_VERSION
662$(OUTPUT)perf.o perf.spec \ 297perf.spec $(SCRIPTS) \
663 $(SCRIPTS) \
664 : $(OUTPUT)PERF-VERSION-FILE 298 : $(OUTPUT)PERF-VERSION-FILE
665 299
666.SUFFIXES: 300.SUFFIXES:
@@ -683,90 +317,33 @@ endif
683# These two need to be here so that when O= is not used they take precedence 317# These two need to be here so that when O= is not used they take precedence
684# over the general rule for .o 318# over the general rule for .o
685 319
686$(OUTPUT)util/%-flex.o: $(OUTPUT)util/%-flex.c $(OUTPUT)PERF-CFLAGS 320# get relative building directory (to $(OUTPUT))
687 $(QUIET_CC)$(CC) -o $@ -c -Iutil/ $(CFLAGS) -w $< 321# and '.' if it's $(OUTPUT) itself
688 322__build-dir = $(subst $(OUTPUT),,$(dir $@))
689$(OUTPUT)util/%-bison.o: $(OUTPUT)util/%-bison.c $(OUTPUT)PERF-CFLAGS 323build-dir = $(if $(__build-dir),$(__build-dir),.)
690 $(QUIET_CC)$(CC) -o $@ -c -Iutil/ $(CFLAGS) -DYYENABLE_NLS=0 -DYYLTYPE_IS_TRIVIAL=0 -w $<
691
692$(OUTPUT)%.o: %.c $(OUTPUT)PERF-CFLAGS
693 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $<
694$(OUTPUT)%.i: %.c $(OUTPUT)PERF-CFLAGS
695 $(QUIET_CC)$(CC) -o $@ -E $(CFLAGS) $<
696$(OUTPUT)%.s: %.c $(OUTPUT)PERF-CFLAGS
697 $(QUIET_CC)$(CC) -o $@ -S $(CFLAGS) $<
698$(OUTPUT)%.o: %.S
699 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $<
700$(OUTPUT)%.s: %.S
701 $(QUIET_CC)$(CC) -o $@ -E $(CFLAGS) $<
702
703$(OUTPUT)util/exec_cmd.o: util/exec_cmd.c $(OUTPUT)PERF-CFLAGS
704 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) \
705 '-DPERF_EXEC_PATH="$(perfexecdir_SQ)"' \
706 '-DPREFIX="$(prefix_SQ)"' \
707 $<
708
709$(OUTPUT)tests/attr.o: tests/attr.c $(OUTPUT)PERF-CFLAGS
710 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) \
711 '-DBINDIR="$(bindir_SQ)"' -DPYTHON='"$(PYTHON_WORD)"' \
712 $<
713
714$(OUTPUT)tests/python-use.o: tests/python-use.c $(OUTPUT)PERF-CFLAGS
715 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) \
716 -DPYTHONPATH='"$(OUTPUT)python"' \
717 -DPYTHON='"$(PYTHON_WORD)"' \
718 $<
719
720$(OUTPUT)tests/dwarf-unwind.o: tests/dwarf-unwind.c
721 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -fno-optimize-sibling-calls $<
722 324
723$(OUTPUT)util/config.o: util/config.c $(OUTPUT)PERF-CFLAGS 325single_dep: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h
724 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
725 326
726$(OUTPUT)ui/setup.o: ui/setup.c $(OUTPUT)PERF-CFLAGS 327$(OUTPUT)%.o: %.c single_dep FORCE
727 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DLIBDIR='"$(libdir_SQ)"' $< 328 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
728 329
729$(OUTPUT)ui/browser.o: ui/browser.c $(OUTPUT)PERF-CFLAGS 330$(OUTPUT)%.i: %.c single_dep FORCE
730 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DENABLE_SLFUTURE_CONST $< 331 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
731 332
732$(OUTPUT)ui/browsers/annotate.o: ui/browsers/annotate.c $(OUTPUT)PERF-CFLAGS 333$(OUTPUT)%.s: %.c single_dep FORCE
733 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DENABLE_SLFUTURE_CONST $< 334 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
734 335
735$(OUTPUT)ui/browsers/hists.o: ui/browsers/hists.c $(OUTPUT)PERF-CFLAGS 336$(OUTPUT)%-bison.o: %.c single_dep FORCE
736 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DENABLE_SLFUTURE_CONST $< 337 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
737 338
738$(OUTPUT)ui/browsers/map.o: ui/browsers/map.c $(OUTPUT)PERF-CFLAGS 339$(OUTPUT)%-flex.o: %.c single_dep FORCE
739 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DENABLE_SLFUTURE_CONST $< 340 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
740 341
741$(OUTPUT)ui/browsers/scripts.o: ui/browsers/scripts.c $(OUTPUT)PERF-CFLAGS 342$(OUTPUT)%.o: %.S single_dep FORCE
742 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DENABLE_SLFUTURE_CONST $< 343 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
743 344
744$(OUTPUT)util/kallsyms.o: ../lib/symbol/kallsyms.c $(OUTPUT)PERF-CFLAGS 345$(OUTPUT)%.i: %.S single_dep FORCE
745 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $< 346 $(Q)$(MAKE) -f $(srctree)/tools/build/Makefile.build dir=$(build-dir) $@
746
747$(OUTPUT)util/rbtree.o: ../../lib/rbtree.c $(OUTPUT)PERF-CFLAGS
748 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -Wno-unused-parameter -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
749
750$(OUTPUT)util/hweight.o: ../../lib/hweight.c $(OUTPUT)PERF-CFLAGS
751 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -Wno-unused-parameter -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
752
753$(OUTPUT)util/find_next_bit.o: ../lib/util/find_next_bit.c $(OUTPUT)PERF-CFLAGS
754 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -Wno-unused-parameter -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
755
756$(OUTPUT)util/parse-events.o: util/parse-events.c $(OUTPUT)PERF-CFLAGS
757 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -Wno-redundant-decls $<
758
759$(OUTPUT)util/scripting-engines/trace-event-perl.o: util/scripting-engines/trace-event-perl.c $(OUTPUT)PERF-CFLAGS
760 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow -Wno-undef -Wno-switch-default $<
761
762$(OUTPUT)scripts/perl/Perf-Trace-Util/Context.o: scripts/perl/Perf-Trace-Util/Context.c $(OUTPUT)PERF-CFLAGS
763 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs -Wno-undef -Wno-switch-default $<
764
765$(OUTPUT)util/scripting-engines/trace-event-python.o: util/scripting-engines/trace-event-python.c $(OUTPUT)PERF-CFLAGS
766 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow $<
767
768$(OUTPUT)scripts/python/Perf-Trace-Util/Context.o: scripts/python/Perf-Trace-Util/Context.c $(OUTPUT)PERF-CFLAGS
769 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs $<
770 347
771$(OUTPUT)perf-%: %.o $(PERFLIBS) 348$(OUTPUT)perf-%: %.o $(PERFLIBS)
772 $(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(filter %.o,$^) $(LIBS) 349 $(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(filter %.o,$^) $(LIBS)
@@ -781,58 +358,34 @@ $(OUTPUT)perf-read-vdsox32: perf-read-vdso.c util/find-vdso-map.c
781 $(QUIET_CC)$(CC) -mx32 $(filter -static,$(LDFLAGS)) -Wall -Werror -o $@ perf-read-vdso.c 358 $(QUIET_CC)$(CC) -mx32 $(filter -static,$(LDFLAGS)) -Wall -Werror -o $@ perf-read-vdso.c
782endif 359endif
783 360
784$(LIB_OBJS) $(BUILTIN_OBJS): $(LIB_H) 361$(patsubst perf-%,%.o,$(PROGRAMS)): $(wildcard */*.h)
785$(patsubst perf-%,%.o,$(PROGRAMS)): $(LIB_H) $(wildcard */*.h)
786 362
787# we compile into subdirectories. if the target directory is not the source directory, they might not exists. So 363LIBPERF_IN := $(OUTPUT)libperf-in.o
788# we depend the various files onto their directories.
789DIRECTORY_DEPS = $(LIB_OBJS) $(BUILTIN_OBJS) $(GTK_OBJS)
790DIRECTORY_DEPS += $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h
791# no need to add flex objects, because they depend on bison ones
792DIRECTORY_DEPS += $(OUTPUT)util/parse-events-bison.c
793DIRECTORY_DEPS += $(OUTPUT)util/pmu-bison.c
794 364
795OUTPUT_DIRECTORIES := $(sort $(dir $(DIRECTORY_DEPS))) 365$(LIBPERF_IN): FORCE
366 $(Q)$(MAKE) $(build)=libperf
796 367
797$(DIRECTORY_DEPS): | $(OUTPUT_DIRECTORIES) 368$(LIB_FILE): $(LIBPERF_IN)
798# In the second step, we make a rule to actually create these directories 369 $(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIBPERF_IN) $(LIB_OBJS)
799$(OUTPUT_DIRECTORIES):
800 $(QUIET_MKDIR)$(MKDIR) -p $@ 2>/dev/null
801 370
802$(LIB_FILE): $(LIB_OBJS)
803 $(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(LIB_OBJS)
804
805# libtraceevent.a
806TE_SOURCES = $(wildcard $(TRACE_EVENT_DIR)*.[ch])
807
808LIBTRACEEVENT_FLAGS = $(QUIET_SUBDIR1) O=$(OUTPUT)
809LIBTRACEEVENT_FLAGS += CFLAGS="-g -Wall $(EXTRA_CFLAGS)"
810LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ) 371LIBTRACEEVENT_FLAGS += plugin_dir=$(plugindir_SQ)
811 372
812$(LIBTRACEEVENT): $(TE_SOURCES) $(OUTPUT)PERF-CFLAGS 373$(LIBTRACEEVENT): FORCE
813 $(QUIET_SUBDIR0)$(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) libtraceevent.a plugins 374 $(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) $(OUTPUT)libtraceevent.a plugins
814 375
815$(LIBTRACEEVENT)-clean: 376$(LIBTRACEEVENT)-clean:
816 $(call QUIET_CLEAN, libtraceevent) 377 $(call QUIET_CLEAN, libtraceevent)
817 @$(MAKE) -C $(TRACE_EVENT_DIR) O=$(OUTPUT) clean >/dev/null 378 $(Q)$(MAKE) -C $(TRACE_EVENT_DIR) O=$(OUTPUT) clean >/dev/null
818 379
819install-traceevent-plugins: $(LIBTRACEEVENT) 380install-traceevent-plugins: $(LIBTRACEEVENT)
820 $(QUIET_SUBDIR0)$(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) install_plugins 381 $(Q)$(MAKE) -C $(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) O=$(OUTPUT) install_plugins
821 382
822LIBAPIKFS_SOURCES = $(wildcard $(LIB_PATH)fs/*.[ch] $(LIB_PATH)fd/*.[ch]) 383$(LIBAPI): FORCE
823 384 $(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) $(OUTPUT)libapi.a
824# if subdir is set, we've been called from above so target has been built
825# already
826$(LIBAPIKFS): $(LIBAPIKFS_SOURCES)
827ifeq ($(subdir),)
828 $(QUIET_SUBDIR0)$(LIB_DIR) $(QUIET_SUBDIR1) O=$(OUTPUT) libapikfs.a
829endif
830 385
831$(LIBAPIKFS)-clean: 386$(LIBAPI)-clean:
832ifeq ($(subdir),) 387 $(call QUIET_CLEAN, libapi)
833 $(call QUIET_CLEAN, libapikfs) 388 $(Q)$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null
834 @$(MAKE) -C $(LIB_DIR) O=$(OUTPUT) clean >/dev/null
835endif
836 389
837help: 390help:
838 @echo 'Perf make targets:' 391 @echo 'Perf make targets:'
@@ -888,17 +441,6 @@ cscope:
888 $(QUIET_GEN)$(RM) cscope*; \ 441 $(QUIET_GEN)$(RM) cscope*; \
889 $(FIND) $(TAG_FOLDERS) -name '*.[hcS]' -print | xargs cscope -b $(TAG_FILES) 442 $(FIND) $(TAG_FOLDERS) -name '*.[hcS]' -print | xargs cscope -b $(TAG_FILES)
890 443
891### Detect prefix changes
892TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):\
893 $(bindir_SQ):$(perfexecdir_SQ):$(template_dir_SQ):$(prefix_SQ):$(plugindir_SQ)
894
895$(OUTPUT)PERF-CFLAGS: .FORCE-PERF-CFLAGS
896 @FLAGS='$(TRACK_CFLAGS)'; \
897 if test x"$$FLAGS" != x"`cat $(OUTPUT)PERF-CFLAGS 2>/dev/null`" ; then \
898 echo 1>&2 " FLAGS: * new build flags or prefix"; \
899 echo "$$FLAGS" >$(OUTPUT)PERF-CFLAGS; \
900 fi
901
902### Testing rules 444### Testing rules
903 445
904# GNU make supports exporting all variables by "export" without parameters. 446# GNU make supports exporting all variables by "export" without parameters.
@@ -981,12 +523,14 @@ $(INSTALL_DOC_TARGETS):
981# 523#
982config-clean: 524config-clean:
983 $(call QUIET_CLEAN, config) 525 $(call QUIET_CLEAN, config)
984 @$(MAKE) -C config/feature-checks clean >/dev/null 526 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
985 527
986clean: $(LIBTRACEEVENT)-clean $(LIBAPIKFS)-clean config-clean 528clean: $(LIBTRACEEVENT)-clean $(LIBAPI)-clean config-clean
987 $(call QUIET_CLEAN, core-objs) $(RM) $(LIB_OBJS) $(BUILTIN_OBJS) $(LIB_FILE) $(OUTPUT)perf-archive $(OUTPUT)perf-with-kcore $(OUTPUT)perf.o $(LANG_BINDINGS) $(GTK_OBJS) 529 $(call QUIET_CLEAN, core-objs) $(RM) $(LIB_FILE) $(OUTPUT)perf-archive $(OUTPUT)perf-with-kcore $(LANG_BINDINGS)
530 $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
531 $(Q)$(RM) .config-detected
988 $(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 perf-read-vdsox32 532 $(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 perf-read-vdsox32
989 $(call QUIET_CLEAN, core-gen) $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo $(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)PERF-CFLAGS $(OUTPUT)PERF-FEATURES $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex* 533 $(call QUIET_CLEAN, core-gen) $(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo $(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)FEATURE-DUMP $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex*
990 $(QUIET_SUBDIR0)Documentation $(QUIET_SUBDIR1) clean 534 $(QUIET_SUBDIR0)Documentation $(QUIET_SUBDIR1) clean
991 $(python-clean) 535 $(python-clean)
992 536
@@ -1000,7 +544,9 @@ else
1000 GIT-HEAD-PHONY = 544 GIT-HEAD-PHONY =
1001endif 545endif
1002 546
547FORCE:
548
1003.PHONY: all install clean config-clean strip install-gtk 549.PHONY: all install clean config-clean strip install-gtk
1004.PHONY: shell_compatibility_test please_set_SHELL_PATH_to_a_more_modern_shell 550.PHONY: shell_compatibility_test please_set_SHELL_PATH_to_a_more_modern_shell
1005.PHONY: $(GIT-HEAD-PHONY) TAGS tags cscope .FORCE-PERF-CFLAGS 551.PHONY: $(GIT-HEAD-PHONY) TAGS tags cscope FORCE single_dep
1006 552
diff --git a/tools/perf/arch/Build b/tools/perf/arch/Build
new file mode 100644
index 000000000000..109eb75cf7de
--- /dev/null
+++ b/tools/perf/arch/Build
@@ -0,0 +1,2 @@
1libperf-y += common.o
2libperf-y += $(ARCH)/
diff --git a/tools/perf/arch/arm/Build b/tools/perf/arch/arm/Build
new file mode 100644
index 000000000000..41bf61da476a
--- /dev/null
+++ b/tools/perf/arch/arm/Build
@@ -0,0 +1,2 @@
1libperf-y += util/
2libperf-$(CONFIG_DWARF_UNWIND) += tests/
diff --git a/tools/perf/arch/arm/Makefile b/tools/perf/arch/arm/Makefile
index 09d62153d384..7fbca175099e 100644
--- a/tools/perf/arch/arm/Makefile
+++ b/tools/perf/arch/arm/Makefile
@@ -1,14 +1,3 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4endif
5ifndef NO_LIBUNWIND
6LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind-libunwind.o
7endif
8ifndef NO_LIBDW_DWARF_UNWIND
9LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind-libdw.o
10endif
11ifndef NO_DWARF_UNWIND
12LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/regs_load.o
13LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/dwarf-unwind.o
14endif 3endif
diff --git a/tools/perf/arch/arm/tests/Build b/tools/perf/arch/arm/tests/Build
new file mode 100644
index 000000000000..b30eff9bcc83
--- /dev/null
+++ b/tools/perf/arch/arm/tests/Build
@@ -0,0 +1,2 @@
1libperf-y += regs_load.o
2libperf-y += dwarf-unwind.o
diff --git a/tools/perf/arch/arm/util/Build b/tools/perf/arch/arm/util/Build
new file mode 100644
index 000000000000..d22e3d07de3d
--- /dev/null
+++ b/tools/perf/arch/arm/util/Build
@@ -0,0 +1,4 @@
1libperf-$(CONFIG_DWARF) += dwarf-regs.o
2
3libperf-$(CONFIG_LIBUNWIND) += unwind-libunwind.o
4libperf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
diff --git a/tools/perf/arch/arm64/Build b/tools/perf/arch/arm64/Build
new file mode 100644
index 000000000000..54afe4a467e7
--- /dev/null
+++ b/tools/perf/arch/arm64/Build
@@ -0,0 +1 @@
libperf-y += util/
diff --git a/tools/perf/arch/arm64/Makefile b/tools/perf/arch/arm64/Makefile
index 67e9b3d38e89..7fbca175099e 100644
--- a/tools/perf/arch/arm64/Makefile
+++ b/tools/perf/arch/arm64/Makefile
@@ -1,7 +1,3 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4endif
5ifndef NO_LIBUNWIND
6LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind-libunwind.o
7endif 3endif
diff --git a/tools/perf/arch/arm64/util/Build b/tools/perf/arch/arm64/util/Build
new file mode 100644
index 000000000000..e58123a8912b
--- /dev/null
+++ b/tools/perf/arch/arm64/util/Build
@@ -0,0 +1,2 @@
1libperf-$(CONFIG_DWARF) += dwarf-regs.o
2libperf-$(CONFIG_LIBUNWIND) += unwind-libunwind.o
diff --git a/tools/perf/arch/powerpc/Build b/tools/perf/arch/powerpc/Build
new file mode 100644
index 000000000000..54afe4a467e7
--- /dev/null
+++ b/tools/perf/arch/powerpc/Build
@@ -0,0 +1 @@
libperf-y += util/
diff --git a/tools/perf/arch/powerpc/Makefile b/tools/perf/arch/powerpc/Makefile
index 6f7782bea5dd..7fbca175099e 100644
--- a/tools/perf/arch/powerpc/Makefile
+++ b/tools/perf/arch/powerpc/Makefile
@@ -1,6 +1,3 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/skip-callchain-idx.o
5endif 3endif
6LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o
diff --git a/tools/perf/arch/powerpc/util/Build b/tools/perf/arch/powerpc/util/Build
new file mode 100644
index 000000000000..0af6e9b3f728
--- /dev/null
+++ b/tools/perf/arch/powerpc/util/Build
@@ -0,0 +1,4 @@
1libperf-y += header.o
2
3libperf-$(CONFIG_DWARF) += dwarf-regs.o
4libperf-$(CONFIG_DWARF) += skip-callchain-idx.o
diff --git a/tools/perf/arch/s390/Build b/tools/perf/arch/s390/Build
new file mode 100644
index 000000000000..54afe4a467e7
--- /dev/null
+++ b/tools/perf/arch/s390/Build
@@ -0,0 +1 @@
libperf-y += util/
diff --git a/tools/perf/arch/s390/Makefile b/tools/perf/arch/s390/Makefile
index 798ac7379c5f..21322e0385b8 100644
--- a/tools/perf/arch/s390/Makefile
+++ b/tools/perf/arch/s390/Makefile
@@ -1,7 +1,4 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4endif 3endif
5LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o
6HAVE_KVM_STAT_SUPPORT := 1 4HAVE_KVM_STAT_SUPPORT := 1
7LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/kvm-stat.o
diff --git a/tools/perf/arch/s390/util/Build b/tools/perf/arch/s390/util/Build
new file mode 100644
index 000000000000..8a61372bb47a
--- /dev/null
+++ b/tools/perf/arch/s390/util/Build
@@ -0,0 +1,4 @@
1libperf-y += header.o
2libperf-y += kvm-stat.o
3
4libperf-$(CONFIG_DWARF) += dwarf-regs.o
diff --git a/tools/perf/arch/sh/Build b/tools/perf/arch/sh/Build
new file mode 100644
index 000000000000..54afe4a467e7
--- /dev/null
+++ b/tools/perf/arch/sh/Build
@@ -0,0 +1 @@
libperf-y += util/
diff --git a/tools/perf/arch/sh/Makefile b/tools/perf/arch/sh/Makefile
index 15130b50dfe3..7fbca175099e 100644
--- a/tools/perf/arch/sh/Makefile
+++ b/tools/perf/arch/sh/Makefile
@@ -1,4 +1,3 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4endif 3endif
diff --git a/tools/perf/arch/sh/util/Build b/tools/perf/arch/sh/util/Build
new file mode 100644
index 000000000000..954e287bbb89
--- /dev/null
+++ b/tools/perf/arch/sh/util/Build
@@ -0,0 +1 @@
libperf-$(CONFIG_DWARF) += dwarf-regs.o
diff --git a/tools/perf/arch/sparc/Build b/tools/perf/arch/sparc/Build
new file mode 100644
index 000000000000..54afe4a467e7
--- /dev/null
+++ b/tools/perf/arch/sparc/Build
@@ -0,0 +1 @@
libperf-y += util/
diff --git a/tools/perf/arch/sparc/Makefile b/tools/perf/arch/sparc/Makefile
index 15130b50dfe3..7fbca175099e 100644
--- a/tools/perf/arch/sparc/Makefile
+++ b/tools/perf/arch/sparc/Makefile
@@ -1,4 +1,3 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4endif 3endif
diff --git a/tools/perf/arch/sparc/util/Build b/tools/perf/arch/sparc/util/Build
new file mode 100644
index 000000000000..954e287bbb89
--- /dev/null
+++ b/tools/perf/arch/sparc/util/Build
@@ -0,0 +1 @@
libperf-$(CONFIG_DWARF) += dwarf-regs.o
diff --git a/tools/perf/arch/x86/Build b/tools/perf/arch/x86/Build
new file mode 100644
index 000000000000..41bf61da476a
--- /dev/null
+++ b/tools/perf/arch/x86/Build
@@ -0,0 +1,2 @@
1libperf-y += util/
2libperf-$(CONFIG_DWARF_UNWIND) += tests/
diff --git a/tools/perf/arch/x86/Makefile b/tools/perf/arch/x86/Makefile
index 9b21881db52f..21322e0385b8 100644
--- a/tools/perf/arch/x86/Makefile
+++ b/tools/perf/arch/x86/Makefile
@@ -1,19 +1,4 @@
1ifndef NO_DWARF 1ifndef NO_DWARF
2PERF_HAVE_DWARF_REGS := 1 2PERF_HAVE_DWARF_REGS := 1
3LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o
4endif 3endif
5ifndef NO_LIBUNWIND
6LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind-libunwind.o
7endif
8ifndef NO_LIBDW_DWARF_UNWIND
9LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind-libdw.o
10endif
11ifndef NO_DWARF_UNWIND
12LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/regs_load.o
13LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/dwarf-unwind.o
14endif
15LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o
16LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/tsc.o
17LIB_H += arch/$(ARCH)/util/tsc.h
18HAVE_KVM_STAT_SUPPORT := 1 4HAVE_KVM_STAT_SUPPORT := 1
19LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/kvm-stat.o
diff --git a/tools/perf/arch/x86/tests/Build b/tools/perf/arch/x86/tests/Build
new file mode 100644
index 000000000000..b30eff9bcc83
--- /dev/null
+++ b/tools/perf/arch/x86/tests/Build
@@ -0,0 +1,2 @@
1libperf-y += regs_load.o
2libperf-y += dwarf-unwind.o
diff --git a/tools/perf/arch/x86/util/Build b/tools/perf/arch/x86/util/Build
new file mode 100644
index 000000000000..cfbccc4e3187
--- /dev/null
+++ b/tools/perf/arch/x86/util/Build
@@ -0,0 +1,8 @@
1libperf-y += header.o
2libperf-y += tsc.o
3libperf-y += kvm-stat.o
4
5libperf-$(CONFIG_DWARF) += dwarf-regs.o
6
7libperf-$(CONFIG_LIBUNWIND) += unwind-libunwind.o
8libperf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build
new file mode 100644
index 000000000000..5ce98023d518
--- /dev/null
+++ b/tools/perf/bench/Build
@@ -0,0 +1,11 @@
1perf-y += sched-messaging.o
2perf-y += sched-pipe.o
3perf-y += mem-memcpy.o
4perf-y += futex-hash.o
5perf-y += futex-wake.o
6perf-y += futex-requeue.o
7
8perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o
9perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o
10
11perf-$(CONFIG_NUMA) += numa.o
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 747f86103599..71bf7451c0ca 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -208,7 +208,7 @@ static int __cmd_annotate(struct perf_annotate *ann)
208 goto out; 208 goto out;
209 } 209 }
210 210
211 ret = perf_session__process_events(session, &ann->tool); 211 ret = perf_session__process_events(session);
212 if (ret) 212 if (ret)
213 goto out; 213 goto out;
214 214
diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
index 50e6b66aea1f..d47a0cdc71c9 100644
--- a/tools/perf/builtin-buildid-cache.c
+++ b/tools/perf/builtin-buildid-cache.c
@@ -125,8 +125,7 @@ static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
125 return ret; 125 return ret;
126} 126}
127 127
128static int build_id_cache__add_kcore(const char *filename, const char *debugdir, 128static int build_id_cache__add_kcore(const char *filename, bool force)
129 bool force)
130{ 129{
131 char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1]; 130 char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1];
132 char from_dir[PATH_MAX], to_dir[PATH_MAX]; 131 char from_dir[PATH_MAX], to_dir[PATH_MAX];
@@ -143,7 +142,7 @@ static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
143 return -1; 142 return -1;
144 143
145 scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s", 144 scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s",
146 debugdir, sbuildid); 145 buildid_dir, sbuildid);
147 146
148 if (!force && 147 if (!force &&
149 !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) { 148 !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
@@ -155,7 +154,7 @@ static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
155 return -1; 154 return -1;
156 155
157 scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s", 156 scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s",
158 debugdir, sbuildid, dir); 157 buildid_dir, sbuildid, dir);
159 158
160 if (mkdir_p(to_dir, 0755)) 159 if (mkdir_p(to_dir, 0755))
161 return -1; 160 return -1;
@@ -183,7 +182,7 @@ static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
183 return 0; 182 return 0;
184} 183}
185 184
186static int build_id_cache__add_file(const char *filename, const char *debugdir) 185static int build_id_cache__add_file(const char *filename)
187{ 186{
188 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 187 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
189 u8 build_id[BUILD_ID_SIZE]; 188 u8 build_id[BUILD_ID_SIZE];
@@ -195,16 +194,14 @@ static int build_id_cache__add_file(const char *filename, const char *debugdir)
195 } 194 }
196 195
197 build_id__sprintf(build_id, sizeof(build_id), sbuild_id); 196 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
198 err = build_id_cache__add_s(sbuild_id, debugdir, filename, 197 err = build_id_cache__add_s(sbuild_id, filename,
199 false, false); 198 false, false);
200 if (verbose) 199 pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
201 pr_info("Adding %s %s: %s\n", sbuild_id, filename, 200 err ? "FAIL" : "Ok");
202 err ? "FAIL" : "Ok");
203 return err; 201 return err;
204} 202}
205 203
206static int build_id_cache__remove_file(const char *filename, 204static int build_id_cache__remove_file(const char *filename)
207 const char *debugdir)
208{ 205{
209 u8 build_id[BUILD_ID_SIZE]; 206 u8 build_id[BUILD_ID_SIZE];
210 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 207 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
@@ -217,10 +214,34 @@ static int build_id_cache__remove_file(const char *filename,
217 } 214 }
218 215
219 build_id__sprintf(build_id, sizeof(build_id), sbuild_id); 216 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
220 err = build_id_cache__remove_s(sbuild_id, debugdir); 217 err = build_id_cache__remove_s(sbuild_id);
221 if (verbose) 218 pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
222 pr_info("Removing %s %s: %s\n", sbuild_id, filename, 219 err ? "FAIL" : "Ok");
223 err ? "FAIL" : "Ok"); 220
221 return err;
222}
223
224static int build_id_cache__purge_path(const char *pathname)
225{
226 struct strlist *list;
227 struct str_node *pos;
228 int err;
229
230 err = build_id_cache__list_build_ids(pathname, &list);
231 if (err)
232 goto out;
233
234 strlist__for_each(pos, list) {
235 err = build_id_cache__remove_s(pos->s);
236 pr_debug("Removing %s %s: %s\n", pos->s, pathname,
237 err ? "FAIL" : "Ok");
238 if (err)
239 break;
240 }
241 strlist__delete(list);
242
243out:
244 pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
224 245
225 return err; 246 return err;
226} 247}
@@ -252,13 +273,12 @@ static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *f
252 return 0; 273 return 0;
253} 274}
254 275
255static int build_id_cache__update_file(const char *filename, 276static int build_id_cache__update_file(const char *filename)
256 const char *debugdir)
257{ 277{
258 u8 build_id[BUILD_ID_SIZE]; 278 u8 build_id[BUILD_ID_SIZE];
259 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 279 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
260 280
261 int err; 281 int err = 0;
262 282
263 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) { 283 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
264 pr_debug("Couldn't read a build-id in %s\n", filename); 284 pr_debug("Couldn't read a build-id in %s\n", filename);
@@ -266,14 +286,14 @@ static int build_id_cache__update_file(const char *filename,
266 } 286 }
267 287
268 build_id__sprintf(build_id, sizeof(build_id), sbuild_id); 288 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
269 err = build_id_cache__remove_s(sbuild_id, debugdir); 289 if (build_id_cache__cached(sbuild_id))
270 if (!err) { 290 err = build_id_cache__remove_s(sbuild_id);
271 err = build_id_cache__add_s(sbuild_id, debugdir, filename, 291
272 false, false); 292 if (!err)
273 } 293 err = build_id_cache__add_s(sbuild_id, filename, false, false);
274 if (verbose) 294
275 pr_info("Updating %s %s: %s\n", sbuild_id, filename, 295 pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
276 err ? "FAIL" : "Ok"); 296 err ? "FAIL" : "Ok");
277 297
278 return err; 298 return err;
279} 299}
@@ -287,6 +307,7 @@ int cmd_buildid_cache(int argc, const char **argv,
287 bool force = false; 307 bool force = false;
288 char const *add_name_list_str = NULL, 308 char const *add_name_list_str = NULL,
289 *remove_name_list_str = NULL, 309 *remove_name_list_str = NULL,
310 *purge_name_list_str = NULL,
290 *missing_filename = NULL, 311 *missing_filename = NULL,
291 *update_name_list_str = NULL, 312 *update_name_list_str = NULL,
292 *kcore_filename = NULL; 313 *kcore_filename = NULL;
@@ -304,6 +325,8 @@ int cmd_buildid_cache(int argc, const char **argv,
304 "file", "kcore file to add"), 325 "file", "kcore file to add"),
305 OPT_STRING('r', "remove", &remove_name_list_str, "file list", 326 OPT_STRING('r', "remove", &remove_name_list_str, "file list",
306 "file(s) to remove"), 327 "file(s) to remove"),
328 OPT_STRING('p', "purge", &purge_name_list_str, "path list",
329 "path(s) to remove (remove old caches too)"),
307 OPT_STRING('M', "missing", &missing_filename, "file", 330 OPT_STRING('M', "missing", &missing_filename, "file",
308 "to find missing build ids in the cache"), 331 "to find missing build ids in the cache"),
309 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 332 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
@@ -320,6 +343,11 @@ int cmd_buildid_cache(int argc, const char **argv,
320 argc = parse_options(argc, argv, buildid_cache_options, 343 argc = parse_options(argc, argv, buildid_cache_options,
321 buildid_cache_usage, 0); 344 buildid_cache_usage, 0);
322 345
346 if (argc || (!add_name_list_str && !kcore_filename &&
347 !remove_name_list_str && !purge_name_list_str &&
348 !missing_filename && !update_name_list_str))
349 usage_with_options(buildid_cache_usage, buildid_cache_options);
350
323 if (missing_filename) { 351 if (missing_filename) {
324 file.path = missing_filename; 352 file.path = missing_filename;
325 file.force = force; 353 file.force = force;
@@ -338,7 +366,7 @@ int cmd_buildid_cache(int argc, const char **argv,
338 list = strlist__new(true, add_name_list_str); 366 list = strlist__new(true, add_name_list_str);
339 if (list) { 367 if (list) {
340 strlist__for_each(pos, list) 368 strlist__for_each(pos, list)
341 if (build_id_cache__add_file(pos->s, buildid_dir)) { 369 if (build_id_cache__add_file(pos->s)) {
342 if (errno == EEXIST) { 370 if (errno == EEXIST) {
343 pr_debug("%s already in the cache\n", 371 pr_debug("%s already in the cache\n",
344 pos->s); 372 pos->s);
@@ -356,7 +384,25 @@ int cmd_buildid_cache(int argc, const char **argv,
356 list = strlist__new(true, remove_name_list_str); 384 list = strlist__new(true, remove_name_list_str);
357 if (list) { 385 if (list) {
358 strlist__for_each(pos, list) 386 strlist__for_each(pos, list)
359 if (build_id_cache__remove_file(pos->s, buildid_dir)) { 387 if (build_id_cache__remove_file(pos->s)) {
388 if (errno == ENOENT) {
389 pr_debug("%s wasn't in the cache\n",
390 pos->s);
391 continue;
392 }
393 pr_warning("Couldn't remove %s: %s\n",
394 pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
395 }
396
397 strlist__delete(list);
398 }
399 }
400
401 if (purge_name_list_str) {
402 list = strlist__new(true, purge_name_list_str);
403 if (list) {
404 strlist__for_each(pos, list)
405 if (build_id_cache__purge_path(pos->s)) {
360 if (errno == ENOENT) { 406 if (errno == ENOENT) {
361 pr_debug("%s wasn't in the cache\n", 407 pr_debug("%s wasn't in the cache\n",
362 pos->s); 408 pos->s);
@@ -377,7 +423,7 @@ int cmd_buildid_cache(int argc, const char **argv,
377 list = strlist__new(true, update_name_list_str); 423 list = strlist__new(true, update_name_list_str);
378 if (list) { 424 if (list) {
379 strlist__for_each(pos, list) 425 strlist__for_each(pos, list)
380 if (build_id_cache__update_file(pos->s, buildid_dir)) { 426 if (build_id_cache__update_file(pos->s)) {
381 if (errno == ENOENT) { 427 if (errno == ENOENT) {
382 pr_debug("%s wasn't in the cache\n", 428 pr_debug("%s wasn't in the cache\n",
383 pos->s); 429 pos->s);
@@ -391,8 +437,7 @@ int cmd_buildid_cache(int argc, const char **argv,
391 } 437 }
392 } 438 }
393 439
394 if (kcore_filename && 440 if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
395 build_id_cache__add_kcore(kcore_filename, buildid_dir, force))
396 pr_warning("Couldn't add %s\n", kcore_filename); 441 pr_warning("Couldn't add %s\n", kcore_filename);
397 442
398out: 443out:
diff --git a/tools/perf/builtin-buildid-list.c b/tools/perf/builtin-buildid-list.c
index ed3873b3e238..feb420f74c2d 100644
--- a/tools/perf/builtin-buildid-list.c
+++ b/tools/perf/builtin-buildid-list.c
@@ -74,7 +74,7 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
74 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID 74 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
75 */ 75 */
76 if (with_hits || perf_data_file__is_pipe(&file)) 76 if (with_hits || perf_data_file__is_pipe(&file))
77 perf_session__process_events(session, &build_id__mark_dso_hit_ops); 77 perf_session__process_events(session);
78 78
79 perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits); 79 perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
80 perf_session__delete(session); 80 perf_session__delete(session);
diff --git a/tools/perf/builtin-data.c b/tools/perf/builtin-data.c
new file mode 100644
index 000000000000..d6525bc54d13
--- /dev/null
+++ b/tools/perf/builtin-data.c
@@ -0,0 +1,123 @@
1#include <linux/compiler.h>
2#include "builtin.h"
3#include "perf.h"
4#include "debug.h"
5#include "parse-options.h"
6#include "data-convert-bt.h"
7
8typedef int (*data_cmd_fn_t)(int argc, const char **argv, const char *prefix);
9
10struct data_cmd {
11 const char *name;
12 const char *summary;
13 data_cmd_fn_t fn;
14};
15
16static struct data_cmd data_cmds[];
17
18#define for_each_cmd(cmd) \
19 for (cmd = data_cmds; cmd && cmd->name; cmd++)
20
21static const struct option data_options[] = {
22 OPT_END()
23};
24
25static const char * const data_subcommands[] = { "convert", NULL };
26
27static const char *data_usage[] = {
28 "perf data [<common options>] <command> [<options>]",
29 NULL
30};
31
32static void print_usage(void)
33{
34 struct data_cmd *cmd;
35
36 printf("Usage:\n");
37 printf("\t%s\n\n", data_usage[0]);
38 printf("\tAvailable commands:\n");
39
40 for_each_cmd(cmd) {
41 printf("\t %s\t- %s\n", cmd->name, cmd->summary);
42 }
43
44 printf("\n");
45}
46
47static const char * const data_convert_usage[] = {
48 "perf data convert [<options>]",
49 NULL
50};
51
52static int cmd_data_convert(int argc, const char **argv,
53 const char *prefix __maybe_unused)
54{
55 const char *to_ctf = NULL;
56 bool force = false;
57 const struct option options[] = {
58 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
59 OPT_STRING('i', "input", &input_name, "file", "input file name"),
60#ifdef HAVE_LIBBABELTRACE_SUPPORT
61 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
62#endif
63 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
64 OPT_END()
65 };
66
67#ifndef HAVE_LIBBABELTRACE_SUPPORT
68 pr_err("No conversion support compiled in.\n");
69 return -1;
70#endif
71
72 argc = parse_options(argc, argv, options,
73 data_convert_usage, 0);
74 if (argc) {
75 usage_with_options(data_convert_usage, options);
76 return -1;
77 }
78
79 if (to_ctf) {
80#ifdef HAVE_LIBBABELTRACE_SUPPORT
81 return bt_convert__perf2ctf(input_name, to_ctf, force);
82#else
83 pr_err("The libbabeltrace support is not compiled in.\n");
84 return -1;
85#endif
86 }
87
88 return 0;
89}
90
91static struct data_cmd data_cmds[] = {
92 { "convert", "converts data file between formats", cmd_data_convert },
93 { .name = NULL, },
94};
95
96int cmd_data(int argc, const char **argv, const char *prefix)
97{
98 struct data_cmd *cmd;
99 const char *cmdstr;
100
101 /* No command specified. */
102 if (argc < 2)
103 goto usage;
104
105 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
106 PARSE_OPT_STOP_AT_NON_OPTION);
107 if (argc < 1)
108 goto usage;
109
110 cmdstr = argv[0];
111
112 for_each_cmd(cmd) {
113 if (strcmp(cmd->name, cmdstr))
114 continue;
115
116 return cmd->fn(argc, argv, prefix);
117 }
118
119 pr_err("Unknown command: %s\n", cmdstr);
120usage:
121 print_usage();
122 return -1;
123}
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index 74aada554b12..df6307b4050a 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -747,7 +747,7 @@ static int __cmd_diff(void)
747 goto out_delete; 747 goto out_delete;
748 } 748 }
749 749
750 ret = perf_session__process_events(d->session, &tool); 750 ret = perf_session__process_events(d->session);
751 if (ret) { 751 if (ret) {
752 pr_err("Failed to process %s\n", d->file.path); 752 pr_err("Failed to process %s\n", d->file.path);
753 goto out_delete; 753 goto out_delete;
@@ -791,6 +791,8 @@ static const struct option options[] = {
791 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 791 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
792 "dump raw trace in ASCII"), 792 "dump raw trace in ASCII"),
793 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 793 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
794 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
795 "file", "kallsyms pathname"),
794 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 796 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
795 "load module symbols - WARNING: use only with -k and LIVE kernel"), 797 "load module symbols - WARNING: use only with -k and LIVE kernel"),
796 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 798 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
@@ -802,7 +804,7 @@ static const struct option options[] = {
802 OPT_STRING('s', "sort", &sort_order, "key[,key2...]", 804 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
803 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..." 805 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
804 " Please refer the man page for the complete list."), 806 " Please refer the man page for the complete list."),
805 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", 807 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
806 "separator for columns, no spaces will be added between " 808 "separator for columns, no spaces will be added between "
807 "columns '.' is reserved."), 809 "columns '.' is reserved."),
808 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", 810 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
diff --git a/tools/perf/builtin-evlist.c b/tools/perf/builtin-evlist.c
index 0f93f859b782..695ec5a50cf2 100644
--- a/tools/perf/builtin-evlist.c
+++ b/tools/perf/builtin-evlist.c
@@ -24,6 +24,7 @@ static int __cmd_evlist(const char *file_name, struct perf_attr_details *details
24 struct perf_data_file file = { 24 struct perf_data_file file = {
25 .path = file_name, 25 .path = file_name,
26 .mode = PERF_DATA_MODE_READ, 26 .mode = PERF_DATA_MODE_READ,
27 .force = details->force,
27 }; 28 };
28 29
29 session = perf_session__new(&file, 0, NULL); 30 session = perf_session__new(&file, 0, NULL);
@@ -47,6 +48,7 @@ int cmd_evlist(int argc, const char **argv, const char *prefix __maybe_unused)
47 "Show all event attr details"), 48 "Show all event attr details"),
48 OPT_BOOLEAN('g', "group", &details.event_group, 49 OPT_BOOLEAN('g', "group", &details.event_group,
49 "Show event group information"), 50 "Show event group information"),
51 OPT_BOOLEAN('f', "force", &details.force, "don't complain, do it"),
50 OPT_END() 52 OPT_END()
51 }; 53 };
52 const char * const evlist_usage[] = { 54 const char * const evlist_usage[] = {
diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c
index 25d20628212e..36486eade1ef 100644
--- a/tools/perf/builtin-help.c
+++ b/tools/perf/builtin-help.c
@@ -437,7 +437,18 @@ int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
437 HELP_FORMAT_INFO), 437 HELP_FORMAT_INFO),
438 OPT_END(), 438 OPT_END(),
439 }; 439 };
440 const char * const builtin_help_usage[] = { 440 const char * const builtin_help_subcommands[] = {
441 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
442 "record", "report", "bench", "stat", "timechart", "top", "annotate",
443 "script", "sched", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
444#ifdef HAVE_LIBELF_SUPPORT
445 "probe",
446#endif
447#ifdef HAVE_LIBAUDIT_SUPPORT
448 "trace",
449#endif
450 NULL };
451 const char *builtin_help_usage[] = {
441 "perf help [--all] [--man|--web|--info] [command]", 452 "perf help [--all] [--man|--web|--info] [command]",
442 NULL 453 NULL
443 }; 454 };
@@ -448,8 +459,8 @@ int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
448 459
449 perf_config(perf_help_config, &help_format); 460 perf_config(perf_help_config, &help_format);
450 461
451 argc = parse_options(argc, argv, builtin_help_options, 462 argc = parse_options_subcommand(argc, argv, builtin_help_options,
452 builtin_help_usage, 0); 463 builtin_help_subcommands, builtin_help_usage, 0);
453 464
454 if (show_all) { 465 if (show_all) {
455 printf("\n usage: %s\n\n", perf_usage_string); 466 printf("\n usage: %s\n\n", perf_usage_string);
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index a13641e066f5..40a33d7334cc 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -53,6 +53,13 @@ static int perf_event__repipe_synth(struct perf_tool *tool,
53 return 0; 53 return 0;
54} 54}
55 55
56static int perf_event__repipe_oe_synth(struct perf_tool *tool,
57 union perf_event *event,
58 struct ordered_events *oe __maybe_unused)
59{
60 return perf_event__repipe_synth(tool, event);
61}
62
56static int perf_event__repipe_op2_synth(struct perf_tool *tool, 63static int perf_event__repipe_op2_synth(struct perf_tool *tool,
57 union perf_event *event, 64 union perf_event *event,
58 struct perf_session *session 65 struct perf_session *session
@@ -359,8 +366,6 @@ static int __cmd_inject(struct perf_inject *inject)
359 } else if (inject->sched_stat) { 366 } else if (inject->sched_stat) {
360 struct perf_evsel *evsel; 367 struct perf_evsel *evsel;
361 368
362 inject->tool.ordered_events = true;
363
364 evlist__for_each(session->evlist, evsel) { 369 evlist__for_each(session->evlist, evsel) {
365 const char *name = perf_evsel__name(evsel); 370 const char *name = perf_evsel__name(evsel);
366 371
@@ -379,7 +384,7 @@ static int __cmd_inject(struct perf_inject *inject)
379 if (!file_out->is_pipe) 384 if (!file_out->is_pipe)
380 lseek(fd, session->header.data_offset, SEEK_SET); 385 lseek(fd, session->header.data_offset, SEEK_SET);
381 386
382 ret = perf_session__process_events(session, &inject->tool); 387 ret = perf_session__process_events(session);
383 388
384 if (!file_out->is_pipe) { 389 if (!file_out->is_pipe) {
385 if (inject->build_ids) 390 if (inject->build_ids)
@@ -408,7 +413,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
408 .unthrottle = perf_event__repipe, 413 .unthrottle = perf_event__repipe,
409 .attr = perf_event__repipe_attr, 414 .attr = perf_event__repipe_attr,
410 .tracing_data = perf_event__repipe_op2_synth, 415 .tracing_data = perf_event__repipe_op2_synth,
411 .finished_round = perf_event__repipe_op2_synth, 416 .finished_round = perf_event__repipe_oe_synth,
412 .build_id = perf_event__repipe_op2_synth, 417 .build_id = perf_event__repipe_op2_synth,
413 .id_index = perf_event__repipe_op2_synth, 418 .id_index = perf_event__repipe_op2_synth,
414 }, 419 },
@@ -438,6 +443,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
438 "be more verbose (show build ids, etc)"), 443 "be more verbose (show build ids, etc)"),
439 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file", 444 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, "file",
440 "kallsyms pathname"), 445 "kallsyms pathname"),
446 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
441 OPT_END() 447 OPT_END()
442 }; 448 };
443 const char * const inject_usage[] = { 449 const char * const inject_usage[] = {
@@ -458,6 +464,8 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
458 return -1; 464 return -1;
459 } 465 }
460 466
467 inject.tool.ordered_events = inject.sched_stat;
468
461 file.path = inject.input_name; 469 file.path = inject.input_name;
462 inject.session = perf_session__new(&file, true, &inject.tool); 470 inject.session = perf_session__new(&file, true, &inject.tool);
463 if (inject.session == NULL) 471 if (inject.session == NULL)
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index f295141025bc..4ebf65c79434 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -20,6 +20,7 @@
20 20
21#include <linux/rbtree.h> 21#include <linux/rbtree.h>
22#include <linux/string.h> 22#include <linux/string.h>
23#include <locale.h>
23 24
24struct alloc_stat; 25struct alloc_stat;
25typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *); 26typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *);
@@ -275,10 +276,10 @@ static void __print_result(struct rb_root *root, struct perf_session *session,
275 struct rb_node *next; 276 struct rb_node *next;
276 struct machine *machine = &session->machines.host; 277 struct machine *machine = &session->machines.host;
277 278
278 printf("%.102s\n", graph_dotted_line); 279 printf("%.105s\n", graph_dotted_line);
279 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); 280 printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr");
280 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); 281 printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n");
281 printf("%.102s\n", graph_dotted_line); 282 printf("%.105s\n", graph_dotted_line);
282 283
283 next = rb_first(root); 284 next = rb_first(root);
284 285
@@ -304,7 +305,7 @@ static void __print_result(struct rb_root *root, struct perf_session *session,
304 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); 305 snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr);
305 printf(" %-34s |", buf); 306 printf(" %-34s |", buf);
306 307
307 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n", 308 printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %9lu | %6.3f%%\n",
308 (unsigned long long)data->bytes_alloc, 309 (unsigned long long)data->bytes_alloc,
309 (unsigned long)data->bytes_alloc / data->hit, 310 (unsigned long)data->bytes_alloc / data->hit,
310 (unsigned long long)data->bytes_req, 311 (unsigned long long)data->bytes_req,
@@ -317,21 +318,21 @@ static void __print_result(struct rb_root *root, struct perf_session *session,
317 } 318 }
318 319
319 if (n_lines == -1) 320 if (n_lines == -1)
320 printf(" ... | ... | ... | ... | ... | ... \n"); 321 printf(" ... | ... | ... | ... | ... | ... \n");
321 322
322 printf("%.102s\n", graph_dotted_line); 323 printf("%.105s\n", graph_dotted_line);
323} 324}
324 325
325static void print_summary(void) 326static void print_summary(void)
326{ 327{
327 printf("\nSUMMARY\n=======\n"); 328 printf("\nSUMMARY\n=======\n");
328 printf("Total bytes requested: %lu\n", total_requested); 329 printf("Total bytes requested: %'lu\n", total_requested);
329 printf("Total bytes allocated: %lu\n", total_allocated); 330 printf("Total bytes allocated: %'lu\n", total_allocated);
330 printf("Total bytes wasted on internal fragmentation: %lu\n", 331 printf("Total bytes wasted on internal fragmentation: %'lu\n",
331 total_allocated - total_requested); 332 total_allocated - total_requested);
332 printf("Internal fragmentation: %f%%\n", 333 printf("Internal fragmentation: %f%%\n",
333 fragmentation(total_requested, total_allocated)); 334 fragmentation(total_requested, total_allocated));
334 printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs); 335 printf("Cross CPU allocations: %'lu/%'lu\n", nr_cross_allocs, nr_allocs);
335} 336}
336 337
337static void print_result(struct perf_session *session) 338static void print_result(struct perf_session *session)
@@ -426,7 +427,7 @@ static int __cmd_kmem(struct perf_session *session)
426 } 427 }
427 428
428 setup_pager(); 429 setup_pager();
429 err = perf_session__process_events(session, &perf_kmem); 430 err = perf_session__process_events(session);
430 if (err != 0) 431 if (err != 0)
431 goto out; 432 goto out;
432 sort_result(); 433 sort_result();
@@ -559,6 +560,7 @@ static int setup_sorting(struct list_head *sort_list, const char *arg)
559{ 560{
560 char *tok; 561 char *tok;
561 char *str = strdup(arg); 562 char *str = strdup(arg);
563 char *pos = str;
562 564
563 if (!str) { 565 if (!str) {
564 pr_err("%s: strdup failed\n", __func__); 566 pr_err("%s: strdup failed\n", __func__);
@@ -566,7 +568,7 @@ static int setup_sorting(struct list_head *sort_list, const char *arg)
566 } 568 }
567 569
568 while (true) { 570 while (true) {
569 tok = strsep(&str, ","); 571 tok = strsep(&pos, ",");
570 if (!tok) 572 if (!tok)
571 break; 573 break;
572 if (sort_dimension__add(tok, sort_list) < 0) { 574 if (sort_dimension__add(tok, sort_list) < 0) {
@@ -660,8 +662,13 @@ static int __cmd_record(int argc, const char **argv)
660int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused) 662int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
661{ 663{
662 const char * const default_sort_order = "frag,hit,bytes"; 664 const char * const default_sort_order = "frag,hit,bytes";
665 struct perf_data_file file = {
666 .mode = PERF_DATA_MODE_READ,
667 };
663 const struct option kmem_options[] = { 668 const struct option kmem_options[] = {
664 OPT_STRING('i', "input", &input_name, "file", "input file name"), 669 OPT_STRING('i', "input", &input_name, "file", "input file name"),
670 OPT_INCR('v', "verbose", &verbose,
671 "be more verbose (show symbol address, etc)"),
665 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL, 672 OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
666 "show per-callsite statistics", parse_caller_opt), 673 "show per-callsite statistics", parse_caller_opt),
667 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL, 674 OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL,
@@ -671,6 +678,7 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
671 parse_sort_opt), 678 parse_sort_opt),
672 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt), 679 OPT_CALLBACK('l', "line", NULL, "num", "show n lines", parse_line_opt),
673 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), 680 OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"),
681 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
674 OPT_END() 682 OPT_END()
675 }; 683 };
676 const char *const kmem_subcommands[] = { "record", "stat", NULL }; 684 const char *const kmem_subcommands[] = { "record", "stat", NULL };
@@ -679,10 +687,6 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
679 NULL 687 NULL
680 }; 688 };
681 struct perf_session *session; 689 struct perf_session *session;
682 struct perf_data_file file = {
683 .path = input_name,
684 .mode = PERF_DATA_MODE_READ,
685 };
686 int ret = -1; 690 int ret = -1;
687 691
688 argc = parse_options_subcommand(argc, argv, kmem_options, 692 argc = parse_options_subcommand(argc, argv, kmem_options,
@@ -696,6 +700,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
696 return __cmd_record(argc, argv); 700 return __cmd_record(argc, argv);
697 } 701 }
698 702
703 file.path = input_name;
704
699 session = perf_session__new(&file, false, &perf_kmem); 705 session = perf_session__new(&file, false, &perf_kmem);
700 if (session == NULL) 706 if (session == NULL)
701 return -1; 707 return -1;
@@ -703,6 +709,8 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused)
703 symbol__init(&session->header.env); 709 symbol__init(&session->header.env);
704 710
705 if (!strcmp(argv[0], "stat")) { 711 if (!strcmp(argv[0], "stat")) {
712 setlocale(LC_ALL, "");
713
706 if (cpu__setup_cpunode_map()) 714 if (cpu__setup_cpunode_map())
707 goto out_delete; 715 goto out_delete;
708 716
diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index 0894a817f67e..1f9338f6109c 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -18,6 +18,7 @@
18#include "util/stat.h" 18#include "util/stat.h"
19#include "util/top.h" 19#include "util/top.h"
20#include "util/data.h" 20#include "util/data.h"
21#include "util/ordered-events.h"
21 22
22#include <sys/prctl.h> 23#include <sys/prctl.h>
23#ifdef HAVE_TIMERFD_SUPPORT 24#ifdef HAVE_TIMERFD_SUPPORT
@@ -730,9 +731,9 @@ static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
730 return -1; 731 return -1;
731 } 732 }
732 733
733 err = perf_session_queue_event(kvm->session, event, &kvm->tool, &sample, 0); 734 err = perf_session__queue_event(kvm->session, event, &sample, 0);
734 /* 735 /*
735 * FIXME: Here we can't consume the event, as perf_session_queue_event will 736 * FIXME: Here we can't consume the event, as perf_session__queue_event will
736 * point to it, and it'll get possibly overwritten by the kernel. 737 * point to it, and it'll get possibly overwritten by the kernel.
737 */ 738 */
738 perf_evlist__mmap_consume(kvm->evlist, idx); 739 perf_evlist__mmap_consume(kvm->evlist, idx);
@@ -783,8 +784,10 @@ static int perf_kvm__mmap_read(struct perf_kvm_stat *kvm)
783 784
784 /* flush queue after each round in which we processed events */ 785 /* flush queue after each round in which we processed events */
785 if (ntotal) { 786 if (ntotal) {
786 kvm->session->ordered_events.next_flush = flush_time; 787 struct ordered_events *oe = &kvm->session->ordered_events;
787 err = kvm->tool.finished_round(&kvm->tool, NULL, kvm->session); 788
789 oe->next_flush = flush_time;
790 err = ordered_events__flush(oe, OE_FLUSH__ROUND);
788 if (err) { 791 if (err) {
789 if (kvm->lost_events) 792 if (kvm->lost_events)
790 pr_info("\nLost events: %" PRIu64 "\n\n", 793 pr_info("\nLost events: %" PRIu64 "\n\n",
@@ -1044,6 +1047,7 @@ static int read_events(struct perf_kvm_stat *kvm)
1044 struct perf_data_file file = { 1047 struct perf_data_file file = {
1045 .path = kvm->file_name, 1048 .path = kvm->file_name,
1046 .mode = PERF_DATA_MODE_READ, 1049 .mode = PERF_DATA_MODE_READ,
1050 .force = kvm->force,
1047 }; 1051 };
1048 1052
1049 kvm->tool = eops; 1053 kvm->tool = eops;
@@ -1066,7 +1070,7 @@ static int read_events(struct perf_kvm_stat *kvm)
1066 if (ret < 0) 1070 if (ret < 0)
1067 return ret; 1071 return ret;
1068 1072
1069 return perf_session__process_events(kvm->session, &kvm->tool); 1073 return perf_session__process_events(kvm->session);
1070} 1074}
1071 1075
1072static int parse_target_str(struct perf_kvm_stat *kvm) 1076static int parse_target_str(struct perf_kvm_stat *kvm)
@@ -1201,6 +1205,7 @@ kvm_events_report(struct perf_kvm_stat *kvm, int argc, const char **argv)
1201 " time (sort by avg time)"), 1205 " time (sort by avg time)"),
1202 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid", 1206 OPT_STRING('p', "pid", &kvm->opts.target.pid, "pid",
1203 "analyze events only for given process id(s)"), 1207 "analyze events only for given process id(s)"),
1208 OPT_BOOLEAN('f', "force", &kvm->force, "don't complain, do it"),
1204 OPT_END() 1209 OPT_END()
1205 }; 1210 };
1206 1211
diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
index 198f3c3aff95..af5bd0514108 100644
--- a/tools/perf/builtin-list.c
+++ b/tools/perf/builtin-list.c
@@ -36,38 +36,36 @@ int cmd_list(int argc, const char **argv, const char *prefix __maybe_unused)
36 36
37 setup_pager(); 37 setup_pager();
38 38
39 if (raw_dump) { 39 if (!raw_dump)
40 print_events(NULL, true); 40 printf("\nList of pre-defined events (to be used in -e):\n\n");
41 return 0;
42 }
43 41
44 if (argc == 0) { 42 if (argc == 0) {
45 print_events(NULL, false); 43 print_events(NULL, raw_dump);
46 return 0; 44 return 0;
47 } 45 }
48 46
49 for (i = 0; i < argc; ++i) { 47 for (i = 0; i < argc; ++i) {
50 if (i) 48 if (strcmp(argv[i], "tracepoint") == 0)
51 putchar('\n'); 49 print_tracepoint_events(NULL, NULL, raw_dump);
52 if (strncmp(argv[i], "tracepoint", 10) == 0)
53 print_tracepoint_events(NULL, NULL, false);
54 else if (strcmp(argv[i], "hw") == 0 || 50 else if (strcmp(argv[i], "hw") == 0 ||
55 strcmp(argv[i], "hardware") == 0) 51 strcmp(argv[i], "hardware") == 0)
56 print_events_type(PERF_TYPE_HARDWARE); 52 print_symbol_events(NULL, PERF_TYPE_HARDWARE,
53 event_symbols_hw, PERF_COUNT_HW_MAX, raw_dump);
57 else if (strcmp(argv[i], "sw") == 0 || 54 else if (strcmp(argv[i], "sw") == 0 ||
58 strcmp(argv[i], "software") == 0) 55 strcmp(argv[i], "software") == 0)
59 print_events_type(PERF_TYPE_SOFTWARE); 56 print_symbol_events(NULL, PERF_TYPE_SOFTWARE,
57 event_symbols_sw, PERF_COUNT_SW_MAX, raw_dump);
60 else if (strcmp(argv[i], "cache") == 0 || 58 else if (strcmp(argv[i], "cache") == 0 ||
61 strcmp(argv[i], "hwcache") == 0) 59 strcmp(argv[i], "hwcache") == 0)
62 print_hwcache_events(NULL, false); 60 print_hwcache_events(NULL, raw_dump);
63 else if (strcmp(argv[i], "pmu") == 0) 61 else if (strcmp(argv[i], "pmu") == 0)
64 print_pmu_events(NULL, false); 62 print_pmu_events(NULL, raw_dump);
65 else { 63 else {
66 char *sep = strchr(argv[i], ':'), *s; 64 char *sep = strchr(argv[i], ':'), *s;
67 int sep_idx; 65 int sep_idx;
68 66
69 if (sep == NULL) { 67 if (sep == NULL) {
70 print_events(argv[i], false); 68 print_events(argv[i], raw_dump);
71 continue; 69 continue;
72 } 70 }
73 sep_idx = sep - argv[i]; 71 sep_idx = sep - argv[i];
@@ -76,7 +74,7 @@ int cmd_list(int argc, const char **argv, const char *prefix __maybe_unused)
76 return -1; 74 return -1;
77 75
78 s[sep_idx] = '\0'; 76 s[sep_idx] = '\0';
79 print_tracepoint_events(s, s + sep_idx + 1, false); 77 print_tracepoint_events(s, s + sep_idx + 1, raw_dump);
80 free(s); 78 free(s);
81 } 79 }
82 } 80 }
diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index e7ec71589da6..d49c2ab85fc2 100644
--- a/tools/perf/builtin-lock.c
+++ b/tools/perf/builtin-lock.c
@@ -846,6 +846,8 @@ static const struct perf_evsel_str_handler lock_tracepoints[] = {
846 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */ 846 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
847}; 847};
848 848
849static bool force;
850
849static int __cmd_report(bool display_info) 851static int __cmd_report(bool display_info)
850{ 852{
851 int err = -EINVAL; 853 int err = -EINVAL;
@@ -857,6 +859,7 @@ static int __cmd_report(bool display_info)
857 struct perf_data_file file = { 859 struct perf_data_file file = {
858 .path = input_name, 860 .path = input_name,
859 .mode = PERF_DATA_MODE_READ, 861 .mode = PERF_DATA_MODE_READ,
862 .force = force,
860 }; 863 };
861 864
862 session = perf_session__new(&file, false, &eops); 865 session = perf_session__new(&file, false, &eops);
@@ -878,7 +881,7 @@ static int __cmd_report(bool display_info)
878 if (select_key()) 881 if (select_key())
879 goto out_delete; 882 goto out_delete;
880 883
881 err = perf_session__process_events(session, &eops); 884 err = perf_session__process_events(session);
882 if (err) 885 if (err)
883 goto out_delete; 886 goto out_delete;
884 887
@@ -945,6 +948,7 @@ int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
945 "dump thread list in perf.data"), 948 "dump thread list in perf.data"),
946 OPT_BOOLEAN('m', "map", &info_map, 949 OPT_BOOLEAN('m', "map", &info_map,
947 "map of lock instances (address:name table)"), 950 "map of lock instances (address:name table)"),
951 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
948 OPT_END() 952 OPT_END()
949 }; 953 };
950 const struct option lock_options[] = { 954 const struct option lock_options[] = {
@@ -956,6 +960,7 @@ int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
956 const struct option report_options[] = { 960 const struct option report_options[] = {
957 OPT_STRING('k', "key", &sort_key, "acquired", 961 OPT_STRING('k', "key", &sort_key, "acquired",
958 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"), 962 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
963 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
959 /* TODO: type */ 964 /* TODO: type */
960 OPT_END() 965 OPT_END()
961 }; 966 };
diff --git a/tools/perf/builtin-mem.c b/tools/perf/builtin-mem.c
index 9b5663950a4d..675216e08bfc 100644
--- a/tools/perf/builtin-mem.c
+++ b/tools/perf/builtin-mem.c
@@ -15,6 +15,7 @@ struct perf_mem {
15 char const *input_name; 15 char const *input_name;
16 bool hide_unresolved; 16 bool hide_unresolved;
17 bool dump_raw; 17 bool dump_raw;
18 bool force;
18 int operation; 19 int operation;
19 const char *cpu_list; 20 const char *cpu_list;
20 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 21 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
@@ -120,6 +121,7 @@ static int report_raw_events(struct perf_mem *mem)
120 struct perf_data_file file = { 121 struct perf_data_file file = {
121 .path = input_name, 122 .path = input_name,
122 .mode = PERF_DATA_MODE_READ, 123 .mode = PERF_DATA_MODE_READ,
124 .force = mem->force,
123 }; 125 };
124 int err = -EINVAL; 126 int err = -EINVAL;
125 int ret; 127 int ret;
@@ -141,7 +143,7 @@ static int report_raw_events(struct perf_mem *mem)
141 143
142 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n"); 144 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
143 145
144 err = perf_session__process_events(session, &mem->tool); 146 err = perf_session__process_events(session);
145 if (err) 147 if (err)
146 return err; 148 return err;
147 149
@@ -286,10 +288,11 @@ int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
286 "input file name"), 288 "input file name"),
287 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", 289 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
288 "list of cpus to profile"), 290 "list of cpus to profile"),
289 OPT_STRING('x', "field-separator", &symbol_conf.field_sep, 291 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
290 "separator", 292 "separator",
291 "separator for columns, no spaces will be added" 293 "separator for columns, no spaces will be added"
292 " between columns '.' is reserved."), 294 " between columns '.' is reserved."),
295 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
293 OPT_END() 296 OPT_END()
294 }; 297 };
295 const char *const mem_subcommands[] = { "record", "report", NULL }; 298 const char *const mem_subcommands[] = { "record", "report", NULL };
diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index 921bb6942503..f7b1af67e9f6 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -56,6 +56,7 @@ static struct {
56 bool mod_events; 56 bool mod_events;
57 bool uprobes; 57 bool uprobes;
58 bool quiet; 58 bool quiet;
59 bool target_used;
59 int nevents; 60 int nevents;
60 struct perf_probe_event events[MAX_PROBES]; 61 struct perf_probe_event events[MAX_PROBES];
61 struct strlist *dellist; 62 struct strlist *dellist;
@@ -78,6 +79,12 @@ static int parse_probe_event(const char *str)
78 } 79 }
79 80
80 pev->uprobes = params.uprobes; 81 pev->uprobes = params.uprobes;
82 if (params.target) {
83 pev->target = strdup(params.target);
84 if (!pev->target)
85 return -ENOMEM;
86 params.target_used = true;
87 }
81 88
82 /* Parse a perf-probe command into event */ 89 /* Parse a perf-probe command into event */
83 ret = parse_perf_probe_command(str, pev); 90 ret = parse_perf_probe_command(str, pev);
@@ -102,6 +109,7 @@ static int set_target(const char *ptr)
102 params.target = strdup(ptr); 109 params.target = strdup(ptr);
103 if (!params.target) 110 if (!params.target)
104 return -ENOMEM; 111 return -ENOMEM;
112 params.target_used = false;
105 113
106 found = 1; 114 found = 1;
107 buf = ptr + (strlen(ptr) - 3); 115 buf = ptr + (strlen(ptr) - 3);
@@ -178,7 +186,7 @@ static int opt_set_target(const struct option *opt, const char *str,
178 int ret = -ENOENT; 186 int ret = -ENOENT;
179 char *tmp; 187 char *tmp;
180 188
181 if (str && !params.target) { 189 if (str) {
182 if (!strcmp(opt->long_name, "exec")) 190 if (!strcmp(opt->long_name, "exec"))
183 params.uprobes = true; 191 params.uprobes = true;
184#ifdef HAVE_DWARF_SUPPORT 192#ifdef HAVE_DWARF_SUPPORT
@@ -200,7 +208,9 @@ static int opt_set_target(const struct option *opt, const char *str,
200 if (!tmp) 208 if (!tmp)
201 return -ENOMEM; 209 return -ENOMEM;
202 } 210 }
211 free(params.target);
203 params.target = tmp; 212 params.target = tmp;
213 params.target_used = false;
204 ret = 0; 214 ret = 0;
205 } 215 }
206 216
@@ -485,9 +495,14 @@ __cmd_probe(int argc, const char **argv, const char *prefix __maybe_unused)
485 } 495 }
486 496
487 if (params.nevents) { 497 if (params.nevents) {
498 /* Ensure the last given target is used */
499 if (params.target && !params.target_used) {
500 pr_warning(" Error: -x/-m must follow the probe definitions.\n");
501 usage_with_options(probe_usage, options);
502 }
503
488 ret = add_perf_probe_events(params.events, params.nevents, 504 ret = add_perf_probe_events(params.events, params.nevents,
489 params.max_probe_points, 505 params.max_probe_points,
490 params.target,
491 params.force_add); 506 params.force_add);
492 if (ret < 0) { 507 if (ret < 0) {
493 pr_err_with_code(" Error: Failed to add events.", ret); 508 pr_err_with_code(" Error: Failed to add events.", ret);
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 404ab3434052..c3efdfb630b5 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -70,8 +70,8 @@ static int process_synthesized_event(struct perf_tool *tool,
70static int record__mmap_read(struct record *rec, int idx) 70static int record__mmap_read(struct record *rec, int idx)
71{ 71{
72 struct perf_mmap *md = &rec->evlist->mmap[idx]; 72 struct perf_mmap *md = &rec->evlist->mmap[idx];
73 unsigned int head = perf_mmap__read_head(md); 73 u64 head = perf_mmap__read_head(md);
74 unsigned int old = md->prev; 74 u64 old = md->prev;
75 unsigned char *data = md->base + page_size; 75 unsigned char *data = md->base + page_size;
76 unsigned long size; 76 unsigned long size;
77 void *buf; 77 void *buf;
@@ -161,8 +161,9 @@ try_again:
161 } 161 }
162 } 162 }
163 163
164 if (perf_evlist__apply_filters(evlist)) { 164 if (perf_evlist__apply_filters(evlist, &pos)) {
165 error("failed to set filter with %d (%s)\n", errno, 165 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
166 pos->filter, perf_evsel__name(pos), errno,
166 strerror_r(errno, msg, sizeof(msg))); 167 strerror_r(errno, msg, sizeof(msg)));
167 rc = -1; 168 rc = -1;
168 goto out; 169 goto out;
@@ -225,7 +226,7 @@ static int process_buildids(struct record *rec)
225 */ 226 */
226 symbol_conf.ignore_vmlinux_buildid = true; 227 symbol_conf.ignore_vmlinux_buildid = true;
227 228
228 return perf_session__process_events(session, &rec->tool); 229 return perf_session__process_events(session);
229} 230}
230 231
231static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 232static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
@@ -343,7 +344,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
343 signal(SIGINT, sig_handler); 344 signal(SIGINT, sig_handler);
344 signal(SIGTERM, sig_handler); 345 signal(SIGTERM, sig_handler);
345 346
346 session = perf_session__new(file, false, NULL); 347 session = perf_session__new(file, false, tool);
347 if (session == NULL) { 348 if (session == NULL) {
348 pr_err("Perf session creation failed.\n"); 349 pr_err("Perf session creation failed.\n");
349 return -1; 350 return -1;
@@ -658,7 +659,7 @@ error:
658 659
659static void callchain_debug(void) 660static void callchain_debug(void)
660{ 661{
661 static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF" }; 662 static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
662 663
663 pr_debug("callchain: type %s\n", str[callchain_param.record_mode]); 664 pr_debug("callchain: type %s\n", str[callchain_param.record_mode]);
664 665
@@ -710,6 +711,90 @@ static int perf_record_config(const char *var, const char *value, void *cb)
710 return perf_default_config(var, value, cb); 711 return perf_default_config(var, value, cb);
711} 712}
712 713
714struct clockid_map {
715 const char *name;
716 int clockid;
717};
718
719#define CLOCKID_MAP(n, c) \
720 { .name = n, .clockid = (c), }
721
722#define CLOCKID_END { .name = NULL, }
723
724
725/*
726 * Add the missing ones, we need to build on many distros...
727 */
728#ifndef CLOCK_MONOTONIC_RAW
729#define CLOCK_MONOTONIC_RAW 4
730#endif
731#ifndef CLOCK_BOOTTIME
732#define CLOCK_BOOTTIME 7
733#endif
734#ifndef CLOCK_TAI
735#define CLOCK_TAI 11
736#endif
737
738static const struct clockid_map clockids[] = {
739 /* available for all events, NMI safe */
740 CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
741 CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
742
743 /* available for some events */
744 CLOCKID_MAP("realtime", CLOCK_REALTIME),
745 CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
746 CLOCKID_MAP("tai", CLOCK_TAI),
747
748 /* available for the lazy */
749 CLOCKID_MAP("mono", CLOCK_MONOTONIC),
750 CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
751 CLOCKID_MAP("real", CLOCK_REALTIME),
752 CLOCKID_MAP("boot", CLOCK_BOOTTIME),
753
754 CLOCKID_END,
755};
756
757static int parse_clockid(const struct option *opt, const char *str, int unset)
758{
759 struct record_opts *opts = (struct record_opts *)opt->value;
760 const struct clockid_map *cm;
761 const char *ostr = str;
762
763 if (unset) {
764 opts->use_clockid = 0;
765 return 0;
766 }
767
768 /* no arg passed */
769 if (!str)
770 return 0;
771
772 /* no setting it twice */
773 if (opts->use_clockid)
774 return -1;
775
776 opts->use_clockid = true;
777
778 /* if its a number, we're done */
779 if (sscanf(str, "%d", &opts->clockid) == 1)
780 return 0;
781
782 /* allow a "CLOCK_" prefix to the name */
783 if (!strncasecmp(str, "CLOCK_", 6))
784 str += 6;
785
786 for (cm = clockids; cm->name; cm++) {
787 if (!strcasecmp(str, cm->name)) {
788 opts->clockid = cm->clockid;
789 return 0;
790 }
791 }
792
793 opts->use_clockid = false;
794 ui__warning("unknown clockid %s, check man page\n", ostr);
795 return -1;
796}
797
713static const char * const __record_usage[] = { 798static const char * const __record_usage[] = {
714 "perf record [<options>] [<command>]", 799 "perf record [<options>] [<command>]",
715 "perf record [<options>] -- <command> [<options>]", 800 "perf record [<options>] -- <command> [<options>]",
@@ -751,9 +836,9 @@ static struct record record = {
751#define CALLCHAIN_HELP "setup and enables call-graph (stack chain/backtrace) recording: " 836#define CALLCHAIN_HELP "setup and enables call-graph (stack chain/backtrace) recording: "
752 837
753#ifdef HAVE_DWARF_UNWIND_SUPPORT 838#ifdef HAVE_DWARF_UNWIND_SUPPORT
754const char record_callchain_help[] = CALLCHAIN_HELP "fp dwarf"; 839const char record_callchain_help[] = CALLCHAIN_HELP "fp dwarf lbr";
755#else 840#else
756const char record_callchain_help[] = CALLCHAIN_HELP "fp"; 841const char record_callchain_help[] = CALLCHAIN_HELP "fp lbr";
757#endif 842#endif
758 843
759/* 844/*
@@ -839,6 +924,11 @@ struct option __record_options[] = {
839 "use per-thread mmaps"), 924 "use per-thread mmaps"),
840 OPT_BOOLEAN('I', "intr-regs", &record.opts.sample_intr_regs, 925 OPT_BOOLEAN('I', "intr-regs", &record.opts.sample_intr_regs,
841 "Sample machine registers on interrupt"), 926 "Sample machine registers on interrupt"),
927 OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
928 "Record running/enabled time of read (:S) events"),
929 OPT_CALLBACK('k', "clockid", &record.opts,
930 "clockid", "clockid to use for events, see clock_gettime()",
931 parse_clockid),
842 OPT_END() 932 OPT_END()
843}; 933};
844 934
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 2f91094e228b..476cdf7afcca 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -249,6 +249,8 @@ static int report__setup_sample_type(struct report *rep)
249 if ((sample_type & PERF_SAMPLE_REGS_USER) && 249 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
250 (sample_type & PERF_SAMPLE_STACK_USER)) 250 (sample_type & PERF_SAMPLE_STACK_USER))
251 callchain_param.record_mode = CALLCHAIN_DWARF; 251 callchain_param.record_mode = CALLCHAIN_DWARF;
252 else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
253 callchain_param.record_mode = CALLCHAIN_LBR;
252 else 254 else
253 callchain_param.record_mode = CALLCHAIN_FP; 255 callchain_param.record_mode = CALLCHAIN_FP;
254 } 256 }
@@ -302,7 +304,7 @@ static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report
302 304
303 if (rep->mem_mode) { 305 if (rep->mem_mode) {
304 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events); 306 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
305 ret += fprintf(fp, "\n# Sort order : %s", sort_order); 307 ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
306 } else 308 } else
307 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events); 309 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
308 return ret + fprintf(fp, "\n#\n"); 310 return ret + fprintf(fp, "\n#\n");
@@ -345,7 +347,7 @@ static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
345static void report__warn_kptr_restrict(const struct report *rep) 347static void report__warn_kptr_restrict(const struct report *rep)
346{ 348{
347 struct map *kernel_map = rep->session->machines.host.vmlinux_maps[MAP__FUNCTION]; 349 struct map *kernel_map = rep->session->machines.host.vmlinux_maps[MAP__FUNCTION];
348 struct kmap *kernel_kmap = map__kmap(kernel_map); 350 struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
349 351
350 if (kernel_map == NULL || 352 if (kernel_map == NULL ||
351 (kernel_map->dso->hit && 353 (kernel_map->dso->hit &&
@@ -480,7 +482,7 @@ static int __cmd_report(struct report *rep)
480 if (ret) 482 if (ret)
481 return ret; 483 return ret;
482 484
483 ret = perf_session__process_events(session, &rep->tool); 485 ret = perf_session__process_events(session);
484 if (ret) 486 if (ret)
485 return ret; 487 return ret;
486 488
@@ -667,6 +669,10 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
667 "only consider symbols in these dsos"), 669 "only consider symbols in these dsos"),
668 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 670 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
669 "only consider symbols in these comms"), 671 "only consider symbols in these comms"),
672 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
673 "only consider symbols in these pids"),
674 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
675 "only consider symbols in these tids"),
670 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", 676 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
671 "only consider these symbols"), 677 "only consider these symbols"),
672 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter", 678 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
@@ -674,7 +680,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
674 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, 680 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
675 "width[,width...]", 681 "width[,width...]",
676 "don't try to adjust column width, use these fixed values"), 682 "don't try to adjust column width, use these fixed values"),
677 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", 683 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
678 "separator for columns, no spaces will be added between " 684 "separator for columns, no spaces will be added between "
679 "columns '.' is reserved."), 685 "columns '.' is reserved."),
680 OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved, 686 OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
@@ -766,7 +772,7 @@ repeat:
766 * 0/1 means the user chose a mode. 772 * 0/1 means the user chose a mode.
767 */ 773 */
768 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) && 774 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
769 branch_call_mode == -1) { 775 !branch_call_mode) {
770 sort__mode = SORT_MODE__BRANCH; 776 sort__mode = SORT_MODE__BRANCH;
771 symbol_conf.cumulate_callchain = false; 777 symbol_conf.cumulate_callchain = false;
772 } 778 }
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 891c3930080e..5275bab70313 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -23,12 +23,13 @@
23#include <semaphore.h> 23#include <semaphore.h>
24#include <pthread.h> 24#include <pthread.h>
25#include <math.h> 25#include <math.h>
26#include <api/fs/fs.h>
26 27
27#define PR_SET_NAME 15 /* Set process name */ 28#define PR_SET_NAME 15 /* Set process name */
28#define MAX_CPUS 4096 29#define MAX_CPUS 4096
29#define COMM_LEN 20 30#define COMM_LEN 20
30#define SYM_LEN 129 31#define SYM_LEN 129
31#define MAX_PID 65536 32#define MAX_PID 1024000
32 33
33struct sched_atom; 34struct sched_atom;
34 35
@@ -124,7 +125,7 @@ struct perf_sched {
124 struct perf_tool tool; 125 struct perf_tool tool;
125 const char *sort_order; 126 const char *sort_order;
126 unsigned long nr_tasks; 127 unsigned long nr_tasks;
127 struct task_desc *pid_to_task[MAX_PID]; 128 struct task_desc **pid_to_task;
128 struct task_desc **tasks; 129 struct task_desc **tasks;
129 const struct trace_sched_handler *tp_handler; 130 const struct trace_sched_handler *tp_handler;
130 pthread_mutex_t start_work_mutex; 131 pthread_mutex_t start_work_mutex;
@@ -169,6 +170,7 @@ struct perf_sched {
169 u64 cpu_last_switched[MAX_CPUS]; 170 u64 cpu_last_switched[MAX_CPUS];
170 struct rb_root atom_root, sorted_atom_root; 171 struct rb_root atom_root, sorted_atom_root;
171 struct list_head sort_list, cmp_pid; 172 struct list_head sort_list, cmp_pid;
173 bool force;
172}; 174};
173 175
174static u64 get_nsecs(void) 176static u64 get_nsecs(void)
@@ -326,8 +328,19 @@ static struct task_desc *register_pid(struct perf_sched *sched,
326 unsigned long pid, const char *comm) 328 unsigned long pid, const char *comm)
327{ 329{
328 struct task_desc *task; 330 struct task_desc *task;
331 static int pid_max;
329 332
330 BUG_ON(pid >= MAX_PID); 333 if (sched->pid_to_task == NULL) {
334 if (sysctl__read_int("kernel/pid_max", &pid_max) < 0)
335 pid_max = MAX_PID;
336 BUG_ON((sched->pid_to_task = calloc(pid_max, sizeof(struct task_desc *))) == NULL);
337 }
338 if (pid >= (unsigned long)pid_max) {
339 BUG_ON((sched->pid_to_task = realloc(sched->pid_to_task, (pid + 1) *
340 sizeof(struct task_desc *))) == NULL);
341 while (pid >= (unsigned long)pid_max)
342 sched->pid_to_task[pid_max++] = NULL;
343 }
331 344
332 task = sched->pid_to_task[pid]; 345 task = sched->pid_to_task[pid];
333 346
@@ -346,7 +359,7 @@ static struct task_desc *register_pid(struct perf_sched *sched,
346 359
347 sched->pid_to_task[pid] = task; 360 sched->pid_to_task[pid] = task;
348 sched->nr_tasks++; 361 sched->nr_tasks++;
349 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_task *)); 362 sched->tasks = realloc(sched->tasks, sched->nr_tasks * sizeof(struct task_desc *));
350 BUG_ON(!sched->tasks); 363 BUG_ON(!sched->tasks);
351 sched->tasks[task->nr] = task; 364 sched->tasks[task->nr] = task;
352 365
@@ -425,24 +438,45 @@ static u64 get_cpu_usage_nsec_parent(void)
425 return sum; 438 return sum;
426} 439}
427 440
428static int self_open_counters(void) 441static int self_open_counters(struct perf_sched *sched, unsigned long cur_task)
429{ 442{
430 struct perf_event_attr attr; 443 struct perf_event_attr attr;
431 char sbuf[STRERR_BUFSIZE]; 444 char sbuf[STRERR_BUFSIZE], info[STRERR_BUFSIZE];
432 int fd; 445 int fd;
446 struct rlimit limit;
447 bool need_privilege = false;
433 448
434 memset(&attr, 0, sizeof(attr)); 449 memset(&attr, 0, sizeof(attr));
435 450
436 attr.type = PERF_TYPE_SOFTWARE; 451 attr.type = PERF_TYPE_SOFTWARE;
437 attr.config = PERF_COUNT_SW_TASK_CLOCK; 452 attr.config = PERF_COUNT_SW_TASK_CLOCK;
438 453
454force_again:
439 fd = sys_perf_event_open(&attr, 0, -1, -1, 455 fd = sys_perf_event_open(&attr, 0, -1, -1,
440 perf_event_open_cloexec_flag()); 456 perf_event_open_cloexec_flag());
441 457
442 if (fd < 0) 458 if (fd < 0) {
459 if (errno == EMFILE) {
460 if (sched->force) {
461 BUG_ON(getrlimit(RLIMIT_NOFILE, &limit) == -1);
462 limit.rlim_cur += sched->nr_tasks - cur_task;
463 if (limit.rlim_cur > limit.rlim_max) {
464 limit.rlim_max = limit.rlim_cur;
465 need_privilege = true;
466 }
467 if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
468 if (need_privilege && errno == EPERM)
469 strcpy(info, "Need privilege\n");
470 } else
471 goto force_again;
472 } else
473 strcpy(info, "Have a try with -f option\n");
474 }
443 pr_err("Error: sys_perf_event_open() syscall returned " 475 pr_err("Error: sys_perf_event_open() syscall returned "
444 "with %d (%s)\n", fd, 476 "with %d (%s)\n%s", fd,
445 strerror_r(errno, sbuf, sizeof(sbuf))); 477 strerror_r(errno, sbuf, sizeof(sbuf)), info);
478 exit(EXIT_FAILURE);
479 }
446 return fd; 480 return fd;
447} 481}
448 482
@@ -460,6 +494,7 @@ static u64 get_cpu_usage_nsec_self(int fd)
460struct sched_thread_parms { 494struct sched_thread_parms {
461 struct task_desc *task; 495 struct task_desc *task;
462 struct perf_sched *sched; 496 struct perf_sched *sched;
497 int fd;
463}; 498};
464 499
465static void *thread_func(void *ctx) 500static void *thread_func(void *ctx)
@@ -470,13 +505,12 @@ static void *thread_func(void *ctx)
470 u64 cpu_usage_0, cpu_usage_1; 505 u64 cpu_usage_0, cpu_usage_1;
471 unsigned long i, ret; 506 unsigned long i, ret;
472 char comm2[22]; 507 char comm2[22];
473 int fd; 508 int fd = parms->fd;
474 509
475 zfree(&parms); 510 zfree(&parms);
476 511
477 sprintf(comm2, ":%s", this_task->comm); 512 sprintf(comm2, ":%s", this_task->comm);
478 prctl(PR_SET_NAME, comm2); 513 prctl(PR_SET_NAME, comm2);
479 fd = self_open_counters();
480 if (fd < 0) 514 if (fd < 0)
481 return NULL; 515 return NULL;
482again: 516again:
@@ -528,6 +562,7 @@ static void create_tasks(struct perf_sched *sched)
528 BUG_ON(parms == NULL); 562 BUG_ON(parms == NULL);
529 parms->task = task = sched->tasks[i]; 563 parms->task = task = sched->tasks[i];
530 parms->sched = sched; 564 parms->sched = sched;
565 parms->fd = self_open_counters(sched, i);
531 sem_init(&task->sleep_sem, 0, 0); 566 sem_init(&task->sleep_sem, 0, 0);
532 sem_init(&task->ready_for_work, 0, 0); 567 sem_init(&task->ready_for_work, 0, 0);
533 sem_init(&task->work_done_sem, 0, 0); 568 sem_init(&task->work_done_sem, 0, 0);
@@ -572,13 +607,13 @@ static void wait_for_tasks(struct perf_sched *sched)
572 cpu_usage_1 = get_cpu_usage_nsec_parent(); 607 cpu_usage_1 = get_cpu_usage_nsec_parent();
573 if (!sched->runavg_cpu_usage) 608 if (!sched->runavg_cpu_usage)
574 sched->runavg_cpu_usage = sched->cpu_usage; 609 sched->runavg_cpu_usage = sched->cpu_usage;
575 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * 9 + sched->cpu_usage) / 10; 610 sched->runavg_cpu_usage = (sched->runavg_cpu_usage * (sched->replay_repeat - 1) + sched->cpu_usage) / sched->replay_repeat;
576 611
577 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0; 612 sched->parent_cpu_usage = cpu_usage_1 - cpu_usage_0;
578 if (!sched->runavg_parent_cpu_usage) 613 if (!sched->runavg_parent_cpu_usage)
579 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage; 614 sched->runavg_parent_cpu_usage = sched->parent_cpu_usage;
580 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * 9 + 615 sched->runavg_parent_cpu_usage = (sched->runavg_parent_cpu_usage * (sched->replay_repeat - 1) +
581 sched->parent_cpu_usage)/10; 616 sched->parent_cpu_usage)/sched->replay_repeat;
582 617
583 ret = pthread_mutex_lock(&sched->start_work_mutex); 618 ret = pthread_mutex_lock(&sched->start_work_mutex);
584 BUG_ON(ret); 619 BUG_ON(ret);
@@ -610,7 +645,7 @@ static void run_one_test(struct perf_sched *sched)
610 sched->sum_fluct += fluct; 645 sched->sum_fluct += fluct;
611 if (!sched->run_avg) 646 if (!sched->run_avg)
612 sched->run_avg = delta; 647 sched->run_avg = delta;
613 sched->run_avg = (sched->run_avg * 9 + delta) / 10; 648 sched->run_avg = (sched->run_avg * (sched->replay_repeat - 1) + delta) / sched->replay_repeat;
614 649
615 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0); 650 printf("#%-3ld: %0.3f, ", sched->nr_runs, (double)delta / 1000000.0);
616 651
@@ -831,7 +866,7 @@ static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
831 return -1; 866 return -1;
832 } 867 }
833 868
834 atoms->thread = thread; 869 atoms->thread = thread__get(thread);
835 INIT_LIST_HEAD(&atoms->work_list); 870 INIT_LIST_HEAD(&atoms->work_list);
836 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid); 871 __thread_latency_insert(&sched->atom_root, atoms, &sched->cmp_pid);
837 return 0; 872 return 0;
@@ -1439,8 +1474,7 @@ static int perf_sched__process_tracepoint_sample(struct perf_tool *tool __maybe_
1439 return err; 1474 return err;
1440} 1475}
1441 1476
1442static int perf_sched__read_events(struct perf_sched *sched, 1477static int perf_sched__read_events(struct perf_sched *sched)
1443 struct perf_session **psession)
1444{ 1478{
1445 const struct perf_evsel_str_handler handlers[] = { 1479 const struct perf_evsel_str_handler handlers[] = {
1446 { "sched:sched_switch", process_sched_switch_event, }, 1480 { "sched:sched_switch", process_sched_switch_event, },
@@ -1453,7 +1487,9 @@ static int perf_sched__read_events(struct perf_sched *sched,
1453 struct perf_data_file file = { 1487 struct perf_data_file file = {
1454 .path = input_name, 1488 .path = input_name,
1455 .mode = PERF_DATA_MODE_READ, 1489 .mode = PERF_DATA_MODE_READ,
1490 .force = sched->force,
1456 }; 1491 };
1492 int rc = -1;
1457 1493
1458 session = perf_session__new(&file, false, &sched->tool); 1494 session = perf_session__new(&file, false, &sched->tool);
1459 if (session == NULL) { 1495 if (session == NULL) {
@@ -1467,27 +1503,21 @@ static int perf_sched__read_events(struct perf_sched *sched,
1467 goto out_delete; 1503 goto out_delete;
1468 1504
1469 if (perf_session__has_traces(session, "record -R")) { 1505 if (perf_session__has_traces(session, "record -R")) {
1470 int err = perf_session__process_events(session, &sched->tool); 1506 int err = perf_session__process_events(session);
1471 if (err) { 1507 if (err) {
1472 pr_err("Failed to process events, error %d", err); 1508 pr_err("Failed to process events, error %d", err);
1473 goto out_delete; 1509 goto out_delete;
1474 } 1510 }
1475 1511
1476 sched->nr_events = session->stats.nr_events[0]; 1512 sched->nr_events = session->evlist->stats.nr_events[0];
1477 sched->nr_lost_events = session->stats.total_lost; 1513 sched->nr_lost_events = session->evlist->stats.total_lost;
1478 sched->nr_lost_chunks = session->stats.nr_events[PERF_RECORD_LOST]; 1514 sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST];
1479 } 1515 }
1480 1516
1481 if (psession) 1517 rc = 0;
1482 *psession = session;
1483 else
1484 perf_session__delete(session);
1485
1486 return 0;
1487
1488out_delete: 1518out_delete:
1489 perf_session__delete(session); 1519 perf_session__delete(session);
1490 return -1; 1520 return rc;
1491} 1521}
1492 1522
1493static void print_bad_events(struct perf_sched *sched) 1523static void print_bad_events(struct perf_sched *sched)
@@ -1515,12 +1545,10 @@ static void print_bad_events(struct perf_sched *sched)
1515static int perf_sched__lat(struct perf_sched *sched) 1545static int perf_sched__lat(struct perf_sched *sched)
1516{ 1546{
1517 struct rb_node *next; 1547 struct rb_node *next;
1518 struct perf_session *session;
1519 1548
1520 setup_pager(); 1549 setup_pager();
1521 1550
1522 /* save session -- references to threads are held in work_list */ 1551 if (perf_sched__read_events(sched))
1523 if (perf_sched__read_events(sched, &session))
1524 return -1; 1552 return -1;
1525 1553
1526 perf_sched__sort_lat(sched); 1554 perf_sched__sort_lat(sched);
@@ -1537,6 +1565,7 @@ static int perf_sched__lat(struct perf_sched *sched)
1537 work_list = rb_entry(next, struct work_atoms, node); 1565 work_list = rb_entry(next, struct work_atoms, node);
1538 output_lat_thread(sched, work_list); 1566 output_lat_thread(sched, work_list);
1539 next = rb_next(next); 1567 next = rb_next(next);
1568 thread__zput(work_list->thread);
1540 } 1569 }
1541 1570
1542 printf(" -----------------------------------------------------------------------------------------------------------------\n"); 1571 printf(" -----------------------------------------------------------------------------------------------------------------\n");
@@ -1548,7 +1577,6 @@ static int perf_sched__lat(struct perf_sched *sched)
1548 print_bad_events(sched); 1577 print_bad_events(sched);
1549 printf("\n"); 1578 printf("\n");
1550 1579
1551 perf_session__delete(session);
1552 return 0; 1580 return 0;
1553} 1581}
1554 1582
@@ -1557,7 +1585,7 @@ static int perf_sched__map(struct perf_sched *sched)
1557 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF); 1585 sched->max_cpu = sysconf(_SC_NPROCESSORS_CONF);
1558 1586
1559 setup_pager(); 1587 setup_pager();
1560 if (perf_sched__read_events(sched, NULL)) 1588 if (perf_sched__read_events(sched))
1561 return -1; 1589 return -1;
1562 print_bad_events(sched); 1590 print_bad_events(sched);
1563 return 0; 1591 return 0;
@@ -1572,7 +1600,7 @@ static int perf_sched__replay(struct perf_sched *sched)
1572 1600
1573 test_calibrations(sched); 1601 test_calibrations(sched);
1574 1602
1575 if (perf_sched__read_events(sched, NULL)) 1603 if (perf_sched__read_events(sched))
1576 return -1; 1604 return -1;
1577 1605
1578 printf("nr_run_events: %ld\n", sched->nr_run_events); 1606 printf("nr_run_events: %ld\n", sched->nr_run_events);
@@ -1693,6 +1721,7 @@ int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)
1693 "be more verbose (show symbol address, etc)"), 1721 "be more verbose (show symbol address, etc)"),
1694 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1722 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1695 "dump raw trace in ASCII"), 1723 "dump raw trace in ASCII"),
1724 OPT_BOOLEAN('f', "force", &sched.force, "don't complain, do it"),
1696 OPT_END() 1725 OPT_END()
1697 }; 1726 };
1698 const struct option sched_options[] = { 1727 const struct option sched_options[] = {
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index ce304dfd962a..58f10b8e6ff2 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -446,9 +446,9 @@ static void print_sample_bts(union perf_event *event,
446} 446}
447 447
448static void process_event(union perf_event *event, struct perf_sample *sample, 448static void process_event(union perf_event *event, struct perf_sample *sample,
449 struct perf_evsel *evsel, struct thread *thread, 449 struct perf_evsel *evsel, struct addr_location *al)
450 struct addr_location *al)
451{ 450{
451 struct thread *thread = al->thread;
452 struct perf_event_attr *attr = &evsel->attr; 452 struct perf_event_attr *attr = &evsel->attr;
453 453
454 if (output[attr->type].fields == 0) 454 if (output[attr->type].fields == 0)
@@ -549,14 +549,6 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
549 struct machine *machine) 549 struct machine *machine)
550{ 550{
551 struct addr_location al; 551 struct addr_location al;
552 struct thread *thread = machine__findnew_thread(machine, sample->pid,
553 sample->tid);
554
555 if (thread == NULL) {
556 pr_debug("problem processing %d event, skipping it.\n",
557 event->header.type);
558 return -1;
559 }
560 552
561 if (debug_mode) { 553 if (debug_mode) {
562 if (sample->time < last_timestamp) { 554 if (sample->time < last_timestamp) {
@@ -581,7 +573,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
581 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) 573 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
582 return 0; 574 return 0;
583 575
584 scripting_ops->process_event(event, sample, evsel, thread, &al); 576 scripting_ops->process_event(event, sample, evsel, &al);
585 577
586 return 0; 578 return 0;
587} 579}
@@ -800,7 +792,7 @@ static int __cmd_script(struct perf_script *script)
800 script->tool.mmap2 = process_mmap2_event; 792 script->tool.mmap2 = process_mmap2_event;
801 } 793 }
802 794
803 ret = perf_session__process_events(script->session, &script->tool); 795 ret = perf_session__process_events(script->session);
804 796
805 if (debug_mode) 797 if (debug_mode)
806 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); 798 pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
@@ -1523,6 +1515,9 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1523 .ordering_requires_timestamps = true, 1515 .ordering_requires_timestamps = true,
1524 }, 1516 },
1525 }; 1517 };
1518 struct perf_data_file file = {
1519 .mode = PERF_DATA_MODE_READ,
1520 };
1526 const struct option options[] = { 1521 const struct option options[] = {
1527 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1522 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1528 "dump raw trace in ASCII"), 1523 "dump raw trace in ASCII"),
@@ -1550,7 +1545,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1550 "When printing symbols do not display call chain"), 1545 "When printing symbols do not display call chain"),
1551 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", 1546 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
1552 "Look for files with symbols relative to this directory"), 1547 "Look for files with symbols relative to this directory"),
1553 OPT_CALLBACK('f', "fields", NULL, "str", 1548 OPT_CALLBACK('F', "fields", NULL, "str",
1554 "comma separated output fields prepend with 'type:'. " 1549 "comma separated output fields prepend with 'type:'. "
1555 "Valid types: hw,sw,trace,raw. " 1550 "Valid types: hw,sw,trace,raw. "
1556 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," 1551 "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
@@ -1562,6 +1557,10 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1562 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"), 1557 OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
1563 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 1558 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1564 "only display events for these comms"), 1559 "only display events for these comms"),
1560 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1561 "only consider symbols in these pids"),
1562 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1563 "only consider symbols in these tids"),
1565 OPT_BOOLEAN('I', "show-info", &show_full_info, 1564 OPT_BOOLEAN('I', "show-info", &show_full_info,
1566 "display extended information from perf.data file"), 1565 "display extended information from perf.data file"),
1567 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path, 1566 OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
@@ -1570,9 +1569,11 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1570 "Show the fork/comm/exit events"), 1569 "Show the fork/comm/exit events"),
1571 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events, 1570 OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
1572 "Show the mmap events"), 1571 "Show the mmap events"),
1572 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
1573 OPT_END() 1573 OPT_END()
1574 }; 1574 };
1575 const char * const script_usage[] = { 1575 const char * const script_subcommands[] = { "record", "report", NULL };
1576 const char *script_usage[] = {
1576 "perf script [<options>]", 1577 "perf script [<options>]",
1577 "perf script [<options>] record <script> [<record-options>] <command>", 1578 "perf script [<options>] record <script> [<record-options>] <command>",
1578 "perf script [<options>] report <script> [script-args]", 1579 "perf script [<options>] report <script> [script-args]",
@@ -1580,13 +1581,10 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
1580 "perf script [<options>] <top-script> [script-args]", 1581 "perf script [<options>] <top-script> [script-args]",
1581 NULL 1582 NULL
1582 }; 1583 };
1583 struct perf_data_file file = {
1584 .mode = PERF_DATA_MODE_READ,
1585 };
1586 1584
1587 setup_scripting(); 1585 setup_scripting();
1588 1586
1589 argc = parse_options(argc, argv, options, script_usage, 1587 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
1590 PARSE_OPT_STOP_AT_NON_OPTION); 1588 PARSE_OPT_STOP_AT_NON_OPTION);
1591 1589
1592 file.path = input_name; 1590 file.path = input_name;
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index e598e4e98170..f7b8218785f6 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -353,39 +353,40 @@ static struct perf_evsel *nth_evsel(int n)
353 * more semantic information such as miss/hit ratios, 353 * more semantic information such as miss/hit ratios,
354 * instruction rates, etc: 354 * instruction rates, etc:
355 */ 355 */
356static void update_shadow_stats(struct perf_evsel *counter, u64 *count) 356static void update_shadow_stats(struct perf_evsel *counter, u64 *count,
357 int cpu)
357{ 358{
358 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK)) 359 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
359 update_stats(&runtime_nsecs_stats[0], count[0]); 360 update_stats(&runtime_nsecs_stats[cpu], count[0]);
360 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) 361 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
361 update_stats(&runtime_cycles_stats[0], count[0]); 362 update_stats(&runtime_cycles_stats[cpu], count[0]);
362 else if (transaction_run && 363 else if (transaction_run &&
363 perf_evsel__cmp(counter, nth_evsel(T_CYCLES_IN_TX))) 364 perf_evsel__cmp(counter, nth_evsel(T_CYCLES_IN_TX)))
364 update_stats(&runtime_cycles_in_tx_stats[0], count[0]); 365 update_stats(&runtime_cycles_in_tx_stats[cpu], count[0]);
365 else if (transaction_run && 366 else if (transaction_run &&
366 perf_evsel__cmp(counter, nth_evsel(T_TRANSACTION_START))) 367 perf_evsel__cmp(counter, nth_evsel(T_TRANSACTION_START)))
367 update_stats(&runtime_transaction_stats[0], count[0]); 368 update_stats(&runtime_transaction_stats[cpu], count[0]);
368 else if (transaction_run && 369 else if (transaction_run &&
369 perf_evsel__cmp(counter, nth_evsel(T_ELISION_START))) 370 perf_evsel__cmp(counter, nth_evsel(T_ELISION_START)))
370 update_stats(&runtime_elision_stats[0], count[0]); 371 update_stats(&runtime_elision_stats[cpu], count[0]);
371 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) 372 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
372 update_stats(&runtime_stalled_cycles_front_stats[0], count[0]); 373 update_stats(&runtime_stalled_cycles_front_stats[cpu], count[0]);
373 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND)) 374 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
374 update_stats(&runtime_stalled_cycles_back_stats[0], count[0]); 375 update_stats(&runtime_stalled_cycles_back_stats[cpu], count[0]);
375 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) 376 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
376 update_stats(&runtime_branches_stats[0], count[0]); 377 update_stats(&runtime_branches_stats[cpu], count[0]);
377 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES)) 378 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
378 update_stats(&runtime_cacherefs_stats[0], count[0]); 379 update_stats(&runtime_cacherefs_stats[cpu], count[0]);
379 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D)) 380 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
380 update_stats(&runtime_l1_dcache_stats[0], count[0]); 381 update_stats(&runtime_l1_dcache_stats[cpu], count[0]);
381 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I)) 382 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
382 update_stats(&runtime_l1_icache_stats[0], count[0]); 383 update_stats(&runtime_l1_icache_stats[cpu], count[0]);
383 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL)) 384 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
384 update_stats(&runtime_ll_cache_stats[0], count[0]); 385 update_stats(&runtime_ll_cache_stats[cpu], count[0]);
385 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB)) 386 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
386 update_stats(&runtime_dtlb_cache_stats[0], count[0]); 387 update_stats(&runtime_dtlb_cache_stats[cpu], count[0]);
387 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB)) 388 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
388 update_stats(&runtime_itlb_cache_stats[0], count[0]); 389 update_stats(&runtime_itlb_cache_stats[cpu], count[0]);
389} 390}
390 391
391static void zero_per_pkg(struct perf_evsel *counter) 392static void zero_per_pkg(struct perf_evsel *counter)
@@ -447,7 +448,8 @@ static int read_cb(struct perf_evsel *evsel, int cpu, int thread __maybe_unused,
447 perf_evsel__compute_deltas(evsel, cpu, count); 448 perf_evsel__compute_deltas(evsel, cpu, count);
448 perf_counts_values__scale(count, scale, NULL); 449 perf_counts_values__scale(count, scale, NULL);
449 evsel->counts->cpu[cpu] = *count; 450 evsel->counts->cpu[cpu] = *count;
450 update_shadow_stats(evsel, count->values); 451 if (aggr_mode == AGGR_NONE)
452 update_shadow_stats(evsel, count->values, cpu);
451 break; 453 break;
452 case AGGR_GLOBAL: 454 case AGGR_GLOBAL:
453 aggr->val += count->val; 455 aggr->val += count->val;
@@ -495,7 +497,7 @@ static int read_counter_aggr(struct perf_evsel *counter)
495 /* 497 /*
496 * Save the full runtime - to allow normalization during printout: 498 * Save the full runtime - to allow normalization during printout:
497 */ 499 */
498 update_shadow_stats(counter, count); 500 update_shadow_stats(counter, count, 0);
499 501
500 return 0; 502 return 0;
501} 503}
@@ -510,6 +512,9 @@ static int read_counter(struct perf_evsel *counter)
510 int ncpus = perf_evsel__nr_cpus(counter); 512 int ncpus = perf_evsel__nr_cpus(counter);
511 int cpu, thread; 513 int cpu, thread;
512 514
515 if (!counter->supported)
516 return -ENOENT;
517
513 if (counter->system_wide) 518 if (counter->system_wide)
514 nthreads = 1; 519 nthreads = 1;
515 520
@@ -679,8 +684,9 @@ static int __run_perf_stat(int argc, const char **argv)
679 unit_width = l; 684 unit_width = l;
680 } 685 }
681 686
682 if (perf_evlist__apply_filters(evsel_list)) { 687 if (perf_evlist__apply_filters(evsel_list, &counter)) {
683 error("failed to set filter with %d (%s)\n", errno, 688 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
689 counter->filter, perf_evsel__name(counter), errno,
684 strerror_r(errno, msg, sizeof(msg))); 690 strerror_r(errno, msg, sizeof(msg)));
685 return -1; 691 return -1;
686 } 692 }
@@ -766,6 +772,19 @@ static int run_perf_stat(int argc, const char **argv)
766 return ret; 772 return ret;
767} 773}
768 774
775static void print_running(u64 run, u64 ena)
776{
777 if (csv_output) {
778 fprintf(output, "%s%" PRIu64 "%s%.2f",
779 csv_sep,
780 run,
781 csv_sep,
782 ena ? 100.0 * run / ena : 100.0);
783 } else if (run != ena) {
784 fprintf(output, " (%.2f%%)", 100.0 * run / ena);
785 }
786}
787
769static void print_noise_pct(double total, double avg) 788static void print_noise_pct(double total, double avg)
770{ 789{
771 double pct = rel_stddev_stats(total, avg); 790 double pct = rel_stddev_stats(total, avg);
@@ -1076,6 +1095,8 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1076 if (total) { 1095 if (total) {
1077 ratio = avg / total; 1096 ratio = avg / total;
1078 fprintf(output, " # %5.2f insns per cycle ", ratio); 1097 fprintf(output, " # %5.2f insns per cycle ", ratio);
1098 } else {
1099 fprintf(output, " ");
1079 } 1100 }
1080 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]); 1101 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
1081 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu])); 1102 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
@@ -1145,6 +1166,8 @@ static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1145 if (total) { 1166 if (total) {
1146 ratio = avg / total; 1167 ratio = avg / total;
1147 fprintf(output, " # %8.3f GHz ", ratio); 1168 fprintf(output, " # %8.3f GHz ", ratio);
1169 } else {
1170 fprintf(output, " ");
1148 } 1171 }
1149 } else if (transaction_run && 1172 } else if (transaction_run &&
1150 perf_evsel__cmp(evsel, nth_evsel(T_CYCLES_IN_TX))) { 1173 perf_evsel__cmp(evsel, nth_evsel(T_CYCLES_IN_TX))) {
@@ -1249,6 +1272,7 @@ static void print_aggr(char *prefix)
1249 fprintf(output, "%s%s", 1272 fprintf(output, "%s%s",
1250 csv_sep, counter->cgrp->name); 1273 csv_sep, counter->cgrp->name);
1251 1274
1275 print_running(run, ena);
1252 fputc('\n', output); 1276 fputc('\n', output);
1253 continue; 1277 continue;
1254 } 1278 }
@@ -1259,13 +1283,10 @@ static void print_aggr(char *prefix)
1259 else 1283 else
1260 abs_printout(id, nr, counter, uval); 1284 abs_printout(id, nr, counter, uval);
1261 1285
1262 if (!csv_output) { 1286 if (!csv_output)
1263 print_noise(counter, 1.0); 1287 print_noise(counter, 1.0);
1264 1288
1265 if (run != ena) 1289 print_running(run, ena);
1266 fprintf(output, " (%.2f%%)",
1267 100.0 * run / ena);
1268 }
1269 fputc('\n', output); 1290 fputc('\n', output);
1270 } 1291 }
1271 } 1292 }
@@ -1281,11 +1302,15 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1281 double avg = avg_stats(&ps->res_stats[0]); 1302 double avg = avg_stats(&ps->res_stats[0]);
1282 int scaled = counter->counts->scaled; 1303 int scaled = counter->counts->scaled;
1283 double uval; 1304 double uval;
1305 double avg_enabled, avg_running;
1306
1307 avg_enabled = avg_stats(&ps->res_stats[1]);
1308 avg_running = avg_stats(&ps->res_stats[2]);
1284 1309
1285 if (prefix) 1310 if (prefix)
1286 fprintf(output, "%s", prefix); 1311 fprintf(output, "%s", prefix);
1287 1312
1288 if (scaled == -1) { 1313 if (scaled == -1 || !counter->supported) {
1289 fprintf(output, "%*s%s", 1314 fprintf(output, "%*s%s",
1290 csv_output ? 0 : 18, 1315 csv_output ? 0 : 18,
1291 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, 1316 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
@@ -1300,6 +1325,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1300 if (counter->cgrp) 1325 if (counter->cgrp)
1301 fprintf(output, "%s%s", csv_sep, counter->cgrp->name); 1326 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
1302 1327
1328 print_running(avg_running, avg_enabled);
1303 fputc('\n', output); 1329 fputc('\n', output);
1304 return; 1330 return;
1305 } 1331 }
@@ -1313,19 +1339,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1313 1339
1314 print_noise(counter, avg); 1340 print_noise(counter, avg);
1315 1341
1316 if (csv_output) { 1342 print_running(avg_running, avg_enabled);
1317 fputc('\n', output);
1318 return;
1319 }
1320
1321 if (scaled) {
1322 double avg_enabled, avg_running;
1323
1324 avg_enabled = avg_stats(&ps->res_stats[1]);
1325 avg_running = avg_stats(&ps->res_stats[2]);
1326
1327 fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
1328 }
1329 fprintf(output, "\n"); 1343 fprintf(output, "\n");
1330} 1344}
1331 1345
@@ -1367,6 +1381,7 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
1367 fprintf(output, "%s%s", 1381 fprintf(output, "%s%s",
1368 csv_sep, counter->cgrp->name); 1382 csv_sep, counter->cgrp->name);
1369 1383
1384 print_running(run, ena);
1370 fputc('\n', output); 1385 fputc('\n', output);
1371 continue; 1386 continue;
1372 } 1387 }
@@ -1378,13 +1393,10 @@ static void print_counter(struct perf_evsel *counter, char *prefix)
1378 else 1393 else
1379 abs_printout(cpu, 0, counter, uval); 1394 abs_printout(cpu, 0, counter, uval);
1380 1395
1381 if (!csv_output) { 1396 if (!csv_output)
1382 print_noise(counter, 1.0); 1397 print_noise(counter, 1.0);
1398 print_running(run, ena);
1383 1399
1384 if (run != ena)
1385 fprintf(output, " (%.2f%%)",
1386 100.0 * run / ena);
1387 }
1388 fputc('\n', output); 1400 fputc('\n', output);
1389 } 1401 }
1390} 1402}
diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
index f3bb1a4bf060..e50fe1187b0b 100644
--- a/tools/perf/builtin-timechart.c
+++ b/tools/perf/builtin-timechart.c
@@ -67,6 +67,7 @@ struct timechart {
67 skip_eagain; 67 skip_eagain;
68 u64 min_time, 68 u64 min_time,
69 merge_dist; 69 merge_dist;
70 bool force;
70}; 71};
71 72
72struct per_pidcomm; 73struct per_pidcomm;
@@ -1598,6 +1599,7 @@ static int __cmd_timechart(struct timechart *tchart, const char *output_name)
1598 struct perf_data_file file = { 1599 struct perf_data_file file = {
1599 .path = input_name, 1600 .path = input_name,
1600 .mode = PERF_DATA_MODE_READ, 1601 .mode = PERF_DATA_MODE_READ,
1602 .force = tchart->force,
1601 }; 1603 };
1602 1604
1603 struct perf_session *session = perf_session__new(&file, false, 1605 struct perf_session *session = perf_session__new(&file, false,
@@ -1623,7 +1625,7 @@ static int __cmd_timechart(struct timechart *tchart, const char *output_name)
1623 goto out_delete; 1625 goto out_delete;
1624 } 1626 }
1625 1627
1626 ret = perf_session__process_events(session, &tchart->tool); 1628 ret = perf_session__process_events(session);
1627 if (ret) 1629 if (ret)
1628 goto out_delete; 1630 goto out_delete;
1629 1631
@@ -1956,9 +1958,11 @@ int cmd_timechart(int argc, const char **argv,
1956 OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time", 1958 OPT_CALLBACK(0, "io-merge-dist", &tchart.merge_dist, "time",
1957 "merge events that are merge-dist us apart", 1959 "merge events that are merge-dist us apart",
1958 parse_time), 1960 parse_time),
1961 OPT_BOOLEAN('f', "force", &tchart.force, "don't complain, do it"),
1959 OPT_END() 1962 OPT_END()
1960 }; 1963 };
1961 const char * const timechart_usage[] = { 1964 const char * const timechart_subcommands[] = { "record", NULL };
1965 const char *timechart_usage[] = {
1962 "perf timechart [<options>] {record}", 1966 "perf timechart [<options>] {record}",
1963 NULL 1967 NULL
1964 }; 1968 };
@@ -1976,8 +1980,8 @@ int cmd_timechart(int argc, const char **argv,
1976 "perf timechart record [<options>]", 1980 "perf timechart record [<options>]",
1977 NULL 1981 NULL
1978 }; 1982 };
1979 argc = parse_options(argc, argv, timechart_options, timechart_usage, 1983 argc = parse_options_subcommand(argc, argv, timechart_options, timechart_subcommands,
1980 PARSE_OPT_STOP_AT_NON_OPTION); 1984 timechart_usage, PARSE_OPT_STOP_AT_NON_OPTION);
1981 1985
1982 if (tchart.power_only && tchart.tasks_only) { 1986 if (tchart.power_only && tchart.tasks_only) {
1983 pr_err("-P and -T options cannot be used at the same time.\n"); 1987 pr_err("-P and -T options cannot be used at the same time.\n");
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index c4c7eac69de4..1cb3436276d1 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -716,7 +716,7 @@ static void perf_event__process_sample(struct perf_tool *tool,
716 716
717 if (!machine) { 717 if (!machine) {
718 pr_err("%u unprocessable samples recorded.\r", 718 pr_err("%u unprocessable samples recorded.\r",
719 top->session->stats.nr_unprocessable_samples++); 719 top->session->evlist->stats.nr_unprocessable_samples++);
720 return; 720 return;
721 } 721 }
722 722
@@ -757,8 +757,10 @@ static void perf_event__process_sample(struct perf_tool *tool,
757 al.map == machine->vmlinux_maps[MAP__FUNCTION] && 757 al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
758 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) { 758 RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
759 if (symbol_conf.vmlinux_name) { 759 if (symbol_conf.vmlinux_name) {
760 ui__warning("The %s file can't be used.\n%s", 760 char serr[256];
761 symbol_conf.vmlinux_name, msg); 761 dso__strerror_load(al.map->dso, serr, sizeof(serr));
762 ui__warning("The %s file can't be used: %s\n%s",
763 symbol_conf.vmlinux_name, serr, msg);
762 } else { 764 } else {
763 ui__warning("A vmlinux file was not found.\n%s", 765 ui__warning("A vmlinux file was not found.\n%s",
764 msg); 766 msg);
@@ -856,7 +858,7 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
856 hists__inc_nr_events(evsel__hists(evsel), event->header.type); 858 hists__inc_nr_events(evsel__hists(evsel), event->header.type);
857 machine__process_event(machine, event, &sample); 859 machine__process_event(machine, event, &sample);
858 } else 860 } else
859 ++session->stats.nr_unknown_events; 861 ++session->evlist->stats.nr_unknown_events;
860next_event: 862next_event:
861 perf_evlist__mmap_consume(top->evlist, idx); 863 perf_evlist__mmap_consume(top->evlist, idx);
862 } 864 }
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index 7e935f1083ec..e124741be187 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -52,7 +52,9 @@ struct tp_field {
52#define TP_UINT_FIELD(bits) \ 52#define TP_UINT_FIELD(bits) \
53static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \ 53static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
54{ \ 54{ \
55 return *(u##bits *)(sample->raw_data + field->offset); \ 55 u##bits value; \
56 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
57 return value; \
56} 58}
57 59
58TP_UINT_FIELD(8); 60TP_UINT_FIELD(8);
@@ -63,7 +65,8 @@ TP_UINT_FIELD(64);
63#define TP_UINT_FIELD__SWAPPED(bits) \ 65#define TP_UINT_FIELD__SWAPPED(bits) \
64static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \ 66static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
65{ \ 67{ \
66 u##bits value = *(u##bits *)(sample->raw_data + field->offset); \ 68 u##bits value; \
69 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
67 return bswap_##bits(value);\ 70 return bswap_##bits(value);\
68} 71}
69 72
@@ -1132,6 +1135,8 @@ static struct syscall_fmt *syscall_fmt__find(const char *name)
1132 1135
1133struct syscall { 1136struct syscall {
1134 struct event_format *tp_format; 1137 struct event_format *tp_format;
1138 int nr_args;
1139 struct format_field *args;
1135 const char *name; 1140 const char *name;
1136 bool filtered; 1141 bool filtered;
1137 bool is_exit; 1142 bool is_exit;
@@ -1219,7 +1224,9 @@ struct trace {
1219 struct syscall *table; 1224 struct syscall *table;
1220 } syscalls; 1225 } syscalls;
1221 struct record_opts opts; 1226 struct record_opts opts;
1227 struct perf_evlist *evlist;
1222 struct machine *host; 1228 struct machine *host;
1229 struct thread *current;
1223 u64 base_time; 1230 u64 base_time;
1224 FILE *output; 1231 FILE *output;
1225 unsigned long nr_events; 1232 unsigned long nr_events;
@@ -1227,6 +1234,10 @@ struct trace {
1227 const char *last_vfs_getname; 1234 const char *last_vfs_getname;
1228 struct intlist *tid_list; 1235 struct intlist *tid_list;
1229 struct intlist *pid_list; 1236 struct intlist *pid_list;
1237 struct {
1238 size_t nr;
1239 pid_t *entries;
1240 } filter_pids;
1230 double duration_filter; 1241 double duration_filter;
1231 double runtime_ms; 1242 double runtime_ms;
1232 struct { 1243 struct {
@@ -1243,6 +1254,7 @@ struct trace {
1243 bool show_comm; 1254 bool show_comm;
1244 bool show_tool_stats; 1255 bool show_tool_stats;
1245 bool trace_syscalls; 1256 bool trace_syscalls;
1257 bool force;
1246 int trace_pgfaults; 1258 int trace_pgfaults;
1247}; 1259};
1248 1260
@@ -1433,14 +1445,14 @@ static int syscall__set_arg_fmts(struct syscall *sc)
1433 struct format_field *field; 1445 struct format_field *field;
1434 int idx = 0; 1446 int idx = 0;
1435 1447
1436 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *)); 1448 sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
1437 if (sc->arg_scnprintf == NULL) 1449 if (sc->arg_scnprintf == NULL)
1438 return -1; 1450 return -1;
1439 1451
1440 if (sc->fmt) 1452 if (sc->fmt)
1441 sc->arg_parm = sc->fmt->arg_parm; 1453 sc->arg_parm = sc->fmt->arg_parm;
1442 1454
1443 for (field = sc->tp_format->format.fields->next; field; field = field->next) { 1455 for (field = sc->args; field; field = field->next) {
1444 if (sc->fmt && sc->fmt->arg_scnprintf[idx]) 1456 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
1445 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx]; 1457 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
1446 else if (field->flags & FIELD_IS_POINTER) 1458 else if (field->flags & FIELD_IS_POINTER)
@@ -1506,18 +1518,37 @@ static int trace__read_syscall_info(struct trace *trace, int id)
1506 if (sc->tp_format == NULL) 1518 if (sc->tp_format == NULL)
1507 return -1; 1519 return -1;
1508 1520
1521 sc->args = sc->tp_format->format.fields;
1522 sc->nr_args = sc->tp_format->format.nr_fields;
1523 /* drop nr field - not relevant here; does not exist on older kernels */
1524 if (sc->args && strcmp(sc->args->name, "nr") == 0) {
1525 sc->args = sc->args->next;
1526 --sc->nr_args;
1527 }
1528
1509 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit"); 1529 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1510 1530
1511 return syscall__set_arg_fmts(sc); 1531 return syscall__set_arg_fmts(sc);
1512} 1532}
1513 1533
1534/*
1535 * args is to be interpreted as a series of longs but we need to handle
1536 * 8-byte unaligned accesses. args points to raw_data within the event
1537 * and raw_data is guaranteed to be 8-byte unaligned because it is
1538 * preceded by raw_size which is a u32. So we need to copy args to a temp
1539 * variable to read it. Most notably this avoids extended load instructions
1540 * on unaligned addresses
1541 */
1542
1514static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, 1543static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1515 unsigned long *args, struct trace *trace, 1544 unsigned char *args, struct trace *trace,
1516 struct thread *thread) 1545 struct thread *thread)
1517{ 1546{
1518 size_t printed = 0; 1547 size_t printed = 0;
1548 unsigned char *p;
1549 unsigned long val;
1519 1550
1520 if (sc->tp_format != NULL) { 1551 if (sc->args != NULL) {
1521 struct format_field *field; 1552 struct format_field *field;
1522 u8 bit = 1; 1553 u8 bit = 1;
1523 struct syscall_arg arg = { 1554 struct syscall_arg arg = {
@@ -1527,16 +1558,21 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1527 .thread = thread, 1558 .thread = thread,
1528 }; 1559 };
1529 1560
1530 for (field = sc->tp_format->format.fields->next; field; 1561 for (field = sc->args; field;
1531 field = field->next, ++arg.idx, bit <<= 1) { 1562 field = field->next, ++arg.idx, bit <<= 1) {
1532 if (arg.mask & bit) 1563 if (arg.mask & bit)
1533 continue; 1564 continue;
1565
1566 /* special care for unaligned accesses */
1567 p = args + sizeof(unsigned long) * arg.idx;
1568 memcpy(&val, p, sizeof(val));
1569
1534 /* 1570 /*
1535 * Suppress this argument if its value is zero and 1571 * Suppress this argument if its value is zero and
1536 * and we don't have a string associated in an 1572 * and we don't have a string associated in an
1537 * strarray for it. 1573 * strarray for it.
1538 */ 1574 */
1539 if (args[arg.idx] == 0 && 1575 if (val == 0 &&
1540 !(sc->arg_scnprintf && 1576 !(sc->arg_scnprintf &&
1541 sc->arg_scnprintf[arg.idx] == SCA_STRARRAY && 1577 sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
1542 sc->arg_parm[arg.idx])) 1578 sc->arg_parm[arg.idx]))
@@ -1545,23 +1581,26 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
1545 printed += scnprintf(bf + printed, size - printed, 1581 printed += scnprintf(bf + printed, size - printed,
1546 "%s%s: ", printed ? ", " : "", field->name); 1582 "%s%s: ", printed ? ", " : "", field->name);
1547 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) { 1583 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
1548 arg.val = args[arg.idx]; 1584 arg.val = val;
1549 if (sc->arg_parm) 1585 if (sc->arg_parm)
1550 arg.parm = sc->arg_parm[arg.idx]; 1586 arg.parm = sc->arg_parm[arg.idx];
1551 printed += sc->arg_scnprintf[arg.idx](bf + printed, 1587 printed += sc->arg_scnprintf[arg.idx](bf + printed,
1552 size - printed, &arg); 1588 size - printed, &arg);
1553 } else { 1589 } else {
1554 printed += scnprintf(bf + printed, size - printed, 1590 printed += scnprintf(bf + printed, size - printed,
1555 "%ld", args[arg.idx]); 1591 "%ld", val);
1556 } 1592 }
1557 } 1593 }
1558 } else { 1594 } else {
1559 int i = 0; 1595 int i = 0;
1560 1596
1561 while (i < 6) { 1597 while (i < 6) {
1598 /* special care for unaligned accesses */
1599 p = args + sizeof(unsigned long) * i;
1600 memcpy(&val, p, sizeof(val));
1562 printed += scnprintf(bf + printed, size - printed, 1601 printed += scnprintf(bf + printed, size - printed,
1563 "%sarg%d: %ld", 1602 "%sarg%d: %ld",
1564 printed ? ", " : "", i, args[i]); 1603 printed ? ", " : "", i, val);
1565 ++i; 1604 ++i;
1566 } 1605 }
1567 } 1606 }
@@ -1642,6 +1681,29 @@ static void thread__update_stats(struct thread_trace *ttrace,
1642 update_stats(stats, duration); 1681 update_stats(stats, duration);
1643} 1682}
1644 1683
1684static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample)
1685{
1686 struct thread_trace *ttrace;
1687 u64 duration;
1688 size_t printed;
1689
1690 if (trace->current == NULL)
1691 return 0;
1692
1693 ttrace = thread__priv(trace->current);
1694
1695 if (!ttrace->entry_pending)
1696 return 0;
1697
1698 duration = sample->time - ttrace->entry_time;
1699
1700 printed = trace__fprintf_entry_head(trace, trace->current, duration, sample->time, trace->output);
1701 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1702 ttrace->entry_pending = false;
1703
1704 return printed;
1705}
1706
1645static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, 1707static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
1646 union perf_event *event __maybe_unused, 1708 union perf_event *event __maybe_unused,
1647 struct perf_sample *sample) 1709 struct perf_sample *sample)
@@ -1673,6 +1735,9 @@ static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
1673 return -1; 1735 return -1;
1674 } 1736 }
1675 1737
1738 if (!trace->summary_only)
1739 trace__printf_interrupted_entry(trace, sample);
1740
1676 ttrace->entry_time = sample->time; 1741 ttrace->entry_time = sample->time;
1677 msg = ttrace->entry_str; 1742 msg = ttrace->entry_str;
1678 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); 1743 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
@@ -1688,6 +1753,11 @@ static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
1688 } else 1753 } else
1689 ttrace->entry_pending = true; 1754 ttrace->entry_pending = true;
1690 1755
1756 if (trace->current != thread) {
1757 thread__put(trace->current);
1758 trace->current = thread__get(thread);
1759 }
1760
1691 return 0; 1761 return 0;
1692} 1762}
1693 1763
@@ -1805,6 +1875,28 @@ out_dump:
1805 return 0; 1875 return 0;
1806} 1876}
1807 1877
1878static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
1879 union perf_event *event __maybe_unused,
1880 struct perf_sample *sample)
1881{
1882 trace__printf_interrupted_entry(trace, sample);
1883 trace__fprintf_tstamp(trace, sample->time, trace->output);
1884
1885 if (trace->trace_syscalls)
1886 fprintf(trace->output, "( ): ");
1887
1888 fprintf(trace->output, "%s:", evsel->name);
1889
1890 if (evsel->tp_format) {
1891 event_format__fprintf(evsel->tp_format, sample->cpu,
1892 sample->raw_data, sample->raw_size,
1893 trace->output);
1894 }
1895
1896 fprintf(trace->output, ")\n");
1897 return 0;
1898}
1899
1808static void print_location(FILE *f, struct perf_sample *sample, 1900static void print_location(FILE *f, struct perf_sample *sample,
1809 struct addr_location *al, 1901 struct addr_location *al,
1810 bool print_dso, bool print_sym) 1902 bool print_dso, bool print_sym)
@@ -2037,10 +2129,39 @@ static int perf_evlist__add_pgfault(struct perf_evlist *evlist,
2037 return 0; 2129 return 0;
2038} 2130}
2039 2131
2040static int trace__run(struct trace *trace, int argc, const char **argv) 2132static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2041{ 2133{
2042 struct perf_evlist *evlist = perf_evlist__new(); 2134 const u32 type = event->header.type;
2043 struct perf_evsel *evsel; 2135 struct perf_evsel *evsel;
2136
2137 if (!trace->full_time && trace->base_time == 0)
2138 trace->base_time = sample->time;
2139
2140 if (type != PERF_RECORD_SAMPLE) {
2141 trace__process_event(trace, trace->host, event, sample);
2142 return;
2143 }
2144
2145 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2146 if (evsel == NULL) {
2147 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2148 return;
2149 }
2150
2151 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2152 sample->raw_data == NULL) {
2153 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2154 perf_evsel__name(evsel), sample->tid,
2155 sample->cpu, sample->raw_size);
2156 } else {
2157 tracepoint_handler handler = evsel->handler;
2158 handler(trace, evsel, event, sample);
2159 }
2160}
2161
2162static int trace__run(struct trace *trace, int argc, const char **argv)
2163{
2164 struct perf_evlist *evlist = trace->evlist;
2044 int err = -1, i; 2165 int err = -1, i;
2045 unsigned long before; 2166 unsigned long before;
2046 const bool forks = argc > 0; 2167 const bool forks = argc > 0;
@@ -2048,11 +2169,6 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
2048 2169
2049 trace->live = true; 2170 trace->live = true;
2050 2171
2051 if (evlist == NULL) {
2052 fprintf(trace->output, "Not enough memory to run!\n");
2053 goto out;
2054 }
2055
2056 if (trace->trace_syscalls && 2172 if (trace->trace_syscalls &&
2057 perf_evlist__add_syscall_newtp(evlist, trace__sys_enter, 2173 perf_evlist__add_syscall_newtp(evlist, trace__sys_enter,
2058 trace__sys_exit)) 2174 trace__sys_exit))
@@ -2105,16 +2221,34 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
2105 if (err < 0) 2221 if (err < 0)
2106 goto out_error_open; 2222 goto out_error_open;
2107 2223
2224 /*
2225 * Better not use !target__has_task() here because we need to cover the
2226 * case where no threads were specified in the command line, but a
2227 * workload was, and in that case we will fill in the thread_map when
2228 * we fork the workload in perf_evlist__prepare_workload.
2229 */
2230 if (trace->filter_pids.nr > 0)
2231 err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
2232 else if (evlist->threads->map[0] == -1)
2233 err = perf_evlist__set_filter_pid(evlist, getpid());
2234
2235 if (err < 0) {
2236 printf("err=%d,%s\n", -err, strerror(-err));
2237 exit(1);
2238 }
2239
2108 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false); 2240 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false);
2109 if (err < 0) 2241 if (err < 0)
2110 goto out_error_mmap; 2242 goto out_error_mmap;
2111 2243
2112 perf_evlist__enable(evlist);
2113
2114 if (forks) 2244 if (forks)
2115 perf_evlist__start_workload(evlist); 2245 perf_evlist__start_workload(evlist);
2246 else
2247 perf_evlist__enable(evlist);
2116 2248
2117 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1; 2249 trace->multiple_threads = evlist->threads->map[0] == -1 ||
2250 evlist->threads->nr > 1 ||
2251 perf_evlist__first(evlist)->attr.inherit;
2118again: 2252again:
2119 before = trace->nr_events; 2253 before = trace->nr_events;
2120 2254
@@ -2122,8 +2256,6 @@ again:
2122 union perf_event *event; 2256 union perf_event *event;
2123 2257
2124 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { 2258 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
2125 const u32 type = event->header.type;
2126 tracepoint_handler handler;
2127 struct perf_sample sample; 2259 struct perf_sample sample;
2128 2260
2129 ++trace->nr_events; 2261 ++trace->nr_events;
@@ -2134,30 +2266,7 @@ again:
2134 goto next_event; 2266 goto next_event;
2135 } 2267 }
2136 2268
2137 if (!trace->full_time && trace->base_time == 0) 2269 trace__handle_event(trace, event, &sample);
2138 trace->base_time = sample.time;
2139
2140 if (type != PERF_RECORD_SAMPLE) {
2141 trace__process_event(trace, trace->host, event, &sample);
2142 continue;
2143 }
2144
2145 evsel = perf_evlist__id2evsel(evlist, sample.id);
2146 if (evsel == NULL) {
2147 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
2148 goto next_event;
2149 }
2150
2151 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2152 sample.raw_data == NULL) {
2153 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2154 perf_evsel__name(evsel), sample.tid,
2155 sample.cpu, sample.raw_size);
2156 goto next_event;
2157 }
2158
2159 handler = evsel->handler;
2160 handler(trace, evsel, event, &sample);
2161next_event: 2270next_event:
2162 perf_evlist__mmap_consume(evlist, i); 2271 perf_evlist__mmap_consume(evlist, i);
2163 2272
@@ -2180,6 +2289,8 @@ next_event:
2180 } 2289 }
2181 2290
2182out_disable: 2291out_disable:
2292 thread__zput(trace->current);
2293
2183 perf_evlist__disable(evlist); 2294 perf_evlist__disable(evlist);
2184 2295
2185 if (!err) { 2296 if (!err) {
@@ -2197,7 +2308,7 @@ out_disable:
2197 2308
2198out_delete_evlist: 2309out_delete_evlist:
2199 perf_evlist__delete(evlist); 2310 perf_evlist__delete(evlist);
2200out: 2311 trace->evlist = NULL;
2201 trace->live = false; 2312 trace->live = false;
2202 return err; 2313 return err;
2203{ 2314{
@@ -2235,6 +2346,7 @@ static int trace__replay(struct trace *trace)
2235 struct perf_data_file file = { 2346 struct perf_data_file file = {
2236 .path = input_name, 2347 .path = input_name,
2237 .mode = PERF_DATA_MODE_READ, 2348 .mode = PERF_DATA_MODE_READ,
2349 .force = trace->force,
2238 }; 2350 };
2239 struct perf_session *session; 2351 struct perf_session *session;
2240 struct perf_evsel *evsel; 2352 struct perf_evsel *evsel;
@@ -2309,7 +2421,7 @@ static int trace__replay(struct trace *trace)
2309 2421
2310 setup_pager(); 2422 setup_pager();
2311 2423
2312 err = perf_session__process_events(session, &trace->tool); 2424 err = perf_session__process_events(session);
2313 if (err) 2425 if (err)
2314 pr_err("Failed to process events, error %d", err); 2426 pr_err("Failed to process events, error %d", err);
2315 2427
@@ -2434,6 +2546,38 @@ static int trace__set_duration(const struct option *opt, const char *str,
2434 return 0; 2546 return 0;
2435} 2547}
2436 2548
2549static int trace__set_filter_pids(const struct option *opt, const char *str,
2550 int unset __maybe_unused)
2551{
2552 int ret = -1;
2553 size_t i;
2554 struct trace *trace = opt->value;
2555 /*
2556 * FIXME: introduce a intarray class, plain parse csv and create a
2557 * { int nr, int entries[] } struct...
2558 */
2559 struct intlist *list = intlist__new(str);
2560
2561 if (list == NULL)
2562 return -1;
2563
2564 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
2565 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
2566
2567 if (trace->filter_pids.entries == NULL)
2568 goto out;
2569
2570 trace->filter_pids.entries[0] = getpid();
2571
2572 for (i = 1; i < trace->filter_pids.nr; ++i)
2573 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
2574
2575 intlist__delete(list);
2576 ret = 0;
2577out:
2578 return ret;
2579}
2580
2437static int trace__open_output(struct trace *trace, const char *filename) 2581static int trace__open_output(struct trace *trace, const char *filename)
2438{ 2582{
2439 struct stat st; 2583 struct stat st;
@@ -2468,9 +2612,17 @@ static int parse_pagefaults(const struct option *opt, const char *str,
2468 return 0; 2612 return 0;
2469} 2613}
2470 2614
2615static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
2616{
2617 struct perf_evsel *evsel;
2618
2619 evlist__for_each(evlist, evsel)
2620 evsel->handler = handler;
2621}
2622
2471int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) 2623int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
2472{ 2624{
2473 const char * const trace_usage[] = { 2625 const char *trace_usage[] = {
2474 "perf trace [<options>] [<command>]", 2626 "perf trace [<options>] [<command>]",
2475 "perf trace [<options>] -- <command> [<options>]", 2627 "perf trace [<options>] -- <command> [<options>]",
2476 "perf trace record [<options>] [<command>]", 2628 "perf trace record [<options>] [<command>]",
@@ -2502,6 +2654,9 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
2502 const char *output_name = NULL; 2654 const char *output_name = NULL;
2503 const char *ev_qualifier_str = NULL; 2655 const char *ev_qualifier_str = NULL;
2504 const struct option trace_options[] = { 2656 const struct option trace_options[] = {
2657 OPT_CALLBACK(0, "event", &trace.evlist, "event",
2658 "event selector. use 'perf list' to list available events",
2659 parse_events_option),
2505 OPT_BOOLEAN(0, "comm", &trace.show_comm, 2660 OPT_BOOLEAN(0, "comm", &trace.show_comm,
2506 "show the thread COMM next to its id"), 2661 "show the thread COMM next to its id"),
2507 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"), 2662 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
@@ -2513,6 +2668,8 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
2513 "trace events on existing process id"), 2668 "trace events on existing process id"),
2514 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", 2669 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
2515 "trace events on existing thread id"), 2670 "trace events on existing thread id"),
2671 OPT_CALLBACK(0, "filter-pids", &trace, "float",
2672 "show only events with duration > N.M ms", trace__set_filter_pids),
2516 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, 2673 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
2517 "system-wide collection from all CPUs"), 2674 "system-wide collection from all CPUs"),
2518 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", 2675 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
@@ -2538,19 +2695,36 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
2538 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", 2695 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
2539 "Trace pagefaults", parse_pagefaults, "maj"), 2696 "Trace pagefaults", parse_pagefaults, "maj"),
2540 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), 2697 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
2698 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
2541 OPT_END() 2699 OPT_END()
2542 }; 2700 };
2701 const char * const trace_subcommands[] = { "record", NULL };
2543 int err; 2702 int err;
2544 char bf[BUFSIZ]; 2703 char bf[BUFSIZ];
2545 2704
2546 argc = parse_options(argc, argv, trace_options, trace_usage, 2705 signal(SIGSEGV, sighandler_dump_stack);
2547 PARSE_OPT_STOP_AT_NON_OPTION); 2706 signal(SIGFPE, sighandler_dump_stack);
2707
2708 trace.evlist = perf_evlist__new();
2709 if (trace.evlist == NULL)
2710 return -ENOMEM;
2711
2712 if (trace.evlist == NULL) {
2713 pr_err("Not enough memory to run!\n");
2714 goto out;
2715 }
2716
2717 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
2718 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
2548 2719
2549 if (trace.trace_pgfaults) { 2720 if (trace.trace_pgfaults) {
2550 trace.opts.sample_address = true; 2721 trace.opts.sample_address = true;
2551 trace.opts.sample_time = true; 2722 trace.opts.sample_time = true;
2552 } 2723 }
2553 2724
2725 if (trace.evlist->nr_entries > 0)
2726 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
2727
2554 if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) 2728 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
2555 return trace__record(&trace, argc-1, &argv[1]); 2729 return trace__record(&trace, argc-1, &argv[1]);
2556 2730
@@ -2558,7 +2732,8 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
2558 if (trace.summary_only) 2732 if (trace.summary_only)
2559 trace.summary = trace.summary_only; 2733 trace.summary = trace.summary_only;
2560 2734
2561 if (!trace.trace_syscalls && !trace.trace_pgfaults) { 2735 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
2736 trace.evlist->nr_entries == 0 /* Was --events used? */) {
2562 pr_err("Please specify something to trace.\n"); 2737 pr_err("Please specify something to trace.\n");
2563 return -1; 2738 return -1;
2564 } 2739 }
diff --git a/tools/perf/builtin.h b/tools/perf/builtin.h
index b210d62907e4..3688ad29085f 100644
--- a/tools/perf/builtin.h
+++ b/tools/perf/builtin.h
@@ -37,6 +37,7 @@ extern int cmd_test(int argc, const char **argv, const char *prefix);
37extern int cmd_trace(int argc, const char **argv, const char *prefix); 37extern int cmd_trace(int argc, const char **argv, const char *prefix);
38extern int cmd_inject(int argc, const char **argv, const char *prefix); 38extern int cmd_inject(int argc, const char **argv, const char *prefix);
39extern int cmd_mem(int argc, const char **argv, const char *prefix); 39extern int cmd_mem(int argc, const char **argv, const char *prefix);
40extern int cmd_data(int argc, const char **argv, const char *prefix);
40 41
41extern int find_scripts(char **scripts_array, char **scripts_path_array); 42extern int find_scripts(char **scripts_array, char **scripts_path_array);
42#endif 43#endif
diff --git a/tools/perf/command-list.txt b/tools/perf/command-list.txt
index 0906fc401c52..00fcaf8a5b8d 100644
--- a/tools/perf/command-list.txt
+++ b/tools/perf/command-list.txt
@@ -7,6 +7,7 @@ perf-archive mainporcelain common
7perf-bench mainporcelain common 7perf-bench mainporcelain common
8perf-buildid-cache mainporcelain common 8perf-buildid-cache mainporcelain common
9perf-buildid-list mainporcelain common 9perf-buildid-list mainporcelain common
10perf-data mainporcelain common
10perf-diff mainporcelain common 11perf-diff mainporcelain common
11perf-evlist mainporcelain common 12perf-evlist mainporcelain common
12perf-inject mainporcelain common 13perf-inject mainporcelain common
diff --git a/tools/perf/config/Makefile b/tools/perf/config/Makefile
index cc224080b525..59a98c643240 100644
--- a/tools/perf/config/Makefile
+++ b/tools/perf/config/Makefile
@@ -11,19 +11,26 @@ ifneq ($(obj-perf),)
11obj-perf := $(abspath $(obj-perf))/ 11obj-perf := $(abspath $(obj-perf))/
12endif 12endif
13 13
14LIB_INCLUDE := $(srctree)/tools/lib/ 14$(shell echo -n > .config-detected)
15detected = $(shell echo "$(1)=y" >> .config-detected)
16detected_var = $(shell echo "$(1)=$($(1))" >> .config-detected)
17
15CFLAGS := $(EXTRA_CFLAGS) $(EXTRA_WARNINGS) 18CFLAGS := $(EXTRA_CFLAGS) $(EXTRA_WARNINGS)
16 19
17include $(src-perf)/config/Makefile.arch 20include $(src-perf)/config/Makefile.arch
18 21
22$(call detected_var,ARCH)
23
19NO_PERF_REGS := 1 24NO_PERF_REGS := 1
20 25
21# Additional ARCH settings for x86 26# Additional ARCH settings for x86
22ifeq ($(ARCH),x86) 27ifeq ($(ARCH),x86)
28 $(call detected,CONFIG_X86)
23 ifeq (${IS_64_BIT}, 1) 29 ifeq (${IS_64_BIT}, 1)
24 CFLAGS += -DHAVE_ARCH_X86_64_SUPPORT 30 CFLAGS += -DHAVE_ARCH_X86_64_SUPPORT
25 ARCH_INCLUDE = ../../arch/x86/lib/memcpy_64.S ../../arch/x86/lib/memset_64.S 31 ARCH_INCLUDE = ../../arch/x86/lib/memcpy_64.S ../../arch/x86/lib/memset_64.S
26 LIBUNWIND_LIBS = -lunwind -lunwind-x86_64 32 LIBUNWIND_LIBS = -lunwind -lunwind-x86_64
33 $(call detected,CONFIG_X86_64)
27 else 34 else
28 LIBUNWIND_LIBS = -lunwind -lunwind-x86 35 LIBUNWIND_LIBS = -lunwind -lunwind-x86
29 endif 36 endif
@@ -40,6 +47,10 @@ ifeq ($(ARCH),arm64)
40 LIBUNWIND_LIBS = -lunwind -lunwind-aarch64 47 LIBUNWIND_LIBS = -lunwind -lunwind-aarch64
41endif 48endif
42 49
50ifeq ($(NO_PERF_REGS),0)
51 $(call detected,CONFIG_PERF_REGS)
52endif
53
43# So far there's only x86 and arm libdw unwind support merged in perf. 54# So far there's only x86 and arm libdw unwind support merged in perf.
44# Disable it on all other architectures in case libdw unwind 55# Disable it on all other architectures in case libdw unwind
45# support is detected in system. Add supported architectures 56# support is detected in system. Add supported architectures
@@ -84,6 +95,17 @@ ifndef NO_LIBELF
84 FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind := $(LIBDW_LDFLAGS) -ldw 95 FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind := $(LIBDW_LDFLAGS) -ldw
85endif 96endif
86 97
98ifdef LIBBABELTRACE
99 # for linking with debug library, run like:
100 # make DEBUG=1 LIBBABELTRACE_DIR=/opt/libbabeltrace/
101 ifdef LIBBABELTRACE_DIR
102 LIBBABELTRACE_CFLAGS := -I$(LIBBABELTRACE_DIR)/include
103 LIBBABELTRACE_LDFLAGS := -L$(LIBBABELTRACE_DIR)/lib
104 endif
105 FEATURE_CHECK_CFLAGS-libbabeltrace := $(LIBBABELTRACE_CFLAGS)
106 FEATURE_CHECK_LDFLAGS-libbabeltrace := $(LIBBABELTRACE_LDFLAGS) -lbabeltrace-ctf
107endif
108
87# include ARCH specific config 109# include ARCH specific config
88-include $(src-perf)/arch/$(ARCH)/Makefile 110-include $(src-perf)/arch/$(ARCH)/Makefile
89 111
@@ -114,6 +136,8 @@ ifdef PARSER_DEBUG
114 PARSER_DEBUG_BISON := -t 136 PARSER_DEBUG_BISON := -t
115 PARSER_DEBUG_FLEX := -d 137 PARSER_DEBUG_FLEX := -d
116 CFLAGS += -DPARSER_DEBUG 138 CFLAGS += -DPARSER_DEBUG
139 $(call detected_var,PARSER_DEBUG_BISON)
140 $(call detected_var,PARSER_DEBUG_FLEX)
117endif 141endif
118 142
119ifndef NO_LIBPYTHON 143ifndef NO_LIBPYTHON
@@ -152,121 +176,7 @@ LDFLAGS += -Wl,-z,noexecstack
152 176
153EXTLIBS = -lpthread -lrt -lm -ldl 177EXTLIBS = -lpthread -lrt -lm -ldl
154 178
155ifneq ($(OUTPUT),) 179include $(srctree)/tools/build/Makefile.feature
156 OUTPUT_FEATURES = $(OUTPUT)config/feature-checks/
157 $(shell mkdir -p $(OUTPUT_FEATURES))
158endif
159
160feature_check = $(eval $(feature_check_code))
161define feature_check_code
162 feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C config/feature-checks test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
163endef
164
165feature_set = $(eval $(feature_set_code))
166define feature_set_code
167 feature-$(1) := 1
168endef
169
170#
171# Build the feature check binaries in parallel, ignore errors, ignore return value and suppress output:
172#
173
174#
175# Note that this is not a complete list of all feature tests, just
176# those that are typically built on a fully configured system.
177#
178# [ Feature tests not mentioned here have to be built explicitly in
179# the rule that uses them - an example for that is the 'bionic'
180# feature check. ]
181#
182CORE_FEATURE_TESTS = \
183 backtrace \
184 dwarf \
185 fortify-source \
186 sync-compare-and-swap \
187 glibc \
188 gtk2 \
189 gtk2-infobar \
190 libaudit \
191 libbfd \
192 libelf \
193 libelf-getphdrnum \
194 libelf-mmap \
195 libnuma \
196 libperl \
197 libpython \
198 libpython-version \
199 libslang \
200 libunwind \
201 pthread-attr-setaffinity-np \
202 stackprotector-all \
203 timerfd \
204 libdw-dwarf-unwind \
205 zlib
206
207LIB_FEATURE_TESTS = \
208 dwarf \
209 glibc \
210 gtk2 \
211 libaudit \
212 libbfd \
213 libelf \
214 libnuma \
215 libperl \
216 libpython \
217 libslang \
218 libunwind \
219 libdw-dwarf-unwind \
220 zlib
221
222VF_FEATURE_TESTS = \
223 backtrace \
224 fortify-source \
225 sync-compare-and-swap \
226 gtk2-infobar \
227 libelf-getphdrnum \
228 libelf-mmap \
229 libpython-version \
230 pthread-attr-setaffinity-np \
231 stackprotector-all \
232 timerfd \
233 libunwind-debug-frame \
234 bionic \
235 liberty \
236 liberty-z \
237 cplus-demangle \
238 compile-32 \
239 compile-x32
240
241# Set FEATURE_CHECK_(C|LD)FLAGS-all for all CORE_FEATURE_TESTS features.
242# If in the future we need per-feature checks/flags for features not
243# mentioned in this list we need to refactor this ;-).
244set_test_all_flags = $(eval $(set_test_all_flags_code))
245define set_test_all_flags_code
246 FEATURE_CHECK_CFLAGS-all += $(FEATURE_CHECK_CFLAGS-$(1))
247 FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1))
248endef
249
250$(foreach feat,$(CORE_FEATURE_TESTS),$(call set_test_all_flags,$(feat)))
251
252#
253# Special fast-path for the 'all features are available' case:
254#
255$(call feature_check,all,$(MSG))
256
257#
258# Just in case the build freshly failed, make sure we print the
259# feature matrix:
260#
261ifeq ($(feature-all), 1)
262 #
263 # test-all.c passed - just set all the core feature flags to 1:
264 #
265 $(foreach feat,$(CORE_FEATURE_TESTS),$(call feature_set,$(feat)))
266else
267 $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CFLAGS="$(EXTRA_CFLAGS)" LDFLAGS=$(LDFLAGS) -i -j -C config/feature-checks $(addsuffix .bin,$(CORE_FEATURE_TESTS)) >/dev/null 2>&1)
268 $(foreach feat,$(CORE_FEATURE_TESTS),$(call feature_check,$(feat)))
269endif
270 180
271ifeq ($(feature-stackprotector-all), 1) 181ifeq ($(feature-stackprotector-all), 1)
272 CFLAGS += -fstack-protector-all 182 CFLAGS += -fstack-protector-all
@@ -295,7 +205,7 @@ endif
295 205
296CFLAGS += -I$(src-perf)/util 206CFLAGS += -I$(src-perf)/util
297CFLAGS += -I$(src-perf) 207CFLAGS += -I$(src-perf)
298CFLAGS += -I$(LIB_INCLUDE) 208CFLAGS += -I$(srctree)/tools/lib/
299 209
300CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE 210CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
301 211
@@ -361,6 +271,7 @@ endif # NO_LIBELF
361ifndef NO_LIBELF 271ifndef NO_LIBELF
362 CFLAGS += -DHAVE_LIBELF_SUPPORT 272 CFLAGS += -DHAVE_LIBELF_SUPPORT
363 EXTLIBS += -lelf 273 EXTLIBS += -lelf
274 $(call detected,CONFIG_LIBELF)
364 275
365 ifeq ($(feature-libelf-mmap), 1) 276 ifeq ($(feature-libelf-mmap), 1)
366 CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT 277 CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT
@@ -381,6 +292,7 @@ ifndef NO_LIBELF
381 CFLAGS += -DHAVE_DWARF_SUPPORT $(LIBDW_CFLAGS) 292 CFLAGS += -DHAVE_DWARF_SUPPORT $(LIBDW_CFLAGS)
382 LDFLAGS += $(LIBDW_LDFLAGS) 293 LDFLAGS += $(LIBDW_LDFLAGS)
383 EXTLIBS += -ldw 294 EXTLIBS += -ldw
295 $(call detected,CONFIG_DWARF)
384 endif # PERF_HAVE_DWARF_REGS 296 endif # PERF_HAVE_DWARF_REGS
385 endif # NO_DWARF 297 endif # NO_DWARF
386endif # NO_LIBELF 298endif # NO_LIBELF
@@ -408,9 +320,11 @@ ifdef NO_LIBUNWIND
408 dwarf-post-unwind := 0 320 dwarf-post-unwind := 0
409 else 321 else
410 dwarf-post-unwind-text := libdw 322 dwarf-post-unwind-text := libdw
323 $(call detected,CONFIG_LIBDW_DWARF_UNWIND)
411 endif 324 endif
412else 325else
413 dwarf-post-unwind-text := libunwind 326 dwarf-post-unwind-text := libunwind
327 $(call detected,CONFIG_LIBUNWIND)
414 # Enable libunwind support by default. 328 # Enable libunwind support by default.
415 ifndef NO_LIBDW_DWARF_UNWIND 329 ifndef NO_LIBDW_DWARF_UNWIND
416 NO_LIBDW_DWARF_UNWIND := 1 330 NO_LIBDW_DWARF_UNWIND := 1
@@ -419,6 +333,7 @@ endif
419 333
420ifeq ($(dwarf-post-unwind),1) 334ifeq ($(dwarf-post-unwind),1)
421 CFLAGS += -DHAVE_DWARF_UNWIND_SUPPORT 335 CFLAGS += -DHAVE_DWARF_UNWIND_SUPPORT
336 $(call detected,CONFIG_DWARF_UNWIND)
422else 337else
423 NO_DWARF_UNWIND := 1 338 NO_DWARF_UNWIND := 1
424endif 339endif
@@ -447,6 +362,7 @@ ifndef NO_LIBAUDIT
447 else 362 else
448 CFLAGS += -DHAVE_LIBAUDIT_SUPPORT 363 CFLAGS += -DHAVE_LIBAUDIT_SUPPORT
449 EXTLIBS += -laudit 364 EXTLIBS += -laudit
365 $(call detected,CONFIG_AUDIT)
450 endif 366 endif
451endif 367endif
452 368
@@ -463,6 +379,7 @@ ifndef NO_SLANG
463 CFLAGS += -I/usr/include/slang 379 CFLAGS += -I/usr/include/slang
464 CFLAGS += -DHAVE_SLANG_SUPPORT 380 CFLAGS += -DHAVE_SLANG_SUPPORT
465 EXTLIBS += -lslang 381 EXTLIBS += -lslang
382 $(call detected,CONFIG_SLANG)
466 endif 383 endif
467endif 384endif
468 385
@@ -497,10 +414,11 @@ else
497 ifneq ($(feature-libperl), 1) 414 ifneq ($(feature-libperl), 1)
498 CFLAGS += -DNO_LIBPERL 415 CFLAGS += -DNO_LIBPERL
499 NO_LIBPERL := 1 416 NO_LIBPERL := 1
500 msg := $(warning Missing perl devel files. Disabling perl scripting support, consider installing perl-ExtUtils-Embed); 417 msg := $(warning Missing perl devel files. Disabling perl scripting support, please install perl-ExtUtils-Embed/libperl-dev);
501 else 418 else
502 LDFLAGS += $(PERL_EMBED_LDFLAGS) 419 LDFLAGS += $(PERL_EMBED_LDFLAGS)
503 EXTLIBS += $(PERL_EMBED_LIBADD) 420 EXTLIBS += $(PERL_EMBED_LIBADD)
421 $(call detected,CONFIG_LIBPERL)
504 endif 422 endif
505endif 423endif
506 424
@@ -513,22 +431,21 @@ endif
513disable-python = $(eval $(disable-python_code)) 431disable-python = $(eval $(disable-python_code))
514define disable-python_code 432define disable-python_code
515 CFLAGS += -DNO_LIBPYTHON 433 CFLAGS += -DNO_LIBPYTHON
516 $(if $(1),$(warning No $(1) was found)) 434 $(warning $1)
517 $(warning Python support will not be built)
518 NO_LIBPYTHON := 1 435 NO_LIBPYTHON := 1
519endef 436endef
520 437
521ifdef NO_LIBPYTHON 438ifdef NO_LIBPYTHON
522 $(call disable-python) 439 $(call disable-python,Python support disabled by user)
523else 440else
524 441
525 ifndef PYTHON 442 ifndef PYTHON
526 $(call disable-python,python interpreter) 443 $(call disable-python,No python interpreter was found: disables Python support - please install python-devel/python-dev)
527 else 444 else
528 PYTHON_WORD := $(call shell-wordify,$(PYTHON)) 445 PYTHON_WORD := $(call shell-wordify,$(PYTHON))
529 446
530 ifndef PYTHON_CONFIG 447 ifndef PYTHON_CONFIG
531 $(call disable-python,python-config tool) 448 $(call disable-python,No 'python-config' tool was found: disables Python support - please install python-devel/python-dev)
532 else 449 else
533 450
534 PYTHON_CONFIG_SQ := $(call shell-sq,$(PYTHON_CONFIG)) 451 PYTHON_CONFIG_SQ := $(call shell-sq,$(PYTHON_CONFIG))
@@ -540,7 +457,7 @@ else
540 FLAGS_PYTHON_EMBED := $(PYTHON_EMBED_CCOPTS) $(PYTHON_EMBED_LDOPTS) 457 FLAGS_PYTHON_EMBED := $(PYTHON_EMBED_CCOPTS) $(PYTHON_EMBED_LDOPTS)
541 458
542 ifneq ($(feature-libpython), 1) 459 ifneq ($(feature-libpython), 1)
543 $(call disable-python,Python.h (for Python 2.x)) 460 $(call disable-python,No 'Python.h' (for Python 2.x support) was found: disables Python support - please install python-devel/python-dev)
544 else 461 else
545 462
546 ifneq ($(feature-libpython-version), 1) 463 ifneq ($(feature-libpython-version), 1)
@@ -560,6 +477,7 @@ else
560 LDFLAGS += $(PYTHON_EMBED_LDFLAGS) 477 LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
561 EXTLIBS += $(PYTHON_EMBED_LIBADD) 478 EXTLIBS += $(PYTHON_EMBED_LIBADD)
562 LANG_BINDINGS += $(obj-perf)python/perf.so 479 LANG_BINDINGS += $(obj-perf)python/perf.so
480 $(call detected,CONFIG_LIBPYTHON)
563 endif 481 endif
564 endif 482 endif
565 endif 483 endif
@@ -600,7 +518,7 @@ else
600 EXTLIBS += -liberty 518 EXTLIBS += -liberty
601 CFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT 519 CFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT
602 else 520 else
603 msg := $(warning No bfd.h/libbfd found, install binutils-dev[el]/zlib-static to gain symbol demangling) 521 msg := $(warning No bfd.h/libbfd found, please install binutils-dev[el]/zlib-static/libiberty-dev to gain symbol demangling)
604 CFLAGS += -DNO_DEMANGLE 522 CFLAGS += -DNO_DEMANGLE
605 endif 523 endif
606 endif 524 endif
@@ -617,11 +535,23 @@ ifndef NO_ZLIB
617 ifeq ($(feature-zlib), 1) 535 ifeq ($(feature-zlib), 1)
618 CFLAGS += -DHAVE_ZLIB_SUPPORT 536 CFLAGS += -DHAVE_ZLIB_SUPPORT
619 EXTLIBS += -lz 537 EXTLIBS += -lz
538 $(call detected,CONFIG_ZLIB)
620 else 539 else
621 NO_ZLIB := 1 540 NO_ZLIB := 1
622 endif 541 endif
623endif 542endif
624 543
544ifndef NO_LZMA
545 ifeq ($(feature-lzma), 1)
546 CFLAGS += -DHAVE_LZMA_SUPPORT
547 EXTLIBS += -llzma
548 $(call detected,CONFIG_LZMA)
549 else
550 msg := $(warning No liblzma found, disables xz kernel module decompression, please install xz-devel/liblzma-dev);
551 NO_LZMA := 1
552 endif
553endif
554
625ifndef NO_BACKTRACE 555ifndef NO_BACKTRACE
626 ifeq ($(feature-backtrace), 1) 556 ifeq ($(feature-backtrace), 1)
627 CFLAGS += -DHAVE_BACKTRACE_SUPPORT 557 CFLAGS += -DHAVE_BACKTRACE_SUPPORT
@@ -635,6 +565,7 @@ ifndef NO_LIBNUMA
635 else 565 else
636 CFLAGS += -DHAVE_LIBNUMA_SUPPORT 566 CFLAGS += -DHAVE_LIBNUMA_SUPPORT
637 EXTLIBS += -lnuma 567 EXTLIBS += -lnuma
568 $(call detected,CONFIG_NUMA)
638 endif 569 endif
639endif 570endif
640 571
@@ -651,7 +582,7 @@ ifeq (${IS_64_BIT}, 1)
651 NO_PERF_READ_VDSO32 := 1 582 NO_PERF_READ_VDSO32 := 1
652 endif 583 endif
653 endif 584 endif
654 ifneq (${IS_X86_64}, 1) 585 ifneq ($(ARCH), x86)
655 NO_PERF_READ_VDSOX32 := 1 586 NO_PERF_READ_VDSOX32 := 1
656 endif 587 endif
657 ifndef NO_PERF_READ_VDSOX32 588 ifndef NO_PERF_READ_VDSOX32
@@ -667,6 +598,18 @@ else
667 NO_PERF_READ_VDSOX32 := 1 598 NO_PERF_READ_VDSOX32 := 1
668endif 599endif
669 600
601ifdef LIBBABELTRACE
602 $(call feature_check,libbabeltrace)
603 ifeq ($(feature-libbabeltrace), 1)
604 CFLAGS += -DHAVE_LIBBABELTRACE_SUPPORT $(LIBBABELTRACE_CFLAGS)
605 LDFLAGS += $(LIBBABELTRACE_LDFLAGS)
606 EXTLIBS += -lbabeltrace-ctf
607 $(call detected,CONFIG_LIBBABELTRACE)
608 else
609 msg := $(warning No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev);
610 endif
611endif
612
670# Among the variables below, these: 613# Among the variables below, these:
671# perfexecdir 614# perfexecdir
672# template_dir 615# template_dir
@@ -699,7 +642,7 @@ sysconfdir = $(prefix)/etc
699ETC_PERFCONFIG = etc/perfconfig 642ETC_PERFCONFIG = etc/perfconfig
700endif 643endif
701ifndef lib 644ifndef lib
702ifeq ($(IS_X86_64),1) 645ifeq ($(ARCH)$(IS_64_BIT), x861)
703lib = lib64 646lib = lib64
704else 647else
705lib = lib 648lib = lib
@@ -735,83 +678,33 @@ plugindir=$(libdir)/traceevent/plugins
735plugindir_SQ= $(subst ','\'',$(plugindir)) 678plugindir_SQ= $(subst ','\'',$(plugindir))
736endif 679endif
737 680
738# 681print_var = $(eval $(print_var_code)) $(info $(MSG))
739# Print the result of the feature test: 682define print_var_code
740#
741feature_print_status = $(eval $(feature_print_status_code)) $(info $(MSG))
742
743define feature_print_status_code
744 ifeq ($(feature-$(1)), 1)
745 MSG = $(shell printf '...%30s: [ \033[32mon\033[m ]' $(1))
746 else
747 MSG = $(shell printf '...%30s: [ \033[31mOFF\033[m ]' $(1))
748 endif
749endef
750
751feature_print_var = $(eval $(feature_print_var_code)) $(info $(MSG))
752define feature_print_var_code
753 MSG = $(shell printf '...%30s: %s' $(1) $($(1))) 683 MSG = $(shell printf '...%30s: %s' $(1) $($(1)))
754endef 684endef
755 685
756feature_print_text = $(eval $(feature_print_text_code)) $(info $(MSG))
757define feature_print_text_code
758 MSG = $(shell printf '...%30s: %s' $(1) $(2))
759endef
760
761PERF_FEATURES := $(foreach feat,$(LIB_FEATURE_TESTS),feature-$(feat)($(feature-$(feat))))
762PERF_FEATURES_FILE := $(shell touch $(OUTPUT)PERF-FEATURES; cat $(OUTPUT)PERF-FEATURES)
763
764ifeq ($(dwarf-post-unwind),1)
765 PERF_FEATURES += dwarf-post-unwind($(dwarf-post-unwind-text))
766endif
767
768# The $(display_lib) controls the default detection message
769# output. It's set if:
770# - detected features differes from stored features from
771# last build (in PERF-FEATURES file)
772# - one of the $(LIB_FEATURE_TESTS) is not detected
773# - VF is enabled
774
775ifneq ("$(PERF_FEATURES)","$(PERF_FEATURES_FILE)")
776 $(shell echo "$(PERF_FEATURES)" > $(OUTPUT)PERF-FEATURES)
777 display_lib := 1
778endif
779
780feature_check = $(eval $(feature_check_code))
781define feature_check_code
782 ifneq ($(feature-$(1)), 1)
783 display_lib := 1
784 endif
785endef
786
787$(foreach feat,$(LIB_FEATURE_TESTS),$(call feature_check,$(feat)))
788
789ifeq ($(VF),1) 686ifeq ($(VF),1)
790 display_lib := 1 687 $(call print_var,prefix)
791 display_vf := 1 688 $(call print_var,bindir)
792endif 689 $(call print_var,libdir)
793 690 $(call print_var,sysconfdir)
794ifeq ($(display_lib),1) 691 $(call print_var,LIBUNWIND_DIR)
692 $(call print_var,LIBDW_DIR)
795 $(info ) 693 $(info )
796 $(info Auto-detecting system features:)
797 $(foreach feat,$(LIB_FEATURE_TESTS),$(call feature_print_status,$(feat),))
798
799 ifeq ($(dwarf-post-unwind),1)
800 $(call feature_print_text,"DWARF post unwind library", $(dwarf-post-unwind-text))
801 endif
802endif
803
804ifeq ($(display_vf),1)
805 $(foreach feat,$(VF_FEATURE_TESTS),$(call feature_print_status,$(feat),))
806 $(info )
807 $(call feature_print_var,prefix)
808 $(call feature_print_var,bindir)
809 $(call feature_print_var,libdir)
810 $(call feature_print_var,sysconfdir)
811 $(call feature_print_var,LIBUNWIND_DIR)
812 $(call feature_print_var,LIBDW_DIR)
813endif 694endif
814 695
815ifeq ($(display_lib),1) 696$(call detected_var,bindir_SQ)
816 $(info ) 697$(call detected_var,PYTHON_WORD)
817endif 698ifneq ($(OUTPUT),)
699$(call detected_var,OUTPUT)
700endif
701$(call detected_var,htmldir_SQ)
702$(call detected_var,infodir_SQ)
703$(call detected_var,mandir_SQ)
704$(call detected_var,ETC_PERFCONFIG_SQ)
705$(call detected_var,prefix_SQ)
706$(call detected_var,perfexecdir_SQ)
707$(call detected_var,LIBDIR)
708$(call detected_var,GTK_CFLAGS)
709$(call detected_var,PERL_EMBED_CCOPTS)
710$(call detected_var,PYTHON_EMBED_CCOPTS)
diff --git a/tools/perf/config/Makefile.arch b/tools/perf/config/Makefile.arch
index ac8721ffa6c8..e11fbd6fae78 100644
--- a/tools/perf/config/Makefile.arch
+++ b/tools/perf/config/Makefile.arch
@@ -1,32 +1,15 @@
1ifndef ARCH
2ARCH := $(shell uname -m 2>/dev/null || echo not)
3endif
1 4
2uname_M := $(shell uname -m 2>/dev/null || echo not) 5ARCH := $(shell echo $(ARCH) | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
3 6 -e s/sun4u/sparc/ -e s/sparc64/sparc/ \
4RAW_ARCH := $(shell echo $(uname_M) | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \ 7 -e /arm64/!s/arm.*/arm/ -e s/sa110/arm/ \
5 -e s/arm.*/arm/ -e s/sa110/arm/ \
6 -e s/s390x/s390/ -e s/parisc64/parisc/ \ 8 -e s/s390x/s390/ -e s/parisc64/parisc/ \
7 -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \ 9 -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
8 -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \ 10 -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \
9 -e s/tile.*/tile/ ) 11 -e s/tile.*/tile/ )
10 12
11# Additional ARCH settings for x86
12ifeq ($(RAW_ARCH),i386)
13 ARCH ?= x86
14endif
15
16ifeq ($(RAW_ARCH),x86_64)
17 ARCH ?= x86
18
19 ifneq (, $(findstring m32,$(CFLAGS)))
20 RAW_ARCH := x86_32
21 endif
22endif
23
24ifeq ($(RAW_ARCH),sparc64)
25 ARCH ?= sparc
26endif
27
28ARCH ?= $(RAW_ARCH)
29
30LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) 13LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
31ifeq ($(LP64), 1) 14ifeq ($(LP64), 1)
32 IS_64_BIT := 1 15 IS_64_BIT := 1
diff --git a/tools/perf/config/utilities.mak b/tools/perf/config/utilities.mak
index 7076a62d0ff7..c16ce833079c 100644
--- a/tools/perf/config/utilities.mak
+++ b/tools/perf/config/utilities.mak
@@ -175,6 +175,5 @@ _ge-abspath = $(if $(is-executable),$(1))
175define get-executable-or-default 175define get-executable-or-default
176$(if $($(1)),$(call _ge_attempt,$($(1)),$(1)),$(call _ge_attempt,$(2))) 176$(if $($(1)),$(call _ge_attempt,$($(1)),$(1)),$(call _ge_attempt,$(2)))
177endef 177endef
178_ge_attempt = $(if $(get-executable),$(get-executable),$(_gea_warn)$(call _gea_err,$(2))) 178_ge_attempt = $(if $(get-executable),$(get-executable),$(call _gea_err,$(2)))
179_gea_warn = $(warning The path '$(1)' is not executable.)
180_gea_err = $(if $(1),$(error Please set '$(1)' appropriately)) 179_gea_err = $(if $(1),$(error Please set '$(1)' appropriately))
diff --git a/tools/perf/perf-completion.sh b/tools/perf/perf-completion.sh
index 33569847fdcc..3ba80b2359cc 100644
--- a/tools/perf/perf-completion.sh
+++ b/tools/perf/perf-completion.sh
@@ -47,8 +47,16 @@ __my_reassemble_comp_words_by_ref()
47 done 47 done
48} 48}
49 49
50type _get_comp_words_by_ref &>/dev/null || 50# Define preload_get_comp_words_by_ref="false", if the function
51_get_comp_words_by_ref() 51# __perf_get_comp_words_by_ref() is required instead.
52preload_get_comp_words_by_ref="true"
53
54if [ $preload_get_comp_words_by_ref = "true" ]; then
55 type _get_comp_words_by_ref &>/dev/null ||
56 preload_get_comp_words_by_ref="false"
57fi
58[ $preload_get_comp_words_by_ref = "true" ] ||
59__perf_get_comp_words_by_ref()
52{ 60{
53 local exclude cur_ words_ cword_ 61 local exclude cur_ words_ cword_
54 if [ "$1" = "-n" ]; then 62 if [ "$1" = "-n" ]; then
@@ -76,8 +84,16 @@ _get_comp_words_by_ref()
76 done 84 done
77} 85}
78 86
79type __ltrim_colon_completions &>/dev/null || 87# Define preload__ltrim_colon_completions="false", if the function
80__ltrim_colon_completions() 88# __perf__ltrim_colon_completions() is required instead.
89preload__ltrim_colon_completions="true"
90
91if [ $preload__ltrim_colon_completions = "true" ]; then
92 type __ltrim_colon_completions &>/dev/null ||
93 preload__ltrim_colon_completions="false"
94fi
95[ $preload__ltrim_colon_completions = "true" ] ||
96__perf__ltrim_colon_completions()
81{ 97{
82 if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then 98 if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
83 # Remove colon-word prefix from COMPREPLY items 99 # Remove colon-word prefix from COMPREPLY items
@@ -97,7 +113,32 @@ __perfcomp ()
97__perfcomp_colon () 113__perfcomp_colon ()
98{ 114{
99 __perfcomp "$1" "$2" 115 __perfcomp "$1" "$2"
100 __ltrim_colon_completions $cur 116 if [ $preload__ltrim_colon_completions = "true" ]; then
117 __ltrim_colon_completions $cur
118 else
119 __perf__ltrim_colon_completions $cur
120 fi
121}
122
123__perf_prev_skip_opts ()
124{
125 local i cmd_ cmds_
126
127 let i=cword-1
128 cmds_=$($cmd $1 --list-cmds)
129 prev_skip_opts=()
130 while [ $i -ge 0 ]; do
131 if [[ ${words[i]} == $1 ]]; then
132 return
133 fi
134 for cmd_ in $cmds_; do
135 if [[ ${words[i]} == $cmd_ ]]; then
136 prev_skip_opts=${words[i]}
137 return
138 fi
139 done
140 ((i--))
141 done
101} 142}
102 143
103__perf_main () 144__perf_main ()
@@ -107,29 +148,36 @@ __perf_main ()
107 cmd=${words[0]} 148 cmd=${words[0]}
108 COMPREPLY=() 149 COMPREPLY=()
109 150
151 # Skip options backward and find the last perf command
152 __perf_prev_skip_opts
110 # List perf subcommands or long options 153 # List perf subcommands or long options
111 if [ $cword -eq 1 ]; then 154 if [ -z $prev_skip_opts ]; then
112 if [[ $cur == --* ]]; then 155 if [[ $cur == --* ]]; then
113 __perfcomp '--help --version \ 156 cmds=$($cmd --list-opts)
114 --exec-path --html-path --paginate --no-pager \
115 --perf-dir --work-tree --debugfs-dir' -- "$cur"
116 else 157 else
117 cmds=$($cmd --list-cmds) 158 cmds=$($cmd --list-cmds)
118 __perfcomp "$cmds" "$cur"
119 fi 159 fi
160 __perfcomp "$cmds" "$cur"
120 # List possible events for -e option 161 # List possible events for -e option
121 elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then 162 elif [[ $prev == @("-e"|"--event") &&
163 $prev_skip_opts == @(record|stat|top) ]]; then
122 evts=$($cmd list --raw-dump) 164 evts=$($cmd list --raw-dump)
123 __perfcomp_colon "$evts" "$cur" 165 __perfcomp_colon "$evts" "$cur"
124 # List subcommands for perf commands 166 else
125 elif [[ $prev == @(kvm|kmem|mem|lock|sched) ]]; then 167 # List subcommands for perf commands
126 subcmds=$($cmd $prev --list-cmds) 168 if [[ $prev_skip_opts == @(kvm|kmem|mem|lock|sched|
127 __perfcomp_colon "$subcmds" "$cur" 169 |data|help|script|test|timechart|trace) ]]; then
128 # List long option names 170 subcmds=$($cmd $prev_skip_opts --list-cmds)
129 elif [[ $cur == --* ]]; then 171 __perfcomp_colon "$subcmds" "$cur"
130 subcmd=${words[1]} 172 fi
131 opts=$($cmd $subcmd --list-opts) 173 # List long option names
132 __perfcomp "$opts" "$cur" 174 if [[ $cur == --* ]]; then
175 subcmd=$prev_skip_opts
176 __perf_prev_skip_opts $subcmd
177 subcmd=$subcmd" "$prev_skip_opts
178 opts=$($cmd $subcmd --list-opts)
179 __perfcomp "$opts" "$cur"
180 fi
133 fi 181 fi
134} 182}
135 183
@@ -198,7 +246,11 @@ type perf &>/dev/null &&
198_perf() 246_perf()
199{ 247{
200 local cur words cword prev 248 local cur words cword prev
201 _get_comp_words_by_ref -n =: cur words cword prev 249 if [ $preload_get_comp_words_by_ref = "true" ]; then
250 _get_comp_words_by_ref -n =: cur words cword prev
251 else
252 __perf_get_comp_words_by_ref -n =: cur words cword prev
253 fi
202 __perf_main 254 __perf_main
203} && 255} &&
204 256
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 3700a7faca6c..b857fcbd00cf 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -13,6 +13,7 @@
13#include "util/quote.h" 13#include "util/quote.h"
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/parse-options.h"
16#include "util/debug.h" 17#include "util/debug.h"
17#include <api/fs/debugfs.h> 18#include <api/fs/debugfs.h>
18#include <pthread.h> 19#include <pthread.h>
@@ -62,6 +63,7 @@ static struct cmd_struct commands[] = {
62#endif 63#endif
63 { "inject", cmd_inject, 0 }, 64 { "inject", cmd_inject, 0 },
64 { "mem", cmd_mem, 0 }, 65 { "mem", cmd_mem, 0 },
66 { "data", cmd_data, 0 },
65}; 67};
66 68
67struct pager_config { 69struct pager_config {
@@ -124,6 +126,23 @@ static void commit_pager_choice(void)
124 } 126 }
125} 127}
126 128
129struct option options[] = {
130 OPT_ARGUMENT("help", "help"),
131 OPT_ARGUMENT("version", "version"),
132 OPT_ARGUMENT("exec-path", "exec-path"),
133 OPT_ARGUMENT("html-path", "html-path"),
134 OPT_ARGUMENT("paginate", "paginate"),
135 OPT_ARGUMENT("no-pager", "no-pager"),
136 OPT_ARGUMENT("perf-dir", "perf-dir"),
137 OPT_ARGUMENT("work-tree", "work-tree"),
138 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
139 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
140 OPT_ARGUMENT("list-cmds", "list-cmds"),
141 OPT_ARGUMENT("list-opts", "list-opts"),
142 OPT_ARGUMENT("debug", "debug"),
143 OPT_END()
144};
145
127static int handle_options(const char ***argv, int *argc, int *envchanged) 146static int handle_options(const char ***argv, int *argc, int *envchanged)
128{ 147{
129 int handled = 0; 148 int handled = 0;
@@ -222,6 +241,16 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
222 struct cmd_struct *p = commands+i; 241 struct cmd_struct *p = commands+i;
223 printf("%s ", p->cmd); 242 printf("%s ", p->cmd);
224 } 243 }
244 putchar('\n');
245 exit(0);
246 } else if (!strcmp(cmd, "--list-opts")) {
247 unsigned int i;
248
249 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
250 struct option *p = options+i;
251 printf("--%s ", p->long_name);
252 }
253 putchar('\n');
225 exit(0); 254 exit(0);
226 } else if (!strcmp(cmd, "--debug")) { 255 } else if (!strcmp(cmd, "--debug")) {
227 if (*argc < 2) { 256 if (*argc < 2) {
diff --git a/tools/perf/perf.h b/tools/perf/perf.h
index 1dabb8553499..e14bb637255c 100644
--- a/tools/perf/perf.h
+++ b/tools/perf/perf.h
@@ -29,7 +29,7 @@ static inline unsigned long long rdclock(void)
29 return ts.tv_sec * 1000000000ULL + ts.tv_nsec; 29 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
30} 30}
31 31
32#define MAX_NR_CPUS 256 32#define MAX_NR_CPUS 1024
33 33
34extern const char *input_name; 34extern const char *input_name;
35extern bool perf_host, perf_guest; 35extern bool perf_host, perf_guest;
@@ -53,6 +53,7 @@ struct record_opts {
53 bool sample_time; 53 bool sample_time;
54 bool period; 54 bool period;
55 bool sample_intr_regs; 55 bool sample_intr_regs;
56 bool running_time;
56 unsigned int freq; 57 unsigned int freq;
57 unsigned int mmap_pages; 58 unsigned int mmap_pages;
58 unsigned int user_freq; 59 unsigned int user_freq;
@@ -61,6 +62,8 @@ struct record_opts {
61 u64 user_interval; 62 u64 user_interval;
62 bool sample_transaction; 63 bool sample_transaction;
63 unsigned initial_delay; 64 unsigned initial_delay;
65 bool use_clockid;
66 clockid_t clockid;
64}; 67};
65 68
66struct option; 69struct option;
diff --git a/tools/perf/scripts/Build b/tools/perf/scripts/Build
new file mode 100644
index 000000000000..41efd7e368b3
--- /dev/null
+++ b/tools/perf/scripts/Build
@@ -0,0 +1,2 @@
1libperf-$(CONFIG_LIBPERL) += perl/Perf-Trace-Util/
2libperf-$(CONFIG_LIBPYTHON) += python/Perf-Trace-Util/
diff --git a/tools/perf/scripts/perl/Perf-Trace-Util/Build b/tools/perf/scripts/perl/Perf-Trace-Util/Build
new file mode 100644
index 000000000000..928e110179cb
--- /dev/null
+++ b/tools/perf/scripts/perl/Perf-Trace-Util/Build
@@ -0,0 +1,3 @@
1libperf-y += Context.o
2
3CFLAGS_Context.o += $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs -Wno-undef -Wno-switch-default
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Build b/tools/perf/scripts/python/Perf-Trace-Util/Build
new file mode 100644
index 000000000000..aefc15c9444a
--- /dev/null
+++ b/tools/perf/scripts/python/Perf-Trace-Util/Build
@@ -0,0 +1,3 @@
1libperf-y += Context.o
2
3CFLAGS_Context.o += $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
new file mode 100644
index 000000000000..6a8801b32017
--- /dev/null
+++ b/tools/perf/tests/Build
@@ -0,0 +1,43 @@
1perf-y += builtin-test.o
2perf-y += parse-events.o
3perf-y += dso-data.o
4perf-y += attr.o
5perf-y += vmlinux-kallsyms.o
6perf-y += open-syscall.o
7perf-y += open-syscall-all-cpus.o
8perf-y += open-syscall-tp-fields.o
9perf-y += mmap-basic.o
10perf-y += perf-record.o
11perf-y += rdpmc.o
12perf-y += evsel-roundtrip-name.o
13perf-y += evsel-tp-sched.o
14perf-y += fdarray.o
15perf-y += pmu.o
16perf-y += hists_common.o
17perf-y += hists_link.o
18perf-y += hists_filter.o
19perf-y += hists_output.o
20perf-y += hists_cumulate.o
21perf-y += python-use.o
22perf-y += bp_signal.o
23perf-y += bp_signal_overflow.o
24perf-y += task-exit.o
25perf-y += sw-clock.o
26perf-y += mmap-thread-lookup.o
27perf-y += thread-mg-share.o
28perf-y += switch-tracking.o
29perf-y += keep-tracking.o
30perf-y += code-reading.o
31perf-y += sample-parsing.o
32perf-y += parse-no-sample-id-all.o
33perf-y += kmod-path.o
34
35perf-$(CONFIG_X86) += perf-time-to-tsc.o
36
37ifeq ($(ARCH),$(filter $(ARCH),x86 arm))
38perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
39endif
40
41CFLAGS_attr.o += -DBINDIR="BUILD_STR($(bindir_SQ))" -DPYTHON="BUILD_STR($(PYTHON_WORD))"
42CFLAGS_python-use.o += -DPYTHONPATH="BUILD_STR($(OUTPUT)python)" -DPYTHON="BUILD_STR($(PYTHON_WORD))"
43CFLAGS_dwarf-unwind.o += -fno-optimize-sibling-calls
diff --git a/tools/perf/tests/attr/base-record b/tools/perf/tests/attr/base-record
index d3095dafed36..7e6d74946e04 100644
--- a/tools/perf/tests/attr/base-record
+++ b/tools/perf/tests/attr/base-record
@@ -5,7 +5,7 @@ group_fd=-1
5flags=0|8 5flags=0|8
6cpu=* 6cpu=*
7type=0|1 7type=0|1
8size=104 8size=112
9config=0 9config=0
10sample_period=4000 10sample_period=4000
11sample_type=263 11sample_type=263
diff --git a/tools/perf/tests/attr/base-stat b/tools/perf/tests/attr/base-stat
index 872ed7e24c7c..f4cf148f14cb 100644
--- a/tools/perf/tests/attr/base-stat
+++ b/tools/perf/tests/attr/base-stat
@@ -5,7 +5,7 @@ group_fd=-1
5flags=0|8 5flags=0|8
6cpu=* 6cpu=*
7type=0 7type=0
8size=104 8size=112
9config=0 9config=0
10sample_period=0 10sample_period=0
11sample_type=0 11sample_type=0
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 4b7d9ab0f049..4f4098167112 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -167,6 +167,10 @@ static struct test {
167 .func = test__fdarray__add, 167 .func = test__fdarray__add,
168 }, 168 },
169 { 169 {
170 .desc = "Test kmod_path__parse function",
171 .func = test__kmod_path__parse,
172 },
173 {
170 .func = NULL, 174 .func = NULL,
171 }, 175 },
172}; 176};
@@ -291,7 +295,7 @@ static int perf_test__list(int argc, const char **argv)
291 295
292int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused) 296int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
293{ 297{
294 const char * const test_usage[] = { 298 const char *test_usage[] = {
295 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 299 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
296 NULL, 300 NULL,
297 }; 301 };
@@ -302,13 +306,14 @@ int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
302 "be more verbose (show symbol address, etc)"), 306 "be more verbose (show symbol address, etc)"),
303 OPT_END() 307 OPT_END()
304 }; 308 };
309 const char * const test_subcommands[] = { "list", NULL };
305 struct intlist *skiplist = NULL; 310 struct intlist *skiplist = NULL;
306 int ret = hists__init(); 311 int ret = hists__init();
307 312
308 if (ret < 0) 313 if (ret < 0)
309 return ret; 314 return ret;
310 315
311 argc = parse_options(argc, argv, test_options, test_usage, 0); 316 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
312 if (argc >= 1 && !strcmp(argv[0], "list")) 317 if (argc >= 1 && !strcmp(argv[0], "list"))
313 return perf_test__list(argc, argv); 318 return perf_test__list(argc, argv);
314 319
diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c
index caaf37f079b1..513e5febbe5a 100644
--- a/tools/perf/tests/dso-data.c
+++ b/tools/perf/tests/dso-data.c
@@ -112,6 +112,9 @@ int test__dso_data(void)
112 112
113 dso = dso__new((const char *)file); 113 dso = dso__new((const char *)file);
114 114
115 TEST_ASSERT_VAL("Failed to access to dso",
116 dso__data_fd(dso, &machine) >= 0);
117
115 /* Basic 10 bytes tests. */ 118 /* Basic 10 bytes tests. */
116 for (i = 0; i < ARRAY_SIZE(offsets); i++) { 119 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
117 struct test_data_offset *data = &offsets[i]; 120 struct test_data_offset *data = &offsets[i];
@@ -243,8 +246,8 @@ int test__dso_data_cache(void)
243 limit = nr * 4; 246 limit = nr * 4;
244 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit)); 247 TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
245 248
246 /* and this is now our dso open FDs limit + 1 extra */ 249 /* and this is now our dso open FDs limit */
247 dso_cnt = limit / 2 + 1; 250 dso_cnt = limit / 2;
248 TEST_ASSERT_VAL("failed to create dsos\n", 251 TEST_ASSERT_VAL("failed to create dsos\n",
249 !dsos__create(dso_cnt, TEST_FILE_SIZE)); 252 !dsos__create(dso_cnt, TEST_FILE_SIZE));
250 253
@@ -252,13 +255,13 @@ int test__dso_data_cache(void)
252 struct dso *dso = dsos[i]; 255 struct dso *dso = dsos[i];
253 256
254 /* 257 /*
255 * Open dsos via dso__data_fd or dso__data_read_offset. 258 * Open dsos via dso__data_fd(), it opens the data
256 * Both opens the data file and keep it open. 259 * file and keep it open (unless open file limit).
257 */ 260 */
261 fd = dso__data_fd(dso, &machine);
262 TEST_ASSERT_VAL("failed to get fd", fd > 0);
263
258 if (i % 2) { 264 if (i % 2) {
259 fd = dso__data_fd(dso, &machine);
260 TEST_ASSERT_VAL("failed to get fd", fd > 0);
261 } else {
262 #define BUFSIZE 10 265 #define BUFSIZE 10
263 u8 buf[BUFSIZE]; 266 u8 buf[BUFSIZE];
264 ssize_t n; 267 ssize_t n;
@@ -268,7 +271,10 @@ int test__dso_data_cache(void)
268 } 271 }
269 } 272 }
270 273
271 /* open +1 dso over the allowed limit */ 274 /* verify the first one is already open */
275 TEST_ASSERT_VAL("dsos[0] is not open", dsos[0]->data.fd != -1);
276
277 /* open +1 dso to reach the allowed limit */
272 fd = dso__data_fd(dsos[i], &machine); 278 fd = dso__data_fd(dsos[i], &machine);
273 TEST_ASSERT_VAL("failed to get fd", fd > 0); 279 TEST_ASSERT_VAL("failed to get fd", fd > 0);
274 280
diff --git a/tools/perf/tests/kmod-path.c b/tools/perf/tests/kmod-path.c
new file mode 100644
index 000000000000..e8d7cbb9320c
--- /dev/null
+++ b/tools/perf/tests/kmod-path.c
@@ -0,0 +1,73 @@
1#include <stdbool.h>
2#include "tests.h"
3#include "dso.h"
4#include "debug.h"
5
6static int test(const char *path, bool alloc_name, bool alloc_ext,
7 bool kmod, bool comp, const char *name, const char *ext)
8{
9 struct kmod_path m;
10
11 memset(&m, 0x0, sizeof(m));
12
13 TEST_ASSERT_VAL("kmod_path__parse",
14 !__kmod_path__parse(&m, path, alloc_name, alloc_ext));
15
16 pr_debug("%s - alloc name %d, alloc ext %d, kmod %d, comp %d, name '%s', ext '%s'\n",
17 path, alloc_name, alloc_ext, m.kmod, m.comp, m.name, m.ext);
18
19 TEST_ASSERT_VAL("wrong kmod", m.kmod == kmod);
20 TEST_ASSERT_VAL("wrong comp", m.comp == comp);
21
22 if (ext)
23 TEST_ASSERT_VAL("wrong ext", m.ext && !strcmp(ext, m.ext));
24 else
25 TEST_ASSERT_VAL("wrong ext", !m.ext);
26
27 if (name)
28 TEST_ASSERT_VAL("wrong name", m.name && !strcmp(name, m.name));
29 else
30 TEST_ASSERT_VAL("wrong name", !m.name);
31
32 free(m.name);
33 free(m.ext);
34 return 0;
35}
36
37#define T(path, an, ae, k, c, n, e) \
38 TEST_ASSERT_VAL("failed", !test(path, an, ae, k, c, n, e))
39
40int test__kmod_path__parse(void)
41{
42 /* path alloc_name alloc_ext kmod comp name ext */
43 T("/xxxx/xxxx/x-x.ko", true , true , true, false, "[x_x]", NULL);
44 T("/xxxx/xxxx/x-x.ko", false , true , true, false, NULL , NULL);
45 T("/xxxx/xxxx/x-x.ko", true , false , true, false, "[x_x]", NULL);
46 T("/xxxx/xxxx/x-x.ko", false , false , true, false, NULL , NULL);
47
48 /* path alloc_name alloc_ext kmod comp name ext */
49 T("/xxxx/xxxx/x.ko.gz", true , true , true, true, "[x]", "gz");
50 T("/xxxx/xxxx/x.ko.gz", false , true , true, true, NULL , "gz");
51 T("/xxxx/xxxx/x.ko.gz", true , false , true, true, "[x]", NULL);
52 T("/xxxx/xxxx/x.ko.gz", false , false , true, true, NULL , NULL);
53
54 /* path alloc_name alloc_ext kmod comp name ext */
55 T("/xxxx/xxxx/x.gz", true , true , false, true, "x.gz" ,"gz");
56 T("/xxxx/xxxx/x.gz", false , true , false, true, NULL ,"gz");
57 T("/xxxx/xxxx/x.gz", true , false , false, true, "x.gz" , NULL);
58 T("/xxxx/xxxx/x.gz", false , false , false, true, NULL , NULL);
59
60 /* path alloc_name alloc_ext kmod comp name ext */
61 T("x.gz", true , true , false, true, "x.gz", "gz");
62 T("x.gz", false , true , false, true, NULL , "gz");
63 T("x.gz", true , false , false, true, "x.gz", NULL);
64 T("x.gz", false , false , false, true, NULL , NULL);
65
66 /* path alloc_name alloc_ext kmod comp name ext */
67 T("x.ko.gz", true , true , true, true, "[x]", "gz");
68 T("x.ko.gz", false , true , true, true, NULL , "gz");
69 T("x.ko.gz", true , false , true, true, "[x]", NULL);
70 T("x.ko.gz", false , false , true, true, NULL , NULL);
71
72 return 0;
73}
diff --git a/tools/perf/tests/make b/tools/perf/tests/make
index 75709d2b17b4..bff85324f799 100644
--- a/tools/perf/tests/make
+++ b/tools/perf/tests/make
@@ -5,7 +5,7 @@ include config/Makefile.arch
5 5
6# FIXME looks like x86 is the only arch running tests ;-) 6# FIXME looks like x86 is the only arch running tests ;-)
7# we need some IS_(32/64) flag to make this generic 7# we need some IS_(32/64) flag to make this generic
8ifeq ($(IS_X86_64),1) 8ifeq ($(ARCH)$(IS_64_BIT), x861)
9lib = lib64 9lib = lib64
10else 10else
11lib = lib 11lib = lib
diff --git a/tools/perf/tests/open-syscall-all-cpus.c b/tools/perf/tests/open-syscall-all-cpus.c
index 8fa82d1700c7..3ec885c48f8f 100644
--- a/tools/perf/tests/open-syscall-all-cpus.c
+++ b/tools/perf/tests/open-syscall-all-cpus.c
@@ -29,7 +29,12 @@ int test__open_syscall_event_on_all_cpus(void)
29 29
30 evsel = perf_evsel__newtp("syscalls", "sys_enter_open"); 30 evsel = perf_evsel__newtp("syscalls", "sys_enter_open");
31 if (evsel == NULL) { 31 if (evsel == NULL) {
32 pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); 32 if (tracefs_configured())
33 pr_debug("is tracefs mounted on /sys/kernel/tracing?\n");
34 else if (debugfs_configured())
35 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
36 else
37 pr_debug("Neither tracefs or debugfs is enabled in this kernel\n");
33 goto out_thread_map_delete; 38 goto out_thread_map_delete;
34 } 39 }
35 40
diff --git a/tools/perf/tests/open-syscall.c b/tools/perf/tests/open-syscall.c
index a33b2daae40f..07aa319bf334 100644
--- a/tools/perf/tests/open-syscall.c
+++ b/tools/perf/tests/open-syscall.c
@@ -18,7 +18,12 @@ int test__open_syscall_event(void)
18 18
19 evsel = perf_evsel__newtp("syscalls", "sys_enter_open"); 19 evsel = perf_evsel__newtp("syscalls", "sys_enter_open");
20 if (evsel == NULL) { 20 if (evsel == NULL) {
21 pr_debug("is debugfs mounted on /sys/kernel/debug?\n"); 21 if (tracefs_configured())
22 pr_debug("is tracefs mounted on /sys/kernel/tracing?\n");
23 else if (debugfs_configured())
24 pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
25 else
26 pr_debug("Neither tracefs or debugfs is enabled in this kernel\n");
22 goto out_thread_map_delete; 27 goto out_thread_map_delete;
23 } 28 }
24 29
diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-events.c
index 1cdab0ce00e2..3de744961739 100644
--- a/tools/perf/tests/parse-events.c
+++ b/tools/perf/tests/parse-events.c
@@ -3,6 +3,7 @@
3#include "evsel.h" 3#include "evsel.h"
4#include "evlist.h" 4#include "evlist.h"
5#include <api/fs/fs.h> 5#include <api/fs/fs.h>
6#include <api/fs/tracefs.h>
6#include <api/fs/debugfs.h> 7#include <api/fs/debugfs.h>
7#include "tests.h" 8#include "tests.h"
8#include "debug.h" 9#include "debug.h"
@@ -294,6 +295,36 @@ static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
294 return test__checkevent_genhw(evlist); 295 return test__checkevent_genhw(evlist);
295} 296}
296 297
298static int test__checkevent_exclude_idle_modifier(struct perf_evlist *evlist)
299{
300 struct perf_evsel *evsel = perf_evlist__first(evlist);
301
302 TEST_ASSERT_VAL("wrong exclude idle", evsel->attr.exclude_idle);
303 TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
304 TEST_ASSERT_VAL("wrong exclude host", !evsel->attr.exclude_host);
305 TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
306 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
307 TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
308 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
309
310 return test__checkevent_symbolic_name(evlist);
311}
312
313static int test__checkevent_exclude_idle_modifier_1(struct perf_evlist *evlist)
314{
315 struct perf_evsel *evsel = perf_evlist__first(evlist);
316
317 TEST_ASSERT_VAL("wrong exclude idle", evsel->attr.exclude_idle);
318 TEST_ASSERT_VAL("wrong exclude guest", !evsel->attr.exclude_guest);
319 TEST_ASSERT_VAL("wrong exclude host", evsel->attr.exclude_host);
320 TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
321 TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
322 TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
323 TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
324
325 return test__checkevent_symbolic_name(evlist);
326}
327
297static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist) 328static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist)
298{ 329{
299 struct perf_evsel *evsel = perf_evlist__first(evlist); 330 struct perf_evsel *evsel = perf_evlist__first(evlist);
@@ -1192,11 +1223,19 @@ static int count_tracepoints(void)
1192{ 1223{
1193 char events_path[PATH_MAX]; 1224 char events_path[PATH_MAX];
1194 struct dirent *events_ent; 1225 struct dirent *events_ent;
1226 const char *mountpoint;
1195 DIR *events_dir; 1227 DIR *events_dir;
1196 int cnt = 0; 1228 int cnt = 0;
1197 1229
1198 scnprintf(events_path, PATH_MAX, "%s/tracing/events", 1230 mountpoint = tracefs_find_mountpoint();
1199 debugfs_find_mountpoint()); 1231 if (mountpoint) {
1232 scnprintf(events_path, PATH_MAX, "%s/events",
1233 mountpoint);
1234 } else {
1235 mountpoint = debugfs_find_mountpoint();
1236 scnprintf(events_path, PATH_MAX, "%s/tracing/events",
1237 mountpoint);
1238 }
1200 1239
1201 events_dir = opendir(events_path); 1240 events_dir = opendir(events_path);
1202 1241
@@ -1485,6 +1524,16 @@ static struct evlist_test test__events[] = {
1485 .id = 100, 1524 .id = 100,
1486 }, 1525 },
1487#endif 1526#endif
1527 {
1528 .name = "instructions:I",
1529 .check = test__checkevent_exclude_idle_modifier,
1530 .id = 45,
1531 },
1532 {
1533 .name = "instructions:kIG",
1534 .check = test__checkevent_exclude_idle_modifier_1,
1535 .id = 46,
1536 },
1488}; 1537};
1489 1538
1490static struct evlist_test test__events_pmu[] = { 1539static struct evlist_test test__events_pmu[] = {
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 00e776a87a9c..52758a33f64c 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -51,6 +51,7 @@ int test__hists_cumulate(void);
51int test__switch_tracking(void); 51int test__switch_tracking(void);
52int test__fdarray__filter(void); 52int test__fdarray__filter(void);
53int test__fdarray__add(void); 53int test__fdarray__add(void);
54int test__kmod_path__parse(void);
54 55
55#if defined(__x86_64__) || defined(__i386__) || defined(__arm__) 56#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
56#ifdef HAVE_DWARF_UNWIND_SUPPORT 57#ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/ui/Build b/tools/perf/ui/Build
new file mode 100644
index 000000000000..0a73538c0441
--- /dev/null
+++ b/tools/perf/ui/Build
@@ -0,0 +1,14 @@
1libperf-y += setup.o
2libperf-y += helpline.o
3libperf-y += progress.o
4libperf-y += util.o
5libperf-y += hist.o
6libperf-y += stdio/hist.o
7
8CFLAGS_setup.o += -DLIBDIR="BUILD_STR($(LIBDIR))"
9
10libperf-$(CONFIG_SLANG) += browser.o
11libperf-$(CONFIG_SLANG) += browsers/
12libperf-$(CONFIG_SLANG) += tui/
13
14CFLAGS_browser.o += -DENABLE_SLFUTURE_CONST
diff --git a/tools/perf/ui/browsers/Build b/tools/perf/ui/browsers/Build
new file mode 100644
index 000000000000..de223f5bed58
--- /dev/null
+++ b/tools/perf/ui/browsers/Build
@@ -0,0 +1,10 @@
1libperf-y += annotate.o
2libperf-y += hists.o
3libperf-y += map.o
4libperf-y += scripts.o
5libperf-y += header.o
6
7CFLAGS_annotate.o += -DENABLE_SLFUTURE_CONST
8CFLAGS_hists.o += -DENABLE_SLFUTURE_CONST
9CFLAGS_map.o += -DENABLE_SLFUTURE_CONST
10CFLAGS_scripts.o += -DENABLE_SLFUTURE_CONST
diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/annotate.c
index 9d32e3c0cfee..e5250eb2dd57 100644
--- a/tools/perf/ui/browsers/annotate.c
+++ b/tools/perf/ui/browsers/annotate.c
@@ -829,10 +829,16 @@ out:
829 return key; 829 return key;
830} 830}
831 831
832int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
833 struct hist_browser_timer *hbt)
834{
835 return symbol__tui_annotate(ms->sym, ms->map, evsel, hbt);
836}
837
832int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel, 838int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
833 struct hist_browser_timer *hbt) 839 struct hist_browser_timer *hbt)
834{ 840{
835 return symbol__tui_annotate(he->ms.sym, he->ms.map, evsel, hbt); 841 return map_symbol__tui_annotate(&he->ms, evsel, hbt);
836} 842}
837 843
838static void annotate_browser__mark_jump_targets(struct annotate_browser *browser, 844static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c
index 788506eef567..995b7a8596b1 100644
--- a/tools/perf/ui/browsers/hists.c
+++ b/tools/perf/ui/browsers/hists.c
@@ -48,6 +48,24 @@ static bool hist_browser__has_filter(struct hist_browser *hb)
48 return hists__has_filter(hb->hists) || hb->min_pcnt; 48 return hists__has_filter(hb->hists) || hb->min_pcnt;
49} 49}
50 50
51static int hist_browser__get_folding(struct hist_browser *browser)
52{
53 struct rb_node *nd;
54 struct hists *hists = browser->hists;
55 int unfolded_rows = 0;
56
57 for (nd = rb_first(&hists->entries);
58 (nd = hists__filter_entries(nd, browser->min_pcnt)) != NULL;
59 nd = rb_next(nd)) {
60 struct hist_entry *he =
61 rb_entry(nd, struct hist_entry, rb_node);
62
63 if (he->ms.unfolded)
64 unfolded_rows += he->nr_rows;
65 }
66 return unfolded_rows;
67}
68
51static u32 hist_browser__nr_entries(struct hist_browser *hb) 69static u32 hist_browser__nr_entries(struct hist_browser *hb)
52{ 70{
53 u32 nr_entries; 71 u32 nr_entries;
@@ -57,6 +75,7 @@ static u32 hist_browser__nr_entries(struct hist_browser *hb)
57 else 75 else
58 nr_entries = hb->hists->nr_entries; 76 nr_entries = hb->hists->nr_entries;
59 77
78 hb->nr_callchain_rows = hist_browser__get_folding(hb);
60 return nr_entries + hb->nr_callchain_rows; 79 return nr_entries + hb->nr_callchain_rows;
61} 80}
62 81
@@ -492,6 +511,7 @@ static void hist_browser__show_callchain_entry(struct hist_browser *browser,
492{ 511{
493 int color, width; 512 int color, width;
494 char folded_sign = callchain_list__folded(chain); 513 char folded_sign = callchain_list__folded(chain);
514 bool show_annotated = browser->show_dso && chain->ms.sym && symbol__annotation(chain->ms.sym)->src;
495 515
496 color = HE_COLORSET_NORMAL; 516 color = HE_COLORSET_NORMAL;
497 width = browser->b.width - (offset + 2); 517 width = browser->b.width - (offset + 2);
@@ -504,7 +524,8 @@ static void hist_browser__show_callchain_entry(struct hist_browser *browser,
504 ui_browser__set_color(&browser->b, color); 524 ui_browser__set_color(&browser->b, color);
505 hist_browser__gotorc(browser, row, 0); 525 hist_browser__gotorc(browser, row, 0);
506 slsmg_write_nstring(" ", offset); 526 slsmg_write_nstring(" ", offset);
507 slsmg_printf("%c ", folded_sign); 527 slsmg_printf("%c", folded_sign);
528 ui_browser__write_graph(&browser->b, show_annotated ? SLSMG_RARROW_CHAR : ' ');
508 slsmg_write_nstring(str, width); 529 slsmg_write_nstring(str, width);
509} 530}
510 531
@@ -1467,7 +1488,7 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
1467 perf_hpp__set_user_width(symbol_conf.col_width_list_str); 1488 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
1468 1489
1469 while (1) { 1490 while (1) {
1470 const struct thread *thread = NULL; 1491 struct thread *thread = NULL;
1471 const struct dso *dso = NULL; 1492 const struct dso *dso = NULL;
1472 int choice = 0, 1493 int choice = 0,
1473 annotate = -2, zoom_dso = -2, zoom_thread = -2, 1494 annotate = -2, zoom_dso = -2, zoom_thread = -2,
@@ -1593,28 +1614,30 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
1593 if (!sort__has_sym) 1614 if (!sort__has_sym)
1594 goto add_exit_option; 1615 goto add_exit_option;
1595 1616
1617 if (browser->selection == NULL)
1618 goto skip_annotation;
1619
1596 if (sort__mode == SORT_MODE__BRANCH) { 1620 if (sort__mode == SORT_MODE__BRANCH) {
1597 bi = browser->he_selection->branch_info; 1621 bi = browser->he_selection->branch_info;
1598 if (browser->selection != NULL && 1622
1599 bi && 1623 if (bi == NULL)
1600 bi->from.sym != NULL && 1624 goto skip_annotation;
1625
1626 if (bi->from.sym != NULL &&
1601 !bi->from.map->dso->annotate_warned && 1627 !bi->from.map->dso->annotate_warned &&
1602 asprintf(&options[nr_options], "Annotate %s", 1628 asprintf(&options[nr_options], "Annotate %s", bi->from.sym->name) > 0) {
1603 bi->from.sym->name) > 0)
1604 annotate_f = nr_options++; 1629 annotate_f = nr_options++;
1630 }
1605 1631
1606 if (browser->selection != NULL && 1632 if (bi->to.sym != NULL &&
1607 bi &&
1608 bi->to.sym != NULL &&
1609 !bi->to.map->dso->annotate_warned && 1633 !bi->to.map->dso->annotate_warned &&
1610 (bi->to.sym != bi->from.sym || 1634 (bi->to.sym != bi->from.sym ||
1611 bi->to.map->dso != bi->from.map->dso) && 1635 bi->to.map->dso != bi->from.map->dso) &&
1612 asprintf(&options[nr_options], "Annotate %s", 1636 asprintf(&options[nr_options], "Annotate %s", bi->to.sym->name) > 0) {
1613 bi->to.sym->name) > 0)
1614 annotate_t = nr_options++; 1637 annotate_t = nr_options++;
1638 }
1615 } else { 1639 } else {
1616 if (browser->selection != NULL && 1640 if (browser->selection->sym != NULL &&
1617 browser->selection->sym != NULL &&
1618 !browser->selection->map->dso->annotate_warned) { 1641 !browser->selection->map->dso->annotate_warned) {
1619 struct annotation *notes; 1642 struct annotation *notes;
1620 1643
@@ -1622,11 +1645,12 @@ static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
1622 1645
1623 if (notes->src && 1646 if (notes->src &&
1624 asprintf(&options[nr_options], "Annotate %s", 1647 asprintf(&options[nr_options], "Annotate %s",
1625 browser->selection->sym->name) > 0) 1648 browser->selection->sym->name) > 0) {
1626 annotate = nr_options++; 1649 annotate = nr_options++;
1650 }
1627 } 1651 }
1628 } 1652 }
1629 1653skip_annotation:
1630 if (thread != NULL && 1654 if (thread != NULL &&
1631 asprintf(&options[nr_options], "Zoom %s %s(%d) thread", 1655 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
1632 (browser->hists->thread_filter ? "out of" : "into"), 1656 (browser->hists->thread_filter ? "out of" : "into"),
@@ -1682,6 +1706,7 @@ retry_popup_menu:
1682 if (choice == annotate || choice == annotate_t || choice == annotate_f) { 1706 if (choice == annotate || choice == annotate_t || choice == annotate_f) {
1683 struct hist_entry *he; 1707 struct hist_entry *he;
1684 struct annotation *notes; 1708 struct annotation *notes;
1709 struct map_symbol ms;
1685 int err; 1710 int err;
1686do_annotate: 1711do_annotate:
1687 if (!objdump_path && perf_session_env__lookup_objdump(env)) 1712 if (!objdump_path && perf_session_env__lookup_objdump(env))
@@ -1691,30 +1716,21 @@ do_annotate:
1691 if (he == NULL) 1716 if (he == NULL)
1692 continue; 1717 continue;
1693 1718
1694 /*
1695 * we stash the branch_info symbol + map into the
1696 * the ms so we don't have to rewrite all the annotation
1697 * code to use branch_info.
1698 * in branch mode, the ms struct is not used
1699 */
1700 if (choice == annotate_f) { 1719 if (choice == annotate_f) {
1701 he->ms.sym = he->branch_info->from.sym; 1720 ms.map = he->branch_info->from.map;
1702 he->ms.map = he->branch_info->from.map; 1721 ms.sym = he->branch_info->from.sym;
1703 } else if (choice == annotate_t) { 1722 } else if (choice == annotate_t) {
1704 he->ms.sym = he->branch_info->to.sym; 1723 ms.map = he->branch_info->to.map;
1705 he->ms.map = he->branch_info->to.map; 1724 ms.sym = he->branch_info->to.sym;
1725 } else {
1726 ms = *browser->selection;
1706 } 1727 }
1707 1728
1708 notes = symbol__annotation(he->ms.sym); 1729 notes = symbol__annotation(ms.sym);
1709 if (!notes->src) 1730 if (!notes->src)
1710 continue; 1731 continue;
1711 1732
1712 /* 1733 err = map_symbol__tui_annotate(&ms, evsel, hbt);
1713 * Don't let this be freed, say, by hists__decay_entry.
1714 */
1715 he->used = true;
1716 err = hist_entry__tui_annotate(he, evsel, hbt);
1717 he->used = false;
1718 /* 1734 /*
1719 * offer option to annotate the other branch source or target 1735 * offer option to annotate the other branch source or target
1720 * (if they exists) when returning from annotate 1736 * (if they exists) when returning from annotate
@@ -1754,13 +1770,13 @@ zoom_thread:
1754 pstack__remove(fstack, &browser->hists->thread_filter); 1770 pstack__remove(fstack, &browser->hists->thread_filter);
1755zoom_out_thread: 1771zoom_out_thread:
1756 ui_helpline__pop(); 1772 ui_helpline__pop();
1757 browser->hists->thread_filter = NULL; 1773 thread__zput(browser->hists->thread_filter);
1758 perf_hpp__set_elide(HISTC_THREAD, false); 1774 perf_hpp__set_elide(HISTC_THREAD, false);
1759 } else { 1775 } else {
1760 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"", 1776 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1761 thread->comm_set ? thread__comm_str(thread) : "", 1777 thread->comm_set ? thread__comm_str(thread) : "",
1762 thread->tid); 1778 thread->tid);
1763 browser->hists->thread_filter = thread; 1779 browser->hists->thread_filter = thread__get(thread);
1764 perf_hpp__set_elide(HISTC_THREAD, false); 1780 perf_hpp__set_elide(HISTC_THREAD, false);
1765 pstack__push(fstack, &browser->hists->thread_filter); 1781 pstack__push(fstack, &browser->hists->thread_filter);
1766 } 1782 }
diff --git a/tools/perf/ui/gtk/Build b/tools/perf/ui/gtk/Build
new file mode 100644
index 000000000000..ec22e899a224
--- /dev/null
+++ b/tools/perf/ui/gtk/Build
@@ -0,0 +1,9 @@
1CFLAGS_gtk += -fPIC $(GTK_CFLAGS)
2
3gtk-y += browser.o
4gtk-y += hists.o
5gtk-y += setup.o
6gtk-y += util.o
7gtk-y += helpline.o
8gtk-y += progress.o
9gtk-y += annotate.o
diff --git a/tools/perf/ui/tui/Build b/tools/perf/ui/tui/Build
new file mode 100644
index 000000000000..9e4c6ca41a9f
--- /dev/null
+++ b/tools/perf/ui/tui/Build
@@ -0,0 +1,4 @@
1libperf-y += setup.o
2libperf-y += util.o
3libperf-y += helpline.o
4libperf-y += progress.o
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
new file mode 100644
index 000000000000..797490a40075
--- /dev/null
+++ b/tools/perf/util/Build
@@ -0,0 +1,145 @@
1libperf-y += abspath.o
2libperf-y += alias.o
3libperf-y += annotate.o
4libperf-y += build-id.o
5libperf-y += config.o
6libperf-y += ctype.o
7libperf-y += db-export.o
8libperf-y += environment.o
9libperf-y += event.o
10libperf-y += evlist.o
11libperf-y += evsel.o
12libperf-y += exec_cmd.o
13libperf-y += find_next_bit.o
14libperf-y += help.o
15libperf-y += kallsyms.o
16libperf-y += levenshtein.o
17libperf-y += parse-options.o
18libperf-y += parse-events.o
19libperf-y += path.o
20libperf-y += rbtree.o
21libperf-y += bitmap.o
22libperf-y += hweight.o
23libperf-y += run-command.o
24libperf-y += quote.o
25libperf-y += strbuf.o
26libperf-y += string.o
27libperf-y += strlist.o
28libperf-y += strfilter.o
29libperf-y += top.o
30libperf-y += usage.o
31libperf-y += wrapper.o
32libperf-y += sigchain.o
33libperf-y += dso.o
34libperf-y += symbol.o
35libperf-y += color.o
36libperf-y += pager.o
37libperf-y += header.o
38libperf-y += callchain.o
39libperf-y += values.o
40libperf-y += debug.o
41libperf-y += machine.o
42libperf-y += map.o
43libperf-y += pstack.o
44libperf-y += session.o
45libperf-y += ordered-events.o
46libperf-y += comm.o
47libperf-y += thread.o
48libperf-y += thread_map.o
49libperf-y += trace-event-parse.o
50libperf-y += parse-events-flex.o
51libperf-y += parse-events-bison.o
52libperf-y += pmu.o
53libperf-y += pmu-flex.o
54libperf-y += pmu-bison.o
55libperf-y += trace-event-read.o
56libperf-y += trace-event-info.o
57libperf-y += trace-event-scripting.o
58libperf-y += trace-event.o
59libperf-y += svghelper.o
60libperf-y += sort.o
61libperf-y += hist.o
62libperf-y += util.o
63libperf-y += xyarray.o
64libperf-y += cpumap.o
65libperf-y += cgroup.o
66libperf-y += target.o
67libperf-y += rblist.o
68libperf-y += intlist.o
69libperf-y += vdso.o
70libperf-y += stat.o
71libperf-y += record.o
72libperf-y += srcline.o
73libperf-y += data.o
74libperf-$(CONFIG_X86) += tsc.o
75libperf-y += cloexec.o
76libperf-y += thread-stack.o
77
78libperf-$(CONFIG_LIBELF) += symbol-elf.o
79libperf-$(CONFIG_LIBELF) += probe-event.o
80
81ifndef CONFIG_LIBELF
82libperf-y += symbol-minimal.o
83endif
84
85libperf-$(CONFIG_DWARF) += probe-finder.o
86libperf-$(CONFIG_DWARF) += dwarf-aux.o
87
88libperf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
89libperf-$(CONFIG_LIBUNWIND) += unwind-libunwind.o
90
91libperf-$(CONFIG_LIBBABELTRACE) += data-convert-bt.o
92
93libperf-y += scripting-engines/
94
95libperf-$(CONFIG_PERF_REGS) += perf_regs.o
96libperf-$(CONFIG_ZLIB) += zlib.o
97libperf-$(CONFIG_LZMA) += lzma.o
98
99CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
100CFLAGS_exec_cmd.o += -DPERF_EXEC_PATH="BUILD_STR($(perfexecdir_SQ))" -DPREFIX="BUILD_STR($(prefix_SQ))"
101
102$(OUTPUT)util/parse-events-flex.c: util/parse-events.l $(OUTPUT)util/parse-events-bison.c
103 $(call rule_mkdir)
104 @$(call echo-cmd,flex)$(FLEX) -o $@ --header-file=$(OUTPUT)util/parse-events-flex.h $(PARSER_DEBUG_FLEX) util/parse-events.l
105
106$(OUTPUT)util/parse-events-bison.c: util/parse-events.y
107 $(call rule_mkdir)
108 @$(call echo-cmd,bison)$(BISON) -v util/parse-events.y -d $(PARSER_DEBUG_BISON) -o $@ -p parse_events_
109
110$(OUTPUT)util/pmu-flex.c: util/pmu.l $(OUTPUT)util/pmu-bison.c
111 $(call rule_mkdir)
112 @$(call echo-cmd,flex)$(FLEX) -o $@ --header-file=$(OUTPUT)util/pmu-flex.h util/pmu.l
113
114$(OUTPUT)util/pmu-bison.c: util/pmu.y
115 $(call rule_mkdir)
116 @$(call echo-cmd,bison)$(BISON) -v util/pmu.y -d -o $@ -p perf_pmu_
117
118CFLAGS_parse-events-flex.o += -w
119CFLAGS_pmu-flex.o += -w
120CFLAGS_parse-events-bison.o += -DYYENABLE_NLS=0 -DYYLTYPE_IS_TRIVIAL=0 -w
121CFLAGS_pmu-bison.o += -DYYENABLE_NLS=0 -DYYLTYPE_IS_TRIVIAL=0 -w
122
123$(OUTPUT)util/parse-events.o: $(OUTPUT)util/parse-events-flex.c $(OUTPUT)util/parse-events-bison.c
124$(OUTPUT)util/pmu.o: $(OUTPUT)util/pmu-flex.c $(OUTPUT)util/pmu-bison.c
125
126CFLAGS_find_next_bit.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
127CFLAGS_rbtree.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
128CFLAGS_hweight.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
129CFLAGS_parse-events.o += -Wno-redundant-decls
130
131$(OUTPUT)util/kallsyms.o: ../lib/symbol/kallsyms.c FORCE
132 $(call rule_mkdir)
133 $(call if_changed_dep,cc_o_c)
134
135$(OUTPUT)util/find_next_bit.o: ../lib/util/find_next_bit.c FORCE
136 $(call rule_mkdir)
137 $(call if_changed_dep,cc_o_c)
138
139$(OUTPUT)util/rbtree.o: ../../lib/rbtree.c FORCE
140 $(call rule_mkdir)
141 $(call if_changed_dep,cc_o_c)
142
143$(OUTPUT)util/hweight.o: ../../lib/hweight.c FORCE
144 $(call rule_mkdir)
145 $(call if_changed_dep,cc_o_c)
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 9d9db3b296dd..7f5bdfc9bc87 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -1010,6 +1010,32 @@ fallback:
1010 } 1010 }
1011 filename = symfs_filename; 1011 filename = symfs_filename;
1012 } 1012 }
1013 } else if (dso__needs_decompress(dso)) {
1014 char tmp[PATH_MAX];
1015 struct kmod_path m;
1016 int fd;
1017 bool ret;
1018
1019 if (kmod_path__parse_ext(&m, symfs_filename))
1020 goto out_free_filename;
1021
1022 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1023
1024 fd = mkstemp(tmp);
1025 if (fd < 0) {
1026 free(m.ext);
1027 goto out_free_filename;
1028 }
1029
1030 ret = decompress_to_file(m.ext, symfs_filename, fd);
1031
1032 free(m.ext);
1033 close(fd);
1034
1035 if (!ret)
1036 goto out_free_filename;
1037
1038 strcpy(symfs_filename, tmp);
1013 } 1039 }
1014 1040
1015 snprintf(command, sizeof(command), 1041 snprintf(command, sizeof(command),
@@ -1029,7 +1055,7 @@ fallback:
1029 1055
1030 file = popen(command, "r"); 1056 file = popen(command, "r");
1031 if (!file) 1057 if (!file)
1032 goto out_free_filename; 1058 goto out_remove_tmp;
1033 1059
1034 while (!feof(file)) 1060 while (!feof(file))
1035 if (symbol__parse_objdump_line(sym, map, file, privsize, 1061 if (symbol__parse_objdump_line(sym, map, file, privsize,
@@ -1044,6 +1070,10 @@ fallback:
1044 delete_last_nop(sym); 1070 delete_last_nop(sym);
1045 1071
1046 pclose(file); 1072 pclose(file);
1073
1074out_remove_tmp:
1075 if (dso__needs_decompress(dso))
1076 unlink(symfs_filename);
1047out_free_filename: 1077out_free_filename:
1048 if (delete_extract) 1078 if (delete_extract)
1049 kcore_extract__delete(&kce); 1079 kcore_extract__delete(&kce);
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 0c72680a977f..61867dff5d5a 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -59,11 +59,8 @@ static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
59 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid, 59 dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
60 event->fork.ppid, event->fork.ptid); 60 event->fork.ppid, event->fork.ptid);
61 61
62 if (thread) { 62 if (thread)
63 rb_erase(&thread->rb_node, &machine->threads); 63 machine__remove_thread(machine, thread);
64 machine->last_match = NULL;
65 thread__delete(thread);
66 }
67 64
68 return 0; 65 return 0;
69} 66}
@@ -93,6 +90,35 @@ int build_id__sprintf(const u8 *build_id, int len, char *bf)
93 return raw - build_id; 90 return raw - build_id;
94} 91}
95 92
93/* asnprintf consolidates asprintf and snprintf */
94static int asnprintf(char **strp, size_t size, const char *fmt, ...)
95{
96 va_list ap;
97 int ret;
98
99 if (!strp)
100 return -EINVAL;
101
102 va_start(ap, fmt);
103 if (*strp)
104 ret = vsnprintf(*strp, size, fmt, ap);
105 else
106 ret = vasprintf(strp, fmt, ap);
107 va_end(ap);
108
109 return ret;
110}
111
112static char *build_id__filename(const char *sbuild_id, char *bf, size_t size)
113{
114 char *tmp = bf;
115 int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
116 sbuild_id, sbuild_id + 2);
117 if (ret < 0 || (tmp && size < (unsigned int)ret))
118 return NULL;
119 return bf;
120}
121
96char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) 122char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
97{ 123{
98 char build_id_hex[BUILD_ID_SIZE * 2 + 1]; 124 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
@@ -101,14 +127,7 @@ char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
101 return NULL; 127 return NULL;
102 128
103 build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex); 129 build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex);
104 if (bf == NULL) { 130 return build_id__filename(build_id_hex, bf, size);
105 if (asprintf(&bf, "%s/.build-id/%.2s/%s", buildid_dir,
106 build_id_hex, build_id_hex + 2) < 0)
107 return NULL;
108 } else
109 snprintf(bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
110 build_id_hex, build_id_hex + 2);
111 return bf;
112} 131}
113 132
114#define dsos__for_each_with_build_id(pos, head) \ 133#define dsos__for_each_with_build_id(pos, head) \
@@ -259,52 +278,113 @@ void disable_buildid_cache(void)
259 no_buildid_cache = true; 278 no_buildid_cache = true;
260} 279}
261 280
262int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, 281static char *build_id_cache__dirname_from_path(const char *name,
263 const char *name, bool is_kallsyms, bool is_vdso) 282 bool is_kallsyms, bool is_vdso)
264{ 283{
265 const size_t size = PATH_MAX; 284 char *realname = (char *)name, *filename;
266 char *realname, *filename = zalloc(size),
267 *linkname = zalloc(size), *targetname;
268 int len, err = -1;
269 bool slash = is_kallsyms || is_vdso; 285 bool slash = is_kallsyms || is_vdso;
270 286
271 if (is_kallsyms) { 287 if (!slash) {
272 if (symbol_conf.kptr_restrict) {
273 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
274 err = 0;
275 goto out_free;
276 }
277 realname = (char *) name;
278 } else
279 realname = realpath(name, NULL); 288 realname = realpath(name, NULL);
289 if (!realname)
290 return NULL;
291 }
280 292
281 if (realname == NULL || filename == NULL || linkname == NULL) 293 if (asprintf(&filename, "%s%s%s", buildid_dir, slash ? "/" : "",
294 is_vdso ? DSO__NAME_VDSO : realname) < 0)
295 filename = NULL;
296
297 if (!slash)
298 free(realname);
299
300 return filename;
301}
302
303int build_id_cache__list_build_ids(const char *pathname,
304 struct strlist **result)
305{
306 struct strlist *list;
307 char *dir_name;
308 DIR *dir;
309 struct dirent *d;
310 int ret = 0;
311
312 list = strlist__new(true, NULL);
313 dir_name = build_id_cache__dirname_from_path(pathname, false, false);
314 if (!list || !dir_name) {
315 ret = -ENOMEM;
316 goto out;
317 }
318
319 /* List up all dirents */
320 dir = opendir(dir_name);
321 if (!dir) {
322 ret = -errno;
323 goto out;
324 }
325
326 while ((d = readdir(dir)) != NULL) {
327 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
328 continue;
329 strlist__add(list, d->d_name);
330 }
331 closedir(dir);
332
333out:
334 free(dir_name);
335 if (ret)
336 strlist__delete(list);
337 else
338 *result = list;
339
340 return ret;
341}
342
343int build_id_cache__add_s(const char *sbuild_id, const char *name,
344 bool is_kallsyms, bool is_vdso)
345{
346 const size_t size = PATH_MAX;
347 char *realname = NULL, *filename = NULL, *dir_name = NULL,
348 *linkname = zalloc(size), *targetname, *tmp;
349 int err = -1;
350
351 if (!is_kallsyms) {
352 realname = realpath(name, NULL);
353 if (!realname)
354 goto out_free;
355 }
356
357 dir_name = build_id_cache__dirname_from_path(name, is_kallsyms, is_vdso);
358 if (!dir_name)
282 goto out_free; 359 goto out_free;
283 360
284 len = scnprintf(filename, size, "%s%s%s", 361 if (mkdir_p(dir_name, 0755))
285 debugdir, slash ? "/" : "",
286 is_vdso ? DSO__NAME_VDSO : realname);
287 if (mkdir_p(filename, 0755))
288 goto out_free; 362 goto out_free;
289 363
290 snprintf(filename + len, size - len, "/%s", sbuild_id); 364 if (asprintf(&filename, "%s/%s", dir_name, sbuild_id) < 0) {
365 filename = NULL;
366 goto out_free;
367 }
291 368
292 if (access(filename, F_OK)) { 369 if (access(filename, F_OK)) {
293 if (is_kallsyms) { 370 if (is_kallsyms) {
294 if (copyfile("/proc/kallsyms", filename)) 371 if (copyfile("/proc/kallsyms", filename))
295 goto out_free; 372 goto out_free;
296 } else if (link(realname, filename) && copyfile(name, filename)) 373 } else if (link(realname, filename) && errno != EEXIST &&
374 copyfile(name, filename))
297 goto out_free; 375 goto out_free;
298 } 376 }
299 377
300 len = scnprintf(linkname, size, "%s/.build-id/%.2s", 378 if (!build_id__filename(sbuild_id, linkname, size))
301 debugdir, sbuild_id); 379 goto out_free;
380 tmp = strrchr(linkname, '/');
381 *tmp = '\0';
302 382
303 if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) 383 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
304 goto out_free; 384 goto out_free;
305 385
306 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); 386 *tmp = '/';
307 targetname = filename + strlen(debugdir) - 5; 387 targetname = filename + strlen(buildid_dir) - 5;
308 memcpy(targetname, "../..", 5); 388 memcpy(targetname, "../..", 5);
309 389
310 if (symlink(targetname, linkname) == 0) 390 if (symlink(targetname, linkname) == 0)
@@ -313,34 +393,46 @@ out_free:
313 if (!is_kallsyms) 393 if (!is_kallsyms)
314 free(realname); 394 free(realname);
315 free(filename); 395 free(filename);
396 free(dir_name);
316 free(linkname); 397 free(linkname);
317 return err; 398 return err;
318} 399}
319 400
320static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, 401static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
321 const char *name, const char *debugdir, 402 const char *name, bool is_kallsyms,
322 bool is_kallsyms, bool is_vdso) 403 bool is_vdso)
323{ 404{
324 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 405 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
325 406
326 build_id__sprintf(build_id, build_id_size, sbuild_id); 407 build_id__sprintf(build_id, build_id_size, sbuild_id);
327 408
328 return build_id_cache__add_s(sbuild_id, debugdir, name, 409 return build_id_cache__add_s(sbuild_id, name, is_kallsyms, is_vdso);
329 is_kallsyms, is_vdso); 410}
411
412bool build_id_cache__cached(const char *sbuild_id)
413{
414 bool ret = false;
415 char *filename = build_id__filename(sbuild_id, NULL, 0);
416
417 if (filename && !access(filename, F_OK))
418 ret = true;
419 free(filename);
420
421 return ret;
330} 422}
331 423
332int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) 424int build_id_cache__remove_s(const char *sbuild_id)
333{ 425{
334 const size_t size = PATH_MAX; 426 const size_t size = PATH_MAX;
335 char *filename = zalloc(size), 427 char *filename = zalloc(size),
336 *linkname = zalloc(size); 428 *linkname = zalloc(size), *tmp;
337 int err = -1; 429 int err = -1;
338 430
339 if (filename == NULL || linkname == NULL) 431 if (filename == NULL || linkname == NULL)
340 goto out_free; 432 goto out_free;
341 433
342 snprintf(linkname, size, "%s/.build-id/%.2s/%s", 434 if (!build_id__filename(sbuild_id, linkname, size))
343 debugdir, sbuild_id, sbuild_id + 2); 435 goto out_free;
344 436
345 if (access(linkname, F_OK)) 437 if (access(linkname, F_OK))
346 goto out_free; 438 goto out_free;
@@ -354,8 +446,8 @@ int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
354 /* 446 /*
355 * Since the link is relative, we must make it absolute: 447 * Since the link is relative, we must make it absolute:
356 */ 448 */
357 snprintf(linkname, size, "%s/.build-id/%.2s/%s", 449 tmp = strrchr(linkname, '/') + 1;
358 debugdir, sbuild_id, filename); 450 snprintf(tmp, size - (tmp - linkname), "%s", filename);
359 451
360 if (unlink(linkname)) 452 if (unlink(linkname))
361 goto out_free; 453 goto out_free;
@@ -367,8 +459,7 @@ out_free:
367 return err; 459 return err;
368} 460}
369 461
370static int dso__cache_build_id(struct dso *dso, struct machine *machine, 462static int dso__cache_build_id(struct dso *dso, struct machine *machine)
371 const char *debugdir)
372{ 463{
373 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; 464 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
374 bool is_vdso = dso__is_vdso(dso); 465 bool is_vdso = dso__is_vdso(dso);
@@ -381,28 +472,26 @@ static int dso__cache_build_id(struct dso *dso, struct machine *machine,
381 name = nm; 472 name = nm;
382 } 473 }
383 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name, 474 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
384 debugdir, is_kallsyms, is_vdso); 475 is_kallsyms, is_vdso);
385} 476}
386 477
387static int __dsos__cache_build_ids(struct list_head *head, 478static int __dsos__cache_build_ids(struct list_head *head,
388 struct machine *machine, const char *debugdir) 479 struct machine *machine)
389{ 480{
390 struct dso *pos; 481 struct dso *pos;
391 int err = 0; 482 int err = 0;
392 483
393 dsos__for_each_with_build_id(pos, head) 484 dsos__for_each_with_build_id(pos, head)
394 if (dso__cache_build_id(pos, machine, debugdir)) 485 if (dso__cache_build_id(pos, machine))
395 err = -1; 486 err = -1;
396 487
397 return err; 488 return err;
398} 489}
399 490
400static int machine__cache_build_ids(struct machine *machine, const char *debugdir) 491static int machine__cache_build_ids(struct machine *machine)
401{ 492{
402 int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine, 493 int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine);
403 debugdir); 494 ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine);
404 ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine,
405 debugdir);
406 return ret; 495 return ret;
407} 496}
408 497
@@ -417,11 +506,11 @@ int perf_session__cache_build_ids(struct perf_session *session)
417 if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST) 506 if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST)
418 return -1; 507 return -1;
419 508
420 ret = machine__cache_build_ids(&session->machines.host, buildid_dir); 509 ret = machine__cache_build_ids(&session->machines.host);
421 510
422 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { 511 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
423 struct machine *pos = rb_entry(nd, struct machine, rb_node); 512 struct machine *pos = rb_entry(nd, struct machine, rb_node);
424 ret |= machine__cache_build_ids(pos, buildid_dir); 513 ret |= machine__cache_build_ids(pos);
425 } 514 }
426 return ret ? -1 : 0; 515 return ret ? -1 : 0;
427} 516}
diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h
index 8236319514d5..85011222cc14 100644
--- a/tools/perf/util/build-id.h
+++ b/tools/perf/util/build-id.h
@@ -4,6 +4,7 @@
4#define BUILD_ID_SIZE 20 4#define BUILD_ID_SIZE 20
5 5
6#include "tool.h" 6#include "tool.h"
7#include "strlist.h"
7#include <linux/types.h> 8#include <linux/types.h>
8 9
9extern struct perf_tool build_id__mark_dso_hit_ops; 10extern struct perf_tool build_id__mark_dso_hit_ops;
@@ -22,9 +23,12 @@ bool perf_session__read_build_ids(struct perf_session *session, bool with_hits);
22int perf_session__write_buildid_table(struct perf_session *session, int fd); 23int perf_session__write_buildid_table(struct perf_session *session, int fd);
23int perf_session__cache_build_ids(struct perf_session *session); 24int perf_session__cache_build_ids(struct perf_session *session);
24 25
25int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, 26int build_id_cache__list_build_ids(const char *pathname,
27 struct strlist **result);
28bool build_id_cache__cached(const char *sbuild_id);
29int build_id_cache__add_s(const char *sbuild_id,
26 const char *name, bool is_kallsyms, bool is_vdso); 30 const char *name, bool is_kallsyms, bool is_vdso);
27int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir); 31int build_id_cache__remove_s(const char *sbuild_id);
28void disable_buildid_cache(void); 32void disable_buildid_cache(void);
29 33
30#endif 34#endif
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index d04d770d90f6..fbcca21d66ab 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -17,6 +17,7 @@
17#define EXEC_PATH_ENVIRONMENT "PERF_EXEC_PATH" 17#define EXEC_PATH_ENVIRONMENT "PERF_EXEC_PATH"
18#define DEFAULT_PERF_DIR_ENVIRONMENT ".perf" 18#define DEFAULT_PERF_DIR_ENVIRONMENT ".perf"
19#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR" 19#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
20#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
20 21
21typedef int (*config_fn_t)(const char *, const char *, void *); 22typedef int (*config_fn_t)(const char *, const char *, void *);
22extern int perf_default_config(const char *, const char *, void *); 23extern int perf_default_config(const char *, const char *, void *);
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 14e7a123d43b..9f643ee77001 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -97,6 +97,14 @@ int parse_callchain_record_opt(const char *arg)
97 callchain_param.dump_size = size; 97 callchain_param.dump_size = size;
98 } 98 }
99#endif /* HAVE_DWARF_UNWIND_SUPPORT */ 99#endif /* HAVE_DWARF_UNWIND_SUPPORT */
100 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
101 if (!strtok_r(NULL, ",", &saveptr)) {
102 callchain_param.record_mode = CALLCHAIN_LBR;
103 ret = 0;
104 } else
105 pr_err("callchain: No more arguments "
106 "needed for --call-graph lbr\n");
107 break;
100 } else { 108 } else {
101 pr_err("callchain: Unknown --call-graph option " 109 pr_err("callchain: Unknown --call-graph option "
102 "value: %s\n", arg); 110 "value: %s\n", arg);
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h
index c0ec1acc38e4..6033a0a212ca 100644
--- a/tools/perf/util/callchain.h
+++ b/tools/perf/util/callchain.h
@@ -11,6 +11,7 @@ enum perf_call_graph_mode {
11 CALLCHAIN_NONE, 11 CALLCHAIN_NONE,
12 CALLCHAIN_FP, 12 CALLCHAIN_FP,
13 CALLCHAIN_DWARF, 13 CALLCHAIN_DWARF,
14 CALLCHAIN_LBR,
14 CALLCHAIN_MAX 15 CALLCHAIN_MAX
15}; 16};
16 17
diff --git a/tools/perf/util/cloexec.c b/tools/perf/util/cloexec.c
index 6da965bdbc2c..85b523885f9d 100644
--- a/tools/perf/util/cloexec.c
+++ b/tools/perf/util/cloexec.c
@@ -7,6 +7,12 @@
7 7
8static unsigned long flag = PERF_FLAG_FD_CLOEXEC; 8static unsigned long flag = PERF_FLAG_FD_CLOEXEC;
9 9
10int __weak sched_getcpu(void)
11{
12 errno = ENOSYS;
13 return -1;
14}
15
10static int perf_flag_probe(void) 16static int perf_flag_probe(void)
11{ 17{
12 /* use 'safest' configuration as used in perf_evsel__fallback() */ 18 /* use 'safest' configuration as used in perf_evsel__fallback() */
diff --git a/tools/perf/util/cloexec.h b/tools/perf/util/cloexec.h
index 94a5a7d829d5..68888c29b04a 100644
--- a/tools/perf/util/cloexec.h
+++ b/tools/perf/util/cloexec.h
@@ -3,4 +3,10 @@
3 3
4unsigned long perf_event_open_cloexec_flag(void); 4unsigned long perf_event_open_cloexec_flag(void);
5 5
6#ifdef __GLIBC_PREREQ
7#if !__GLIBC_PREREQ(2, 6)
8extern int sched_getcpu(void) __THROW;
9#endif
10#endif
11
6#endif /* __PERF_CLOEXEC_H */ 12#endif /* __PERF_CLOEXEC_H */
diff --git a/tools/perf/util/data-convert-bt.c b/tools/perf/util/data-convert-bt.c
new file mode 100644
index 000000000000..dd17c9a32fbc
--- /dev/null
+++ b/tools/perf/util/data-convert-bt.c
@@ -0,0 +1,857 @@
1/*
2 * CTF writing support via babeltrace.
3 *
4 * Copyright (C) 2014, Jiri Olsa <jolsa@redhat.com>
5 * Copyright (C) 2014, Sebastian Andrzej Siewior <bigeasy@linutronix.de>
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
10#include <linux/compiler.h>
11#include <babeltrace/ctf-writer/writer.h>
12#include <babeltrace/ctf-writer/clock.h>
13#include <babeltrace/ctf-writer/stream.h>
14#include <babeltrace/ctf-writer/event.h>
15#include <babeltrace/ctf-writer/event-types.h>
16#include <babeltrace/ctf-writer/event-fields.h>
17#include <babeltrace/ctf/events.h>
18#include <traceevent/event-parse.h>
19#include "asm/bug.h"
20#include "data-convert-bt.h"
21#include "session.h"
22#include "util.h"
23#include "debug.h"
24#include "tool.h"
25#include "evlist.h"
26#include "evsel.h"
27#include "machine.h"
28
29#define pr_N(n, fmt, ...) \
30 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
31
32#define pr(fmt, ...) pr_N(1, pr_fmt(fmt), ##__VA_ARGS__)
33#define pr2(fmt, ...) pr_N(2, pr_fmt(fmt), ##__VA_ARGS__)
34
35#define pr_time2(t, fmt, ...) pr_time_N(2, debug_data_convert, t, pr_fmt(fmt), ##__VA_ARGS__)
36
37struct evsel_priv {
38 struct bt_ctf_event_class *event_class;
39};
40
41struct ctf_writer {
42 /* writer primitives */
43 struct bt_ctf_writer *writer;
44 struct bt_ctf_stream *stream;
45 struct bt_ctf_stream_class *stream_class;
46 struct bt_ctf_clock *clock;
47
48 /* data types */
49 union {
50 struct {
51 struct bt_ctf_field_type *s64;
52 struct bt_ctf_field_type *u64;
53 struct bt_ctf_field_type *s32;
54 struct bt_ctf_field_type *u32;
55 struct bt_ctf_field_type *string;
56 struct bt_ctf_field_type *u64_hex;
57 };
58 struct bt_ctf_field_type *array[6];
59 } data;
60};
61
62struct convert {
63 struct perf_tool tool;
64 struct ctf_writer writer;
65
66 u64 events_size;
67 u64 events_count;
68};
69
70static int value_set(struct bt_ctf_field_type *type,
71 struct bt_ctf_event *event,
72 const char *name, u64 val)
73{
74 struct bt_ctf_field *field;
75 bool sign = bt_ctf_field_type_integer_get_signed(type);
76 int ret;
77
78 field = bt_ctf_field_create(type);
79 if (!field) {
80 pr_err("failed to create a field %s\n", name);
81 return -1;
82 }
83
84 if (sign) {
85 ret = bt_ctf_field_signed_integer_set_value(field, val);
86 if (ret) {
87 pr_err("failed to set field value %s\n", name);
88 goto err;
89 }
90 } else {
91 ret = bt_ctf_field_unsigned_integer_set_value(field, val);
92 if (ret) {
93 pr_err("failed to set field value %s\n", name);
94 goto err;
95 }
96 }
97
98 ret = bt_ctf_event_set_payload(event, name, field);
99 if (ret) {
100 pr_err("failed to set payload %s\n", name);
101 goto err;
102 }
103
104 pr2(" SET [%s = %" PRIu64 "]\n", name, val);
105
106err:
107 bt_ctf_field_put(field);
108 return ret;
109}
110
111#define __FUNC_VALUE_SET(_name, _val_type) \
112static __maybe_unused int value_set_##_name(struct ctf_writer *cw, \
113 struct bt_ctf_event *event, \
114 const char *name, \
115 _val_type val) \
116{ \
117 struct bt_ctf_field_type *type = cw->data._name; \
118 return value_set(type, event, name, (u64) val); \
119}
120
121#define FUNC_VALUE_SET(_name) __FUNC_VALUE_SET(_name, _name)
122
123FUNC_VALUE_SET(s32)
124FUNC_VALUE_SET(u32)
125FUNC_VALUE_SET(s64)
126FUNC_VALUE_SET(u64)
127__FUNC_VALUE_SET(u64_hex, u64)
128
129static struct bt_ctf_field_type*
130get_tracepoint_field_type(struct ctf_writer *cw, struct format_field *field)
131{
132 unsigned long flags = field->flags;
133
134 if (flags & FIELD_IS_STRING)
135 return cw->data.string;
136
137 if (!(flags & FIELD_IS_SIGNED)) {
138 /* unsigned long are mostly pointers */
139 if (flags & FIELD_IS_LONG || flags & FIELD_IS_POINTER)
140 return cw->data.u64_hex;
141 }
142
143 if (flags & FIELD_IS_SIGNED) {
144 if (field->size == 8)
145 return cw->data.s64;
146 else
147 return cw->data.s32;
148 }
149
150 if (field->size == 8)
151 return cw->data.u64;
152 else
153 return cw->data.u32;
154}
155
156static int add_tracepoint_field_value(struct ctf_writer *cw,
157 struct bt_ctf_event_class *event_class,
158 struct bt_ctf_event *event,
159 struct perf_sample *sample,
160 struct format_field *fmtf)
161{
162 struct bt_ctf_field_type *type;
163 struct bt_ctf_field *array_field;
164 struct bt_ctf_field *field;
165 const char *name = fmtf->name;
166 void *data = sample->raw_data;
167 unsigned long long value_int;
168 unsigned long flags = fmtf->flags;
169 unsigned int n_items;
170 unsigned int i;
171 unsigned int offset;
172 unsigned int len;
173 int ret;
174
175 offset = fmtf->offset;
176 len = fmtf->size;
177 if (flags & FIELD_IS_STRING)
178 flags &= ~FIELD_IS_ARRAY;
179
180 if (flags & FIELD_IS_DYNAMIC) {
181 unsigned long long tmp_val;
182
183 tmp_val = pevent_read_number(fmtf->event->pevent,
184 data + offset, len);
185 offset = tmp_val;
186 len = offset >> 16;
187 offset &= 0xffff;
188 }
189
190 if (flags & FIELD_IS_ARRAY) {
191
192 type = bt_ctf_event_class_get_field_by_name(
193 event_class, name);
194 array_field = bt_ctf_field_create(type);
195 bt_ctf_field_type_put(type);
196 if (!array_field) {
197 pr_err("Failed to create array type %s\n", name);
198 return -1;
199 }
200
201 len = fmtf->size / fmtf->arraylen;
202 n_items = fmtf->arraylen;
203 } else {
204 n_items = 1;
205 array_field = NULL;
206 }
207
208 type = get_tracepoint_field_type(cw, fmtf);
209
210 for (i = 0; i < n_items; i++) {
211 if (!(flags & FIELD_IS_STRING))
212 value_int = pevent_read_number(
213 fmtf->event->pevent,
214 data + offset + i * len, len);
215
216 if (flags & FIELD_IS_ARRAY)
217 field = bt_ctf_field_array_get_field(array_field, i);
218 else
219 field = bt_ctf_field_create(type);
220
221 if (!field) {
222 pr_err("failed to create a field %s\n", name);
223 return -1;
224 }
225
226 if (flags & FIELD_IS_STRING)
227 ret = bt_ctf_field_string_set_value(field,
228 data + offset + i * len);
229 else if (!(flags & FIELD_IS_SIGNED))
230 ret = bt_ctf_field_unsigned_integer_set_value(
231 field, value_int);
232 else
233 ret = bt_ctf_field_signed_integer_set_value(
234 field, value_int);
235 if (ret) {
236 pr_err("failed to set file value %s\n", name);
237 goto err_put_field;
238 }
239 if (!(flags & FIELD_IS_ARRAY)) {
240 ret = bt_ctf_event_set_payload(event, name, field);
241 if (ret) {
242 pr_err("failed to set payload %s\n", name);
243 goto err_put_field;
244 }
245 }
246 bt_ctf_field_put(field);
247 }
248 if (flags & FIELD_IS_ARRAY) {
249 ret = bt_ctf_event_set_payload(event, name, array_field);
250 if (ret) {
251 pr_err("Failed add payload array %s\n", name);
252 return -1;
253 }
254 bt_ctf_field_put(array_field);
255 }
256 return 0;
257
258err_put_field:
259 bt_ctf_field_put(field);
260 return -1;
261}
262
263static int add_tracepoint_fields_values(struct ctf_writer *cw,
264 struct bt_ctf_event_class *event_class,
265 struct bt_ctf_event *event,
266 struct format_field *fields,
267 struct perf_sample *sample)
268{
269 struct format_field *field;
270 int ret;
271
272 for (field = fields; field; field = field->next) {
273 ret = add_tracepoint_field_value(cw, event_class, event, sample,
274 field);
275 if (ret)
276 return -1;
277 }
278 return 0;
279}
280
281static int add_tracepoint_values(struct ctf_writer *cw,
282 struct bt_ctf_event_class *event_class,
283 struct bt_ctf_event *event,
284 struct perf_evsel *evsel,
285 struct perf_sample *sample)
286{
287 struct format_field *common_fields = evsel->tp_format->format.common_fields;
288 struct format_field *fields = evsel->tp_format->format.fields;
289 int ret;
290
291 ret = add_tracepoint_fields_values(cw, event_class, event,
292 common_fields, sample);
293 if (!ret)
294 ret = add_tracepoint_fields_values(cw, event_class, event,
295 fields, sample);
296
297 return ret;
298}
299
300static int add_generic_values(struct ctf_writer *cw,
301 struct bt_ctf_event *event,
302 struct perf_evsel *evsel,
303 struct perf_sample *sample)
304{
305 u64 type = evsel->attr.sample_type;
306 int ret;
307
308 /*
309 * missing:
310 * PERF_SAMPLE_TIME - not needed as we have it in
311 * ctf event header
312 * PERF_SAMPLE_READ - TODO
313 * PERF_SAMPLE_CALLCHAIN - TODO
314 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
315 * PERF_SAMPLE_BRANCH_STACK - TODO
316 * PERF_SAMPLE_REGS_USER - TODO
317 * PERF_SAMPLE_STACK_USER - TODO
318 */
319
320 if (type & PERF_SAMPLE_IP) {
321 ret = value_set_u64_hex(cw, event, "perf_ip", sample->ip);
322 if (ret)
323 return -1;
324 }
325
326 if (type & PERF_SAMPLE_TID) {
327 ret = value_set_s32(cw, event, "perf_tid", sample->tid);
328 if (ret)
329 return -1;
330
331 ret = value_set_s32(cw, event, "perf_pid", sample->pid);
332 if (ret)
333 return -1;
334 }
335
336 if ((type & PERF_SAMPLE_ID) ||
337 (type & PERF_SAMPLE_IDENTIFIER)) {
338 ret = value_set_u64(cw, event, "perf_id", sample->id);
339 if (ret)
340 return -1;
341 }
342
343 if (type & PERF_SAMPLE_STREAM_ID) {
344 ret = value_set_u64(cw, event, "perf_stream_id", sample->stream_id);
345 if (ret)
346 return -1;
347 }
348
349 if (type & PERF_SAMPLE_CPU) {
350 ret = value_set_u32(cw, event, "perf_cpu", sample->cpu);
351 if (ret)
352 return -1;
353 }
354
355 if (type & PERF_SAMPLE_PERIOD) {
356 ret = value_set_u64(cw, event, "perf_period", sample->period);
357 if (ret)
358 return -1;
359 }
360
361 if (type & PERF_SAMPLE_WEIGHT) {
362 ret = value_set_u64(cw, event, "perf_weight", sample->weight);
363 if (ret)
364 return -1;
365 }
366
367 if (type & PERF_SAMPLE_DATA_SRC) {
368 ret = value_set_u64(cw, event, "perf_data_src",
369 sample->data_src);
370 if (ret)
371 return -1;
372 }
373
374 if (type & PERF_SAMPLE_TRANSACTION) {
375 ret = value_set_u64(cw, event, "perf_transaction",
376 sample->transaction);
377 if (ret)
378 return -1;
379 }
380
381 return 0;
382}
383
384static int process_sample_event(struct perf_tool *tool,
385 union perf_event *_event __maybe_unused,
386 struct perf_sample *sample,
387 struct perf_evsel *evsel,
388 struct machine *machine __maybe_unused)
389{
390 struct convert *c = container_of(tool, struct convert, tool);
391 struct evsel_priv *priv = evsel->priv;
392 struct ctf_writer *cw = &c->writer;
393 struct bt_ctf_event_class *event_class;
394 struct bt_ctf_event *event;
395 int ret;
396
397 if (WARN_ONCE(!priv, "Failed to setup all events.\n"))
398 return 0;
399
400 event_class = priv->event_class;
401
402 /* update stats */
403 c->events_count++;
404 c->events_size += _event->header.size;
405
406 pr_time2(sample->time, "sample %" PRIu64 "\n", c->events_count);
407
408 event = bt_ctf_event_create(event_class);
409 if (!event) {
410 pr_err("Failed to create an CTF event\n");
411 return -1;
412 }
413
414 bt_ctf_clock_set_time(cw->clock, sample->time);
415
416 ret = add_generic_values(cw, event, evsel, sample);
417 if (ret)
418 return -1;
419
420 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
421 ret = add_tracepoint_values(cw, event_class, event,
422 evsel, sample);
423 if (ret)
424 return -1;
425 }
426
427 bt_ctf_stream_append_event(cw->stream, event);
428 bt_ctf_event_put(event);
429 return 0;
430}
431
432static int add_tracepoint_fields_types(struct ctf_writer *cw,
433 struct format_field *fields,
434 struct bt_ctf_event_class *event_class)
435{
436 struct format_field *field;
437 int ret;
438
439 for (field = fields; field; field = field->next) {
440 struct bt_ctf_field_type *type;
441 unsigned long flags = field->flags;
442
443 pr2(" field '%s'\n", field->name);
444
445 type = get_tracepoint_field_type(cw, field);
446 if (!type)
447 return -1;
448
449 /*
450 * A string is an array of chars. For this we use the string
451 * type and don't care that it is an array. What we don't
452 * support is an array of strings.
453 */
454 if (flags & FIELD_IS_STRING)
455 flags &= ~FIELD_IS_ARRAY;
456
457 if (flags & FIELD_IS_ARRAY)
458 type = bt_ctf_field_type_array_create(type, field->arraylen);
459
460 ret = bt_ctf_event_class_add_field(event_class, type,
461 field->name);
462
463 if (flags & FIELD_IS_ARRAY)
464 bt_ctf_field_type_put(type);
465
466 if (ret) {
467 pr_err("Failed to add field '%s\n", field->name);
468 return -1;
469 }
470 }
471
472 return 0;
473}
474
475static int add_tracepoint_types(struct ctf_writer *cw,
476 struct perf_evsel *evsel,
477 struct bt_ctf_event_class *class)
478{
479 struct format_field *common_fields = evsel->tp_format->format.common_fields;
480 struct format_field *fields = evsel->tp_format->format.fields;
481 int ret;
482
483 ret = add_tracepoint_fields_types(cw, common_fields, class);
484 if (!ret)
485 ret = add_tracepoint_fields_types(cw, fields, class);
486
487 return ret;
488}
489
490static int add_generic_types(struct ctf_writer *cw, struct perf_evsel *evsel,
491 struct bt_ctf_event_class *event_class)
492{
493 u64 type = evsel->attr.sample_type;
494
495 /*
496 * missing:
497 * PERF_SAMPLE_TIME - not needed as we have it in
498 * ctf event header
499 * PERF_SAMPLE_READ - TODO
500 * PERF_SAMPLE_CALLCHAIN - TODO
501 * PERF_SAMPLE_RAW - tracepoint fields are handled separately
502 * PERF_SAMPLE_BRANCH_STACK - TODO
503 * PERF_SAMPLE_REGS_USER - TODO
504 * PERF_SAMPLE_STACK_USER - TODO
505 */
506
507#define ADD_FIELD(cl, t, n) \
508 do { \
509 pr2(" field '%s'\n", n); \
510 if (bt_ctf_event_class_add_field(cl, t, n)) { \
511 pr_err("Failed to add field '%s;\n", n); \
512 return -1; \
513 } \
514 } while (0)
515
516 if (type & PERF_SAMPLE_IP)
517 ADD_FIELD(event_class, cw->data.u64_hex, "perf_ip");
518
519 if (type & PERF_SAMPLE_TID) {
520 ADD_FIELD(event_class, cw->data.s32, "perf_tid");
521 ADD_FIELD(event_class, cw->data.s32, "perf_pid");
522 }
523
524 if ((type & PERF_SAMPLE_ID) ||
525 (type & PERF_SAMPLE_IDENTIFIER))
526 ADD_FIELD(event_class, cw->data.u64, "perf_id");
527
528 if (type & PERF_SAMPLE_STREAM_ID)
529 ADD_FIELD(event_class, cw->data.u64, "perf_stream_id");
530
531 if (type & PERF_SAMPLE_CPU)
532 ADD_FIELD(event_class, cw->data.u32, "perf_cpu");
533
534 if (type & PERF_SAMPLE_PERIOD)
535 ADD_FIELD(event_class, cw->data.u64, "perf_period");
536
537 if (type & PERF_SAMPLE_WEIGHT)
538 ADD_FIELD(event_class, cw->data.u64, "perf_weight");
539
540 if (type & PERF_SAMPLE_DATA_SRC)
541 ADD_FIELD(event_class, cw->data.u64, "perf_data_src");
542
543 if (type & PERF_SAMPLE_TRANSACTION)
544 ADD_FIELD(event_class, cw->data.u64, "perf_transaction");
545
546#undef ADD_FIELD
547 return 0;
548}
549
550static int add_event(struct ctf_writer *cw, struct perf_evsel *evsel)
551{
552 struct bt_ctf_event_class *event_class;
553 struct evsel_priv *priv;
554 const char *name = perf_evsel__name(evsel);
555 int ret;
556
557 pr("Adding event '%s' (type %d)\n", name, evsel->attr.type);
558
559 event_class = bt_ctf_event_class_create(name);
560 if (!event_class)
561 return -1;
562
563 ret = add_generic_types(cw, evsel, event_class);
564 if (ret)
565 goto err;
566
567 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
568 ret = add_tracepoint_types(cw, evsel, event_class);
569 if (ret)
570 goto err;
571 }
572
573 ret = bt_ctf_stream_class_add_event_class(cw->stream_class, event_class);
574 if (ret) {
575 pr("Failed to add event class into stream.\n");
576 goto err;
577 }
578
579 priv = malloc(sizeof(*priv));
580 if (!priv)
581 goto err;
582
583 priv->event_class = event_class;
584 evsel->priv = priv;
585 return 0;
586
587err:
588 bt_ctf_event_class_put(event_class);
589 pr_err("Failed to add event '%s'.\n", name);
590 return -1;
591}
592
593static int setup_events(struct ctf_writer *cw, struct perf_session *session)
594{
595 struct perf_evlist *evlist = session->evlist;
596 struct perf_evsel *evsel;
597 int ret;
598
599 evlist__for_each(evlist, evsel) {
600 ret = add_event(cw, evsel);
601 if (ret)
602 return ret;
603 }
604 return 0;
605}
606
607static int ctf_writer__setup_env(struct ctf_writer *cw,
608 struct perf_session *session)
609{
610 struct perf_header *header = &session->header;
611 struct bt_ctf_writer *writer = cw->writer;
612
613#define ADD(__n, __v) \
614do { \
615 if (bt_ctf_writer_add_environment_field(writer, __n, __v)) \
616 return -1; \
617} while (0)
618
619 ADD("host", header->env.hostname);
620 ADD("sysname", "Linux");
621 ADD("release", header->env.os_release);
622 ADD("version", header->env.version);
623 ADD("machine", header->env.arch);
624 ADD("domain", "kernel");
625 ADD("tracer_name", "perf");
626
627#undef ADD
628 return 0;
629}
630
631static int ctf_writer__setup_clock(struct ctf_writer *cw)
632{
633 struct bt_ctf_clock *clock = cw->clock;
634
635 bt_ctf_clock_set_description(clock, "perf clock");
636
637#define SET(__n, __v) \
638do { \
639 if (bt_ctf_clock_set_##__n(clock, __v)) \
640 return -1; \
641} while (0)
642
643 SET(frequency, 1000000000);
644 SET(offset_s, 0);
645 SET(offset, 0);
646 SET(precision, 10);
647 SET(is_absolute, 0);
648
649#undef SET
650 return 0;
651}
652
653static struct bt_ctf_field_type *create_int_type(int size, bool sign, bool hex)
654{
655 struct bt_ctf_field_type *type;
656
657 type = bt_ctf_field_type_integer_create(size);
658 if (!type)
659 return NULL;
660
661 if (sign &&
662 bt_ctf_field_type_integer_set_signed(type, 1))
663 goto err;
664
665 if (hex &&
666 bt_ctf_field_type_integer_set_base(type, BT_CTF_INTEGER_BASE_HEXADECIMAL))
667 goto err;
668
669 pr2("Created type: INTEGER %d-bit %ssigned %s\n",
670 size, sign ? "un" : "", hex ? "hex" : "");
671 return type;
672
673err:
674 bt_ctf_field_type_put(type);
675 return NULL;
676}
677
678static void ctf_writer__cleanup_data(struct ctf_writer *cw)
679{
680 unsigned int i;
681
682 for (i = 0; i < ARRAY_SIZE(cw->data.array); i++)
683 bt_ctf_field_type_put(cw->data.array[i]);
684}
685
686static int ctf_writer__init_data(struct ctf_writer *cw)
687{
688#define CREATE_INT_TYPE(type, size, sign, hex) \
689do { \
690 (type) = create_int_type(size, sign, hex); \
691 if (!(type)) \
692 goto err; \
693} while (0)
694
695 CREATE_INT_TYPE(cw->data.s64, 64, true, false);
696 CREATE_INT_TYPE(cw->data.u64, 64, false, false);
697 CREATE_INT_TYPE(cw->data.s32, 32, true, false);
698 CREATE_INT_TYPE(cw->data.u32, 32, false, false);
699 CREATE_INT_TYPE(cw->data.u64_hex, 64, false, true);
700
701 cw->data.string = bt_ctf_field_type_string_create();
702 if (cw->data.string)
703 return 0;
704
705err:
706 ctf_writer__cleanup_data(cw);
707 pr_err("Failed to create data types.\n");
708 return -1;
709}
710
711static void ctf_writer__cleanup(struct ctf_writer *cw)
712{
713 ctf_writer__cleanup_data(cw);
714
715 bt_ctf_clock_put(cw->clock);
716 bt_ctf_stream_put(cw->stream);
717 bt_ctf_stream_class_put(cw->stream_class);
718 bt_ctf_writer_put(cw->writer);
719
720 /* and NULL all the pointers */
721 memset(cw, 0, sizeof(*cw));
722}
723
724static int ctf_writer__init(struct ctf_writer *cw, const char *path)
725{
726 struct bt_ctf_writer *writer;
727 struct bt_ctf_stream_class *stream_class;
728 struct bt_ctf_stream *stream;
729 struct bt_ctf_clock *clock;
730
731 /* CTF writer */
732 writer = bt_ctf_writer_create(path);
733 if (!writer)
734 goto err;
735
736 cw->writer = writer;
737
738 /* CTF clock */
739 clock = bt_ctf_clock_create("perf_clock");
740 if (!clock) {
741 pr("Failed to create CTF clock.\n");
742 goto err_cleanup;
743 }
744
745 cw->clock = clock;
746
747 if (ctf_writer__setup_clock(cw)) {
748 pr("Failed to setup CTF clock.\n");
749 goto err_cleanup;
750 }
751
752 /* CTF stream class */
753 stream_class = bt_ctf_stream_class_create("perf_stream");
754 if (!stream_class) {
755 pr("Failed to create CTF stream class.\n");
756 goto err_cleanup;
757 }
758
759 cw->stream_class = stream_class;
760
761 /* CTF clock stream setup */
762 if (bt_ctf_stream_class_set_clock(stream_class, clock)) {
763 pr("Failed to assign CTF clock to stream class.\n");
764 goto err_cleanup;
765 }
766
767 if (ctf_writer__init_data(cw))
768 goto err_cleanup;
769
770 /* CTF stream instance */
771 stream = bt_ctf_writer_create_stream(writer, stream_class);
772 if (!stream) {
773 pr("Failed to create CTF stream.\n");
774 goto err_cleanup;
775 }
776
777 cw->stream = stream;
778
779 /* CTF clock writer setup */
780 if (bt_ctf_writer_add_clock(writer, clock)) {
781 pr("Failed to assign CTF clock to writer.\n");
782 goto err_cleanup;
783 }
784
785 return 0;
786
787err_cleanup:
788 ctf_writer__cleanup(cw);
789err:
790 pr_err("Failed to setup CTF writer.\n");
791 return -1;
792}
793
794int bt_convert__perf2ctf(const char *input, const char *path, bool force)
795{
796 struct perf_session *session;
797 struct perf_data_file file = {
798 .path = input,
799 .mode = PERF_DATA_MODE_READ,
800 .force = force,
801 };
802 struct convert c = {
803 .tool = {
804 .sample = process_sample_event,
805 .mmap = perf_event__process_mmap,
806 .mmap2 = perf_event__process_mmap2,
807 .comm = perf_event__process_comm,
808 .exit = perf_event__process_exit,
809 .fork = perf_event__process_fork,
810 .lost = perf_event__process_lost,
811 .tracing_data = perf_event__process_tracing_data,
812 .build_id = perf_event__process_build_id,
813 .ordered_events = true,
814 .ordering_requires_timestamps = true,
815 },
816 };
817 struct ctf_writer *cw = &c.writer;
818 int err = -1;
819
820 /* CTF writer */
821 if (ctf_writer__init(cw, path))
822 return -1;
823
824 /* perf.data session */
825 session = perf_session__new(&file, 0, &c.tool);
826 if (!session)
827 goto free_writer;
828
829 /* CTF writer env/clock setup */
830 if (ctf_writer__setup_env(cw, session))
831 goto free_session;
832
833 /* CTF events setup */
834 if (setup_events(cw, session))
835 goto free_session;
836
837 err = perf_session__process_events(session);
838 if (!err)
839 err = bt_ctf_stream_flush(cw->stream);
840
841 fprintf(stderr,
842 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n",
843 file.path, path);
844
845 fprintf(stderr,
846 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples) ]\n",
847 (double) c.events_size / 1024.0 / 1024.0,
848 c.events_count);
849
850 /* its all good */
851free_session:
852 perf_session__delete(session);
853
854free_writer:
855 ctf_writer__cleanup(cw);
856 return err;
857}
diff --git a/tools/perf/util/data-convert-bt.h b/tools/perf/util/data-convert-bt.h
new file mode 100644
index 000000000000..4c204342a9d8
--- /dev/null
+++ b/tools/perf/util/data-convert-bt.h
@@ -0,0 +1,8 @@
1#ifndef __DATA_CONVERT_BT_H
2#define __DATA_CONVERT_BT_H
3#ifdef HAVE_LIBBABELTRACE_SUPPORT
4
5int bt_convert__perf2ctf(const char *input_name, const char *to_ctf, bool force);
6
7#endif /* HAVE_LIBBABELTRACE_SUPPORT */
8#endif /* __DATA_CONVERT_BT_H */
diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
index c81dae399763..bb39a3ffc70b 100644
--- a/tools/perf/util/db-export.c
+++ b/tools/perf/util/db-export.c
@@ -282,13 +282,13 @@ int db_export__branch_type(struct db_export *dbe, u32 branch_type,
282 282
283int db_export__sample(struct db_export *dbe, union perf_event *event, 283int db_export__sample(struct db_export *dbe, union perf_event *event,
284 struct perf_sample *sample, struct perf_evsel *evsel, 284 struct perf_sample *sample, struct perf_evsel *evsel,
285 struct thread *thread, struct addr_location *al) 285 struct addr_location *al)
286{ 286{
287 struct thread* thread = al->thread;
287 struct export_sample es = { 288 struct export_sample es = {
288 .event = event, 289 .event = event,
289 .sample = sample, 290 .sample = sample,
290 .evsel = evsel, 291 .evsel = evsel,
291 .thread = thread,
292 .al = al, 292 .al = al,
293 }; 293 };
294 struct thread *main_thread; 294 struct thread *main_thread;
diff --git a/tools/perf/util/db-export.h b/tools/perf/util/db-export.h
index adbd22d66798..25e22fd76aca 100644
--- a/tools/perf/util/db-export.h
+++ b/tools/perf/util/db-export.h
@@ -34,7 +34,6 @@ struct export_sample {
34 union perf_event *event; 34 union perf_event *event;
35 struct perf_sample *sample; 35 struct perf_sample *sample;
36 struct perf_evsel *evsel; 36 struct perf_evsel *evsel;
37 struct thread *thread;
38 struct addr_location *al; 37 struct addr_location *al;
39 u64 db_id; 38 u64 db_id;
40 u64 comm_db_id; 39 u64 comm_db_id;
@@ -97,7 +96,7 @@ int db_export__branch_type(struct db_export *dbe, u32 branch_type,
97 const char *name); 96 const char *name);
98int db_export__sample(struct db_export *dbe, union perf_event *event, 97int db_export__sample(struct db_export *dbe, union perf_event *event,
99 struct perf_sample *sample, struct perf_evsel *evsel, 98 struct perf_sample *sample, struct perf_evsel *evsel,
100 struct thread *thread, struct addr_location *al); 99 struct addr_location *al);
101 100
102int db_export__branch_types(struct db_export *dbe); 101int db_export__branch_types(struct db_export *dbe);
103 102
diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c
index ad60b2f20258..2da5581ec74d 100644
--- a/tools/perf/util/debug.c
+++ b/tools/perf/util/debug.c
@@ -20,6 +20,7 @@ int verbose;
20bool dump_trace = false, quiet = false; 20bool dump_trace = false, quiet = false;
21int debug_ordered_events; 21int debug_ordered_events;
22static int redirect_to_stderr; 22static int redirect_to_stderr;
23int debug_data_convert;
23 24
24static int _eprintf(int level, int var, const char *fmt, va_list args) 25static int _eprintf(int level, int var, const char *fmt, va_list args)
25{ 26{
@@ -147,6 +148,7 @@ static struct debug_variable {
147 { .name = "verbose", .ptr = &verbose }, 148 { .name = "verbose", .ptr = &verbose },
148 { .name = "ordered-events", .ptr = &debug_ordered_events}, 149 { .name = "ordered-events", .ptr = &debug_ordered_events},
149 { .name = "stderr", .ptr = &redirect_to_stderr}, 150 { .name = "stderr", .ptr = &redirect_to_stderr},
151 { .name = "data-convert", .ptr = &debug_data_convert },
150 { .name = NULL, } 152 { .name = NULL, }
151}; 153};
152 154
diff --git a/tools/perf/util/debug.h b/tools/perf/util/debug.h
index be264d6f3b30..caac2fdc6105 100644
--- a/tools/perf/util/debug.h
+++ b/tools/perf/util/debug.h
@@ -12,6 +12,7 @@
12extern int verbose; 12extern int verbose;
13extern bool quiet, dump_trace; 13extern bool quiet, dump_trace;
14extern int debug_ordered_events; 14extern int debug_ordered_events;
15extern int debug_data_convert;
15 16
16#ifndef pr_fmt 17#ifndef pr_fmt
17#define pr_fmt(fmt) fmt 18#define pr_fmt(fmt) fmt
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index c2f7d3b90966..fc0ddd5792a9 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -45,13 +45,13 @@ int dso__read_binary_type_filename(const struct dso *dso,
45 case DSO_BINARY_TYPE__DEBUGLINK: { 45 case DSO_BINARY_TYPE__DEBUGLINK: {
46 char *debuglink; 46 char *debuglink;
47 47
48 strncpy(filename, dso->long_name, size); 48 len = __symbol__join_symfs(filename, size, dso->long_name);
49 debuglink = filename + dso->long_name_len; 49 debuglink = filename + len;
50 while (debuglink != filename && *debuglink != '/') 50 while (debuglink != filename && *debuglink != '/')
51 debuglink--; 51 debuglink--;
52 if (*debuglink == '/') 52 if (*debuglink == '/')
53 debuglink++; 53 debuglink++;
54 ret = filename__read_debuglink(dso->long_name, debuglink, 54 ret = filename__read_debuglink(filename, debuglink,
55 size - (debuglink - filename)); 55 size - (debuglink - filename));
56 } 56 }
57 break; 57 break;
@@ -148,6 +148,9 @@ static const struct {
148#ifdef HAVE_ZLIB_SUPPORT 148#ifdef HAVE_ZLIB_SUPPORT
149 { "gz", gzip_decompress_to_file }, 149 { "gz", gzip_decompress_to_file },
150#endif 150#endif
151#ifdef HAVE_LZMA_SUPPORT
152 { "xz", lzma_decompress_to_file },
153#endif
151 { NULL, NULL }, 154 { NULL, NULL },
152}; 155};
153 156
@@ -162,32 +165,14 @@ bool is_supported_compression(const char *ext)
162 return false; 165 return false;
163} 166}
164 167
165bool is_kmodule_extension(const char *ext) 168bool is_kernel_module(const char *pathname)
166{
167 if (strncmp(ext, "ko", 2))
168 return false;
169
170 if (ext[2] == '\0' || (ext[2] == '.' && is_supported_compression(ext+3)))
171 return true;
172
173 return false;
174}
175
176bool is_kernel_module(const char *pathname, bool *compressed)
177{ 169{
178 const char *ext = strrchr(pathname, '.'); 170 struct kmod_path m;
179 171
180 if (ext == NULL) 172 if (kmod_path__parse(&m, pathname))
181 return false; 173 return NULL;
182
183 if (is_supported_compression(ext + 1)) {
184 if (compressed)
185 *compressed = true;
186 ext -= 3;
187 } else if (compressed)
188 *compressed = false;
189 174
190 return is_kmodule_extension(ext + 1); 175 return m.kmod;
191} 176}
192 177
193bool decompress_to_file(const char *ext, const char *filename, int output_fd) 178bool decompress_to_file(const char *ext, const char *filename, int output_fd)
@@ -209,6 +194,72 @@ bool dso__needs_decompress(struct dso *dso)
209} 194}
210 195
211/* 196/*
197 * Parses kernel module specified in @path and updates
198 * @m argument like:
199 *
200 * @comp - true if @path contains supported compression suffix,
201 * false otherwise
202 * @kmod - true if @path contains '.ko' suffix in right position,
203 * false otherwise
204 * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
205 * of the kernel module without suffixes, otherwise strudup-ed
206 * base name of @path
207 * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
208 * the compression suffix
209 *
210 * Returns 0 if there's no strdup error, -ENOMEM otherwise.
211 */
212int __kmod_path__parse(struct kmod_path *m, const char *path,
213 bool alloc_name, bool alloc_ext)
214{
215 const char *name = strrchr(path, '/');
216 const char *ext = strrchr(path, '.');
217
218 memset(m, 0x0, sizeof(*m));
219 name = name ? name + 1 : path;
220
221 /* No extension, just return name. */
222 if (ext == NULL) {
223 if (alloc_name) {
224 m->name = strdup(name);
225 return m->name ? 0 : -ENOMEM;
226 }
227 return 0;
228 }
229
230 if (is_supported_compression(ext + 1)) {
231 m->comp = true;
232 ext -= 3;
233 }
234
235 /* Check .ko extension only if there's enough name left. */
236 if (ext > name)
237 m->kmod = !strncmp(ext, ".ko", 3);
238
239 if (alloc_name) {
240 if (m->kmod) {
241 if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
242 return -ENOMEM;
243 } else {
244 if (asprintf(&m->name, "%s", name) == -1)
245 return -ENOMEM;
246 }
247
248 strxfrchar(m->name, '-', '_');
249 }
250
251 if (alloc_ext && m->comp) {
252 m->ext = strdup(ext + 4);
253 if (!m->ext) {
254 free((void *) m->name);
255 return -ENOMEM;
256 }
257 }
258
259 return 0;
260}
261
262/*
212 * Global list of open DSOs and the counter. 263 * Global list of open DSOs and the counter.
213 */ 264 */
214static LIST_HEAD(dso__data_open); 265static LIST_HEAD(dso__data_open);
@@ -240,7 +291,7 @@ static int do_open(char *name)
240 if (fd >= 0) 291 if (fd >= 0)
241 return fd; 292 return fd;
242 293
243 pr_debug("dso open failed, mmap: %s\n", 294 pr_debug("dso open failed: %s\n",
244 strerror_r(errno, sbuf, sizeof(sbuf))); 295 strerror_r(errno, sbuf, sizeof(sbuf)));
245 if (!dso__data_open_cnt || errno != EMFILE) 296 if (!dso__data_open_cnt || errno != EMFILE)
246 break; 297 break;
@@ -1002,21 +1053,24 @@ struct dso *dsos__find(const struct dsos *dsos, const char *name,
1002 return dso__find_by_longname(&dsos->root, name); 1053 return dso__find_by_longname(&dsos->root, name);
1003} 1054}
1004 1055
1005struct dso *__dsos__findnew(struct dsos *dsos, const char *name) 1056struct dso *dsos__addnew(struct dsos *dsos, const char *name)
1006{ 1057{
1007 struct dso *dso = dsos__find(dsos, name, false); 1058 struct dso *dso = dso__new(name);
1008 1059
1009 if (!dso) { 1060 if (dso != NULL) {
1010 dso = dso__new(name); 1061 dsos__add(dsos, dso);
1011 if (dso != NULL) { 1062 dso__set_basename(dso);
1012 dsos__add(dsos, dso);
1013 dso__set_basename(dso);
1014 }
1015 } 1063 }
1016
1017 return dso; 1064 return dso;
1018} 1065}
1019 1066
1067struct dso *__dsos__findnew(struct dsos *dsos, const char *name)
1068{
1069 struct dso *dso = dsos__find(dsos, name, false);
1070
1071 return dso ? dso : dsos__addnew(dsos, name);
1072}
1073
1020size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, 1074size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1021 bool (skip)(struct dso *dso, int parm), int parm) 1075 bool (skip)(struct dso *dso, int parm), int parm)
1022{ 1076{
@@ -1083,3 +1137,36 @@ enum dso_type dso__type(struct dso *dso, struct machine *machine)
1083 1137
1084 return dso__type_fd(fd); 1138 return dso__type_fd(fd);
1085} 1139}
1140
1141int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1142{
1143 int idx, errnum = dso->load_errno;
1144 /*
1145 * This must have a same ordering as the enum dso_load_errno.
1146 */
1147 static const char *dso_load__error_str[] = {
1148 "Internal tools/perf/ library error",
1149 "Invalid ELF file",
1150 "Can not read build id",
1151 "Mismatching build id",
1152 "Decompression failure",
1153 };
1154
1155 BUG_ON(buflen == 0);
1156
1157 if (errnum >= 0) {
1158 const char *err = strerror_r(errnum, buf, buflen);
1159
1160 if (err != buf)
1161 scnprintf(buf, buflen, "%s", err);
1162
1163 return 0;
1164 }
1165
1166 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1167 return -1;
1168
1169 idx = errnum - __DSO_LOAD_ERRNO__START;
1170 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1171 return 0;
1172}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index ced92841ff97..e0901b4ed8de 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -60,6 +60,31 @@ enum dso_type {
60 DSO__TYPE_X32BIT, 60 DSO__TYPE_X32BIT,
61}; 61};
62 62
63enum dso_load_errno {
64 DSO_LOAD_ERRNO__SUCCESS = 0,
65
66 /*
67 * Choose an arbitrary negative big number not to clash with standard
68 * errno since SUS requires the errno has distinct positive values.
69 * See 'Issue 6' in the link below.
70 *
71 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
72 */
73 __DSO_LOAD_ERRNO__START = -10000,
74
75 DSO_LOAD_ERRNO__INTERNAL_ERROR = __DSO_LOAD_ERRNO__START,
76
77 /* for symsrc__init() */
78 DSO_LOAD_ERRNO__INVALID_ELF,
79 DSO_LOAD_ERRNO__CANNOT_READ_BUILDID,
80 DSO_LOAD_ERRNO__MISMATCHING_BUILDID,
81
82 /* for decompress_kmodule */
83 DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE,
84
85 __DSO_LOAD_ERRNO__END,
86};
87
63#define DSO__SWAP(dso, type, val) \ 88#define DSO__SWAP(dso, type, val) \
64({ \ 89({ \
65 type ____r = val; \ 90 type ____r = val; \
@@ -113,6 +138,7 @@ struct dso {
113 enum dso_swap_type needs_swap; 138 enum dso_swap_type needs_swap;
114 enum dso_binary_type symtab_type; 139 enum dso_binary_type symtab_type;
115 enum dso_binary_type binary_type; 140 enum dso_binary_type binary_type;
141 enum dso_load_errno load_errno;
116 u8 adjust_symbols:1; 142 u8 adjust_symbols:1;
117 u8 has_build_id:1; 143 u8 has_build_id:1;
118 u8 has_srcline:1; 144 u8 has_srcline:1;
@@ -139,7 +165,8 @@ struct dso {
139 u32 status_seen; 165 u32 status_seen;
140 size_t file_size; 166 size_t file_size;
141 struct list_head open_entry; 167 struct list_head open_entry;
142 u64 frame_offset; 168 u64 debug_frame_offset;
169 u64 eh_frame_hdr_offset;
143 } data; 170 } data;
144 171
145 union { /* Tool specific area */ 172 union { /* Tool specific area */
@@ -189,11 +216,24 @@ char dso__symtab_origin(const struct dso *dso);
189int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type, 216int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
190 char *root_dir, char *filename, size_t size); 217 char *root_dir, char *filename, size_t size);
191bool is_supported_compression(const char *ext); 218bool is_supported_compression(const char *ext);
192bool is_kmodule_extension(const char *ext); 219bool is_kernel_module(const char *pathname);
193bool is_kernel_module(const char *pathname, bool *compressed);
194bool decompress_to_file(const char *ext, const char *filename, int output_fd); 220bool decompress_to_file(const char *ext, const char *filename, int output_fd);
195bool dso__needs_decompress(struct dso *dso); 221bool dso__needs_decompress(struct dso *dso);
196 222
223struct kmod_path {
224 char *name;
225 char *ext;
226 bool comp;
227 bool kmod;
228};
229
230int __kmod_path__parse(struct kmod_path *m, const char *path,
231 bool alloc_name, bool alloc_ext);
232
233#define kmod_path__parse(__m, __p) __kmod_path__parse(__m, __p, false, false)
234#define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true , false)
235#define kmod_path__parse_ext(__m, __p) __kmod_path__parse(__m, __p, false, true)
236
197/* 237/*
198 * The dso__data_* external interface provides following functions: 238 * The dso__data_* external interface provides following functions:
199 * dso__data_fd 239 * dso__data_fd
@@ -249,6 +289,7 @@ struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
249 const char *short_name, int dso_type); 289 const char *short_name, int dso_type);
250 290
251void dsos__add(struct dsos *dsos, struct dso *dso); 291void dsos__add(struct dsos *dsos, struct dso *dso);
292struct dso *dsos__addnew(struct dsos *dsos, const char *name);
252struct dso *dsos__find(const struct dsos *dsos, const char *name, 293struct dso *dsos__find(const struct dsos *dsos, const char *name,
253 bool cmp_short); 294 bool cmp_short);
254struct dso *__dsos__findnew(struct dsos *dsos, const char *name); 295struct dso *__dsos__findnew(struct dsos *dsos, const char *name);
@@ -279,4 +320,6 @@ void dso__free_a2l(struct dso *dso);
279 320
280enum dso_type dso__type(struct dso *dso, struct machine *machine); 321enum dso_type dso__type(struct dso *dso, struct machine *machine);
281 322
323int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
324
282#endif /* __PERF_DSO */ 325#endif /* __PERF_DSO */
diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
index cc66c4049e09..c34e024020c7 100644
--- a/tools/perf/util/dwarf-aux.c
+++ b/tools/perf/util/dwarf-aux.c
@@ -278,6 +278,21 @@ bool die_is_func_def(Dwarf_Die *dw_die)
278} 278}
279 279
280/** 280/**
281 * die_is_func_instance - Ensure that this DIE is an instance of a subprogram
282 * @dw_die: a DIE
283 *
284 * Ensure that this DIE is an instance (which has an entry address).
285 * This returns true if @dw_die is a function instance. If not, you need to
286 * call die_walk_instances() to find actual instances.
287 **/
288bool die_is_func_instance(Dwarf_Die *dw_die)
289{
290 Dwarf_Addr tmp;
291
292 /* Actually gcc optimizes non-inline as like as inlined */
293 return !dwarf_func_inline(dw_die) && dwarf_entrypc(dw_die, &tmp) == 0;
294}
295/**
281 * die_get_data_member_location - Get the data-member offset 296 * die_get_data_member_location - Get the data-member offset
282 * @mb_die: a DIE of a member of a data structure 297 * @mb_die: a DIE of a member of a data structure
283 * @offs: The offset of the member in the data structure 298 * @offs: The offset of the member in the data structure
@@ -786,10 +801,16 @@ static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
786{ 801{
787 const char *name = data; 802 const char *name = data;
788 803
789 if ((dwarf_tag(die_mem) == DW_TAG_member) && 804 if (dwarf_tag(die_mem) == DW_TAG_member) {
790 die_compare_name(die_mem, name)) 805 if (die_compare_name(die_mem, name))
791 return DIE_FIND_CB_END; 806 return DIE_FIND_CB_END;
792 807 else if (!dwarf_diename(die_mem)) { /* Unnamed structure */
808 Dwarf_Die type_die, tmp_die;
809 if (die_get_type(die_mem, &type_die) &&
810 die_find_member(&type_die, name, &tmp_die))
811 return DIE_FIND_CB_END;
812 }
813 }
793 return DIE_FIND_CB_SIBLING; 814 return DIE_FIND_CB_SIBLING;
794} 815}
795 816
diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h
index b4fe90c6cb2d..af7dbcd5f929 100644
--- a/tools/perf/util/dwarf-aux.h
+++ b/tools/perf/util/dwarf-aux.h
@@ -41,6 +41,9 @@ extern int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
41/* Ensure that this DIE is a subprogram and definition (not declaration) */ 41/* Ensure that this DIE is a subprogram and definition (not declaration) */
42extern bool die_is_func_def(Dwarf_Die *dw_die); 42extern bool die_is_func_def(Dwarf_Die *dw_die);
43 43
44/* Ensure that this DIE is an instance of a subprogram */
45extern bool die_is_func_instance(Dwarf_Die *dw_die);
46
44/* Compare diename and tname */ 47/* Compare diename and tname */
45extern bool die_compare_name(Dwarf_Die *dw_die, const char *tname); 48extern bool die_compare_name(Dwarf_Die *dw_die, const char *tname);
46 49
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 6c6d044e959a..ff866c4d2e2f 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -49,72 +49,103 @@ static struct perf_sample synth_sample = {
49 .period = 1, 49 .period = 1,
50}; 50};
51 51
52static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len) 52/*
53 * Assumes that the first 4095 bytes of /proc/pid/stat contains
54 * the comm, tgid and ppid.
55 */
56static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
57 pid_t *tgid, pid_t *ppid)
53{ 58{
54 char filename[PATH_MAX]; 59 char filename[PATH_MAX];
55 char bf[BUFSIZ]; 60 char bf[4096];
56 FILE *fp; 61 int fd;
57 size_t size = 0; 62 size_t size = 0, n;
58 pid_t tgid = -1; 63 char *nl, *name, *tgids, *ppids;
64
65 *tgid = -1;
66 *ppid = -1;
59 67
60 snprintf(filename, sizeof(filename), "/proc/%d/status", pid); 68 snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
61 69
62 fp = fopen(filename, "r"); 70 fd = open(filename, O_RDONLY);
63 if (fp == NULL) { 71 if (fd < 0) {
64 pr_debug("couldn't open %s\n", filename); 72 pr_debug("couldn't open %s\n", filename);
65 return 0; 73 return -1;
66 } 74 }
67 75
68 while (!comm[0] || (tgid < 0)) { 76 n = read(fd, bf, sizeof(bf) - 1);
69 if (fgets(bf, sizeof(bf), fp) == NULL) { 77 close(fd);
70 pr_warning("couldn't get COMM and pgid, malformed %s\n", 78 if (n <= 0) {
71 filename); 79 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
72 break; 80 pid);
73 } 81 return -1;
82 }
83 bf[n] = '\0';
74 84
75 if (memcmp(bf, "Name:", 5) == 0) { 85 name = strstr(bf, "Name:");
76 char *name = bf + 5; 86 tgids = strstr(bf, "Tgid:");
77 while (*name && isspace(*name)) 87 ppids = strstr(bf, "PPid:");
78 ++name; 88
79 size = strlen(name) - 1; 89 if (name) {
80 if (size >= len) 90 name += 5; /* strlen("Name:") */
81 size = len - 1; 91
82 memcpy(comm, name, size); 92 while (*name && isspace(*name))
83 comm[size] = '\0'; 93 ++name;
84 94
85 } else if (memcmp(bf, "Tgid:", 5) == 0) { 95 nl = strchr(name, '\n');
86 char *tgids = bf + 5; 96 if (nl)
87 while (*tgids && isspace(*tgids)) 97 *nl = '\0';
88 ++tgids; 98
89 tgid = atoi(tgids); 99 size = strlen(name);
90 } 100 if (size >= len)
101 size = len - 1;
102 memcpy(comm, name, size);
103 comm[size] = '\0';
104 } else {
105 pr_debug("Name: string not found for pid %d\n", pid);
91 } 106 }
92 107
93 fclose(fp); 108 if (tgids) {
109 tgids += 5; /* strlen("Tgid:") */
110 *tgid = atoi(tgids);
111 } else {
112 pr_debug("Tgid: string not found for pid %d\n", pid);
113 }
94 114
95 return tgid; 115 if (ppids) {
116 ppids += 5; /* strlen("PPid:") */
117 *ppid = atoi(ppids);
118 } else {
119 pr_debug("PPid: string not found for pid %d\n", pid);
120 }
121
122 return 0;
96} 123}
97 124
98static pid_t perf_event__synthesize_comm(struct perf_tool *tool, 125static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
99 union perf_event *event, pid_t pid, 126 struct machine *machine,
100 perf_event__handler_t process, 127 pid_t *tgid, pid_t *ppid)
101 struct machine *machine)
102{ 128{
103 size_t size; 129 size_t size;
104 pid_t tgid; 130
131 *ppid = -1;
105 132
106 memset(&event->comm, 0, sizeof(event->comm)); 133 memset(&event->comm, 0, sizeof(event->comm));
107 134
108 if (machine__is_host(machine)) 135 if (machine__is_host(machine)) {
109 tgid = perf_event__get_comm_tgid(pid, event->comm.comm, 136 if (perf_event__get_comm_ids(pid, event->comm.comm,
110 sizeof(event->comm.comm)); 137 sizeof(event->comm.comm),
111 else 138 tgid, ppid) != 0) {
112 tgid = machine->pid; 139 return -1;
140 }
141 } else {
142 *tgid = machine->pid;
143 }
113 144
114 if (tgid < 0) 145 if (*tgid < 0)
115 goto out; 146 return -1;
116 147
117 event->comm.pid = tgid; 148 event->comm.pid = *tgid;
118 event->comm.header.type = PERF_RECORD_COMM; 149 event->comm.header.type = PERF_RECORD_COMM;
119 150
120 size = strlen(event->comm.comm) + 1; 151 size = strlen(event->comm.comm) + 1;
@@ -125,23 +156,45 @@ static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
125 machine->id_hdr_size); 156 machine->id_hdr_size);
126 event->comm.tid = pid; 157 event->comm.tid = pid;
127 158
159 return 0;
160}
161
162static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
163 union perf_event *event, pid_t pid,
164 perf_event__handler_t process,
165 struct machine *machine)
166{
167 pid_t tgid, ppid;
168
169 if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
170 return -1;
171
128 if (process(tool, event, &synth_sample, machine) != 0) 172 if (process(tool, event, &synth_sample, machine) != 0)
129 return -1; 173 return -1;
130 174
131out:
132 return tgid; 175 return tgid;
133} 176}
134 177
135static int perf_event__synthesize_fork(struct perf_tool *tool, 178static int perf_event__synthesize_fork(struct perf_tool *tool,
136 union perf_event *event, pid_t pid, 179 union perf_event *event,
137 pid_t tgid, perf_event__handler_t process, 180 pid_t pid, pid_t tgid, pid_t ppid,
181 perf_event__handler_t process,
138 struct machine *machine) 182 struct machine *machine)
139{ 183{
140 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); 184 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
141 185
142 /* this is really a clone event but we use fork to synthesize it */ 186 /*
143 event->fork.ppid = tgid; 187 * for main thread set parent to ppid from status file. For other
144 event->fork.ptid = tgid; 188 * threads set parent pid to main thread. ie., assume main thread
189 * spawns all threads in a process
190 */
191 if (tgid == pid) {
192 event->fork.ppid = ppid;
193 event->fork.ptid = ppid;
194 } else {
195 event->fork.ppid = tgid;
196 event->fork.ptid = tgid;
197 }
145 event->fork.pid = tgid; 198 event->fork.pid = tgid;
146 event->fork.tid = pid; 199 event->fork.tid = pid;
147 event->fork.header.type = PERF_RECORD_FORK; 200 event->fork.header.type = PERF_RECORD_FORK;
@@ -333,7 +386,8 @@ static int __event__synthesize_thread(union perf_event *comm_event,
333 char filename[PATH_MAX]; 386 char filename[PATH_MAX];
334 DIR *tasks; 387 DIR *tasks;
335 struct dirent dirent, *next; 388 struct dirent dirent, *next;
336 pid_t tgid; 389 pid_t tgid, ppid;
390 int rc = 0;
337 391
338 /* special case: only send one comm event using passed in pid */ 392 /* special case: only send one comm event using passed in pid */
339 if (!full) { 393 if (!full) {
@@ -361,34 +415,38 @@ static int __event__synthesize_thread(union perf_event *comm_event,
361 415
362 while (!readdir_r(tasks, &dirent, &next) && next) { 416 while (!readdir_r(tasks, &dirent, &next) && next) {
363 char *end; 417 char *end;
364 int rc = 0;
365 pid_t _pid; 418 pid_t _pid;
366 419
367 _pid = strtol(dirent.d_name, &end, 10); 420 _pid = strtol(dirent.d_name, &end, 10);
368 if (*end) 421 if (*end)
369 continue; 422 continue;
370 423
371 tgid = perf_event__synthesize_comm(tool, comm_event, _pid, 424 rc = -1;
372 process, machine); 425 if (perf_event__prepare_comm(comm_event, _pid, machine,
373 if (tgid == -1) 426 &tgid, &ppid) != 0)
374 return -1; 427 break;
375 428
429 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
430 ppid, process, machine) < 0)
431 break;
432 /*
433 * Send the prepared comm event
434 */
435 if (process(tool, comm_event, &synth_sample, machine) != 0)
436 break;
437
438 rc = 0;
376 if (_pid == pid) { 439 if (_pid == pid) {
377 /* process the parent's maps too */ 440 /* process the parent's maps too */
378 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, 441 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
379 process, machine, mmap_data); 442 process, machine, mmap_data);
380 } else { 443 if (rc)
381 /* only fork the tid's map, to save time */ 444 break;
382 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
383 process, machine);
384 } 445 }
385
386 if (rc)
387 return rc;
388 } 446 }
389 447
390 closedir(tasks); 448 closedir(tasks);
391 return 0; 449 return rc;
392} 450}
393 451
394int perf_event__synthesize_thread_map(struct perf_tool *tool, 452int perf_event__synthesize_thread_map(struct perf_tool *tool,
@@ -615,7 +673,7 @@ size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
615 else 673 else
616 s = ""; 674 s = "";
617 675
618 return fprintf(fp, "%s: %s:%d\n", s, event->comm.comm, event->comm.tid); 676 return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
619} 677}
620 678
621int perf_event__process_comm(struct perf_tool *tool __maybe_unused, 679int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index c4ffe2bd0738..09b9e8d3fcf7 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -242,7 +242,6 @@ struct events_stats {
242 u32 nr_invalid_chains; 242 u32 nr_invalid_chains;
243 u32 nr_unknown_id; 243 u32 nr_unknown_id;
244 u32 nr_unprocessable_samples; 244 u32 nr_unprocessable_samples;
245 u32 nr_unordered_events;
246}; 245};
247 246
248struct attr_event { 247struct attr_event {
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 28b8ce86bf12..080be93eea96 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -7,7 +7,6 @@
7 * Released under the GPL v2. (and only v2, not any later version) 7 * Released under the GPL v2. (and only v2, not any later version)
8 */ 8 */
9#include "util.h" 9#include "util.h"
10#include <api/fs/debugfs.h>
11#include <api/fs/fs.h> 10#include <api/fs/fs.h>
12#include <poll.h> 11#include <poll.h>
13#include "cpumap.h" 12#include "cpumap.h"
@@ -635,8 +634,8 @@ static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
635union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 634union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
636{ 635{
637 struct perf_mmap *md = &evlist->mmap[idx]; 636 struct perf_mmap *md = &evlist->mmap[idx];
638 unsigned int head = perf_mmap__read_head(md); 637 u64 head = perf_mmap__read_head(md);
639 unsigned int old = md->prev; 638 u64 old = md->prev;
640 unsigned char *data = md->base + page_size; 639 unsigned char *data = md->base + page_size;
641 union perf_event *event = NULL; 640 union perf_event *event = NULL;
642 641
@@ -696,7 +695,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
696 695
697static bool perf_mmap__empty(struct perf_mmap *md) 696static bool perf_mmap__empty(struct perf_mmap *md)
698{ 697{
699 return perf_mmap__read_head(md) != md->prev; 698 return perf_mmap__read_head(md) == md->prev;
700} 699}
701 700
702static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 701static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
@@ -717,7 +716,7 @@ void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
717 struct perf_mmap *md = &evlist->mmap[idx]; 716 struct perf_mmap *md = &evlist->mmap[idx];
718 717
719 if (!evlist->overwrite) { 718 if (!evlist->overwrite) {
720 unsigned int old = md->prev; 719 u64 old = md->prev;
721 720
722 perf_mmap__write_tail(md, old); 721 perf_mmap__write_tail(md, old);
723 } 722 }
@@ -1051,7 +1050,7 @@ out_delete_threads:
1051 return -1; 1050 return -1;
1052} 1051}
1053 1052
1054int perf_evlist__apply_filters(struct perf_evlist *evlist) 1053int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
1055{ 1054{
1056 struct perf_evsel *evsel; 1055 struct perf_evsel *evsel;
1057 int err = 0; 1056 int err = 0;
@@ -1063,8 +1062,10 @@ int perf_evlist__apply_filters(struct perf_evlist *evlist)
1063 continue; 1062 continue;
1064 1063
1065 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 1064 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
1066 if (err) 1065 if (err) {
1066 *err_evsel = evsel;
1067 break; 1067 break;
1068 }
1068 } 1069 }
1069 1070
1070 return err; 1071 return err;
@@ -1086,6 +1087,38 @@ int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1086 return err; 1087 return err;
1087} 1088}
1088 1089
1090int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1091{
1092 char *filter;
1093 int ret = -1;
1094 size_t i;
1095
1096 for (i = 0; i < npids; ++i) {
1097 if (i == 0) {
1098 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1099 return -1;
1100 } else {
1101 char *tmp;
1102
1103 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1104 goto out_free;
1105
1106 free(filter);
1107 filter = tmp;
1108 }
1109 }
1110
1111 ret = perf_evlist__set_filter(evlist, filter);
1112out_free:
1113 free(filter);
1114 return ret;
1115}
1116
1117int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1118{
1119 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1120}
1121
1089bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 1122bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1090{ 1123{
1091 struct perf_evsel *pos; 1124 struct perf_evsel *pos;
@@ -1329,7 +1362,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *tar
1329 * writing exactly one byte, in workload.cork_fd, usually via 1362 * writing exactly one byte, in workload.cork_fd, usually via
1330 * perf_evlist__start_workload(). 1363 * perf_evlist__start_workload().
1331 * 1364 *
1332 * For cancelling the workload without actuallin running it, 1365 * For cancelling the workload without actually running it,
1333 * the parent will just close workload.cork_fd, without writing 1366 * the parent will just close workload.cork_fd, without writing
1334 * anything, i.e. read will return zero and we just exit() 1367 * anything, i.e. read will return zero and we just exit()
1335 * here. 1368 * here.
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index e99a67632831..b5cce95d644e 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -27,7 +27,7 @@ struct perf_mmap {
27 void *base; 27 void *base;
28 int mask; 28 int mask;
29 int refcnt; 29 int refcnt;
30 unsigned int prev; 30 u64 prev;
31 char event_copy[PERF_SAMPLE_MAX_SIZE] __attribute__((aligned(8))); 31 char event_copy[PERF_SAMPLE_MAX_SIZE] __attribute__((aligned(8)));
32}; 32};
33 33
@@ -51,6 +51,7 @@ struct perf_evlist {
51 struct thread_map *threads; 51 struct thread_map *threads;
52 struct cpu_map *cpus; 52 struct cpu_map *cpus;
53 struct perf_evsel *selected; 53 struct perf_evsel *selected;
54 struct events_stats stats;
54}; 55};
55 56
56struct perf_evsel_str_handler { 57struct perf_evsel_str_handler {
@@ -77,6 +78,8 @@ int perf_evlist__add_newtp(struct perf_evlist *evlist,
77 const char *sys, const char *name, void *handler); 78 const char *sys, const char *name, void *handler);
78 79
79int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter); 80int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter);
81int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid);
82int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids);
80 83
81struct perf_evsel * 84struct perf_evsel *
82perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id); 85perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id);
@@ -149,7 +152,7 @@ static inline void perf_evlist__set_maps(struct perf_evlist *evlist,
149} 152}
150 153
151int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target); 154int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target);
152int perf_evlist__apply_filters(struct perf_evlist *evlist); 155int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel);
153 156
154void __perf_evlist__set_leader(struct list_head *list); 157void __perf_evlist__set_leader(struct list_head *list);
155void perf_evlist__set_leader(struct perf_evlist *evlist); 158void perf_evlist__set_leader(struct perf_evlist *evlist);
@@ -186,16 +189,15 @@ size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp);
186int perf_evlist__strerror_open(struct perf_evlist *evlist, int err, char *buf, size_t size); 189int perf_evlist__strerror_open(struct perf_evlist *evlist, int err, char *buf, size_t size);
187int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size); 190int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size);
188 191
189static inline unsigned int perf_mmap__read_head(struct perf_mmap *mm) 192static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
190{ 193{
191 struct perf_event_mmap_page *pc = mm->base; 194 struct perf_event_mmap_page *pc = mm->base;
192 int head = ACCESS_ONCE(pc->data_head); 195 u64 head = ACCESS_ONCE(pc->data_head);
193 rmb(); 196 rmb();
194 return head; 197 return head;
195} 198}
196 199
197static inline void perf_mmap__write_tail(struct perf_mmap *md, 200static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
198 unsigned long tail)
199{ 201{
200 struct perf_event_mmap_page *pc = md->base; 202 struct perf_event_mmap_page *pc = md->base;
201 203
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index ea51a90e20a0..33e3fd8c2e68 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -32,8 +32,12 @@ static struct {
32 bool exclude_guest; 32 bool exclude_guest;
33 bool mmap2; 33 bool mmap2;
34 bool cloexec; 34 bool cloexec;
35 bool clockid;
36 bool clockid_wrong;
35} perf_missing_features; 37} perf_missing_features;
36 38
39static clockid_t clockid;
40
37static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused) 41static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
38{ 42{
39 return 0; 43 return 0;
@@ -537,13 +541,30 @@ int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
537} 541}
538 542
539static void 543static void
540perf_evsel__config_callgraph(struct perf_evsel *evsel) 544perf_evsel__config_callgraph(struct perf_evsel *evsel,
545 struct record_opts *opts)
541{ 546{
542 bool function = perf_evsel__is_function_event(evsel); 547 bool function = perf_evsel__is_function_event(evsel);
543 struct perf_event_attr *attr = &evsel->attr; 548 struct perf_event_attr *attr = &evsel->attr;
544 549
545 perf_evsel__set_sample_bit(evsel, CALLCHAIN); 550 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
546 551
552 if (callchain_param.record_mode == CALLCHAIN_LBR) {
553 if (!opts->branch_stack) {
554 if (attr->exclude_user) {
555 pr_warning("LBR callstack option is only available "
556 "to get user callchain information. "
557 "Falling back to framepointers.\n");
558 } else {
559 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
560 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
561 PERF_SAMPLE_BRANCH_CALL_STACK;
562 }
563 } else
564 pr_warning("Cannot use LBR callstack with branch stack. "
565 "Falling back to framepointers.\n");
566 }
567
547 if (callchain_param.record_mode == CALLCHAIN_DWARF) { 568 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
548 if (!function) { 569 if (!function) {
549 perf_evsel__set_sample_bit(evsel, REGS_USER); 570 perf_evsel__set_sample_bit(evsel, REGS_USER);
@@ -667,7 +688,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
667 evsel->attr.exclude_callchain_user = 1; 688 evsel->attr.exclude_callchain_user = 1;
668 689
669 if (callchain_param.enabled && !evsel->no_aux_samples) 690 if (callchain_param.enabled && !evsel->no_aux_samples)
670 perf_evsel__config_callgraph(evsel); 691 perf_evsel__config_callgraph(evsel, opts);
671 692
672 if (opts->sample_intr_regs) { 693 if (opts->sample_intr_regs) {
673 attr->sample_regs_intr = PERF_REGS_MASK; 694 attr->sample_regs_intr = PERF_REGS_MASK;
@@ -717,6 +738,12 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
717 if (opts->sample_transaction) 738 if (opts->sample_transaction)
718 perf_evsel__set_sample_bit(evsel, TRANSACTION); 739 perf_evsel__set_sample_bit(evsel, TRANSACTION);
719 740
741 if (opts->running_time) {
742 evsel->attr.read_format |=
743 PERF_FORMAT_TOTAL_TIME_ENABLED |
744 PERF_FORMAT_TOTAL_TIME_RUNNING;
745 }
746
720 /* 747 /*
721 * XXX see the function comment above 748 * XXX see the function comment above
722 * 749 *
@@ -738,6 +765,12 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
738 attr->disabled = 0; 765 attr->disabled = 0;
739 attr->enable_on_exec = 0; 766 attr->enable_on_exec = 0;
740 } 767 }
768
769 clockid = opts->clockid;
770 if (opts->use_clockid) {
771 attr->use_clockid = 1;
772 attr->clockid = opts->clockid;
773 }
741} 774}
742 775
743static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) 776static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
@@ -978,67 +1011,126 @@ static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
978 return fd; 1011 return fd;
979} 1012}
980 1013
981#define __PRINT_ATTR(fmt, cast, field) \ 1014struct bit_names {
982 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field) 1015 int bit;
983 1016 const char *name;
984#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field) 1017};
985#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field) 1018
986#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field) 1019static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
987#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field) 1020{
988 1021 bool first_bit = true;
989#define PRINT_ATTR2N(name1, field1, name2, field2) \ 1022 int i = 0;
990 fprintf(fp, " %-19s %u %-19s %u\n", \ 1023
991 name1, attr->field1, name2, attr->field2) 1024 do {
992 1025 if (value & bits[i].bit) {
993#define PRINT_ATTR2(field1, field2) \ 1026 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
994 PRINT_ATTR2N(#field1, field1, #field2, field2) 1027 first_bit = false;
995 1028 }
996static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp) 1029 } while (bits[++i].name != NULL);
997{ 1030}
998 size_t ret = 0; 1031
999 1032static void __p_sample_type(char *buf, size_t size, u64 value)
1000 ret += fprintf(fp, "%.60s\n", graph_dotted_line); 1033{
1001 ret += fprintf(fp, "perf_event_attr:\n"); 1034#define bit_name(n) { PERF_SAMPLE_##n, #n }
1002 1035 struct bit_names bits[] = {
1003 ret += PRINT_ATTR_U32(type); 1036 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1004 ret += PRINT_ATTR_U32(size); 1037 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1005 ret += PRINT_ATTR_X64(config); 1038 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1006 ret += PRINT_ATTR_U64(sample_period); 1039 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1007 ret += PRINT_ATTR_U64(sample_freq); 1040 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1008 ret += PRINT_ATTR_X64(sample_type); 1041 { .name = NULL, }
1009 ret += PRINT_ATTR_X64(read_format); 1042 };
1010 1043#undef bit_name
1011 ret += PRINT_ATTR2(disabled, inherit); 1044 __p_bits(buf, size, value, bits);
1012 ret += PRINT_ATTR2(pinned, exclusive); 1045}
1013 ret += PRINT_ATTR2(exclude_user, exclude_kernel); 1046
1014 ret += PRINT_ATTR2(exclude_hv, exclude_idle); 1047static void __p_read_format(char *buf, size_t size, u64 value)
1015 ret += PRINT_ATTR2(mmap, comm); 1048{
1016 ret += PRINT_ATTR2(mmap2, comm_exec); 1049#define bit_name(n) { PERF_FORMAT_##n, #n }
1017 ret += PRINT_ATTR2(freq, inherit_stat); 1050 struct bit_names bits[] = {
1018 ret += PRINT_ATTR2(enable_on_exec, task); 1051 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1019 ret += PRINT_ATTR2(watermark, precise_ip); 1052 bit_name(ID), bit_name(GROUP),
1020 ret += PRINT_ATTR2(mmap_data, sample_id_all); 1053 { .name = NULL, }
1021 ret += PRINT_ATTR2(exclude_host, exclude_guest); 1054 };
1022 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel, 1055#undef bit_name
1023 "excl.callchain_user", exclude_callchain_user); 1056 __p_bits(buf, size, value, bits);
1024 1057}
1025 ret += PRINT_ATTR_U32(wakeup_events); 1058
1026 ret += PRINT_ATTR_U32(wakeup_watermark); 1059#define BUF_SIZE 1024
1027 ret += PRINT_ATTR_X32(bp_type); 1060
1028 ret += PRINT_ATTR_X64(bp_addr); 1061#define p_hex(val) snprintf(buf, BUF_SIZE, "%"PRIx64, (uint64_t)(val))
1029 ret += PRINT_ATTR_X64(config1); 1062#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1030 ret += PRINT_ATTR_U64(bp_len); 1063#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1031 ret += PRINT_ATTR_X64(config2); 1064#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1032 ret += PRINT_ATTR_X64(branch_sample_type); 1065#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1033 ret += PRINT_ATTR_X64(sample_regs_user); 1066
1034 ret += PRINT_ATTR_U32(sample_stack_user); 1067#define PRINT_ATTRn(_n, _f, _p) \
1035 ret += PRINT_ATTR_X64(sample_regs_intr); 1068do { \
1036 1069 if (attr->_f) { \
1037 ret += fprintf(fp, "%.60s\n", graph_dotted_line); 1070 _p(attr->_f); \
1071 ret += attr__fprintf(fp, _n, buf, priv);\
1072 } \
1073} while (0)
1074
1075#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1076
1077int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1078 attr__fprintf_f attr__fprintf, void *priv)
1079{
1080 char buf[BUF_SIZE];
1081 int ret = 0;
1082
1083 PRINT_ATTRf(type, p_unsigned);
1084 PRINT_ATTRf(size, p_unsigned);
1085 PRINT_ATTRf(config, p_hex);
1086 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1087 PRINT_ATTRf(sample_type, p_sample_type);
1088 PRINT_ATTRf(read_format, p_read_format);
1089
1090 PRINT_ATTRf(disabled, p_unsigned);
1091 PRINT_ATTRf(inherit, p_unsigned);
1092 PRINT_ATTRf(pinned, p_unsigned);
1093 PRINT_ATTRf(exclusive, p_unsigned);
1094 PRINT_ATTRf(exclude_user, p_unsigned);
1095 PRINT_ATTRf(exclude_kernel, p_unsigned);
1096 PRINT_ATTRf(exclude_hv, p_unsigned);
1097 PRINT_ATTRf(exclude_idle, p_unsigned);
1098 PRINT_ATTRf(mmap, p_unsigned);
1099 PRINT_ATTRf(comm, p_unsigned);
1100 PRINT_ATTRf(freq, p_unsigned);
1101 PRINT_ATTRf(inherit_stat, p_unsigned);
1102 PRINT_ATTRf(enable_on_exec, p_unsigned);
1103 PRINT_ATTRf(task, p_unsigned);
1104 PRINT_ATTRf(watermark, p_unsigned);
1105 PRINT_ATTRf(precise_ip, p_unsigned);
1106 PRINT_ATTRf(mmap_data, p_unsigned);
1107 PRINT_ATTRf(sample_id_all, p_unsigned);
1108 PRINT_ATTRf(exclude_host, p_unsigned);
1109 PRINT_ATTRf(exclude_guest, p_unsigned);
1110 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1111 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1112 PRINT_ATTRf(mmap2, p_unsigned);
1113 PRINT_ATTRf(comm_exec, p_unsigned);
1114 PRINT_ATTRf(use_clockid, p_unsigned);
1115
1116 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1117 PRINT_ATTRf(bp_type, p_unsigned);
1118 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1119 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1120 PRINT_ATTRf(sample_regs_user, p_hex);
1121 PRINT_ATTRf(sample_stack_user, p_unsigned);
1122 PRINT_ATTRf(clockid, p_signed);
1123 PRINT_ATTRf(sample_regs_intr, p_hex);
1038 1124
1039 return ret; 1125 return ret;
1040} 1126}
1041 1127
1128static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1129 void *priv __attribute__((unused)))
1130{
1131 return fprintf(fp, " %-32s %s\n", name, val);
1132}
1133
1042static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, 1134static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1043 struct thread_map *threads) 1135 struct thread_map *threads)
1044{ 1136{
@@ -1062,6 +1154,12 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1062 } 1154 }
1063 1155
1064fallback_missing_features: 1156fallback_missing_features:
1157 if (perf_missing_features.clockid_wrong)
1158 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1159 if (perf_missing_features.clockid) {
1160 evsel->attr.use_clockid = 0;
1161 evsel->attr.clockid = 0;
1162 }
1065 if (perf_missing_features.cloexec) 1163 if (perf_missing_features.cloexec)
1066 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; 1164 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
1067 if (perf_missing_features.mmap2) 1165 if (perf_missing_features.mmap2)
@@ -1072,8 +1170,12 @@ retry_sample_id:
1072 if (perf_missing_features.sample_id_all) 1170 if (perf_missing_features.sample_id_all)
1073 evsel->attr.sample_id_all = 0; 1171 evsel->attr.sample_id_all = 0;
1074 1172
1075 if (verbose >= 2) 1173 if (verbose >= 2) {
1076 perf_event_attr__fprintf(&evsel->attr, stderr); 1174 fprintf(stderr, "%.60s\n", graph_dotted_line);
1175 fprintf(stderr, "perf_event_attr:\n");
1176 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1177 fprintf(stderr, "%.60s\n", graph_dotted_line);
1178 }
1077 1179
1078 for (cpu = 0; cpu < cpus->nr; cpu++) { 1180 for (cpu = 0; cpu < cpus->nr; cpu++) {
1079 1181
@@ -1099,6 +1201,17 @@ retry_open:
1099 goto try_fallback; 1201 goto try_fallback;
1100 } 1202 }
1101 set_rlimit = NO_CHANGE; 1203 set_rlimit = NO_CHANGE;
1204
1205 /*
1206 * If we succeeded but had to kill clockid, fail and
1207 * have perf_evsel__open_strerror() print us a nice
1208 * error.
1209 */
1210 if (perf_missing_features.clockid ||
1211 perf_missing_features.clockid_wrong) {
1212 err = -EINVAL;
1213 goto out_close;
1214 }
1102 } 1215 }
1103 } 1216 }
1104 1217
@@ -1132,7 +1245,17 @@ try_fallback:
1132 if (err != -EINVAL || cpu > 0 || thread > 0) 1245 if (err != -EINVAL || cpu > 0 || thread > 0)
1133 goto out_close; 1246 goto out_close;
1134 1247
1135 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) { 1248 /*
1249 * Must probe features in the order they were added to the
1250 * perf_event_attr interface.
1251 */
1252 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1253 perf_missing_features.clockid_wrong = true;
1254 goto fallback_missing_features;
1255 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1256 perf_missing_features.clockid = true;
1257 goto fallback_missing_features;
1258 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1136 perf_missing_features.cloexec = true; 1259 perf_missing_features.cloexec = true;
1137 goto fallback_missing_features; 1260 goto fallback_missing_features;
1138 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) { 1261 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
@@ -1892,7 +2015,7 @@ u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1892 value = *(u32 *)ptr; 2015 value = *(u32 *)ptr;
1893 break; 2016 break;
1894 case 8: 2017 case 8:
1895 value = *(u64 *)ptr; 2018 memcpy(&value, ptr, sizeof(u64));
1896 break; 2019 break;
1897 default: 2020 default:
1898 return 0; 2021 return 0;
@@ -1933,62 +2056,9 @@ static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1933 return ret; 2056 return ret;
1934} 2057}
1935 2058
1936static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value) 2059static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
1937{
1938 if (value == 0)
1939 return 0;
1940
1941 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1942}
1943
1944#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1945
1946struct bit_names {
1947 int bit;
1948 const char *name;
1949};
1950
1951static int bits__fprintf(FILE *fp, const char *field, u64 value,
1952 struct bit_names *bits, bool *first)
1953{
1954 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1955 bool first_bit = true;
1956
1957 do {
1958 if (value & bits[i].bit) {
1959 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1960 first_bit = false;
1961 }
1962 } while (bits[++i].name != NULL);
1963
1964 return printed;
1965}
1966
1967static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1968{
1969#define bit_name(n) { PERF_SAMPLE_##n, #n }
1970 struct bit_names bits[] = {
1971 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1972 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1973 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1974 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1975 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1976 { .name = NULL, }
1977 };
1978#undef bit_name
1979 return bits__fprintf(fp, "sample_type", value, bits, first);
1980}
1981
1982static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1983{ 2060{
1984#define bit_name(n) { PERF_FORMAT_##n, #n } 2061 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
1985 struct bit_names bits[] = {
1986 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1987 bit_name(ID), bit_name(GROUP),
1988 { .name = NULL, }
1989 };
1990#undef bit_name
1991 return bits__fprintf(fp, "read_format", value, bits, first);
1992} 2062}
1993 2063
1994int perf_evsel__fprintf(struct perf_evsel *evsel, 2064int perf_evsel__fprintf(struct perf_evsel *evsel,
@@ -2017,47 +2087,13 @@ int perf_evsel__fprintf(struct perf_evsel *evsel,
2017 2087
2018 printed += fprintf(fp, "%s", perf_evsel__name(evsel)); 2088 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2019 2089
2020 if (details->verbose || details->freq) { 2090 if (details->verbose) {
2091 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2092 __print_attr__fprintf, &first);
2093 } else if (details->freq) {
2021 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64, 2094 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2022 (u64)evsel->attr.sample_freq); 2095 (u64)evsel->attr.sample_freq);
2023 } 2096 }
2024
2025 if (details->verbose) {
2026 if_print(type);
2027 if_print(config);
2028 if_print(config1);
2029 if_print(config2);
2030 if_print(size);
2031 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
2032 if (evsel->attr.read_format)
2033 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
2034 if_print(disabled);
2035 if_print(inherit);
2036 if_print(pinned);
2037 if_print(exclusive);
2038 if_print(exclude_user);
2039 if_print(exclude_kernel);
2040 if_print(exclude_hv);
2041 if_print(exclude_idle);
2042 if_print(mmap);
2043 if_print(mmap2);
2044 if_print(comm);
2045 if_print(comm_exec);
2046 if_print(freq);
2047 if_print(inherit_stat);
2048 if_print(enable_on_exec);
2049 if_print(task);
2050 if_print(watermark);
2051 if_print(precise_ip);
2052 if_print(mmap_data);
2053 if_print(sample_id_all);
2054 if_print(exclude_host);
2055 if_print(exclude_guest);
2056 if_print(__reserved_1);
2057 if_print(wakeup_events);
2058 if_print(bp_type);
2059 if_print(branch_sample_type);
2060 }
2061out: 2097out:
2062 fputc('\n', fp); 2098 fputc('\n', fp);
2063 return ++printed; 2099 return ++printed;
@@ -2135,6 +2171,12 @@ int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
2135 "The PMU counters are busy/taken by another profiler.\n" 2171 "The PMU counters are busy/taken by another profiler.\n"
2136 "We found oprofile daemon running, please stop it and try again."); 2172 "We found oprofile daemon running, please stop it and try again.");
2137 break; 2173 break;
2174 case EINVAL:
2175 if (perf_missing_features.clockid)
2176 return scnprintf(msg, size, "clockid feature not supported.");
2177 if (perf_missing_features.clockid_wrong)
2178 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2179 break;
2138 default: 2180 default:
2139 break; 2181 break;
2140 } 2182 }
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 38622747d130..e486151b0308 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -335,6 +335,7 @@ struct perf_attr_details {
335 bool freq; 335 bool freq;
336 bool verbose; 336 bool verbose;
337 bool event_group; 337 bool event_group;
338 bool force;
338}; 339};
339 340
340int perf_evsel__fprintf(struct perf_evsel *evsel, 341int perf_evsel__fprintf(struct perf_evsel *evsel,
@@ -355,4 +356,14 @@ for ((_evsel) = list_entry((_leader)->node.next, struct perf_evsel, node); \
355 (_evsel) && (_evsel)->leader == (_leader); \ 356 (_evsel) && (_evsel)->leader == (_leader); \
356 (_evsel) = list_entry((_evsel)->node.next, struct perf_evsel, node)) 357 (_evsel) = list_entry((_evsel)->node.next, struct perf_evsel, node))
357 358
359static inline bool has_branch_callstack(struct perf_evsel *evsel)
360{
361 return evsel->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
362}
363
364typedef int (*attr__fprintf_f)(FILE *, const char *, const char *, void *);
365
366int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
367 attr__fprintf_f attr__fprintf, void *priv);
368
358#endif /* __PERF_EVSEL_H */ 369#endif /* __PERF_EVSEL_H */
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 1f407f7352a7..918fd8ae2d80 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -1055,6 +1055,12 @@ error:
1055 goto out; 1055 goto out;
1056} 1056}
1057 1057
1058static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1059 void *priv __attribute__((unused)))
1060{
1061 return fprintf(fp, ", %s = %s", name, val);
1062}
1063
1058static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) 1064static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1059{ 1065{
1060 struct perf_evsel *evsel, *events = read_event_desc(ph, fd); 1066 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
@@ -1069,26 +1075,6 @@ static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1069 for (evsel = events; evsel->attr.size; evsel++) { 1075 for (evsel = events; evsel->attr.size; evsel++) {
1070 fprintf(fp, "# event : name = %s, ", evsel->name); 1076 fprintf(fp, "# event : name = %s, ", evsel->name);
1071 1077
1072 fprintf(fp, "type = %d, config = 0x%"PRIx64
1073 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1074 evsel->attr.type,
1075 (u64)evsel->attr.config,
1076 (u64)evsel->attr.config1,
1077 (u64)evsel->attr.config2);
1078
1079 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1080 evsel->attr.exclude_user,
1081 evsel->attr.exclude_kernel);
1082
1083 fprintf(fp, ", excl_host = %d, excl_guest = %d",
1084 evsel->attr.exclude_host,
1085 evsel->attr.exclude_guest);
1086
1087 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
1088
1089 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1090 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1091 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
1092 if (evsel->ids) { 1078 if (evsel->ids) {
1093 fprintf(fp, ", id = {"); 1079 fprintf(fp, ", id = {");
1094 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { 1080 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
@@ -1099,6 +1085,8 @@ static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1099 fprintf(fp, " }"); 1085 fprintf(fp, " }");
1100 } 1086 }
1101 1087
1088 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1089
1102 fputc('\n', fp); 1090 fputc('\n', fp);
1103 } 1091 }
1104 1092
@@ -1266,7 +1254,7 @@ static int __event_process_build_id(struct build_id_event *bev,
1266 1254
1267 dso__set_build_id(dso, &bev->build_id); 1255 dso__set_build_id(dso, &bev->build_id);
1268 1256
1269 if (!is_kernel_module(filename, NULL)) 1257 if (!is_kernel_module(filename))
1270 dso->kernel = dso_type; 1258 dso->kernel = dso_type;
1271 1259
1272 build_id__sprintf(dso->build_id, sizeof(dso->build_id), 1260 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
@@ -2516,8 +2504,11 @@ int perf_session__read_header(struct perf_session *session)
2516 if (read_attr(fd, header, &f_attr) < 0) 2504 if (read_attr(fd, header, &f_attr) < 0)
2517 goto out_errno; 2505 goto out_errno;
2518 2506
2519 if (header->needs_swap) 2507 if (header->needs_swap) {
2508 f_attr.ids.size = bswap_64(f_attr.ids.size);
2509 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2520 perf_event__attr_swap(&f_attr.attr); 2510 perf_event__attr_swap(&f_attr.attr);
2511 }
2521 2512
2522 tmp = lseek(fd, 0, SEEK_CUR); 2513 tmp = lseek(fd, 0, SEEK_CUR);
2523 evsel = perf_evsel__new(&f_attr.attr); 2514 evsel = perf_evsel__new(&f_attr.attr);
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 70b48a65064c..cc22b9158b93 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -263,15 +263,9 @@ void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
263 while (next) { 263 while (next) {
264 n = rb_entry(next, struct hist_entry, rb_node); 264 n = rb_entry(next, struct hist_entry, rb_node);
265 next = rb_next(&n->rb_node); 265 next = rb_next(&n->rb_node);
266 /*
267 * We may be annotating this, for instance, so keep it here in
268 * case some it gets new samples, we'll eventually free it when
269 * the user stops browsing and it agains gets fully decayed.
270 */
271 if (((zap_user && n->level == '.') || 266 if (((zap_user && n->level == '.') ||
272 (zap_kernel && n->level != '.') || 267 (zap_kernel && n->level != '.') ||
273 hists__decay_entry(hists, n)) && 268 hists__decay_entry(hists, n))) {
274 !n->used) {
275 hists__delete_entry(hists, n); 269 hists__delete_entry(hists, n);
276 } 270 }
277 } 271 }
@@ -355,6 +349,7 @@ static struct hist_entry *hist_entry__new(struct hist_entry *template,
355 callchain_init(he->callchain); 349 callchain_init(he->callchain);
356 350
357 INIT_LIST_HEAD(&he->pairs.node); 351 INIT_LIST_HEAD(&he->pairs.node);
352 thread__get(he->thread);
358 } 353 }
359 354
360 return he; 355 return he;
@@ -941,6 +936,7 @@ hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
941 936
942void hist_entry__delete(struct hist_entry *he) 937void hist_entry__delete(struct hist_entry *he)
943{ 938{
939 thread__zput(he->thread);
944 zfree(&he->branch_info); 940 zfree(&he->branch_info);
945 zfree(&he->mem_info); 941 zfree(&he->mem_info);
946 zfree(&he->stat_acc); 942 zfree(&he->stat_acc);
@@ -1169,6 +1165,7 @@ static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h
1169 /* force fold unfiltered entry for simplicity */ 1165 /* force fold unfiltered entry for simplicity */
1170 h->ms.unfolded = false; 1166 h->ms.unfolded = false;
1171 h->row_offset = 0; 1167 h->row_offset = 0;
1168 h->nr_rows = 0;
1172 1169
1173 hists->stats.nr_non_filtered_samples += h->stat.nr_events; 1170 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1174 1171
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index 2b690d028907..9f31b89a527a 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -60,7 +60,7 @@ struct hists {
60 struct rb_root entries_collapsed; 60 struct rb_root entries_collapsed;
61 u64 nr_entries; 61 u64 nr_entries;
62 u64 nr_non_filtered_entries; 62 u64 nr_non_filtered_entries;
63 const struct thread *thread_filter; 63 struct thread *thread_filter;
64 const struct dso *dso_filter; 64 const struct dso *dso_filter;
65 const char *uid_filter_str; 65 const char *uid_filter_str;
66 const char *symbol_filter_str; 66 const char *symbol_filter_str;
@@ -303,6 +303,9 @@ struct hist_browser_timer {
303 303
304#ifdef HAVE_SLANG_SUPPORT 304#ifdef HAVE_SLANG_SUPPORT
305#include "../ui/keysyms.h" 305#include "../ui/keysyms.h"
306int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
307 struct hist_browser_timer *hbt);
308
306int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel, 309int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
307 struct hist_browser_timer *hbt); 310 struct hist_browser_timer *hbt);
308 311
@@ -321,6 +324,12 @@ int perf_evlist__tui_browse_hists(struct perf_evlist *evlist __maybe_unused,
321{ 324{
322 return 0; 325 return 0;
323} 326}
327static inline int map_symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
328 struct perf_evsel *evsel __maybe_unused,
329 struct hist_browser_timer *hbt __maybe_unused)
330{
331 return 0;
332}
324 333
325static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused, 334static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
326 struct perf_evsel *evsel __maybe_unused, 335 struct perf_evsel *evsel __maybe_unused,
diff --git a/tools/perf/util/kvm-stat.h b/tools/perf/util/kvm-stat.h
index cf1d7913783b..ae825d4ec110 100644
--- a/tools/perf/util/kvm-stat.h
+++ b/tools/perf/util/kvm-stat.h
@@ -99,6 +99,7 @@ struct perf_kvm_stat {
99 int timerfd; 99 int timerfd;
100 unsigned int display_time; 100 unsigned int display_time;
101 bool live; 101 bool live;
102 bool force;
102}; 103};
103 104
104struct kvm_reg_events_ops { 105struct kvm_reg_events_ops {
diff --git a/tools/perf/util/lzma.c b/tools/perf/util/lzma.c
new file mode 100644
index 000000000000..95a1acb61245
--- /dev/null
+++ b/tools/perf/util/lzma.c
@@ -0,0 +1,95 @@
1#include <lzma.h>
2#include <stdio.h>
3#include <linux/compiler.h>
4#include "util.h"
5#include "debug.h"
6
7#define BUFSIZE 8192
8
9static const char *lzma_strerror(lzma_ret ret)
10{
11 switch ((int) ret) {
12 case LZMA_MEM_ERROR:
13 return "Memory allocation failed";
14 case LZMA_OPTIONS_ERROR:
15 return "Unsupported decompressor flags";
16 case LZMA_FORMAT_ERROR:
17 return "The input is not in the .xz format";
18 case LZMA_DATA_ERROR:
19 return "Compressed file is corrupt";
20 case LZMA_BUF_ERROR:
21 return "Compressed file is truncated or otherwise corrupt";
22 default:
23 return "Unknown error, possibly a bug";
24 }
25}
26
27int lzma_decompress_to_file(const char *input, int output_fd)
28{
29 lzma_action action = LZMA_RUN;
30 lzma_stream strm = LZMA_STREAM_INIT;
31 lzma_ret ret;
32
33 u8 buf_in[BUFSIZE];
34 u8 buf_out[BUFSIZE];
35 FILE *infile;
36
37 infile = fopen(input, "rb");
38 if (!infile) {
39 pr_err("lzma: fopen failed on %s: '%s'\n",
40 input, strerror(errno));
41 return -1;
42 }
43
44 ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
45 if (ret != LZMA_OK) {
46 pr_err("lzma: lzma_stream_decoder failed %s (%d)\n",
47 lzma_strerror(ret), ret);
48 return -1;
49 }
50
51 strm.next_in = NULL;
52 strm.avail_in = 0;
53 strm.next_out = buf_out;
54 strm.avail_out = sizeof(buf_out);
55
56 while (1) {
57 if (strm.avail_in == 0 && !feof(infile)) {
58 strm.next_in = buf_in;
59 strm.avail_in = fread(buf_in, 1, sizeof(buf_in), infile);
60
61 if (ferror(infile)) {
62 pr_err("lzma: read error: %s\n", strerror(errno));
63 return -1;
64 }
65
66 if (feof(infile))
67 action = LZMA_FINISH;
68 }
69
70 ret = lzma_code(&strm, action);
71
72 if (strm.avail_out == 0 || ret == LZMA_STREAM_END) {
73 ssize_t write_size = sizeof(buf_out) - strm.avail_out;
74
75 if (writen(output_fd, buf_out, write_size) != write_size) {
76 pr_err("lzma: write error: %s\n", strerror(errno));
77 return -1;
78 }
79
80 strm.next_out = buf_out;
81 strm.avail_out = sizeof(buf_out);
82 }
83
84 if (ret != LZMA_OK) {
85 if (ret == LZMA_STREAM_END)
86 return 0;
87
88 pr_err("lzma: failed %s\n", lzma_strerror(ret));
89 return -1;
90 }
91 }
92
93 fclose(infile);
94 return 0;
95}
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 1bca3a9f2b16..527e032e24f6 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -89,16 +89,6 @@ static void dsos__delete(struct dsos *dsos)
89 } 89 }
90} 90}
91 91
92void machine__delete_dead_threads(struct machine *machine)
93{
94 struct thread *n, *t;
95
96 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
97 list_del(&t->node);
98 thread__delete(t);
99 }
100}
101
102void machine__delete_threads(struct machine *machine) 92void machine__delete_threads(struct machine *machine)
103{ 93{
104 struct rb_node *nd = rb_first(&machine->threads); 94 struct rb_node *nd = rb_first(&machine->threads);
@@ -106,9 +96,8 @@ void machine__delete_threads(struct machine *machine)
106 while (nd) { 96 while (nd) {
107 struct thread *t = rb_entry(nd, struct thread, rb_node); 97 struct thread *t = rb_entry(nd, struct thread, rb_node);
108 98
109 rb_erase(&t->rb_node, &machine->threads);
110 nd = rb_next(nd); 99 nd = rb_next(nd);
111 thread__delete(t); 100 machine__remove_thread(machine, t);
112 } 101 }
113} 102}
114 103
@@ -361,9 +350,13 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
361 * the full rbtree: 350 * the full rbtree:
362 */ 351 */
363 th = machine->last_match; 352 th = machine->last_match;
364 if (th && th->tid == tid) { 353 if (th != NULL) {
365 machine__update_thread_pid(machine, th, pid); 354 if (th->tid == tid) {
366 return th; 355 machine__update_thread_pid(machine, th, pid);
356 return th;
357 }
358
359 thread__zput(machine->last_match);
367 } 360 }
368 361
369 while (*p != NULL) { 362 while (*p != NULL) {
@@ -371,7 +364,7 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
371 th = rb_entry(parent, struct thread, rb_node); 364 th = rb_entry(parent, struct thread, rb_node);
372 365
373 if (th->tid == tid) { 366 if (th->tid == tid) {
374 machine->last_match = th; 367 machine->last_match = thread__get(th);
375 machine__update_thread_pid(machine, th, pid); 368 machine__update_thread_pid(machine, th, pid);
376 return th; 369 return th;
377 } 370 }
@@ -403,8 +396,11 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
403 thread__delete(th); 396 thread__delete(th);
404 return NULL; 397 return NULL;
405 } 398 }
406 399 /*
407 machine->last_match = th; 400 * It is now in the rbtree, get a ref
401 */
402 thread__get(th);
403 machine->last_match = thread__get(th);
408 } 404 }
409 405
410 return th; 406 return th;
@@ -462,30 +458,61 @@ int machine__process_lost_event(struct machine *machine __maybe_unused,
462 return 0; 458 return 0;
463} 459}
464 460
461static struct dso*
462machine__module_dso(struct machine *machine, struct kmod_path *m,
463 const char *filename)
464{
465 struct dso *dso;
466
467 dso = dsos__find(&machine->kernel_dsos, m->name, true);
468 if (!dso) {
469 dso = dsos__addnew(&machine->kernel_dsos, m->name);
470 if (dso == NULL)
471 return NULL;
472
473 if (machine__is_host(machine))
474 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
475 else
476 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
477
478 /* _KMODULE_COMP should be next to _KMODULE */
479 if (m->kmod && m->comp)
480 dso->symtab_type++;
481
482 dso__set_short_name(dso, strdup(m->name), true);
483 dso__set_long_name(dso, strdup(filename), true);
484 }
485
486 return dso;
487}
488
465struct map *machine__new_module(struct machine *machine, u64 start, 489struct map *machine__new_module(struct machine *machine, u64 start,
466 const char *filename) 490 const char *filename)
467{ 491{
468 struct map *map; 492 struct map *map = NULL;
469 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename); 493 struct dso *dso;
470 bool compressed; 494 struct kmod_path m;
471 495
472 if (dso == NULL) 496 if (kmod_path__parse_name(&m, filename))
473 return NULL; 497 return NULL;
474 498
475 map = map__new2(start, dso, MAP__FUNCTION); 499 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION,
476 if (map == NULL) 500 m.name);
477 return NULL; 501 if (map)
502 goto out;
478 503
479 if (machine__is_host(machine)) 504 dso = machine__module_dso(machine, &m, filename);
480 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; 505 if (dso == NULL)
481 else 506 goto out;
482 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
483 507
484 /* _KMODULE_COMP should be next to _KMODULE */ 508 map = map__new2(start, dso, MAP__FUNCTION);
485 if (is_kernel_module(filename, &compressed) && compressed) 509 if (map == NULL)
486 dso->symtab_type++; 510 goto out;
487 511
488 map_groups__insert(&machine->kmaps, map); 512 map_groups__insert(&machine->kmaps, map);
513
514out:
515 free(m.name);
489 return map; 516 return map;
490} 517}
491 518
@@ -650,6 +677,9 @@ int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
650 machine->vmlinux_maps[type]->unmap_ip = 677 machine->vmlinux_maps[type]->unmap_ip =
651 identity__map_ip; 678 identity__map_ip;
652 kmap = map__kmap(machine->vmlinux_maps[type]); 679 kmap = map__kmap(machine->vmlinux_maps[type]);
680 if (!kmap)
681 return -1;
682
653 kmap->kmaps = &machine->kmaps; 683 kmap->kmaps = &machine->kmaps;
654 map_groups__insert(&machine->kmaps, 684 map_groups__insert(&machine->kmaps,
655 machine->vmlinux_maps[type]); 685 machine->vmlinux_maps[type]);
@@ -671,7 +701,7 @@ void machine__destroy_kernel_maps(struct machine *machine)
671 kmap = map__kmap(machine->vmlinux_maps[type]); 701 kmap = map__kmap(machine->vmlinux_maps[type]);
672 map_groups__remove(&machine->kmaps, 702 map_groups__remove(&machine->kmaps,
673 machine->vmlinux_maps[type]); 703 machine->vmlinux_maps[type]);
674 if (kmap->ref_reloc_sym) { 704 if (kmap && kmap->ref_reloc_sym) {
675 /* 705 /*
676 * ref_reloc_sym is shared among all maps, so free just 706 * ref_reloc_sym is shared among all maps, so free just
677 * on one of them. 707 * on one of them.
@@ -827,6 +857,39 @@ static char *get_kernel_version(const char *root_dir)
827 return strdup(name); 857 return strdup(name);
828} 858}
829 859
860static bool is_kmod_dso(struct dso *dso)
861{
862 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
863 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
864}
865
866static int map_groups__set_module_path(struct map_groups *mg, const char *path,
867 struct kmod_path *m)
868{
869 struct map *map;
870 char *long_name;
871
872 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
873 if (map == NULL)
874 return 0;
875
876 long_name = strdup(path);
877 if (long_name == NULL)
878 return -ENOMEM;
879
880 dso__set_long_name(map->dso, long_name, true);
881 dso__kernel_module_get_build_id(map->dso, "");
882
883 /*
884 * Full name could reveal us kmod compression, so
885 * we need to update the symtab_type if needed.
886 */
887 if (m->comp && is_kmod_dso(map->dso))
888 map->dso->symtab_type++;
889
890 return 0;
891}
892
830static int map_groups__set_modules_path_dir(struct map_groups *mg, 893static int map_groups__set_modules_path_dir(struct map_groups *mg,
831 const char *dir_name, int depth) 894 const char *dir_name, int depth)
832{ 895{
@@ -865,35 +928,19 @@ static int map_groups__set_modules_path_dir(struct map_groups *mg,
865 if (ret < 0) 928 if (ret < 0)
866 goto out; 929 goto out;
867 } else { 930 } else {
868 char *dot = strrchr(dent->d_name, '.'), 931 struct kmod_path m;
869 dso_name[PATH_MAX];
870 struct map *map;
871 char *long_name;
872 932
873 if (dot == NULL) 933 ret = kmod_path__parse_name(&m, dent->d_name);
874 continue; 934 if (ret)
875 935 goto out;
876 /* On some system, modules are compressed like .ko.gz */
877 if (is_supported_compression(dot + 1) &&
878 is_kmodule_extension(dot - 2))
879 dot -= 3;
880 936
881 snprintf(dso_name, sizeof(dso_name), "[%.*s]", 937 if (m.kmod)
882 (int)(dot - dent->d_name), dent->d_name); 938 ret = map_groups__set_module_path(mg, path, &m);
883 939
884 strxfrchar(dso_name, '-', '_'); 940 free(m.name);
885 map = map_groups__find_by_name(mg, MAP__FUNCTION,
886 dso_name);
887 if (map == NULL)
888 continue;
889 941
890 long_name = strdup(path); 942 if (ret)
891 if (long_name == NULL) {
892 ret = -1;
893 goto out; 943 goto out;
894 }
895 dso__set_long_name(map->dso, long_name, true);
896 dso__kernel_module_get_build_id(map->dso, "");
897 } 944 }
898 } 945 }
899 946
@@ -1046,40 +1093,11 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
1046 strlen(kmmap_prefix) - 1) == 0; 1093 strlen(kmmap_prefix) - 1) == 0;
1047 if (event->mmap.filename[0] == '/' || 1094 if (event->mmap.filename[0] == '/' ||
1048 (!is_kernel_mmap && event->mmap.filename[0] == '[')) { 1095 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1049
1050 char short_module_name[1024];
1051 char *name, *dot;
1052
1053 if (event->mmap.filename[0] == '/') {
1054 name = strrchr(event->mmap.filename, '/');
1055 if (name == NULL)
1056 goto out_problem;
1057
1058 ++name; /* skip / */
1059 dot = strrchr(name, '.');
1060 if (dot == NULL)
1061 goto out_problem;
1062 /* On some system, modules are compressed like .ko.gz */
1063 if (is_supported_compression(dot + 1))
1064 dot -= 3;
1065 if (!is_kmodule_extension(dot + 1))
1066 goto out_problem;
1067 snprintf(short_module_name, sizeof(short_module_name),
1068 "[%.*s]", (int)(dot - name), name);
1069 strxfrchar(short_module_name, '-', '_');
1070 } else
1071 strcpy(short_module_name, event->mmap.filename);
1072
1073 map = machine__new_module(machine, event->mmap.start, 1096 map = machine__new_module(machine, event->mmap.start,
1074 event->mmap.filename); 1097 event->mmap.filename);
1075 if (map == NULL) 1098 if (map == NULL)
1076 goto out_problem; 1099 goto out_problem;
1077 1100
1078 name = strdup(short_module_name);
1079 if (name == NULL)
1080 goto out_problem;
1081
1082 dso__set_short_name(map->dso, name, true);
1083 map->end = map->start + event->mmap.len; 1101 map->end = map->start + event->mmap.len;
1084 } else if (is_kernel_mmap) { 1102 } else if (is_kernel_mmap) {
1085 const char *symbol_name = (event->mmap.filename + 1103 const char *symbol_name = (event->mmap.filename +
@@ -1092,7 +1110,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
1092 struct dso *dso; 1110 struct dso *dso;
1093 1111
1094 list_for_each_entry(dso, &machine->kernel_dsos.head, node) { 1112 list_for_each_entry(dso, &machine->kernel_dsos.head, node) {
1095 if (is_kernel_module(dso->long_name, NULL)) 1113 if (is_kernel_module(dso->long_name))
1096 continue; 1114 continue;
1097 1115
1098 kernel = dso; 1116 kernel = dso;
@@ -1236,15 +1254,19 @@ out_problem:
1236 return 0; 1254 return 0;
1237} 1255}
1238 1256
1239static void machine__remove_thread(struct machine *machine, struct thread *th) 1257void machine__remove_thread(struct machine *machine, struct thread *th)
1240{ 1258{
1241 machine->last_match = NULL; 1259 if (machine->last_match == th)
1260 thread__zput(machine->last_match);
1261
1242 rb_erase(&th->rb_node, &machine->threads); 1262 rb_erase(&th->rb_node, &machine->threads);
1243 /* 1263 /*
1244 * We may have references to this thread, for instance in some hist_entry 1264 * Move it first to the dead_threads list, then drop the reference,
1245 * instances, so just move them to a separate list. 1265 * if this is the last reference, then the thread__delete destructor
1266 * will be called and we will remove it from the dead_threads list.
1246 */ 1267 */
1247 list_add_tail(&th->node, &machine->dead_threads); 1268 list_add_tail(&th->node, &machine->dead_threads);
1269 thread__put(th);
1248} 1270}
1249 1271
1250int machine__process_fork_event(struct machine *machine, union perf_event *event, 1272int machine__process_fork_event(struct machine *machine, union perf_event *event,
@@ -1387,29 +1409,27 @@ struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1387static int add_callchain_ip(struct thread *thread, 1409static int add_callchain_ip(struct thread *thread,
1388 struct symbol **parent, 1410 struct symbol **parent,
1389 struct addr_location *root_al, 1411 struct addr_location *root_al,
1390 bool branch_history, 1412 u8 *cpumode,
1391 u64 ip) 1413 u64 ip)
1392{ 1414{
1393 struct addr_location al; 1415 struct addr_location al;
1394 1416
1395 al.filtered = 0; 1417 al.filtered = 0;
1396 al.sym = NULL; 1418 al.sym = NULL;
1397 if (branch_history) 1419 if (!cpumode) {
1398 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, 1420 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1399 ip, &al); 1421 ip, &al);
1400 else { 1422 } else {
1401 u8 cpumode = PERF_RECORD_MISC_USER;
1402
1403 if (ip >= PERF_CONTEXT_MAX) { 1423 if (ip >= PERF_CONTEXT_MAX) {
1404 switch (ip) { 1424 switch (ip) {
1405 case PERF_CONTEXT_HV: 1425 case PERF_CONTEXT_HV:
1406 cpumode = PERF_RECORD_MISC_HYPERVISOR; 1426 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
1407 break; 1427 break;
1408 case PERF_CONTEXT_KERNEL: 1428 case PERF_CONTEXT_KERNEL:
1409 cpumode = PERF_RECORD_MISC_KERNEL; 1429 *cpumode = PERF_RECORD_MISC_KERNEL;
1410 break; 1430 break;
1411 case PERF_CONTEXT_USER: 1431 case PERF_CONTEXT_USER:
1412 cpumode = PERF_RECORD_MISC_USER; 1432 *cpumode = PERF_RECORD_MISC_USER;
1413 break; 1433 break;
1414 default: 1434 default:
1415 pr_debug("invalid callchain context: " 1435 pr_debug("invalid callchain context: "
@@ -1423,8 +1443,8 @@ static int add_callchain_ip(struct thread *thread,
1423 } 1443 }
1424 return 0; 1444 return 0;
1425 } 1445 }
1426 thread__find_addr_location(thread, cpumode, MAP__FUNCTION, 1446 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1427 ip, &al); 1447 ip, &al);
1428 } 1448 }
1429 1449
1430 if (al.sym != NULL) { 1450 if (al.sym != NULL) {
@@ -1502,18 +1522,102 @@ static int remove_loops(struct branch_entry *l, int nr)
1502 return nr; 1522 return nr;
1503} 1523}
1504 1524
1505static int thread__resolve_callchain_sample(struct thread *thread, 1525/*
1506 struct ip_callchain *chain, 1526 * Recolve LBR callstack chain sample
1507 struct branch_stack *branch, 1527 * Return:
1508 struct symbol **parent, 1528 * 1 on success get LBR callchain information
1509 struct addr_location *root_al, 1529 * 0 no available LBR callchain information, should try fp
1510 int max_stack) 1530 * negative error code on other errors.
1531 */
1532static int resolve_lbr_callchain_sample(struct thread *thread,
1533 struct perf_sample *sample,
1534 struct symbol **parent,
1535 struct addr_location *root_al,
1536 int max_stack)
1511{ 1537{
1538 struct ip_callchain *chain = sample->callchain;
1512 int chain_nr = min(max_stack, (int)chain->nr); 1539 int chain_nr = min(max_stack, (int)chain->nr);
1540 u8 cpumode = PERF_RECORD_MISC_USER;
1541 int i, j, err;
1542 u64 ip;
1543
1544 for (i = 0; i < chain_nr; i++) {
1545 if (chain->ips[i] == PERF_CONTEXT_USER)
1546 break;
1547 }
1548
1549 /* LBR only affects the user callchain */
1550 if (i != chain_nr) {
1551 struct branch_stack *lbr_stack = sample->branch_stack;
1552 int lbr_nr = lbr_stack->nr;
1553 /*
1554 * LBR callstack can only get user call chain.
1555 * The mix_chain_nr is kernel call chain
1556 * number plus LBR user call chain number.
1557 * i is kernel call chain number,
1558 * 1 is PERF_CONTEXT_USER,
1559 * lbr_nr + 1 is the user call chain number.
1560 * For details, please refer to the comments
1561 * in callchain__printf
1562 */
1563 int mix_chain_nr = i + 1 + lbr_nr + 1;
1564
1565 if (mix_chain_nr > PERF_MAX_STACK_DEPTH + PERF_MAX_BRANCH_DEPTH) {
1566 pr_warning("corrupted callchain. skipping...\n");
1567 return 0;
1568 }
1569
1570 for (j = 0; j < mix_chain_nr; j++) {
1571 if (callchain_param.order == ORDER_CALLEE) {
1572 if (j < i + 1)
1573 ip = chain->ips[j];
1574 else if (j > i + 1)
1575 ip = lbr_stack->entries[j - i - 2].from;
1576 else
1577 ip = lbr_stack->entries[0].to;
1578 } else {
1579 if (j < lbr_nr)
1580 ip = lbr_stack->entries[lbr_nr - j - 1].from;
1581 else if (j > lbr_nr)
1582 ip = chain->ips[i + 1 - (j - lbr_nr)];
1583 else
1584 ip = lbr_stack->entries[0].to;
1585 }
1586
1587 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip);
1588 if (err)
1589 return (err < 0) ? err : 0;
1590 }
1591 return 1;
1592 }
1593
1594 return 0;
1595}
1596
1597static int thread__resolve_callchain_sample(struct thread *thread,
1598 struct perf_evsel *evsel,
1599 struct perf_sample *sample,
1600 struct symbol **parent,
1601 struct addr_location *root_al,
1602 int max_stack)
1603{
1604 struct branch_stack *branch = sample->branch_stack;
1605 struct ip_callchain *chain = sample->callchain;
1606 int chain_nr = min(max_stack, (int)chain->nr);
1607 u8 cpumode = PERF_RECORD_MISC_USER;
1513 int i, j, err; 1608 int i, j, err;
1514 int skip_idx = -1; 1609 int skip_idx = -1;
1515 int first_call = 0; 1610 int first_call = 0;
1516 1611
1612 callchain_cursor_reset(&callchain_cursor);
1613
1614 if (has_branch_callstack(evsel)) {
1615 err = resolve_lbr_callchain_sample(thread, sample, parent,
1616 root_al, max_stack);
1617 if (err)
1618 return (err < 0) ? err : 0;
1619 }
1620
1517 /* 1621 /*
1518 * Based on DWARF debug information, some architectures skip 1622 * Based on DWARF debug information, some architectures skip
1519 * a callchain entry saved by the kernel. 1623 * a callchain entry saved by the kernel.
@@ -1521,8 +1625,6 @@ static int thread__resolve_callchain_sample(struct thread *thread,
1521 if (chain->nr < PERF_MAX_STACK_DEPTH) 1625 if (chain->nr < PERF_MAX_STACK_DEPTH)
1522 skip_idx = arch_skip_callchain_idx(thread, chain); 1626 skip_idx = arch_skip_callchain_idx(thread, chain);
1523 1627
1524 callchain_cursor_reset(&callchain_cursor);
1525
1526 /* 1628 /*
1527 * Add branches to call stack for easier browsing. This gives 1629 * Add branches to call stack for easier browsing. This gives
1528 * more context for a sample than just the callers. 1630 * more context for a sample than just the callers.
@@ -1568,10 +1670,10 @@ static int thread__resolve_callchain_sample(struct thread *thread,
1568 1670
1569 for (i = 0; i < nr; i++) { 1671 for (i = 0; i < nr; i++) {
1570 err = add_callchain_ip(thread, parent, root_al, 1672 err = add_callchain_ip(thread, parent, root_al,
1571 true, be[i].to); 1673 NULL, be[i].to);
1572 if (!err) 1674 if (!err)
1573 err = add_callchain_ip(thread, parent, root_al, 1675 err = add_callchain_ip(thread, parent, root_al,
1574 true, be[i].from); 1676 NULL, be[i].from);
1575 if (err == -EINVAL) 1677 if (err == -EINVAL)
1576 break; 1678 break;
1577 if (err) 1679 if (err)
@@ -1600,7 +1702,7 @@ check_calls:
1600#endif 1702#endif
1601 ip = chain->ips[j]; 1703 ip = chain->ips[j];
1602 1704
1603 err = add_callchain_ip(thread, parent, root_al, false, ip); 1705 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip);
1604 1706
1605 if (err) 1707 if (err)
1606 return (err < 0) ? err : 0; 1708 return (err < 0) ? err : 0;
@@ -1623,9 +1725,9 @@ int thread__resolve_callchain(struct thread *thread,
1623 struct addr_location *root_al, 1725 struct addr_location *root_al,
1624 int max_stack) 1726 int max_stack)
1625{ 1727{
1626 int ret = thread__resolve_callchain_sample(thread, sample->callchain, 1728 int ret = thread__resolve_callchain_sample(thread, evsel,
1627 sample->branch_stack, 1729 sample, parent,
1628 parent, root_al, max_stack); 1730 root_al, max_stack);
1629 if (ret) 1731 if (ret)
1630 return ret; 1732 return ret;
1631 1733
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h
index e8b7779a0a3f..6d64cedb9d1e 100644
--- a/tools/perf/util/machine.h
+++ b/tools/perf/util/machine.h
@@ -118,9 +118,9 @@ void machines__set_comm_exec(struct machines *machines, bool comm_exec);
118struct machine *machine__new_host(void); 118struct machine *machine__new_host(void);
119int machine__init(struct machine *machine, const char *root_dir, pid_t pid); 119int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
120void machine__exit(struct machine *machine); 120void machine__exit(struct machine *machine);
121void machine__delete_dead_threads(struct machine *machine);
122void machine__delete_threads(struct machine *machine); 121void machine__delete_threads(struct machine *machine);
123void machine__delete(struct machine *machine); 122void machine__delete(struct machine *machine);
123void machine__remove_thread(struct machine *machine, struct thread *th);
124 124
125struct branch_info *sample__resolve_bstack(struct perf_sample *sample, 125struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
126 struct addr_location *al); 126 struct addr_location *al);
diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index 62ca9f2607d5..a14f08f41686 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -778,3 +778,23 @@ struct map *maps__next(struct map *map)
778 return rb_entry(next, struct map, rb_node); 778 return rb_entry(next, struct map, rb_node);
779 return NULL; 779 return NULL;
780} 780}
781
782struct kmap *map__kmap(struct map *map)
783{
784 if (!map->dso || !map->dso->kernel) {
785 pr_err("Internal error: map__kmap with a non-kernel map\n");
786 return NULL;
787 }
788 return (struct kmap *)(map + 1);
789}
790
791struct map_groups *map__kmaps(struct map *map)
792{
793 struct kmap *kmap = map__kmap(map);
794
795 if (!kmap || !kmap->kmaps) {
796 pr_err("Internal error: map__kmaps with a non-kernel map\n");
797 return NULL;
798 }
799 return kmap->kmaps;
800}
diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h
index 0e42438b1e59..ec19c59ca38e 100644
--- a/tools/perf/util/map.h
+++ b/tools/perf/util/map.h
@@ -76,10 +76,8 @@ static inline struct map_groups *map_groups__get(struct map_groups *mg)
76 76
77void map_groups__put(struct map_groups *mg); 77void map_groups__put(struct map_groups *mg);
78 78
79static inline struct kmap *map__kmap(struct map *map) 79struct kmap *map__kmap(struct map *map);
80{ 80struct map_groups *map__kmaps(struct map *map);
81 return (struct kmap *)(map + 1);
82}
83 81
84static inline u64 map__map_ip(struct map *map, u64 ip) 82static inline u64 map__map_ip(struct map *map, u64 ip)
85{ 83{
diff --git a/tools/perf/util/ordered-events.c b/tools/perf/util/ordered-events.c
index fd4be94125fb..52be201b9b25 100644
--- a/tools/perf/util/ordered-events.c
+++ b/tools/perf/util/ordered-events.c
@@ -2,7 +2,6 @@
2#include <linux/compiler.h> 2#include <linux/compiler.h>
3#include <linux/string.h> 3#include <linux/string.h>
4#include "ordered-events.h" 4#include "ordered-events.h"
5#include "evlist.h"
6#include "session.h" 5#include "session.h"
7#include "asm/bug.h" 6#include "asm/bug.h"
8#include "debug.h" 7#include "debug.h"
@@ -131,8 +130,8 @@ static struct ordered_event *alloc_event(struct ordered_events *oe,
131 return new; 130 return new;
132} 131}
133 132
134struct ordered_event * 133static struct ordered_event *
135ordered_events__new(struct ordered_events *oe, u64 timestamp, 134ordered_events__new_event(struct ordered_events *oe, u64 timestamp,
136 union perf_event *event) 135 union perf_event *event)
137{ 136{
138 struct ordered_event *new; 137 struct ordered_event *new;
@@ -153,20 +152,47 @@ void ordered_events__delete(struct ordered_events *oe, struct ordered_event *eve
153 free_dup_event(oe, event->event); 152 free_dup_event(oe, event->event);
154} 153}
155 154
156static int __ordered_events__flush(struct perf_session *s, 155int ordered_events__queue(struct ordered_events *oe, union perf_event *event,
157 struct perf_tool *tool) 156 struct perf_sample *sample, u64 file_offset)
157{
158 u64 timestamp = sample->time;
159 struct ordered_event *oevent;
160
161 if (!timestamp || timestamp == ~0ULL)
162 return -ETIME;
163
164 if (timestamp < oe->last_flush) {
165 pr_oe_time(timestamp, "out of order event\n");
166 pr_oe_time(oe->last_flush, "last flush, last_flush_type %d\n",
167 oe->last_flush_type);
168
169 oe->nr_unordered_events++;
170 }
171
172 oevent = ordered_events__new_event(oe, timestamp, event);
173 if (!oevent) {
174 ordered_events__flush(oe, OE_FLUSH__HALF);
175 oevent = ordered_events__new_event(oe, timestamp, event);
176 }
177
178 if (!oevent)
179 return -ENOMEM;
180
181 oevent->file_offset = file_offset;
182 return 0;
183}
184
185static int __ordered_events__flush(struct ordered_events *oe)
158{ 186{
159 struct ordered_events *oe = &s->ordered_events;
160 struct list_head *head = &oe->events; 187 struct list_head *head = &oe->events;
161 struct ordered_event *tmp, *iter; 188 struct ordered_event *tmp, *iter;
162 struct perf_sample sample;
163 u64 limit = oe->next_flush; 189 u64 limit = oe->next_flush;
164 u64 last_ts = oe->last ? oe->last->timestamp : 0ULL; 190 u64 last_ts = oe->last ? oe->last->timestamp : 0ULL;
165 bool show_progress = limit == ULLONG_MAX; 191 bool show_progress = limit == ULLONG_MAX;
166 struct ui_progress prog; 192 struct ui_progress prog;
167 int ret; 193 int ret;
168 194
169 if (!tool->ordered_events || !limit) 195 if (!limit)
170 return 0; 196 return 0;
171 197
172 if (show_progress) 198 if (show_progress)
@@ -178,16 +204,9 @@ static int __ordered_events__flush(struct perf_session *s,
178 204
179 if (iter->timestamp > limit) 205 if (iter->timestamp > limit)
180 break; 206 break;
181 207 ret = oe->deliver(oe, iter);
182 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
183 if (ret) 208 if (ret)
184 pr_err("Can't parse sample, err = %d\n", ret); 209 return ret;
185 else {
186 ret = perf_session__deliver_event(s, iter->event, &sample, tool,
187 iter->file_offset);
188 if (ret)
189 return ret;
190 }
191 210
192 ordered_events__delete(oe, iter); 211 ordered_events__delete(oe, iter);
193 oe->last_flush = iter->timestamp; 212 oe->last_flush = iter->timestamp;
@@ -204,10 +223,8 @@ static int __ordered_events__flush(struct perf_session *s,
204 return 0; 223 return 0;
205} 224}
206 225
207int ordered_events__flush(struct perf_session *s, struct perf_tool *tool, 226int ordered_events__flush(struct ordered_events *oe, enum oe_flush how)
208 enum oe_flush how)
209{ 227{
210 struct ordered_events *oe = &s->ordered_events;
211 static const char * const str[] = { 228 static const char * const str[] = {
212 "NONE", 229 "NONE",
213 "FINAL", 230 "FINAL",
@@ -216,6 +233,9 @@ int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
216 }; 233 };
217 int err; 234 int err;
218 235
236 if (oe->nr_events == 0)
237 return 0;
238
219 switch (how) { 239 switch (how) {
220 case OE_FLUSH__FINAL: 240 case OE_FLUSH__FINAL:
221 oe->next_flush = ULLONG_MAX; 241 oe->next_flush = ULLONG_MAX;
@@ -248,7 +268,7 @@ int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
248 str[how], oe->nr_events); 268 str[how], oe->nr_events);
249 pr_oe_time(oe->max_timestamp, "max_timestamp\n"); 269 pr_oe_time(oe->max_timestamp, "max_timestamp\n");
250 270
251 err = __ordered_events__flush(s, tool); 271 err = __ordered_events__flush(oe);
252 272
253 if (!err) { 273 if (!err) {
254 if (how == OE_FLUSH__ROUND) 274 if (how == OE_FLUSH__ROUND)
@@ -264,13 +284,14 @@ int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
264 return err; 284 return err;
265} 285}
266 286
267void ordered_events__init(struct ordered_events *oe) 287void ordered_events__init(struct ordered_events *oe, ordered_events__deliver_t deliver)
268{ 288{
269 INIT_LIST_HEAD(&oe->events); 289 INIT_LIST_HEAD(&oe->events);
270 INIT_LIST_HEAD(&oe->cache); 290 INIT_LIST_HEAD(&oe->cache);
271 INIT_LIST_HEAD(&oe->to_free); 291 INIT_LIST_HEAD(&oe->to_free);
272 oe->max_alloc_size = (u64) -1; 292 oe->max_alloc_size = (u64) -1;
273 oe->cur_alloc_size = 0; 293 oe->cur_alloc_size = 0;
294 oe->deliver = deliver;
274} 295}
275 296
276void ordered_events__free(struct ordered_events *oe) 297void ordered_events__free(struct ordered_events *oe)
diff --git a/tools/perf/util/ordered-events.h b/tools/perf/util/ordered-events.h
index 7b8f9b011f38..f403991e3bfd 100644
--- a/tools/perf/util/ordered-events.h
+++ b/tools/perf/util/ordered-events.h
@@ -2,9 +2,8 @@
2#define __ORDERED_EVENTS_H 2#define __ORDERED_EVENTS_H
3 3
4#include <linux/types.h> 4#include <linux/types.h>
5#include "tool.h"
6 5
7struct perf_session; 6struct perf_sample;
8 7
9struct ordered_event { 8struct ordered_event {
10 u64 timestamp; 9 u64 timestamp;
@@ -20,6 +19,11 @@ enum oe_flush {
20 OE_FLUSH__HALF, 19 OE_FLUSH__HALF,
21}; 20};
22 21
22struct ordered_events;
23
24typedef int (*ordered_events__deliver_t)(struct ordered_events *oe,
25 struct ordered_event *event);
26
23struct ordered_events { 27struct ordered_events {
24 u64 last_flush; 28 u64 last_flush;
25 u64 next_flush; 29 u64 next_flush;
@@ -31,18 +35,19 @@ struct ordered_events {
31 struct list_head to_free; 35 struct list_head to_free;
32 struct ordered_event *buffer; 36 struct ordered_event *buffer;
33 struct ordered_event *last; 37 struct ordered_event *last;
38 ordered_events__deliver_t deliver;
34 int buffer_idx; 39 int buffer_idx;
35 unsigned int nr_events; 40 unsigned int nr_events;
36 enum oe_flush last_flush_type; 41 enum oe_flush last_flush_type;
42 u32 nr_unordered_events;
37 bool copy_on_queue; 43 bool copy_on_queue;
38}; 44};
39 45
40struct ordered_event *ordered_events__new(struct ordered_events *oe, u64 timestamp, 46int ordered_events__queue(struct ordered_events *oe, union perf_event *event,
41 union perf_event *event); 47 struct perf_sample *sample, u64 file_offset);
42void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event); 48void ordered_events__delete(struct ordered_events *oe, struct ordered_event *event);
43int ordered_events__flush(struct perf_session *s, struct perf_tool *tool, 49int ordered_events__flush(struct ordered_events *oe, enum oe_flush how);
44 enum oe_flush how); 50void ordered_events__init(struct ordered_events *oe, ordered_events__deliver_t deliver);
45void ordered_events__init(struct ordered_events *oe);
46void ordered_events__free(struct ordered_events *oe); 51void ordered_events__free(struct ordered_events *oe);
47 52
48static inline 53static inline
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 7f8ec6ce2823..be0655388b38 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -20,11 +20,6 @@
20 20
21#define MAX_NAME_LEN 100 21#define MAX_NAME_LEN 100
22 22
23struct event_symbol {
24 const char *symbol;
25 const char *alias;
26};
27
28#ifdef PARSER_DEBUG 23#ifdef PARSER_DEBUG
29extern int parse_events_debug; 24extern int parse_events_debug;
30#endif 25#endif
@@ -39,7 +34,7 @@ static struct perf_pmu_event_symbol *perf_pmu_events_list;
39 */ 34 */
40static int perf_pmu_events_list_num; 35static int perf_pmu_events_list_num;
41 36
42static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 37struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
43 [PERF_COUNT_HW_CPU_CYCLES] = { 38 [PERF_COUNT_HW_CPU_CYCLES] = {
44 .symbol = "cpu-cycles", 39 .symbol = "cpu-cycles",
45 .alias = "cycles", 40 .alias = "cycles",
@@ -82,7 +77,7 @@ static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
82 }, 77 },
83}; 78};
84 79
85static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 80struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
86 [PERF_COUNT_SW_CPU_CLOCK] = { 81 [PERF_COUNT_SW_CPU_CLOCK] = {
87 .symbol = "cpu-clock", 82 .symbol = "cpu-clock",
88 .alias = "", 83 .alias = "",
@@ -175,9 +170,6 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
175 char evt_path[MAXPATHLEN]; 170 char evt_path[MAXPATHLEN];
176 char dir_path[MAXPATHLEN]; 171 char dir_path[MAXPATHLEN];
177 172
178 if (debugfs_valid_mountpoint(tracing_events_path))
179 return NULL;
180
181 sys_dir = opendir(tracing_events_path); 173 sys_dir = opendir(tracing_events_path);
182 if (!sys_dir) 174 if (!sys_dir)
183 return NULL; 175 return NULL;
@@ -473,12 +465,6 @@ static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
473int parse_events_add_tracepoint(struct list_head *list, int *idx, 465int parse_events_add_tracepoint(struct list_head *list, int *idx,
474 char *sys, char *event) 466 char *sys, char *event)
475{ 467{
476 int ret;
477
478 ret = debugfs_valid_mountpoint(tracing_events_path);
479 if (ret)
480 return ret;
481
482 if (strpbrk(sys, "*?")) 468 if (strpbrk(sys, "*?"))
483 return add_tracepoint_multi_sys(list, idx, sys, event); 469 return add_tracepoint_multi_sys(list, idx, sys, event);
484 else 470 else
@@ -723,6 +709,7 @@ struct event_modifier {
723 int eh; 709 int eh;
724 int eH; 710 int eH;
725 int eG; 711 int eG;
712 int eI;
726 int precise; 713 int precise;
727 int exclude_GH; 714 int exclude_GH;
728 int sample_read; 715 int sample_read;
@@ -737,6 +724,7 @@ static int get_event_modifier(struct event_modifier *mod, char *str,
737 int eh = evsel ? evsel->attr.exclude_hv : 0; 724 int eh = evsel ? evsel->attr.exclude_hv : 0;
738 int eH = evsel ? evsel->attr.exclude_host : 0; 725 int eH = evsel ? evsel->attr.exclude_host : 0;
739 int eG = evsel ? evsel->attr.exclude_guest : 0; 726 int eG = evsel ? evsel->attr.exclude_guest : 0;
727 int eI = evsel ? evsel->attr.exclude_idle : 0;
740 int precise = evsel ? evsel->attr.precise_ip : 0; 728 int precise = evsel ? evsel->attr.precise_ip : 0;
741 int sample_read = 0; 729 int sample_read = 0;
742 int pinned = evsel ? evsel->attr.pinned : 0; 730 int pinned = evsel ? evsel->attr.pinned : 0;
@@ -767,6 +755,8 @@ static int get_event_modifier(struct event_modifier *mod, char *str,
767 if (!exclude_GH) 755 if (!exclude_GH)
768 exclude_GH = eG = eH = 1; 756 exclude_GH = eG = eH = 1;
769 eH = 0; 757 eH = 0;
758 } else if (*str == 'I') {
759 eI = 1;
770 } else if (*str == 'p') { 760 } else if (*str == 'p') {
771 precise++; 761 precise++;
772 /* use of precise requires exclude_guest */ 762 /* use of precise requires exclude_guest */
@@ -800,6 +790,7 @@ static int get_event_modifier(struct event_modifier *mod, char *str,
800 mod->eh = eh; 790 mod->eh = eh;
801 mod->eH = eH; 791 mod->eH = eH;
802 mod->eG = eG; 792 mod->eG = eG;
793 mod->eI = eI;
803 mod->precise = precise; 794 mod->precise = precise;
804 mod->exclude_GH = exclude_GH; 795 mod->exclude_GH = exclude_GH;
805 mod->sample_read = sample_read; 796 mod->sample_read = sample_read;
@@ -817,7 +808,7 @@ static int check_modifier(char *str)
817 char *p = str; 808 char *p = str;
818 809
819 /* The sizeof includes 0 byte as well. */ 810 /* The sizeof includes 0 byte as well. */
820 if (strlen(str) > (sizeof("ukhGHpppSD") - 1)) 811 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
821 return -1; 812 return -1;
822 813
823 while (*p) { 814 while (*p) {
@@ -853,6 +844,7 @@ int parse_events__modifier_event(struct list_head *list, char *str, bool add)
853 evsel->attr.precise_ip = mod.precise; 844 evsel->attr.precise_ip = mod.precise;
854 evsel->attr.exclude_host = mod.eH; 845 evsel->attr.exclude_host = mod.eH;
855 evsel->attr.exclude_guest = mod.eG; 846 evsel->attr.exclude_guest = mod.eG;
847 evsel->attr.exclude_idle = mod.eI;
856 evsel->exclude_GH = mod.exclude_GH; 848 evsel->exclude_GH = mod.exclude_GH;
857 evsel->sample_read = mod.sample_read; 849 evsel->sample_read = mod.sample_read;
858 850
@@ -1098,6 +1090,14 @@ static const char * const event_type_descriptors[] = {
1098 "Hardware breakpoint", 1090 "Hardware breakpoint",
1099}; 1091};
1100 1092
1093static int cmp_string(const void *a, const void *b)
1094{
1095 const char * const *as = a;
1096 const char * const *bs = b;
1097
1098 return strcmp(*as, *bs);
1099}
1100
1101/* 1101/*
1102 * Print the events from <debugfs_mount_point>/tracing/events 1102 * Print the events from <debugfs_mount_point>/tracing/events
1103 */ 1103 */
@@ -1109,18 +1109,21 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1109 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1109 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1110 char evt_path[MAXPATHLEN]; 1110 char evt_path[MAXPATHLEN];
1111 char dir_path[MAXPATHLEN]; 1111 char dir_path[MAXPATHLEN];
1112 char sbuf[STRERR_BUFSIZE]; 1112 char **evt_list = NULL;
1113 1113 unsigned int evt_i = 0, evt_num = 0;
1114 if (debugfs_valid_mountpoint(tracing_events_path)) { 1114 bool evt_num_known = false;
1115 printf(" [ Tracepoints not available: %s ]\n",
1116 strerror_r(errno, sbuf, sizeof(sbuf)));
1117 return;
1118 }
1119 1115
1116restart:
1120 sys_dir = opendir(tracing_events_path); 1117 sys_dir = opendir(tracing_events_path);
1121 if (!sys_dir) 1118 if (!sys_dir)
1122 return; 1119 return;
1123 1120
1121 if (evt_num_known) {
1122 evt_list = zalloc(sizeof(char *) * evt_num);
1123 if (!evt_list)
1124 goto out_close_sys_dir;
1125 }
1126
1124 for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1127 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1125 if (subsys_glob != NULL && 1128 if (subsys_glob != NULL &&
1126 !strglobmatch(sys_dirent.d_name, subsys_glob)) 1129 !strglobmatch(sys_dirent.d_name, subsys_glob))
@@ -1137,19 +1140,56 @@ void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1137 !strglobmatch(evt_dirent.d_name, event_glob)) 1140 !strglobmatch(evt_dirent.d_name, event_glob))
1138 continue; 1141 continue;
1139 1142
1140 if (name_only) { 1143 if (!evt_num_known) {
1141 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name); 1144 evt_num++;
1142 continue; 1145 continue;
1143 } 1146 }
1144 1147
1145 snprintf(evt_path, MAXPATHLEN, "%s:%s", 1148 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1146 sys_dirent.d_name, evt_dirent.d_name); 1149 sys_dirent.d_name, evt_dirent.d_name);
1147 printf(" %-50s [%s]\n", evt_path, 1150
1148 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1151 evt_list[evt_i] = strdup(evt_path);
1152 if (evt_list[evt_i] == NULL)
1153 goto out_close_evt_dir;
1154 evt_i++;
1149 } 1155 }
1150 closedir(evt_dir); 1156 closedir(evt_dir);
1151 } 1157 }
1152 closedir(sys_dir); 1158 closedir(sys_dir);
1159
1160 if (!evt_num_known) {
1161 evt_num_known = true;
1162 goto restart;
1163 }
1164 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1165 evt_i = 0;
1166 while (evt_i < evt_num) {
1167 if (name_only) {
1168 printf("%s ", evt_list[evt_i++]);
1169 continue;
1170 }
1171 printf(" %-50s [%s]\n", evt_list[evt_i++],
1172 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1173 }
1174 if (evt_num)
1175 printf("\n");
1176
1177out_free:
1178 evt_num = evt_i;
1179 for (evt_i = 0; evt_i < evt_num; evt_i++)
1180 zfree(&evt_list[evt_i]);
1181 zfree(&evt_list);
1182 return;
1183
1184out_close_evt_dir:
1185 closedir(evt_dir);
1186out_close_sys_dir:
1187 closedir(sys_dir);
1188
1189 printf("FATAL: not enough memory to print %s\n",
1190 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1191 if (evt_list)
1192 goto out_free;
1153} 1193}
1154 1194
1155/* 1195/*
@@ -1163,9 +1203,6 @@ int is_valid_tracepoint(const char *event_string)
1163 char evt_path[MAXPATHLEN]; 1203 char evt_path[MAXPATHLEN];
1164 char dir_path[MAXPATHLEN]; 1204 char dir_path[MAXPATHLEN];
1165 1205
1166 if (debugfs_valid_mountpoint(tracing_events_path))
1167 return 0;
1168
1169 sys_dir = opendir(tracing_events_path); 1206 sys_dir = opendir(tracing_events_path);
1170 if (!sys_dir) 1207 if (!sys_dir)
1171 return 0; 1208 return 0;
@@ -1233,38 +1270,19 @@ static bool is_event_supported(u8 type, unsigned config)
1233 return ret; 1270 return ret;
1234} 1271}
1235 1272
1236static void __print_events_type(u8 type, struct event_symbol *syms,
1237 unsigned max)
1238{
1239 char name[64];
1240 unsigned i;
1241
1242 for (i = 0; i < max ; i++, syms++) {
1243 if (!is_event_supported(type, i))
1244 continue;
1245
1246 if (strlen(syms->alias))
1247 snprintf(name, sizeof(name), "%s OR %s",
1248 syms->symbol, syms->alias);
1249 else
1250 snprintf(name, sizeof(name), "%s", syms->symbol);
1251
1252 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1253 }
1254}
1255
1256void print_events_type(u8 type)
1257{
1258 if (type == PERF_TYPE_SOFTWARE)
1259 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1260 else
1261 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1262}
1263
1264int print_hwcache_events(const char *event_glob, bool name_only) 1273int print_hwcache_events(const char *event_glob, bool name_only)
1265{ 1274{
1266 unsigned int type, op, i, printed = 0; 1275 unsigned int type, op, i, evt_i = 0, evt_num = 0;
1267 char name[64]; 1276 char name[64];
1277 char **evt_list = NULL;
1278 bool evt_num_known = false;
1279
1280restart:
1281 if (evt_num_known) {
1282 evt_list = zalloc(sizeof(char *) * evt_num);
1283 if (!evt_list)
1284 goto out_enomem;
1285 }
1268 1286
1269 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 1287 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1270 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 1288 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
@@ -1282,27 +1300,66 @@ int print_hwcache_events(const char *event_glob, bool name_only)
1282 type | (op << 8) | (i << 16))) 1300 type | (op << 8) | (i << 16)))
1283 continue; 1301 continue;
1284 1302
1285 if (name_only) 1303 if (!evt_num_known) {
1286 printf("%s ", name); 1304 evt_num++;
1287 else 1305 continue;
1288 printf(" %-50s [%s]\n", name, 1306 }
1289 event_type_descriptors[PERF_TYPE_HW_CACHE]); 1307
1290 ++printed; 1308 evt_list[evt_i] = strdup(name);
1309 if (evt_list[evt_i] == NULL)
1310 goto out_enomem;
1311 evt_i++;
1291 } 1312 }
1292 } 1313 }
1293 } 1314 }
1294 1315
1295 if (printed) 1316 if (!evt_num_known) {
1317 evt_num_known = true;
1318 goto restart;
1319 }
1320 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1321 evt_i = 0;
1322 while (evt_i < evt_num) {
1323 if (name_only) {
1324 printf("%s ", evt_list[evt_i++]);
1325 continue;
1326 }
1327 printf(" %-50s [%s]\n", evt_list[evt_i++],
1328 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1329 }
1330 if (evt_num)
1296 printf("\n"); 1331 printf("\n");
1297 return printed; 1332
1333out_free:
1334 evt_num = evt_i;
1335 for (evt_i = 0; evt_i < evt_num; evt_i++)
1336 zfree(&evt_list[evt_i]);
1337 zfree(&evt_list);
1338 return evt_num;
1339
1340out_enomem:
1341 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1342 if (evt_list)
1343 goto out_free;
1344 return evt_num;
1298} 1345}
1299 1346
1300static void print_symbol_events(const char *event_glob, unsigned type, 1347void print_symbol_events(const char *event_glob, unsigned type,
1301 struct event_symbol *syms, unsigned max, 1348 struct event_symbol *syms, unsigned max,
1302 bool name_only) 1349 bool name_only)
1303{ 1350{
1304 unsigned i, printed = 0; 1351 unsigned int i, evt_i = 0, evt_num = 0;
1305 char name[MAX_NAME_LEN]; 1352 char name[MAX_NAME_LEN];
1353 char **evt_list = NULL;
1354 bool evt_num_known = false;
1355
1356restart:
1357 if (evt_num_known) {
1358 evt_list = zalloc(sizeof(char *) * evt_num);
1359 if (!evt_list)
1360 goto out_enomem;
1361 syms -= max;
1362 }
1306 1363
1307 for (i = 0; i < max; i++, syms++) { 1364 for (i = 0; i < max; i++, syms++) {
1308 1365
@@ -1314,23 +1371,49 @@ static void print_symbol_events(const char *event_glob, unsigned type,
1314 if (!is_event_supported(type, i)) 1371 if (!is_event_supported(type, i))
1315 continue; 1372 continue;
1316 1373
1317 if (name_only) { 1374 if (!evt_num_known) {
1318 printf("%s ", syms->symbol); 1375 evt_num++;
1319 continue; 1376 continue;
1320 } 1377 }
1321 1378
1322 if (strlen(syms->alias)) 1379 if (!name_only && strlen(syms->alias))
1323 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 1380 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1324 else 1381 else
1325 strncpy(name, syms->symbol, MAX_NAME_LEN); 1382 strncpy(name, syms->symbol, MAX_NAME_LEN);
1326 1383
1327 printf(" %-50s [%s]\n", name, event_type_descriptors[type]); 1384 evt_list[evt_i] = strdup(name);
1328 1385 if (evt_list[evt_i] == NULL)
1329 printed++; 1386 goto out_enomem;
1387 evt_i++;
1330 } 1388 }
1331 1389
1332 if (printed) 1390 if (!evt_num_known) {
1391 evt_num_known = true;
1392 goto restart;
1393 }
1394 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1395 evt_i = 0;
1396 while (evt_i < evt_num) {
1397 if (name_only) {
1398 printf("%s ", evt_list[evt_i++]);
1399 continue;
1400 }
1401 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1402 }
1403 if (evt_num)
1333 printf("\n"); 1404 printf("\n");
1405
1406out_free:
1407 evt_num = evt_i;
1408 for (evt_i = 0; evt_i < evt_num; evt_i++)
1409 zfree(&evt_list[evt_i]);
1410 zfree(&evt_list);
1411 return;
1412
1413out_enomem:
1414 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1415 if (evt_list)
1416 goto out_free;
1334} 1417}
1335 1418
1336/* 1419/*
@@ -1338,11 +1421,6 @@ static void print_symbol_events(const char *event_glob, unsigned type,
1338 */ 1421 */
1339void print_events(const char *event_glob, bool name_only) 1422void print_events(const char *event_glob, bool name_only)
1340{ 1423{
1341 if (!name_only) {
1342 printf("\n");
1343 printf("List of pre-defined events (to be used in -e):\n");
1344 }
1345
1346 print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 1424 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1347 event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 1425 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1348 1426
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index ff6e1fa4111e..52a2dda4f954 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -116,12 +116,21 @@ void parse_events_update_lists(struct list_head *list_event,
116void parse_events_error(void *data, void *scanner, char const *msg); 116void parse_events_error(void *data, void *scanner, char const *msg);
117 117
118void print_events(const char *event_glob, bool name_only); 118void print_events(const char *event_glob, bool name_only);
119void print_events_type(u8 type); 119
120struct event_symbol {
121 const char *symbol;
122 const char *alias;
123};
124extern struct event_symbol event_symbols_hw[];
125extern struct event_symbol event_symbols_sw[];
126void print_symbol_events(const char *event_glob, unsigned type,
127 struct event_symbol *syms, unsigned max,
128 bool name_only);
120void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 129void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
121 bool name_only); 130 bool name_only);
122int print_hwcache_events(const char *event_glob, bool name_only); 131int print_hwcache_events(const char *event_glob, bool name_only);
123extern int is_valid_tracepoint(const char *event_string); 132extern int is_valid_tracepoint(const char *event_string);
124 133
125extern int valid_debugfs_mount(const char *debugfs); 134int valid_event_mount(const char *eventfs);
126 135
127#endif /* __PERF_PARSE_EVENTS_H */ 136#endif /* __PERF_PARSE_EVENTS_H */
diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
index 94eacb6c1ef7..8895cf3132ab 100644
--- a/tools/perf/util/parse-events.l
+++ b/tools/perf/util/parse-events.l
@@ -101,7 +101,7 @@ num_raw_hex [a-fA-F0-9]+
101name [a-zA-Z_*?][a-zA-Z0-9_*?]* 101name [a-zA-Z_*?][a-zA-Z0-9_*?]*
102name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?]* 102name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?]*
103/* If you add a modifier you need to update check_modifier() */ 103/* If you add a modifier you need to update check_modifier() */
104modifier_event [ukhpGHSD]+ 104modifier_event [ukhpGHSDI]+
105modifier_bp [rwx]{1,3} 105modifier_bp [rwx]{1,3}
106 106
107%% 107%%
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index 4a015f77e2b5..01626be2a8eb 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -37,6 +37,7 @@ static int get_value(struct parse_opt_ctx_t *p,
37{ 37{
38 const char *s, *arg = NULL; 38 const char *s, *arg = NULL;
39 const int unset = flags & OPT_UNSET; 39 const int unset = flags & OPT_UNSET;
40 int err;
40 41
41 if (unset && p->opt) 42 if (unset && p->opt)
42 return opterror(opt, "takes no value", flags); 43 return opterror(opt, "takes no value", flags);
@@ -114,13 +115,29 @@ static int get_value(struct parse_opt_ctx_t *p,
114 return 0; 115 return 0;
115 116
116 case OPTION_STRING: 117 case OPTION_STRING:
118 err = 0;
117 if (unset) 119 if (unset)
118 *(const char **)opt->value = NULL; 120 *(const char **)opt->value = NULL;
119 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) 121 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
120 *(const char **)opt->value = (const char *)opt->defval; 122 *(const char **)opt->value = (const char *)opt->defval;
121 else 123 else
122 return get_arg(p, opt, flags, (const char **)opt->value); 124 err = get_arg(p, opt, flags, (const char **)opt->value);
123 return 0; 125
126 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
127 if (opt->flags & PARSE_OPT_NOEMPTY) {
128 const char *val = *(const char **)opt->value;
129
130 if (!val)
131 return err;
132
133 /* Similar to unset if we are given an empty string. */
134 if (val[0] == '\0') {
135 *(const char **)opt->value = NULL;
136 return 0;
137 }
138 }
139
140 return err;
124 141
125 case OPTION_CALLBACK: 142 case OPTION_CALLBACK:
126 if (unset) 143 if (unset)
@@ -505,13 +522,18 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
505 break; 522 break;
506 case PARSE_OPT_LIST_OPTS: 523 case PARSE_OPT_LIST_OPTS:
507 while (options->type != OPTION_END) { 524 while (options->type != OPTION_END) {
508 printf("--%s ", options->long_name); 525 if (options->long_name)
526 printf("--%s ", options->long_name);
509 options++; 527 options++;
510 } 528 }
529 putchar('\n');
511 exit(130); 530 exit(130);
512 case PARSE_OPT_LIST_SUBCMDS: 531 case PARSE_OPT_LIST_SUBCMDS:
513 for (int i = 0; subcommands[i]; i++) 532 if (subcommands) {
514 printf("%s ", subcommands[i]); 533 for (int i = 0; subcommands[i]; i++)
534 printf("%s ", subcommands[i]);
535 }
536 putchar('\n');
515 exit(130); 537 exit(130);
516 default: /* PARSE_OPT_UNKNOWN */ 538 default: /* PARSE_OPT_UNKNOWN */
517 if (ctx.argv[0][1] == '-') { 539 if (ctx.argv[0][1] == '-') {
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index 97b153fb4999..59561fd86278 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -40,6 +40,7 @@ enum parse_opt_option_flags {
40 PARSE_OPT_LASTARG_DEFAULT = 16, 40 PARSE_OPT_LASTARG_DEFAULT = 16,
41 PARSE_OPT_DISABLED = 32, 41 PARSE_OPT_DISABLED = 32,
42 PARSE_OPT_EXCLUSIVE = 64, 42 PARSE_OPT_EXCLUSIVE = 64,
43 PARSE_OPT_NOEMPTY = 128,
43}; 44};
44 45
45struct option; 46struct option;
@@ -122,6 +123,7 @@ struct option {
122#define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) } 123#define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = check_vtype(v, long *), .help = (h) }
123#define OPT_U64(s, l, v, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) } 124#define OPT_U64(s, l, v, h) { .type = OPTION_U64, .short_name = (s), .long_name = (l), .value = check_vtype(v, u64 *), .help = (h) }
124#define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h) } 125#define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h) }
126#define OPT_STRING_NOEMPTY(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = check_vtype(v, const char **), (a), .help = (h), .flags = PARSE_OPT_NOEMPTY}
125#define OPT_DATE(s, l, v, h) \ 127#define OPT_DATE(s, l, v, h) \
126 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb } 128 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
127#define OPT_CALLBACK(s, l, v, a, h, f) \ 129#define OPT_CALLBACK(s, l, v, a, h, f) \
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 919937eb0be2..30545ce2c712 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -41,6 +41,7 @@
41#include "symbol.h" 41#include "symbol.h"
42#include "thread.h" 42#include "thread.h"
43#include <api/fs/debugfs.h> 43#include <api/fs/debugfs.h>
44#include <api/fs/tracefs.h>
44#include "trace-event.h" /* For __maybe_unused */ 45#include "trace-event.h" /* For __maybe_unused */
45#include "probe-event.h" 46#include "probe-event.h"
46#include "probe-finder.h" 47#include "probe-finder.h"
@@ -79,6 +80,7 @@ static int init_symbol_maps(bool user_only)
79 int ret; 80 int ret;
80 81
81 symbol_conf.sort_by_name = true; 82 symbol_conf.sort_by_name = true;
83 symbol_conf.allow_aliases = true;
82 ret = symbol__init(NULL); 84 ret = symbol__init(NULL);
83 if (ret < 0) { 85 if (ret < 0) {
84 pr_debug("Failed to init symbol map.\n"); 86 pr_debug("Failed to init symbol map.\n");
@@ -133,6 +135,8 @@ static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
133 return NULL; 135 return NULL;
134 136
135 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]); 137 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
138 if (!kmap)
139 return NULL;
136 return kmap->ref_reloc_sym; 140 return kmap->ref_reloc_sym;
137} 141}
138 142
@@ -150,7 +154,7 @@ static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
150 sym = __find_kernel_function_by_name(name, &map); 154 sym = __find_kernel_function_by_name(name, &map);
151 if (sym) 155 if (sym)
152 return map->unmap_ip(map, sym->start) - 156 return map->unmap_ip(map, sym->start) -
153 (reloc) ? 0 : map->reloc; 157 ((reloc) ? 0 : map->reloc);
154 } 158 }
155 return 0; 159 return 0;
156} 160}
@@ -177,6 +181,25 @@ static struct map *kernel_get_module_map(const char *module)
177 return NULL; 181 return NULL;
178} 182}
179 183
184static struct map *get_target_map(const char *target, bool user)
185{
186 /* Init maps of given executable or kernel */
187 if (user)
188 return dso__new_map(target);
189 else
190 return kernel_get_module_map(target);
191}
192
193static void put_target_map(struct map *map, bool user)
194{
195 if (map && user) {
196 /* Only the user map needs to be released */
197 dso__delete(map->dso);
198 map__delete(map);
199 }
200}
201
202
180static struct dso *kernel_get_module_dso(const char *module) 203static struct dso *kernel_get_module_dso(const char *module)
181{ 204{
182 struct dso *dso; 205 struct dso *dso;
@@ -248,6 +271,13 @@ out:
248 return ret; 271 return ret;
249} 272}
250 273
274static void clear_perf_probe_point(struct perf_probe_point *pp)
275{
276 free(pp->file);
277 free(pp->function);
278 free(pp->lazy_line);
279}
280
251static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) 281static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
252{ 282{
253 int i; 283 int i;
@@ -257,6 +287,103 @@ static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
257} 287}
258 288
259#ifdef HAVE_DWARF_SUPPORT 289#ifdef HAVE_DWARF_SUPPORT
290/*
291 * Some binaries like glibc have special symbols which are on the symbol
292 * table, but not in the debuginfo. If we can find the address of the
293 * symbol from map, we can translate the address back to the probe point.
294 */
295static int find_alternative_probe_point(struct debuginfo *dinfo,
296 struct perf_probe_point *pp,
297 struct perf_probe_point *result,
298 const char *target, bool uprobes)
299{
300 struct map *map = NULL;
301 struct symbol *sym;
302 u64 address = 0;
303 int ret = -ENOENT;
304
305 /* This can work only for function-name based one */
306 if (!pp->function || pp->file)
307 return -ENOTSUP;
308
309 map = get_target_map(target, uprobes);
310 if (!map)
311 return -EINVAL;
312
313 /* Find the address of given function */
314 map__for_each_symbol_by_name(map, pp->function, sym) {
315 if (uprobes)
316 address = sym->start;
317 else
318 address = map->unmap_ip(map, sym->start);
319 break;
320 }
321 if (!address) {
322 ret = -ENOENT;
323 goto out;
324 }
325 pr_debug("Symbol %s address found : %" PRIx64 "\n",
326 pp->function, address);
327
328 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
329 result);
330 if (ret <= 0)
331 ret = (!ret) ? -ENOENT : ret;
332 else {
333 result->offset += pp->offset;
334 result->line += pp->line;
335 ret = 0;
336 }
337
338out:
339 put_target_map(map, uprobes);
340 return ret;
341
342}
343
344static int get_alternative_probe_event(struct debuginfo *dinfo,
345 struct perf_probe_event *pev,
346 struct perf_probe_point *tmp,
347 const char *target)
348{
349 int ret;
350
351 memcpy(tmp, &pev->point, sizeof(*tmp));
352 memset(&pev->point, 0, sizeof(pev->point));
353 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
354 target, pev->uprobes);
355 if (ret < 0)
356 memcpy(&pev->point, tmp, sizeof(*tmp));
357
358 return ret;
359}
360
361static int get_alternative_line_range(struct debuginfo *dinfo,
362 struct line_range *lr,
363 const char *target, bool user)
364{
365 struct perf_probe_point pp = { .function = lr->function,
366 .file = lr->file,
367 .line = lr->start };
368 struct perf_probe_point result;
369 int ret, len = 0;
370
371 memset(&result, 0, sizeof(result));
372
373 if (lr->end != INT_MAX)
374 len = lr->end - lr->start;
375 ret = find_alternative_probe_point(dinfo, &pp, &result,
376 target, user);
377 if (!ret) {
378 lr->function = result.function;
379 lr->file = result.file;
380 lr->start = result.line;
381 if (lr->end != INT_MAX)
382 lr->end = lr->start + len;
383 clear_perf_probe_point(&pp);
384 }
385 return ret;
386}
260 387
261/* Open new debuginfo of given module */ 388/* Open new debuginfo of given module */
262static struct debuginfo *open_debuginfo(const char *module, bool silent) 389static struct debuginfo *open_debuginfo(const char *module, bool silent)
@@ -465,6 +592,7 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
465 int max_tevs, const char *target) 592 int max_tevs, const char *target)
466{ 593{
467 bool need_dwarf = perf_probe_event_need_dwarf(pev); 594 bool need_dwarf = perf_probe_event_need_dwarf(pev);
595 struct perf_probe_point tmp;
468 struct debuginfo *dinfo; 596 struct debuginfo *dinfo;
469 int ntevs, ret = 0; 597 int ntevs, ret = 0;
470 598
@@ -481,6 +609,20 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
481 /* Searching trace events corresponding to a probe event */ 609 /* Searching trace events corresponding to a probe event */
482 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); 610 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs);
483 611
612 if (ntevs == 0) { /* Not found, retry with an alternative */
613 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
614 if (!ret) {
615 ntevs = debuginfo__find_trace_events(dinfo, pev,
616 tevs, max_tevs);
617 /*
618 * Write back to the original probe_event for
619 * setting appropriate (user given) event name
620 */
621 clear_perf_probe_point(&pev->point);
622 memcpy(&pev->point, &tmp, sizeof(tmp));
623 }
624 }
625
484 debuginfo__delete(dinfo); 626 debuginfo__delete(dinfo);
485 627
486 if (ntevs > 0) { /* Succeeded to find trace events */ 628 if (ntevs > 0) { /* Succeeded to find trace events */
@@ -495,11 +637,9 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
495 } 637 }
496 638
497 if (ntevs == 0) { /* No error but failed to find probe point. */ 639 if (ntevs == 0) { /* No error but failed to find probe point. */
498 pr_warning("Probe point '%s' not found in debuginfo.\n", 640 pr_warning("Probe point '%s' not found.\n",
499 synthesize_perf_probe_point(&pev->point)); 641 synthesize_perf_probe_point(&pev->point));
500 if (need_dwarf) 642 return -ENOENT;
501 return -ENOENT;
502 return 0;
503 } 643 }
504 /* Error path : ntevs < 0 */ 644 /* Error path : ntevs < 0 */
505 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); 645 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
@@ -532,7 +672,7 @@ static int get_real_path(const char *raw_path, const char *comp_dir,
532 else { 672 else {
533 if (access(raw_path, R_OK) == 0) { 673 if (access(raw_path, R_OK) == 0) {
534 *new_path = strdup(raw_path); 674 *new_path = strdup(raw_path);
535 return 0; 675 return *new_path ? 0 : -ENOMEM;
536 } else 676 } else
537 return -errno; 677 return -errno;
538 } 678 }
@@ -548,9 +688,11 @@ static int get_real_path(const char *raw_path, const char *comp_dir,
548 if (access(*new_path, R_OK) == 0) 688 if (access(*new_path, R_OK) == 0)
549 return 0; 689 return 0;
550 690
551 if (!symbol_conf.source_prefix) 691 if (!symbol_conf.source_prefix) {
552 /* In case of searching comp_dir, don't retry */ 692 /* In case of searching comp_dir, don't retry */
693 zfree(new_path);
553 return -errno; 694 return -errno;
695 }
554 696
555 switch (errno) { 697 switch (errno) {
556 case ENAMETOOLONG: 698 case ENAMETOOLONG:
@@ -622,7 +764,8 @@ static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
622 * Show line-range always requires debuginfo to find source file and 764 * Show line-range always requires debuginfo to find source file and
623 * line number. 765 * line number.
624 */ 766 */
625static int __show_line_range(struct line_range *lr, const char *module) 767static int __show_line_range(struct line_range *lr, const char *module,
768 bool user)
626{ 769{
627 int l = 1; 770 int l = 1;
628 struct int_node *ln; 771 struct int_node *ln;
@@ -638,6 +781,11 @@ static int __show_line_range(struct line_range *lr, const char *module)
638 return -ENOENT; 781 return -ENOENT;
639 782
640 ret = debuginfo__find_line_range(dinfo, lr); 783 ret = debuginfo__find_line_range(dinfo, lr);
784 if (!ret) { /* Not found, retry with an alternative */
785 ret = get_alternative_line_range(dinfo, lr, module, user);
786 if (!ret)
787 ret = debuginfo__find_line_range(dinfo, lr);
788 }
641 debuginfo__delete(dinfo); 789 debuginfo__delete(dinfo);
642 if (ret == 0 || ret == -ENOENT) { 790 if (ret == 0 || ret == -ENOENT) {
643 pr_warning("Specified source line is not found.\n"); 791 pr_warning("Specified source line is not found.\n");
@@ -650,7 +798,11 @@ static int __show_line_range(struct line_range *lr, const char *module)
650 /* Convert source file path */ 798 /* Convert source file path */
651 tmp = lr->path; 799 tmp = lr->path;
652 ret = get_real_path(tmp, lr->comp_dir, &lr->path); 800 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
653 free(tmp); /* Free old path */ 801
802 /* Free old path when new path is assigned */
803 if (tmp != lr->path)
804 free(tmp);
805
654 if (ret < 0) { 806 if (ret < 0) {
655 pr_warning("Failed to find source file path.\n"); 807 pr_warning("Failed to find source file path.\n");
656 return ret; 808 return ret;
@@ -707,7 +859,7 @@ int show_line_range(struct line_range *lr, const char *module, bool user)
707 ret = init_symbol_maps(user); 859 ret = init_symbol_maps(user);
708 if (ret < 0) 860 if (ret < 0)
709 return ret; 861 return ret;
710 ret = __show_line_range(lr, module); 862 ret = __show_line_range(lr, module, user);
711 exit_symbol_maps(); 863 exit_symbol_maps();
712 864
713 return ret; 865 return ret;
@@ -716,12 +868,13 @@ int show_line_range(struct line_range *lr, const char *module, bool user)
716static int show_available_vars_at(struct debuginfo *dinfo, 868static int show_available_vars_at(struct debuginfo *dinfo,
717 struct perf_probe_event *pev, 869 struct perf_probe_event *pev,
718 int max_vls, struct strfilter *_filter, 870 int max_vls, struct strfilter *_filter,
719 bool externs) 871 bool externs, const char *target)
720{ 872{
721 char *buf; 873 char *buf;
722 int ret, i, nvars; 874 int ret, i, nvars;
723 struct str_node *node; 875 struct str_node *node;
724 struct variable_list *vls = NULL, *vl; 876 struct variable_list *vls = NULL, *vl;
877 struct perf_probe_point tmp;
725 const char *var; 878 const char *var;
726 879
727 buf = synthesize_perf_probe_point(&pev->point); 880 buf = synthesize_perf_probe_point(&pev->point);
@@ -731,6 +884,15 @@ static int show_available_vars_at(struct debuginfo *dinfo,
731 884
732 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls, 885 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls,
733 max_vls, externs); 886 max_vls, externs);
887 if (!ret) { /* Not found, retry with an alternative */
888 ret = get_alternative_probe_event(dinfo, pev, &tmp, target);
889 if (!ret) {
890 ret = debuginfo__find_available_vars_at(dinfo, pev,
891 &vls, max_vls, externs);
892 /* Release the old probe_point */
893 clear_perf_probe_point(&tmp);
894 }
895 }
734 if (ret <= 0) { 896 if (ret <= 0) {
735 if (ret == 0 || ret == -ENOENT) { 897 if (ret == 0 || ret == -ENOENT) {
736 pr_err("Failed to find the address of %s\n", buf); 898 pr_err("Failed to find the address of %s\n", buf);
@@ -793,7 +955,7 @@ int show_available_vars(struct perf_probe_event *pevs, int npevs,
793 955
794 for (i = 0; i < npevs && ret >= 0; i++) 956 for (i = 0; i < npevs && ret >= 0; i++)
795 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter, 957 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter,
796 externs); 958 externs, module);
797 959
798 debuginfo__delete(dinfo); 960 debuginfo__delete(dinfo);
799out: 961out:
@@ -1739,15 +1901,13 @@ static int convert_to_perf_probe_event(struct probe_trace_event *tev,
1739 1901
1740void clear_perf_probe_event(struct perf_probe_event *pev) 1902void clear_perf_probe_event(struct perf_probe_event *pev)
1741{ 1903{
1742 struct perf_probe_point *pp = &pev->point;
1743 struct perf_probe_arg_field *field, *next; 1904 struct perf_probe_arg_field *field, *next;
1744 int i; 1905 int i;
1745 1906
1746 free(pev->event); 1907 free(pev->event);
1747 free(pev->group); 1908 free(pev->group);
1748 free(pp->file); 1909 free(pev->target);
1749 free(pp->function); 1910 clear_perf_probe_point(&pev->point);
1750 free(pp->lazy_line);
1751 1911
1752 for (i = 0; i < pev->nargs; i++) { 1912 for (i = 0; i < pev->nargs; i++) {
1753 free(pev->args[i].name); 1913 free(pev->args[i].name);
@@ -1805,7 +1965,7 @@ static void print_open_warning(int err, bool is_kprobe)
1805 " - please rebuild kernel with %s.\n", 1965 " - please rebuild kernel with %s.\n",
1806 is_kprobe ? 'k' : 'u', config); 1966 is_kprobe ? 'k' : 'u', config);
1807 } else if (err == -ENOTSUP) 1967 } else if (err == -ENOTSUP)
1808 pr_warning("Debugfs is not mounted.\n"); 1968 pr_warning("Tracefs or debugfs is not mounted.\n");
1809 else 1969 else
1810 pr_warning("Failed to open %cprobe_events: %s\n", 1970 pr_warning("Failed to open %cprobe_events: %s\n",
1811 is_kprobe ? 'k' : 'u', 1971 is_kprobe ? 'k' : 'u',
@@ -1816,7 +1976,7 @@ static void print_both_open_warning(int kerr, int uerr)
1816{ 1976{
1817 /* Both kprobes and uprobes are disabled, warn it. */ 1977 /* Both kprobes and uprobes are disabled, warn it. */
1818 if (kerr == -ENOTSUP && uerr == -ENOTSUP) 1978 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
1819 pr_warning("Debugfs is not mounted.\n"); 1979 pr_warning("Tracefs or debugfs is not mounted.\n");
1820 else if (kerr == -ENOENT && uerr == -ENOENT) 1980 else if (kerr == -ENOENT && uerr == -ENOENT)
1821 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS " 1981 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1822 "or/and CONFIG_UPROBE_EVENTS.\n"); 1982 "or/and CONFIG_UPROBE_EVENTS.\n");
@@ -1833,13 +1993,20 @@ static int open_probe_events(const char *trace_file, bool readwrite)
1833{ 1993{
1834 char buf[PATH_MAX]; 1994 char buf[PATH_MAX];
1835 const char *__debugfs; 1995 const char *__debugfs;
1996 const char *tracing_dir = "";
1836 int ret; 1997 int ret;
1837 1998
1838 __debugfs = debugfs_find_mountpoint(); 1999 __debugfs = tracefs_find_mountpoint();
1839 if (__debugfs == NULL) 2000 if (__debugfs == NULL) {
1840 return -ENOTSUP; 2001 tracing_dir = "tracing/";
2002
2003 __debugfs = debugfs_find_mountpoint();
2004 if (__debugfs == NULL)
2005 return -ENOTSUP;
2006 }
1841 2007
1842 ret = e_snprintf(buf, PATH_MAX, "%s/%s", __debugfs, trace_file); 2008 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2009 __debugfs, tracing_dir, trace_file);
1843 if (ret >= 0) { 2010 if (ret >= 0) {
1844 pr_debug("Opening %s write=%d\n", buf, readwrite); 2011 pr_debug("Opening %s write=%d\n", buf, readwrite);
1845 if (readwrite && !probe_event_dry_run) 2012 if (readwrite && !probe_event_dry_run)
@@ -1855,12 +2022,12 @@ static int open_probe_events(const char *trace_file, bool readwrite)
1855 2022
1856static int open_kprobe_events(bool readwrite) 2023static int open_kprobe_events(bool readwrite)
1857{ 2024{
1858 return open_probe_events("tracing/kprobe_events", readwrite); 2025 return open_probe_events("kprobe_events", readwrite);
1859} 2026}
1860 2027
1861static int open_uprobe_events(bool readwrite) 2028static int open_uprobe_events(bool readwrite)
1862{ 2029{
1863 return open_probe_events("tracing/uprobe_events", readwrite); 2030 return open_probe_events("uprobe_events", readwrite);
1864} 2031}
1865 2032
1866/* Get raw string list of current kprobe_events or uprobe_events */ 2033/* Get raw string list of current kprobe_events or uprobe_events */
@@ -1895,6 +2062,95 @@ static struct strlist *get_probe_trace_command_rawlist(int fd)
1895 return sl; 2062 return sl;
1896} 2063}
1897 2064
2065struct kprobe_blacklist_node {
2066 struct list_head list;
2067 unsigned long start;
2068 unsigned long end;
2069 char *symbol;
2070};
2071
2072static void kprobe_blacklist__delete(struct list_head *blacklist)
2073{
2074 struct kprobe_blacklist_node *node;
2075
2076 while (!list_empty(blacklist)) {
2077 node = list_first_entry(blacklist,
2078 struct kprobe_blacklist_node, list);
2079 list_del(&node->list);
2080 free(node->symbol);
2081 free(node);
2082 }
2083}
2084
2085static int kprobe_blacklist__load(struct list_head *blacklist)
2086{
2087 struct kprobe_blacklist_node *node;
2088 const char *__debugfs = debugfs_find_mountpoint();
2089 char buf[PATH_MAX], *p;
2090 FILE *fp;
2091 int ret;
2092
2093 if (__debugfs == NULL)
2094 return -ENOTSUP;
2095
2096 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2097 if (ret < 0)
2098 return ret;
2099
2100 fp = fopen(buf, "r");
2101 if (!fp)
2102 return -errno;
2103
2104 ret = 0;
2105 while (fgets(buf, PATH_MAX, fp)) {
2106 node = zalloc(sizeof(*node));
2107 if (!node) {
2108 ret = -ENOMEM;
2109 break;
2110 }
2111 INIT_LIST_HEAD(&node->list);
2112 list_add_tail(&node->list, blacklist);
2113 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2114 ret = -EINVAL;
2115 break;
2116 }
2117 p = strchr(buf, '\t');
2118 if (p) {
2119 p++;
2120 if (p[strlen(p) - 1] == '\n')
2121 p[strlen(p) - 1] = '\0';
2122 } else
2123 p = (char *)"unknown";
2124 node->symbol = strdup(p);
2125 if (!node->symbol) {
2126 ret = -ENOMEM;
2127 break;
2128 }
2129 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2130 node->start, node->end, node->symbol);
2131 ret++;
2132 }
2133 if (ret < 0)
2134 kprobe_blacklist__delete(blacklist);
2135 fclose(fp);
2136
2137 return ret;
2138}
2139
2140static struct kprobe_blacklist_node *
2141kprobe_blacklist__find_by_address(struct list_head *blacklist,
2142 unsigned long address)
2143{
2144 struct kprobe_blacklist_node *node;
2145
2146 list_for_each_entry(node, blacklist, list) {
2147 if (node->start <= address && address <= node->end)
2148 return node;
2149 }
2150
2151 return NULL;
2152}
2153
1898/* Show an event */ 2154/* Show an event */
1899static int show_perf_probe_event(struct perf_probe_event *pev, 2155static int show_perf_probe_event(struct perf_probe_event *pev,
1900 const char *module) 2156 const char *module)
@@ -2100,6 +2356,27 @@ static int get_new_event_name(char *buf, size_t len, const char *base,
2100 return ret; 2356 return ret;
2101} 2357}
2102 2358
2359/* Warn if the current kernel's uprobe implementation is old */
2360static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2361{
2362 int i;
2363 char *buf = synthesize_probe_trace_command(tev);
2364
2365 /* Old uprobe event doesn't support memory dereference */
2366 if (!tev->uprobes || tev->nargs == 0 || !buf)
2367 goto out;
2368
2369 for (i = 0; i < tev->nargs; i++)
2370 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2371 pr_warning("Please upgrade your kernel to at least "
2372 "3.14 to have access to feature %s\n",
2373 tev->args[i].value);
2374 break;
2375 }
2376out:
2377 free(buf);
2378}
2379
2103static int __add_probe_trace_events(struct perf_probe_event *pev, 2380static int __add_probe_trace_events(struct perf_probe_event *pev,
2104 struct probe_trace_event *tevs, 2381 struct probe_trace_event *tevs,
2105 int ntevs, bool allow_suffix) 2382 int ntevs, bool allow_suffix)
@@ -2109,6 +2386,8 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,
2109 char buf[64]; 2386 char buf[64];
2110 const char *event, *group; 2387 const char *event, *group;
2111 struct strlist *namelist; 2388 struct strlist *namelist;
2389 LIST_HEAD(blacklist);
2390 struct kprobe_blacklist_node *node;
2112 2391
2113 if (pev->uprobes) 2392 if (pev->uprobes)
2114 fd = open_uprobe_events(true); 2393 fd = open_uprobe_events(true);
@@ -2126,11 +2405,25 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,
2126 pr_debug("Failed to get current event list.\n"); 2405 pr_debug("Failed to get current event list.\n");
2127 return -EIO; 2406 return -EIO;
2128 } 2407 }
2408 /* Get kprobe blacklist if exists */
2409 if (!pev->uprobes) {
2410 ret = kprobe_blacklist__load(&blacklist);
2411 if (ret < 0)
2412 pr_debug("No kprobe blacklist support, ignored\n");
2413 }
2129 2414
2130 ret = 0; 2415 ret = 0;
2131 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":"); 2416 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
2132 for (i = 0; i < ntevs; i++) { 2417 for (i = 0; i < ntevs; i++) {
2133 tev = &tevs[i]; 2418 tev = &tevs[i];
2419 /* Ensure that the address is NOT blacklisted */
2420 node = kprobe_blacklist__find_by_address(&blacklist,
2421 tev->point.address);
2422 if (node) {
2423 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2424 continue;
2425 }
2426
2134 if (pev->event) 2427 if (pev->event)
2135 event = pev->event; 2428 event = pev->event;
2136 else 2429 else
@@ -2180,14 +2473,18 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,
2180 */ 2473 */
2181 allow_suffix = true; 2474 allow_suffix = true;
2182 } 2475 }
2476 if (ret == -EINVAL && pev->uprobes)
2477 warn_uprobe_event_compat(tev);
2183 2478
2184 if (ret >= 0) { 2479 /* Note that it is possible to skip all events because of blacklist */
2480 if (ret >= 0 && tev->event) {
2185 /* Show how to use the event. */ 2481 /* Show how to use the event. */
2186 pr_info("\nYou can now use it in all perf tools, such as:\n\n"); 2482 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2187 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, 2483 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
2188 tev->event); 2484 tev->event);
2189 } 2485 }
2190 2486
2487 kprobe_blacklist__delete(&blacklist);
2191 strlist__delete(namelist); 2488 strlist__delete(namelist);
2192 close(fd); 2489 close(fd);
2193 return ret; 2490 return ret;
@@ -2199,8 +2496,7 @@ static int find_probe_functions(struct map *map, char *name)
2199 struct symbol *sym; 2496 struct symbol *sym;
2200 2497
2201 map__for_each_symbol_by_name(map, name, sym) { 2498 map__for_each_symbol_by_name(map, name, sym) {
2202 if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) 2499 found++;
2203 found++;
2204 } 2500 }
2205 2501
2206 return found; 2502 return found;
@@ -2218,7 +2514,6 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2218 int max_tevs, const char *target) 2514 int max_tevs, const char *target)
2219{ 2515{
2220 struct map *map = NULL; 2516 struct map *map = NULL;
2221 struct kmap *kmap = NULL;
2222 struct ref_reloc_sym *reloc_sym = NULL; 2517 struct ref_reloc_sym *reloc_sym = NULL;
2223 struct symbol *sym; 2518 struct symbol *sym;
2224 struct probe_trace_event *tev; 2519 struct probe_trace_event *tev;
@@ -2227,11 +2522,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2227 int num_matched_functions; 2522 int num_matched_functions;
2228 int ret, i; 2523 int ret, i;
2229 2524
2230 /* Init maps of given executable or kernel */ 2525 map = get_target_map(target, pev->uprobes);
2231 if (pev->uprobes)
2232 map = dso__new_map(target);
2233 else
2234 map = kernel_get_module_map(target);
2235 if (!map) { 2526 if (!map) {
2236 ret = -EINVAL; 2527 ret = -EINVAL;
2237 goto out; 2528 goto out;
@@ -2255,8 +2546,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2255 } 2546 }
2256 2547
2257 if (!pev->uprobes && !pp->retprobe) { 2548 if (!pev->uprobes && !pp->retprobe) {
2258 kmap = map__kmap(map); 2549 reloc_sym = kernel_get_ref_reloc_sym();
2259 reloc_sym = kmap->ref_reloc_sym;
2260 if (!reloc_sym) { 2550 if (!reloc_sym) {
2261 pr_warning("Relocated base symbol is not found!\n"); 2551 pr_warning("Relocated base symbol is not found!\n");
2262 ret = -EINVAL; 2552 ret = -EINVAL;
@@ -2324,11 +2614,7 @@ static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
2324 } 2614 }
2325 2615
2326out: 2616out:
2327 if (map && pev->uprobes) { 2617 put_target_map(map, pev->uprobes);
2328 /* Only when using uprobe(exec) map needs to be released */
2329 dso__delete(map->dso);
2330 map__delete(map);
2331 }
2332 return ret; 2618 return ret;
2333 2619
2334nomem_out: 2620nomem_out:
@@ -2369,7 +2655,7 @@ struct __event_package {
2369}; 2655};
2370 2656
2371int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, 2657int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2372 int max_tevs, const char *target, bool force_add) 2658 int max_tevs, bool force_add)
2373{ 2659{
2374 int i, j, ret; 2660 int i, j, ret;
2375 struct __event_package *pkgs; 2661 struct __event_package *pkgs;
@@ -2393,7 +2679,7 @@ int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
2393 ret = convert_to_probe_trace_events(pkgs[i].pev, 2679 ret = convert_to_probe_trace_events(pkgs[i].pev,
2394 &pkgs[i].tevs, 2680 &pkgs[i].tevs,
2395 max_tevs, 2681 max_tevs,
2396 target); 2682 pkgs[i].pev->target);
2397 if (ret < 0) 2683 if (ret < 0)
2398 goto end; 2684 goto end;
2399 pkgs[i].ntevs = ret; 2685 pkgs[i].ntevs = ret;
@@ -2568,8 +2854,7 @@ static struct strfilter *available_func_filter;
2568static int filter_available_functions(struct map *map __maybe_unused, 2854static int filter_available_functions(struct map *map __maybe_unused,
2569 struct symbol *sym) 2855 struct symbol *sym)
2570{ 2856{
2571 if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) && 2857 if (strfilter__compare(available_func_filter, sym->name))
2572 strfilter__compare(available_func_filter, sym->name))
2573 return 0; 2858 return 0;
2574 return 1; 2859 return 1;
2575} 2860}
diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
index e01e9943139f..d6b783447be9 100644
--- a/tools/perf/util/probe-event.h
+++ b/tools/perf/util/probe-event.h
@@ -73,7 +73,8 @@ struct perf_probe_event {
73 char *group; /* Group name */ 73 char *group; /* Group name */
74 struct perf_probe_point point; /* Probe point */ 74 struct perf_probe_point point; /* Probe point */
75 int nargs; /* Number of arguments */ 75 int nargs; /* Number of arguments */
76 bool uprobes; 76 bool uprobes; /* Uprobe event flag */
77 char *target; /* Target binary */
77 struct perf_probe_arg *args; /* Arguments */ 78 struct perf_probe_arg *args; /* Arguments */
78}; 79};
79 80
@@ -124,8 +125,7 @@ extern int line_range__init(struct line_range *lr);
124extern const char *kernel_get_module_path(const char *module); 125extern const char *kernel_get_module_path(const char *module);
125 126
126extern int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, 127extern int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
127 int max_probe_points, const char *module, 128 int max_probe_points, bool force_add);
128 bool force_add);
129extern int del_perf_probe_events(struct strlist *dellist); 129extern int del_perf_probe_events(struct strlist *dellist);
130extern int show_perf_probe_events(void); 130extern int show_perf_probe_events(void);
131extern int show_line_range(struct line_range *lr, const char *module, 131extern int show_line_range(struct line_range *lr, const char *module,
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index d32cf93565da..e3074230f236 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -460,7 +460,8 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
460 " nor array.\n", varname); 460 " nor array.\n", varname);
461 return -EINVAL; 461 return -EINVAL;
462 } 462 }
463 if (field->ref) { 463 /* While prcessing unnamed field, we don't care about this */
464 if (field->ref && dwarf_diename(vr_die)) {
464 pr_err("Semantic error: %s must be referred by '.'\n", 465 pr_err("Semantic error: %s must be referred by '.'\n",
465 field->name); 466 field->name);
466 return -EINVAL; 467 return -EINVAL;
@@ -491,6 +492,11 @@ static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
491 } 492 }
492 ref->offset += (long)offs; 493 ref->offset += (long)offs;
493 494
495 /* If this member is unnamed, we need to reuse this field */
496 if (!dwarf_diename(die_mem))
497 return convert_variable_fields(die_mem, varname, field,
498 &ref, die_mem);
499
494next: 500next:
495 /* Converting next field */ 501 /* Converting next field */
496 if (field->next) 502 if (field->next)
@@ -915,17 +921,13 @@ static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
915 dwarf_decl_line(sp_die, &pf->lno); 921 dwarf_decl_line(sp_die, &pf->lno);
916 pf->lno += pp->line; 922 pf->lno += pp->line;
917 param->retval = find_probe_point_by_line(pf); 923 param->retval = find_probe_point_by_line(pf);
918 } else if (!dwarf_func_inline(sp_die)) { 924 } else if (die_is_func_instance(sp_die)) {
925 /* Instances always have the entry address */
926 dwarf_entrypc(sp_die, &pf->addr);
919 /* Real function */ 927 /* Real function */
920 if (pp->lazy_line) 928 if (pp->lazy_line)
921 param->retval = find_probe_point_lazy(sp_die, pf); 929 param->retval = find_probe_point_lazy(sp_die, pf);
922 else { 930 else {
923 if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
924 pr_warning("Failed to get entry address of "
925 "%s.\n", dwarf_diename(sp_die));
926 param->retval = -ENOENT;
927 return DWARF_CB_ABORT;
928 }
929 pf->addr += pp->offset; 931 pf->addr += pp->offset;
930 /* TODO: Check the address in this function */ 932 /* TODO: Check the address in this function */
931 param->retval = call_probe_finder(sp_die, pf); 933 param->retval = call_probe_finder(sp_die, pf);
@@ -1349,11 +1351,8 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
1349 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp; 1351 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp;
1350 int baseline = 0, lineno = 0, ret = 0; 1352 int baseline = 0, lineno = 0, ret = 0;
1351 1353
1352 /* Adjust address with bias */
1353 addr += dbg->bias;
1354
1355 /* Find cu die */ 1354 /* Find cu die */
1356 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr - dbg->bias, &cudie)) { 1355 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) {
1357 pr_warning("Failed to find debug information for address %lx\n", 1356 pr_warning("Failed to find debug information for address %lx\n",
1358 addr); 1357 addr);
1359 ret = -EINVAL; 1358 ret = -EINVAL;
@@ -1536,7 +1535,7 @@ static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
1536 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e); 1535 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
1537 lr->start = lf->lno_s; 1536 lr->start = lf->lno_s;
1538 lr->end = lf->lno_e; 1537 lr->end = lf->lno_e;
1539 if (dwarf_func_inline(sp_die)) 1538 if (!die_is_func_instance(sp_die))
1540 param->retval = die_walk_instances(sp_die, 1539 param->retval = die_walk_instances(sp_die,
1541 line_range_inline_cb, lf); 1540 line_range_inline_cb, lf);
1542 else 1541 else
diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources
index 6c6a6953fa93..4d28624a1eca 100644
--- a/tools/perf/util/python-ext-sources
+++ b/tools/perf/util/python-ext-sources
@@ -17,6 +17,5 @@ util/xyarray.c
17util/cgroup.c 17util/cgroup.c
18util/rblist.c 18util/rblist.c
19util/strlist.c 19util/strlist.c
20../lib/api/fs/fs.c
21util/trace-event.c 20util/trace-event.c
22../../lib/rbtree.c 21../../lib/rbtree.c
diff --git a/tools/perf/util/scripting-engines/Build b/tools/perf/util/scripting-engines/Build
new file mode 100644
index 000000000000..6516e220c247
--- /dev/null
+++ b/tools/perf/util/scripting-engines/Build
@@ -0,0 +1,6 @@
1libperf-$(CONFIG_LIBPERL) += trace-event-perl.o
2libperf-$(CONFIG_LIBPYTHON) += trace-event-python.o
3
4CFLAGS_trace-event-perl.o += $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow -Wno-undef -Wno-switch-default
5
6CFLAGS_trace-event-python.o += $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow
diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c
index 22ebc46226e7..430b5d27828e 100644
--- a/tools/perf/util/scripting-engines/trace-event-perl.c
+++ b/tools/perf/util/scripting-engines/trace-event-perl.c
@@ -214,6 +214,11 @@ static void define_event_symbols(struct event_format *event,
214 define_event_symbols(event, ev_name, args->hex.field); 214 define_event_symbols(event, ev_name, args->hex.field);
215 define_event_symbols(event, ev_name, args->hex.size); 215 define_event_symbols(event, ev_name, args->hex.size);
216 break; 216 break;
217 case PRINT_INT_ARRAY:
218 define_event_symbols(event, ev_name, args->int_array.field);
219 define_event_symbols(event, ev_name, args->int_array.count);
220 define_event_symbols(event, ev_name, args->int_array.el_size);
221 break;
217 case PRINT_BSTRING: 222 case PRINT_BSTRING:
218 case PRINT_DYNAMIC_ARRAY: 223 case PRINT_DYNAMIC_ARRAY:
219 case PRINT_STRING: 224 case PRINT_STRING:
@@ -355,10 +360,9 @@ static void perl_process_event_generic(union perf_event *event,
355static void perl_process_event(union perf_event *event, 360static void perl_process_event(union perf_event *event,
356 struct perf_sample *sample, 361 struct perf_sample *sample,
357 struct perf_evsel *evsel, 362 struct perf_evsel *evsel,
358 struct thread *thread, 363 struct addr_location *al)
359 struct addr_location *al __maybe_unused)
360{ 364{
361 perl_process_tracepoint(sample, evsel, thread); 365 perl_process_tracepoint(sample, evsel, al->thread);
362 perl_process_event_generic(event, sample, evsel); 366 perl_process_event_generic(event, sample, evsel);
363} 367}
364 368
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 0c815a40a6e8..5544b8cdd1ee 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -231,6 +231,11 @@ static void define_event_symbols(struct event_format *event,
231 define_event_symbols(event, ev_name, args->hex.field); 231 define_event_symbols(event, ev_name, args->hex.field);
232 define_event_symbols(event, ev_name, args->hex.size); 232 define_event_symbols(event, ev_name, args->hex.size);
233 break; 233 break;
234 case PRINT_INT_ARRAY:
235 define_event_symbols(event, ev_name, args->int_array.field);
236 define_event_symbols(event, ev_name, args->int_array.count);
237 define_event_symbols(event, ev_name, args->int_array.el_size);
238 break;
234 case PRINT_STRING: 239 case PRINT_STRING:
235 break; 240 break;
236 case PRINT_TYPE: 241 case PRINT_TYPE:
@@ -376,7 +381,6 @@ exit:
376 381
377static void python_process_tracepoint(struct perf_sample *sample, 382static void python_process_tracepoint(struct perf_sample *sample,
378 struct perf_evsel *evsel, 383 struct perf_evsel *evsel,
379 struct thread *thread,
380 struct addr_location *al) 384 struct addr_location *al)
381{ 385{
382 struct event_format *event = evsel->tp_format; 386 struct event_format *event = evsel->tp_format;
@@ -390,7 +394,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
390 int cpu = sample->cpu; 394 int cpu = sample->cpu;
391 void *data = sample->raw_data; 395 void *data = sample->raw_data;
392 unsigned long long nsecs = sample->time; 396 unsigned long long nsecs = sample->time;
393 const char *comm = thread__comm_str(thread); 397 const char *comm = thread__comm_str(al->thread);
394 398
395 t = PyTuple_New(MAX_FIELDS); 399 t = PyTuple_New(MAX_FIELDS);
396 if (!t) 400 if (!t)
@@ -675,7 +679,7 @@ static int python_export_sample(struct db_export *dbe,
675 tuple_set_u64(t, 0, es->db_id); 679 tuple_set_u64(t, 0, es->db_id);
676 tuple_set_u64(t, 1, es->evsel->db_id); 680 tuple_set_u64(t, 1, es->evsel->db_id);
677 tuple_set_u64(t, 2, es->al->machine->db_id); 681 tuple_set_u64(t, 2, es->al->machine->db_id);
678 tuple_set_u64(t, 3, es->thread->db_id); 682 tuple_set_u64(t, 3, es->al->thread->db_id);
679 tuple_set_u64(t, 4, es->comm_db_id); 683 tuple_set_u64(t, 4, es->comm_db_id);
680 tuple_set_u64(t, 5, es->dso_db_id); 684 tuple_set_u64(t, 5, es->dso_db_id);
681 tuple_set_u64(t, 6, es->sym_db_id); 685 tuple_set_u64(t, 6, es->sym_db_id);
@@ -761,7 +765,6 @@ static int python_process_call_return(struct call_return *cr, void *data)
761 765
762static void python_process_general_event(struct perf_sample *sample, 766static void python_process_general_event(struct perf_sample *sample,
763 struct perf_evsel *evsel, 767 struct perf_evsel *evsel,
764 struct thread *thread,
765 struct addr_location *al) 768 struct addr_location *al)
766{ 769{
767 PyObject *handler, *t, *dict, *callchain, *dict_sample; 770 PyObject *handler, *t, *dict, *callchain, *dict_sample;
@@ -811,7 +814,7 @@ static void python_process_general_event(struct perf_sample *sample,
811 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize( 814 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
812 (const char *)sample->raw_data, sample->raw_size)); 815 (const char *)sample->raw_data, sample->raw_size));
813 pydict_set_item_string_decref(dict, "comm", 816 pydict_set_item_string_decref(dict, "comm",
814 PyString_FromString(thread__comm_str(thread))); 817 PyString_FromString(thread__comm_str(al->thread)));
815 if (al->map) { 818 if (al->map) {
816 pydict_set_item_string_decref(dict, "dso", 819 pydict_set_item_string_decref(dict, "dso",
817 PyString_FromString(al->map->dso->name)); 820 PyString_FromString(al->map->dso->name));
@@ -838,22 +841,20 @@ exit:
838static void python_process_event(union perf_event *event, 841static void python_process_event(union perf_event *event,
839 struct perf_sample *sample, 842 struct perf_sample *sample,
840 struct perf_evsel *evsel, 843 struct perf_evsel *evsel,
841 struct thread *thread,
842 struct addr_location *al) 844 struct addr_location *al)
843{ 845{
844 struct tables *tables = &tables_global; 846 struct tables *tables = &tables_global;
845 847
846 switch (evsel->attr.type) { 848 switch (evsel->attr.type) {
847 case PERF_TYPE_TRACEPOINT: 849 case PERF_TYPE_TRACEPOINT:
848 python_process_tracepoint(sample, evsel, thread, al); 850 python_process_tracepoint(sample, evsel, al);
849 break; 851 break;
850 /* Reserve for future process_hw/sw/raw APIs */ 852 /* Reserve for future process_hw/sw/raw APIs */
851 default: 853 default:
852 if (tables->db_export_mode) 854 if (tables->db_export_mode)
853 db_export__sample(&tables->dbe, event, sample, evsel, 855 db_export__sample(&tables->dbe, event, sample, evsel, al);
854 thread, al);
855 else 856 else
856 python_process_general_event(sample, evsel, thread, al); 857 python_process_general_event(sample, evsel, al);
857 } 858 }
858} 859}
859 860
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 0baf75f12b7c..0c74012575ac 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -16,6 +16,12 @@
16#include "perf_regs.h" 16#include "perf_regs.h"
17#include "asm/bug.h" 17#include "asm/bug.h"
18 18
19static int machines__deliver_event(struct machines *machines,
20 struct perf_evlist *evlist,
21 union perf_event *event,
22 struct perf_sample *sample,
23 struct perf_tool *tool, u64 file_offset);
24
19static int perf_session__open(struct perf_session *session) 25static int perf_session__open(struct perf_session *session)
20{ 26{
21 struct perf_data_file *file = session->file; 27 struct perf_data_file *file = session->file;
@@ -86,6 +92,23 @@ static void perf_session__set_comm_exec(struct perf_session *session)
86 machines__set_comm_exec(&session->machines, comm_exec); 92 machines__set_comm_exec(&session->machines, comm_exec);
87} 93}
88 94
95static int ordered_events__deliver_event(struct ordered_events *oe,
96 struct ordered_event *event)
97{
98 struct perf_sample sample;
99 struct perf_session *session = container_of(oe, struct perf_session,
100 ordered_events);
101 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
102
103 if (ret) {
104 pr_err("Can't parse sample, err = %d\n", ret);
105 return ret;
106 }
107
108 return machines__deliver_event(&session->machines, session->evlist, event->event,
109 &sample, session->tool, event->file_offset);
110}
111
89struct perf_session *perf_session__new(struct perf_data_file *file, 112struct perf_session *perf_session__new(struct perf_data_file *file,
90 bool repipe, struct perf_tool *tool) 113 bool repipe, struct perf_tool *tool)
91{ 114{
@@ -95,8 +118,9 @@ struct perf_session *perf_session__new(struct perf_data_file *file,
95 goto out; 118 goto out;
96 119
97 session->repipe = repipe; 120 session->repipe = repipe;
98 ordered_events__init(&session->ordered_events); 121 session->tool = tool;
99 machines__init(&session->machines); 122 machines__init(&session->machines);
123 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
100 124
101 if (file) { 125 if (file) {
102 if (perf_data_file__open(file)) 126 if (perf_data_file__open(file))
@@ -138,11 +162,6 @@ struct perf_session *perf_session__new(struct perf_data_file *file,
138 return NULL; 162 return NULL;
139} 163}
140 164
141static void perf_session__delete_dead_threads(struct perf_session *session)
142{
143 machine__delete_dead_threads(&session->machines.host);
144}
145
146static void perf_session__delete_threads(struct perf_session *session) 165static void perf_session__delete_threads(struct perf_session *session)
147{ 166{
148 machine__delete_threads(&session->machines.host); 167 machine__delete_threads(&session->machines.host);
@@ -167,7 +186,6 @@ static void perf_session_env__delete(struct perf_session_env *env)
167void perf_session__delete(struct perf_session *session) 186void perf_session__delete(struct perf_session *session)
168{ 187{
169 perf_session__destroy_kernel_maps(session); 188 perf_session__destroy_kernel_maps(session);
170 perf_session__delete_dead_threads(session);
171 perf_session__delete_threads(session); 189 perf_session__delete_threads(session);
172 perf_session_env__delete(&session->header.env); 190 perf_session_env__delete(&session->header.env);
173 machines__exit(&session->machines); 191 machines__exit(&session->machines);
@@ -215,10 +233,17 @@ static int process_event_stub(struct perf_tool *tool __maybe_unused,
215 return 0; 233 return 0;
216} 234}
217 235
236static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
237 union perf_event *event __maybe_unused,
238 struct perf_session *session __maybe_unused)
239{
240 dump_printf(": unhandled!\n");
241 return 0;
242}
243
218static int process_finished_round_stub(struct perf_tool *tool __maybe_unused, 244static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
219 union perf_event *event __maybe_unused, 245 union perf_event *event __maybe_unused,
220 struct perf_session *perf_session 246 struct ordered_events *oe __maybe_unused)
221 __maybe_unused)
222{ 247{
223 dump_printf(": unhandled!\n"); 248 dump_printf(": unhandled!\n");
224 return 0; 249 return 0;
@@ -226,7 +251,7 @@ static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
226 251
227static int process_finished_round(struct perf_tool *tool, 252static int process_finished_round(struct perf_tool *tool,
228 union perf_event *event, 253 union perf_event *event,
229 struct perf_session *session); 254 struct ordered_events *oe);
230 255
231static int process_id_index_stub(struct perf_tool *tool __maybe_unused, 256static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
232 union perf_event *event __maybe_unused, 257 union perf_event *event __maybe_unused,
@@ -264,7 +289,7 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
264 if (tool->tracing_data == NULL) 289 if (tool->tracing_data == NULL)
265 tool->tracing_data = process_event_synth_tracing_data_stub; 290 tool->tracing_data = process_event_synth_tracing_data_stub;
266 if (tool->build_id == NULL) 291 if (tool->build_id == NULL)
267 tool->build_id = process_finished_round_stub; 292 tool->build_id = process_build_id_stub;
268 if (tool->finished_round == NULL) { 293 if (tool->finished_round == NULL) {
269 if (tool->ordered_events) 294 if (tool->ordered_events)
270 tool->finished_round = process_finished_round; 295 tool->finished_round = process_finished_round;
@@ -514,54 +539,80 @@ static perf_event__swap_op perf_event__swap_ops[] = {
514 * Flush every events below timestamp 7 539 * Flush every events below timestamp 7
515 * etc... 540 * etc...
516 */ 541 */
517static int process_finished_round(struct perf_tool *tool, 542static int process_finished_round(struct perf_tool *tool __maybe_unused,
518 union perf_event *event __maybe_unused, 543 union perf_event *event __maybe_unused,
519 struct perf_session *session) 544 struct ordered_events *oe)
520{ 545{
521 return ordered_events__flush(session, tool, OE_FLUSH__ROUND); 546 return ordered_events__flush(oe, OE_FLUSH__ROUND);
522} 547}
523 548
524int perf_session_queue_event(struct perf_session *s, union perf_event *event, 549int perf_session__queue_event(struct perf_session *s, union perf_event *event,
525 struct perf_tool *tool, struct perf_sample *sample, 550 struct perf_sample *sample, u64 file_offset)
526 u64 file_offset)
527{ 551{
528 struct ordered_events *oe = &s->ordered_events; 552 return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
529 u64 timestamp = sample->time; 553}
530 struct ordered_event *new;
531
532 if (!timestamp || timestamp == ~0ULL)
533 return -ETIME;
534 554
535 if (timestamp < oe->last_flush) { 555static void callchain__lbr_callstack_printf(struct perf_sample *sample)
536 pr_oe_time(timestamp, "out of order event\n"); 556{
537 pr_oe_time(oe->last_flush, "last flush, last_flush_type %d\n", 557 struct ip_callchain *callchain = sample->callchain;
538 oe->last_flush_type); 558 struct branch_stack *lbr_stack = sample->branch_stack;
559 u64 kernel_callchain_nr = callchain->nr;
560 unsigned int i;
539 561
540 s->stats.nr_unordered_events++; 562 for (i = 0; i < kernel_callchain_nr; i++) {
563 if (callchain->ips[i] == PERF_CONTEXT_USER)
564 break;
541 } 565 }
542 566
543 new = ordered_events__new(oe, timestamp, event); 567 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
544 if (!new) { 568 u64 total_nr;
545 ordered_events__flush(s, tool, OE_FLUSH__HALF); 569 /*
546 new = ordered_events__new(oe, timestamp, event); 570 * LBR callstack can only get user call chain,
547 } 571 * i is kernel call chain number,
572 * 1 is PERF_CONTEXT_USER.
573 *
574 * The user call chain is stored in LBR registers.
575 * LBR are pair registers. The caller is stored
576 * in "from" register, while the callee is stored
577 * in "to" register.
578 * For example, there is a call stack
579 * "A"->"B"->"C"->"D".
580 * The LBR registers will recorde like
581 * "C"->"D", "B"->"C", "A"->"B".
582 * So only the first "to" register and all "from"
583 * registers are needed to construct the whole stack.
584 */
585 total_nr = i + 1 + lbr_stack->nr + 1;
586 kernel_callchain_nr = i + 1;
548 587
549 if (!new) 588 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
550 return -ENOMEM;
551 589
552 new->file_offset = file_offset; 590 for (i = 0; i < kernel_callchain_nr; i++)
553 return 0; 591 printf("..... %2d: %016" PRIx64 "\n",
592 i, callchain->ips[i]);
593
594 printf("..... %2d: %016" PRIx64 "\n",
595 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
596 for (i = 0; i < lbr_stack->nr; i++)
597 printf("..... %2d: %016" PRIx64 "\n",
598 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
599 }
554} 600}
555 601
556static void callchain__printf(struct perf_sample *sample) 602static void callchain__printf(struct perf_evsel *evsel,
603 struct perf_sample *sample)
557{ 604{
558 unsigned int i; 605 unsigned int i;
606 struct ip_callchain *callchain = sample->callchain;
607
608 if (has_branch_callstack(evsel))
609 callchain__lbr_callstack_printf(sample);
559 610
560 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr); 611 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
561 612
562 for (i = 0; i < sample->callchain->nr; i++) 613 for (i = 0; i < callchain->nr; i++)
563 printf("..... %2d: %016" PRIx64 "\n", 614 printf("..... %2d: %016" PRIx64 "\n",
564 i, sample->callchain->ips[i]); 615 i, callchain->ips[i]);
565} 616}
566 617
567static void branch_stack__printf(struct perf_sample *sample) 618static void branch_stack__printf(struct perf_sample *sample)
@@ -636,14 +687,14 @@ static void stack_user__printf(struct stack_dump *dump)
636 dump->size, dump->offset); 687 dump->size, dump->offset);
637} 688}
638 689
639static void perf_session__print_tstamp(struct perf_session *session, 690static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
640 union perf_event *event, 691 union perf_event *event,
641 struct perf_sample *sample) 692 struct perf_sample *sample)
642{ 693{
643 u64 sample_type = __perf_evlist__combined_sample_type(session->evlist); 694 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
644 695
645 if (event->header.type != PERF_RECORD_SAMPLE && 696 if (event->header.type != PERF_RECORD_SAMPLE &&
646 !perf_evlist__sample_id_all(session->evlist)) { 697 !perf_evlist__sample_id_all(evlist)) {
647 fputs("-1 -1 ", stdout); 698 fputs("-1 -1 ", stdout);
648 return; 699 return;
649 } 700 }
@@ -685,7 +736,7 @@ static void sample_read__printf(struct perf_sample *sample, u64 read_format)
685 sample->read.one.id, sample->read.one.value); 736 sample->read.one.id, sample->read.one.value);
686} 737}
687 738
688static void dump_event(struct perf_session *session, union perf_event *event, 739static void dump_event(struct perf_evlist *evlist, union perf_event *event,
689 u64 file_offset, struct perf_sample *sample) 740 u64 file_offset, struct perf_sample *sample)
690{ 741{
691 if (!dump_trace) 742 if (!dump_trace)
@@ -697,7 +748,7 @@ static void dump_event(struct perf_session *session, union perf_event *event,
697 trace_event(event); 748 trace_event(event);
698 749
699 if (sample) 750 if (sample)
700 perf_session__print_tstamp(session, event, sample); 751 perf_evlist__print_tstamp(evlist, event, sample);
701 752
702 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, 753 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
703 event->header.size, perf_event__name(event->header.type)); 754 event->header.size, perf_event__name(event->header.type));
@@ -718,9 +769,9 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
718 sample_type = evsel->attr.sample_type; 769 sample_type = evsel->attr.sample_type;
719 770
720 if (sample_type & PERF_SAMPLE_CALLCHAIN) 771 if (sample_type & PERF_SAMPLE_CALLCHAIN)
721 callchain__printf(sample); 772 callchain__printf(evsel, sample);
722 773
723 if (sample_type & PERF_SAMPLE_BRANCH_STACK) 774 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
724 branch_stack__printf(sample); 775 branch_stack__printf(sample);
725 776
726 if (sample_type & PERF_SAMPLE_REGS_USER) 777 if (sample_type & PERF_SAMPLE_REGS_USER)
@@ -745,8 +796,7 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
745 sample_read__printf(sample, evsel->attr.read_format); 796 sample_read__printf(sample, evsel->attr.read_format);
746} 797}
747 798
748static struct machine * 799static struct machine *machines__find_for_cpumode(struct machines *machines,
749 perf_session__find_machine_for_cpumode(struct perf_session *session,
750 union perf_event *event, 800 union perf_event *event,
751 struct perf_sample *sample) 801 struct perf_sample *sample)
752{ 802{
@@ -764,26 +814,24 @@ static struct machine *
764 else 814 else
765 pid = sample->pid; 815 pid = sample->pid;
766 816
767 machine = perf_session__find_machine(session, pid); 817 machine = machines__find(machines, pid);
768 if (!machine) 818 if (!machine)
769 machine = perf_session__findnew_machine(session, 819 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
770 DEFAULT_GUEST_KERNEL_ID);
771 return machine; 820 return machine;
772 } 821 }
773 822
774 return &session->machines.host; 823 return &machines->host;
775} 824}
776 825
777static int deliver_sample_value(struct perf_session *session, 826static int deliver_sample_value(struct perf_evlist *evlist,
778 struct perf_tool *tool, 827 struct perf_tool *tool,
779 union perf_event *event, 828 union perf_event *event,
780 struct perf_sample *sample, 829 struct perf_sample *sample,
781 struct sample_read_value *v, 830 struct sample_read_value *v,
782 struct machine *machine) 831 struct machine *machine)
783{ 832{
784 struct perf_sample_id *sid; 833 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
785 834
786 sid = perf_evlist__id2sid(session->evlist, v->id);
787 if (sid) { 835 if (sid) {
788 sample->id = v->id; 836 sample->id = v->id;
789 sample->period = v->value - sid->period; 837 sample->period = v->value - sid->period;
@@ -791,14 +839,14 @@ static int deliver_sample_value(struct perf_session *session,
791 } 839 }
792 840
793 if (!sid || sid->evsel == NULL) { 841 if (!sid || sid->evsel == NULL) {
794 ++session->stats.nr_unknown_id; 842 ++evlist->stats.nr_unknown_id;
795 return 0; 843 return 0;
796 } 844 }
797 845
798 return tool->sample(tool, event, sample, sid->evsel, machine); 846 return tool->sample(tool, event, sample, sid->evsel, machine);
799} 847}
800 848
801static int deliver_sample_group(struct perf_session *session, 849static int deliver_sample_group(struct perf_evlist *evlist,
802 struct perf_tool *tool, 850 struct perf_tool *tool,
803 union perf_event *event, 851 union perf_event *event,
804 struct perf_sample *sample, 852 struct perf_sample *sample,
@@ -808,7 +856,7 @@ static int deliver_sample_group(struct perf_session *session,
808 u64 i; 856 u64 i;
809 857
810 for (i = 0; i < sample->read.group.nr; i++) { 858 for (i = 0; i < sample->read.group.nr; i++) {
811 ret = deliver_sample_value(session, tool, event, sample, 859 ret = deliver_sample_value(evlist, tool, event, sample,
812 &sample->read.group.values[i], 860 &sample->read.group.values[i],
813 machine); 861 machine);
814 if (ret) 862 if (ret)
@@ -819,7 +867,7 @@ static int deliver_sample_group(struct perf_session *session,
819} 867}
820 868
821static int 869static int
822perf_session__deliver_sample(struct perf_session *session, 870 perf_evlist__deliver_sample(struct perf_evlist *evlist,
823 struct perf_tool *tool, 871 struct perf_tool *tool,
824 union perf_event *event, 872 union perf_event *event,
825 struct perf_sample *sample, 873 struct perf_sample *sample,
@@ -836,41 +884,40 @@ perf_session__deliver_sample(struct perf_session *session,
836 884
837 /* For PERF_SAMPLE_READ we have either single or group mode. */ 885 /* For PERF_SAMPLE_READ we have either single or group mode. */
838 if (read_format & PERF_FORMAT_GROUP) 886 if (read_format & PERF_FORMAT_GROUP)
839 return deliver_sample_group(session, tool, event, sample, 887 return deliver_sample_group(evlist, tool, event, sample,
840 machine); 888 machine);
841 else 889 else
842 return deliver_sample_value(session, tool, event, sample, 890 return deliver_sample_value(evlist, tool, event, sample,
843 &sample->read.one, machine); 891 &sample->read.one, machine);
844} 892}
845 893
846int perf_session__deliver_event(struct perf_session *session, 894static int machines__deliver_event(struct machines *machines,
847 union perf_event *event, 895 struct perf_evlist *evlist,
848 struct perf_sample *sample, 896 union perf_event *event,
849 struct perf_tool *tool, u64 file_offset) 897 struct perf_sample *sample,
898 struct perf_tool *tool, u64 file_offset)
850{ 899{
851 struct perf_evsel *evsel; 900 struct perf_evsel *evsel;
852 struct machine *machine; 901 struct machine *machine;
853 902
854 dump_event(session, event, file_offset, sample); 903 dump_event(evlist, event, file_offset, sample);
855 904
856 evsel = perf_evlist__id2evsel(session->evlist, sample->id); 905 evsel = perf_evlist__id2evsel(evlist, sample->id);
857 906
858 machine = perf_session__find_machine_for_cpumode(session, event, 907 machine = machines__find_for_cpumode(machines, event, sample);
859 sample);
860 908
861 switch (event->header.type) { 909 switch (event->header.type) {
862 case PERF_RECORD_SAMPLE: 910 case PERF_RECORD_SAMPLE:
863 dump_sample(evsel, event, sample); 911 dump_sample(evsel, event, sample);
864 if (evsel == NULL) { 912 if (evsel == NULL) {
865 ++session->stats.nr_unknown_id; 913 ++evlist->stats.nr_unknown_id;
866 return 0; 914 return 0;
867 } 915 }
868 if (machine == NULL) { 916 if (machine == NULL) {
869 ++session->stats.nr_unprocessable_samples; 917 ++evlist->stats.nr_unprocessable_samples;
870 return 0; 918 return 0;
871 } 919 }
872 return perf_session__deliver_sample(session, tool, event, 920 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
873 sample, evsel, machine);
874 case PERF_RECORD_MMAP: 921 case PERF_RECORD_MMAP:
875 return tool->mmap(tool, event, sample, machine); 922 return tool->mmap(tool, event, sample, machine);
876 case PERF_RECORD_MMAP2: 923 case PERF_RECORD_MMAP2:
@@ -883,7 +930,7 @@ int perf_session__deliver_event(struct perf_session *session,
883 return tool->exit(tool, event, sample, machine); 930 return tool->exit(tool, event, sample, machine);
884 case PERF_RECORD_LOST: 931 case PERF_RECORD_LOST:
885 if (tool->lost == perf_event__process_lost) 932 if (tool->lost == perf_event__process_lost)
886 session->stats.total_lost += event->lost.lost; 933 evlist->stats.total_lost += event->lost.lost;
887 return tool->lost(tool, event, sample, machine); 934 return tool->lost(tool, event, sample, machine);
888 case PERF_RECORD_READ: 935 case PERF_RECORD_READ:
889 return tool->read(tool, event, sample, evsel, machine); 936 return tool->read(tool, event, sample, evsel, machine);
@@ -892,20 +939,21 @@ int perf_session__deliver_event(struct perf_session *session,
892 case PERF_RECORD_UNTHROTTLE: 939 case PERF_RECORD_UNTHROTTLE:
893 return tool->unthrottle(tool, event, sample, machine); 940 return tool->unthrottle(tool, event, sample, machine);
894 default: 941 default:
895 ++session->stats.nr_unknown_events; 942 ++evlist->stats.nr_unknown_events;
896 return -1; 943 return -1;
897 } 944 }
898} 945}
899 946
900static s64 perf_session__process_user_event(struct perf_session *session, 947static s64 perf_session__process_user_event(struct perf_session *session,
901 union perf_event *event, 948 union perf_event *event,
902 struct perf_tool *tool,
903 u64 file_offset) 949 u64 file_offset)
904{ 950{
951 struct ordered_events *oe = &session->ordered_events;
952 struct perf_tool *tool = session->tool;
905 int fd = perf_data_file__fd(session->file); 953 int fd = perf_data_file__fd(session->file);
906 int err; 954 int err;
907 955
908 dump_event(session, event, file_offset, NULL); 956 dump_event(session->evlist, event, file_offset, NULL);
909 957
910 /* These events are processed right away */ 958 /* These events are processed right away */
911 switch (event->header.type) { 959 switch (event->header.type) {
@@ -929,7 +977,7 @@ static s64 perf_session__process_user_event(struct perf_session *session,
929 case PERF_RECORD_HEADER_BUILD_ID: 977 case PERF_RECORD_HEADER_BUILD_ID:
930 return tool->build_id(tool, event, session); 978 return tool->build_id(tool, event, session);
931 case PERF_RECORD_FINISHED_ROUND: 979 case PERF_RECORD_FINISHED_ROUND:
932 return tool->finished_round(tool, event, session); 980 return tool->finished_round(tool, event, oe);
933 case PERF_RECORD_ID_INDEX: 981 case PERF_RECORD_ID_INDEX:
934 return tool->id_index(tool, event, session); 982 return tool->id_index(tool, event, session);
935 default: 983 default:
@@ -939,15 +987,17 @@ static s64 perf_session__process_user_event(struct perf_session *session,
939 987
940int perf_session__deliver_synth_event(struct perf_session *session, 988int perf_session__deliver_synth_event(struct perf_session *session,
941 union perf_event *event, 989 union perf_event *event,
942 struct perf_sample *sample, 990 struct perf_sample *sample)
943 struct perf_tool *tool)
944{ 991{
945 events_stats__inc(&session->stats, event->header.type); 992 struct perf_evlist *evlist = session->evlist;
993 struct perf_tool *tool = session->tool;
994
995 events_stats__inc(&evlist->stats, event->header.type);
946 996
947 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 997 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
948 return perf_session__process_user_event(session, event, tool, 0); 998 return perf_session__process_user_event(session, event, 0);
949 999
950 return perf_session__deliver_event(session, event, sample, tool, 0); 1000 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
951} 1001}
952 1002
953static void event_swap(union perf_event *event, bool sample_id_all) 1003static void event_swap(union perf_event *event, bool sample_id_all)
@@ -1015,40 +1065,39 @@ out_parse_sample:
1015} 1065}
1016 1066
1017static s64 perf_session__process_event(struct perf_session *session, 1067static s64 perf_session__process_event(struct perf_session *session,
1018 union perf_event *event, 1068 union perf_event *event, u64 file_offset)
1019 struct perf_tool *tool,
1020 u64 file_offset)
1021{ 1069{
1070 struct perf_evlist *evlist = session->evlist;
1071 struct perf_tool *tool = session->tool;
1022 struct perf_sample sample; 1072 struct perf_sample sample;
1023 int ret; 1073 int ret;
1024 1074
1025 if (session->header.needs_swap) 1075 if (session->header.needs_swap)
1026 event_swap(event, perf_evlist__sample_id_all(session->evlist)); 1076 event_swap(event, perf_evlist__sample_id_all(evlist));
1027 1077
1028 if (event->header.type >= PERF_RECORD_HEADER_MAX) 1078 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1029 return -EINVAL; 1079 return -EINVAL;
1030 1080
1031 events_stats__inc(&session->stats, event->header.type); 1081 events_stats__inc(&evlist->stats, event->header.type);
1032 1082
1033 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1083 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1034 return perf_session__process_user_event(session, event, tool, file_offset); 1084 return perf_session__process_user_event(session, event, file_offset);
1035 1085
1036 /* 1086 /*
1037 * For all kernel events we get the sample data 1087 * For all kernel events we get the sample data
1038 */ 1088 */
1039 ret = perf_evlist__parse_sample(session->evlist, event, &sample); 1089 ret = perf_evlist__parse_sample(evlist, event, &sample);
1040 if (ret) 1090 if (ret)
1041 return ret; 1091 return ret;
1042 1092
1043 if (tool->ordered_events) { 1093 if (tool->ordered_events) {
1044 ret = perf_session_queue_event(session, event, tool, &sample, 1094 ret = perf_session__queue_event(session, event, &sample, file_offset);
1045 file_offset);
1046 if (ret != -ETIME) 1095 if (ret != -ETIME)
1047 return ret; 1096 return ret;
1048 } 1097 }
1049 1098
1050 return perf_session__deliver_event(session, event, &sample, tool, 1099 return machines__deliver_event(&session->machines, evlist, event,
1051 file_offset); 1100 &sample, tool, file_offset);
1052} 1101}
1053 1102
1054void perf_event_header__bswap(struct perf_event_header *hdr) 1103void perf_event_header__bswap(struct perf_event_header *hdr)
@@ -1076,54 +1125,57 @@ static struct thread *perf_session__register_idle_thread(struct perf_session *se
1076 return thread; 1125 return thread;
1077} 1126}
1078 1127
1079static void perf_session__warn_about_errors(const struct perf_session *session, 1128static void perf_session__warn_about_errors(const struct perf_session *session)
1080 const struct perf_tool *tool)
1081{ 1129{
1082 if (tool->lost == perf_event__process_lost && 1130 const struct events_stats *stats = &session->evlist->stats;
1083 session->stats.nr_events[PERF_RECORD_LOST] != 0) { 1131 const struct ordered_events *oe = &session->ordered_events;
1132
1133 if (session->tool->lost == perf_event__process_lost &&
1134 stats->nr_events[PERF_RECORD_LOST] != 0) {
1084 ui__warning("Processed %d events and lost %d chunks!\n\n" 1135 ui__warning("Processed %d events and lost %d chunks!\n\n"
1085 "Check IO/CPU overload!\n\n", 1136 "Check IO/CPU overload!\n\n",
1086 session->stats.nr_events[0], 1137 stats->nr_events[0],
1087 session->stats.nr_events[PERF_RECORD_LOST]); 1138 stats->nr_events[PERF_RECORD_LOST]);
1088 } 1139 }
1089 1140
1090 if (session->stats.nr_unknown_events != 0) { 1141 if (stats->nr_unknown_events != 0) {
1091 ui__warning("Found %u unknown events!\n\n" 1142 ui__warning("Found %u unknown events!\n\n"
1092 "Is this an older tool processing a perf.data " 1143 "Is this an older tool processing a perf.data "
1093 "file generated by a more recent tool?\n\n" 1144 "file generated by a more recent tool?\n\n"
1094 "If that is not the case, consider " 1145 "If that is not the case, consider "
1095 "reporting to linux-kernel@vger.kernel.org.\n\n", 1146 "reporting to linux-kernel@vger.kernel.org.\n\n",
1096 session->stats.nr_unknown_events); 1147 stats->nr_unknown_events);
1097 } 1148 }
1098 1149
1099 if (session->stats.nr_unknown_id != 0) { 1150 if (stats->nr_unknown_id != 0) {
1100 ui__warning("%u samples with id not present in the header\n", 1151 ui__warning("%u samples with id not present in the header\n",
1101 session->stats.nr_unknown_id); 1152 stats->nr_unknown_id);
1102 } 1153 }
1103 1154
1104 if (session->stats.nr_invalid_chains != 0) { 1155 if (stats->nr_invalid_chains != 0) {
1105 ui__warning("Found invalid callchains!\n\n" 1156 ui__warning("Found invalid callchains!\n\n"
1106 "%u out of %u events were discarded for this reason.\n\n" 1157 "%u out of %u events were discarded for this reason.\n\n"
1107 "Consider reporting to linux-kernel@vger.kernel.org.\n\n", 1158 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1108 session->stats.nr_invalid_chains, 1159 stats->nr_invalid_chains,
1109 session->stats.nr_events[PERF_RECORD_SAMPLE]); 1160 stats->nr_events[PERF_RECORD_SAMPLE]);
1110 } 1161 }
1111 1162
1112 if (session->stats.nr_unprocessable_samples != 0) { 1163 if (stats->nr_unprocessable_samples != 0) {
1113 ui__warning("%u unprocessable samples recorded.\n" 1164 ui__warning("%u unprocessable samples recorded.\n"
1114 "Do you have a KVM guest running and not using 'perf kvm'?\n", 1165 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1115 session->stats.nr_unprocessable_samples); 1166 stats->nr_unprocessable_samples);
1116 } 1167 }
1117 1168
1118 if (session->stats.nr_unordered_events != 0) 1169 if (oe->nr_unordered_events != 0)
1119 ui__warning("%u out of order events recorded.\n", session->stats.nr_unordered_events); 1170 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1120} 1171}
1121 1172
1122volatile int session_done; 1173volatile int session_done;
1123 1174
1124static int __perf_session__process_pipe_events(struct perf_session *session, 1175static int __perf_session__process_pipe_events(struct perf_session *session)
1125 struct perf_tool *tool)
1126{ 1176{
1177 struct ordered_events *oe = &session->ordered_events;
1178 struct perf_tool *tool = session->tool;
1127 int fd = perf_data_file__fd(session->file); 1179 int fd = perf_data_file__fd(session->file);
1128 union perf_event *event; 1180 union perf_event *event;
1129 uint32_t size, cur_size = 0; 1181 uint32_t size, cur_size = 0;
@@ -1187,7 +1239,7 @@ more:
1187 } 1239 }
1188 } 1240 }
1189 1241
1190 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) { 1242 if ((skip = perf_session__process_event(session, event, head)) < 0) {
1191 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1243 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1192 head, event->header.size, event->header.type); 1244 head, event->header.size, event->header.type);
1193 err = -EINVAL; 1245 err = -EINVAL;
@@ -1203,10 +1255,10 @@ more:
1203 goto more; 1255 goto more;
1204done: 1256done:
1205 /* do the final flush for ordered samples */ 1257 /* do the final flush for ordered samples */
1206 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL); 1258 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1207out_err: 1259out_err:
1208 free(buf); 1260 free(buf);
1209 perf_session__warn_about_errors(session, tool); 1261 perf_session__warn_about_errors(session);
1210 ordered_events__free(&session->ordered_events); 1262 ordered_events__free(&session->ordered_events);
1211 return err; 1263 return err;
1212} 1264}
@@ -1253,8 +1305,10 @@ fetch_mmaped_event(struct perf_session *session,
1253 1305
1254static int __perf_session__process_events(struct perf_session *session, 1306static int __perf_session__process_events(struct perf_session *session,
1255 u64 data_offset, u64 data_size, 1307 u64 data_offset, u64 data_size,
1256 u64 file_size, struct perf_tool *tool) 1308 u64 file_size)
1257{ 1309{
1310 struct ordered_events *oe = &session->ordered_events;
1311 struct perf_tool *tool = session->tool;
1258 int fd = perf_data_file__fd(session->file); 1312 int fd = perf_data_file__fd(session->file);
1259 u64 head, page_offset, file_offset, file_pos, size; 1313 u64 head, page_offset, file_offset, file_pos, size;
1260 int err, mmap_prot, mmap_flags, map_idx = 0; 1314 int err, mmap_prot, mmap_flags, map_idx = 0;
@@ -1323,8 +1377,7 @@ more:
1323 size = event->header.size; 1377 size = event->header.size;
1324 1378
1325 if (size < sizeof(struct perf_event_header) || 1379 if (size < sizeof(struct perf_event_header) ||
1326 (skip = perf_session__process_event(session, event, tool, file_pos)) 1380 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
1327 < 0) {
1328 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1381 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1329 file_offset + head, event->header.size, 1382 file_offset + head, event->header.size,
1330 event->header.type); 1383 event->header.type);
@@ -1348,17 +1401,16 @@ more:
1348 1401
1349out: 1402out:
1350 /* do the final flush for ordered samples */ 1403 /* do the final flush for ordered samples */
1351 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL); 1404 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1352out_err: 1405out_err:
1353 ui_progress__finish(); 1406 ui_progress__finish();
1354 perf_session__warn_about_errors(session, tool); 1407 perf_session__warn_about_errors(session);
1355 ordered_events__free(&session->ordered_events); 1408 ordered_events__free(&session->ordered_events);
1356 session->one_mmap = false; 1409 session->one_mmap = false;
1357 return err; 1410 return err;
1358} 1411}
1359 1412
1360int perf_session__process_events(struct perf_session *session, 1413int perf_session__process_events(struct perf_session *session)
1361 struct perf_tool *tool)
1362{ 1414{
1363 u64 size = perf_data_file__size(session->file); 1415 u64 size = perf_data_file__size(session->file);
1364 int err; 1416 int err;
@@ -1369,10 +1421,9 @@ int perf_session__process_events(struct perf_session *session,
1369 if (!perf_data_file__is_pipe(session->file)) 1421 if (!perf_data_file__is_pipe(session->file))
1370 err = __perf_session__process_events(session, 1422 err = __perf_session__process_events(session,
1371 session->header.data_offset, 1423 session->header.data_offset,
1372 session->header.data_size, 1424 session->header.data_size, size);
1373 size, tool);
1374 else 1425 else
1375 err = __perf_session__process_pipe_events(session, tool); 1426 err = __perf_session__process_pipe_events(session);
1376 1427
1377 return err; 1428 return err;
1378} 1429}
@@ -1415,6 +1466,9 @@ int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1415 1466
1416 for (i = 0; i < MAP__NR_TYPES; ++i) { 1467 for (i = 0; i < MAP__NR_TYPES; ++i) {
1417 struct kmap *kmap = map__kmap(maps[i]); 1468 struct kmap *kmap = map__kmap(maps[i]);
1469
1470 if (!kmap)
1471 continue;
1418 kmap->ref_reloc_sym = ref; 1472 kmap->ref_reloc_sym = ref;
1419 } 1473 }
1420 1474
@@ -1436,7 +1490,7 @@ size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1436{ 1490{
1437 size_t ret = fprintf(fp, "Aggregated stats:\n"); 1491 size_t ret = fprintf(fp, "Aggregated stats:\n");
1438 1492
1439 ret += events_stats__fprintf(&session->stats, fp); 1493 ret += events_stats__fprintf(&session->evlist->stats, fp);
1440 return ret; 1494 return ret;
1441} 1495}
1442 1496
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index 6d663dc76404..d5fa7b7916ef 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -20,13 +20,13 @@ struct perf_session {
20 struct machines machines; 20 struct machines machines;
21 struct perf_evlist *evlist; 21 struct perf_evlist *evlist;
22 struct trace_event tevent; 22 struct trace_event tevent;
23 struct events_stats stats;
24 bool repipe; 23 bool repipe;
25 bool one_mmap; 24 bool one_mmap;
26 void *one_mmap_addr; 25 void *one_mmap_addr;
27 u64 one_mmap_offset; 26 u64 one_mmap_offset;
28 struct ordered_events ordered_events; 27 struct ordered_events ordered_events;
29 struct perf_data_file *file; 28 struct perf_data_file *file;
29 struct perf_tool *tool;
30}; 30};
31 31
32#define PRINT_IP_OPT_IP (1<<0) 32#define PRINT_IP_OPT_IP (1<<0)
@@ -49,20 +49,13 @@ int perf_session__peek_event(struct perf_session *session, off_t file_offset,
49 union perf_event **event_ptr, 49 union perf_event **event_ptr,
50 struct perf_sample *sample); 50 struct perf_sample *sample);
51 51
52int perf_session__process_events(struct perf_session *session, 52int perf_session__process_events(struct perf_session *session);
53 struct perf_tool *tool);
54 53
55int perf_session_queue_event(struct perf_session *s, union perf_event *event, 54int perf_session__queue_event(struct perf_session *s, union perf_event *event,
56 struct perf_tool *tool, struct perf_sample *sample, 55 struct perf_sample *sample, u64 file_offset);
57 u64 file_offset);
58 56
59void perf_tool__fill_defaults(struct perf_tool *tool); 57void perf_tool__fill_defaults(struct perf_tool *tool);
60 58
61int perf_session__deliver_event(struct perf_session *session,
62 union perf_event *event,
63 struct perf_sample *sample,
64 struct perf_tool *tool, u64 file_offset);
65
66int perf_session__resolve_callchain(struct perf_session *session, 59int perf_session__resolve_callchain(struct perf_session *session,
67 struct perf_evsel *evsel, 60 struct perf_evsel *evsel,
68 struct thread *thread, 61 struct thread *thread,
@@ -126,8 +119,7 @@ extern volatile int session_done;
126 119
127int perf_session__deliver_synth_event(struct perf_session *session, 120int perf_session__deliver_synth_event(struct perf_session *session,
128 union perf_event *event, 121 union perf_event *event,
129 struct perf_sample *sample, 122 struct perf_sample *sample);
130 struct perf_tool *tool);
131 123
132int perf_event__process_id_index(struct perf_tool *tool, 124int perf_event__process_id_index(struct perf_tool *tool,
133 union perf_event *event, 125 union perf_event *event,
diff --git a/tools/perf/util/setup.py b/tools/perf/util/setup.py
index d0aee4b9dfd4..1833103768cb 100644
--- a/tools/perf/util/setup.py
+++ b/tools/perf/util/setup.py
@@ -25,7 +25,7 @@ cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter'
25build_lib = getenv('PYTHON_EXTBUILD_LIB') 25build_lib = getenv('PYTHON_EXTBUILD_LIB')
26build_tmp = getenv('PYTHON_EXTBUILD_TMP') 26build_tmp = getenv('PYTHON_EXTBUILD_TMP')
27libtraceevent = getenv('LIBTRACEEVENT') 27libtraceevent = getenv('LIBTRACEEVENT')
28libapikfs = getenv('LIBAPIKFS') 28libapikfs = getenv('LIBAPI')
29 29
30ext_sources = [f.strip() for f in file('util/python-ext-sources') 30ext_sources = [f.strip() for f in file('util/python-ext-sources')
31 if len(f.strip()) > 0 and f[0] != '#'] 31 if len(f.strip()) > 0 and f[0] != '#']
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 7a39c1ed8d37..4593f36ecc4c 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -1463,6 +1463,15 @@ int sort_dimension__add(const char *tok)
1463 sort__has_parent = 1; 1463 sort__has_parent = 1;
1464 } else if (sd->entry == &sort_sym) { 1464 } else if (sd->entry == &sort_sym) {
1465 sort__has_sym = 1; 1465 sort__has_sym = 1;
1466 /*
1467 * perf diff displays the performance difference amongst
1468 * two or more perf.data files. Those files could come
1469 * from different binaries. So we should not compare
1470 * their ips, but the name of symbol.
1471 */
1472 if (sort__mode == SORT_MODE__DIFF)
1473 sd->entry->se_collapse = sort__sym_sort;
1474
1466 } else if (sd->entry == &sort_dso) { 1475 } else if (sd->entry == &sort_dso) {
1467 sort__has_dso = 1; 1476 sort__has_dso = 1;
1468 } 1477 }
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
index c03e4ff8beff..846036a921dc 100644
--- a/tools/perf/util/sort.h
+++ b/tools/perf/util/sort.h
@@ -44,6 +44,7 @@ extern struct sort_entry sort_dso_to;
44extern struct sort_entry sort_sym_from; 44extern struct sort_entry sort_sym_from;
45extern struct sort_entry sort_sym_to; 45extern struct sort_entry sort_sym_to;
46extern enum sort_type sort__first_dimension; 46extern enum sort_type sort__first_dimension;
47extern const char default_mem_sort_order[];
47 48
48struct he_stat { 49struct he_stat {
49 u64 period; 50 u64 period;
@@ -102,7 +103,6 @@ struct hist_entry {
102 103
103 bool init_have_children; 104 bool init_have_children;
104 char level; 105 char level;
105 bool used;
106 u8 filtered; 106 u8 filtered;
107 char *srcline; 107 char *srcline;
108 struct symbol *parent; 108 struct symbol *parent;
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 33b7a2aef713..a7ab6063e038 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -74,6 +74,10 @@ static inline uint8_t elf_sym__type(const GElf_Sym *sym)
74 return GELF_ST_TYPE(sym->st_info); 74 return GELF_ST_TYPE(sym->st_info);
75} 75}
76 76
77#ifndef STT_GNU_IFUNC
78#define STT_GNU_IFUNC 10
79#endif
80
77static inline int elf_sym__is_function(const GElf_Sym *sym) 81static inline int elf_sym__is_function(const GElf_Sym *sym)
78{ 82{
79 return (elf_sym__type(sym) == STT_FUNC || 83 return (elf_sym__type(sym) == STT_FUNC ||
@@ -575,32 +579,37 @@ static int dso__swap_init(struct dso *dso, unsigned char eidata)
575static int decompress_kmodule(struct dso *dso, const char *name, 579static int decompress_kmodule(struct dso *dso, const char *name,
576 enum dso_binary_type type) 580 enum dso_binary_type type)
577{ 581{
578 int fd; 582 int fd = -1;
579 const char *ext = strrchr(name, '.');
580 char tmpbuf[] = "/tmp/perf-kmod-XXXXXX"; 583 char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
584 struct kmod_path m;
581 585
582 if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP && 586 if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
583 type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP && 587 type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP &&
584 type != DSO_BINARY_TYPE__BUILD_ID_CACHE) 588 type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
585 return -1; 589 return -1;
586 590
587 if (!ext || !is_supported_compression(ext + 1)) { 591 if (type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
588 ext = strrchr(dso->name, '.'); 592 name = dso->long_name;
589 if (!ext || !is_supported_compression(ext + 1))
590 return -1;
591 }
592 593
593 fd = mkstemp(tmpbuf); 594 if (kmod_path__parse_ext(&m, name) || !m.comp)
594 if (fd < 0)
595 return -1; 595 return -1;
596 596
597 if (!decompress_to_file(ext + 1, name, fd)) { 597 fd = mkstemp(tmpbuf);
598 if (fd < 0) {
599 dso->load_errno = errno;
600 goto out;
601 }
602
603 if (!decompress_to_file(m.ext, name, fd)) {
604 dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
598 close(fd); 605 close(fd);
599 fd = -1; 606 fd = -1;
600 } 607 }
601 608
602 unlink(tmpbuf); 609 unlink(tmpbuf);
603 610
611out:
612 free(m.ext);
604 return fd; 613 return fd;
605} 614}
606 615
@@ -629,37 +638,49 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
629 Elf *elf; 638 Elf *elf;
630 int fd; 639 int fd;
631 640
632 if (dso__needs_decompress(dso)) 641 if (dso__needs_decompress(dso)) {
633 fd = decompress_kmodule(dso, name, type); 642 fd = decompress_kmodule(dso, name, type);
634 else 643 if (fd < 0)
644 return -1;
645 } else {
635 fd = open(name, O_RDONLY); 646 fd = open(name, O_RDONLY);
636 647 if (fd < 0) {
637 if (fd < 0) 648 dso->load_errno = errno;
638 return -1; 649 return -1;
650 }
651 }
639 652
640 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 653 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
641 if (elf == NULL) { 654 if (elf == NULL) {
642 pr_debug("%s: cannot read %s ELF file.\n", __func__, name); 655 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
656 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
643 goto out_close; 657 goto out_close;
644 } 658 }
645 659
646 if (gelf_getehdr(elf, &ehdr) == NULL) { 660 if (gelf_getehdr(elf, &ehdr) == NULL) {
661 dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
647 pr_debug("%s: cannot get elf header.\n", __func__); 662 pr_debug("%s: cannot get elf header.\n", __func__);
648 goto out_elf_end; 663 goto out_elf_end;
649 } 664 }
650 665
651 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) 666 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
667 dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
652 goto out_elf_end; 668 goto out_elf_end;
669 }
653 670
654 /* Always reject images with a mismatched build-id: */ 671 /* Always reject images with a mismatched build-id: */
655 if (dso->has_build_id) { 672 if (dso->has_build_id) {
656 u8 build_id[BUILD_ID_SIZE]; 673 u8 build_id[BUILD_ID_SIZE];
657 674
658 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) 675 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
676 dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
659 goto out_elf_end; 677 goto out_elf_end;
678 }
660 679
661 if (!dso__build_id_equal(dso, build_id)) 680 if (!dso__build_id_equal(dso, build_id)) {
681 dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
662 goto out_elf_end; 682 goto out_elf_end;
683 }
663 } 684 }
664 685
665 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64); 686 ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
@@ -695,8 +716,10 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
695 } 716 }
696 717
697 ss->name = strdup(name); 718 ss->name = strdup(name);
698 if (!ss->name) 719 if (!ss->name) {
720 dso->load_errno = errno;
699 goto out_elf_end; 721 goto out_elf_end;
722 }
700 723
701 ss->elf = elf; 724 ss->elf = elf;
702 ss->fd = fd; 725 ss->fd = fd;
@@ -753,6 +776,7 @@ int dso__load_sym(struct dso *dso, struct map *map,
753 symbol_filter_t filter, int kmodule) 776 symbol_filter_t filter, int kmodule)
754{ 777{
755 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL; 778 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
779 struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
756 struct map *curr_map = map; 780 struct map *curr_map = map;
757 struct dso *curr_dso = dso; 781 struct dso *curr_dso = dso;
758 Elf_Data *symstrs, *secstrs; 782 Elf_Data *symstrs, *secstrs;
@@ -768,6 +792,9 @@ int dso__load_sym(struct dso *dso, struct map *map,
768 int nr = 0; 792 int nr = 0;
769 bool remap_kernel = false, adjust_kernel_syms = false; 793 bool remap_kernel = false, adjust_kernel_syms = false;
770 794
795 if (kmap && !kmaps)
796 return -1;
797
771 dso->symtab_type = syms_ss->type; 798 dso->symtab_type = syms_ss->type;
772 dso->is_64_bit = syms_ss->is_64_bit; 799 dso->is_64_bit = syms_ss->is_64_bit;
773 dso->rel = syms_ss->ehdr.e_type == ET_REL; 800 dso->rel = syms_ss->ehdr.e_type == ET_REL;
@@ -864,10 +891,9 @@ int dso__load_sym(struct dso *dso, struct map *map,
864 /* Reject ARM ELF "mapping symbols": these aren't unique and 891 /* Reject ARM ELF "mapping symbols": these aren't unique and
865 * don't identify functions, so will confuse the profile 892 * don't identify functions, so will confuse the profile
866 * output: */ 893 * output: */
867 if (ehdr.e_machine == EM_ARM) { 894 if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
868 if (!strcmp(elf_name, "$a") || 895 if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
869 !strcmp(elf_name, "$d") || 896 && (elf_name[2] == '\0' || elf_name[2] == '.'))
870 !strcmp(elf_name, "$t"))
871 continue; 897 continue;
872 } 898 }
873 899
@@ -936,8 +962,10 @@ int dso__load_sym(struct dso *dso, struct map *map,
936 map->map_ip = map__map_ip; 962 map->map_ip = map__map_ip;
937 map->unmap_ip = map__unmap_ip; 963 map->unmap_ip = map__unmap_ip;
938 /* Ensure maps are correctly ordered */ 964 /* Ensure maps are correctly ordered */
939 map_groups__remove(kmap->kmaps, map); 965 if (kmaps) {
940 map_groups__insert(kmap->kmaps, map); 966 map_groups__remove(kmaps, map);
967 map_groups__insert(kmaps, map);
968 }
941 } 969 }
942 970
943 /* 971 /*
@@ -961,7 +989,7 @@ int dso__load_sym(struct dso *dso, struct map *map,
961 snprintf(dso_name, sizeof(dso_name), 989 snprintf(dso_name, sizeof(dso_name),
962 "%s%s", dso->short_name, section_name); 990 "%s%s", dso->short_name, section_name);
963 991
964 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name); 992 curr_map = map_groups__find_by_name(kmaps, map->type, dso_name);
965 if (curr_map == NULL) { 993 if (curr_map == NULL) {
966 u64 start = sym.st_value; 994 u64 start = sym.st_value;
967 995
@@ -991,7 +1019,7 @@ int dso__load_sym(struct dso *dso, struct map *map,
991 curr_map->unmap_ip = identity__map_ip; 1019 curr_map->unmap_ip = identity__map_ip;
992 } 1020 }
993 curr_dso->symtab_type = dso->symtab_type; 1021 curr_dso->symtab_type = dso->symtab_type;
994 map_groups__insert(kmap->kmaps, curr_map); 1022 map_groups__insert(kmaps, curr_map);
995 /* 1023 /*
996 * The new DSO should go to the kernel DSOS 1024 * The new DSO should go to the kernel DSOS
997 */ 1025 */
@@ -1045,14 +1073,15 @@ new_symbol:
1045 * For misannotated, zeroed, ASM function sizes. 1073 * For misannotated, zeroed, ASM function sizes.
1046 */ 1074 */
1047 if (nr > 0) { 1075 if (nr > 0) {
1048 symbols__fixup_duplicate(&dso->symbols[map->type]); 1076 if (!symbol_conf.allow_aliases)
1077 symbols__fixup_duplicate(&dso->symbols[map->type]);
1049 symbols__fixup_end(&dso->symbols[map->type]); 1078 symbols__fixup_end(&dso->symbols[map->type]);
1050 if (kmap) { 1079 if (kmap) {
1051 /* 1080 /*
1052 * We need to fixup this here too because we create new 1081 * We need to fixup this here too because we create new
1053 * maps here, for things like vsyscall sections. 1082 * maps here, for things like vsyscall sections.
1054 */ 1083 */
1055 __map_groups__fixup_end(kmap->kmaps, map->type); 1084 __map_groups__fixup_end(kmaps, map->type);
1056 } 1085 }
1057 } 1086 }
1058 err = nr; 1087 err = nr;
diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index d7efb03b3f9a..fd8477cacf88 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -246,13 +246,12 @@ out:
246 return ret; 246 return ret;
247} 247}
248 248
249int symsrc__init(struct symsrc *ss, struct dso *dso __maybe_unused, 249int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
250 const char *name,
251 enum dso_binary_type type) 250 enum dso_binary_type type)
252{ 251{
253 int fd = open(name, O_RDONLY); 252 int fd = open(name, O_RDONLY);
254 if (fd < 0) 253 if (fd < 0)
255 return -1; 254 goto out_errno;
256 255
257 ss->name = strdup(name); 256 ss->name = strdup(name);
258 if (!ss->name) 257 if (!ss->name)
@@ -264,6 +263,8 @@ int symsrc__init(struct symsrc *ss, struct dso *dso __maybe_unused,
264 return 0; 263 return 0;
265out_close: 264out_close:
266 close(fd); 265 close(fd);
266out_errno:
267 dso->load_errno = errno;
267 return -1; 268 return -1;
268} 269}
269 270
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index a69066865a55..201f6c4ca738 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -15,6 +15,7 @@
15#include "machine.h" 15#include "machine.h"
16#include "symbol.h" 16#include "symbol.h"
17#include "strlist.h" 17#include "strlist.h"
18#include "intlist.h"
18#include "header.h" 19#include "header.h"
19 20
20#include <elf.h> 21#include <elf.h>
@@ -629,13 +630,16 @@ static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
629static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map, 630static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
630 symbol_filter_t filter) 631 symbol_filter_t filter)
631{ 632{
632 struct map_groups *kmaps = map__kmap(map)->kmaps; 633 struct map_groups *kmaps = map__kmaps(map);
633 struct map *curr_map; 634 struct map *curr_map;
634 struct symbol *pos; 635 struct symbol *pos;
635 int count = 0, moved = 0; 636 int count = 0, moved = 0;
636 struct rb_root *root = &dso->symbols[map->type]; 637 struct rb_root *root = &dso->symbols[map->type];
637 struct rb_node *next = rb_first(root); 638 struct rb_node *next = rb_first(root);
638 639
640 if (!kmaps)
641 return -1;
642
639 while (next) { 643 while (next) {
640 char *module; 644 char *module;
641 645
@@ -681,8 +685,8 @@ static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
681static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta, 685static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
682 symbol_filter_t filter) 686 symbol_filter_t filter)
683{ 687{
684 struct map_groups *kmaps = map__kmap(map)->kmaps; 688 struct map_groups *kmaps = map__kmaps(map);
685 struct machine *machine = kmaps->machine; 689 struct machine *machine;
686 struct map *curr_map = map; 690 struct map *curr_map = map;
687 struct symbol *pos; 691 struct symbol *pos;
688 int count = 0, moved = 0; 692 int count = 0, moved = 0;
@@ -690,6 +694,11 @@ static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
690 struct rb_node *next = rb_first(root); 694 struct rb_node *next = rb_first(root);
691 int kernel_range = 0; 695 int kernel_range = 0;
692 696
697 if (!kmaps)
698 return -1;
699
700 machine = kmaps->machine;
701
693 while (next) { 702 while (next) {
694 char *module; 703 char *module;
695 704
@@ -1024,9 +1033,12 @@ static bool filename_from_kallsyms_filename(char *filename,
1024static int validate_kcore_modules(const char *kallsyms_filename, 1033static int validate_kcore_modules(const char *kallsyms_filename,
1025 struct map *map) 1034 struct map *map)
1026{ 1035{
1027 struct map_groups *kmaps = map__kmap(map)->kmaps; 1036 struct map_groups *kmaps = map__kmaps(map);
1028 char modules_filename[PATH_MAX]; 1037 char modules_filename[PATH_MAX];
1029 1038
1039 if (!kmaps)
1040 return -EINVAL;
1041
1030 if (!filename_from_kallsyms_filename(modules_filename, "modules", 1042 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1031 kallsyms_filename)) 1043 kallsyms_filename))
1032 return -EINVAL; 1044 return -EINVAL;
@@ -1042,6 +1054,9 @@ static int validate_kcore_addresses(const char *kallsyms_filename,
1042{ 1054{
1043 struct kmap *kmap = map__kmap(map); 1055 struct kmap *kmap = map__kmap(map);
1044 1056
1057 if (!kmap)
1058 return -EINVAL;
1059
1045 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { 1060 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1046 u64 start; 1061 u64 start;
1047 1062
@@ -1080,8 +1095,8 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1080static int dso__load_kcore(struct dso *dso, struct map *map, 1095static int dso__load_kcore(struct dso *dso, struct map *map,
1081 const char *kallsyms_filename) 1096 const char *kallsyms_filename)
1082{ 1097{
1083 struct map_groups *kmaps = map__kmap(map)->kmaps; 1098 struct map_groups *kmaps = map__kmaps(map);
1084 struct machine *machine = kmaps->machine; 1099 struct machine *machine;
1085 struct kcore_mapfn_data md; 1100 struct kcore_mapfn_data md;
1086 struct map *old_map, *new_map, *replacement_map = NULL; 1101 struct map *old_map, *new_map, *replacement_map = NULL;
1087 bool is_64_bit; 1102 bool is_64_bit;
@@ -1089,6 +1104,11 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
1089 char kcore_filename[PATH_MAX]; 1104 char kcore_filename[PATH_MAX];
1090 struct symbol *sym; 1105 struct symbol *sym;
1091 1106
1107 if (!kmaps)
1108 return -EINVAL;
1109
1110 machine = kmaps->machine;
1111
1092 /* This function requires that the map is the kernel map */ 1112 /* This function requires that the map is the kernel map */
1093 if (map != machine->vmlinux_maps[map->type]) 1113 if (map != machine->vmlinux_maps[map->type])
1094 return -EINVAL; 1114 return -EINVAL;
@@ -1201,6 +1221,9 @@ static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1201 struct kmap *kmap = map__kmap(map); 1221 struct kmap *kmap = map__kmap(map);
1202 u64 addr; 1222 u64 addr;
1203 1223
1224 if (!kmap)
1225 return -1;
1226
1204 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) 1227 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1205 return 0; 1228 return 0;
1206 1229
@@ -1859,6 +1882,20 @@ int setup_list(struct strlist **list, const char *list_str,
1859 return 0; 1882 return 0;
1860} 1883}
1861 1884
1885int setup_intlist(struct intlist **list, const char *list_str,
1886 const char *list_name)
1887{
1888 if (list_str == NULL)
1889 return 0;
1890
1891 *list = intlist__new(list_str);
1892 if (!*list) {
1893 pr_err("problems parsing %s list\n", list_name);
1894 return -1;
1895 }
1896 return 0;
1897}
1898
1862static bool symbol__read_kptr_restrict(void) 1899static bool symbol__read_kptr_restrict(void)
1863{ 1900{
1864 bool value = false; 1901 bool value = false;
@@ -1909,9 +1946,17 @@ int symbol__init(struct perf_session_env *env)
1909 symbol_conf.comm_list_str, "comm") < 0) 1946 symbol_conf.comm_list_str, "comm") < 0)
1910 goto out_free_dso_list; 1947 goto out_free_dso_list;
1911 1948
1949 if (setup_intlist(&symbol_conf.pid_list,
1950 symbol_conf.pid_list_str, "pid") < 0)
1951 goto out_free_comm_list;
1952
1953 if (setup_intlist(&symbol_conf.tid_list,
1954 symbol_conf.tid_list_str, "tid") < 0)
1955 goto out_free_pid_list;
1956
1912 if (setup_list(&symbol_conf.sym_list, 1957 if (setup_list(&symbol_conf.sym_list,
1913 symbol_conf.sym_list_str, "symbol") < 0) 1958 symbol_conf.sym_list_str, "symbol") < 0)
1914 goto out_free_comm_list; 1959 goto out_free_tid_list;
1915 1960
1916 /* 1961 /*
1917 * A path to symbols of "/" is identical to "" 1962 * A path to symbols of "/" is identical to ""
@@ -1930,6 +1975,10 @@ int symbol__init(struct perf_session_env *env)
1930 symbol_conf.initialized = true; 1975 symbol_conf.initialized = true;
1931 return 0; 1976 return 0;
1932 1977
1978out_free_tid_list:
1979 intlist__delete(symbol_conf.tid_list);
1980out_free_pid_list:
1981 intlist__delete(symbol_conf.pid_list);
1933out_free_comm_list: 1982out_free_comm_list:
1934 strlist__delete(symbol_conf.comm_list); 1983 strlist__delete(symbol_conf.comm_list);
1935out_free_dso_list: 1984out_free_dso_list:
@@ -1944,6 +1993,8 @@ void symbol__exit(void)
1944 strlist__delete(symbol_conf.sym_list); 1993 strlist__delete(symbol_conf.sym_list);
1945 strlist__delete(symbol_conf.dso_list); 1994 strlist__delete(symbol_conf.dso_list);
1946 strlist__delete(symbol_conf.comm_list); 1995 strlist__delete(symbol_conf.comm_list);
1996 intlist__delete(symbol_conf.tid_list);
1997 intlist__delete(symbol_conf.pid_list);
1947 vmlinux_path__exit(); 1998 vmlinux_path__exit();
1948 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 1999 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1949 symbol_conf.initialized = false; 2000 symbol_conf.initialized = false;
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 1650dcb3a67b..09561500164a 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -78,6 +78,7 @@ static inline size_t symbol__size(const struct symbol *sym)
78} 78}
79 79
80struct strlist; 80struct strlist;
81struct intlist;
81 82
82struct symbol_conf { 83struct symbol_conf {
83 unsigned short priv_size; 84 unsigned short priv_size;
@@ -87,6 +88,7 @@ struct symbol_conf {
87 ignore_vmlinux_buildid, 88 ignore_vmlinux_buildid,
88 show_kernel_path, 89 show_kernel_path,
89 use_modules, 90 use_modules,
91 allow_aliases,
90 sort_by_name, 92 sort_by_name,
91 show_nr_samples, 93 show_nr_samples,
92 show_total_period, 94 show_total_period,
@@ -114,6 +116,8 @@ struct symbol_conf {
114 const char *guestmount; 116 const char *guestmount;
115 const char *dso_list_str, 117 const char *dso_list_str,
116 *comm_list_str, 118 *comm_list_str,
119 *pid_list_str,
120 *tid_list_str,
117 *sym_list_str, 121 *sym_list_str,
118 *col_width_list_str; 122 *col_width_list_str;
119 struct strlist *dso_list, 123 struct strlist *dso_list,
@@ -123,6 +127,8 @@ struct symbol_conf {
123 *dso_to_list, 127 *dso_to_list,
124 *sym_from_list, 128 *sym_from_list,
125 *sym_to_list; 129 *sym_to_list;
130 struct intlist *pid_list,
131 *tid_list;
126 const char *symfs; 132 const char *symfs;
127}; 133};
128 134
@@ -294,5 +300,7 @@ int compare_proc_modules(const char *from, const char *to);
294 300
295int setup_list(struct strlist **list, const char *list_str, 301int setup_list(struct strlist **list, const char *list_str,
296 const char *list_name); 302 const char *list_name);
303int setup_intlist(struct intlist **list, const char *list_str,
304 const char *list_name);
297 305
298#endif /* __PERF_SYMBOL */ 306#endif /* __PERF_SYMBOL */
diff --git a/tools/perf/util/target.c b/tools/perf/util/target.c
index e74c5963dc7a..a53603b27e52 100644
--- a/tools/perf/util/target.c
+++ b/tools/perf/util/target.c
@@ -123,11 +123,8 @@ int target__strerror(struct target *target, int errnum,
123 if (errnum >= 0) { 123 if (errnum >= 0) {
124 const char *err = strerror_r(errnum, buf, buflen); 124 const char *err = strerror_r(errnum, buf, buflen);
125 125
126 if (err != buf) { 126 if (err != buf)
127 size_t len = strlen(err); 127 scnprintf(buf, buflen, "%s", err);
128 memcpy(buf, err, min(buflen - 1, len));
129 *(buf + min(buflen - 1, len)) = '\0';
130 }
131 128
132 return 0; 129 return 0;
133 } 130 }
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 9ebc8b1f9be5..1c8fbc9588c5 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -82,6 +82,20 @@ void thread__delete(struct thread *thread)
82 free(thread); 82 free(thread);
83} 83}
84 84
85struct thread *thread__get(struct thread *thread)
86{
87 ++thread->refcnt;
88 return thread;
89}
90
91void thread__put(struct thread *thread)
92{
93 if (thread && --thread->refcnt == 0) {
94 list_del_init(&thread->node);
95 thread__delete(thread);
96 }
97}
98
85struct comm *thread__comm(const struct thread *thread) 99struct comm *thread__comm(const struct thread *thread)
86{ 100{
87 if (list_empty(&thread->comm_list)) 101 if (list_empty(&thread->comm_list))
@@ -192,7 +206,6 @@ int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
192 err = thread__set_comm(thread, comm, timestamp); 206 err = thread__set_comm(thread, comm, timestamp);
193 if (err) 207 if (err)
194 return err; 208 return err;
195 thread->comm_set = true;
196 } 209 }
197 210
198 thread->ppid = parent->tid; 211 thread->ppid = parent->tid;
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 160fd066a7d1..9b8a54dc34a8 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -7,6 +7,7 @@
7#include <sys/types.h> 7#include <sys/types.h>
8#include "symbol.h" 8#include "symbol.h"
9#include <strlist.h> 9#include <strlist.h>
10#include <intlist.h>
10 11
11struct thread_stack; 12struct thread_stack;
12 13
@@ -20,6 +21,7 @@ struct thread {
20 pid_t tid; 21 pid_t tid;
21 pid_t ppid; 22 pid_t ppid;
22 int cpu; 23 int cpu;
24 int refcnt;
23 char shortname[3]; 25 char shortname[3];
24 bool comm_set; 26 bool comm_set;
25 bool dead; /* if set thread has exited */ 27 bool dead; /* if set thread has exited */
@@ -37,6 +39,18 @@ struct comm;
37struct thread *thread__new(pid_t pid, pid_t tid); 39struct thread *thread__new(pid_t pid, pid_t tid);
38int thread__init_map_groups(struct thread *thread, struct machine *machine); 40int thread__init_map_groups(struct thread *thread, struct machine *machine);
39void thread__delete(struct thread *thread); 41void thread__delete(struct thread *thread);
42
43struct thread *thread__get(struct thread *thread);
44void thread__put(struct thread *thread);
45
46static inline void __thread__zput(struct thread **thread)
47{
48 thread__put(*thread);
49 *thread = NULL;
50}
51
52#define thread__zput(thread) __thread__zput(&thread)
53
40static inline void thread__exited(struct thread *thread) 54static inline void thread__exited(struct thread *thread)
41{ 55{
42 thread->dead = true; 56 thread->dead = true;
@@ -87,6 +101,16 @@ static inline bool thread__is_filtered(struct thread *thread)
87 return true; 101 return true;
88 } 102 }
89 103
104 if (symbol_conf.pid_list &&
105 !intlist__has_entry(symbol_conf.pid_list, thread->pid_)) {
106 return true;
107 }
108
109 if (symbol_conf.tid_list &&
110 !intlist__has_entry(symbol_conf.tid_list, thread->tid)) {
111 return true;
112 }
113
90 return false; 114 return false;
91} 115}
92 116
diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h
index bb2708bbfaca..51d9e56c0f84 100644
--- a/tools/perf/util/tool.h
+++ b/tools/perf/util/tool.h
@@ -10,6 +10,7 @@ struct perf_evsel;
10struct perf_sample; 10struct perf_sample;
11struct perf_tool; 11struct perf_tool;
12struct machine; 12struct machine;
13struct ordered_events;
13 14
14typedef int (*event_sample)(struct perf_tool *tool, union perf_event *event, 15typedef int (*event_sample)(struct perf_tool *tool, union perf_event *event,
15 struct perf_sample *sample, 16 struct perf_sample *sample,
@@ -25,6 +26,9 @@ typedef int (*event_attr_op)(struct perf_tool *tool,
25typedef int (*event_op2)(struct perf_tool *tool, union perf_event *event, 26typedef int (*event_op2)(struct perf_tool *tool, union perf_event *event,
26 struct perf_session *session); 27 struct perf_session *session);
27 28
29typedef int (*event_oe)(struct perf_tool *tool, union perf_event *event,
30 struct ordered_events *oe);
31
28struct perf_tool { 32struct perf_tool {
29 event_sample sample, 33 event_sample sample,
30 read; 34 read;
@@ -38,8 +42,8 @@ struct perf_tool {
38 unthrottle; 42 unthrottle;
39 event_attr_op attr; 43 event_attr_op attr;
40 event_op2 tracing_data; 44 event_op2 tracing_data;
41 event_op2 finished_round, 45 event_oe finished_round;
42 build_id, 46 event_op2 build_id,
43 id_index; 47 id_index;
44 bool ordered_events; 48 bool ordered_events;
45 bool ordering_requires_timestamps; 49 bool ordering_requires_timestamps;
diff --git a/tools/perf/util/trace-event-parse.c b/tools/perf/util/trace-event-parse.c
index c36636fd825b..25d6c737be3e 100644
--- a/tools/perf/util/trace-event-parse.c
+++ b/tools/perf/util/trace-event-parse.c
@@ -112,8 +112,8 @@ unsigned long long read_size(struct event_format *event, void *ptr, int size)
112 return pevent_read_number(event->pevent, ptr, size); 112 return pevent_read_number(event->pevent, ptr, size);
113} 113}
114 114
115void event_format__print(struct event_format *event, 115void event_format__fprintf(struct event_format *event,
116 int cpu, void *data, int size) 116 int cpu, void *data, int size, FILE *fp)
117{ 117{
118 struct pevent_record record; 118 struct pevent_record record;
119 struct trace_seq s; 119 struct trace_seq s;
@@ -125,10 +125,16 @@ void event_format__print(struct event_format *event,
125 125
126 trace_seq_init(&s); 126 trace_seq_init(&s);
127 pevent_event_info(&s, event, &record); 127 pevent_event_info(&s, event, &record);
128 trace_seq_do_printf(&s); 128 trace_seq_do_fprintf(&s, fp);
129 trace_seq_destroy(&s); 129 trace_seq_destroy(&s);
130} 130}
131 131
132void event_format__print(struct event_format *event,
133 int cpu, void *data, int size)
134{
135 return event_format__fprintf(event, cpu, data, size, stdout);
136}
137
132void parse_proc_kallsyms(struct pevent *pevent, 138void parse_proc_kallsyms(struct pevent *pevent,
133 char *file, unsigned int size __maybe_unused) 139 char *file, unsigned int size __maybe_unused)
134{ 140{
diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c
index 5c9bdd1591a9..9df61059a85d 100644
--- a/tools/perf/util/trace-event-scripting.c
+++ b/tools/perf/util/trace-event-scripting.c
@@ -43,7 +43,6 @@ static int stop_script_unsupported(void)
43static void process_event_unsupported(union perf_event *event __maybe_unused, 43static void process_event_unsupported(union perf_event *event __maybe_unused,
44 struct perf_sample *sample __maybe_unused, 44 struct perf_sample *sample __maybe_unused,
45 struct perf_evsel *evsel __maybe_unused, 45 struct perf_evsel *evsel __maybe_unused,
46 struct thread *thread __maybe_unused,
47 struct addr_location *al __maybe_unused) 46 struct addr_location *al __maybe_unused)
48{ 47{
49} 48}
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index 52aaa19e1eb1..d5168f0be4ec 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -23,6 +23,9 @@ trace_event__tp_format(const char *sys, const char *name);
23 23
24int bigendian(void); 24int bigendian(void);
25 25
26void event_format__fprintf(struct event_format *event,
27 int cpu, void *data, int size, FILE *fp);
28
26void event_format__print(struct event_format *event, 29void event_format__print(struct event_format *event,
27 int cpu, void *data, int size); 30 int cpu, void *data, int size);
28 31
@@ -69,8 +72,7 @@ struct scripting_ops {
69 void (*process_event) (union perf_event *event, 72 void (*process_event) (union perf_event *event,
70 struct perf_sample *sample, 73 struct perf_sample *sample,
71 struct perf_evsel *evsel, 74 struct perf_evsel *evsel,
72 struct thread *thread, 75 struct addr_location *al);
73 struct addr_location *al);
74 int (*generate_script) (struct pevent *pevent, const char *outfile); 76 int (*generate_script) (struct pevent *pevent, const char *outfile);
75}; 77};
76 78
diff --git a/tools/perf/util/unwind-libunwind.c b/tools/perf/util/unwind-libunwind.c
index e3c40a520a25..7b09a443a280 100644
--- a/tools/perf/util/unwind-libunwind.c
+++ b/tools/perf/util/unwind-libunwind.c
@@ -266,7 +266,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
266 u64 *fde_count) 266 u64 *fde_count)
267{ 267{
268 int ret = -EINVAL, fd; 268 int ret = -EINVAL, fd;
269 u64 offset = dso->data.frame_offset; 269 u64 offset = dso->data.eh_frame_hdr_offset;
270 270
271 if (offset == 0) { 271 if (offset == 0) {
272 fd = dso__data_fd(dso, machine); 272 fd = dso__data_fd(dso, machine);
@@ -275,7 +275,7 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
275 275
276 /* Check the .eh_frame section for unwinding info */ 276 /* Check the .eh_frame section for unwinding info */
277 offset = elf_section_offset(fd, ".eh_frame_hdr"); 277 offset = elf_section_offset(fd, ".eh_frame_hdr");
278 dso->data.frame_offset = offset; 278 dso->data.eh_frame_hdr_offset = offset;
279 } 279 }
280 280
281 if (offset) 281 if (offset)
@@ -291,7 +291,7 @@ static int read_unwind_spec_debug_frame(struct dso *dso,
291 struct machine *machine, u64 *offset) 291 struct machine *machine, u64 *offset)
292{ 292{
293 int fd; 293 int fd;
294 u64 ofs = dso->data.frame_offset; 294 u64 ofs = dso->data.debug_frame_offset;
295 295
296 if (ofs == 0) { 296 if (ofs == 0) {
297 fd = dso__data_fd(dso, machine); 297 fd = dso__data_fd(dso, machine);
@@ -300,7 +300,7 @@ static int read_unwind_spec_debug_frame(struct dso *dso,
300 300
301 /* Check the .debug_frame section for unwinding info */ 301 /* Check the .debug_frame section for unwinding info */
302 ofs = elf_section_offset(fd, ".debug_frame"); 302 ofs = elf_section_offset(fd, ".debug_frame");
303 dso->data.frame_offset = ofs; 303 dso->data.debug_frame_offset = ofs;
304 } 304 }
305 305
306 *offset = ofs; 306 *offset = ofs;
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index b86744f29eef..4ee6d0d4c993 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -269,6 +269,13 @@ void dump_stack(void)
269void dump_stack(void) {} 269void dump_stack(void) {}
270#endif 270#endif
271 271
272void sighandler_dump_stack(int sig)
273{
274 psignal(sig, "perf");
275 dump_stack();
276 exit(sig);
277}
278
272void get_term_dimensions(struct winsize *ws) 279void get_term_dimensions(struct winsize *ws)
273{ 280{
274 char *s = getenv("LINES"); 281 char *s = getenv("LINES");
@@ -303,13 +310,26 @@ void set_term_quiet_input(struct termios *old)
303 tcsetattr(0, TCSANOW, &tc); 310 tcsetattr(0, TCSANOW, &tc);
304} 311}
305 312
306static void set_tracing_events_path(const char *mountpoint) 313static void set_tracing_events_path(const char *tracing, const char *mountpoint)
307{ 314{
308 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s", 315 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
309 mountpoint, "tracing/events"); 316 mountpoint, tracing, "events");
310} 317}
311 318
312const char *perf_debugfs_mount(const char *mountpoint) 319static const char *__perf_tracefs_mount(const char *mountpoint)
320{
321 const char *mnt;
322
323 mnt = tracefs_mount(mountpoint);
324 if (!mnt)
325 return NULL;
326
327 set_tracing_events_path("", mnt);
328
329 return mnt;
330}
331
332static const char *__perf_debugfs_mount(const char *mountpoint)
313{ 333{
314 const char *mnt; 334 const char *mnt;
315 335
@@ -317,7 +337,20 @@ const char *perf_debugfs_mount(const char *mountpoint)
317 if (!mnt) 337 if (!mnt)
318 return NULL; 338 return NULL;
319 339
320 set_tracing_events_path(mnt); 340 set_tracing_events_path("tracing/", mnt);
341
342 return mnt;
343}
344
345const char *perf_debugfs_mount(const char *mountpoint)
346{
347 const char *mnt;
348
349 mnt = __perf_tracefs_mount(mountpoint);
350 if (mnt)
351 return mnt;
352
353 mnt = __perf_debugfs_mount(mountpoint);
321 354
322 return mnt; 355 return mnt;
323} 356}
@@ -325,12 +358,19 @@ const char *perf_debugfs_mount(const char *mountpoint)
325void perf_debugfs_set_path(const char *mntpt) 358void perf_debugfs_set_path(const char *mntpt)
326{ 359{
327 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt); 360 snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt);
328 set_tracing_events_path(mntpt); 361 set_tracing_events_path("tracing/", mntpt);
362}
363
364static const char *find_tracefs(void)
365{
366 const char *path = __perf_tracefs_mount(NULL);
367
368 return path;
329} 369}
330 370
331static const char *find_debugfs(void) 371static const char *find_debugfs(void)
332{ 372{
333 const char *path = perf_debugfs_mount(NULL); 373 const char *path = __perf_debugfs_mount(NULL);
334 374
335 if (!path) 375 if (!path)
336 fprintf(stderr, "Your kernel does not support the debugfs filesystem"); 376 fprintf(stderr, "Your kernel does not support the debugfs filesystem");
@@ -344,6 +384,7 @@ static const char *find_debugfs(void)
344 */ 384 */
345const char *find_tracing_dir(void) 385const char *find_tracing_dir(void)
346{ 386{
387 const char *tracing_dir = "";
347 static char *tracing; 388 static char *tracing;
348 static int tracing_found; 389 static int tracing_found;
349 const char *debugfs; 390 const char *debugfs;
@@ -351,11 +392,15 @@ const char *find_tracing_dir(void)
351 if (tracing_found) 392 if (tracing_found)
352 return tracing; 393 return tracing;
353 394
354 debugfs = find_debugfs(); 395 debugfs = find_tracefs();
355 if (!debugfs) 396 if (!debugfs) {
356 return NULL; 397 tracing_dir = "/tracing";
398 debugfs = find_debugfs();
399 if (!debugfs)
400 return NULL;
401 }
357 402
358 if (asprintf(&tracing, "%s/tracing", debugfs) < 0) 403 if (asprintf(&tracing, "%s%s", debugfs, tracing_dir) < 0)
359 return NULL; 404 return NULL;
360 405
361 tracing_found = 1; 406 tracing_found = 1;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 027a5153495c..1ff23e04ad27 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -75,6 +75,7 @@
75#include <linux/types.h> 75#include <linux/types.h>
76#include <sys/ttydefaults.h> 76#include <sys/ttydefaults.h>
77#include <api/fs/debugfs.h> 77#include <api/fs/debugfs.h>
78#include <api/fs/tracefs.h>
78#include <termios.h> 79#include <termios.h>
79#include <linux/bitops.h> 80#include <linux/bitops.h>
80#include <termios.h> 81#include <termios.h>
@@ -276,6 +277,7 @@ char *ltrim(char *s);
276char *rtrim(char *s); 277char *rtrim(char *s);
277 278
278void dump_stack(void); 279void dump_stack(void);
280void sighandler_dump_stack(int sig);
279 281
280extern unsigned int page_size; 282extern unsigned int page_size;
281extern int cacheline_size; 283extern int cacheline_size;
@@ -327,4 +329,8 @@ bool find_process(const char *name);
327int gzip_decompress_to_file(const char *input, int output_fd); 329int gzip_decompress_to_file(const char *input, int output_fd);
328#endif 330#endif
329 331
332#ifdef HAVE_LZMA_SUPPORT
333int lzma_decompress_to_file(const char *input, int output_fd);
334#endif
335
330#endif /* GIT_COMPAT_UTIL_H */ 336#endif /* GIT_COMPAT_UTIL_H */