1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 "share_memory_block.h"
17
18 #include <cstring>
19 #include <fcntl.h>
20 #include <sys/mman.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23 #include "ashmem.h"
24 #include "logging.h"
25 #include "securec.h"
26
27 namespace {
28 const int PIECE_HEAD_LEN = 4;
29 constexpr uint32_t INVALID_LENGTH = (uint32_t)-1;
30 constexpr uint32_t TIMEOUT_SEC = 1;
31 const int WAIT_RELEASE_TIMEOUT_US = 10;
32 #ifndef PAGE_SIZE
33 constexpr uint32_t PAGE_SIZE = 4096;
34 #endif
35 } // namespace
36
37 struct PthreadLocker {
PthreadLockerPthreadLocker38 explicit PthreadLocker(pthread_mutex_t& mutex) : mutex_(mutex)
39 {
40 pthread_mutex_lock(&mutex_);
41 }
42
~PthreadLockerPthreadLocker43 ~PthreadLocker()
44 {
45 pthread_mutex_unlock(&mutex_);
46 }
47
48 private:
49 pthread_mutex_t& mutex_;
50 };
51
ShareMemoryBlock()52 ShareMemoryBlock::ShareMemoryBlock()
53 : fileDescriptor_(-1),
54 memoryPoint_(nullptr),
55 memorySize_(0),
56 memoryName_(),
57 header_(nullptr),
58 reusePloicy_(ReusePolicy::DROP_NONE)
59 {
60 }
61
CreateBlockWithFd(std::string name,uint32_t size,int fd)62 bool ShareMemoryBlock::CreateBlockWithFd(std::string name, uint32_t size, int fd)
63 {
64 CHECK_TRUE(fd >= 0, false, "CreateBlock FAIL SYS_memfd_create");
65
66 auto ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
67 if (ptr == MAP_FAILED) {
68 const int bufSize = 256;
69 char buf[bufSize] = {0};
70 strerror_r(errno, buf, bufSize);
71 PROFILER_LOG_ERROR(LOG_CORE, "CreateBlockWithFd mmap ERR : %s", buf);
72 return false;
73 }
74
75 fileDescriptor_ = fd;
76 memoryPoint_ = ptr;
77 memorySize_ = size;
78
79 memoryName_ = name;
80 header_ = reinterpret_cast<BlockHeader*>(ptr);
81
82 // Reserve 4 bytes to fill the message length.
83 messageWriteOffset_ = PIECE_HEAD_LEN;
84 // Functions required to bind the BaseMessage class.
85 smbCtx_.block = this;
86 smbCtx_.ctx.getMemory = [](RandomWriteCtx* ctx, uint32_t size, uint8_t** memory, uint32_t* offset) -> bool {
87 ShareMemoryBlockCtx* smbCtx = reinterpret_cast<ShareMemoryBlockCtx*>(ctx);
88 return smbCtx->block->GetMemory(size, memory, offset);
89 };
90 smbCtx_.ctx.seek = [](RandomWriteCtx* ctx, uint32_t offset) -> bool {
91 ShareMemoryBlockCtx* smbCtx = reinterpret_cast<ShareMemoryBlockCtx*>(ctx);
92 return smbCtx->block->Seek(offset);
93 };
94 return true;
95 }
96
CreateBlock(std::string name,uint32_t size)97 bool ShareMemoryBlock::CreateBlock(std::string name, uint32_t size)
98 {
99 PROFILER_LOG_INFO(LOG_CORE, "CreateBlock %s %d", name.c_str(), size);
100 CHECK_TRUE(size > sizeof(BlockHeader), false, "size %u too less!", size);
101 CHECK_TRUE(size % PAGE_SIZE == 0, false, "size %u not times of %d!", size, PAGE_SIZE);
102
103 int fd = OHOS::AshmemCreate(name.c_str(), size);
104 CHECK_TRUE(fd >= 0, false, "OHOS::AshmemCreate fail.");
105
106 int check = OHOS::AshmemSetProt(fd, PROT_READ | PROT_WRITE);
107 if (check < 0) {
108 close(fd);
109 const int bufSize = 256;
110 char buf[bufSize] = {0};
111 strerror_r(errno, buf, bufSize);
112 PROFILER_LOG_ERROR(LOG_CORE, "OHOS::AshmemSetProt ERR : %s", buf);
113 return false;
114 }
115
116 auto ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
117 if (ptr == MAP_FAILED) {
118 close(fd);
119 const int bufSize = 256;
120 char buf[bufSize] = {0};
121 strerror_r(errno, buf, bufSize);
122 PROFILER_LOG_ERROR(LOG_CORE, "CreateBlock mmap ERR : %s", buf);
123 return false;
124 }
125
126 fileDescriptor_ = fd;
127 memoryPoint_ = ptr;
128 memorySize_ = size;
129
130 memoryName_ = name;
131 header_ = reinterpret_cast<BlockHeader*>(ptr);
132
133 // initialize header infos
134 header_->info.readOffset_ = 0;
135 header_->info.writeOffset_ = 0;
136 header_->info.memorySize_ = size - sizeof(BlockHeader);
137 header_->info.bytesCount_ = 0;
138 header_->info.chunkCount_ = 0;
139
140 pthread_mutexattr_t muAttr;
141 pthread_mutexattr_init(&muAttr);
142 pthread_mutexattr_setpshared(&muAttr, PTHREAD_PROCESS_SHARED);
143 pthread_mutexattr_settype(&muAttr, PTHREAD_MUTEX_RECURSIVE);
144 pthread_mutex_init(&header_->info.mutex_, &muAttr);
145 return true;
146 }
147
Valid() const148 bool ShareMemoryBlock::Valid() const
149 {
150 return header_ != nullptr;
151 }
152
ShareMemoryBlock(const std::string & name,uint32_t size)153 ShareMemoryBlock::ShareMemoryBlock(const std::string& name, uint32_t size) : ShareMemoryBlock()
154 {
155 CreateBlock(name, size);
156 }
157
ShareMemoryBlock(const std::string & name,uint32_t size,int fd)158 ShareMemoryBlock::ShareMemoryBlock(const std::string& name, uint32_t size, int fd) : ShareMemoryBlock()
159 {
160 CreateBlockWithFd(name, size, fd);
161 }
162
~ShareMemoryBlock()163 ShareMemoryBlock::~ShareMemoryBlock()
164 {
165 ReleaseBlock();
166 }
167
ReleaseBlock()168 bool ShareMemoryBlock::ReleaseBlock()
169 {
170 if (memorySize_ > 0) {
171 munmap(memoryPoint_, memorySize_);
172 memoryPoint_ = nullptr;
173 memorySize_ = 0;
174 }
175
176 if (fileDescriptor_ >= 0) {
177 close(fileDescriptor_);
178 fileDescriptor_ = -1;
179 }
180 return true;
181 }
182
GetCurrentFreeMemory(uint32_t size)183 int8_t* ShareMemoryBlock::GetCurrentFreeMemory(uint32_t size)
184 {
185 CHECK_NOTNULL(header_, nullptr, "header not ready!");
186 uint32_t realSize = size + PIECE_HEAD_LEN + PIECE_HEAD_LEN;
187
188 uint32_t wp = header_->info.writeOffset_.load();
189 if (wp + realSize > header_->info.memorySize_) { // 后面部分放不下,从头开始放
190 if (header_->info.readOffset_.load() == 0) {
191 return nullptr;
192 }
193 *((uint32_t*)(&header_->data[wp])) = INVALID_LENGTH;
194 wp = 0;
195 }
196 if (wp < header_->info.readOffset_.load() && header_->info.readOffset_.load() < wp + realSize) { //
197 return nullptr;
198 }
199
200 return &header_->data[wp + PIECE_HEAD_LEN];
201 }
202
GetFreeMemory(uint32_t size)203 int8_t* ShareMemoryBlock::GetFreeMemory(uint32_t size)
204 {
205 if (reusePloicy_ == ReusePolicy::DROP_NONE) {
206 return GetCurrentFreeMemory(size);
207 }
208 int8_t* ret = nullptr;
209 while (true) {
210 ret = GetCurrentFreeMemory(size);
211 if (ret != nullptr) {
212 break;
213 }
214 if (!Next()) {
215 return nullptr;
216 }
217 }
218 return ret;
219 }
220
UseFreeMemory(int8_t * pmem,uint32_t size)221 bool ShareMemoryBlock::UseFreeMemory(int8_t* pmem, uint32_t size)
222 {
223 uint32_t wp = pmem - PIECE_HEAD_LEN - header_->data;
224 *((int*)(&header_->data[wp])) = size;
225
226 header_->info.writeOffset_ = wp + PIECE_HEAD_LEN + size;
227 return true;
228 }
229
PutRaw(const int8_t * data,uint32_t size)230 bool ShareMemoryBlock::PutRaw(const int8_t* data, uint32_t size)
231 {
232 CHECK_NOTNULL(header_, false, "header not ready!");
233 PthreadLocker locker(header_->info.mutex_);
234 int8_t* rawMemory = GetFreeMemory(size);
235 if (rawMemory == nullptr) {
236 PROFILER_LOG_ERROR(LOG_CORE, "PutRaw not enough space [%d]", size);
237 return false;
238 }
239 if (memcpy_s(rawMemory, size, data, size) != EOK) {
240 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s error");
241 return false;
242 }
243
244 UseFreeMemory(rawMemory, size);
245 ++header_->info.bytesCount_;
246 ++header_->info.chunkCount_;
247 return true;
248 }
249
PutRawTimeout(const int8_t * data,uint32_t size)250 bool ShareMemoryBlock::PutRawTimeout(const int8_t* data, uint32_t size)
251 {
252 CHECK_NOTNULL(header_, false, "header not ready!");
253
254 struct timespec time_out;
255 clock_gettime(CLOCK_REALTIME, &time_out);
256 time_out.tv_sec += TIMEOUT_SEC;
257 if (pthread_mutex_timedlock(&header_->info.mutex_, &time_out) != 0) {
258 PROFILER_LOG_ERROR(LOG_CORE, "PutRawTimeout failed %d", errno);
259 return false;
260 }
261
262 int8_t* rawMemory = GetFreeMemory(size);
263 if (rawMemory == nullptr) {
264 PROFILER_LOG_ERROR(LOG_CORE, "PutRaw not enough space [%d]", size);
265 pthread_mutex_unlock(&header_->info.mutex_);
266 return false;
267 }
268 if (memcpy_s(rawMemory, size, data, size) != EOK) {
269 PROFILER_LOG_ERROR(LOG_CORE, "memcpy_s error");
270 pthread_mutex_unlock(&header_->info.mutex_);
271 return false;
272 }
273
274 UseFreeMemory(rawMemory, size);
275 ++header_->info.bytesCount_;
276 ++header_->info.chunkCount_;
277
278 pthread_mutex_unlock(&header_->info.mutex_);
279 return true;
280 }
281
PutWithPayloadTimeout(const int8_t * header,uint32_t headerSize,const int8_t * payload,uint32_t payloadSize)282 bool ShareMemoryBlock::PutWithPayloadTimeout(const int8_t* header, uint32_t headerSize,
283 const int8_t* payload, uint32_t payloadSize)
284 {
285 CHECK_NOTNULL(header_, false, "header not ready!");
286 struct timespec time_out;
287 clock_gettime(CLOCK_REALTIME, &time_out);
288 time_out.tv_sec += TIMEOUT_SEC;
289 if (pthread_mutex_timedlock(&header_->info.mutex_, &time_out) != 0) {
290 return false;
291 }
292
293 int8_t* rawMemory = GetFreeMemory(headerSize + payloadSize);
294 if (rawMemory == nullptr) {
295 pthread_mutex_unlock(&header_->info.mutex_);
296 return false;
297 }
298 if (memcpy_s(rawMemory, headerSize, header, headerSize) != EOK) {
299 pthread_mutex_unlock(&header_->info.mutex_);
300 return false;
301 }
302 if (payloadSize > 0) {
303 if (memcpy_s(rawMemory + headerSize, payloadSize, payload, payloadSize) != EOK) {
304 pthread_mutex_unlock(&header_->info.mutex_);
305 return false;
306 }
307 }
308 UseFreeMemory(rawMemory, headerSize + payloadSize);
309 ++header_->info.bytesCount_;
310 ++header_->info.chunkCount_;
311
312 pthread_mutex_unlock(&header_->info.mutex_);
313 return true;
314 }
315
316 #ifndef NO_PROTOBUF
PutMessage(const google::protobuf::Message & pmsg,const std::string & pluginName)317 bool ShareMemoryBlock::PutMessage(const google::protobuf::Message& pmsg, const std::string& pluginName)
318 {
319 size_t size = pmsg.ByteSizeLong();
320
321 CHECK_NOTNULL(header_, false, "header not ready!");
322 PthreadLocker locker(header_->info.mutex_);
323 int8_t* rawMemory = GetFreeMemory(size);
324 if (rawMemory == nullptr) {
325 PROFILER_LOG_ERROR(LOG_CORE, "%s: PutMessage not enough space [%zu]", pluginName.c_str(), size);
326 return false;
327 }
328
329 int ret = pmsg.SerializeToArray(rawMemory, size);
330 if (ret <= 0) {
331 PROFILER_LOG_ERROR(LOG_CORE, "%s: SerializeToArray failed with %d, size: %zu", __func__, ret, size);
332 return false;
333 }
334 UseFreeMemory(rawMemory, size);
335 ++header_->info.bytesCount_;
336 ++header_->info.chunkCount_;
337 return true;
338 }
339 #endif
340
TakeData(const DataHandler & func,bool isProtobufSerialize)341 bool ShareMemoryBlock::TakeData(const DataHandler& func, bool isProtobufSerialize)
342 {
343 if (!isProtobufSerialize) {
344 return TakeDataOptimize(func);
345 }
346
347 CHECK_NOTNULL(header_, false, "header not ready!");
348 CHECK_TRUE(static_cast<bool>(func), false, "func invalid!");
349
350 auto size = GetDataSize();
351 if (size == 0) {
352 return false;
353 }
354 auto ptr = GetDataPoint();
355 CHECK_TRUE(func(ptr, size), false, "call func FAILED!");
356 CHECK_TRUE(Next(), false, "move read pointer FAILED!");
357 --header_->info.chunkCount_;
358 return true;
359 }
360
GetDataSize()361 uint32_t ShareMemoryBlock::GetDataSize()
362 {
363 if (header_->info.readOffset_.load() == header_->info.writeOffset_.load()) {
364 return 0;
365 }
366 uint32_t ret = *((uint32_t*)(&header_->data[header_->info.readOffset_.load()]));
367 if (ret == INVALID_LENGTH) {
368 ret = *((uint32_t*)(&header_->data[0]));
369 }
370 return ret;
371 }
372
GetDataPoint()373 const int8_t* ShareMemoryBlock::GetDataPoint()
374 {
375 if (*((uint32_t*)(&header_->data[header_->info.readOffset_.load()])) == INVALID_LENGTH) {
376 return &header_->data[PIECE_HEAD_LEN];
377 }
378 return &header_->data[header_->info.readOffset_ .load()+ PIECE_HEAD_LEN];
379 }
380
Next()381 bool ShareMemoryBlock::Next()
382 {
383 if (header_->info.readOffset_.load() == header_->info.writeOffset_.load()) {
384 return false;
385 }
386 uint32_t size = *((uint32_t*)(&header_->data[header_->info.readOffset_.load()]));
387 if (size == INVALID_LENGTH) {
388 size = *((uint32_t*)(&header_->data[0]));
389 header_->info.readOffset_ = size + PIECE_HEAD_LEN;
390 } else {
391 header_->info.readOffset_ += size + PIECE_HEAD_LEN;
392 }
393 return true;
394 }
395
GetName()396 std::string ShareMemoryBlock::GetName()
397 {
398 return memoryName_;
399 }
400
GetSize()401 uint32_t ShareMemoryBlock::GetSize()
402 {
403 return memorySize_;
404 }
405
GetfileDescriptor()406 int ShareMemoryBlock::GetfileDescriptor()
407 {
408 return fileDescriptor_;
409 }
410
PutWithPayloadSync(const int8_t * header,uint32_t headerSize,const int8_t * payload,uint32_t payloadSize,const std::function<bool ()> & callback)411 bool ShareMemoryBlock::PutWithPayloadSync(const int8_t* header, uint32_t headerSize,
412 const int8_t* payload, uint32_t payloadSize, const std::function<bool()>& callback)
413 {
414 CHECK_NOTNULL(header_, false, "header not ready!");
415 pthread_mutex_lock(&header_->info.mutex_);
416 int8_t* rawMemory = GetFreeMemory(headerSize + payloadSize);
417 if (rawMemory == nullptr) {
418 while (true) {
419 if (rawMemory == nullptr) {
420 if (callback && callback()) {
421 pthread_mutex_unlock(&header_->info.mutex_);
422 return false;
423 }
424 pthread_mutex_unlock(&header_->info.mutex_);
425 usleep(WAIT_RELEASE_TIMEOUT_US);
426 pthread_mutex_lock(&header_->info.mutex_);
427 rawMemory = GetFreeMemory(headerSize + payloadSize);
428 continue;
429 }
430 break;
431 }
432 }
433 if (memcpy_s(rawMemory, headerSize + payloadSize, header, headerSize) != EOK) {
434 pthread_mutex_unlock(&header_->info.mutex_);
435 return false;
436 }
437 if (payloadSize > 0) {
438 if (memcpy_s(rawMemory + headerSize, payloadSize, payload, payloadSize) != EOK) {
439 pthread_mutex_unlock(&header_->info.mutex_);
440 return false;
441 }
442 }
443 UseFreeMemory(rawMemory, headerSize + payloadSize);
444 ++header_->info.bytesCount_;
445 ++header_->info.chunkCount_;
446 pthread_mutex_unlock(&header_->info.mutex_);
447 return true;
448 }
449
UseMemory(int32_t size)450 void ShareMemoryBlock::UseMemory(int32_t size)
451 {
452 CHECK_TRUE(header_ != nullptr, NO_RETVAL, "header not ready!");
453 CHECK_TRUE(size > 0, NO_RETVAL, "size(%d) is invalid", size);
454
455 uint32_t wp = header_->info.writeOffset_.load(std::memory_order_relaxed);
456 *((int*)(&header_->data[wp])) = size;
457 header_->info.writeOffset_.store(wp + PIECE_HEAD_LEN + size, std::memory_order_release);
458 }
459
GetMemory(uint32_t size,uint8_t ** memory,uint32_t * offset)460 bool ShareMemoryBlock::GetMemory(uint32_t size, uint8_t** memory, uint32_t* offset)
461 {
462 CHECK_NOTNULL(header_, false, "header not ready!");
463
464 // The actual size is to store data with a size of offset and a size of data and a four byte tail tag.
465 uint32_t realSize = messageWriteOffset_ + size + PIECE_HEAD_LEN;
466 uint32_t wp = header_->info.writeOffset_.load(std::memory_order_relaxed);
467 uint32_t rp = header_->info.readOffset_.load(std::memory_order_acquire);
468 if (rp <= wp) {
469 if (wp + realSize <= header_->info.memorySize_) {
470 // enough tail space to store data.
471 *memory = reinterpret_cast<uint8_t *>(&header_->data[wp + messageWriteOffset_]);
472 *offset = messageWriteOffset_;
473 return true;
474 } else if (realSize <= rp) {
475 // there is data in the tail, and it is need to copy the data in the tail to the header for saving.
476 auto ret = memcpy_s(&header_->data[0], messageWriteOffset_, &header_->data[wp], messageWriteOffset_);
477 CHECK_TRUE(ret == EOK, false, "memcpy_s messageWriteOffset_(%d) data failed", messageWriteOffset_);
478 // set trailing data end tag.
479 *((uint32_t*)(&header_->data[wp])) = INVALID_LENGTH;
480 // set writeOffset_ to zero.
481 header_->info.writeOffset_.store(0, std::memory_order_release);
482 *memory = reinterpret_cast<uint8_t *>(&header_->data[messageWriteOffset_]);
483 *offset = messageWriteOffset_;
484 return true;
485 }
486 } else {
487 if (wp + realSize <= rp) {
488 // rp is after wp and there is enough space to store data.
489 *memory = reinterpret_cast<uint8_t *>(&header_->data[wp + messageWriteOffset_]);
490 *offset = messageWriteOffset_;
491 return true;
492 }
493 }
494
495 PROFILER_LOG_ERROR(LOG_CORE, "Write not enough space, realSize=%u, rp=%u, wp=%u", realSize, rp, wp);
496 return false;
497 }
498
TakeDataOptimize(const DataHandler & func)499 bool ShareMemoryBlock::TakeDataOptimize(const DataHandler& func)
500 {
501 CHECK_NOTNULL(header_, false, "header not ready!");
502 CHECK_TRUE(static_cast<bool>(func), false, "func invalid!");
503
504 uint32_t wp = header_->info.writeOffset_.load(std::memory_order_acquire);
505 uint32_t rp = header_->info.readOffset_.load(std::memory_order_relaxed);
506 int8_t* ptr = nullptr;
507 uint32_t size = 0;
508 if (rp < wp) {
509 // |---rp<---data--->wp---|
510 size = *((uint32_t*)(&header_->data[rp]));
511 ptr = &header_->data[rp + PIECE_HEAD_LEN];
512 } else if (wp < rp) {
513 // |<---data2--->wp---rp<---data1--->|
514 size = *((uint32_t*)(&header_->data[rp]));
515 // Size is the end tag of the tail and needs to be retrieved from the header.
516 if (size == INVALID_LENGTH) {
517 if (wp == 0) {
518 // no data to read.
519 return false;
520 }
521 rp = 0;
522 size = *((uint32_t*)(&header_->data[rp]));
523 }
524 ptr = &header_->data[rp + PIECE_HEAD_LEN];
525 } else {
526 // wp == rp
527 return false;
528 }
529 CHECK_NOTNULL(ptr, false, "ptr is nullptr");
530
531 // Start writing file.
532 CHECK_TRUE(func(ptr, size), false, "call func FAILED!");
533
534 header_->info.readOffset_.store(rp + size + PIECE_HEAD_LEN, std::memory_order_release);
535 return true;
536 }
537
Seek(uint32_t pos)538 bool ShareMemoryBlock::Seek(uint32_t pos)
539 {
540 messageWriteOffset_ = pos;
541 return true;
542 }
543
ResetPos()544 void ShareMemoryBlock::ResetPos()
545 {
546 messageWriteOffset_ = PIECE_HEAD_LEN;
547 }