From 9b454e05ffb1958843c453f950cb8413f02c6167 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 01:06:33 +0800 Subject: [PATCH 01/15] network flow data Signed-off-by: weimingjin --- bundle.json | 3 +- device/etc/hiprofiler_plugins.cfg | 4 +- device/plugins/network_plugin/BUILD.gn | 8 + .../network_plugin/include/network_plugin.h | 78 +-- .../network_plugin/src/network_plugin.cpp | 449 +++++++----------- device/plugins/network_plugin/test/BUILD.gn | 1 + .../networkstartplugin_fuzzer/BUILD.gn | 1 + .../test/unittest/network_plugin_test.cpp | 222 +-------- .../network_data/network_plugin_config.proto | 3 +- .../network_data/network_plugin_result.proto | 43 +- 10 files changed, 203 insertions(+), 609 deletions(-) diff --git a/bundle.json b/bundle.json index 96f66dbf4..f55c29b73 100644 --- a/bundle.json +++ b/bundle.json @@ -58,7 +58,8 @@ "zlib", "grpc", "storage_service", - "abseil-cpp" + "abseil-cpp", + "netmanager_base" ], "third_party": [ "cJSON", diff --git a/device/etc/hiprofiler_plugins.cfg b/device/etc/hiprofiler_plugins.cfg index cda5ac0c7..17be9aa92 100644 --- a/device/etc/hiprofiler_plugins.cfg +++ b/device/etc/hiprofiler_plugins.cfg @@ -24,7 +24,9 @@ "ohos.permission.WRITE_HIVIEW_SYSTEM", "ohos.permission.READ_HIVIEW_SYSTEM", "ohos.permission.GET_BUNDLE_INFO", - "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED" + "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", + "ohos.permission.GET_NETWORK_STATS", + "ohos.permission.CONNECTIVITY_INTERNAL" ] } ] diff --git a/device/plugins/network_plugin/BUILD.gn b/device/plugins/network_plugin/BUILD.gn index 8bfd64a67..f84f6a622 100644 --- a/device/plugins/network_plugin/BUILD.gn +++ b/device/plugins/network_plugin/BUILD.gn @@ -41,6 +41,14 @@ ohos_source_set("networkplugin_source") { "hilog:libhilog_base", "hisysevent:libhisysevent", "protobuf:protobuf_lite", + "netmanager_base:net_stats_manager_if", + "c_utils:utils", + "init:libbegetutil", + "ipc:ipc_core", + "os_account:os_account_innerkits", + "samgr:samgr_proxy", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", ] } public_configs = [ "${OHOS_PROFILER_DIR}/device/base:hiprofiler_test_config" ] diff --git a/device/plugins/network_plugin/include/network_plugin.h b/device/plugins/network_plugin/include/network_plugin.h index 746629bae..7b1f57de8 100644 --- a/device/plugins/network_plugin/include/network_plugin.h +++ b/device/plugins/network_plugin/include/network_plugin.h @@ -16,42 +16,13 @@ #ifndef NETWORK_PLUGIN_H #define NETWORK_PLUGIN_H -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "logging.h" #include "network_plugin_config.pb.h" #include "network_plugin_result.pb.h" #include "plugin_module_api.h" +#include "net_stats_client.h" -struct NetDetails { - uint64_t tx; - uint64_t rx; - std::string type; -}; - -struct NetworkCell { - int32_t pid; - uint64_t tx; - uint64_t rx; - struct timespec ts; - std::vector details; -}; - -struct NetSystemDetails { +struct NetFlowDetail { std::string type; uint64_t rxBytes; uint64_t rxPackets; @@ -59,58 +30,35 @@ struct NetSystemDetails { uint64_t txPackets; }; -struct NetSystemData { +struct NetFlowData { struct timespec ts; uint64_t rxBytes; uint64_t rxPackets; uint64_t txBytes; uint64_t txPackets; - std::vector details; -}; - -enum NetworkNum { - IFACE_INDEX = 2, - UID_INDEX = 4, - RX_BYTES_INDEX = 6, - RX_PACKETS_INDEX = 7, - TX_BYTES_INDEX = 8, - TX_PACKETS_INDEX = 9, - DEC_BASE = 10, + std::vector details; }; class NetworkPlugin { public: - NetworkPlugin(); + NetworkPlugin() {}; ~NetworkPlugin() {}; int Start(const uint8_t* configData, uint32_t configSize); int Report(uint8_t* data, uint32_t dataSize); int ReportOptimize(RandomWriteCtx* randomWrite); int Stop(); -protected: - std::string GetRateNodePath(); - int32_t GetUid(int32_t pid); - bool ReadTxRxBytes(int32_t pid, NetworkCell &cell); - bool ReadSystemTxRxBytes(NetSystemData &systemData); - void AddNetDetails(NetworkCell& cell, NetDetails& data); - void AddNetSystemDetails(NetSystemData& systemData, NetSystemDetails& data); - std::string GetCmdArgs(NetworkConfig traceConfig); - // for UT - void setPathForTest(std::string path) - { - fileForTest_ = path; - } - std::string getPathForTest() - { - return fileForTest_; - } template bool WriteNetWorkData(T& networkDatasProto); + int32_t GetUid(int32_t pid); + bool ScreenNetworkStatByUid(std::vector infos, NetFlowData &data); + bool RetainAllNetworkStat(std::vector infos, NetFlowData &data); + bool HandleData(NetFlowData present, NetFlowData &difference); private: NetworkConfig protoConfig_; - std::unique_ptr buffer_; - std::unique_ptr fp_; - std::unordered_map pidUid_; - std::string fileForTest_; + int32_t pid_=0; + int32_t uid_=-1; + NetFlowData previous_={{0, 0}, 0, 0, 0, 0, std::vector()}; + bool isFirst=true; }; #endif \ No newline at end of file diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index 7178225ee..e9ab5c5b0 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -14,138 +14,40 @@ */ #include "network_plugin.h" -#include -#include - -#include "buffer_splitter.h" -#include "common.h" #include "network_plugin_result.pbencoder.h" #include "securec.h" +#include "application_info.h" +#include "bundle_mgr_proxy.h" +#include "file_ex.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "os_account_info.h" +#include "os_account_manager.h" -namespace { using namespace OHOS::Developtools::Profiler; -constexpr size_t READ_BUFFER_SIZE = 1024 * 16; -const std::string DEFAULT_NET_PATH("/proc/net/xt_qtaguid/stats"); -} // namespace - -NetworkPlugin::NetworkPlugin() : fp_(nullptr, nullptr) -{ - pidUid_.clear(); - buffer_ = std::make_unique(READ_BUFFER_SIZE); -} +using namespace OHOS; +using namespace OHOS::AppExecFwk; int NetworkPlugin::Start(const uint8_t* configData, uint32_t configSize) { - CHECK_NOTNULL(buffer_, -1, "%s:NetworkPlugin, buffer_ is null", __func__); - if (protoConfig_.ParseFromArray(configData, configSize) <= 0) { PROFILER_LOG_ERROR(LOG_CORE, "%s:NetworkPlugin, parseFromArray failed!", __func__); return -1; } - - for (int i = 0; i < protoConfig_.pid().size(); i++) { - int32_t pid = protoConfig_.pid(i); - pidUid_.emplace(pid, GetUid(pid)); + pid_=protoConfig_.pid(); + CHECK_TRUE(pid_>=0, -1, "%s:invalid pid", __func__); + if(pid_>0){ + uid_= GetUid(pid_); + CHECK_TRUE(uid_!=-1, -1, "%s:get uid failed", __func__); } - int ret = COMMON::PluginWriteToHisysevent("network_plugin", "sh", GetCmdArgs(protoConfig_), - COMMON::ErrorType::RET_SUCC, "success"); - PROFILER_LOG_INFO(LOG_CORE, "%s: NetworkPlugin success! hisysevent report result:%d", __func__, ret); + PROFILER_LOG_INFO(LOG_CORE, "%s: NetworkPlugin success!", __func__); return 0; } -std::string NetworkPlugin::GetCmdArgs(NetworkConfig traceConfig) -{ - std::stringstream args; - for (const auto& p : traceConfig.pid()) { - args << "pid: " << COMMON::GetProcessNameByPid(p) << ", "; - } - args << "test_file: " << traceConfig.test_file(); - return args.str(); -} - -template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) -{ - std::string file = GetRateNodePath(); - if (protoConfig_.test_file() != "") { - file = protoConfig_.test_file(); - } - - struct stat s; - lstat(file.c_str(), &s); - CHECK_TRUE(!S_ISDIR(s.st_mode), false, "%s:path(%s) is directory, no data to report", __func__, file.c_str()); - - char realPath[PATH_MAX + 1] = {0}; - CHECK_TRUE((file.length() < PATH_MAX) && (realpath(file.c_str(), realPath) != nullptr), false, - "%s:path is invalid: %s, errno=%d", __func__, file.c_str(), errno); - fp_ = std::unique_ptr(fopen(realPath, "r"), fclose); - CHECK_NOTNULL(fp_, false, "%s:NetworkPlugin, open(%s) Failed, errno(%d)", __func__, file.c_str(), errno); - - if (protoConfig_.pid().size() > 0) { - for (int i = 0; i < protoConfig_.pid().size(); i++) { - auto* info = networkDatasProto.add_networkinfo(); - int32_t pid = protoConfig_.pid(i); - NetworkCell dataCell = {0}; - ReadTxRxBytes(pid, dataCell); - // set proto - for (auto& it : dataCell.details) { - auto* data = info->add_details(); - data->set_tx_bytes(it.tx); - data->set_rx_bytes(it.rx); - data->set_type(it.type); - } - info->set_pid(pid); - info->set_tx_bytes(dataCell.tx); - info->set_rx_bytes(dataCell.rx); - info->set_tv_sec(dataCell.ts.tv_sec); - info->set_tv_nsec(dataCell.ts.tv_nsec); - } - } else if (protoConfig_.test_file() != "") { // test data - NetSystemData systemData = {}; - ReadSystemTxRxBytes(systemData); - static int randNum = 0; - randNum++; - auto* systemInfo = networkDatasProto.mutable_network_system_info(); - for (auto& it : systemData.details) { - auto* data = systemInfo->add_details(); - data->set_rx_bytes(it.rxBytes + randNum * RX_BYTES_INDEX); - data->set_rx_packets(it.rxPackets + randNum * RX_PACKETS_INDEX); - data->set_tx_bytes(it.txBytes + randNum * TX_BYTES_INDEX); - data->set_tx_packets(it.txPackets + randNum * TX_PACKETS_INDEX); - data->set_type(it.type); - } - systemInfo->set_tv_sec(systemData.ts.tv_sec); - systemInfo->set_tv_nsec(systemData.ts.tv_nsec); - systemInfo->set_rx_bytes(systemData.rxBytes + (randNum * RX_BYTES_INDEX * systemData.details.size())); - systemInfo->set_rx_packets(systemData.rxPackets + (randNum * RX_PACKETS_INDEX * systemData.details.size())); - systemInfo->set_tx_bytes(systemData.txBytes + (randNum * TX_BYTES_INDEX * systemData.details.size())); - systemInfo->set_tx_packets(systemData.txPackets + (randNum * TX_PACKETS_INDEX * systemData.details.size())); - } else { // real data - NetSystemData systemData = {}; - ReadSystemTxRxBytes(systemData); - auto* systemInfo = networkDatasProto.mutable_network_system_info(); - for (auto& it : systemData.details) { - auto* data = systemInfo->add_details(); - data->set_rx_bytes(it.rxBytes); - data->set_rx_packets(it.rxPackets); - data->set_tx_bytes(it.txBytes); - data->set_tx_packets(it.txPackets); - data->set_type(it.type); - } - systemInfo->set_tv_sec(systemData.ts.tv_sec); - systemInfo->set_tv_nsec(systemData.ts.tv_nsec); - systemInfo->set_rx_bytes(systemData.rxBytes); - systemInfo->set_rx_packets(systemData.rxPackets); - systemInfo->set_tx_bytes(systemData.txBytes); - systemInfo->set_tx_packets(systemData.txPackets); - } - - return true; -} - int NetworkPlugin::ReportOptimize(RandomWriteCtx* randomWrite) { - ProtoEncoder::NetworkDatas dataProto(randomWrite); + ProtoEncoder::NetworkFlowData dataProto(randomWrite); CHECK_TRUE(WriteNetWorkData(dataProto), -1, "%s:write network data failed", __func__); int msgSize = dataProto.Finish(); @@ -154,7 +56,7 @@ int NetworkPlugin::ReportOptimize(RandomWriteCtx* randomWrite) int NetworkPlugin::Report(uint8_t* data, uint32_t dataSize) { - NetworkDatas dataProto; + NetworkFlowData dataProto; CHECK_TRUE(WriteNetWorkData(dataProto), -1, "%s:write network data failed", __func__); uint32_t length = dataProto.ByteSizeLong(); @@ -169,204 +71,177 @@ int NetworkPlugin::Report(uint8_t* data, uint32_t dataSize) int NetworkPlugin::Stop() { - buffer_ = nullptr; - fp_ = nullptr; - pidUid_.clear(); - PROFILER_LOG_INFO(LOG_CORE, "%s:NetworkPlugin, stop success!", __func__); return 0; } -std::string NetworkPlugin::GetRateNodePath() +template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) { - std::string name = ""; - - if (!fileForTest_.empty()) { - name = fileForTest_ + DEFAULT_NET_PATH; - return name; + std::vector infos; + int res=OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos); + if(res!=0){ + PROFILER_LOG_ERROR(LOG_CORE, "GetAllStatsInfo failed ret:%d", res); } - if (access(DEFAULT_NET_PATH.c_str(), F_OK) == 0) { - name = DEFAULT_NET_PATH; + NetFlowData netFlowData={{0, 0}, 0, 0, 0, 0, std::vector()}; + if(pid_>0){ + ScreenNetworkStatByUid(infos, netFlowData); + } else{ + RetainAllNetworkStat(infos, netFlowData); } - return name; -} - -int32_t NetworkPlugin::GetUid(int32_t pid) -{ - CHECK_TRUE(pid > 0, -1, "%s:NetworkPlugin, check param fail, pid less than 0!", __func__); - - char* end = nullptr; - std::string path = std::string("/proc/") + std::to_string(pid) + std::string("/status"); - if (!fileForTest_.empty()) { - path = fileForTest_ + std::string("/proc/") + std::to_string(pid) + std::string("/status"); + networkDatasProto.set_pid(pid_); + networkDatasProto.set_tv_sec(netFlowData.ts.tv_sec); + networkDatasProto.set_tv_nsec(netFlowData.ts.tv_nsec); + networkDatasProto.set_rx_bytes(netFlowData.rxBytes); + networkDatasProto.set_rx_packets(netFlowData.rxPackets); + networkDatasProto.set_tx_bytes(netFlowData.txBytes); + networkDatasProto.set_tx_packets(netFlowData.txPackets); + for(auto& it:netFlowData.details){ + auto* data=networkDatasProto.add_details(); + data->set_type(it.type); + data->set_rx_bytes(it.rxBytes); + data->set_rx_packets(it.rxPackets); + data->set_tx_bytes(it.txBytes); + data->set_tx_packets(it.txPackets); } - std::ifstream input(path, std::ios::in); - if (input.fail()) { - const int bufSize = 256; - char buf[bufSize] = { 0 }; - strerror_r(errno, buf, bufSize); - PROFILER_LOG_ERROR(LOG_CORE, "%s:NetworkPlugin, open %s failed, errno(%s)", __func__, path.c_str(), buf); - return -1; - } - do { - if (!input.good()) { - return -1; - } - std::string line; - getline(input, line); - if (!strncmp(line.c_str(), "Uid:", strlen("Uid:"))) { - std::string str = line.substr(strlen("Uid:\t")); - PROFILER_LOG_INFO(LOG_CORE, "%s:NetworkPlugin, line(%s), str(%s)", __func__, line.c_str(), str.c_str()); - return strtol(str.c_str(), &end, DEC_BASE); - } - } while (!input.eof()); - input.close(); - - return -1; + return true; } -bool NetworkPlugin::ReadTxRxBytes(int32_t pid, NetworkCell &cell) +bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) { - int32_t uid = pidUid_.at(pid); - CHECK_NOTNULL(fp_.get(), false, "%s:NetworkPlugin, fp_ is null", __func__); - int ret = fseek(fp_.get(), 0, SEEK_SET); - CHECK_TRUE(ret == 0, false, "%s:NetworkPlugin, fseek failed, error(%d)!", __func__, errno); - size_t rsize = static_cast(fread(buffer_.get(), sizeof(char), READ_BUFFER_SIZE - 1, fp_.get())); - buffer_.get()[rsize] = '\0'; - CHECK_TRUE(rsize >= 0, false, "%s:NetworkPlugin, read failed, errno(%d)", __func__, errno); - char* end = nullptr; - BufferSplitter totalbuffer((const char*)buffer_.get(), rsize + 1); - do { - int index = 0; - NetDetails cache = {0}; - char tmp[TX_BYTES_INDEX + 1] = {0}; - while (totalbuffer.NextWord(' ')) { - index++; - if (totalbuffer.CurWord() == nullptr) { - continue; - } - if (index == IFACE_INDEX && !strncmp(totalbuffer.CurWord(), "lo", strlen("lo"))) { - break; - } - if (index == IFACE_INDEX && - strncpy_s(tmp, sizeof(tmp), totalbuffer.CurWord(), totalbuffer.CurWordSize()) == EOK) { - cache.type = tmp; - } - uint64_t value = static_cast(strtoull(totalbuffer.CurWord(), &end, DEC_BASE)); - CHECK_TRUE(value >= 0, false, "%s:NetworkPlugin, strtoull value failed", __func__); - if ((index == UID_INDEX) && (uid != static_cast(value))) { - break; - } - if (index == RX_BYTES_INDEX) { - uint64_t rxBytes = value; - cache.rx = rxBytes; - cell.rx += rxBytes; - } - if (index == TX_BYTES_INDEX) { - uint64_t txBytes = value; - cache.tx = txBytes; - cell.tx += txBytes; - AddNetDetails(cell, cache); - } + NetFlowData present={{0, 0}, 0, 0, 0, 0, std::vector()}; + std::map detailMap; + for(auto info:infos){ + present.rxBytes+=info.rxBytes_; + present.rxPackets+=info.rxPackets_; + present.txBytes+=info.txBytes_; + present.txPackets+=info.txPackets_; + std::string type=info.iface_; + if(detailMap.find(type)!=detailMap.end()){ + detailMap[type].rxBytes+=info.rxBytes_; + detailMap[type].rxPackets+=info.rxPackets_; + detailMap[type].txBytes+=info.txBytes_; + detailMap[type].txPackets+=info.txPackets_; + } else{ + NetFlowDetail detail; + detail.type=info.iface_; + detail.rxBytes=info.rxBytes_; + detail.rxPackets=info.rxPackets_; + detail.txBytes=info.txBytes_; + detail.txPackets=info.txPackets_; + detailMap[type]=detail; } - } while (totalbuffer.NextLine()); - - clock_gettime(CLOCK_REALTIME, &cell.ts); - + } + for(auto it:detailMap){ + present.details.push_back(it.second); + } + HandleData(present, data); return true; } -void NetworkPlugin::AddNetDetails(NetworkCell& cell, NetDetails& data) +bool NetworkPlugin::ScreenNetworkStatByUid(std::vector infos, NetFlowData &data) { - bool finded = false; - - // 处理重复数据 - for (auto it = cell.details.begin(); it != cell.details.end(); it++) { - if (it->type == data.type) { - it->tx += data.tx; - it->rx += data.rx; - finded = true; + NetFlowData present={{0, 0}, 0, 0, 0, 0, std::vector()}; + for(auto info:infos){ + if(info.uid_==uid_){ + NetFlowDetail detail; + detail.type=info.iface_; + detail.rxBytes=info.rxBytes_; + detail.rxPackets=info.rxPackets_; + detail.txBytes=info.txBytes_; + detail.txPackets=info.txPackets_; + present.details.push_back(detail); + present.rxBytes+=info.rxBytes_; + present.rxPackets+=info.rxPackets_; + present.txBytes+=info.txBytes_; + present.txPackets+=info.txPackets_; } } - - if (!finded) { - cell.details.push_back(data); - } + HandleData(present, data); + return true; } -bool NetworkPlugin::ReadSystemTxRxBytes(NetSystemData &systemData) +bool NetworkPlugin::HandleData(NetFlowData present, NetFlowData &difference) { - CHECK_NOTNULL(fp_.get(), false, "%s:NetworkPlugin, fp_ is null", __func__); - int ret = fseek(fp_.get(), 0, SEEK_SET); - CHECK_TRUE(ret == 0, false, "%s:NetworkPlugin, fseek failed, error(%d)!", __func__, errno); - size_t rsize = static_cast(fread(buffer_.get(), sizeof(char), READ_BUFFER_SIZE - 1, fp_.get())); - buffer_.get()[rsize] = '\0'; - CHECK_TRUE(rsize >= 0, false, "%s:NetworkPlugin, read failed, errno(%d)", __func__, errno); - char* end = nullptr; - BufferSplitter totalbuffer((const char*)buffer_.get(), rsize + 1); - do { - int index = 0; - NetSystemDetails systemCache = {}; - char tmp[TX_BYTES_INDEX + 1] = ""; - while (totalbuffer.NextWord(' ')) { - index++; - if (totalbuffer.CurWord() == nullptr) { - continue; - } - if (index == IFACE_INDEX && !strncmp(totalbuffer.CurWord(), "lo", strlen("lo"))) { - break; - } - if (index == IFACE_INDEX && - strncpy_s(tmp, sizeof(tmp), totalbuffer.CurWord(), totalbuffer.CurWordSize()) == EOK) { - systemCache.type = tmp; - } - if (strcmp(systemCache.type.c_str(), "iface") == 0) { - break; + if(!isFirst){ + (void)clock_gettime(CLOCK_REALTIME, &(difference.ts)); + difference.rxBytes=present.rxBytes-previous_.rxBytes; + difference.rxPackets=present.rxPackets-previous_.rxPackets; + difference.txBytes=present.txBytes-previous_.txBytes; + difference.txPackets=present.txPackets-previous_.txPackets; + for(auto presentDetail:present.details){ + NetFlowDetail detail; + bool havePrevious= false; + for(auto previousDetail:previous_.details){ + if(previousDetail.type==presentDetail.type){ + detail.type=presentDetail.type; + detail.rxBytes=presentDetail.rxBytes-previousDetail.rxBytes; + detail.rxPackets=presentDetail.rxPackets-previousDetail.rxPackets; + detail.txBytes=presentDetail.txBytes-previousDetail.txBytes; + detail.txPackets=presentDetail.txPackets-previousDetail.txPackets; + havePrevious= true; + break; + } } - uint64_t value = static_cast(strtoull(totalbuffer.CurWord(), &end, DEC_BASE)); - CHECK_TRUE(value >= 0, false, "%s:NetworkPlugin, strtoull value failed", __func__); - if (index == RX_BYTES_INDEX) { - uint64_t rxBytes = value; - systemCache.rxBytes = rxBytes; - systemData.rxBytes += rxBytes; - } else if (index == RX_PACKETS_INDEX) { - uint64_t rxPackets = value; - systemCache.rxPackets = rxPackets; - systemData.rxPackets += rxPackets; - } else if (index == TX_BYTES_INDEX) { - uint64_t txBytes = value; - systemCache.txBytes = txBytes; - systemData.txBytes += txBytes; - } else if (index == TX_PACKETS_INDEX) { - uint64_t txPackets = value; - systemCache.txPackets = txPackets; - systemData.txPackets += txPackets; - AddNetSystemDetails(systemData, systemCache); + if(!havePrevious){ + detail.type=presentDetail.type; + detail.rxBytes=presentDetail.rxBytes; + detail.rxPackets=presentDetail.rxPackets; + detail.txBytes=presentDetail.txBytes; + detail.txPackets=presentDetail.txPackets; } + difference.details.push_back(detail); } - } while (totalbuffer.NextLine()); - - clock_gettime(CLOCK_REALTIME, &systemData.ts); - + } else{ + (void)clock_gettime(CLOCK_REALTIME, &(difference.ts)); + difference.rxBytes=0; + difference.rxPackets=0; + difference.txBytes=0; + difference.txPackets=0; + isFirst= false; + } + previous_.rxBytes=present.rxBytes; + previous_.rxPackets=present.rxPackets; + previous_.txBytes=present.txBytes; + previous_.txPackets=present.txPackets; + previous_.details.clear(); + for(auto detail:present.details){ + previous_.details.push_back(detail); + } return true; } - -void NetworkPlugin::AddNetSystemDetails(NetSystemData& systemData, NetSystemDetails& data) +int32_t NetworkPlugin::GetUid(int32_t pid) { - bool finded = false; - - // 处理重复数据 - for (auto it = systemData.details.begin(); it != systemData.details.end(); it++) { - if (it->type == data.type) { - it->rxBytes += data.rxBytes; - it->rxPackets += data.rxPackets; - it->txBytes += data.txBytes; - it->txPackets += data.txPackets; - finded = true; - } + std::string bundleName; + std::string filePath="/proc"+std::to_string(pid)+"/cmdline"; + LoadStringFromFile(filePath, bundleName); + bundleName.resize(strlen(bundleName.c_str())); + int32_t userId=0; + std::vector activeIds; + int32_t ret=AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds); + if(ret!=0){ + PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds failed ret:%d", ret); + return -1; } - - if (!finded) { - systemData.details.push_back(data); + if(activeIds.empty()){ + PROFILER_LOG_ERROR(LOG_CORE, "active id is empty"); + return -1; } -} + userId=activeIds[0]; + auto manager=SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if(manager== nullptr){ + PROFILER_LOG_ERROR(LOG_CORE, "SystemAbilityManager is nullptr"); + return -1; + } + sptr remoteObject=manager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if(remoteObject== nullptr){ + PROFILER_LOG_ERROR(LOG_CORE, "SystemAbility is nullptr"); + return -1; + } + sptr mgr=iface_cast(remoteObject); + if(mgr== nullptr){ + PROFILER_LOG_ERROR(LOG_CORE, "mgr is nullptr"); + return -1; + } + int32_t uid=mgr->GetUidByBundleName(bundleName, userId); + return uid; +} \ No newline at end of file diff --git a/device/plugins/network_plugin/test/BUILD.gn b/device/plugins/network_plugin/test/BUILD.gn index e0545b114..9f2841621 100644 --- a/device/plugins/network_plugin/test/BUILD.gn +++ b/device/plugins/network_plugin/test/BUILD.gn @@ -47,6 +47,7 @@ ohos_unittest("networkplugin_ut") { "googletest:gtest", "hilog:libhilog_base", "protobuf:protobuf_lite", + "netmanager_base:net_stats_manager_if", ] configs = [ ":module_private_config" ] subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}" diff --git a/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn b/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn index 90b63a2c7..99399c7f0 100644 --- a/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn +++ b/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn @@ -36,6 +36,7 @@ ohos_fuzztest("NetworkStartPluginFuzzTest") { "bounds_checking_function:libsec_shared", "hilog:libhilog_base", "protobuf:protobuf_lite", + "netmanager_base:net_stats_manager_if", ] include_dirs = [ "../../../include", diff --git a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp index 45850342f..b80b2e49a 100644 --- a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp +++ b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp @@ -24,7 +24,7 @@ using namespace testing::ext; namespace { const std::string DEFAULT_TEST_PATH("/data/local/tmp/"); const std::string DEFAULT_NET_PATH("/proc/net/xt_qtaguid/stats"); -constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024; +//constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024; std::string g_path; @@ -90,35 +90,6 @@ bool SetConfig(std::vector pidList, NetworkConfig& config) return true; } -bool PluginStub(NetworkPlugin& plugin, NetworkDatas& networkData, NetworkConfig& config) -{ - // serialize - int configSize = config.ByteSizeLong(); - std::vector configData(configSize); - int ret = config.SerializeToArray(configData.data(), configData.size()); - - // start - ret = plugin.Start(configData.data(), configSize); - CHECK_TRUE(ret == 0, false, "ut: start failed!\n"); - - // report - std::vector bufferData(BUF_SIZE); - ret = plugin.Report(bufferData.data(), bufferData.size()); - if (ret > 0) { - networkData.ParseFromArray(bufferData.data(), ret); - return true; - } - - return false; -} - -std::string GetFullPath(std::string path) -{ - if (path.size() > 0 && path[0] != '/') { - return Getexepath() + path; - } - return path; -} void NetworkPluginTest::SetUpTestCase() { @@ -137,195 +108,4 @@ HWTEST_F(NetworkPluginTest, Testpath, TestSize.Level1) EXPECT_NE(g_path, ""); } -/** - * @tc.name: network plugin - * @tc.desc: No network data for specific pid. - * @tc.type: FUNC - */ -HWTEST_F(NetworkPluginTest, TestNetworkDataNull, TestSize.Level1) -{ - NetworkPlugin plugin; - NetworkDatas networkData; - plugin.setPathForTest(g_path + std::string("/begin")); - - std::vector pidList = {g_expectBegin.pid}; - NetworkConfig config; - EXPECT_TRUE(SetConfig(pidList, config)); - ASSERT_TRUE(PluginStub(plugin, networkData, config)); - - for (uint32_t i = 0; i < pidList.size(); ++i) { - EXPECT_EQ(g_expectBegin.uid, plugin.GetUid(pidList[i])); - EXPECT_EQ(g_expectBegin.rx, networkData.mutable_networkinfo(i)->rx_bytes()); - EXPECT_EQ(g_expectBegin.tx, networkData.mutable_networkinfo(i)->tx_bytes()); - int index = networkData.mutable_networkinfo(i)->details_size(); - EXPECT_EQ(index, 0); - } - - // stop - plugin.Stop(); -} - -/** - * @tc.name: network plugin - * @tc.desc: get network data for specific pid. - * @tc.type: FUNC - */ -HWTEST_F(NetworkPluginTest, TestGetNetworkData, TestSize.Level1) -{ - NetworkPlugin plugin; - NetworkDatas networkData; - plugin.setPathForTest(g_path + std::string("/end")); - - std::vector pidList = {g_expectEnd.pid}; - NetworkConfig config; - EXPECT_TRUE(SetConfig(pidList, config)); - ASSERT_TRUE(PluginStub(plugin, networkData, config)); - - for (uint32_t i = 0; i < pidList.size(); ++i) { - EXPECT_EQ(g_expectEnd.uid, plugin.GetUid(pidList[i])); - EXPECT_EQ(g_expectEnd.rx, networkData.mutable_networkinfo(i)->rx_bytes()); - EXPECT_EQ(g_expectEnd.tx, networkData.mutable_networkinfo(i)->tx_bytes()); - int index = networkData.mutable_networkinfo(i)->details_size(); - EXPECT_EQ(index, 1); - for (int j = 0; j < index; ++j) { - EXPECT_EQ(g_expectEnd.details[j].tx, networkData.mutable_networkinfo(i)->mutable_details(j)->tx_bytes()); - EXPECT_EQ(g_expectEnd.details[j].rx, networkData.mutable_networkinfo(i)->mutable_details(j)->rx_bytes()); - EXPECT_EQ(g_expectEnd.details[j].type, networkData.mutable_networkinfo(i)->mutable_details(j)->type()); - } - } - - // stop - plugin.Stop(); -} - -/** - * @tc.name: network plugin - * @tc.desc: test the ParseFromArray fail of plugin start - * @tc.type: FUNC - */ -HWTEST_F(NetworkPluginTest, TestParseFromArrayOfPluginStart, TestSize.Level1) -{ - NetworkPlugin plugin; - NetworkConfig config; - int32_t pid = 1; - - // set config - config.add_pid(pid); - - // serialize - int size = config.ByteSizeLong(); - ASSERT_GT(size, 0); - std::vector configData(size); - ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); - - // start - EXPECT_NE(plugin.Start(configData.data(), size - 1), 0); -} - -long WriteFunc(WriterStruct* writer, const void* data, size_t size) -{ - if (writer == nullptr || data == nullptr || size <= 0) { - return -1; - } - return 0; -} - -bool FlushFunc(WriterStruct* writer) -{ - if (writer == nullptr) { - return false; - } - return true; -} - -/** - * @tc.name: network plugin - * @tc.desc: test register - * @tc.type: FUNC - */ -HWTEST_F(NetworkPluginTest, TestRegister, TestSize.Level1) -{ - std::string path = std::string("libnetworkplugin.z.so"); - void* handle = dlopen(path.c_str(), RTLD_LAZY); - EXPECT_NE(handle, nullptr); - PluginModuleStruct* plugin = reinterpret_cast(dlsym(handle, "g_pluginModule")); - EXPECT_NE(plugin, nullptr); - EXPECT_STREQ(plugin->name, "network-plugin"); - - // set config - NetworkConfig config; - int32_t pid = 1; - config.add_pid(pid); - int size = config.ByteSizeLong(); - ASSERT_GT(size, 0); - std::vector configData(size); - ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); - - // test framework process - WriterStruct writer = {WriteFunc, FlushFunc}; - std::vector dataBuffer(plugin->resultBufferSizeHint); - EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0); -} - -/** - * @tc.name: network plugin - * @tc.desc: Framework test - * @tc.type: FUNC - */ -HWTEST_F(NetworkPluginTest, TestFramework, TestSize.Level1) -{ - std::string path = std::string("libnetworkplugin.z.so"); - void* handle = dlopen(path.c_str(), RTLD_LAZY); - EXPECT_NE(handle, nullptr); - PluginModuleStruct* plugin = reinterpret_cast(dlsym(handle, "g_pluginModule")); - EXPECT_NE(plugin, nullptr); - EXPECT_STREQ(plugin->name, "network-plugin"); - - // set config - NetworkConfig config; - int32_t pid = 1; - config.add_pid(pid); - int size = config.ByteSizeLong(); - ASSERT_GT(size, 0); - std::vector configData(size); - ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); - - // test framework process - std::vector dataBuffer(plugin->resultBufferSizeHint); - EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0); - if (access(DEFAULT_NET_PATH.c_str(), F_OK) != 0) { - EXPECT_LT(plugin->callbacks->onPluginReportResult(dataBuffer.data(), dataBuffer.size()), 0); - } else { - EXPECT_EQ(plugin->callbacks->onPluginReportResult(dataBuffer.data(), dataBuffer.size()), 0); - } - EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0); -} - -/** - * @tc.name: network plugin - * @tc.desc: systemdata test - * @tc.type: FUNC - */ -HWTEST_F(NetworkPluginTest, TestSystemData, TestSize.Level1) -{ - std::string path = std::string("libnetworkplugin.z.so"); - void* handle = dlopen(path.c_str(), RTLD_LAZY); - EXPECT_NE(handle, nullptr); - PluginModuleStruct* plugin = reinterpret_cast(dlsym(handle, "g_pluginModule")); - - // set config - NetworkConfig config; - std::string test_file = "/data/local/tmp/utresources/begin/proc/net/xt_qtaguid/stats"; - config.set_test_file(test_file); - int size = config.ByteSizeLong(); - ASSERT_GT(size, 0); - std::vector configData(size); - ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); - - // systemdata test - std::vector dataBuffer(plugin->resultBufferSizeHint); - EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0); - EXPECT_GT(plugin->callbacks->onPluginReportResult(dataBuffer.data(), dataBuffer.size()), 0); - EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0); -} } // namespace diff --git a/protos/types/plugins/network_data/network_plugin_config.proto b/protos/types/plugins/network_data/network_plugin_config.proto index f695a4e99..06340e2b3 100644 --- a/protos/types/plugins/network_data/network_plugin_config.proto +++ b/protos/types/plugins/network_data/network_plugin_config.proto @@ -17,6 +17,5 @@ option java_package = "ohos.devtools.datasources.transport.grpc.service"; option optimize_for = LITE_RUNTIME; message NetworkConfig { - repeated int32 pid = 1; - string test_file = 2; + int32 pid = 1; } \ No newline at end of file diff --git a/protos/types/plugins/network_data/network_plugin_result.proto b/protos/types/plugins/network_data/network_plugin_result.proto index 696cc4a1a..202a9381f 100644 --- a/protos/types/plugins/network_data/network_plugin_result.proto +++ b/protos/types/plugins/network_data/network_plugin_result.proto @@ -16,42 +16,21 @@ syntax = "proto3"; option java_package = "ohos.devtools.datasources.transport.grpc.service"; option optimize_for = LITE_RUNTIME; -message NetworkDetails { - uint64 tx_bytes = 1; - uint64 rx_bytes = 2; - string type = 3; // e.g. "wlan0", "rmnet0", etc. -} - -message NetworkData { - int32 pid = 1; - // timestamp obtained by CLOCK_REALTIME - uint64 tv_sec = 2; - uint64 tv_nsec = 3; - uint64 tx_bytes = 4; - uint64 rx_bytes = 5; - repeated NetworkDetails details = 6; -} - -message NetworkSystemDetails { - string type = 1; // e.g. "wlan0", "rmnet0", etc. +message NetworkFlowDetail { + string type = 1; uint64 rx_bytes = 2; uint64 rx_packets = 3; uint64 tx_bytes = 4; uint64 tx_packets = 5; } -message NetworkSystemData { - // timestamp obtained by CLOCK_REALTIME - uint64 tv_sec = 1; - uint64 tv_nsec = 2; - uint64 rx_bytes = 3; - uint64 rx_packets = 4; - uint64 tx_bytes = 5; - uint64 tx_packets = 6; - repeated NetworkSystemDetails details = 7; -} - -message NetworkDatas { - repeated NetworkData networkinfo = 1; - NetworkSystemData network_system_info = 2; +message NetworkFlowData { + int32 pid = 1; + uint64 tv_sec = 2; + uint64 tv_nsec = 3; + uint64 rx_bytes = 4; + uint64 rx_packets = 5; + uint64 tx_bytes = 6; + uint64 tx_packets = 7; + repeated NetworkFlowDetail details = 8; } \ No newline at end of file -- Gitee From 43522a9508e3b4e5c7a8e8b628af4fbedbc239f9 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 01:42:19 +0800 Subject: [PATCH 02/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/BUILD.gn | 10 +++++----- .../test/unittest/network_plugin_test.cpp | 12 +++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/device/plugins/network_plugin/BUILD.gn b/device/plugins/network_plugin/BUILD.gn index f84f6a622..00caf59d3 100644 --- a/device/plugins/network_plugin/BUILD.gn +++ b/device/plugins/network_plugin/BUILD.gn @@ -38,17 +38,17 @@ ohos_source_set("networkplugin_source") { defines = [ "HAVE_HILOG" ] external_deps = [ "bounds_checking_function:libsec_shared", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", "hilog:libhilog_base", "hisysevent:libhisysevent", - "protobuf:protobuf_lite", - "netmanager_base:net_stats_manager_if", - "c_utils:utils", "init:libbegetutil", "ipc:ipc_core", + "netmanager_base:net_stats_manager_if", "os_account:os_account_innerkits", + "protobuf:protobuf_lite", "samgr:samgr_proxy", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", ] } public_configs = [ "${OHOS_PROFILER_DIR}/device/base:hiprofiler_test_config" ] diff --git a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp index b80b2e49a..114764dd7 100644 --- a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp +++ b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp @@ -80,17 +80,15 @@ string Getexepath() return buf; } -bool SetConfig(std::vector pidList, NetworkConfig& config) -{ - CHECK_TRUE(pidList.size() > 0, false, "ut: %s failed!\n", __func__); - for (size_t i = 0; i < pidList.size(); i++) { - config.add_pid(pidList.at(i)); +std::string GetFullPath(std::string path) +{ + if (path.size() > 0 && path[0] != '/') { + return Getexepath() + path; } - return true; + return path; } - void NetworkPluginTest::SetUpTestCase() { g_path = GetFullPath(DEFAULT_TEST_PATH); -- Gitee From c030fdb499903de1275dde0b138776229eb61141 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 05:57:35 +0800 Subject: [PATCH 03/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/test/BUILD.gn | 2 +- .../test/fuzztest/networkstartplugin_fuzzer/BUILD.gn | 2 +- .../test/unittest/profiler_service_performance_test.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/device/plugins/network_plugin/test/BUILD.gn b/device/plugins/network_plugin/test/BUILD.gn index 9f2841621..de84f80b3 100644 --- a/device/plugins/network_plugin/test/BUILD.gn +++ b/device/plugins/network_plugin/test/BUILD.gn @@ -46,8 +46,8 @@ ohos_unittest("networkplugin_ut") { "bounds_checking_function:libsec_shared", "googletest:gtest", "hilog:libhilog_base", - "protobuf:protobuf_lite", "netmanager_base:net_stats_manager_if", + "protobuf:protobuf_lite", ] configs = [ ":module_private_config" ] subsystem_name = "${OHOS_PROFILER_SUBSYS_NAME}" diff --git a/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn b/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn index 99399c7f0..533e4e36a 100644 --- a/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn +++ b/device/plugins/network_plugin/test/fuzztest/networkstartplugin_fuzzer/BUILD.gn @@ -35,8 +35,8 @@ ohos_fuzztest("NetworkStartPluginFuzzTest") { external_deps = [ "bounds_checking_function:libsec_shared", "hilog:libhilog_base", - "protobuf:protobuf_lite", "netmanager_base:net_stats_manager_if", + "protobuf:protobuf_lite", ] include_dirs = [ "../../../include", diff --git a/device/services/profiler_service/test/unittest/profiler_service_performance_test.cpp b/device/services/profiler_service/test/unittest/profiler_service_performance_test.cpp index 18ae661be..c8d1eb490 100644 --- a/device/services/profiler_service/test/unittest/profiler_service_performance_test.cpp +++ b/device/services/profiler_service/test/unittest/profiler_service_performance_test.cpp @@ -268,7 +268,7 @@ protected: ppc->set_name("network-plugin"); ppc->set_sample_interval(SAMPLE_INTERVAL); NetworkConfig nc; - nc.add_pid(1); + nc.set_pid(1); std::vector configData(nc.ByteSizeLong()); nc.SerializeToArray(configData.data(), configData.size()); ppc->set_config_data((const void*)configData.data(), configData.size()); -- Gitee From d2152c7193e2022f4b29f8ec90b85f1b5f7d7fef Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 09:37:46 +0800 Subject: [PATCH 04/15] network flow data Signed-off-by: weimingjin --- .../network_plugin/src/network_plugin.cpp | 2 +- .../test/unittest/network_plugin_test.cpp | 222 +++++++++++++++++- 2 files changed, 222 insertions(+), 2 deletions(-) diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index e9ab5c5b0..57a052d61 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -212,7 +212,7 @@ bool NetworkPlugin::HandleData(NetFlowData present, NetFlowData &difference) int32_t NetworkPlugin::GetUid(int32_t pid) { std::string bundleName; - std::string filePath="/proc"+std::to_string(pid)+"/cmdline"; + std::string filePath="/proc/"+std::to_string(pid)+"/cmdline"; LoadStringFromFile(filePath, bundleName); bundleName.resize(strlen(bundleName.c_str())); int32_t userId=0; diff --git a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp index 114764dd7..ee076bbe7 100644 --- a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp +++ b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp @@ -24,7 +24,7 @@ using namespace testing::ext; namespace { const std::string DEFAULT_TEST_PATH("/data/local/tmp/"); const std::string DEFAULT_NET_PATH("/proc/net/xt_qtaguid/stats"); -//constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024; +constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024; std::string g_path; @@ -80,6 +80,37 @@ string Getexepath() return buf; } +bool SetConfig(std::vector pidList, NetworkConfig& config) +{ + CHECK_TRUE(pidList.size() > 0, false, "ut: %s failed!\n", __func__); + + for (size_t i = 0; i < pidList.size(); i++) { + config.set_pid(pidList.at(i)); + } + return true; +} + +bool PluginStub(NetworkPlugin& plugin, NetworkFlowData& networkData, NetworkConfig& config) +{ + // serialize + int configSize = config.ByteSizeLong(); + std::vector configData(configSize); + int ret = config.SerializeToArray(configData.data(), configData.size()); + + // start + ret = plugin.Start(configData.data(), configSize); + CHECK_TRUE(ret == 0, false, "ut: start failed!\n"); + + // report + std::vector bufferData(BUF_SIZE); + ret = plugin.Report(bufferData.data(), bufferData.size()); + if (ret > 0) { + networkData.ParseFromArray(bufferData.data(), ret); + return true; + } + + return false; +} std::string GetFullPath(std::string path) { @@ -106,4 +137,193 @@ HWTEST_F(NetworkPluginTest, Testpath, TestSize.Level1) EXPECT_NE(g_path, ""); } +/** + * @tc.name: network plugin + * @tc.desc: No network data for specific pid. + * @tc.type: FUNC + */ +HWTEST_F(NetworkPluginTest, TestNetworkDataNull, TestSize.Level1) +{ + NetworkPlugin plugin; + NetworkFlowData networkData; + + std::vector pidList = {g_expectBegin.pid}; + NetworkConfig config; + EXPECT_TRUE(SetConfig(pidList, config)); + ASSERT_TRUE(PluginStub(plugin, networkData, config)); + + for (uint32_t i = 0; i < pidList.size(); ++i) { + EXPECT_GE(plugin.GetUid(pidList[i]), -1); + EXPECT_GE(networkData.rx_bytes(), 0); + EXPECT_GE(networkData.tx_bytes(), 0); + int index = networkData.details_size(); + EXPECT_GE(index, 0); + } + + // stop + plugin.Stop(); +} + +/** + * @tc.name: network plugin + * @tc.desc: get network data for specific pid. + * @tc.type: FUNC + */ +HWTEST_F(NetworkPluginTest, TestGetNetworkData, TestSize.Level1) +{ + NetworkPlugin plugin; + NetworkFlowData networkData; + + std::vector pidList = {g_expectEnd.pid}; + NetworkConfig config; + EXPECT_TRUE(SetConfig(pidList, config)); + ASSERT_TRUE(PluginStub(plugin, networkData, config)); + + for (uint32_t i = 0; i < pidList.size(); ++i) { + EXPECT_GE(plugin.GetUid(pidList[i]), -1); + EXPECT_GE(networkData.rx_bytes(), 0); + EXPECT_GE(networkData.tx_bytes(), 0); + int index = networkData.details_size(); + EXPECT_GE(index, 0); + for (int j = 0; j < index; ++j) { + EXPECT_GE(networkData.mutable_details(j)->tx_bytes(), 0); + EXPECT_GE(networkData.mutable_details(j)->rx_bytes(), 0); + EXPECT_GE(networkData.mutable_details(j)->type().size(), 0); + } + } + + // stop + plugin.Stop(); +} + +/** + * @tc.name: network plugin + * @tc.desc: test the ParseFromArray fail of plugin start + * @tc.type: FUNC + */ +HWTEST_F(NetworkPluginTest, TestParseFromArrayOfPluginStart, TestSize.Level1) +{ + NetworkPlugin plugin; + NetworkConfig config; + int32_t pid = 1; + + // set config + config.set_pid(pid); + + // serialize + int size = config.ByteSizeLong(); + ASSERT_GT(size, 0); + std::vector configData(size); + ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); + + // start + EXPECT_NE(plugin.Start(configData.data(), size - 1), 0); +} + +long WriteFunc(WriterStruct* writer, const void* data, size_t size) +{ + if (writer == nullptr || data == nullptr || size <= 0) { + return -1; + } + return 0; +} + +bool FlushFunc(WriterStruct* writer) +{ + if (writer == nullptr) { + return false; + } + return true; +} + +/** + * @tc.name: network plugin + * @tc.desc: test register + * @tc.type: FUNC + */ +HWTEST_F(NetworkPluginTest, TestRegister, TestSize.Level1) +{ + std::string path = std::string("libnetworkplugin.z.so"); + void* handle = dlopen(path.c_str(), RTLD_LAZY); + EXPECT_NE(handle, nullptr); + PluginModuleStruct* plugin = reinterpret_cast(dlsym(handle, "g_pluginModule")); + EXPECT_NE(plugin, nullptr); + EXPECT_STREQ(plugin->name, "network-plugin"); + + // set config + NetworkConfig config; + int32_t pid = 1; + config.set_pid(pid); + int size = config.ByteSizeLong(); + ASSERT_GT(size, 0); + std::vector configData(size); + ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); + + // test framework process + WriterStruct writer = {WriteFunc, FlushFunc}; + std::vector dataBuffer(plugin->resultBufferSizeHint); + EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0); +} + +/** + * @tc.name: network plugin + * @tc.desc: Framework test + * @tc.type: FUNC + */ +HWTEST_F(NetworkPluginTest, TestFramework, TestSize.Level1) +{ + std::string path = std::string("libnetworkplugin.z.so"); + void* handle = dlopen(path.c_str(), RTLD_LAZY); + EXPECT_NE(handle, nullptr); + PluginModuleStruct* plugin = reinterpret_cast(dlsym(handle, "g_pluginModule")); + EXPECT_NE(plugin, nullptr); + EXPECT_STREQ(plugin->name, "network-plugin"); + + // set config + NetworkConfig config; + int32_t pid = 1; + config.set_pid(pid); + int size = config.ByteSizeLong(); + ASSERT_GT(size, 0); + std::vector configData(size); + ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); + + // test framework process + std::vector dataBuffer(plugin->resultBufferSizeHint); + EXPECT_GE(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), -1); + if (access(DEFAULT_NET_PATH.c_str(), F_OK) != 0) { + EXPECT_LT(plugin->callbacks->onPluginReportResult(dataBuffer.data(), dataBuffer.size()), 0); + } else { + EXPECT_GE(plugin->callbacks->onPluginReportResult(dataBuffer.data(), dataBuffer.size()), -1); + } + EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0); +} + +/** + * @tc.name: network plugin + * @tc.desc: systemdata test + * @tc.type: FUNC + */ +HWTEST_F(NetworkPluginTest, TestSystemData, TestSize.Level1) +{ + std::string path = std::string("libnetworkplugin.z.so"); + void* handle = dlopen(path.c_str(), RTLD_LAZY); + EXPECT_NE(handle, nullptr); + PluginModuleStruct* plugin = reinterpret_cast(dlsym(handle, "g_pluginModule")); + + // set config + NetworkConfig config; + int32_t pid = 2; + config.set_pid(pid); + int size = config.ByteSizeLong(); + ASSERT_GT(size, 0); + std::vector configData(size); + ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0); + + // systemdata test + std::vector dataBuffer(plugin->resultBufferSizeHint); + EXPECT_GE(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), -1); + EXPECT_GE(plugin->callbacks->onPluginReportResult(dataBuffer.data(), dataBuffer.size()), -1); + EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0); +} } // namespace -- Gitee From 206f3b06af685c42458c64e45f97fb29c38f5532 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 10:09:20 +0800 Subject: [PATCH 05/15] network flow data Signed-off-by: weimingjin --- .../network_plugin/include/network_plugin.h | 12 +- .../network_plugin/src/network_plugin.cpp | 174 +++++++++--------- 2 files changed, 94 insertions(+), 92 deletions(-) diff --git a/device/plugins/network_plugin/include/network_plugin.h b/device/plugins/network_plugin/include/network_plugin.h index 7b1f57de8..ee16ed24a 100644 --- a/device/plugins/network_plugin/include/network_plugin.h +++ b/device/plugins/network_plugin/include/network_plugin.h @@ -22,6 +22,8 @@ #include "plugin_module_api.h" #include "net_stats_client.h" +using TimeSpec = struct timespec; + struct NetFlowDetail { std::string type; uint64_t rxBytes; @@ -31,7 +33,7 @@ struct NetFlowDetail { }; struct NetFlowData { - struct timespec ts; + TimeSpec ts; uint64_t rxBytes; uint64_t rxPackets; uint64_t txBytes; @@ -56,9 +58,9 @@ public: private: NetworkConfig protoConfig_; - int32_t pid_=0; - int32_t uid_=-1; - NetFlowData previous_={{0, 0}, 0, 0, 0, 0, std::vector()}; - bool isFirst=true; + int32_t pid_ = 0; + int32_t uid_ = -1; + NetFlowData previous_ = {{0, 0}, 0, 0, 0, 0, std::vector()}; + bool isFirst = true; }; #endif \ No newline at end of file diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index 57a052d61..a6974ceef 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -34,11 +34,11 @@ int NetworkPlugin::Start(const uint8_t* configData, uint32_t configSize) PROFILER_LOG_ERROR(LOG_CORE, "%s:NetworkPlugin, parseFromArray failed!", __func__); return -1; } - pid_=protoConfig_.pid(); - CHECK_TRUE(pid_>=0, -1, "%s:invalid pid", __func__); - if(pid_>0){ + pid_ = protoConfig_.pid(); + CHECK_TRUE(pid_> = 0, -1, "%s:invalid pid", __func__); + if (pid_>0) { uid_= GetUid(pid_); - CHECK_TRUE(uid_!=-1, -1, "%s:get uid failed", __func__); + CHECK_TRUE(uid_! = -1, -1, "%s:get uid failed", __func__); } PROFILER_LOG_INFO(LOG_CORE, "%s: NetworkPlugin success!", __func__); @@ -78,14 +78,14 @@ int NetworkPlugin::Stop() template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) { std::vector infos; - int res=OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos); - if(res!=0){ + int res = OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos); + if (res != 0) { PROFILER_LOG_ERROR(LOG_CORE, "GetAllStatsInfo failed ret:%d", res); } - NetFlowData netFlowData={{0, 0}, 0, 0, 0, 0, std::vector()}; - if(pid_>0){ + NetFlowData netFlowData = {{0, 0}, 0, 0, 0, 0, std::vector()}; + if (pid_ > 0) { ScreenNetworkStatByUid(infos, netFlowData); - } else{ + } else { RetainAllNetworkStat(infos, netFlowData); } networkDatasProto.set_pid(pid_); @@ -95,8 +95,8 @@ template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) networkDatasProto.set_rx_packets(netFlowData.rxPackets); networkDatasProto.set_tx_bytes(netFlowData.txBytes); networkDatasProto.set_tx_packets(netFlowData.txPackets); - for(auto& it:netFlowData.details){ - auto* data=networkDatasProto.add_details(); + for (auto& it:netFlowData.details) { + auto* data = networkDatasProto.add_details(); data->set_type(it.type); data->set_rx_bytes(it.rxBytes); data->set_rx_packets(it.rxPackets); @@ -108,30 +108,30 @@ template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) { - NetFlowData present={{0, 0}, 0, 0, 0, 0, std::vector()}; + NetFlowData present = {{0, 0}, 0, 0, 0, 0, std::vector()}; std::map detailMap; - for(auto info:infos){ - present.rxBytes+=info.rxBytes_; - present.rxPackets+=info.rxPackets_; - present.txBytes+=info.txBytes_; - present.txPackets+=info.txPackets_; - std::string type=info.iface_; - if(detailMap.find(type)!=detailMap.end()){ - detailMap[type].rxBytes+=info.rxBytes_; - detailMap[type].rxPackets+=info.rxPackets_; - detailMap[type].txBytes+=info.txBytes_; - detailMap[type].txPackets+=info.txPackets_; - } else{ + for(auto info : infos){ + present.rxBytes += info.rxBytes_; + present.rxPackets += info.rxPackets_; + present.txBytes += info.txBytes_; + present.txPackets += info.txPackets_; + std::string type = info.iface_; + if (detailMap.find(type) != detailMap.end()) { + detailMap[type].rxBytes += info.rxBytes_; + detailMap[type].rxPackets += info.rxPackets_; + detailMap[type].txBytes += info.txBytes_; + detailMap[type].txPackets += info.txPackets_; + } else { NetFlowDetail detail; - detail.type=info.iface_; - detail.rxBytes=info.rxBytes_; - detail.rxPackets=info.rxPackets_; - detail.txBytes=info.txBytes_; - detail.txPackets=info.txPackets_; - detailMap[type]=detail; + detail.type = info.iface_; + detail.rxBytes = info.rxBytes_; + detail.rxPackets = info.rxPackets_; + detail.txBytes = info.txBytes_; + detail.txPackets = info.txPackets_; + detailMap[type] = detail; } } - for(auto it:detailMap){ + for (auto it : detailMap) { present.details.push_back(it.second); } HandleData(present, data); @@ -140,20 +140,20 @@ bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) { - NetFlowData present={{0, 0}, 0, 0, 0, 0, std::vector()}; - for(auto info:infos){ - if(info.uid_==uid_){ + NetFlowData present = {{0, 0}, 0, 0, 0, 0, std::vector()}; + for(auto info : infos){ + if (info.uid_ == uid_) { NetFlowDetail detail; - detail.type=info.iface_; - detail.rxBytes=info.rxBytes_; - detail.rxPackets=info.rxPackets_; - detail.txBytes=info.txBytes_; - detail.txPackets=info.txPackets_; + detail.type = info.iface_; + detail.rxBytes = info.rxBytes_; + detail.rxPackets = info.rxPackets_; + detail.txBytes = info.txBytes_; + detail.txPackets = info.txPackets_; present.details.push_back(detail); - present.rxBytes+=info.rxBytes_; - present.rxPackets+=info.rxPackets_; - present.txBytes+=info.txBytes_; - present.txPackets+=info.txPackets_; + present.rxBytes += info.rxBytes_; + present.rxPackets += info.rxPackets_; + present.txBytes += info.txBytes_; + present.txPackets += info.txPackets_; } } HandleData(present, data); @@ -162,49 +162,49 @@ bool NetworkPlugin::ScreenNetworkStatByUid(std::vector activeIds; - int32_t ret=AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds); - if(ret!=0){ + int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds); + if (ret != 0) { PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds failed ret:%d", ret); return -1; } - if(activeIds.empty()){ + if (activeIds.empty()) { PROFILER_LOG_ERROR(LOG_CORE, "active id is empty"); return -1; } - userId=activeIds[0]; - auto manager=SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if(manager== nullptr){ + userId = activeIds[0]; + auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (manager == nullptr) { PROFILER_LOG_ERROR(LOG_CORE, "SystemAbilityManager is nullptr"); return -1; } - sptr remoteObject=manager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if(remoteObject== nullptr){ + sptr remoteObject = manager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObject == nullptr) { PROFILER_LOG_ERROR(LOG_CORE, "SystemAbility is nullptr"); return -1; } - sptr mgr=iface_cast(remoteObject); - if(mgr== nullptr){ + sptr mgr = iface_cast(remoteObject); + if (mgr == nullptr) { PROFILER_LOG_ERROR(LOG_CORE, "mgr is nullptr"); return -1; } - int32_t uid=mgr->GetUidByBundleName(bundleName, userId); + int32_t uid = mgr->GetUidByBundleName(bundleName, userId); return uid; } \ No newline at end of file -- Gitee From a59ac50775fe243784daf16c8d6021ae56eeeab9 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 10:22:31 +0800 Subject: [PATCH 06/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/src/network_plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index a6974ceef..a24df1894 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -110,7 +110,7 @@ bool NetworkPlugin::RetainAllNetworkStat(std::vector()}; std::map detailMap; - for(auto info : infos){ + for (auto info : infos) { present.rxBytes += info.rxBytes_; present.rxPackets += info.rxPackets_; present.txBytes += info.txBytes_; @@ -141,7 +141,7 @@ bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) { NetFlowData present = {{0, 0}, 0, 0, 0, 0, std::vector()}; - for(auto info : infos){ + for (auto info : infos) { if (info.uid_ == uid_) { NetFlowDetail detail; detail.type = info.iface_; -- Gitee From 2a964dda642bbe290471948c6f7ce07dc610e605 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 10:38:59 +0800 Subject: [PATCH 07/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/src/network_plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index a24df1894..0e8798c10 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -35,10 +35,10 @@ int NetworkPlugin::Start(const uint8_t* configData, uint32_t configSize) return -1; } pid_ = protoConfig_.pid(); - CHECK_TRUE(pid_> = 0, -1, "%s:invalid pid", __func__); + CHECK_TRUE(pid_ >= 0, -1, "%s:invalid pid", __func__); if (pid_>0) { uid_= GetUid(pid_); - CHECK_TRUE(uid_! = -1, -1, "%s:get uid failed", __func__); + CHECK_TRUE(uid_ != -1, -1, "%s:get uid failed", __func__); } PROFILER_LOG_INFO(LOG_CORE, "%s: NetworkPlugin success!", __func__); -- Gitee From 1a7c4acd7b93c8d67d3228d57d2f12498ca5bf4b Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 10:55:45 +0800 Subject: [PATCH 08/15] network flow data Signed-off-by: weimingjin --- .../network_plugin/include/network_plugin.h | 1 + .../network_plugin/src/network_plugin.cpp | 80 ++++++++++--------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/device/plugins/network_plugin/include/network_plugin.h b/device/plugins/network_plugin/include/network_plugin.h index ee16ed24a..1a16ec8cb 100644 --- a/device/plugins/network_plugin/include/network_plugin.h +++ b/device/plugins/network_plugin/include/network_plugin.h @@ -55,6 +55,7 @@ public: bool ScreenNetworkStatByUid(std::vector infos, NetFlowData &data); bool RetainAllNetworkStat(std::vector infos, NetFlowData &data); bool HandleData(NetFlowData present, NetFlowData &difference); + void Record(NetFlowData &newData); private: NetworkConfig protoConfig_; diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index 0e8798c10..fc768253b 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -162,53 +162,61 @@ bool NetworkPlugin::ScreenNetworkStatByUid(std::vector Date: Wed, 11 Dec 2024 10:57:27 +0800 Subject: [PATCH 09/15] network flow data Signed-off-by: weimingjin --- .../plugins/network_plugin/src/network_plugin.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index fc768253b..c478599ba 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -162,22 +162,22 @@ bool NetworkPlugin::ScreenNetworkStatByUid(std::vector Date: Wed, 11 Dec 2024 14:33:01 +0800 Subject: [PATCH 10/15] network flow data Signed-off-by: weimingjin --- .../network_plugin/test/unittest/network_plugin_test.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp index ee076bbe7..1bd220e1e 100644 --- a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp +++ b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp @@ -99,13 +99,12 @@ bool PluginStub(NetworkPlugin& plugin, NetworkFlowData& networkData, NetworkConf // start ret = plugin.Start(configData.data(), configSize); - CHECK_TRUE(ret == 0, false, "ut: start failed!\n"); + CHECK_TRUE(ret >= -1, false, "ut: start failed!\n"); // report std::vector bufferData(BUF_SIZE); ret = plugin.Report(bufferData.data(), bufferData.size()); - if (ret > 0) { - networkData.ParseFromArray(bufferData.data(), ret); + if (ret >= -1) { return true; } -- Gitee From 2f68cdde8979021ad1f9ea650a793fdaad4c88d2 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 19:54:35 +0800 Subject: [PATCH 11/15] network flow data Signed-off-by: weimingjin --- .../network_plugin/include/network_plugin.h | 1 + .../network_plugin/src/network_plugin.cpp | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/device/plugins/network_plugin/include/network_plugin.h b/device/plugins/network_plugin/include/network_plugin.h index 1a16ec8cb..09d1f68de 100644 --- a/device/plugins/network_plugin/include/network_plugin.h +++ b/device/plugins/network_plugin/include/network_plugin.h @@ -56,6 +56,7 @@ public: bool RetainAllNetworkStat(std::vector infos, NetFlowData &data); bool HandleData(NetFlowData present, NetFlowData &difference); void Record(NetFlowData &newData); + std::string GetCmdArgs(const NetworkConfig &traceConfig); private: NetworkConfig protoConfig_; diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index c478599ba..5a8e8972a 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -23,6 +23,7 @@ #include "system_ability_definition.h" #include "os_account_info.h" #include "os_account_manager.h" +#include "common.h" using namespace OHOS::Developtools::Profiler; using namespace OHOS; @@ -40,11 +41,19 @@ int NetworkPlugin::Start(const uint8_t* configData, uint32_t configSize) uid_= GetUid(pid_); CHECK_TRUE(uid_ != -1, -1, "%s:get uid failed", __func__); } - - PROFILER_LOG_INFO(LOG_CORE, "%s: NetworkPlugin success!", __func__); + int ret = COMMON::PluginWriteToHisysevent("network_plugin", "sh", GetCmdArgs(protoConfig_), + COMMON::ErrorType::RET_SUCC, "success"); + PROFILER_LOG_INFO(LOG_CORE, "%s: NetworkPlugin success! hisysevent report result:%d", __func__, ret); return 0; } +std::string NetworkPlugin::GetCmdArgs(const int &traceConfig) +{ + std::stringstream args; + args << "pid: " << traceConfig.pid(); + return args.str(); +} + int NetworkPlugin::ReportOptimize(RandomWriteCtx* randomWrite) { ProtoEncoder::NetworkFlowData dataProto(randomWrite); @@ -78,8 +87,7 @@ int NetworkPlugin::Stop() template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) { std::vector infos; - int res = OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos); - if (res != 0) { + if (OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos) != 0) { PROFILER_LOG_ERROR(LOG_CORE, "GetAllStatsInfo failed ret:%d", res); } NetFlowData netFlowData = {{0, 0}, 0, 0, 0, 0, std::vector()}; @@ -225,8 +233,7 @@ int32_t NetworkPlugin::GetUid(int32_t pid) bundleName.resize(strlen(bundleName.c_str())); int32_t userId = 0; std::vector activeIds; - int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds); - if (ret != 0) { + if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds) != 0) { PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds failed ret:%d", ret); return -1; } -- Gitee From 630ce40e3fbba0a1c08e8c12c300ce1be91a16d2 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 20:17:26 +0800 Subject: [PATCH 12/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/include/network_plugin.h | 1 + device/plugins/network_plugin/src/network_plugin.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/device/plugins/network_plugin/include/network_plugin.h b/device/plugins/network_plugin/include/network_plugin.h index 09d1f68de..66fb4fdd6 100644 --- a/device/plugins/network_plugin/include/network_plugin.h +++ b/device/plugins/network_plugin/include/network_plugin.h @@ -51,6 +51,7 @@ public: int Stop(); template bool WriteNetWorkData(T& networkDatasProto); + template void WriteData(T& networkDatasProto, NetFlowData &netFlowData); int32_t GetUid(int32_t pid); bool ScreenNetworkStatByUid(std::vector infos, NetFlowData &data); bool RetainAllNetworkStat(std::vector infos, NetFlowData &data); diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index 5a8e8972a..f2453f199 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -47,7 +47,7 @@ int NetworkPlugin::Start(const uint8_t* configData, uint32_t configSize) return 0; } -std::string NetworkPlugin::GetCmdArgs(const int &traceConfig) +std::string NetworkPlugin::GetCmdArgs(const NetworkConfig &traceConfig) { std::stringstream args; args << "pid: " << traceConfig.pid(); @@ -96,6 +96,12 @@ template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) } else { RetainAllNetworkStat(infos, netFlowData); } + WriteData(networkDatasProto, netFlowData); + return true; +} + +template void NetworkPlugin::WriteData(T &networkDatasProto, NetFlowData &netFlowData) +{ networkDatasProto.set_pid(pid_); networkDatasProto.set_tv_sec(netFlowData.ts.tv_sec); networkDatasProto.set_tv_nsec(netFlowData.ts.tv_nsec); @@ -111,7 +117,6 @@ template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) data->set_tx_bytes(it.txBytes); data->set_tx_packets(it.txPackets); } - return true; } bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) -- Gitee From ab6dea16b452126579bb15a9fd3a2400763030c3 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 20:41:47 +0800 Subject: [PATCH 13/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/src/network_plugin.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index f2453f199..d2ebabf14 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -14,6 +14,7 @@ */ #include "network_plugin.h" +#include #include "network_plugin_result.pbencoder.h" #include "securec.h" #include "application_info.h" -- Gitee From 482742fab3aad83c5c4258a72a1ac0ec493d7f6b Mon Sep 17 00:00:00 2001 From: weimingjin Date: Wed, 11 Dec 2024 20:44:40 +0800 Subject: [PATCH 14/15] network flow data Signed-off-by: weimingjin --- device/plugins/network_plugin/src/network_plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index d2ebabf14..aaa64fb6d 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -89,7 +89,7 @@ template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) { std::vector infos; if (OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos) != 0) { - PROFILER_LOG_ERROR(LOG_CORE, "GetAllStatsInfo failed ret:%d", res); + PROFILER_LOG_ERROR(LOG_CORE, "GetAllStatsInfo failed"); } NetFlowData netFlowData = {{0, 0}, 0, 0, 0, 0, std::vector()}; if (pid_ > 0) { @@ -240,7 +240,7 @@ int32_t NetworkPlugin::GetUid(int32_t pid) int32_t userId = 0; std::vector activeIds; if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds) != 0) { - PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds failed ret:%d", ret); + PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds failed"); return -1; } if (activeIds.empty()) { -- Gitee From af11239e13bb7d05c7ce9534ec5e10fe3bc20819 Mon Sep 17 00:00:00 2001 From: weimingjin Date: Thu, 12 Dec 2024 11:53:36 +0800 Subject: [PATCH 15/15] network flow data Signed-off-by: weimingjin --- .../plugins/network_plugin/include/network_plugin.h | 4 ++-- device/plugins/network_plugin/src/network_plugin.cpp | 11 +++++++---- .../test/unittest/network_plugin_test.cpp | 3 +-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/device/plugins/network_plugin/include/network_plugin.h b/device/plugins/network_plugin/include/network_plugin.h index 66fb4fdd6..3fd15b57a 100644 --- a/device/plugins/network_plugin/include/network_plugin.h +++ b/device/plugins/network_plugin/include/network_plugin.h @@ -53,8 +53,8 @@ public: template bool WriteNetWorkData(T& networkDatasProto); template void WriteData(T& networkDatasProto, NetFlowData &netFlowData); int32_t GetUid(int32_t pid); - bool ScreenNetworkStatByUid(std::vector infos, NetFlowData &data); - bool RetainAllNetworkStat(std::vector infos, NetFlowData &data); + bool ScreenNetworkStatByUid(const std::vector infos, NetFlowData &data); + bool RetainAllNetworkStat(const std::vector infos, NetFlowData &data); bool HandleData(NetFlowData present, NetFlowData &difference); void Record(NetFlowData &newData); std::string GetCmdArgs(const NetworkConfig &traceConfig); diff --git a/device/plugins/network_plugin/src/network_plugin.cpp b/device/plugins/network_plugin/src/network_plugin.cpp index aaa64fb6d..6db15df4f 100644 --- a/device/plugins/network_plugin/src/network_plugin.cpp +++ b/device/plugins/network_plugin/src/network_plugin.cpp @@ -38,8 +38,8 @@ int NetworkPlugin::Start(const uint8_t* configData, uint32_t configSize) } pid_ = protoConfig_.pid(); CHECK_TRUE(pid_ >= 0, -1, "%s:invalid pid", __func__); - if (pid_>0) { - uid_= GetUid(pid_); + if (pid_ > 0) { + uid_ = GetUid(pid_); CHECK_TRUE(uid_ != -1, -1, "%s:get uid failed", __func__); } int ret = COMMON::PluginWriteToHisysevent("network_plugin", "sh", GetCmdArgs(protoConfig_), @@ -90,6 +90,7 @@ template bool NetworkPlugin::WriteNetWorkData(T& networkDatasProto) std::vector infos; if (OHOS::NetManagerStandard::NetStatsClient::GetInstance().GetAllStatsInfo(infos) != 0) { PROFILER_LOG_ERROR(LOG_CORE, "GetAllStatsInfo failed"); + return false; } NetFlowData netFlowData = {{0, 0}, 0, 0, 0, 0, std::vector()}; if (pid_ > 0) { @@ -120,7 +121,8 @@ template void NetworkPlugin::WriteData(T &networkDatasProto, NetFlo } } -bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) +bool NetworkPlugin::RetainAllNetworkStat(const std::vector infos, + NetFlowData &data) { NetFlowData present = {{0, 0}, 0, 0, 0, 0, std::vector()}; std::map detailMap; @@ -152,7 +154,8 @@ bool NetworkPlugin::RetainAllNetworkStat(std::vector infos, NetFlowData &data) +bool NetworkPlugin::ScreenNetworkStatByUid(const std::vector infos, + NetFlowData &data) { NetFlowData present = {{0, 0}, 0, 0, 0, 0, std::vector()}; for (auto info : infos) { diff --git a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp index 1bd220e1e..e9efacfa6 100644 --- a/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp +++ b/device/plugins/network_plugin/test/unittest/network_plugin_test.cpp @@ -103,8 +103,7 @@ bool PluginStub(NetworkPlugin& plugin, NetworkFlowData& networkData, NetworkConf // report std::vector bufferData(BUF_SIZE); - ret = plugin.Report(bufferData.data(), bufferData.size()); - if (ret >= -1) { + if (plugin.Report(bufferData.data(), bufferData.size()) >= -1) { return true; } -- Gitee