xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/ftrace/pixel_mm_kswapd_event_tracker.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
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 
17 #include "src/trace_processor/importers/ftrace/pixel_mm_kswapd_event_tracker.h"
18 
19 #include <cmath>
20 #include <cstdint>
21 
22 #include "perfetto/protozero/field.h"
23 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
24 #include "protos/perfetto/trace/ftrace/pixel_mm.pbzero.h"
25 #include "src/trace_processor/importers/common/process_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/storage/trace_storage.h"
29 #include "src/trace_processor/types/variadic.h"
30 
31 namespace perfetto::trace_processor {
32 
PixelMmKswapdEventTracker(TraceProcessorContext * context)33 PixelMmKswapdEventTracker::PixelMmKswapdEventTracker(
34     TraceProcessorContext* context)
35     : context_(context),
36       kswapd_efficiency_name_(
37           context->storage->InternString("kswapd_efficiency")),
38       efficiency_pct_name_(context->storage->InternString("efficiency %")),
39       pages_scanned_name_(context->storage->InternString("pages scanned")),
40       pages_reclaimed_name_(context->storage->InternString("pages reclaimed")) {
41 }
42 
ParsePixelMmKswapdWake(int64_t timestamp,uint32_t pid)43 void PixelMmKswapdEventTracker::ParsePixelMmKswapdWake(int64_t timestamp,
44                                                        uint32_t pid) {
45   UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
46   TrackId details_track = context_->track_tracker->InternThreadTrack(utid);
47 
48   context_->slice_tracker->Begin(timestamp, details_track, kNullStringId,
49                                  kswapd_efficiency_name_);
50 }
51 
ParsePixelMmKswapdDone(int64_t timestamp,uint32_t pid,protozero::ConstBytes blob)52 void PixelMmKswapdEventTracker::ParsePixelMmKswapdDone(
53     int64_t timestamp,
54     uint32_t pid,
55     protozero::ConstBytes blob) {
56   UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
57   TrackId details_track = context_->track_tracker->InternThreadTrack(utid);
58 
59   protos::pbzero::PixelMmKswapdDoneFtraceEvent::Decoder decoder(blob.data,
60                                                                 blob.size);
61 
62   context_->slice_tracker->End(
63       timestamp, details_track, kNullStringId, kswapd_efficiency_name_,
64       [this, &decoder](ArgsTracker::BoundInserter* inserter) {
65         if (decoder.has_delta_nr_scanned()) {
66           inserter->AddArg(
67               pages_scanned_name_,
68               Variadic::UnsignedInteger(decoder.delta_nr_scanned()));
69         }
70         if (decoder.has_delta_nr_reclaimed()) {
71           inserter->AddArg(
72               pages_reclaimed_name_,
73               Variadic::UnsignedInteger(decoder.delta_nr_reclaimed()));
74         }
75 
76         if (decoder.has_delta_nr_reclaimed() &&
77             decoder.has_delta_nr_scanned()) {
78           double efficiency =
79               static_cast<double>(decoder.delta_nr_reclaimed()) * 100 /
80               static_cast<double>(decoder.delta_nr_scanned());
81 
82           inserter->AddArg(efficiency_pct_name_,
83                            Variadic::UnsignedInteger(
84                                static_cast<uint64_t>(std::round(efficiency))));
85         }
86       });
87 }
88 
89 }  // namespace perfetto::trace_processor
90