Skip to content
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

fix(bpfassets): Fix object file lookup #1419

Merged
merged 1 commit into from
May 13, 2024
Merged
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
51 changes: 30 additions & 21 deletions pkg/bpfassets/attacher/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ import (
)

const (
objectFilename = "kepler.%s.o"
bpfAssesstsLocation = "/var/lib/kepler/bpfassets"
bpfAssesstsLocalPath = "../../../bpfassets/libbpf/bpf.o"
cpuOnline = "/sys/devices/system/cpu/online"
CPUCycleLabel = config.CPUCycle
CPURefCycleLabel = config.CPURefCycle
CPUInstructionLabel = config.CPUInstruction
CacheMissLabel = config.CacheMiss
TaskClockLabel = config.TaskClock
objectFilename = "kepler.%s.o"
bpfAssestsLocation = "/var/lib/kepler/bpfassets"
cpuOnline = "/sys/devices/system/cpu/online"
CPUCycleLabel = config.CPUCycle
CPURefCycleLabel = config.CPURefCycle
CPUInstructionLabel = config.CPUInstruction
CacheMissLabel = config.CacheMiss
TaskClockLabel = config.TaskClock

// Per /sys/kernel/debug/tracing/events/irq/softirq_entry/format
// { 0, "HI" }, { 1, "TIMER" }, { 2, "NET_TX" }, { 3, "NET_RX" }, { 4, "BLOCK" }, { 5, "IRQ_POLL" }, { 6, "TASKLET" }, { 7, "SCHED" }, { 8, "HRTIMER" }, { 9, "RCU" }
Expand Down Expand Up @@ -149,20 +148,27 @@ func getLibbpfObjectFilePath() (string, error) {
endianness = "bpfeb"
}
filename := fmt.Sprintf(objectFilename, endianness)
bpfassetsPath := fmt.Sprintf("%s/%s", bpfAssesstsLocation, filename)
bpfassetsPath := fmt.Sprintf("%s/%s", bpfAssestsLocation, filename)
_, err := os.Stat(bpfassetsPath)
if err != nil {
var absPath string
// try relative path
absPath, err = filepath.Abs(bpfAssesstsLocalPath)
// attempt to find the bpf assets in the same directory as the binary
// this is useful for running locally
var matches []string
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if info.Name() == filename {
matches = append(matches, path)
return filepath.SkipAll
}
return nil
})
if err != nil {
return "", err
return "", fmt.Errorf("failed to find bpf object file: %v", err)
}
bpfassetsPath = fmt.Sprintf("%s/%s", absPath, filename)
_, err = os.Stat(bpfassetsPath)
if err != nil {
return "", err
if len(matches) < 1 {
return "", fmt.Errorf("failed to find bpf object file: no matches found")
}
klog.Infof("found bpf object file: %s", matches[0])
return matches[0], nil
dave-tucker marked this conversation as resolved.
Show resolved Hide resolved
}
return bpfassetsPath, nil
}
Expand All @@ -187,12 +193,15 @@ func attachLibbpfModule() (*bpf.Module, error) {
}()
var libbpfObjectFilePath string
libbpfObjectFilePath, err = getLibbpfObjectFilePath()
if err == nil {
libbpfModule, err = bpf.NewModuleFromFile(libbpfObjectFilePath)
if err != nil {
return nil, fmt.Errorf("failed to find ebpf bytecode: %v", err)
}

libbpfModule, err = bpf.NewModuleFromFile(libbpfObjectFilePath)
if err != nil {
return nil, fmt.Errorf("failed to load module: %v", err)
return nil, fmt.Errorf("failed to load eBPF module from libbpf object: %v", err)
}

// resize array entries
for _, arrayName := range bpfArrays {
err = resizeArrayEntries(arrayName, cpuCores)
Expand Down
Loading