From 39e2643cf58af56dfc8254cd377e8bf111c2b9d0 Mon Sep 17 00:00:00 2001 From: lhc Date: Tue, 12 Aug 2025 15:30:32 +0800 Subject: [PATCH] Deletion of unnecessary code Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICSKBE?from=project-issue Signed-off-by: lhc Change-Id: Idc1d74fbd3c9accf04bfaeb5339ace442873bc53 --- .../common_runtime/base/log_file.cpp | 308 ------------------ .../common_runtime/base/log_file.h | 283 ---------------- 2 files changed, 591 deletions(-) delete mode 100644 common_components/common_runtime/base/log_file.cpp delete mode 100755 common_components/common_runtime/base/log_file.h diff --git a/common_components/common_runtime/base/log_file.cpp b/common_components/common_runtime/base/log_file.cpp deleted file mode 100644 index 0a3f9b7cd7..0000000000 --- a/common_components/common_runtime/base/log_file.cpp +++ /dev/null @@ -1,308 +0,0 @@ -/* - * 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 "common_components/common_runtime/log_file.h" - -#include - -#include "common_components/base/sys_call.h" -#include "securec.h" - -namespace common { -LogFile::LogFileItem LogFile::logFile_[LOG_TYPE_NUMBER]; - -const char* LOG_TYPE_NAMES[LOG_TYPE_NUMBER] = { - "report", - - "debug", - - "alloc", "region", "fragment", - - "barrier", "ebarrier", "tbarrier", "pbarrier", "fbarrier", - - "gcphase", "enum", "marking", "preforward", "copy", "fix", "finalize", - - "unwind", "exception", "signal", - - "arkthread", -#ifdef ARKCOMMON_SANITIZER_SUPPORT - "sanitizer", -#endif -}; - -Level LogFile::logLevel_ = InitLogLevel(); - -void LogFile::Init() -{ - SetFlags(); -#ifndef NDEBUG - OpenLogFiles(); -#endif -} - -void LogFile::Fini() { CloseLogFiles(); } - -void LogFile::SetFlags() -{ -#ifndef NDEBUG - logFile_[REPORT].enableLog = ARK_ENABLED_LOG(ARK_REPORT); - logFile_[ALLOC].enableLog = ARK_ENABLED_LOG(ARK_LOG_ALLOC); - logFile_[REGION].enableLog = ARK_ENABLED_LOG(ARK_LOG_REGION); - logFile_[FRAGMENT].enableLog = ARK_ENABLED_LOG(ARK_LOG_FRAGMENT); - logFile_[DEBUGY].enableLog = ARK_ENABLED_LOG(ARK_LOG_DEBUG); - logFile_[BARRIER].enableLog = ARK_ENABLED_LOG(ARK_LOG_BARRIER); - logFile_[EBARRIER].enableLog = ARK_ENABLED_LOG(ARK_LOG_EBARRIER); - logFile_[TBARRIER].enableLog = ARK_ENABLED_LOG(ARK_LOG_TBARRIER); - logFile_[PBARRIER].enableLog = ARK_ENABLED_LOG(ARK_LOG_PBARRIER); - logFile_[FBARRIER].enableLog = ARK_ENABLED_LOG(ARK_LOG_FBARRIER); - logFile_[GCPHASE].enableLog = ARK_ENABLED_LOG(ARK_LOG_COMMONPHASE); - logFile_[ENUM].enableLog = ARK_ENABLED_LOG(ARK_LOG_ENUM); - logFile_[TRACE].enableLog = ARK_ENABLED_LOG(ARK_LOG_TRACE); - logFile_[PREFORWARD].enableLog = ARK_ENABLED_LOG(ARK_LOG_PREFORWARD); - logFile_[COPY].enableLog = ARK_ENABLED_LOG(ARK_LOG_COPY); - logFile_[FIX].enableLog = ARK_ENABLED_LOG(ARK_LOG_FIX); - logFile_[FINALIZE].enableLog = ARK_ENABLED_LOG(ARK_LOG_FINALIZE); - logFile_[UNWIND].enableLog = ARK_ENABLED_LOG(ARK_LOG_UNWIND); - logFile_[EXCEPTION].enableLog = ARK_ENABLED_LOG(ARK_LOG_EXCEPTION); - logFile_[SIGNAL].enableLog = ARK_ENABLED_LOG(ARK_LOG_SIGNAL); - logFile_[ARKTHREAD].enableLog = ARK_ENABLED_LOG(ARK_LOG_THREAD); -#ifdef ARKCOMMON_SANITIZER_SUPPORT - logFile_[SANITIZER].enableLog = ARK_ENABLED_LOG(ARK_LOG_SANITIZER); -#endif -#endif -} - -#ifndef NDEBUG -void LogFile::OpenLogFiles() -{ - CString pid = CString(GetPid()); - CString dateDigit = ""; - CString dirName = "."; - - for (int i = 0; i < LOG_TYPE_NUMBER; ++i) { - if (logFile_[i].enableLog) { - if (GetEnv("ARK_LOG_STDOUT", 0) == 1) { - logFile_[i].file = fopen("/dev/stdout", "w"); - if (logFile_[i].file == nullptr) { - LOG_COMMON(ERROR) << "LogFile::OpenLogFiles(): fail to set file /dev/stdout"; - continue; - } - } else { - CString fileName = dirName + "/" + dateDigit + "_" + pid + "." + LOG_TYPE_NAMES[i] + ".txt"; - LOG_COMMON(INFO) << "create log file " << fileName.Str(); - logFile_[i].file = fopen(fileName.Str(), "a+"); // Assignment closes the old file. - if (logFile_[i].file == nullptr) { - LOG_COMMON(ERROR) << "LogFile::OpenLogFiles(): fail to open the file"; - continue; - } - } - } - } -} -#endif - -void LogFile::CloseLogFiles() -{ - for (int i = 0; i < LOG_TYPE_NUMBER; ++i) { - if (logFile_[i].enableLog) { - logFile_[i].enableLog = false; - fclose(logFile_[i].file); - } - } -} - -static bool MaybeRotate(size_t curPos, size_t maxSize, FILE* file) -{ - if (curPos < maxSize) { - return false; - } - (void)fflush(file); - (void)ftruncate(fileno(file), ftell(file)); - rewind(file); - return true; -} - - -static void WriteLogImpl(bool addPrefix, LogType type, const char* format, va_list& args) -{ - char buf[LOG_BUFFER_SIZE]; - if (!LogFile::LogIsEnabled(type)) { - return; - } - int index = 0; - if (addPrefix) { - index = sprintf_s(buf, sizeof(buf), "%s %d ", TimeUtil::GetTimestamp().Str(), GetTid()); - if (index == -1) { - LOG_COMMON(ERROR) << "WriteLogImpl sprintf_s failed. msg: " << strerror(errno); - return; - } - } - - int ret = vsprintf_s(buf + index, sizeof(buf) - index, format, args); - if (ret == -1) { - LOG_COMMON(ERROR) << "WriteLogImpl vsprintf_s failed. msg: " << strerror(errno); - return; - } - index += ret; - - LogFile::LogFileLock(type); -#ifdef PANDA_TARGET_OHOS - auto env = CString(std::getenv("ARK_REPORT")); - if (env.Str() != nullptr) { -#endif - FILE* file = LogFile::GetFile(type); - if (file == nullptr) { - LOG_COMMON(ERROR) << "WriteLog failed. ARK_REPORT is not a valid path. Please check again."; - LogFile::LogFileUnLock(type); - return; - } - int err = fprintf(file, "%s\n", buf); - if ((err - 1) != index) { // 1 = '\n' - LOG_COMMON(ERROR) << "WriteLogImpl fprintf failed. msg: %s\n", strerror(errno); - LogFile::LogFileUnLock(type); - return; - } -#ifdef PANDA_TARGET_OHOS - fflush(file); - } -#else -#ifdef NDEBUG - size_t curPos = LogFile::GetCurPosLocation(type); - LogFile::SetCurPosLocation(type, curPos + index); - if (MaybeRotate(curPos + index, LogFile::GetMaxFileSize(type), file)) { - LogFile::SetCurPosLocation(type, 0); - } -#endif - fflush(file); -#endif - LogFile::LogFileUnLock(type); -} - -void WriteLog(bool addPrefix, LogType type, const char* format, ...) noexcept -{ - va_list args; - va_start(args, format); - WriteLogImpl(addPrefix, type, format, args); - va_end(args); -} - -// Orders of magnitudes. Note: The upperbound of uint64_t is 16E (16 * (1024 ^ 6)) -const char* g_orderOfMagnitude[] = { "", "K", "M", "G", "T", "P", "E" }; - -// Orders of magnitudes. Note: The upperbound of uint64_t is 16E (16 * (1024 ^ 6)) -const char* g_orderOfMagnitudeFromNano[] = { "n", "u", "m", nullptr }; - -// number of digits in a pretty format segment (100,000,000 each has three digits) -constexpr int NUM_DIGITS_PER_SEGMENT = 3; - -CString Pretty(uint64_t number) noexcept -{ - CString orig = CString(number); - int pos = static_cast(orig.Length()) - NUM_DIGITS_PER_SEGMENT; - while (pos > 0) { - orig.Insert(pos, ","); - pos -= NUM_DIGITS_PER_SEGMENT; - } - return orig; -} - -// Useful for informatic units, such as KiB, MiB, GiB, ... -CString PrettyOrderInfo(uint64_t number, const char* unit) -{ - size_t order = 0; - const uint64_t factor = 1024; - - while (number > factor) { - number /= factor; - order += 1; - } - - const char* prefix = g_orderOfMagnitude[order]; - const char* infix = order > 0 ? "i" : ""; // 1KiB = 1024B, but there is no "1iB" - - return CString(number) + prefix + infix + unit; -} - -// Useful for scientific units where number is in nanos: ns, us, ms, s -CString PrettyOrderMathNano(uint64_t number, const char* unit) -{ - size_t order = 0; - const uint64_t factor = 1000; // show in us if under 10ms - - while (number > factor && g_orderOfMagnitudeFromNano[order] != nullptr) { - number /= factor; - order += 1; - } - - const char* prefix = g_orderOfMagnitudeFromNano[order]; - if (prefix == nullptr) { - prefix = ""; - } - - return CString(number) + prefix + unit; -} - -#ifndef NDEBUG -long GetEnv(const char* envName, long defaultValue) -{ - const char* ev = getenv(envName); - if (ev != nullptr) { - char* endptr = nullptr; - long rv = std::strtol(ev, &endptr, 0); // support dec, oct and hex - if (*endptr == '\0') { - return rv; - } - } - - return defaultValue; -} -#endif - -Level InitLogLevel() -{ - auto env = CString(std::getenv("ARK_LOG_LEVEL")); - if (env.Str() == nullptr) { - return Level::ERROR; - } - - CString logLevel = env.RemoveBlankSpace(); - if (logLevel.Length() != 1) { - LOG_COMMON(ERROR) << "Unsupported in ARK_LOG_LEVEL length. Valid length must be 1." - " Valid ARK_LOG_LEVEL must be in ['v', 'd', 'i', 'w', 'e', 'f' 's']."; - return Level::ERROR; - } - - switch (logLevel.Str()[0]) { - case 'v': - return Level::VERBOSE; - case 'd': - return Level::DEBUG; - case 'i': - return Level::INFO; - case 'w': - return Level::WARN; - case 'e': - return Level::ERROR; - case 'f': - return Level::FATAL; - case 's': - return Level::FATAL_WITHOUT_ABORT; - default: - LOG_COMMON(ERROR) << "Unsupported in ARK_LOG_LEVEL. Valid ARK_LOG_LEVEL must be in" - "['v', 'd', 'i', 'w', 'e', 'f' 's'].\n"; - } - return Level::ERROR; -} -} // namespace common diff --git a/common_components/common_runtime/base/log_file.h b/common_components/common_runtime/base/log_file.h deleted file mode 100755 index d20583d740..0000000000 --- a/common_components/common_runtime/base/log_file.h +++ /dev/null @@ -1,283 +0,0 @@ -/* - * 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 ARK_COMMON_LOG_FILE_H -#define ARK_COMMON_LOG_FILE_H - -#include - -#include "common_components/base/time_utils.h" - - -namespace common { -enum LogType { - // for overall brief report - REPORT = 0, - - // for debug purpose - DEBUGY, - - // for allocator - ALLOC, - REGION, - FRAGMENT, - - // for barriers - BARRIER, // idle phase - EBARRIER, // enum phase - TBARRIER, // marking phase - PBARRIER, // preforward phase - FBARRIER, // copy phase - - // for gc - GCPHASE, - ENUM, - TRACE, - PREFORWARD, - COPY, - FIX, - FINALIZE, - - UNWIND, - EXCEPTION, - SIGNAL, - - ARKTHREAD, - -#ifdef ARKCOMMON_SANITIZER_SUPPORT - SANITIZER, -#endif - - LOG_TYPE_NUMBER -}; - -#ifndef DEFAULT_ARK_REPORT -#define DEFAULT_ARK_REPORT 0 -#endif // DEFAULT_ARK_REPORT - -#ifndef DEFAULT_ARK_LOG_ALLOC -#define DEFAULT_ARK_LOG_ALLOC 0 -#endif // DEFAULT_ARK_LOG_ALLOC - -#ifndef DEFAULT_ARK_LOG_REGION -#define DEFAULT_ARK_LOG_REGION 0 -#endif // DEFAULT_ARK_LOG_REGION - -#ifndef DEFAULT_ARK_LOG_FRAGMENT -#define DEFAULT_ARK_LOG_FRAGMENT 0 -#endif // DEFAULT_ARK_LOG_FRAGMENT - -#ifndef DEFAULT_ARK_LOG_DEBUG -#define DEFAULT_ARK_LOG_DEBUG 0 -#endif // DEFAULT_ARK_LOG_DEBUG - -#ifndef DEFAULT_ARK_LOG_BARRIER -#define DEFAULT_ARK_LOG_BARRIER 0 -#endif // DEFAULT_ARK_LOG_BARRIER - -#ifndef DEFAULT_ARK_LOG_EBARRIER -#define DEFAULT_ARK_LOG_EBARRIER 0 -#endif // DEFAULT_ARK_LOG_EBARRIER - -#ifndef DEFAULT_ARK_LOG_TBARRIER -#define DEFAULT_ARK_LOG_TBARRIER 0 -#endif // DEFAULT_ARK_LOG_TBARRIER - -#ifndef DEFAULT_ARK_LOG_PBARRIER -#define DEFAULT_ARK_LOG_PBARRIER 0 -#endif // DEFAULT_ARK_LOG_PBARRIER - -#ifndef DEFAULT_ARK_LOG_FBARRIER -#define DEFAULT_ARK_LOG_FBARRIER 0 -#endif // DEFAULT_ARK_LOG_FBARRIER - -#ifndef DEFAULT_ARK_LOG_COMMONPHASE -#define DEFAULT_ARK_LOG_COMMONPHASE 0 -#endif // DEFAULT_ARK_LOG_COMMONPHASE - -#ifndef DEFAULT_ARK_LOG_ENUM -#define DEFAULT_ARK_LOG_ENUM 0 -#endif // DEFAULT_ARK_LOG_ENUM - -#ifndef DEFAULT_ARK_LOG_TRACE -#define DEFAULT_ARK_LOG_TRACE 0 -#endif // DEFAULT_ARK_LOG_TRACE - -#ifndef DEFAULT_ARK_LOG_PREFORWARD -#define DEFAULT_ARK_LOG_PREFORWARD 0 -#endif // DEFAULT_ARK_LOG_PREFORWARD - -#ifndef DEFAULT_ARK_LOG_COPY -#define DEFAULT_ARK_LOG_COPY 0 -#endif // DEFAULT_ARK_LOG_COPY - -#ifndef DEFAULT_ARK_LOG_FIX -#define DEFAULT_ARK_LOG_FIX 0 -#endif // DEFAULT_ARK_LOG_FIX - -#ifndef DEFAULT_ARK_LOG_FINALIZE -#define DEFAULT_ARK_LOG_FINALIZE 0 -#endif // DEFAULT_ARK_LOG_FINALIZE - -#ifndef DEFAULT_ARK_LOG_UNWIND -#define DEFAULT_ARK_LOG_UNWIND 0 -#endif // DEFAULT_ARK_LOG_UNWIND - -#ifndef DEFAULT_ARK_LOG_EXCEPTION -#define DEFAULT_ARK_LOG_EXCEPTION 0 -#endif // DEFAULT_ARK_LOG_EXCEPTION - -#ifndef DEFAULT_ARK_LOG_SIGNAL -#define DEFAULT_ARK_LOG_SIGNAL 0 -#endif // DEFAULT_ARK_LOG_SIGNAL - -#ifndef DEFAULT_ARK_LOG_THREAD -#define DEFAULT_ARK_LOG_THREAD 0 -#endif // DEFAULT_ARK_LOG_THREAD - -#ifndef DEFAULT_ARK_LOG2STDOUT -#define DEFAULT_ARK_LOG2STDOUT 0 -#endif // DEFAULT_ARK_LOG2STDOUT - -#ifdef ARKCOMMON_SANITIZER_SUPPORT -#ifndef DEFAULT_ARK_LOG_SANITIZER -#define DEFAULT_ARK_LOG_SANITIZER 0 -#endif // DEFAULT_ARK_LOG_SANITIZER -#endif - -#ifndef NDEBUG -long GetEnv(const char* envName, long defaultValue); // do not use directly - -// Use this macro to get environment variable for log. -// For example: ARK_ENABLED_LOG(ARK_REPORT) -// Will first check if an environment variable "ARK_REPORT" is present, -// and is a valid integer, and use its value. If not present or not a valid -// integer, it will fall back to the default value of the ARK_REPORT -// macro. This lets the user override configuration at run time, which is useful -// for debugging. -#define ARK_ENABLED_LOG(conf) (panda::GetEnv(#conf, DEFAULT_##conf) == 1) -#else -#define ARK_ENABLED_LOG(conf) (0) -#endif - -CString Pretty(uint64_t number) noexcept; -CString PrettyOrderInfo(uint64_t number, const char* unit); -CString PrettyOrderMathNano(uint64_t number, const char* unit); -Level InitLogLevel(); - -void WriteLog(bool addPrefix, LogType type, const char* format, ...) noexcept; - -#define ENABLE_LOG(type) LogFile::LogIsEnabled(type) - -#ifndef NDEBUG -#define DLOG(type, format...) \ - if (LogFile::LogIsEnabled(type)) { \ - WriteLog(true, type, format); \ - } -#define VLOG(type, format...) \ - if (LogFile::LogIsEnabled(type)) { \ - WriteLog(true, type, format); \ - } -#else -#define DLOG(type, format...) (void)(0) -#define VLOG(type, format...) \ - if (LogFile::LogIsEnabled(type)) { \ - WriteLog(true, type, format); \ - } -#endif - -constexpr size_t DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; -constexpr size_t LOG_BUFFER_SIZE = 1024; - -class LogFile { -public: - LogFile() = default; - ~LogFile() = default; - static void Init(); - static void Fini(); - - struct LogFileItem { - bool enableLog = false; - std::mutex fileMutex; - FILE* file = nullptr; - size_t maxFileSize = DEFAULT_MAX_FILE_SIZE; - size_t curPosLocation = 0; - }; - - static FILE* GetFile(LogType type) { return logFile_[type].file; } - - static void LogFileLock(LogType type) { logFile_[type].fileMutex.lock(); } - - static void LogFileUnLock(LogType type) { logFile_[type].fileMutex.unlock(); } - - static bool LogIsEnabled(LogType type) noexcept - { -#ifdef PANDA_TARGET_OHOS - if (type == REPORT) { - return true; - } -#endif - return logFile_[type].enableLog; - } - - static void EnableLog(LogType type, bool key) { logFile_[type].enableLog = key; } - - static size_t GetMaxFileSize(LogType type) { return logFile_[type].maxFileSize; } - - static size_t GetCurPosLocation(LogType type) { return logFile_[type].curPosLocation; } - - static void SetCurPosLocation(LogType type, size_t curPos) { logFile_[type].curPosLocation = curPos; } - - static Level GetLogLevel() { return logLevel_; } - -private: -#ifndef NDEBUG - static void OpenLogFiles(); -#endif - static void CloseLogFiles(); - - static void SetFlags(); - static LogFileItem logFile_[LOG_TYPE_NUMBER]; - - static Level logLevel_; -}; - -#define ARK_COMMON_PHASE_TIMER(...) Timer ARK_pt_##__LINE__(__VA_ARGS__) - -class Timer { -public: - explicit Timer(const CString& pName, LogType type = REPORT) : name_(pName), logType_(type) - { - if (ENABLE_LOG(type)) { - startTime_ = TimeUtil::MicroSeconds(); - } - } - - ~Timer() - { - if (ENABLE_LOG(logType_)) { - uint64_t stopTime = TimeUtil::MicroSeconds(); - uint64_t diffTime = stopTime - startTime_; - WriteLog(true, logType_, "%s time: %sus", name_.Str(), Pretty(diffTime).Str()); - } - } - -private: - CString name_; - uint64_t startTime_ = 0; - LogType logType_; -}; -} // namespace common -#endif // ARK_COMMON_LOG_FILE_H -- Gitee