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" |
| 21 | #include "perfetto/tracing/core/trace_writer.h" |
| 22 | #include "src/base/test/test_task_runner.h" |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 23 | #include "src/tracing/core/shared_memory_arbiter_impl.h" |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 24 | #include "src/tracing/test/aligned_buffer_test.h" |
| 25 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 26 | #include "perfetto/trace/test_event.pbzero.h" |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 27 | #include "perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 28 | |
| 29 | namespace perfetto { |
| 30 | namespace { |
| 31 | |
| 32 | class TraceWriterImplTest : public AlignedBufferTest { |
| 33 | public: |
| 34 | void SetUp() override { |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 35 | SharedMemoryArbiterImpl::set_default_layout_for_testing( |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 36 | SharedMemoryABI::PageLayout::kPageDiv4); |
| 37 | AlignedBufferTest::SetUp(); |
| 38 | auto callback = [](const std::vector<uint32_t>& arg) {}; |
| 39 | task_runner_.reset(new base::TestTaskRunner()); |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 40 | arbiter_.reset(new SharedMemoryArbiterImpl(buf(), buf_size(), page_size(), |
| 41 | callback, task_runner_.get())); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void TearDown() override { |
| 45 | arbiter_.reset(); |
| 46 | task_runner_.reset(); |
| 47 | } |
| 48 | |
| 49 | std::unique_ptr<base::TestTaskRunner> task_runner_; |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 50 | std::unique_ptr<SharedMemoryArbiterImpl> arbiter_; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 51 | std::function<void(const std::vector<uint32_t>&)> on_pages_complete_; |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | size_t const kPageSizes[] = {4096, 65536}; |
| 55 | INSTANTIATE_TEST_CASE_P(PageSize, |
| 56 | TraceWriterImplTest, |
| 57 | ::testing::ValuesIn(kPageSizes)); |
| 58 | |
| 59 | TEST_P(TraceWriterImplTest, SingleWriter) { |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 60 | const BufferID kBufId = 42; |
| 61 | std::unique_ptr<TraceWriter> writer = arbiter_->CreateTraceWriter(kBufId); |
| 62 | const size_t kNumPackets = 32; |
| 63 | for (size_t i = 0; i < kNumPackets; i++) { |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 64 | auto packet = writer->NewTracePacket(); |
| 65 | char str[16]; |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 66 | sprintf(str, "foobar %zu", i); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 67 | packet->set_for_testing()->set_str(str); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 68 | } |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 69 | |
| 70 | // Destroying the TraceWriteImpl should cause the last packet to be finalized |
| 71 | // and the chunk to be put back in the kChunkComplete state. |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 72 | writer.reset(); |
| 73 | |
| 74 | SharedMemoryABI* abi = arbiter_->shmem_abi_for_testing(); |
| 75 | size_t packets_count = 0; |
| 76 | for (size_t page_idx = 0; page_idx < kNumPages; page_idx++) { |
| 77 | uint32_t page_layout = abi->page_layout_dbg(page_idx); |
| 78 | size_t num_chunks = SharedMemoryABI::GetNumChunksForLayout(page_layout); |
| 79 | for (size_t chunk_idx = 0; chunk_idx < num_chunks; chunk_idx++) { |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 80 | auto chunk_state = abi->GetChunkState(page_idx, chunk_idx); |
| 81 | ASSERT_TRUE(chunk_state == SharedMemoryABI::kChunkFree || |
| 82 | chunk_state == SharedMemoryABI::kChunkComplete); |
| 83 | auto chunk = abi->TryAcquireChunkForReading(page_idx, chunk_idx, kBufId); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 84 | if (!chunk.is_valid()) |
| 85 | continue; |
Primiano Tucci | a0ad8c1 | 2018-02-21 14:56:16 +0000 | [diff] [blame^] | 86 | packets_count += chunk.header()->packets.load().count; |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
Primiano Tucci | 9e8ebb0 | 2018-01-29 20:48:11 +0000 | [diff] [blame] | 89 | EXPECT_EQ(kNumPackets, packets_count); |
Primiano Tucci | d7b59c4 | 2017-12-19 01:43:07 +0100 | [diff] [blame] | 90 | // TODO(primiano): check also the content of the packets decoding the protos. |
| 91 | } |
| 92 | |
| 93 | // TODO(primiano): add multi-writer test. |
| 94 | |
| 95 | } // namespace |
| 96 | } // namespace perfetto |