linux perf event features and overhead

35
Linux perf event Features and Overhead 2013 FastPath Workshop Vince Weaver http://www.eece.maine.edu/vweaver [email protected] 21 April 2013

Upload: others

Post on 16-Nov-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linux perf event Features and Overhead

Linux perf event Features andOverhead

2013 FastPath Workshop

Vince Weaver

http://www.eece.maine.edu/∼[email protected]

21 April 2013

Page 2: Linux perf event Features and Overhead

Performance Counters and WorkloadOptimized Systems

• With processor speeds constant, cannot depend on

Moore’s Law to deliver increased performance

• Code analysis and optimization can provide speedups in

existing code on existing hardware

• Systems with a single workload are best target for cross-

stack hardware/kernel/application optimization

• Hardware performance counters are the perfect tool for

this type of optimization

1

Page 3: Linux perf event Features and Overhead

Some Uses of Performance Counters

• Traditional analysis and optimization

• Finding architectural reasons for slowdown

• Validating Simulators

• Auto-tuning

• Operating System optimization

• Estimating power/energy in software

2

Page 4: Linux perf event Features and Overhead

Linux and Performance Counters

• Linux has become the operating system of choice in

many domains

• Runs most of the Top500 list (over 90%) on down to

embedded devices (Android Phones)

• Until recently had no easy access to hardware

performance counters, limiting code analysis and

optimization.

3

Page 5: Linux perf event Features and Overhead

Linux Performance Counter History

• oprofile – system-wide sampling profiler since 2002

• perfctr – widely used general interface available since

1999, required patching kernel

• perfmon2 – another general interface, included in kernel

for itanium, made generic, big push for kernel inclusion

4

Page 6: Linux perf event Features and Overhead

Linux perf event

• Developed in response to perfmon2 by Molnar and

Gleixner in 2009

• Merged in 2.6.31 as “PCL”

• Unusual design pushes most functionality into kernel

• Not well documented nor well characterized

5

Page 7: Linux perf event Features and Overhead

perf event Interface

• sys perf event open() system call

• complex perf event attr structure (over 40 fields)

• counters started/stopped with ioctl() call

• values read either with read() or samples in mmap()

circular buffer

6

Page 8: Linux perf event Features and Overhead

perf event Kernel Features

• Generalized Events – commonly used events on various

architectures provided common names

• Event Scheduling – kernel handles mapping events to

appropriate counters

• Multiplexing – if more events than counters, time based

multiplexing extrapolates full counts

• Per-process counts – values saved on context switch

• Software Events – kernel events exposed by same API

7

Page 9: Linux perf event Features and Overhead

Advanced Hardware Features

• Offcore Response – filtered measuring of memory

accesses that leave the core

• Uncore and Northbridge Events – special support needed

for shared resources (L2, L3, memory)

• Sampled Interfaces

+ AMD Instruction Based Sampling (IBS) – can provide

address, latency, etc., as well as minimal skid

+ Intel Precise Event Sampling (PEBS) – gathers extra

data on triggered event (registers, latency), low-skid

8

Page 10: Linux perf event Features and Overhead

Virtualized Counters

• Recent versions of KVM can trap on access

to performance MSRs and pass in guest-specific

performance counts, allowing use of performance

counters in a virtualized environment

• counter values have to be save/restored when guest

scheduled

9

Page 11: Linux perf event Features and Overhead

More on Generalized Events

• Unlike those provided by user-space libraries (PAPI),

hard to know what the actual event is (this is changing)

• Kernel events are sometimes wrong, a lot more hassle to

update kernel than update library

10

Page 12: Linux perf event Features and Overhead

Generalized Events – Wrong Events

Until 2.6.35 total “branches” preset accidentally mapped

to “taken branches”

0 50 100 150 200

Instruction Interval (100M)

0M

7M

14M

21M

28M

Bra

nches

176.gcc.static.166

Total

Taken

0 50 100 150 200 250

Instruction Interval (100M)

0

5

10

15

Bra

nch M

iss %

164.gzip.static.log

Misses/Total

Misses/Taken

11

Page 13: Linux perf event Features and Overhead

Generalized Events – Similar Events,Different Meaning

On Nehalem,• perf event defines L1D.OP READ.RESULT ACCESS

(perf: L1-dcache-loads) as MEM INT RETIRED:LOADS

• PAPI defines PAPI L1 DCR as L1D CACHE LD:MESI

0 100 200 300 400

Instruction Interval (100M)

0M

44M

88M

132M

176M

L1 D

Cache L

oads

181.mcf.static.default

PAPI

perf_event

12

Page 14: Linux perf event Features and Overhead

Context-Switch Test Methodology

• To give per-process events, have to save counts on

context-switch. This has overhead

• We use lmbench lat ctx benchmark. Run it with and

without perf measuring it.

• Up to 20% overhead when perf monitoring the threads.

Benchmark documentation claim 10-15% accuracy at

best

13

Page 15: Linux perf event Features and Overhead

Core2 Context-Switch Overhead

2.6.

30

2.6.

31

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

39 3.0

3.1

3.2

3.3

3.4

Kernel Being Tested

0

