{{short description|Performance analyzing tool in Linux}} {{Lowercase title}} {{Infobox software | name = perf | logo = <!-- Image name is enough --> | logo caption = | logo_size = | logo_alt = | screenshot = <!-- Image name is enough --> | caption = | screenshot_size = | screenshot_alt = | collapsible = | author = | developer = | released = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | discontinued = | latest release version = | latest release date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | latest preview version = | latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | status = | programming language = C | operating system = Linux kernel | platform = | size = | language = | language count = <!-- DO NOT include this parameter unless you know what it does --> | language footnote = | genre = Performance monitor and testing | license = GNU GPL | repo = https://github.com/torvalds/linux/tree/master/tools/perf | website = {{URL|https://perf.wiki.kernel.org/index.php/Main_Page}} }}
'''perf''' (sometimes called '''perf_events'''<ref>Vince Weaver, <!-- author of PAPI -->[http://web.eece.maine.edu/~vweaver/projects/perf_events/ The Unofficial Linux Perf Events Web-Page]</ref> or '''perf tools''', originally '''Performance Counters for Linux''', '''PCL''')<ref>[http://web.eece.maine.edu/~vweaver/projects/perf_events/overhead/fastpath2013_perfevent_slides.pdf Linux perf event Features and Overhead] // 2013 FastPath Workshop, Vince Weaver</ref> is a performance analyzing tool in Linux, available from Linux kernel version 2.6.31 in 2009.<ref>Jake Edge, [https://lwn.net/Articles/339361/ Perfcounters added to the mainline], LWN July 1, 2009, "perfcounters being included into the mainline during the recently completed 2.6.31 merge window"</ref> Userspace controlling utility, named <code>perf</code>, is accessed from the command line and provides a number of subcommands; it is capable of statistical profiling of the entire system (both kernel and userland code).
It supports hardware performance counters, tracepoints, software performance counters (e.g. hrtimer), and dynamic probes (for example, kprobes or uprobes).<ref name="lk2010-perf-acme.pdf" /> In 2012, two IBM engineers recognized perf (along with OProfile) as one of the two most commonly used performance counter profiling tools on Linux.<ref>A. Zanella, R. Arnold. [https://www.ibm.com/developerworks/linux/library/l-evaluatelinuxonpower/ Evaluate performance for Linux on POWER. Analyze performance using Linux tools], 12 Jun 2012 // IBM DeveloperWorks Technical library</ref>
== {{Anchor|RAPL}}Implementation == The interface between the perf utility and the kernel consists of only one syscall and is done via a file descriptor and a mapped memory region.<ref name="rvitillo">Roberto A. Vitillo (LBNL). [https://indico.cern.ch/materialDisplay.py?contribId=20&sessionId=4&materialId=slides&confId=141309 PERFORMANCE TOOLS DEVELOPMENTS], 16 June 2011, presentation from "Future computing in particle physics" conference</ref> Unlike LTTng or older versions of oprofile, no service daemons are needed, as most functionality is integrated into the kernel. The perf utility dumps raw data from the mapped buffer to disk when the buffer becomes filled up. According to R. Vitillo (LBNL), profiling performed by perf involves a very low overhead.<ref name="rvitillo" />
{{As of|2010}}, architectures that provide support for hardware counters include x86, PowerPC64, UltraSPARC (III and IV), ARM (v5, v6, v7, Cortex-A8 and -A9), Alpha EV56 and SuperH.<ref name="lk2010-perf-acme.pdf">Arnaldo Carvalho de Melo, [https://www.linux-kongress.org/2010/slides/lk2010-perf-acme.pdf The New Linux ’perf’ tools], presentation from Linux Kongress, September, 2010</ref> Usage of Last Branch Records,<ref>{{cite book|title=Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 3B: System Programming Guide, Part 2|date=June 2009|publisher=Intel|page=19-2 vol. 3}}</ref> a branch tracing implementation available in Intel CPUs since Pentium 4, is available as a patch.<ref name="rvitillo" /> Since version 3.14 of the Linux kernel mainline, released on 31 March 2014, perf also supports ''running average power limit'' (RAPL) for power consumption measurements, which is available as a feature of certain Intel CPUs.<ref>{{cite web | url = https://lwn.net/Articles/593690/ | title = Lots of new perf features | date = 2014-04-09 | access-date = 2014-04-22 | author = Jake Edge | publisher = LWN.net }}</ref><ref>{{cite web | url = https://lwn.net/Articles/545745/ | title = RAPL (Running Average Power Limit) driver | date = 2013-04-02 | access-date = 2014-04-22 | author = Jacob Pan | publisher = LWN.net }}</ref><ref>{{cite web|url=https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=9326657abe1a83ed4b4f396b923ca1217fd50cba |title=kernel/git/torvalds/linux.git - Linux kernel source tree |publisher=Git.kernel.org |date=2014-01-20 |access-date=2014-03-31}}</ref>
Perf is natively supported in many popular Linux distributions, including Red Hat Enterprise Linux (since its version 6 released in 2010)<ref>[https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Developer_Guide/perf.html 6.4. Performance Counters for Linux (PCL) Tools and perf] // RHEL Developer Guide</ref> and Debian in the ''linux-tools-common'' package (since Debian 6.0 (Squeeze) released in 2011).<ref>{{cite web|url=https://packages.debian.org/squeeze/linux-tools-2.6.32 |title=Debian - Details of package linux-tools-2.6.32 in squeeze |publisher=Packages.debian.org |access-date=2014-03-31}}</ref>
== Subcommands == perf is used with several subcommands: * <code>stat</code>: measure total event count for single program or for system for some time * <code>top</code>: top-like dynamic view of hottest functions * <code>record</code>: measure and save sampling data for single program<ref name="perfff">Urs Fässler [http://openlab.web.cern.ch/sites/openlab.web.cern.ch/files/technical_documents/Urs_Fassler_report.pdf perf file format] {{Webarchive|url=https://web.archive.org/web/20121214181811/http://openlab.web.cern.ch/sites/openlab.web.cern.ch/files/technical_documents/Urs_Fassler_report.pdf |date=2012-12-14 }}, CERN openlab, 2011</ref> * <code>report</code>: analyze file generated by perf record; can generate flat, or graph profile.<ref name="perfff" /> * <code>annotate</code>: annotate sources or assembly * <code>sched</code>: tracing/measuring of scheduler actions and latencies<ref>Ingo Molnar, [https://lwn.net/Articles/353295/ 'perf sched': Utility to capture, measure and analyze scheduler latencies and behavior], 17 Sep 2009</ref> * <code>list</code>: list available events
== Criticism == The documentation of perf is not very detailed (as of 2014); for example, it does not document most events or explain their aliases (often external tools are used to get names and codes of events<ref>[https://www.bnikolic.co.uk/blog/hpc-prof-events.html How to monitor the full range of CPU performance events] // Bojan Nikolic, 2012</ref>).<ref name="haas"/> Perf tools also cannot profile based on true wall-clock time,<ref name="haas">Robert Haas (PostgreSQL), [https://rhaas.blogspot.com/2012/06/perf-good-bad-ugly.html perf: the good, the bad, the ugly] // 6 June 2012</ref> something that has been addressed by the addition of off-CPU profiling.
== Security == The perf subsystem of Linux kernels from 2.6.37 up to 3.8.8 and RHEL6 kernel 2.6.32 contained a security vulnerability ({{CVE|2013-2094}}), which was exploited to gain root privileges by a local user.<ref>{{cite web|url=https://www.phoronix.com/scan.php?page=news_item&px=MTM3MjA|title=New Linux Kernel Vulnerability Exploited|publisher=Phoronix|author=Michael Larabel |date=2013-05-15}}</ref><ref>{{cite web|url=https://lwn.net/Articles/550678/|publisher=LWN|date=2013-05-15|author=corbet|title=Local root vulnerability in the kernel}}</ref> The problem was due to an incorrect type being used (32-bit int instead of 64-bit) in the event_id verification code path.<ref>{{cite web|url=http://timetobleed.com/a-closer-look-at-a-recent-privilege-escalation-bug-in-linux-cve-2013-2094/|title=A closer look at a recent privilege escalation bug in Linux (CVE-2013-2094)|date=2013-05-20|author=Joe Damato|access-date=2013-07-18|archive-date=2019-07-24|archive-url=https://web.archive.org/web/20190724020353/http://timetobleed.com/a-closer-look-at-a-recent-privilege-escalation-bug-in-linux-cve-2013-2094/|url-status=dead}}</ref>
== See also== * List of performance analysis tools * OProfile * Performance Application Programming Interface * Profiling (computer programming)
== References == {{Reflist|30em}}
== External links == * [https://perf.wiki.kernel.org/ perf's wiki] on kernel.org * Arnaldo Carvalho de Melo, [https://www.linux-kongress.org/2010/slides/lk2010-perf-acme.pdf The New Linux ’perf’ tools], presentation from Linux Kongress, September, 2010 * [https://web.archive.org/web/20121105003923/http://www.eece.maine.edu/~vweaver/projects/perf_events/support.html Hardware PMU support charts] – check perf_event column
{{Linux kernel}} {{Linux}}
Category:Linux kernel features Category:Linux programming tools Category:Profilers