github.com/koko1123/flow-go-1@v0.29.6/fvm/meter/meter.go (about) 1 package meter 2 3 import ( 4 "math" 5 6 "github.com/onflow/cadence/runtime/common" 7 8 "github.com/koko1123/flow-go-1/fvm/errors" 9 "github.com/koko1123/flow-go-1/model/flow" 10 ) 11 12 // TODO(patrick): rm after https://github.com/onflow/flow-emulator/pull/229 13 // is merged and integrated. 14 const ( 15 ComputationKindCreateAccount = 2006 16 ComputationKindGetValue = 2020 17 ComputationKindSetValue = 2026 18 ) 19 20 type MeteredComputationIntensities map[common.ComputationKind]uint 21 type MeteredMemoryIntensities map[common.MemoryKind]uint 22 type MeteredStorageInteractionMap map[StorageInteractionKey]uint64 23 24 type StorageInteractionKey struct { 25 Owner, Key string 26 } 27 28 // MeterExecutionInternalPrecisionBytes are the amount of bytes that are used internally by the WeigthedMeter 29 // to allow for metering computation smaller than one unit of computation. This allows for more fine weights. 30 // A weight of 1 unit of computation is equal to 1<<16. The minimum possible weight is 1/65536. 31 const MeterExecutionInternalPrecisionBytes = 16 32 33 type ExecutionEffortWeights map[common.ComputationKind]uint64 34 type ExecutionMemoryWeights map[common.MemoryKind]uint64 35 36 var ( 37 // DefaultComputationWeights is the default weights for computation intensities 38 // these weighs make the computation metering the same as it was before dynamic execution fees 39 DefaultComputationWeights = ExecutionEffortWeights{ 40 common.ComputationKindStatement: 1 << MeterExecutionInternalPrecisionBytes, 41 common.ComputationKindLoop: 1 << MeterExecutionInternalPrecisionBytes, 42 common.ComputationKindFunctionInvocation: 1 << MeterExecutionInternalPrecisionBytes, 43 } 44 45 // DefaultMemoryWeights are currently hard-coded here. In the future we might like to 46 // define this in a contract similar to the computation weights 47 DefaultMemoryWeights = ExecutionMemoryWeights{ 48 49 // Values 50 51 common.MemoryKindAddressValue: 32, 52 common.MemoryKindStringValue: 138, 53 common.MemoryKindCharacterValue: 24, 54 common.MemoryKindNumberValue: 8, 55 // weights for these values include the cost of the Go struct itself (first number) 56 // as well as the overhead for creation of the underlying atree (second number) 57 common.MemoryKindArrayValueBase: 57 + 48, 58 common.MemoryKindDictionaryValueBase: 33 + 96, 59 common.MemoryKindCompositeValueBase: 233 + 96, 60 common.MemoryKindSimpleCompositeValue: 73, 61 common.MemoryKindSimpleCompositeValueBase: 89, 62 common.MemoryKindOptionalValue: 41, 63 common.MemoryKindTypeValue: 17, 64 common.MemoryKindPathValue: 24, 65 common.MemoryKindStorageCapabilityValue: 1, 66 common.MemoryKindPathLinkValue: 1, 67 common.MemoryKindAccountLinkValue: 1, 68 common.MemoryKindAccountReferenceValue: 1, 69 common.MemoryKindPublishedValue: 1, 70 common.MemoryKindStorageReferenceValue: 41, 71 common.MemoryKindEphemeralReferenceValue: 41, 72 common.MemoryKindInterpretedFunctionValue: 128, 73 common.MemoryKindHostFunctionValue: 41, 74 common.MemoryKindBoundFunctionValue: 25, 75 common.MemoryKindBigInt: 50, 76 common.MemoryKindVoidExpression: 1, 77 78 // Atree 79 80 common.MemoryKindAtreeArrayDataSlab: 80, 81 common.MemoryKindAtreeArrayMetaDataSlab: 1024, 82 common.MemoryKindAtreeArrayElementOverhead: 16, 83 common.MemoryKindAtreeMapDataSlab: 144, 84 common.MemoryKindAtreeMapMetaDataSlab: 1024, 85 common.MemoryKindAtreeMapElementOverhead: 64, 86 common.MemoryKindAtreeMapPreAllocatedElement: 24, 87 common.MemoryKindAtreeEncodedSlab: 1536, 88 89 // Static Types 90 91 common.MemoryKindPrimitiveStaticType: 8, 92 common.MemoryKindCompositeStaticType: 17, 93 common.MemoryKindInterfaceStaticType: 17, 94 common.MemoryKindVariableSizedStaticType: 17, 95 common.MemoryKindConstantSizedStaticType: 25, 96 common.MemoryKindDictionaryStaticType: 33, 97 common.MemoryKindOptionalStaticType: 17, 98 common.MemoryKindRestrictedStaticType: 41, 99 common.MemoryKindReferenceStaticType: 41, 100 common.MemoryKindCapabilityStaticType: 17, 101 common.MemoryKindFunctionStaticType: 9, 102 103 // Cadence Values 104 105 common.MemoryKindCadenceVoidValue: 1, 106 common.MemoryKindCadenceOptionalValue: 17, 107 common.MemoryKindCadenceBoolValue: 8, 108 common.MemoryKindCadenceStringValue: 16, 109 common.MemoryKindCadenceCharacterValue: 16, 110 common.MemoryKindCadenceAddressValue: 8, 111 common.MemoryKindCadenceIntValue: 50, 112 common.MemoryKindCadenceNumberValue: 1, 113 common.MemoryKindCadenceArrayValueBase: 41, 114 common.MemoryKindCadenceArrayValueLength: 16, 115 common.MemoryKindCadenceDictionaryValue: 41, 116 common.MemoryKindCadenceKeyValuePair: 33, 117 common.MemoryKindCadenceStructValueBase: 33, 118 common.MemoryKindCadenceStructValueSize: 16, 119 common.MemoryKindCadenceResourceValueBase: 33, 120 common.MemoryKindCadenceResourceValueSize: 16, 121 common.MemoryKindCadenceEventValueBase: 33, 122 common.MemoryKindCadenceEventValueSize: 16, 123 common.MemoryKindCadenceContractValueBase: 33, 124 common.MemoryKindCadenceContractValueSize: 16, 125 common.MemoryKindCadenceEnumValueBase: 33, 126 common.MemoryKindCadenceEnumValueSize: 16, 127 common.MemoryKindCadencePathLinkValue: 1, 128 common.MemoryKindCadencePathValue: 33, 129 common.MemoryKindCadenceTypeValue: 17, 130 common.MemoryKindCadenceStorageCapabilityValue: 1, 131 common.MemoryKindCadenceFunctionValue: 1, 132 133 // Cadence Types 134 135 common.MemoryKindCadenceSimpleType: 1, 136 common.MemoryKindCadenceOptionalType: 17, 137 common.MemoryKindCadenceVariableSizedArrayType: 17, 138 common.MemoryKindCadenceConstantSizedArrayType: 25, 139 common.MemoryKindCadenceDictionaryType: 33, 140 common.MemoryKindCadenceField: 33, 141 common.MemoryKindCadenceParameter: 49, 142 common.MemoryKindCadenceStructType: 81, 143 common.MemoryKindCadenceResourceType: 81, 144 common.MemoryKindCadenceEventType: 81, 145 common.MemoryKindCadenceContractType: 81, 146 common.MemoryKindCadenceStructInterfaceType: 81, 147 common.MemoryKindCadenceResourceInterfaceType: 81, 148 common.MemoryKindCadenceContractInterfaceType: 81, 149 common.MemoryKindCadenceFunctionType: 41, 150 common.MemoryKindCadenceReferenceType: 25, 151 common.MemoryKindCadenceRestrictedType: 57, 152 common.MemoryKindCadenceCapabilityType: 17, 153 common.MemoryKindCadenceEnumType: 97, 154 155 // Misc 156 157 common.MemoryKindRawString: 9, 158 common.MemoryKindAddressLocation: 18, 159 common.MemoryKindBytes: 24, 160 common.MemoryKindVariable: 18, 161 common.MemoryKindCompositeTypeInfo: 41, 162 common.MemoryKindCompositeField: 33, 163 common.MemoryKindInvocation: 89, 164 common.MemoryKindStorageMap: 58, 165 common.MemoryKindStorageKey: 41, 166 167 // Tokens 168 169 common.MemoryKindErrorToken: 41, 170 common.MemoryKindTypeToken: 25, 171 common.MemoryKindSpaceToken: 50, 172 173 // AST nodes 174 175 common.MemoryKindProgram: 220, 176 common.MemoryKindIdentifier: 17, 177 common.MemoryKindArgument: 49, 178 common.MemoryKindBlock: 25, 179 common.MemoryKindFunctionBlock: 25, 180 common.MemoryKindParameter: 25, 181 common.MemoryKindParameterList: 59, 182 common.MemoryKindTransfer: 1, 183 common.MemoryKindMembers: 276, 184 common.MemoryKindTypeAnnotation: 25, 185 common.MemoryKindDictionaryEntry: 33, 186 187 common.MemoryKindFunctionDeclaration: 49, 188 common.MemoryKindCompositeDeclaration: 65, 189 common.MemoryKindInterfaceDeclaration: 41, 190 common.MemoryKindEnumCaseDeclaration: 25, 191 common.MemoryKindFieldDeclaration: 41, 192 common.MemoryKindTransactionDeclaration: 81, 193 common.MemoryKindImportDeclaration: 41, 194 common.MemoryKindVariableDeclaration: 97, 195 common.MemoryKindSpecialFunctionDeclaration: 17, 196 common.MemoryKindPragmaDeclaration: 17, 197 198 common.MemoryKindAssignmentStatement: 41, 199 common.MemoryKindBreakStatement: 1, 200 common.MemoryKindContinueStatement: 1, 201 common.MemoryKindEmitStatement: 9, 202 common.MemoryKindExpressionStatement: 17, 203 common.MemoryKindForStatement: 33, 204 common.MemoryKindIfStatement: 33, 205 common.MemoryKindReturnStatement: 17, 206 common.MemoryKindSwapStatement: 33, 207 common.MemoryKindSwitchStatement: 41, 208 common.MemoryKindWhileStatement: 25, 209 210 common.MemoryKindBooleanExpression: 9, 211 common.MemoryKindNilExpression: 1, 212 common.MemoryKindStringExpression: 17, 213 common.MemoryKindIntegerExpression: 33, 214 common.MemoryKindFixedPointExpression: 49, 215 common.MemoryKindArrayExpression: 25, 216 common.MemoryKindDictionaryExpression: 25, 217 common.MemoryKindIdentifierExpression: 1, 218 common.MemoryKindInvocationExpression: 49, 219 common.MemoryKindMemberExpression: 25, 220 common.MemoryKindIndexExpression: 33, 221 common.MemoryKindConditionalExpression: 49, 222 common.MemoryKindUnaryExpression: 25, 223 common.MemoryKindBinaryExpression: 41, 224 common.MemoryKindFunctionExpression: 25, 225 common.MemoryKindCastingExpression: 41, 226 common.MemoryKindCreateExpression: 9, 227 common.MemoryKindDestroyExpression: 17, 228 common.MemoryKindReferenceExpression: 33, 229 common.MemoryKindForceExpression: 17, 230 common.MemoryKindPathExpression: 1, 231 232 common.MemoryKindConstantSizedType: 25, 233 common.MemoryKindDictionaryType: 33, 234 common.MemoryKindFunctionType: 33, 235 common.MemoryKindInstantiationType: 41, 236 common.MemoryKindNominalType: 25, 237 common.MemoryKindOptionalType: 17, 238 common.MemoryKindReferenceType: 25, 239 common.MemoryKindRestrictedType: 41, 240 common.MemoryKindVariableSizedType: 17, 241 242 common.MemoryKindPosition: 25, 243 common.MemoryKindRange: 1, 244 common.MemoryKindActivation: 128, 245 common.MemoryKindActivationEntries: 256, 246 common.MemoryKindElaboration: 501, 247 248 // sema types 249 common.MemoryKindVariableSizedSemaType: 51, 250 common.MemoryKindConstantSizedSemaType: 59, 251 common.MemoryKindDictionarySemaType: 67, 252 common.MemoryKindOptionalSemaType: 17, 253 common.MemoryKindRestrictedSemaType: 75, 254 common.MemoryKindReferenceSemaType: 25, 255 common.MemoryKindCapabilitySemaType: 51, 256 257 // ordered-map 258 common.MemoryKindOrderedMap: 17, 259 common.MemoryKindOrderedMapEntryList: 50, 260 common.MemoryKindOrderedMapEntry: 64, 261 } 262 ) 263 264 func _() { 265 // A compiler error signifies that we have not accounted for all memory kinds 266 var x [1]struct{} 267 _ = x[int(common.MemoryKindLast)-len(DefaultMemoryWeights)-1] 268 } 269 270 type MeterParameters struct { 271 computationLimit uint64 272 computationWeights ExecutionEffortWeights 273 274 memoryLimit uint64 275 memoryWeights ExecutionMemoryWeights 276 277 storageInteractionLimit uint64 278 eventEmitByteLimit uint64 279 } 280 281 func DefaultParameters() MeterParameters { 282 // This is needed to work around golang's compiler bug 283 umax := uint(math.MaxUint) 284 return MeterParameters{ 285 computationLimit: uint64(umax) << MeterExecutionInternalPrecisionBytes, 286 memoryLimit: math.MaxUint64, 287 computationWeights: DefaultComputationWeights, 288 memoryWeights: DefaultMemoryWeights, 289 storageInteractionLimit: math.MaxUint64, 290 eventEmitByteLimit: math.MaxUint64, 291 } 292 } 293 294 func (params MeterParameters) WithComputationLimit(limit uint) MeterParameters { 295 newParams := params 296 newParams.computationLimit = uint64(limit) << MeterExecutionInternalPrecisionBytes 297 return newParams 298 } 299 300 func (params MeterParameters) WithComputationWeights( 301 weights ExecutionEffortWeights, 302 ) MeterParameters { 303 newParams := params 304 newParams.computationWeights = weights 305 return newParams 306 } 307 308 func (params MeterParameters) WithMemoryLimit(limit uint64) MeterParameters { 309 newParams := params 310 newParams.memoryLimit = limit 311 return newParams 312 } 313 314 func (params MeterParameters) WithMemoryWeights( 315 weights ExecutionMemoryWeights, 316 ) MeterParameters { 317 newParams := params 318 newParams.memoryWeights = weights 319 return newParams 320 } 321 322 func (params MeterParameters) WithStorageInteractionLimit( 323 maxStorageInteractionLimit uint64, 324 ) MeterParameters { 325 newParams := params 326 newParams.storageInteractionLimit = maxStorageInteractionLimit 327 return newParams 328 } 329 330 func (params MeterParameters) WithEventEmitByteLimit( 331 byteLimit uint64, 332 ) MeterParameters { 333 newParams := params 334 newParams.eventEmitByteLimit = byteLimit 335 return newParams 336 } 337 338 func (params MeterParameters) ComputationWeights() ExecutionEffortWeights { 339 return params.computationWeights 340 } 341 342 // TotalComputationLimit returns the total computation limit 343 func (params MeterParameters) TotalComputationLimit() uint { 344 return uint(params.computationLimit >> MeterExecutionInternalPrecisionBytes) 345 } 346 347 func (params MeterParameters) MemoryWeights() ExecutionMemoryWeights { 348 return params.memoryWeights 349 } 350 351 // TotalMemoryLimit returns the total memory limit 352 func (params MeterParameters) TotalMemoryLimit() uint64 { 353 return params.memoryLimit 354 } 355 356 // Meter collects memory and computation usage and enforces limits 357 // for any each memory/computation usage call it sums intensity multiplied by the weight of the intensity to the total 358 // memory/computation usage metrics and returns error if limits are not met. 359 type Meter struct { 360 MeterParameters 361 362 computationUsed uint64 363 memoryEstimate uint64 364 365 computationIntensities MeteredComputationIntensities 366 memoryIntensities MeteredMemoryIntensities 367 368 storageUpdateSizeMap map[StorageInteractionKey]uint64 369 totalStorageBytesRead uint64 370 totalStorageBytesWritten uint64 371 372 totalEmittedEventBytes uint64 373 } 374 375 type MeterOptions func(*Meter) 376 377 // NewMeter constructs a new Meter 378 func NewMeter(params MeterParameters) *Meter { 379 m := &Meter{ 380 MeterParameters: params, 381 computationIntensities: make(MeteredComputationIntensities), 382 memoryIntensities: make(MeteredMemoryIntensities), 383 storageUpdateSizeMap: make(MeteredStorageInteractionMap), 384 } 385 386 return m 387 } 388 389 // MergeMeter merges the input meter into the current meter and checks for the limits 390 func (m *Meter) MergeMeter(child *Meter) { 391 if child == nil { 392 return 393 } 394 395 var childComputationUsed = child.computationUsed 396 m.computationUsed = m.computationUsed + childComputationUsed 397 398 for key, intensity := range child.computationIntensities { 399 m.computationIntensities[key] += intensity 400 } 401 402 m.memoryEstimate = m.memoryEstimate + child.TotalMemoryEstimate() 403 404 for key, intensity := range child.memoryIntensities { 405 m.memoryIntensities[key] += intensity 406 } 407 408 // merge storage meters 409 for key, value := range child.storageUpdateSizeMap { 410 m.storageUpdateSizeMap[key] = value 411 } 412 m.totalStorageBytesRead += child.TotalBytesReadFromStorage() 413 m.totalStorageBytesWritten += child.TotalBytesWrittenToStorage() 414 415 m.totalEmittedEventBytes += child.TotalEmittedEventBytes() 416 } 417 418 // MeterComputation captures computation usage and returns an error if it goes beyond the limit 419 func (m *Meter) MeterComputation(kind common.ComputationKind, intensity uint) error { 420 m.computationIntensities[kind] += intensity 421 w, ok := m.computationWeights[kind] 422 if !ok { 423 return nil 424 } 425 m.computationUsed += w * uint64(intensity) 426 if m.computationUsed > m.computationLimit { 427 return errors.NewComputationLimitExceededError(uint64(m.TotalComputationLimit())) 428 } 429 return nil 430 } 431 432 // ComputationIntensities returns all the measured computational intensities 433 func (m *Meter) ComputationIntensities() MeteredComputationIntensities { 434 return m.computationIntensities 435 } 436 437 // TotalComputationUsed returns the total computation used 438 func (m *Meter) TotalComputationUsed() uint64 { 439 return m.computationUsed >> MeterExecutionInternalPrecisionBytes 440 } 441 442 // MeterMemory captures memory usage and returns an error if it goes beyond the limit 443 func (m *Meter) MeterMemory(kind common.MemoryKind, intensity uint) error { 444 m.memoryIntensities[kind] += intensity 445 w, ok := m.memoryWeights[kind] 446 if !ok { 447 return nil 448 } 449 m.memoryEstimate += w * uint64(intensity) 450 if m.memoryEstimate > m.memoryLimit { 451 return errors.NewMemoryLimitExceededError(uint64(m.TotalMemoryLimit())) 452 } 453 return nil 454 } 455 456 // MemoryIntensities returns all the measured memory intensities 457 func (m *Meter) MemoryIntensities() MeteredMemoryIntensities { 458 return m.memoryIntensities 459 } 460 461 // TotalMemoryEstimate returns the total memory used 462 func (m *Meter) TotalMemoryEstimate() uint64 { 463 return m.memoryEstimate 464 } 465 466 // MeterStorageRead captures storage read bytes count and returns an error 467 // if it goes beyond the total interaction limit and limit is enforced 468 func (m *Meter) MeterStorageRead( 469 storageKey StorageInteractionKey, 470 value flow.RegisterValue, 471 enforceLimit bool) error { 472 473 // all reads are on a View which only read from storage at the first read of a given key 474 if _, ok := m.storageUpdateSizeMap[storageKey]; !ok { 475 readByteSize := getStorageKeyValueSize(storageKey, value) 476 m.totalStorageBytesRead += readByteSize 477 m.storageUpdateSizeMap[storageKey] = readByteSize 478 } 479 480 return m.checkStorageInteractionLimit(enforceLimit) 481 } 482 483 // MeterStorageRead captures storage written bytes count and returns an error 484 // if it goes beyond the total interaction limit and limit is enforced 485 func (m *Meter) MeterStorageWrite( 486 storageKey StorageInteractionKey, 487 value flow.RegisterValue, 488 enforceLimit bool) error { 489 // all writes are on a View which only writes the latest updated value to storage at commit 490 if old, ok := m.storageUpdateSizeMap[storageKey]; ok { 491 m.totalStorageBytesWritten -= old 492 } 493 494 updateSize := getStorageKeyValueSize(storageKey, value) 495 m.totalStorageBytesWritten += updateSize 496 m.storageUpdateSizeMap[storageKey] = updateSize 497 498 return m.checkStorageInteractionLimit(enforceLimit) 499 } 500 501 func (m *Meter) checkStorageInteractionLimit(enforceLimit bool) error { 502 if enforceLimit && 503 m.TotalBytesOfStorageInteractions() > m.storageInteractionLimit { 504 return errors.NewLedgerInteractionLimitExceededError( 505 m.TotalBytesOfStorageInteractions(), m.storageInteractionLimit) 506 } 507 return nil 508 } 509 510 // TotalBytesReadFromStorage returns total number of byte read from storage 511 func (m *Meter) TotalBytesReadFromStorage() uint64 { 512 return m.totalStorageBytesRead 513 } 514 515 // TotalBytesReadFromStorage returns total number of byte written to storage 516 func (m *Meter) TotalBytesWrittenToStorage() uint64 { 517 return m.totalStorageBytesWritten 518 } 519 520 // TotalBytesOfStorageInteractions returns total number of byte read and written from/to storage 521 func (m *Meter) TotalBytesOfStorageInteractions() uint64 { 522 return m.TotalBytesReadFromStorage() + m.TotalBytesWrittenToStorage() 523 } 524 525 func getStorageKeyValueSize(storageKey StorageInteractionKey, 526 value flow.RegisterValue) uint64 { 527 return uint64(len(storageKey.Owner) + len(storageKey.Key) + len(value)) 528 } 529 530 func GetStorageKeyValueSizeForTesting( 531 storageKey StorageInteractionKey, 532 value flow.RegisterValue) uint64 { 533 return getStorageKeyValueSize(storageKey, value) 534 } 535 536 func (m *Meter) GetStorageUpdateSizeMapForTesting() MeteredStorageInteractionMap { 537 return m.storageUpdateSizeMap 538 } 539 540 func (m *Meter) MeterEmittedEvent(byteSize uint64) error { 541 m.totalEmittedEventBytes += uint64(byteSize) 542 543 if m.totalEmittedEventBytes > m.eventEmitByteLimit { 544 return errors.NewEventLimitExceededError( 545 m.totalEmittedEventBytes, 546 m.eventEmitByteLimit) 547 } 548 return nil 549 } 550 551 func (m *Meter) TotalEmittedEventBytes() uint64 { 552 return m.totalEmittedEventBytes 553 }