-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
250 lines (231 loc) · 8.86 KB
/
validate.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
// Copyright (C) 2018 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 validate
import (
"context"
"fmt"
"math"
"github.com/google/gapid/core/log"
"github.com/google/gapid/gapis/perfetto"
perfetto_service "github.com/google/gapid/gapis/perfetto/service"
)
const (
counterIDQuery = "select id from gpu_counter_track where name = '%v'"
counterValuesQuery = "" +
"select value from counter " +
"where track_id = %v order by ts " +
"limit %v offset 10"
trackIDQuery = "select id from gpu_track where scope = '%v'"
renderStageTrackScope = "gpu_render_stage"
vulkanEventsTrackScope = "vulkan_events"
renderStageSlicesQuery = "" +
"select name, command_buffer, submission_id " +
"from gpu_slice " +
"where track_id = %v " +
"order by id"
vulkanEventSlicesQuery = "" +
"select name, submission_id " +
"from gpu_slice " +
"where track_id = %v " +
"order by id"
sampleCounter = 100
)
type Scope string
// Checker is a function that checks the validity of the values of the given result set column.
type Checker func(column *perfetto_service.QueryResult_ColumnValues, columnType perfetto_service.QueryResult_ColumnDesc_Type) bool
// GpuCounter represents a GPU counter for which the profiling data is validated.
type GpuCounter struct {
Id uint32
Name string
Check Checker
}
// Validator is an interface implemented by the various hardware specific validators.
type Validator interface {
Validate(ctx context.Context, processor *perfetto.Processor) error
GetCounters() []GpuCounter
}
// And returns a checker that is only valid if both of its arguments are.
func And(c1, c2 Checker) Checker {
return func(column *perfetto_service.QueryResult_ColumnValues, columnType perfetto_service.QueryResult_ColumnDesc_Type) bool {
return c1(column, columnType) && c2(column, columnType)
}
}
// IsNumber is a checker that checks that the column is a number type.
func IsNumber(column *perfetto_service.QueryResult_ColumnValues, columnType perfetto_service.QueryResult_ColumnDesc_Type) bool {
if columnType != perfetto_service.QueryResult_ColumnDesc_LONG && columnType != perfetto_service.QueryResult_ColumnDesc_DOUBLE {
return false
}
return true
}
// CheckLargerThanZero is a checker that checks that the values are all greater than zero.
func CheckLargerThanZero(column *perfetto_service.QueryResult_ColumnValues, columnType perfetto_service.QueryResult_ColumnDesc_Type) bool {
longValues := column.GetLongValues()
doubleValues := column.GetDoubleValues()
for i := 0; i < sampleCounter; i++ {
if columnType == perfetto_service.QueryResult_ColumnDesc_LONG {
if longValues[i] <= 0 {
return false
}
} else if columnType == perfetto_service.QueryResult_ColumnDesc_DOUBLE {
if doubleValues[i] <= 0.0 {
return false
}
}
}
return true
}
// CheckEqualTo returns a checker that checks that all returned value equal the given value.
func CheckEqualTo(num float64) Checker {
return func(column *perfetto_service.QueryResult_ColumnValues, columnType perfetto_service.QueryResult_ColumnDesc_Type) bool {
longValues := column.GetLongValues()
doubleValues := column.GetDoubleValues()
for i := 0; i < sampleCounter; i++ {
if columnType == perfetto_service.QueryResult_ColumnDesc_LONG {
if longValues[i] != int64(num) {
return false
}
} else if columnType == perfetto_service.QueryResult_ColumnDesc_DOUBLE {
if doubleValues[i] != num {
return false
}
}
}
return true
}
}
// CheckApproximateTo returns a checker that checks that values are within a margin of the given value.
func CheckApproximateTo(num, err float64) Checker {
return func(column *perfetto_service.QueryResult_ColumnValues, columnType perfetto_service.QueryResult_ColumnDesc_Type) bool {
longValues := column.GetLongValues()
doubleValues := column.GetDoubleValues()
for i := 0; i < sampleCounter; i++ {
if columnType == perfetto_service.QueryResult_ColumnDesc_LONG {
if math.Abs(num-float64(longValues[i])) > err {
return false
}
} else if columnType == perfetto_service.QueryResult_ColumnDesc_DOUBLE {
if math.Abs(num-doubleValues[i]) > err {
return false
}
}
}
return true
}
}
// ValidateGpuCounters validates the GPU counters.
// GPU counters validation will fail in the below cases:
// 1. Fail to query
// 2. Missing GPU counter samples
// 3. Fail to check
func ValidateGpuCounters(ctx context.Context, processor *perfetto.Processor, counters []GpuCounter) error {
for _, counter := range counters {
queryResult, err := processor.Query(fmt.Sprintf(counterIDQuery, counter.Name))
if err != nil {
return log.Errf(ctx, err, "Failed to query with %v", fmt.Sprintf(counterIDQuery, counter.Name))
}
if len(queryResult.GetColumns()) != 1 {
return log.Errf(ctx, err, "Expect one result with query: %v", fmt.Sprintf(counterIDQuery, counter.Name))
}
var counterID int64
for _, column := range queryResult.GetColumns() {
longValues := column.GetLongValues()
if len(longValues) != 1 {
// This should never happen, but sill have a check.
return log.Err(ctx, nil, "Query result is not 1.")
}
counterID = longValues[0]
break
}
queryResult, err = processor.Query(fmt.Sprintf(counterValuesQuery, counterID, sampleCounter))
if err != nil {
return log.Errf(ctx, err, "Failed to query with %v for counter %v", fmt.Sprintf(counterValuesQuery, counterID), counter)
}
// Query exactly #sampleCounter samples, fail if not enough samples
if queryResult.GetNumRecords() != sampleCounter {
return log.Errf(ctx, nil, "Number of samples is incorrect for counter: %v %v", counter, queryResult.GetNumRecords())
}
if !counter.Check(queryResult.GetColumns()[0], queryResult.GetColumnDescriptors()[0].GetType()) {
return log.Errf(ctx, nil, "Check failed for counter: %v", counter)
}
}
return nil
}
// GetTrackIDs returns all track ids from gpu_track with the given scope.
func GetTrackIDs(ctx context.Context, s Scope, processor *perfetto.Processor) ([]int64, error) {
queryResult, err := processor.Query(fmt.Sprintf(trackIDQuery, s))
if err != nil || queryResult.GetNumRecords() <= 0 {
return []int64{}, log.Err(ctx, err, "Failed to query GPU render stage track ids")
}
result := make([]int64, queryResult.GetNumRecords())
for i, v := range queryResult.GetColumns()[0].GetLongValues() {
result[i] = v
}
return result, nil
}
// ValidateGpuSlices validates gpu slices, returns nil if all validation passes.
func ValidateGpuSlices(ctx context.Context, processor *perfetto.Processor) error {
tIds, err := GetTrackIDs(ctx, renderStageTrackScope, processor)
if err != nil {
return err
}
for _, tId := range tIds {
queryResult, err := processor.Query(fmt.Sprintf(renderStageSlicesQuery, tId))
if err != nil || queryResult.GetNumRecords() <= 0 {
return log.Errf(ctx, err, "Failed to query with %v", fmt.Sprintf(renderStageSlicesQuery, tId))
}
columns := queryResult.GetColumns()
numRecords := queryResult.GetNumRecords()
names := columns[0].GetStringValues()
commandBuffers := columns[1].GetLongValues()
submissionIds := columns[2].GetLongValues()
if numRecords == 0 {
return log.Err(ctx, nil, "No gpu slices")
}
for i := uint64(0); i < numRecords; i++ {
if commandBuffers[i] == 0 {
return log.Errf(ctx, nil, "Gpu slice %v has null command buffer", names[i])
}
if submissionIds[i] == 0 {
return log.Errf(ctx, nil, "Gpu slice %v has null submission id", names[i])
}
}
}
return nil
}
// ValidateVulkanEvents validates vulkan events slices, returns nil if all validation passes.
func ValidateVulkanEvents(ctx context.Context, processor *perfetto.Processor) error {
tIds, err := GetTrackIDs(ctx, vulkanEventsTrackScope, processor)
if err != nil {
return err
}
for _, tId := range tIds {
queryResult, err := processor.Query(fmt.Sprintf(vulkanEventSlicesQuery, tId))
if err != nil || queryResult.GetNumRecords() <= 0 {
return log.Errf(ctx, err, "Failed to query with %v", fmt.Sprintf(vulkanEventSlicesQuery, tId))
}
columns := queryResult.GetColumns()
numRecords := queryResult.GetNumRecords()
names := columns[0].GetStringValues()
submissionIds := columns[1].GetLongValues()
if numRecords == 0 {
return log.Err(ctx, nil, "No render stage slices")
}
for i := uint64(0); i < numRecords; i++ {
if names[i] == "vkQueueSubmit" && submissionIds[i] == 0 {
return log.Errf(ctx, nil, "Vulkan event slice %v has null submission id", names[i])
}
}
}
return nil
}