-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the Perfetto config parsing to its own class.
- Loading branch information
1 parent
7b6edd1
commit 93a8e58
Showing
3 changed files
with
101 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
gapic/src/main/com/google/gapid/perfetto/PerfettoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters