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