Skip to content

Commit

Permalink
Add process info (#59)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rakyll authored Feb 28, 2018
1 parent e09130d commit 09ff00a
Show file tree
Hide file tree
Showing 512 changed files with 177,350 additions and 39,896 deletions.
32 changes: 28 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 45 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ 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.
Usage:
gops [command [pid|address:port]]
gops <cmd> <pid|addr> ...
gops <pid> # displays process info
Commands:
stack Prints the stack trace.
Expand Down Expand Up @@ -49,12 +50,17 @@ func main() {
}

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("")
}
if len(os.Args) < 3 {
usage("missing PID or address")
}
fn, ok := cmds[cmd]
if !ok {
usage("unknown subcommand")
Expand Down Expand Up @@ -110,6 +116,39 @@ func processes() {
}
}

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 {
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/StackExchange/wmi/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/StackExchange/wmi/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 09ff00a

Please sign in to comment.