github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/meter/meter.go (about)

     1  package meter
     2  
     3  type MeterParameters struct {
     4  	ComputationMeterParameters
     5  	MemoryMeterParameters
     6  	EventMeterParameters
     7  	InteractionMeterParameters
     8  }
     9  
    10  func DefaultParameters() MeterParameters {
    11  	return MeterParameters{
    12  		ComputationMeterParameters: DefaultComputationMeterParameters(),
    13  		MemoryMeterParameters:      DefaultMemoryParameters(),
    14  		EventMeterParameters:       DefaultEventMeterParameters(),
    15  		InteractionMeterParameters: DefaultInteractionMeterParameters(),
    16  	}
    17  }
    18  
    19  // Meter collects memory and computation usage and enforces limits
    20  // for any each memory/computation usage call it sums intensity multiplied by the weight of the intensity to the total
    21  // memory/computation usage metrics and returns error if limits are not met.
    22  type Meter struct {
    23  	MeterParameters
    24  
    25  	MemoryMeter
    26  	ComputationMeter
    27  	EventMeter
    28  	InteractionMeter
    29  }
    30  
    31  // NewMeter constructs a new Meter
    32  func NewMeter(params MeterParameters) *Meter {
    33  	return &Meter{
    34  		MeterParameters:  params,
    35  		ComputationMeter: NewComputationMeter(params.ComputationMeterParameters),
    36  		MemoryMeter:      NewMemoryMeter(params.MemoryMeterParameters),
    37  		EventMeter:       NewEventMeter(params.EventMeterParameters),
    38  		InteractionMeter: NewInteractionMeter(params.InteractionMeterParameters),
    39  	}
    40  }
    41  
    42  // MergeMeter merges the input meter into the current meter
    43  func (m *Meter) MergeMeter(child *Meter) {
    44  	if child == nil {
    45  		return
    46  	}
    47  
    48  	m.ComputationMeter.Merge(child.ComputationMeter)
    49  	m.MemoryMeter.Merge(child.MemoryMeter)
    50  	m.EventMeter.Merge(child.EventMeter)
    51  	m.InteractionMeter.Merge(child.InteractionMeter)
    52  }