-
Notifications
You must be signed in to change notification settings - Fork 270
Add LBR decoder support #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bobrik
wants to merge
3
commits into
cloudflare:master
Choose a base branch
from
bobrik:ivan/lbr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,7 @@ definitions: | |
| - inet_ip | ||
| - kstack | ||
| - ksym | ||
| - lbr | ||
| - majorminor | ||
| - pci_class | ||
| - pci_device | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package decoder | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/cloudflare/ebpf_exporter/v2/config" | ||
| "github.com/cloudflare/ebpf_exporter/v2/kallsyms" | ||
| "github.com/cloudflare/ebpf_exporter/v2/util" | ||
| ) | ||
|
|
||
| // struct perf_branch_entry is 24 bytes | ||
| const perfBranchEntrySize = 24 | ||
|
|
||
| // LBR is a decoder that transforms LBR entry array into a stack | ||
| type LBR struct { | ||
| decoder *kallsyms.Decoder | ||
| } | ||
|
|
||
| // Decode transforms LBR entry array into a stack | ||
| func (l *LBR) Decode(in []byte, _ config.Decoder) ([]byte, error) { | ||
| if len(in) == 0 { | ||
| return []byte("<empty>"), nil | ||
| } | ||
|
|
||
| byteOrder := util.GetHostByteOrder() | ||
|
|
||
| lines := make([]string, len(in)/perfBranchEntrySize) | ||
|
|
||
| for i := range lines { | ||
| from := uintptr(byteOrder.Uint64(in[i*24 : i*24+8])) | ||
| to := uintptr(byteOrder.Uint64(in[i*24+8 : i*24+16])) | ||
|
|
||
| lines[i] = fmt.Sprintf("0x%08x -> 0x%08x | %s -> %s", from, to, l.decoder.Sym(from), l.decoder.Sym(to)) | ||
| } | ||
|
|
||
| return []byte(strings.Join(lines, "\n")), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #include <vmlinux.h> | ||
| #include <bpf/bpf_tracing.h> | ||
| #include "tracing.bpf.h" | ||
|
|
||
| // On Zen v4 this is the depth. | ||
| #define LBR_DEPTH 16 | ||
|
|
||
| struct failure_span_t { | ||
| struct span_base_t span_base; | ||
| struct perf_branch_entry entries[LBR_DEPTH]; | ||
| u32 errno; | ||
| }; | ||
|
|
||
| struct { | ||
| __uint(type, BPF_MAP_TYPE_RINGBUF); | ||
| __uint(max_entries, 256 * 1024); | ||
| } failure_spans SEC(".maps"); | ||
|
|
||
| SEC("fexit/do_sys_openat2") | ||
| int BPF_PROG(do_sys_openat2, int dfd, const char *filename, struct open_how *how, int retval) | ||
| { | ||
| struct failure_span_t span = { 0 }; | ||
| struct failure_span_t *submit; | ||
| u64 ts; | ||
|
|
||
| if (retval >= 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| s64 snapshot_bytes = bpf_get_branch_snapshot(span.entries, sizeof(span.entries), 0); | ||
| if (snapshot_bytes == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| ts = bpf_ktime_get_ns(); | ||
|
|
||
| span.span_base.parent.trace_id_lo = ts; | ||
| span.span_base.span_id = ts; | ||
| span.span_base.span_monotonic_timestamp_ns = ts; | ||
| span.errno = -retval; | ||
|
|
||
| submit = bpf_ringbuf_reserve(&failure_spans, sizeof(struct failure_span_t), 0); | ||
| if (!submit) { | ||
| return 0; | ||
| } | ||
|
|
||
| *submit = span; | ||
|
|
||
| bpf_ringbuf_submit(submit, 0); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| char LICENSE[] SEC("license") = "GPL"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| tracing: | ||
| spans: | ||
| - name: failure | ||
| ringbuf: failure_spans | ||
| service: syscall-failures | ||
| labels: | ||
| - name: trace_id | ||
| size: 16 | ||
| decoders: | ||
| - name: hex | ||
| - name: parent_span_id | ||
| size: 8 | ||
| decoders: | ||
| - name: hex | ||
| - name: span_id | ||
| size: 8 | ||
| decoders: | ||
| - name: hex | ||
| - name: span_monotonic_timestamp_ns | ||
| size: 8 | ||
| decoders: | ||
| - name: uint | ||
| - name: span_duration_ns | ||
| size: 8 | ||
| decoders: | ||
| - name: uint | ||
| - name: data | ||
| size: 384 | ||
| decoders: | ||
| - name: lbr | ||
| - name: errno | ||
| size: 4 | ||
| decoders: | ||
| - name: uint | ||
| - name: errno |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package exporter | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/elastic/go-perf" | ||
| "github.com/iovisor/gobpf/pkg/cpuonline" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // EnableLBR configures a perf event that enables LBR | ||
| func EnableLBR() error { | ||
| attr := &perf.Attr{} | ||
| attr.Type = perf.HardwareEvent | ||
| attr.Config = unix.PERF_COUNT_HW_CPU_CYCLES | ||
| attr.SampleFormat = perf.SampleFormat{BranchStack: true} | ||
| attr.BranchSampleFormat = perf.BranchSampleFormat{Privilege: perf.BranchPrivilegeKernel, Sample: perf.BranchSampleAnyReturn} | ||
|
|
||
| cpus, err := cpuonline.Get() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to determine online cpus: %w", err) | ||
| } | ||
|
|
||
| for _, cpu := range cpus { | ||
| _, err := perf.Open(attr, perf.AllThreads, int(cpu), nil) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open perf_event: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add docs for this.