Skip to content

Commit

Permalink
Add VM throttler metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Feb 15, 2024
1 parent ccc75ac commit 6c8a186
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
16 changes: 16 additions & 0 deletions node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,19 @@ func makeJeMallocMetrics() {
})
prometheus.MustRegister(active)
}

func makeVMThrottlerMetrics(throttledVM *ThrottledVM) {
vmJobs := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "vm",
Name: "jobs",
}, func() float64 {
return float64(throttledVM.JobsRunning())
})
vmQueue := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "vm",
Name: "queue",
}, func() float64 {
return float64(throttledVM.QueueLen())
})
prometheus.MustRegister(vmJobs, vmQueue)
}
1 change: 1 addition & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
var metricsService service.Service
if cfg.Metrics {
makeJeMallocMetrics()
makeVMThrottlerMetrics(throttledVM)
makePebbleMetrics(database)
chain.WithListener(makeBlockchainMetrics())
makeJunoMetrics(version)
Expand Down
14 changes: 8 additions & 6 deletions node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import (

var _ vm.VM = (*ThrottledVM)(nil)

type ThrottledVM utils.Throttler[vm.VM]
type ThrottledVM struct {
*utils.Throttler[vm.VM]
}

func NewThrottledVM(res vm.VM, concurrenyBudget uint, maxQueueLen int32) *ThrottledVM {
return (*ThrottledVM)(utils.NewThrottler[vm.VM](concurrenyBudget, &res).WithMaxQueueLen(maxQueueLen))
return &ThrottledVM{
Throttler: utils.NewThrottler[vm.VM](concurrenyBudget, &res).WithMaxQueueLen(maxQueueLen),
}
}

func (tvm *ThrottledVM) Call(contractAddr, classHash, selector *felt.Felt, calldata []felt.Felt, blockNumber,
blockTimestamp uint64, state core.StateReader, network *utils.Network, maxSteps uint64,
) ([]*felt.Felt, error) {
var ret []*felt.Felt
throttler := (*utils.Throttler[vm.VM])(tvm)
return ret, throttler.Do(func(vm *vm.VM) error {
return ret, tvm.Do(func(vm *vm.VM) error {
var err error
ret, err = (*vm).Call(contractAddr, classHash, selector, calldata, blockNumber, blockTimestamp,
state, network, maxSteps)
Expand All @@ -34,8 +37,7 @@ func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.
) ([]*felt.Felt, []vm.TransactionTrace, error) {
var ret []*felt.Felt
var traces []vm.TransactionTrace
throttler := (*utils.Throttler[vm.VM])(tvm)
return ret, traces, throttler.Do(func(vm *vm.VM) error {
return ret, traces, tvm.Do(func(vm *vm.VM) error {
var err error
ret, traces, err = (*vm).Execute(txns, declaredClasses, blockNumber, blockTimestamp, sequencerAddress,
state, network, paidFeesOnL1, skipChargeFee, skipValidate, errOnRevert, gasPriceWEI, gasPriceSTRK, legacyTraceJSON)
Expand Down
5 changes: 5 additions & 0 deletions utils/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ func (t *Throttler[T]) Do(doer func(resource *T) error) error {
func (t *Throttler[T]) QueueLen() int {
return int(t.queue.Load())
}

// JobsRunning returns the number of Do calls that are running at the moment
func (t *Throttler[T]) JobsRunning() int {
return len(t.sem)
}

0 comments on commit 6c8a186

Please sign in to comment.