-
Notifications
You must be signed in to change notification settings - Fork 178
/
memory_meter.go
349 lines (303 loc) · 13.1 KB
/
memory_meter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package meter
import (
"math"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
)
var (
// DefaultMemoryWeights are currently hard-coded here. In the future we might like to
// define this in a contract similar to the computation weights
DefaultMemoryWeights = ExecutionMemoryWeights{
// Values
common.MemoryKindAddressValue: 32,
common.MemoryKindStringValue: 138,
common.MemoryKindCharacterValue: 24,
common.MemoryKindNumberValue: 8,
// weights for these values include the cost of the Go struct itself (first number)
// as well as the overhead for creation of the underlying atree (second number)
common.MemoryKindArrayValueBase: 57 + 48,
common.MemoryKindDictionaryValueBase: 33 + 96,
common.MemoryKindCompositeValueBase: 233 + 96,
common.MemoryKindSimpleCompositeValue: 73,
common.MemoryKindSimpleCompositeValueBase: 89,
common.MemoryKindOptionalValue: 41,
common.MemoryKindTypeValue: 17,
common.MemoryKindPathValue: 24,
common.MemoryKindCapabilityValue: 1, // TODO: update with proper weight
common.MemoryKindStorageCapabilityControllerValue: 32,
common.MemoryKindAccountCapabilityControllerValue: 32,
common.MemoryKindPublishedValue: 1,
common.MemoryKindStorageReferenceValue: 41,
common.MemoryKindEphemeralReferenceValue: 41,
common.MemoryKindInterpretedFunctionValue: 128,
common.MemoryKindHostFunctionValue: 41,
common.MemoryKindBoundFunctionValue: 25,
common.MemoryKindBigInt: 50,
common.MemoryKindVoidExpression: 1,
// Atree
common.MemoryKindAtreeArrayDataSlab: 80,
common.MemoryKindAtreeArrayMetaDataSlab: 1024,
common.MemoryKindAtreeArrayElementOverhead: 16,
common.MemoryKindAtreeMapDataSlab: 144,
common.MemoryKindAtreeMapMetaDataSlab: 1024,
common.MemoryKindAtreeMapElementOverhead: 64,
common.MemoryKindAtreeMapPreAllocatedElement: 24,
common.MemoryKindAtreeEncodedSlab: 1536,
// Static Types
common.MemoryKindPrimitiveStaticType: 8,
common.MemoryKindCompositeStaticType: 17,
common.MemoryKindInterfaceStaticType: 17,
common.MemoryKindVariableSizedStaticType: 17,
common.MemoryKindConstantSizedStaticType: 25,
common.MemoryKindDictionaryStaticType: 33,
common.MemoryKindOptionalStaticType: 17,
common.MemoryKindIntersectionStaticType: 41,
common.MemoryKindReferenceStaticType: 41,
common.MemoryKindCapabilityStaticType: 17,
common.MemoryKindFunctionStaticType: 9,
// Cadence Values
common.MemoryKindCadenceVoidValue: 1,
common.MemoryKindCadenceOptionalValue: 17,
common.MemoryKindCadenceBoolValue: 8,
common.MemoryKindCadenceStringValue: 16,
common.MemoryKindCadenceCharacterValue: 16,
common.MemoryKindCadenceAddressValue: 8,
common.MemoryKindCadenceIntValue: 50,
common.MemoryKindCadenceNumberValue: 1,
common.MemoryKindCadenceArrayValueBase: 41,
common.MemoryKindCadenceArrayValueLength: 16,
common.MemoryKindCadenceDictionaryValue: 41,
common.MemoryKindCadenceKeyValuePair: 33,
common.MemoryKindCadenceStructValueBase: 33,
common.MemoryKindCadenceStructValueSize: 16,
common.MemoryKindCadenceResourceValueBase: 33,
common.MemoryKindCadenceResourceValueSize: 16,
common.MemoryKindCadenceEventValueBase: 33,
common.MemoryKindCadenceEventValueSize: 16,
common.MemoryKindCadenceContractValueBase: 33,
common.MemoryKindCadenceContractValueSize: 16,
common.MemoryKindCadenceEnumValueBase: 33,
common.MemoryKindCadenceEnumValueSize: 16,
common.MemoryKindCadencePathValue: 33,
common.MemoryKindCadenceTypeValue: 17,
common.MemoryKindCadenceCapabilityValue: 1, // TODO: update with proper weight
common.MemoryKindCadenceDeprecatedPathCapabilityType: 1, // TODO: remove, deprecated. Also has a wrong name
common.MemoryKindCadenceFunctionValue: 1,
common.MemoryKindCadenceAttachmentValueBase: 33,
common.MemoryKindCadenceAttachmentValueSize: 16,
// Cadence Types
common.MemoryKindCadenceTypeParameter: 17,
common.MemoryKindCadenceOptionalType: 17,
common.MemoryKindCadenceVariableSizedArrayType: 17,
common.MemoryKindCadenceConstantSizedArrayType: 25,
common.MemoryKindCadenceDictionaryType: 33,
common.MemoryKindCadenceField: 33,
common.MemoryKindCadenceParameter: 49,
common.MemoryKindCadenceStructType: 81,
common.MemoryKindCadenceResourceType: 81,
common.MemoryKindCadenceEventType: 81,
common.MemoryKindCadenceContractType: 81,
common.MemoryKindCadenceStructInterfaceType: 81,
common.MemoryKindCadenceResourceInterfaceType: 81,
common.MemoryKindCadenceContractInterfaceType: 81,
common.MemoryKindCadenceFunctionType: 41,
common.MemoryKindCadenceReferenceType: 25,
common.MemoryKindCadenceIntersectionType: 57,
common.MemoryKindCadenceDeprecatedRestrictedType: 57,
common.MemoryKindCadenceCapabilityType: 17,
common.MemoryKindCadenceEnumType: 97,
common.MemoryKindCadenceAttachmentType: 81,
// Misc
common.MemoryKindRawString: 9,
common.MemoryKindAddressLocation: 18,
common.MemoryKindBytes: 24,
common.MemoryKindVariable: 18,
common.MemoryKindCompositeTypeInfo: 41,
common.MemoryKindCompositeField: 33,
common.MemoryKindInvocation: 89,
common.MemoryKindStorageMap: 58,
common.MemoryKindStorageKey: 41,
// Tokens
common.MemoryKindErrorToken: 41,
common.MemoryKindTypeToken: 25,
common.MemoryKindSpaceToken: 50,
// AST nodes
common.MemoryKindProgram: 220,
common.MemoryKindIdentifier: 17,
common.MemoryKindArgument: 49,
common.MemoryKindBlock: 25,
common.MemoryKindFunctionBlock: 25,
common.MemoryKindParameter: 25,
common.MemoryKindParameterList: 59,
common.MemoryKindTypeParameter: 9,
common.MemoryKindTypeParameterList: 59,
common.MemoryKindTransfer: 1,
common.MemoryKindMembers: 276,
common.MemoryKindTypeAnnotation: 25,
common.MemoryKindDictionaryEntry: 33,
common.MemoryKindFunctionDeclaration: 49,
common.MemoryKindCompositeDeclaration: 65,
common.MemoryKindInterfaceDeclaration: 41,
common.MemoryKindEnumCaseDeclaration: 25,
common.MemoryKindFieldDeclaration: 41,
common.MemoryKindTransactionDeclaration: 81,
common.MemoryKindImportDeclaration: 41,
common.MemoryKindVariableDeclaration: 97,
common.MemoryKindSpecialFunctionDeclaration: 17,
common.MemoryKindPragmaDeclaration: 17,
common.MemoryKindAttachmentDeclaration: 70,
common.MemoryKindEntitlementDeclaration: 33,
common.MemoryKindEntitlementMappingElement: 17,
common.MemoryKindEntitlementMappingDeclaration: 57,
common.MemoryKindAssignmentStatement: 41,
common.MemoryKindBreakStatement: 1,
common.MemoryKindContinueStatement: 1,
common.MemoryKindEmitStatement: 9,
common.MemoryKindExpressionStatement: 17,
common.MemoryKindForStatement: 33,
common.MemoryKindIfStatement: 33,
common.MemoryKindReturnStatement: 17,
common.MemoryKindSwapStatement: 33,
common.MemoryKindSwitchStatement: 41,
common.MemoryKindWhileStatement: 25,
common.MemoryKindRemoveStatement: 33,
common.MemoryKindBooleanExpression: 9,
common.MemoryKindNilExpression: 1,
common.MemoryKindStringExpression: 17,
common.MemoryKindIntegerExpression: 33,
common.MemoryKindFixedPointExpression: 49,
common.MemoryKindArrayExpression: 25,
common.MemoryKindDictionaryExpression: 25,
common.MemoryKindIdentifierExpression: 1,
common.MemoryKindInvocationExpression: 49,
common.MemoryKindMemberExpression: 25,
common.MemoryKindIndexExpression: 33,
common.MemoryKindConditionalExpression: 49,
common.MemoryKindUnaryExpression: 25,
common.MemoryKindBinaryExpression: 41,
common.MemoryKindFunctionExpression: 25,
common.MemoryKindCastingExpression: 41,
common.MemoryKindCreateExpression: 9,
common.MemoryKindDestroyExpression: 17,
common.MemoryKindReferenceExpression: 33,
common.MemoryKindForceExpression: 17,
common.MemoryKindPathExpression: 1,
common.MemoryKindAttachExpression: 33,
common.MemoryKindConstantSizedType: 25,
common.MemoryKindDictionaryType: 33,
common.MemoryKindFunctionType: 33,
common.MemoryKindInstantiationType: 41,
common.MemoryKindNominalType: 25,
common.MemoryKindOptionalType: 17,
common.MemoryKindReferenceType: 25,
common.MemoryKindIntersectionType: 41,
common.MemoryKindVariableSizedType: 17,
common.MemoryKindPosition: 25,
common.MemoryKindRange: 1,
common.MemoryKindActivation: 128,
common.MemoryKindActivationEntries: 256,
common.MemoryKindElaboration: 501,
// sema types
common.MemoryKindVariableSizedSemaType: 51,
common.MemoryKindConstantSizedSemaType: 59,
common.MemoryKindDictionarySemaType: 67,
common.MemoryKindOptionalSemaType: 17,
common.MemoryKindIntersectionSemaType: 75,
common.MemoryKindReferenceSemaType: 25,
common.MemoryKindCapabilitySemaType: 51,
common.MemoryKindEntitlementSemaType: 49,
common.MemoryKindEntitlementMapSemaType: 73,
common.MemoryKindEntitlementRelationSemaType: 73,
// ordered-map
common.MemoryKindOrderedMap: 17,
common.MemoryKindOrderedMapEntryList: 50,
common.MemoryKindOrderedMapEntry: 64,
// Entitlement access
common.MemoryKindEntitlementSetStaticAccess: 17,
common.MemoryKindEntitlementMapStaticAccess: 17,
common.MemoryKindCadenceEntitlementSetAccess: 33,
common.MemoryKindCadenceEntitlementMapAccess: 17,
// InclusiveRange
common.MemoryKindInclusiveRangeStaticType: 17,
common.MemoryKindCadenceInclusiveRangeValue: 81,
common.MemoryKindCadenceInclusiveRangeType: 33,
common.MemoryKindInclusiveRangeSemaType: 17,
}
)
func _() {
// A compiler error signifies that we have not accounted for all memory kinds
var x [1]struct{}
_ = x[int(common.MemoryKindLast)-len(DefaultMemoryWeights)-1]
}
type ExecutionMemoryWeights map[common.MemoryKind]uint64
type MeteredMemoryIntensities map[common.MemoryKind]uint
type MemoryMeterParameters struct {
memoryLimit uint64
memoryWeights ExecutionMemoryWeights
}
func DefaultMemoryParameters() MemoryMeterParameters {
return MemoryMeterParameters{
memoryLimit: math.MaxUint64,
memoryWeights: DefaultMemoryWeights,
}
}
func (params MemoryMeterParameters) MemoryWeights() ExecutionMemoryWeights {
return params.memoryWeights
}
// TotalMemoryLimit returns the total memory limit
func (params MemoryMeterParameters) TotalMemoryLimit() uint64 {
return params.memoryLimit
}
func (params MeterParameters) WithMemoryLimit(limit uint64) MeterParameters {
newParams := params
newParams.memoryLimit = limit
return newParams
}
func (params MeterParameters) WithMemoryWeights(
weights ExecutionMemoryWeights,
) MeterParameters {
newParams := params
newParams.memoryWeights = weights
return newParams
}
type MemoryMeter struct {
params MemoryMeterParameters
memoryIntensities MeteredMemoryIntensities
memoryEstimate uint64
}
// MemoryIntensities returns all the measured memory intensities
func (m *MemoryMeter) MemoryIntensities() MeteredMemoryIntensities {
return m.memoryIntensities
}
// NewMemoryMeter constructs a new Meter
func NewMemoryMeter(params MemoryMeterParameters) MemoryMeter {
m := MemoryMeter{
params: params,
memoryIntensities: make(MeteredMemoryIntensities),
}
return m
}
// MeterMemory captures memory usage and returns an error if it goes beyond the limit
func (m *MemoryMeter) MeterMemory(kind common.MemoryKind, intensity uint) error {
m.memoryIntensities[kind] += intensity
w, ok := m.params.memoryWeights[kind]
if !ok {
return nil
}
m.memoryEstimate += w * uint64(intensity)
if m.memoryEstimate > m.params.memoryLimit {
return errors.NewMemoryLimitExceededError(m.params.TotalMemoryLimit())
}
return nil
}
// TotalMemoryEstimate returns the total memory used
func (m *MemoryMeter) TotalMemoryEstimate() uint64 {
return m.memoryEstimate
}
// Merge merges the input meter into the current meter and checks for the limits
func (m *MemoryMeter) Merge(child MemoryMeter) {
m.memoryEstimate = m.memoryEstimate + child.TotalMemoryEstimate()
for key, intensity := range child.memoryIntensities {
m.memoryIntensities[key] += intensity
}
}