1 /*
2 * Copyright (c) 2021-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 "buffer_writer.h"
17 #include "command_poller.h"
18 #include "common_types.pb.h"
19 #include "logging.h"
20 #include "plugin_service_types.pb.h"
21 #include "share_memory_allocator.h"
22
23 #include <algorithm>
24 #include <cinttypes>
25 #include <thread>
26 #include <unistd.h>
27
BufferWriter(std::string name,std::string version,uint32_t size,int smbFd,int eventFd,uint32_t pluginId)28 BufferWriter::BufferWriter(std::string name,
29 std::string version,
30 uint32_t size,
31 int smbFd,
32 int eventFd,
33 uint32_t pluginId)
34 : pluginName_(name), pluginVersion_(version)
35 {
36 HILOG_INFO(LOG_CORE, "%s:%s %d [%d] [%d]", __func__, name.c_str(), size, smbFd, eventFd);
37 shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
38 if (shareMemoryBlock_ == nullptr) {
39 HILOG_DEBUG(LOG_CORE, "%s:create shareMemoryBlock_ failed!", __func__);
40 }
41 eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
42 pluginId_ = pluginId;
43 lastFlushTime_ = std::chrono::steady_clock::now();
44 }
45
~BufferWriter()46 BufferWriter::~BufferWriter()
47 {
48 HILOG_DEBUG(LOG_CORE, "%s:destroy eventfd = %d!", __func__, eventNotifier_ ? eventNotifier_->GetFd() : -1);
49 eventNotifier_ = nullptr;
50 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
51 }
52
Report() const53 void BufferWriter::Report() const
54 {
55 HILOG_DEBUG(LOG_CORE, "%s:stats B: %" PRIu64 ", P: %d, W:%" PRIu64 ", F: %d", __func__,
56 bytesCount_.load(), bytesPending_.load(), writeCount_.load(), flushCount_.load());
57 }
58
DoStats(long bytes)59 void BufferWriter::DoStats(long bytes)
60 {
61 ++writeCount_;
62 bytesCount_ += bytes;
63 bytesPending_ += bytes;
64 }
65
Write(const void * data,size_t size)66 long BufferWriter::Write(const void* data, size_t size)
67 {
68 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
69 return false;
70 }
71
72 ProfilerPluginData pluginData;
73 pluginData.set_name(pluginName_);
74 pluginData.set_version(pluginVersion_);
75 pluginData.set_status(0);
76 pluginData.set_data(data, size);
77
78 struct timespec ts = { 0, 0 };
79 clock_gettime(clockId_, &ts);
80
81 pluginData.set_clock_id(static_cast<ProfilerPluginData_ClockId>(clockId_));
82 pluginData.set_tv_sec(ts.tv_sec);
83 pluginData.set_tv_nsec(ts.tv_nsec);
84
85 DoStats(pluginData.ByteSizeLong());
86 return shareMemoryBlock_->PutMessage(pluginData, pluginName_);
87 }
88
WriteMessage(const google::protobuf::Message & pmsg,const std::string & pluginName)89 bool BufferWriter::WriteMessage(const google::protobuf::Message& pmsg, const std::string& pluginName)
90 {
91 if (shareMemoryBlock_ == nullptr) {
92 return false;
93 }
94 DoStats(pmsg.ByteSizeLong());
95 return shareMemoryBlock_->PutMessage(pmsg, pluginName);
96 }
97
Flush()98 bool BufferWriter::Flush()
99 {
100 ++flushCount_;
101 if (eventNotifier_ == nullptr) {
102 return false;
103 }
104 eventNotifier_->Post(flushCount_.load());
105 lastFlushTime_ = std::chrono::steady_clock::now();
106 bytesPending_ = 0;
107 return true;
108 }
109
Clear()110 bool BufferWriter::Clear()
111 {
112 if (shareMemoryBlock_ == nullptr) {
113 return false;
114 }
115 shareMemoryBlock_->ClearShareMemoryBlock();
116 return true;
117 }
118