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/system_info/system_info_data_source.h"
18 #include "src/traced/probes/common/cpu_freq_info_for_testing.h"
19 #include "src/tracing/core/trace_writer_for_testing.h"
20 #include "test/gtest_and_gmock.h"
21
22 #include "protos/perfetto/trace/system_info/cpu_info.gen.h"
23
24 using ::testing::AnyOf;
25 using ::testing::ElementsAre;
26 using ::testing::Return;
27
28 namespace perfetto {
29 namespace {
30
31 const char kMockCpuInfoAndroid[] = R"(
32 Processor : AArch64 Processor rev 13 (aarch64)
33 processor : 0
34 BogoMIPS : 38.00
35 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
36 CPU implementer : 0x51
37 CPU architecture: 8
38 CPU variant : 0x7
39 CPU part : 0x803
40 CPU revision : 12
41
42 processor : 1
43 BogoMIPS : 38.00
44 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
45 CPU implementer : 0x51
46 CPU architecture: 8
47 CPU variant : 0x7
48 CPU part : 0x803
49 CPU revision : 12
50
51 processor : 2
52 BogoMIPS : 38.00
53 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
54 CPU implementer : 0x51
55 CPU architecture: 8
56 CPU variant : 0x7
57 CPU part : 0x803
58 CPU revision : 12
59
60 processor : 3
61 BogoMIPS : 38.00
62 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
63 CPU implementer : 0x51
64 CPU architecture: 8
65 CPU variant : 0x7
66 CPU part : 0x803
67 CPU revision : 12
68
69 processor : 4
70 BogoMIPS : 38.00
71 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
72 CPU implementer : 0x51
73 CPU architecture: 8
74 CPU variant : 0x7
75 CPU part : 0x803
76 CPU revision : 12
77
78 processor : 5
79 BogoMIPS : 38.00
80 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
81 CPU implementer : 0x51
82 CPU architecture: 8
83 CPU variant : 0x7
84 CPU part : 0x803
85 CPU revision : 12
86
87 processor : 6
88 BogoMIPS : 38.00
89 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
90 CPU implementer : 0x51
91 CPU architecture: 8
92 CPU variant : 0x6
93 CPU part : 0x802
94 CPU revision : 13
95
96 processor : 7
97 BogoMIPS : 38.00
98 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp
99 CPU implementer : 0x51
100 CPU architecture: 8
101 CPU variant : 0x6
102 CPU part : 0x802
103 CPU revision : 13
104
105 Hardware : Qualcomm Technologies, Inc SDM670
106
107 )";
108
109 class TestSystemInfoDataSource : public SystemInfoDataSource {
110 public:
TestSystemInfoDataSource(std::unique_ptr<TraceWriter> writer,std::unique_ptr<CpuFreqInfo> cpu_freq_info)111 TestSystemInfoDataSource(std::unique_ptr<TraceWriter> writer,
112 std::unique_ptr<CpuFreqInfo> cpu_freq_info)
113 : SystemInfoDataSource(
114 /* session_id */ 0,
115 std::move(writer),
116 std::move(cpu_freq_info)) {}
117
118 MOCK_METHOD1(ReadFile, std::string(std::string));
119 };
120
121 class SystemInfoDataSourceTest : public ::testing::Test {
122 protected:
GetSystemInfoDataSource()123 std::unique_ptr<TestSystemInfoDataSource> GetSystemInfoDataSource() {
124 auto writer =
125 std::unique_ptr<TraceWriterForTesting>(new TraceWriterForTesting());
126 writer_raw_ = writer.get();
127 auto instance =
128 std::unique_ptr<TestSystemInfoDataSource>(new TestSystemInfoDataSource(
129 std::move(writer), cpu_freq_info_for_testing.GetInstance()));
130 return instance;
131 }
132
133 TraceWriterForTesting* writer_raw_ = nullptr;
134 CpuFreqInfoForTesting cpu_freq_info_for_testing;
135 };
136
TEST_F(SystemInfoDataSourceTest,CpuInfoAndroid)137 TEST_F(SystemInfoDataSourceTest, CpuInfoAndroid) {
138 auto data_source = GetSystemInfoDataSource();
139 EXPECT_CALL(*data_source, ReadFile("/proc/cpuinfo"))
140 .WillOnce(Return(kMockCpuInfoAndroid));
141 data_source->Start();
142
143 protos::gen::TracePacket packet = writer_raw_->GetOnlyTracePacket();
144 ASSERT_TRUE(packet.has_cpu_info());
145 auto cpu_info = packet.cpu_info();
146 ASSERT_EQ(cpu_info.cpus_size(), 8);
147 auto cpu = cpu_info.cpus()[0];
148 ASSERT_EQ(cpu.processor(), "AArch64 Processor rev 13 (aarch64)");
149 ASSERT_THAT(cpu.frequencies(),
150 ElementsAre(300000, 576000, 748800, 998400, 1209600, 1324800,
151 1516800, 1612800, 1708800));
152 cpu = cpu_info.cpus()[1];
153 ASSERT_EQ(cpu.processor(), "AArch64 Processor rev 13 (aarch64)");
154 ASSERT_THAT(cpu.frequencies(),
155 ElementsAre(300000, 652800, 825600, 979200, 1132800, 1363200,
156 1536000, 1747200, 1843200, 1996800, 2803200));
157 }
158
159 } // namespace
160 } // namespace perfetto
161