Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dashboard/app: export coverage json #5726

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions dashboard/app/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,20 @@ func GetCoverageDBClient(ctx context.Context) spannerclient.SpannerClient {

type funcStyleBodyJS func(
ctx context.Context, client spannerclient.SpannerClient,
scope *cover.SelectScope, onlyUnique bool, sss, managers []string,
scope *coveragedb.SelectScope, onlyUnique bool, sss, managers []string,
) (template.CSS, template.HTML, template.HTML, error)

func handleCoverageHeatmap(c context.Context, w http.ResponseWriter, r *http.Request) error {
if r.FormValue("jsonl") == "1" {
hdr, err := commonHeader(c, r, w, "")
if err != nil {
return err
}
ns := hdr.Namespace
repo, _ := getNsConfig(c, ns).mainRepoBranch()
w.Header().Set("Content-Type", "application/json")
return writeExtAPICoverageFor(c, w, ns, repo)
}
return handleHeatmap(c, w, r, cover.DoHeatMapStyleBodyJS)
}

Expand Down Expand Up @@ -112,7 +122,7 @@ func handleHeatmap(c context.Context, w http.ResponseWriter, r *http.Request, f
var style template.CSS
var body, js template.HTML
if style, body, js, err = f(c, GetCoverageDBClient(c),
&cover.SelectScope{
&coveragedb.SelectScope{
Ns: hdr.Namespace,
Subsystem: ss,
Manager: manager,
Expand Down Expand Up @@ -180,7 +190,7 @@ func handleFileCoverage(c context.Context, w http.ResponseWriter, r *http.Reques
}
hitLines, hitCounts, err := coveragedb.ReadLinesHitCount(
c, client, hdr.Namespace, targetCommit, kernelFilePath, manager, tp)
covMap := cover.MakeCovMap(hitLines, hitCounts)
covMap := coveragedb.MakeCovMap(hitLines, hitCounts)
if err != nil {
return fmt.Errorf("coveragedb.ReadLinesHitCount(%s): %w", manager, err)
}
Expand All @@ -192,7 +202,7 @@ func handleFileCoverage(c context.Context, w http.ResponseWriter, r *http.Reques
if err != nil {
return fmt.Errorf("coveragedb.ReadLinesHitCount(*): %w", err)
}
covMap = cover.UniqCoverage(cover.MakeCovMap(allHitLines, allHitCounts), covMap)
covMap = coveragedb.UniqCoverage(coveragedb.MakeCovMap(allHitLines, allHitCounts), covMap)
}

webGit := getWebGit(c) // Get mock if available.
Expand Down
120 changes: 120 additions & 0 deletions dashboard/app/public_json_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
package main

import (
"context"
"encoding/json"
"fmt"
"io"
"time"

"cloud.google.com/go/civil"
"github.com/google/syzkaller/dashboard/api"
"github.com/google/syzkaller/pkg/coveragedb"
)

func getExtAPIDescrForBugPage(bugPage *uiBugPage) *api.Bug {
Expand Down Expand Up @@ -170,3 +176,117 @@ func GetJSONDescrFor(page interface{}) ([]byte, error) {
}
return json.MarshalIndent(res, "", "\t")
}

type coveredBlock struct {
FromLine int `json:"from_line"`
FromCol int `json:"from_column"`
ToLine int `json:"to_line"`
ToCol int `json:"to_column"`
}

type functionCoverage struct {
FuncName string `json:"func_name"`
Instrumented int `json:"total_blocks"`
Blocks []*coveredBlock `json:"covered_blocks"`
}

type fileCoverage struct {
Repo string `json:"repo"`
Commit string `json:"commit"`
FilePath string `json:"file_path"`
Functions []*functionCoverage `json:"functions"`
}

func writeExtAPICoverageFor(ctx context.Context, w io.Writer, ns, repo string) error {
// By default, return the previous month coverage. It guarantees the good numbers.
//
// The alternative is to return the current month.
// The numbers will jump every day, on the 1st date may drop down.
tps, err := coveragedb.GenNPeriodsTill(1, civil.DateOf(time.Now()).AddDays(-31), "month")
if err != nil {
return fmt.Errorf("coveragedb.GenNPeriodsTill: %w", err)
}
defaultTimePeriod := tps[0]
covDBClient := GetCoverageDBClient(ctx)
ff, err := coveragedb.MakeFuncFinder(ctx, covDBClient, ns, defaultTimePeriod)
if err != nil {
return fmt.Errorf("coveragedb.MakeFuncFinder: %w", err)
}
covCh, errCh := coveragedb.FilesCoverageStream(ctx, covDBClient, ns, defaultTimePeriod)
if err := writeFileCoverage(ctx, w, repo, ff, covCh); err != nil {
return fmt.Errorf("populateFileCoverage: %w", err)
}
if err := <-errCh; err != nil {
return fmt.Errorf("coveragedb.FilesCoverageStream: %w", err)
}
return nil
}

func writeFileCoverage(ctx context.Context, w io.Writer, repo string, ff *coveragedb.FunctionFinder,
covCh <-chan *coveragedb.FileCoverageWithLineInfo) error {
enc := json.NewEncoder(w)
enc.SetIndent("\t\t", "\t")
for {
select {
case fileCov := <-covCh:
if fileCov == nil {
return nil
}
funcsCov, err := genFuncsCov(fileCov, ff)
if err != nil {
return fmt.Errorf("genFuncsCov: %w", err)
}
if err := enc.Encode(&fileCoverage{
Repo: repo,
Commit: fileCov.Commit,
FilePath: fileCov.Filepath,
Functions: funcsCov,
}); err != nil {
return fmt.Errorf("enc.Encode: %w", err)
}
case <-ctx.Done():
return nil
}
}
}

func genFuncsCov(fc *coveragedb.FileCoverageWithLineInfo, ff *coveragedb.FunctionFinder,
) ([]*functionCoverage, error) {
nameToLines := map[string][]int{}
funcInstrumented := map[string]int{}
for i, hitCount := range fc.HitCounts {
lineNum := int(fc.LinesInstrumented[i])
funcName, err := ff.FileLineToFuncName(fc.Filepath, lineNum)
if err != nil {
return nil, fmt.Errorf("ff.FileLineToFuncName: %w", err)
}
funcInstrumented[funcName]++
if hitCount == 0 {
continue
}
nameToLines[funcName] = append(nameToLines[funcName], lineNum)
}

var res []*functionCoverage
for funcName, lines := range nameToLines {
res = append(res, &functionCoverage{
FuncName: funcName,
Instrumented: funcInstrumented[funcName],
Blocks: linesToBlocks(lines),
})
}
return res, nil
}

func linesToBlocks(lines []int) []*coveredBlock {
var res []*coveredBlock
for _, lineNum := range lines {
res = append(res, &coveredBlock{
FromLine: lineNum,
FromCol: 0,
ToLine: lineNum,
ToCol: -1,
})
}
return res
}
Loading