/* * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "logging.h" #include "share_memory_allocator.h" #include "stack_writer.h" namespace { const int WAIT_TIMEOUT_SA = 3000000; } StackWriter::StackWriter(std::string name, uint32_t size, int smbFd, int eventFd, bool blocked, bool isSaMode) : pluginName_(name), blocked_(blocked) { shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd); if (isSaMode && shareMemoryBlock_ != nullptr) { shareMemoryBlock_->SetWaitTime(WAIT_TIMEOUT_SA); } eventNotifier_ = EventNotifier::CreateWithFd(eventFd); lastFlushTime_ = std::chrono::steady_clock::now(); } StackWriter::~StackWriter() { eventNotifier_ = nullptr; ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_); shareMemoryBlock_ = nullptr; } void StackWriter::Report() const { } void StackWriter::DoStats(long bytes) { ++writeCount_; bytesCount_ += bytes; bytesPending_ += bytes; } long StackWriter::Write(const void* data, size_t size) { if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) { return false; } return shareMemoryBlock_->PutRaw(reinterpret_cast(data), size); } long StackWriter::WriteTimeout(const void* data, size_t size) { if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) { return false; } return shareMemoryBlock_->PutRawTimeout(reinterpret_cast(data), size); } long StackWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload, size_t payloadSize, const std::function& callback) { if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) { return false; } if (blocked_) { return shareMemoryBlock_->PutWithPayloadSync( reinterpret_cast(data), size, reinterpret_cast(payload), payloadSize, callback); } else { return shareMemoryBlock_->PutWithPayloadTimeout( reinterpret_cast(data), size, reinterpret_cast(payload), payloadSize); } } bool StackWriter::Flush() { ++flushCount_; eventNotifier_->Post(flushCount_.load()); lastFlushTime_ = std::chrono::steady_clock::now(); bytesPending_ = 0; return true; }