-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
180 lines (159 loc) · 5.29 KB
/
manager.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
// 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 replay
import (
"context"
"sync"
"time"
"github.com/google/gapid/core/app/status"
"github.com/google/gapid/core/data/id"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/device/bind"
gapir "github.com/google/gapid/gapir/client"
"github.com/google/gapid/gapis/replay/scheduler"
"github.com/google/gapid/gapis/service/path"
)
const (
lowestPriority = 0
lowPriority = 1
defaultPriority = 2
highPriorty = 3
backgroundBatchDelay = time.Millisecond * 500
defaultBatchDelay = time.Millisecond * 100
)
// Manager executes replay requests.
type Manager interface {
// Replay requests that req is to be performed on the device described by
// intent, using the capture described by intent.
Replay(
ctx context.Context,
intent Intent,
cfg Config,
req Request,
generator Generator,
hints *path.UsageHints,
forceNonSplitReplay bool) (val interface{}, err error)
}
// Manager is used discover replay devices and to send replay requests to those
// discovered devices.
type manager struct {
gapir *gapir.Client
schedulers map[id.ID]*scheduler.Scheduler
mutex sync.Mutex // guards schedulers
}
// batchKey is used as a key for the batch that's being formed.
type batchKey struct {
// Do not be tempted to turn these IDs into path nodes - go equality will
// break and no batches will be formed.
capture id.ID
device id.ID
config Config
generator Generator
forceNonSplitReplay bool
}
// New returns a new Manager instance using the database db.
func New(ctx context.Context) Manager {
out := &manager{
gapir: gapir.New(ctx),
schedulers: make(map[id.ID]*scheduler.Scheduler),
}
bind.GetRegistry(ctx).Listen(bind.NewDeviceListener(out.createScheduler, out.destroyScheduler))
return out
}
// NewManagerForTest returns a new Manager for the test.
func NewManagerForTest(client *gapir.Client) Manager {
return &manager{gapir: client}
}
// Replay requests that req is to be performed on the device described by intent,
// using the capture described by intent. Replay requests made with configs that
// have equality (==) will likely be batched into the same replay pass.
func (m *manager) Replay(
ctx context.Context,
intent Intent,
cfg Config,
req Request,
generator Generator,
hints *path.UsageHints,
forceNonSplitReplay bool) (val interface{}, err error) {
ctx = status.Start(ctx, "Replay Request")
defer status.Finish(ctx)
status.Block(ctx)
defer status.Unblock(ctx)
log.D(ctx, "Replay request")
s, err := m.scheduler(ctx, intent.Device.ID.ID())
if err != nil {
return nil, err
}
b := scheduler.Batch{
Key: batchKey{
capture: intent.Capture.ID.ID(),
device: intent.Device.ID.ID(),
config: cfg,
generator: generator,
forceNonSplitReplay: forceNonSplitReplay,
},
Priority: defaultPriority,
Precondition: defaultBatchDelay,
}
if hints != nil {
if hints.Preview {
b.Priority = lowPriority
}
if hints.Primary {
b.Priority = highPriorty
b.Precondition = nil
}
if hints.Background {
b.Priority = lowestPriority
b.Precondition = backgroundBatchDelay
}
}
return s.Schedule(ctx, req, b)
}
func (m *manager) scheduler(ctx context.Context, deviceID id.ID) (*scheduler.Scheduler, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
s, found := m.schedulers[deviceID]
if !found {
return nil, log.Err(ctx, nil, "Device scheduler not found")
}
return s, nil
}
func (m *manager) createScheduler(ctx context.Context, device bind.Device) {
deviceID := device.Instance().ID.ID()
log.I(ctx, "New scheduler for device: %v", deviceID)
m.mutex.Lock()
defer m.mutex.Unlock()
m.schedulers[deviceID] = scheduler.New(ctx, deviceID, m.batch)
}
func (m *manager) destroyScheduler(ctx context.Context, device bind.Device) {
deviceID := device.Instance().ID.ID()
log.I(ctx, "Destroying scheduler for device: %v", deviceID)
m.mutex.Lock()
defer m.mutex.Unlock()
delete(m.schedulers, deviceID)
}
func (m *manager) connect(ctx context.Context, device bind.Device, replayABI *device.ABI) (*gapir.ReplayerKey, error) {
return m.gapir.Connect(ctx, device, replayABI)
}
func (m *manager) BeginReplay(ctx context.Context, key *gapir.ReplayerKey, payload string, dependent string) error {
return m.gapir.BeginReplay(ctx, key, payload, dependent)
}
func (m *manager) SetReplayExecutor(ctx context.Context, key *gapir.ReplayerKey, executor gapir.ReplayExecutor) (func(), error) {
return m.gapir.SetReplayExecutor(ctx, key, executor)
}
func (m *manager) PrewarmReplay(ctx context.Context, key *gapir.ReplayerKey, payload string, cleanup string) error {
return m.gapir.PrewarmReplay(ctx, key, payload, cleanup)
}