blob: a52a307561bbfcba1a0e29e60ed23514d1bba8c0 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// 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 gapis provides helper methods and types for communicating with the
// GAPIS service.
package gapis
import (
"fmt"
"net"
"android.googlesource.com/platform/tools/gpu/client/process"
"android.googlesource.com/platform/tools/gpu/framework/binary/registry"
"android.googlesource.com/platform/tools/gpu/framework/binary/schema"
"android.googlesource.com/platform/tools/gpu/framework/file"
"android.googlesource.com/platform/tools/gpu/framework/log"
"android.googlesource.com/platform/tools/gpu/gapid/auth"
"android.googlesource.com/platform/tools/gpu/gapid/service"
)
const (
mtu = 1024
BinaryName = "gapis"
)
var (
// GapisPath is the full filepath to the gapir executable.
GapisPath file.Path
)
func init() {
// Search directory that this executable is in.
if path, err := file.FindExecutable(file.ExecutablePath().Parent().Join(BinaryName).System()); err == nil {
GapisPath = path
return
}
// Search $PATH.
if path, err := file.FindExecutable(BinaryName); err == nil {
GapisPath = path
return
}
}
// Connect attempts to connect to a GAPIS process.
// If port is zero, a new GAPIS server will be started, otherwise a connection
// will be made to the specified port.
func Connect(ctx log.Context, port int, authToken auth.Token, args ...string) (service.Service, schema.Message, error) {
return ConnectUsing(ctx, GapisPath, port, authToken, args...)
}
func ConnectUsing(ctx log.Context, gapisPath file.Path, port int, authToken auth.Token, args ...string) (service.Service, schema.Message, error) {
var socket net.Conn
var err error
if port == 0 || len(args) > 0 {
if authToken != auth.NoAuth {
args = append(args, "--gapis-auth-token", string(authToken))
}
if ctx.Info().Active() {
args = append(args, "--log-level", "Info")
}
socket, err = process.StartAndConnect(ctx, gapisPath.System(), authToken, args...)
} else {
socket, err = process.Connect(port, authToken)
}
if err != nil {
return nil, schema.Message{}, err
}
client := service.NewClient(ctx, socket, nil)
message, err := client.GetSchema(ctx)
if err != nil {
return nil, schema.Message{}, fmt.Errorf("Error resolving schema: %v", err)
}
namespace := registry.NewNamespace()
for _, entity := range message.Entities {
namespace.Add((*schema.ObjectClass)(entity))
}
// Replace the current client with the schema aggregated namespace.
client.SetNamespace(registry.NewNamespace(registry.Global, namespace))
return client, message, nil
}