5

10

15

20

Tim

e (

us)

core2 Context Switch Time

perf_event - inactiveperf_event - activeperfctr - inactiveperfctr - activeperfmon2 - inactiveperfmon2 - active

14

Page 16: Linux perf event Features and Overhead

Common Performance Counter UsageModels

• Aggregate

• Sampled

• Self-monitoring

Linux perf event can do all three.

15

Page 17: Linux perf event Features and Overhead

Aggregate Counts

$ perf stat -e instructions,cycles,branches,branch-misses,cache-misses

./matrix_multiply_atlas

Matrix multiply sum: s=3650244631906855424.000000

Performance counter stats for ’./matrix_multiply_atlas’:

194,492,378,876 instructions # 2.51 insns per cycle

77,585,141,514 cycles # 0.000 GHz

584,202,927 branches

3,963,325 branch-misses # 0.68% of all branches

89,863,007 cache-misses

49.973787489 seconds time elapsed

perf event sets up events, forks process (start counts on

exec()), handles overflow, waits for exit, prints totals.

16

Page 18: Linux perf event Features and Overhead

Sampled Profiling

$ perf record ./matrix_multiply_atlas

Matrix multiply sum: s=3650244631906855424.000000

[ perf record: Woken up 14 times to write data ]

[ perf record: Captured and wrote 3.757 MB perf.data (~164126 samples) ]

$ perf report

Events: 98K cycles

97.36% matrix_multiply libblas.so.3.0 [.] ATL_dJIK48x48x48TN48x48x0_

0.62% matrix_multiply matrix_multiply_atlas [.] naive_matrix_multiply

0.27% matrix_multiply libblas.so.3.0 [.] 0x1f1728

0.18% matrix_multiply libblas.so.3.0 [.] ATL_dupMBmm0_8_0_b1

0.16% matrix_multiply libblas.so.3.0 [.] ATL_dupKBmm8_2_1_b1

0.14% matrix_multiply libblas.so.3.0 [.] ATL_dupNBmm0_1_0_b1

0.13% matrix_multiply libblas.so.3.0 [.] ATL_dcol2blk_a1

0.09% matrix_multiply [kernel.kallsyms] [k] page_fault

Periodically sample, grad state, record for later analysis.

17

Page 19: Linux perf event Features and Overhead

Self-Monitoringretval = PAPI_library_init(PAPI_VER_CURRENT );

if (retval != PAPI_VER_CURRENT) fprintf(stderr ,"Wrong PAPI version\n");

retval = PAPI_create_eventset( &event_set );

if (retval != PAPI_OK) fprintf(stderr ,"Error creating eventset\n");

retval = PAPI_add_named_event( event_set , "PAPI_TOT_INS" );

if (retval != PAPI_OK) fprintf(stderr ,"Error adding event\n");

retval = PAPI_start(event_set );

naive_matrix_multiply (0);

retval = PAPI_stop(event_set ,& count);

printf("Total instructions: %lld\n",count);

18

Page 20: Linux perf event Features and Overhead

Self-Monitoring Overhead

• Typical pattern is Start/Stop/Read

• Want minimal possible overhead

• Read performance is typically most important, especially

if doing multiple reads

19

Page 21: Linux perf event Features and Overhead

Methodology

• DVFS disabled

• Use rdtsc() 64-bit timestamp counter. Typically 150

cycle overhead

• Measure start/stop/read with no code in between

• All three (start/stop/read) measured at same time

• Environment variables should not matter

20

Page 22: Linux perf event Features and Overhead

perf event Measurement Codestart_before=rdtsc ();

ioctl(fd[0], PERF_EVENT_IOC_ENABLE ,0);

start_after=rdtsc ();

ioctl(fd[0], PERF_EVENT_IOC_DISABLE ,0);

stop_after=rdtsc ();

read(fd[0],buffer ,BUFFER_SIZE*sizeof(long long ));

read_after=rdtsc ();

21

Page 23: Linux perf event Features and Overhead

perfctr Measurement Codestart_before=rdtsc ();

perfctr_ioctl_w(fd,VPERFCTR_CONTROL ,

&control , &vperfctr_control_sdesc );

start_after=rdtsc ();

cstatus=kstate ->cpu_state.cstatus;

nrctrs=perfctr_cstatus_nrctrs(cstatus );

retry:

tsc0=kstate ->cpu_state.tsc_start;

rdtscl(now);

sum.tsc = kstate ->cpu_state.tsc_sum +(now -tsc0);

for(i = nrctrs; --i >=0 ;) {

rdpmcl(kstate ->cpu_state.pmc[i].map , now);

sum.pmc[i] = kstate ->cpu_state.pmc[i].sum+

(now -kstate ->cpu_state.pmc[i].start );

}

if (tsc0!=kstate ->cpu_state.tsc_start) goto retry;

read_after=rdtsc ();

_vperfctr_control(fd, &control_stop );

stop_after=rdtsc ();

22

Page 24: Linux perf event Features and Overhead

perfmon2 Measurement Codestart_before=rdtsc ();

pfm_start(ctx_fd ,NULL);

start_after=rdtsc ();

