From dbeb29cd3eececdc80f27b286014952fb717b1a3 Mon Sep 17 00:00:00 2001 From: segevda Date: Fri, 10 Feb 2023 12:24:10 +0200 Subject: [PATCH] cmd/shared: add 'off' option to setgc (#201) To stop GC completely, debug.SetGCPercentage needs to receive a negative integer. Passing a negative integer is cumbersome: $ gops setgc -- -1 This commit adds a more user-friendly way to achieve that: $ gops setgc off This behavior is aligned with that of the GOGC env variable, where negative or "off" can be set to stop the GC. --- README.md | 5 +++++ internal/cmd/shared.go | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c8afeb7..38ea3c1 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,11 @@ The following command sets it to 10%: ``` sh $ gops setgc (|) 10 ``` +The following command turns off the garbage collector: + +```sh +$ gops setgc (|) off +``` #### $ gops version (\|\) diff --git a/internal/cmd/shared.go b/internal/cmd/shared.go index c90880d..669de35 100644 --- a/internal/cmd/shared.go +++ b/internal/cmd/shared.go @@ -43,7 +43,7 @@ func AgentCommands() []*cobra.Command { }, { name: "setgc", - short: "Sets the garbage collection target percentage.", + short: "Sets the garbage collection target percentage. To completely stop GC, set to 'off'", fn: setGC, }, { @@ -127,9 +127,17 @@ func setGC(addr net.TCPAddr, params []string) error { if len(params) != 1 { return errors.New("missing gc percentage") } - perc, err := strconv.ParseInt(params[0], 10, strconv.IntSize) - if err != nil { - return err + var ( + perc int64 + err error + ) + if strings.ToLower(params[0]) == "off" { + perc = -1 + } else { + perc, err = strconv.ParseInt(params[0], 10, strconv.IntSize) + if err != nil { + return err + } } buf := make([]byte, binary.MaxVarintLen64) binary.PutVarint(buf, perc)