Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: components/tracing/core/trace_ring_buffer_unittest.cc

Issue 2164013002: [OBSOLETE] tracing v2: introduce TraceBufferWriter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proto_handle
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/tracing/core/trace_ring_buffer.cc ('k') | components/tracing/proto/event.proto » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/tracing/core/trace_ring_buffer.h" 5 #include "components/tracing/core/trace_ring_buffer.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 namespace tracing { 9 namespace tracing {
10 namespace v2 { 10 namespace v2 {
11 11
12 namespace { 12 namespace {
13 13
14 const size_t kChunkSize = TraceRingBuffer::Chunk::kSize; 14 const size_t kChunkSize = TraceRingBuffer::Chunk::kSize;
15 15
16 bool overlap(uint8_t* start1, uint8_t* end1, uint8_t* start2, uint8_t* end2) { 16 bool overlap(uint8_t* start1, uint8_t* end1, uint8_t* start2, uint8_t* end2) {
17 return start1 < end2 && start2 < end1; 17 return start1 < end2 && start2 < end1;
18 } 18 }
19 19
20 TEST(TraceRingBufferTest, BasicChunkWrapping) { 20 TEST(TraceRingBufferTest, BasicChunkWrapping) {
21 const uint32_t kNumChunks = 5; 21 const uint32_t kNumChunks = 5;
22 const size_t kBufferSize = kChunkSize * kNumChunks; 22 const size_t kBufferSize = kChunkSize * kNumChunks;
23 std::unique_ptr<uint8_t[]> storage(new uint8_t[kBufferSize]); 23 std::unique_ptr<uint8_t[]> storage(new uint8_t[kBufferSize]);
24 TraceRingBuffer ring_buffer(storage.get(), kBufferSize); 24 TraceRingBuffer ring_buffer(storage.get(), kBufferSize);
25 25
26 uint8_t* last_chunk_end = nullptr; 26 uint8_t* last_chunk_end = nullptr;
27 // Fill the buffer twice to test wrapping logic. 27 // Fill the buffer twice to test wrapping logic.
28 for (uint32_t i = 0; i < kNumChunks * 2; ++i) { 28 for (uint32_t i = 0; i < kNumChunks * 2; ++i) {
29 TraceRingBuffer::Chunk* chunk = ring_buffer.TakeChunk(); 29 TraceRingBuffer::Chunk* chunk = ring_buffer.TakeChunk(42 /* owner */);
30 ASSERT_NE(nullptr, chunk); 30 ASSERT_NE(nullptr, chunk);
31 EXPECT_EQ(42u, chunk->owner());
31 const uint32_t chunk_idx = i % kNumChunks; 32 const uint32_t chunk_idx = i % kNumChunks;
32 EXPECT_EQ(chunk_idx == 0 ? storage.get() : last_chunk_end, chunk->begin()); 33 EXPECT_EQ(chunk_idx == 0 ? storage.get() : last_chunk_end, chunk->begin());
33 const uint32_t kPayloadSize = (chunk_idx + 1) * 8; 34 const uint32_t kPayloadSize = (chunk_idx + 1) * 8;
34 memset(chunk->payload(), static_cast<int>(chunk_idx + 1), kPayloadSize); 35 memset(chunk->payload(), static_cast<int>(chunk_idx + 1), kPayloadSize);
35 last_chunk_end = chunk->end(); 36 last_chunk_end = chunk->end();
36 ring_buffer.ReturnChunk(chunk, /* used_size = */ kPayloadSize); 37 chunk->set_used_size(kPayloadSize);
38 ring_buffer.ReturnChunk(chunk);
37 } 39 }
38 40
39 // Now scrape the |storage| buffer and check its contents. 41 // Now scrape the |storage| buffer and check its contents.
40 for (uint32_t chunk_idx = 0; chunk_idx < kNumChunks; ++chunk_idx) { 42 for (uint32_t chunk_idx = 0; chunk_idx < kNumChunks; ++chunk_idx) {
41 uint8_t* chunk_start = storage.get() + (chunk_idx * kChunkSize); 43 uint8_t* chunk_start = storage.get() + (chunk_idx * kChunkSize);
42 const uint32_t kPayloadSize = (chunk_idx + 1) * 8; 44 const uint32_t kPayloadSize = (chunk_idx + 1) * 8;
43 EXPECT_EQ(kPayloadSize, *reinterpret_cast<uint32_t*>(chunk_start)); 45 EXPECT_EQ(kPayloadSize, *reinterpret_cast<uint32_t*>(chunk_start));
44 for (uint32_t i = 0; i < kPayloadSize; ++i) 46 for (uint32_t i = 0; i < kPayloadSize; ++i)
45 EXPECT_EQ(chunk_idx + 1, *(chunk_start + sizeof(uint32_t) + i)); 47 EXPECT_EQ(chunk_idx + 1, *(chunk_start + sizeof(uint32_t) + i));
46 } 48 }
47 } 49 }
48 50
49 TEST(TraceRingBufferTest, ChunkBankrupcyDoesNotCrash) { 51 TEST(TraceRingBufferTest, ChunkBankrupcyDoesNotCrash) {
50 const size_t kNumChunks = 2; 52 const size_t kNumChunks = 2;
51 const size_t kBufferSize = TraceRingBuffer::Chunk::kSize * kNumChunks; 53 const size_t kBufferSize = TraceRingBuffer::Chunk::kSize * kNumChunks;
52 std::unique_ptr<uint8_t[]> storage(new uint8_t[kBufferSize]); 54 std::unique_ptr<uint8_t[]> storage(new uint8_t[kBufferSize]);
53 TraceRingBuffer ring_buffer(storage.get(), kBufferSize); 55 TraceRingBuffer ring_buffer(storage.get(), kBufferSize);
54 56
55 TraceRingBuffer::Chunk* chunk1 = ring_buffer.TakeChunk(); 57 TraceRingBuffer::Chunk* chunk1 = ring_buffer.TakeChunk(1);
56 ASSERT_NE(nullptr, chunk1); 58 ASSERT_NE(nullptr, chunk1);
57 59
58 TraceRingBuffer::Chunk* chunk2 = ring_buffer.TakeChunk(); 60 TraceRingBuffer::Chunk* chunk2 = ring_buffer.TakeChunk(1);
59 ASSERT_NE(nullptr, chunk2); 61 ASSERT_NE(nullptr, chunk2);
60 62
61 for (int i = 0; i < 3; ++i) { 63 for (int i = 0; i < 3; ++i) {
62 TraceRingBuffer::Chunk* bankrupcy_chunk = ring_buffer.TakeChunk(); 64 TraceRingBuffer::Chunk* bankrupcy_chunk = ring_buffer.TakeChunk(1);
63 ASSERT_NE(nullptr, bankrupcy_chunk); 65 ASSERT_NE(nullptr, bankrupcy_chunk);
64 ASSERT_FALSE(overlap(bankrupcy_chunk->begin(), bankrupcy_chunk->end(), 66 ASSERT_FALSE(overlap(bankrupcy_chunk->begin(), bankrupcy_chunk->end(),
65 storage.get(), storage.get() + kBufferSize)); 67 storage.get(), storage.get() + kBufferSize));
66 68
67 // Make sure that the memory of the bankrupty chunk can be dereferenced. 69 // Make sure that the memory of the bankrupty chunk can be dereferenced.
68 memset(bankrupcy_chunk->begin(), 0, kChunkSize); 70 memset(bankrupcy_chunk->begin(), 0, kChunkSize);
69 } 71 }
70 72
71 // Return a chunk and check that the ring buffer is not bankrupt anymore. 73 // Return a chunk and check that the ring buffer is not bankrupt anymore.
72 ring_buffer.ReturnChunk(chunk2, 42); 74 chunk2->set_used_size(42);
73 TraceRingBuffer::Chunk* chunk = ring_buffer.TakeChunk(); 75 ring_buffer.ReturnChunk(chunk2);
76 TraceRingBuffer::Chunk* chunk = ring_buffer.TakeChunk(1);
74 ASSERT_NE(nullptr, chunk); 77 ASSERT_NE(nullptr, chunk);
75 ASSERT_TRUE(overlap(chunk->begin(), chunk->end(), storage.get(), 78 ASSERT_TRUE(overlap(chunk->begin(), chunk->end(), storage.get(),
76 storage.get() + kBufferSize)); 79 storage.get() + kBufferSize));
77 } 80 }
78 81
79 } // namespace 82 } // namespace
80 } // namespace v2 83 } // namespace v2
81 } // namespace tracing 84 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/core/trace_ring_buffer.cc ('k') | components/tracing/proto/event.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698