Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [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 "src/tracing/core/trace_writer_impl.h" |
| 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | #include "perfetto/base/utils.h" |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 21 | #include "perfetto/tracing/core/commit_data_request.h" |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 22 | #include "perfetto/tracing/core/trace_writer.h" |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 23 | #include "perfetto/tracing/core/tracing_service.h" |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 24 | #include "src/base/test/test_task_runner.h" |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 25 | #include "src/tracing/core/shared_memory_arbiter_impl.h" |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 26 | #include "src/tracing/test/aligned_buffer_test.h" |
| 27 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 28 | #include "perfetto/trace/test_event.pbzero.h" |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 29 | #include "perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 30 | |
| 31 | namespace perfetto { |
| 32 | namespace { |
| 33 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 34 | class FakeProducerEndpoint : public TracingService::ProducerEndpoint { |
Eric Seckler | 1569796 | 2018-12-11 14:01:34 +0000 | [diff] [blame] | 35 | public: |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 36 | void RegisterDataSource(const DataSourceDescriptor&) override {} |
| 37 | void UnregisterDataSource(const std::string&) override {} |
Eric Seckler | 1c4e1ac | 2018-11-29 10:23:14 +0000 | [diff] [blame] | 38 | void RegisterTraceWriter(uint32_t, uint32_t) override {} |
| 39 | void UnregisterTraceWriter(uint32_t) override {} |
Eric Seckler | 1569796 | 2018-12-11 14:01:34 +0000 | [diff] [blame] | 40 | void CommitData(const CommitDataRequest& req, CommitDataCallback) override { |
| 41 | last_commit_data_request = req; |
| 42 | } |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 43 | void NotifyFlushComplete(FlushRequestID) override {} |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 44 | void NotifyDataSourceStopped(DataSourceInstanceID) override {} |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 45 | SharedMemory* shared_memory() const override { return nullptr; } |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 46 | size_t shared_buffer_page_size_kb() const override { return 0; } |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 47 | std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override { |
| 48 | return nullptr; |
| 49 | } |
Eric Seckler | 1569796 | 2018-12-11 14:01:34 +0000 | [diff] [blame] | 50 | |
| 51 | CommitDataRequest last_commit_data_request; |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 54 | class TraceWriterImplTest : public AlignedBufferTest { |
| 55 | public: |
| 56 | void SetUp() override { |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 57 | SharedMemoryArbiterImpl::set_default_layout_for_testing( |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 58 | SharedMemoryABI::PageLayout::kPageDiv4); |
| 59 | AlignedBufferTest::SetUp(); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 60 | task_runner_.reset(new base::TestTaskRunner()); |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 61 | arbiter_.reset(new SharedMemoryArbiterImpl(buf(), buf_size(), page_size(), |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 62 | &fake_producer_endpoint_, |
| 63 | task_runner_.get())); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void TearDown() override { |
| 67 | arbiter_.reset(); |
| 68 | task_runner_.reset(); |
| 69 | } |
| 70 | |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 71 | FakeProducerEndpoint fake_producer_endpoint_; |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 72 | std::unique_ptr<base::TestTaskRunner> task_runner_; |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 73 | std::unique_ptr<SharedMemoryArbiterImpl> arbiter_; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 74 | std::function<void(const std::vector<uint32_t>&)> on_pages_complete_; |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | size_t const kPageSizes[] = {4096, 65536}; |
| 78 | INSTANTIATE_TEST_CASE_P(PageSize, |
| 79 | TraceWriterImplTest, |
| 80 | ::testing::ValuesIn(kPageSizes)); |
| 81 | |
| 82 | TEST_P(TraceWriterImplTest, SingleWriter) { |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 83 | const BufferID kBufId = 42; |
| 84 | std::unique_ptr<TraceWriter> writer = arbiter_->CreateTraceWriter(kBufId); |
| 85 | const size_t kNumPackets = 32; |
| 86 | for (size_t i = 0; i < kNumPackets; i++) { |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 87 | auto packet = writer->NewTracePacket(); |
| 88 | char str[16]; |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 89 | sprintf(str, "foobar %zu", i); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 90 | packet->set_for_testing()->set_str(str); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 91 | } |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 92 | |
| 93 | // Destroying the TraceWriteImpl should cause the last packet to be finalized |
| 94 | // and the chunk to be put back in the kChunkComplete state. |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 95 | writer.reset(); |
| 96 | |
| 97 | SharedMemoryABI* abi = arbiter_->shmem_abi_for_testing(); |
| 98 | size_t packets_count = 0; |
| 99 | for (size_t page_idx = 0; page_idx < kNumPages; page_idx++) { |
| 100 | uint32_t page_layout = abi->page_layout_dbg(page_idx); |
| 101 | size_t num_chunks = SharedMemoryABI::GetNumChunksForLayout(page_layout); |
| 102 | for (size_t chunk_idx = 0; chunk_idx < num_chunks; chunk_idx++) { |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 103 | auto chunk_state = abi->GetChunkState(page_idx, chunk_idx); |
| 104 | ASSERT_TRUE(chunk_state == SharedMemoryABI::kChunkFree || |
| 105 | chunk_state == SharedMemoryABI::kChunkComplete); |
Primiano Tucci | 42b36d0 | 2018-03-08 17:47:29 +0000 | [diff] [blame] | 106 | auto chunk = abi->TryAcquireChunkForReading(page_idx, chunk_idx); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 107 | if (!chunk.is_valid()) |
| 108 | continue; |
Primiano Tucci | a0ad8c1 | 2018-02-21 14:56:16 +0000 | [diff] [blame] | 109 | packets_count += chunk.header()->packets.load().count; |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 112 | EXPECT_EQ(kNumPackets, packets_count); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 113 | // TODO(primiano): check also the content of the packets decoding the protos. |
| 114 | } |
| 115 | |
Eric Seckler | 58d81e7 | 2018-12-06 08:42:47 +0000 | [diff] [blame] | 116 | TEST_P(TraceWriterImplTest, FragmentingPacket) { |
| 117 | const BufferID kBufId = 42; |
| 118 | std::unique_ptr<TraceWriter> writer = arbiter_->CreateTraceWriter(kBufId); |
| 119 | |
| 120 | // Write a packet that's guaranteed to span more than a single chunk. |
| 121 | auto packet = writer->NewTracePacket(); |
| 122 | size_t chunk_size = page_size() / 4; |
| 123 | std::stringstream large_string_writer; |
| 124 | for (size_t pos = 0; pos < chunk_size; pos++) |
| 125 | large_string_writer << "x"; |
| 126 | std::string large_string = large_string_writer.str(); |
| 127 | packet->set_for_testing()->set_str(large_string.data(), large_string.size()); |
| 128 | |
Eric Seckler | 1569796 | 2018-12-11 14:01:34 +0000 | [diff] [blame] | 129 | // First chunk should be committed. |
| 130 | arbiter_->FlushPendingCommitDataRequests(); |
| 131 | const auto& last_commit = fake_producer_endpoint_.last_commit_data_request; |
| 132 | ASSERT_EQ(1, last_commit.chunks_to_move_size()); |
| 133 | EXPECT_EQ(0u, last_commit.chunks_to_move()[0].page()); |
| 134 | EXPECT_EQ(0u, last_commit.chunks_to_move()[0].chunk()); |
| 135 | EXPECT_EQ(kBufId, last_commit.chunks_to_move()[0].target_buffer()); |
| 136 | EXPECT_EQ(0, last_commit.chunks_to_patch_size()); |
| 137 | |
Eric Seckler | 58d81e7 | 2018-12-06 08:42:47 +0000 | [diff] [blame] | 138 | SharedMemoryABI* abi = arbiter_->shmem_abi_for_testing(); |
| 139 | |
| 140 | // The first allocated chunk should be complete but need patching, since the |
| 141 | // packet extended past the chunk and no patches for the packet size or string |
| 142 | // field size were applied yet. |
Eric Seckler | 1569796 | 2018-12-11 14:01:34 +0000 | [diff] [blame] | 143 | ASSERT_EQ(SharedMemoryABI::kChunkComplete, abi->GetChunkState(0u, 0u)); |
| 144 | auto chunk = abi->TryAcquireChunkForReading(0u, 0u); |
Eric Seckler | 58d81e7 | 2018-12-06 08:42:47 +0000 | [diff] [blame] | 145 | ASSERT_TRUE(chunk.is_valid()); |
| 146 | ASSERT_EQ(1, chunk.header()->packets.load().count); |
| 147 | ASSERT_TRUE(chunk.header()->packets.load().flags & |
| 148 | SharedMemoryABI::ChunkHeader::kChunkNeedsPatching); |
| 149 | ASSERT_TRUE(chunk.header()->packets.load().flags & |
| 150 | SharedMemoryABI::ChunkHeader::kLastPacketContinuesOnNextChunk); |
| 151 | |
Eric Seckler | 1569796 | 2018-12-11 14:01:34 +0000 | [diff] [blame] | 152 | // Starting a new packet should cause patches to be applied. |
| 153 | packet->Finalize(); |
| 154 | auto packet2 = writer->NewTracePacket(); |
| 155 | arbiter_->FlushPendingCommitDataRequests(); |
| 156 | EXPECT_EQ(0, last_commit.chunks_to_move_size()); |
| 157 | ASSERT_EQ(1, last_commit.chunks_to_patch_size()); |
| 158 | EXPECT_EQ(writer->writer_id(), last_commit.chunks_to_patch()[0].writer_id()); |
| 159 | EXPECT_EQ(kBufId, last_commit.chunks_to_patch()[0].target_buffer()); |
| 160 | EXPECT_EQ(chunk.header()->chunk_id.load(), |
| 161 | last_commit.chunks_to_patch()[0].chunk_id()); |
| 162 | EXPECT_FALSE(last_commit.chunks_to_patch()[0].has_more_patches()); |
| 163 | ASSERT_EQ(1, last_commit.chunks_to_patch()[0].patches_size()); |
Eric Seckler | 58d81e7 | 2018-12-06 08:42:47 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 166 | // TODO(primiano): add multi-writer test. |
Primiano Tucci | 5317b5c | 2018-03-13 13:55:40 +0000 | [diff] [blame] | 167 | // TODO(primiano): add Flush() test. |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 168 | |
| 169 | } // namespace |
| 170 | } // namespace perfetto |