-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
93 lines (70 loc) · 1.73 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"time"
"gioui.org/app"
"gioui.org/unit"
"golang.org/x/sync/errgroup"
"loov.dev/allocview/internal/prof"
)
func init() {
runtime.LockOSThread()
}
func main() {
ctx := context.Background()
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, `Usage: %s [flags] subcommand...
This tool visualizes allocations of a Go program.
Only programs that have imported "loov.dev/allocview/attach" are supported at the moment.
When given a subcommand, it executes that subcommand and starts live-visualization
of the program. As an example:
allocview go run ./testdata
Flags:
`, os.Args[0])
flag.PrintDefaults()
}
var profcfg prof.Config
flag.StringVar(&profcfg.Cpu, "cpuprofile", "", "write cpu profile to `file`")
flag.StringVar(&profcfg.Mem, "memprofile", "", "write memory profile to `file`")
var config Config
flag.DurationVar(&config.SampleDuration, "sample-duration", time.Second, "sample duration")
flag.IntVar(&config.SampleCount, "sample-count", 1024, "sample count")
flag.Parse()
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(2)
}
defer profcfg.Run()()
// Setup command that we want to monitor.
args := flag.Args()
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var group errgroup.Group
server := NewServer()
err := server.Exec(ctx, &group, cmd)
if err != nil {
log.Fatal(err)
}
group.Go(func() error {
window := app.NewWindow(
app.Size(unit.Dp(800), unit.Dp(650)),
app.Title("AllocView"),
)
view := NewView(config, server)
return view.Run(window)
})
app.Main()
err = group.Wait()
if err != nil {
log.Println(err)
}
}