mirror of
https://gitee.com/openharmony/developtools_profiler
synced 2024-11-27 00:51:47 +00:00
新增SystemCpuUsage接口
Signed-off-by: yuhaoqiang <yuhaoqiang@huawei.com>
This commit is contained in:
parent
30ed46816a
commit
9c80be5dac
@ -28,6 +28,11 @@ double OH_HiDebug_GetAppCpuUsage()
|
||||
return cpuUsage;
|
||||
}
|
||||
|
||||
double OH_HiDebug_GetSystemCpuUsage()
|
||||
{
|
||||
return OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage();
|
||||
}
|
||||
|
||||
HiDebug_ThreadCpuUsagePtr OH_HiDebug_GetAppThreadCpuUsage()
|
||||
{
|
||||
auto nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::CreateInstance();
|
||||
|
@ -32,6 +32,7 @@ ohos_source_set("libhidebug_source") {
|
||||
"hichecker:libhichecker",
|
||||
"hidumper:lib_dump_usage",
|
||||
"hilog:libhilog",
|
||||
"hiview:libucollection_client",
|
||||
"init:libbegetutil",
|
||||
]
|
||||
}
|
||||
|
@ -26,6 +26,13 @@ public:
|
||||
static std::unique_ptr<HidebugNativeInterface> CreateInstance();
|
||||
virtual ~HidebugNativeInterface() = default;
|
||||
|
||||
/**
|
||||
* GetSystemCpuUsage
|
||||
*
|
||||
* @return the cpu usage of the system
|
||||
*/
|
||||
virtual 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;
|
||||
|
@ -22,7 +22,7 @@
|
||||
|
||||
#include "dump_usage.h"
|
||||
#include "hilog/log.h"
|
||||
|
||||
#include "client/cpu_collector.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace HiviewDFX {
|
||||
@ -41,6 +41,18 @@ public:
|
||||
std::map<uint32_t, double> GetAppThreadCpuUsage() override;
|
||||
std::string StartAppTraceCapture(uint64_t tags, uint32_t flag, uint32_t limitsize) override;
|
||||
void StopAppTraceCapture() override;
|
||||
double GetSystemCpuUsage() override;
|
||||
private:
|
||||
/**
|
||||
* GetElapsedNanoSecondsSinceBoot
|
||||
*
|
||||
* @return NanoSecondsSinceBoot
|
||||
*/
|
||||
int64_t GetElapsedNanoSecondsSinceBoot();
|
||||
constexpr static int secondToNanoSecond = 1 * 1000 * 1000 * 1000;
|
||||
constexpr static int cpuUsageValidity = 2 * secondToNanoSecond; // 2s
|
||||
int64_t lastCpuUsageGetTime_ = 0;
|
||||
double lastCpuUsage_ = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<HidebugNativeInterface> HidebugNativeInterface::CreateInstance()
|
||||
@ -71,6 +83,35 @@ void HidebugNativeInterfaceImpl::StopAppTraceCapture()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
double HidebugNativeInterfaceImpl::GetSystemCpuUsage()
|
||||
{
|
||||
HILOG_INFO(LOG_CORE, "GetSystemCpuUsage");
|
||||
int64_t now = GetElapsedNanoSecondsSinceBoot();
|
||||
if (lastCpuUsageGetTime_ > 0 && now <= lastCpuUsageGetTime_ + cpuUsageValidity) {
|
||||
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 lastCpuUsage_;
|
||||
}
|
||||
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 lastCpuUsage_;
|
||||
}
|
||||
lastCpuUsage_ = collectResult.data;
|
||||
lastCpuUsageGetTime_ = GetElapsedNanoSecondsSinceBoot();
|
||||
return lastCpuUsage_;
|
||||
}
|
||||
|
||||
int64_t HidebugNativeInterfaceImpl::GetElapsedNanoSecondsSinceBoot()
|
||||
{
|
||||
struct timespec times = {0, 0};
|
||||
clock_gettime(CLOCK_MONOTONIC, ×);
|
||||
return times.tv_sec * secondToNanoSecond + times.tv_nsec;
|
||||
}
|
||||
}
|
||||
}
|
@ -482,6 +482,13 @@ napi_value GetVss(napi_env env, napi_callback_info info)
|
||||
return vss;
|
||||
}
|
||||
|
||||
static napi_value GetSystemCpuUsage(napi_env env, napi_callback_info info)
|
||||
{
|
||||
napi_value retMsg = nullptr;
|
||||
napi_create_double(env, HidebugNativeInterface::CreateInstance()->GetSystemCpuUsage(), &retMsg);
|
||||
return retMsg;
|
||||
}
|
||||
|
||||
static napi_value RemoveNapiWrap(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = REMOVE_NAPI_WRAP_PARAM_COUNT;
|
||||
@ -610,7 +617,8 @@ napi_value DeclareHiDebugInterface(napi_env env, napi_value exports)
|
||||
DECLARE_NAPI_FUNCTION("getVss", GetVss),
|
||||
DECLARE_NAPI_FUNCTION("removeNapiWrap", RemoveNapiWrap),
|
||||
DECLARE_NAPI_FUNCTION("getAppVMMemoryInfo", GetAppVMMemoryInfo),
|
||||
DECLARE_NAPI_FUNCTION("getAppThreadCpuUsage", GetAppThreadCpuUsage)
|
||||
DECLARE_NAPI_FUNCTION("getAppThreadCpuUsage", GetAppThreadCpuUsage),
|
||||
DECLARE_NAPI_FUNCTION("getSystemCpuUsage", GetSystemCpuUsage)
|
||||
};
|
||||
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
|
||||
InitNapiClass(env, exports);
|
||||
|
@ -250,6 +250,24 @@ describe("HidebugJsTest", function () {
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* @tc.name: HidebugJsTest_011
|
||||
* @tc.desc: getSysCpuUsage的正常测试,查询system cpu usage
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI90Z36
|
||||
*/
|
||||
it('HidebugJsTest_0011', 0, function () {
|
||||
console.info("---------------------------HidebugJsTest_0011----------------------------------");
|
||||
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_013
|
||||
* @tc.desc: getAppVMMemoryInfo正常测试
|
||||
|
@ -174,4 +174,14 @@ 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);
|
||||
}
|
||||
} // namespace
|
||||
|
Loading…
Reference in New Issue
Block a user