From 5b9df9bae428bcdd2839bebcbd93dc48d6aa6964 Mon Sep 17 00:00:00 2001 From: Pascal Muetschard Date: Tue, 23 Jul 2019 05:06:12 -0700 Subject: [PATCH] Fix handling of CPU/CPU Frequency track presence. - Don't crash if no CPU tracks are present - Don't assume the presence of CPU frequency tracks --- .../gapid/perfetto/models/CpuTrack.java | 16 +++++++++-- .../google/gapid/perfetto/models/Tracks.java | 28 ++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/gapic/src/main/com/google/gapid/perfetto/models/CpuTrack.java b/gapic/src/main/com/google/gapid/perfetto/models/CpuTrack.java index 3045f78d0b..3dfcf590c2 100644 --- a/gapic/src/main/com/google/gapid/perfetto/models/CpuTrack.java +++ b/gapic/src/main/com/google/gapid/perfetto/models/CpuTrack.java @@ -146,9 +146,10 @@ private static String sliceRangeSql(int cpu, TimeSpan ts) { return format(SLICE_RANGE_SQL, cpu, ts.end, ts.start); } - public static ListenableFuture enumerate( + public static ListenableFuture> enumerate( String parent, Perfetto.Data.Builder data) { return transform(freqMap(data.qe), freqMap -> { + List configs = Lists.newArrayList(); for (int i = 0; i < data.getNumCpus(); i++) { QueryEngine.Row freq = freqMap.get(Long.valueOf(i)); CpuTrack track = new CpuTrack(i); @@ -161,8 +162,9 @@ public static ListenableFuture enumerate( parent, freqTrack.getId(), "CPU " + (i + 1) + " Frequency", single(state -> new CpuFrequencyPanel(state, freqTrack), false)); } + configs.add(new CpuConfig(i, freq != null)); } - return data; + return configs; }); } @@ -170,6 +172,16 @@ private static ListenableFuture> freqMap(QueryEngine return transform(qe.query(FREQ_IDLE_QUERY), res -> res.map(row -> row.getLong(0), identity())); } + public static class CpuConfig { + public final int id; + public final boolean hasFrequency; + + public CpuConfig(int id, boolean hasFrequency) { + this.id = id; + this.hasFrequency = hasFrequency; + } + } + public static class Data extends Track.Data { public final Kind kind; // Summary. diff --git a/gapic/src/main/com/google/gapid/perfetto/models/Tracks.java b/gapic/src/main/com/google/gapid/perfetto/models/Tracks.java index dcedc54311..27a35a9e34 100644 --- a/gapic/src/main/com/google/gapid/perfetto/models/Tracks.java +++ b/gapic/src/main/com/google/gapid/perfetto/models/Tracks.java @@ -21,9 +21,11 @@ import static com.google.gapid.util.MoreFutures.transformAsync; import com.google.common.collect.Lists; +import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.gapid.models.Perfetto; import com.google.gapid.perfetto.canvas.Panel; +import com.google.gapid.perfetto.models.TrackConfig.Group; import com.google.gapid.perfetto.views.CounterPanel; import com.google.gapid.perfetto.views.CpuSummaryPanel; import com.google.gapid.perfetto.views.ProcessSummaryPanel; @@ -55,14 +57,26 @@ public static ListenableFuture enumerate(Perfetto.Data.Bu } private static ListenableFuture enumerateCpu(Perfetto.Data.Builder data) { + if (data.getNumCpus() == 0) { + return Futures.immediateFuture(data); + } + CpuSummaryTrack summary = new CpuSummaryTrack(data.getNumCpus()); - data.tracks.addLabelGroup(null, summary.getId(), "CPU Usage", - group(state -> new CpuSummaryPanel(state, summary), false, (group, filtered) -> { - for (int i = 1; i < group.size(); i += 2) { - group.setVisible(i, !filtered); - } - }, true)); - return CpuTrack.enumerate(summary.getId(), data); + return transform(CpuTrack.enumerate(summary.getId(), data), configs -> { + boolean hasAnyFrequency = configs.stream().anyMatch(c -> c.hasFrequency); + Group.UiFactory ui = hasAnyFrequency ? + group(state -> new CpuSummaryPanel(state, summary), false, (group, filtered) -> { + for (int cpu = 0, track = 0; cpu < configs.size(); cpu++, track++) { + if (configs.get(cpu).hasFrequency) { + track++; + group.setVisible(track, !filtered); + } + } + }, true) : + group(state -> new CpuSummaryPanel(state, summary), false); + data.tracks.addLabelGroup(null, summary.getId(), "CPU Usage", ui); + return data; + }); } public static ListenableFuture enumerateCounters(