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/task_runner.h" 29 #include "perfetto/base/weak_ptr.h" 30 #include "perfetto/traced/data_source_types.h" 31 #include "perfetto/tracing/core/basic_types.h" 32 #include "perfetto/tracing/core/data_source_config.h" 33 #include "perfetto/tracing/core/trace_writer.h" 34 #include "src/traced/probes/filesystem/file_scanner.h" 35 #include "src/traced/probes/filesystem/fs_mount.h" 36 #include "src/traced/probes/filesystem/lru_inode_cache.h" 37 38 #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" 39 40 namespace perfetto { 41 42 using InodeFileMap = protos::pbzero::InodeFileMap; 43 class TraceWriter; 44 45 // Creates block_device_map for /system partition 46 void CreateStaticDeviceToInodeMap( 47 const std::string& root_directory, 48 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* 49 static_file_map); 50 51 class InodeFileDataSource : public FileScanner::Delegate { 52 public: 53 InodeFileDataSource( 54 DataSourceConfig, 55 base::TaskRunner*, 56 TracingSessionID, 57 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* 58 static_file_map, 59 LRUInodeCache* cache, 60 std::unique_ptr<TraceWriter> writer); 61 session_id()62 TracingSessionID session_id() const { return session_id_; } 63 base::WeakPtr<InodeFileDataSource> GetWeakPtr() const; 64 65 // Called when Inodes are seen in the FtraceEventBundle 66 // TODO(fmayer): Change to std::pair<BlockDeviceID, Inode>. 67 void OnInodes(const std::vector<std::pair<Inode, BlockDeviceID>>& inodes); 68 69 // Search in /system partition and add inodes to InodeFileMap proto if found 70 void AddInodesFromStaticMap(BlockDeviceID block_device_id, 71 std::set<Inode>* inode_numbers); 72 73 // Search in LRUInodeCache and add inodes to InodeFileMap if found 74 void AddInodesFromLRUCache(BlockDeviceID block_device_id, 75 std::set<Inode>* inode_numbers); 76 77 void Flush(); 78 79 virtual ~InodeFileDataSource(); 80 81 virtual void FillInodeEntry(InodeFileMap* destination, 82 Inode inode_number, 83 const InodeMapValue& inode_map_value); 84 85 protected: 86 std::multimap<BlockDeviceID, std::string> mount_points_; 87 88 private: 89 InodeFileMap* AddToCurrentTracePacket(BlockDeviceID block_device_id); 90 void ResetTracePacket(); 91 void FindMissingInodes(); 92 93 // Callbacks for dynamic filesystem scan. 94 bool OnInodeFound(BlockDeviceID block_device_id, 95 Inode inode_number, 96 const std::string& path, 97 protos::pbzero::InodeFileMap_Entry_Type type); 98 void OnInodeScanDone(); 99 100 void AddRootsForBlockDevice(BlockDeviceID block_device_id, 101 std::vector<std::string>* roots); 102 void RemoveFromNextMissingInodes(BlockDeviceID block_device_id, 103 Inode inode_number); 104 105 uint32_t GetScanIntervalMs() const; 106 uint32_t GetScanDelayMs() const; 107 uint32_t GetScanBatchSize() const; 108 109 const DataSourceConfig source_config_; 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 const TracingSessionID session_id_; 115 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* 116 static_file_map_; 117 LRUInodeCache* cache_; 118 std::unique_ptr<TraceWriter> writer_; 119 std::map<BlockDeviceID, std::set<Inode>> missing_inodes_; 120 std::map<BlockDeviceID, std::set<Inode>> next_missing_inodes_; 121 std::set<BlockDeviceID> seen_block_devices_; 122 BlockDeviceID current_block_device_id_; 123 TraceWriter::TracePacketHandle current_trace_packet_; 124 InodeFileMap* current_file_map_; 125 bool has_current_trace_packet_ = false; 126 bool scan_running_ = false; 127 std::unique_ptr<FileScanner> file_scanner_; 128 base::WeakPtrFactory<InodeFileDataSource> weak_factory_; // Keep last. 129 }; 130 131 } // namespace perfetto 132 133 #endif // SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_ 134