From b3a5b908c53781e416c0165c731b6a1e42006e4a Mon Sep 17 00:00:00 2001 From: miaojianzhuang Date: Sat, 17 Feb 2024 16:07:53 +0800 Subject: [PATCH] =?UTF-8?q?bms=E6=94=AF=E6=8C=81ap=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E8=90=BD=E7=9B=98=20issue:https://gitee.com/?= =?UTF-8?q?openharmony/arkcompiler=5Fets=5Fruntime/issues/I8YDLC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: miaojianzhuang Change-Id: Ib0e148aa575dd49cd4de30dad16a2a42b5b2c38c --- frameworks/include/bundle_command.h | 10 +++ frameworks/src/bundle_command.cpp | 108 ++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/frameworks/include/bundle_command.h b/frameworks/include/bundle_command.h index 4625b14..54d5a6e 100644 --- a/frameworks/include/bundle_command.h +++ b/frameworks/include/bundle_command.h @@ -37,11 +37,18 @@ const std::string HELP_MSG = "usage: bm \n" " get obtain device udid\n" " quickfix quick fix, including query and install\n" " compile Compile the software package\n" + " save-pgo Force save pgo\n" " dump-overlay dump overlay info of the specific overlay bundle\n" " dump-target-overlay dump overlay info of the specific target bundle\n" " dump-dependencies dump dependencies by given bundle name and module name\n" " dump-shared dump inter-application shared library information by bundle name\n"; +const std::string HELP_MSG_SAVE_PGO = + "usage: bm save-pgo [-b bundlename] (bundle-name)\n" + "options list:\n" + " -h, --help list available commands.\n" + " -b, --bundle-name select bundlename save pgo.\n"; + const std::string HELP_MSG_COMPILE = "usage: bm compile [-m mode] [-r reset] (bundle-name | -a)\n" "options list:\n" @@ -215,6 +222,7 @@ const std::string STRING_DUMP_TARGET_OVERLAY_NG = "error: failed to get target o const std::string MSG_ERR_BUNDLEMANAGER_OVERLAY_FEATURE_IS_NOT_SUPPORTED = "feature is not supported.\n"; const std::string COMPILE_SUCCESS_OK = "compile AOT success.\n"; const std::string COMPILE_RESET = "reset AOT success.\n"; +const std::string SAVE_PGO_OK = "force save PGO success.\n"; } // namespace class BundleManagerShellCommand : public ShellCommand { @@ -242,7 +250,9 @@ private: ErrCode RunAsDumpSharedDependenciesCommand(); ErrCode RunAsDumpSharedCommand(); ErrCode RunAsCompileCommand(); + ErrCode RunAsSavePgoCommand(); + std::string ForceSavePgo(const std::string &bundleName) const; std::string CompileProcessAot( const std::string &bundleName, const std::string &compileMode, bool isAllBundle) const; std::string CompileReset(const std::string &bundleName, bool isAllBundle) const; diff --git a/frameworks/src/bundle_command.cpp b/frameworks/src/bundle_command.cpp index 46b81ec..52dc206 100644 --- a/frameworks/src/bundle_command.cpp +++ b/frameworks/src/bundle_command.cpp @@ -53,6 +53,13 @@ const int32_t MAX_OVERLAY_ARGUEMENTS_NUMBER = 8; const int32_t MINIMUM_WAITTING_TIME = 180; // 3 mins const int32_t MAXIMUM_WAITTING_TIME = 600; // 10 mins +const std::string SHORT_OPTIONS_SAVE_PGO = "hb:m:"; +const struct option LONG_OPTIONS_SAVE_PGO[] = { + {"help", no_argument, nullptr, 'h'}, + {"bundle-name", required_argument, nullptr, 'b'}, + {nullptr, 0, nullptr, 0}, +}; + const std::string SHORT_OPTIONS_COMPILE = "hm:r:"; const struct option LONG_OPTIONS_COMPILE[] = { {"help", no_argument, nullptr, 'h'}, @@ -196,6 +203,7 @@ ErrCode BundleManagerShellCommand::CreateCommandMap() {"get", std::bind(&BundleManagerShellCommand::RunAsGetCommand, this)}, {"quickfix", std::bind(&BundleManagerShellCommand::RunAsQuickFixCommand, this)}, {"compile", std::bind(&BundleManagerShellCommand::RunAsCompileCommand, this)}, + {"save-pgo", std::bind(&BundleManagerShellCommand::RunAsSavePgoCommand, this)}, {"dump-overlay", std::bind(&BundleManagerShellCommand::RunAsDumpOverlay, this)}, {"dump-target-overlay", std::bind(&BundleManagerShellCommand::RunAsDumpTargetOverlay, this)}, {"dump-dependencies", std::bind(&BundleManagerShellCommand::RunAsDumpSharedDependenciesCommand, this)}, @@ -254,6 +262,94 @@ bool BundleManagerShellCommand::IsInstallOption(int index) const return false; } +ErrCode BundleManagerShellCommand::RunAsSavePgoCommand() +{ + int result = OHOS::ERR_OK; + int counter = 0; + std::string bundleName = ""; + bool bundleSavePgo = false; + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_SAVE_PGO.c_str(), LONG_OPTIONS_SAVE_PGO, nullptr); + APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind); + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + if (option == -1) { + if (counter == 1) { + // When scanning the first argument + if (strcmp(argv_[optind], cmd_.c_str()) == 0) { + // 'bm save-pgo' with no option: bm save-pgo + // 'bm save-pgo' with a wrong argument: bm save-pgo xxx + APP_LOGD("'bm save-pgo' %{public}s", HELP_MSG_NO_OPTION.c_str()); + resultReceiver_.append(HELP_MSG_NO_OPTION + "\n"); + result = OHOS::ERR_INVALID_VALUE; + } + } + break; + } + if (option == '?') { + switch (optopt) { + case 'b': { + // 'bm save-pgo -b' with no argument: bm save-pgo -n + // 'bm save-pgo --bundle-name' with no argument: bm save-pgo --bundle-name + APP_LOGD("'bm save-pgo -b' with no argument."); + resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE); + result = OHOS::ERR_INVALID_VALUE; + break; + } + default: { + // 'bm save-pgo' with an unknown option: bm save-pgo -x + // 'bm save-pgo' with an unknown option: bm save-pgo -xxx + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + APP_LOGE("'bm save-pgo' with an unknown option."); + resultReceiver_.append(unknownOptionMsg); + result = OHOS::ERR_INVALID_VALUE; + break; + } + } + break; + } + switch (option) { + case 'h': { + // 'bm save-pgo -h' + // 'bm save-pgo --help' + APP_LOGD("'bm save-pgo %{public}s'", argv_[optind - 1]); + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'b': { + // 'bm save-pgo -b xxx' + // 'bm save-pgo --bundle-name xxx' + APP_LOGD("'bm save-pgo %{public}s'", argv_[optind - 1]); + bundleName = optarg; + bundleSavePgo = true; + break; + } + default: { + APP_LOGD("'bm save-pgo %{public}s'", argv_[optind - 1]); + result = OHOS::ERR_INVALID_VALUE; + break; + } + } + } + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_SAVE_PGO); + } else { + std::string savePgoResults = ""; + APP_LOGD("savePgoResults: %{public}s", savePgoResults.c_str()); + if (bundleSavePgo) { + savePgoResults = ForceSavePgo(bundleName); + } + if (savePgoResults.empty() || (savePgoResults == "")) { + savePgoResults = HELP_MSG_SAVE_PGO + "\n"; + } + resultReceiver_.append(savePgoResults); + } + return result; +} + ErrCode BundleManagerShellCommand::RunAsCompileCommand() { APP_LOGI("begin to RunAsCompileCommand"); @@ -1746,6 +1842,18 @@ std::string BundleManagerShellCommand::GetUdid() const return udid; } +std::string BundleManagerShellCommand::ForceSavePgo(const std::string &bundleName) const +{ + std::string savePgoResults; + ErrCode SavePgoRet = bundleMgrProxy_->ForceSavePgo(bundleName); + if (SavePgoRet == ERR_APPEXECFWK_PARCEL_ERROR) { + APP_LOGE("failed to save PGO."); + return savePgoResults; + } + savePgoResults = SAVE_PGO_OK; + return savePgoResults; +} + std::string BundleManagerShellCommand::CompileProcessAot( const std::string &bundleName, const std::string &compileMode, bool isAllBundle) const { -- Gitee