-
Notifications
You must be signed in to change notification settings - Fork 136
/
graph_builder.go
362 lines (331 loc) · 10.4 KB
/
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
// 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"
"math/bits"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/capture"
)
type GraphBuilder interface {
AddDependencies(context.Context,
map[NodeID][]FragmentAccess,
map[NodeID][]MemoryAccess,
map[NodeID][]ForwardAccess,
bool)
BuildReverseDependencies()
GetCmdNodeID(api.CmdID, api.SubCmdIdx) NodeID
GetOrCreateCmdNodeID(context.Context, api.CmdID, api.SubCmdIdx, api.Cmd) NodeID
GetObsNodeIDs(api.CmdID, []api.CmdObservation, bool) []NodeID
GetCmdContext(context.Context, api.CmdID, api.Cmd) CmdContext
GetSubCmdContext(api.CmdID, api.SubCmdIdx) CmdContext
GetNodeStats(NodeID) *NodeStats
GetStats() *GraphBuilderStats
GetGraph() *dependencyGraph
OnBeginCmd(ctx context.Context, cmdCtx CmdContext)
OnBeginSubCmd(ctx context.Context, cmdCtx CmdContext, subCmdCtx CmdContext, recordIdx api.RecordIdx)
OnRecordSubCmd(ctx context.Context, cmdCtx CmdContext, recordIdx api.RecordIdx)
}
type GraphBuilderStats struct {
UniqueFragReads uint64
UniqueFragWrites uint64
UniqueMemReads uint64
UniqueMemWrites uint64
UniqueDeps uint64
NumDeps uint64
NumFragDeps uint64
NumCompleteFragDeps uint64
NumMemDeps uint64
NumCmdNodes uint64
NumObsNodes uint64
DepDist Distribution
}
type graphBuilder struct {
pendingNodes []NodeID
nodeStats []*NodeStats
graph *dependencyGraph
stats GraphBuilderStats
isDep []bool
depSlice []NodeID
subCmdContexts map[NodeID]CmdContext
initCmdNodeIDs map[NodeID][]NodeID
recordNodes *RecordNodeTrie
state *api.GlobalState
}
func NewGraphBuilder(ctx context.Context, config DependencyGraphConfig,
c *capture.GraphicsCapture, initialCmds []api.Cmd, state *api.GlobalState) *graphBuilder {
return &graphBuilder{
graph: newDependencyGraph(ctx, config, c, initialCmds, []Node{}),
subCmdContexts: make(map[NodeID]CmdContext),
initCmdNodeIDs: make(map[NodeID][]NodeID),
recordNodes: &RecordNodeTrie{},
state: state,
}
}
// AddDependencies adds the dependencies for all pendingNodes.
func (b *graphBuilder) AddDependencies(
ctx context.Context,
fragAcc map[NodeID][]FragmentAccess,
memAcc map[NodeID][]MemoryAccess,
forwardAcc map[NodeID][]ForwardAccess,
isUnopened bool) {
for _, n := range b.pendingNodes {
cmdCtx, ok := b.subCmdContexts[n]
if !ok {
cmdCtx.cmdID = api.CmdNoID
cmdCtx.nodeID = n
cmdCtx.parentNodeID = NodeNoID
}
b.AddNodeDependencies(ctx, cmdCtx, fragAcc[n], memAcc[n], forwardAcc[n], isUnopened)
}
b.subCmdContexts = make(map[NodeID]CmdContext)
b.initCmdNodeIDs = make(map[NodeID][]NodeID)
b.pendingNodes = b.pendingNodes[:0]
}
// AddNodeDependencies adds the dependencies of a single node.
func (b *graphBuilder) AddNodeDependencies(
ctx context.Context, cmdCtx CmdContext,
fragAccesses []FragmentAccess,
memAccesses []MemoryAccess,
forwardAccesses []ForwardAccess,
isUnopened bool) {
if len(b.isDep) < len(b.graph.nodes) {
n := uint(len(b.graph.nodes))
b.isDep = make([]bool, 1<<uint(bits.Len(n-1)))
}
isDep := b.isDep
depSlice := b.depSlice
parent := cmdCtx.parentNodeID
if parent != NodeNoID {
isDep[parent] = true
depSlice = append(depSlice, parent)
}
nodeID := cmdCtx.nodeID
initCmdNodeIDs := b.initCmdNodeIDs[nodeID]
if len(initCmdNodeIDs) > 0 {
for _, initNodeID := range initCmdNodeIDs {
isDep[initNodeID] = true
depSlice = append(depSlice, initNodeID)
}
}
stats := b.nodeStats[nodeID]
for _, a := range fragAccesses {
if a.Mode&ACCESS_READ != 0 {
stats.UniqueFragReads++
}
if a.Mode&ACCESS_WRITE != 0 {
stats.UniqueFragWrites++
}
stats.NumDeps += uint64(len(a.Deps))
stats.NumFragDeps += uint64(len(a.Deps))
if _, ok := a.Fragment.(api.CompleteFragment); ok {
stats.NumCompleteFragDeps += uint64(len(a.Deps))
}
for _, d := range a.Deps {
if !isDep[d] {
isDep[d] = true
depSlice = append(depSlice, d)
}
}
}
for _, a := range memAccesses {
if a.Mode&ACCESS_READ != 0 {
stats.UniqueMemReads++
}
if a.Mode&ACCESS_WRITE != 0 {
stats.UniqueMemWrites++
}
stats.NumDeps += uint64(len(a.Deps))
stats.NumMemDeps += uint64(len(a.Deps))
for _, d := range a.Deps {
if !isDep[d] {
isDep[d] = true
depSlice = append(depSlice, d)
}
}
}
openForwardDeps := 0
for _, a := range forwardAccesses {
if a.Mode == FORWARD_OPEN {
stats.NumDeps++
d := a.Nodes.Close
if d == NodeNoID || d > nodeID {
// Forward dep going to later node.
// Dependency will be added when processing the CLOSE
openForwardDeps++
} else {
// Forward dep actually going to earlier node
// (possibly from subcommand to parent).
// Add dependency now, since dependency has already been processed.
if !isDep[d] {
isDep[d] = true
depSlice = append(depSlice, d)
}
}
} else if a.Mode == FORWARD_CLOSE && a.Nodes.Open < a.Nodes.Close {
// Close is on a later node than open,
// so dependency hasn't been added yet
b.graph.addDependency(a.Nodes.Open, a.Nodes.Close)
}
}
if isUnopened {
b.graph.addUnopenedForwardDependency(cmdCtx.cmdID)
}
newDepSlice := make([]NodeID, len(depSlice), len(depSlice)+openForwardDeps)
for i, d := range depSlice {
isDep[d] = false
newDepSlice[i] = depSlice[i]
}
b.depSlice = depSlice[:0]
b.graph.setDependencies(nodeID, newDepSlice)
if b.graph.config.SaveNodeAccesses {
initCmdNodeIDs := b.initCmdNodeIDs[nodeID]
initCmdNodeIDsCopy := make([]NodeID, len(initCmdNodeIDs))
copy(initCmdNodeIDsCopy, initCmdNodeIDs)
b.graph.setNodeAccesses(nodeID, NodeAccesses{
fragAccesses,
memAccesses,
forwardAccesses,
parent,
initCmdNodeIDsCopy,
})
}
stats.UniqueDeps = uint64(cap(newDepSlice))
b.stats.DepDist.Add(stats.UniqueDeps)
b.stats.NumDeps += stats.NumDeps
b.stats.NumFragDeps += stats.NumFragDeps
b.stats.NumCompleteFragDeps += stats.NumCompleteFragDeps
b.stats.NumMemDeps += stats.NumMemDeps
b.stats.UniqueDeps += stats.UniqueDeps
b.stats.UniqueFragReads += stats.UniqueFragReads
b.stats.UniqueFragWrites += stats.UniqueFragWrites
b.stats.UniqueMemReads += stats.UniqueMemReads
b.stats.UniqueMemWrites += stats.UniqueMemWrites
}
func (b *graphBuilder) GetCmdNodeID(cmdID api.CmdID, idx api.SubCmdIdx) NodeID {
if cmdID == api.CmdNoID {
return NodeNoID
}
return b.graph.GetCmdNodeID(cmdID, idx)
}
func (b *graphBuilder) GetOrCreateCmdNodeID(ctx context.Context, cmdID api.CmdID, idx api.SubCmdIdx, cmd api.Cmd) NodeID {
if cmdID == api.CmdNoID {
return NodeNoID
}
nodeID := b.graph.GetCmdNodeID(cmdID, idx)
if nodeID != NodeNoID {
return nodeID
}
fullIdx := append(api.SubCmdIdx{(uint64)(cmdID)}, idx...)
node := CmdNode{fullIdx, cmd.CmdFlags()}
return b.addNode(node)
}
func (b *graphBuilder) GetObsNodeIDs(cmdID api.CmdID, obs []api.CmdObservation, isWrite bool) []NodeID {
nodeIDs := make([]NodeID, len(obs))
for i, o := range obs {
nodeIDs[i] = b.addNode(ObsNode{
CmdObservation: o,
CmdID: cmdID,
IsWrite: isWrite,
Index: i,
})
}
return nodeIDs
}
func (b *graphBuilder) GetCmdContext(ctx context.Context, cmdID api.CmdID, cmd api.Cmd) CmdContext {
nodeID := b.GetOrCreateCmdNodeID(ctx, cmdID, api.SubCmdIdx{}, cmd)
stats := b.nodeStats[nodeID]
return CmdContext{cmdID, cmd, api.SubCmdIdx{}, nodeID, 0, NodeNoID, stats}
}
func (b *graphBuilder) GetSubCmdContext(cmdID api.CmdID, idx api.SubCmdIdx) CmdContext {
ancestors := b.graph.GetCmdAncestorNodeIDs(cmdID, idx)
nodeID := ancestors[len(idx)]
if nodeID == NodeNoID {
fullIdx := append(api.SubCmdIdx{(uint64)(cmdID)}, idx...)
node := CmdNode{fullIdx, api.CmdFlags(0)}
nodeID = b.addNode(node)
}
parentID := NodeNoID
for i, _ := range idx {
parentID = ancestors[len(idx)-1-i]
if parentID != NodeNoID {
break
}
}
stats := b.nodeStats[nodeID]
cmdCtx := CmdContext{cmdID, nil, idx, nodeID, 0, parentID, stats}
if parentID != NodeNoID {
if parentCtx, ok := b.subCmdContexts[parentID]; ok {
cmdCtx.cmd = parentCtx.cmd
cmdCtx.depth = parentCtx.depth + 1
}
}
return cmdCtx
}
func (b *graphBuilder) GetNodeStats(nodeID NodeID) *NodeStats {
return b.nodeStats[nodeID]
}
func (b *graphBuilder) GetStats() *GraphBuilderStats {
return &b.stats
}
func (b *graphBuilder) GetGraph() *dependencyGraph {
return b.graph
}
func (b *graphBuilder) OnBeginCmd(ctx context.Context, cmdCtx CmdContext) {
b.subCmdContexts[cmdCtx.nodeID] = cmdCtx
}
func (b *graphBuilder) OnBeginSubCmd(ctx context.Context, cmdCtx CmdContext, subCmdCtx CmdContext, recordIdx api.RecordIdx) {
if _, ok := b.subCmdContexts[subCmdCtx.nodeID]; !ok {
b.subCmdContexts[subCmdCtx.nodeID] = subCmdCtx
}
r := b.recordNodes.GetRecordNode(recordIdx)
if r != NodeNoID {
s := subCmdCtx.nodeID
b.initCmdNodeIDs[s] = append(b.initCmdNodeIDs[s], r)
}
}
func (b *graphBuilder) OnRecordSubCmd(ctx context.Context, cmdCtx CmdContext, recordIdx api.RecordIdx) {
b.recordNodes.SetRecordNode(recordIdx, cmdCtx.nodeID)
}
func (b *graphBuilder) BuildReverseDependencies() {
b.graph.buildDependenciesTo()
}
func (b *graphBuilder) addNode(node Node) NodeID {
if _, ok := node.(CmdNode); ok {
b.stats.NumCmdNodes++
}
if _, ok := node.(ObsNode); ok {
b.stats.NumObsNodes++
}
nodeID := b.graph.addNode(node)
b.pendingNodes = append(b.pendingNodes, nodeID)
newNodeStats := append(b.nodeStats, make([]*NodeStats, int(nodeID)+1-len(b.nodeStats))...)
for i := len(b.nodeStats); i < len(newNodeStats); i++ {
newNodeStats[i] = &NodeStats{}
}
b.nodeStats = newNodeStats
return nodeID
}
type RecordNodeTrie struct {
api.SubCmdIdxTrie
}
func (t *RecordNodeTrie) SetRecordNode(recordIdx api.RecordIdx, nodeID NodeID) {
t.SetValue(api.SubCmdIdx(recordIdx), nodeID)
}
func (t *RecordNodeTrie) GetRecordNode(recordIdx api.RecordIdx) NodeID {
if nodeID, ok := t.Value(api.SubCmdIdx(recordIdx)).(NodeID); ok {
return nodeID
}
return NodeNoID
}