From 9c80be5dacd1b46e3efbeb643b9aad44dbe2c3ed Mon Sep 17 00:00:00 2001 From: yuhaoqiang Date: Wed, 7 Feb 2024 16:05:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ESystemCpuUsage=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuhaoqiang --- .../frameworks/hidebug_ndk/hidebug_ndk.cpp | 5 +++ hidebug/frameworks/native/BUILD.gn | 1 + .../native/include/hidebug_native_interface.h | 7 +++ .../src/hidebug_native_interface_impl.cpp | 45 ++++++++++++++++++- .../interfaces/js/kits/napi/napi_hidebug.cpp | 10 ++++- .../test/unittest/js/ExampleJsunit.test.js | 18 ++++++++ hidebug/test/unittest/native/hidebug_test.cpp | 10 +++++ 7 files changed, 93 insertions(+), 3 deletions(-) diff --git a/hidebug/frameworks/hidebug_ndk/hidebug_ndk.cpp b/hidebug/frameworks/hidebug_ndk/hidebug_ndk.cpp index 05b4e439f..8fba895c5 100644 --- a/hidebug/frameworks/hidebug_ndk/hidebug_ndk.cpp +++ b/hidebug/frameworks/hidebug_ndk/hidebug_ndk.cpp @@ -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(); diff --git a/hidebug/frameworks/native/BUILD.gn b/hidebug/frameworks/native/BUILD.gn index 316dfa587..192735fe7 100644 --- a/hidebug/frameworks/native/BUILD.gn +++ b/hidebug/frameworks/native/BUILD.gn @@ -32,6 +32,7 @@ ohos_source_set("libhidebug_source") { "hichecker:libhichecker", "hidumper:lib_dump_usage", "hilog:libhilog", + "hiview:libucollection_client", "init:libbegetutil", ] } diff --git a/hidebug/frameworks/native/include/hidebug_native_interface.h b/hidebug/frameworks/native/include/hidebug_native_interface.h index fd89a8d15..37971aab3 100644 --- a/hidebug/frameworks/native/include/hidebug_native_interface.h +++ b/hidebug/frameworks/native/include/hidebug_native_interface.h @@ -26,6 +26,13 @@ public: static std::unique_ptr CreateInstance(); virtual ~HidebugNativeInterface() = default; + /** + * GetSystemCpuUsage + * + * @return the cpu usage of the system + */ + virtual double GetSystemCpuUsage() = 0; + virtual double GetCpuUsage() = 0; virtual std::map GetAppThreadCpuUsage() = 0; virtual std::string StartAppTraceCapture(uint64_t tags, uint32_t flag, uint32_t limitsize) = 0; diff --git a/hidebug/frameworks/native/src/hidebug_native_interface_impl.cpp b/hidebug/frameworks/native/src/hidebug_native_interface_impl.cpp index e849ce7aa..ab165f925 100644 --- a/hidebug/frameworks/native/src/hidebug_native_interface_impl.cpp +++ b/hidebug/frameworks/native/src/hidebug_native_interface_impl.cpp @@ -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 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::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 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(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; +} +} +} \ No newline at end of file diff --git a/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp b/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp index 68d035e23..805df862e 100644 --- a/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp +++ b/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp @@ -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); diff --git a/hidebug/test/unittest/js/ExampleJsunit.test.js b/hidebug/test/unittest/js/ExampleJsunit.test.js index 829aadf7c..bdba56229 100644 --- a/hidebug/test/unittest/js/ExampleJsunit.test.js +++ b/hidebug/test/unittest/js/ExampleJsunit.test.js @@ -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正常测试 diff --git a/hidebug/test/unittest/native/hidebug_test.cpp b/hidebug/test/unittest/native/hidebug_test.cpp index 34533fed6..ad6a07a17 100644 --- a/hidebug/test/unittest/native/hidebug_test.cpp +++ b/hidebug/test/unittest/native/hidebug_test.cpp @@ -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