什么时候在 linux perf 中过滤函数细节?

When to filter function details in linux perf?

当使用 linux perf 使用 intel_pt 事件记录跟踪时,可以过滤 特定函数 (func1) 跟踪.

 perf record -e intel_pt/branch_type=call/u --filter ' filter func1 @ a.out ' -- ./a.out

另一种方法可能是:

  perf record -e intel_pt/branch_type=call/u -T --switch-events -- ./a.out

接着是

  perf script --itrace -c | grep 'func1'

--itrace -c 到 select 只有那些函数调用的分支。

我的问题是第一种方法记录时间戳的准确性是否优于第二种方法?

可以这样,第一种方法专门过滤并记录特定的功能轨迹,第二种方法记录所有轨迹。

不可能如此,因为第一种方法在记录跟踪时需要更多处理(在线过滤开销),而在第二种方法中,所有过滤都是离线的。

理论上,过滤可以在不同的层次上完成:在硬件上,在OS内核中,或者在用户-space代码中。对于最新版本的英特尔处理器跟踪 (intel_pt),过滤由硬件跟踪 PMU 单元完成:

Manpage of perf-record

   --filter=<filter>
       Event filter. This option should follow an event selector (-e)
       which selects either tracepoint event(s) or a hardware trace PMU
       (e.g. Intel PT or CoreSight).  ...

       ·   address filters

               A hardware trace PMU advertises its ability to accept a number of
               address filters by specifying a non-zero value in
               /sys/bus/event_source/devices/<pmu>/nr_addr_filters.

               Address filters have the format:

               filter|start|stop|tracestop <start> [/ <size>] [@<file name>]

               Where:
               - 'filter': defines a region that will be traced.

因此,第一种方法在跟踪日志大小方面更好。它只对func1函数的EIP地址范围产生tracing输出,其他地址不会由硬件产生trace log包。第二种方法是完整跟踪,它应该为每个执行的调用每秒生成数百兆字节。

过滤处理是在硬件中完成的,因此没有巨大的开销。我认为跟踪硬件不会为执行过滤地址范围之外的代码产生任何开销,但会产生 1-5% 的开销来执行过滤(必需)范围内的代码。

http://halobates.de/blog/p/406 gives link to hardware description intel sdm vol 3 chapter 35 intel processor trace "35.2.4.3 Filtering by IP"(Table35-6.IA32_RTIT_CTLMSR中有4个硬件过滤范围寄存器,所以最多可以有4个地址范围被定义)

Trace packet generation with configurable filtering by IP is supported if CPUID.(EAX=14H, ECX=0):EBX[bit 2] = 1. Intel PT can be configured to enable the generation of packets containing architectural states only when the processor is executing code within certain IP ranges. If the IP is outside of these ranges, generation of some packets is blocked. ... When ADDRn_CFG is set to enable IP filtering (see Section 35.3.1), tracing will commence when a taken branch or event is seen whose target address is in the ADDRn range. ... Note that some packets, such as MTC (Section 35.3.7) and other timing packets, do not depend on FilterEn.

35.2.5.5 Filter Enable (FilterEn) Filter Enable indicates that the Instruction Pointer (IP) is within the range of IPs that Intel PT is configured to watch. ... When FilterEn is 0, control flow packets are not generated (e.g., TNT, TIP). However, some packets, such as PIP, MTC, and PSB, may still be generated while FilterEn is clear.

要获得准确的时间戳,您应该正确配置 intel_pt 选项。 intel_pt 跟踪模式下的时间戳由硬件生成,如 perf record intel_pt 选项中配置的那样。 Manpage of perf-intel-pt 表示获取时间戳有tsc, mtc, cyc选项

   tsc Always supported. Produces TSC timestamp packets to provide
   timing information.  ...


   mtc Produces MTC timing packets.

       MTC packets provide finer grain timestamp information than TSC
       packets.  MTC packets record time using the hardware crystal
       clock (CTC) which is related to TSC packets using a TMA packet.

       Support for this feature is indicated by:

       /sys/bus/event_source/devices/intel_pt/caps/mtc

       which contains "1" if the feature is supported and
       "0" otherwise.

       The frequency of MTC packets can also be specified - see
       mtc_period below.


   cyc Produces CYC timing packets.

       CYC packets provide even finer grain timestamp information than
       MTC and TSC packets.  A CYC packet contains the number of CPU
       cycles since the last CYC packet. Unlike MTC and TSC packets,
       CYC packets are only sent when another packet is also sent.