blob: 8dd848fb7598e22042163d9248924397dd052393 [file] [log] [blame]
Primiano Tuccide82dae2018-06-04 16:17:49 +02001/*
2 * Copyright (C) 2017 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 <fstream>
18#include <set>
19#include <sstream>
20#include <string>
21
Primiano Tucci2c5488f2019-06-01 03:27:28 +010022#include "perfetto/ext/base/file_utils.h"
Primiano Tuccide82dae2018-06-04 16:17:49 +020023#include "src/traced/probes/ftrace/ftrace_controller.h"
24#include "src/traced/probes/ftrace/ftrace_procfs.h"
Primiano Tucci919ca1e2019-08-21 20:26:58 +020025#include "test/gtest_and_gmock.h"
Primiano Tuccide82dae2018-06-04 16:17:49 +020026
Primiano Tuccide82dae2018-06-04 16:17:49 +020027using testing::Contains;
Hector Dearman98a97a82020-02-20 22:25:33 +000028using testing::HasSubstr;
29using testing::IsEmpty;
30using testing::Not;
31using testing::UnorderedElementsAre;
Primiano Tuccide82dae2018-06-04 16:17:49 +020032
33namespace perfetto {
34namespace {
35
Primiano Tuccif9d9c732018-11-28 21:16:15 +000036std::string GetFtracePath() {
37 size_t i = 0;
38 while (!FtraceProcfs::Create(FtraceController::kTracingPaths[i])) {
39 i++;
40 }
41 return std::string(FtraceController::kTracingPaths[i]);
42}
Primiano Tuccide82dae2018-06-04 16:17:49 +020043
44void ResetFtrace(FtraceProcfs* ftrace) {
45 ftrace->DisableAllEvents();
46 ftrace->ClearTrace();
47 ftrace->EnableTracing();
48}
49
50std::string ReadFile(const std::string& name) {
51 std::string result;
Primiano Tuccif9d9c732018-11-28 21:16:15 +000052 PERFETTO_CHECK(base::ReadFile(GetFtracePath() + name, &result));
Primiano Tuccide82dae2018-06-04 16:17:49 +020053 return result;
54}
55
56std::string GetTraceOutput() {
57 std::string output = ReadFile("trace");
58 if (output.empty()) {
59 ADD_FAILURE() << "Could not read trace output";
60 }
61 return output;
62}
63
64} // namespace
65
66// TODO(lalitm): reenable these tests (see b/72306171).
67#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
68#define MAYBE_CreateWithGoodPath CreateWithGoodPath
69#else
70#define MAYBE_CreateWithGoodPath DISABLED_CreateWithGoodPath
71#endif
72TEST(FtraceProcfsIntegrationTest, MAYBE_CreateWithGoodPath) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +000073 EXPECT_TRUE(FtraceProcfs::Create(GetFtracePath()));
Primiano Tuccide82dae2018-06-04 16:17:49 +020074}
75
76#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
77#define MAYBE_CreateWithBadPath CreateWithBadPath
78#else
79#define MAYBE_CreateWithBadPath DISABLED_CreateWithBadath
80#endif
81TEST(FtraceProcfsIntegrationTest, MAYBE_CreateWithBadPath) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +000082 EXPECT_FALSE(FtraceProcfs::Create(GetFtracePath() + std::string("bad_path")));
Primiano Tuccide82dae2018-06-04 16:17:49 +020083}
84
85#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
86#define MAYBE_ClearTrace ClearTrace
87#else
88#define MAYBE_ClearTrace DISABLED_ClearTrace
89#endif
90TEST(FtraceProcfsIntegrationTest, MAYBE_ClearTrace) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +000091 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +020092 ResetFtrace(&ftrace);
93 ftrace.WriteTraceMarker("Hello, World!");
94 ftrace.ClearTrace();
95 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("Hello, World!")));
96}
97
98#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
99#define MAYBE_TraceMarker TraceMarker
100#else
101#define MAYBE_TraceMarker DISABLED_TraceMarker
102#endif
103TEST(FtraceProcfsIntegrationTest, MAYBE_TraceMarker) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000104 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200105 ResetFtrace(&ftrace);
106 ftrace.WriteTraceMarker("Hello, World!");
107 EXPECT_THAT(GetTraceOutput(), HasSubstr("Hello, World!"));
108}
109
110#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
111#define MAYBE_EnableDisableEvent EnableDisableEvent
112#else
113#define MAYBE_EnableDisableEvent DISABLED_EnableDisableEvent
114#endif
115TEST(FtraceProcfsIntegrationTest, MAYBE_EnableDisableEvent) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000116 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200117 ResetFtrace(&ftrace);
118 ftrace.EnableEvent("sched", "sched_switch");
119 sleep(1);
120 EXPECT_THAT(GetTraceOutput(), HasSubstr("sched_switch"));
121
122 ftrace.DisableEvent("sched", "sched_switch");
123 ftrace.ClearTrace();
124 sleep(1);
125 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("sched_switch")));
126}
127
128#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
129#define MAYBE_EnableDisableTracing EnableDisableTracing
130#else
131#define MAYBE_EnableDisableTracing DISABLED_EnableDisableTracing
132#endif
133TEST(FtraceProcfsIntegrationTest, MAYBE_EnableDisableTracing) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000134 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200135 ResetFtrace(&ftrace);
136 EXPECT_TRUE(ftrace.IsTracingEnabled());
137 ftrace.WriteTraceMarker("Before");
138 ftrace.DisableTracing();
139 EXPECT_FALSE(ftrace.IsTracingEnabled());
140 ftrace.WriteTraceMarker("During");
141 ftrace.EnableTracing();
142 EXPECT_TRUE(ftrace.IsTracingEnabled());
143 ftrace.WriteTraceMarker("After");
144 EXPECT_THAT(GetTraceOutput(), HasSubstr("Before"));
145 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("During")));
146 EXPECT_THAT(GetTraceOutput(), HasSubstr("After"));
147}
148
149#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
150#define MAYBE_ReadFormatFile ReadFormatFile
151#else
152#define MAYBE_ReadFormatFile DISABLED_ReadFormatFile
153#endif
154TEST(FtraceProcfsIntegrationTest, MAYBE_ReadFormatFile) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000155 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200156 std::string format = ftrace.ReadEventFormat("ftrace", "print");
157 EXPECT_THAT(format, HasSubstr("name: print"));
158 EXPECT_THAT(format, HasSubstr("field:char buf"));
159}
160
161#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
162#define MAYBE_CanOpenTracePipeRaw CanOpenTracePipeRaw
163#else
164#define MAYBE_CanOpenTracePipeRaw DISABLED_CanOpenTracePipeRaw
165#endif
166TEST(FtraceProcfsIntegrationTest, MAYBE_CanOpenTracePipeRaw) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000167 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200168 EXPECT_TRUE(ftrace.OpenPipeForCpu(0));
169}
170
171#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
172#define MAYBE_Clock Clock
173#else
174#define MAYBE_Clock DISABLED_Clock
175#endif
176TEST(FtraceProcfsIntegrationTest, MAYBE_Clock) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000177 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200178 std::set<std::string> clocks = ftrace.AvailableClocks();
179 EXPECT_THAT(clocks, Contains("local"));
180 EXPECT_THAT(clocks, Contains("global"));
181
182 EXPECT_TRUE(ftrace.SetClock("global"));
183 EXPECT_EQ(ftrace.GetClock(), "global");
184 EXPECT_TRUE(ftrace.SetClock("local"));
185 EXPECT_EQ(ftrace.GetClock(), "local");
186}
187
188#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
189#define MAYBE_CanSetBufferSize CanSetBufferSize
190#else
191#define MAYBE_CanSetBufferSize DISABLED_CanSetBufferSize
192#endif
193TEST(FtraceProcfsIntegrationTest, MAYBE_CanSetBufferSize) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000194 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200195 EXPECT_TRUE(ftrace.SetCpuBufferSizeInPages(4ul));
196 EXPECT_EQ(ReadFile("buffer_size_kb"), "16\n"); // (4096 * 4) / 1024
197 EXPECT_TRUE(ftrace.SetCpuBufferSizeInPages(5ul));
198 EXPECT_EQ(ReadFile("buffer_size_kb"), "20\n"); // (4096 * 5) / 1024
199}
200
201#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
202#define MAYBE_FtraceControllerHardReset FtraceControllerHardReset
203#else
204#define MAYBE_FtraceControllerHardReset DISABLED_FtraceControllerHardReset
205#endif
206TEST(FtraceProcfsIntegrationTest, MAYBE_FtraceControllerHardReset) {
Primiano Tuccif9d9c732018-11-28 21:16:15 +0000207 FtraceProcfs ftrace(GetFtracePath());
Primiano Tuccide82dae2018-06-04 16:17:49 +0200208 ResetFtrace(&ftrace);
209
210 ftrace.SetCpuBufferSizeInPages(4ul);
211 ftrace.EnableTracing();
212 ftrace.EnableEvent("sched", "sched_switch");
213 ftrace.WriteTraceMarker("Hello, World!");
214
215 EXPECT_EQ(ReadFile("buffer_size_kb"), "16\n");
216 EXPECT_EQ(ReadFile("tracing_on"), "1\n");
217 EXPECT_EQ(ReadFile("events/enable"), "X\n");
218 EXPECT_THAT(GetTraceOutput(), HasSubstr("Hello"));
219
220 HardResetFtraceState();
221
222 EXPECT_EQ(ReadFile("buffer_size_kb"), "4\n");
223 EXPECT_EQ(ReadFile("tracing_on"), "0\n");
224 EXPECT_EQ(ReadFile("events/enable"), "0\n");
225 EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("Hello")));
226}
227
Hector Dearman98a97a82020-02-20 22:25:33 +0000228#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
229#define MAYBE_ReadEnabledEvents ReadEnabledEvents
230#else
231#define MAYBE_ReadEnabledEvents DISABLED_ReadEnabledEvents
232#endif
233TEST(FtraceProcfsIntegrationTest, MAYBE_ReadEnabledEvents) {
234 FtraceProcfs ftrace(GetFtracePath());
235 ResetFtrace(&ftrace);
236
237 EXPECT_THAT(ftrace.ReadEnabledEvents(), IsEmpty());
238
239 ftrace.EnableEvent("sched", "sched_switch");
240 ftrace.EnableEvent("kmem", "kmalloc");
241
242 EXPECT_THAT(ftrace.ReadEnabledEvents(),
243 UnorderedElementsAre("sched/sched_switch", "kmem/kmalloc"));
244
245 ftrace.DisableEvent("sched", "sched_switch");
246 ftrace.DisableEvent("kmem", "kmalloc");
247
248 EXPECT_THAT(ftrace.ReadEnabledEvents(), IsEmpty());
249}
250
Primiano Tuccide82dae2018-06-04 16:17:49 +0200251} // namespace perfetto