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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
// Copyright 2024 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.
package cover
import (
"bytes"
"context"
_ "embed"
"fmt"
"html/template"
"slices"
"sort"
"strings"
"github.com/google/syzkaller/pkg/coveragedb"
"github.com/google/syzkaller/pkg/coveragedb/spannerclient"
_ "github.com/google/syzkaller/pkg/subsystem/lists"
"golang.org/x/exp/maps"
)
type templateHeatmapRow struct {
Items []*templateHeatmapRow
Name string
Coverage []int64 // in percent
Covered []int64 // in lines count
IsDir bool
Depth int
Summary int64 // right column, may be negative to show drops
Tooltips []string
FileCoverageLink []string
builder map[string]*templateHeatmapRow
instrumented map[coveragedb.TimePeriod]int64
covered map[coveragedb.TimePeriod]int64
filePath string
}
type templateHeatmap struct {
Root *templateHeatmapRow
Periods []string
Subsystems []string
Managers []string
}
func (th *templateHeatmap) Filter(pred func(*templateHeatmapRow) bool) {
th.Root.filter(pred)
}
func (th *templateHeatmap) Transform(f func(*templateHeatmapRow)) {
th.Root.transform(f)
}
func (th *templateHeatmap) Sort(pred func(*templateHeatmapRow, *templateHeatmapRow) int) {
th.Root.sort(pred)
}
func (thm *templateHeatmapRow) transform(f func(*templateHeatmapRow)) {
for _, item := range thm.Items {
item.transform(f)
}
f(thm)
}
func (thm *templateHeatmapRow) filter(pred func(*templateHeatmapRow) bool) {
var filteredItems []*templateHeatmapRow
for _, item := range thm.Items {
item.filter(pred)
if pred(item) {
filteredItems = append(filteredItems, item)
}
}
thm.Items = filteredItems
}
func (thm *templateHeatmapRow) sort(pred func(*templateHeatmapRow, *templateHeatmapRow) int) {
for _, item := range thm.Items {
item.sort(pred)
}
slices.SortFunc(thm.Items, pred)
}
func (thm *templateHeatmapRow) addParts(depth int, pathLeft []string, filePath string, instrumented, covered int64,
timePeriod coveragedb.TimePeriod) {
thm.instrumented[timePeriod] += instrumented
thm.covered[timePeriod] += covered
if len(pathLeft) == 0 {
return
}
nextElement := pathLeft[0]
isDir := len(pathLeft) > 1
fp := ""
if !isDir {
fp = filePath
}
if _, ok := thm.builder[nextElement]; !ok {
thm.builder[nextElement] = &templateHeatmapRow{
Name: nextElement,
Depth: depth,
IsDir: isDir,
filePath: fp,
builder: make(map[string]*templateHeatmapRow),
instrumented: make(map[coveragedb.TimePeriod]int64),
covered: make(map[coveragedb.TimePeriod]int64),
}
}
thm.builder[nextElement].addParts(depth+1, pathLeft[1:], filePath, instrumented, covered, timePeriod)
}
func (thm *templateHeatmapRow) prepareDataFor(pageColumns []pageColumnTarget) {
for _, item := range thm.builder {
thm.Items = append(thm.Items, item)
}
sort.Slice(thm.Items, func(i, j int) bool {
if thm.Items[i].IsDir != thm.Items[j].IsDir {
return thm.Items[i].IsDir
}
return thm.Items[i].Name < thm.Items[j].Name
})
for _, pageColumn := range pageColumns {
var dateCoverage int64
tp := pageColumn.TimePeriod
if thm.instrumented[tp] != 0 {
dateCoverage = Percent(thm.covered[tp], thm.instrumented[tp])
}
thm.Coverage = append(thm.Coverage, dateCoverage)
thm.Covered = append(thm.Covered, thm.covered[tp])
thm.Tooltips = append(thm.Tooltips, fmt.Sprintf("Instrumented:\t%d blocks\nCovered:\t%d blocks",
thm.instrumented[tp], thm.covered[tp]))
if !thm.IsDir {
thm.FileCoverageLink = append(thm.FileCoverageLink,
fmt.Sprintf("/coverage/file?dateto=%s&period=%s&commit=%s&filepath=%s",
tp.DateTo.String(),
tp.Type,
pageColumn.Commit,
thm.filePath))
}
}
if len(pageColumns) > 0 {
lastDate := pageColumns[len(pageColumns)-1].TimePeriod
thm.Summary = thm.instrumented[lastDate]
}
for _, item := range thm.builder {
item.prepareDataFor(pageColumns)
}
}
func (thm *templateHeatmapRow) Visit(v func(string, int64, bool), path ...string) {
curPath := append(path, thm.Name)
v(strings.Join(curPath, "/"), thm.Summary, thm.IsDir)
for _, item := range thm.Items {
item.Visit(v, curPath...)
}
}
type pageColumnTarget struct {
TimePeriod coveragedb.TimePeriod
Commit string
}
func FilesCoverageToTemplateData(fCov []*coveragedb.FileCoverageWithDetails) *templateHeatmap {
res := templateHeatmap{
Root: &templateHeatmapRow{
IsDir: true,
builder: map[string]*templateHeatmapRow{},
instrumented: map[coveragedb.TimePeriod]int64{},
covered: map[coveragedb.TimePeriod]int64{},
},
}
columns := map[pageColumnTarget]struct{}{}
for _, fc := range fCov {
var pathLeft []string
if fc.Subsystem != "" {
pathLeft = append(pathLeft, fc.Subsystem)
}
res.Root.addParts(
0,
append(pathLeft, strings.Split(fc.Filepath, "/")...),
fc.Filepath,
fc.Instrumented,
fc.Covered,
fc.TimePeriod)
columns[pageColumnTarget{TimePeriod: fc.TimePeriod, Commit: fc.Commit}] = struct{}{}
}
targetDateAndCommits := maps.Keys(columns)
sort.Slice(targetDateAndCommits, func(i, j int) bool {
return targetDateAndCommits[i].TimePeriod.DateTo.Before(targetDateAndCommits[j].TimePeriod.DateTo)
})
for _, tdc := range targetDateAndCommits {
tp := tdc.TimePeriod
res.Periods = append(res.Periods, fmt.Sprintf("%s(%d)", tp.DateTo.String(), tp.Days))
}
res.Root.prepareDataFor(targetDateAndCommits)
return &res
}
type StyleBodyJS struct {
Style template.CSS
Body template.HTML
JS template.HTML
}
func stylesBodyJSTemplate(templData *templateHeatmap,
) (template.CSS, template.HTML, template.HTML, error) {
var styles, body, js bytes.Buffer
if err := heatmapTemplate.ExecuteTemplate(&styles, "style", templData); err != nil {
return "", "", "", fmt.Errorf("failed to get styles: %w", err)
}
if err := heatmapTemplate.ExecuteTemplate(&body, "body", templData); err != nil {
return "", "", "", fmt.Errorf("failed to get body: %w", err)
}
if err := heatmapTemplate.ExecuteTemplate(&js, "js", templData); err != nil {
return "", "", "", fmt.Errorf("failed to get js: %w", err)
}
return template.CSS(styles.String()),
template.HTML(body.String()),
template.HTML(js.Bytes()), nil
}
type Format struct {
FilterMinCoveredLinesDrop int
OrderByCoveredLinesDrop bool
DropCoveredLines0 bool
}
func DoHeatMapStyleBodyJS(
ctx context.Context, client spannerclient.SpannerClient, scope *coveragedb.SelectScope, onlyUnique bool,
sss, managers []string, dataFilters Format) (template.CSS, template.HTML, template.HTML, error) {
covAndDates, err := coveragedb.FilesCoverageWithDetails(ctx, client, scope, onlyUnique)
if err != nil {
return "", "", "", fmt.Errorf("failed to FilesCoverageWithDetails: %w", err)
}
templData := FilesCoverageToTemplateData(covAndDates)
templData.Subsystems = sss
templData.Managers = managers
FormatResult(templData, dataFilters)
return stylesBodyJSTemplate(templData)
}
func DoSubsystemsHeatMapStyleBodyJS(
ctx context.Context, client spannerclient.SpannerClient, scope *coveragedb.SelectScope, onlyUnique bool,
sss, managers []string, format Format) (template.CSS, template.HTML, template.HTML, error) {
covWithDetails, err := coveragedb.FilesCoverageWithDetails(ctx, client, scope, onlyUnique)
if err != nil {
panic(err)
}
var ssCovAndDates []*coveragedb.FileCoverageWithDetails
for _, cwd := range covWithDetails {
for _, ssName := range cwd.Subsystems {
newRecord := coveragedb.FileCoverageWithDetails{
Filepath: cwd.Filepath,
Subsystem: ssName,
Instrumented: cwd.Instrumented,
Covered: cwd.Covered,
TimePeriod: cwd.TimePeriod,
Commit: cwd.Commit,
}
ssCovAndDates = append(ssCovAndDates, &newRecord)
}
}
templData := FilesCoverageToTemplateData(ssCovAndDates)
templData.Managers = managers
FormatResult(templData, format)
return stylesBodyJSTemplate(templData)
}
func FormatResult(thm *templateHeatmap, format Format) {
// Remove file coverage lines with drop less than a threshold.
if format.FilterMinCoveredLinesDrop > 0 {
thm.Filter(func(row *templateHeatmapRow) bool {
return row.IsDir ||
slices.Max(row.Covered)-row.Covered[len(row.Covered)-1] >= int64(format.FilterMinCoveredLinesDrop)
})
}
// Remove file coverage lines with zero coverage during the analysis period.
if format.DropCoveredLines0 {
thm.Filter(func(row *templateHeatmapRow) bool {
return slices.Max(row.Covered) > 0
})
}
// Drop empty dir elements.
thm.Filter(func(row *templateHeatmapRow) bool {
return !row.IsDir || len(row.Items) > 0
})
// The files are sorted lexicographically by default.
if format.OrderByCoveredLinesDrop {
thm.Sort(func(row1 *templateHeatmapRow, row2 *templateHeatmapRow) int {
row1CoveredDrop := slices.Max(row1.Covered) - row1.Covered[len(row1.Covered)-1]
row2CoveredDrop := slices.Max(row2.Covered) - row2.Covered[len(row2.Covered)-1]
return int(row2CoveredDrop - row1CoveredDrop)
})
// We want to show the coverage drop numbers instead of total instrumented blocks.
thm.Transform(func(row *templateHeatmapRow) {
if !row.IsDir {
row.Summary = -1 * (slices.Max(row.Covered) - row.Covered[len(row.Covered)-1])
return
}
row.Summary = 0
for _, item := range row.Items {
if item.Summary < 0 { // only the items with coverage drop
row.Summary += item.Summary
}
}
})
}
}
func approximateInstrumented(points int64) string {
dim := " "
if abs(points) > 10000 {
dim = "K"
points /= 1000
}
return fmt.Sprintf("%d%s", points, dim)
}
func abs(a int64) int64 {
if a < 0 {
return -a
}
return a
}
//go:embed templates/heatmap.html
var templatesHeatmap string
var templateHeatmapFuncs = template.FuncMap{
"approxInstr": approximateInstrumented,
}
var heatmapTemplate = template.Must(template.New("").Funcs(templateHeatmapFuncs).Parse(templatesHeatmap))
|