From 788be9206718c25cc755c780a0b99941f67efd2e Mon Sep 17 00:00:00 2001
From: jinhoonbang <jin.bang@smartcontract.com>
Date: Wed, 30 Oct 2024 16:04:25 -0700
Subject: [PATCH] turn on profiler

---
 core/capabilities/compute/cache.go   | 13 +++++++++
 core/capabilities/compute/compute.go |  3 ++
 core/main.go                         | 43 ++++++++++++++++++++++++++++
 go.mod                               |  2 +-
 go.sum                               |  4 +--
 5 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/core/capabilities/compute/cache.go b/core/capabilities/compute/cache.go
index 7b7cd78aaab..41e1e0c474a 100644
--- a/core/capabilities/compute/cache.go
+++ b/core/capabilities/compute/cache.go
@@ -1,6 +1,7 @@
 package compute
 
 import (
+	"log"
 	"sync"
 	"time"
 
@@ -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,
@@ -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
 		}
 	}
@@ -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 {
diff --git a/core/capabilities/compute/compute.go b/core/capabilities/compute/compute.go
index 5a43b7bf40b..687b3e60eb8 100644
--- a/core/capabilities/compute/compute.go
+++ b/core/capabilities/compute/compute.go
@@ -5,6 +5,7 @@ import (
 	"crypto/sha256"
 	"errors"
 	"fmt"
+	"log"
 	"time"
 
 	"github.com/google/uuid"
@@ -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(),
diff --git a/core/main.go b/core/main.go
index 4ff013d9c96..a21a8d0c066 100644
--- a/core/main.go
+++ b/core/main.go
@@ -3,7 +3,12 @@ package core
 import (
 	"fmt"
 	"log"
+	"net/http"
+	_ "net/http/pprof"
 	"os"
+	"runtime"
+	"runtime/debug"
+	"time"
 
 	"github.com/Masterminds/semver/v3"
 
@@ -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)))
+
+		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))
+	}()
 	recovery.ReportPanics(func() {
 		app := cmd.NewApp(newProductionClient())
 		if err := app.Run(os.Args); err != nil {
diff --git a/go.mod b/go.mod
index 4f836787f67..c526544c906 100644
--- a/go.mod
+++ b/go.mod
@@ -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
diff --git a/go.sum b/go.sum
index 385aaca7a4a..61395784406 100644
--- a/go.sum
+++ b/go.sum
@@ -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=