1 /*
2 * Copyright (C) 2019 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/rss_stat_tracker.h"
18
19 #include <cstdint>
20 #include <optional>
21
22 #include "perfetto/base/logging.h"
23 #include "src/trace_processor/importers/common/event_tracker.h"
24 #include "src/trace_processor/importers/common/process_tracker.h"
25 #include "src/trace_processor/storage/stats.h"
26 #include "src/trace_processor/storage/trace_storage.h"
27 #include "src/trace_processor/types/trace_processor_context.h"
28
29 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
30 #include "protos/perfetto/trace/ftrace/kmem.pbzero.h"
31 #include "protos/perfetto/trace/ftrace/synthetic.pbzero.h"
32
33 namespace perfetto::trace_processor {
34 namespace {
35
36 using FtraceEvent = protos::pbzero::FtraceEvent;
37
GetProcessMemoryKey(uint32_t member)38 const char* GetProcessMemoryKey(uint32_t member) {
39 switch (member) {
40 case 0:
41 return "rss.file";
42 case 1:
43 return "rss.anon";
44 case 2:
45 return "swap";
46 case 3:
47 return "rss.shmem";
48 case 4:
49 return "unreclaimable";
50 default:
51 return "unknown";
52 }
53 }
54
55 } // namespace
56
RssStatTracker(TraceProcessorContext * context)57 RssStatTracker::RssStatTracker(TraceProcessorContext* context)
58 : context_(context) {}
59
ParseRssStat(int64_t ts,uint32_t field_id,uint32_t pid,ConstBytes blob)60 void RssStatTracker::ParseRssStat(int64_t ts,
61 uint32_t field_id,
62 uint32_t pid,
63 ConstBytes blob) {
64 uint32_t member;
65 int64_t size;
66 std::optional<bool> curr;
67 std::optional<int64_t> mm_id;
68
69 if (field_id == FtraceEvent::kRssStatFieldNumber) {
70 protos::pbzero::RssStatFtraceEvent::Decoder rss(blob.data, blob.size);
71
72 member = static_cast<uint32_t>(rss.member());
73 size = rss.size();
74 if (rss.has_curr()) {
75 curr = std::make_optional(static_cast<bool>(rss.curr()));
76 }
77 if (rss.has_mm_id()) {
78 mm_id = std::make_optional(rss.mm_id());
79 }
80
81 ParseRssStat(ts, pid, size, member, curr, mm_id);
82 } else if (field_id == FtraceEvent::kRssStatThrottledFieldNumber) {
83 protos::pbzero::RssStatThrottledFtraceEvent::Decoder rss(blob.data,
84 blob.size);
85
86 member = static_cast<uint32_t>(rss.member());
87 size = rss.size();
88 curr = std::make_optional(static_cast<bool>(rss.curr()));
89 mm_id = std::make_optional(rss.mm_id());
90
91 ParseRssStat(ts, pid, size, member, curr, mm_id);
92 } else {
93 PERFETTO_DFATAL("Unexpected field id");
94 }
95 }
96
ParseRssStat(int64_t ts,uint32_t pid,int64_t size,uint32_t member,std::optional<bool> curr,std::optional<int64_t> mm_id)97 void RssStatTracker::ParseRssStat(int64_t ts,
98 uint32_t pid,
99 int64_t size,
100 uint32_t member,
101 std::optional<bool> curr,
102 std::optional<int64_t> mm_id) {
103 const char* memory_key = GetProcessMemoryKey(member);
104 if (!memory_key) {
105 context_->storage->IncrementStats(stats::rss_stat_unknown_keys);
106 return;
107 }
108 if (size < 0) {
109 context_->storage->IncrementStats(stats::rss_stat_negative_size);
110 return;
111 }
112
113 std::optional<UniqueTid> utid;
114 if (mm_id.has_value() && curr.has_value()) {
115 utid = FindUtidForMmId(*mm_id, *curr, pid);
116 } else {
117 utid = context_->process_tracker->GetOrCreateThread(pid);
118 }
119
120 if (utid) {
121 context_->event_tracker->PushProcessCounterForThread(
122 EventTracker::RssStat{memory_key}, ts, static_cast<double>(size),
123 *utid);
124 } else {
125 context_->storage->IncrementStats(stats::rss_stat_unknown_thread_for_mm_id);
126 }
127 }
128
FindUtidForMmId(int64_t mm_id,bool is_curr,uint32_t pid)129 std::optional<UniqueTid> RssStatTracker::FindUtidForMmId(int64_t mm_id,
130 bool is_curr,
131 uint32_t pid) {
132 // If curr is true, we can just overwrite the state in the map and return
133 // the utid correspodning to |pid|.
134 if (is_curr) {
135 UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
136 mm_id_to_utid_[mm_id] = utid;
137 return utid;
138 }
139
140 // If curr is false, try and lookup the utid we previously saw for this
141 // mm id.
142 auto* it = mm_id_to_utid_.Find(mm_id);
143 if (!it)
144 return std::nullopt;
145
146 // If the utid in the map is the same as our current utid but curr is false,
147 // that means we are in the middle of a process changing mm structs (i.e. in
148 // the middle of a vfork + exec). Therefore, we should discard the association
149 // of this vm struct with this thread.
150 const UniqueTid mm_utid = *it;
151 const UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
152 if (mm_utid == utid) {
153 mm_id_to_utid_.Erase(mm_id);
154 return std::nullopt;
155 }
156
157 // Verify that the utid in the map is still alive. This can happen if an mm
158 // struct we saw in the past is about to be reused after thread but we don't
159 // know the new process that struct will be associated with.
160 if (!context_->process_tracker->IsThreadAlive(mm_utid)) {
161 mm_id_to_utid_.Erase(mm_id);
162 return std::nullopt;
163 }
164
165 // This case happens when a process is changing the VM of another process and
166 // we know that the utid corresponding to the target process. Just return that
167 // utid.
168 return mm_utid;
169 }
170
171 } // namespace perfetto::trace_processor
172