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
17 #include "network_profiler_write.h"
18
19 #include <cinttypes>
20 #include <unistd.h>
21
22 #include "logging.h"
23 #include "share_memory_allocator.h"
24
25 namespace OHOS::Developtools::Profiler {
NetworkProfilerWriter(std::string name,uint32_t size,int smbFd,int eventFd,bool block)26 NetworkProfilerWriter::NetworkProfilerWriter(std::string name,
27 uint32_t size,
28 int smbFd,
29 int eventFd,
30 bool block)
31 : pluginName_(name), block_(block)
32 {
33 PROFILER_LOG_INFO(LOG_CORE, "%s:%s %d [%d] [%d]", __func__, name.c_str(), size, smbFd, eventFd);
34 shareMemoryBlock_ = ShareMemoryAllocator::GetInstance().CreateMemoryBlockRemote(name, size, smbFd);
35 if (shareMemoryBlock_ == nullptr) {
36 PROFILER_LOG_DEBUG(LOG_CORE, "%s:create shareMemoryBlock_ failed!", __func__);
37 }
38 eventNotifier_ = EventNotifier::CreateWithFd(eventFd);
39 lastFlushTime_ = std::chrono::steady_clock::now();
40 }
41
~NetworkProfilerWriter()42 NetworkProfilerWriter::~NetworkProfilerWriter()
43 {
44 PROFILER_LOG_DEBUG(LOG_CORE, "%s:destroy eventfd = %d!", __func__, eventNotifier_ ? eventNotifier_->GetFd() : -1);
45 eventNotifier_ = nullptr;
46 ShareMemoryAllocator::GetInstance().ReleaseMemoryBlockRemote(pluginName_);
47 shareMemoryBlock_ = nullptr;
48 }
49
Report() const50 void NetworkProfilerWriter::Report() const
51 {
52 PROFILER_LOG_DEBUG(LOG_CORE, "%s:stats B: %" PRIu64 ", P: %d, W:%" PRIu64 ", F: %d", __func__,
53 bytesCount_.load(), bytesPending_.load(), writeCount_.load(), flushCount_.load());
54 }
55
DoStats(long bytes)56 void NetworkProfilerWriter::DoStats(long bytes)
57 {
58 ++writeCount_;
59 bytesCount_ += bytes;
60 bytesPending_ += bytes;
61 }
62
Write(const void * data,size_t size)63 long NetworkProfilerWriter::Write(const void* data, size_t size)
64 {
65 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
66 return 0;
67 }
68 return shareMemoryBlock_->PutRaw(reinterpret_cast<const int8_t*>(data), size);
69 }
70
WriteTimeout(const void * data,size_t size,const std::function<bool ()> & callback)71 long NetworkProfilerWriter::WriteTimeout(const void* data, size_t size, const std::function<bool()>& callback)
72 {
73 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
74 return 0;
75 }
76 return shareMemoryBlock_->PutWithPayloadSync(reinterpret_cast<const int8_t*>(data), size, nullptr, 0, callback);
77 }
78
WriteWithPayloadTimeout(const void * data,size_t size,const void * payload,size_t payloadSize,const std::function<bool ()> & callback)79 long NetworkProfilerWriter::WriteWithPayloadTimeout(const void* data, size_t size, const void* payload,
80 size_t payloadSize, const std::function<bool()>& callback)
81 {
82 if (shareMemoryBlock_ == nullptr || data == nullptr || size == 0) {
83 return 0;
84 }
85 if (block_) {
86 return shareMemoryBlock_->PutWithPayloadSync(
87 reinterpret_cast<const int8_t*>(data),
88 size,
89 reinterpret_cast<const int8_t*>(payload),
90 payloadSize,
91 callback);
92 } else {
93 return shareMemoryBlock_->PutWithPayloadTimeout(
94 reinterpret_cast<const int8_t*>(data), size, reinterpret_cast<const int8_t*>(payload), payloadSize);
95 }
96 }
97
Flush()98 bool NetworkProfilerWriter::Flush()
99 {
100 ++flushCount_;
101 eventNotifier_->Post(flushCount_.load());
102 lastFlushTime_ = std::chrono::steady_clock::now();
103 bytesPending_ = 0;
104 return true;
105 }
106 }