-
Notifications
You must be signed in to change notification settings - Fork 10
/
benchcmp.go
265 lines (247 loc) · 6.63 KB
/
benchcmp.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// syz-benchcmp visualizes syz-manager benchmarking results.
// First, run syz-manager with -bench=old flag.
// Then, do experimental modifications and run syz-manager again with -bench=new flag.
// Then, run syz-benchcmp old new.
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"github.com/google/syzkaller/pkg/osutil"
)
var (
flagSkip = flag.Int("skip", -30, "skip that many seconds after start (skip first 20% by default)")
)
type Graph struct {
Name string
Headers []string
Points []Point
}
type Point struct {
Time uint64
Vals []uint64
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Fprintf(os.Stderr, "usage: syz-benchcmp [flags] bench_file0 [bench_file1 [bench_file2]]...\n")
flag.PrintDefaults()
os.Exit(1)
}
graphs := []*Graph{
{Name: "cover"},
{Name: "corpus"},
{Name: "exec total"},
{Name: "crash types"},
}
for i, fname := range flag.Args() {
data := readFile(fname)
addExecSpeed(data)
for _, g := range graphs {
g.Headers = append(g.Headers, filepath.Base(fname))
for _, v := range data {
pt := Point{
Time: v["fuzzing"],
Vals: make([]uint64, len(flag.Args())),
}
pt.Vals[i] = v[g.Name]
g.Points = append(g.Points, pt)
}
}
}
for _, g := range graphs {
if len(g.Points) == 0 {
failf("no data points")
}
sort.Sort(pointSlice(g.Points))
skipStart(g)
restoreMissingPoints(g)
}
printFinalStats(graphs)
display(graphs)
}
func readFile(fname string) (data []map[string]uint64) {
f, err := os.Open(fname)
if err != nil {
failf("failed to open input file: %v", err)
}
defer f.Close()
dec := json.NewDecoder(bufio.NewReader(f))
for dec.More() {
v := make(map[string]uint64)
if err := dec.Decode(&v); err != nil {
failf("failed to decode input file %v: %v", fname, err)
}
data = append(data, v)
}
return
}
func addExecSpeed(data []map[string]uint64) {
// Speed between consecutive samples is very unstable.
const (
window = 100
step = 10
)
for i := window; i < len(data); i += step {
cur := data[i]
prev := data[i-window]
dx := cur["exec total"] - prev["exec total"]
dt := cur["fuzzing"] - prev["fuzzing"]
cur["exec speed"] = dx * 1000 / dt
}
}
func skipStart(g *Graph) {
skipTime := uint64(*flagSkip)
if *flagSkip < 0 {
// Negative skip means percents.
max := g.Points[len(g.Points)-1].Time
skipTime = max * -skipTime / 100
}
if skipTime > 0 {
skip := sort.Search(len(g.Points), func(i int) bool {
return g.Points[i].Time > skipTime
})
g.Points = g.Points[skip:]
}
}
func restoreMissingPoints(g *Graph) {
for i := range g.Headers {
// Find previous and next non-zero point for each zero point,
// and restore its value with linear inerpolation.
type Pt struct {
Time uint64
Val uint64
}
var prev Pt
prevs := make(map[uint64]Pt)
for _, pt := range g.Points {
if pt.Vals[i] != 0 {
prev = Pt{pt.Time, pt.Vals[i]}
continue
}
prevs[pt.Time] = prev
}
var next Pt
for pti := len(g.Points) - 1; pti >= 0; pti-- {
pt := g.Points[pti]
if pt.Vals[i] != 0 {
next = Pt{pt.Time, pt.Vals[i]}
continue
}
prev := prevs[pt.Time]
if prev.Val == 0 || next.Val == 0 {
continue
}
pt.Vals[i] = prev.Val
if next.Time != prev.Time {
// Use signed calculations as corpus can go backwards.
pt.Vals[i] += uint64(int64(next.Val-prev.Val) * int64(pt.Time-prev.Time) / int64(next.Time-prev.Time))
}
}
}
}
func printFinalStats(graphs []*Graph) {
for i := 1; i < len(graphs[0].Headers); i++ {
fmt.Printf("%-12v%16v%16v%16v\n", "", graphs[0].Headers[0], graphs[0].Headers[i], "diff")
for _, g := range graphs {
lastNonZero := func(x int) uint64 {
for j := len(g.Points) - 1; j >= 0; j-- {
if v := g.Points[j].Vals[x]; v != 0 {
return v
}
}
return 0
}
old := lastNonZero(0)
new := lastNonZero(i)
fmt.Printf("%-12v%16v%16v%+16d\n", g.Name, old, new, int64(new-old))
}
fmt.Printf("\n")
}
}
func display(graphs []*Graph) {
outf, err := ioutil.TempFile("", "")
if err != nil {
failf("failed to create temp file: %v", err)
}
if err := htmlTemplate.Execute(outf, graphs); err != nil {
failf("failed to execute template: %v", err)
}
outf.Close()
name := outf.Name() + ".html"
if err := osutil.Rename(outf.Name(), name); err != nil {
failf("failed to rename file: %v", err)
}
if err := exec.Command("xdg-open", name).Start(); err != nil {
failf("failed to start browser: %v", err)
}
}
type pointSlice []Point
func (a pointSlice) Len() int { return len(a) }
func (a pointSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a pointSlice) Less(i, j int) bool { return a[i].Time < a[j].Time }
func failf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
var htmlTemplate = template.Must(
template.New("").Parse(`
<!doctype html>
<html>
<head>
<title>Syzkaller Bench</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawCharts);
function drawCharts() {
{{range $id, $graph := .}}
{
var data = new google.visualization.DataTable();
data.addColumn({type: 'number'});
{{range $graph.Headers}}
data.addColumn({type: 'number', label: '{{.}}'});
{{end}}
data.addRows([
{{range $graph.Points}} [ {{.Time}}, {{range .Vals}} {{if .}} {{.}} {{end}}, {{end}}
],
{{end}}
]);
new google.visualization.LineChart(document.getElementById('graph_div_{{$id}}')).
draw(data, {
title: '{{$graph.Name}}',
width: "100%",
height: document.documentElement.clientHeight * 0.48,
legend: {position: "in"},
focusTarget: "category",
hAxis: {title: "Time, sec"},
chartArea: {left: "5%", top: "5%", width: "90%", height:"85%"}
})
}
{{end}}
}
</script>
</head>
<body>
<table style="width: 100%; height: 98vh">
<tr>
<td style="width: 50%"> <div id="graph_div_0"></div> </td>
<td style="width: 50%"> <div id="graph_div_2"></div> </td>
</tr>
<tr>
<td style="width: 50%"> <div id="graph_div_1"></div> </td>
<td style="width: 50%"> <div id="graph_div_3"></div> </td>
</tr>
</table>
</body>
</html>
`))