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);
60
61 context_->slice_tracker->End(
62 timestamp, details_track, kNullStringId, kswapd_efficiency_name_,
63 [this, &decoder](ArgsTracker::BoundInserter* inserter) {
64 if (decoder.has_delta_nr_scanned()) {
65 inserter->AddArg(
66 pages_scanned_name_,
67 Variadic::UnsignedInteger(decoder.delta_nr_scanned()));
68 }
69 if (decoder.has_delta_nr_reclaimed()) {
70 inserter->AddArg(
71 pages_reclaimed_name_,
72 Variadic::UnsignedInteger(decoder.delta_nr_reclaimed()));
73 }
74
75 if (decoder.has_delta_nr_reclaimed() &&
76 decoder.has_delta_nr_scanned()) {
77 double efficiency =
78 static_cast<double>(decoder.delta_nr_reclaimed()) * 100 /
79 static_cast<double>(decoder.delta_nr_scanned());
80
81 inserter->AddArg(efficiency_pct_name_,
82 Variadic::UnsignedInteger(
83 static_cast<uint64_t>(std::round(efficiency))));
84 }
85 });
86 }
87
88 } // namespace perfetto::trace_processor
89