Skip to content

Commit

Permalink
Extract the Perfetto config parsing to its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuetschard committed Jul 26, 2019
1 parent 7b6edd1 commit 93a8e58
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 26 deletions.
4 changes: 2 additions & 2 deletions gapic/src/main/com/google/gapid/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.gapid.models.Follower;
import com.google.gapid.models.Models;
import com.google.gapid.models.Settings;
import com.google.gapid.perfetto.PerfettoConfig;
import com.google.gapid.server.GapiPaths;
import com.google.gapid.server.GapisProcess;
import com.google.gapid.util.Crash2ExceptionHandler;
Expand All @@ -35,7 +36,6 @@
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 @@ -207,6 +207,6 @@ private static interface ShellRunnable {
Logging.logDir,
Follower.logFollowRequests,
Server.useCache,
TracerDialog.perfettoConfig,
PerfettoConfig.perfettoConfig,
};
}
95 changes: 95 additions & 0 deletions gapic/src/main/com/google/gapid/perfetto/PerfettoConfig.java
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 com.google.gapid.perfetto;

import static java.util.logging.Level.WARNING;

import com.google.common.collect.ImmutableList;
import com.google.gapid.util.Flags;
import com.google.gapid.util.Flags.Flag;
import com.google.protobuf.TextFormat;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.function.Supplier;
import java.util.logging.Logger;

public class PerfettoConfig {
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 static final Logger LOG = Logger.getLogger(PerfettoConfig.class.getName());
private static final PerfettoConfig MISSING = new PerfettoConfig(null);

private static PerfettoConfig instance;

private final perfetto.protos.PerfettoConfig.TraceConfig config;

public PerfettoConfig(perfetto.protos.PerfettoConfig.TraceConfig config) {
this.config = config;
}

public static synchronized PerfettoConfig get() {
if (instance == null) {
instance = findConfig();
}
return instance;
}

public boolean hasConfig() {
return config != null;
}

public perfetto.protos.PerfettoConfig.TraceConfig getConfig() {
return config;
}

private static PerfettoConfig findConfig() {
return ImmutableList.<Supplier<File>>of(
() -> {
String path = perfettoConfig.get();
return "".equals(path) ? null : new File(path);
}
).stream()
.map(dir -> checkForConfig(dir.get()))
.filter(PerfettoConfig::shouldUse)
.findFirst()
.orElse(MISSING);
}

private static boolean shouldUse(PerfettoConfig config) {
return config != null && config.hasConfig();
}

private static PerfettoConfig checkForConfig(File file) {
if (file == null || !file.isFile()) {
return null;
}
try (Reader in = new InputStreamReader(new FileInputStream(file))) {
perfetto.protos.PerfettoConfig.TraceConfig.Builder config =
perfetto.protos.PerfettoConfig.TraceConfig.newBuilder();
TextFormat.merge(in, config);
return new PerfettoConfig(config.build());
} catch (IOException e) {
LOG.log(WARNING, "Failed to read Perfetto config from " + file, e);
}
return null;
}
}
28 changes: 4 additions & 24 deletions gapic/src/main/com/google/gapid/views/TracerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.gapid.models.Models;
import com.google.gapid.models.Settings;
import com.google.gapid.models.TraceTargets;
import com.google.gapid.perfetto.PerfettoConfig;
import com.google.gapid.proto.service.Service;
import com.google.gapid.proto.service.Service.ClientAction;
import com.google.gapid.proto.service.Service.DeviceTraceConfiguration;
Expand All @@ -48,8 +49,6 @@
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 @@ -59,7 +58,6 @@
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 @@ -86,10 +84,6 @@
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 @@ -105,10 +99,6 @@
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 @@ -180,17 +170,6 @@ public void onCaptureDevicesLoaded() {
}
}

protected static void readPerfettoConfig(Service.TraceOptions.Builder options, int durationMs) {
try (Reader in = new InputStreamReader(new FileInputStream(perfettoConfig.get()))) {
TextFormat.merge(in, options.getPerfettoConfigBuilder());
options.getPerfettoConfigBuilder().setDurationMs(durationMs);
} 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 @@ -548,7 +527,7 @@ private void updateDevicesDropDown(Settings settings) {
private void updateApiDropdown(DeviceTraceConfiguration config, Settings settings) {
if (api != null && config != null) {
List<TraceTypeCapabilities> caps = config.getApisList().stream()
.filter(t -> !isPerfetto(t) || !perfettoConfig.get().isEmpty())
.filter(t -> !isPerfetto(t) || PerfettoConfig.get().hasConfig())
.collect(toList());
api.setInput(caps);
if (!caps.isEmpty()) {
Expand Down Expand Up @@ -689,7 +668,8 @@ public TraceRequest getTraceRequest(Settings settings) {
int duration = frameCount.getSelection() * 1000;
// TODO: this isn't really unlimitted.
duration = (duration == 0) ? (int)MINUTES.toMillis(10) : duration;
readPerfettoConfig(options, duration);
options.setPerfettoConfig(PerfettoConfig.get().getConfig().toBuilder()
.setDurationMs(duration));
}

return new TraceRequest(output, options.build());
Expand Down

0 comments on commit 93a8e58

Please sign in to comment.