-
Notifications
You must be signed in to change notification settings - Fork 175
/
atree_reporter.go
255 lines (210 loc) · 5.67 KB
/
atree_reporter.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
package reporters
import (
"bytes"
"errors"
"fmt"
goRuntime "runtime"
"sync"
"github.com/onflow/atree"
"github.com/rs/zerolog"
"github.com/schollz/progressbar/v3"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
)
// AtreeReporter iterates payloads and generates payload and atree stats.
type AtreeReporter struct {
Log zerolog.Logger
RWF ReportWriterFactory
}
var _ ledger.Reporter = &AtreeReporter{}
func (r *AtreeReporter) Name() string {
return "Atree Reporter"
}
func (r *AtreeReporter) Report(payloads []ledger.Payload, commit ledger.State) error {
rwa := r.RWF.ReportWriter("atree_report")
defer rwa.Close()
progress := progressbar.Default(int64(len(payloads)), "Processing:")
workerCount := goRuntime.NumCPU() / 2
if workerCount == 0 {
workerCount = 1
}
jobs := make(chan *ledger.Payload, workerCount)
results := make(chan payloadStats, workerCount)
defer close(results)
// create multiple workers to process payloads concurrently
wg := &sync.WaitGroup{}
wg.Add(workerCount)
for i := 0; i < workerCount; i++ {
go func() {
defer wg.Done()
var stats payloadStats
for p := range jobs {
err := stats.process(p)
if err != nil {
k, keyErr := p.Key()
if keyErr != nil {
r.Log.Err(keyErr).Msg("failed to get payload key")
} else {
r.Log.Err(err).Msgf("failed to process payload %s", k.String())
}
}
}
results <- stats
}()
}
// produce jobs for workers to process
for i := 0; i < len(payloads); i++ {
jobs <- &payloads[i]
err := progress.Add(1)
if err != nil {
panic(fmt.Errorf("progress.Add(1): %w", err))
}
}
close(jobs)
// wait until all jobs are done
wg.Wait()
err := progress.Finish()
if err != nil {
panic(fmt.Errorf("progress.Finish(): %w", err))
}
// aggregate all payload stats
var stats payloadStats
for i := 0; i < workerCount; i++ {
r := <-results
stats.add(&r)
}
rwa.Write(stats)
return nil
}
type payloadType uint
const (
unknownPayloadType payloadType = iota
fvmPayloadType
storagePayloadType
slabPayloadType
)
func getPayloadType(p *ledger.Payload) (payloadType, error) {
k, err := p.Key()
if err != nil {
return unknownPayloadType, err
}
if len(k.KeyParts) < 2 {
return unknownPayloadType, nil
}
id := flow.NewRegisterID(
flow.BytesToAddress(k.KeyParts[0].Value),
string(k.KeyParts[1].Value))
if id.IsInternalState() {
return fvmPayloadType, nil
}
if bytes.HasPrefix(k.KeyParts[1].Value, []byte(atree.LedgerBaseStorageSlabPrefix)) {
return slabPayloadType, nil
}
return storagePayloadType, nil
}
type slabPayloadStats struct {
SlabArrayMetaCount uint
SlabArrayDataCount uint
SlabMapMetaCount uint
SlabMapDataCount uint
SlabMapExternalCollisionCount uint
SlabStorableCount uint
SlabMapCollisionGroupCount uint
SlabMapCollisionCounts []uint
}
type payloadStats struct {
FVMPayloadCount uint
StoragePayloadCount uint
SlabPayloadCount uint
slabPayloadStats
}
func (s *payloadStats) add(s1 *payloadStats) {
s.FVMPayloadCount += s1.FVMPayloadCount
s.StoragePayloadCount += s1.StoragePayloadCount
s.SlabPayloadCount += s1.SlabPayloadCount
s.SlabArrayMetaCount += s1.SlabArrayMetaCount
s.SlabArrayDataCount += s1.SlabArrayDataCount
s.SlabMapMetaCount += s1.SlabMapMetaCount
s.SlabMapDataCount += s1.SlabMapDataCount
s.SlabMapExternalCollisionCount += s1.SlabMapExternalCollisionCount
s.SlabStorableCount += s1.SlabStorableCount
s.SlabMapCollisionGroupCount += s1.SlabMapCollisionGroupCount
if len(s1.SlabMapCollisionCounts) > 0 {
s.SlabMapCollisionCounts = append(s.SlabMapCollisionCounts, s1.SlabMapCollisionCounts...)
}
}
func (s *payloadStats) process(p *ledger.Payload) error {
pt, err := getPayloadType(p)
if err != nil {
return err
}
switch pt {
case unknownPayloadType:
return fmt.Errorf("unknown payload: %s", p.String())
case fvmPayloadType:
s.FVMPayloadCount++
case storagePayloadType:
s.StoragePayloadCount++
case slabPayloadType:
s.SlabPayloadCount++
}
if pt != slabPayloadType {
return nil
}
if len(p.Value()) < versionAndFlagSize {
return errors.New("data is too short")
}
flag := p.Value()[flagIndex]
switch dataType := getSlabType(flag); dataType {
case slabArray:
switch arrayDataType := getSlabArrayType(flag); arrayDataType {
case slabArrayData:
s.SlabArrayDataCount++
case slabArrayMeta:
s.SlabArrayMetaCount++
default:
return fmt.Errorf("slab array has invalid flag 0x%x", flag)
}
case slabMap:
switch mapDataType := getSlabMapType(flag); mapDataType {
case slabMapData:
_, collisionGroupCount, err := getCollisionGroupCountFromSlabMapData(p.Value())
if err != nil {
return err
}
if collisionGroupCount > 0 {
_, inlineCollisionCount, err := getInlineCollisionCountsFromSlabMapData(p.Value())
if err != nil {
return err
}
if len(inlineCollisionCount) > 0 {
s.SlabMapCollisionCounts = append(s.SlabMapCollisionCounts, inlineCollisionCount...)
}
}
s.SlabMapCollisionGroupCount += collisionGroupCount
s.SlabMapDataCount++
case slabMapCollisionGroup:
elements, err := parseSlabMapData(p.Value())
if err != nil {
return err
}
_, rawElements, err := parseRawElements(elements, decMode)
if err != nil {
return err
}
if len(rawElements) > 0 {
s.SlabMapCollisionCounts = append(s.SlabMapCollisionCounts, uint(len(rawElements)))
}
s.SlabMapExternalCollisionCount++
case slabMapMeta:
s.SlabMapMetaCount++
default:
return fmt.Errorf("slab map has invalid flag 0x%x", flag)
}
case slabStorable:
s.SlabStorableCount++
default:
return fmt.Errorf("slab data has invalid flag 0x%x", flag)
}
return nil
}