mirror of
https://github.com/google/gops.git
synced 2024-11-24 08:22:25 +02:00
09ff00a4dd
Example output: $ gops <pid> parent PID: 5985 threads: 27 memory usage: 0.199% cpu usage: 0.139% username: jbd cmd+args: /Applications/Splice.app/Contents/Resources/Splice Helper.app/Contents/MacOS/Splice Helper -pid 5985 local/remote: 127.0.0.1:56765 <-> :0 (LISTEN) local/remote: 127.0.0.1:56765 <-> 127.0.0.1:50955 (ESTABLISHED) local/remote: 100.76.175.164:52353 <-> 54.241.191.232:443 (ESTABLISHED) Fixes #58.
186 lines
4.2 KiB
Go
186 lines
4.2 KiB
Go
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Program gops is a tool to list currently running Go processes.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/google/gops/goprocess"
|
|
"github.com/shirou/gopsutil/process"
|
|
)
|
|
|
|
const helpText = `gops is a tool to list and diagnose Go processes.
|
|
|
|
gops <cmd> <pid|addr> ...
|
|
gops <pid> # displays process info
|
|
|
|
Commands:
|
|
stack Prints the stack trace.
|
|
gc Runs the garbage collector and blocks until successful.
|
|
setgc Sets the garbage collection target percentage.
|
|
memstats Prints the allocation and garbage collection stats.
|
|
version Prints the Go version used to build the program.
|
|
stats Prints the vital runtime stats.
|
|
help Prints this help text.
|
|
|
|
Profiling commands:
|
|
trace Runs the runtime tracer for 5 secs and launches "go tool trace".
|
|
pprof-heap Reads the heap profile and launches "go tool pprof".
|
|
pprof-cpu Reads the CPU profile and launches "go tool pprof".
|
|
|
|
|
|
All commands require the agent running on the Go process.
|
|
Symbol "*" indicates the process runs the agent.`
|
|
|
|
// TODO(jbd): add link that explains the use of agent.
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
processes()
|
|
return
|
|
}
|
|
|
|
cmd := os.Args[1]
|
|
|
|
// See if it is a PID.
|
|
pid, err := strconv.Atoi(cmd)
|
|
if err == nil {
|
|
processInfo(pid)
|
|
return
|
|
}
|
|
|
|
if cmd == "help" {
|
|
usage("")
|
|
}
|
|
fn, ok := cmds[cmd]
|
|
if !ok {
|
|
usage("unknown subcommand")
|
|
}
|
|
addr, err := targetToAddr(os.Args[2])
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Couldn't resolve addr or pid %v to TCPAddress: %v\n", os.Args[2], err)
|
|
os.Exit(1)
|
|
}
|
|
var params []string
|
|
if len(os.Args) > 3 {
|
|
params = append(params, os.Args[3:]...)
|
|
}
|
|
if err := fn(*addr, params); err != nil {
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func processes() {
|
|
ps := goprocess.FindAll()
|
|
|
|
var maxPID, maxPPID, maxExec, maxVersion int
|
|
for i, p := range ps {
|
|
ps[i].BuildVersion = shortenVersion(p.BuildVersion)
|
|
maxPID = max(maxPID, len(strconv.Itoa(p.PID)))
|
|
maxPPID = max(maxPPID, len(strconv.Itoa(p.PPID)))
|
|
maxExec = max(maxExec, len(p.Exec))
|
|
maxVersion = max(maxVersion, len(ps[i].BuildVersion))
|
|
|
|
}
|
|
|
|
for _, p := range ps {
|
|
buf := bytes.NewBuffer(nil)
|
|
pid := strconv.Itoa(p.PID)
|
|
fmt.Fprint(buf, pad(pid, maxPID))
|
|
fmt.Fprint(buf, " ")
|
|
ppid := strconv.Itoa(p.PPID)
|
|
fmt.Fprint(buf, pad(ppid, maxPPID))
|
|
fmt.Fprint(buf, " ")
|
|
fmt.Fprint(buf, pad(p.Exec, maxExec))
|
|
if p.Agent {
|
|
fmt.Fprint(buf, "*")
|
|
} else {
|
|
fmt.Fprint(buf, " ")
|
|
}
|
|
fmt.Fprint(buf, " ")
|
|
fmt.Fprint(buf, pad(p.BuildVersion, maxVersion))
|
|
fmt.Fprint(buf, " ")
|
|
fmt.Fprint(buf, p.Path)
|
|
fmt.Fprintln(buf)
|
|
buf.WriteTo(os.Stdout)
|
|
}
|
|
}
|
|
|
|
func processInfo(pid int) {
|
|
p, err := process.NewProcess(int32(pid))
|
|
if err != nil {
|
|
log.Fatalf("Cannot read process info: %v", err)
|
|
}
|
|
if v, err := p.Parent(); err == nil {
|
|
fmt.Printf("parent PID:\t%v\n", v.Pid)
|
|
}
|
|
if v, err := p.NumThreads(); err == nil {
|
|
fmt.Printf("threads:\t%v\n", v)
|
|
}
|
|
if v, err := p.MemoryPercent(); err == nil {
|
|
fmt.Printf("memory usage:\t%.3f%%\n", v)
|
|
}
|
|
if v, err := p.CPUPercent(); err == nil {
|
|
fmt.Printf("cpu usage:\t%.3f%%\n", v)
|
|
}
|
|
if v, err := p.Username(); err == nil {
|
|
fmt.Printf("username:\t%v\n", v)
|
|
}
|
|
if v, err := p.Cmdline(); err == nil {
|
|
fmt.Printf("cmd+args:\t%v\n", v)
|
|
}
|
|
if v, err := p.Connections(); err == nil {
|
|
if len(v) > 0 {
|
|
for _, conn := range v {
|
|
fmt.Printf("local/remote:\t%v:%v <-> %v:%v (%v)\n",
|
|
conn.Laddr.IP, conn.Laddr.Port, conn.Raddr.IP, conn.Raddr.Port, conn.Status)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var develRe = regexp.MustCompile(`devel\s+\+\w+`)
|
|
|
|
func shortenVersion(v string) string {
|
|
if !strings.HasPrefix(v, "devel") {
|
|
return v
|
|
}
|
|
results := develRe.FindAllString(v, 1)
|
|
if len(results) == 0 {
|
|
return v
|
|
}
|
|
return results[0]
|
|
}
|
|
|
|
func usage(msg string) {
|
|
if msg != "" {
|
|
fmt.Printf("gops: %v\n", msg)
|
|
}
|
|
fmt.Fprintf(os.Stderr, "%v\n", helpText)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func pad(s string, total int) string {
|
|
if len(s) >= total {
|
|
return s
|
|
}
|
|
return s + strings.Repeat(" ", total-len(s))
|
|
}
|
|
|
|
func max(i, j int) int {
|
|
if i > j {
|
|
return i
|
|
}
|
|
return j
|
|
}
|