1 /*
2  * Copyright (C) 2017 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 #ifndef SRC_TRACED_PROBES_FTRACE_FTRACE_METADATA_H_
18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_METADATA_H_
19 
20 #include <stdint.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #include <bitset>
25 
26 #include "perfetto/base/flat_set.h"
27 #include "perfetto/base/logging.h"
28 #include "perfetto/ext/traced/data_source_types.h"
29 
30 namespace perfetto {
31 
32 using BlockDeviceID = decltype(stat::st_dev);
33 using Inode = decltype(stat::st_ino);
34 
35 // Container for tracking miscellaneous information while parsing ftrace events,
36 // scoped to an individual data source.
37 struct FtraceMetadata {
38   struct KernelAddr {
KernelAddrFtraceMetadata::KernelAddr39     KernelAddr(uint64_t _addr, uint32_t _index) : addr(_addr), index(_index) {}
40     uint64_t addr = 0;
41     uint32_t index = 0;
42 
43     // We never keep more than one KernelAddr entry per address in the set. This
44     // is really just a workaround for the lack of a FlatMap.
45     // The |index| is written only after the entry is added to the set, to have
46     // a monotonic value that reflects the insertion order.
47     friend bool operator<(const KernelAddr& lhs, const KernelAddr& rhs) {
48       return lhs.addr < rhs.addr;
49     }
50     friend bool operator==(const KernelAddr& lhs, const KernelAddr& rhs) {
51       return lhs.addr == rhs.addr;
52     }
53   };
54 
FtraceMetadataFtraceMetadata55   FtraceMetadata() {
56     // A sched_switch is 64 bytes, a page is 4096 bytes and we expect
57     // 2 pid's per sched_switch. 4096/64*2=128. Give it a 2x margin.
58     pids.reserve(256);
59 
60     // We expect to see only a small number of task rename events.
61     rename_pids.reserve(32);
62 
63     kernel_addrs.reserve(256);
64   }
65 
AddDeviceFtraceMetadata66   void AddDevice(BlockDeviceID device_id) {
67     last_seen_device_id = device_id;
68 #if PERFETTO_DCHECK_IS_ON()
69     seen_device_id = true;
70 #endif
71   }
72 
AddInodeFtraceMetadata73   void AddInode(Inode inode_number) {
74 #if PERFETTO_DCHECK_IS_ON()
75     PERFETTO_DCHECK(seen_device_id);
76 #endif
77     static int32_t cached_pid = 0;
78     if (!cached_pid)
79       cached_pid = getpid();
80 
81     PERFETTO_DCHECK(last_seen_common_pid);
82     PERFETTO_DCHECK(cached_pid == getpid());
83     // Ignore own scanning activity.
84     if (cached_pid != last_seen_common_pid) {
85       inode_and_device.insert(
86           std::make_pair(inode_number, last_seen_device_id));
87     }
88   }
89 
AddRenamePidFtraceMetadata90   void AddRenamePid(int32_t pid) { rename_pids.insert(pid); }
91 
AddPidFtraceMetadata92   void AddPid(int32_t pid) {
93     const size_t pid_bit = static_cast<size_t>(pid);
94     if (PERFETTO_LIKELY(pid_bit < pids_cache.size())) {
95       if (pids_cache.test(pid_bit))
96         return;
97       pids_cache.set(pid_bit);
98     }
99     pids.insert(pid);
100   }
101 
AddCommonPidFtraceMetadata102   void AddCommonPid(int32_t pid) {
103     last_seen_common_pid = pid;
104     AddPid(pid);
105   }
106 
107   // Returns the index of the symbol (a monotonic counter, which is set when
108   // the symbol is inserted the first time).
AddSymbolAddrFtraceMetadata109   uint32_t AddSymbolAddr(uint64_t addr) {
110     auto it_and_inserted = kernel_addrs.insert(KernelAddr(addr, 0));
111     // Deliberately prefer a branch here to always computing and passing
112     // size + 1 to the above.
113     if (it_and_inserted.second) {
114       const auto index = static_cast<uint32_t>(kernel_addrs.size());
115       it_and_inserted.first->index = index;
116     }
117     return it_and_inserted.first->index;
118   }
119 
ClearFtraceMetadata120   void Clear() {
121     inode_and_device.clear();
122     rename_pids.clear();
123     pids.clear();
124     pids_cache.reset();
125     kernel_addrs.clear();
126     last_kernel_addr_index_written = 0;
127     FinishEvent();
128   }
129 
FinishEventFtraceMetadata130   void FinishEvent() {
131     last_seen_device_id = 0;
132     last_seen_common_pid = 0;
133 #if PERFETTO_DCHECK_IS_ON()
134     seen_device_id = false;
135 #endif
136   }
137 
138   BlockDeviceID last_seen_device_id = 0;
139 #if PERFETTO_DCHECK_IS_ON()
140   bool seen_device_id = false;
141 #endif
142   int32_t last_seen_common_pid = 0;
143   uint32_t last_kernel_addr_index_written = 0;
144 
145   base::FlatSet<InodeBlockPair> inode_and_device;
146   base::FlatSet<int32_t> rename_pids;
147   base::FlatSet<int32_t> pids;
148   base::FlatSet<KernelAddr> kernel_addrs;
149 
150   // This bitmap is a cache for |pids|. It speculates on the fact that on most
151   // Android kernels, PID_MAX=32768. It saves ~1-2% cpu time on high load
152   // scenarios, as AddPid() is a very hot path.
153   std::bitset<32768> pids_cache;
154 };
155 
156 }  // namespace perfetto
157 
158 #endif  // SRC_TRACED_PROBES_FTRACE_FTRACE_METADATA_H_
159