54
© 2021 Percona. 1 Peter Zaitsev, CEO Percona bpfTrace – Finally DTrace Replacement on Linux Is not that great ? February 11th, 2021 TriLUG – Triangle Linux Users Group

bpfTrace–Finally DTraceReplacement on Linux

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 1

Peter Zaitsev, CEO Percona

bpfTrace – Finally DTrace Replacement on LinuxIs not that great ?

February 11th, 2021

TriLUG – Triangle Linux Users Group

Page 2: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 2

About the Presentation

Dtrace and eBPF Refresher

eBPF Tools Landscape

Look at BPFTrace

Page 3: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 3

Instrumentation Basics

Page 4: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 4

Observability

Being Able to See Inside Running System

Critical for System Operation Monitoring, Troubleshooting, Performance Optimization

Achieved through Instrumentation

Page 5: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 5

Instrumentation

Capturing Information from

the Running system

There is Static instrumentation

and Dynamic Instrumentation

Page 6: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 6

The Instrumentation Approach

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

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

Page 7: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 7

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 8: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 8

DTrace

Page 9: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 9

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 10: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 10

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 available on Windows in 2019

Page 11: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 11

DTrace on Linux

Is not available in stock Linux Kernel

Is not available from major Linux Distributions

Recent GPL Code release is likely too little too late

Page 12: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 12

Tracing in Linux

Page 13: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 13

Linux Tracing

Many competing tracing frameworks and

frontends rather than single one

Page 14: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 14

Linux Tracing in Pictures

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

Page 15: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 15

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 16: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 16

eBPF – emerging Linux Standard

Page 17: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 17

eBPF - Extended Berkeley Packet Filter

Berkeley 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 18: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 18

eBPF vs BPF

Page 19: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 19

eBPF in Linux

Has been in Linux Kernel since 2014

Actively being improved

Integrated in “perf” tooling

system

Page 20: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 20

Improvements in recent Kernels

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

Page 21: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 21

eBPF Programs

Linux 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 22: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 22

eBPF User Space vs Kernel

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

Page 23: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 23

eBPF features in different kernel versions

Page 24: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 24

Project to Know

https://github.com/iovisor

Page 25: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 25

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

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

Page 26: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 26

eBPF Frontends

Page 27: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 27

Tracing Landscape per Brendan Gregg

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

Page 28: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 28

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 29: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 29

Page 30: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 30

BCC Tools Available

Page 31: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 31

DTrace vs bpfTrace

Page 32: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 32

General Landscape Comparison

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

Page 33: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 33

bpfTrace and DTrace

There is no Direct

compatibility

Similar in Spirit

bpfTrace is more

powerful

Page 34: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 34

Function Comparison Checklist

Page 35: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 35

Script Example

Page 36: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 36

bpfTrace

Page 37: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 37

Linux Requirements

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

Page 38: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 38

bpfTrace Probe Types

Page 39: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 39

How BPFTrace Works

Page 40: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 40

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 41: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 41

Install bpftrace

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

Warning: snap packages have limited functionality

Page 42: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 42

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 43: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 43

Saving it as Script File

Page 44: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 44

Concept Overview

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

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

• Mini-Program to be ran Action

https://github.com/iovisor/bpftrace/blob/master/docs/reference_guide.md

Page 45: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 45

BpfTrace Tools

https://github.com/iovisor/bpftrace

Page 46: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 46

Curios to see BPF Code ?

Page 47: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 47

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 48: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 48

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 49: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 49

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

Page 50: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 50

Check out eBPF Bible

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

Page 51: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 51

Books

Page 52: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 52

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 53: bpfTrace–Finally DTraceReplacement on Linux

53 http://www.perconalive.com

Page 54: bpfTrace–Finally DTraceReplacement on Linux

© 2021 Percona. 54

Thank You!