Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions examples/scx/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/scx/bpf_bpfeb.o
Binary file not shown.
144 changes: 144 additions & 0 deletions examples/scx/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/scx/bpf_bpfel.o
Binary file not shown.
42 changes: 42 additions & 0 deletions examples/scx/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

// This program demonstrates attaching an eBPF program to sched_ext

import (
"log"
"os"
"os/signal"
"syscall"

"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -tags linux bpf sched.c -- -I../headers

func main() {
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}

objs := bpfObjects{}
if err := loadBpfObjects(&objs, nil); err != nil {
log.Fatalf("loading objects: %v", err)
}
defer objs.Close()

m := objs.Scx

Check failure on line 28 in examples/scx/main.go

View workflow job for this annotation

GitHub Actions / Build and Lint

objs.Scx undefined (type bpfObjects has no field or method Scx) (compile)
l, err := link.AttachStructOps(m)
if err != nil {
log.Fatalf("Failed to attach sched_ext: %s", err)
}
defer l.DetachStructOps()

log.Println("Successfully attached sched_ext struct operations")

stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
log.Print("Press Ctrl+C to stop...")

<-stopper
}
59 changes: 59 additions & 0 deletions examples/scx/sched.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// go:build ignore

#include "common.h"

char __license[] SEC("license") = "Dual MIT/GPL";

struct sched_ext_ops {
__s32 (*init)(void);
void (*runnable)(void *, __u64);
void (*running)(void *);
void (*stopping)(void *, __u8);
void (*quiescent)(void *, __u64);
__u8 (*yield)(void *, void *);
__u32 timeout_ms;
char name[128];
};

SEC("struct_ops/scx_runnable")
void scx_runnable(void *p, __u64 enq_flags) {
bpf_printk("scheduler runnable\n");
};

SEC("struct_ops/scx_running")
void scx_running(void *p) {
bpf_printk("scheduler running\n");
};

SEC("struct_ops/scx_stopping")
void scx_stopping(void *p, __u8 runnable) {
bpf_printk("scheduler stopping\n");
};

SEC("struct_ops/scx_quiescent")
void scx_quiescent(void *p, __u64 deq_flags) {
bpf_printk("scheduler quiescent\n");
};

SEC("struct_ops/scx_yield")
void scx_yield(void *from, void *to) {
bpf_printk("scheduler yield\n");
};

SEC("struct_ops.s/scx_init")
int scx_init(void) {
bpf_printk("scheduler init\n");
return 0;
};

SEC(".struct_ops.link")
struct sched_ext_ops scx = {
.init = (void *)scx_init,
.running = (void *)scx_running,
.runnable = (void *)scx_runnable,
.stopping = (void *)scx_stopping,
.quiescent = (void *)scx_quiescent,
.yield = (void *)scx_yield,
.timeout_ms = 10000U,
.name = "scx",
};
Loading
Loading