diff --git a/erigon-lib/diagnostics/block_execution.go b/erigon-lib/diagnostics/block_execution.go index eb1c3dd4949..54cf9de70d8 100644 --- a/erigon-lib/diagnostics/block_execution.go +++ b/erigon-lib/diagnostics/block_execution.go @@ -18,10 +18,45 @@ package diagnostics import ( "context" + "encoding/json" + "io" + "sync" "github.com/ledgerwatch/erigon-lib/log/v3" ) +type BlockEexcStatsData struct { + data BlockExecutionStatistics + mu sync.Mutex +} + +type BlockExecutionStatistics struct { + From uint64 `json:"from"` + To uint64 `json:"to"` + BlockNumber uint64 `json:"blockNumber"` + BlkPerSec float64 `json:"blkPerSec"` + TxPerSec float64 `json:"txPerSec"` + MgasPerSec float64 `json:"mgasPerSec"` + GasState float64 `json:"gasState"` + Batch uint64 `json:"batch"` + Alloc uint64 `json:"alloc"` + Sys uint64 `json:"sys"` + TimeElapsed float64 `json:"timeElapsed"` +} + +func (b *BlockEexcStatsData) SetData(d BlockExecutionStatistics) { + b.mu.Lock() + defer b.mu.Unlock() + b.data = d +} + +func (b *BlockEexcStatsData) Data() (d BlockExecutionStatistics) { + b.mu.Lock() + d = b.data + b.mu.Unlock() + return +} + func (d *DiagnosticClient) setupBlockExecutionDiagnostics(rootCtx context.Context) { d.runBlockExecutionListener(rootCtx) } @@ -37,10 +72,7 @@ func (d *DiagnosticClient) runBlockExecutionListener(rootCtx context.Context) { case <-rootCtx.Done(): return case info := <-ch: - d.mu.Lock() - d.syncStats.BlockExecution = info - d.mu.Unlock() - + d.BlockExecution.SetData(info) if d.syncStats.SyncFinished { return } @@ -48,3 +80,9 @@ func (d *DiagnosticClient) runBlockExecutionListener(rootCtx context.Context) { } }() } + +func (d *DiagnosticClient) BlockExecutionInfoJson(w io.Writer) { + if err := json.NewEncoder(w).Encode(d.BlockExecution.Data()); err != nil { + log.Debug("[diagnostics] BlockExecutionInfoJson", "err", err) + } +} diff --git a/erigon-lib/diagnostics/client.go b/erigon-lib/diagnostics/client.go index 0f653b589a1..983a63459ee 100644 --- a/erigon-lib/diagnostics/client.go +++ b/erigon-lib/diagnostics/client.go @@ -41,6 +41,7 @@ type DiagnosticClient struct { syncStages []SyncStage syncStats SyncStatistics + BlockExecution BlockEexcStatsData snapshotFileList SnapshoFilesList mu sync.Mutex headerMutex sync.Mutex diff --git a/erigon-lib/diagnostics/entities.go b/erigon-lib/diagnostics/entities.go index ea83615c163..08bbeca5f20 100644 --- a/erigon-lib/diagnostics/entities.go +++ b/erigon-lib/diagnostics/entities.go @@ -91,7 +91,6 @@ type SyncStatistics struct { SnapshotDownload SnapshotDownloadStatistics `json:"snapshotDownload"` SnapshotIndexing SnapshotIndexingStatistics `json:"snapshotIndexing"` SnapshotFillDB SnapshotFillDBStatistics `json:"snapshotFillDB"` - BlockExecution BlockExecutionStatistics `json:"blockExecution"` SyncFinished bool `json:"syncFinished"` } @@ -167,19 +166,6 @@ type SnapshotFillDBStageUpdate struct { Stage SnapshotFillDBStage `json:"stage"` TimeElapsed float64 `json:"timeElapsed"` } -type BlockExecutionStatistics struct { - From uint64 `json:"from"` - To uint64 `json:"to"` - BlockNumber uint64 `json:"blockNumber"` - BlkPerSec float64 `json:"blkPerSec"` - TxPerSec float64 `json:"txPerSec"` - MgasPerSec float64 `json:"mgasPerSec"` - GasState float64 `json:"gasState"` - Batch uint64 `json:"batch"` - Alloc uint64 `json:"alloc"` - Sys uint64 `json:"sys"` - TimeElapsed float64 `json:"timeElapsed"` -} type SnapshoFilesList struct { Files []string `json:"files"`