From 4f32cd8393ec0c771e57f4e4295137a9dd677dbb Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 24 Mar 2023 06:42:10 +0000 Subject: [PATCH] Refactor launchpad config The launchpad config is implemented using C++ language. Change-Id: I0b4e01abed0e8b07fc2763b79362312177064959 Signed-off-by: Hwankyu Jhun --- src/launchpad-process-pool/config.cc | 167 ++++++++++++++ src/launchpad-process-pool/config.hh | 106 +++++++++ src/launchpad-process-pool/launchpad.cc | 67 ++---- src/launchpad-process-pool/launchpad_config.cc | 303 ------------------------- src/launchpad-process-pool/launchpad_config.h | 48 ---- src/launchpad-process-pool/log.cc | 6 +- src/launchpad-process-pool/logger.cc | 6 +- src/launchpad-process-pool/memory_monitor.cc | 7 +- src/lib/launchpad-common/CMakeLists.txt | 1 + src/lib/launchpad-common/ini_parser.cc | 43 ++++ src/lib/launchpad-common/ini_parser.hh | 42 ++++ 11 files changed, 392 insertions(+), 404 deletions(-) create mode 100644 src/launchpad-process-pool/config.cc create mode 100644 src/launchpad-process-pool/config.hh delete mode 100644 src/launchpad-process-pool/launchpad_config.cc delete mode 100644 src/launchpad-process-pool/launchpad_config.h create mode 100644 src/lib/launchpad-common/ini_parser.cc create mode 100644 src/lib/launchpad-common/ini_parser.hh diff --git a/src/launchpad-process-pool/config.cc b/src/launchpad-process-pool/config.cc new file mode 100644 index 0000000..1d5b2a0 --- /dev/null +++ b/src/launchpad-process-pool/config.cc @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "launchpad-process-pool/config.hh" + +#include + +#include "launchpad-process-pool/log_private.hh" + +namespace launchpad { +namespace { + +constexpr const char kPathLaunchpadConf[] = "/usr/share/aul/launchpad.conf"; + +constexpr const char kTagMemoryStatus[] = "MemoryStatus"; +constexpr const char kKeyMemoryStatusLowKey[] = "LowKey"; +constexpr const char kKeyMemoryStatusLowValue[] = "LowValue"; +constexpr const char kKeyMemoryStatusNormalKey[] = "NormalKey"; +constexpr const char kKeyMemoryStatusNormalValue[] = "NormalValue"; + +constexpr const char kTagMemoryMonitor[] = "MemoryMonitor"; +constexpr const char kKeyMemoryMonitorThreshold[] = "Threshold"; +constexpr const char kKeyMemoryMonitorInterval[] = "Interval"; + +constexpr const char kTagCpuChecker[] = "CpuChecker"; +constexpr const char kKeyCpuCheckerMaxCount[] = "MaxCount"; + +constexpr const char kTagLogger[] = "Logger"; +constexpr const char kKeyLoggerPath[] = "Path"; +constexpr const char kKeyLoggerEnable[] = "Enable"; + +} // namespace + +const std::string& Config::MemoryStatus::GetLowKey() const { + return low_key_; +} + +const int Config::MemoryStatus::GetLowValue() const { + return low_value_; +} + +const std::string& Config::MemoryStatus::GetNormalKey() const { + return normal_key_; +} + +const int Config::MemoryStatus::GetNormalValue() const { + return normal_value_; +} + +Config::MemoryStatus::MemoryStatus(const IniParser& parser) { + auto low_key = parser.Get(kTagMemoryStatus, kKeyMemoryStatusLowKey); + if (!low_key.empty()) + low_key_ = std::move(low_key); + + auto low_value = parser.Get(kTagMemoryStatus, kKeyMemoryStatusLowValue); + if (!low_value.empty() && std::isdigit(low_value[0])) + low_value_ = std::stoi(low_value); + + auto normal_key = parser.Get(kTagMemoryStatus, kKeyMemoryStatusNormalKey); + if (!normal_key.empty()) + normal_key_ = std::move(normal_key); + + auto normal_value = parser.Get(kTagMemoryStatus, + kKeyMemoryStatusNormalValue); + if (!normal_value.empty() && std::isdigit(normal_value[0])) + normal_value_ = std::stoi(normal_value); + + _W("[MemoryStatus] Low: %s:%d, Normal: %s:%d", + low_key_.c_str(), low_value_, normal_key_.c_str(), normal_value_); +} + +const int Config::MemoryMonitor::GetThreshold() const { + return threshold_; +} + +const int Config::MemoryMonitor::GetInterval() const { + return interval_; +} + +Config::MemoryMonitor::MemoryMonitor(const IniParser& parser) { + auto threshold = parser.Get(kTagMemoryMonitor, kKeyMemoryMonitorThreshold); + if (!threshold.empty() && std::isdigit(threshold[0])) + threshold_ = std::stoi(threshold); + + auto interval = parser.Get(kTagMemoryMonitor, kKeyMemoryMonitorInterval); + if (!interval.empty() && std::isdigit(interval[0])) + interval_ = std::stoi(interval); + + _W("[MemoryMonitor] thrshold: %d, interval: %d", threshold_, interval_); +} + +const int Config::CPUChecker::GetMaxCount() const { + return max_count_; +} + +Config::CPUChecker::CPUChecker(const IniParser& parser) { + auto max_count = parser.Get(kTagCpuChecker, kKeyCpuCheckerMaxCount); + if (!max_count.empty() && std::isdigit(max_count[0])) + max_count_ = std::stoi(max_count); + + _W("[CPUChecker] max_count: %d", max_count_); +} + +const std::string& Config::Logger::GetPath() const { + return path_; +} + +const bool Config::Logger::IsEnabled() const { + return enable_; +} + +Config::Logger::Logger(const IniParser& parser) { + auto path = parser.Get(kTagLogger, kKeyLoggerPath); + if (!path.empty()) + path_ = std::move(path); + + auto enable = parser.Get(kTagLogger, kKeyLoggerEnable); + if (!enable.empty() && std::isdigit(enable[0])) + enable_ = std::stoi(enable) ? true : false; + + _W("[Logger] path: %s, enable: %s", + path_.c_str(), enable_ ? "true" : "false"); +} + +Config& Config::GetInst() { + static Config inst; + return inst; +} + +Config::Config() + : parser_(IniParser(kPathLaunchpadConf)), + memory_status_(Config::MemoryStatus(parser_)), + memory_monitor_(Config::MemoryMonitor(parser_)), + cpu_checker_(Config::CPUChecker(parser_)), + logger_(Config::Logger(parser_)) { +} + +const Config::MemoryStatus& Config::GetMemoryStatus() const { + return memory_status_; +} + +const Config::MemoryMonitor& Config::GetMemoryMonitor() const { + return memory_monitor_; +} + +const Config::CPUChecker& Config::GetCPUChecker() const { + return cpu_checker_; +} + +const Config::Logger& Config::GetLogger() const { + return logger_; +} + +} // namespace launchpad diff --git a/src/launchpad-process-pool/config.hh b/src/launchpad-process-pool/config.hh new file mode 100644 index 0000000..4a5f1f6 --- /dev/null +++ b/src/launchpad-process-pool/config.hh @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 LAUNCHPAD_PROCESS_POOL_CONFIG_HH_ +#define LAUNCHPAD_PROCESS_POOL_CONFIG_HH_ + +#include + +#include + +#include + +namespace launchpad { + +class Config { + public: + class MemoryStatus { + public: + explicit MemoryStatus(const IniParser& parser); + + const std::string& GetLowKey() const; + const int GetLowValue() const; + const std::string& GetNormalKey() const; + const int GetNormalValue() const; + + private: + std::string low_key_ = VCONFKEY_SYSMAN_LOW_MEMORY; + int low_value_ = VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING; + std::string normal_key_ = VCONFKEY_SYSMAN_LOW_MEMORY; + int normal_value_ = VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL; + }; + + class MemoryMonitor { + public: + explicit MemoryMonitor(const IniParser& parser); + + const int GetThreshold() const; + const int GetInterval() const; + + private: + int threshold_ = 80; + int interval_ = 5000; + }; + + class CPUChecker { + public: + explicit CPUChecker(const IniParser& parser); + + const int GetMaxCount() const; + + private: + int max_count_ = 10; + }; + + class Logger { + public: + explicit Logger(const IniParser& parser); + + const std::string& GetPath() const; + const bool IsEnabled() const; + + private: + std::string path_ = "/var/log/appfw"; + bool enable_ = false; + }; + + Config(const Config&) = delete; + Config& operator = (const Config&) = delete; + Config(Config&&) = delete; + Config& operator = (Config&&) = delete; + + static Config& GetInst(); + + const MemoryStatus& GetMemoryStatus() const; + const MemoryMonitor& GetMemoryMonitor() const; + const CPUChecker& GetCPUChecker() const; + const Logger& GetLogger() const; + + private: + Config(); + ~Config() = default; + + private: + IniParser parser_; + MemoryStatus memory_status_; + MemoryMonitor memory_monitor_; + CPUChecker cpu_checker_; + Logger logger_; +}; + +} // namespace launchpad + +#endif // LAUNCHPAD_PROCESS_POOL_CONFIG_HH_ diff --git a/src/launchpad-process-pool/launchpad.cc b/src/launchpad-process-pool/launchpad.cc index 1acda44..e814ef4 100644 --- a/src/launchpad-process-pool/launchpad.cc +++ b/src/launchpad-process-pool/launchpad.cc @@ -44,7 +44,6 @@ #include #include "launchpad-process-pool/launcher_info.hh" -#include "launchpad-process-pool/launchpad_config.h" #include "launchpad-process-pool/launchpad_inotify.h" #include "launchpad-process-pool/launchpad_io_channel.h" #include "launchpad-process-pool/slot_info.h" @@ -54,6 +53,7 @@ #include "lib/common/inc/launchpad_types.h" #include "lib/common/inc/perf.h" +#include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/debug.hh" #include "launchpad-process-pool/dbus.hh" #include "launchpad-process-pool/loader_info.hh" @@ -269,10 +269,6 @@ GHashTable* __pid_table; int __memory_status_low; int __memory_status_normal; sequencer __sequencer; -int MEMORY_STATUS_LOW; -int MEMORY_STATUS_NORMAL; -int MAX_CPU_CHECK_COUNT; - io_channel_h __logger_channel; io_channel_h __label_monitor_channel; io_channel_h __launchpad_channel; @@ -1700,7 +1696,8 @@ static gboolean __handle_idle_checker(gpointer data) { __update_threshold(cpc, 0.05f); cpc->cpu_check_count++; - if (cpc->cpu_check_count == MAX_CPU_CHECK_COUNT) { + if (cpc->cpu_check_count == + launchpad::Config::GetInst().GetCPUChecker().GetMaxCount()) { _W("CPU check count has exceeded %d times. loader(%s:%d)", cpc->cpu_check_count, cpc->loader_name, cpc->type); __sequencer.idle_checker = 0; @@ -2950,7 +2947,9 @@ static bool __is_low_memory(void) { if (MemoryManager::GetInst().IsLowMemory()) return true; - if (__memory_status_low >= MEMORY_STATUS_LOW) + + if (__memory_status_low >= + launchpad::Config::GetInst().GetMemoryStatus().GetLowValue()) return true; return false; @@ -2990,7 +2989,8 @@ static void __memory_status_low_changed_cb(keynode_t* node, void* data) { GList* iter; __memory_status_low = vconf_keynode_get_int(node); - if (__memory_status_low >= MEMORY_STATUS_LOW) { + if (__memory_status_low >= + launchpad::Config::GetInst().GetMemoryStatus().GetLowValue()) { _W("Low memory"); iter = candidate_slot_list; while (iter) { @@ -3006,7 +3006,8 @@ static void __memory_status_normal_changed_cb(keynode_t* node, void* data) { GList* iter; __memory_status_normal = vconf_keynode_get_int(node); - if (__memory_status_normal == MEMORY_STATUS_NORMAL) { + if (__memory_status_normal == + launchpad::Config::GetInst().GetMemoryStatus().GetNormalValue()) { _W("Normal"); iter = candidate_slot_list; while (iter) { @@ -3018,17 +3019,11 @@ static void __memory_status_normal_changed_cb(keynode_t* node, void* data) { } static void __unregister_vconf_events(void) { - const char* key; - config_type_e type; - - type = CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY; - key = _config_get_string_value(type); - vconf_ignore_key_changed(key, __memory_status_normal_changed_cb); - - type = CONFIG_TYPE_MEMORY_STATUS_LOW_KEY; - key = _config_get_string_value(type); - vconf_ignore_key_changed(key, __memory_status_low_changed_cb); - + auto& config = launchpad::Config::GetInst(); + vconf_ignore_key_changed(config.GetMemoryStatus().GetNormalKey().c_str(), + __memory_status_normal_changed_cb); + vconf_ignore_key_changed(config.GetMemoryStatus().GetLowKey().c_str(), + __memory_status_low_changed_cb); vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __region_format_changed_cb); vconf_ignore_key_changed(VCONFKEY_LANGSET, __update_lang); vconf_ignore_key_changed(VCONFKEY_SETAPPL_APP_HW_ACCELERATION, @@ -3039,8 +3034,6 @@ static int __register_vconf_events(void) { int r; char* lang; char* region; - const char* key; - config_type_e type; r = vconf_get_int(VCONFKEY_SETAPPL_APP_HW_ACCELERATION, &__sys_hwacc); if (r != VCONF_OK) @@ -3073,29 +3066,24 @@ static int __register_vconf_events(void) { if (r != VCONF_OK) _E("Failed to register callback for regionformat. err = %d", r); - type = CONFIG_TYPE_MEMORY_STATUS_LOW_KEY; - key = _config_get_string_value(type); - type = CONFIG_TYPE_MEMORY_STATUS_LOW_VALUE; - MEMORY_STATUS_LOW = _config_get_int_value(type); - - r = vconf_get_int(key, &__memory_status_low); + auto& config = launchpad::Config::GetInst(); + r = vconf_get_int(config.GetMemoryStatus().GetLowKey().c_str(), + &__memory_status_low); if (r != VCONF_OK) _E("Failed to get vconf low memory. err = %d", r); - r = vconf_notify_key_changed(key, __memory_status_low_changed_cb, nullptr); + r = vconf_notify_key_changed(config.GetMemoryStatus().GetLowKey().c_str(), + __memory_status_low_changed_cb, nullptr); if (r != 0) _E("Failed to register callback for low memory. err = %d", r); - type = CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY; - key = _config_get_string_value(type); - type = CONFIG_TYPE_MEMORY_STATUS_NORMAL_VALUE; - MEMORY_STATUS_NORMAL = _config_get_int_value(type); - - r = vconf_get_int(key, &__memory_status_normal); + r = vconf_get_int(config.GetMemoryStatus().GetNormalKey().c_str(), + &__memory_status_normal); if (r != VCONF_OK) _E("Failed to get vconf normal memory. err = %d", r); - r = vconf_notify_key_changed(key, __memory_status_normal_changed_cb, nullptr); + r = vconf_notify_key_changed(config.GetMemoryStatus().GetNormalKey().c_str(), + __memory_status_normal_changed_cb, nullptr); if (r != 0) _E("Failed to register callback for normal memory. err = %d", r); @@ -3253,15 +3241,9 @@ static int __before_loop(int argc, char** argv) { if (ret != 0) _W("Failed to initialize label monitor"); - ret = _config_init(); - if (ret != 0) - _W("Failed to initialize config"); _inotify_init(); MemoryManager::GetInst().Init(); - - MAX_CPU_CHECK_COUNT = - _config_get_int_value(CONFIG_TYPE_CPU_CHECKER_MAX_COUNT); __add_default_slots(); launchpad::LauncherInfoInflator inflator; @@ -3303,7 +3285,6 @@ static void __after_loop(void) { launchpad::Debug::GetInst().Dispose(); launcher_info_list.clear(); - _config_fini(); _inotify_fini(); app_defined_loader_info_manager->Dispose(); diff --git a/src/launchpad-process-pool/launchpad_config.cc b/src/launchpad-process-pool/launchpad_config.cc deleted file mode 100644 index 434973d..0000000 --- a/src/launchpad-process-pool/launchpad_config.cc +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 "launchpad-process-pool/launchpad_config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "lib/common/inc/launchpad_common.h" - -#define PATH_LAUNCHPAD_CONF "/usr/share/aul/launchpad.conf" -#define TAG_MEMORY_STATUS "MemoryStatus" -#define KEY_MEMORY_STATUS_LOW_KEY "LowKey" -#define KEY_MEMORY_STATUS_LOW_VALUE "LowValue" -#define KEY_MEMORY_STATUS_NORMAL_KEY "NormalKey" -#define KEY_MEMORY_STATUS_NORMAL_VALUE "NormalValue" - -#define TAG_MEMORY_MONITOR "MemoryMonitor" -#define KEY_MEMORY_MONITOR_THRESHOLD "Threshold" -#define KEY_MEMORY_MONITOR_INTERVAL "Interval" - -#define TAG_CPU_CHECKER "CpuChecker" -#define KEY_CPU_CHECKER_MAX_COUNT "MaxCount" - -#define TAG_LOGGER "Logger" -#define KEY_LOGGER_PATH "Path" -#define KEY_LOGGER_ENABLE "Enable" - -struct memory_status_s { - char* low_key; - int low_value; - char* normal_key; - int normal_value; -}; - -struct memory_monitor_s { - int threshold; - int interval; -}; - -struct cpu_checker_s { - int max_count; -}; - -struct logger_s { - char* path; - int enable; -}; - -static struct memory_status_s __memory_status; -static struct memory_monitor_s __memory_monitor; -static struct cpu_checker_s __cpu_checker; -static struct logger_s __logger; - -const char* _config_get_string_value(config_type_e type) { - const char* value; - - switch (type) { - case CONFIG_TYPE_MEMORY_STATUS_LOW_KEY: - value = __memory_status.low_key; - break; - case CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY: - value = __memory_status.normal_key; - break; - case CONFIG_TYPE_LOGGER_PATH: - value = __logger.path; - break; - default: - _E("Unknown type"); - value = NULL; - break; - } - - return value; -} - -int _config_get_int_value(config_type_e type) { - int value; - - switch (type) { - case CONFIG_TYPE_MEMORY_STATUS_LOW_VALUE: - value = __memory_status.low_value; - break; - case CONFIG_TYPE_MEMORY_STATUS_NORMAL_VALUE: - value = __memory_status.normal_value; - break; - case CONFIG_TYPE_MEMORY_MONITOR_THRESHOLD: - value = __memory_monitor.threshold; - break; - case CONFIG_TYPE_MEMORY_MONITOR_INTERVAL: - value = __memory_monitor.interval; - break; - case CONFIG_TYPE_CPU_CHECKER_MAX_COUNT: - value = __cpu_checker.max_count; - break; - case CONFIG_TYPE_LOGGER_ENABLE: - value = __logger.enable; - break; - default: - _E("Unknown type"); - value = INT_MIN; - } - - return value; -} - -static const char* __get_string_value(dictionary* d, - const char* tag, - const char* key) { - char buf[128]; - - snprintf(buf, sizeof(buf), "%s:%s", tag, key); - return iniparser_getstring(d, buf, NULL); -} - -static int __get_int_value(dictionary* d, const char* tag, const char* key) { - char buf[128]; - - snprintf(buf, sizeof(buf), "%s:%s", tag, key); - return iniparser_getint(d, buf, INT_MIN); -} - -static void __memory_status_init(void) { - __memory_status.low_key = strdup(VCONFKEY_SYSMAN_LOW_MEMORY); - __memory_status.low_value = VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING; - __memory_status.normal_key = strdup(VCONFKEY_SYSMAN_LOW_MEMORY); - __memory_status.normal_value = VCONFKEY_SYSMAN_LOW_MEMORY_NORMAL; -} - -static void __memory_status_fini(void) { - __memory_status.normal_value = 0; - free(__memory_status.normal_key); - __memory_status.low_value = 0; - free(__memory_status.low_key); -} - -static void __memory_status_set(dictionary* d) { - const char* str; - char* val; - int ret; - - str = __get_string_value(d, TAG_MEMORY_STATUS, KEY_MEMORY_STATUS_LOW_KEY); - if (str) { - val = strdup(str); - if (val) { - free(__memory_status.low_key); - __memory_status.low_key = val; - } - } - - ret = __get_int_value(d, TAG_MEMORY_STATUS, KEY_MEMORY_STATUS_LOW_VALUE); - if (ret != INT_MIN) - __memory_status.low_value = ret; - - str = __get_string_value(d, TAG_MEMORY_STATUS, KEY_MEMORY_STATUS_NORMAL_KEY); - if (str) { - val = strdup(str); - if (val) { - free(__memory_status.normal_key); - __memory_status.normal_key = val; - } - } - - ret = __get_int_value(d, TAG_MEMORY_STATUS, KEY_MEMORY_STATUS_NORMAL_VALUE); - if (ret != INT_MIN) - __memory_status.normal_value = ret; - - _W("Memory Status Low(%s:%d), Normal(%s:%d)", __memory_status.low_key, - __memory_status.low_value, __memory_status.normal_key, - __memory_status.normal_value); -} - -static void __memory_monitor_init(void) { - __memory_monitor.threshold = 80; /* 80 % */ - __memory_monitor.interval = 5000; /* 5 seconds */ -} - -static void __memory_monitor_fini(void) { - __memory_monitor.threshold = 0; - __memory_monitor.interval = 0; -} - -static void __memory_monitor_set(dictionary* d) { - int ret; - - ret = __get_int_value(d, TAG_MEMORY_MONITOR, KEY_MEMORY_MONITOR_THRESHOLD); - if (ret != INT_MIN) - __memory_monitor.threshold = ret; - - ret = __get_int_value(d, TAG_MEMORY_MONITOR, KEY_MEMORY_MONITOR_INTERVAL); - if (ret != INT_MIN) - __memory_monitor.interval = ret; - - _W("Memory Monitor Threshold(%d), Interval(%d)", __memory_monitor.threshold, - __memory_monitor.interval); -} - -static void __cpu_checker_init(void) { - __cpu_checker.max_count = 10; -} - -static void __cpu_checker_fini(void) { - __cpu_checker.max_count = 0; -} - -static void __cpu_checker_set(dictionary* d) { - int ret; - - ret = __get_int_value(d, TAG_CPU_CHECKER, KEY_CPU_CHECKER_MAX_COUNT); - if (ret != INT_MIN) - __cpu_checker.max_count = ret; - - _W("CPU Checker MaxCount(%d)", __cpu_checker.max_count); -} - -static void __logger_init(void) { - __logger.path = strdup("/var/log/appfw"); - __logger.enable = false; -} - -static void __logger_fini(void) { - free(__logger.path); -} - -static void __logger_set(dictionary* d) { - const char* str; - int val; - - str = __get_string_value(d, TAG_LOGGER, KEY_LOGGER_PATH); - if (str) { - free(__logger.path); - __logger.path = strdup(str); - } - _W("Logger path(%s)", __logger.path); - - val = __get_int_value(d, TAG_LOGGER, KEY_LOGGER_ENABLE); - if (val >= 0) - __logger.enable = val; - _W("Logger enable(%d)", __logger.enable); -} - -int _config_init(void) { - dictionary* d; - int ret; - - _D("config init"); - - __memory_status_init(); - __memory_monitor_init(); - __cpu_checker_init(); - __logger_init(); - - ret = access(PATH_LAUNCHPAD_CONF, F_OK); - if (ret != 0) { - _W("The file doesn't exist. errno(%d)", errno); - return 0; - } - - d = iniparser_load(PATH_LAUNCHPAD_CONF); - if (!d) { - _E("Failed to load the config file"); - return -1; - } - - __memory_status_set(d); - __memory_monitor_set(d); - __cpu_checker_set(d); - __logger_set(d); - - iniparser_freedict(d); - - return 0; -} - -void _config_fini(void) { - _D("config fini"); - - __logger_fini(); - __cpu_checker_fini(); - __memory_monitor_fini(); - __memory_status_fini(); -} diff --git a/src/launchpad-process-pool/launchpad_config.h b/src/launchpad-process-pool/launchpad_config.h deleted file mode 100644 index 23fd60b..0000000 --- a/src/launchpad-process-pool/launchpad_config.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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 __LAUNCHPAD_CONFIG_H__ -#define __LAUNCHPAD_CONFIG_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - CONFIG_TYPE_MEMORY_STATUS_LOW_KEY, - CONFIG_TYPE_MEMORY_STATUS_LOW_VALUE, - CONFIG_TYPE_MEMORY_STATUS_NORMAL_KEY, - CONFIG_TYPE_MEMORY_STATUS_NORMAL_VALUE, - CONFIG_TYPE_MEMORY_MONITOR_THRESHOLD, - CONFIG_TYPE_MEMORY_MONITOR_INTERVAL, - CONFIG_TYPE_CPU_CHECKER_MAX_COUNT, - CONFIG_TYPE_LOGGER_PATH, - CONFIG_TYPE_LOGGER_ENABLE, -} config_type_e; - -const char *_config_get_string_value(config_type_e type); - -int _config_get_int_value(config_type_e type); - -int _config_init(void); - -void _config_fini(void); - -#ifdef __cplusplus -} -#endif - -#endif /* __LAUNCHPAD_CONFIG_H__ */ diff --git a/src/launchpad-process-pool/log.cc b/src/launchpad-process-pool/log.cc index db2da84..8d22e5b 100644 --- a/src/launchpad-process-pool/log.cc +++ b/src/launchpad-process-pool/log.cc @@ -23,8 +23,7 @@ #include -#include "launchpad-process-pool/launchpad_config.h" - +#include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/log_private.hh" #include "launchpad-process-pool/logger.hh" @@ -36,8 +35,7 @@ std::unique_ptr logger_; } // namespace void Log::Init() { - const std::string log_path = - _config_get_string_value(CONFIG_TYPE_LOGGER_PATH) + + const std::string log_path = Config::GetInst().GetLogger().GetPath() + std::string("/launchpad/launchpad.log"); try { logger_.reset(new Logger(log_path)); diff --git a/src/launchpad-process-pool/logger.cc b/src/launchpad-process-pool/logger.cc index 2ac3229..689068c 100644 --- a/src/launchpad-process-pool/logger.cc +++ b/src/launchpad-process-pool/logger.cc @@ -30,7 +30,7 @@ #include -#include "launchpad-process-pool/launchpad_config.h" +#include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/log_private.hh" namespace launchpad { @@ -66,7 +66,7 @@ int CreateDirectory(const std::string &path) { int CreateLaunchpadDirectories() { const std::string logger_path = - _config_get_string_value(CONFIG_TYPE_LOGGER_PATH); + launchpad::Config::GetInst().GetLogger().GetPath(); if (CreateDirectory(logger_path) != 0) return -1; @@ -83,7 +83,7 @@ int CreateLaunchpadDirectories() { } // namespace Logger::Logger(std::string path) : path_(std::move(path)) { - if (!_config_get_int_value(CONFIG_TYPE_LOGGER_ENABLE)) { + if (!launchpad::Config::GetInst().GetLogger().IsEnabled()) { disabled_ = true; return; } diff --git a/src/launchpad-process-pool/memory_monitor.cc b/src/launchpad-process-pool/memory_monitor.cc index 54acde4..ebe809a 100644 --- a/src/launchpad-process-pool/memory_monitor.cc +++ b/src/launchpad-process-pool/memory_monitor.cc @@ -18,7 +18,7 @@ #include -#include "launchpad-process-pool/launchpad_config.h" +#include "launchpad-process-pool/config.hh" #include "launchpad-process-pool/log_private.hh" namespace launchpad { @@ -29,8 +29,9 @@ constexpr const float INTERVAL_BASE_RATE = 0.15f; } // namespace MemoryMonitor::MemoryMonitor(IEvent* listener) : listener_(listener) { - threshold_ = _config_get_int_value(CONFIG_TYPE_MEMORY_MONITOR_THRESHOLD); - base_interval_ = _config_get_int_value(CONFIG_TYPE_MEMORY_MONITOR_INTERVAL); + auto& config = Config::GetInst(); + threshold_ = config.GetMemoryMonitor().GetThreshold(); + base_interval_ = config.GetMemoryMonitor().GetInterval(); interval_ = base_interval_; } diff --git a/src/lib/launchpad-common/CMakeLists.txt b/src/lib/launchpad-common/CMakeLists.txt index 35449e3..3a434d4 100644 --- a/src/lib/launchpad-common/CMakeLists.txt +++ b/src/lib/launchpad-common/CMakeLists.txt @@ -16,6 +16,7 @@ APPLY_PKG_CONFIG(${TARGET_LAUNCHPAD_COMMON} PUBLIC BUNDLE_DEPS DLOG_DEPS GIO_DEPS + INIPARSER_DEPS ) TARGET_LINK_LIBRARIES(${TARGET_LAUNCHPAD_COMMON} PUBLIC "-ldl") diff --git a/src/lib/launchpad-common/ini_parser.cc b/src/lib/launchpad-common/ini_parser.cc new file mode 100644 index 0000000..5969225 --- /dev/null +++ b/src/lib/launchpad-common/ini_parser.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "launchpad-common/ini_parser.hh" + +namespace launchpad { + +IniParser::IniParser(const std::string& path) + : dict_(iniparser_load(path.c_str())) {} + +IniParser::~IniParser() { + if (dict_ != nullptr) + iniparser_freedict(dict_); +} + +std::string IniParser::Get(const std::string& section, + const std::string& key) const { + if (dict_ == nullptr) + return ""; + + std::string full_key = section + ":" + key; + const char* value = iniparser_getstring(dict_, full_key.c_str(), nullptr); + if (value == nullptr) + return ""; + + return std::string(value); +} + +} // namespace launchpad + diff --git a/src/lib/launchpad-common/ini_parser.hh b/src/lib/launchpad-common/ini_parser.hh new file mode 100644 index 0000000..661e88c --- /dev/null +++ b/src/lib/launchpad-common/ini_parser.hh @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 LIB_LAUNCHPAD_COMMON_INI_PARSER_HH_ +#define LIB_LAUNCHPAD_COMMON_INI_PARSER_HH_ + +#include + +#include + +#undef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) + +namespace launchpad { + +class EXPORT_API IniParser { + public: + explicit IniParser(const std::string& path); + ~IniParser(); + + std::string Get(const std::string& section, const std::string& key) const; + + private: + dictionary* dict_; +}; + +} // namespace launchpad + +#endif // LIB_LAUNCHPAD_COMMON_INI_PARSER_HH_ -- 2.7.4