【性能优化工具】带你了解 Linux perf

本小节主要对 Linux perf 做一个简单的介绍,从是什么、可以用来干什么的角度、以及使用注意的角度来做介绍,作为初认 Linux perf 楔子。

@author: Mercury_Lc

Linux perf(性能剖析器)是一个功能强大的性能分析工具,用于帮助开发人员诊断、调优和监控 Linux 系统及应用程序的性能问题。它实现了基于硬件性能计数器(hardware performance counters),追踪点和软件测量等多种数据收集手段,以便分析系统中各种现象。perf 工具集成在 Linux 内核中,主要通过 perf_event 子系统实现。

性能计数器是对硬件事件进行计数的 CPU 硬件寄存器,例如执行的指令、缓存未命中或预测错误的分支。它们构成了分析应用程序以跟踪动态控制流和识别热点的基础。perf 在硬件特定功能上提供了丰富的通用抽象。

总的可以说,perf 可以在不同层次上分析系统性能,包括 CPU、内存、I/O、锁等资源。你可以使用 perf 处理各种任务,如分析应用程序、查看硬件事件(如缓存未命中、分支预测错误等)和系统调用,追踪内核和用户空间事件,以及生成性能报告等。

perf 提供了多种子命令来满足不同的性能分析需求,例如:

● perf stat:收集并显示运行过程中的性能计数器统计数据。

● perf record:记录事件样本信息。

● perf report:根据 perf record 记录的数据生成性能报告。

● perf annotate:对二进制文件进行注释和分析。

● perf top:实时显示系统活动及其性能计数器数据。

● perf trace:收集,解析,显示或处理内核和用户空间的跟踪性能数据。

● perf bench:运行各种内置的基准测试。

可以在安装 perf 后通过 perf -h 获取以上内容:

[root ~]# perf -h

 usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

 The most commonly used perf commands are:
   annotate        Read perf.data (created by perf record) and display annotated code
   archive         Create archive with object files with build-ids found in perf.data file
   bench           General framework for benchmark suites
   buildid-cache   Manage build-id cache.
   buildid-list    List the buildids in a perf.data file
   c2c             Shared Data C2C/HITM Analyzer.
   config          Get and set variables in a configuration file.
   data            Data file related processing
   diff            Read perf.data files and display the differential profile
   evlist          List the event names in a perf.data file
   ftrace          simple wrapper for kernel's ftrace functionality
   inject          Filter to augment the events stream with additional information
   kallsyms        Searches running kernel for symbols
   kmem            Tool to trace/measure kernel memory properties
   kvm             Tool to trace/measure kvm guest os
   list            List all symbolic event types
   lock            Analyze lock events
   mem             Profile memory accesses
   record          Run a command and record its profile into perf.data
   report          Read perf.data (created by perf record) and display the profile
   sched           Tool to trace/measure scheduler properties (latencies)
   script          Read perf.data (created by perf record) and display trace output
   stat            Run a command and gather performance counter statistics
   test            Runs sanity tests.
   timechart       Tool to visualize total system behavior during a workload
   top             System profiling tool.
   version         display the version of perf binary
   probe           Define new dynamic tracepoints
   trace           strace inspired tool

 See 'perf help COMMAND' for more information on a specific command.

需要注意的是,在使用 perf 之前,应确保系统内核支持perf_event子系统。使用 perf 命令时,通常需要具有 root 权限或归属于特定用户组(如:perf-users)。

总之,Linux perf 是一个强大的性能分析工具,可以帮助开发人员定位性能问题、优化系统性能,提高系统以及应用程序的运行效率。

Ref

https://perf.wiki.kernel.org/index.php/Main_Page

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/131341695