Skip to content

Commit

Permalink
feat: disable memory profiling by default
Browse files Browse the repository at this point in the history
To save resources.

Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
  • Loading branch information
AlekSi authored and talos-bot committed May 13, 2021
1 parent c6d0ae2 commit 5b292e5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
29 changes: 19 additions & 10 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ func ListenAndServe(ctx context.Context, addr string, log LogFunc) error {
return nil
}

// that's defaults, just make them explicit
runtime.SetCPUProfileRate(100)
runtime.MemProfileRate = 512 * 1024

// https://github.com/DataDog/go-profiler-notes/blob/main/block.md#overhead
runtime.SetBlockProfileRate(10000)

// no science behind that value
runtime.SetMutexProfileFraction(10000)

log("starting debug server")

for _, h := range handlers() {
Expand Down Expand Up @@ -85,3 +75,22 @@ func handlers() []string {

return res
}

func init() {
if !Enabled {
// explicitly disable memory profiling to save around 1.4MiB of memory
runtime.MemProfileRate = 0

return
}

// that's defaults, just make them explicit
runtime.SetCPUProfileRate(100)
runtime.MemProfileRate = 512 * 1024

// https://github.com/DataDog/go-profiler-notes/blob/main/block.md#overhead
runtime.SetBlockProfileRate(10000)

// no science behind that value
runtime.SetMutexProfileFraction(10000)
}
2 changes: 2 additions & 0 deletions debug_off.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
package debug

// Enabled is false when compiled without sidero.debug build tag.
//
// Profiling is disabled to safe resources as a side-effect of importing this package.
const Enabled = false
3 changes: 3 additions & 0 deletions debug_off_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package debug //nolint:testpackage // to test unexported method

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,4 +16,6 @@ import (
func TestDebugOff(t *testing.T) {
expected := []string{}
assert.Equal(t, expected, handlers())

assert.Equal(t, 0, runtime.MemProfileRate)
}
2 changes: 2 additions & 0 deletions debug_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ import (
)

// Enabled is true when compiled with sidero.debug build tag.
//
// Profiling rates are configured as a side-effect of importing this package.
const Enabled = true
3 changes: 3 additions & 0 deletions debug_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package debug //nolint:testpackage // to test unexported method

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -24,4 +25,6 @@ func TestDebugOn(t *testing.T) {
"/debug/vars",
}
assert.Equal(t, expected, handlers())

assert.Equal(t, 524288, runtime.MemProfileRate)
}

0 comments on commit 5b292e5

Please sign in to comment.