1 /*
2 * Copyright (C) 2023 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
17 #include "src/trace_processor/importers/ftrace/gpu_work_period_tracker.h"
18
19 #include <cstdint>
20
21 #include "perfetto/ext/base/string_utils.h"
22 #include "perfetto/protozero/field.h"
23 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
24 #include "protos/perfetto/trace/ftrace/power.pbzero.h"
25 #include "src/trace_processor/importers/common/async_track_set_tracker.h"
26 #include "src/trace_processor/importers/common/event_tracker.h"
27 #include "src/trace_processor/importers/common/slice_tracker.h"
28 #include "src/trace_processor/importers/common/track_tracker.h"
29 #include "src/trace_processor/importers/common/tracks.h"
30 #include "src/trace_processor/importers/common/tracks_common.h"
31 #include "src/trace_processor/storage/trace_storage.h"
32 #include "src/trace_processor/tables/slice_tables_py.h"
33
34 namespace perfetto::trace_processor {
35
GpuWorkPeriodTracker(TraceProcessorContext * context)36 GpuWorkPeriodTracker::GpuWorkPeriodTracker(TraceProcessorContext* context)
37 : context_(context) {}
38
ParseGpuWorkPeriodEvent(int64_t timestamp,protozero::ConstBytes blob)39 void GpuWorkPeriodTracker::ParseGpuWorkPeriodEvent(int64_t timestamp,
40 protozero::ConstBytes blob) {
41 protos::pbzero::GpuWorkPeriodFtraceEvent::Decoder evt(blob);
42
43 static constexpr auto kTrackBlueprint = tracks::SliceBlueprint(
44 "android_gpu_work_period",
45 tracks::DimensionBlueprints(tracks::kGpuDimensionBlueprint,
46 tracks::kUidDimensionBlueprint));
47 TrackId track_id = context_->track_tracker->InternTrack(
48 kTrackBlueprint, {evt.gpu_id(), static_cast<int32_t>(evt.uid())});
49
50 const auto duration =
51 static_cast<int64_t>(evt.end_time_ns() - evt.start_time_ns());
52 const auto active_duration =
53 static_cast<int64_t>(evt.total_active_duration_ns());
54 const double active_percent = 100.0 * (static_cast<double>(active_duration) /
55 static_cast<double>(duration));
56
57 base::StackString<255> entry_name("%%%.2f", active_percent);
58 StringId entry_name_id =
59 context_->storage->InternString(entry_name.string_view());
60
61 tables::SliceTable::Row row;
62 row.ts = timestamp;
63 row.dur = duration;
64 row.track_id = track_id;
65 row.category = kNullStringId;
66 row.name = entry_name_id;
67 row.thread_ts = timestamp;
68 row.thread_dur = active_duration;
69 context_->slice_tracker->ScopedTyped(context_->storage->mutable_slice_table(),
70 row);
71 }
72
73 } // namespace perfetto::trace_processor
74