Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 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> |
Primiano Tucci | 569ad31 | 2018-03-14 22:28:31 +0000 | [diff] [blame] | 23 | #include <memory> |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 24 | #include <set> |
| 25 | #include <string> |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 26 | #include <unordered_map> |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 27 | |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 28 | #include "perfetto/base/task_runner.h" |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 29 | #include "perfetto/base/weak_ptr.h" |
Hector Dearman | cd3b6d3 | 2018-03-20 11:35:20 +0000 | [diff] [blame] | 30 | #include "perfetto/traced/data_source_types.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 31 | #include "perfetto/tracing/core/basic_types.h" |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 32 | #include "perfetto/tracing/core/data_source_config.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 33 | #include "perfetto/tracing/core/trace_writer.h" |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 34 | #include "src/traced/probes/filesystem/file_scanner.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 35 | #include "src/traced/probes/filesystem/fs_mount.h" |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 36 | #include "src/traced/probes/filesystem/lru_inode_cache.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 37 | |
| 38 | #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" |
| 39 | |
| 40 | namespace perfetto { |
| 41 | |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 42 | using InodeFileMap = protos::pbzero::InodeFileMap; |
Primiano Tucci | 569ad31 | 2018-03-14 22:28:31 +0000 | [diff] [blame] | 43 | class TraceWriter; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 44 | |
Florian Mayer | 7ca14bf | 2018-03-20 14:13:02 +0000 | [diff] [blame] | 45 | void ScanFilesDFS( |
| 46 | const std::string& root_directory, |
Florian Mayer | fe28afd | 2018-03-22 16:24:10 +0000 | [diff] [blame] | 47 | const std::function<bool(BlockDeviceID block_device_id, |
Florian Mayer | 7ca14bf | 2018-03-20 14:13:02 +0000 | [diff] [blame] | 48 | Inode inode_number, |
| 49 | const std::string& path, |
| 50 | protos::pbzero::InodeFileMap_Entry_Type type)>&); |
| 51 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 52 | // Creates block_device_map for /system partition |
| 53 | void CreateStaticDeviceToInodeMap( |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 54 | const std::string& root_directory, |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 55 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* |
| 56 | static_file_map); |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 57 | |
| 58 | void FillInodeEntry(InodeFileMap* destination, |
| 59 | Inode inode_number, |
| 60 | const InodeMapValue& inode_map_value); |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 61 | |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 62 | class InodeFileDataSource : public FileScanner::Delegate { |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 63 | public: |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 64 | InodeFileDataSource( |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 65 | DataSourceConfig, |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 66 | base::TaskRunner*, |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 67 | TracingSessionID, |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 68 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* |
| 69 | static_file_map, |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 70 | LRUInodeCache* cache, |
| 71 | std::unique_ptr<TraceWriter> writer); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 72 | |
| 73 | TracingSessionID session_id() const { return session_id_; } |
| 74 | base::WeakPtr<InodeFileDataSource> GetWeakPtr() const; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 75 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 76 | // Called when Inodes are seen in the FtraceEventBundle |
Anna Zappone | 80e098e | 2018-03-16 14:50:28 +0000 | [diff] [blame] | 77 | void OnInodes(const std::vector<std::pair<Inode, BlockDeviceID>>& inodes); |
| 78 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 79 | // Search in /system partition and add inodes to InodeFileMap proto if found |
| 80 | void AddInodesFromStaticMap(BlockDeviceID block_device_id, |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 81 | std::set<Inode>* inode_numbers); |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 82 | |
| 83 | // Search in LRUInodeCache and add inodes to InodeFileMap if found |
| 84 | void AddInodesFromLRUCache(BlockDeviceID block_device_id, |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 85 | std::set<Inode>* inode_numbers); |
| 86 | |
| 87 | virtual ~InodeFileDataSource() {} |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 90 | InodeFileMap* AddToCurrentTracePacket(BlockDeviceID block_device_id); |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 91 | void FindMissingInodes(); |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 92 | bool OnInodeFound(BlockDeviceID block_device_id, |
| 93 | Inode inode_number, |
| 94 | const std::string& path, |
| 95 | protos::pbzero::InodeFileMap_Entry_Type type); |
| 96 | void OnInodeScanDone(); |
| 97 | void AddRootsForBlockDevice(BlockDeviceID block_device_id, |
| 98 | std::vector<std::string>* roots); |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 99 | |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 100 | uint64_t GetScanIntervalMs(); |
| 101 | uint64_t GetScanDelayMs(); |
| 102 | uint64_t GetScanBatchSize(); |
| 103 | |
| 104 | const DataSourceConfig source_config_; |
Florian Mayer | 18614fe | 2018-03-29 17:51:20 +0100 | [diff] [blame^] | 105 | std::set<std::string> scan_mount_points_; |
| 106 | std::map<std::string, std::vector<std::string>> mount_point_mapping_; |
| 107 | |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 108 | base::TaskRunner* task_runner_; |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 109 | const TracingSessionID session_id_; |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 110 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* |
| 111 | static_file_map_; |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 112 | LRUInodeCache* cache_; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 113 | std::multimap<BlockDeviceID, std::string> mount_points_; |
| 114 | std::unique_ptr<TraceWriter> writer_; |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 115 | std::map<BlockDeviceID, std::set<Inode>> missing_inodes_; |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 116 | std::map<BlockDeviceID, std::set<Inode>> next_missing_inodes_; |
| 117 | BlockDeviceID current_block_device_id_; |
| 118 | TraceWriter::TracePacketHandle current_trace_packet_; |
| 119 | InodeFileMap* current_file_map_; |
| 120 | bool has_current_trace_packet_ = false; |
| 121 | bool scan_running_ = false; |
| 122 | std::unique_ptr<FileScanner> file_scanner_; |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 123 | base::WeakPtrFactory<InodeFileDataSource> weak_factory_; // Keep last. |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // namespace perfetto |
| 127 | |
| 128 | #endif // SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_ |