Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for taking a Perfetto trace in GAPIS #2709

Merged
merged 5 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<classpathentry kind="lib" path="bazel-bin/core/log/log_pb/liblog_pb_proto-speed.jar" sourcepath="bazel-genfiles/core/log/log_pb/log_pb_proto-speed-src.jar"/>
<classpathentry kind="lib" path="bazel-bin/core/os/device/libdevice_proto-speed.jar" sourcepath="bazel-genfiles/core/os/device/device_proto-speed-src.jar"/>
<classpathentry kind="lib" path="bazel-bin/core/stream/libstream_proto-speed.jar" sourcepath="bazel-genfiles/core/stream/stream_proto-speed-src.jar"/>
<classpathentry kind="lib" path="bazel-bin/external/perfetto/libconfig_proto-speed.jar" sourcepath="bazel-genfiles/external/perfetto/config_proto-speed-src.jar"/>
<classpathentry kind="lib" path="bazel-bin/external/com_google_protobuf/libprotobuf_java.jar"/>
<classpathentry kind="lib" path="bazel-bin/gapidapk/pkginfo/libpkginfo_proto-speed.jar" sourcepath="bazel-genfiles/gapidapk/pkginfo/pkginfo_proto-speed-src.jar"/>
<classpathentry kind="lib" path="bazel-bin/gapis/api/libapi_proto-speed.jar" sourcepath="bazel-genfiles/gapis/api/api_proto-speed-src.jar"/>
Expand Down
2 changes: 2 additions & 0 deletions core/os/android/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//core/app:go_default_library",
"//core/event/task:go_default_library",
"//core/log:go_default_library",
"//core/os/device:go_default_library",
"//core/os/device/bind:go_default_library",
"@perfetto//:go_default_library",
],
)

Expand Down
3 changes: 3 additions & 0 deletions core/os/android/adb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ go_library(
"inputs.go",
"installed_package.go",
"logcat.go",
"perfetto.go",
"screen.go",
],
importpath = "github.com/google/gapid/core/os/android/adb",
Expand All @@ -44,6 +45,8 @@ go_library(
"//core/os/device/bind:go_default_library",
"//core/os/file:go_default_library",
"//core/os/shell:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@perfetto//:go_default_library",
],
)

Expand Down
95 changes: 95 additions & 0 deletions core/os/android/adb/perfetto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (C) 2019 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 adb

import (
"bufio"
"bytes"
"context"
"io"
"strings"

"perfetto_pb"

"github.com/golang/protobuf/proto"
"github.com/google/gapid/core/app/crash"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/log"
)

// StartPerfettoTrace starts a perfetto trace on this device.
func (b *binding) StartPerfettoTrace(ctx context.Context, config *perfetto_pb.TraceConfig, out string, stop task.Signal) error {
reader, stdout := io.Pipe()
data, err := proto.Marshal(config)
if err != nil {
return err
}

fail := make(chan error, 1)
crash.Go(func() {
buf := bufio.NewReader(reader)
for {
line, e := buf.ReadString('\n')
switch e {
default:
log.E(ctx, "[perfetto] Read error %v", e)
fail <- e
return
case io.EOF:
fail <- nil
return
case nil:
log.I(ctx, "[perfetto] %s", strings.TrimSuffix(line, "\n"))
}
}
})

process, err := b.Shell("perfetto", "-c", "-", "-o", out).
Read(bytes.NewReader(data)).
Capture(stdout, stdout).
Start(ctx)
if err != nil {
stdout.Close()
return err
}

wait := make(chan error, 1)
crash.Go(func() {
wait <- process.Wait(ctx)
})

select {
case err = <-fail:
return err
case err = <-wait:
// Do nothing.
case <-stop:
// TODO: figure out why "killall -2 perfetto" doesn't work.
var pid string
if pid, err = b.Shell("pidof perfetto").Call(ctx); err != nil {
break
}
if err = b.Shell("kill -2 " + pid).Run(ctx); err != nil {
break
}
err = <-wait
}

stdout.Close()
if err != nil {
return err
}
return <-fail
}
5 changes: 5 additions & 0 deletions core/os/android/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"fmt"
"time"

"perfetto_pb"

"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"
Expand Down Expand Up @@ -91,6 +94,8 @@ type Device interface {
// DeleteSystemSetting removes the system setting with with the given
// namespaced key.
DeleteSystemSetting(ctx context.Context, namespace, key string) error
// StartPerfettoTrace starts a perfetto trace.
StartPerfettoTrace(ctx context.Context, config *perfetto_pb.TraceConfig, out string, stop task.Signal) error
}

// LogcatMessage represents a single logcat message.
Expand Down
1 change: 1 addition & 0 deletions gapic/src/main/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ java_library(
"//gapis/service/path:path_java_proto",
"//gapis/stringtable:stringtable_java_proto",
"//gapis/vertex:vertex_java_proto",
"@perfetto//:config_java_proto",
],
)
2 changes: 2 additions & 0 deletions gapic/src/main/com/google/gapid/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.gapid.util.Logging;
import com.google.gapid.util.Messages;
import com.google.gapid.util.Scheduler;
import com.google.gapid.views.TracerDialog;
import com.google.gapid.widgets.Theme;
import com.google.gapid.widgets.Widgets;

