• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "perfetto/base/file_utils.h"
25 #include "src/traced/probes/ftrace/ftrace_controller.h"
26 #include "src/traced/probes/ftrace/ftrace_procfs.h"
27 
28 using testing::HasSubstr;
29 using testing::Not;
30 using testing::Contains;
31 
32 namespace perfetto {
33 namespace {
34 
GetFtracePath()35 std::string GetFtracePath() {
36   size_t i = 0;
37   while (!FtraceProcfs::Create(FtraceController::kTracingPaths[i])) {
38     i++;
39   }
40   return std::string(FtraceController::kTracingPaths[i]);
41 }
42 
ResetFtrace(FtraceProcfs * ftrace)43 void ResetFtrace(FtraceProcfs* ftrace) {
44   ftrace->DisableAllEvents();
45   ftrace->ClearTrace();
46   ftrace->EnableTracing();
47 }
48 
ReadFile(const std::string & name)49 std::string ReadFile(const std::string& name) {
50   std::string result;
51   PERFETTO_CHECK(base::ReadFile(GetFtracePath() + name, &result));
52   return result;
53 }
54 
GetTraceOutput()55 std::string GetTraceOutput() {
56   std::string output = ReadFile("trace");
57   if (output.empty()) {
58     ADD_FAILURE() << "Could not read trace output";
59   }
60   return output;
61 }
62 
63 }  // namespace
64 
65 // TODO(lalitm): reenable these tests (see b/72306171).
66 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
67 #define MAYBE_CreateWithGoodPath CreateWithGoodPath
68 #else
69 #define MAYBE_CreateWithGoodPath DISABLED_CreateWithGoodPath
70 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_CreateWithGoodPath)71 TEST(FtraceProcfsIntegrationTest, MAYBE_CreateWithGoodPath) {
72   EXPECT_TRUE(FtraceProcfs::Create(GetFtracePath()));
73 }
74 
75 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
76 #define MAYBE_CreateWithBadPath CreateWithBadPath
77 #else
78 #define MAYBE_CreateWithBadPath DISABLED_CreateWithBadath
79 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_CreateWithBadPath)80 TEST(FtraceProcfsIntegrationTest, MAYBE_CreateWithBadPath) {
81   EXPECT_FALSE(FtraceProcfs::Create(GetFtracePath() + std::string("bad_path")));
82 }
83 
84 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
85 #define MAYBE_ClearTrace ClearTrace
86 #else
87 #define MAYBE_ClearTrace DISABLED_ClearTrace
88 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_ClearTrace)89 TEST(FtraceProcfsIntegrationTest, MAYBE_ClearTrace) {
90   FtraceProcfs ftrace(GetFtracePath());
91   ResetFtrace(&ftrace);
92   ftrace.WriteTraceMarker("Hello, World!");
93   ftrace.ClearTrace();
94   EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("Hello, World!")));
95 }
96 
97 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
98 #define MAYBE_TraceMarker TraceMarker
99 #else
100 #define MAYBE_TraceMarker DISABLED_TraceMarker
101 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_TraceMarker)102 TEST(FtraceProcfsIntegrationTest, MAYBE_TraceMarker) {
103   FtraceProcfs ftrace(GetFtracePath());
104   ResetFtrace(&ftrace);
105   ftrace.WriteTraceMarker("Hello, World!");
106   EXPECT_THAT(GetTraceOutput(), HasSubstr("Hello, World!"));
107 }
108 
109 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
110 #define MAYBE_EnableDisableEvent EnableDisableEvent
111 #else
112 #define MAYBE_EnableDisableEvent DISABLED_EnableDisableEvent
113 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_EnableDisableEvent)114 TEST(FtraceProcfsIntegrationTest, MAYBE_EnableDisableEvent) {
115   FtraceProcfs ftrace(GetFtracePath());
116   ResetFtrace(&ftrace);
117   ftrace.EnableEvent("sched", "sched_switch");
118   sleep(1);
119   EXPECT_THAT(GetTraceOutput(), HasSubstr("sched_switch"));
120 
121   ftrace.DisableEvent("sched", "sched_switch");
122   ftrace.ClearTrace();
123   sleep(1);
124   EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("sched_switch")));
125 }
126 
127 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
128 #define MAYBE_EnableDisableTracing EnableDisableTracing
129 #else
130 #define MAYBE_EnableDisableTracing DISABLED_EnableDisableTracing
131 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_EnableDisableTracing)132 TEST(FtraceProcfsIntegrationTest, MAYBE_EnableDisableTracing) {
133   FtraceProcfs ftrace(GetFtracePath());
134   ResetFtrace(&ftrace);
135   EXPECT_TRUE(ftrace.IsTracingEnabled());
136   ftrace.WriteTraceMarker("Before");
137   ftrace.DisableTracing();
138   EXPECT_FALSE(ftrace.IsTracingEnabled());
139   ftrace.WriteTraceMarker("During");
140   ftrace.EnableTracing();
141   EXPECT_TRUE(ftrace.IsTracingEnabled());
142   ftrace.WriteTraceMarker("After");
143   EXPECT_THAT(GetTraceOutput(), HasSubstr("Before"));
144   EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("During")));
145   EXPECT_THAT(GetTraceOutput(), HasSubstr("After"));
146 }
147 
148 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
149 #define MAYBE_ReadFormatFile ReadFormatFile
150 #else
151 #define MAYBE_ReadFormatFile DISABLED_ReadFormatFile
152 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_ReadFormatFile)153 TEST(FtraceProcfsIntegrationTest, MAYBE_ReadFormatFile) {
154   FtraceProcfs ftrace(GetFtracePath());
155   std::string format = ftrace.ReadEventFormat("ftrace", "print");
156   EXPECT_THAT(format, HasSubstr("name: print"));
157   EXPECT_THAT(format, HasSubstr("field:char buf"));
158 }
159 
160 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
161 #define MAYBE_CanOpenTracePipeRaw CanOpenTracePipeRaw
162 #else
163 #define MAYBE_CanOpenTracePipeRaw DISABLED_CanOpenTracePipeRaw
164 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_CanOpenTracePipeRaw)165 TEST(FtraceProcfsIntegrationTest, MAYBE_CanOpenTracePipeRaw) {
166   FtraceProcfs ftrace(GetFtracePath());
167   EXPECT_TRUE(ftrace.OpenPipeForCpu(0));
168 }
169 
170 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
171 #define MAYBE_Clock Clock
172 #else
173 #define MAYBE_Clock DISABLED_Clock
174 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_Clock)175 TEST(FtraceProcfsIntegrationTest, MAYBE_Clock) {
176   FtraceProcfs ftrace(GetFtracePath());
177   std::set<std::string> clocks = ftrace.AvailableClocks();
178   EXPECT_THAT(clocks, Contains("local"));
179   EXPECT_THAT(clocks, Contains("global"));
180 
181   EXPECT_TRUE(ftrace.SetClock("global"));
182   EXPECT_EQ(ftrace.GetClock(), "global");
183   EXPECT_TRUE(ftrace.SetClock("local"));
184   EXPECT_EQ(ftrace.GetClock(), "local");
185 }
186 
187 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
188 #define MAYBE_CanSetBufferSize CanSetBufferSize
189 #else
190 #define MAYBE_CanSetBufferSize DISABLED_CanSetBufferSize
191 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_CanSetBufferSize)192 TEST(FtraceProcfsIntegrationTest, MAYBE_CanSetBufferSize) {
193   FtraceProcfs ftrace(GetFtracePath());
194   EXPECT_TRUE(ftrace.SetCpuBufferSizeInPages(4ul));
195   EXPECT_EQ(ReadFile("buffer_size_kb"), "16\n");  // (4096 * 4) / 1024
196   EXPECT_TRUE(ftrace.SetCpuBufferSizeInPages(5ul));
197   EXPECT_EQ(ReadFile("buffer_size_kb"), "20\n");  // (4096 * 5) / 1024
198 }
199 
200 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
201 #define MAYBE_FtraceControllerHardReset FtraceControllerHardReset
202 #else
203 #define MAYBE_FtraceControllerHardReset DISABLED_FtraceControllerHardReset
204 #endif
TEST(FtraceProcfsIntegrationTest,MAYBE_FtraceControllerHardReset)205 TEST(FtraceProcfsIntegrationTest, MAYBE_FtraceControllerHardReset) {
206   FtraceProcfs ftrace(GetFtracePath());
207   ResetFtrace(&ftrace);
208 
209   ftrace.SetCpuBufferSizeInPages(4ul);
210   ftrace.EnableTracing();
211   ftrace.EnableEvent("sched", "sched_switch");
212   ftrace.WriteTraceMarker("Hello, World!");
213 
214   EXPECT_EQ(ReadFile("buffer_size_kb"), "16\n");
215   EXPECT_EQ(ReadFile("tracing_on"), "1\n");
216   EXPECT_EQ(ReadFile("events/enable"), "X\n");
217   EXPECT_THAT(GetTraceOutput(), HasSubstr("Hello"));
218 
219   HardResetFtraceState();
220 
221   EXPECT_EQ(ReadFile("buffer_size_kb"), "4\n");
222   EXPECT_EQ(ReadFile("tracing_on"), "0\n");
223   EXPECT_EQ(ReadFile("events/enable"), "0\n");
224   EXPECT_THAT(GetTraceOutput(), Not(HasSubstr("Hello")));
225 }
226 
227 }  // namespace perfetto
228