1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. 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 "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
28 using namespace OHOS::Developtools::Profiler;
29
BufferWriter(std::string name,std::string version,uint32_t size,int smbFd,int eventFd,uint32_t pluginId)30 BufferWriter::BufferWriter(std::string name,
31 std::string version,
32 uint32_t size,
33 int smbFd,
34 int eventFd,
35 uint32_t pluginId)
36 : pluginName_(name), pluginVersion_(version)
37 {
38 PROFILER_LOG_INFO(LOG_CORE, "%s:%s %d [%d] [%d]", __func__, name.c_str(), size, smbFd, eventFd);
39 shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
40 if (shareMemoryBlock_ == nullptr) {
41 PROFILER_LOG_DEBUG(LOG_CORE, "%s:create shareMemoryBlock_ failed!", __func__);
42 }
43 eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
44 pluginId_ = pluginId;
45 lastFlushTime_ = std::chrono::steady_clock::now();
46 if (shareMemoryBlock_ != nullptr) {
47 writeCtx_ = reinterpret_cast<RandomWriteCtx*>(shareMemoryBlock_->GetCtx());
48 }
49 }
50
~BufferWriter()51 BufferWriter::~BufferWriter()
52 {
53 PROFILER_LOG_DEBUG(LOG_CORE, "%s:destroy eventfd = %d!", __func__, eventNotifier_ ? eventNotifier_->GetFd() : -1);
54 eventNotifier_ = nullptr;
55 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
56 }
57
Report() const58 void BufferWriter::Report() const
59 {
60 PROFILER_LOG_DEBUG(LOG_CORE, "%s:stats B: %" PRIu64 ", P: %d, W:%" PRIu64 ", F: %d", __func__,
61 bytesCount_.load(), bytesPending_.load(), writeCount_.load(), flushCount_.load());
62 }
63
DoStats(long bytes)64 void BufferWriter::DoStats(long bytes)
65 {
66 ++writeCount_;
67 bytesCount_ += static_cast<uint64_t>(bytes);
68 bytesPending_ += static_cast<uint32_t>(bytes);
69 }
70
Write(const void * data,size_t size)71 long BufferWriter::Write(const void* data, size_t size)
72 {
73 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
74 return false;
75 }
76 ProfilerPluginData pluginData;
77 pluginData.set_name(pluginName_);
78 pluginData.set_version(pluginVersion_);
79 pluginData.set_status(0);
80 pluginData.set_data(data, size);
81
82 struct timespec ts = { 0, 0 };
83 clock_gettime(clockId_, &ts);
84
85 pluginData.set_clock_id(static_cast<ProfilerPluginData_ClockId>(clockId_));
86 pluginData.set_tv_sec(ts.tv_sec);
87 pluginData.set_tv_nsec(ts.tv_nsec);
88
89 DoStats(pluginData.ByteSizeLong());
90 return shareMemoryBlock_->PutMessage(pluginData, pluginName_);
91 }
92
StartReport()93 RandomWriteCtx* BufferWriter::StartReport()
94 {
95 if (shareMemoryBlock_ == nullptr || writeCtx_ == nullptr) {
96 return nullptr;
97 }
98
99 profilerPluginData_.reset();
100 shareMemoryBlock_->ResetPos();
101 profilerPluginData_ = std::make_shared<ProtoEncoder::ProfilerPluginData>(writeCtx_);
102 profilerPluginData_->set_name(pluginName_);
103 profilerPluginData_->set_version(pluginVersion_);
104 profilerPluginData_->set_status(0);
105 return profilerPluginData_->startAdd_data();
106 }
107
FinishReport(int32_t size)108 void BufferWriter::FinishReport(int32_t size)
109 {
110 if (shareMemoryBlock_ == nullptr) {
111 return;
112 }
113
114 profilerPluginData_->finishAdd_data(size);
115 struct timespec ts;
116 clock_gettime(CLOCK_REALTIME, &ts);
117 profilerPluginData_->set_clock_id(static_cast<ProfilerPluginData_ClockId>(clockId_));
118 profilerPluginData_->set_tv_sec(ts.tv_sec);
119 profilerPluginData_->set_tv_nsec(ts.tv_nsec);
120 int32_t len = profilerPluginData_->Finish();
121 shareMemoryBlock_->UseMemory(len);
122 DoStats(len);
123 }
124
WriteMessage(const google::protobuf::Message & pmsg,const std::string & pluginName)125 bool BufferWriter::WriteMessage(const google::protobuf::Message& pmsg, const std::string& pluginName)
126 {
127 if (shareMemoryBlock_ == nullptr) {
128 return false;
129 }
130 DoStats(pmsg.ByteSizeLong());
131 return shareMemoryBlock_->PutMessage(pmsg, pluginName);
132 }
133
Flush()134 bool BufferWriter::Flush()
135 {
136 ++flushCount_;
137 if (eventNotifier_ == nullptr) {
138 return false;
139 }
140 eventNotifier_->Post(flushCount_.load());
141 lastFlushTime_ = std::chrono::steady_clock::now();
142 bytesPending_ = 0;
143 return true;
144 }
145
UseMemory(int32_t size)146 void BufferWriter::UseMemory(int32_t size)
147 {
148 if (shareMemoryBlock_ == nullptr) {
149 return;
150 }
151
152 shareMemoryBlock_->UseMemory(size);
153 DoStats(size);
154 }
155
ResetPos()156 void BufferWriter::ResetPos()
157 {
158 if (shareMemoryBlock_ == nullptr) {
159 return;
160 }
161
162 shareMemoryBlock_->ResetPos();
163 }