Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [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 <sstream> |
| 19 | |
| 20 | #include "base/unix_task_runner.h" |
| 21 | #include "ftrace_procfs.h" |
| 22 | #include "ftrace_reader/ftrace_controller.h" |
| 23 | #include "gmock/gmock.h" |
| 24 | #include "google/protobuf/text_format.h" |
| 25 | #include "gtest/gtest.h" |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 26 | #include "protozero/scattered_stream_writer.h" |
| 27 | #include "scattered_stream_delegate_for_testing.h" |
| 28 | |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 29 | #include "protos/ftrace/ftrace_event_bundle.pb.h" |
| 30 | #include "protos/ftrace/ftrace_event_bundle.pbzero.h" |
| 31 | #include "protos/ftrace/test_bundle_wrapper.pb.h" |
| 32 | #include "protos/ftrace/test_bundle_wrapper.pbzero.h" |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 33 | |
| 34 | using testing::HasSubstr; |
| 35 | using testing::Not; |
| 36 | |
| 37 | namespace perfetto { |
| 38 | namespace { |
| 39 | |
| 40 | const size_t kPageSize = 4096; |
| 41 | |
| 42 | const char kTracingPath[] = "/sys/kernel/debug/tracing/"; |
| 43 | |
| 44 | using BundleHandle = |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 45 | protozero::ProtoZeroMessageHandle<protos::pbzero::FtraceEventBundle>; |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 46 | |
| 47 | class EndToEndIntegrationTest : public ::testing::Test, |
| 48 | public FtraceSink::Delegate { |
| 49 | public: |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 50 | void Finalize(protos::TestBundleWrapper* wrapper) { |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 51 | message->set_after("--- Bundle wrapper after ---"); |
| 52 | PERFETTO_CHECK(message); |
| 53 | size_t msg_size = message->Finalize(); |
| 54 | std::unique_ptr<uint8_t[]> buffer = writer_delegate->StitchChunks(msg_size); |
| 55 | wrapper->ParseFromArray(buffer.get(), static_cast<int>(msg_size)); |
| 56 | message.reset(); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | virtual void SetUp() { |
| 61 | writer_delegate = std::unique_ptr<ScatteredStreamDelegateForTesting>( |
| 62 | new ScatteredStreamDelegateForTesting(kPageSize * 100)); |
| 63 | writer = std::unique_ptr<protozero::ScatteredStreamWriter>( |
| 64 | new protozero::ScatteredStreamWriter(writer_delegate.get())); |
| 65 | writer_delegate->set_writer(writer.get()); |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 66 | message = std::unique_ptr<protos::pbzero::TestBundleWrapper>( |
| 67 | new protos::pbzero::TestBundleWrapper); |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 68 | message->Reset(writer.get()); |
| 69 | message->set_before("--- Bundle wrapper before ---"); |
| 70 | } |
| 71 | |
| 72 | virtual BundleHandle GetBundleForCpu(size_t cpu) { |
| 73 | PERFETTO_CHECK(!currently_writing_); |
| 74 | currently_writing_ = true; |
| 75 | cpu_being_written_ = cpu; |
| 76 | return BundleHandle(message->add_bundle()); |
| 77 | } |
| 78 | |
| 79 | virtual void OnBundleComplete(size_t cpu, BundleHandle bundle) { |
| 80 | PERFETTO_CHECK(currently_writing_); |
| 81 | currently_writing_ = false; |
| 82 | EXPECT_EQ(cpu_being_written_, cpu); |
| 83 | if (!count--) |
| 84 | runner_.Quit(); |
| 85 | } |
| 86 | |
| 87 | base::UnixTaskRunner* runner() { return &runner_; } |
| 88 | |
| 89 | private: |
| 90 | size_t count = 3; |
| 91 | base::UnixTaskRunner runner_; |
| 92 | bool currently_writing_ = false; |
| 93 | size_t cpu_being_written_ = 9999; |
| 94 | std::unique_ptr<ScatteredStreamDelegateForTesting> writer_delegate = nullptr; |
| 95 | std::unique_ptr<protozero::ScatteredStreamWriter> writer = nullptr; |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 96 | std::unique_ptr<protos::pbzero::TestBundleWrapper> message = nullptr; |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace |
| 100 | |
| 101 | TEST_F(EndToEndIntegrationTest, SchedSwitchAndPrint) { |
| 102 | FtraceProcfs procfs(kTracingPath); |
| 103 | procfs.ClearTrace(); |
| 104 | procfs.WriteTraceMarker("Hello, World!"); |
| 105 | |
| 106 | // Create a sink listening for our favorite events: |
| 107 | std::unique_ptr<FtraceController> ftrace = FtraceController::Create(runner()); |
| 108 | FtraceConfig config(std::set<std::string>({"print", "sched_switch"})); |
| 109 | std::unique_ptr<FtraceSink> sink = ftrace->CreateSink(config, this); |
| 110 | |
| 111 | // Let some events build up. |
| 112 | sleep(1); |
| 113 | |
| 114 | // Start watching pipe fds. |
| 115 | ftrace->Start(); |
| 116 | |
| 117 | // Start processing the tasks (OnBundleComplete will quit the task runner). |
| 118 | runner()->Run(); |
| 119 | |
| 120 | // Stop listening to fds. |
| 121 | ftrace->Stop(); |
| 122 | |
| 123 | // Disable events. |
| 124 | sink.reset(); |
| 125 | |
| 126 | // Read the output into a full proto so we can use reflection. |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 127 | protos::TestBundleWrapper output; |
Hector Dearman | 8d8ccd3 | 2017-11-27 16:06:34 +0000 | [diff] [blame] | 128 | Finalize(&output); |
| 129 | |
| 130 | // Check we can see the guards: |
| 131 | EXPECT_THAT(output.before(), HasSubstr("before")); |
| 132 | EXPECT_THAT(output.after(), HasSubstr("after")); |
| 133 | |
| 134 | std::string output_as_text; |
| 135 | google::protobuf::TextFormat::PrintToString(output, &output_as_text); |
| 136 | printf("%s\n", output_as_text.c_str()); |
| 137 | } |
| 138 | |
| 139 | } // namespace perfetto |