1 /*
2 * Copyright (C) 2023 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
16 #include "circle_buffer.h"
17 #include "base.h"
18 #ifdef CONFIG_USE_JEMALLOC_DFX_INIF
19 #include <malloc.h>
20 #endif
21
22 namespace Hdc {
CircleBuffer()23 CircleBuffer::CircleBuffer()
24 {
25 run_ = false;
26 TimerStart();
27 #ifdef CONFIG_USE_JEMALLOC_DFX_INIF
28 mallopt(M_DELAYED_FREE, M_DELAYED_FREE_DISABLE);
29 mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_DISABLE);
30 #endif
31 }
32
~CircleBuffer()33 CircleBuffer::~CircleBuffer()
34 {
35 TimerStop();
36 for (auto iter = buffers_.begin(); iter != buffers_.end();) {
37 Data *data = iter->second;
38 delete[] data->buf;
39 delete data;
40 iter = buffers_.erase(iter);
41 }
42 }
43
Malloc()44 uint8_t *CircleBuffer::Malloc()
45 {
46 const size_t bufSize = static_cast<size_t>(Base::GetUsbffsBulkSize());
47 uint8_t *buf = nullptr;
48 std::unique_lock<std::mutex> lock(mutex_);
49 for (auto iter = buffers_.begin(); iter != buffers_.end(); ++iter) {
50 Data *data = iter->second;
51 if (data->used == false) {
52 data->used = true;
53 data->begin = std::chrono::steady_clock::now();
54 buf = data->buf;
55 break;
56 }
57 }
58 if (buf == nullptr) {
59 Data *data = new(std::nothrow) Data();
60 if (data == nullptr) {
61 return nullptr;
62 }
63 data->used = true;
64 data->begin = std::chrono::steady_clock::now();
65 data->buf = new(std::nothrow) uint8_t[bufSize];
66 if (data->buf == nullptr) {
67 delete data;
68 return nullptr;
69 }
70 uint64_t key = reinterpret_cast<uint64_t>(data->buf);
71 buffers_[key] = data;
72 buf = data->buf;
73 }
74 (void)memset_s(buf, bufSize, 0, bufSize);
75 return buf;
76 }
77
Free(const uint8_t * buf)78 void CircleBuffer::Free(const uint8_t *buf)
79 {
80 std::unique_lock<std::mutex> lock(mutex_);
81 uint64_t key = reinterpret_cast<uint64_t>(buf);
82 Data *data = buffers_[key];
83 data->used = false;
84 data->begin = std::chrono::steady_clock::now();
85 }
86
FreeMemory()87 void CircleBuffer::FreeMemory()
88 {
89 std::unique_lock<std::mutex> lock(mutex_);
90 constexpr int64_t decreaseTime = 5; // 5s
91 auto end = std::chrono::steady_clock::now();
92 for (auto iter = buffers_.begin(); iter != buffers_.end();) {
93 bool remove = false;
94 Data *data = iter->second;
95 if (data->used == false) {
96 auto begin = data->begin;
97 auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - begin);
98 if (duration.count() > decreaseTime) {
99 remove = true;
100 }
101 }
102 if (remove) {
103 delete[] data->buf;
104 delete data;
105 iter = buffers_.erase(iter);
106 } else {
107 ++iter;
108 }
109 }
110 }
111
Timer(void * object)112 void CircleBuffer::Timer(void *object)
113 {
114 #ifdef CONFIG_USE_JEMALLOC_DFX_INIF
115 mallopt(M_DELAYED_FREE, M_DELAYED_FREE_DISABLE);
116 mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_DISABLE);
117 #endif
118 CircleBuffer *cirbuf = reinterpret_cast<CircleBuffer *>(object);
119 while (cirbuf->run_) {
120 cirbuf->FreeMemory();
121 cirbuf->TimerSleep();
122 }
123 }
124
TimerStart()125 void CircleBuffer::TimerStart()
126 {
127 #ifndef HDC_HOST
128 if (!run_) {
129 run_ = true;
130 thread_ = std::thread(Timer, this);
131 }
132 #endif
133 }
134
TimerStop()135 void CircleBuffer::TimerStop()
136 {
137 #ifndef HDC_HOST
138 if (run_) {
139 run_ = false;
140 TimerNotify();
141 thread_.join();
142 }
143 #endif
144 }
145
TimerSleep()146 void CircleBuffer::TimerSleep()
147 {
148 std::unique_lock<std::mutex> lock(timerMutex_);
149 timerCv_.wait_for(lock, std::chrono::seconds(1));
150 }
151
TimerNotify()152 void CircleBuffer::TimerNotify()
153 {
154 std::unique_lock<std::mutex> lock(timerMutex_);
155 timerCv_.notify_one();
156 }
157 }
158