From d080eabfc69f9598761a7e861902df44e8b8b109 Mon Sep 17 00:00:00 2001 From: fly_1997 Date: Fri, 31 May 2024 16:31:04 +0800 Subject: [PATCH] refactor instance interfaces and add signal function (cherry picked from commit 0509a1c1085175fc5f7061da747eed724d59563e) --- 0002-refactor-plugin-interface.patch | 1032 ++++++++++++++++ ...s-to-the-new-interface-and-refactor-.patch | 1080 +++++++++++++++++ ...nterfaces-and-add-interface-comments.patch | 121 ++ ...ignal-processing-and-singleton-class.patch | 401 ++++++ oeAware-manager.spec | 14 +- 5 files changed, 2645 insertions(+), 3 deletions(-) create mode 100644 0002-refactor-plugin-interface.patch create mode 100644 0003-the-client-adapts-to-the-new-interface-and-refactor-.patch create mode 100644 0004-modify-some-interfaces-and-add-interface-comments.patch create mode 100644 0005-add-the-SIGINT-signal-processing-and-singleton-class.patch diff --git a/0002-refactor-plugin-interface.patch b/0002-refactor-plugin-interface.patch new file mode 100644 index 0000000..db9666a --- /dev/null +++ b/0002-refactor-plugin-interface.patch @@ -0,0 +1,1032 @@ +From 3d8160661afa6dfd045dc51577c4bb040c25b591 Mon Sep 17 00:00:00 2001 +From: fly_1997 +Date: Sat, 25 May 2024 15:48:33 +0800 +Subject: [PATCH 1/4] refactor plugin interface + +--- + src/common/default_path.h | 4 +- + src/plugin_mgr/config.cpp | 20 +-- + src/plugin_mgr/config.h | 8 +- + src/plugin_mgr/dep_handler.cpp | 7 +- + src/plugin_mgr/dep_handler.h | 6 +- + src/plugin_mgr/error_code.cpp | 1 + + src/plugin_mgr/error_code.h | 1 + + src/plugin_mgr/instance_run_handler.cpp | 181 ++++-------------------- + src/plugin_mgr/instance_run_handler.h | 25 ++-- + src/plugin_mgr/interface.h | 44 +++--- + src/plugin_mgr/message_manager.h | 2 +- + src/plugin_mgr/plugin.cpp | 23 ++- + src/plugin_mgr/plugin.h | 84 ++--------- + src/plugin_mgr/plugin_manager.cpp | 142 +++++++------------ + src/plugin_mgr/plugin_manager.h | 12 +- + 15 files changed, 166 insertions(+), 394 deletions(-) + +diff --git a/src/common/default_path.h b/src/common/default_path.h +index 4ce68b4..0ef3aae 100644 +--- a/src/common/default_path.h ++++ b/src/common/default_path.h +@@ -14,9 +14,7 @@ + + #include + +-const std::string DEFAULT_COLLECTOR_PATH = "/usr/lib64/oeAware-plugin/collector"; +-const std::string DEFAULT_SCENARIO_PATH = "/usr/lib64/oeAware-plugin/scenario"; +-const std::string DEFAULT_TUNE_PATH = "/usr/lib64/oeAware-plugin/tune"; ++const std::string DEFAULT_PLUGIN_PATH = "/usr/lib64/oeAware-plugin"; + const std::string DEFAULT_RUN_PATH = "/var/run/oeAware"; + const std::string DEFAULT_LOG_PATH = "/var/log/oeAware"; + +diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp +index 588eda0..6e0e231 100644 +--- a/src/plugin_mgr/config.cpp ++++ b/src/plugin_mgr/config.cpp +@@ -42,7 +42,7 @@ bool check_plugin_list(YAML::Node plugin_list_item) { + return true; + } + +-bool Config::load(const std::string path) { ++bool Config::load(const std::string &path) { + YAML::Node node; + struct stat buffer; + if (stat(path.c_str(), &buffer) != 0) { +@@ -106,20 +106,6 @@ bool Config::load(const std::string path) { + return true; + } + +-std::string get_path(PluginType type) { +- switch (type) { +- case PluginType::COLLECTOR:{ +- return DEFAULT_COLLECTOR_PATH; +- } +- case PluginType::SCENARIO: { +- return DEFAULT_SCENARIO_PATH; +- } +- case PluginType::TUNE: { +- return DEFAULT_TUNE_PATH; +- } +- default: { +- break; +- } +- } +- return ""; ++std::string get_path() { ++ return DEFAULT_PLUGIN_PATH; + } +\ No newline at end of file +diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h +index c2d1f37..3844f71 100644 +--- a/src/plugin_mgr/config.h ++++ b/src/plugin_mgr/config.h +@@ -22,7 +22,7 @@ static int log_levels[] = {0, 10000, 20000, 30000, 40000, 50000, 60000}; + + class PluginInfo { + public: +- PluginInfo(std::string name, std::string description, std::string url) : name(name), description(description), url(url) { } ++ PluginInfo(const std::string &name, const std::string &description, const std::string &url) : name(name), description(description), url(url) { } + std::string get_name() const { + return this->name; + } +@@ -52,7 +52,7 @@ namespace std { + + class EnableItem { + public: +- EnableItem(std::string name) : name(name), enabled(false) { } ++ EnableItem(const std::string &name) : name(name), enabled(false) { } + void set_enabled(bool enabled) { + this->enabled = enabled; + } +@@ -82,7 +82,7 @@ public: + Config() { + this->log_level = log_levels[2]; + } +- bool load(const std::string path); ++ bool load(const std::string &path); + int get_log_level() const { + return this->log_level; + } +@@ -122,7 +122,7 @@ private: + std::vector enable_list; + }; + +-std::string get_path(PluginType type); ++std::string get_path(); + bool create_dir(const std::string &path); + + #endif +\ No newline at end of file +diff --git a/src/plugin_mgr/dep_handler.cpp b/src/plugin_mgr/dep_handler.cpp +index 816056d..c6d0985 100644 +--- a/src/plugin_mgr/dep_handler.cpp ++++ b/src/plugin_mgr/dep_handler.cpp +@@ -39,7 +39,7 @@ void DepHandler::add_arc_node(std::shared_ptr node, const std::vector dep_nodes) { ++void DepHandler::add_node(const std::string &name, const std::vector &dep_nodes) { + std::shared_ptr cur_node = add_new_node(name); + this->nodes[name] = cur_node; + add_arc_node(cur_node, dep_nodes); +@@ -52,11 +52,10 @@ void DepHandler::del_node(const std::string &name) { + } + + +-std::shared_ptr DepHandler::get_node(std::string name) { ++std::shared_ptr DepHandler::get_node(const std::string &name) { + return this->nodes[name]; + } + +- + std::shared_ptr DepHandler::add_new_node(std::string name) { + std::shared_ptr cur_node = std::make_shared(name); + cur_node->head = std::make_shared(); +@@ -80,6 +79,7 @@ void DepHandler::del_node_and_arc_nodes(std::shared_ptr node) { + arc = tmp; + } + } ++ + void DepHandler::change_arc_nodes(std::string name, bool state) { + if (!nodes[name]->state || !arc_nodes.count(name)) return; + std::unordered_map, bool> &mp = arc_nodes[name]; +@@ -101,7 +101,6 @@ void DepHandler::change_arc_nodes(std::string name, bool state) { + } + } + +- + void DepHandler::query_all_top(std::vector> &query) { + for (auto &p : nodes) { + query_node_top(p.first, query); +diff --git a/src/plugin_mgr/dep_handler.h b/src/plugin_mgr/dep_handler.h +index b81d574..40b8748 100644 +--- a/src/plugin_mgr/dep_handler.h ++++ b/src/plugin_mgr/dep_handler.h +@@ -34,7 +34,7 @@ struct Node { + int real_cnt; + bool state; // dependency closed-loop + Node() : next(nullptr), head(nullptr), cnt(0), real_cnt(0), state(true) {} +- Node(std::string name): next(nullptr), head(nullptr), name(name), cnt(0), real_cnt(0), state(true) {} ++ Node(const std::string &name): next(nullptr), head(nullptr), name(name), cnt(0), real_cnt(0), state(true) {} + }; + + class DepHandler { +@@ -43,11 +43,11 @@ public: + this->head = std::make_shared(); + this->tail = head; + } +- std::shared_ptr get_node(std::string name); ++ std::shared_ptr get_node(const std::string &name); + bool get_node_state(std::string name) { + return this->nodes[name]->state; + } +- void add_node(const std::string &name, std::vector dep_nodes = {}); ++ void add_node(const std::string &name, const std::vector &dep_nodes = {}); + void del_node(const std::string &name); + std::vector get_pre_dependencies(const std::string &name); + // query instance dependency +diff --git a/src/plugin_mgr/error_code.cpp b/src/plugin_mgr/error_code.cpp +index 09f0eb9..8c7af00 100644 +--- a/src/plugin_mgr/error_code.cpp ++++ b/src/plugin_mgr/error_code.cpp +@@ -15,6 +15,7 @@ const std::unordered_map ErrorText::error_codes = { + {ErrorCode::ENABLE_INSTANCE_NOT_LOAD, "instance is not loaded"}, + {ErrorCode::ENABLE_INSTANCE_UNAVAILABLE, "instance is unavailable"}, + {ErrorCode::ENABLE_INSTANCE_ALREADY_ENABLED, "instance is already enabled"}, ++ {ErrorCode::ENABLE_INSTANCE_ENV, "instance init environment failed"}, + {ErrorCode::DISABLE_INSTANCE_NOT_LOAD, "instance is not loaded"}, + {ErrorCode::DISABLE_INSTANCE_UNAVAILABLE, "instance is unavailable"}, + {ErrorCode::DISABLE_INSTANCE_ALREADY_DISABLED, "instance is already disabled"}, +diff --git a/src/plugin_mgr/error_code.h b/src/plugin_mgr/error_code.h +index dd028f3..cdbf542 100644 +--- a/src/plugin_mgr/error_code.h ++++ b/src/plugin_mgr/error_code.h +@@ -18,6 +18,7 @@ enum class ErrorCode { + ENABLE_INSTANCE_NOT_LOAD, + ENABLE_INSTANCE_UNAVAILABLE, + ENABLE_INSTANCE_ALREADY_ENABLED, ++ ENABLE_INSTANCE_ENV, + DISABLE_INSTANCE_NOT_LOAD, + DISABLE_INSTANCE_UNAVAILABLE, + DISABLE_INSTANCE_ALREADY_DISABLED, +diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp +index 8ba10db..9eed762 100644 +--- a/src/plugin_mgr/instance_run_handler.cpp ++++ b/src/plugin_mgr/instance_run_handler.cpp +@@ -13,104 +13,45 @@ + #include + #include + +-static void* get_ring_buf(std::shared_ptr instance) { ++static const void* get_ring_buf(std::shared_ptr instance) { + if (instance == nullptr) { + return nullptr; + } +- switch (instance->get_type()) { +- case PluginType::COLLECTOR: { +- return (std::dynamic_pointer_cast(instance))->get_interface()->get_ring_buf(); +- } +- case PluginType::SCENARIO: { +- return (std::dynamic_pointer_cast(instance))->get_interface()->get_ring_buf(); +- } +- case PluginType::TUNE: { +- break; +- } +- default: { +- break; +- } +- } +- return nullptr; +-} +- +-static void reflash_ring_buf(std::shared_ptr instance) { +- (std::dynamic_pointer_cast(instance))->get_interface()->reflash_ring_buf(); +-} +- +-void InstanceRunHandler::run_aware(std::shared_ptr instance, std::vector &deps) { +- void *a[MAX_DEPENDENCIES_SIZE]; +- for (size_t i = 0; i < deps.size(); ++i) { +- std::shared_ptr ins = memory_store.get_instance(deps[i]); +- a[i] = get_ring_buf(ins); +- } +- (std::dynamic_pointer_cast(instance))->get_interface()->aware(a, (int)deps.size()); ++ return instance->get_interface()->get_ring_buf(); + } + +-void InstanceRunHandler::run_tune(std::shared_ptr instance, std::vector &deps) { +- void *a[MAX_DEPENDENCIES_SIZE]; ++void InstanceRunHandler::run_instance(std::shared_ptr instance) { ++ std::vector input_data; ++ std::vector deps = instance->get_deps(); + for (size_t i = 0; i < deps.size(); ++i) { + std::shared_ptr ins = memory_store.get_instance(deps[i]); +- a[i] = get_ring_buf(ins); ++ input_data.emplace_back(get_ring_buf(ins)); + } +- (std::dynamic_pointer_cast(instance))->get_interface()->tune(a, (int)deps.size()); ++ Param param; ++ param.args = input_data.data(); ++ param.len = input_data.size(); ++ instance->get_interface()->run(¶m); + } + +-void InstanceRunHandler::insert_instance(std::shared_ptr instance) { +- switch (instance->get_type()) { +- case PluginType::COLLECTOR: { +- collector[instance->get_name()] = instance; +- (std::dynamic_pointer_cast(instance))->get_interface()->enable(); +- break; +- } +- case PluginType::SCENARIO: { +- scenario[instance->get_name()] = instance; +- (std::dynamic_pointer_cast(instance))->get_interface()->enable(); +- break; +- } +- case PluginType::TUNE: { +- tune[instance->get_name()] = instance; +- (std::dynamic_pointer_cast(instance))->get_interface()->enable(); +- break; +- } +- default: { +- break; +- } +- } ++void InstanceRunHandler::insert_instance(std::shared_ptr instance, uint64_t time) { ++ instance->set_enabled(true); ++ schedule_queue.push(ScheduleInstance{instance, time}); + INFO("[PluginManager] " << instance->get_name() << " instance insert into running queue."); + } + + void InstanceRunHandler::delete_instance(std::shared_ptr instance) { +- switch (instance->get_type()) { +- case PluginType::COLLECTOR: { +- collector.erase(instance->get_name()); +- (std::dynamic_pointer_cast(instance))->get_interface()->disable(); +- break; +- } +- case PluginType::SCENARIO: { +- scenario.erase(instance->get_name()); +- (std::dynamic_pointer_cast(instance))->get_interface()->disable(); +- break; +- } +- case PluginType::TUNE: { +- tune.erase(instance->get_name()); +- (std::dynamic_pointer_cast(instance))->get_interface()->disable(); +- break; +- } +- default: { +- break; +- } +- } ++ instance->get_interface()->disable(); ++ instance->set_enabled(false); + INFO("[PluginManager] " << instance->get_name() << " instance delete from running queue."); + } + +-void InstanceRunHandler::handle_instance() { ++void InstanceRunHandler::handle_instance(uint64_t time) { + InstanceRunMessage msg; + while(this->recv_queue_try_pop(msg)){ + std::shared_ptr instance = msg.get_instance(); + switch (msg.get_type()){ + case RunType::ENABLED: { +- insert_instance(std::move(instance)); ++ insert_instance(std::move(instance), time); + break; + } + case RunType::DISABLED: { +@@ -121,78 +62,19 @@ void InstanceRunHandler::handle_instance() { + } + } + +-template +-static std::vector get_deps(std::shared_ptr instance) { +- std::shared_ptr t_instance = std::dynamic_pointer_cast(instance); +- std::string deps = (t_instance)->get_interface()->get_dep(); +- std::string dep = ""; +- std::vector vec; +- for (size_t i = 0; i < deps.length(); ++i) { +- if (deps[i] != '-') { +- dep += deps[i]; +- }else { +- vec.emplace_back(dep); +- dep = ""; +- } +- } +- vec.emplace_back(dep); +- return vec; +-} +- +-void InstanceRunHandler::adjust_collector_queue(const std::vector &deps, const std::vector &m_deps, bool flag) { +- for (auto &m_dep : m_deps) { +- bool ok = false; +- for (auto &dep : deps) { +- if (m_dep == dep) { +- ok = true; +- } ++void InstanceRunHandler::schedule(uint64_t time) { ++ while (!schedule_queue.empty()) { ++ auto schedule_instance = schedule_queue.top(); ++ if (schedule_instance.time != time) { ++ break; + } +- if (ok) continue; +- if (flag) { +- if (is_instance_exist(m_dep) && !collector.count(m_dep)) { +- this->insert_instance(memory_store.get_instance(m_dep)); +- } +- } else { +- if (is_instance_exist(m_dep) && collector.count(m_dep)) { +- this->delete_instance(memory_store.get_instance(m_dep)); +- } ++ schedule_queue.pop(); ++ if (!schedule_instance.instance->get_enabled()) { ++ break; + } +- } +-} +- +-void InstanceRunHandler::check_scenario_dependency(const std::vector &origin_deps, const std::vector &cur_deps) { +- adjust_collector_queue(origin_deps, cur_deps, true); +- adjust_collector_queue(cur_deps, origin_deps, false); +-} +- +-void InstanceRunHandler::schedule_collector(uint64_t time) { +- for (auto &p : collector) { +- std::shared_ptr instance = p.second; +- int t = (std::dynamic_pointer_cast(instance))->get_interface()->get_cycle(); +- if (time % t != 0) return; +- reflash_ring_buf(instance); +- } +-} +- +-void InstanceRunHandler::schedule_scenario(uint64_t time) { +- for (auto &p : scenario) { +- std::shared_ptr instance = p.second; +- int t = (std::dynamic_pointer_cast(instance))->get_interface()->get_cycle(); +- if (time % t != 0) return; +- std::vector origin_deps = get_deps(instance); +- run_aware(instance, origin_deps); +- std::vector cur_deps = get_deps(instance); +- check_scenario_dependency(origin_deps, cur_deps); +- } +-} +- +-void InstanceRunHandler::schedule_tune(uint64_t time) { +- for (auto &p : tune) { +- std::shared_ptr instance = p.second; +- int t = (std::dynamic_pointer_cast(instance))->get_interface()->get_cycle(); +- if (time % t != 0) return; +- std::vector deps = get_deps(instance); +- run_tune(instance, deps); ++ run_instance(schedule_instance.instance); ++ schedule_instance.time += schedule_instance.instance->get_interface()->get_cycle(); ++ schedule_queue.push(schedule_instance); + } + } + +@@ -200,11 +82,8 @@ void start(InstanceRunHandler *instance_run_handler) { + unsigned long long time = 0; + INFO("[PluginManager] instance schedule started!"); + while(true) { +- instance_run_handler->handle_instance(); +- instance_run_handler->schedule_collector(time); +- instance_run_handler->schedule_scenario(time); +- instance_run_handler->schedule_tune(time); +- ++ instance_run_handler->handle_instance(time); ++ instance_run_handler->schedule(time); + usleep(instance_run_handler->get_cycle() * 1000); + time += instance_run_handler->get_cycle(); + } +diff --git a/src/plugin_mgr/instance_run_handler.h b/src/plugin_mgr/instance_run_handler.h +index 83f9f4a..4172e99 100644 +--- a/src/plugin_mgr/instance_run_handler.h ++++ b/src/plugin_mgr/instance_run_handler.h +@@ -20,6 +20,7 @@ + #include + #include + #include ++#include + #include + + enum class RunType { +@@ -43,15 +44,22 @@ private: + std::shared_ptr instance; + }; + ++class ScheduleInstance { ++public: ++ bool operator < (const ScheduleInstance &rhs) const { ++ return time > rhs.time || (time == rhs.time && instance->get_type() > rhs.instance->get_type()); ++ } ++ std::shared_ptr instance; ++ uint64_t time; ++}; ++ + // A handler to schedule plugin instance + class InstanceRunHandler { + public: + InstanceRunHandler(MemoryStore &memory_store) : memory_store(memory_store), cycle(DEFAULT_CYCLE_SIZE) {} + void run(); +- void schedule_collector(uint64_t time); +- void schedule_scenario(uint64_t time); +- void schedule_tune(uint64_t time); +- void handle_instance(); ++ void schedule(uint64_t time); ++ void handle_instance(uint64_t time); + void set_cycle(int cycle) { + this->cycle = cycle; + } +@@ -71,16 +79,13 @@ public: + return this->recv_queue.try_pop(msg); + } + private: +- void run_aware(std::shared_ptr instance, std::vector &deps); +- void run_tune(std::shared_ptr instance, std::vector &deps); ++ void run_instance(std::shared_ptr instance); + void delete_instance(std::shared_ptr instance); +- void insert_instance(std::shared_ptr instance); ++ void insert_instance(std::shared_ptr instance, uint64_t time); + void adjust_collector_queue(const std::vector &deps, const std::vector &m_deps, bool flag); + void check_scenario_dependency(const std::vector &deps, const std::vector &m_deps); + +- std::unordered_map> collector; +- std::unordered_map> scenario; +- std::unordered_map> tune; ++ std::priority_queue schedule_queue; + SafeQueue recv_queue; + MemoryStore &memory_store; + int cycle; +diff --git a/src/plugin_mgr/interface.h b/src/plugin_mgr/interface.h +index 075378a..2106698 100644 +--- a/src/plugin_mgr/interface.h ++++ b/src/plugin_mgr/interface.h +@@ -12,40 +12,28 @@ + #ifndef PLUGIN_MGR_INTERFACE_H + #define PLUGIN_MGR_INTERFACE_H + +-struct CollectorInterface { +- char* (*get_version)(); +- char* (*get_name)(); +- char* (*get_description)(); +- char* (*get_type)(); +- int (*get_cycle)(); +- char* (*get_dep)(); +- void (*enable)(); +- void (*disable)(); +- void* (*get_ring_buf)(); +- void (*reflash_ring_buf)(); ++enum PluginType { ++ COLLECTOR, ++ SCENARIO, ++ TUNE, + }; + +-struct ScenarioInterface { +- char* (*get_version)(); +- char* (*get_name)(); +- char* (*get_description)(); +- char* (*get_dep)(); +- int (*get_cycle)(); +- void (*enable)(); +- void (*disable)(); +- void (*aware)(void*[], int); +- void* (*get_ring_buf)(); ++struct Param { ++ void *args; ++ int len; + }; + +-struct TuneInterface { +- char* (*get_version)(); +- char* (*get_name)(); +- char* (*get_description)(); +- char* (*get_dep)(); ++struct Interface { ++ const char* (*get_version)(); ++ const char* (*get_name)(); ++ const char* (*get_description)(); ++ const char* (*get_dep)(); ++ PluginType (*get_type)(); + int (*get_cycle)(); +- void (*enable)(); ++ bool (*enable)(); + void (*disable)(); +- void (*tune)(void*[], int); ++ const void* (*get_ring_buf)(); ++ void (*run)(const Param*); + }; + + #endif // !PLUGIN_MGR_INTERFACE_H +diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h +index 416be48..00ea4c7 100644 +--- a/src/plugin_mgr/message_manager.h ++++ b/src/plugin_mgr/message_manager.h +@@ -31,7 +31,7 @@ public: + Message() : type(MessageType::EXTERNAL) {} + Message(Opt opt) : opt(opt) {} + Message(Opt opt, MessageType type) : opt(opt), type(type) {} +- Message(Opt opt, std::vector payload) : opt(opt), payload(payload) {} ++ Message(Opt opt, const std::vector &payload) : opt(opt), payload(payload) {} + Opt get_opt() { + return this->opt; + } +diff --git a/src/plugin_mgr/plugin.cpp b/src/plugin_mgr/plugin.cpp +index 1a2db0d..bdd8226 100644 +--- a/src/plugin_mgr/plugin.cpp ++++ b/src/plugin_mgr/plugin.cpp +@@ -16,7 +16,7 @@ const std::string Instance::PLUGIN_DISABLED = "close"; + const std::string Instance::PLUGIN_STATE_ON = "available"; + const std::string Instance::PLUGIN_STATE_OFF = "unavailable"; + +-int Plugin::load(const std::string dl_path) { ++int Plugin::load(const std::string &dl_path) { + void *handler = dlopen(dl_path.c_str(), RTLD_LAZY); + if (handler == nullptr) { + return -1; +@@ -30,3 +30,24 @@ std::string Instance::get_info() const { + std::string run_text = this->enabled ? PLUGIN_ENABLED : PLUGIN_DISABLED; + return name + "(" + state_text + ", " + run_text + ")"; + } ++ ++std::vector Instance::get_deps() { ++ std::vector vec; ++ if (get_interface()->get_dep == nullptr) { ++ return vec; ++ } ++ std::string deps = get_interface()->get_dep(); ++ std::string dep = ""; ++ for (size_t i = 0; i < deps.length(); ++i) { ++ if (deps[i] != '-') { ++ dep += deps[i]; ++ } else { ++ vec.emplace_back(dep); ++ dep = ""; ++ } ++ } ++ if (!dep.empty()) { ++ vec.emplace_back(dep); ++ } ++ return vec; ++} +diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h +index d59ab8a..ac0e0b5 100644 +--- a/src/plugin_mgr/plugin.h ++++ b/src/plugin_mgr/plugin.h +@@ -17,31 +17,21 @@ + #include + #include + +-enum class PluginType { +- COLLECTOR, +- SCENARIO, +- TUNE, +- EMPTY, +-}; +- +-std::string plugin_type_to_string(PluginType type); +- + class Instance { + public: +- virtual ~Instance() = default; +- void set_name(std::string name) { ++ void set_name(const std::string &name) { + this->name = name; + } + std::string get_name() const { + return this->name; + } +- void set_type(PluginType type) { +- this->type = type; +- } + PluginType get_type() const { +- return type; ++ return interface->get_type(); ++ } ++ Interface* get_interface() const { ++ return this->interface; + } +- void set_plugin_name(std::string name) { ++ void set_plugin_name(const std::string &name) { + this->plugin_name = name; + } + std::string get_plugin_name() const { +@@ -59,82 +49,35 @@ public: + bool get_enabled() const { + return this->enabled; + } ++ void set_interface(Interface *interface) { ++ this->interface = interface; ++ } + std::string get_info() const; ++ std::vector get_deps(); + private: + std::string name; + std::string plugin_name; +- PluginType type; + bool state; + bool enabled; ++ Interface *interface; + const static std::string PLUGIN_ENABLED; + const static std::string PLUGIN_DISABLED; + const static std::string PLUGIN_STATE_ON; + const static std::string PLUGIN_STATE_OFF; + }; + +-class CollectorInstance : public Instance { +-public: +- CollectorInstance() : interface(nullptr) { } +- ~CollectorInstance() { +- interface = nullptr; +- } +- void set_interface(CollectorInterface *interface) { +- this->interface = interface; +- } +- CollectorInterface* get_interface() const { +- return this->interface; +- } +-private: +- CollectorInterface* interface; +-}; +- +-class ScenarioInstance : public Instance { +-public: +- ScenarioInstance() : interface(nullptr) { } +- ~ScenarioInstance() { +- interface = nullptr; +- } +- void set_interface(ScenarioInterface *interface) { +- this->interface = interface; +- } +- ScenarioInterface* get_interface() const { +- return this->interface; +- } +-private: +- ScenarioInterface* interface; +-}; +- +-class TuneInstance : public Instance { +-public: +- TuneInstance() : interface(nullptr) { } +- ~TuneInstance() { +- interface = nullptr; +- } +- void set_interface(TuneInterface *interface) { +- this->interface = interface; +- } +- TuneInterface* get_interface() const { +- return this->interface; +- } +-private: +- TuneInterface *interface; +-}; +- + class Plugin { + public: +- Plugin(std::string name, PluginType type) : name(name), type(type), handler(nullptr) { } ++ Plugin(const std::string &name) : name(name), handler(nullptr) { } + ~Plugin() { + if (handler != nullptr) { + dlclose(handler); + } + } +- int load(const std::string dl_path); ++ int load(const std::string &dl_path); + std::string get_name() const { + return this->name; + } +- PluginType get_type() const { +- return this->type; +- } + void add_instance(std::shared_ptr ins) { + instances.emplace_back(ins); + } +@@ -150,7 +93,6 @@ public: + private: + std::vector> instances; + std::string name; +- PluginType type; + void *handler; + }; + +diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp +index 5324512..1503fc1 100644 +--- a/src/plugin_mgr/plugin_manager.cpp ++++ b/src/plugin_mgr/plugin_manager.cpp +@@ -78,9 +78,8 @@ ErrorCode PluginManager::query_plugin(const std::string &name, std::string &res) + return ErrorCode::OK; + } + +-template +-int PluginManager::load_dl_instance(std::shared_ptr plugin, T **interface_list) { +- int (*get_instance)(T**) = (int(*)(T**))dlsym(plugin->get_handler(), "get_instance"); ++int PluginManager::load_dl_instance(std::shared_ptr plugin, Interface **interface_list) { ++ int (*get_instance)(Interface**) = (int(*)(Interface**))dlsym(plugin->get_handler(), "get_instance"); + if (get_instance == nullptr) { + ERROR("[PluginManager] dlsym error!\n"); + return -1; +@@ -90,78 +89,33 @@ int PluginManager::load_dl_instance(std::shared_ptr plugin, T **interfac + return len; + } + +-template +-std::vector get_dep(T *interface) { +- char *deps = interface->get_dep(); +- std::vector res; +- std::string dep; +- for (int i = 0; deps[i] != 0; ++i) { +- if (deps[i] != '-') { +- dep += deps[i]; +- } else { +- res.emplace_back(dep); +- dep = ""; +- } +- } +- if (!dep.empty()) res.emplace_back(dep); +- return res; +-} +- +-template +-void PluginManager::save_instance(std::shared_ptr plugin, T *interface_list, int len) { ++void PluginManager::save_instance(std::shared_ptr plugin, Interface *interface_list, int len) { + if (interface_list == nullptr) return; + for (int i = 0; i < len; ++i) { +- T *interface = interface_list + i; +- std::shared_ptr instance = std::make_shared(); ++ Interface *interface = interface_list + i; ++ std::shared_ptr instance = std::make_shared(); + std::string name = interface->get_name(); ++ instance->set_interface(interface); + instance->set_name(name); + instance->set_plugin_name(plugin->get_name()); +- instance->set_type(plugin->get_type()); + instance->set_enabled(false); +- if (plugin->get_type() == PluginType::COLLECTOR) { +- DEBUG("[PluginManager] add node"); +- dep_handler.add_node(name); +- } else { +- dep_handler.add_node(name, get_dep(interface)); +- } ++ dep_handler.add_node(name, instance->get_deps()); + instance->set_state(dep_handler.get_node_state(name)); +- (std::dynamic_pointer_cast(instance))->set_interface(interface); + DEBUG("[PluginManager] Instance: " << name.c_str()); + memory_store.add_instance(name, instance); +- plugin->add_instance(instance); ++ plugin->add_instance(instance); + } + } + + bool PluginManager::load_instance(std::shared_ptr plugin) { + int len = 0; + DEBUG("plugin: " << plugin->get_name()); +- switch (plugin->get_type()) { +- case PluginType::COLLECTOR: { +- CollectorInterface *interface_list = nullptr; +- len = load_dl_instance(plugin, &interface_list); +- if (len == -1) return false; +- DEBUG("[PluginManager] save collect instance"); +- save_instance(plugin, interface_list, len); +- break; +- } +- case PluginType::SCENARIO: { +- ScenarioInterface *interface_list = nullptr; +- len = load_dl_instance(plugin, &interface_list); +- if (len == -1) return false; +- save_instance(plugin, interface_list, len); +- break; +- } +- case PluginType::TUNE: { +- TuneInterface *interface_list = nullptr; +- len = load_dl_instance(plugin, &interface_list); +- if (len == -1) return false; +- save_instance(plugin, interface_list, len); +- break; +- } +- default: { +- return false; +- } ++ Interface *interface_list = nullptr; ++ len = load_dl_instance(plugin, &interface_list); ++ if (len < 0) { ++ return false; + } ++ save_instance(plugin, interface_list, len); + update_instance_state(); + return true; + } +@@ -177,8 +131,8 @@ void PluginManager::update_instance_state() { + } + } + +-ErrorCode PluginManager::load_plugin(const std::string &name, PluginType type) { +- std::string plugin_path = get_path(type) + "/" + name; ++ErrorCode PluginManager::load_plugin(const std::string &name) { ++ std::string plugin_path = get_path() + "/" + name; + if (!file_exist(plugin_path)) { + return ErrorCode::LOAD_PLUGIN_FILE_NOT_EXIST; + } +@@ -191,9 +145,8 @@ ErrorCode PluginManager::load_plugin(const std::string &name, PluginType type) { + if (memory_store.is_plugin_exist(name)) { + return ErrorCode::LOAD_PLUGIN_EXIST; + } +- const std::string dl_path = get_path(type) + '/' + name; +- std::shared_ptr plugin = std::make_shared(name, type); +- int error = plugin->load(dl_path); ++ std::shared_ptr plugin = std::make_shared(name); ++ int error = plugin->load(plugin_path); + if (error) { + return ErrorCode::LOAD_PLUGIN_DLOPEN_FAILED; + } +@@ -268,16 +221,35 @@ ErrorCode PluginManager::instance_enabled(std::string name) { + return ErrorCode::ENABLE_INSTANCE_ALREADY_ENABLED; + } + std::vector pre_dependencies = dep_handler.get_pre_dependencies(name); ++ std::vector> new_enabled; ++ bool enabled = true; + for (int i = pre_dependencies.size() - 1; i >= 0; --i) { + instance = memory_store.get_instance(pre_dependencies[i]); + if (instance->get_enabled()) { + continue; + } +- instance->set_enabled(true); +- instance_run_handler->recv_queue_push(InstanceRunMessage(RunType::ENABLED, instance)); +- DEBUG("[PluginManager] " << instance->get_name() << " instance enabled."); ++ new_enabled.emplace_back(instance); ++ if (!instance->get_interface()->enable()) { ++ enabled = false; ++ WARN("[PluginManager] " << instance->get_name() << " instance enabled failed, because instance init environment failed."); ++ break; ++ } ++ } ++ if (enabled) { ++ for (int i = pre_dependencies.size() - 1; i >= 0; --i) { ++ instance = memory_store.get_instance(pre_dependencies[i]); ++ if (instance->get_enabled()) { ++ continue; ++ } ++ instance_run_handler->recv_queue_push(InstanceRunMessage(RunType::ENABLED, instance)); ++ } ++ return ErrorCode::OK; ++ } else { ++ for (auto instance : new_enabled) { ++ instance->get_interface()->disable(); ++ } ++ return ErrorCode::ENABLE_INSTANCE_ENV; + } +- return ErrorCode::OK; + } + + ErrorCode PluginManager::instance_disabled(std::string name) { +@@ -291,7 +263,6 @@ ErrorCode PluginManager::instance_disabled(std::string name) { + if (!instance->get_enabled()) { + return ErrorCode::DISABLE_INSTANCE_ALREADY_DISABLED; + } +- instance->set_enabled(false); + instance_run_handler->recv_queue_push(InstanceRunMessage(RunType::DISABLED, instance)); + return ErrorCode::OK; + } +@@ -331,9 +302,7 @@ ErrorCode PluginManager::add_list(std::string &res) { + res += p.first + "\n"; + } + res += "Installed Plugins:\n"; +- res += get_plugin_in_dir(DEFAULT_COLLECTOR_PATH); +- res += get_plugin_in_dir(DEFAULT_SCENARIO_PATH); +- res += get_plugin_in_dir(DEFAULT_TUNE_PATH); ++ res += get_plugin_in_dir(DEFAULT_PLUGIN_PATH); + return ErrorCode::OK; + } + +@@ -371,15 +340,15 @@ void PluginManager::pre_enable() { + } + } + +-void PluginManager::pre_load_plugin(PluginType type) { +- std::string path = get_path(type); ++void PluginManager::pre_load_plugin() { ++ std::string path = get_path(); + DIR *dir = opendir(path.c_str()); + if (dir == nullptr) return; + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) { + std::string name = entry->d_name; + if (end_with(name, ".so")) { +- auto ret = load_plugin(name, type); ++ auto ret = load_plugin(name); + if (ret != ErrorCode::OK) { + WARN("[PluginManager] " << name << " plugin preload failed, because " << ErrorText::get_error_text(ret) << "."); + } else { +@@ -391,27 +360,13 @@ void PluginManager::pre_load_plugin(PluginType type) { + } + + void PluginManager::pre_load() { +- pre_load_plugin(PluginType::COLLECTOR); +- pre_load_plugin(PluginType::SCENARIO); +- pre_load_plugin(PluginType::TUNE); ++ pre_load_plugin(); + pre_enable(); + } + +-void* PluginManager::get_data_buffer(std::string name) { ++const void* PluginManager::get_data_buffer(std::string name) { + std::shared_ptr instance = memory_store.get_instance(name); +- switch (instance->get_type()) { +- case PluginType::COLLECTOR: { +- CollectorInterface *collector_interface = (std::dynamic_pointer_cast(instance))->get_interface(); +- return collector_interface->get_ring_buf(); +- } +- case PluginType::SCENARIO: { +- ScenarioInterface *scenario_interface = (std::dynamic_pointer_cast(instance))->get_interface(); +- return scenario_interface->get_ring_buf(); +- } +- default: +- return nullptr; +- } +- return nullptr; ++ return instance->get_interface()->get_ring_buf(); + } + + std::string PluginManager::instance_dep_check(const std::string &name) { +@@ -465,8 +420,7 @@ int PluginManager::run() { + switch (msg.get_opt()) { + case Opt::LOAD: { + std::string plugin_name = msg.get_payload(0); +- PluginType type = plugin_types[msg.get_payload(1)]; +- ErrorCode ret_code = load_plugin(plugin_name, type); ++ ErrorCode ret_code = load_plugin(plugin_name); + if(ret_code == ErrorCode::OK) { + INFO("[PluginManager] " << plugin_name << " plugin loaded."); + res.set_opt(Opt::RESPONSE_OK); +diff --git a/src/plugin_mgr/plugin_manager.h b/src/plugin_mgr/plugin_manager.h +index 709d42e..9339f0d 100644 +--- a/src/plugin_mgr/plugin_manager.h ++++ b/src/plugin_mgr/plugin_manager.h +@@ -33,10 +33,10 @@ public: + void pre_load(); + void pre_enable(); + void init(); +- void* get_data_buffer(std::string name); ++ const void* get_data_buffer(std::string name); + private: +- void pre_load_plugin(PluginType type); +- ErrorCode load_plugin(const std::string &path, PluginType type); ++ void pre_load_plugin(); ++ ErrorCode load_plugin(const std::string &path); + ErrorCode remove(const std::string &name); + ErrorCode query_all_plugins(std::string &res); + ErrorCode query_plugin(const std::string &name, std::string &res); +@@ -47,10 +47,8 @@ private: + ErrorCode add_list(std::string &res); + ErrorCode download(const std::string &name, std::string &res); + std::string instance_dep_check(const std::string &name); +- template +- int load_dl_instance(std::shared_ptr plugin, T **interface_list); +- template +- void save_instance(std::shared_ptr plugin, T *interface_list, int len); ++ int load_dl_instance(std::shared_ptr plugin, Interface **interface_list); ++ void save_instance(std::shared_ptr plugin, Interface *interface_list, int len); + bool load_instance(std::shared_ptr plugin); + void batch_load(); + void batch_remove(); +-- +2.33.0 + diff --git a/0003-the-client-adapts-to-the-new-interface-and-refactor-.patch b/0003-the-client-adapts-to-the-new-interface-and-refactor-.patch new file mode 100644 index 0000000..1c7f8c2 --- /dev/null +++ b/0003-the-client-adapts-to-the-new-interface-and-refactor-.patch @@ -0,0 +1,1080 @@ +From 2f00d1f6c4a0dd199cdfb13e9a541c5f87f5cf4e Mon Sep 17 00:00:00 2001 +From: fly_1997 +Date: Tue, 28 May 2024 11:09:13 +0800 +Subject: [PATCH 2/4] the client adapts to the new interface and refactor + memory store + +--- + src/client/arg_parse.cpp | 18 +----- + src/client/arg_parse.h | 4 -- + src/client/client.cpp | 1 - + src/client/cmd_handler.cpp | 7 +-- + src/client/cmd_handler.h | 4 -- + src/common/message_protocol.cpp | 1 - + src/common/message_protocol.h | 4 +- + src/plugin_mgr/config.h | 7 +-- + src/plugin_mgr/dep_handler.cpp | 84 +++++++++++++------------ + src/plugin_mgr/dep_handler.h | 56 ++++++++--------- + src/plugin_mgr/instance_run_handler.cpp | 8 +-- + src/plugin_mgr/instance_run_handler.h | 11 +--- + src/plugin_mgr/interface.h | 33 ++++++++-- + src/plugin_mgr/logger.cpp | 1 - + src/plugin_mgr/main.cpp | 1 - + src/plugin_mgr/memory_store.h | 39 +++++++----- + src/plugin_mgr/message_manager.h | 3 +- + src/plugin_mgr/plugin.cpp | 2 +- + src/plugin_mgr/plugin.h | 2 +- + src/plugin_mgr/plugin_manager.cpp | 75 ++++++++-------------- + src/plugin_mgr/plugin_manager.h | 29 +++------ + 21 files changed, 174 insertions(+), 216 deletions(-) + +diff --git a/src/client/arg_parse.cpp b/src/client/arg_parse.cpp +index 947c4c4..75e9671 100644 +--- a/src/client/arg_parse.cpp ++++ b/src/client/arg_parse.cpp +@@ -13,11 +13,10 @@ + #include + #include + +-const std::string ArgParse::OPT_STRING = "Qqd:t:l:r:e:i:"; ++const std::string ArgParse::OPT_STRING = "Qqd:l:r:e:i:"; + const struct option ArgParse::long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"load", required_argument, NULL, 'l'}, +- {"type", required_argument, NULL, 't'}, + {"remove", required_argument, NULL, 'r'}, + {"query", required_argument, NULL, 'q'}, + {"query-dep", required_argument, NULL, 'Q'}, +@@ -34,10 +33,6 @@ void ArgParse::arg_error(const std::string &msg) { + exit(EXIT_FAILURE); + } + +-void ArgParse::set_type(char *_type) { +- type = _type; +-} +- + void ArgParse::set_arg(char *_arg) { + arg = std::string(_arg); + } +@@ -46,10 +41,6 @@ void ArgParse::print_help() { + std::cout << "usage: oeawarectl [options]...\n" + " options\n" + " -l|--load [plugin] load plugin and need plugin type.\n" +- " -t|--type [plugin_type] assign plugin type. there are three types:\n" +- " collector: collection plugin.\n" +- " scenario: awareness plugin.\n" +- " tune: tune plugin.\n" + " -r|--remove [plugin] remove plugin from system.\n" + " -e|--enable [instance] enable the plugin instance.\n" + " -d|--disable [instance] disable the plugin instance.\n" +@@ -70,7 +61,6 @@ void ArgParse::init_opts() { + opts.insert('e'); + opts.insert('d'); + opts.insert('i'); +- opts.insert('t'); + } + + int ArgParse::init(int argc, char *argv[]) { +@@ -82,9 +72,6 @@ int ArgParse::init(int argc, char *argv[]) { + while((opt = getopt_long(argc, argv, OPT_STRING.c_str(), long_options, nullptr)) != -1) { + std::string full_opt; + switch (opt) { +- case 't': +- set_type(optarg); +- break; + case 'h': + help = true; + break; +@@ -118,9 +105,6 @@ int ArgParse::init(int argc, char *argv[]) { + + } + } +- if (cmd == 'l' && type.empty()) { +- arg_error("missing arguments."); +- } + if (help) { + print_help(); + exit(EXIT_SUCCESS); +diff --git a/src/client/arg_parse.h b/src/client/arg_parse.h +index 6a0a25b..e3e55cd 100644 +--- a/src/client/arg_parse.h ++++ b/src/client/arg_parse.h +@@ -22,15 +22,11 @@ public: + static void init_opts(); + static void set_type(char* _type); + static void set_arg(char* _arg); +- static std::string get_type() { +- return type; +- } + static std::string get_arg() { + return arg; + } + private: + static std::string arg; +- static std::string type; + static std::unordered_set opts; + static const std::string OPT_STRING; + static const int MAX_OPT_SIZE = 20; +diff --git a/src/client/client.cpp b/src/client/client.cpp +index afaa189..6318d5d 100644 +--- a/src/client/client.cpp ++++ b/src/client/client.cpp +@@ -12,7 +12,6 @@ + #include "client.h" + + std::string ArgParse::arg; +-std::string ArgParse::type; + std::unordered_set ArgParse::opts; + + void Client::cmd_groups_init() { +diff --git a/src/client/cmd_handler.cpp b/src/client/cmd_handler.cpp +index 6f3f760..009a9bc 100644 +--- a/src/client/cmd_handler.cpp ++++ b/src/client/cmd_handler.cpp +@@ -23,10 +23,7 @@ void LoadHandler::check(const std::string &type) { + + void LoadHandler::handler(Msg &msg) { + std::string arg = ArgParse::get_arg(); +- std::string type = ArgParse::get_type(); +- check(type); + msg.add_payload(arg); +- msg.add_payload(type); + msg.set_opt(Opt::LOAD); + } + +@@ -109,10 +106,10 @@ void write_to_file(const std::string &file_name, const std::string &text) { + void QueryTopHandler::handler(Msg &msg) { + std::string arg = ArgParse::get_arg(); + if (arg.empty()) { +- msg.set_opt(Opt::QUERY_ALL_TOP); ++ msg.set_opt(Opt::QUERY_ALL_DEPS); + } else { + msg.add_payload(arg); +- msg.set_opt(Opt::QUERY_TOP); ++ msg.set_opt(Opt::QUERY_DEP); + } + } + +diff --git a/src/client/cmd_handler.h b/src/client/cmd_handler.h +index 2e32098..53178fd 100644 +--- a/src/client/cmd_handler.h ++++ b/src/client/cmd_handler.h +@@ -13,9 +13,6 @@ + #define CLIENT_CMD_HANDLER_H + #include "message_protocol.h" + #include "arg_parse.h" +-#include +-#include +-#include + + class CmdHandler { + public: +@@ -27,7 +24,6 @@ public: + + class LoadHandler : public CmdHandler { + public: +- // LoadHandler(const ArgParse &arg_parse) { } + void handler(Msg &msg) override; + void res_handler(Msg &msg) override; + private: +diff --git a/src/common/message_protocol.cpp b/src/common/message_protocol.cpp +index a813a6c..9cf9415 100644 +--- a/src/common/message_protocol.cpp ++++ b/src/common/message_protocol.cpp +@@ -10,7 +10,6 @@ + * See the Mulan PSL v2 for more details. + ******************************************************************************/ + #include "message_protocol.h" +-#include + + template + inline ssize_t handle_error(T fn) { +diff --git a/src/common/message_protocol.h b/src/common/message_protocol.h +index b2c3c0d..d659f59 100644 +--- a/src/common/message_protocol.h ++++ b/src/common/message_protocol.h +@@ -34,8 +34,8 @@ enum class Opt { + REMOVE, + QUERY, + QUERY_ALL, +- QUERY_TOP, +- QUERY_ALL_TOP, ++ QUERY_DEP, ++ QUERY_ALL_DEPS, + LIST, + DOWNLOAD, + RESPONSE_OK, +diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h +index 3844f71..9d097ab 100644 +--- a/src/plugin_mgr/config.h ++++ b/src/plugin_mgr/config.h +@@ -13,7 +13,6 @@ + #define PLUGIN_MGR_CONFIG_H + + #include "plugin.h" +-#include + #include + #include + #include +@@ -52,7 +51,7 @@ namespace std { + + class EnableItem { + public: +- EnableItem(const std::string &name) : name(name), enabled(false) { } ++ explicit EnableItem(const std::string &name) : name(name), enabled(false) { } + void set_enabled(bool enabled) { + this->enabled = enabled; + } +@@ -86,9 +85,6 @@ public: + int get_log_level() const { + return this->log_level; + } +- int get_schedule_cycle() const { +- return this->schedule_cycle; +- } + std::string get_log_type() const { + return this->log_type; + } +@@ -115,7 +111,6 @@ public: + } + private: + int log_level; +- int schedule_cycle; + std::string log_path; + std::string log_type; + std::unordered_map plugin_list; +diff --git a/src/plugin_mgr/dep_handler.cpp b/src/plugin_mgr/dep_handler.cpp +index c6d0985..1006175 100644 +--- a/src/plugin_mgr/dep_handler.cpp ++++ b/src/plugin_mgr/dep_handler.cpp +@@ -11,39 +11,52 @@ + ******************************************************************************/ + #include "dep_handler.h" + #include +-#include +-#include + ++bool DepHandler::is_instance_exist(const std::string &name) { ++ return nodes.count(name); ++} ++ ++void DepHandler::add_instance(std::shared_ptr instance) { ++ add_node(instance); ++} ++ ++void DepHandler::delete_instance(const std::string &name) { ++ del_node(name); ++} ++ + void DepHandler::add_arc_node(std::shared_ptr node, const std::vector &dep_nodes) { + std::shared_ptr arc_head = node->head; + node->cnt = dep_nodes.size(); + int real_cnt = 0; +- bool state = true; + for (auto name : dep_nodes) { + std::shared_ptr tmp = std::make_shared(); + tmp->arc_name = name; +- tmp->node_name = node->name; ++ tmp->node_name = node->instance->get_name(); + tmp->next = arc_head->next; + arc_head->next = tmp; + + if (nodes.count(name)) { +- arc_nodes[name][tmp] = true; ++ tmp->is_exist = true; ++ arc_nodes[name].insert(tmp); + real_cnt++; + } else { +- arc_nodes[name][tmp] = false; +- state = false; ++ tmp->is_exist = false; ++ arc_nodes[name].insert(tmp); + } + } +- node->state = state; ++ if (real_cnt == node->cnt) { ++ node->instance->set_state(true); ++ } + node->real_cnt = real_cnt; + } + +- +-void DepHandler::add_node(const std::string &name, const std::vector &dep_nodes) { +- std::shared_ptr cur_node = add_new_node(name); ++void DepHandler::add_node(std::shared_ptr instance) { ++ std::string name = instance->get_name(); ++ std::shared_ptr cur_node = add_new_node(instance); + this->nodes[name] = cur_node; ++ std::vector dep_nodes = instance->get_deps(); + add_arc_node(cur_node, dep_nodes); +- change_arc_nodes(name, true); ++ update_instance_state(name); + } + + void DepHandler::del_node(const std::string &name) { +@@ -56,16 +69,14 @@ std::shared_ptr DepHandler::get_node(const std::string &name) { + return this->nodes[name]; + } + +-std::shared_ptr DepHandler::add_new_node(std::string name) { +- std::shared_ptr cur_node = std::make_shared(name); ++std::shared_ptr DepHandler::add_new_node(std::shared_ptr instance) { ++ std::shared_ptr cur_node = std::make_shared(); ++ cur_node->instance = instance; + cur_node->head = std::make_shared(); +- tail->next = cur_node; +- tail = cur_node; + return cur_node; + } + + void DepHandler::del_node_and_arc_nodes(std::shared_ptr node) { +- std::shared_ptr next = node->next; + std::shared_ptr arc = node->head; + while(arc) { + std::shared_ptr tmp = arc->next; +@@ -80,34 +91,28 @@ void DepHandler::del_node_and_arc_nodes(std::shared_ptr node) { + } + } + +-void DepHandler::change_arc_nodes(std::string name, bool state) { +- if (!nodes[name]->state || !arc_nodes.count(name)) return; +- std::unordered_map, bool> &mp = arc_nodes[name]; +- for (auto &vec : mp) { +- vec.second = state; +- if (nodes.count(vec.first->node_name)) { +- std::shared_ptr tmp = nodes[vec.first->node_name]; +- if (state) { +- tmp->real_cnt++; +- if (tmp->real_cnt == tmp->cnt) { +- tmp->state = true; +- } +- } else { +- tmp->real_cnt--; +- tmp->state = false; ++void DepHandler::update_instance_state(const std::string &name) { ++ if (!nodes[name]->instance->get_state() || !arc_nodes.count(name)) return; ++ std::unordered_set> &arcs = arc_nodes[name]; ++ for (auto &arc_node : arcs) { ++ if (nodes.count(arc_node->node_name)) { ++ auto tmp = nodes[arc_node->node_name]; ++ tmp->real_cnt++; ++ if (tmp->real_cnt == tmp->cnt) { ++ tmp->instance->set_state(true); + } +- change_arc_nodes(vec.first->node_name, state); ++ update_instance_state(tmp->instance->get_name()); + } + } + } + +-void DepHandler::query_all_top(std::vector> &query) { ++void DepHandler::query_all_dependencies(std::vector> &query) { + for (auto &p : nodes) { + query_node_top(p.first, query); + } + } + +-void DepHandler::query_node_top(std::string name, std::vector> &query) { ++void DepHandler::query_node_top(const std::string &name, std::vector> &query) { + std::shared_ptr p = nodes[name]->head; + if (p->next == nullptr) { + query.emplace_back(std::vector{name}); +@@ -119,7 +124,7 @@ void DepHandler::query_node_top(std::string name, std::vector> &query) { ++void DepHandler::query_node_dependency(const std::string &name, std::vector> &query) { + if (!nodes.count(name)) return; + std::queue q; + std::unordered_set vis; +@@ -128,9 +133,10 @@ void DepHandler::query_node(const std::string &name, std::vector{node->name}); ++ std::string node_name = node->instance->get_name(); ++ query.emplace_back(std::vector{node_name}); + for (auto cur = node->head->next; cur != nullptr; cur = cur->next) { +- query.emplace_back(std::vector{node->name, cur->arc_name}); ++ query.emplace_back(std::vector{node_name, cur->arc_name}); + if (!vis.count(cur->arc_name) && nodes.count(cur->arc_name)) { + vis.insert(cur->arc_name); + q.push(cur->arc_name); +@@ -148,7 +154,7 @@ std::vector DepHandler::get_pre_dependencies(const std::string &nam + while (!q.empty()) { + auto &node = q.front(); + q.pop(); +- res.emplace_back(node->name); ++ res.emplace_back(node->instance->get_name()); + for (auto arc_node = node->head->next; arc_node != nullptr; arc_node = arc_node->next) { + if (!vis.count(arc_node->arc_name)) { + vis.insert(arc_node->arc_name); +diff --git a/src/plugin_mgr/dep_handler.h b/src/plugin_mgr/dep_handler.h +index 40b8748..a7a8ef6 100644 +--- a/src/plugin_mgr/dep_handler.h ++++ b/src/plugin_mgr/dep_handler.h +@@ -12,68 +12,62 @@ + #ifndef PLUGIN_MGR_DEP_HANDLER_H + #define PLUGIN_MGR_DEP_HANDLER_H + ++#include "plugin.h" + #include +-#include +-#include +-#include +-#include ++#include + + struct ArcNode { + std::shared_ptr next; + std::string arc_name; + std::string node_name; +- ArcNode() : next(nullptr) {} ++ bool is_exist; ++ ArcNode() : next(nullptr), is_exist(false) { } + }; + + // a instance node + struct Node { + std::shared_ptr next; + std::shared_ptr head; +- std::string name; ++ std::shared_ptr instance; + int cnt; + int real_cnt; +- bool state; // dependency closed-loop +- Node() : next(nullptr), head(nullptr), cnt(0), real_cnt(0), state(true) {} +- Node(const std::string &name): next(nullptr), head(nullptr), name(name), cnt(0), real_cnt(0), state(true) {} ++ Node(): next(nullptr), head(nullptr), cnt(0), real_cnt(0) { } + }; + + class DepHandler { + public: + DepHandler() { + this->head = std::make_shared(); +- this->tail = head; + } + std::shared_ptr get_node(const std::string &name); +- bool get_node_state(std::string name) { +- return this->nodes[name]->state; ++ bool get_node_state(const std::string &name) { ++ return this->nodes[name]->instance->get_state(); + } +- void add_node(const std::string &name, const std::vector &dep_nodes = {}); +- void del_node(const std::string &name); +- std::vector get_pre_dependencies(const std::string &name); +- // query instance dependency +- void query_node(const std::string &name, std::vector> &query); +- // query all instance dependencies +- void query_all_top(std::vector> &query); ++ void add_instance(std::shared_ptr instance); ++ void delete_instance(const std::string &name); ++ bool is_instance_exist(const std::string &name); ++ std::shared_ptr get_instance(const std::string &name) const { ++ return nodes.at(name)->instance; ++ } ++ void query_node_dependency(const std::string &name, std::vector> &query); ++ void query_all_dependencies(std::vector> &query); ++ /* check whether the instance has dependencies */ + bool have_dep(const std::string &name) { + return arc_nodes.count(name); + } +- bool is_empty() const { +- return nodes.empty(); +- } +- size_t get_node_nums() const { +- return nodes.size(); +- } ++ std::vector get_pre_dependencies(const std::string &name); + private: +- void query_node_top(std::string name, std::vector> &query); ++ void add_node(std::shared_ptr instance); ++ void del_node(const std::string &name); ++ void query_node_top(const std::string &name, std::vector> &query); + void add_arc_node(std::shared_ptr node, const std::vector &dep_nodes); +- void change_arc_nodes(std::string name, bool state); ++ void update_instance_state(const std::string &name); + void del_node_and_arc_nodes(std::shared_ptr node); +- std::shared_ptr add_new_node(std::string name); +- +- std::unordered_map, bool>> arc_nodes; ++ std::shared_ptr add_new_node(std::shared_ptr instance); ++ /* indegree edges */ ++ std::unordered_map>> arc_nodes; + std::unordered_map> nodes; + std::shared_ptr head; +- std::shared_ptr tail; + }; + + #endif // !PLUGIN_MGR_DEP_HANDLER_H +diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp +index 9eed762..c60207b 100644 +--- a/src/plugin_mgr/instance_run_handler.cpp ++++ b/src/plugin_mgr/instance_run_handler.cpp +@@ -13,7 +13,7 @@ + #include + #include + +-static const void* get_ring_buf(std::shared_ptr instance) { ++static const DataRingBuf* get_ring_buf(std::shared_ptr instance) { + if (instance == nullptr) { + return nullptr; + } +@@ -21,14 +21,14 @@ static const void* get_ring_buf(std::shared_ptr instance) { + } + + void InstanceRunHandler::run_instance(std::shared_ptr instance) { +- std::vector input_data; ++ std::vector input_data; + std::vector deps = instance->get_deps(); + for (size_t i = 0; i < deps.size(); ++i) { + std::shared_ptr ins = memory_store.get_instance(deps[i]); + input_data.emplace_back(get_ring_buf(ins)); + } + Param param; +- param.args = input_data.data(); ++ param.ring_bufs = input_data.data(); + param.len = input_data.size(); + instance->get_interface()->run(¶m); + } +@@ -73,7 +73,7 @@ void InstanceRunHandler::schedule(uint64_t time) { + break; + } + run_instance(schedule_instance.instance); +- schedule_instance.time += schedule_instance.instance->get_interface()->get_cycle(); ++ schedule_instance.time += schedule_instance.instance->get_interface()->get_period(); + schedule_queue.push(schedule_instance); + } + } +diff --git a/src/plugin_mgr/instance_run_handler.h b/src/plugin_mgr/instance_run_handler.h +index 4172e99..fc45874 100644 +--- a/src/plugin_mgr/instance_run_handler.h ++++ b/src/plugin_mgr/instance_run_handler.h +@@ -16,19 +16,14 @@ + #include "plugin.h" + #include "logger.h" + #include "memory_store.h" +-#include +-#include +-#include +-#include + #include +-#include + + enum class RunType { + ENABLED, + DISABLED, + }; + +-// Message for communication between plugin manager and instance scheduling ++/* Message for communication between plugin manager and instance scheduling */ + class InstanceRunMessage { + public: + InstanceRunMessage() {} +@@ -53,10 +48,10 @@ public: + uint64_t time; + }; + +-// A handler to schedule plugin instance ++/* A handler to schedule instances. */ + class InstanceRunHandler { + public: +- InstanceRunHandler(MemoryStore &memory_store) : memory_store(memory_store), cycle(DEFAULT_CYCLE_SIZE) {} ++ explicit InstanceRunHandler(MemoryStore &memory_store) : memory_store(memory_store), cycle(DEFAULT_CYCLE_SIZE) {} + void run(); + void schedule(uint64_t time); + void handle_instance(uint64_t time); +diff --git a/src/plugin_mgr/interface.h b/src/plugin_mgr/interface.h +index 2106698..6495b92 100644 +--- a/src/plugin_mgr/interface.h ++++ b/src/plugin_mgr/interface.h +@@ -11,6 +11,8 @@ + ******************************************************************************/ + #ifndef PLUGIN_MGR_INTERFACE_H + #define PLUGIN_MGR_INTERFACE_H ++#include ++#include + + enum PluginType { + COLLECTOR, +@@ -18,8 +20,24 @@ enum PluginType { + TUNE, + }; + ++struct DataBuf { ++ int len; ++ void *data; ++}; ++ ++struct DataRingBuf { ++ /* instance name */ ++ const char *instance_name; ++ /* buf write index, initial value is -1 */ ++ int index; ++ /* instance run times */ ++ uint64_t count; ++ struct DataBuf *buf; ++ int buf_len; ++}; ++ + struct Param { +- void *args; ++ const struct DataRingBuf **ring_bufs; + int len; + }; + +@@ -28,12 +46,17 @@ struct Interface { + const char* (*get_name)(); + const char* (*get_description)(); + const char* (*get_dep)(); +- PluginType (*get_type)(); +- int (*get_cycle)(); ++ enum PluginType (*get_type)(); ++ int (*get_period)(); + bool (*enable)(); + void (*disable)(); +- const void* (*get_ring_buf)(); +- void (*run)(const Param*); ++ const struct DataRingBuf* (*get_ring_buf)(); ++ void (*run)(const struct Param*); + }; + ++/* Obtains the instances from the plugin. ++ * The return value is the number of instances. ++ */ ++int get_instance(struct Interface **interface); ++ + #endif // !PLUGIN_MGR_INTERFACE_H +diff --git a/src/plugin_mgr/logger.cpp b/src/plugin_mgr/logger.cpp +index 318eafb..7a924c2 100644 +--- a/src/plugin_mgr/logger.cpp ++++ b/src/plugin_mgr/logger.cpp +@@ -11,7 +11,6 @@ + ******************************************************************************/ + #include "logger.h" + #include +-#include + + Logger::Logger() { + logger = log4cplus::Logger::getInstance("oeAware"); +diff --git a/src/plugin_mgr/main.cpp b/src/plugin_mgr/main.cpp +index 4e6acdc..698ba62 100644 +--- a/src/plugin_mgr/main.cpp ++++ b/src/plugin_mgr/main.cpp +@@ -53,7 +53,6 @@ int main(int argc, char **argv) { + INFO("[PluginManager] Start plugin manager!"); + PluginManager plugin_manager(config, handler_msg, res_msg); + plugin_manager.init(); +- plugin_manager.pre_load(); + plugin_manager.run(); + return 0; + } +\ No newline at end of file +diff --git a/src/plugin_mgr/memory_store.h b/src/plugin_mgr/memory_store.h +index ac3ff98..999795a 100644 +--- a/src/plugin_mgr/memory_store.h ++++ b/src/plugin_mgr/memory_store.h +@@ -11,37 +11,36 @@ + ******************************************************************************/ + #ifndef PLUGIN_MGR_MEMORY_STORE_H + #define PLUGIN_MGR_MEMORY_STORE_H +-#include "plugin.h" + #include "logger.h" ++#include "dep_handler.h" + #include +-#include + +-//OeAware memory storage, which is used to store plugins and instances in the memory. ++/* OeAware memory storage, which is used to store plugins and instances in the memory. */ + class MemoryStore { + public: + void add_plugin(const std::string &name, std::shared_ptr plugin) { + this->plugins.insert(std::make_pair(name, plugin)); + } +- void add_instance(const std::string &name, std::shared_ptr instance) { +- this->instances.insert(std::make_pair(name, instance)); ++ void add_instance(std::shared_ptr instance) { ++ dep_handler.add_instance(instance); + } + std::shared_ptr get_plugin(const std::string &name) const { + return this->plugins.at(name); + } + std::shared_ptr get_instance(const std::string &name) const { +- return this->instances.at(name); ++ return dep_handler.get_instance(name); + } + void delete_plugin(const std::string &name) { + this->plugins.erase(name); + } + void delete_instance(const std::string &name) { +- this->instances.erase(name); ++ dep_handler.delete_instance(name); + } + bool is_plugin_exist(const std::string &name) const { + return this->plugins.count(name); + } +- bool is_instance_exist(const std::string &name) const { +- return this->instances.count(name); ++ bool is_instance_exist(const std::string &name) { ++ return dep_handler.is_instance_exist(name); + } + std::vector> get_all_plugins() { + std::vector> res; +@@ -50,16 +49,24 @@ public: + } + return res; + } +- std::vector> get_all_instances() { +- std::vector> res; +- for (auto &p : instances) { +- res.emplace_back(p.second); +- } +- return res; ++ void query_node_dependency(const std::string &name, std::vector> &query) { ++ return dep_handler.query_node_dependency(name, query); ++ } ++ void query_all_dependencies(std::vector> &query) { ++ return dep_handler.query_all_dependencies(query); ++ } ++ bool have_dep(const std::string &name) { ++ return dep_handler.have_dep(name); ++ } ++ std::vector get_pre_dependencies(const std::string &name) { ++ return dep_handler.get_pre_dependencies(name); + } + private: ++ /* instance are stored in the form of DAG. ++ * DepHandler stores instances and manages dependencies. ++ */ ++ DepHandler dep_handler; + std::unordered_map> plugins; +- std::unordered_map> instances; + }; + + #endif // !PLUGIN_MGR_MEMORY_STORE_H +diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h +index 00ea4c7..95bbd1a 100644 +--- a/src/plugin_mgr/message_manager.h ++++ b/src/plugin_mgr/message_manager.h +@@ -29,7 +29,7 @@ enum class MessageType { + class Message { + public: + Message() : type(MessageType::EXTERNAL) {} +- Message(Opt opt) : opt(opt) {} ++ explicit Message(Opt opt) : opt(opt) {} + Message(Opt opt, MessageType type) : opt(opt), type(type) {} + Message(Opt opt, const std::vector &payload) : opt(opt), payload(payload) {} + Opt get_opt() { +@@ -79,6 +79,7 @@ public: + MessageManager(SafeQueue *handler_msg, SafeQueue *res_msg) { + this->handler_msg = handler_msg; + this->res_msg = res_msg; ++ this->tcp_socket = nullptr; + } + void init(){ + this->tcp_socket = new TcpSocket(); +diff --git a/src/plugin_mgr/plugin.cpp b/src/plugin_mgr/plugin.cpp +index bdd8226..4096fef 100644 +--- a/src/plugin_mgr/plugin.cpp ++++ b/src/plugin_mgr/plugin.cpp +@@ -33,7 +33,7 @@ std::string Instance::get_info() const { + + std::vector Instance::get_deps() { + std::vector vec; +- if (get_interface()->get_dep == nullptr) { ++ if (get_interface()->get_dep == nullptr || get_interface()->get_dep() == nullptr) { + return vec; + } + std::string deps = get_interface()->get_dep(); +diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h +index ac0e0b5..f6b5029 100644 +--- a/src/plugin_mgr/plugin.h ++++ b/src/plugin_mgr/plugin.h +@@ -68,7 +68,7 @@ private: + + class Plugin { + public: +- Plugin(const std::string &name) : name(name), handler(nullptr) { } ++ explicit Plugin(const std::string &name) : name(name), handler(nullptr) { } + ~Plugin() { + if (handler != nullptr) { + dlclose(handler); +diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp +index 1503fc1..7900ecf 100644 +--- a/src/plugin_mgr/plugin_manager.cpp ++++ b/src/plugin_mgr/plugin_manager.cpp +@@ -11,21 +11,13 @@ + ******************************************************************************/ + #include "plugin_manager.h" + #include "default_path.h" +-#include "utils.h" +-#include +-#include + #include +-#include + +-const std::string PluginManager::COLLECTOR_TEXT = "collector"; +-const std::string PluginManager::SCENARIO_TEXT = "scenario"; +-const std::string PluginManager::TUNE_TEXT = "tune"; + const static int ST_MODE_MASK = 0777; + + void PluginManager::init() { +- plugin_types[COLLECTOR_TEXT] = PluginType::COLLECTOR; +- plugin_types[SCENARIO_TEXT] = PluginType::SCENARIO; +- plugin_types[TUNE_TEXT] = PluginType::TUNE; ++ instance_run_handler.reset(new InstanceRunHandler(memory_store)); ++ pre_load(); + } + + ErrorCode PluginManager::remove(const std::string &name) { +@@ -40,19 +32,18 @@ ErrorCode PluginManager::remove(const std::string &name) { + if (instance->get_enabled()) { + return ErrorCode::REMOVE_INSTANCE_IS_RUNNING; + } +- if (dep_handler.have_dep(iname)) { ++ if (memory_store.have_dep(iname)) { + return ErrorCode::REMOVE_INSTANCE_HAVE_DEP; + } + instance_names.emplace_back(iname); + } + for(auto &iname : instance_names) { + memory_store.delete_instance(iname); +- dep_handler.del_node(iname); + } + memory_store.delete_plugin(name); +- update_instance_state(); + return ErrorCode::OK; + } ++ + ErrorCode PluginManager::query_all_plugins(std::string &res) { + std::vector> all_plugins = memory_store.get_all_plugins(); + for (auto &p : all_plugins) { +@@ -99,10 +90,8 @@ void PluginManager::save_instance(std::shared_ptr plugin, Interface *int + instance->set_name(name); + instance->set_plugin_name(plugin->get_name()); + instance->set_enabled(false); +- dep_handler.add_node(name, instance->get_deps()); +- instance->set_state(dep_handler.get_node_state(name)); + DEBUG("[PluginManager] Instance: " << name.c_str()); +- memory_store.add_instance(name, instance); ++ memory_store.add_instance(instance); + plugin->add_instance(instance); + } + } +@@ -116,21 +105,9 @@ bool PluginManager::load_instance(std::shared_ptr plugin) { + return false; + } + save_instance(plugin, interface_list, len); +- update_instance_state(); + return true; + } + +-void PluginManager::update_instance_state() { +- std::vector> all_instances = memory_store.get_all_instances(); +- for (auto &instance : all_instances) { +- if (dep_handler.get_node_state(instance->get_name())) { +- instance->set_state(true); +- } else { +- instance->set_state(false); +- } +- } +-} +- + ErrorCode PluginManager::load_plugin(const std::string &name) { + std::string plugin_path = get_path() + "/" + name; + if (!file_exist(plugin_path)) { +@@ -160,6 +137,8 @@ ErrorCode PluginManager::load_plugin(const std::string &name) { + std::string generate_dot(MemoryStore &memory_store, const std::vector> &query) { + std::string res; + res += "digraph G {\n"; ++ res += " rankdir = TB\n"; ++ res += " ranksep = 1\n"; + std::unordered_map> sub_graph; + for (auto &vec : query) { + std::shared_ptr instance = memory_store.get_instance(vec[0]); +@@ -171,45 +150,45 @@ std::string generate_dot(MemoryStore &memory_store, const std::vectorget_plugin_name()].insert(vec[1]); + } else { +- res += vec[1] + "[label=\"(missing)\\n" + vec[1] + "\", fontcolor=red];\n"; ++ res += " " + vec[1] + "[label=\"(missing)\\n" + vec[1] + "\", fontcolor=red];\n"; + } +- res += vec[0] + "->" + vec[1] + ";\n"; ++ res += " " + vec[0] + "->" + vec[1] + ";\n"; + } + int id = 0; + for (auto &p : sub_graph) { +- res += "subgraph cluster_" + std::to_string(id) + " {\n"; +- res += "node [style=filled];\n"; +- res += "label = \"" + p.first + "\";\n"; ++ res += " subgraph cluster_" + std::to_string(id) + " {\n"; ++ res += " node [style=filled];\n"; ++ res += " label = \"" + p.first + "\";\n"; + for (auto &i_name : p.second) { +- res += i_name + ";\n"; ++ res += " " + i_name + ";\n"; + } +- res += "}\n"; ++ res += " }\n"; + id++; + } + res += "}"; + return res; + } + +-ErrorCode PluginManager::query_top(const std::string &name, std::string &res) { ++ErrorCode PluginManager::query_dependency(const std::string &name, std::string &res) { + if (!memory_store.is_instance_exist(name)) { + return ErrorCode::QUERY_DEP_NOT_EXIST; + } + DEBUG("[PluginManager] query top : " << name); + std::vector> query; +- dep_handler.query_node(name, query); ++ memory_store.query_node_dependency(name, query); + res = generate_dot(memory_store, query); + return ErrorCode::OK; + } + +-ErrorCode PluginManager::query_all_tops(std::string &res) { ++ErrorCode PluginManager::query_all_dependencies(std::string &res) { + std::vector> query; +- dep_handler.query_all_top(query); ++ memory_store.query_all_dependencies(query); + DEBUG("[PluginManager] query size:" << query.size()); + res = generate_dot(memory_store, query); + return ErrorCode::OK; + } + +-ErrorCode PluginManager::instance_enabled(std::string name) { ++ErrorCode PluginManager::instance_enabled(const std::string &name) { + if (!memory_store.is_instance_exist(name)) { + return ErrorCode::ENABLE_INSTANCE_NOT_LOAD; + } +@@ -220,7 +199,7 @@ ErrorCode PluginManager::instance_enabled(std::string name) { + if (instance->get_enabled()) { + return ErrorCode::ENABLE_INSTANCE_ALREADY_ENABLED; + } +- std::vector pre_dependencies = dep_handler.get_pre_dependencies(name); ++ std::vector pre_dependencies = memory_store.get_pre_dependencies(name); + std::vector> new_enabled; + bool enabled = true; + for (int i = pre_dependencies.size() - 1; i >= 0; --i) { +@@ -252,7 +231,7 @@ ErrorCode PluginManager::instance_enabled(std::string name) { + } + } + +-ErrorCode PluginManager::instance_disabled(std::string name) { ++ErrorCode PluginManager::instance_disabled(const std::string &name) { + if (!memory_store.is_instance_exist(name)) { + return ErrorCode::DISABLE_INSTANCE_NOT_LOAD; + } +@@ -364,7 +343,7 @@ void PluginManager::pre_load() { + pre_enable(); + } + +-const void* PluginManager::get_data_buffer(std::string name) { ++const void* PluginManager::get_data_buffer(const std::string &name) { + std::shared_ptr instance = memory_store.get_instance(name); + return instance->get_interface()->get_ring_buf(); + } +@@ -375,7 +354,7 @@ std::string PluginManager::instance_dep_check(const std::string &name) { + for (size_t i = 0; i < plugin->get_instance_len(); ++i) { + std::string instance_name = plugin->get_instance(i)->get_name(); + std::vector> query; +- dep_handler.query_node(instance_name, query); ++ memory_store.query_node_dependency(instance_name, query); + std::vector lack; + for (auto &item : query) { + if (item.size() < 2) continue; +@@ -478,10 +457,10 @@ int PluginManager::run() { + } + break; + } +- case Opt::QUERY_TOP: { ++ case Opt::QUERY_DEP: { + std::string res_text; + std::string name = msg.get_payload(0); +- ErrorCode ret_code = query_top(name , res_text); ++ ErrorCode ret_code = query_dependency(name , res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] query " << name << " instance dependencies."); + res.set_opt(Opt::RESPONSE_OK); +@@ -494,9 +473,9 @@ int PluginManager::run() { + } + break; + } +- case Opt::QUERY_ALL_TOP: { ++ case Opt::QUERY_ALL_DEPS: { + std::string res_text; +- ErrorCode ret_code = query_all_tops(res_text); ++ ErrorCode ret_code = query_all_dependencies(res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] query all instances dependencies."); + res.set_opt(Opt::RESPONSE_OK); +diff --git a/src/plugin_mgr/plugin_manager.h b/src/plugin_mgr/plugin_manager.h +index 9339f0d..18d3f35 100644 +--- a/src/plugin_mgr/plugin_manager.h ++++ b/src/plugin_mgr/plugin_manager.h +@@ -15,35 +15,28 @@ + #include "instance_run_handler.h" + #include "config.h" + #include "memory_store.h" +-#include "dep_handler.h" + #include "message_manager.h" + #include "error_code.h" +-#include +-#include +-#include +-#include + + class PluginManager { + public: + PluginManager(Config &config, SafeQueue &handler_msg, SafeQueue &res_msg) : +- config(config), handler_msg(handler_msg), res_msg(res_msg) { +- instance_run_handler.reset(new InstanceRunHandler(memory_store)); +- } ++ config(config), handler_msg(handler_msg), res_msg(res_msg) { } + int run(); +- void pre_load(); +- void pre_enable(); + void init(); +- const void* get_data_buffer(std::string name); ++ const void* get_data_buffer(const std::string &name); + private: ++ void pre_load(); ++ void pre_enable(); + void pre_load_plugin(); + ErrorCode load_plugin(const std::string &path); + ErrorCode remove(const std::string &name); + ErrorCode query_all_plugins(std::string &res); + ErrorCode query_plugin(const std::string &name, std::string &res); +- ErrorCode query_top(const std::string &name, std::string &res); +- ErrorCode query_all_tops(std::string &res); +- ErrorCode instance_enabled(std::string name); +- ErrorCode instance_disabled(std::string name); ++ ErrorCode query_dependency(const std::string &name, std::string &res); ++ ErrorCode query_all_dependencies(std::string &res); ++ ErrorCode instance_enabled(const std::string &name); ++ ErrorCode instance_disabled(const std::string &name); + ErrorCode add_list(std::string &res); + ErrorCode download(const std::string &name, std::string &res); + std::string instance_dep_check(const std::string &name); +@@ -61,13 +54,9 @@ private: + SafeQueue &handler_msg; + SafeQueue &res_msg; + MemoryStore memory_store; +- DepHandler dep_handler; +- std::unordered_map plugin_types; +- static const std::string COLLECTOR_TEXT; +- static const std::string SCENARIO_TEXT; +- static const std::string TUNE_TEXT; + }; + + bool check_permission(std::string path, int mode); + bool file_exist(const std::string &file_name); ++ + #endif +-- +2.33.0 + diff --git a/0004-modify-some-interfaces-and-add-interface-comments.patch b/0004-modify-some-interfaces-and-add-interface-comments.patch new file mode 100644 index 0000000..ecb93dd --- /dev/null +++ b/0004-modify-some-interfaces-and-add-interface-comments.patch @@ -0,0 +1,121 @@ +From 0758b61d2e9af546d3c55bc9cb3704cd7a4fe59a Mon Sep 17 00:00:00 2001 +From: fly_1997 +Date: Tue, 28 May 2024 20:00:07 +0800 +Subject: [PATCH 3/4] modify some interfaces and add interface comments + +--- + src/plugin_mgr/instance_run_handler.cpp | 1 + + src/plugin_mgr/instance_run_handler.h | 10 +++------- + src/plugin_mgr/interface.h | 17 ++++++++++------- + src/plugin_mgr/plugin.h | 4 ++-- + 4 files changed, 16 insertions(+), 16 deletions(-) + +diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp +index c60207b..fc9dc04 100644 +--- a/src/plugin_mgr/instance_run_handler.cpp ++++ b/src/plugin_mgr/instance_run_handler.cpp +@@ -34,6 +34,7 @@ void InstanceRunHandler::run_instance(std::shared_ptr instance) { + } + + void InstanceRunHandler::insert_instance(std::shared_ptr instance, uint64_t time) { ++ /* To check if an instance is enabled, enable() is called in the PluginManager. */ + instance->set_enabled(true); + schedule_queue.push(ScheduleInstance{instance, time}); + INFO("[PluginManager] " << instance->get_name() << " instance insert into running queue."); +diff --git a/src/plugin_mgr/instance_run_handler.h b/src/plugin_mgr/instance_run_handler.h +index fc45874..f0bf263 100644 +--- a/src/plugin_mgr/instance_run_handler.h ++++ b/src/plugin_mgr/instance_run_handler.h +@@ -42,7 +42,7 @@ private: + class ScheduleInstance { + public: + bool operator < (const ScheduleInstance &rhs) const { +- return time > rhs.time || (time == rhs.time && instance->get_type() > rhs.instance->get_type()); ++ return time > rhs.time || (time == rhs.time && instance->get_priority() > rhs.instance->get_priority()); + } + std::shared_ptr instance; + uint64_t time; +@@ -61,9 +61,6 @@ public: + int get_cycle() { + return cycle; + } +- bool is_instance_exist(const std::string &name) { +- return memory_store.is_instance_exist(name); +- } + void recv_queue_push(InstanceRunMessage &msg) { + this->recv_queue.push(msg); + } +@@ -77,15 +74,14 @@ private: + void run_instance(std::shared_ptr instance); + void delete_instance(std::shared_ptr instance); + void insert_instance(std::shared_ptr instance, uint64_t time); +- void adjust_collector_queue(const std::vector &deps, const std::vector &m_deps, bool flag); +- void check_scenario_dependency(const std::vector &deps, const std::vector &m_deps); + ++ /* Instance execution queue. */ + std::priority_queue schedule_queue; ++ /*Receives messages from the PluginManager. */ + SafeQueue recv_queue; + MemoryStore &memory_store; + int cycle; + static const int DEFAULT_CYCLE_SIZE = 10; +- static const int MAX_DEPENDENCIES_SIZE = 20; + }; + + #endif // !PLUGIN_MGR_INSTANCE_RUN_HANDLER_H +diff --git a/src/plugin_mgr/interface.h b/src/plugin_mgr/interface.h +index 6495b92..8aa6933 100644 +--- a/src/plugin_mgr/interface.h ++++ b/src/plugin_mgr/interface.h +@@ -14,12 +14,6 @@ + #include + #include + +-enum PluginType { +- COLLECTOR, +- SCENARIO, +- TUNE, +-}; +- + struct DataBuf { + int len; + void *data; +@@ -43,10 +37,19 @@ struct Param { + + struct Interface { + const char* (*get_version)(); ++ /* The instance name is a unique identifier in the system. */ + const char* (*get_name)(); + const char* (*get_description)(); ++ /* Specifies the instance dependencies, which is used as the input information ++ * for instance execution. ++ */ + const char* (*get_dep)(); +- enum PluginType (*get_type)(); ++ /* Instance scheduling priority. In a uniform time period, a instance with a ++ * lower priority is scheduled first. ++ */ ++ int (*get_priority)(); ++ int (*get_type)(); ++ /* Instance execution period. */ + int (*get_period)(); + bool (*enable)(); + void (*disable)(); +diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h +index f6b5029..7415b99 100644 +--- a/src/plugin_mgr/plugin.h ++++ b/src/plugin_mgr/plugin.h +@@ -25,8 +25,8 @@ public: + std::string get_name() const { + return this->name; + } +- PluginType get_type() const { +- return interface->get_type(); ++ int get_priority() const { ++ return interface->get_priority(); + } + Interface* get_interface() const { + return this->interface; +-- +2.33.0 + diff --git a/0005-add-the-SIGINT-signal-processing-and-singleton-class.patch b/0005-add-the-SIGINT-signal-processing-and-singleton-class.patch new file mode 100644 index 0000000..551ba6f --- /dev/null +++ b/0005-add-the-SIGINT-signal-processing-and-singleton-class.patch @@ -0,0 +1,401 @@ +From 6b8debf93307ee68f2ecd4b0bf5ac2d01ee278b8 Mon Sep 17 00:00:00 2001 +From: fly_1997 +Date: Wed, 29 May 2024 15:12:31 +0800 +Subject: [PATCH 4/4] add the SIGINT signal processing and singleton classes + +--- + src/plugin_mgr/instance_run_handler.cpp | 1 - + src/plugin_mgr/logger.cpp | 2 +- + src/plugin_mgr/logger.h | 3 +- + src/plugin_mgr/main.cpp | 37 +++++++++++++++++++------ + src/plugin_mgr/memory_store.h | 2 +- + src/plugin_mgr/message_manager.cpp | 22 ++++++++++----- + src/plugin_mgr/message_manager.h | 32 +++++++++++---------- + src/plugin_mgr/plugin.h | 2 +- + src/plugin_mgr/plugin_manager.cpp | 26 ++++++++++------- + src/plugin_mgr/plugin_manager.h | 21 ++++++++++---- + 10 files changed, 97 insertions(+), 51 deletions(-) + +diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp +index fc9dc04..ecd7778 100644 +--- a/src/plugin_mgr/instance_run_handler.cpp ++++ b/src/plugin_mgr/instance_run_handler.cpp +@@ -35,7 +35,6 @@ void InstanceRunHandler::run_instance(std::shared_ptr instance) { + + void InstanceRunHandler::insert_instance(std::shared_ptr instance, uint64_t time) { + /* To check if an instance is enabled, enable() is called in the PluginManager. */ +- instance->set_enabled(true); + schedule_queue.push(ScheduleInstance{instance, time}); + INFO("[PluginManager] " << instance->get_name() << " instance insert into running queue."); + } +diff --git a/src/plugin_mgr/logger.cpp b/src/plugin_mgr/logger.cpp +index 7a924c2..af39583 100644 +--- a/src/plugin_mgr/logger.cpp ++++ b/src/plugin_mgr/logger.cpp +@@ -26,7 +26,7 @@ Logger::Logger() { + logger.addAppender(appender); + } + +-void Logger::init(Config *config) { ++void Logger::init(std::shared_ptr config) { + log4cplus::SharedAppenderPtr appender(new log4cplus::FileAppender(config->get_log_path() + "/server.log")); + appender->setName("file"); + log4cplus::tstring pattern = LOG4CPLUS_TEXT("%D{%m/%d/%y %H:%M:%S} [%t] %-5p %c - %m" +diff --git a/src/plugin_mgr/logger.h b/src/plugin_mgr/logger.h +index e86dd72..a219c86 100644 +--- a/src/plugin_mgr/logger.h ++++ b/src/plugin_mgr/logger.h +@@ -13,6 +13,7 @@ + #define PLUGIN_MGR_LOGGER_H + + #include "config.h" ++#include + #include + + #define INFO(fmt) LOG4CPLUS_INFO(logger.get(), fmt) +@@ -24,7 +25,7 @@ + class Logger { + public: + Logger(); +- void init(Config *config); ++ void init(std::shared_ptr config); + log4cplus::Logger get() { + return logger; + } +diff --git a/src/plugin_mgr/main.cpp b/src/plugin_mgr/main.cpp +index 698ba62..5cfb020 100644 +--- a/src/plugin_mgr/main.cpp ++++ b/src/plugin_mgr/main.cpp +@@ -10,6 +10,7 @@ + * See the Mulan PSL v2 for more details. + ******************************************************************************/ + #include "plugin_manager.h" ++#include + + Logger logger; + +@@ -20,8 +21,26 @@ void print_help() { + " ./oeaware /etc/oeAware/config.yaml\n"); + } + ++void signal_handler(int signum) { ++ auto &plugin_manager = PluginManager::get_instance(); ++ auto memory_store = plugin_manager.get_memory_store(); ++ auto all_plugins = memory_store.get_all_plugins(); ++ for (auto plugin : all_plugins) { ++ for (size_t i = 0; i < plugin->get_instance_len(); ++i) { ++ auto instance = plugin->get_instance(i); ++ if (!instance->get_enabled()) { ++ continue; ++ } ++ instance->get_interface()->disable(); ++ INFO("[PluginManager] " << instance->get_name() << " instance disabled."); ++ } ++ } ++ exit(signum); ++} ++ + int main(int argc, char **argv) { +- Config config; ++ signal(SIGINT, signal_handler); ++ std::shared_ptr config = std::make_shared(); + if (argc < 2) { + ERROR("System need a argument!"); + exit(EXIT_FAILURE); +@@ -39,20 +58,20 @@ int main(int argc, char **argv) { + ERROR("Insufficient permission on " << config_path); + exit(EXIT_FAILURE); + } +- if (!config.load(config_path)) { ++ if (!config->load(config_path)) { + ERROR("Config load error!"); + exit(EXIT_FAILURE); + } +- logger.init(&config); +- SafeQueue handler_msg; +- SafeQueue res_msg; ++ logger.init(config); ++ std::shared_ptr> handler_msg = std::make_shared>(); ++ std::shared_ptr> res_msg = std::make_shared>(); + INFO("[MessageManager] Start message manager!"); +- MessageManager message_manager(&handler_msg, &res_msg); +- message_manager.init(); ++ MessageManager &message_manager = MessageManager::get_instance(); ++ message_manager.init(handler_msg, res_msg); + message_manager.run(); + INFO("[PluginManager] Start plugin manager!"); +- PluginManager plugin_manager(config, handler_msg, res_msg); +- plugin_manager.init(); ++ PluginManager& plugin_manager = PluginManager::get_instance(); ++ plugin_manager.init(config, handler_msg, res_msg); + plugin_manager.run(); + return 0; + } +\ No newline at end of file +diff --git a/src/plugin_mgr/memory_store.h b/src/plugin_mgr/memory_store.h +index 999795a..0e862db 100644 +--- a/src/plugin_mgr/memory_store.h ++++ b/src/plugin_mgr/memory_store.h +@@ -42,7 +42,7 @@ public: + bool is_instance_exist(const std::string &name) { + return dep_handler.is_instance_exist(name); + } +- std::vector> get_all_plugins() { ++ const std::vector> get_all_plugins() { + std::vector> res; + for (auto &p : plugins) { + res.emplace_back(p.second); +diff --git a/src/plugin_mgr/message_manager.cpp b/src/plugin_mgr/message_manager.cpp +index 815122c..a9f862d 100644 +--- a/src/plugin_mgr/message_manager.cpp ++++ b/src/plugin_mgr/message_manager.cpp +@@ -57,7 +57,7 @@ bool TcpSocket::init() { + return true; + + } +-static void send_msg(Msg &msg, SafeQueue *handler_msg) { ++static void send_msg(Msg &msg, std::shared_ptr> handler_msg) { + Message message; + message.set_opt(msg.opt()); + message.set_type(MessageType::EXTERNAL); +@@ -66,7 +66,7 @@ static void send_msg(Msg &msg, SafeQueue *handler_msg) { + } + handler_msg->push(message); + } +-static void recv_msg(Msg &msg, SafeQueue *res_msg) { ++static void recv_msg(Msg &msg, std::shared_ptr> res_msg) { + Message res; + res_msg->wait_and_pop(res); + msg.set_opt(res.get_opt()); +@@ -75,7 +75,7 @@ static void recv_msg(Msg &msg, SafeQueue *res_msg) { + } + } + +-void TcpSocket::serve_accept(SafeQueue *handler_msg, SafeQueue *res_msg){ ++void TcpSocket::serve_accept(std::shared_ptr> handler_msg, std::shared_ptr> res_msg){ + struct epoll_event evs[MAX_EVENT_SIZE]; + int sz = sizeof(evs) / sizeof(struct epoll_event); + while (true) { +@@ -112,12 +112,20 @@ void TcpSocket::serve_accept(SafeQueue *handler_msg, SafeQueue + } + } + +-void handler(MessageManager *mgr) { +- TcpSocket* tcp_socket = mgr->tcp_socket; +- if (!tcp_socket->init()) { ++void MessageManager::tcp_start() { ++ if (!tcp_socket.init()) { + return; + } +- tcp_socket->serve_accept(mgr->handler_msg, mgr->res_msg); ++ tcp_socket.serve_accept(handler_msg, res_msg); ++} ++ ++static void handler(MessageManager *mgr) { ++ mgr->tcp_start(); ++} ++ ++void MessageManager::init(std::shared_ptr> handler_msg, std::shared_ptr> res_msg) { ++ this->handler_msg = handler_msg; ++ this->res_msg = res_msg; + } + + void MessageManager::run() { +diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h +index 95bbd1a..f5240aa 100644 +--- a/src/plugin_mgr/message_manager.h ++++ b/src/plugin_mgr/message_manager.h +@@ -61,34 +61,38 @@ private: + + class TcpSocket { + public: +- TcpSocket() {} ++ TcpSocket() : sock(-1), epfd(-1) { } + ~TcpSocket() { + close(sock); + } + bool init(); +- void serve_accept(SafeQueue *handler_msg, SafeQueue *res_msg); ++ void serve_accept(std::shared_ptr> handler_msg, std::shared_ptr> res_msg); + private: + int domain_listen(const char *name); +- ++private: + int sock; + int epfd; + }; + + class MessageManager { + public: +- MessageManager(SafeQueue *handler_msg, SafeQueue *res_msg) { +- this->handler_msg = handler_msg; +- this->res_msg = res_msg; +- this->tcp_socket = nullptr; +- } +- void init(){ +- this->tcp_socket = new TcpSocket(); ++ MessageManager(const MessageManager&) = delete; ++ MessageManager& operator=(const MessageManager&) = delete; ++ static MessageManager& get_instance() { ++ static MessageManager message_manager; ++ return message_manager; + } ++ void init(std::shared_ptr> handler_msg, std::shared_ptr> res_msg); ++ void tcp_start(); + void run(); +- +- SafeQueue *handler_msg; +- SafeQueue *res_msg; +- TcpSocket *tcp_socket; ++private: ++ MessageManager() { } ++private: ++ /* Message queue stores messages from the client and is consumed by PluginManager. */ ++ std::shared_ptr> handler_msg; ++ /* Message queue stores messages from PluginManager and is consumed by TcpSocket. */ ++ std::shared_ptr> res_msg; ++ TcpSocket tcp_socket; + }; + + #endif // !PLUGIN_MGR_MESSAGE_MANAGER_H +\ No newline at end of file +diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h +index 7415b99..a011261 100644 +--- a/src/plugin_mgr/plugin.h ++++ b/src/plugin_mgr/plugin.h +@@ -81,7 +81,7 @@ public: + void add_instance(std::shared_ptr ins) { + instances.emplace_back(ins); + } +- std::shared_ptr get_instance(int i) const { ++ std::shared_ptr get_instance(size_t i) const { + return instances[i]; + } + size_t get_instance_len() const { +diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp +index 7900ecf..4c5f5fc 100644 +--- a/src/plugin_mgr/plugin_manager.cpp ++++ b/src/plugin_mgr/plugin_manager.cpp +@@ -15,7 +15,12 @@ + + const static int ST_MODE_MASK = 0777; + +-void PluginManager::init() { ++ ++void PluginManager::init(std::shared_ptr config, std::shared_ptr> handler_msg, ++ std::shared_ptr> res_msg) { ++ this->config = config; ++ this->handler_msg = handler_msg; ++ this->res_msg = res_msg; + instance_run_handler.reset(new InstanceRunHandler(memory_store)); + pre_load(); + } +@@ -220,12 +225,13 @@ ErrorCode PluginManager::instance_enabled(const std::string &name) { + if (instance->get_enabled()) { + continue; + } ++ instance->set_enabled(true); + instance_run_handler->recv_queue_push(InstanceRunMessage(RunType::ENABLED, instance)); + } + return ErrorCode::OK; + } else { +- for (auto instance : new_enabled) { +- instance->get_interface()->disable(); ++ for (auto ins : new_enabled) { ++ ins->get_interface()->disable(); + } + return ErrorCode::ENABLE_INSTANCE_ENV; + } +@@ -275,7 +281,7 @@ std::string PluginManager::get_plugin_in_dir(const std::string &path) { + } + + ErrorCode PluginManager::add_list(std::string &res) { +- auto plugin_list = config.get_plugin_list(); ++ auto plugin_list = config->get_plugin_list(); + res += "Supported Packages:\n"; + for (auto &p : plugin_list) { + res += p.first + "\n"; +@@ -286,16 +292,16 @@ ErrorCode PluginManager::add_list(std::string &res) { + } + + ErrorCode PluginManager::download(const std::string &name, std::string &res) { +- if (!config.is_plugin_info_exist(name)) { ++ if (!config->is_plugin_info_exist(name)) { + return ErrorCode::DOWNLOAD_NOT_FOUND; + } +- res += config.get_plugin_info(name).get_url(); ++ res += config->get_plugin_info(name).get_url(); + return ErrorCode::OK; + } + + void PluginManager::pre_enable() { +- for (size_t i = 0; i < config.get_enable_list_size(); ++i) { +- EnableItem item = config.get_enable_list(i); ++ for (size_t i = 0; i < config->get_enable_list_size(); ++i) { ++ EnableItem item = config->get_enable_list(i); + if (item.get_enabled()) { + std::string name = item.get_name(); + if (!memory_store.is_plugin_exist(name)) { +@@ -394,7 +400,7 @@ int PluginManager::run() { + while (true) { + Message msg; + Message res; +- this->handler_msg.wait_and_pop(msg); ++ this->handler_msg->wait_and_pop(msg); + if (msg.get_opt() == Opt::SHUTDOWN) break; + switch (msg.get_opt()) { + case Opt::LOAD: { +@@ -557,7 +563,7 @@ int PluginManager::run() { + break; + } + if (msg.get_type() == MessageType::EXTERNAL) +- res_msg.push(res); ++ res_msg->push(res); + } + return 0; + } +diff --git a/src/plugin_mgr/plugin_manager.h b/src/plugin_mgr/plugin_manager.h +index 18d3f35..b7c04fe 100644 +--- a/src/plugin_mgr/plugin_manager.h ++++ b/src/plugin_mgr/plugin_manager.h +@@ -20,12 +20,21 @@ + + class PluginManager { + public: +- PluginManager(Config &config, SafeQueue &handler_msg, SafeQueue &res_msg) : +- config(config), handler_msg(handler_msg), res_msg(res_msg) { } ++ PluginManager(const PluginManager&) = delete; ++ PluginManager& operator=(const PluginManager&) = delete; ++ static PluginManager& get_instance() { ++ static PluginManager plugin_manager; ++ return plugin_manager; ++ } + int run(); +- void init(); ++ void init(std::shared_ptr config, std::shared_ptr> handler_msg, ++ std::shared_ptr> res_msg); ++ const MemoryStore& get_memory_store() { ++ return this->memory_store; ++ } + const void* get_data_buffer(const std::string &name); + private: ++ PluginManager() { } + void pre_load(); + void pre_enable(); + void pre_load_plugin(); +@@ -50,9 +59,9 @@ private: + std::string get_plugin_in_dir(const std::string &path); + private: + std::unique_ptr instance_run_handler; +- Config &config; +- SafeQueue &handler_msg; +- SafeQueue &res_msg; ++ std::shared_ptr config; ++ std::shared_ptr> handler_msg; ++ std::shared_ptr> res_msg; + MemoryStore memory_store; + }; + +-- +2.33.0 + diff --git a/oeAware-manager.spec b/oeAware-manager.spec index 70d3e2b..85534c4 100644 --- a/oeAware-manager.spec +++ b/oeAware-manager.spec @@ -1,11 +1,15 @@ Name: oeAware-manager Version: v1.0.1 -Release: 2 +Release: 3 Summary: OeAware server and client License: MulanPSL2 URL: https://gitee.com/openeuler/%{name} Source0: %{name}-%{version}.tar.gz Patch1: 0001-fix-issues.patch +Patch2: 0002-refactor-plugin-interface.patch +Patch3: 0003-the-client-adapts-to-the-new-interface-and-refactor-.patch +Patch4: 0004-modify-some-interfaces-and-add-interface-comments.patch +Patch5: 0005-add-the-SIGINT-signal-processing-and-singleton-class.patch BuildRequires: cmake make gcc-c++ BuildRequires: boost-devel @@ -14,8 +18,9 @@ BuildRequires: log4cplus-devel BuildRequires: yaml-cpp-devel BuildRequires: gtest-devel gmock-devel -Requires: oeAware-collector >= v1.0.0 -Requires: oeAware-scenario >= v1.0.0 +Requires: oeAware-collector >= v1.0.2 +Requires: oeAware-scenario >= v1.0.2 +Requires: oeAware-tune >= v1.0.0 Requires: graphviz %description @@ -49,6 +54,9 @@ install -D -p -m 0644 oeaware.service %{buildroot}%{_unitdir}/oeaware.service %attr(0644, root, root) %{_unitdir}/oeaware.service %changelog +* Fri May 31 2024 fly_1997 -v1.0.1-3 +- refactor instance interfaces and add signal function + * Wed May 15 2024 fly_1997 -v1.0.1-2 - fix pre-enable failed, dependencies missing error, memory leak - fix warning message