Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 17 | #include "perfetto/ext/tracing/core/startup_trace_writer.h" |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 18 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 19 | #include "perfetto/ext/tracing/core/startup_trace_writer_registry.h" |
| 20 | #include "perfetto/ext/tracing/core/trace_packet.h" |
| 21 | #include "perfetto/ext/tracing/core/tracing_service.h" |
Victor Costan | 4da8c67 | 2019-03-01 02:40:49 -0800 | [diff] [blame] | 22 | #include "src/base/test/gtest_test_suite.h" |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 23 | #include "src/base/test/test_task_runner.h" |
Eric Seckler | b52d52a | 2019-07-24 14:55:40 +0100 | [diff] [blame] | 24 | #include "src/tracing/core/patch_list.h" |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 25 | #include "src/tracing/core/shared_memory_arbiter_impl.h" |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 26 | #include "src/tracing/core/sliced_protobuf_input_stream.h" |
| 27 | #include "src/tracing/core/trace_buffer.h" |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 28 | #include "src/tracing/test/aligned_buffer_test.h" |
| 29 | #include "src/tracing/test/fake_producer_endpoint.h" |
Primiano Tucci | 919ca1e | 2019-08-21 20:26:58 +0200 | [diff] [blame] | 30 | #include "test/gtest_and_gmock.h" |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 31 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame^] | 32 | #include "protos/perfetto/trace/test_event.pbzero.h" |
| 33 | #include "protos/perfetto/trace/trace_packet.pb.h" |
| 34 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 35 | |
| 36 | namespace perfetto { |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 37 | |
| 38 | class StartupTraceWriterTest : public AlignedBufferTest { |
| 39 | public: |
| 40 | void SetUp() override { |
| 41 | SharedMemoryArbiterImpl::set_default_layout_for_testing( |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 42 | SharedMemoryABI::PageLayout::kPageDiv1); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 43 | AlignedBufferTest::SetUp(); |
| 44 | task_runner_.reset(new base::TestTaskRunner()); |
| 45 | arbiter_.reset(new SharedMemoryArbiterImpl(buf(), buf_size(), page_size(), |
| 46 | &fake_producer_endpoint_, |
| 47 | task_runner_.get())); |
| 48 | } |
| 49 | |
| 50 | void TearDown() override { |
| 51 | arbiter_.reset(); |
| 52 | task_runner_.reset(); |
| 53 | } |
| 54 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 55 | std::unique_ptr<StartupTraceWriter> CreateUnboundWriter() { |
| 56 | std::shared_ptr<StartupTraceWriterRegistryHandle> registry; |
Eric Seckler | 3d99b0f | 2019-08-09 15:15:19 +0100 | [diff] [blame] | 57 | return std::unique_ptr<StartupTraceWriter>( |
| 58 | new StartupTraceWriter(registry, BufferExhaustedPolicy::kDrop)); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 61 | bool BindWriter(StartupTraceWriter* writer, size_t chunks_per_batch = 0) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 62 | const BufferID kBufId = 42; |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 63 | return writer->BindToArbiter(arbiter_.get(), kBufId, chunks_per_batch); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 66 | void WritePackets(StartupTraceWriter* writer, size_t packet_count) { |
| 67 | for (size_t i = 0; i < packet_count; i++) { |
| 68 | auto packet = writer->NewTracePacket(); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 69 | packet->set_for_testing()->set_str(kPacketPayload); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 73 | size_t CountCompleteChunksInSMB() { |
| 74 | SharedMemoryABI* abi = arbiter_->shmem_abi_for_testing(); |
| 75 | size_t num_complete_chunks = 0; |
| 76 | for (size_t page_idx = 0; page_idx < kNumPages; page_idx++) { |
| 77 | uint32_t page_layout = abi->GetPageLayout(page_idx); |
| 78 | size_t num_chunks = SharedMemoryABI::GetNumChunksForLayout(page_layout); |
| 79 | for (size_t chunk_idx = 0; chunk_idx < num_chunks; chunk_idx++) { |
| 80 | auto chunk_state = abi->GetChunkState(page_idx, chunk_idx); |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 81 | if (chunk_state == SharedMemoryABI::kChunkComplete) |
| 82 | num_complete_chunks++; |
| 83 | } |
| 84 | } |
| 85 | return num_complete_chunks; |
| 86 | } |
| 87 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 88 | void VerifyPackets(size_t expected_count) { |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 89 | SharedMemoryABI* abi = arbiter_->shmem_abi_for_testing(); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 90 | auto buffer = TraceBuffer::Create(abi->size()); |
| 91 | |
| 92 | size_t total_packets_count = 0; |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 93 | ChunkID current_max_chunk_id = 0; |
| 94 | for (size_t page_idx = 0; page_idx < kNumPages; page_idx++) { |
| 95 | uint32_t page_layout = abi->GetPageLayout(page_idx); |
| 96 | size_t num_chunks = SharedMemoryABI::GetNumChunksForLayout(page_layout); |
| 97 | for (size_t chunk_idx = 0; chunk_idx < num_chunks; chunk_idx++) { |
| 98 | auto chunk_state = abi->GetChunkState(page_idx, chunk_idx); |
| 99 | ASSERT_TRUE(chunk_state == SharedMemoryABI::kChunkFree || |
| 100 | chunk_state == SharedMemoryABI::kChunkComplete); |
| 101 | auto chunk = abi->TryAcquireChunkForReading(page_idx, chunk_idx); |
| 102 | if (!chunk.is_valid()) |
| 103 | continue; |
| 104 | |
| 105 | // Should only see new chunks with IDs larger than the previous read |
| 106 | // since our reads and writes are serialized. |
| 107 | ChunkID chunk_id = chunk.header()->chunk_id.load(); |
Eric Seckler | 4a0ee4e | 2019-01-15 08:37:46 +0000 | [diff] [blame] | 108 | if (last_read_max_chunk_id_ != 0) { |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 109 | EXPECT_LT(last_read_max_chunk_id_, chunk_id); |
Eric Seckler | 4a0ee4e | 2019-01-15 08:37:46 +0000 | [diff] [blame] | 110 | } |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 111 | current_max_chunk_id = std::max(current_max_chunk_id, chunk_id); |
| 112 | |
| 113 | auto packets_header = chunk.header()->packets.load(); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 114 | total_packets_count += packets_header.count; |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 115 | if (packets_header.flags & |
| 116 | SharedMemoryABI::ChunkHeader::kFirstPacketContinuesFromPrevChunk) { |
| 117 | // Don't count fragmented packets twice. |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 118 | total_packets_count--; |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 119 | } |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 120 | |
| 121 | buffer->CopyChunkUntrusted( |
| 122 | /*producer_id_trusted=*/1, /*producer_uid_trusted=*/1, |
| 123 | chunk.header()->writer_id.load(), chunk_id, packets_header.count, |
| 124 | packets_header.flags, /*chunk_complete=*/true, |
| 125 | chunk.payload_begin(), chunk.payload_size()); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 126 | abi->ReleaseChunkAsFree(std::move(chunk)); |
| 127 | } |
| 128 | } |
| 129 | last_read_max_chunk_id_ = current_max_chunk_id; |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 130 | EXPECT_EQ(expected_count, total_packets_count); |
| 131 | |
| 132 | // Now verify chunk and packet contents. |
| 133 | buffer->BeginRead(); |
| 134 | size_t num_packets_read = 0; |
| 135 | while (true) { |
| 136 | TracePacket packet; |
Eric Seckler | d0ac7ca | 2019-02-06 09:13:45 +0000 | [diff] [blame] | 137 | TraceBuffer::PacketSequenceProperties sequence_properties{}; |
Eric Seckler | 062be90 | 2019-03-08 17:05:12 +0000 | [diff] [blame] | 138 | bool previous_packet_dropped; |
| 139 | if (!buffer->ReadNextTracePacket(&packet, &sequence_properties, |
| 140 | &previous_packet_dropped)) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 141 | break; |
Eric Seckler | 062be90 | 2019-03-08 17:05:12 +0000 | [diff] [blame] | 142 | } |
Eric Seckler | d0ac7ca | 2019-02-06 09:13:45 +0000 | [diff] [blame] | 143 | EXPECT_EQ(static_cast<uid_t>(1), |
| 144 | sequence_properties.producer_uid_trusted); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 145 | |
| 146 | SlicedProtobufInputStream stream(&packet.slices()); |
| 147 | size_t size = 0; |
| 148 | for (const Slice& slice : packet.slices()) |
| 149 | size += slice.size; |
| 150 | protos::TracePacket parsed_packet; |
| 151 | bool success = parsed_packet.ParseFromBoundedZeroCopyStream( |
| 152 | &stream, static_cast<int>(size)); |
| 153 | EXPECT_TRUE(success); |
| 154 | if (!success) |
| 155 | break; |
| 156 | EXPECT_TRUE(parsed_packet.has_for_testing()); |
| 157 | EXPECT_EQ(kPacketPayload, parsed_packet.for_testing().str()); |
| 158 | num_packets_read++; |
| 159 | } |
| 160 | EXPECT_EQ(expected_count, num_packets_read); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 163 | size_t GetUnboundWriterCount( |
| 164 | const StartupTraceWriterRegistry& registry) const { |
| 165 | return registry.unbound_writers_.size() + |
| 166 | registry.unbound_owned_writers_.size(); |
| 167 | } |
| 168 | |
| 169 | size_t GetBindingRegistriesCount( |
| 170 | const SharedMemoryArbiterImpl& arbiter) const { |
| 171 | return arbiter.startup_trace_writer_registries_.size(); |
| 172 | } |
| 173 | |
| 174 | size_t GetUnboundWriterCount(const SharedMemoryArbiterImpl& arbiter) const { |
| 175 | size_t count = 0u; |
| 176 | for (const auto& reg : arbiter.startup_trace_writer_registries_) { |
| 177 | count += reg->unbound_writers_.size(); |
| 178 | count += reg->unbound_owned_writers_.size(); |
| 179 | } |
| 180 | return count; |
| 181 | } |
| 182 | |
| 183 | protected: |
| 184 | static constexpr char kPacketPayload[] = "foo"; |
| 185 | |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 186 | FakeProducerEndpoint fake_producer_endpoint_; |
| 187 | std::unique_ptr<base::TestTaskRunner> task_runner_; |
| 188 | std::unique_ptr<SharedMemoryArbiterImpl> arbiter_; |
| 189 | std::function<void(const std::vector<uint32_t>&)> on_pages_complete_; |
| 190 | |
| 191 | ChunkID last_read_max_chunk_id_ = 0; |
| 192 | }; |
| 193 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 194 | constexpr char StartupTraceWriterTest::kPacketPayload[]; |
| 195 | |
| 196 | namespace { |
| 197 | |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 198 | size_t const kPageSizes[] = {4096, 32768}; |
Victor Costan | 4da8c67 | 2019-03-01 02:40:49 -0800 | [diff] [blame] | 199 | INSTANTIATE_TEST_SUITE_P(PageSize, |
| 200 | StartupTraceWriterTest, |
| 201 | ::testing::ValuesIn(kPageSizes)); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 202 | |
| 203 | TEST_P(StartupTraceWriterTest, CreateUnboundAndBind) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 204 | auto writer = CreateUnboundWriter(); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 205 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 206 | // Bind writer right away without having written any data before. |
| 207 | EXPECT_TRUE(BindWriter(writer.get())); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 208 | |
| 209 | const size_t kNumPackets = 32; |
| 210 | WritePackets(writer.get(), kNumPackets); |
| 211 | // Finalizes the last packet and returns the chunk. |
| 212 | writer.reset(); |
| 213 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 214 | VerifyPackets(kNumPackets); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST_P(StartupTraceWriterTest, CreateBound) { |
| 218 | // Create a bound writer immediately. |
| 219 | const BufferID kBufId = 42; |
| 220 | std::unique_ptr<StartupTraceWriter> writer( |
| 221 | new StartupTraceWriter(arbiter_->CreateTraceWriter(kBufId))); |
| 222 | |
| 223 | const size_t kNumPackets = 32; |
| 224 | WritePackets(writer.get(), kNumPackets); |
| 225 | // Finalizes the last packet and returns the chunk. |
| 226 | writer.reset(); |
| 227 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 228 | VerifyPackets(kNumPackets); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST_P(StartupTraceWriterTest, WriteWhileUnboundAndDiscard) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 232 | auto writer = CreateUnboundWriter(); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 233 | |
| 234 | const size_t kNumPackets = 32; |
| 235 | WritePackets(writer.get(), kNumPackets); |
| 236 | |
| 237 | // Should discard the written data. |
| 238 | writer.reset(); |
| 239 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 240 | VerifyPackets(0); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | TEST_P(StartupTraceWriterTest, WriteWhileUnboundAndBind) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 244 | auto writer = CreateUnboundWriter(); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 245 | |
| 246 | const size_t kNumPackets = 32; |
| 247 | WritePackets(writer.get(), kNumPackets); |
| 248 | |
| 249 | // Binding the writer should cause the previously written packets to be |
| 250 | // written to the SMB and committed. |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 251 | EXPECT_TRUE(BindWriter(writer.get())); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 252 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 253 | VerifyPackets(kNumPackets); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 254 | |
| 255 | // Any further packets should be written to the SMB directly. |
| 256 | const size_t kNumAdditionalPackets = 16; |
| 257 | WritePackets(writer.get(), kNumAdditionalPackets); |
| 258 | // Finalizes the last packet and returns the chunk. |
| 259 | writer.reset(); |
| 260 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 261 | VerifyPackets(kNumAdditionalPackets); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | TEST_P(StartupTraceWriterTest, WriteMultipleChunksWhileUnboundAndBind) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 265 | auto writer = CreateUnboundWriter(); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 266 | |
| 267 | // Write a single packet to determine its size in the buffer. |
| 268 | WritePackets(writer.get(), 1); |
| 269 | size_t packet_size = writer->used_buffer_size(); |
| 270 | |
| 271 | // Write at least 3 pages worth of packets. |
| 272 | const size_t kNumPackets = (page_size() * 3 + packet_size - 1) / packet_size; |
| 273 | WritePackets(writer.get(), kNumPackets); |
| 274 | |
| 275 | // Binding the writer should cause the previously written packets to be |
| 276 | // written to the SMB and committed. |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 277 | EXPECT_TRUE(BindWriter(writer.get())); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 278 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 279 | VerifyPackets(kNumPackets + 1); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 280 | |
| 281 | // Any further packets should be written to the SMB directly. |
| 282 | const size_t kNumAdditionalPackets = 16; |
| 283 | WritePackets(writer.get(), kNumAdditionalPackets); |
| 284 | // Finalizes the last packet and returns the chunk. |
| 285 | writer.reset(); |
| 286 | |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 287 | VerifyPackets(kNumAdditionalPackets); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | TEST_P(StartupTraceWriterTest, BindingWhileWritingFails) { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 291 | auto writer = CreateUnboundWriter(); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 292 | |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 293 | { |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 294 | // Begin a write by opening a TracePacket. |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 295 | auto packet = writer->NewTracePacket(); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 296 | packet->set_for_testing()->set_str(kPacketPayload); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 297 | |
| 298 | // Binding while writing should fail. |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 299 | EXPECT_FALSE(BindWriter(writer.get())); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Packet was completed, so binding should work now and emit the packet. |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 303 | EXPECT_TRUE(BindWriter(writer.get())); |
| 304 | VerifyPackets(1); |
| 305 | } |
| 306 | |
| 307 | TEST_P(StartupTraceWriterTest, CreateAndBindViaRegistry) { |
| 308 | std::unique_ptr<StartupTraceWriterRegistry> registry( |
| 309 | new StartupTraceWriterRegistry()); |
| 310 | |
| 311 | // Create unbound writers. |
Eric Seckler | 3d99b0f | 2019-08-09 15:15:19 +0100 | [diff] [blame] | 312 | auto writer1 = |
| 313 | registry->CreateUnboundTraceWriter(BufferExhaustedPolicy::kDrop); |
| 314 | auto writer2 = |
| 315 | registry->CreateUnboundTraceWriter(BufferExhaustedPolicy::kDrop); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 316 | |
| 317 | EXPECT_EQ(2u, GetUnboundWriterCount(*registry)); |
| 318 | |
| 319 | // Return |writer2|. It should be kept alive until the registry is bound. |
Eric Seckler | 75ed550 | 2019-05-28 16:20:51 +0100 | [diff] [blame] | 320 | StartupTraceWriter::ReturnToRegistry(std::move(writer2)); |
Eric Seckler | 42777e5 | 2019-01-23 10:13:22 +0000 | [diff] [blame] | 321 | |
| 322 | { |
| 323 | // Begin a write by opening a TracePacket on |writer1|. |
| 324 | auto packet = writer1->NewTracePacket(); |
| 325 | |
| 326 | // Binding |writer1| writing should fail, but |writer2| should be bound. |
| 327 | const BufferID kBufId = 42; |
| 328 | arbiter_->BindStartupTraceWriterRegistry(std::move(registry), kBufId); |
| 329 | EXPECT_EQ(1u, GetUnboundWriterCount(*arbiter_)); |
| 330 | } |
| 331 | |
| 332 | // Wait for |writer1| to be bound and the registry to be deleted. |
| 333 | auto checkpoint_name = "all_bound"; |
| 334 | auto all_bound = task_runner_->CreateCheckpoint(checkpoint_name); |
| 335 | std::function<void()> task; |
| 336 | task = [&task, &all_bound, this]() { |
| 337 | if (!GetBindingRegistriesCount(*arbiter_)) { |
| 338 | all_bound(); |
| 339 | return; |
| 340 | } |
| 341 | task_runner_->PostDelayedTask(task, 1); |
| 342 | }; |
| 343 | task_runner_->PostDelayedTask(task, 1); |
| 344 | task_runner_->RunUntilCheckpoint(checkpoint_name); |
Eric Seckler | 75ed550 | 2019-05-28 16:20:51 +0100 | [diff] [blame] | 345 | |
| 346 | StartupTraceWriter::ReturnToRegistry(std::move(writer1)); |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 349 | TEST_P(StartupTraceWriterTest, BindAndCommitInBatches) { |
| 350 | auto writer = CreateUnboundWriter(); |
| 351 | |
| 352 | // Write a single packet to determine its size in the buffer. |
| 353 | WritePackets(writer.get(), 1); |
| 354 | size_t packet_size = writer->used_buffer_size(); |
| 355 | |
| 356 | // Write at least 3 pages/chunks worth of packets. |
| 357 | const size_t kNumPackets = (page_size() * 3 + packet_size - 1) / packet_size; |
| 358 | WritePackets(writer.get(), kNumPackets); |
| 359 | |
| 360 | static constexpr size_t kChunksPerBatch = 2; |
| 361 | |
| 362 | // Binding the writer with a batch size of 2 chunks should cause the first 2 |
| 363 | // chunks of previously written packets to be written to the SMB and |
| 364 | // committed. The remaining chunks will be written when the |
| 365 | // |commit_data_callback| is executed. |
| 366 | EXPECT_TRUE(BindWriter(writer.get(), kChunksPerBatch)); |
| 367 | |
| 368 | EXPECT_EQ( |
| 369 | fake_producer_endpoint_.last_commit_data_request.chunks_to_move().size(), |
| 370 | kChunksPerBatch); |
| 371 | EXPECT_EQ(CountCompleteChunksInSMB(), kChunksPerBatch); |
| 372 | auto commit_data_callback = fake_producer_endpoint_.last_commit_data_callback; |
| 373 | EXPECT_TRUE(commit_data_callback); |
| 374 | |
| 375 | // Send a commit with a single packet from the bound trace writer before the |
| 376 | // remaining chunk batches of the buffered data are written. |
| 377 | const size_t kNumAdditionalPackets = 1; |
| 378 | WritePackets(writer.get(), 1); |
| 379 | // Finalizes the packet and returns the chunk. |
| 380 | writer.reset(); |
| 381 | |
| 382 | // The packet should fit into a chunk. |
| 383 | EXPECT_EQ( |
| 384 | fake_producer_endpoint_.last_commit_data_request.chunks_to_move().size(), |
Ryan Savitski | 45b6e59 | 2019-07-11 13:36:20 +0100 | [diff] [blame] | 385 | 1u); |
Eric Seckler | 0637578 | 2019-07-10 14:16:58 +0100 | [diff] [blame] | 386 | EXPECT_EQ(CountCompleteChunksInSMB(), kChunksPerBatch + 1); |
| 387 | |
| 388 | // Write and commit the remaining chunks to the SMB. |
| 389 | while (commit_data_callback) { |
| 390 | commit_data_callback(); |
| 391 | commit_data_callback = fake_producer_endpoint_.last_commit_data_callback; |
| 392 | } |
| 393 | |
| 394 | // Verify that all chunks + packets are in the SMB. |
| 395 | VerifyPackets(1 + kNumPackets + kNumAdditionalPackets); |
| 396 | } |
| 397 | |
Eric Seckler | b52d52a | 2019-07-24 14:55:40 +0100 | [diff] [blame] | 398 | TEST_P(StartupTraceWriterTest, BindAndCommitInBatchesWithSMBExhaustion) { |
| 399 | auto writer = CreateUnboundWriter(); |
| 400 | |
| 401 | // Write a single packet to determine its size in the buffer. |
| 402 | WritePackets(writer.get(), 1); |
| 403 | size_t packet_size = writer->used_buffer_size(); |
| 404 | |
| 405 | // Write at least 3 pages/chunks worth of packets. |
| 406 | const size_t kNumPackets = (page_size() * 3 + packet_size - 1) / packet_size; |
| 407 | WritePackets(writer.get(), kNumPackets); |
| 408 | |
| 409 | // Acquire all chunks in the SMB. |
| 410 | static constexpr size_t kTotChunks = kNumPages; |
| 411 | SharedMemoryABI::Chunk chunks[kTotChunks]; |
| 412 | for (size_t i = 0; i < kTotChunks; i++) { |
Eric Seckler | 3d99b0f | 2019-08-09 15:15:19 +0100 | [diff] [blame] | 413 | chunks[i] = arbiter_->GetNewChunk({}, BufferExhaustedPolicy::kDrop); |
Eric Seckler | b52d52a | 2019-07-24 14:55:40 +0100 | [diff] [blame] | 414 | ASSERT_TRUE(chunks[i].is_valid()); |
| 415 | } |
| 416 | |
| 417 | // Binding the writer should fail if no chunks are available in the SMB. |
| 418 | static constexpr size_t kChunksPerBatch = 2; |
| 419 | EXPECT_FALSE(BindWriter(writer.get(), kChunksPerBatch)); |
| 420 | |
| 421 | // Return and free the first chunk, so that there is only a single free chunk. |
| 422 | PatchList ignored; |
| 423 | arbiter_->ReturnCompletedChunk(std::move(chunks[0]), 0, &ignored); |
| 424 | chunks[0] = |
| 425 | arbiter_->shmem_abi_for_testing()->TryAcquireChunkForReading(0, 0); |
| 426 | ASSERT_TRUE(chunks[0].is_valid()); |
| 427 | arbiter_->shmem_abi_for_testing()->ReleaseChunkAsFree(std::move(chunks[0])); |
| 428 | arbiter_->FlushPendingCommitDataRequests(); |
| 429 | |
| 430 | // Binding the writer should only cause the first chunks of previously written |
| 431 | // packets to be written to the SMB and committed because no further chunks |
| 432 | // are available in the SMB. The remaining chunks will be written when the |
| 433 | // |commit_data_callback| is executed. |
| 434 | EXPECT_TRUE(BindWriter(writer.get(), kChunksPerBatch)); |
| 435 | |
| 436 | EXPECT_EQ( |
| 437 | fake_producer_endpoint_.last_commit_data_request.chunks_to_move().size(), |
| 438 | 1u); |
| 439 | EXPECT_EQ(CountCompleteChunksInSMB(), 1u); |
| 440 | auto commit_data_callback = fake_producer_endpoint_.last_commit_data_callback; |
| 441 | EXPECT_TRUE(commit_data_callback); |
| 442 | |
| 443 | // Free up the other SMB chunks. |
| 444 | for (size_t i = 1; i < kTotChunks; i++) { |
| 445 | arbiter_->ReturnCompletedChunk(std::move(chunks[i]), 0, &ignored); |
| 446 | chunks[i] = |
| 447 | arbiter_->shmem_abi_for_testing()->TryAcquireChunkForReading(i, 0); |
| 448 | ASSERT_TRUE(chunks[i].is_valid()); |
| 449 | arbiter_->shmem_abi_for_testing()->ReleaseChunkAsFree(std::move(chunks[i])); |
| 450 | } |
| 451 | arbiter_->FlushPendingCommitDataRequests(); |
| 452 | |
| 453 | // Write and commit the remaining buffered startup writer data to the SMB. |
| 454 | while (commit_data_callback) { |
| 455 | commit_data_callback(); |
| 456 | commit_data_callback = fake_producer_endpoint_.last_commit_data_callback; |
| 457 | } |
| 458 | EXPECT_GT( |
| 459 | fake_producer_endpoint_.last_commit_data_request.chunks_to_move().size(), |
| 460 | 0u); |
| 461 | |
| 462 | // Verify that all chunks + packets are in the SMB. |
| 463 | VerifyPackets(1 + kNumPackets); |
| 464 | } |
| 465 | |
Eric Seckler | c65693d | 2019-01-11 15:12:48 +0000 | [diff] [blame] | 466 | } // namespace |
| 467 | } // namespace perfetto |