已合并
feat(ability_runtime): add ARKTS_HEAP dump type for hidumper --show-arktsheap #20270
denganliang创建于 16 天前
feat(ability_runtime): add ARKTS_HEAP dump type for hidumper --show-arktsheap #20270
已合并
共 4 个文件变更+68-0
| @@ -17,6 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | + | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | 23 | ||
| @@ -402,6 +403,9 @@ void DumpRuntimeHelper::DumpMem(const OHOS::AppExecFwk::MemDumpInfo &info, sptr< | |||
| 402 | if (info.dumpType == MemDumpType::ARKWEB_JS) { | 403 | if (info.dumpType == MemDumpType::ARKWEB_JS) { |
| 403 | DumpArkwebJsHeap(info); | 404 | DumpArkwebJsHeap(info); |
| 404 | } | 405 | } |
| 406 | + if (info.dumpType == MemDumpType::ARKTS_HEAP) { | ||
| 407 | + DumpArktsHeapSize(info, dumpResult); | ||
| 408 | + } | ||
| 405 | if (client != nullptr && callback != nullptr) { | 409 | if (client != nullptr && callback != nullptr) { |
| 406 | client->ReportDumpMemResult(callback, dumpResult); | 410 | client->ReportDumpMemResult(callback, dumpResult); |
| 407 | } | 411 | } |
| @@ -583,6 +587,48 @@ void DumpRuntimeHelper::DumpArkwebJsHeap(const OHOS::AppExecFwk::MemDumpInfo &in | |||
| 583 | 587 | ||
| 584 | } | 588 | } |
| 585 | 589 | ||
| 590 | +void DumpRuntimeHelper::DumpArktsHeapSize(const OHOS::AppExecFwk::MemDumpInfo &info, std::string &dumpResult) | ||
D | |||
| 591 | +{ | ||
| 592 | + if (application_ == nullptr) { | ||
| 593 | + TAG_LOGE(AAFwkTag::APPKIT, "null application"); | ||
| 594 | + return; | ||
| 595 | + } | ||
| 596 | + auto &runtime = application_->GetRuntime(); | ||
| 597 | + if (runtime == nullptr) { | ||
| 598 | + TAG_LOGE(AAFwkTag::APPKIT, "null runtime"); | ||
| 599 | + return; | ||
| 600 | + } | ||
| 601 | + auto language = runtime->GetLanguage(); | ||
| 602 | + if (language != AbilityRuntime::Runtime::Language::JS && | ||
| 603 | + language != AbilityRuntime::Runtime::Language::ETS) { | ||
| 604 | + TAG_LOGE(AAFwkTag::APPKIT, "runtime language is not JS or ETS"); | ||
| 605 | + return; | ||
| 606 | + } | ||
| 607 | + AbilityRuntime::JsRuntime* jsRuntime = nullptr; | ||
| 608 | + if (language == AbilityRuntime::Runtime::Language::JS) { | ||
| 609 | + jsRuntime = static_cast<AbilityRuntime::JsRuntime*>(runtime.get()); | ||
| 610 | + } else { | ||
| 611 | + auto &etsRuntime = static_cast<AbilityRuntime::ETSRuntime&>(*runtime); | ||
| 612 | + auto &jsRuntimePtr = etsRuntime.GetJsRuntime(); | ||
| 613 | + if (jsRuntimePtr == nullptr) { | ||
| 614 | + TAG_LOGE(AAFwkTag::APPKIT, "null jsRuntime in ets"); | ||
| 615 | + return; | ||
| 616 | + } | ||
| 617 | + jsRuntime = static_cast<AbilityRuntime::JsRuntime*>(jsRuntimePtr.get()); | ||
| 618 | + } | ||
| 619 | + if (jsRuntime == nullptr) { | ||
| 620 | + TAG_LOGE(AAFwkTag::APPKIT, "null jsRuntime"); | ||
| 621 | + return; | ||
| 622 | + } | ||
| 623 | + size_t heapSize = jsRuntime->GetHeapTotalSize(); | ||
| 624 | + char threadName[THREAD_NAME_MAX_LEN] = {0}; | ||
| 625 | + if (pthread_getname_np(pthread_self(), threadName, sizeof(threadName)) != 0) { | ||
| 626 | + TAG_LOGW(AAFwkTag::APPKIT, "pthread_getname_np failed"); | ||
| 627 | + } | ||
| 628 | + dumpResult = std::to_string(heapSize) + "|" + threadName; | ||
| 629 | + TAG_LOGI(AAFwkTag::APPKIT, "arkts heap size: %{public}zu bytes, thread: %{public}s", heapSize, threadName); | ||
| 630 | +} | ||
| 631 | + | ||
| 586 | void DumpRuntimeHelper::GetCheckList(const std::unique_ptr<AbilityRuntime::Runtime> &runtime, std::string &checkList) | 632 | void DumpRuntimeHelper::GetCheckList(const std::unique_ptr<AbilityRuntime::Runtime> &runtime, std::string &checkList) |
| 587 | { | 633 | { |
| 588 | if (runtime->GetLanguage() != AbilityRuntime::Runtime::Language::JS) { | 634 | if (runtime->GetLanguage() != AbilityRuntime::Runtime::Language::JS) { |
| @@ -27,6 +27,7 @@ enum class MemDumpType : uint32_t { | |||
| 27 | JSVM = 2, | 27 | JSVM = 2, |
| 28 | KMP_KOTLIN = 3, | 28 | KMP_KOTLIN = 3, |
| 29 | ARKWEB_JS = 4, | 29 | ARKWEB_JS = 4, |
| 30 | + ARKTS_HEAP = 5, | ||
| 30 | }; | 31 | }; |
| 31 | 32 | ||
| 32 | struct MemDumpInfo : public Parcelable { | 33 | struct MemDumpInfo : public Parcelable { |
| @@ -83,6 +83,8 @@ private: | |||
| 83 | void DumpKmpKotlinHeap(const OHOS::AppExecFwk::MemDumpInfo &info); | 83 | void DumpKmpKotlinHeap(const OHOS::AppExecFwk::MemDumpInfo &info); |
| 84 | void DumpJsvmHeap(const OHOS::AppExecFwk::MemDumpInfo &info); | 84 | void DumpJsvmHeap(const OHOS::AppExecFwk::MemDumpInfo &info); |
| 85 | void DumpArkwebJsHeap(const OHOS::AppExecFwk::MemDumpInfo &info); | 85 | void DumpArkwebJsHeap(const OHOS::AppExecFwk::MemDumpInfo &info); |
| 86 | + static constexpr int32_t THREAD_NAME_MAX_LEN = 16; | ||
| 87 | + void DumpArktsHeapSize(const OHOS::AppExecFwk::MemDumpInfo &info, std::string &dumpResult); | ||
| 86 | }; | 88 | }; |
| 87 | } // namespace AppExecFwk | 89 | } // namespace AppExecFwk |
| 88 | } // namespace OHOS | 90 | } // namespace OHOS |
| @@ -205,6 +205,25 @@ HWTEST_F(DumpRuntimeHelperTest, DumpMem_0400, Function | MediumTest | Level1) | |||
| 205 | GTEST_LOG_(INFO) << "DumpRuntimeHelperTest DumpMem_0400 end"; | 205 | GTEST_LOG_(INFO) << "DumpRuntimeHelperTest DumpMem_0400 end"; |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | +/** | ||
| 209 | + * @tc.number: DumpMem_0500 | ||
| 210 | + * @tc.name: DumpMem | ||
| 211 | + * @tc.desc: Test DumpMem with ARKTS_HEAP type does not crash when runtime is null. | ||
| 212 | + */ | ||
| 213 | +HWTEST_F(DumpRuntimeHelperTest, DumpMem_0500, Function | MediumTest | Level1) | ||
| 214 | +{ | ||
| 215 | + GTEST_LOG_(INFO) << "DumpRuntimeHelperTest DumpMem_0500 start"; | ||
| 216 | + std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>(); | ||
| 217 | + EXPECT_NE(application, nullptr); | ||
| 218 | + OHOS::AppExecFwk::MemDumpInfo info; | ||
| 219 | + info.pid = 1; | ||
| 220 | + info.needLeakobj = false; | ||
| 221 | + info.dumpType = MemDumpType::ARKTS_HEAP; | ||
| 222 | + auto helper = std::make_shared<DumpRuntimeHelper>(application); | ||
| 223 | + helper->DumpMem(info, nullptr); | ||
| 224 | + GTEST_LOG_(INFO) << "DumpRuntimeHelperTest DumpMem_0500 end"; | ||
| 225 | +} | ||
| 226 | + | ||
| 208 | /** | 227 | /** |
| 209 | * @tc.number: CheckOomdumpSwitch_0100 | 228 | * @tc.number: CheckOomdumpSwitch_0100 |
| 210 | * @tc.name: CheckOomdumpSwitch | 229 | * @tc.name: CheckOomdumpSwitch |
复用原回调通路返回mem数据