• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/event_tracker.h"
26 #include "src/trace_processor/importers/common/slice_tracker.h"
27 #include "src/trace_processor/importers/common/track_tracker.h"
28 #include "src/trace_processor/importers/common/tracks.h"
29 #include "src/trace_processor/importers/common/tracks_common.h"
30 #include "src/trace_processor/storage/trace_storage.h"
31 #include "src/trace_processor/tables/slice_tables_py.h"
32 
33 namespace perfetto::trace_processor {
34 
GpuWorkPeriodTracker(TraceProcessorContext * context)35 GpuWorkPeriodTracker::GpuWorkPeriodTracker(TraceProcessorContext* context)
36     : context_(context) {}
37 
ParseGpuWorkPeriodEvent(int64_t timestamp,protozero::ConstBytes blob)38 void GpuWorkPeriodTracker::ParseGpuWorkPeriodEvent(int64_t timestamp,
39                                                    protozero::ConstBytes blob) {
40   protos::pbzero::GpuWorkPeriodFtraceEvent::Decoder evt(blob);
41 
42   static constexpr auto kTrackBlueprint = tracks::SliceBlueprint(
43       "android_gpu_work_period",
44       tracks::DimensionBlueprints(tracks::kGpuDimensionBlueprint,
45                                   tracks::kUidDimensionBlueprint));
46   TrackId track_id = context_->track_tracker->InternTrack(
47       kTrackBlueprint, {evt.gpu_id(), static_cast<int32_t>(evt.uid())});
48 
49   const auto duration =
50       static_cast<int64_t>(evt.end_time_ns() - evt.start_time_ns());
51   const auto active_duration =
52       static_cast<int64_t>(evt.total_active_duration_ns());
53   const double active_percent = 100.0 * (static_cast<double>(active_duration) /
54                                          static_cast<double>(duration));
55 
56   base::StackString<255> entry_name("%%%.2f", active_percent);
57   StringId entry_name_id =
58       context_->storage->InternString(entry_name.string_view());
59 
60   tables::SliceTable::Row row;
61   row.ts = timestamp;
62   row.dur = duration;
63   row.track_id = track_id;
64   row.category = kNullStringId;
65   row.name = entry_name_id;
66   auto slice_id = context_->slice_tracker->Scoped(
67       timestamp, track_id, kNullStringId, entry_name_id, duration);
68   if (slice_id) {
69     auto rr = context_->storage->mutable_slice_table()->FindById(*slice_id);
70     rr->set_thread_ts(timestamp);
71     rr->set_thread_dur(active_duration);
72   }
73 }
74 
75 }  // namespace perfetto::trace_processor
76