1 /* 2 * Copyright (c) 2024 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 "netstack_network_profiler.h" 17 18 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 19 #include "network_profiler.h" 20 #include "time_service_client.h" 21 #endif 22 23 namespace OHOS::NetStack { 24 namespace { 25 constexpr const size_t BUFFER_MAX_SIZE = 256 * 1024; 26 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 27 constexpr const uint64_t NS_TO_MICRO = 1000; 28 #endif 29 } 30 NetworkProfilerUtils()31NetworkProfilerUtils::NetworkProfilerUtils() 32 { 33 if (!IsProfilerEnable()) { 34 return; 35 } 36 enable_ = true; 37 requestBeginTime_ = GetBootTime(); 38 } 39 ~NetworkProfilerUtils()40NetworkProfilerUtils::~NetworkProfilerUtils() 41 { 42 if (data_ != nullptr) { 43 free(data_); 44 data_ = nullptr; 45 } 46 } 47 NetworkProfiling(INetworkMessage & networkMessage)48void NetworkProfilerUtils::NetworkProfiling(INetworkMessage &networkMessage) 49 { 50 if (!enable_) { 51 return; 52 } 53 networkMessage.SetRequestBeginTime(requestBeginTime_); 54 msg_ = networkMessage.Parse(); 55 if (data_ == nullptr) { 56 data_ = malloc(BUFFER_MAX_SIZE); 57 if (data_ == nullptr) { 58 return; 59 } 60 } 61 auto ret = TlvUtils::Encode(msg_, data_, dataSize_); 62 if (ret != TLV_OK) { 63 return; 64 } 65 SendNetworkProfiling(); 66 } 67 IsProfilerEnable()68bool NetworkProfilerUtils::IsProfilerEnable() 69 { 70 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 71 auto profiler = OHOS::Developtools::Profiler::NetworkProfiler::GetInstance(); 72 if (profiler->IsProfilerEnable()) { 73 return true; 74 } 75 return false; 76 #else 77 return false; 78 #endif 79 } 80 SendNetworkProfiling()81void NetworkProfilerUtils::SendNetworkProfiling() 82 { 83 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 84 if (data_ == nullptr || dataSize_ <= 0) { 85 return; 86 } 87 auto profiler = OHOS::Developtools::Profiler::NetworkProfiler::GetInstance(); 88 profiler->NetworkProfiling(0, static_cast<char *>(data_), dataSize_); 89 #endif 90 } 91 GetBootTime()92uint64_t NetworkProfilerUtils::GetBootTime() 93 { 94 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 95 auto powerTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeNs(); 96 if (powerTime < 0) { 97 return 0; 98 } 99 return static_cast<uint64_t>(powerTime / NS_TO_MICRO); 100 #else 101 return 0; 102 #endif 103 } 104 } 105