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" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 29 | #include "perfetto/ext/base/weak_ptr.h" |
| 30 | #include "perfetto/ext/traced/data_source_types.h" |
| 31 | #include "perfetto/ext/tracing/core/basic_types.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 32 | #include "perfetto/ext/tracing/core/trace_writer.h" |
Primiano Tucci | 0f9e022 | 2019-06-05 09:36:41 +0100 | [diff] [blame] | 33 | #include "perfetto/tracing/core/data_source_config.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" |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 37 | #include "src/traced/probes/probes_data_source.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 38 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame^] | 39 | #include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 40 | |
| 41 | namespace perfetto { |
| 42 | |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 43 | using InodeFileMap = protos::pbzero::InodeFileMap; |
Primiano Tucci | 569ad31 | 2018-03-14 22:28:31 +0000 | [diff] [blame] | 44 | class TraceWriter; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 45 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 46 | // Creates block_device_map for /system partition |
| 47 | void CreateStaticDeviceToInodeMap( |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 48 | const std::string& root_directory, |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 49 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* |
| 50 | static_file_map); |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 51 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 52 | class InodeFileDataSource : public ProbesDataSource, |
| 53 | public FileScanner::Delegate { |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 54 | public: |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 55 | static constexpr int kTypeId = 2; |
| 56 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 57 | InodeFileDataSource( |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 58 | DataSourceConfig, |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 59 | base::TaskRunner*, |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 60 | TracingSessionID, |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 61 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* |
| 62 | static_file_map, |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 63 | LRUInodeCache* cache, |
| 64 | std::unique_ptr<TraceWriter> writer); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 65 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 66 | ~InodeFileDataSource() override; |
| 67 | |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 68 | base::WeakPtr<InodeFileDataSource> GetWeakPtr() const; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 69 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 70 | // Called when Inodes are seen in the FtraceEventBundle |
Florian Mayer | dd88914 | 2018-04-05 16:19:37 +0100 | [diff] [blame] | 71 | // TODO(fmayer): Change to std::pair<BlockDeviceID, Inode>. |
Anna Zappone | 80e098e | 2018-03-16 14:50:28 +0000 | [diff] [blame] | 72 | void OnInodes(const std::vector<std::pair<Inode, BlockDeviceID>>& inodes); |
| 73 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 74 | // Search in /system partition and add inodes to InodeFileMap proto if found |
| 75 | void AddInodesFromStaticMap(BlockDeviceID block_device_id, |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 76 | std::set<Inode>* inode_numbers); |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 77 | |
| 78 | // Search in LRUInodeCache and add inodes to InodeFileMap if found |
| 79 | void AddInodesFromLRUCache(BlockDeviceID block_device_id, |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 80 | std::set<Inode>* inode_numbers); |
| 81 | |
Florian Mayer | dd88914 | 2018-04-05 16:19:37 +0100 | [diff] [blame] | 82 | virtual void FillInodeEntry(InodeFileMap* destination, |
| 83 | Inode inode_number, |
| 84 | const InodeMapValue& inode_map_value); |
| 85 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 86 | // ProbesDataSource implementation. |
| 87 | void Start() override; |
Primiano Tucci | 50e9eda | 2018-11-20 10:52:35 +0100 | [diff] [blame] | 88 | void Flush(FlushRequestID, std::function<void()> callback) override; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 89 | |
Florian Mayer | dd88914 | 2018-04-05 16:19:37 +0100 | [diff] [blame] | 90 | protected: |
| 91 | std::multimap<BlockDeviceID, std::string> mount_points_; |
| 92 | |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 93 | private: |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 94 | InodeFileMap* AddToCurrentTracePacket(BlockDeviceID block_device_id); |
Florian Mayer | 420b6c0 | 2018-04-03 13:37:48 +0100 | [diff] [blame] | 95 | void ResetTracePacket(); |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 96 | void FindMissingInodes(); |
Florian Mayer | dd88914 | 2018-04-05 16:19:37 +0100 | [diff] [blame] | 97 | |
| 98 | // Callbacks for dynamic filesystem scan. |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 99 | bool OnInodeFound(BlockDeviceID block_device_id, |
| 100 | Inode inode_number, |
| 101 | const std::string& path, |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 102 | protos::pbzero::InodeFileMap_Entry_Type type) override; |
| 103 | void OnInodeScanDone() override; |
Florian Mayer | dd88914 | 2018-04-05 16:19:37 +0100 | [diff] [blame] | 104 | |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 105 | void AddRootsForBlockDevice(BlockDeviceID block_device_id, |
| 106 | std::vector<std::string>* roots); |
Florian Mayer | 5e38ea2 | 2018-04-10 18:09:11 +0100 | [diff] [blame] | 107 | void RemoveFromNextMissingInodes(BlockDeviceID block_device_id, |
| 108 | Inode inode_number); |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 109 | |
Florian Mayer | 18614fe | 2018-03-29 17:51:20 +0100 | [diff] [blame] | 110 | std::set<std::string> scan_mount_points_; |
| 111 | std::map<std::string, std::vector<std::string>> mount_point_mapping_; |
| 112 | |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 113 | base::TaskRunner* task_runner_; |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 114 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>* |
| 115 | static_file_map_; |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 116 | LRUInodeCache* cache_; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 117 | std::unique_ptr<TraceWriter> writer_; |
Florian Mayer | 112376e | 2018-03-28 16:20:41 +0100 | [diff] [blame] | 118 | std::map<BlockDeviceID, std::set<Inode>> missing_inodes_; |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 119 | std::map<BlockDeviceID, std::set<Inode>> next_missing_inodes_; |
Florian Mayer | d9f3913 | 2018-04-11 14:12:43 +0100 | [diff] [blame] | 120 | std::set<BlockDeviceID> seen_block_devices_; |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 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; |
Primiano Tucci | 0f2f3b4 | 2019-05-21 19:37:01 +0100 | [diff] [blame] | 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; |
Florian Mayer | 20c9ac7 | 2018-03-29 12:20:48 +0100 | [diff] [blame] | 130 | std::unique_ptr<FileScanner> file_scanner_; |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 131 | base::WeakPtrFactory<InodeFileDataSource> weak_factory_; // Keep last. |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace perfetto |
| 135 | |
| 136 | #endif // SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_ |