pfm_stop(ctx_fd );

stop_after=rdtsc ();

pfm_read_pmds(ctx_fd ,pd,inp.pfp_event_count );

read_after=rdtsc ();

23

Page 25: Linux perf event Features and Overhead

Overall Overhead / 1 Event, AMD Athlon64

Boxplot: 25th/median/75th, stddev whiskers, outliers

2.6.

30-p

erfm

on2

2.6.

32-p

erfctr

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

393.

0.03.

1.03.

2.03.

3.03.

4.0

3.4.

0-rd

pmc

3.5.

0

3.5.

0-rd

pmc

0

10000

20000

30000

40000

50000A

ve

rag

e O

ve

rhe

ad

(C

ycle

s)

amd0fh Overall Overhead of Start/Stop/Read with 1 Event

24

Page 26: Linux perf event Features and Overhead

Overall Overhead / 1 Event, Intel Atom

2.6.

30-p

erfm

on2

2.6.

32-p

erfctr

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

393.

0.03.

1.03.

2.03.

3.03.

4.0

3.4.

0-rd

pmc

3.5.

0

3.5.

0-rd

pmc

0

20000

40000

60000

80000

Ave

rag

e O

ve

rhe

ad

(C

ycle

s)

atom Overall Overhead of Start/Stop/Read with 1 Event

25

Page 27: Linux perf event Features and Overhead

Overall Overhead / 1 Event, Intel Core2

2.6.

30-p

erfm

on2

2.6.

32-p

erfctr

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

393.

0.03.

1.03.

2.03.

3.03.

4.0

3.4.

0-rd

pmc

3.5.

0

3.5.

0-rd

pmc

0

10000

20000

30000

Ave

rag

e O

ve

rhe

ad

(C

ycle

s)

core2 Overall Overhead of Start/Stop/Read with 1 Event

26

Page 28: Linux perf event Features and Overhead

Start Overhead / 1 Event, Intel Core2

2.6.

30-p

erfm

on2

2.6.

32-p

erfctr

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

393.

0.03.

1.03.

2.03.

3.03.

4.0

3.4.

0-rd

pmc

3.5.

0

3.5.

0-rd

pmc

0

5000

10000

15000

20000

Ave

rag

e O

ve

rhe

ad

(C

ycle

s)

core2 Overall Overhead of Start with 1 Event

27

Page 29: Linux perf event Features and Overhead

Stop Overhead / 1 Event, Intel Core2

2.6.

30-p

erfm

on2

2.6.

32-p

erfctr

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

393.

0.03.

1.03.

2.03.

3.03.

4.0

3.4.

0-rd

pmc

3.5.

0

3.5.

0-rd

pmc

0

2000

4000

6000

8000

10000

Ave

rag

e O

ve

rhe

ad

(C

ycle

s)

core2 Overall Overhead of Stop with 1 Event

28

Page 30: Linux perf event Features and Overhead

Read Overhead / 1 Event, Intel Core2

perfctr uses rdpmc

2.6.

30-p

erfm

on2

2.6.

32-p

erfctr

2.6.

32

2.6.

33

2.6.

34

2.6.

35

2.6.

36

2.6.

37

2.6.

38

2.6.

393.

0.03.

1.03.

2.03.

3.03.

4.0

3.4.

0-rd

pmc

3.5.

0

3.5.

0-rd

pmc

0

5000

10000

15000

20000A

ve

rag

e O

ve

rhe

ad

(C

ycle

s)

core2 Overall Overhead of Read with 1 Event

29

Page 31: Linux perf event Features and Overhead

Overall Overhead / Multiple Events, Core2

1 2 3 4Simultaneous Events Being Measured

0

10000

20000

30000

40000

Ave

rag

e O

ve

rhe

ad

(C

ycle

s)

core2 Overall Start/Stop/Read Overhead

2.6.30-perfmon2

2.6.32-perfctr

2.6.32

3.5.0

3.5.0-rdpmc

30

Page 32: Linux perf event Features and Overhead

Self-Monitoring Overhead Summary

• perfmon2 low-overhead due to very thin layer over

hardware, most of work done in userspace

• perfctr has very fast rdpmc reads

• Some of perf event overhead because key tasks are in-

kernel and cannot be done before starting events

• Is 20,000 cycles too much to get an event count?

Unclear, but perfctr is much faster, showing there is

room for improvement.

31

Page 33: Linux perf event Features and Overhead

New Non-perf event Developments

• LIKWID – bypasses Linux kernel, accesses MSRs directly.

Low overhead, but system-wide only, and conflicts with

perf event

• LiMiT – new patch interface similar to perfctr

32

Page 34: Linux perf event Features and Overhead

Future Work

• AMD Lightweight Profiling (LWP) – (Bulldozer) events

can be setup and read purely from userspace

• Intel Xeon Phi spflt userspace setup instruction

• Investigate causes of overhead in greater depth, as well

as rdpmc performance issues.

• What can we learn from low overhead of perfctr and

perfmon2?

33

Page 35: Linux perf event Features and Overhead

Questions?

[email protected]

All code and data is available:

git clone

git://github.com/deater/perfevent overhead.git

34