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 "src/trace_processor/event_tracker.h"
20 #include "src/trace_processor/process_tracker.h"
21 #include "src/trace_processor/trace_processor_context.h"
22
23 #include "protos/perfetto/trace/ftrace/kmem.pbzero.h"
24
25 namespace perfetto {
26 namespace trace_processor {
27
RssStatTracker(TraceProcessorContext * context)28 RssStatTracker::RssStatTracker(TraceProcessorContext* context)
29 : context_(context) {
30 rss_members_.emplace_back(context->storage->InternString("mem.rss.file"));
31 rss_members_.emplace_back(context->storage->InternString("mem.rss.anon"));
32 rss_members_.emplace_back(context->storage->InternString("mem.swap"));
33 rss_members_.emplace_back(context->storage->InternString("mem.rss.shmem"));
34 rss_members_.emplace_back(
35 context->storage->InternString("mem.rss.unknown")); // Keep this last.
36 }
37
ParseRssStat(int64_t ts,uint32_t pid,ConstBytes blob)38 void RssStatTracker::ParseRssStat(int64_t ts, uint32_t pid, ConstBytes blob) {
39 protos::pbzero::RssStatFtraceEvent::Decoder rss(blob.data, blob.size);
40 const auto kRssStatUnknown = static_cast<uint32_t>(rss_members_.size()) - 1;
41 auto member = static_cast<uint32_t>(rss.member());
42 int64_t size = rss.size();
43 if (member >= rss_members_.size()) {
44 context_->storage->IncrementStats(stats::rss_stat_unknown_keys);
45 member = kRssStatUnknown;
46 }
47
48 if (size < 0) {
49 context_->storage->IncrementStats(stats::rss_stat_negative_size);
50 return;
51 }
52
53 base::Optional<UniqueTid> utid;
54 if (rss.has_mm_id()) {
55 PERFETTO_DCHECK(rss.has_curr());
56 utid = FindUtidForMmId(rss.mm_id(), rss.curr(), pid);
57 } else {
58 utid = context_->process_tracker->GetOrCreateThread(pid);
59 }
60
61 if (utid) {
62 context_->event_tracker->PushProcessCounterForThread(
63 ts, size, rss_members_[member], *utid);
64 } else {
65 context_->storage->IncrementStats(stats::rss_stat_unknown_thread_for_mm_id);
66 }
67 }
68
FindUtidForMmId(int64_t mm_id,bool is_curr,uint32_t pid)69 base::Optional<UniqueTid> RssStatTracker::FindUtidForMmId(int64_t mm_id,
70 bool is_curr,
71 uint32_t pid) {
72 // If curr is true, we can just overwrite the state in the map and return
73 // the utid correspodning to |pid|.
74 if (is_curr) {
75 UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
76 mm_id_to_utid_[mm_id] = utid;
77 return utid;
78 }
79
80 // If curr is false, try and lookup the utid we previously saw for this
81 // mm id.
82 auto it = mm_id_to_utid_.find(mm_id);
83 if (it == mm_id_to_utid_.end())
84 return base::nullopt;
85
86 // If the utid in the map is the same as our current utid but curr is false,
87 // that means we are in the middle of a process changing mm structs (i.e. in
88 // the middle of a vfork + exec). Therefore, we should discard the association
89 // of this vm struct with this thread.
90 UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
91 if (it->second == utid) {
92 mm_id_to_utid_.erase(it);
93 return base::nullopt;
94 }
95
96 // Verify that the utid in the map is still alive. This can happen if an mm
97 // struct we saw in the past is about to be reused after thread but we don't
98 // know the new process that struct will be associated with.
99 if (!context_->process_tracker->IsThreadAlive(it->second)) {
100 mm_id_to_utid_.erase(it);
101 return base::nullopt;
102 }
103
104 // This case happens when a process is changing the VM of another process and
105 // we know that the utid corresponding to the target process. Just return that
106 // utid.
107 return it->second;
108 }
109
110 } // namespace trace_processor
111 } // namespace perfetto
112