From 828d4df6f06343d35f14d6ba1cf5a35a7cf9c4b5 Mon Sep 17 00:00:00 2001 From: Anis Eleuch Date: Thu, 29 Feb 2024 17:28:18 +0100 Subject: [PATCH] debug: Add --search to print only specific goroutines (#19158) Easier to filter goroutines belonging to a specific subsystem --- docs/debugging/pprofgoparser/main.go | 29 ++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/docs/debugging/pprofgoparser/main.go b/docs/debugging/pprofgoparser/main.go index 692cc2ea0..b35f25638 100644 --- a/docs/debugging/pprofgoparser/main.go +++ b/docs/debugging/pprofgoparser/main.go @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2023 MinIO, Inc. +// Copyright (c) 2015-2024 MinIO, Inc. // // This file is part of MinIO Object Storage stack // @@ -33,16 +33,19 @@ import ( ) var ( - re *regexp.Regexp + goroutinesRE, searchRE *regexp.Regexp + + // User input flags + searchText string goTime, less, margin time.Duration ) func init() { - re = regexp.MustCompile(`^goroutine [0-9]+ \[[^,]+(, ([0-9]+) minutes)?\]:$`) flag.DurationVar(&less, "less", 0, "goroutine waiting less than the specified time") flag.DurationVar(&goTime, "time", 0, "goroutine waiting for exactly the specified time") flag.DurationVar(&margin, "margin", 0, "margin time") + flag.StringVar(&searchText, "search", "", "Regex to search for a text in one goroutine stacktrace") } func parseGoroutineType2(path string) (map[time.Duration][]string, error) { @@ -77,13 +80,16 @@ func parseGoroutineType2(path string) (map[time.Duration][]string, error) { case skip && line == "": skip = false case record && line == "": - record = false - ret[t] = append(ret[t], bf.String()) + stackTrace := bf.String() reset() + record = false + if searchRE == nil || searchRE.MatchString(stackTrace) { + ret[t] = append(ret[t], stackTrace) + } case record: save(line) default: - z := re.FindStringSubmatch(line) + z := goroutinesRE.FindStringSubmatch(line) if len(z) == 3 { if z[2] != "" { a, _ := strconv.Atoi(z[2]) @@ -115,6 +121,17 @@ func main() { log.Fatal(helpUsage) } + var err error + + goroutinesRE = regexp.MustCompile(`^goroutine [0-9]+ \[[^,]+(, ([0-9]+) minutes)?\]:$`) + + if searchText != "" { + searchRE, err = regexp.Compile(searchText) + if err != nil { + log.Fatal(err) + } + } + for _, arg := range flag.Args() { if !strings.HasSuffix(arg, "-goroutines-before,debug=2.txt") { continue