!991 [新需求]: 增加IPC通信负载的API

Merge pull request !991 from chenchong_666/master
This commit is contained in:
openharmony_ci 2024-03-28 15:43:41 +00:00 committed by Gitee
commit 8f23965471
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
16 changed files with 1355 additions and 5 deletions

View File

@ -59,6 +59,7 @@
"name": "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core",
"header": {
"header_files": [
"ipc_payload_statistics.h",
"ipc_types.h",
"ipc_skeleton.h",
"iremote_object.h",
@ -80,6 +81,7 @@
"name": "//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single",
"header": {
"header_files": [
"ipc_payload_statistics.h",
"ipc_types.h",
"ipc_skeleton.h",
"iremote_object.h",

View File

@ -47,6 +47,7 @@ ohos_shared_library("ipc_core") {
"$IPC_CORE_ROOT/src/core/source/ipc_file_descriptor.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_object_proxy.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_object_stub.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_payload_statistics.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_process_skeleton.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_skeleton.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_thread_pool.cpp",

View File

@ -0,0 +1,123 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IPC_PAYLOAD_STATISTICS_H
#define OHOS_IPC_PAYLOAD_STATISTICS_H
#include "ipc_payload_statistics_impl.h"
namespace OHOS {
class IPCPayloadStatistics {
public:
IPCPayloadStatistics() = default;
~IPCPayloadStatistics() = default;
/**
* @brief Obtain the total number of times the entire process has been processed.
* @return Returns the total number of times the entire process has been retrieved.
* @since 12
*/
static uint64_t GetTotalCount();
/**
* @brief Obtain the total time spent on all processes.
* @return Returns the total time spent on all processes.
* @since 12
*/
static uint64_t GetTotalCost();
/**
* @brief Obtain the PID of all processes as a whole.
* @return Return a vector<int32_t>, which internally stores the PID of all processes obtained.
* @since 12
*/
static std::vector<int32_t> GetPids();
/**
* @brief Obtain the total number of times the corresponding process has been processed.
* @param pid is the process number.
* @return Return the total number of times the corresponding process has been processed.
* @since 12
*/
static uint64_t GetCount(const int32_t pid);
/**
* @brief Obtain the total time spent on the corresponding process.
* @param pid is the process number.
* @return Return the total time taken by the corresponding process.
* @since 12
*/
static uint64_t GetCost(const int32_t pid);
/**
* @brief Obtain the descriptor and code of the corresponding process.
* @param pid is the process number.
* @return Return a vector<IPCInterfaceInfo>, which contains all the descriptor and
* code of the corresponding process in the container.
* @since 12
*/
static std::vector<IPCInterfaceInfo> GetDescriptorCodes(const int32_t pid);
/**
* @brief Obtain the number of times corresponding to the specified pid, decs, and code.
* @param pid is the process number.
* @param desc is the interface descriptor used for IPC communication.
* @param code is the communication code used for IPC communication.
* @return Return the number of times corresponding to the specified pid, decs, and code.
* @since 12
*/
static uint64_t GetDescriptorCodeCount(const int32_t pid, const std::u16string &desc, const int32_t code);
/**
* @brief Obtain the total time consumption corresponding to the specified pid, decs, and code.
* @param pid is the process number.
* @param desc is the interface descriptor used for IPC communication.
* @param code is the communication code used for IPC communication.
* @return Returns the total time consumption corresponding to the specified pid, decs, and code.
* @since 12
*/
static IPCPayloadCost GetDescriptorCodeCost(const int32_t pid, const std::u16string &desc, const int32_t code);
/**
* @brief Start IPC load statistics.
* @return Returns <b>true</b> if the operation succeeds; return <b>false</b> Otherwise.
* @since 12
*/
static bool StartStatistics();
/**
* @brief Stop IPC load statistics.
* @return Returns <b>true</b> if the operation succeeds; return <b>false</b> Otherwise.
* @since 12
*/
static bool StopStatistics();
/**
* @brief Obtain the current IPC load statistics status.
* @return Returns <b>true</b> if the operation succeeds,Indicates that statistics are currently enabled;
* return <b>false</b>,Indicates that the statistics have been stopped currently.
* @since 12
*/
static bool GetStatisticsStatus();
/**
* @brief Clear all the data that has already been counted.
* @return Returns <b>true</b> if the operation succeeds; return <b>false</b> Otherwise.
* @since 12
*/
static bool ClearStatisticsData();
};
} // namespace OHOS
#endif // OHOS_IPC_PAYLOAD_STATISTICS_H

View File

@ -33,6 +33,7 @@
OHOS::InvokerFactory*;
OHOS::IPCDfx*;
OHOS::IPCSkeleton*;
OHOS::IPCPayloadStatistics*;
OHOS::IPCProcessSkeleton::LockForNumExecuting;
OHOS::IPCProcessSkeleton::UnlockForNumExecuting;
OHOS::IRemoteObject*;

View File

@ -38,6 +38,7 @@ ohos_shared_library("ipc_single") {
"$IPC_CORE_ROOT/src/core/source/ipc_file_descriptor.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_object_proxy.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_object_stub.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_payload_statistics.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_process_skeleton.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_skeleton.cpp",
"$IPC_CORE_ROOT/src/core/source/ipc_thread_pool.cpp",

View File

@ -24,6 +24,7 @@
OHOS::BrokerRegistration::Unregister*;
OHOS::BrokerRegistration::NewInstance*;
OHOS::IPCSkeleton*;
OHOS::IPCPayloadStatistics*;
OHOS::IPC_SINGLE::IPCProcessSkeleton::LockForNumExecuting;
OHOS::IPC_SINGLE::IPCProcessSkeleton::UnlockForNumExecuting;
OHOS::IPC_SINGLE::IPCThreadSkeleton*;

View File

@ -33,6 +33,7 @@ ohos_shared_library("ipc_common") {
sources = [
"$IPC_CORE_ROOT/src/mock/source/binder_connector.cpp",
"source/ipc_payload_statistics_impl.cpp",
"source/process_skeleton.cpp",
]

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IPC_PAYLOAD_STATISTICS_IMPL_H
#define OHOS_IPC_PAYLOAD_STATISTICS_IMPL_H
#include <map>
#include <shared_mutex>
#include <string>
#include <vector>
namespace OHOS {
struct IPCInterfaceInfo {
std::u16string desc;
int32_t code;
};
struct IPCPayloadCost {
uint64_t totalCost;
uint32_t maxCost;
uint32_t minCost;
uint32_t averCost;
};
struct IPCPayloadInfo {
IPCInterfaceInfo interface;
IPCPayloadCost cost;
uint64_t count;
};
class IPCPayloadStatisticsImpl {
public:
static IPCPayloadStatisticsImpl& GetInstance();
IPCPayloadStatisticsImpl();
~IPCPayloadStatisticsImpl();
uint64_t GetTotalCount();
uint64_t GetTotalCost();
std::vector<int32_t> GetPids();
uint64_t GetCount(const int32_t pid);
uint64_t GetCost(const int32_t pid);
std::vector<IPCInterfaceInfo> GetDescriptorCodes(const int32_t pid);
uint64_t GetDescriptorCodeCount(const int32_t pid, const std::u16string &desc, const int32_t code);
IPCPayloadCost GetDescriptorCodeCost(const int32_t pid, const std::u16string &desc, const int32_t code);
bool StartStatistics();
bool StopStatistics();
bool GetStatisticsStatus();
bool ClearStatisticsData();
bool UpdatePayloadInfo(
const int32_t pid, const std::u16string &desc, const int32_t code, const uint32_t currentCost);
private:
bool GetPayloadInfo(
const int32_t pid, const std::u16string &desc, const int32_t code, IPCPayloadInfo &payloadInfo);
std::shared_mutex dataMutex_;
std::map<int32_t, std::map<std::u16string, IPCPayloadInfo>> payloadStat_;
std::atomic<bool> isStatisticsFlag_;
};
} // namespace OHOS
#endif // OHOS_IPC_PAYLOAD_STATISTICS_IMPL_H

View File

@ -23,6 +23,7 @@
#include "hilog/log_cpp.h"
#include "iosfwd"
#include "ipc_debug.h"
#include "ipc_payload_statistics_impl.h"
#include "ipc_process_skeleton.h"
#include "ipc_skeleton.h"
#include "ipc_thread_skeleton.h"
@ -57,7 +58,8 @@ static constexpr pid_t ALLOWED_UID = 10000;
#endif
static constexpr int SHELL_UID = 2000;
static constexpr int HIDUMPER_SERVICE_UID = 1212;
static constexpr int IPC_CMD_PROCESS_WARN_TIME = 500;
static constexpr int COEFF_MILLI_TO_MICRO = 1000;
static constexpr int IPC_CMD_PROCESS_WARN_TIME = 500 * COEFF_MILLI_TO_MICRO; // 500 ms
static constexpr uint32_t IPC_OBJECT_MASK = 0xffffff;
IPCObjectStub::IPCObjectStub(std::u16string descriptor, bool serialInvokeFlag)
@ -324,12 +326,22 @@ int IPCObjectStub::SendRequest(uint32_t code, MessageParcel &data, MessageParcel
start.time_since_epoch()).count());
result = OnRemoteRequest(code, data, reply, option);
auto finish = std::chrono::steady_clock::now();
int duration = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(
uint32_t duration = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::microseconds>(
finish - start).count());
if (duration >= IPC_CMD_PROCESS_WARN_TIME) {
ZLOGW(LABEL, "stub:%{public}s deal request code:%{public}u cost time:%{public}dms",
ZLOGW(LABEL, "stub:%{public}s deal request code:%{public}u cost time:%{public}ums",
ProcessSkeleton::ConvertToSecureDesc(Str16ToStr8(GetObjectDescriptor())).c_str(),
code, duration);
code, duration / COEFF_MILLI_TO_MICRO);
}
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
if (instance.GetStatisticsStatus()) {
int32_t currentPid = IPCSkeleton::GetCallingPid();
std::u16string currentDesc = GetObjectDescriptor();
int32_t currentCode = code;
if (!instance.UpdatePayloadInfo(currentPid, currentDesc, currentCode, duration)) {
ZLOGE(LABEL, "Process load information update failed");
}
}
break;
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipc_payload_statistics.h"
namespace OHOS {
uint64_t IPCPayloadStatistics::GetTotalCount()
{
return IPCPayloadStatisticsImpl::GetInstance().GetTotalCount();
}
uint64_t IPCPayloadStatistics::GetTotalCost()
{
return IPCPayloadStatisticsImpl::GetInstance().GetTotalCost();
}
std::vector<int32_t> IPCPayloadStatistics::GetPids()
{
return IPCPayloadStatisticsImpl::GetInstance().GetPids();
}
uint64_t IPCPayloadStatistics::GetCount(const int32_t pid)
{
return IPCPayloadStatisticsImpl::GetInstance().GetCount(pid);
}
uint64_t IPCPayloadStatistics::GetCost(const int32_t pid)
{
return IPCPayloadStatisticsImpl::GetInstance().GetCost(pid);
}
std::vector<IPCInterfaceInfo> IPCPayloadStatistics::GetDescriptorCodes(const int32_t pid)
{
return IPCPayloadStatisticsImpl::GetInstance().GetDescriptorCodes(pid);
}
uint64_t IPCPayloadStatistics::GetDescriptorCodeCount(
const int32_t pid, const std::u16string &desc, const int32_t code)
{
return IPCPayloadStatisticsImpl::GetInstance().GetDescriptorCodeCount(pid, desc, code);
}
IPCPayloadCost IPCPayloadStatistics::GetDescriptorCodeCost(
const int32_t pid, const std::u16string &desc, const int32_t code)
{
return IPCPayloadStatisticsImpl::GetInstance().GetDescriptorCodeCost(pid, desc, code);
}
bool IPCPayloadStatistics::StartStatistics()
{
return IPCPayloadStatisticsImpl::GetInstance().StartStatistics();
}
bool IPCPayloadStatistics::StopStatistics()
{
return IPCPayloadStatisticsImpl::GetInstance().StopStatistics();
}
bool IPCPayloadStatistics::GetStatisticsStatus()
{
return IPCPayloadStatisticsImpl::GetInstance().GetStatisticsStatus();
}
bool IPCPayloadStatistics::ClearStatisticsData()
{
return IPCPayloadStatisticsImpl::GetInstance().ClearStatisticsData();
}
} // namespace OHOS

View File

@ -0,0 +1,289 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipc_payload_statistics_impl.h"
#include "ipc_debug.h"
#include "iremote_object.h"
#include "log_tags.h"
#include "string_ex.h"
namespace OHOS {
using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC_PAYLOAD_STATISTICS_IMPL, "IPCPayloadStatisticsImpl"};
IPCPayloadStatisticsImpl::IPCPayloadStatisticsImpl() : isStatisticsFlag_(false)
{
}
IPCPayloadStatisticsImpl::~IPCPayloadStatisticsImpl()
{
std::unique_lock<std::shared_mutex> lockGuard(dataMutex_);
payloadStat_.clear();
}
IPCPayloadStatisticsImpl& IPCPayloadStatisticsImpl::GetInstance()
{
static IPCPayloadStatisticsImpl instance;
return instance;
}
uint64_t IPCPayloadStatisticsImpl::GetTotalCount()
{
uint64_t totalCount = 0;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
for (auto iterPid = payloadStat_.begin(); iterPid != payloadStat_.end(); ++iterPid) {
auto &mapTmp = iterPid->second;
for (auto iterDesc = mapTmp.begin(); iterDesc != mapTmp.end(); ++iterDesc) {
totalCount += iterDesc->second.count;
}
}
if (totalCount == 0) {
ZLOGD(LOG_LABEL, "Statistics may not be enabled.");
}
return totalCount;
}
uint64_t IPCPayloadStatisticsImpl::GetTotalCost()
{
uint64_t totalCost = 0;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
for (auto iterPid = payloadStat_.begin(); iterPid != payloadStat_.end(); ++iterPid) {
auto &mapTmp = iterPid->second;
for (auto iterDesc = mapTmp.begin(); iterDesc != mapTmp.end(); ++iterDesc) {
totalCost += iterDesc->second.cost.totalCost;
}
}
if (totalCost == 0) {
ZLOGD(LOG_LABEL, "Statistics may not be enabled.");
}
return totalCost;
}
std::vector<int32_t> IPCPayloadStatisticsImpl::GetPids()
{
std::vector<int32_t> vec;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
for (auto iterPid = payloadStat_.begin(); iterPid != payloadStat_.end(); ++iterPid) {
vec.emplace_back(iterPid->first);
}
if (vec.size() == 0) {
ZLOGD(LOG_LABEL, "Statistics may not be enabled.");
}
return vec;
}
uint64_t IPCPayloadStatisticsImpl::GetCount(const int32_t pid)
{
uint64_t count = 0;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
auto iterPid = payloadStat_.find(pid);
if (iterPid != payloadStat_.end()) {
auto &mapTmp = iterPid->second;
for (auto iterDesc = mapTmp.begin(); iterDesc != mapTmp.end(); ++iterDesc) {
count += iterDesc->second.count;
}
} else {
ZLOGD(LOG_LABEL, "There is no data corresponding to the current PID.");
}
return count;
}
uint64_t IPCPayloadStatisticsImpl::GetCost(const int32_t pid)
{
uint64_t cost = 0;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
auto iterPid = payloadStat_.find(pid);
if (iterPid != payloadStat_.end()) {
auto &mapTmp = iterPid->second;
for (auto iterDesc = mapTmp.begin(); iterDesc != mapTmp.end(); ++iterDesc) {
cost += iterDesc->second.cost.totalCost;
}
} else {
ZLOGD(LOG_LABEL, "There is no data corresponding to the current PID.");
}
return cost;
}
std::vector<IPCInterfaceInfo> IPCPayloadStatisticsImpl::GetDescriptorCodes(const int32_t pid)
{
std::vector<IPCInterfaceInfo> vec;
IPCInterfaceInfo info;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
auto iterPid = payloadStat_.find(pid);
if (iterPid == payloadStat_.end()) {
ZLOGD(LOG_LABEL, "There is no data corresponding to the current PID.");
return vec;
}
auto &mapTmp = iterPid->second;
for (auto iterDesc = mapTmp.begin(); iterDesc != mapTmp.end(); ++iterDesc) {
vec.emplace_back(iterDesc->second.interface);
}
return vec;
}
uint64_t IPCPayloadStatisticsImpl::GetDescriptorCodeCount(
const int32_t pid, const std::u16string &desc, const int32_t code)
{
IPCPayloadInfo payloadInfo = {{u"", 0}, {0, 0, 0, 0}, 0};
bool ret = GetPayloadInfo(pid, desc, code, payloadInfo);
if (!ret) {
ZLOGD(LOG_LABEL, "Failed to obtain the total number of count.");
}
return payloadInfo.count;
}
IPCPayloadCost IPCPayloadStatisticsImpl::GetDescriptorCodeCost(
const int32_t pid, const std::u16string &desc, const int32_t code)
{
IPCPayloadInfo payloadInfo = {{u"", 0}, {0, 0, 0, 0}, 0};
IPCPayloadCost cost = { 0, 0, 0, 0 };
bool ret = GetPayloadInfo(pid, desc, code, payloadInfo);
if (!ret) {
ZLOGD(LOG_LABEL, "Failed to obtain IPCPayloadCost.");
return cost;
}
if (payloadInfo.count == 0) {
ZLOGD(LOG_LABEL, "Divisor cannot be 0.");
return cost;
}
payloadInfo.cost.averCost = payloadInfo.cost.totalCost / payloadInfo.count;
return payloadInfo.cost;
}
bool IPCPayloadStatisticsImpl::GetPayloadInfo(
const int32_t pid, const std::u16string &desc, const int32_t code, IPCPayloadInfo &payloadInfo)
{
bool ret = false;
std::shared_lock<std::shared_mutex> lockGuard(dataMutex_);
auto iterPid = payloadStat_.find(pid);
if (iterPid == payloadStat_.end()) {
ZLOGD(LOG_LABEL, "There is no data corresponding to the current PID.");
return ret;
}
auto &mapTmp = iterPid->second;
for (auto iterDesc = mapTmp.begin(); iterDesc != mapTmp.end(); ++iterDesc) {
if ((code == iterDesc->second.interface.code) && (desc == iterDesc->second.interface.desc)) {
payloadInfo = iterDesc->second;
ret = true;
break;
}
}
if (!ret) {
ZLOGD(LOG_LABEL, "There is no corresponding descriptor and code in the current process.");
}
return ret;
}
bool IPCPayloadStatisticsImpl::StartStatistics()
{
if (!isStatisticsFlag_) {
std::unique_lock<std::shared_mutex> lockGuard(dataMutex_);
payloadStat_.clear();
} else {
ZLOGD(LOG_LABEL, "Statistics have started, no need to start again.");
return true;
}
isStatisticsFlag_ = true;
return true;
}
bool IPCPayloadStatisticsImpl::StopStatistics()
{
if (!isStatisticsFlag_) {
ZLOGD(LOG_LABEL, "Statistics have been stopped, no need to stop again.");
return true;
}
isStatisticsFlag_ = false;
return true;
}
bool IPCPayloadStatisticsImpl::GetStatisticsStatus()
{
return isStatisticsFlag_;
}
bool IPCPayloadStatisticsImpl::ClearStatisticsData()
{
std::unique_lock<std::shared_mutex> lockGuard(dataMutex_);
payloadStat_.clear();
return true;
}
bool IPCPayloadStatisticsImpl::UpdatePayloadInfo(
const int32_t pid, const std::u16string &desc, const int32_t code, const uint32_t currentCost)
{
if (currentCost == 0 || !isStatisticsFlag_) {
return false;
}
IPCPayloadInfo payloadInfo;
std::u16string descTmp = desc;
std::u16string descKey = descTmp + u"_" + Str8ToStr16(std::to_string(code));
payloadInfo.interface.desc = desc;
payloadInfo.interface.code = code;
payloadInfo.count = 1; // Update the map once and increase the count once.
payloadInfo.cost.totalCost = currentCost;
payloadInfo.cost.maxCost = currentCost;
payloadInfo.cost.minCost = currentCost;
payloadInfo.cost.averCost = 0; // To improve performance, the average time consumption is not calculated.
std::unique_lock<std::shared_mutex> lockGuard(dataMutex_);
auto iterPid = payloadStat_.find(pid);
if (iterPid == payloadStat_.end()) {
std::map<std::u16string, IPCPayloadInfo> temp;
temp.insert(std::make_pair(descKey, payloadInfo));
payloadStat_.insert(std::make_pair(pid, temp));
return true;
}
auto iterDesc = iterPid->second.find(descKey);
if (iterDesc == iterPid->second.end()) {
iterPid->second.insert(std::make_pair(descKey, payloadInfo));
return true;
}
iterDesc->second.count++;
iterDesc->second.cost.totalCost += currentCost;
if (iterDesc->second.cost.maxCost < currentCost) {
iterDesc->second.cost.maxCost = currentCost;
}
if (iterDesc->second.cost.minCost > currentCost) {
iterDesc->second.cost.minCost = currentCost;
}
return true;
}
} // namespace OHOS

View File

@ -37,6 +37,8 @@ ohos_unittest("IPCNativeUnitTest") {
"ipc_core_unittest.cpp",
"ipc_object_proxy_unittest.cpp",
"ipc_object_stub_unittest.cpp",
"ipc_payload_statistics_impl_unittest.cpp",
"ipc_payload_statistics_unittest.cpp",
"ipc_process_skeleton_unittest.cpp",
"ipc_skeleton_unittest.cpp",
"ipc_thread_pool_unittest.cpp",

View File

@ -0,0 +1,549 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <ipc_payload_statistics_impl.h>
#include <climits>
using namespace testing::ext;
using namespace OHOS;
namespace {
constexpr int32_t PID_1 = 1;
constexpr int32_t PID_2 = 2;
constexpr int32_t PID_3 = INT_MAX;
constexpr int32_t DESC_CODE_1 = 1;
constexpr int32_t DESC_CODE_2 = 2;
constexpr int32_t DESC_CODE_3 = INT_MAX;
constexpr uint64_t DEFAULT_COUNT = 100000;
constexpr uint64_t DEFAULT_TOTAL_COST = 5000050000;
constexpr uint64_t DEFAULT_MAX_COST = 100000;
constexpr uint64_t DEFAULT_MIN_COST = 1;
constexpr uint64_t DOUBLE_COEFFICIENT = 2;
constexpr uint64_t TRIPLE_COEFFICIENT = 3;
}
class IPCPayloadStatisticsImplUnitTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
bool InitIPCPayloadStatisticsData();
bool ClearIPCPayloadStatisticsData();
};
void IPCPayloadStatisticsImplUnitTest::SetUpTestCase()
{
}
void IPCPayloadStatisticsImplUnitTest::TearDownTestCase()
{
}
void IPCPayloadStatisticsImplUnitTest::SetUp()
{
}
void IPCPayloadStatisticsImplUnitTest::TearDown()
{
}
bool IPCPayloadStatisticsImplUnitTest::InitIPCPayloadStatisticsData()
{
bool ret = true;
std::u16string descStr = u"string1";
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
instance.StartStatistics();
for (int32_t i = 1; i <= DEFAULT_COUNT; i++) {
ret = instance.UpdatePayloadInfo(PID_1, descStr, DESC_CODE_1, i);
if (!ret) {
return false;
}
}
descStr = u"string2";
for (int32_t i = 1; i <= DEFAULT_COUNT; i++) {
ret = instance.UpdatePayloadInfo(PID_2, descStr, DESC_CODE_2, i * DOUBLE_COEFFICIENT);
if (!ret) {
return false;
}
}
descStr = u"string3";
for (int32_t i = 1; i <= DEFAULT_COUNT; i++) {
ret = instance.UpdatePayloadInfo(PID_3, descStr, DESC_CODE_3, i * TRIPLE_COEFFICIENT);
if (!ret) {
return false;
}
}
return ret;
}
/**
* @tc.name: GetTotalCount001
* @tc.desc: Verify the GetTotalCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetTotalCount001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.GetTotalCount(), 0);
}
/**
* @tc.name: GetTotalCount002
* @tc.desc: Verify the GetTotalCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetTotalCount002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
uint64_t totalCount = DEFAULT_COUNT + DEFAULT_COUNT + DEFAULT_COUNT;
EXPECT_EQ(instance.GetTotalCount(), totalCount);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetTotalCost001
* @tc.desc: Verify the GetTotalCost function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetTotalCost001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.GetTotalCost(), 0);
}
/**
* @tc.name: GetTotalCost002
* @tc.desc: Verify the GetTotalCost function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetTotalCost002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
uint64_t totalCost =
DEFAULT_TOTAL_COST + DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT + DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT;
EXPECT_EQ(instance.GetTotalCost(), totalCost);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetPids001
* @tc.desc: Verify the GetPids function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetPids001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
std::vector<int32_t> result = instance.GetPids();
EXPECT_EQ(result.size(), 0);
}
/**
* @tc.name: GetPids002
* @tc.desc: Verify the GetPids function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetPids002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
std::vector<int32_t> result = instance.GetPids();
EXPECT_NE(result.size(), 0);
EXPECT_EQ(result[0], PID_1);
EXPECT_EQ(result[1], PID_2);
EXPECT_EQ(result[2], PID_3);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetCount001
* @tc.desc: Verify the GetCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetCount001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.GetCount(PID_1), 0);
}
/**
* @tc.name: GetCount002
* @tc.desc: Verify the GetCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetCount002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
EXPECT_EQ(instance.GetCount(PID_1), DEFAULT_COUNT);
EXPECT_EQ(instance.GetCount(PID_2), DEFAULT_COUNT);
EXPECT_EQ(instance.GetCount(PID_3), DEFAULT_COUNT);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetCost001
* @tc.desc: Verify the GetCost function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetCost001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.GetCost(PID_1), 0);
}
/**
* @tc.name: GetCost002
* @tc.desc: Verify the GetCost function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetCost002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
EXPECT_EQ(instance.GetCost(PID_1), DEFAULT_TOTAL_COST);
EXPECT_EQ(instance.GetCost(PID_2), DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT);
EXPECT_EQ(instance.GetCost(PID_3), DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetDescriptorCodes001
* @tc.desc: Verify the GetDescriptorCodes function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetDescriptorCodes001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
std::vector<IPCInterfaceInfo> info = instance.GetDescriptorCodes(PID_1);
EXPECT_EQ(info.size(), 0);
}
/**
* @tc.name: GetDescriptorCodes002
* @tc.desc: Verify the GetDescriptorCodes function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetDescriptorCodes002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
std::vector<IPCInterfaceInfo> info_1 = instance.GetDescriptorCodes(PID_1);
EXPECT_NE(info_1.size(), 0);
EXPECT_EQ(info_1[0].desc, u"string1");
EXPECT_EQ(info_1[0].code, DESC_CODE_1);
std::vector<IPCInterfaceInfo> info_2 = instance.GetDescriptorCodes(PID_2);
EXPECT_NE(info_2.size(), 0);
EXPECT_EQ(info_2[0].desc, u"string2");
EXPECT_EQ(info_2[0].code, DESC_CODE_2);
std::vector<IPCInterfaceInfo> info_3 = instance.GetDescriptorCodes(PID_3);
EXPECT_NE(info_3.size(), 0);
EXPECT_EQ(info_3[0].desc, u"string3");
EXPECT_EQ(info_3[0].code, DESC_CODE_3);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetDescriptorCodeCount001
* @tc.desc: Verify the GetDescriptorCodeCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetDescriptorCodeCount001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.GetDescriptorCodeCount(PID_1, u"string1", DESC_CODE_1), 0);
}
/**
* @tc.name: GetDescriptorCodeCount002
* @tc.desc: Verify the GetDescriptorCodeCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetDescriptorCodeCount002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
int32_t count1 = instance.GetDescriptorCodeCount(PID_1, u"string1", DESC_CODE_1);
int32_t count2 = instance.GetDescriptorCodeCount(PID_2, u"string2", DESC_CODE_2);
int32_t count3 = instance.GetDescriptorCodeCount(PID_3, u"string3", DESC_CODE_3);
EXPECT_EQ(count1, DEFAULT_COUNT);
EXPECT_EQ(count2, DEFAULT_COUNT);
EXPECT_EQ(count3, DEFAULT_COUNT);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetDescriptorCodeCost001
* @tc.desc: Verify the GetDescriptorCodeCost function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetDescriptorCodeCost001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
IPCPayloadCost cost = instance.GetDescriptorCodeCost(PID_1, u"string1", DESC_CODE_1);
EXPECT_EQ(cost.totalCost, 0);
EXPECT_EQ(cost.maxCost, 0);
EXPECT_EQ(cost.minCost, 0);
EXPECT_EQ(cost.averCost, 0);
}
/**
* @tc.name: GetDescriptorCodeCost002
* @tc.desc: Verify the GetDescriptorCodeCost function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetDescriptorCodeCost002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
IPCPayloadCost cost1 = instance.GetDescriptorCodeCost(PID_1, u"string1", DESC_CODE_1);
EXPECT_EQ(cost1.totalCost, DEFAULT_TOTAL_COST);
EXPECT_EQ(cost1.maxCost, DEFAULT_MAX_COST);
EXPECT_EQ(cost1.minCost, DEFAULT_MIN_COST);
EXPECT_EQ(cost1.averCost, DEFAULT_TOTAL_COST / DEFAULT_COUNT);
IPCPayloadCost cost2 = instance.GetDescriptorCodeCost(PID_2, u"string2", DESC_CODE_2);
EXPECT_EQ(cost2.totalCost, DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT);
EXPECT_EQ(cost2.maxCost, DEFAULT_MAX_COST * DOUBLE_COEFFICIENT);
EXPECT_EQ(cost2.minCost, DEFAULT_MIN_COST * DOUBLE_COEFFICIENT);
EXPECT_EQ(cost2.averCost, DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT / DEFAULT_COUNT);
IPCPayloadCost cost3 = instance.GetDescriptorCodeCost(PID_3, u"string3", DESC_CODE_3);
EXPECT_EQ(cost3.totalCost, DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT);
EXPECT_EQ(cost3.maxCost, DEFAULT_MAX_COST * TRIPLE_COEFFICIENT);
EXPECT_EQ(cost3.minCost, DEFAULT_MIN_COST * TRIPLE_COEFFICIENT);
EXPECT_EQ(cost3.averCost, DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT / DEFAULT_COUNT);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: GetStatisticsStatus001
* @tc.desc: Verify the GetStatisticsStatus function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, GetStatisticsStatus001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.GetStatisticsStatus(), false);
}
/**
* @tc.name: StartStatistics001
* @tc.desc: Verify the StartStatistics function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, StartStatistics001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.StartStatistics(), true);
EXPECT_EQ(instance.GetStatisticsStatus(), true);
}
/**
* @tc.name: StopStatistics001
* @tc.desc: Verify the StopStatistics function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, StopStatistics001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(instance.StopStatistics(), true);
EXPECT_EQ(instance.GetStatisticsStatus(), false);
}
/**
* @tc.name: ClearStatisticsData001
* @tc.desc: Verify the ClearStatisticsData function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, ClearStatisticsData001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.GetTotalCost(), 0);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: UpdatePayloadInfo001
* @tc.desc: Verify the UpdatePayloadInfo function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, UpdatePayloadInfo001, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
int32_t pid = 4;
IPCInterfaceInfo interfaceInfo = { u"string4", 4 };
uint32_t currentCost = 10;
uint32_t currentCount = 1;
bool ret = instance.UpdatePayloadInfo(pid, interfaceInfo.desc, interfaceInfo.code, currentCost);
EXPECT_EQ(ret, true);
uint64_t totalCount = currentCount + DEFAULT_COUNT + DEFAULT_COUNT + DEFAULT_COUNT;
uint64_t totalCost = currentCost + DEFAULT_TOTAL_COST +
DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT + DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT;
EXPECT_EQ(instance.GetTotalCount(), totalCount);
EXPECT_EQ(instance.GetTotalCost(), totalCost);
std::vector<int32_t> result = instance.GetPids();
EXPECT_NE(result.size(), 0);
EXPECT_EQ(result[0], PID_1);
EXPECT_EQ(result[1], PID_2);
EXPECT_EQ(result[2], pid);
EXPECT_EQ(result[3], PID_3);
EXPECT_EQ(instance.GetCount(pid), currentCount);
EXPECT_EQ(instance.GetCost(pid), currentCost);
std::vector<IPCInterfaceInfo> res = instance.GetDescriptorCodes(pid);
EXPECT_NE(res.size(), 0);
EXPECT_EQ(res[0].desc, interfaceInfo.desc);
EXPECT_EQ(res[0].code, interfaceInfo.code);
EXPECT_EQ(instance.GetDescriptorCodeCount(pid, interfaceInfo.desc, interfaceInfo.code), currentCount);
IPCPayloadCost cost = instance.GetDescriptorCodeCost(pid, interfaceInfo.desc, interfaceInfo.code);
EXPECT_EQ(cost.totalCost, currentCost);
EXPECT_EQ(cost.maxCost, currentCost);
EXPECT_EQ(cost.minCost, currentCost);
EXPECT_EQ(cost.averCost, currentCost / currentCount);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: UpdatePayloadInfo002
* @tc.desc: Verify the UpdatePayloadInfo function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, UpdatePayloadInfo002, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
int32_t pid = PID_1;
IPCInterfaceInfo interfaceInfo = { u"string5", 5 };
uint32_t currentCost = 5;
uint32_t currentCount = 1;
bool ret = instance.UpdatePayloadInfo(pid, interfaceInfo.desc, interfaceInfo.code, currentCost);
EXPECT_EQ(ret, true);
uint64_t totalCount = currentCount + DEFAULT_COUNT + DEFAULT_COUNT + DEFAULT_COUNT;
uint64_t totalCost = currentCost + DEFAULT_TOTAL_COST +
DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT + DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT;
EXPECT_EQ(instance.GetTotalCount(), totalCount);
EXPECT_EQ(instance.GetTotalCost(), totalCost);
std::vector<int32_t> result = instance.GetPids();
EXPECT_NE(result.size(), 0);
EXPECT_EQ(result[0], pid);
EXPECT_EQ(result[1], PID_2);
EXPECT_EQ(result[2], PID_3);
EXPECT_EQ(instance.GetCount(pid), DEFAULT_COUNT + currentCount);
EXPECT_EQ(instance.GetCost(pid), DEFAULT_TOTAL_COST + currentCost);
std::vector<IPCInterfaceInfo> res = instance.GetDescriptorCodes(pid);
EXPECT_NE(res.size(), 0);
EXPECT_EQ(res[1].desc, interfaceInfo.desc);
EXPECT_EQ(res[1].code, interfaceInfo.code);
EXPECT_EQ(instance.GetDescriptorCodeCount(pid, interfaceInfo.desc, interfaceInfo.code), currentCount);
IPCPayloadCost cost = instance.GetDescriptorCodeCost(pid, interfaceInfo.desc, interfaceInfo.code);
EXPECT_EQ(cost.totalCost, currentCost);
EXPECT_EQ(cost.maxCost, currentCost);
EXPECT_EQ(cost.minCost, currentCost);
EXPECT_EQ(cost.averCost, currentCost / currentCount);
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}
/**
* @tc.name: UpdatePayloadInfo003
* @tc.desc: Verify the UpdatePayloadInfo function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsImplUnitTest, UpdatePayloadInfo003, TestSize.Level1)
{
IPCPayloadStatisticsImpl& instance = IPCPayloadStatisticsImpl::GetInstance();
EXPECT_EQ(InitIPCPayloadStatisticsData(), true);
int32_t pid = PID_1;
IPCInterfaceInfo interfaceInfo = { u"string1", DESC_CODE_1 };
uint32_t currentCost = 5;
uint32_t currentCount = 1;
bool ret = instance.UpdatePayloadInfo(pid, interfaceInfo.desc, interfaceInfo.code, currentCost);
EXPECT_EQ(ret, true);
uint64_t totalCount = currentCount + DEFAULT_COUNT + DEFAULT_COUNT + DEFAULT_COUNT;
uint64_t totalCost = currentCost + DEFAULT_TOTAL_COST +
DEFAULT_TOTAL_COST * DOUBLE_COEFFICIENT + DEFAULT_TOTAL_COST * TRIPLE_COEFFICIENT;
EXPECT_EQ(instance.GetTotalCount(), totalCount);
EXPECT_EQ(instance.GetTotalCost(), totalCost);
std::vector<int32_t> result = instance.GetPids();
EXPECT_EQ(result.size(), 3);
EXPECT_EQ(result[0], pid);
EXPECT_EQ(result[1], PID_2);
EXPECT_EQ(result[2], PID_3);
EXPECT_EQ(instance.GetCount(pid), DEFAULT_COUNT + currentCount);
EXPECT_EQ(instance.GetCost(pid), DEFAULT_TOTAL_COST + currentCost);
std::vector<IPCInterfaceInfo> res = instance.GetDescriptorCodes(pid);
EXPECT_NE(res.size(), 0);
EXPECT_EQ(res[0].desc, interfaceInfo.desc);
EXPECT_EQ(res[0].code, interfaceInfo.code);
uint32_t count = instance.GetDescriptorCodeCount(pid, interfaceInfo.desc, interfaceInfo.code);
EXPECT_EQ(count, DEFAULT_COUNT + currentCount);
IPCPayloadCost cost = instance.GetDescriptorCodeCost(pid, interfaceInfo.desc, interfaceInfo.code);
EXPECT_EQ(cost.totalCost, DEFAULT_TOTAL_COST + currentCost);
EXPECT_EQ(cost.maxCost, DEFAULT_MAX_COST);
EXPECT_EQ(cost.minCost, DEFAULT_MIN_COST);
EXPECT_EQ(cost.averCost, (DEFAULT_TOTAL_COST + currentCost) / (DEFAULT_COUNT + currentCount));
EXPECT_EQ(instance.ClearStatisticsData(), true);
EXPECT_EQ(instance.StopStatistics(), true);
}

View File

@ -0,0 +1,181 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <ipc_payload_statistics.h>
using namespace testing::ext;
using namespace OHOS;
namespace {
constexpr int32_t PID = 1;
constexpr int32_t CODE = 1;
}
class IPCPayloadStatisticsUnitTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void IPCPayloadStatisticsUnitTest::SetUpTestCase()
{
}
void IPCPayloadStatisticsUnitTest::TearDownTestCase()
{
}
void IPCPayloadStatisticsUnitTest::SetUp()
{
}
void IPCPayloadStatisticsUnitTest::TearDown()
{
}
/**
* @tc.name: GetTotalCount001
* @tc.desc: Verify the GetTotalCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetTotalCount001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::GetTotalCount(), 0);
}
/**
* @tc.name: GetTotalCost001
* @tc.desc: Verify the GetTotalCost function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetTotalCost001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::GetTotalCost(), 0);
}
/**
* @tc.name: GetPids001
* @tc.desc: Verify the GetPids function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetPids001, TestSize.Level1)
{
std::vector<int32_t> result = IPCPayloadStatistics::GetPids();
EXPECT_EQ(result.size(), 0);
}
/**
* @tc.name: GetCount001
* @tc.desc: Verify the GetCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetCount001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::GetCount(PID), 0);
}
/**
* @tc.name: GetCost001
* @tc.desc: Verify the GetCost function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetCost001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::GetCost(PID), 0);
}
/**
* @tc.name: GetDescriptorCodes001
* @tc.desc: Verify the GetDescriptorCodes function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetDescriptorCodes001, TestSize.Level1)
{
std::vector<IPCInterfaceInfo> info = IPCPayloadStatistics::GetDescriptorCodes(PID);
EXPECT_EQ(info.size(), 0);
}
/**
* @tc.name: GetDescriptorCodeCount001
* @tc.desc: Verify the GetDescriptorCodeCount function.
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetDescriptorCodeCount001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::GetDescriptorCodeCount(PID, u"string1", CODE), 0);
}
/**
* @tc.name: GetDescriptorCodeCost001
* @tc.desc: Verify the GetDescriptorCodeCost function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetDescriptorCodeCost001, TestSize.Level1)
{
IPCPayloadCost cost = IPCPayloadStatistics::GetDescriptorCodeCost(PID, u"string1", CODE);
EXPECT_EQ(cost.totalCost, 0);
EXPECT_EQ(cost.maxCost, 0);
EXPECT_EQ(cost.minCost, 0);
EXPECT_EQ(cost.averCost, 0);
}
/**
* @tc.name: GetStatisticsStatus001
* @tc.desc: Verify the GetStatisticsStatus function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, GetStatisticsStatus001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::GetStatisticsStatus(), false);
}
/**
* @tc.name: StartStatistics001
* @tc.desc: Verify the StartStatistics function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, StartStatistics001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::StartStatistics(), true);
EXPECT_EQ(IPCPayloadStatistics::GetStatisticsStatus(), true);
}
/**
* @tc.name: StopStatistics001
* @tc.desc: Verify the StopStatistics function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, StopStatistics001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::StopStatistics(), true);
EXPECT_EQ(IPCPayloadStatistics::GetStatisticsStatus(), false);
}
/**
* @tc.name: ClearStatisticsData001
* @tc.desc: Verify the ClearStatisticsData function
* @tc.type: FUNC
*/
HWTEST_F(IPCPayloadStatisticsUnitTest, ClearStatisticsData001, TestSize.Level1)
{
EXPECT_EQ(IPCPayloadStatistics::ClearStatisticsData(), true);
}

View File

@ -14,10 +14,14 @@
*/
#include "test_service.h"
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include "ipc_skeleton.h"
#include "ipc_debug.h"
#include "ipc_payload_statistics.h"
#include "string_ex.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
@ -241,6 +245,31 @@ int TestService::Dump(int fd, const std::vector<std::u16string> &args)
writeCount = write(fd, context.data(), context.size());
}
if (!IPCPayloadStatistics::GetStatisticsStatus()) {
IPCPayloadStatistics::StartStatistics();
}
std::cout << " ---------------------------------------- "<< std::endl;
std::cout << " TotalCount = " << IPCPayloadStatistics::GetTotalCount();
std::cout << " TotalCost = " << IPCPayloadStatistics::GetTotalCost() << std::endl;
std::vector<int32_t> pidVec = IPCPayloadStatistics::GetPids();
for (int32_t &val : pidVec) {
std::cout << " Pid = " << val << std::endl;
std::cout << " PidCount = " << IPCPayloadStatistics::GetCount(val)
<< " PidCost = " << IPCPayloadStatistics::GetCost(val) << std::endl;
std::vector<IPCInterfaceInfo> infoVec = IPCPayloadStatistics::GetDescriptorCodes(val);
for (auto &info : infoVec) {
std::cout << " desc = " << Str16ToStr8(info.desc) << " code = " << info.code;
std::cout << " DescCount = " << IPCPayloadStatistics::GetDescriptorCodeCount(val, info.desc, info.code);
IPCPayloadCost payloadCost = IPCPayloadStatistics::GetDescriptorCodeCost(val, info.desc, info.code);
std::cout << " DescTotalCost = " << payloadCost.totalCost;
std::cout << " DescMaxCost = " << payloadCost.maxCost;
std::cout << " DescMinCost = " << payloadCost.minCost;
std::cout << " DescAverCost = " << payloadCost.averCost << std::endl;
}
}
std::cout << " ---------------------------------------- "<< std::endl;
return writeCount > 0 ? ERR_NONE : ERR_TRANSACTION_FAILED;
}

View File

@ -31,6 +31,7 @@ const unsigned int LOG_ID_IPC_NAPI = LOG_ID_IPC_BASE | 0x08;
const unsigned int LOG_ID_IPC_OTHER = LOG_ID_IPC_BASE | 0x09;
const unsigned int LOG_ID_IPC_RUST = LOG_ID_IPC_BASE | 0x0A;
const unsigned int LOG_ID_IPC_PARCEL = LOG_ID_IPC_BASE | 0x0B;
const unsigned int LOG_ID_IPC_PAYLOAD_STATISTICS_IMPL = LOG_ID_IPC_BASE | 0x0C;
const unsigned int LOG_ID_IPC_TEST = 0xD000F00;
const unsigned int LOG_ID_RPC_COMMON = LOG_ID_IPC_BASE | 0x10;