Skip to content

Commit

Permalink
auto: enable mutex, blocking profiles by default
Browse files Browse the repository at this point in the history
This enables the mutex and blocking profiles and adds an escape hatch to
disable or tune them for now.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jul 7, 2023
1 parent 86f7a86 commit 1e574c2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions initialize/auto/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var msgs = []func(context.Context){}
func init() {
CPU()
Memory()
Profiling()
}

// PrintLogs uses zlog to report any messages queued up from the runs of
Expand Down
57 changes: 57 additions & 0 deletions initialize/auto/profiling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package auto

import (
"context"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/quay/zlog"
)

// Profiling enables block and mutex profiling.
//
// This function uses the magic environment variable "CLAIRDEBUG" to control the
// values used. This escape hatch will go away in the future.
func Profiling() {
// Catch contention at a granularity of 1 microsecond.
blockCur := 1000
// Catch 1/10 mutex contention events.
mutexCur := 10
fromEnv := false
if s, ok := os.LookupEnv(`CLAIRDEBUG`); ok {
var err error
for _, kv := range strings.Split(s, ",") {
k, v, ok := strings.Cut(kv, "=")
if !ok {
continue
}
switch k {
case "blockprofile":
fromEnv = true
blockCur, err = strconv.Atoi(v)
case "mutexprofile":
fromEnv = true
mutexCur, err = strconv.Atoi(v)
default:
}
if err != nil {
panic(err)
}
}
}

runtime.SetBlockProfileRate(blockCur)
mutexPrev := runtime.SetMutexProfileFraction(mutexCur)
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).
Bool("from_env", fromEnv).
Dur("block_rate", time.Duration(blockCur)*time.Nanosecond).
Str("prev_mutex_frac", fmt.Sprintf("1/%d", mutexPrev)).
Str("cur_mutex_frac", fmt.Sprintf("1/%d", mutexCur)).
Msg("profiling rates configured")
})
}

0 comments on commit 1e574c2

Please sign in to comment.