1 /*
2 * Copyright (c) 2021 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,uint32_t size,int smbFd,int eventFd,uint32_t pluginId)28 BufferWriter::BufferWriter(std::string name,
29 uint32_t size,
30 int smbFd,
31 int eventFd,
32 uint32_t pluginId)
33 : pluginName_(name)
34 {
35 HILOG_INFO(LOG_CORE, "%s:%s %d [%d] [%d]", __func__, name.c_str(), size, smbFd, eventFd);
36 shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
37 if (shareMemoryBlock_ == nullptr) {
38 HILOG_DEBUG(LOG_CORE, "%s:create shareMemoryBlock_ failed!", __func__);
39 }
40 eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
41 pluginId_ = pluginId;
42 lastFlushTime_ = std::chrono::steady_clock::now();
43 }
44
~BufferWriter()45 BufferWriter::~BufferWriter()
46 {
47 HILOG_DEBUG(LOG_CORE, "%s:destroy eventfd = %d!", __func__, eventNotifier_ ? eventNotifier_->GetFd() : -1);
48 eventNotifier_ = nullptr;
49 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
50 }
51
Report() const52 void BufferWriter::Report() const
53 {
54 HILOG_DEBUG(LOG_CORE, "%s:stats B: %" PRIu64 ", P: %d, W:%" PRIu64 ", F: %d", __func__,
55 bytesCount_.load(), bytesPending_.load(), writeCount_.load(), flushCount_.load());
56 }
57
DoStats(long bytes)58 void BufferWriter::DoStats(long bytes)
59 {
60 ++writeCount_;
61 bytesCount_ += bytes;
62 bytesPending_ += bytes;
63 }
64
Write(const void * data,size_t size)65 long BufferWriter::Write(const void* data, size_t size)
66 {
67 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
68 return false;
69 }
70
71 ProfilerPluginData pluginData;
72 pluginData.set_name(pluginName_);
73 pluginData.set_status(0);
74 pluginData.set_data(data, size);
75
76 struct timespec ts = { 0, 0 };
77 clock_gettime(CLOCK_REALTIME, &ts);
78
79 pluginData.set_clock_id(ProfilerPluginData::CLOCKID_REALTIME);
80 pluginData.set_tv_sec(ts.tv_sec);
81 pluginData.set_tv_nsec(ts.tv_nsec);
82
83 DoStats(pluginData.ByteSizeLong());
84 return shareMemoryBlock_->PutMessage(pluginData);
85 }
86
WriteMessage(const google::protobuf::Message & pmsg)87 bool BufferWriter::WriteMessage(const google::protobuf::Message& pmsg)
88 {
89 if (shareMemoryBlock_ == nullptr) {
90 return false;
91 }
92 DoStats(pmsg.ByteSizeLong());
93 return shareMemoryBlock_->PutMessage(pmsg);
94 }
95
Flush()96 bool BufferWriter::Flush()
97 {
98 ++flushCount_;
99 eventNotifier_->Post(flushCount_.load());
100 lastFlushTime_ = std::chrono::steady_clock::now();
101 bytesPending_ = 0;
102 return true;
103 }
104