• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cinttypes>
17 #include <unistd.h>
18 #include "logging.h"
19 #include "share_memory_allocator.h"
20 #include "stack_writer.h"
21 
StackWriter(std::string name,uint32_t size,int smbFd,int eventFd,bool blocked)22 StackWriter::StackWriter(std::string name,
23                          uint32_t size,
24                          int smbFd,
25                          int eventFd,
26                          bool blocked)
27     : pluginName_(name), blocked_(blocked)
28 {
29     shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
30     eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
31     lastFlushTime_ = std::chrono::steady_clock::now();
32 }
33 
~StackWriter()34 StackWriter::~StackWriter()
35 {
36     eventNotifier_ = nullptr;
37     ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
38     shareMemoryBlock_ = nullptr;
39 }
40 
Report() const41 void StackWriter::Report() const
42 {
43 }
44 
DoStats(long bytes)45 void StackWriter::DoStats(long bytes)
46 {
47     ++writeCount_;
48     bytesCount_ += bytes;
49     bytesPending_ += bytes;
50 }
51 
Write(const void * data,size_t size)52 long StackWriter::Write(const void* data, size_t size)
53 {
54     if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
55         return false;
56     }
57     return shareMemoryBlock_->PutRaw(reinterpret_cast<const int8_t*>(data), size);
58 }
59 
WriteTimeout(const void * data,size_t size)60 long StackWriter::WriteTimeout(const void* data, size_t size)
61 {
62     if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
63         return false;
64     }
65     return shareMemoryBlock_->PutRawTimeout(reinterpret_cast<const int8_t*>(data), size);
66 }
67 
WriteWithPayloadTimeout(const void * data,size_t size,const void * payload,size_t payloadSize,const std::function<bool ()> & callback)68 long StackWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize,
69                                           const std::function<bool()>& callback)
70 {
71     if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
72         return false;
73     }
74     if (blocked_) {
75         return shareMemoryBlock_->PutWithPayloadSync(
76             reinterpret_cast<const int8_t*>(data),
77             size,
78             reinterpret_cast<const int8_t*>(payload),
79             payloadSize,
80             callback);
81     } else {
82         return shareMemoryBlock_->PutWithPayloadTimeout(
83             reinterpret_cast<const int8_t*>(data), size, reinterpret_cast<const int8_t*>(payload), payloadSize);
84     }
85 }
86 
Flush()87 bool StackWriter::Flush()
88 {
89     ++flushCount_;
90     eventNotifier_->Post(flushCount_.load());
91     lastFlushTime_ = std::chrono::steady_clock::now();
92     bytesPending_ = 0;
93     return true;
94 }
95