/
client.go
219 lines (198 loc) · 6.54 KB
/
client.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
// 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 trace
import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/google/gapid/core/app/crash"
"github.com/google/gapid/core/app/layout"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/device/bind"
"github.com/google/gapid/core/os/file"
"github.com/google/gapid/core/os/shell"
"github.com/google/gapid/test/robot/job"
"github.com/google/gapid/test/robot/job/worker"
"github.com/google/gapid/test/robot/stash"
_ "github.com/google/gapid/gapidapk"
)
const (
traceTimeout = time.Hour
// this string is returned when GAPIT fails to connect to the GAPIS, particularly due to ETXTBSY
// look at https://github.com/google/gapid/pull/933 for more information
retryString = "Failed to connect to the GAPIS server"
// The value to use for gapit's --observe-frames flag.
observeEveryNthFrame = 5
)
type client struct {
store *stash.Client
manager Manager
tempDir file.Path
}
// Run starts new trace client if any hardware is available.
func Run(ctx context.Context, store *stash.Client, manager Manager, tempDir file.Path) error {
c := &client{store: store, manager: manager, tempDir: tempDir}
job.OnDeviceAdded(ctx, c.onDeviceAdded)
return nil
}
// TODO: not assume all android devices are valid trace targets
func (c *client) onDeviceAdded(ctx context.Context, host *device.Instance, target bind.Device) {
traceOnTarget := func(ctx context.Context, t *Task) error {
job.LockDevice(ctx, target)
defer job.UnlockDevice(ctx, target)
if target.Status(ctx) != bind.Status_Online {
log.I(ctx, "Trying to trace %s on %s not started, device status %s",
t.Input.Subject, target.Instance().GetSerial(), target.Status(ctx).String())
return nil
}
return c.trace(ctx, target, t)
}
crash.Go(func() {
if err := c.manager.Register(ctx, host, target.Instance(), traceOnTarget); err != nil {
log.E(ctx, "Error running trace client: %v", err)
}
})
}
func (c *client) trace(ctx context.Context, d bind.Device, t *Task) error {
if err := c.manager.Update(ctx, t.Action, job.Running, nil); err != nil {
return err
}
var output *Output
err := worker.RetryFunction(ctx, 4, time.Millisecond*100, func() (err error) {
ctx, cancel := task.WithTimeout(ctx, traceTimeout)
defer cancel()
output, err = doTrace(ctx, t.Action, t.Input, c.store, d, c.tempDir)
return
})
status := job.Succeeded
if err != nil {
status = job.Failed
log.E(ctx, "Error running trace: %v", err)
} else if output.Err != "" {
status = job.Failed
log.E(ctx, "Error during trace: %v", output.Err)
}
return c.manager.Update(ctx, t.Action, status, output)
}
// doTrace extracts input files and runs `gapit trace` on them, capturing the output. The output object will
// be partially filled in the event of an upload error from store in order to allow examination of the logs.
func doTrace(ctx context.Context, action string, in *Input, store *stash.Client, d bind.Device, tempDir file.Path) (*Output, error) {
subject := tempDir.Join(action + ".apk")
obb := tempDir.Join(action + ".obb")
tracefile := tempDir.Join(action + ".gfxtrace")
extractedDir := tempDir.Join(action + "_tools")
extractedLayout, err := layout.NewPkgLayout(extractedDir, true)
if err != nil {
return nil, err
}
gapidAPK, err := extractedLayout.GapidApk(ctx, in.GetToolingLayout().GetGapidAbi())
if err != nil {
return nil, err
}
gapis, err := extractedLayout.Gapis(ctx)
if err != nil {
return nil, err
}
gapit, err := extractedLayout.Gapit(ctx)
if err != nil {
return nil, err
}
traceTime, err := ptypes.Duration(in.GetHints().GetTraceTime())
if err != nil {
traceTime = 10 * time.Minute // TODO: support Robot-wide override
}
defer func() {
file.Remove(subject)
file.Remove(tracefile)
file.RemoveAll(extractedDir)
}()
if err := store.GetFile(ctx, in.Subject, subject); err != nil {
return nil, err
}
if err := store.GetFile(ctx, in.Gapis, gapis); err != nil {
return nil, err
}
if err := store.GetFile(ctx, in.Gapit, gapit); err != nil {
return nil, err
}
if err := store.GetFile(ctx, in.GapidApk, gapidAPK); err != nil {
return nil, err
}
params := []string{
"trace",
"-out", tracefile.System(),
"-for", traceTime.String(),
"-disable-pcs",
"-observe-frames", strconv.Itoa(observeEveryNthFrame),
"-record-errors",
"-serial", d.Instance().Serial,
"-api", in.GetHints().GetAPI(),
}
if frames := in.GetHints().GetObserveFrames(); frames > 0 {
params = append(params, "-capture-frames", strconv.Itoa(int(frames*observeEveryNthFrame+1)))
}
if in.Obb != "" {
if err := store.GetFile(ctx, in.Obb, obb); err != nil {
return nil, err
}
defer func() {
file.Remove(obb)
}()
// TODO fix this
// params = append(params, "-obb", obb.System())
return log.Errf(ctx, nil, "OBBs are currently not supported")
}
cmd := shell.Command(gapit.System(), append(params, "apk:"+subject.System())...)
outBuf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
outputObj := &Output{}
errs := []string{}
log.I(ctx, "Running trace action %s", cmd)
if err := cmd.Capture(outBuf, errBuf).Run(ctx); err != nil {
if err := worker.NeedsRetry(err.Error()); err != nil {
return nil, err
}
errs = append(errs, err.Error())
}
errs = append(errs, strings.TrimSpace(errBuf.String()))
outputObj.Err = strings.Join(errs, "\n")
if err := worker.NeedsRetry(outputObj.Err, retryString); err != nil {
return nil, err
}
output := fmt.Sprintf("%s\n\n%s", cmd, strings.TrimSpace(outBuf.String()))
log.I(ctx, output)
logID, err := store.UploadString(ctx, stash.Upload{Name: []string{"trace.log"}, Type: []string{"text/plain"}}, output)
if err != nil {
return outputObj, err
}
outputObj.Log = logID
traceID, err := store.UploadFile(ctx, tracefile)
if err != nil {
return outputObj, err
}
outputObj.Trace = traceID
return outputObj, nil
}
type offlineDevice struct {
bind.Device
}
func (offlineDevice) Status() bind.Status {
return bind.Status_Offline
}