From 1be1a06281dccada078a2a51e8b483811af8f596 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 29 Mar 2024 15:02:10 +0100 Subject: all: refactor stats Add ability for each package to create and export own stats. Each stat is self-contained, describes how it should be presented, and there is not need to copy them from one package to another. Stats also keep historical data and allow building graphs over time. --- pkg/corpus/corpus.go | 30 ++++++++++++------------------ pkg/corpus/corpus_test.go | 11 ++++------- 2 files changed, 16 insertions(+), 25 deletions(-) (limited to 'pkg/corpus') diff --git a/pkg/corpus/corpus.go b/pkg/corpus/corpus.go index 19dfee483..46e388366 100644 --- a/pkg/corpus/corpus.go +++ b/pkg/corpus/corpus.go @@ -10,6 +10,7 @@ import ( "github.com/google/syzkaller/pkg/cover" "github.com/google/syzkaller/pkg/hash" "github.com/google/syzkaller/pkg/signal" + "github.com/google/syzkaller/pkg/stats" "github.com/google/syzkaller/prog" ) @@ -23,6 +24,9 @@ type Corpus struct { cover cover.Cover // total coverage of all items updates chan<- NewItemEvent *ProgramsList + StatProgs *stats.Val + StatSignal *stats.Val + StatCover *stats.Val } func NewCorpus(ctx context.Context) *Corpus { @@ -30,12 +34,19 @@ func NewCorpus(ctx context.Context) *Corpus { } func NewMonitoredCorpus(ctx context.Context, updates chan<- NewItemEvent) *Corpus { - return &Corpus{ + corpus := &Corpus{ ctx: ctx, progs: make(map[string]*Item), updates: updates, ProgramsList: &ProgramsList{}, } + corpus.StatProgs = stats.Create("corpus", "Number of test programs in the corpus", stats.Console, + stats.Link("/corpus"), stats.Graph("corpus"), stats.LenOf(&corpus.progs, &corpus.mu)) + corpus.StatSignal = stats.Create("signal", "Fuzzing signal in the corpus", + stats.LenOf(&corpus.signal, &corpus.mu)) + corpus.StatCover = stats.Create("coverage", "Source coverage in the corpus", stats.Console, + stats.Link("/cover"), stats.Prometheus("syz_corpus_cover"), stats.LenOf(&corpus.cover, &corpus.mu)) + return corpus } // It may happen that a single program is relevant because of several @@ -161,23 +172,6 @@ func (corpus *Corpus) Item(sig string) *Item { return corpus.progs[sig] } -// Stats is a snapshot of the relevant current state figures. -type Stats struct { - Progs int - Signal int - Cover int -} - -func (corpus *Corpus) Stats() Stats { - corpus.mu.RLock() - defer corpus.mu.RUnlock() - return Stats{ - Progs: len(corpus.progs), - Signal: len(corpus.signal), - Cover: len(corpus.cover), - } -} - type CallCov struct { Count int Cover cover.Cover diff --git a/pkg/corpus/corpus_test.go b/pkg/corpus/corpus_test.go index 0b5a85505..62aad1e04 100644 --- a/pkg/corpus/corpus_test.go +++ b/pkg/corpus/corpus_test.go @@ -48,12 +48,9 @@ func TestCorpusOperation(t *testing.T) { } // Verify the total signal. - stat := corpus.Stats() - assert.Equal(t, Stats{ - Signal: 5, - Cover: 0, - Progs: 2, - }, stat) + assert.Equal(t, corpus.StatSignal.Val(), 5) + assert.Equal(t, corpus.StatCover.Val(), 0) + assert.Equal(t, corpus.StatProgs.Val(), 2) corpus.Minimize(true) } @@ -77,7 +74,7 @@ func TestCorpusCoverage(t *testing.T) { assert.Equal(t, []uint32{12}, event.NewCover) // Check the total corpus size. - assert.Equal(t, corpus.Stats().Cover, 3) + assert.Equal(t, corpus.StatCover.Val(), 3) } func TestCorpusSaveConcurrency(t *testing.T) { -- cgit mrf-deployment