From 98435d0aac0ea7294d1a45401675b85c82721854 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Wed, 14 Jun 2023 14:10:33 +0200 Subject: [PATCH] Remove Profiler from runtime environment Signed-off-by: Pablo Chacin --- cmd/agent/commands/root.go | 4 ++-- pkg/runtime/fake.go | 31 ------------------------------- pkg/runtime/profiler.go | 19 ++++++++----------- pkg/runtime/runtime.go | 8 -------- 4 files changed, 10 insertions(+), 52 deletions(-) diff --git a/cmd/agent/commands/root.go b/cmd/agent/commands/root.go index a7896c42..cd6ee3ca 100644 --- a/cmd/agent/commands/root.go +++ b/cmd/agent/commands/root.go @@ -67,13 +67,13 @@ func (r *RootCommand) Do(ctx context.Context) error { _ = r.env.Lock().Release() }() - profiler, err := r.env.Profiler().Start(r.profilerConfig) + profiler, err := runtime.StartProfiler(r.profilerConfig) if err != nil { return fmt.Errorf("could not create profiler %w", err) } defer func() { - _ = profiler.Close() + _ = profiler.Stop() }() // set context for command diff --git a/pkg/runtime/fake.go b/pkg/runtime/fake.go index a20741ed..7cbb2319 100644 --- a/pkg/runtime/fake.go +++ b/pkg/runtime/fake.go @@ -1,7 +1,6 @@ package runtime import ( - "io" "strings" ) @@ -94,29 +93,6 @@ func NewCallbackExecutor(callback ExecCallback) *CallbackExecutor { } } -// FakeProfiler is a noop profiler for testing -type FakeProfiler struct { - started bool - stopped bool -} - -// NewFakeProfiler creates a new FakeProfiler -func NewFakeProfiler() *FakeProfiler { - return &FakeProfiler{} -} - -// Start updates the FakeProfiler to registers it was started -func (p *FakeProfiler) Start(c ProfilerConfig) (io.Closer, error) { - p.started = true - return p, nil -} - -// Close updates the FakeProfiler to registers it was stopped operation -func (p *FakeProfiler) Close() error { - p.stopped = true - return nil -} - // FakeLock implements a Lock for testing type FakeLock struct { locked bool @@ -145,7 +121,6 @@ type FakeRuntime struct { FakeArgs []string FakeVars map[string]string FakeExecutor *FakeExecutor - FakeProfiler *FakeProfiler FakeLock *FakeLock } @@ -154,7 +129,6 @@ func NewFakeRuntime(args []string, vars map[string]string) *FakeRuntime { return &FakeRuntime{ FakeArgs: args, FakeVars: vars, - FakeProfiler: NewFakeProfiler(), FakeExecutor: NewFakeExecutor(nil, nil), FakeLock: NewFakeLock(), } @@ -162,11 +136,6 @@ func NewFakeRuntime(args []string, vars map[string]string) *FakeRuntime { // implement Runtime interface -// Profiler implements Profiler method from Runtime interface -func (f *FakeRuntime) Profiler() Profiler { - return f.FakeProfiler -} - // Executor implements Executor method from Runtime interface func (f *FakeRuntime) Executor() Executor { return f.FakeExecutor diff --git a/pkg/runtime/profiler.go b/pkg/runtime/profiler.go index da196782..f9974b45 100644 --- a/pkg/runtime/profiler.go +++ b/pkg/runtime/profiler.go @@ -2,7 +2,6 @@ package runtime import ( "fmt" - "io" "os" goruntime "runtime" "runtime/pprof" @@ -20,10 +19,10 @@ type ProfilerConfig struct { TraceFileName string } -// Profiler defines the methods to control execution profiling +// Profiler defines the methods to control the execution profiling type Profiler interface { - // Start initiates the tracing. If already active, has no effect - Start(ProfilerConfig) (io.Closer, error) + // Stops profiling + Stop() error } // profiler maintains the configuration state of the profiler @@ -34,12 +33,10 @@ type profiler struct { traceFile *os.File } -// DefaultProfiler creates a Profiler instance -func DefaultProfiler() Profiler { - return &profiler{} -} - -func (p *profiler) Start(config ProfilerConfig) (io.Closer, error) { +// StartProfiler starts the profiler with the given configuration, returning a Profiler +// to control its execution. +func StartProfiler(config ProfilerConfig) (Profiler, error) { + p := &profiler{} var err error if config.MemProfile { @@ -94,7 +91,7 @@ func (p *profiler) Start(config ProfilerConfig) (io.Closer, error) { return p, nil } -func (p *profiler) Close() error { +func (p *profiler) Stop() error { if p.CPUProfile { pprof.StopCPUProfile() } diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index 930f35e5..5c3af215 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -13,8 +13,6 @@ type Environment interface { Executor() Executor // Lock returns an interface for a process lock Lock() Lock - // Profiler return an execution profiler - Profiler() Profiler // Vars returns the environment variables Vars() map[string]string // Args returns the arguments passed to the process @@ -25,7 +23,6 @@ type Environment interface { type environment struct { executor Executor lock Lock - profiler Profiler vars map[string]string args []string } @@ -47,7 +44,6 @@ func DefaultEnvironment() Environment { vars := getEnv() return &environment{ executor: DefaultExecutor(), - profiler: DefaultProfiler(), lock: DefaultLock(), vars: vars, args: args, @@ -62,10 +58,6 @@ func (e *environment) Lock() Lock { return e.lock } -func (e *environment) Profiler() Profiler { - return e.profiler -} - func (e *environment) Vars() map[string]string { return e.vars }