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 { FtraceMetadataFtraceMetadata38 FtraceMetadata() { 39 // A sched_switch is 64 bytes, a page is 4096 bytes and we expect 40 // 2 pid's per sched_switch. 4096/64*2=128. Give it a 2x margin. 41 pids.reserve(256); 42 43 // We expect to see only a small number of task rename events. 44 rename_pids.reserve(32); 45 } 46 AddDeviceFtraceMetadata47 void AddDevice(BlockDeviceID device_id) { 48 last_seen_device_id = device_id; 49 #if PERFETTO_DCHECK_IS_ON() 50 seen_device_id = true; 51 #endif 52 } 53 AddInodeFtraceMetadata54 void AddInode(Inode inode_number) { 55 #if PERFETTO_DCHECK_IS_ON() 56 PERFETTO_DCHECK(seen_device_id); 57 #endif 58 static int32_t cached_pid = 0; 59 if (!cached_pid) 60 cached_pid = getpid(); 61 62 PERFETTO_DCHECK(last_seen_common_pid); 63 PERFETTO_DCHECK(cached_pid == getpid()); 64 // Ignore own scanning activity. 65 if (cached_pid != last_seen_common_pid) { 66 inode_and_device.insert( 67 std::make_pair(inode_number, last_seen_device_id)); 68 } 69 } 70 AddRenamePidFtraceMetadata71 void AddRenamePid(int32_t pid) { rename_pids.insert(pid); } 72 AddPidFtraceMetadata73 void AddPid(int32_t pid) { 74 const size_t pid_bit = static_cast<size_t>(pid); 75 if (PERFETTO_LIKELY(pid_bit < pids_cache.size())) { 76 if (pids_cache.test(pid_bit)) 77 return; 78 pids_cache.set(pid_bit); 79 } 80 pids.insert(pid); 81 } 82 AddCommonPidFtraceMetadata83 void AddCommonPid(int32_t pid) { 84 last_seen_common_pid = pid; 85 AddPid(pid); 86 } 87 ClearFtraceMetadata88 void Clear() { 89 inode_and_device.clear(); 90 rename_pids.clear(); 91 pids.clear(); 92 pids_cache.reset(); 93 FinishEvent(); 94 } 95 FinishEventFtraceMetadata96 void FinishEvent() { 97 last_seen_device_id = 0; 98 last_seen_common_pid = 0; 99 #if PERFETTO_DCHECK_IS_ON() 100 seen_device_id = false; 101 #endif 102 } 103 104 BlockDeviceID last_seen_device_id = 0; 105 #if PERFETTO_DCHECK_IS_ON() 106 bool seen_device_id = false; 107 #endif 108 int32_t last_seen_common_pid = 0; 109 110 base::FlatSet<InodeBlockPair> inode_and_device; 111 base::FlatSet<int32_t> rename_pids; 112 base::FlatSet<int32_t> pids; 113 114 // This bitmap is a cache for |pids|. It speculates on the fact that on most 115 // Android kernels, PID_MAX=32768. It saves ~1-2% cpu time on high load 116 // scenarios, as AddPid() is a very hot path. 117 std::bitset<32768> pids_cache; 118 }; 119 120 } // namespace perfetto 121 122 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_METADATA_H_ 123