-
Notifications
You must be signed in to change notification settings - Fork 327
/
subject.go
188 lines (171 loc) · 5.47 KB
/
subject.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
// 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 main
import (
"context"
"flag"
"os"
"strings"
"time"
"github.com/google/gapid/core/os/file"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/gapid/core/app"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/net/grpcutil"
"github.com/google/gapid/test/robot/search/script"
"github.com/google/gapid/test/robot/subject"
"google.golang.org/grpc"
)
func init() {
uploadVerb.Add(&app.Verb{
Name: "subject",
ShortHelp: "Upload a traceable application to the server",
ShortUsage: "<filenames>",
Action: &subjectUploadVerb{RobotOptions: defaultRobotOptions, API: GLESAPI, ObserveFrames: 0},
})
searchVerb.Add(&app.Verb{
Name: "subject",
ShortHelp: "List traceable applications in the server",
ShortUsage: "<query>",
Action: &subjectSearchVerb{RobotOptions: defaultRobotOptions},
})
setVerb.Add(&app.Verb{
Name: "subject",
ShortHelp: "Sets values on a subject",
ShortUsage: "<id or name>",
Action: &subjectUpdateVerb{RobotOptions: defaultRobotOptions},
})
}
const (
GLESAPI APIType = iota
VulkanAPI
)
type APIType uint8
var apiTypeToName = map[APIType]string{
GLESAPI: "gles",
VulkanAPI: "vulkan",
}
func (a *APIType) Choose(c interface{}) {
*a = c.(APIType)
}
func (a APIType) String() string {
return apiTypeToName[a]
}
type subjectUploadVerb struct {
RobotOptions
API APIType `help:"the api to capture, can be either gles or vulkan (default:gles)"`
TraceTime time.Duration `help:"trace time override (if non-zero)"`
OBB file.Path `help:"path of the subject's obb file"`
obbID string
ObserveFrames uint `help:"Observe the given number of frames. 0 for all (default:0)"`
subjects subject.Subjects
}
func (v *subjectUploadVerb) Run(ctx context.Context, flags flag.FlagSet) error {
if !v.OBB.IsEmpty() {
if len(flags.Args()) != 1 {
return log.Err(ctx, nil, "Cannot specify multiple subjects with OBB flag")
}
log.I(ctx, "Uploading OBB: %s", v.OBB)
return upload(ctx, []uploadable{path(v.OBB.String()), path(flags.Arg(0))}, v.ServerAddress, v)
}
return upload(ctx, paths(flags.Args()), v.ServerAddress, v)
}
func (v *subjectUploadVerb) prepare(ctx context.Context, conn *grpc.ClientConn) error {
v.subjects = subject.NewRemote(ctx, conn)
return nil
}
func (v *subjectUploadVerb) process(ctx context.Context, id string) error {
if !v.OBB.IsEmpty() && v.obbID == "" {
// obbID always comes first so just set it here.
log.I(ctx, "Uploaded OBB, ID: %s", id)
v.obbID = id
return nil
}
hints := &subject.Hints{}
if v.TraceTime != 0 {
hints.TraceTime = ptypes.DurationProto(v.TraceTime)
}
hints.ObserveFrames = uint32(v.ObserveFrames)
hints.API = v.API.String()
subject, created, err := v.subjects.Add(ctx, id, v.obbID, hints)
if err != nil {
return log.Err(ctx, err, "Failed processing subject")
}
if created {
log.I(ctx, "Added new subject")
} else {
log.I(ctx, "Already existing subject")
}
log.I(ctx, "Subject info %s", subject)
return nil
}
type subjectSearchVerb struct {
RobotOptions
}
func (v *subjectSearchVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
subjects := subject.NewRemote(ctx, conn)
expression := strings.Join(flags.Args(), " ")
out := os.Stdout
expr, err := script.Parse(ctx, expression)
if err != nil {
return log.Err(ctx, err, "Malformed search query")
}
return subjects.Search(ctx, expr.Query(), func(ctx context.Context, entry *subject.Subject) error {
proto.MarshalText(out, entry)
return nil
})
}, grpc.WithInsecure())
}
var idOrApkName = script.MustParse("Id == $ or Information.APK.Name == $").Using("$")
type subjectUpdateVerb struct {
RobotOptions
API APIType `help:"the new api to capture, can be either gles or vulkan"`
TraceTime time.Duration `help:"the new trace time override"`
}
func (v *subjectUpdateVerb) Run(ctx context.Context, flags flag.FlagSet) error {
return grpcutil.Client(ctx, v.ServerAddress, func(ctx context.Context, conn *grpc.ClientConn) error {
s := subject.NewRemote(ctx, conn)
args := flags.Args()
subj := &subject.Subject{
Hints: &subject.Hints{
TraceTime: ptypes.DurationProto(v.TraceTime),
API: v.API.String(),
},
}
if len(args) == 0 {
return log.Err(ctx, nil, "Missing argument, must specify a subject to update")
}
err := s.Search(ctx, idOrApkName(args[0]).Query(), func(ctx context.Context, entry *subject.Subject) error {
if subj.Id != "" {
return log.Err(ctx, nil, "Multiple subjects matched")
}
subj.Id = entry.Id
return nil
})
if err != nil {
return err
}
if subj.Id == "" {
return log.Err(ctx, nil, "No packages matched")
}
subj, err = s.Update(ctx, subj)
if err != nil {
return err
}
log.I(ctx, subj.String())
return nil
}, grpc.WithInsecure())
}