Add Switch For external package compiling

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I84SCZ
Signed-off-by: hzzhouzebin <zhouzebin1@huawei.com>
Change-Id: Ib4853662fc4d833ad1a5e35eef48785e8b512a0f
This commit is contained in:
hzzhouzebin 2023-09-27 14:36:04 +08:00
parent 0f514da3c4
commit 672e0f7e42
5 changed files with 89 additions and 53 deletions

View File

@ -26,6 +26,7 @@
#include "ecmascript/js_runtime_options.h"
#include "ecmascript/jspandafile/js_pandafile_manager.h"
#include "ecmascript/log.h"
#include "ecmascript/log_wrapper.h"
#include "ecmascript/module/js_module_manager.h"
#include "ecmascript/napi/include/jsnapi.h"
#include "ecmascript/platform/file.h"
@ -42,8 +43,7 @@ std::string GetHelper()
return str;
}
CompilationOptions::CompilationOptions(EcmaVM *vm, JSRuntimeOptions &runtimeOptions,
OhosPkgArgs &pkgArgs, arg_list_t &pandaFileNames)
CompilationOptions::CompilationOptions(EcmaVM *vm, JSRuntimeOptions &runtimeOptions)
{
triple_ = runtimeOptions.GetTargetTriple();
if (runtimeOptions.GetAOTOutputFile().empty()) {
@ -91,22 +91,21 @@ bool CompilationPreprocessor::HandleTargetCompilerMode(CompilationOptions &cOpti
bool CompilationPreprocessor::HandleOhosPkgArgs()
{
ASSERT(runtimeOptions_.IsTargetCompilerMode());
OhosPkgArgs pkgArgs;
if (!runtimeOptions_.GetCompilerPkgJsonInfo().empty()) {
if (pkgArgs_.ParseFromJson(vm_, runtimeOptions_.GetCompilerPkgJsonInfo())) {
if (pkgArgs.ParseFromJson(vm_, runtimeOptions_.GetCompilerPkgJsonInfo())) {
LOG_COMPILER(INFO) << "Parse main pkg info success.";
pkgArgs_.Dump();
pandaFileNames_.emplace_back(pkgArgs_.GetFullName());
pkgsArgs_[pkgArgs.GetFullName()] = pkgArgs;
} else {
return false;
}
}
// for external pkg, dump it first.
if (!runtimeOptions_.GetCompilerExternalPkgJsonInfo().empty()) {
std::list<OhosPkgArgs> externalList;
OhosPkgArgs::ParseListFromJson(vm_, runtimeOptions_.GetCompilerExternalPkgJsonInfo(), externalList);
for (const auto &externalPkg : externalList) {
externalPkg.Dump();
}
if (runtimeOptions_.GetCompilerEnableExternalPkg() && !runtimeOptions_.GetCompilerExternalPkgJsonInfo().empty()) {
OhosPkgArgs::ParseListFromJson(vm_, runtimeOptions_.GetCompilerExternalPkgJsonInfo(), pkgsArgs_);
}
for (const auto &pkgInfo : pkgsArgs_) {
pandaFileNames_.emplace_back(pkgInfo.first);
pkgInfo.second.Dump();
}
return true;
}
@ -125,7 +124,7 @@ void CompilationPreprocessor::HandleTargetModeInfo(CompilationOptions &cOptions)
bool CompilationPreprocessor::HandlePandaFileNames(const int argc, const char **argv)
{
if (runtimeOptions_.GetCompilerPkgJsonInfo().empty() || !pkgArgs_.Valid()) {
if (runtimeOptions_.GetCompilerPkgJsonInfo().empty() || pkgsArgs_.empty()) {
// if no pkgArgs, last param must be abc file
std::string files = argv[argc - 1];
if (!base::StringHelper::EndsWith(files, ".abc")) {
@ -182,37 +181,14 @@ std::shared_ptr<JSPandaFile> CompilationPreprocessor::CreateAndVerifyJSPandaFile
JSPandaFileManager *jsPandaFileManager = JSPandaFileManager::GetInstance();
std::shared_ptr<JSPandaFile> jsPandaFile = nullptr;
if (runtimeOptions_.IsTargetCompilerMode()) {
std::string hapPath;
uint32_t offset {};
uint32_t size {};
if (pkgArgs_.Valid()) {
hapPath = pkgArgs_.GetPath();
offset = pkgArgs_.GetOffset();
size = pkgArgs_.GetSize();
} else {
// for legacy params
hapPath = runtimeOptions_.GetHapPath();
offset = runtimeOptions_.GetHapAbcOffset();
size = runtimeOptions_.GetHapAbcSize();
}
if (size == 0) {
LOG_ECMA(ERROR) << "buffer is empty in target compiler mode!";
auto pkgArgsIter = pkgsArgs_.find(fileName);
if (pkgArgsIter == pkgsArgs_.end()) {
LOG_COMPILER(ERROR) << "Can not find file in ohos pkgs args. file name: " << fileName;
return nullptr;
}
std::string realPath;
if (!RealPath(hapPath, realPath, false)) {
LOG_ECMA(ERROR) << "realpath for hap path failed!";
if (!(pkgArgsIter->second.GetJSPandaFile(runtimeOptions_, jsPandaFile))) {
return nullptr;
}
MemMap fileMapMem = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READ);
if (fileMapMem.GetOriginAddr() == nullptr) {
LOG_ECMA(ERROR) << "File mmap failed";
return nullptr;
}
uint8_t *buffer = reinterpret_cast<uint8_t *>(fileMapMem.GetOriginAddr()) + offset;
jsPandaFile = jsPandaFileManager->OpenJSPandaFileFromBuffer(buffer, size, fileName.c_str());
FileUnMap(fileMapMem);
fileMapMem.Reset();
} else {
jsPandaFile = jsPandaFileManager->OpenJSPandaFile(fileName.c_str());
}
@ -342,15 +318,15 @@ int Main(const int argc, const char **argv)
{
LocalScope scope(vm);
arg_list_t pandaFileNames {};
OhosPkgArgs pkgArgs;
CompilationOptions cOptions(vm, runtimeOptions, pkgArgs, pandaFileNames);
std::map<std::string, OhosPkgArgs> pkgArgsMap;
CompilationOptions cOptions(vm, runtimeOptions);
CompilerLog log(cOptions.logOption_);
log.SetEnableCompilerLogTime(cOptions.compilerLogTime_);
AotMethodLogList logList(cOptions.logMethodsList_);
PGOProfilerDecoder profilerDecoder(cOptions.profilerIn_, cOptions.hotnessThreshold_);
CompilationPreprocessor cPreprocessor(vm, runtimeOptions, pkgArgs, profilerDecoder, pandaFileNames);
CompilationPreprocessor cPreprocessor(vm, runtimeOptions, pkgArgsMap, profilerDecoder, pandaFileNames);
if (!cPreprocessor.HandleTargetCompilerMode(cOptions) ||
!cPreprocessor.HandlePandaFileNames(argc, argv)) {
return 1;

View File

@ -33,8 +33,7 @@ struct AbcFileInfo {
};
struct CompilationOptions {
explicit CompilationOptions(EcmaVM *vm, JSRuntimeOptions &runtimeOptions,
OhosPkgArgs &pkgArgs, arg_list_t &pandaFileNames);
explicit CompilationOptions(EcmaVM *vm, JSRuntimeOptions &runtimeOptions);
std::string triple_;
std::string outputFileName_;
@ -64,9 +63,9 @@ struct CompilationOptions {
class CompilationPreprocessor {
public:
CompilationPreprocessor(EcmaVM *vm, JSRuntimeOptions &runtimeOptions, OhosPkgArgs &pkgArgs,
CompilationPreprocessor(EcmaVM *vm, JSRuntimeOptions &runtimeOptions, std::map<std::string, OhosPkgArgs> &pkgsArgs,
PGOProfilerDecoder &profilerDecoder, arg_list_t &pandaFileNames)
: vm_(vm), runtimeOptions_(runtimeOptions), pkgArgs_(pkgArgs),
: vm_(vm), runtimeOptions_(runtimeOptions), pkgsArgs_(pkgsArgs),
profilerDecoder_(profilerDecoder), pandaFileNames_(pandaFileNames) {};
~CompilationPreprocessor() = default;
@ -106,7 +105,7 @@ private:
EcmaVM *vm_;
JSRuntimeOptions &runtimeOptions_;
OhosPkgArgs &pkgArgs_;
std::map<std::string, OhosPkgArgs> &pkgsArgs_;
PGOProfilerDecoder &profilerDecoder_;
arg_list_t &pandaFileNames_;
CVector<AbcFileInfo> fileInfos_;

View File

@ -17,12 +17,14 @@
#define ECMASCRIPT_COMPILER_OHOS_PKG_ARGS_H
#include <limits>
#include <vector>
#include "ecmascript/ecma_vm.h"
#include "ecmascript/base/json_parser.h"
#include "ecmascript/js_array.h"
#include "ecmascript/js_handle.h"
#include "ecmascript/js_tagged_value.h"
#include "ecmascript/jspandafile/js_pandafile_manager.h"
#include "ecmascript/log_wrapper.h"
#include "ecmascript/mem/c_string.h"
#include "ecmascript/platform/file.h"
@ -39,7 +41,44 @@ public:
OhosPkgArgs() = default;
static bool ParseListFromJson(EcmaVM *vm, const std::string &jsonInfo, std::list<OhosPkgArgs> &infoList)
bool GetJSPandaFile(const JSRuntimeOptions &runtimeOptions, std::shared_ptr<JSPandaFile> &pf) const
{
std::string hapPath;
uint32_t offset {};
uint32_t size {};
if (Valid()) {
hapPath = GetPath();
offset = GetOffset();
size = GetSize();
} else {
// for legacy params
hapPath = runtimeOptions.GetHapPath();
offset = runtimeOptions.GetHapAbcOffset();
size = runtimeOptions.GetHapAbcSize();
}
if (size == 0) {
LOG_ECMA(ERROR) << "buffer is empty in target compiler mode!";
return false;
}
std::string realPath;
if (!RealPath(hapPath, realPath, false)) {
LOG_ECMA(ERROR) << "realpath for hap path failed!";
return false;
}
MemMap fileMapMem = FileMap(realPath.c_str(), FILE_RDONLY, PAGE_PROT_READ);
if (fileMapMem.GetOriginAddr() == nullptr) {
LOG_ECMA(ERROR) << "File mmap failed";
return false;
}
uint8_t *buffer = reinterpret_cast<uint8_t *>(fileMapMem.GetOriginAddr()) + offset;
JSPandaFileManager *jsPandaFileManager = JSPandaFileManager::GetInstance();
pf = jsPandaFileManager->OpenJSPandaFileFromBuffer(buffer, size, GetFullName().c_str());
FileUnMap(fileMapMem);
fileMapMem.Reset();
return true;
}
static bool ParseListFromJson(EcmaVM *vm, const std::string &jsonInfo, std::map<std::string, OhosPkgArgs> &argsMap)
{
LocalScope scope(vm);
ObjectFactory *factory = vm->GetFactory();
@ -67,7 +106,7 @@ public:
LOG_COMPILER(ERROR) << "Pkg list entry info parse failed. jsonData: " << jsonInfo.c_str();
return false;
}
infoList.emplace_back(pkgInfo);
argsMap[pkgInfo.GetFullName()] = pkgInfo;
}
return true;
}
@ -140,9 +179,9 @@ public:
void Dump() const
{
LOG_ECMA(INFO) << "PkgInfo: " << KEY_BUNDLE_NAME << ": " << bundleName_ << ", " << KEY_MODULE_NAME << ": "
<< moduleName_ << ", " << KEY_PKG_PATH << ": " << pkgPath_ << ", " << KEY_ABC_OFFSET << ": "
<< std::hex << abcOffset_ << ", " << KEY_ABC_SIZE << ": " << abcSize_;
LOG_COMPILER(INFO) << "PkgInfo: " << KEY_BUNDLE_NAME << ": " << bundleName_ << ", " << KEY_MODULE_NAME << ": "
<< moduleName_ << ", " << KEY_PKG_PATH << ": " << pkgPath_ << ", " << KEY_ABC_OFFSET << ": "
<< std::hex << abcOffset_ << ", " << KEY_ABC_SIZE << ": " << abcSize_;
}
const std::string &GetBundleName() const

View File

@ -147,6 +147,7 @@ const std::string PUBLIC_API HELP_OPTION_MSG =
"--compiler-opt-loop-peeling: Enable loop peeling for aot compiler: Default: 'false'\n"
"--compiler-pkg-info Specify the package json info for ark aot compiler\n"
"--compiler-external-pkg-info Specify the external package json info for ark aot compiler\n"
"--compiler-enable-external-pkg Enable compile with external package for ark aot compiler\n"
"--compiler-opt-array-onheap-check: Enable TypedArray on heap check for aot compiler: Default: 'false'\n\n";
bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv)
@ -230,6 +231,7 @@ bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv)
{"compiler-opt-array-onheap-check", required_argument, nullptr, OPTION_COMPILER_OPT_ON_HEAP_CHECK},
{"compiler-pkg-info", required_argument, nullptr, OPTION_COMPILER_PKG_INFO},
{"compiler-external-pkg-info", required_argument, nullptr, OPTION_COMPILER_EXTERNAL_PKG_INFO},
{"compiler-enable-external-pkg", required_argument, nullptr, OPTION_COMPILER_ENABLE_EXTERNAL_PKG},
{nullptr, 0, nullptr, 0},
};
@ -759,6 +761,14 @@ bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv)
case OPTION_COMPILER_EXTERNAL_PKG_INFO:
SetCompilerExternalPkgJsonInfo(optarg);
break;
case OPTION_COMPILER_ENABLE_EXTERNAL_PKG:
ret = ParseBoolParam(&argBool);
if (ret) {
SetCompilerEnableExternalPkg(argBool);
} else {
return false;
}
break;
default:
LOG_ECMA(ERROR) << "Invalid option\n";
return false;

View File

@ -140,6 +140,7 @@ enum CommandValues {
OPTION_COMPILER_OPT_ON_HEAP_CHECK,
OPTION_COMPILER_PKG_INFO,
OPTION_COMPILER_EXTERNAL_PKG_INFO,
OPTION_COMPILER_ENABLE_EXTERNAL_PKG,
OPTION_COMPILER_OPT_ARRAY_BOUNDS_CHECK_ELIMINATION,
OPTION_COMPILER_OPT_LOOP_INVARIANT_CODE_MOTION,
};
@ -214,6 +215,16 @@ public:
return compilerExternalPkgInfo_;
}
void SetCompilerEnableExternalPkg(bool compilerEnableExternalPkg)
{
compilerEnableExternalPkg_ = compilerEnableExternalPkg;
}
bool GetCompilerEnableExternalPkg() const
{
return compilerEnableExternalPkg_;
}
bool WasStubFileSet() const
{
return WasOptionSet(OPTION_STUB_FILE);
@ -1246,6 +1257,7 @@ private:
std::string stubFile_ {"stub.an"};
std::string compilerPkgInfo_ {};
std::string compilerExternalPkgInfo_ {};
bool compilerEnableExternalPkg_ {false};
bool enableForceGc_ {true};
bool forceFullGc_ {true};
int arkProperties_ = GetDefaultProperties();