bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona...

55
© 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020 Scale 18x Pasadena,CA

Transcript of bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona...

Page 1: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 1

Peter Zaitsev, CEO Percona

bpfTrace – Finally DTrace Replacement on LinuxIs not that great ?

March 7, 2020

Scale 18xPasadena,CA

Page 2: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 2

Question

Who is Familiar with Dtrace ?

Page 3: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 3

Question

Who is familiar with eBPF ?

Page 4: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 4

About the Presentation

Dtrace and eBPF Refresher

eBPF Tools Landscape

Look at eBPFTrace

Page 5: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 5

Instrumentation Basics

Page 6: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 6

Observability

Being Able to See Inside Running System

Critical for System Operation Monitoring, Troubleshooting, Performance Optimization

Achieved through Instrumentation

Page 7: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 7

Instrumentation

Capturing Information

from the Running system

There is Static instrumentation

and Dynamic Instrumentation

Page 8: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 8

The Instrumentation Approach

• Emitting event when particular code point is reached“Tracing”

• Checking the system state (ie program stack) at periodic interval

“Sampling”

Page 9: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 9

Static vs Dynamic Instrumentation

Static

• Counters, Logging points etc placed throughout the code

• Need to be mindful about overhead

• Limited Depth

Dynamic

• Dynamically chose what you want to instrument with running system

• Dynamically change level of instrumentation

• Can go very deep

Page 10: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 10

DTrace

Page 11: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 11

DTraceDynamic Tracing Framework

Developed Sun Microsystems starting 2001

Released in Solaris 10 in 2005

Define Specific Tracepoints in Kernel and User Land

Trace Function Calls and More

No Overhead than not enabled

D Language (Inspired by C and Awk)

Page 12: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 12

DTrace Beyond Solaris MacOS 10.5 (2007)

FreeBSD (2008)

NetBSD (2010)

Oracle Linux Supported dTrace since 2011

Code Re-Licensed GPLv2+ by Oracle in 2017

Dtrace is Coming to Windows

Page 13: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 13

DTrace on Linux

Is not available in stock Linux KernelIs not available from major Linux DistributionsRecent GPL Code release is likely too little too late

Page 14: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 14

Tracing in Linux

Page 15: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 15

Linux Tracing

Many competing tracing frameworks and frontends rather

than single one

Page 16: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 16

Linux Tracing in Pictures

Source: https://jvns.ca/blog/2017/07/05/linux-tracing-systems/

Page 17: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 17

Linux Tracing Infrastructure

• Kprobe, uprobe, Dtrace probe etc The Type of Kernel Interface

• Built in Kernel Buffer, Kernel Module, eBPF

The Type of “Program”

Connected to it

• Perf, SystemTap, SysDig, Bcc etc Front-end Tools to

work with it from the user space

Page 18: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 18

eBPF – emerging Linux Standard

Page 19: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 19

eBPF - Extended Berkeley Packet FilterBerkeley Packet Filter - Originated in 1992 as efficient virtual machine for Packet Filtering

Extended Berkeley Packet Filter – Extended Version found in Linux

General Event Processing Framework

JIT Compiler for high efficiency

Page 20: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 20

eBPF vs BPF

Page 21: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 21

eBPF in Linux

Has been in Linux

Kernel since 2014

Actively being

improved

Integrated in “perf” tooling system

Page 22: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 22

Improvements in recent Kernels

https://github.com/iovisor/bcc/blob/master/docs/kernel-versions.md

Page 23: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 23

eBPF ProgramsLinux Kernel can load programs in custom byte code

Programs verified before load to prevent misuse

LLVM Clang can compile to eBPF byte code

This compilation is kernel-dependent

Few will need to write eBPF programs Directly

Page 24: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 24

eBPF User Space vs Kernel

Source: http://www.brendangregg.com/ebpf.html

Page 25: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 25

eBPF features in different kernel versions

Page 26: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 26

Project to Know

https://github.com/iovisor

Page 27: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 27

eBPF OverheadeBPF Programs can be run million+ times per second per core

More Details: https://github.com/cloudflare/ebpf_exporter/tree/master/benchmark

Page 28: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 28

eBPF Frontends

Page 29: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 29

