blob: a1a042f7724de4ff377addf5f4e79fecdf32ef68 [file] [log] [blame]
Primiano Tuccid7b59c42017-12-19 01:43:07 +01001/*
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 Eftevaag6d0bc7f2018-01-13 12:21:55 -080023#include "src/tracing/core/shared_memory_arbiter_impl.h"
Primiano Tuccid7b59c42017-12-19 01:43:07 +010024#include "src/tracing/test/aligned_buffer_test.h"
25
Primiano Tucci20b760c2018-01-19 12:36:12 +000026#include "perfetto/trace/trace_packet.pbzero.h"
Primiano Tuccid7b59c42017-12-19 01:43:07 +010027
28namespace perfetto {
29namespace {
30
31class TraceWriterImplTest : public AlignedBufferTest {
32 public:
33 void SetUp() override {
Oystein Eftevaag6d0bc7f2018-01-13 12:21:55 -080034 SharedMemoryArbiterImpl::set_default_layout_for_testing(
Primiano Tuccid7b59c42017-12-19 01:43:07 +010035 SharedMemoryABI::PageLayout::kPageDiv4);
36 AlignedBufferTest::SetUp();
37 auto callback = [](const std::vector<uint32_t>& arg) {};
38 task_runner_.reset(new base::TestTaskRunner());
Oystein Eftevaag6d0bc7f2018-01-13 12:21:55 -080039 arbiter_.reset(new SharedMemoryArbiterImpl(buf(), buf_size(), page_size(),
40 callback, task_runner_.get()));
Primiano Tuccid7b59c42017-12-19 01:43:07 +010041 }
42
43 void TearDown() override {
44 arbiter_.reset();
45 task_runner_.reset();
46 }
47
48 std::unique_ptr<base::TestTaskRunner> task_runner_;
Oystein Eftevaag6d0bc7f2018-01-13 12:21:55 -080049 std::unique_ptr<SharedMemoryArbiterImpl> arbiter_;
Primiano Tucciaf429f92017-12-19 01:51:50 +010050 std::function<void(const std::vector<uint32_t>&)> on_pages_complete_;
Primiano Tuccid7b59c42017-12-19 01:43:07 +010051};
52
53size_t const kPageSizes[] = {4096, 65536};
54INSTANTIATE_TEST_CASE_P(PageSize,
55 TraceWriterImplTest,
56 ::testing::ValuesIn(kPageSizes));
57
58TEST_P(TraceWriterImplTest, SingleWriter) {
Primiano Tucci9e8ebb02018-01-29 20:48:11 +000059 const BufferID kBufId = 42;
60 std::unique_ptr<TraceWriter> writer = arbiter_->CreateTraceWriter(kBufId);
61 const size_t kNumPackets = 32;
62 for (size_t i = 0; i < kNumPackets; i++) {
Primiano Tuccid7b59c42017-12-19 01:43:07 +010063 auto packet = writer->NewTracePacket();
64 char str[16];
Primiano Tucci9e8ebb02018-01-29 20:48:11 +000065 sprintf(str, "foobar %zu", i);
Primiano Tuccid7b59c42017-12-19 01:43:07 +010066 packet->set_test(str);
67 }
Primiano Tucci9e8ebb02018-01-29 20:48:11 +000068
69 // Destroying the TraceWriteImpl should cause the last packet to be finalized
70 // and the chunk to be put back in the kChunkComplete state.
Primiano Tuccid7b59c42017-12-19 01:43:07 +010071 writer.reset();
72
73 SharedMemoryABI* abi = arbiter_->shmem_abi_for_testing();
74 size_t packets_count = 0;
75 for (size_t page_idx = 0; page_idx < kNumPages; page_idx++) {
76 uint32_t page_layout = abi->page_layout_dbg(page_idx);
77 size_t num_chunks = SharedMemoryABI::GetNumChunksForLayout(page_layout);
78 for (size_t chunk_idx = 0; chunk_idx < num_chunks; chunk_idx++) {
Primiano Tucci9e8ebb02018-01-29 20:48:11 +000079 auto chunk_state = abi->GetChunkState(page_idx, chunk_idx);
80 ASSERT_TRUE(chunk_state == SharedMemoryABI::kChunkFree ||
81 chunk_state == SharedMemoryABI::kChunkComplete);
82 auto chunk = abi->TryAcquireChunkForReading(page_idx, chunk_idx, kBufId);
Primiano Tuccid7b59c42017-12-19 01:43:07 +010083 if (!chunk.is_valid())
84 continue;
85 packets_count += chunk.header()->packets_state.load().count;
86 }
87 }
Primiano Tucci9e8ebb02018-01-29 20:48:11 +000088 EXPECT_EQ(kNumPackets, packets_count);
Primiano Tuccid7b59c42017-12-19 01:43:07 +010089 // TODO(primiano): check also the content of the packets decoding the protos.
90}
91
92// TODO(primiano): add multi-writer test.
93
94} // namespace
95} // namespace perfetto