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