-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.go
305 lines (265 loc) · 8.2 KB
/
capture.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
// Copyright (C) 2017 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 capture
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"sync"
"github.com/google/gapid/core/app/status"
"github.com/google/gapid/core/data/id"
"github.com/google/gapid/core/data/protoconv"
"github.com/google/gapid/core/log"
"github.com/google/gapid/gapis/database"
"github.com/google/gapid/gapis/messages"
"github.com/google/gapid/gapis/service"
"github.com/google/gapid/gapis/service/path"
"github.com/pkg/errors"
)
// The list of captures currently imported.
// TODO: This needs to be moved to persistent storage.
var (
capturesLock sync.RWMutex
captures = []id.ID{}
)
// Capture represents data from a trace.
type Capture interface {
// Name returns the name of the capture.
Name() string
// Service returns the service.Capture description for this capture.
Service(ctx context.Context, p *path.Capture) *service.Capture
// Export exports the capture in binary format to the given writer.
Export(ctx context.Context, w io.Writer) error
}
func init() {
protoconv.Register(toProtoWrapped, fromProtoWrapped)
}
// New returns a path to a new capture stored in the database.
func New(ctx context.Context, c Capture) (*path.Capture, error) {
id, err := database.Store(ctx, wrapper{c})
if err != nil {
return nil, err
}
capturesLock.Lock()
captures = append(captures, id)
capturesLock.Unlock()
return &path.Capture{ID: path.NewID(id)}, nil
}
// Captures returns all the captures stored by the database by identifier.
func Captures() []*path.Capture {
capturesLock.RLock()
defer capturesLock.RUnlock()
out := make([]*path.Capture, len(captures))
for i, c := range captures {
out[i] = &path.Capture{ID: path.NewID(c)}
}
return out
}
// ResolveFromID resolves a single capture with the ID id.
func ResolveFromID(ctx context.Context, id id.ID) (Capture, error) {
obj, err := database.Resolve(ctx, id)
if err != nil {
return nil, log.Err(ctx, err, "Error resolving capture")
}
return obj.(wrapper).c, nil
}
// ResolveGraphicsFromID resolves a single graphics capture with the ID id.
func ResolveGraphicsFromID(ctx context.Context, id id.ID) (*GraphicsCapture, error) {
c, err := ResolveFromID(ctx, id)
if err != nil {
return nil, err
}
if gc, ok := c.(*GraphicsCapture); ok {
return gc, nil
}
return nil, errors.New("not a graphics capture")
}
// ResolvePerfettoFromID resolves a single perfetto capture with the ID id.
func ResolvePerfettoFromID(ctx context.Context, id id.ID) (*PerfettoCapture, error) {
c, err := ResolveFromID(ctx, id)
if err != nil {
return nil, err
}
if pc, ok := c.(*PerfettoCapture); ok {
return pc, nil
}
return nil, errors.New("not a Perfetto capture")
}
// ResolveFromPath resolves a single capture with the path p.
func ResolveFromPath(ctx context.Context, p *path.Capture) (Capture, error) {
return ResolveFromID(ctx, p.ID.ID())
}
// ResolveGraphicsFromPath resolves a single graphics capture with the path p.
func ResolveGraphicsFromPath(ctx context.Context, p *path.Capture) (*GraphicsCapture, error) {
return ResolveGraphicsFromID(ctx, p.ID.ID())
}
// ResolvePerfettoFromPath resolves a single perfetto capture with the path p.
func ResolvePerfettoFromPath(ctx context.Context, p *path.Capture) (*PerfettoCapture, error) {
return ResolvePerfettoFromID(ctx, p.ID.ID())
}
// Import imports the capture by name and data, and stores it in the database.
func Import(ctx context.Context, name string, key string, src Source) (*path.Capture, error) {
dataID, err := database.Store(ctx, src)
if err != nil {
return nil, fmt.Errorf("Unable to store capture data source: %v", err)
}
id, err := database.Store(ctx, &Record{
Key: key,
Name: name,
Data: dataID[:],
})
if err != nil {
return nil, err
}
capturesLock.Lock()
captures = append(captures, id)
capturesLock.Unlock()
return &path.Capture{ID: path.NewID(id)}, nil
}
// Export encodes the given capture and associated resources
// and writes it to the supplied io.Writer in the pack file format,
// producing output suitable for use with Import or opening in the trace editor.
func Export(ctx context.Context, p *path.Capture, w io.Writer) error {
c, err := ResolveFromPath(ctx, p)
if err != nil {
return err
}
return c.Export(ctx, w)
}
// Source represents the source of capture data.
type Source interface {
// ReadCloser returns an io.ReadCloser instance, from which capture data
// can be read and closed after reading.
ReadCloser() (io.ReadCloser, error)
// Size returns the total size in bytes of this source.
Size() (uint64, error)
}
// blobReadCloser implements the Source interface, it represents the capture
// data in raw byte form.
type blobReadCloser struct {
*bytes.Reader
}
// Close implements the io.ReadCloser interface.
func (blobReadCloser) Close() error { return nil }
// ReadCloser implements the Source interface.
func (b *Blob) ReadCloser() (io.ReadCloser, error) {
return blobReadCloser{bytes.NewReader(b.GetData())}, nil
}
// Size implements the Source interface.
func (b *Blob) Size() (uint64, error) {
return uint64(len(b.GetData())), nil
}
// ReadCloser implements the Source interface.
func (f *File) ReadCloser() (io.ReadCloser, error) {
o, err := os.Open(f.GetPath())
if err != nil {
return nil, &service.ErrDataUnavailable{
Reason: messages.ErrFileCannotBeRead(),
}
}
return o, nil
}
// Size implements the Source interface.
func (f *File) Size() (uint64, error) {
fi, err := os.Stat(f.GetPath())
if err != nil {
return 0, err
}
return uint64(fi.Size()), nil
}
func toProto(ctx context.Context, c Capture) (*Record, error) {
buf := bytes.Buffer{}
if err := c.Export(ctx, &buf); err != nil {
return nil, err
}
id, err := database.Store(ctx, buf.Bytes())
if err != nil {
return nil, fmt.Errorf("Unable to store capture data: %v", err)
}
return &Record{
Name: c.Name(),
Data: id[:],
}, nil
}
type loggingRC struct {
onProgress func(uint64)
total uint64
rc io.ReadCloser
}
// Read implements the io.Reader interface.
func (l *loggingRC) Read(p []byte) (n int, err error) {
n, err = l.rc.Read(p)
l.total += uint64(n)
l.onProgress(l.total)
return n, err
}
// Close implements the io.Closer interface.
func (l *loggingRC) Close() error {
return l.rc.Close()
}
func open(ctx context.Context, src Source) (r *bufio.Reader, close func() error, err error) {
in, err := src.ReadCloser()
if err != nil {
return nil, nil, err
}
if size, err := src.Size(); err == nil {
in = &loggingRC{
onProgress: func(p uint64) { status.UpdateProgress(ctx, p, size) },
rc: in,
}
}
return bufio.NewReader(in), in.Close, nil
}
func fromProto(ctx context.Context, r *Record) (Capture, error) {
ctx = status.Start(ctx, "Loading capture '%v'", r.Name)
defer status.Finish(ctx)
var dataID id.ID
copy(dataID[:], r.Data)
data, err := database.Resolve(ctx, dataID)
if err != nil {
return nil, fmt.Errorf("Unable to load capture data source: %v", err)
}
src, ok := data.(Source)
if !ok {
return nil, fmt.Errorf("Unable to load capture data source: Failed to resolve capture.Source")
}
in, close, err := open(ctx, src)
if err != nil {
return nil, err
}
defer close()
switch {
case isGFXTraceFormat(in):
return deserializeGFXTrace(ctx, r, in)
case isPerfettoTraceFormat(in):
return deserializePerfettoTrace(ctx, r, in)
default:
return nil, fmt.Errorf("Not a recognized capture format")
}
}
// wrapper wraps a Capture. This is needed because protoconv, which is used by
// the database, doesn't support interfaces.
type wrapper struct {
c Capture
}
func toProtoWrapped(ctx context.Context, w wrapper) (*Record, error) {
return toProto(ctx, w.c)
}
func fromProtoWrapped(ctx context.Context, r *Record) (wrapper, error) {
c, err := fromProto(ctx, r)
return wrapper{c}, err
}