forked from google/agi
/
transform_mapping_exporter.go
265 lines (220 loc) · 8.44 KB
/
transform_mapping_exporter.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
// Copyright (C) 2020 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 vulkan
import (
"bytes"
"context"
"fmt"
"os"
"reflect"
"sort"
"github.com/google/gapid/core/data/endian"
"github.com/google/gapid/core/log"
"github.com/google/gapid/gapir"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/api/transform"
"github.com/google/gapid/gapis/memory"
"github.com/google/gapid/gapis/replay/builder"
"github.com/google/gapid/gapis/replay/value"
"github.com/google/gapid/gapis/service"
)
var _ transform.Transform = &mappingExporter{}
type mappingHandle struct {
traceValue uint64
replayAddress value.Pointer
size uint64
name string
}
type mappingExporter struct {
mappings *map[uint64][]service.VulkanHandleMappingItem
thread uint64
path string
traceValues []mappingHandle
notificationID uint64
usedFrameBuffers map[uint64]struct{}
numOfInitialCmds uint64
}
func newMappingExporter(ctx context.Context, numOfInitialCmds uint64, mappings *map[uint64][]service.VulkanHandleMappingItem) *mappingExporter {
return &mappingExporter{
mappings: mappings,
usedFrameBuffers: map[uint64]struct{}{},
numOfInitialCmds: numOfInitialCmds,
}
}
func newMappingExporterWithPrint(ctx context.Context, path string) *mappingExporter {
mapping := make(map[uint64][]service.VulkanHandleMappingItem)
return &mappingExporter{
mappings: &mapping,
thread: 0,
path: path,
traceValues: make([]mappingHandle, 0, 0),
notificationID: 0,
}
}
func (mappingTransform *mappingExporter) RequiresAccurateState() bool {
return false
}
func (mappingTransform *mappingExporter) RequiresInnerStateMutation() bool {
return false
}
func (mappingTransform *mappingExporter) SetInnerStateMutationFunction(mutator transform.StateMutator) {
// This transform do not require inner state mutation
}
func (mappingTransform *mappingExporter) BeginTransform(ctx context.Context, inputState *api.GlobalState) error {
return nil
}
func (mappingTransform *mappingExporter) EndTransform(ctx context.Context, inputState *api.GlobalState) ([]api.Cmd, error) {
cb := CommandBuilder{Thread: mappingTransform.thread}
newCmd := cb.Custom(func(ctx context.Context, s *api.GlobalState, b *builder.Builder) error {
return mappingTransform.extractRemappings(ctx, s, b)
})
return []api.Cmd{newCmd}, nil
}
func (mappingTransform *mappingExporter) ClearTransformResources(ctx context.Context) {
// No resource allocated
}
func (mappingTransform *mappingExporter) TransformCommand(ctx context.Context, id transform.CommandID, inputCommands []api.Cmd, inputState *api.GlobalState) ([]api.Cmd, error) {
if mappingTransform.thread == 0 && len(inputCommands) > 0 {
mappingTransform.thread = inputCommands[0].Thread()
}
// This is a workaround for b/180532468.
// This does not solve the issue of mismapping due to duplication of Vulkan Handles.
// But it does reduces chance to hit the case by not mapping the framebuffer used in
// initialCmds.
if uint64(id.GetID()) >= mappingTransform.numOfInitialCmds {
for _, cmd := range inputCommands {
if queueSubmit, ok := cmd.(*VkQueueSubmit); ok {
if err := mappingTransform.recordFramebuffersInSubmittedRenderPasses(ctx, queueSubmit, inputState); err != nil {
return nil, err
}
}
}
}
return inputCommands, nil
}
// Record the framebuffers used in the renderpasses that actually submitted to GPU.
func (mappingTransform *mappingExporter) recordFramebuffersInSubmittedRenderPasses(ctx context.Context, cmd *VkQueueSubmit, inputState *api.GlobalState) error {
cmd.Extras().Observations().ApplyReads(inputState.Memory.ApplicationPool())
layout := inputState.MemoryLayout
stateObj := GetState(inputState)
submitInfos, err := cmd.PSubmits().Slice(0, uint64(cmd.SubmitCount()), layout).Read(ctx, cmd, inputState, nil)
if err != nil {
return err
}
for _, submitInfo := range submitInfos {
commandBuffers, err := submitInfo.PCommandBuffers().Slice(0, uint64(submitInfo.CommandBufferCount()), layout).Read(ctx, cmd, inputState, nil)
if err != nil {
return err
}
for _, cmdBuffer := range commandBuffers {
cmdBufferObj := GetState(inputState).CommandBuffers().Get(cmdBuffer)
for cmdIndex := 0; cmdIndex < cmdBufferObj.CommandReferences().Len(); cmdIndex++ {
currentCmd := cmdBufferObj.CommandReferences().Get(uint32(cmdIndex))
args := GetCommandArgs(ctx, currentCmd, stateObj)
if beginRenderPassArgs, ok := args.(VkCmdBeginRenderPassArgsʳ); ok {
mappingTransform.usedFrameBuffers[uint64(beginRenderPassArgs.Framebuffer())] = struct{}{}
}
}
}
}
return nil
}
func (mappingTransform *mappingExporter) extractRemappings(ctx context.Context, inputState *api.GlobalState, b *builder.Builder) error {
bufferSize := uint64(0)
for k, v := range b.Remappings {
typ := reflect.TypeOf(k)
size := memory.SizeOf(typ, inputState.MemoryLayout)
if size != 1 && size != 2 && size != 4 && size != 8 {
// Ignore objects that are not handles
continue
}
traceValue := reflect.ValueOf(k).Uint()
mappingTransform.traceValues = append(mappingTransform.traceValues, mappingHandle{traceValue: traceValue, replayAddress: v, size: size, name: typ.Name()})
bufferSize += size
}
handleBuffer := b.AllocateMemory(bufferSize)
target := handleBuffer
for _, handle := range mappingTransform.traceValues {
b.Memcpy(target, handle.replayAddress, handle.size)
target = target.Offset(handle.size)
}
mappingTransform.notificationID = b.GetNotificationID()
b.Notification(mappingTransform.notificationID, handleBuffer, bufferSize)
err := b.RegisterNotificationReader(mappingTransform.notificationID, func(n gapir.Notification) {
mappingTransform.processNotification(ctx, inputState, n)
})
if err != nil {
log.W(ctx, "Vulkan Mapping Notification could not be registered: ", err)
return err
}
return nil
}
func (mappingTransform *mappingExporter) processNotification(ctx context.Context, inputState *api.GlobalState, notification gapir.Notification) {
if mappingTransform.notificationID != notification.GetId() {
log.I(ctx, "Invalid notificationID %d", mappingTransform.notificationID)
return
}
notificationData := notification.GetData()
mappingData := notificationData.GetData()
byteOrder := inputState.MemoryLayout.GetEndian()
reader := endian.Reader(bytes.NewReader(mappingData), byteOrder)
for _, handle := range mappingTransform.traceValues {
var replayValue uint64
switch handle.size {
case 1:
replayValue = uint64(reader.Uint8())
case 2:
replayValue = uint64(reader.Uint16())
case 4:
replayValue = uint64(reader.Uint32())
case 8:
replayValue = reader.Uint64()
default:
log.F(ctx, true, "Invalid Handle size %s: %d", handle.name, handle.size)
}
if _, ok := (*mappingTransform.mappings)[replayValue]; !ok {
(*mappingTransform.mappings)[replayValue] = make([]service.VulkanHandleMappingItem, 0, 0)
}
// Eliminate the framebuffers that are not marked.
if _, ok := mappingTransform.usedFrameBuffers[handle.traceValue]; !ok && handle.name == "VkFramebuffer" {
continue
}
(*mappingTransform.mappings)[replayValue] = append(
(*mappingTransform.mappings)[replayValue],
service.VulkanHandleMappingItem{HandleType: handle.name, TraceValue: handle.traceValue, ReplayValue: replayValue})
}
mappingTransform.notificationID = 0
if len(mappingTransform.path) > 0 {
printToFile2(ctx, mappingTransform.path, mappingTransform.mappings)
}
}
func printToFile2(ctx context.Context, path string, mappings *map[uint64][]service.VulkanHandleMappingItem) {
f, err := os.Create(path)
if err != nil {
log.E(ctx, "Failed to create mapping file %v: %v", path, err)
return
}
mappingsToFile := make([]string, 0, 0)
for _, v := range *mappings {
for i := range v {
m := v[i]
mappingsToFile = append(mappingsToFile, fmt.Sprintf("%v(%v): %v\n", m.HandleType, m.TraceValue, m.ReplayValue))
}
}
sort.Strings(mappingsToFile)
for _, l := range mappingsToFile {
fmt.Fprint(f, l)
}
f.Close()
}