/
server.go
954 lines (841 loc) · 27.9 KB
/
server.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
// 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 server implements the rpc gpu debugger service, queriable by the
// clients, along with some helpers exposed via an http listener.
package server
import (
"bytes"
"context"
"fmt"
"io"
"os"
"runtime"
"runtime/pprof"
"sync"
"sync/atomic"
"time"
"github.com/google/gapid/core/app/crash"
"github.com/google/gapid/core/app/crash/reporting"
"github.com/google/gapid/core/context/keys"
"github.com/google/gapid/core/app"
"github.com/google/gapid/core/app/analytics"
"github.com/google/gapid/core/app/auth"
"github.com/google/gapid/core/app/benchmark"
"github.com/google/gapid/core/app/status"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/android/adb"
"github.com/google/gapid/core/os/device/bind"
"github.com/google/gapid/core/os/file"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/capture"
"github.com/google/gapid/gapis/config"
"github.com/google/gapid/gapis/messages"
perfetto "github.com/google/gapid/gapis/perfetto/service"
"github.com/google/gapid/gapis/replay"
"github.com/google/gapid/gapis/replay/devices"
"github.com/google/gapid/gapis/resolve"
"github.com/google/gapid/gapis/resolve/dependencygraph2"
"github.com/google/gapid/gapis/resolve/dependencygraph2/graph_visualization"
"github.com/google/gapid/gapis/service"
"github.com/google/gapid/gapis/service/path"
"github.com/google/gapid/gapis/stringtable"
"github.com/google/gapid/gapis/trace"
// Register all the apis
_ "github.com/google/gapid/gapis/api/all"
)
const (
FILE_SIZE_LIMIT_IN_BYTES = 2147483647
)
// Config holds the server configuration settings.
type Config struct {
Info *service.ServerInfo
StringTables []*stringtable.StringTable
EnableLocalFiles bool
PreloadDepGraph bool
AuthToken auth.Token
DeviceScanDone task.Signal
LogBroadcaster *log.Broadcaster
IdleTimeout time.Duration
}
// Server is the server interface to GAPIS.
type Server interface {
service.Service
}
// New constructs and returns a new Server.
func New(ctx context.Context, cfg Config) Server {
return &server{
cfg.Info,
cfg.StringTables,
cfg.EnableLocalFiles,
cfg.PreloadDepGraph,
cfg.DeviceScanDone,
cfg.LogBroadcaster,
}
}
type server struct {
info *service.ServerInfo
stbs []*stringtable.StringTable
enableLocalFiles bool
preloadDepGraph bool
deviceScanDone task.Signal
logBroadcaster *log.Broadcaster
}
func (s *server) Ping(ctx context.Context) error {
return nil
}
func (s *server) GetServerInfo(ctx context.Context) (*service.ServerInfo, error) {
ctx = status.Start(ctx, "RPC GetServerInfo")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetServerInfo")
return s.info, nil
}
func (s *server) CheckForUpdates(ctx context.Context, includeDevReleases bool) (*service.Releases, error) {
ctx = status.Start(ctx, "RPC CheckForUpdates")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "CheckForUpdates")
return checkForUpdates(ctx, includeDevReleases)
}
func (s *server) GetAvailableStringTables(ctx context.Context) ([]*stringtable.Info, error) {
ctx = status.Start(ctx, "RPC GetAvailableStringTables")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetAvailableStringTables")
infos := make([]*stringtable.Info, len(s.stbs))
for i, table := range s.stbs {
infos[i] = table.Info
}
return infos, nil
}
func (s *server) GetStringTable(ctx context.Context, info *stringtable.Info) (*stringtable.StringTable, error) {
ctx = status.Start(ctx, "RPC GetStringTable")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetStringTable")
for _, table := range s.stbs {
if table.Info.CultureCode == info.CultureCode {
return table, nil
}
}
return nil, fmt.Errorf("String table not found")
}
func (s *server) ImportCapture(ctx context.Context, name string, data []uint8) (*path.Capture, error) {
ctx = status.Start(ctx, "RPC ImportCapture")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "ImportCapture")
src := &capture.Blob{Data: data}
p, err := capture.Import(ctx, name, name, src)
if err != nil {
return nil, err
}
// Ensure the capture can be read by resolving it now.
if _, err = capture.ResolveFromPath(ctx, p); err != nil {
return nil, err
}
return p, nil
}
func (s *server) ExportCapture(ctx context.Context, c *path.Capture) ([]byte, error) {
ctx = status.Start(ctx, "RPC ExportCapture")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "ExportCapture")
b := bytes.Buffer{}
if err := capture.Export(ctx, c, &b); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// ReadFile exists because ioutil.ReadFile is broken on Windows.
// https://github.com/golang/go/issues/26923
func ReadFile(f *os.File) ([]byte, error) {
in := []byte{}
buff := [1024 * 1024 * 1024]byte{}
for {
n, err := f.Read(buff[:])
if err != nil && err != io.EOF {
return nil, err
}
in = append(in, buff[:n]...)
if err == io.EOF {
break
}
}
return in, nil
}
func (s *server) LoadCapture(ctx context.Context, path string) (*path.Capture, error) {
ctx = status.Start(ctx, "RPC LoadCapture")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "LoadCapture")
if !s.enableLocalFiles {
return nil, fmt.Errorf("Server not configured to allow reading of local files")
}
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
name := fileInfo.Name()
// Create a key to prevent name collusion between traces.
key := fmt.Sprintf("%v%v%v", name, fileInfo.Size(), fileInfo.ModTime().Unix())
src := &capture.File{Path: path}
p, err := capture.Import(ctx, key, name, src)
if err != nil {
return nil, err
}
// Ensure the capture can be read by resolving it now.
c, err := capture.ResolveFromPath(ctx, p)
if err != nil {
return nil, err
}
// For graphics capture, pre-resolve the dependency graph.
if c.Service(ctx, p).Type == service.TraceType_Graphics && !config.DisableDeadCodeElimination && s.preloadDepGraph {
newCtx := keys.Clone(context.Background(), ctx)
crash.Go(func() {
cctx := status.PutTask(newCtx, nil)
cctx = status.StartBackground(cctx, "Precaching Dependency Graph")
defer status.Finish(cctx)
var err error
cfg := dependencygraph2.DependencyGraphConfig{
MergeSubCmdNodes: true,
IncludeInitialCommands: false,
}
_, err = dependencygraph2.GetDependencyGraph(cctx, p, cfg)
if err != nil {
log.E(newCtx, "Error resolve dependency graph: %v", err)
}
})
}
return p, nil
}
func (s *server) SaveCapture(ctx context.Context, c *path.Capture, path string) error {
ctx = status.Start(ctx, "RPC SaveCapture")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "SaveCapture")
if !s.enableLocalFiles {
return fmt.Errorf("Server not configured to allow writing of local files")
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer f.Close()
return capture.Export(ctx, c, f)
}
func (s *server) ExportReplay(ctx context.Context, c *path.Capture, d *path.Device, out string, opts *service.ExportReplayOptions) error {
ctx = status.Start(ctx, "RPC ExportReplay")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "ExportReplay")
if !s.enableLocalFiles {
return fmt.Errorf("Server not configured to allow writing of local files")
}
return exportReplay(ctx, c, d, out, opts)
}
func (s *server) DCECapture(ctx context.Context, p *path.Capture, requested []*path.Command) (*path.Capture, error) {
ctx = log.Enter(ctx, "DCECapture")
c, err := capture.ResolveFromPath(ctx, p)
if err != nil {
return nil, err
}
trimmed, err := dependencygraph2.DCECapture(ctx, c.Name()+"_dce", p, requested)
if err != nil {
return nil, err
}
return trimmed, nil
}
func (s *server) SplitCapture(ctx context.Context, rng *path.Commands) (*path.Capture, error) {
ctx = log.Enter(ctx, "SplitCapture")
c, err := capture.ResolveGraphicsFromPath(ctx, rng.Capture)
if err != nil {
return nil, err
}
var from, to int
if len(rng.From) > 0 {
from = int(rng.From[0])
}
if len(rng.To) > 0 {
to = int(rng.To[0])
}
if from >= len(c.Commands) || to > len(c.Commands) || (to != 0 && to <= from) {
return nil, fmt.Errorf("Invalid range: [%d, %d) not in [%d, %d)", from, to, 0, len(c.Commands))
} else if to == 0 {
to = len(c.Commands)
}
name := fmt.Sprintf("%s [%d,%d)", c.Name(), from, to)
if gc, err := capture.NewGraphicsCapture(ctx, name, c.Header, c.InitialState, c.Commands[from:to]); err != nil {
return nil, err
} else {
return capture.New(ctx, gc)
}
}
func (s *server) TrimCaptureInitialState(ctx context.Context, p *path.Capture) (*path.Capture, error) {
ctx = log.Enter(ctx, "TrimCaptureInitialState")
c, err := capture.ResolveGraphicsFromPath(ctx, p)
if err != nil {
return nil, err
}
initialState := c.CloneInitialState()
for _, state := range initialState.APIs {
state.TrimInitialState(ctx, p)
}
newCapture, err := capture.NewGraphicsCapture(ctx, c.Name(), c.Header, initialState, c.Commands)
if err != nil {
return nil, err
}
return newCapture.Path(ctx)
}
func (s *server) GetGraphVisualization(ctx context.Context, p *path.Capture, format service.GraphFormat) ([]byte, error) {
ctx = status.Start(ctx, "RPC GetGraphVisualization")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetGraphVisualization")
graphVisualization, err := graph_visualization.GetGraphVisualizationFromCapture(ctx, p, format)
if err != nil {
return []byte{}, err
}
if len(graphVisualization) > FILE_SIZE_LIMIT_IN_BYTES {
return []byte{}, log.Errf(ctx, err, "The file size for graph visualization exceeds %d bytes", FILE_SIZE_LIMIT_IN_BYTES)
}
return graphVisualization, nil
}
func (s *server) GetDevices(ctx context.Context) ([]*path.Device, error) {
ctx = status.Start(ctx, "RPC GetDevices")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetDevices")
s.deviceScanDone.Wait(ctx)
devices := bind.GetRegistry(ctx).Devices()
paths := make([]*path.Device, len(devices))
for i, d := range devices {
paths[i] = path.NewDevice(d.Instance().ID.ID())
}
return paths, nil
}
type prioritizedDevice struct {
device bind.Device
priority uint32
}
type prioritizedDevices []prioritizedDevice
func (p prioritizedDevices) Len() int {
return len(p)
}
func (p prioritizedDevices) Less(i, j int) bool {
return p[i].priority < p[j].priority
}
func (p prioritizedDevices) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (s *server) GetDevicesForReplay(ctx context.Context, p *path.Capture) ([]*path.Device, []bool, []*stringtable.Msg, error) {
ctx = status.Start(ctx, "RPC GetDevicesForReplay")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetDevicesForReplay")
s.deviceScanDone.Wait(ctx)
return devices.ForReplay(ctx, p)
}
func (s *server) Get(ctx context.Context, p *path.Any, c *path.ResolveConfig) (interface{}, error) {
ctx = status.Start(ctx, "RPC Get<%v>", p)
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Get")
if err := p.Validate(); err != nil {
return nil, log.Errf(ctx, err, "Invalid path: %v", p)
}
v, err := resolve.Get(ctx, p, c)
if err != nil {
return nil, err
}
return v, nil
}
func (s *server) Set(ctx context.Context, p *path.Any, v interface{}, r *path.ResolveConfig) (*path.Any, error) {
ctx = status.Start(ctx, "RPC Set<%v>", p)
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Set")
if err := p.Validate(); err != nil {
return nil, log.Errf(ctx, err, "Invalid path: %v", p)
}
return resolve.Set(ctx, p, v, r)
}
func (s *server) Delete(ctx context.Context, p *path.Any, r *path.ResolveConfig) (*path.Any, error) {
ctx = status.Start(ctx, "RPC Delete<%v>", p)
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Delete")
if err := p.Validate(); err != nil {
return nil, log.Errf(ctx, err, "Invalid path: %v", p)
}
return resolve.Delete(ctx, p, r)
}
func (s *server) Follow(ctx context.Context, p *path.Any, r *path.ResolveConfig) (*path.Any, error) {
ctx = status.Start(ctx, "RPC Follow")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Follow")
if err := p.Validate(); err != nil {
return nil, log.Errf(ctx, err, "Invalid path: %v", p)
}
return resolve.Follow(ctx, p, r)
}
func (s *server) GetLogStream(ctx context.Context, handler log.Handler) error {
ctx = status.StartBackground(ctx, "RPC GetLogStream")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetLogStream")
closed := make(chan struct{})
handler = log.OnClosed(handler, func() { close(closed) })
handler = log.Channel(handler, 64)
unregister := s.logBroadcaster.Listen(handler)
defer unregister()
select {
case <-closed:
// Logs were closed - likely server is shutting down.
case <-task.ShouldStop(ctx):
// Context was stopped - likely client has disconnected.
}
return task.StopReason(ctx)
}
func (s *server) Find(ctx context.Context, req *service.FindRequest, handler service.FindHandler) error {
ctx = status.Start(ctx, "RPC Find")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Find")
return resolve.Find(ctx, req, handler)
}
func (s *server) Profile(ctx context.Context, pprofW, traceW io.Writer, memorySnapshotInterval uint32) (stop func() error, err error) {
ctx = status.Start(ctx, "RPC Profile")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Profile")
stop = task.Async(ctx, func(ctx context.Context) error {
// flush is a list of functions that need to be called once the function
// returns.
flush := []func(){}
defer func() {
for _, f := range flush {
f()
}
}()
if pprofW != nil {
// Pprof CPU profiling requested.
if err := pprof.StartCPUProfile(pprofW); err != nil {
return err
}
flush = append(flush, pprof.StopCPUProfile)
}
if traceW != nil {
// Chrome trace data requested.
stop := status.RegisterTracer(traceW)
flush = append(flush, stop)
}
// Poll the memory. This will block until the context is cancelled.
msi := time.Second * time.Duration(memorySnapshotInterval)
return task.Poll(ctx, msi, func(ctx context.Context) error {
status.SnapshotMemory(ctx)
return nil
})
})
return stop, nil
}
type statusListener struct {
f func(*service.TaskUpdate)
m func(*service.MemoryStatus)
r func(*service.ReplayUpdate)
lastProgressUpdate map[*status.Task]time.Time // guarded by progressMutex
progressUpdateFreq time.Duration // guarded by progressMutex
progressMutex sync.Mutex
}
func (l *statusListener) OnTaskStart(ctx context.Context, task *status.Task) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
l.f(&service.TaskUpdate{
Status: service.TaskStatus_STARTING,
Id: task.ID(),
Parent: task.ParentID(),
Name: task.Name(),
CompletePercent: 0,
Background: task.Background(),
})
l.lastProgressUpdate[task] = time.Now()
}
func (l *statusListener) OnTaskProgress(ctx context.Context, task *status.Task) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
if t, ok := l.lastProgressUpdate[task]; ok {
if time.Since(t) > l.progressUpdateFreq || int32(task.Completion()) == 100 {
l.f(&service.TaskUpdate{
Status: service.TaskStatus_PROGRESS,
Id: task.ID(),
Parent: task.ParentID(),
Name: task.Name(),
CompletePercent: int32(task.Completion()),
Background: task.Background(),
})
l.lastProgressUpdate[task] = time.Now()
}
}
}
func (l *statusListener) OnTaskBlock(ctx context.Context, task *status.Task) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
l.f(&service.TaskUpdate{
Status: service.TaskStatus_BLOCKED,
Id: task.ID(),
Parent: task.ParentID(),
Name: task.Name(),
CompletePercent: int32(task.Completion()),
Background: task.Background(),
})
}
func (l *statusListener) OnTaskUnblock(ctx context.Context, task *status.Task) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
l.f(&service.TaskUpdate{
Status: service.TaskStatus_UNBLOCKED,
Id: task.ID(),
Parent: task.ParentID(),
Name: task.Name(),
CompletePercent: int32(task.Completion()),
Background: task.Background(),
})
}
func (l *statusListener) OnTaskFinish(ctx context.Context, task *status.Task) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
l.f(&service.TaskUpdate{
Status: service.TaskStatus_FINISHED,
Id: task.ID(),
Parent: task.ParentID(),
Name: task.Name(),
CompletePercent: 100,
Background: task.Background(),
})
delete(l.lastProgressUpdate, task)
}
func (l *statusListener) OnEvent(ctx context.Context, task *status.Task, event string, scope status.EventScope) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
if task != nil {
l.f(&service.TaskUpdate{
Status: service.TaskStatus_EVENT,
Id: task.ID(),
Parent: task.ParentID(),
Name: task.Name(),
CompletePercent: int32(task.Completion()),
Background: task.Background(),
Event: event,
})
} else {
l.f(&service.TaskUpdate{
Status: service.TaskStatus_EVENT,
Id: 0,
Parent: 0,
Name: "Global",
CompletePercent: 0,
Background: false,
Event: event,
})
}
}
func (l *statusListener) OnMemorySnapshot(ctx context.Context, stats runtime.MemStats) {
l.progressMutex.Lock()
defer l.progressMutex.Unlock()
l.m(&service.MemoryStatus{
TotalHeap: stats.Alloc,
})
}
func (l *statusListener) OnReplayStatusUpdate(ctx context.Context, r *status.Replay, label uint64, totalInstrs, finishedInstrs uint32) {
// Not using lock here for efficiency. Because replay status update is of high frequency.
// l.progressMutex.Lock()
// defer l.progressMutex.Unlock()
device := path.NewDevice(r.Device)
started, finished := r.Started(), r.Finished()
var status service.ReplayStatus
switch {
case finished:
status = service.ReplayStatus_REPLAY_FINISHED
case totalInstrs > 0:
status = service.ReplayStatus_REPLAY_EXECUTING
case started:
status = service.ReplayStatus_REPLAY_STARTED
default:
status = service.ReplayStatus_REPLAY_QUEUED
}
l.r(&service.ReplayUpdate{
ReplayId: r.ID,
Device: device,
Status: status,
Label: label,
TotalInstrs: totalInstrs,
FinishedInstrs: finishedInstrs,
})
}
func (s *server) Status(ctx context.Context, snapshotInterval, statusInterval time.Duration, f func(*service.TaskUpdate), m func(*service.MemoryStatus), r func(*service.ReplayUpdate)) error {
ctx = status.StartBackground(ctx, "RPC Status")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "Status")
l := &statusListener{f, m, r, make(map[*status.Task]time.Time), statusInterval, sync.Mutex{}}
unregister := status.RegisterListener(l)
defer unregister()
if snapshotInterval > 0 {
// Poll the memory. This will block until the context is cancelled.
stopSnapshot := task.Async(ctx, func(ctx context.Context) error {
return task.Poll(ctx, snapshotInterval, func(ctx context.Context) error {
status.SnapshotMemory(ctx)
return nil
})
})
defer stopSnapshot()
}
select {
case <-task.ShouldStop(ctx):
}
return task.StopReason(ctx)
}
func (s *server) GetPerformanceCounters(ctx context.Context) (string, error) {
ctx = status.Start(ctx, "RPC GetPerformanceCounters")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetPerformanceCounters")
return fmt.Sprintf("%+v", benchmark.GlobalCounters.All()), nil
}
func (s *server) GetProfile(ctx context.Context, name string, debug int32) ([]byte, error) {
ctx = status.Start(ctx, "RPC GetProfile")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetProfile")
p := pprof.Lookup(name)
if p == nil {
return []byte{}, fmt.Errorf("Profile not found: %s", name)
}
var b bytes.Buffer
if err := p.WriteTo(&b, int(debug)); err != nil {
return []byte{}, err
}
return b.Bytes(), nil
}
func (s *server) ClientEvent(ctx context.Context, req *service.ClientEventRequest) error {
if i := req.GetInteraction(); i != nil {
analytics.SendEvent("client", i.View, i.Action.String())
}
return nil
}
func (s *server) TraceTargetTreeNode(ctx context.Context, req *service.TraceTargetTreeNodeRequest) (*service.TraceTargetTreeNode, error) {
tttn, err := trace.TraceTargetTreeNode(ctx, *req.Device, req.Uri, req.Density)
if err != nil {
return nil, err
}
return &service.TraceTargetTreeNode{
Name: tttn.Name,
Icon: tttn.Icon,
Uri: tttn.URI,
ParentUri: tttn.Parent,
ChildrenUris: tttn.Children,
TraceUri: tttn.TraceURI,
FriendlyApplication: tttn.ApplicationName,
FriendlyExecutable: tttn.ExecutableName,
}, nil
}
func (s *server) FindTraceTargets(ctx context.Context, req *service.FindTraceTargetsRequest) ([]*service.TraceTargetTreeNode, error) {
nodes, err := trace.FindTraceTargets(ctx, *req.Device, req.Uri)
if err != nil {
return nil, err
}
out := make([]*service.TraceTargetTreeNode, len(nodes))
for i, n := range nodes {
out[i] = &service.TraceTargetTreeNode{
Name: n.Name,
Icon: n.Icon,
Uri: n.URI,
ParentUri: n.Parent,
ChildrenUris: n.Children,
TraceUri: n.TraceURI,
FriendlyApplication: n.ApplicationName,
FriendlyExecutable: n.ExecutableName,
}
}
return out, nil
}
// traceHandler implements the TraceHandler interface
// It wraps all of the state for a trace operation
type traceHandler struct {
initialized bool // Has the trace been initialized yet
started bool // Has the trace been started yet
done bool // Has the trace been finished
err error // Was there an error to report at next request
bytesWritten int64 // How many bytes have been written so far
startSignal task.Signal // If we are in MEC this signal will start the trace
startFunc task.Task // This is the function to go with the above signal.
stopFunc task.Task // stopFunc can be used to stop/clean up the trace
doneSignal task.Signal // doneSignal can be waited on to make sure the trace is actually done
doneSignalFunc task.Task // doneSignalFunc is called when tracing finished normally
}
func (r *traceHandler) Initialize(ctx context.Context, opts *service.TraceOptions) (*service.StatusResponse, error) {
if r.initialized {
return nil, log.Errf(ctx, nil, "Error initialize a running trace")
}
r.initialized = true
stopSignal, stopFunc := task.NewSignal()
readyFunc := task.Noop()
r.stopFunc = stopFunc
go func() {
r.err = trace.Trace(ctx, opts.Device, r.startSignal, stopSignal, readyFunc, opts, &r.bytesWritten)
r.done = true
r.doneSignalFunc(ctx)
}()
stat := service.TraceStatus_Initializing
if !opts.DeferStart {
r.started = true
}
resp := &service.StatusResponse{
BytesCaptured: 0,
Status: stat,
}
return resp, nil
}
func (r *traceHandler) Event(ctx context.Context, req service.TraceEvent) (*service.StatusResponse, error) {
if !r.initialized {
return nil, log.Errf(ctx, nil, "Cannot get/change status of an uninitialized trace")
}
if r.err != nil {
return nil, log.Errf(ctx, r.err, "Tracing Failed")
}
switch req {
case service.TraceEvent_Begin:
if r.started {
return nil, log.Errf(ctx, nil, "Invalid to start an already running trace")
}
r.startFunc(ctx)
r.started = true
case service.TraceEvent_Stop:
if !r.started {
return nil, log.Errf(ctx, nil, "Cannot end a trace that was not started")
}
r.stopFunc(ctx)
r.doneSignal.Wait(ctx)
case service.TraceEvent_Status:
// intentionally empty
}
status := service.TraceStatus_Uninitialized
bytes := atomic.LoadInt64(&r.bytesWritten)
if r.initialized {
if bytes == 0 {
status = service.TraceStatus_Initializing
} else {
status = service.TraceStatus_WaitingToStart
}
}
if r.started {
// TODO: this is not a good way to signal that we are ready to capture.
if bytes == 0 {
status = service.TraceStatus_Initializing
} else {
status = service.TraceStatus_Capturing
}
}
if r.done {
status = service.TraceStatus_Done
}
resp := &service.StatusResponse{
BytesCaptured: atomic.LoadInt64(&r.bytesWritten),
Status: status,
}
return resp, nil
}
func (r *traceHandler) Dispose(ctx context.Context) {
if !r.done {
r.stopFunc(ctx)
}
return
}
func (s *server) Trace(ctx context.Context) (service.TraceHandler, error) {
startSignal, startFunc := task.NewSignal()
doneSignal, doneSigFunc := task.NewSignal()
return &traceHandler{
startSignal: startSignal,
startFunc: startFunc,
doneSignal: doneSignal,
doneSignalFunc: doneSigFunc,
stopFunc: doneSigFunc,
}, nil
}
func (s *server) UpdateSettings(ctx context.Context, settings *service.UpdateSettingsRequest) error {
if settings.EnableAnalytics {
analytics.Enable(ctx, settings.ClientId, analytics.AppVersion{
Name: app.Name, Build: app.Version.Build,
Major: app.Version.Major, Minor: app.Version.Minor, Point: app.Version.Point,
})
} else {
analytics.Disable()
}
if settings.EnableCrashReporting {
reporting.Enable(ctx, app.Name, app.Version.String())
} else {
reporting.Disable()
}
if settings.Adb != "" {
adb.ADB = file.Abs(settings.Adb)
}
return nil
}
func (s *server) GetTimestamps(ctx context.Context, req *service.GetTimestampsRequest, h service.TimeStampsHandler) error {
ctx = status.Start(ctx, "RPC GetTimestamps")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GetTimestamps")
return replay.GetTimestamps(ctx, req.Capture, req.Device, h)
}
func (s *server) GpuProfile(ctx context.Context, req *service.GpuProfileRequest) (*service.ProfilingData, error) {
ctx = status.Start(ctx, "RPC GpuProfile")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "GpuProfile")
var replayErr, analysisErr error
var result *service.ProfilingData
var wg sync.WaitGroup
// TODO(pmuetschard): sending the data across via this channel is a bit hacky, but the best option
// at this point. When we clean-up the multi-API stuff, we can probably make this go away.
staticAnalysisResult := make(chan *api.StaticAnalysisProfileData, 1)
wg.Add(1)
go func() {
defer wg.Done()
result, replayErr = replay.GpuProfile(ctx, staticAnalysisResult, req.Capture, req.Device, req.Experiments, req.LoopCount)
}()
wg.Add(1)
go func() {
defer wg.Done()
var analysisResult *api.StaticAnalysisProfileData
analysisResult, analysisErr = resolve.ProfileStaticAnalysis(ctx, req.Capture)
staticAnalysisResult <- analysisResult
}()
wg.Wait()
if replayErr != nil {
return nil, replayErr
} else if analysisErr != nil {
return nil, analysisErr
}
return result, nil
}
func (s *server) PerfettoQuery(ctx context.Context, c *path.Capture, query string) (*perfetto.QueryResult, error) {
ctx = status.Start(ctx, "RPC PerfettoQuery")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "PerfettoQuery")
p, err := capture.ResolvePerfettoFromPath(ctx, c)
if err != nil {
return nil, err
}
res, err := p.Processor.Query(query)
if err != nil {
return nil, err
}
return res, nil
}
func (s *server) ValidateDevice(ctx context.Context, d *path.Device) (*service.DeviceValidationResult, error) {
ctx = status.Start(ctx, "RPC ValidateDevice")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "ValidateDevice")
return trace.Validate(ctx, d, s.enableLocalFiles)
}
func (s *server) InstallApp(ctx context.Context, d *path.Device, app string) error {
ctx = status.Start(ctx, "RPC Install App")
defer status.Finish(ctx)
ctx = log.Enter(ctx, "InstallApp")
if !s.enableLocalFiles {
return fmt.Errorf("Server not configured to allow reading of local files")
}
device := bind.GetRegistry(ctx).Device(d.GetID().ID())
if device == nil {
return &service.ErrDataUnavailable{Reason: messages.ErrUnknownDevice()}
}
return device.InstallApp(ctx, app)
}