1
0
mirror of https://github.com/google/gops.git synced 2025-07-13 00:10:13 +02:00

add "setmemlimit" command for utilizing new memory limit feature in Go 1.19+

This commit is contained in:
Qishuai Liu
2023-07-07 13:18:14 +09:00
parent 3e466bcedc
commit c8c413fa57
5 changed files with 75 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net"
"os"
"os/exec"
@ -45,6 +46,11 @@ func AgentCommands() []*cobra.Command {
short: "Sets the garbage collection target percentage. To completely stop GC, set to 'off'",
fn: setGC,
},
{
name: "setmemlimit",
short: "Sets the memory limit for the process. To disable the limit, set to 'off'",
fn: setMemoryLimit,
},
{
name: "memstats",
short: "Prints the allocation and garbage collection stats.",
@ -143,6 +149,27 @@ func setGC(addr net.TCPAddr, params []string) error {
return cmdWithPrint(addr, signal.SetGCPercent, buf...)
}
func setMemoryLimit(addr net.TCPAddr, params []string) error {
if len(params) != 1 {
return errors.New("missing memory limit")
}
var (
limit int64
err error
)
if strings.ToLower(params[0]) == "off" {
limit = math.MaxInt64
} else {
limit, err = strconv.ParseInt(params[0], 10, strconv.IntSize)
if err != nil {
return err
}
}
buf := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(buf, limit)
return cmdWithPrint(addr, signal.SetMemLimit, buf...)
}
func stackTrace(addr net.TCPAddr, _ []string) error {
return cmdWithPrint(addr, signal.StackTrace)
}