-
Notifications
You must be signed in to change notification settings - Fork 328
/
dependency_graph_builder.go
410 lines (355 loc) · 13.8 KB
/
dependency_graph_builder.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dependencygraph2
import (
"context"
"fmt"
"sort"
"github.com/google/gapid/core/app/benchmark"
"github.com/google/gapid/core/app/status"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/math/interval"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/capture"
"github.com/google/gapid/gapis/config"
"github.com/google/gapid/gapis/memory"
)
var (
dependencyGraphBuilderCounter = benchmark.Duration("DependencyGraph.Builder")
)
type NodeStats struct {
NumFragReads uint64
NumFragWrites uint64
NumMemReads uint64
NumMemWrites uint64
NumForwardDepOpens uint64
NumForwardDepCloses uint64
NumForwardDepDrops uint64
NumDeps uint64
NumFragDeps uint64
NumCompleteFragDeps uint64
NumMemDeps uint64
UniqueFragReads uint64
UniqueFragWrites uint64
UniqueMemReads uint64
UniqueMemWrites uint64
UniqueDeps uint64
}
type AccessMode uint
const (
ACCESS_READ AccessMode = 1 << 0
ACCESS_WRITE AccessMode = 1 << 1
ACCESS_READ_WRITE AccessMode = ACCESS_READ | ACCESS_WRITE
)
// The data needed to build a dependency graph by iterating through the commands in a trace
type dependencyGraphBuilder struct {
// graph is the dependency graph being constructed
// graph *dependencyGraph
capture *capture.GraphicsCapture
config DependencyGraphConfig
fragWatcher FragWatcher
memWatcher MemWatcher
forwardWatcher ForwardWatcher
graphBuilder GraphBuilder
subCmdStack []CmdContext
Stats struct {
NumFragReads uint64
NumFragWrites uint64
NumMemReads uint64
NumMemWrites uint64
NumForwardDepOpens uint64
NumForwardDepCloses uint64
NumForwardDepDrops uint64
}
}
// Build a new dependencyGraphBuilder.
func newDependencyGraphBuilder(ctx context.Context, config DependencyGraphConfig,
c *capture.GraphicsCapture, initialCmds []api.Cmd, state *api.GlobalState) *dependencyGraphBuilder {
builder := &dependencyGraphBuilder{}
builder.capture = c
builder.config = config
builder.fragWatcher = NewFragWatcher()
builder.memWatcher = NewMemWatcher()
builder.forwardWatcher = NewForwardWatcher()
builder.graphBuilder = NewGraphBuilder(ctx, config, c, initialCmds, state)
return builder
}
// BeginCmd is called at the beginning of each API call
func (b *dependencyGraphBuilder) OnBeginCmd(ctx context.Context, cmdID api.CmdID, cmd api.Cmd) {
if len(b.subCmdStack) > 0 {
log.E(ctx, "OnBeginCmd called while processing another command")
b.subCmdStack = b.subCmdStack[:0]
}
cmdCtx := b.graphBuilder.GetCmdContext(ctx, cmdID, cmd)
b.graphBuilder.OnBeginCmd(ctx, cmdCtx)
b.fragWatcher.OnBeginCmd(ctx, cmdCtx)
b.memWatcher.OnBeginCmd(ctx, cmdCtx)
b.forwardWatcher.OnBeginCmd(ctx, cmdCtx)
b.subCmdStack = append(b.subCmdStack, cmdCtx)
}
// EndCmd is called at the end of each API call
func (b *dependencyGraphBuilder) OnEndCmd(ctx context.Context, cmdID api.CmdID, cmd api.Cmd) {
if len(b.subCmdStack) > 1 {
log.E(ctx, "OnEndCmd called while still processing subcommands")
}
cmdCtx := b.cmdCtx()
fragAcc := b.fragWatcher.OnEndCmd(ctx, cmdCtx)
memAcc := b.memWatcher.OnEndCmd(ctx, cmdCtx)
accesses := b.forwardWatcher.OnEndCmd(ctx, cmdCtx)
b.graphBuilder.AddDependencies(ctx, fragAcc, memAcc, accesses.nodeAccesses, accesses.isUnopened)
b.subCmdStack = b.subCmdStack[:0]
}
func (b *dependencyGraphBuilder) OnBeginSubCmd(ctx context.Context, subCmdIdx api.SubCmdIdx, recordIdx api.RecordIdx) {
if len(b.subCmdStack) == 0 {
log.E(ctx, "OnBeginSubCmd called while not processing any command")
}
cmdCtx := b.cmdCtx()
if b.config.MergeSubCmdNodes {
subCmdCtx := cmdCtx
subCmdCtx.subCmdIdx = subCmdIdx
b.graphBuilder.OnBeginSubCmd(ctx, cmdCtx, subCmdCtx, recordIdx)
return
}
subCmdCtx := b.graphBuilder.GetSubCmdContext(cmdCtx.cmdID, subCmdIdx)
b.graphBuilder.OnBeginSubCmd(ctx, cmdCtx, subCmdCtx, recordIdx)
b.fragWatcher.OnBeginSubCmd(ctx, cmdCtx, subCmdCtx)
b.memWatcher.OnBeginSubCmd(ctx, cmdCtx, subCmdCtx)
b.forwardWatcher.OnBeginSubCmd(ctx, cmdCtx, subCmdCtx)
b.subCmdStack = append(b.subCmdStack, subCmdCtx)
}
func (b *dependencyGraphBuilder) OnEndSubCmd(ctx context.Context) {
if b.config.MergeSubCmdNodes {
return
}
if len(b.subCmdStack) < 2 {
log.E(ctx, "OnEndSubCmd called while not processing any subcommand")
}
cmdCtx := b.cmdCtx()
b.fragWatcher.OnEndSubCmd(ctx, cmdCtx)
b.memWatcher.OnEndSubCmd(ctx, cmdCtx)
b.forwardWatcher.OnEndSubCmd(ctx, cmdCtx)
b.subCmdStack = b.subCmdStack[:len(b.subCmdStack)-1]
}
func (b *dependencyGraphBuilder) OnReadFrag(ctx context.Context, owner api.RefObject, frag api.Fragment, valueRef api.RefObject, track bool) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumFragReads++
b.Stats.NumFragReads++
b.fragWatcher.OnReadFrag(ctx, cmdCtx, owner, frag, valueRef, track)
}
func (b *dependencyGraphBuilder) OnWriteFrag(ctx context.Context, owner api.RefObject, frag api.Fragment, oldValueRef api.RefObject, newValueRef api.RefObject, track bool) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumFragWrites++
b.Stats.NumFragWrites++
b.fragWatcher.OnWriteFrag(ctx, cmdCtx, owner, frag, oldValueRef, newValueRef, track)
}
// OnWriteSlice is called when writing to a slice
func (b *dependencyGraphBuilder) OnWriteSlice(ctx context.Context, slice memory.Slice) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumMemWrites++
b.Stats.NumMemWrites++
b.memWatcher.OnWriteSlice(ctx, cmdCtx, slice)
}
// OnReadSlice is called when reading from a slice
func (b *dependencyGraphBuilder) OnReadSlice(ctx context.Context, slice memory.Slice) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumMemReads++
b.Stats.NumMemReads++
b.memWatcher.OnReadSlice(ctx, cmdCtx, slice)
}
// OnWriteObs is called when a memory write observation becomes visible
func (b *dependencyGraphBuilder) OnWriteObs(ctx context.Context, obs []api.CmdObservation) {
cmdCtx := b.cmdCtx()
b.memWatcher.OnWriteObs(ctx, cmdCtx, obs, b.graphBuilder.GetObsNodeIDs(cmdCtx.cmdID, obs, true))
}
// OnReadObs is called when a memory read observation becomes visible
func (b *dependencyGraphBuilder) OnReadObs(ctx context.Context, obs []api.CmdObservation) {
cmdCtx := b.cmdCtx()
b.memWatcher.OnReadObs(ctx, cmdCtx, obs, b.graphBuilder.GetObsNodeIDs(cmdCtx.cmdID, obs, false))
}
// OpenForwardDependency is called to begin a forward dependency.
// See `StateWatcher.OpenForwardDependency` for an explanation of forward dependencies.
func (b *dependencyGraphBuilder) OpenForwardDependency(ctx context.Context, dependencyID interface{}) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumForwardDepOpens++
b.Stats.NumForwardDepOpens++
b.forwardWatcher.OpenForwardDependency(ctx, cmdCtx, dependencyID)
}
// CloseForwardDependency is called to end a forward dependency.
// See `StateWatcher.OpenForwardDependency` for an explanation of forward dependencies.
func (b *dependencyGraphBuilder) CloseForwardDependency(ctx context.Context, dependencyID interface{}) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumForwardDepCloses++
b.Stats.NumForwardDepCloses++
b.forwardWatcher.CloseForwardDependency(ctx, cmdCtx, dependencyID)
}
// DropForwardDependency is called to abandon a previously opened
// forward dependency, without actually adding the forward dependency.
// See `StateWatcher.OpenForwardDependency` for an explanation of forward dependencies.
func (b *dependencyGraphBuilder) DropForwardDependency(ctx context.Context, dependencyID interface{}) {
cmdCtx := b.cmdCtx()
cmdCtx.stats.NumForwardDepDrops++
b.Stats.NumForwardDepDrops++
b.forwardWatcher.DropForwardDependency(ctx, cmdCtx, dependencyID)
}
func (b *dependencyGraphBuilder) OnRecordSubCmd(ctx context.Context, recordIdx api.RecordIdx) {
cmdCtx := b.cmdCtx()
b.graphBuilder.OnRecordSubCmd(ctx, cmdCtx, recordIdx)
}
// LogStats logs some interesting stats about the graph construction
func (b *dependencyGraphBuilder) LogStats(ctx context.Context, full bool) {
log.I(ctx, "Dependency Graph Stats:")
graphStats := b.graphBuilder.GetStats()
log.I(ctx, " NumCmdNodes: %-8v NumObsNodes: %v", graphStats.NumCmdNodes, graphStats.NumObsNodes)
log.I(ctx, " Accesses:")
log.I(ctx, " NumFragReads: %-8v UniqueFragReads: %v", b.Stats.NumFragReads, graphStats.UniqueFragReads)
log.I(ctx, " NumFragWrites: %-7v UniqueFragWrites: %v", b.Stats.NumFragWrites, graphStats.UniqueFragWrites)
log.I(ctx, " NumMemReads: %-9v UniqueMemReads: %v", b.Stats.NumMemReads, graphStats.UniqueMemReads)
log.I(ctx, " NumMemWrites: %-8v UniqueMemWrites: %v", b.Stats.NumMemWrites, graphStats.UniqueMemWrites)
log.I(ctx, " NumForwardDepOpens: %-4v NumForwardDepCloses: %-4v NumForwardDepDrops: %v", b.Stats.NumForwardDepOpens, b.Stats.NumForwardDepCloses, b.Stats.NumForwardDepDrops)
log.I(ctx, " Deps:")
log.I(ctx, " NumDeps: %-15v UniqueDeps: %v", graphStats.NumDeps, graphStats.UniqueDeps)
log.I(ctx, " NumFragDeps: %-4v NumCompleteFragDeps: %-4v NumMemDeps: %v", graphStats.NumFragDeps, graphStats.NumCompleteFragDeps, graphStats.NumMemDeps)
if full {
graph := b.graphBuilder.GetGraph()
nodeIDs := make([]NodeID, len(graph.nodes))
for i := range nodeIDs {
nodeIDs[i] = (NodeID)(i)
}
sortBy := func(f func(n NodeID) uint64) {
sort.Slice(nodeIDs, func(i, j int) bool {
return f(nodeIDs[i]) > f(nodeIDs[j])
})
}
logNode := func(v uint64, n NodeID) {
var cmdStr string
if node, ok := graph.nodes[n].(CmdNode); ok {
if len(node.Index) == 1 {
cmdID := (api.CmdID)(node.Index[0])
cmd := graph.GetCommand(cmdID)
cmdStr = fmt.Sprintf("%v", cmd)
}
}
log.I(ctx, "%-9v %v %s", v, graph.nodes[n], cmdStr)
s := b.graphBuilder.GetNodeStats(n)
log.I(ctx, " Accesses:")
log.I(ctx, " NumFragReads: %-8v UniqueFragReads: %v", s.NumFragReads, s.UniqueFragReads)
log.I(ctx, " NumFragWrites: %-7v UniqueFragWrites: %v", s.NumFragWrites, s.UniqueFragWrites)
log.I(ctx, " NumMemReads: %-9v UniqueMemReads: %v", s.NumMemReads, s.UniqueMemReads)
log.I(ctx, " NumMemWrites: %-8v UniqueMemWrites: %v", s.NumMemWrites, s.UniqueMemWrites)
log.I(ctx, " NumForwardDepOpens: %-4v NumForwardDepCloses: %-4v NumForwardDepDrops: %v", s.NumForwardDepOpens, s.NumForwardDepCloses, s.NumForwardDepDrops)
log.I(ctx, " Deps:")
log.I(ctx, " NumDeps: %-15v UniqueDeps: %v", s.NumDeps, s.UniqueDeps)
log.I(ctx, " NumFragDeps: %-4v NumCompleteFragDeps: %-4v NumMemDeps: %v", s.NumFragDeps, s.NumCompleteFragDeps, s.NumMemDeps)
}
logTop := func(c uint, f func(n NodeID) uint64) {
sortBy(f)
for _, n := range nodeIDs[:c] {
logNode(f(n), n)
}
}
log.I(ctx, "Top Nodes by total accesses:")
totalAccesses := func(n NodeID) uint64 {
s := b.graphBuilder.GetNodeStats(n)
return s.NumFragReads +
s.NumFragWrites +
s.NumMemReads +
s.NumMemWrites +
s.NumForwardDepOpens +
s.NumForwardDepCloses +
s.NumForwardDepDrops
}
logTop(10, totalAccesses)
log.I(ctx, "Top Nodes by unique accesses:")
uniqueAccesses := func(n NodeID) uint64 {
s := b.graphBuilder.GetNodeStats(n)
return s.UniqueFragReads +
s.UniqueFragWrites +
s.UniqueMemReads +
s.UniqueMemWrites +
s.NumForwardDepOpens +
s.NumForwardDepCloses +
s.NumForwardDepDrops
}
logTop(10, uniqueAccesses)
}
}
func BuildDependencyGraph(ctx context.Context, config DependencyGraphConfig,
c *capture.GraphicsCapture, initialCmds []api.Cmd, initialRanges interval.U64RangeList) (DependencyGraph, error) {
ctx = status.Start(ctx, "BuildDependencyGraph")
defer status.Finish(ctx)
var state *api.GlobalState
if config.IncludeInitialCommands {
state = c.NewUninitializedState(ctx).ReserveMemory(initialRanges)
} else {
state = c.NewState(ctx)
}
b := newDependencyGraphBuilder(ctx, config, c, initialCmds, state)
mutate := func(ctx context.Context, id api.CmdID, cmd api.Cmd) error {
return cmd.Mutate(ctx, id, state, nil, b)
}
mutateD := func(ctx context.Context, id api.CmdID, cmd api.Cmd) error {
return mutate(ctx, id.Derived(), cmd)
}
err := api.ForeachCmd(ctx, initialCmds, mutateD)
if err != nil {
return nil, err
}
err = api.ForeachCmd(ctx, c.Commands, mutate)
if err != nil {
return nil, err
}
if config.ReverseDependencies {
b.graphBuilder.BuildReverseDependencies()
}
graph := b.graphBuilder.GetGraph()
b.LogStats(ctx, false)
if graph.config.SaveNodeAccesses {
graph.setStateRefs(b.fragWatcher.GetStateRefs())
}
return graph, nil
}
func (b *dependencyGraphBuilder) debug(ctx context.Context, fmt string, args ...interface{}) {
if config.DebugDependencyGraph || (len(b.cmdCtx().subCmdIdx) == 4 && b.cmdCtx().subCmdIdx[0] == 351 && b.cmdCtx().subCmdIdx[3] == 1) {
log.D(ctx, fmt, args...)
}
}
func (b *dependencyGraphBuilder) cmdCtx() CmdContext {
if len(b.subCmdStack) == 0 {
return CmdContext{}
}
return b.subCmdStack[len(b.subCmdStack)-1]
}
type Distribution struct {
SmallBins []uint64
LargeBins map[uint64]uint64
}
func (d Distribution) Add(x uint64) {
if x < uint64(len(d.SmallBins)) {
d.SmallBins[x]++
} else {
if d.LargeBins == nil {
d.LargeBins = make(map[uint64]uint64)
}
d.LargeBins[x]++
}
}
type CmdContext struct {
cmdID api.CmdID
cmd api.Cmd
subCmdIdx api.SubCmdIdx
nodeID NodeID
depth int
parentNodeID NodeID
stats *NodeStats
}