xref: /aosp_15_r20/external/perfetto/src/traced/probes/filesystem/inode_file_data_source_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #include "src/traced/probes/filesystem/inode_file_data_source.h"
18 
19 #include "perfetto/protozero/scattered_heap_buffer.h"
20 #include "src/base/test/test_task_runner.h"
21 #include "src/base/test/utils.h"
22 #include "src/traced/probes/filesystem/lru_inode_cache.h"
23 #include "src/tracing/core/null_trace_writer.h"
24 
25 #include "test/gtest_and_gmock.h"
26 
27 #include "protos/perfetto/config/inode_file/inode_file_config.pbzero.h"
28 #include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h"
29 
30 namespace perfetto {
31 namespace {
32 
33 using ::testing::_;
34 using ::testing::Eq;
35 using ::testing::InvokeWithoutArgs;
36 using ::testing::IsNull;
37 using ::testing::Pointee;
38 
39 class TestInodeFileDataSource : public InodeFileDataSource {
40  public:
TestInodeFileDataSource(DataSourceConfig cfg,base::TaskRunner * task_runner,TracingSessionID tsid,std::map<BlockDeviceID,std::unordered_map<Inode,InodeMapValue>> * static_file_map,LRUInodeCache * cache,std::unique_ptr<TraceWriter> writer)41   TestInodeFileDataSource(
42       DataSourceConfig cfg,
43       base::TaskRunner* task_runner,
44       TracingSessionID tsid,
45       std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>*
46           static_file_map,
47       LRUInodeCache* cache,
48       std::unique_ptr<TraceWriter> writer)
49       : InodeFileDataSource(std::move(cfg),
50                             task_runner,
51                             tsid,
52                             static_file_map,
53                             cache,
54                             std::move(writer)) {
55     struct stat buf;
56     PERFETTO_CHECK(
57         lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata")
58                   .c_str(),
59               &buf) != -1);
60     mount_points_.emplace(
61         buf.st_dev,
62         base::GetTestDataPath("src/traced/probes/filesystem/testdata"));
63   }
64 
65   MOCK_METHOD(void,
66               FillInodeEntry,
67               (InodeFileMap * destination,
68                Inode inode_number,
69                const InodeMapValue& inode_map_value),
70               (override));
71 };
72 
73 class InodeFileDataSourceTest : public ::testing::Test {
74  protected:
InodeFileDataSourceTest()75   InodeFileDataSourceTest() {}
76 
GetInodeFileDataSource(DataSourceConfig cfg)77   std::unique_ptr<TestInodeFileDataSource> GetInodeFileDataSource(
78       DataSourceConfig cfg) {
79     return std::unique_ptr<TestInodeFileDataSource>(new TestInodeFileDataSource(
80         cfg, &task_runner_, 0, &static_file_map_, &cache_,
81         std::unique_ptr<NullTraceWriter>(new NullTraceWriter)));
82   }
83 
84   LRUInodeCache cache_{100};
85   std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>
86       static_file_map_;
87   base::TestTaskRunner task_runner_;
88 };
89 
TEST_F(InodeFileDataSourceTest,TestFileSystemScan)90 TEST_F(InodeFileDataSourceTest, TestFileSystemScan) {
91   DataSourceConfig ds_config;
92   protozero::HeapBuffered<protos::pbzero::InodeFileConfig> inode_cfg;
93   inode_cfg->set_scan_interval_ms(1);
94   inode_cfg->set_scan_delay_ms(1);
95   ds_config.set_inode_file_config_raw(inode_cfg.SerializeAsString());
96   auto data_source = GetInodeFileDataSource(ds_config);
97 
98   struct stat buf;
99   PERFETTO_CHECK(
100       lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
101                 .c_str(),
102             &buf) != -1);
103 
104   auto done = task_runner_.CreateCheckpoint("done");
105   InodeMapValue value(
106       protos::pbzero::InodeFileMap::Entry::Type::FILE,
107       {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
108   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)))
109       .WillOnce(InvokeWithoutArgs(done));
110 
111   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
112   task_runner_.RunUntilCheckpoint("done");
113 
114   // Expect that the found inode is added in the LRU cache.
115   EXPECT_THAT(cache_.Get(std::make_pair(buf.st_dev, buf.st_ino)),
116               Pointee(Eq(value)));
117 }
118 
TEST_F(InodeFileDataSourceTest,TestStaticMap)119 TEST_F(InodeFileDataSourceTest, TestStaticMap) {
120   DataSourceConfig config;
121   auto data_source = GetInodeFileDataSource(config);
122   CreateStaticDeviceToInodeMap(
123       base::GetTestDataPath("src/traced/probes/filesystem/testdata"),
124       &static_file_map_);
125 
126   struct stat buf;
127   PERFETTO_CHECK(
128       lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
129                 .c_str(),
130             &buf) != -1);
131 
132   InodeMapValue value(
133       protos::pbzero::InodeFileMap::Entry::Type::FILE,
134       {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
135   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)));
136 
137   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
138   // Expect that the found inode is not added the LRU cache.
139   EXPECT_THAT(cache_.Get(std::make_pair(buf.st_dev, buf.st_ino)), IsNull());
140 }
141 
TEST_F(InodeFileDataSourceTest,TestCache)142 TEST_F(InodeFileDataSourceTest, TestCache) {
143   DataSourceConfig config;
144   auto data_source = GetInodeFileDataSource(config);
145   CreateStaticDeviceToInodeMap(
146       base::GetTestDataPath("src/traced/probes/filesystem/testdata"),
147       &static_file_map_);
148 
149   struct stat buf;
150   PERFETTO_CHECK(
151       lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
152                 .c_str(),
153             &buf) != -1);
154 
155   InodeMapValue value(
156       protos::pbzero::InodeFileMap::Entry::Type::FILE,
157       {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
158   cache_.Insert(std::make_pair(buf.st_dev, buf.st_ino), value);
159 
160   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)));
161 
162   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
163 }
164 
165 }  // namespace
166 }  // namespace perfetto
167