Skip to content

Commit

Permalink
turn on profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhoonbang committed Nov 2, 2024
1 parent 57ad825 commit 788be92
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
13 changes: 13 additions & 0 deletions core/capabilities/compute/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compute

import (
"log"
"sync"
"time"

Expand Down Expand Up @@ -43,6 +44,9 @@ type moduleCache struct {
}

func newModuleCache(clock clockwork.Clock, tick, timeout time.Duration, evictAfterSize int) *moduleCache {
log.Println("evictAfterSize", evictAfterSize)
log.Println("timeout in seconds", timeout.Seconds())
log.Println("mc.tickInterval", tick)
return &moduleCache{
m: map[string]*module{},
tickInterval: tick,
Expand All @@ -67,15 +71,19 @@ func (mc *moduleCache) close() {
}

func (mc *moduleCache) reapLoop() {
log.Println("reapLoop started")
ticker := mc.clock.NewTicker(mc.tickInterval)
for {
select {
case <-ticker.Chan():
log.Println("before evictOlderThan")
mc.evictOlderThan(mc.timeout)
log.Println("after evictOlderThan")
if mc.onReaper != nil {
mc.onReaper <- struct{}{}
}
case <-mc.stopChan:
log.Println("stopChan")
return
}
}
Expand Down Expand Up @@ -109,12 +117,17 @@ func (mc *moduleCache) evictOlderThan(duration time.Duration) {

evicted := 0

log.Println("len(mc.m)", len(mc.m))
if len(mc.m) > mc.evictAfterSize {
for id, m := range mc.m {
log.Println("mc.clock", mc.clock.Now())
log.Println("m.lastFetchedAt", m.lastFetchedAt)
log.Println("duration", duration)
if mc.clock.Now().Sub(m.lastFetchedAt) > duration {
delete(mc.m, id)
m.module.Close()
evicted++
log.Println("evicted. id:", id)
}

if len(mc.m) <= mc.evictAfterSize {
Expand Down
3 changes: 3 additions & 0 deletions core/capabilities/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"log"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -135,6 +136,8 @@ func (c *Compute) initModule(id string, cfg *host.ModuleConfig, binary []byte, w
func (c *Compute) executeWithModule(module *host.Module, config []byte, req capabilities.CapabilityRequest) (capabilities.CapabilityResponse, error) {
executeStart := time.Now()
capReq := capabilitiespb.CapabilityRequestToProto(req)
log.Println("executeWithModule req", req)
log.Println("executeWithModule capReq", capReq)

wasmReq := &wasmpb.Request{
Id: uuid.New().String(),
Expand Down
43 changes: 43 additions & 0 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package core
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"

Check failure on line 7 in core/main.go

View workflow job for this annotation

GitHub Actions / lint

G108: Profiling endpoint is automatically exposed on /debug/pprof (gosec)
"os"
"runtime"
"runtime/debug"
"time"

"github.com/Masterminds/semver/v3"

Expand All @@ -25,7 +30,45 @@ func init() {
}
}

// Helper function to convert bytes to megabytes
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

func printGCStats() {
for {
// Create MemStats and GCStats structs to hold stats data
var memStats runtime.MemStats
var gcStats debug.GCStats

// Read memory statistics
runtime.ReadMemStats(&memStats)
debug.ReadGCStats(&gcStats)

// Print memory allocation and GC details
log.Printf("Alloc = %v MiB", bToMb(memStats.Alloc))
log.Printf("TotalAlloc = %v MiB", bToMb(memStats.TotalAlloc))
log.Printf("Sys = %v MiB", bToMb(memStats.Sys))
log.Printf("NumGC = %v", memStats.NumGC)
log.Printf("PauseTotalNs = %v", memStats.PauseTotalNs)
log.Printf("LastGC = %v", time.Unix(0, int64(memStats.LastGC)))

Check failure on line 54 in core/main.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int64 (gosec)

log.Printf("GC Last Run: %v", gcStats.LastGC)
log.Printf("GC NumGC: %v", gcStats.NumGC)
log.Printf("GC PauseTotal: %v", gcStats.PauseTotal)
log.Printf("GC Pause history (last few GCs): %v", gcStats.Pause)

// Sleep for the specified interval
time.Sleep(5 * time.Minute) // Adjust interval as needed
}
}

func Main() (code int) {
go printGCStats() // Run printGCStats in a separate goroutine
go func() {
log.Println("Starting pprof server on :6060")
log.Println(http.ListenAndServe(":6060", nil))

Check failure on line 70 in core/main.go

View workflow job for this annotation

GitHub Actions / lint

G114: Use of net/http serve function that has no support for setting timeouts (gosec)
}()
recovery.ReportPanics(func() {
app := cmd.NewApp(newProductionClient())
if err := app.Run(os.Args); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ require (
github.com/smartcontractkit/chain-selectors v1.0.27
github.com/smartcontractkit/chainlink-automation v0.8.0
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241014104242-9227e5c976a7
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241016235738-9ba7522206c3
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241101202308-e11c3552f2b5
github.com/smartcontractkit/chainlink-cosmos v0.5.1
github.com/smartcontractkit/chainlink-data-streams v0.1.0
github.com/smartcontractkit/chainlink-feeds v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1057,8 +1057,8 @@ github.com/smartcontractkit/chainlink-automation v0.8.0 h1:hFz2EHU06bkEfhcqhK8Jd
github.com/smartcontractkit/chainlink-automation v0.8.0/go.mod h1:ObdjDfgGIaiE48Bb3yYcx1CeGBm392WlEw92U83LlUA=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241014104242-9227e5c976a7 h1:aMG3BllvgeL/vsqkebuAhWoIWOnitKnN1VxibdzGnYo=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241014104242-9227e5c976a7/go.mod h1:H4BTXnZBhwRdsAFjqWZpB1/f3IZnuB/Ql7pXPmokzXg=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241016235738-9ba7522206c3 h1:kx/oGE7fgbzIUJAX+l+rN7HuEiOCHWaSwWnsNuGcYZs=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241016235738-9ba7522206c3/go.mod h1:tsGgeEJc5SUSlfVGSX0wR0EkRU3pM58D6SKF97V68ko=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241101202308-e11c3552f2b5 h1:E82sW2Rg6JjO7UrHtluz+HWB8LG2wouMgeYEu8wORus=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241101202308-e11c3552f2b5/go.mod h1:tsGgeEJc5SUSlfVGSX0wR0EkRU3pM58D6SKF97V68ko=
github.com/smartcontractkit/chainlink-cosmos v0.5.1 h1:2xeZWh+4/w7xalTdAu8jqgFuxZ291aYTEwZhlQEv/BY=
github.com/smartcontractkit/chainlink-cosmos v0.5.1/go.mod h1:c1wUtVxXUqW4PzuCQhuHaBDZFv9XAQjhKTqam7GLGIU=
github.com/smartcontractkit/chainlink-data-streams v0.1.0 h1:wcRJRm7eqfbgN+Na+GjAe0/IUn6XwmSagFHqIWHHBGk=
Expand Down

0 comments on commit 788be92

Please sign in to comment.