Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 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 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 22 | #include "perfetto/ext/base/file_utils.h" |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 23 | #include "src/traced/probes/ftrace/ftrace_controller.h" |
| 24 | #include "src/traced/probes/ftrace/ftrace_procfs.h" |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 25 | #include "test/gtest_and_gmock.h" |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 26 | |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 27 | using testing::Contains; |
Hector Dearman | 98a97a8 | 2020-02-20 22:25:33 +0000 | [diff] [blame^] | 28 | using testing::HasSubstr; |
| 29 | using testing::IsEmpty; |
| 30 | using testing::Not; |
| 31 | using testing::UnorderedElementsAre; |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 32 | |
| 33 | namespace perfetto { |
| 34 | namespace { |
| 35 | |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 36 | std::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 Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 43 | |
| 44 | void ResetFtrace(FtraceProcfs* ftrace) { |
| 45 | ftrace->DisableAllEvents(); |
| 46 | ftrace->ClearTrace(); |
| 47 | ftrace->EnableTracing(); |
| 48 | } |
| 49 | |
| 50 | std::string ReadFile(const std::string& name) { |
| 51 | std::string result; |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 52 | PERFETTO_CHECK(base::ReadFile(GetFtracePath() + name, &result)); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 53 | return result; |
| 54 | } |
| 55 | |
| 56 | std::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 |
| 72 | TEST(FtraceProcfsIntegrationTest, MAYBE_CreateWithGoodPath) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 73 | EXPECT_TRUE(FtraceProcfs::Create(GetFtracePath())); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
| 77 | #define MAYBE_CreateWithBadPath CreateWithBadPath |
| 78 | #else |
| 79 | #define MAYBE_CreateWithBadPath DISABLED_CreateWithBadath |
| 80 | #endif |
| 81 | TEST(FtraceProcfsIntegrationTest, MAYBE_CreateWithBadPath) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 82 | EXPECT_FALSE(FtraceProcfs::Create(GetFtracePath() + std::string("bad_path"))); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
| 86 | #define MAYBE_ClearTrace ClearTrace |
| 87 | #else |
| 88 | #define MAYBE_ClearTrace DISABLED_ClearTrace |
| 89 | #endif |
| 90 | TEST(FtraceProcfsIntegrationTest, MAYBE_ClearTrace) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 91 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 92 | 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 |
| 103 | TEST(FtraceProcfsIntegrationTest, MAYBE_TraceMarker) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 104 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 105 | 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 |
| 115 | TEST(FtraceProcfsIntegrationTest, MAYBE_EnableDisableEvent) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 116 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 117 | 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 |
| 133 | TEST(FtraceProcfsIntegrationTest, MAYBE_EnableDisableTracing) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 134 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 135 | 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 |
| 154 | TEST(FtraceProcfsIntegrationTest, MAYBE_ReadFormatFile) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 155 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 156 | 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 |
| 166 | TEST(FtraceProcfsIntegrationTest, MAYBE_CanOpenTracePipeRaw) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 167 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 168 | 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 |
| 176 | TEST(FtraceProcfsIntegrationTest, MAYBE_Clock) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 177 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 178 | 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 |
| 193 | TEST(FtraceProcfsIntegrationTest, MAYBE_CanSetBufferSize) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 194 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 195 | 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 |
| 206 | TEST(FtraceProcfsIntegrationTest, MAYBE_FtraceControllerHardReset) { |
Primiano Tucci | f9d9c73 | 2018-11-28 21:16:15 +0000 | [diff] [blame] | 207 | FtraceProcfs ftrace(GetFtracePath()); |
Primiano Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 208 | 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 Dearman | 98a97a8 | 2020-02-20 22:25:33 +0000 | [diff] [blame^] | 228 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
| 229 | #define MAYBE_ReadEnabledEvents ReadEnabledEvents |
| 230 | #else |
| 231 | #define MAYBE_ReadEnabledEvents DISABLED_ReadEnabledEvents |
| 232 | #endif |
| 233 | TEST(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 Tucci | de82dae | 2018-06-04 16:17:49 +0200 | [diff] [blame] | 251 | } // namespace perfetto |