1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ring_buffer_test.h"
16
17 #include <chrono>
18 #include <cinttypes>
19 #include <cstdlib>
20
21 #include <linux/perf_event.h>
22
23 #include "debug_logger.h"
24 #include "utilities.h"
25
26 using namespace testing::ext;
27 using namespace std;
28 using namespace OHOS::HiviewDFX;
29 namespace OHOS {
30 namespace Developtools {
31 namespace HiPerf {
32 class RingBufferTest : public testing::Test {
33 public:
34 static constexpr size_t MEGA = 1024 * 1024;
35 static constexpr size_t CAP = 16 * MEGA;
36 static constexpr size_t U8MASK = 0xFF;
37 static constexpr size_t MAX_TESTSIZE = 1024;
38
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43
44 static void ReadBufferAndCheck(RingBuffer &buf);
45 static void WriteBuffer(RingBuffer &buf);
46 };
47
SetUpTestCase()48 void RingBufferTest::SetUpTestCase() {}
49
TearDownTestCase()50 void RingBufferTest::TearDownTestCase() {}
51
SetUp()52 void RingBufferTest::SetUp() {}
53
TearDown()54 void RingBufferTest::TearDown() {}
55
ReadBufferAndCheck(RingBuffer & buf)56 void RingBufferTest::ReadBufferAndCheck(RingBuffer &buf)
57 {
58 __u16 checkSize = sizeof(perf_event_header);
59 perf_event_header readData = {0, 0, 0};
60 uint8_t *p = nullptr;
61 while ((p = buf.GetReadData()) != nullptr) {
62 ASSERT_EQ(memcpy_s(&readData, sizeof(perf_event_header), p, sizeof(perf_event_header)), 0);
63 ASSERT_EQ(checkSize, readData.size) \
64 << " read data size " << readData.size << " expect data size " << checkSize;
65 p += sizeof(perf_event_header);
66 uint8_t data = static_cast<uint8_t>(checkSize & U8MASK);
67 for (size_t i = 0; i < (checkSize - sizeof(perf_event_header)); i++) {
68 ASSERT_EQ(*p, data) << "checkSize = " << hex << checkSize;
69 p++;
70 }
71 checkSize++;
72 checkSize %= MAX_TESTSIZE;
73 if (checkSize < sizeof(perf_event_header)) {
74 checkSize = sizeof(perf_event_header);
75 }
76 buf.EndRead();
77 }
78 }
79
WriteBuffer(RingBuffer & buf)80 void RingBufferTest::WriteBuffer(RingBuffer &buf)
81 {
82 perf_event_header writeData = {PERF_RECORD_MMAP, 0, sizeof(perf_event_header)};
83 uint8_t *p = nullptr;
84 while ((p = buf.AllocForWrite(writeData.size)) != nullptr) {
85 ASSERT_EQ(memcpy_s(p, writeData.size, &writeData, sizeof(perf_event_header)), 0);
86 p += sizeof(perf_event_header);
87 uint8_t data = static_cast<uint8_t>(writeData.size & U8MASK);
88 for (size_t i = 0; i < (writeData.size - sizeof(perf_event_header)); i++) {
89 *p = data;
90 p++;
91 }
92 buf.EndWrite();
93 writeData.size++;
94 writeData.size %= MAX_TESTSIZE;
95 if (writeData.size < sizeof(perf_event_header)) {
96 writeData.size = sizeof(perf_event_header);
97 }
98 }
99 }
100
101 /**
102 * @tc.name: Test
103 * @tc.desc: push several records of all type into the buffer
104 * @tc.type: FUNC
105 */
106 HWTEST_F(RingBufferTest, Basic, TestSize.Level1)
107 {
108 RingBuffer rb {CAP};
109 ASSERT_TRUE(rb.GetFreeSize() == CAP) << "the buffer should be empty now";
110 WriteBuffer(rb);
111 ASSERT_LE(rb.GetFreeSize(), CAP);
112
113 ReadBufferAndCheck(rb);
114 ASSERT_TRUE(rb.GetFreeSize() == CAP) << "the buffer should be empty now";
115 }
116
117 HWTEST_F(RingBufferTest, Wrap, TestSize.Level1)
118 {
119 RingBuffer rb {CAP};
120 const __u16 maxWriteSize = 65535;
121 perf_event_header writeData = {PERF_RECORD_MMAP, 0, maxWriteSize};
122 size_t half = 0;
123 uint8_t *p = nullptr;
124 while ((p = rb.AllocForWrite(writeData.size)) != nullptr) {
125 ASSERT_EQ(memcpy_s(p, writeData.size, &writeData, sizeof(perf_event_header)), 0);
126 rb.EndWrite();
127 half += writeData.size;
128 if (half >= (rb.GetFreeSize() / 2)) {
129 break;
130 }
131 }
132 ASSERT_LE(rb.GetFreeSize(), CAP);
133
134 while (rb.GetReadData() != nullptr) {
135 rb.EndRead();
136 }
137 ASSERT_EQ(rb.GetFreeSize(), CAP);
138
139 WriteBuffer(rb); // write_head has turned round
140 uint8_t *tail = rb.AllocForWrite(sizeof(perf_event_header));
141 ASSERT_NE(tail, nullptr);
142 p = rb.GetReadData();
143 ASSERT_NE(p, nullptr);
144 ASSERT_GE(p, tail); // reading has been ahead of writing
145
146 ReadBufferAndCheck(rb);
147 ASSERT_TRUE(rb.GetFreeSize() == CAP) << "the buffer should be empty now";
148 }
149 } // namespace HiPerf
150 } // namespace Developtools
151 } // namespace OHOS
152