Skip to content

Commit

Permalink
Merge pull request #352 from bobrik/ivan/usdt-tracing
Browse files Browse the repository at this point in the history
Switch from uprobes to usdt for tracing
  • Loading branch information
bobrik authored Feb 27, 2024
2 parents 85ad56e + 717a627 commit 9be354d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 61 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ jobs:
with:
go-version: ^1.22

- name: Install systemtap-sdt-dev
run: sudo apt-get install -y systemtap-sdt-dev

- name: Download libbpf.tar.gz
uses: actions/download-artifact@v4
with:
Expand Down Expand Up @@ -300,6 +303,9 @@ jobs:
with:
go-version: ^1.22

- name: Install systemtap-sdt-dev
run: sudo apt-get install -y systemtap-sdt-dev

- name: Install libelf-dev
run: sudo apt-get install -y libelf-dev

Expand All @@ -325,6 +331,9 @@ jobs:
go-version: ^1.22
cache: false # https://github.com/golangci/golangci-lint-action/issues/807

- name: Install systemtap-sdt-dev
run: sudo apt-get install -y systemtap-sdt-dev

- name: Install libelf-dev
run: sudo apt-get install -y libelf-dev

Expand Down Expand Up @@ -396,6 +405,9 @@ jobs:
with:
go-version: ^1.22

- name: Install systemtap-sdt-dev
run: sudo apt-get install -y systemtap-sdt-dev

- name: Build
run: make -j $(nproc) tracing-demos

Expand Down
12 changes: 5 additions & 7 deletions examples/cfs-throttling-trace.bpf.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include <bpf/usdt.bpf.h>
#include "tracing.bpf.h"

// Skipping 3 frames off the top as they are just bpf trampoline
Expand All @@ -25,22 +26,19 @@ struct {
__type(value, struct span_parent_t);
} traced_cgroups SEC(".maps");

SEC("uprobe/./tracing/demos/cfs-throttling/demo:cfs_set_parent_span")
int sock_set_parent_span(struct pt_regs *ctx)
SEC("usdt/./tracing/demos/cfs-throttling/demo:ebpf_exporter:cfs_set_parent_span")
int BPF_USDT(cfs_set_parent_span, u64 trace_id_hi, u64 trace_id_lo, u64 span_id)
{
u32 cgroup = bpf_get_current_cgroup_id();
u64 trace_id_hi = PT_REGS_PARM1(ctx);
u64 trace_id_lo = PT_REGS_PARM2(ctx);
u64 span_id = PT_REGS_PARM3(ctx);
struct span_parent_t parent = { .trace_id_hi = trace_id_hi, .trace_id_lo = trace_id_lo, .span_id = span_id };

bpf_map_update_elem(&traced_cgroups, &cgroup, &parent, BPF_ANY);

return 0;
}

SEC("uprobe/./tracing/demos/cfs-throttling/demo:cfs_clear_parent_span")
int sched_clear_parent_span(struct pt_regs *ctx)
SEC("usdt/./tracing/demos/cfs-throttling/demo:ebpf_exporter:cfs_clear_parent_span")
int BPF_USDT(cfs_clear_parent_span)
{
u32 cgroup = bpf_get_current_cgroup_id();

Expand Down
12 changes: 5 additions & 7 deletions examples/sched-trace.bpf.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include <bpf/usdt.bpf.h>
#include "tracing.bpf.h"

// https://github.com/torvalds/linux/blob/v6.1/include/linux/sched.h#L84
Expand Down Expand Up @@ -68,22 +69,19 @@ static int task_state(unsigned int state)
return STATE_TASK_PROBABLY_RUNNING;
}

SEC("uprobe/./tracing/demos/sched/demo:sched_set_parent_span")
int sock_set_parent_span(struct pt_regs *ctx)
SEC("usdt/./tracing/demos/sched/demo:ebpf_exporter:sched_set_parent_span")
int BPF_USDT(sock_set_parent_span, u64 trace_id_hi, u64 trace_id_lo, u64 span_id)
{
u32 tgid = bpf_get_current_pid_tgid() >> 32;
u64 trace_id_hi = PT_REGS_PARM1(ctx);
u64 trace_id_lo = PT_REGS_PARM2(ctx);
u64 span_id = PT_REGS_PARM3(ctx);
struct span_parent_t parent = { .trace_id_hi = trace_id_hi, .trace_id_lo = trace_id_lo, .span_id = span_id };

bpf_map_update_elem(&traced_tgids, &tgid, &parent, BPF_ANY);

return 0;
}

SEC("uprobe/./tracing/demos/sched/demo:sched_clear_parent_span")
int sched_clear_parent_span(struct pt_regs *ctx)
SEC("usdt/./tracing/demos/sched/demo:ebpf_exporter:sched_clear_parent_span")
int BPF_USDT(sched_clear_parent_span)
{
u32 tgid = bpf_get_current_pid_tgid() >> 32;

Expand Down
13 changes: 5 additions & 8 deletions examples/sock-trace.bpf.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include <bpf/usdt.bpf.h>
#include "tracing.bpf.h"

u32 yes = true;
Expand Down Expand Up @@ -92,8 +93,8 @@ int BPF_PROG(close_fd, unsigned int fd)
return 0;
}

