• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::visit([&](auto& protoData) {
36         FlushData(protoData);
37         }, protoData_);
38 }
39 
SetWriter(const std::shared_ptr<Writer> & writer)40 void NetworkProfilerHandle::SetWriter(const std::shared_ptr<Writer>& writer)
41 {
42     writer_ = writer;
43     protoData_ = ::NetworkProfilerResult();
44 }
45 
SetWriter(const WriterStructPtr & writer)46 void NetworkProfilerHandle::SetWriter(const WriterStructPtr& writer)
47 {
48     writerStruct_ = writer;
49     auto ctx = writerStruct_->startReport(writerStruct_);
50     if (ctx == nullptr) {
51         PROFILER_LOG_ERROR(LOG_CORE, "%s: get RandomWriteCtx FAILED!", __func__);
52         return;
53     }
54     protoData_ = ProtoEncoder::NetworkProfilerResult(ctx);
55 }
56 
SerializeData(const int8_t data[],uint32_t size)57 void NetworkProfilerHandle::SerializeData(const int8_t data[], uint32_t size)
58 {
59     std::visit([&](auto& protoData) {
60         if (size < sizeof(NetworkEvent)) {
61             PROFILER_LOG_ERROR(LOG_CORE, "%s the size=%d is too small", __FUNCTION__, size);
62             return;
63         }
64         SerializeDataImpl(protoData, data, size);
65         FlushCheck(protoData);
66         }, protoData_);
67 }
68 
69 template <typename T>
SerializeDataImpl(T & protoData,const int8_t data[],uint32_t size)70 void NetworkProfilerHandle::SerializeDataImpl(T& protoData, const int8_t data[], uint32_t size)
71 {
72     if (size < sizeof(NetworkEvent)) {
73         PROFILER_LOG_ERROR(LOG_CORE, "%s the size=%d is too small", __FUNCTION__, size);
74         return;
75     }
76     auto networkEvent = protoData.add_network_event();
77     NetworkEvent* event = reinterpret_cast<NetworkEvent*>(const_cast<int8_t*>(data));
78     if (event->type == static_cast<int32_t>(NetworkEventType::INVALID)) {
79         PROFILER_LOG_ERROR(LOG_CORE, "%s type is invalid", __FUNCTION__);
80         return;
81     }
82     networkEvent->set_tv_sec(event->ts.tv_sec);
83     networkEvent->set_tv_nsec(event->ts.tv_nsec);
84     networkEvent->set_pid(pid_);
85     networkEvent->set_tid(event->tid);
86     networkEvent->set_type(event->type);
87 
88     std::call_once(g_onceProcessName, [&]() {
89         networkEvent->set_process_name(processName_.c_str(), processName_.size());
90     });
91 
92     size_t threadNameSize = strlen(event->threadName);
93     if (threadNameSize > 0) {
94         networkEvent->set_thread_name(event->threadName, threadNameSize);
95     }
96     size_t baseDataSize = sizeof(NetworkEvent);
97     if (size > baseDataSize) {
98         networkEvent->set_payload(data + baseDataSize, size - baseDataSize);
99     }
100 }
101 
102 template <typename T>
FlushCheck(T & protoData)103 void NetworkProfilerHandle::FlushCheck(T& protoData)
104 {
105     if ((++flushCount_ & FLUSH_INTERVAL) != 0) {
106         return;
107     }
108     FlushData(protoData);
109 }
110 
FlushData(::NetworkProfilerResult & data)111 void NetworkProfilerHandle::FlushData(::NetworkProfilerResult& data)
112 {
113     size_t length = data.ByteSizeLong();
114     if (length < bufferSize_) {
115         data.SerializeToArray(buffer_.get(), length);
116         if (buffer_.get() == nullptr) {
117             PROFILER_LOG_ERROR(LOG_CORE, "Flush src is nullptr");
118             return;
119         }
120 
121         if (writer_ == nullptr) {
122             PROFILER_LOG_ERROR(LOG_CORE, "Flush writer_ is nullptr");
123             return;
124         }
125         writer_->Write(buffer_.get(), length);
126         writer_->Flush();
127         std::get<::NetworkProfilerResult>(protoData_).clear_network_event();
128     }
129 }
130 
FlushData(ProtoEncoder::NetworkProfilerResult & data)131 void NetworkProfilerHandle::FlushData(ProtoEncoder::NetworkProfilerResult& data)
132 {
133     if (data.Size() == 0) {
134         return;
135     }
136 
137     int messageLen = data.Finish();
138 
139     RandomWriteCtx* ctx = nullptr;
140     writerStruct_->finishReport(writerStruct_, messageLen);
141     writerStruct_->flush(writerStruct_);
142     ctx = writerStruct_->startReport(writerStruct_);
143     if (ctx == nullptr) {
144         PROFILER_LOG_ERROR(LOG_CORE, "%s: get RandomWriteCtx FAILED!", __func__);
145         return;
146     }
147     protoData_ = ProtoEncoder::NetworkProfilerResult(ctx);
148 }
149 }