From 3f553f4fcf3cf854e67c67fda2b9546c285e7381 Mon Sep 17 00:00:00 2001 From: nikozer Date: Wed, 11 Jun 2025 20:09:45 +0300 Subject: [PATCH] add resource logger (now without collecting phase from phase calls(recheck, rebind)) --- ets2panda/BUILD.gn | 1 + ets2panda/CMakeLists.txt | 1 + ets2panda/aot/main.cpp | 3 + ets2panda/compiler/core/compilerImpl.cpp | 9 ++- ets2panda/util/options.yaml | 5 ++ ets2panda/util/resourceLogger.cpp | 42 +++++++++++++ ets2panda/util/resourceLogger.h | 78 ++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 ets2panda/util/resourceLogger.cpp create mode 100644 ets2panda/util/resourceLogger.h diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index 42c9d7f055..6ac3893274 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -491,6 +491,7 @@ libes2panda_sources = [ "util/es2pandaMacros.cpp", "util/helpers.cpp", "util/importPathManager.cpp", + "util/resourceLogger.cpp", "util/path.cpp", "util/plugin.cpp", "util/ustring.cpp", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index 695f3a76f9..bd839b11fb 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -645,6 +645,7 @@ set(ES2PANDA_LIB_SRC util/es2pandaMacros.cpp util/helpers.cpp util/importPathManager.cpp + util/resourceLogger.cpp util/path.cpp util/ustring.cpp test/utils/panda_executable_path_getter.cpp diff --git a/ets2panda/aot/main.cpp b/ets2panda/aot/main.cpp index 411df8364a..5eea333388 100644 --- a/ets2panda/aot/main.cpp +++ b/ets2panda/aot/main.cpp @@ -24,6 +24,7 @@ #include "util/generateBin.h" #include "util/options.h" #include "util/plugin.h" +#include "util/resourceLogger.h" #include "libpandabase/os/stacktrace.h" #include "generated/diagnostic.h" @@ -70,6 +71,7 @@ static int CompileFromSource(es2panda::Compiler &compiler, es2panda::SourceFile return 1; } + util::ResourceLogger genResourcesLogger("gen stage"); return util::GenerateProgram( program.get(), options, [&diagnosticEngine](const diagnostic::DiagnosticKind &kind, const util::DiagnosticMessageParams ¶ms) { @@ -172,6 +174,7 @@ static int Run(Span args) mask.set(ark::Logger::Component::ES2PANDA); ark::Logger::InitializeStdLogging(options->LogLevel(), mask); util::DiagnosticEngine::InitializeSignalHandlers(); + util::ResourceLogger compilationResourcesLogger("full compilation", options->GetLogResourcesSpent(), true); auto pluginsOpt = InitializePlugins(options->GetPlugins(), diagnosticEngine); if (!pluginsOpt.has_value()) { diff --git a/ets2panda/compiler/core/compilerImpl.cpp b/ets2panda/compiler/core/compilerImpl.cpp index 7086b2707d..dd1c619d77 100644 --- a/ets2panda/compiler/core/compilerImpl.cpp +++ b/ets2panda/compiler/core/compilerImpl.cpp @@ -51,6 +51,7 @@ #include "varbinder/ASBinder.h" #include "varbinder/TSBinder.h" #include "varbinder/ETSBinder.h" +#include "util/resourceLogger.h" namespace ark::es2panda::compiler { @@ -201,6 +202,7 @@ static bool RunVerifierAndPhases(public_lib::Context &context, parser::Program & if (CheckIfPhaseToSkip(options, name)) { continue; } + util::ResourceLogger phaseResourcesLogger(phase->Name()); if (options.IsGenerateDeclEnableIsolated() && name == "plugins-after-check") { return false; @@ -419,12 +421,16 @@ static bool ExecuteParsingAndCompiling(const CompilationUnit &unit, public_lib:: AddExternalPrograms(context, unit, program); } - context->parser->ParseScript(unit.input, unit.options.GetCompilationMode() == CompilationMode::GEN_STD_LIB); + { + util::ResourceLogger parseResourcesLogger("parse stage"); + context->parser->ParseScript(unit.input, unit.options.GetCompilationMode() == CompilationMode::GEN_STD_LIB); + } // We have to check the return status of 'RunVerifierAndPhase` and 'RunPhases` separately because there can be // some internal errors (say, in Post-Conditional check) or terminate options (say in 'CheckOptionsAfterPhase') // that were not reported to the log. if (unit.ext == ScriptExtension::ETS) { + util::ResourceLogger allPhasesResourcesLogger("all compilation phases", true); if (!RunVerifierAndPhases(*context, *program)) { return false; } @@ -492,6 +498,7 @@ static pandasm::Program *Compile(const CompilationUnit &unit, CompilerImpl *comp } MarkAsLowered(program); + util::ResourceLogger emitResourcesLogger("emit stage"); return EmitProgram(compilerImpl, context, unit); } diff --git a/ets2panda/util/options.yaml b/ets2panda/util/options.yaml index 2a1894aeb7..1a392c78c8 100644 --- a/ets2panda/util/options.yaml +++ b/ets2panda/util/options.yaml @@ -378,3 +378,8 @@ options: type: bool default: false description: Place AST trees in permanent arena + +- name: log-resources-spent + type: std::string + default: "" + description: Log resource usage into file \ No newline at end of file diff --git a/ets2panda/util/resourceLogger.cpp b/ets2panda/util/resourceLogger.cpp new file mode 100644 index 0000000000..bec8d0226e --- /dev/null +++ b/ets2panda/util/resourceLogger.cpp @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "resourceLogger.h" +#include + +namespace ark::es2panda::util { + +std::ofstream ResourceLogger::out_; +bool ResourceLogger::isLoggerInited_ = false; + +ResourceLogger::~ResourceLogger() +{ + if (resourcePointName_.Empty() || !isLoggerEnabled_) { + return; + } + auto endTime = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(endTime - startTime_).count(); + auto rss = getrss() - startRss_; + std::stringstream ss; + if (addNewLine_) { + out_ << std::endl; + } + out_ << std::setw(50) << std::left << resourcePointName_ << std::right << std::setw(20) << duration << "ms " + << std::setw(20) << rss << "KB" << std::endl; + if (isInitLogger_) { + out_.close(); + } +} + +} // namespace ark::es2panda::util \ No newline at end of file diff --git a/ets2panda/util/resourceLogger.h b/ets2panda/util/resourceLogger.h new file mode 100644 index 0000000000..53a50bf526 --- /dev/null +++ b/ets2panda/util/resourceLogger.h @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef ES2PANDA_UTIL_REASOURCE_LOGGER_H +#define ES2PANDA_UTIL_REASOURCE_LOGGER_H + +#include "es2panda.h" +#include + +namespace ark::es2panda::util { + +class ResourceLogger { +public: + ResourceLogger(util::StringView resourcePointName, std::string filepath = "", bool addNewLine = false) + : addNewLine_(addNewLine), + resourcePointName_(resourcePointName), + startTime_(std::chrono::system_clock::now()), + startRss_(getrss()) + { + if (filepath.empty() && !isLoggerInited_) { + return; + } + if (!filepath.empty() && !isLoggerInited_) { + out_.open(filepath); + if (!out_.is_open()) { + std::cout << "log file open failed" << std::endl; + return; + } + isInitLogger_ = true; + isLoggerInited_ = true; + } + isLoggerEnabled_ = true; + } + + ResourceLogger(util::StringView resourcePointName, bool addNewLine) : ResourceLogger(resourcePointName) + { + addNewLine_ = addNewLine; + } + + NO_COPY_SEMANTIC(ResourceLogger); + NO_MOVE_SEMANTIC(ResourceLogger); + + static long getrss() + { + struct rusage usage; + if (getrusage(RUSAGE_SELF, &usage) != 0) { + abort(); + } + return usage.ru_maxrss; + } + + ~ResourceLogger(); + +private: + bool isInitLogger_ = false; + bool isLoggerEnabled_ = false; + bool addNewLine_ = false; + static bool isLoggerInited_; + static std::ofstream out_; + util::StringView resourcePointName_; + std::chrono::time_point startTime_; + long startRss_; +}; + +} // namespace ark::es2panda::util + +#endif // ES2PANDA_UTIL_REASOURCE_LOGGER_H \ No newline at end of file -- Gitee