1 /* 2 * Copyright (C) 2018 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_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_ 18 #define SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_ 19 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <map> 23 #include <memory> 24 #include <set> 25 #include <string> 26 #include <unordered_map> 27 28 #include "perfetto/base/flat_set.h" 29 #include "perfetto/base/task_runner.h" 30 #include "perfetto/ext/base/weak_ptr.h" 31 #include "perfetto/ext/traced/data_source_types.h" 32 #include "perfetto/ext/tracing/core/basic_types.h" 33 #include "perfetto/ext/tracing/core/trace_writer.h" 34 #include "perfetto/tracing/core/data_source_config.h" 35 #include "src/traced/probes/filesystem/file_scanner.h" 36 #include "src/traced/probes/filesystem/fs_mount.h" 37 #include "src/traced/probes/filesystem/lru_inode_cache.h" 38 #include "src/traced/probes/probes_data_source.h" 39 40 #include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h" 41 42 namespace perfetto { 43 44 using InodeFileMap = protos::pbzero::InodeFileMap; 45 class TraceWriter; 46 47 // Creates block_device_map for /system partition 48 void CreateStaticDeviceToInodeMap( 49 const std::string& root_directory, 50 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* 51 static_file_map); 52 53 class InodeFileDataSource : public ProbesDataSource, 54 public FileScanner::Delegate { 55 public: 56 static const ProbesDataSource::Descriptor descriptor; 57 58 InodeFileDataSource( 59 DataSourceConfig, 60 base::TaskRunner*, 61 TracingSessionID, 62 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* 63 static_file_map, 64 LRUInodeCache* cache, 65 std::unique_ptr<TraceWriter> writer); 66 67 ~InodeFileDataSource() override; 68 69 base::WeakPtr<InodeFileDataSource> GetWeakPtr() const; 70 71 // Called when Inodes are seen in the FtraceEventBundle 72 void OnInodes(const base::FlatSet<InodeBlockPair>& inodes); 73 74 // Search in /system partition and add inodes to InodeFileMap proto if found 75 void AddInodesFromStaticMap(BlockDeviceID block_device_id, 76 std::set<Inode>* inode_numbers); 77 78 // Search in LRUInodeCache and add inodes to InodeFileMap if found 79 void AddInodesFromLRUCache(BlockDeviceID block_device_id, 80 std::set<Inode>* inode_numbers); 81 82 virtual void FillInodeEntry(InodeFileMap* destination, 83 Inode inode_number, 84 const InodeMapValue& inode_map_value); 85 86 // ProbesDataSource implementation. 87 void Start() override; 88 void Flush(FlushRequestID, std::function<void()> callback) override; 89 90 protected: 91 std::multimap<BlockDeviceID, std::string> mount_points_; 92 93 private: 94 InodeFileMap* AddToCurrentTracePacket(BlockDeviceID block_device_id); 95 void ResetTracePacket(); 96 void FindMissingInodes(); 97 98 // Callbacks for dynamic filesystem scan. 99 bool OnInodeFound(BlockDeviceID block_device_id, 100 Inode inode_number, 101 const std::string& path, 102 InodeFileMap_Entry_Type type) override; 103 void OnInodeScanDone() override; 104 105 void AddRootsForBlockDevice(BlockDeviceID block_device_id, 106 std::vector<std::string>* roots); 107 void RemoveFromNextMissingInodes(BlockDeviceID block_device_id, 108 Inode inode_number); 109 110 std::set<std::string> scan_mount_points_; 111 std::map<std::string, std::vector<std::string>> mount_point_mapping_; 112 113 base::TaskRunner* task_runner_; 114 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* 115 static_file_map_; 116 LRUInodeCache* cache_; 117 std::unique_ptr<TraceWriter> writer_; 118 std::map<BlockDeviceID, std::set<Inode>> missing_inodes_; 119 std::map<BlockDeviceID, std::set<Inode>> next_missing_inodes_; 120 std::set<BlockDeviceID> seen_block_devices_; 121 BlockDeviceID current_block_device_id_; 122 TraceWriter::TracePacketHandle current_trace_packet_; 123 InodeFileMap* current_file_map_; 124 bool has_current_trace_packet_ = false; 125 bool scan_running_ = false; 126 bool do_not_scan_ = false; 127 uint32_t scan_interval_ms_ = 0; 128 uint32_t scan_delay_ms_ = 0; 129 uint32_t scan_batch_size_ = 0; 130 std::unique_ptr<FileScanner> file_scanner_; 131 base::WeakPtrFactory<InodeFileDataSource> weak_factory_; // Keep last. 132 }; 133 134 } // namespace perfetto 135 136 #endif // SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_ 137