1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 "network_profiler_handle.h"
17 #include "network_profiler_common.h"
18
19 namespace {
20 constexpr uint8_t FLUSH_INTERVAL = 10;
21 std::once_flag g_onceProcessName;
22 }
23
24 namespace OHOS::Developtools::Profiler {
NetworkProfilerHandle(clockid_t pluginDataClockId,uint32_t bufferSize,bool isProtobufSerialize)25 NetworkProfilerHandle::NetworkProfilerHandle(clockid_t pluginDataClockId, uint32_t bufferSize, bool isProtobufSerialize)
26 : isProtobufSerialize_(isProtobufSerialize), pluginDataClockId_(pluginDataClockId), bufferSize_(bufferSize)
27 {
28 if (isProtobufSerialize_) {
29 buffer_ = std::make_unique<uint8_t[]>(bufferSize);
30 }
31 }
32
~NetworkProfilerHandle()33 NetworkProfilerHandle::~NetworkProfilerHandle()
34 {
35 }
36
StopHandle()37 void NetworkProfilerHandle::StopHandle()
38 {
39 std::visit([&](auto& protoData) {
40 FlushData(protoData);
41 }, protoData_);
42 }
43
SetWriter(const std::shared_ptr<Writer> & writer)44 void NetworkProfilerHandle::SetWriter(const std::shared_ptr<Writer>& writer)
45 {
46 writer_ = writer;
47 protoData_ = ::NetworkProfilerResult();
48 }
49
SetWriter(const WriterStructPtr & writer)50 void NetworkProfilerHandle::SetWriter(const WriterStructPtr& writer)
51 {
52 writerStruct_ = writer;
53 auto ctx = writerStruct_->startReport(writerStruct_);
54 if (ctx == nullptr) {
55 PROFILER_LOG_ERROR(LOG_CORE, "%s: get RandomWriteCtx FAILED!", __func__);
56 return;
57 }
58 protoData_ = ProtoEncoder::NetworkProfilerResult(ctx);
59 }
60
SerializeData(const int8_t data[],uint32_t size)61 void NetworkProfilerHandle::SerializeData(const int8_t data[], uint32_t size)
62 {
63 std::visit([&](auto& protoData) {
64 if (size < sizeof(NetworkEvent)) {
65 PROFILER_LOG_ERROR(LOG_CORE, "%s the size=%d is too small", __FUNCTION__, size);
66 return;
67 }
68 SerializeDataImpl(protoData, data, size);
69 FlushCheck(protoData);
70 }, protoData_);
71 }
72
73 template <typename T>
SerializeDataImpl(T & protoData,const int8_t data[],uint32_t size)74 void NetworkProfilerHandle::SerializeDataImpl(T& protoData, const int8_t data[], uint32_t size)
75 {
76 if (size < sizeof(NetworkEvent)) {
77 PROFILER_LOG_ERROR(LOG_CORE, "%s the size=%d is too small", __FUNCTION__, size);
78 return;
79 }
80 auto networkEvent = protoData.add_network_event();
81 NetworkEvent* event = reinterpret_cast<NetworkEvent*>(const_cast<int8_t*>(data));
82 if (event->type == static_cast<int32_t>(NetworkEventType::INVALID)) {
83 PROFILER_LOG_ERROR(LOG_CORE, "%s type is invalid", __FUNCTION__);
84 return;
85 }
86 networkEvent->set_tv_sec(event->ts.tv_sec);
87 networkEvent->set_tv_nsec(event->ts.tv_nsec);
88 networkEvent->set_pid(pid_);
89 networkEvent->set_tid(event->tid);
90 networkEvent->set_type(event->type);
91
92 std::call_once(g_onceProcessName, [&]() {
93 networkEvent->set_process_name(processName_.c_str(), processName_.size());
94 });
95
96 size_t threadNameSize = strlen(event->threadName);
97 if (threadNameSize > 0) {
98 networkEvent->set_thread_name(event->threadName, threadNameSize);
99 }
100 size_t baseDataSize = sizeof(NetworkEvent);
101 if (size > baseDataSize) {
102 networkEvent->set_payload(data + baseDataSize, size - baseDataSize);
103 }
104 }
105
106 template <typename T>
FlushCheck(T & protoData)107 void NetworkProfilerHandle::FlushCheck(T& protoData)
108 {
109 if ((++flushCount_ & FLUSH_INTERVAL) != 0) {
110 return;
111 }
112 FlushData(protoData);
113 }
114
FlushData(::NetworkProfilerResult & data)115 void NetworkProfilerHandle::FlushData(::NetworkProfilerResult& data)
116 {
117 size_t length = data.ByteSizeLong();
118 if (length < bufferSize_) {
119 data.SerializeToArray(buffer_.get(), length);
120 if (buffer_.get() == nullptr) {
121 PROFILER_LOG_ERROR(LOG_CORE, "Flush src is nullptr");
122 return;
123 }
124
125 if (writer_ == nullptr) {
126 PROFILER_LOG_ERROR(LOG_CORE, "Flush writer_ is nullptr");
127 return;
128 }
129 writer_->Write(buffer_.get(), length);
130 writer_->Flush();
131 std::get<::NetworkProfilerResult>(protoData_).clear_network_event();
132 }
133 }
134
FlushData(ProtoEncoder::NetworkProfilerResult & data)135 void NetworkProfilerHandle::FlushData(ProtoEncoder::NetworkProfilerResult& data)
136 {
137 if (data.Size() == 0) {
138 return;
139 }
140
141 int messageLen = data.Finish();
142
143 RandomWriteCtx* ctx = nullptr;
144 writerStruct_->finishReport(writerStruct_, messageLen);
145 writerStruct_->flush(writerStruct_);
146 ctx = writerStruct_->startReport(writerStruct_);
147 if (ctx == nullptr) {
148 PROFILER_LOG_ERROR(LOG_CORE, "%s: get RandomWriteCtx FAILED!", __func__);
149 return;
150 }
151 protoData_ = ProtoEncoder::NetworkProfilerResult(ctx);
152 }
153 }