• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
28 namespace Developtools {
29 namespace HiPerf {
30 class RingBufferTest : public testing::Test {
31 public:
32     static constexpr size_t mega = 1024 * 1024;
33     static constexpr size_t cap = 16 * mega;
34     static constexpr size_t u8Mask = 0xFF;
35     static constexpr size_t maxTestSize = 1024;
36 
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 
42     static void ReadBufferAndCheck(RingBuffer &buf);
43     static void WriteBuffer(RingBuffer &buf);
44 };
45 
SetUpTestCase()46 void RingBufferTest::SetUpTestCase() {}
47 
TearDownTestCase()48 void RingBufferTest::TearDownTestCase() {}
49 
SetUp()50 void RingBufferTest::SetUp() {}
51 
TearDown()52 void RingBufferTest::TearDown() {}
53 
ReadBufferAndCheck(RingBuffer & buf)54 void RingBufferTest::ReadBufferAndCheck(RingBuffer &buf)
55 {
56     __u16 checkSize = sizeof(perf_event_header);
57     perf_event_header readData = {0, 0, 0};
58     uint8_t *p = nullptr;
59     while ((p = buf.GetReadData()) != nullptr) {
60         ASSERT_EQ(memcpy_s(&readData, sizeof(perf_event_header), p, sizeof(perf_event_header)), 0);
61         ASSERT_EQ(checkSize, readData.size) \
62             << " read data size " << readData.size << " expect data size " << checkSize;
63         p += sizeof(perf_event_header);
64         uint8_t data = static_cast<uint8_t>(checkSize & u8Mask);
65         for (size_t i = 0; i < (checkSize - sizeof(perf_event_header)); i++) {
66             ASSERT_EQ(*p, data) << "checkSize = " << std::hex << checkSize;
67             p++;
68         }
69         checkSize++;
70         checkSize %= maxTestSize;
71         if (checkSize < sizeof(perf_event_header)) {
72             checkSize = sizeof(perf_event_header);
73         }
74         buf.EndRead();
75     }
76 }
77 
WriteBuffer(RingBuffer & buf)78 void RingBufferTest::WriteBuffer(RingBuffer &buf)
79 {
80     perf_event_header writeData = {PERF_RECORD_MMAP, 0, sizeof(perf_event_header)};
81     uint8_t *p = nullptr;
82     while ((p = buf.AllocForWrite(writeData.size)) != nullptr) {
83         ASSERT_EQ(memcpy_s(p, writeData.size, &writeData, sizeof(perf_event_header)), 0);
84         p += sizeof(perf_event_header);
85         uint8_t data = static_cast<uint8_t>(writeData.size & u8Mask);
86         for (size_t i = 0; i < (writeData.size - sizeof(perf_event_header)); i++) {
87             *p = data;
88             p++;
89         }
90         buf.EndWrite();
91         writeData.size++;
92         writeData.size %= maxTestSize;
93         if (writeData.size < sizeof(perf_event_header)) {
94             writeData.size = sizeof(perf_event_header);
95         }
96     }
97 }
98 
99 /**
100  * @tc.name: Test
101  * @tc.desc: push several records of all type into the buffer
102  * @tc.type: FUNC
103  */
104 HWTEST_F(RingBufferTest, Basic, TestSize.Level1)
105 {
106     RingBuffer rb {cap};
107     ASSERT_TRUE(rb.GetFreeSize() == cap) << "the buffer should be empty now";
108     WriteBuffer(rb);
109     ASSERT_LE(rb.GetFreeSize(), cap);
110 
111     ReadBufferAndCheck(rb);
112     ASSERT_TRUE(rb.GetFreeSize() == cap) << "the buffer should be empty now";
113 }
114 
115 HWTEST_F(RingBufferTest, Wrap, TestSize.Level0)
116 {
117     RingBuffer rb {cap};
118     const __u16 maxWriteSize = 65535;
119     perf_event_header writeData = {PERF_RECORD_MMAP, 0, maxWriteSize};
120     size_t half = 0;
121     uint8_t *p = nullptr;
122     while ((p = rb.AllocForWrite(writeData.size)) != nullptr) {
123         ASSERT_EQ(memcpy_s(p, writeData.size, &writeData, sizeof(perf_event_header)), 0);
124         rb.EndWrite();
125         half += writeData.size;
126         if (half >= (rb.GetFreeSize() / 2)) {
127             break;
128         }
129     }
130     ASSERT_LE(rb.GetFreeSize(), cap);
131 
132     while (rb.GetReadData() != nullptr) {
133         rb.EndRead();
134     }
135     ASSERT_EQ(rb.GetFreeSize(), cap);
136 
137     WriteBuffer(rb); // write_head has turned round
138     uint8_t *tail = rb.AllocForWrite(sizeof(perf_event_header));
139     ASSERT_NE(tail, nullptr);
140     p = rb.GetReadData();
141     ASSERT_NE(p, nullptr);
142     ASSERT_GE(p, tail); // reading has been ahead of writing
143 
144     ReadBufferAndCheck(rb);
145     ASSERT_TRUE(rb.GetFreeSize() == cap) << "the buffer should be empty now";
146 }
147 } // namespace HiPerf
148 } // namespace Developtools
149 } // namespace OHOS
150