-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping_exporter.go
202 lines (170 loc) · 5.56 KB
/
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
// Copyright (C) 2019 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 replay
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"
)
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
}
func NewMappingExporter(ctx context.Context, mappings *map[uint64][]service.VulkanHandleMappingItem) *MappingExporter {
return &MappingExporter{
mappings: mappings,
thread: 0,
path: "",
traceValues: make([]mappingHandle, 0, 0),
notificationID: 0,
}
}
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 (m *MappingExporter) processNotification(ctx context.Context, s *api.GlobalState, n gapir.Notification) {
if m.notificationID != n.GetId() {
log.I(ctx, "Invalid notificationID %d", m.notificationID)
return
}
notificationData := n.GetData()
mappingData := notificationData.GetData()
byteOrder := s.MemoryLayout.GetEndian()
r := endian.Reader(bytes.NewReader(mappingData), byteOrder)
for _, handle := range m.traceValues {
var replayValue uint64
switch handle.size {
case 1:
replayValue = uint64(r.Uint8())
case 2:
replayValue = uint64(r.Uint16())
case 4:
replayValue = uint64(r.Uint32())
case 8:
replayValue = r.Uint64()
default:
log.F(ctx, true, "Invalid Handle size %s: %d", handle.name, handle.size)
}
if _, ok := (*m.mappings)[replayValue]; !ok {
(*m.mappings)[replayValue] = make([]service.VulkanHandleMappingItem, 0, 0)
}
(*m.mappings)[replayValue] = append(
(*m.mappings)[replayValue],
service.VulkanHandleMappingItem{HandleType: handle.name, TraceValue: handle.traceValue, ReplayValue: replayValue},
)
}
m.notificationID = 0
if len(m.path) > 0 {
printToFile(ctx, m.path, m.mappings)
}
}
func (m *MappingExporter) ExtractRemappings(ctx context.Context, s *api.GlobalState, b *builder.Builder) error {
bufferSize := uint64(0)
for k, v := range b.Remappings {
typ := reflect.TypeOf(k)
size := memory.SizeOf(typ, s.MemoryLayout)
if size != 1 && size != 2 && size != 4 && size != 8 {
// Ignore objects that are not handles
continue
}
traceValue := reflect.ValueOf(k).Uint()
m.traceValues = append(m.traceValues, mappingHandle{traceValue: traceValue, replayAddress: v, size: size, name: typ.Name()})
bufferSize += size
}
handleBuffer := b.AllocateMemory(bufferSize)
target := handleBuffer
for _, handle := range m.traceValues {
b.Memcpy(target, handle.replayAddress, handle.size)
target = target.Offset(handle.size)
}
m.notificationID = b.GetNotificationID()
b.Notification(m.notificationID, handleBuffer, bufferSize)
err := b.RegisterNotificationReader(m.notificationID, func(n gapir.Notification) {
m.processNotification(ctx, s, n)
})
if err != nil {
log.W(ctx, "Vulkan Mapping Notification could not be registered: ", err)
return err
}
return nil
}
func (m *MappingExporter) Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, out transform.Writer) error {
m.thread = cmd.Thread()
return out.MutateAndWrite(ctx, id, cmd)
}
func printToFile(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()
}
func (m *MappingExporter) Flush(ctx context.Context, out transform.Writer) error {
return out.MutateAndWrite(ctx, api.CmdNoID, Custom{m.thread,
func(ctx context.Context, s *api.GlobalState, b *builder.Builder) error {
return m.ExtractRemappings(ctx, s, b)
},
})
}
func (m *MappingExporter) PreLoop(ctx context.Context, out transform.Writer) {}
func (m *MappingExporter) PostLoop(ctx context.Context, out transform.Writer) {
out.MutateAndWrite(ctx, api.CmdNoID, Custom{m.thread,
func(ctx context.Context, s *api.GlobalState, b *builder.Builder) error {
return m.ExtractRemappings(ctx, s, b)
},
})
}
func (t *MappingExporter) BuffersCommands() bool {
return false
}