-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (86 loc) · 1.94 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
96
97
98
package main
import (
"github.com/UoB-CSA/go-trace-json/trace"
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"sync"
)
type gtype struct {
ID uint64 // Unique identifier (PC).
Name string // Start function.
N int // Total number of goroutines in this group.
ExecTime int64 // Total execution time of all goroutines in this group.
}
type jsonOutput struct {
Goroutines []gtype `json:"goroutines"`
}
func main() {
inputFile := flag.String("input-file", "", "The input file. Required")
outputFile := flag.String("output-file", "trace.json", "The output file. (default: traces.json)")
flag.Parse()
if *inputFile == "" {
flag.PrintDefaults()
os.Exit(1)
}
parsedTraces, err := parseTrace(*inputFile)
if err != nil {
os.Exit(1)
}
events := parsedTraces.Events
// Get goroutines
gs := trace.GoroutineStats(events)
gss := make(map[uint64]gtype)
for _, g := range gs {
gs1 := gss[g.PC]
gs1.ID = g.PC
gs1.Name = g.Name
gs1.N++
gs1.ExecTime += g.ExecTime
gss[g.PC] = gs1
}
var glist []gtype
for k, v := range gss {
v.ID = k
glist = append(glist, v)
}
jsonData := &jsonOutput{
Goroutines: glist,
}
jsonString, err := json.Marshal(jsonData)
if err != nil {
fmt.Errorf("Failed to parse to JSON")
os.Exit(1)
}
err = ioutil.WriteFile(*outputFile, jsonString, 0644)
if err != nil {
fmt.Errorf("Failed to write to output file")
os.Exit(1)
}
}
var loader struct {
once sync.Once
res trace.ParseResult
err error
}
func parseTrace(traceFile string) (trace.ParseResult, error) {
loader.once.Do(func() {
tracef, err := os.Open(traceFile)
if err != nil {
loader.err = fmt.Errorf("failed to open trace file: %v", err)
return
}
defer tracef.Close()
// Parse and symbolize.
res, err := trace.Parse(bufio.NewReader(tracef), "")
if err != nil {
loader.err = fmt.Errorf("failed to parse trace: %v", err)
return
}
loader.res = res
})
return loader.res, loader.err
}