diff --git a/cmd/exporter.go b/cmd/exporter.go index 6e74ecec61..c19c918d41 100644 --- a/cmd/exporter.go +++ b/cmd/exporter.go @@ -152,7 +152,9 @@ func main() { config.SetEnabledHardwareCounterMetrics(*exposeHardwareCounterMetrics) config.SetEnabledGPU(*enableGPU) config.EnabledMSR = *enabledMSR - config.SetKernelSourceDir(*kernelSourceDirPath) + if err := config.SetKernelSourceDir(*kernelSourceDirPath); err != nil { + klog.Warningf("failed to set kernel source dir to %q: %v", *kernelSourceDirPath, err) + } // the ebpf batch deletion operation was introduced in linux kernel 5.6, which provides better performance to delete keys. // but the user can enable it if the kernel has backported this functionality. diff --git a/pkg/bpfassets/attacher/bcc_attacher.go b/pkg/bpfassets/attacher/bcc_attacher.go index 1d52b31fc0..5df5277b8c 100644 --- a/pkg/bpfassets/attacher/bcc_attacher.go +++ b/pkg/bpfassets/attacher/bcc_attacher.go @@ -141,7 +141,7 @@ func AttachBPFAssets() (*BpfModuleTables, error) { m, err := loadModule(objProg, options) if err != nil { klog.Infof("failed to attach perf module with options %v: %v, from default kernel source.\n", options, err) - dirs := config.GetKernelSourceDir() + dirs := config.GetKernelSourceDirs() for _, dir := range dirs { klog.Infof("trying to load eBPF module with kernel source dir %s", dir) os.Setenv("BCC_KERNEL_SOURCE", dir) diff --git a/pkg/config/config.go b/pkg/config/config.go index 756f584769..a2286ab6ac 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -88,7 +88,7 @@ var ( configPath = "/etc/kepler/kepler.config" // dir of kernel sources for bcc - kernelSourceDir = []string{} + kernelSourceDirs = []string{} //////////////////////////////////// ModelServerEnable = getBoolConfig("MODEL_SERVER_ENABLE", false) @@ -153,35 +153,30 @@ func getConfig(configKey, defaultValue string) (result string) { // SetKernelSourceDir sets the directory for all kernel source. This is used for bcc. Only the top level directory is needed. func SetKernelSourceDir(dir string) error { + fileInfo, err := os.Stat(dir) + if err != nil { + return err + } -fileInfo, err := os.Stat(path) -if err != nil { - return err -} - -if !fileInfo.IsDir() { - return fmt.Errorf("expected kernel root path %s to be a directory", dir) -} - // read all the kernel source directories - if dir != "" { - // list all the directories under `dir` and store in kernelSourceDir - klog.V(4).Infoln("kernel source dir is set to", dir) - files, err := os.ReadDir(dir) - if err != nil { - klog.Warning("failed to read kernel source dir", err) - } - kernelVers := fmt.Sprintf("%v", getKernelVersion(c)) - for _, file := range files { - // only store directories and the directory name should contain the kernel version - if strings.Contains(file.Name(), kernelVers) && file.IsDir() { - kernelSourceDir = append(kernelSourceDir, filepath.Join(dir, file.Name())) - } + if !fileInfo.IsDir() { + return fmt.Errorf("expected kernel root path %s to be a directory", dir) + } + // list all the directories under dir and store in kernelSourceDir + klog.Infoln("kernel source dir is set to", dir) + files, err := os.ReadDir(dir) + if err != nil { + klog.Warning("failed to read kernel source dir", err) + } + for _, file := range files { + if file.IsDir() { + kernelSourceDirs = append(kernelSourceDirs, filepath.Join(dir, file.Name())) } } + return nil } -func GetKernelSourceDir() []string { - return kernelSourceDir +func GetKernelSourceDirs() []string { + return kernelSourceDirs } func SetModelServerReqEndpoint() (modelServerReqEndpoint string) {