• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
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);
84 
85     member = static_cast<uint32_t>(rss.member());
86     size = rss.size();
87     curr = std::make_optional(static_cast<bool>(rss.curr()));
88     mm_id = std::make_optional(rss.mm_id());
89 
90     ParseRssStat(ts, pid, size, member, curr, mm_id);
91   } else {
92     PERFETTO_DFATAL("Unexpected field id");
93   }
94 }
95 
ParseRssStat(int64_t ts,uint32_t pid,int64_t size,uint32_t member,std::optional<bool> curr,std::optional<int64_t> mm_id)96 void RssStatTracker::ParseRssStat(int64_t ts,
97                                   uint32_t pid,
98                                   int64_t size,
99                                   uint32_t member,
100                                   std::optional<bool> curr,
101                                   std::optional<int64_t> mm_id) {
102   const char* memory_key = GetProcessMemoryKey(member);
103   if (!memory_key) {
104     context_->storage->IncrementStats(stats::rss_stat_unknown_keys);
105     return;
106   }
107   if (size < 0) {
108     context_->storage->IncrementStats(stats::rss_stat_negative_size);
109     return;
110   }
111 
112   std::optional<UniqueTid> utid;
113   if (mm_id.has_value() && curr.has_value()) {
114     utid = FindUtidForMmId(*mm_id, *curr, pid);
115   } else {
116     utid = context_->process_tracker->GetOrCreateThread(pid);
117   }
118 
119   if (utid) {
120     context_->event_tracker->PushProcessCounterForThread(
121         EventTracker::RssStat{memory_key}, ts, static_cast<double>(size),
122         *utid);
123   } else {
124     context_->storage->IncrementStats(stats::rss_stat_unknown_thread_for_mm_id);
125   }
126 }
127 
FindUtidForMmId(int64_t mm_id,bool is_curr,uint32_t pid)128 std::optional<UniqueTid> RssStatTracker::FindUtidForMmId(int64_t mm_id,
129                                                          bool is_curr,
130                                                          uint32_t pid) {
131   // If curr is true, we can just overwrite the state in the map and return
132   // the utid correspodning to |pid|.
133   if (is_curr) {
134     UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
135     mm_id_to_utid_[mm_id] = utid;
136     return utid;
137   }
138 
139   // If curr is false, try and lookup the utid we previously saw for this
140   // mm id.
141   auto* it = mm_id_to_utid_.Find(mm_id);
142   if (!it)
143     return std::nullopt;
144 
145   // If the utid in the map is the same as our current utid but curr is false,
146   // that means we are in the middle of a process changing mm structs (i.e. in
147   // the middle of a vfork + exec). Therefore, we should discard the association
148   // of this vm struct with this thread.
149   const UniqueTid mm_utid = *it;
150   const UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
151   if (mm_utid == utid) {
152     mm_id_to_utid_.Erase(mm_id);
153     return std::nullopt;
154   }
155 
156   // Verify that the utid in the map is still alive. This can happen if an mm
157   // struct we saw in the past is about to be reused after thread but we don't
158   // know the new process that struct will be associated with.
159   if (!context_->process_tracker->IsThreadAlive(mm_utid)) {
160     mm_id_to_utid_.Erase(mm_id);
161     return std::nullopt;
162   }
163 
164   // This case happens when a process is changing the VM of another process and
165   // we know that the utid corresponding to the target process. Just return that
166   // utid.
167   return mm_utid;
168 }
169 
170 }  // namespace perfetto::trace_processor
171