blob: 4b7d3fed79d8636663afea0d4d71fff608b266b2 [file] [log] [blame]
Anna Zappone2a6f9042018-03-14 13:26:07 +00001/*
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 Tucci569ad312018-03-14 22:28:31 +000023#include <memory>
Anna Zappone2a6f9042018-03-14 13:26:07 +000024#include <set>
25#include <string>
Anna Zappone9ab79602018-03-27 11:57:44 +010026#include <unordered_map>
Anna Zappone2a6f9042018-03-14 13:26:07 +000027
Florian Mayer112376e2018-03-28 16:20:41 +010028#include "perfetto/base/task_runner.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010029#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 Tucci2c5488f2019-06-01 03:27:28 +010032#include "perfetto/ext/tracing/core/trace_writer.h"
Primiano Tucci0f9e0222019-06-05 09:36:41 +010033#include "perfetto/tracing/core/data_source_config.h"
Florian Mayer20c9ac72018-03-29 12:20:48 +010034#include "src/traced/probes/filesystem/file_scanner.h"
Anna Zappone2a6f9042018-03-14 13:26:07 +000035#include "src/traced/probes/filesystem/fs_mount.h"
Anna Zappone3be7b672018-03-23 17:26:10 +000036#include "src/traced/probes/filesystem/lru_inode_cache.h"
Primiano Tuccifd8240d2018-08-01 09:34:54 +010037#include "src/traced/probes/probes_data_source.h"
Anna Zappone2a6f9042018-03-14 13:26:07 +000038
Primiano Tucci355b8c82019-08-29 08:37:51 +020039#include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h"
Anna Zappone2a6f9042018-03-14 13:26:07 +000040
41namespace perfetto {
42
Anna Zappone2a6f9042018-03-14 13:26:07 +000043using InodeFileMap = protos::pbzero::InodeFileMap;
Primiano Tucci569ad312018-03-14 22:28:31 +000044class TraceWriter;
Anna Zappone2a6f9042018-03-14 13:26:07 +000045
Anna Zappone3be7b672018-03-23 17:26:10 +000046// Creates block_device_map for /system partition
47void CreateStaticDeviceToInodeMap(
Anna Zappone2a6f9042018-03-14 13:26:07 +000048 const std::string& root_directory,
Anna Zappone9ab79602018-03-27 11:57:44 +010049 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
50 static_file_map);
Anna Zappone3be7b672018-03-23 17:26:10 +000051
Primiano Tuccifd8240d2018-08-01 09:34:54 +010052class InodeFileDataSource : public ProbesDataSource,
53 public FileScanner::Delegate {
Anna Zappone2a6f9042018-03-14 13:26:07 +000054 public:
Primiano Tuccifd8240d2018-08-01 09:34:54 +010055 static constexpr int kTypeId = 2;
56
Anna Zappone3be7b672018-03-23 17:26:10 +000057 InodeFileDataSource(
Primiano Tuccidae35652018-03-29 18:32:02 +010058 DataSourceConfig,
Florian Mayer112376e2018-03-28 16:20:41 +010059 base::TaskRunner*,
Anna Zappone3be7b672018-03-23 17:26:10 +000060 TracingSessionID,
Anna Zappone9ab79602018-03-27 11:57:44 +010061 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
62 static_file_map,
Anna Zappone3be7b672018-03-23 17:26:10 +000063 LRUInodeCache* cache,
64 std::unique_ptr<TraceWriter> writer);
Hector Dearman0ff07c72018-03-15 09:54:46 +000065
Primiano Tuccifd8240d2018-08-01 09:34:54 +010066 ~InodeFileDataSource() override;
67
Hector Dearman0ff07c72018-03-15 09:54:46 +000068 base::WeakPtr<InodeFileDataSource> GetWeakPtr() const;
Anna Zappone2a6f9042018-03-14 13:26:07 +000069
Anna Zappone3be7b672018-03-23 17:26:10 +000070 // Called when Inodes are seen in the FtraceEventBundle
Florian Mayerdd889142018-04-05 16:19:37 +010071 // TODO(fmayer): Change to std::pair<BlockDeviceID, Inode>.
Anna Zappone80e098e2018-03-16 14:50:28 +000072 void OnInodes(const std::vector<std::pair<Inode, BlockDeviceID>>& inodes);
73
Anna Zappone3be7b672018-03-23 17:26:10 +000074 // Search in /system partition and add inodes to InodeFileMap proto if found
75 void AddInodesFromStaticMap(BlockDeviceID block_device_id,
Florian Mayer20c9ac72018-03-29 12:20:48 +010076 std::set<Inode>* inode_numbers);
Anna Zappone3be7b672018-03-23 17:26:10 +000077
78 // Search in LRUInodeCache and add inodes to InodeFileMap if found
79 void AddInodesFromLRUCache(BlockDeviceID block_device_id,
Florian Mayer20c9ac72018-03-29 12:20:48 +010080 std::set<Inode>* inode_numbers);
81
Florian Mayerdd889142018-04-05 16:19:37 +010082 virtual void FillInodeEntry(InodeFileMap* destination,
83 Inode inode_number,
84 const InodeMapValue& inode_map_value);
85
Primiano Tucci674076d2018-10-01 10:41:09 +010086 // ProbesDataSource implementation.
87 void Start() override;
Primiano Tucci50e9eda2018-11-20 10:52:35 +010088 void Flush(FlushRequestID, std::function<void()> callback) override;
Primiano Tucci674076d2018-10-01 10:41:09 +010089
Florian Mayerdd889142018-04-05 16:19:37 +010090 protected:
91 std::multimap<BlockDeviceID, std::string> mount_points_;
92
Anna Zappone2a6f9042018-03-14 13:26:07 +000093 private:
Florian Mayer20c9ac72018-03-29 12:20:48 +010094 InodeFileMap* AddToCurrentTracePacket(BlockDeviceID block_device_id);
Florian Mayer420b6c02018-04-03 13:37:48 +010095 void ResetTracePacket();
Florian Mayer112376e2018-03-28 16:20:41 +010096 void FindMissingInodes();
Florian Mayerdd889142018-04-05 16:19:37 +010097
98 // Callbacks for dynamic filesystem scan.
Florian Mayer20c9ac72018-03-29 12:20:48 +010099 bool OnInodeFound(BlockDeviceID block_device_id,
100 Inode inode_number,
101 const std::string& path,
Primiano Tuccifd8240d2018-08-01 09:34:54 +0100102 protos::pbzero::InodeFileMap_Entry_Type type) override;
103 void OnInodeScanDone() override;
Florian Mayerdd889142018-04-05 16:19:37 +0100104
Florian Mayer20c9ac72018-03-29 12:20:48 +0100105 void AddRootsForBlockDevice(BlockDeviceID block_device_id,
106 std::vector<std::string>* roots);
Florian Mayer5e38ea22018-04-10 18:09:11 +0100107 void RemoveFromNextMissingInodes(BlockDeviceID block_device_id,
108 Inode inode_number);
Florian Mayer112376e2018-03-28 16:20:41 +0100109
Florian Mayer18614fe2018-03-29 17:51:20 +0100110 std::set<std::string> scan_mount_points_;
111 std::map<std::string, std::vector<std::string>> mount_point_mapping_;
112
Florian Mayer112376e2018-03-28 16:20:41 +0100113 base::TaskRunner* task_runner_;
Anna Zappone9ab79602018-03-27 11:57:44 +0100114 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
115 static_file_map_;
Anna Zappone3be7b672018-03-23 17:26:10 +0000116 LRUInodeCache* cache_;
Anna Zappone2a6f9042018-03-14 13:26:07 +0000117 std::unique_ptr<TraceWriter> writer_;
Florian Mayer112376e2018-03-28 16:20:41 +0100118 std::map<BlockDeviceID, std::set<Inode>> missing_inodes_;
Florian Mayer20c9ac72018-03-29 12:20:48 +0100119 std::map<BlockDeviceID, std::set<Inode>> next_missing_inodes_;
Florian Mayerd9f39132018-04-11 14:12:43 +0100120 std::set<BlockDeviceID> seen_block_devices_;
Florian Mayer20c9ac72018-03-29 12:20:48 +0100121 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 Tucci0f2f3b42019-05-21 19:37:01 +0100126 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 Mayer20c9ac72018-03-29 12:20:48 +0100130 std::unique_ptr<FileScanner> file_scanner_;
Hector Dearman0ff07c72018-03-15 09:54:46 +0000131 base::WeakPtrFactory<InodeFileDataSource> weak_factory_; // Keep last.
Anna Zappone2a6f9042018-03-14 13:26:07 +0000132};
133
134} // namespace perfetto
135
136#endif // SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_