1 /*
2 * Copyright (C) 2020 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/ftrace/discover_vendor_tracepoints.h"
18
19 #include <vector>
20
21 #include "test/gtest_and_gmock.h"
22
23 #include "src/traced/probes/ftrace/atrace_hal_wrapper.h"
24 #include "src/traced/probes/ftrace/atrace_wrapper.h"
25 #include "src/traced/probes/ftrace/ftrace_procfs.h"
26
27 using testing::_;
28 using testing::AnyNumber;
29 using testing::ElementsAre;
30 using testing::NiceMock;
31 using testing::Return;
32 using testing::Sequence;
33
34 namespace perfetto {
35 namespace vendor_tracepoints {
36 namespace {
37
38 class MockHal : public AtraceHalWrapper {
39 public:
MockHal()40 MockHal() : AtraceHalWrapper() {}
41 MOCK_METHOD0(ListCategories, std::vector<std::string>());
42 MOCK_METHOD1(EnableCategories, bool(const std::vector<std::string>&));
43 MOCK_METHOD0(DisableAllCategories, bool());
44 };
45
46 class MockFtraceProcfs : public FtraceProcfs {
47 public:
MockFtraceProcfs()48 MockFtraceProcfs() : FtraceProcfs("/root/") {
49 ON_CALL(*this, NumberOfCpus()).WillByDefault(Return(1));
50 ON_CALL(*this, WriteToFile(_, _)).WillByDefault(Return(true));
51 ON_CALL(*this, ClearFile(_)).WillByDefault(Return(true));
52 EXPECT_CALL(*this, NumberOfCpus()).Times(AnyNumber());
53 }
54
55 MOCK_METHOD2(WriteToFile,
56 bool(const std::string& path, const std::string& str));
57 MOCK_METHOD2(AppendToFile,
58 bool(const std::string& path, const std::string& str));
59 MOCK_METHOD1(ReadOneCharFromFile, char(const std::string& path));
60 MOCK_METHOD1(ClearFile, bool(const std::string& path));
61 MOCK_CONST_METHOD1(ReadFileIntoString, std::string(const std::string& path));
62 MOCK_METHOD0(ReadEnabledEvents, std::vector<std::string>());
63 MOCK_CONST_METHOD0(NumberOfCpus, size_t());
64 MOCK_CONST_METHOD1(GetEventNamesForGroup,
65 const std::set<std::string>(const std::string& path));
66 };
67
TEST(DiscoverVendorTracepointsTest,DiscoverTracepointsTest)68 TEST(DiscoverVendorTracepointsTest, DiscoverTracepointsTest) {
69 MockHal hal;
70 MockFtraceProcfs ftrace;
71 Sequence s;
72
73 EXPECT_CALL(ftrace, WriteToFile("/root/events/enable", "0"))
74 .InSequence(s)
75 .WillOnce(Return(true));
76 EXPECT_CALL(hal, EnableCategories(ElementsAre("gfx")))
77 .InSequence(s)
78 .WillOnce(Return(true));
79 EXPECT_CALL(ftrace, ReadEnabledEvents())
80 .InSequence(s)
81 .WillOnce(Return(std::vector<std::string>({"foo/bar", "a/b"})));
82 EXPECT_CALL(hal, DisableAllCategories()).InSequence(s).WillOnce(Return(true));
83 EXPECT_CALL(ftrace, WriteToFile("/root/events/enable", "0"))
84 .InSequence(s)
85 .WillOnce(Return(true));
86
87 EXPECT_THAT(DiscoverTracepoints(&hal, &ftrace, "gfx"),
88 ElementsAre(GroupAndName("foo", "bar"), GroupAndName("a", "b")));
89 }
90
91 } // namespace
92 } // namespace vendor_tracepoints
93 } // namespace perfetto
94