-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 36-tracepoint-args-sched_switch-use-custom-struct
- Loading branch information
Showing
12 changed files
with
366 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 @@ | ||
../common/Makefile |
18 changes: 18 additions & 0 deletions
18
36-tracepoint-args-sched_switch-use-custom-struct/README.md
This file contains 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,18 @@ | ||
|
||
https://mozillazg.com/2022/05/ebpf-libbpf-tracepoint-common-questions.html | ||
|
||
## Usage | ||
|
||
build: | ||
|
||
``` | ||
$ make | ||
``` | ||
|
||
run: | ||
|
||
``` | ||
$ make run | ||
$ make cat | ||
``` |
1 change: 1 addition & 0 deletions
1
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/Makefile
This file contains 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 @@ | ||
../../common/cilium-ebpf.Makefile |
17 changes: 17 additions & 0 deletions
17
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/README.md
This file contains 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,17 @@ | ||
|
||
|
||
## Usage | ||
|
||
build: | ||
|
||
``` | ||
$ make | ||
``` | ||
|
||
run: | ||
|
||
``` | ||
$ make run | ||
$ make cat | ||
``` |
115 changes: 115 additions & 0 deletions
115
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/bpf_bpfeb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+2.21 KB
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/bpf_bpfeb.o
Binary file not shown.
115 changes: 115 additions & 0 deletions
115
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/bpf_bpfel.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+2.21 KB
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/bpf_bpfel.o
Binary file not shown.
35 changes: 35 additions & 0 deletions
35
36-tracepoint-args-sched_switch-use-custom-struct/cilium-ebpf/main.go
This file contains 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 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/cilium/ebpf/link" | ||
"github.com/cilium/ebpf/rlimit" | ||
) | ||
|
||
// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile | ||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../main.bpf.c -- -I../ -I../output | ||
|
||
func main() { | ||
if err := rlimit.RemoveMemlock(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
objs := bpfObjects{} | ||
if err := loadBpfObjects(&objs, nil); err != nil { | ||
log.Fatal(err) | ||
} | ||
defer objs.Close() | ||
|
||
tp, err := link.Tracepoint("sched", "sched_switch", objs.TracepointSchedSchedSwitch, nil) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer tp.Close() | ||
|
||
log.Println("Waiting for events...") | ||
time.Sleep(time.Minute * 1024) | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
36-tracepoint-args-sched_switch-use-custom-struct/main.bpf.c
This file contains 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,29 @@ | ||
#include "vmlinux.h" | ||
|
||
#include <bpf/bpf_core_read.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
// sudo cat /sys/kernel/debug/tracing/events/sched/sched_switch/format | ||
struct sched_switch_args { | ||
char _[8]; | ||
char prev_comm[16]; | ||
pid_t prev_pid; | ||
int prev_prio; | ||
long prev_state; | ||
char next_comm[16]; | ||
pid_t next_pid; | ||
int next_prio; | ||
}; | ||
|
||
SEC("tracepoint/sched/sched_switch") | ||
int tracepoint__sched__sched_switch(struct sched_switch_args *ctx) { | ||
u32 prev_pid = (u32)ctx->prev_pid; | ||
u32 next_pid = (u32)ctx->next_pid; | ||
|
||
char fmt[] = "sched_switch %d -> %d\n"; | ||
bpf_trace_printk(fmt, sizeof(fmt), prev_pid, next_pid); | ||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
This file contains 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 main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
bpf "github.com/aquasecurity/libbpfgo" | ||
) | ||
|
||
func main() { | ||
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer bpfModule.Close() | ||
|
||
if err := bpfModule.BPFLoadObject(); err != nil { | ||
panic(err) | ||
} | ||
prog, err := bpfModule.GetProgram("tracepoint__sched__sched_switch") | ||
if err != nil { | ||
panic(err) | ||
} | ||
if _, err := prog.AttachTracepoint("sched", "sched_switch"); err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
fmt.Println("Waiting...") | ||
time.Sleep(10 * time.Second) | ||
} | ||
} |
This file contains 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