Skip to content

Commit

Permalink
Add process info
Browse files Browse the repository at this point in the history
Example output:

$ gops <pid>
threads:      14
memory usage: 0.090%
cpu usage:    0.088%
username:     jbd
cmd+args:     /Users/jbd/bin/gocode -s -sock unix -addr 127.0.0.1:37373

Fixes #58.
  • Loading branch information
rakyll committed Feb 28, 2018
1 parent e09130d commit 92413e6
Show file tree
Hide file tree
Showing 512 changed files with 177,339 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.

40 changes: 34 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,28 @@ 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.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)
}
}

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 92413e6

Please sign in to comment.