Tracing Landscape per Brendan Gregg

Page 30: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 30

Most Valuable

• Has Great set of Pre-Built tools• Tricky to Develop your own

Tools BCC

• Much Easier to use Language • Collection of Tools is being Built

out

BpfTrace

Page 31: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 31

Page 32: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 32

BCC Tools Available

Page 33: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 33

DTrace vs bpfTrace

Page 34: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 34

General Landscape Comparison

http://www.brendangregg.com/blog/2018-10-08/dtrace-for-linux-2018.html

Page 35: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 35

bpfTrace and DTrace

There is no Direct

compatibility

Similar in Spirit

bpfTrace is more

powerful

Page 36: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 36

Function Comparison Checklist

Page 37: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 37

Script Example

Page 38: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 38

bpfTrace

Page 39: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 39

Linux Requirements

https://tracingsummit.org/w/images/8/82/Tracingsummit2018-bpftrace-robertson.pdf

Page 40: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 40

bpfTrace Probe Types

Page 41: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 41

How BPFTrace Works

Page 42: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 42

Support In Linux Distributions

Not all Distributions have packages

Development is Quick Paced – Many have outdated packages

If you use eBPF consider getting new packages

Page 43: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 43

Install bpftrace

More Details: https://github.com/iovisor/bpftrace/blob/master/INSTALL.md

Warning: snap packages have limited functionality

Page 44: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 44

Timing Reads by Processbpftrace -e 'kprobe:vfs_read { @start[tid] = nsecs; } kretprobe:vfs_read /@start[tid]/ { @ns[comm] = hist(nsecs - @start[tid]); delete(@start[tid]); }'

Page 45: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 45

Saving it as Script File

Page 46: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 46

Concept Overview

• probe[,probe,...] /filter/ { action }General Syntax

• Filtering output of the Probe (ie by Pid)Filter

• Mini-Program to be ran Actionhttps://github.com/iovisor/bpftrace/blob/master/docs/reference_guide.md

Page 47: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 47

BpfTrace Tools

https://github.com/iovisor/bpftrace

Page 48: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 48

Curios to see BPF Code ?

Page 49: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 49

Tracing MySQLbpftrace -e 'uprobe:/usr/sbin/mysqld:dispatch_command { printf("%s\n", str(arg2)); }' failed to stat uprobe target file /usr/sbin/mysqld: No such file or directory

root@mysql1:/# ls -la /usr/sbin/mysqld -rwxr-xr-x 1 root root 60718384 Oct 25 09:19 /usr/sbin/mysqld

Page 50: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 50

Tracing MySQL

root@mysql1:~# bpftrace -e 'uprobe:/usr/sbin/mysqld:dispatch_command { printf("%s\n", str(arg2)); }' Attaching 1 probe... Could not resolve symbol: /usr/sbin/mysqld:dispatch_command

Using apt installed bpftrace rather than snap package

Page 51: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 51

Tracing MySQL(MariaDB)root@mysql1:~# nm -D /usr/sbin/mysqld | grep dispatch_command 00000000005af770 T _Z16dispatch_command19enum_server_commandP3THDPcjbb root@localhost:~# bpftrace -e 'uprobe:/usr/sbin/mysqld:_Z16dispatch_command19enum_server_commandP3THDPcjbb { printf("%s\n", str(arg2)); }' Attaching 1 probe... select @@version_comment limit 1 select 1

Page 52: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 52

Check out eBPF Bible

http://www.brendangregg.com/ebpf.html

Page 53: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 53

Further Reading Listhttps://github.com/zoidbergwill/awesome-ebpfhttps://slideplayer.com/slide/12710510/http://www.brendangregg.com/ebpf.htmlhttp://vger.kernel.org/netconf2018_files/BrendanGregg_netconf2018.pdfhttp://www.brendangregg.com/Slides/Velocity2017_BPF_superpowers.pdfhttps://lwn.net/Articles/740157/https://tracingsummit.org/w/images/8/82/Tracingsummit2018-bpftrace-robertson.pdf

Page 54: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 54

Page 55: bpfTrace – Finally DTrace Replacement on Linux · © 2020 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? March 7, 2020

© 2020 Percona. 55

Thank You!