Skip to content

Commit cae15ed

Browse files
committed
trace_processor: Parse adreno_cmdbatch_retired/sync events
Add custom parsing for kgsl adreno_cmdbatch_sync and adreno_cmdbatch_retired ftrace events to produce GPU timeline slices in the trace processor. The sync event provides a per-cmdbatch reference point (ftrace timestamp paired with GPU ticks) used to convert GPU tick values from the retired event into trace timestamps. This avoids cross-clock-domain issues with the submitted event's secs/usecs (CLOCK_REALTIME) fields.
1 parent f68b128 commit cae15ed

5 files changed

Lines changed: 138 additions & 0 deletions

File tree

src/trace_processor/importers/ftrace/ftrace_parser.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ FtraceParser::FtraceParser(TraceProcessorContext* context,
542542
gpu_power_state_off_id_(context->storage->InternString("OFF")),
543543
gpu_power_state_pg_id_(context->storage->InternString("PG")),
544544
gpu_power_state_on_id_(context->storage->InternString("ON")),
545+
gpu_cmdbatch_slice_name_id_(context->storage->InternString("GPU")),
545546
ddic_underrun_id_(context_->storage->InternString("ddic_underrun")),
546547
memcg_reclaim_order_id_(
547548
context->storage->InternString("memcg_reclaim_order")),
@@ -939,6 +940,14 @@ base::Status FtraceParser::ParseFtraceEvent(uint32_t cpu,
939940
ParseKgslGpuFreq(ts, fld_bytes);
940941
break;
941942
}
943+
case FtraceEvent::kKgslAdrenoCmdbatchSyncFieldNumber: {
944+
ParseKgslAdrenoCmdbatchSync(ts, fld_bytes);
945+
break;
946+
}
947+
case FtraceEvent::kKgslAdrenoCmdbatchRetiredFieldNumber: {
948+
ParseKgslAdrenoCmdbatchRetired(ts, fld_bytes);
949+
break;
950+
}
942951
case FtraceEvent::kCpuIdleFieldNumber: {
943952
ParseCpuIdle(ts, fld_bytes);
944953
break;
@@ -1931,6 +1940,53 @@ void FtraceParser::ParseKgslGpuFreq(int64_t timestamp, ConstBytes blob) {
19311940
context_->event_tracker->PushCounter(timestamp, new_freq, track);
19321941
}
19331942

1943+
void FtraceParser::ParseKgslAdrenoCmdbatchSync(int64_t timestamp,
1944+
protozero::ConstBytes data) {
1945+
protos::pbzero::KgslAdrenoCmdbatchSyncFtraceEvent::Decoder evt(data);
1946+
adreno_cmdbatch_sync_points_.Insert(
1947+
evt.timestamp(), AdrenoCmdbatchSyncPoint{timestamp, evt.ticks()});
1948+
}
1949+
1950+
void FtraceParser::ParseKgslAdrenoCmdbatchRetired(int64_t timestamp,
1951+
protozero::ConstBytes data) {
1952+
protos::pbzero::KgslAdrenoCmdbatchRetiredFtraceEvent::Decoder evt(data);
1953+
1954+
static constexpr auto kBlueprint = TrackCompressor::SliceBlueprint(
1955+
"adreno_gpu_cmdbatch",
1956+
tracks::DimensionBlueprints(tracks::UintDimensionBlueprint("context_id"),
1957+
tracks::UintDimensionBlueprint("prio")),
1958+
tracks::FnNameBlueprint([](uint32_t context_id, uint32_t prio) {
1959+
return base::StackString<64>("GPU (Ctx=%u, Prio=%u)", context_id, prio);
1960+
}));
1961+
1962+
if (evt.retire() < evt.start()) {
1963+
return;
1964+
}
1965+
1966+
// Adreno GPU ticks run at 19.2 MHz, fixed across all Qualcomm mobile SoCs
1967+
// (see KGSL_XO_CLK_FREQ in kgsl_pwrctrl.h).
1968+
constexpr int64_t kAdrenoXoFreqHz = 19200000;
1969+
const int64_t duration = static_cast<int64_t>(evt.retire() - evt.start()) *
1970+
1000000000 / kAdrenoXoFreqHz;
1971+
1972+
int64_t gpu_start_ts = timestamp;
1973+
auto* sync = adreno_cmdbatch_sync_points_.Find(evt.timestamp());
1974+
if (sync) {
1975+
gpu_start_ts =
1976+
sync->trace_ts + static_cast<int64_t>(evt.start() - sync->gpu_ticks) *
1977+
1000000000 / kAdrenoXoFreqHz;
1978+
adreno_cmdbatch_sync_points_.Erase(evt.timestamp());
1979+
}
1980+
1981+
const uint32_t context_id = evt.id();
1982+
TrackId track_id = context_->track_compressor->InternScoped(
1983+
kBlueprint,
1984+
tracks::Dimensions(context_id, static_cast<uint32_t>(evt.prio())),
1985+
gpu_start_ts, duration);
1986+
context_->slice_tracker->Scoped(gpu_start_ts, track_id, kNullStringId,
1987+
gpu_cmdbatch_slice_name_id_, duration);
1988+
}
1989+
19341990
void FtraceParser::ParseCpuIdle(int64_t timestamp, ConstBytes blob) {
19351991
protos::pbzero::CpuIdleFtraceEvent::Decoder idle(blob);
19361992
TrackId track = context_->track_tracker->InternTrack(

src/trace_processor/importers/ftrace/ftrace_parser.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class FtraceParser {
9595
void ParseCpuFreqThrottle(int64_t timestamp, protozero::ConstBytes);
9696
void ParseGpuFreq(int64_t timestamp, protozero::ConstBytes);
9797
void ParseKgslGpuFreq(int64_t timestamp, protozero::ConstBytes);
98+
void ParseKgslAdrenoCmdbatchSync(int64_t timestamp, protozero::ConstBytes);
99+
void ParseKgslAdrenoCmdbatchRetired(int64_t timestamp, protozero::ConstBytes);
98100
void ParseCpuIdle(int64_t timestamp, protozero::ConstBytes);
99101
void ParsePrint(int64_t timestamp, uint32_t pid, protozero::ConstBytes);
100102
void ParseZero(int64_t timestamp, uint32_t pid, protozero::ConstBytes);
@@ -463,6 +465,15 @@ class FtraceParser {
463465
const StringId gpu_power_state_off_id_;
464466
const StringId gpu_power_state_pg_id_;
465467
const StringId gpu_power_state_on_id_;
468+
469+
struct AdrenoCmdbatchSyncPoint {
470+
int64_t trace_ts;
471+
uint64_t gpu_ticks;
472+
};
473+
base::FlatHashMap<uint32_t, AdrenoCmdbatchSyncPoint>
474+
adreno_cmdbatch_sync_points_;
475+
476+
const StringId gpu_cmdbatch_slice_name_id_;
466477
const StringId ddic_underrun_id_;
467478
std::array<StringId, 8> f2fs_checkpoint_reason_ids_;
468479

test/trace_processor/diff_tests/include_index.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from diff_tests.parser.etm.tests import Etm
7676
from diff_tests.parser.etw.tests import Etw
7777
from diff_tests.parser.fs.tests import Fs
78+
from diff_tests.parser.ftrace.adreno_cmdbatch_tests import AdrenoCmdbatch
7879
from diff_tests.parser.ftrace.block_io_tests import BlockIo
7980
from diff_tests.parser.ftrace.ftrace_crop_tests import FtraceCrop
8081
from diff_tests.parser.ftrace.kprobes_tests import Kprobes
@@ -266,6 +267,7 @@ def fetch_all_diff_tests(
266267
ParsingRssStats,
267268
ParsingSysStats,
268269
ParsingMemoryCounters,
270+
AdrenoCmdbatch,
269271
BlockIo,
270272
FtraceCrop,
271273
Kprobes,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2024 The Android Open Source Project
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from python.generators.diff_tests.testing import Csv, TextProto
17+
from python.generators.diff_tests.testing import DiffTestBlueprint
18+
from python.generators.diff_tests.testing import TestSuite
19+
20+
21+
class AdrenoCmdbatch(TestSuite):
22+
23+
def test_adreno_cmdbatch_retired_slice(self):
24+
return DiffTestBlueprint(
25+
trace=TextProto(r"""
26+
packet { trusted_packet_sequence_id: 1 ftrace_events {
27+
cpu: 0
28+
event {
29+
timestamp: 1000000000
30+
pid: 100
31+
kgsl_adreno_cmdbatch_sync {
32+
id: 1
33+
timestamp: 42
34+
ticks: 19200000
35+
prio: 0
36+
}
37+
}
38+
event {
39+
timestamp: 3100000000
40+
pid: 100
41+
kgsl_adreno_cmdbatch_retired {
42+
id: 1
43+
timestamp: 42
44+
start: 38400000
45+
retire: 57600000
46+
prio: 0
47+
}
48+
}
49+
}}
50+
"""),
51+
query="""
52+
SELECT
53+
slice.name as name,
54+
slice.ts as ts,
55+
slice.dur as dur,
56+
track.name as track_name
57+
FROM slice
58+
JOIN track ON slice.track_id = track.id
59+
WHERE track.type = 'adreno_gpu_cmdbatch'
60+
""",
61+
out=Csv("""
62+
"name","ts","dur","track_name"
63+
"GPU",2000000000,1000000000,"GPU (Ctx=1, Prio=0)"
64+
"""))

ui/src/plugins/dev.perfetto.TraceProcessorTrack/slice_tracks.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ export const SLICE_TRACK_SCHEMAS: ReadonlyArray<SliceTrackTypeSchema> = [
258258
topLevelGroup: 'HARDWARE',
259259
group: undefined,
260260
},
261+
{
262+
type: 'adreno_gpu_cmdbatch',
263+
topLevelGroup: 'GPU',
264+
group: 'Adreno Cmdbatch',
265+
},
261266
{
262267
type: 'triggers',
263268
topLevelGroup: 'SYSTEM',

0 commit comments

Comments
 (0)