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. Cleared periodically, after the metadata 37 // is processed by the data sources interested in it, see 38 // |OnFtraceDataWrittenIntoDataSourceBuffers|. 39 struct FtraceMetadata { 40 struct KernelAddr { KernelAddrFtraceMetadata::KernelAddr41 KernelAddr(uint64_t _addr, uint32_t _index) : addr(_addr), index(_index) {} 42 uint64_t addr = 0; 43 uint32_t index = 0; 44 45 // We never keep more than one KernelAddr entry per address in the set. This 46 // is really just a workaround for the lack of a FlatMap. 47 // The |index| is written only after the entry is added to the set, to have 48 // a monotonic value that reflects the insertion order. 49 friend bool operator<(const KernelAddr& lhs, const KernelAddr& rhs) { 50 return lhs.addr < rhs.addr; 51 } 52 friend bool operator==(const KernelAddr& lhs, const KernelAddr& rhs) { 53 return lhs.addr == rhs.addr; 54 } 55 }; 56 FtraceMetadataFtraceMetadata57 FtraceMetadata() { 58 // A sched_switch is 64 bytes, a page is 4096 bytes and we expect 59 // 2 pid's per sched_switch. 4096/64*2=128. Give it a 2x margin. 60 pids.reserve(256); 61 62 // We expect to see only a small number of task rename events. 63 rename_pids.reserve(32); 64 65 kernel_addrs.reserve(256); 66 } 67 AddDeviceFtraceMetadata68 void AddDevice(BlockDeviceID device_id) { 69 last_seen_device_id = device_id; 70 #if PERFETTO_DCHECK_IS_ON() 71 seen_device_id = true; 72 #endif 73 } 74 AddInodeFtraceMetadata75 void AddInode(Inode inode_number) { 76 #if PERFETTO_DCHECK_IS_ON() 77 PERFETTO_DCHECK(seen_device_id); 78 #endif 79 static int32_t cached_pid = 0; 80 if (!cached_pid) 81 cached_pid = getpid(); 82 83 PERFETTO_DCHECK(last_seen_common_pid); 84 PERFETTO_DCHECK(cached_pid == getpid()); 85 // Ignore own scanning activity. 86 if (cached_pid != last_seen_common_pid) { 87 inode_and_device.insert( 88 std::make_pair(inode_number, last_seen_device_id)); 89 } 90 } 91 AddRenamePidFtraceMetadata92 void AddRenamePid(int32_t pid) { rename_pids.insert(pid); } 93 AddPidFtraceMetadata94 void AddPid(int32_t pid) { 95 const size_t pid_bit = static_cast<size_t>(pid); 96 if (PERFETTO_LIKELY(pid_bit < pids_cache.size())) { 97 if (pids_cache.test(pid_bit)) 98 return; 99 pids_cache.set(pid_bit); 100 } 101 pids.insert(pid); 102 } 103 AddCommonPidFtraceMetadata104 void AddCommonPid(int32_t pid) { 105 last_seen_common_pid = pid; 106 AddPid(pid); 107 } 108 109 // Returns the index of the symbol (a monotonic counter, which is set when 110 // the symbol is inserted the first time). AddSymbolAddrFtraceMetadata111 uint32_t AddSymbolAddr(uint64_t addr) { 112 auto it_and_inserted = kernel_addrs.insert(KernelAddr(addr, 0)); 113 // Deliberately prefer a branch here to always computing and passing 114 // size + 1 to the above. 115 if (it_and_inserted.second) { 116 const auto index = static_cast<uint32_t>(kernel_addrs.size()); 117 it_and_inserted.first->index = index; 118 } 119 return it_and_inserted.first->index; 120 } 121 ClearFtraceMetadata122 void Clear() { 123 inode_and_device.clear(); 124 rename_pids.clear(); 125 pids.clear(); 126 pids_cache.reset(); 127 kernel_addrs.clear(); 128 fds.clear(); 129 last_kernel_addr_index_written = 0; 130 FinishEvent(); 131 } 132 FinishEventFtraceMetadata133 void FinishEvent() { 134 last_seen_device_id = 0; 135 last_seen_common_pid = 0; 136 #if PERFETTO_DCHECK_IS_ON() 137 seen_device_id = false; 138 #endif 139 } 140 141 BlockDeviceID last_seen_device_id = 0; 142 #if PERFETTO_DCHECK_IS_ON() 143 bool seen_device_id = false; 144 #endif 145 int32_t last_seen_common_pid = 0; 146 uint32_t last_kernel_addr_index_written = 0; 147 148 base::FlatSet<InodeBlockPair> inode_and_device; 149 base::FlatSet<int32_t> rename_pids; 150 base::FlatSet<int32_t> pids; 151 base::FlatSet<KernelAddr> kernel_addrs; 152 base::FlatSet<std::pair<pid_t, uint64_t>> fds; 153 154 // This bitmap is a cache for |pids|. It speculates on the fact that on most 155 // Android kernels, PID_MAX=32768. It saves ~1-2% cpu time on high load 156 // scenarios, as AddPid() is a very hot path. 157 std::bitset<32768> pids_cache; 158 }; 159 160 } // namespace perfetto 161 162 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_METADATA_H_ 163