Skip to content

Commit

Permalink
Fix handling of CPU/CPU Frequency track presence.
Browse files Browse the repository at this point in the history
- Don't crash if no CPU tracks are present
- Don't assume the presence of CPU frequency tracks
  • Loading branch information
pmuetschard committed Jul 26, 2019
1 parent a3a4010 commit 5b9df9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
16 changes: 14 additions & 2 deletions gapic/src/main/com/google/gapid/perfetto/models/CpuTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Perfetto.Data.Builder> enumerate(
public static ListenableFuture<List<CpuConfig>> enumerate(
String parent, Perfetto.Data.Builder data) {
return transform(freqMap(data.qe), freqMap -> {
List<CpuConfig> configs = Lists.newArrayList();
for (int i = 0; i < data.getNumCpus(); i++) {
QueryEngine.Row freq = freqMap.get(Long.valueOf(i));
CpuTrack track = new CpuTrack(i);
Expand All @@ -161,15 +162,26 @@ public static ListenableFuture<Perfetto.Data.Builder> 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;
});
}

private static ListenableFuture<Map<Long, QueryEngine.Row>> freqMap(QueryEngine qe) {
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.
Expand Down
28 changes: 21 additions & 7 deletions gapic/src/main/com/google/gapid/perfetto/models/Tracks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,14 +57,26 @@ public static ListenableFuture<Perfetto.Data.Builder> enumerate(Perfetto.Data.Bu
}

private static ListenableFuture<Perfetto.Data.Builder> 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<Perfetto.Data.Builder> enumerateCounters(
Expand Down

0 comments on commit 5b9df9b

Please sign in to comment.