Skip to content

Commit

Permalink
Add stat logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mininny committed Sep 23, 2024
1 parent 89460e6 commit 7d9b5fa
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 36 deletions.
2 changes: 2 additions & 0 deletions rvgo/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ func Run(ctx *cli.Context) error {
if err := jsonutil.WriteJSON(ctx.Path(cannon.RunOutputFlag.Name), state, OutFilePerm); err != nil {
return fmt.Errorf("failed to write state output: %w", err)
}

state.Close()
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions rvgo/fast/instrumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fast
import (
"encoding/binary"
"fmt"
"github.com/google/uuid"

Check failure on line 6 in rvgo/fast/instrumented.go

View workflow job for this annotation

GitHub Actions / rvgo-lint

File is not `goimports`-ed (goimports)
"io"
)

Expand Down Expand Up @@ -42,6 +43,13 @@ func NewInstrumentedState(state *VMState, po PreimageOracle, stdOut, stdErr io.W
}
}

func (m *InstrumentedState) Close() {
// When done, close the Memory instance
id := uuid.New()
m.state.Memory.statsFile = id.String()
m.state.Close()
}

func (m *InstrumentedState) Step(proof bool) (wit *StepWitness, err error) {
m.memProofEnabled = proof
m.memAccess = m.memAccess[:0]
Expand Down
27 changes: 27 additions & 0 deletions rvgo/fast/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
_ "net/http/pprof"
"sort"

"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -45,6 +48,9 @@ type Memory struct {
radix *L1
branchFactors [10]uint64

stats *Stats // Reference to your Stats struct
statsFile string // Filename for the CSV output

// Note: since we don't de-alloc pages, we don't do ref-counting.
// Once a page exists, it doesn't leave memory

Expand All @@ -56,14 +62,34 @@ type Memory struct {

func NewMemory() *Memory {
node := &L1{}
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
return &Memory{
radix: node,
stats: NewStats(),
pages: make(map[uint64]*CachedPage),
branchFactors: [10]uint64{4, 4, 4, 4, 4, 4, 4, 8, 8, 8},
lastPageKeys: [2]uint64{^uint64(0), ^uint64(0)}, // default to invalid keys, to not match any pages
}
}

func (m *Memory) Close() error {
// Perform any necessary cleanup here

// Write the statistics to the CSV file
if m.stats != nil && m.statsFile != "" {
err := m.stats.WriteToCSV(m.statsFile)
if err != nil {
return fmt.Errorf("failed to write stats to CSV: %w", err)
}
}

// If you have any other resources to clean up, do it here

return nil
}

func (m *Memory) PageCount() int {
return len(m.pages)
}
Expand Down Expand Up @@ -202,6 +228,7 @@ func (m *Memory) UnmarshalJSON(data []byte) error {

m.branchFactors = [10]uint64{4, 4, 4, 4, 4, 4, 4, 8, 8, 8}
m.radix = &L1{}
m.stats = NewStats()
m.pages = make(map[uint64]*CachedPage)
m.lastPageKeys = [2]uint64{^uint64(0), ^uint64(0)}
m.lastPage = [2]*CachedPage{nil, nil}
Expand Down
15 changes: 15 additions & 0 deletions rvgo/fast/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func BenchmarkMemoryOperations(b *testing.B) {
{"MerkleProofGeneration_Large", benchMerkleProofGeneration(largeDataset)},
{"MerkleRootCalculation_Small", benchMerkleRootCalculation(smallDataset)},
{"MerkleRootCalculation_Large", benchMerkleRootCalculation(largeDataset)},
{"CacheInvalidation", benchCacheInvalidation},
}

for _, bm := range benchmarks {
Expand Down Expand Up @@ -522,3 +523,17 @@ func benchMerkleRootCalculation(size int) func(b *testing.B, m *Memory) {
}
}
}

func benchCacheInvalidation(b *testing.B, m *Memory) {
data := make([]byte, 8)
addresses := make([]uint64, b.N)
for i := range addresses {
addresses[i] = mathrand.Uint64()
m.SetUnaligned(addresses[i], data)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Invalidate(addresses[i%len(addresses)])
}
}
Loading

0 comments on commit 7d9b5fa

Please sign in to comment.