1 /*
2 * Copyright (c) 2021 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 #define HILOG_TAG "RingBuffer"
16
17 #include <linux/perf_event.h>
18 #include "ring_buffer.h"
19
20 namespace OHOS {
21 namespace Developtools {
22 namespace HiPerf {
RingBuffer(size_t size)23 RingBuffer::RingBuffer(size_t size) : size_(size)
24 {
25 if (size > 0) {
26 buf_ = std::make_unique<uint8_t[]>(size);
27 }
28 }
29
~RingBuffer()30 RingBuffer::~RingBuffer() {}
31
32 // get size of the writable space
GetFreeSize() const33 size_t RingBuffer::GetFreeSize() const
34 {
35 return size_ - (head_.load(std::memory_order_relaxed) - tail_.load(std::memory_order_relaxed));
36 }
37
AllocForWrite(size_t writeSize)38 uint8_t *RingBuffer::AllocForWrite(size_t writeSize)
39 {
40 size_t writeHead = head_.load(std::memory_order_relaxed);
41 size_t readHead = tail_.load(std::memory_order_acquire);
42 size_t writePos = writeHead % size_;
43 size_t readPos = readHead % size_;
44 writeSize_ = writeSize;
45 if (writePos < readPos) {
46 // |---writePos<---writeSize--->readPos---|
47 if (writePos + writeSize > readPos) {
48 return nullptr;
49 }
50 } else if (writePos == readPos and writeHead != readHead) {
51 // writePos catch up with readPos, but buffer is full
52 return nullptr;
53 } else {
54 // two cases: 1, writePos catch up with readPos, but buffer is empty
55 // 2, |---readPos---writePos<---writeSize--->|
56 if (writePos + writeSize > size_) {
57 // no enough space at the end
58 if (readPos < writeSize) {
59 return nullptr;
60 }
61 // wrap to the start, set mark byte
62 buf_.get()[writePos] = MARGIN_BYTE;
63 writeSize_ += (size_ - writePos);
64 writePos = 0;
65 }
66 }
67
68 return buf_.get() + writePos;
69 }
70
EndWrite()71 void RingBuffer::EndWrite()
72 {
73 size_t head = head_.load(std::memory_order_relaxed);
74 head += writeSize_;
75 head_.store(head, std::memory_order_release);
76 }
77
GetReadData()78 uint8_t *RingBuffer::GetReadData()
79 {
80 size_t writeHead = head_.load(std::memory_order_acquire);
81 size_t readHead = tail_.load(std::memory_order_relaxed);
82 if (writeHead == readHead) {
83 return nullptr;
84 }
85
86 readSize_ = 0;
87 size_t writePos = writeHead % size_;
88 size_t readPos = readHead % size_;
89 if (writePos <= readPos) {
90 // |<---data2--->writePos---readPos<---data1--->|
91 if (buf_.get()[readPos] == MARGIN_BYTE) {
92 if (writePos == 0) {
93 return nullptr;
94 }
95 readSize_ = (size_ - readPos);
96 readPos = 0;
97 }
98 }
99 // else |---readPos<---data--->writePos---|
100 perf_event_header *header = reinterpret_cast<perf_event_header *>(buf_.get() + readPos);
101 readSize_ += header->size;
102 return buf_.get() + readPos;
103 }
104
EndRead()105 void RingBuffer::EndRead()
106 {
107 size_t tail = tail_.load(std::memory_order_relaxed);
108 tail += readSize_;
109 tail_.store(tail, std::memory_order_release);
110 }
111 } // namespace HiPerf
112 } // namespace Developtools
113 } // namespace OHOS
114