Skip to content

Commit

Permalink
Log memory usage info
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Sep 29, 2020
1 parent f330eed commit 820198a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/check_construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func runCheckConstructionCmd(cmd *cobra.Command, args []string) {
return constructionTester.WatchEndConditions(ctx)
})

g.Go(func() error {
return tester.LogMemoryLoop(ctx)
})

sigListeners := []context.CancelFunc{cancel}
go handleSignals(&sigListeners)

Expand Down
4 changes: 4 additions & 0 deletions cmd/check_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func runCheckDataCmd(cmd *cobra.Command, args []string) {
return dataTester.WatchEndConditions(ctx)
})

g.Go(func() error {
return tester.LogMemoryLoop(ctx)
})

sigListeners := []context.CancelFunc{cancel}
go handleSignals(&sigListeners)

Expand Down
14 changes: 14 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ func (l *Logger) LogConstructionStats(ctx context.Context, inflightTransactions
return nil
}

// LogMemoryStats logs memory usage information.
func LogMemoryStats(ctx context.Context) {
memUsage := utils.MonitorMemoryUsage(ctx, -1)
statsMessage := fmt.Sprintf(
"[MEMORY] Heap: %f MB Stack: %f MB System: %f MB GCs: %d",
memUsage.Heap,
memUsage.Stack,
memUsage.System,
memUsage.GarbageCollections,
)

color.Cyan(statsMessage)
}

// AddBlockStream writes the next processed block to the end of the
// blockStreamFile output file.
func (l *Logger) AddBlockStream(
Expand Down
32 changes: 32 additions & 0 deletions pkg/tester/general.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tester

import (
"context"
"time"

"github.com/coinbase/rosetta-cli/pkg/logger"
)

const (
// MemoryLoggingFrequency is the frequency that memory
// usage stats are logged to the terminal.
MemoryLoggingFrequency = 10 * time.Second
)

// LogMemoryLoop runs a loop that logs memory usage.
func LogMemoryLoop(
ctx context.Context,
) error {
timer := time.NewTimer(MemoryLoggingFrequency)
defer timer.Stop()

for {
select {
case <-ctx.Done():
logger.LogMemoryStats(ctx)
return ctx.Err()
case <-timer.C:
logger.LogMemoryStats(ctx)
}
}
}

0 comments on commit 820198a

Please sign in to comment.