Skip to content

Commit

Permalink
log counters for mempool and trackers for tx finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
zsystm committed Dec 16, 2024
1 parent fdf90d1 commit d502e08
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
47 changes: 47 additions & 0 deletions mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -55,6 +56,12 @@ type CListMempool struct {

logger log.Logger
metrics *Metrics

// counter
addedTx uint32
rejectedTx uint32
// File for logging counters
counterFile *os.File
}

var _ Mempool = &CListMempool{}
Expand Down Expand Up @@ -92,9 +99,47 @@ func NewCListMempool(
option(mp)
}

var err error
homeDir, err := os.UserHomeDir()
if err != nil {
mp.logger.Error("Failed to get home directory", "err", err)
}
mp.counterFile, err = os.OpenFile(homeDir+"/customlogs/comet_mempool-counters.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
mp.logger.Error("Failed to open counters.log", "err", err)
}

go mp.logCounters()
return mp
}

// logCounters logs the counter values every 1 second
func (mem *CListMempool) logCounters() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for {

Check failure on line 121 in mempool/clist_mempool.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1000: should use for range instead of for { select {} } (gosimple)
select {
case <-ticker.C:
// Atomically load and reset counters
addedTx := atomic.SwapUint32(&mem.addedTx, 0)
rejectedTx := atomic.SwapUint32(&mem.rejectedTx, 0)

// Get the current timestamp
timestamp := time.Now().UTC().UnixNano()

// Create a CSV line
logLine := fmt.Sprintf("%d,%d,%d\n",
timestamp, addedTx, rejectedTx)

// Append the line to the file
if _, err := mem.counterFile.WriteString(logLine); err != nil {
fmt.Printf("failed to write counter log: %v\n", err)
}
}
}
}

func (mem *CListMempool) getCElement(txKey types.TxKey) (*clist.CElement, bool) {
if e, ok := mem.txsMap.Load(txKey); ok {
return e.(*clist.CElement), true
Expand Down Expand Up @@ -443,6 +488,7 @@ func (mem *CListMempool) resCbFirstTime(
}
memTx.addSender(txInfo.SenderID)
mem.addTx(memTx)
atomic.AddUint32(&mem.addedTx, 1)
mem.logger.Debug(
"added good transaction",
"tx", types.Tx(tx).Hash(),
Expand All @@ -452,6 +498,7 @@ func (mem *CListMempool) resCbFirstTime(
)
mem.notifyTxsAvailable()
} else {
atomic.AddUint32(&mem.rejectedTx, 1)
// ignore bad transaction
mem.logger.Debug(
"rejected bad transaction",
Expand Down
15 changes: 15 additions & 0 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"time"

abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -43,6 +44,9 @@ type BlockExecutor struct {
logger log.Logger

metrics *Metrics

// timeTrackingLog is a file to log the time taken for each block processing
timeTrackingLog *os.File
}

type BlockExecutorOption func(executor *BlockExecutor)
Expand Down Expand Up @@ -79,6 +83,12 @@ func NewBlockExecutor(
option(res)
}

var err error
res.timeTrackingLog, err = os.OpenFile("/tmp/blockExec-timeTracking.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
res.logger.Error("failed to open time tracking log", "err", err)
os.Exit(1)
}
return res
}

Expand Down Expand Up @@ -319,6 +329,11 @@ func (blockExec *BlockExecutor) applyBlock(state State, blockID types.BlockID, b
// NOTE: if we crash between Commit and Save, events wont be fired during replay
fireEvents(blockExec.logger, blockExec.eventBus, block, blockID, abciResponse, validatorUpdates)

logLine := fmt.Sprintf("applyBlock(%d):: FinishedAt %d\n", block.Height, time.Now().UTC().UnixNano())
if _, err := blockExec.timeTrackingLog.WriteString(logLine); err != nil {
blockExec.logger.Error("failed to write to time tracking log", "err", err)
}

return state, nil
}

Expand Down

0 comments on commit d502e08

Please sign in to comment.