SEC("uprobe/./tracing/demos/sock/demo:enable_kernel_tracing")
int enable_tracing()
SEC("usdt/./tracing/demos/sock/demo:ebpf_exporter:enable_kernel_tracing")
int BPF_USDT(enable_kernel_tracing)
{
u32 tgid = bpf_get_current_pid_tgid() >> 32;

Expand All @@ -116,14 +117,10 @@ int BPF_PROG(sched_process_exit, struct task_struct *p)
return 0;
}

SEC("uprobe/./tracing/demos/sock/demo:sock_set_parent_span")
int sock_set_parent_span(struct pt_regs *ctx)
SEC("usdt/./tracing/demos/sock/demo:ebpf_exporter:sock_set_parent_span")
int BPF_USDT(sock_set_parent_span, int fd, u64 trace_id_hi, u64 trace_id_lo, u64 span_id)
{
u32 tgid = bpf_get_current_pid_tgid() >> 32;
int fd = PT_REGS_PARM1(ctx);
u64 trace_id_hi = PT_REGS_PARM2(ctx);
u64 trace_id_lo = PT_REGS_PARM3(ctx);
u64 span_id = PT_REGS_PARM4(ctx);
struct span_parent_t parent = { .trace_id_hi = trace_id_hi, .trace_id_lo = trace_id_lo, .span_id = span_id };
struct file_key_t key = { .tgid = tgid, .fd = fd };
struct sock **sk = bpf_map_lookup_elem(&fd_to_sock, &key);
Expand Down
19 changes: 9 additions & 10 deletions tracing/demos/cfs-throttling/stitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package main

/*
#include <stdint.h>
#include <sys/sdt.h>
void
#if defined(__clang__)
__attribute__ ((optnone))
#endif
cfs_set_parent_span(uint64_t trace_id_hi, uint64_t trace_id_lo, uint64_t span_id) { }
void cfs_set_parent_span(uint64_t trace_id_hi, uint64_t trace_id_lo, uint64_t span_id)
{
DTRACE_PROBE3(ebpf_exporter, cfs_set_parent_span, trace_id_hi, trace_id_lo, span_id);
}
void
#if defined(__clang__)
__attribute__ ((optnone))
#endif
cfs_clear_parent_span() { }
void cfs_clear_parent_span()
{
DTRACE_PROBE(ebpf_exporter, cfs_clear_parent_span);
}
*/
import "C"
import (
Expand Down
19 changes: 9 additions & 10 deletions tracing/demos/sched/stitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package main

/*
#include <stdint.h>
#include <sys/sdt.h>
void
#if defined(__clang__)
__attribute__ ((optnone))
#endif
sched_set_parent_span(uint64_t trace_id_hi, uint64_t trace_id_lo, uint64_t span_id) { }
void sched_set_parent_span(uint64_t trace_id_hi, uint64_t trace_id_lo, uint64_t span_id)
{
DTRACE_PROBE3(ebpf_exporter, sched_set_parent_span, trace_id_hi, trace_id_lo, span_id);
}
void
#if defined(__clang__)
__attribute__ ((optnone))
#endif
sched_clear_parent_span() { }
void sched_clear_parent_span()
{
DTRACE_PROBE(ebpf_exporter, sched_clear_parent_span);
}
*/
import "C"
import (
Expand Down
14 changes: 0 additions & 14 deletions tracing/demos/sock/enable.go

This file was deleted.

20 changes: 15 additions & 5 deletions tracing/demos/sock/stitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ package main

/*
#include <stdint.h>
void
#if defined(__clang__)
__attribute__ ((optnone))
#endif
sock_set_parent_span(int sock_fd, uint64_t trace_id_hi, uint64_t trace_id_lo, uint64_t span_id) { }
#include <sys/sdt.h>
void enable_kernel_tracing()
{
DTRACE_PROBE(ebpf_exporter, enable_kernel_tracing);
}
void sock_set_parent_span(int sock_fd, uint64_t trace_id_hi, uint64_t trace_id_lo, uint64_t span_id)
{
DTRACE_PROBE4(ebpf_exporter, sock_set_parent_span, sock_fd, trace_id_hi, trace_id_lo, span_id);
}
*/
import "C"
import (
"github.com/cloudflare/ebpf_exporter/v2/util"
"go.opentelemetry.io/otel/trace"
)

func enableKernelTracing() {
C.enable_kernel_tracing()
}

func sockSentParentSpan(fd uintptr, span trace.Span) {
byteOrder := util.GetHostByteOrder()

Expand Down

0 comments on commit 9be354d

Please sign in to comment.