Skip to content

Commit dbeb29c

Browse files
authored
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 <pid> -- -1 This commit adds a more user-friendly way to achieve that: $ gops setgc <pid> off This behavior is aligned with that of the GOGC env variable, where negative or "off" can be set to stop the GC.
1 parent 2b68179 commit dbeb29c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ The following command sets it to 10%:
189189
``` sh
190190
$ gops setgc (<pid>|<addr>) 10
191191
```
192+
The following command turns off the garbage collector:
193+
194+
```sh
195+
$ gops setgc (<pid>|<addr>) off
196+
```
192197

193198
#### $ gops version (\<pid\>|\<addr\>)
194199

internal/cmd/shared.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func AgentCommands() []*cobra.Command {
4343
},
4444
{
4545
name: "setgc",
46-
short: "Sets the garbage collection target percentage.",
46+
short: "Sets the garbage collection target percentage. To completely stop GC, set to 'off'",
4747
fn: setGC,
4848
},
4949
{
@@ -127,9 +127,17 @@ func setGC(addr net.TCPAddr, params []string) error {
127127
if len(params) != 1 {
128128
return errors.New("missing gc percentage")
129129
}
130-
perc, err := strconv.ParseInt(params[0], 10, strconv.IntSize)
131-
if err != nil {
132-
return err
130+
var (
131+
perc int64
132+
err error
133+
)
134+
if strings.ToLower(params[0]) == "off" {
135+
perc = -1
136+
} else {
137+
perc, err = strconv.ParseInt(params[0], 10, strconv.IntSize)
138+
if err != nil {
139+
return err
140+
}
133141
}
134142
buf := make([]byte, binary.MaxVarintLen64)
135143
binary.PutVarint(buf, perc)

0 commit comments

Comments
 (0)