Expand Down Expand Up @@ -206,5 +207,6 @@ private static interface ShellRunnable {
Logging.logDir,
Follower.logFollowRequests,
Server.useCache,
TracerDialog.perfettoConfig,
};
}
3 changes: 3 additions & 0 deletions gapic/src/main/com/google/gapid/models/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class Settings {
public int[] shaderSplitterWeights = new int[] { 70, 30 };
public int[] texturesSplitterWeights = new int[] { 20, 80 };
public String traceDevice = "";
public String traceType = "Graphics";
public String traceApi = "";
public String traceUri = "";
public String traceArguments = "";
Expand Down Expand Up @@ -217,6 +218,7 @@ private void updateFrom(Properties properties) {
texturesSplitterWeights =
getIntList(properties, "texture.splitter.weights", texturesSplitterWeights);
traceDevice = properties.getProperty("trace.device", traceDevice);
traceType = properties.getProperty("trace.type", traceType);
traceApi = properties.getProperty("trace.api", traceApi);
traceUri = properties.getProperty("trace.uri", traceUri);
traceArguments = properties.getProperty("trace.arguments", traceArguments);
Expand Down Expand Up @@ -260,6 +262,7 @@ private void updateTo(Properties properties) {
setIntList(properties, "shader.splitter.weights", shaderSplitterWeights);
setIntList(properties, "texture.splitter.weights", texturesSplitterWeights);
properties.setProperty("trace.device", traceDevice);
properties.setProperty("trace.type", traceType);
properties.setProperty("trace.api", traceApi);
properties.setProperty("trace.uri", traceUri);
properties.setProperty("trace.arguments", traceArguments);
Expand Down
79 changes: 60 additions & 19 deletions gapic/src/main/com/google/gapid/views/TracerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.gapid.widgets.Widgets.withLayoutData;
import static com.google.gapid.widgets.Widgets.withMargin;
import static com.google.gapid.widgets.Widgets.withSpans;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.Lists;
import com.google.gapid.models.Analytics;
Expand All @@ -39,12 +40,15 @@
import com.google.gapid.models.TraceTargets;
import com.google.gapid.proto.service.Service;
import com.google.gapid.proto.service.Service.ClientAction;
import com.google.gapid.proto.service.Service.DeviceAPITraceConfiguration;
import com.google.gapid.proto.service.Service.DeviceTraceConfiguration;
import com.google.gapid.proto.service.Service.StatusResponse;
import com.google.gapid.proto.service.Service.TraceType;
import com.google.gapid.proto.service.Service.TraceTypeCapabilities;
import com.google.gapid.server.Client;
import com.google.gapid.server.Tracer;
import com.google.gapid.server.Tracer.TraceRequest;
import com.google.gapid.util.Flags;
import com.google.gapid.util.Flags.Flag;
import com.google.gapid.util.Messages;
import com.google.gapid.util.OS;
import com.google.gapid.util.Scheduler;
Expand All @@ -54,6 +58,7 @@
import com.google.gapid.widgets.LoadingIndicator;
import com.google.gapid.widgets.Theme;
import com.google.gapid.widgets.Widgets;
import com.google.protobuf.TextFormat;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
Expand All @@ -80,6 +85,10 @@
import org.eclipse.swt.widgets.Text;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
Expand All @@ -95,6 +104,10 @@
public class TracerDialog {
protected static final Logger LOG = Logger.getLogger(TracerDialog.class.getName());

public static final Flag<String> perfettoConfig = Flags.value("perfetto", "",
"Path to a file containing a perfetto trace config proto in text format. " +
"Specifying this flag will enable the Perfetto tracing UI features");

private TracerDialog() {
}

Expand Down Expand Up @@ -153,6 +166,16 @@ public void onCaptureDevicesLoaded() {
}
}

protected static void readPerfettoConfig(Service.TraceOptions.Builder options) {
try (Reader in = new InputStreamReader(new FileInputStream(perfettoConfig.get()))) {
TextFormat.merge(in, options.getPerfettoConfigBuilder());
} catch (IOException e) {
// This is temporary, experimental code, so just sort of crash.
throw new RuntimeException("Failed to read perfetto config from " + perfettoConfig.get(), e);
}
}


/**
* Dialog to request the information from the user to start a trace (which app, filename, etc.).
*/
Expand Down Expand Up @@ -277,7 +300,7 @@ public TraceInput(Composite parent, Models models, Widgets widgets, Runnable ref

deviceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

createLabel(this, "API:");
createLabel(this, "Type:");
api = createApiDropDown(this);
api.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

Expand Down Expand Up @@ -388,7 +411,7 @@ protected void configureDialog(DirectoryDialog dialog) {
api.getCombo().addListener(SWT.Selection, e -> update(models.settings, getSelectedApi()));

Listener mecListener = e -> {
DeviceAPITraceConfiguration config = getSelectedApi();
TraceTypeCapabilities config = getSelectedApi();
if (fromBeginning.getSelection() || config == null ||
config.getMidExecutionCaptureSupport() != Service.FeatureStatus.Experimental) {
fromBeginning.setText(MEC_LABEL);
Expand Down Expand Up @@ -435,7 +458,12 @@ private static ComboViewer createApiDropDown(Composite parent) {
combo.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
return ((DeviceAPITraceConfiguration)element).getApi();
TraceTypeCapabilities ttc = (TraceTypeCapabilities)element;
switch (ttc.getType()) {
case Graphics: return ttc.getApi();
case Perfetto: return "Perfetto";
default: throw new AssertionError();
}
}
});
return combo;
Expand All @@ -450,7 +478,7 @@ private void update(Settings settings, DeviceCaptureInfo dev) {
updateApiDropdown(config, settings);
}

private void update(Settings settings, DeviceAPITraceConfiguration config) {
private void update(Settings settings, TraceTypeCapabilities config) {
boolean pcs = config != null && config.getCanDisablePcs();
disablePcs.setEnabled(pcs);
disablePcs.setSelection(pcs && settings.traceDisablePcs);
Expand Down Expand Up @@ -485,16 +513,22 @@ private void updateDevicesDropDown(Settings settings) {

private void updateApiDropdown(DeviceTraceConfiguration config, Settings settings) {
if (api != null && config != null) {
api.setInput(config.getApisList());
DeviceAPITraceConfiguration deflt = config.getApis(0);
for (DeviceAPITraceConfiguration c : config.getApisList()) {
if (c.getApi().equals(settings.traceApi)) {
deflt = c;
break;
List<TraceTypeCapabilities> caps = config.getApisList().stream()
.filter(t -> t.getType() != TraceType.Perfetto || !perfettoConfig.get().isEmpty())
.collect(toList());
api.setInput(caps);
if (!caps.isEmpty()) {
TraceTypeCapabilities deflt = caps.get(0);
for (TraceTypeCapabilities c : caps) {
if (c.getType().name().equals(settings.traceType) &&
(c.getApi().isEmpty() || c.getApi().equals(settings.traceApi))) {
deflt = c;
break;
}
}
api.setSelection(new StructuredSelection(deflt));
api.getCombo().notifyListeners(SWT.Selection, new Event());
}
api.setSelection(new StructuredSelection(deflt));
api.getCombo().notifyListeners(SWT.Selection, new Event());
}
}

Expand Down Expand Up @@ -548,9 +582,10 @@ protected String formatTraceName(String name) {
}

public boolean isReady() {
return getSelectedDevice() != null && getSelectedApi() != null &&
!traceTarget.getText().isEmpty() && !directory.getText().isEmpty() &&
!file.getText().isEmpty();
TraceTypeCapabilities config = getSelectedApi();
return getSelectedDevice() != null && config != null &&
(!config.getRequiresApplication() || !traceTarget.getText().isEmpty()) &&
!directory.getText().isEmpty() && !file.getText().isEmpty();
}

public void addModifyListener(Listener listener) {
Expand All @@ -568,10 +603,11 @@ public void setDevices(Settings settings, List<DeviceCaptureInfo> devices) {

public TraceRequest getTraceRequest(Settings settings) {
DeviceCaptureInfo dev = devices.get(device.getCombo().getSelectionIndex());
DeviceAPITraceConfiguration config = getSelectedApi();
TraceTypeCapabilities config = getSelectedApi();
File output = getOutputFile();

settings.traceDevice = dev.device.getSerial();
settings.traceType = config.getType().name();
settings.traceApi = config.getApi();
settings.traceUri = traceTarget.getText();
settings.traceArguments = arguments.getText();
Expand All @@ -583,6 +619,7 @@ public TraceRequest getTraceRequest(Settings settings) {

Service.TraceOptions.Builder options = Service.TraceOptions.newBuilder()
.setDevice(dev.path)
.setType(config.getType())
.addApis(config.getApi())
.setUri(traceTarget.getText())
.setAdditionalCommandLineArgs(arguments.getText())
Expand Down Expand Up @@ -612,6 +649,10 @@ public TraceRequest getTraceRequest(Settings settings) {
options.setDisablePcs(disablePcs.getSelection());
}

if (config.getType() == TraceType.Perfetto) {
readPerfettoConfig(options);
}

return new TraceRequest(output, options.build());
}

Expand Down Expand Up @@ -649,9 +690,9 @@ protected DeviceCaptureInfo getSelectedDevice() {
return sel.isEmpty() ? null : (DeviceCaptureInfo)sel.getFirstElement();
}

protected DeviceAPITraceConfiguration getSelectedApi() {
protected TraceTypeCapabilities getSelectedApi() {
IStructuredSelection sel = api.getStructuredSelection();
return sel.isEmpty() ? null : ((DeviceAPITraceConfiguration)sel.getFirstElement());
return sel.isEmpty() ? null : ((TraceTypeCapabilities)sel.getFirstElement());
}

private File getOutputFile() {
Expand Down
Loading