From 7eb26a395d9d1bd0c81c27e0f2cb07664dfe6a5c Mon Sep 17 00:00:00 2001 From: seiuneko Date: Mon, 18 Nov 2024 15:52:44 +0800 Subject: [PATCH] optimize: reduce memory usage by reading kallsyms only in the trace command lowers RSS usage from 196 MiB to 77 MiB using a minimal bootable config --- cmd/trace.go | 3 +++ trace/kallsyms.go | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/trace.go b/cmd/trace.go index 0c67775f5..2bd47fff2 100644 --- a/cmd/trace.go +++ b/cmd/trace.go @@ -31,6 +31,9 @@ func init() { traceCmd := &cobra.Command{ Use: "trace", Short: "To trace traffic", + PreRun: func(cmd *cobra.Command, args []string) { + trace.ReadKallsyms() + }, Run: func(cmd *cobra.Command, args []string) { internal.AutoSu() diff --git a/trace/kallsyms.go b/trace/kallsyms.go index aa575d919..c920cb87d 100644 --- a/trace/kallsyms.go +++ b/trace/kallsyms.go @@ -26,11 +26,7 @@ var kallsyms []Symbol var kallsymsByName map[string]Symbol = make(map[string]Symbol) var kallsymsByAddr map[uint64]Symbol = make(map[uint64]Symbol) -func init() { - readKallsyms() -} - -func readKallsyms() { +func ReadKallsyms() { file, err := os.Open("/proc/kallsyms") if err != nil { logrus.Fatalf("failed to open /proc/kallsyms: %v", err)