• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::Eq;
34 using ::testing::InvokeWithoutArgs;
35 using ::testing::IsNull;
36 using ::testing::Pointee;
37 using ::testing::_;
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_METHOD3(FillInodeEntry,
66                void(InodeFileMap* destination,
67                     Inode inode_number,
68                     const InodeMapValue& inode_map_value));
69 };
70 
71 class InodeFileDataSourceTest : public ::testing::Test {
72  protected:
InodeFileDataSourceTest()73   InodeFileDataSourceTest() {}
74 
GetInodeFileDataSource(DataSourceConfig cfg)75   std::unique_ptr<TestInodeFileDataSource> GetInodeFileDataSource(
76       DataSourceConfig cfg) {
77     return std::unique_ptr<TestInodeFileDataSource>(new TestInodeFileDataSource(
78         cfg, &task_runner_, 0, &static_file_map_, &cache_,
79         std::unique_ptr<NullTraceWriter>(new NullTraceWriter)));
80   }
81 
82   LRUInodeCache cache_{100};
83   std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>
84       static_file_map_;
85   base::TestTaskRunner task_runner_;
86 };
87 
TEST_F(InodeFileDataSourceTest,TestFileSystemScan)88 TEST_F(InodeFileDataSourceTest, TestFileSystemScan) {
89   DataSourceConfig ds_config;
90   protozero::HeapBuffered<protos::pbzero::InodeFileConfig> inode_cfg;
91   inode_cfg->set_scan_interval_ms(1);
92   inode_cfg->set_scan_delay_ms(1);
93   ds_config.set_inode_file_config_raw(inode_cfg.SerializeAsString());
94   auto data_source = GetInodeFileDataSource(ds_config);
95 
96   struct stat buf;
97   PERFETTO_CHECK(
98       lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
99                 .c_str(),
100             &buf) != -1);
101 
102   auto done = task_runner_.CreateCheckpoint("done");
103   InodeMapValue value(
104       protos::pbzero::InodeFileMap_Entry_Type_FILE,
105       {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
106   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)))
107       .WillOnce(InvokeWithoutArgs(done));
108 
109   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
110   task_runner_.RunUntilCheckpoint("done");
111 
112   // Expect that the found inode is added in the LRU cache.
113   EXPECT_THAT(cache_.Get(std::make_pair(buf.st_dev, buf.st_ino)),
114               Pointee(Eq(value)));
115 }
116 
TEST_F(InodeFileDataSourceTest,TestStaticMap)117 TEST_F(InodeFileDataSourceTest, TestStaticMap) {
118   DataSourceConfig config;
119   auto data_source = GetInodeFileDataSource(config);
120   CreateStaticDeviceToInodeMap(
121       base::GetTestDataPath("src/traced/probes/filesystem/testdata"),
122       &static_file_map_);
123 
124   struct stat buf;
125   PERFETTO_CHECK(
126       lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
127                 .c_str(),
128             &buf) != -1);
129 
130   InodeMapValue value(
131       protos::pbzero::InodeFileMap_Entry_Type_FILE,
132       {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
133   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)));
134 
135   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
136   // Expect that the found inode is not added the the LRU cache.
137   EXPECT_THAT(cache_.Get(std::make_pair(buf.st_dev, buf.st_ino)), IsNull());
138 }
139 
TEST_F(InodeFileDataSourceTest,TestCache)140 TEST_F(InodeFileDataSourceTest, TestCache) {
141   DataSourceConfig config;
142   auto data_source = GetInodeFileDataSource(config);
143   CreateStaticDeviceToInodeMap(
144       base::GetTestDataPath("src/traced/probes/filesystem/testdata"),
145       &static_file_map_);
146 
147   struct stat buf;
148   PERFETTO_CHECK(
149       lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
150                 .c_str(),
151             &buf) != -1);
152 
153   InodeMapValue value(
154       protos::pbzero::InodeFileMap_Entry_Type_FILE,
155       {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
156   cache_.Insert(std::make_pair(buf.st_dev, buf.st_ino), value);
157 
158   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)));
159 
160   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
161 }
162 
163 }  // namespace
164 }  // namespace perfetto
165