forked from davecheney/gcvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 1.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// gcvis is a tool to assist you visualising the operation of
// the go runtime garbage collector.
//
// usage:
//
// gcvis program [arguments]...
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/pkg/browser"
"golang.org/x/crypto/ssh/terminal"
)
var iface = flag.String("i", "127.0.0.1", "specify interface to use. defaults to 127.0.0.1.")
var port = flag.String("p", "0", "specify port to use.")
var openBrowser = flag.Bool("o", true, "automatically open browser")
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: command <args>...\n", os.Args[0])
flag.PrintDefaults()
}
var pipeRead io.ReadCloser
var subcommand *SubCommand
flag.Parse()
if len(flag.Args()) < 1 {
if terminal.IsTerminal(int(os.Stdin.Fd())) {
flag.Usage()
return
} else {
pipeRead = os.Stdin
}
} else {
subcommand = NewSubCommand(flag.Args())
pipeRead = subcommand.PipeRead
go subcommand.Run()
}
parser := NewParser(pipeRead)
title := strings.Join(flag.Args(), " ")
if len(title) == 0 {
title = fmt.Sprintf("%s:%s", *iface, *port)
}
gcvisGraph := NewGraph(title, GCVIS_TMPL)
server := NewHttpServer(*iface, *port, &gcvisGraph)
go parser.Run()
go server.Start()
url := server.Url()
if *openBrowser {
log.Printf("opening browser window, if this fails, navigate to %s", url)
browser.OpenURL(url)
} else {
log.Printf("server started on %s", url)
}
for {
select {
case gcTrace := <-parser.GcChan:
gcvisGraph.AddGCTraceGraphPoint(gcTrace)
case scvgTrace := <-parser.ScvgChan:
gcvisGraph.AddScavengerGraphPoint(scvgTrace)
case output := <-parser.NoMatchChan:
fmt.Fprintln(os.Stderr, output)
case <-parser.done:
if parser.Err != nil {
fmt.Fprintf(os.Stderr, parser.Err.Error())
os.Exit(1)
}
os.Exit(0)
}
}
if subcommand != nil && subcommand.Err() != nil {
fmt.Fprintf(os.Stderr, subcommand.Err().Error())
os.Exit(1)
}
os.Exit(0)
}