Home | History | Annotate | Download | only in filesystem
      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 #include "src/traced/probes/probes_data_source.h"
     38 
     39 #include "perfetto/trace/filesystem/inode_file_map.pbzero.h"
     40 
     41 namespace perfetto {
     42 
     43 using InodeFileMap = protos::pbzero::InodeFileMap;
     44 class TraceWriter;
     45 
     46 // Creates block_device_map for /system partition
     47 void CreateStaticDeviceToInodeMap(
     48     const std::string& root_directory,
     49     std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
     50         static_file_map);
     51 
     52 class InodeFileDataSource : public ProbesDataSource,
     53                             public FileScanner::Delegate {
     54  public:
     55   static constexpr int kTypeId = 2;
     56 
     57   InodeFileDataSource(
     58       DataSourceConfig,
     59       base::TaskRunner*,
     60       TracingSessionID,
     61       std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
     62           static_file_map,
     63       LRUInodeCache* cache,
     64       std::unique_ptr<TraceWriter> writer);
     65 
     66   ~InodeFileDataSource() override;
     67 
     68   base::WeakPtr<InodeFileDataSource> GetWeakPtr() const;
     69 
     70   // Called when Inodes are seen in the FtraceEventBundle
     71   // TODO(fmayer): Change  to std::pair<BlockDeviceID, Inode>.
     72   void OnInodes(const std::vector<std::pair<Inode, BlockDeviceID>>& 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                     protos::pbzero::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   uint32_t GetScanIntervalMs() const;
    111   uint32_t GetScanDelayMs() const;
    112   uint32_t GetScanBatchSize() const;
    113 
    114   const DataSourceConfig source_config_;
    115   std::set<std::string> scan_mount_points_;
    116   std::map<std::string, std::vector<std::string>> mount_point_mapping_;
    117 
    118   base::TaskRunner* task_runner_;
    119   std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
    120       static_file_map_;
    121   LRUInodeCache* cache_;
    122   std::unique_ptr<TraceWriter> writer_;
    123   std::map<BlockDeviceID, std::set<Inode>> missing_inodes_;
    124   std::map<BlockDeviceID, std::set<Inode>> next_missing_inodes_;
    125   std::set<BlockDeviceID> seen_block_devices_;
    126   BlockDeviceID current_block_device_id_;
    127   TraceWriter::TracePacketHandle current_trace_packet_;
    128   InodeFileMap* current_file_map_;
    129   bool has_current_trace_packet_ = false;
    130   bool scan_running_ = false;
    131   std::unique_ptr<FileScanner> file_scanner_;
    132   base::WeakPtrFactory<InodeFileDataSource> weak_factory_;  // Keep last.
    133 };
    134 
    135 }  // namespace perfetto
    136 
    137 #endif  // SRC_TRACED_PROBES_FILESYSTEM_INODE_FILE_DATA_SOURCE_H_
    138