Merge branch 'master' of gitee.com:openharmony/developtools_profiler into dev0227

Signed-off-by: z30044609 <zhouxiaoqin4@huawei.com>
This commit is contained in:
z30044609 2024-02-28 08:56:18 +00:00 committed by Gitee
commit f2c459662e
7 changed files with 107 additions and 3 deletions

View File

@ -28,6 +28,15 @@ double OH_HiDebug_GetAppCpuUsage()
return cpuUsage;
}
double OH_HiDebug_GetSystemCpuUsage()
{
auto cpuUsageOptional = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage();
if (cpuUsageOptional.has_value()) {
return cpuUsageOptional.value();
}
return 0;
}
HiDebug_ThreadCpuUsagePtr OH_HiDebug_GetAppThreadCpuUsage()
{
auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();

View File

@ -32,6 +32,7 @@ ohos_source_set("libhidebug_source") {
"hichecker:libhichecker",
"hidumper:lib_dump_usage",
"hilog:libhilog",
"hiview:libucollection_client",
"hiview:libucollection_utility",
"init:libbegetutil",
]

View File

@ -18,6 +18,7 @@
#include <map>
#include <memory>
#include <optional>
#include "resource/memory.h"
namespace OHOS {
@ -27,6 +28,13 @@ public:
static std::unique_ptr<HidebugNativeInterface> CreateInstance();
virtual ~HidebugNativeInterface() = default;
/**
* GetSystemCpuUsage
*
* @return the cpu usage of the system
*/
virtual std::optional<double> GetSystemCpuUsage() = 0;
virtual double GetCpuUsage() = 0;
virtual std::map<uint32_t, double> GetAppThreadCpuUsage() = 0;
virtual std::string StartAppTraceCapture(uint64_t tags, uint32_t flag, uint32_t limitsize) = 0;

View File

@ -22,7 +22,8 @@
#include "dump_usage.h"
#include "hilog/log.h"
#include "memory_collector.h"
#include "client/cpu_collector.h"
#include "utility/memory_collector.h"
namespace OHOS {
namespace HiviewDFX {
@ -41,9 +42,21 @@ public:
std::map<uint32_t, double> GetAppThreadCpuUsage() override;
std::string StartAppTraceCapture(uint64_t tags, uint32_t flag, uint32_t limitsize) override;
void StopAppTraceCapture() override;
std::optional<double> GetSystemCpuUsage() override;
std::optional<MemoryLimit> GetAppMemoryLimit() override;
std::optional<ProcessMemory> GetAppNativeMemInfo() override;
std::optional<SysMemory> GetSystemMemInfo() override;
private:
/**
* GetElapsedNanoSecondsSinceBoot
*
* @return NanoSecondsSinceBoot
*/
int64_t GetElapsedNanoSecondsSinceBoot();
constexpr static int SECOND_TO_NANOSECOND = 1 * 1000 * 1000 * 1000;
constexpr static int CPU_USAGE_VALIDITY = 2 * SECOND_TO_NANOSECOND; // 2s
int64_t lastCpuUsageGetTime_ = 0;
double lastCpuUsage_ = 0;
};
std::unique_ptr<HidebugNativeInterface> HidebugNativeInterface::CreateInstance()
@ -74,6 +87,30 @@ void HidebugNativeInterfaceImpl::StopAppTraceCapture()
{
}
std::optional<double> HidebugNativeInterfaceImpl::GetSystemCpuUsage()
{
HILOG_INFO(LOG_CORE, "GetSystemCpuUsage");
int64_t now = GetElapsedNanoSecondsSinceBoot();
if (lastCpuUsageGetTime_ > 0 && now <= lastCpuUsageGetTime_ + CPU_USAGE_VALIDITY) {
HILOG_WARN(LOG_CORE, "GetSystemCpuUsage too frequently, return the last result");
return lastCpuUsage_;
}
std::shared_ptr<UCollectClient::CpuCollector> collector = UCollectClient::CpuCollector::Create();
if (!collector) {
HILOG_ERROR(LOG_CORE, "GetSystemCpuUsage Failed, return the last result");
return std::nullopt;
}
auto collectResult = collector->GetSysCpuUsage();
if (collectResult.retCode != UCollect::UcError::SUCCESS) {
HILOG_ERROR(LOG_CORE, "GetSystemCpuUsage Failed, retCode: %{public}d, return the last result",
static_cast<int>(collectResult.retCode));
return std::nullopt;
}
lastCpuUsage_ = collectResult.data;
lastCpuUsageGetTime_ = GetElapsedNanoSecondsSinceBoot();
return lastCpuUsage_;
}
std::optional<MemoryLimit> HidebugNativeInterfaceImpl::GetAppMemoryLimit()
{
auto collector = UCollectUtil::MemoryCollector::Create();
@ -152,6 +189,11 @@ std::optional<SysMemory> HidebugNativeInterfaceImpl::GetSystemMemInfo()
return sysMemInfo;
}
int64_t HidebugNativeInterfaceImpl::GetElapsedNanoSecondsSinceBoot()
{
struct timespec times = {0, 0};
clock_gettime(CLOCK_MONOTONIC, &times);
return times.tv_sec * SECOND_TO_NANOSECOND + times.tv_nsec;
}
}
}

View File

@ -59,7 +59,8 @@ enum ErrorCode {
PERMISSION_ERROR = 201,
PARAMETER_ERROR = 401,
VERSION_ERROR = 801,
SYSTEM_ABILITY_NOT_FOUND = 11400101
SYSTEM_ABILITY_NOT_FOUND = 11400101,
SYSTEM_STATUS_ABNORMAL = 11400104,
};
}
@ -482,6 +483,19 @@ napi_value GetVss(napi_env env, napi_callback_info info)
return vss;
}
static napi_value GetSystemCpuUsage(napi_env env, napi_callback_info info)
{
auto cpuUsageOptional = HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage();
if (!cpuUsageOptional.has_value()) {
std::string paramErrorMessage = "The status of the system cpu usage is abnormal.";
napi_throw_error(env, std::to_string(ErrorCode::SYSTEM_STATUS_ABNORMAL).c_str(), paramErrorMessage.c_str());
return CreateUndefined(env);
}
napi_value retMsg = nullptr;
napi_create_double(env, cpuUsageOptional.value(), &retMsg);
return retMsg;
}
static napi_value RemoveNapiWrap(napi_env env, napi_callback_info info)
{
size_t argc = REMOVE_NAPI_WRAP_PARAM_COUNT;
@ -717,6 +731,7 @@ napi_value DeclareHiDebugInterface(napi_env env, napi_value exports)
DECLARE_NAPI_FUNCTION("removeNapiWrap", RemoveNapiWrap),
DECLARE_NAPI_FUNCTION("getAppVMMemoryInfo", GetAppVMMemoryInfo),
DECLARE_NAPI_FUNCTION("getAppThreadCpuUsage", GetAppThreadCpuUsage),
DECLARE_NAPI_FUNCTION("getSystemCpuUsage", GetSystemCpuUsage),
DECLARE_NAPI_FUNCTION("getAppMemoryLimit", GetAppMemoryLimit),
DECLARE_NAPI_FUNCTION("getAppNativeMemInfo", GetAppNativeMemInfo),
DECLARE_NAPI_FUNCTION("getSystemMemInfo", GetSystemMemInfo)

View File

@ -294,6 +294,24 @@ describe("HidebugJsTest", function () {
}
})
/**
* @tc.name: HidebugJsTest_011
* @tc.desc: getSystemCpuUsage的正常测试查询system cpu usage
* @tc.type: FUNC
* @tc.require: issueI90Z36
*/
it('HidebugJsTest_011', 0, function () {
console.info("---------------------------HidebugJsTest_011----------------------------------");
try {
let sysCpuUsage = hidebug.getSystemCpuUsage();
expect(sysCpuUsage >= 0 && sysCpuUsage <= 1).assertTrue();
} catch (error) {
console.info(error.code);
console.info(error.message);
expect(false).assertTrue();
}
})
/**
* @tc.name: HidebugJsTest_012
* @tc.desc: getAppMemoryLimit正常测试

View File

@ -174,6 +174,17 @@ HWTEST_F(HidebugTest, OH_HiDebug_GetAppThreadCpuUsage1, TestSize.Level1)
EXPECT_TRUE(true);
}
/**
* @tc.name: GetSystemCpuUsage
* @tc.desc: test InitEnvironmentParam for libc.hook_mode param set wrong_proc
* @tc.type: FUNC
*/
HWTEST_F(HidebugTest, GetSystemCpuUsage, TestSize.Level1)
{
double systemCpuUsage = OH_HiDebug_GetAppCpuUsage();
EXPECT_TRUE(systemCpuUsage >= 0 && systemCpuUsage <= 1);
}
/**
* @tc.name: GetAppMemoryLimit1
* @tc.desc: test GetAppMemoryLimit1