#include"backend/backend_engine.h"// Define the plugin API version#define NIXL_PLUGIN_API_VERSION 1// Define the plugin interface classclassnixlBackendPlugin{public:intapi_version;// Function pointer for creating a new backend engine instancenixlBackendEngine*(*create_engine)(constnixlBackendInitParams*init_params);// Function pointer for destroying a backend engine instancevoid(*destroy_engine)(nixlBackendEngine*engine);// Function to get the plugin nameconstchar*(*get_plugin_name)();// Function to get the plugin versionconstchar*(*get_plugin_version)();// Function to get backend optionsnixl_b_params_t(*get_backend_options)();// Function to get supported backend mem typesnixl_mem_list_t(*get_backend_mems)();};// Macro to define exported C functions for the plugin#define NIXL_PLUGIN_EXPORT __attribute__((visibility("default")))// Creator Function type for static pluginstypedefnixlBackendPlugin*(*nixlStaticPluginCreatorFunc)();// Plugin must implement these functions for dynamic loadingextern"C"{// Initialize the pluginNIXL_PLUGIN_EXPORTnixlBackendPlugin*nixl_plugin_init();// Cleanup the pluginNIXL_PLUGIN_EXPORTvoidnixl_plugin_fini();}
nixlPluginManager&nixlPluginManager::getInstance(){// Meyers singleton initialization is safe in multi-threaded environment.// Consult standard [stmt.dcl] chapter for details.staticnixlPluginManagerinstance;returninstance;}
classnixlPluginHandle{private:void*handle_;// Handle to the dynamically loaded librarynixlBackendPlugin*plugin_;// Plugin interfacepublic:nixlPluginHandle(void*handle,nixlBackendPlugin*plugin);~nixlPluginHandle();nixlBackendEngine*createEngine(constnixlBackendInitParams*init_params)const;voiddestroyEngine(nixlBackendEngine*engine)const;constchar*getName()const;constchar*getVersion()const;nixl_b_params_tgetBackendOptions()const;nixl_mem_list_tgetBackendMems()const;};
// Creator Function for static pluginstypedefnixlBackendPlugin*(*nixlStaticPluginCreatorFunc)();// Structure to hold static plugin infostructnixlStaticPluginInfo{constchar*name;nixlStaticPluginCreatorFunccreateFunc;};
voidnixlPluginManager::registerStaticPlugin(constchar*name,nixlStaticPluginCreatorFunccreator){lock_guardlg(lock);nixlStaticPluginInfoinfo;info.name=name;info.createFunc=creator;static_plugins_.push_back(info);//Static Plugins are considered pre-loadednixlBackendPlugin*plugin=info.createFunc();NIXL_INFO<<"Loading static plugin: "<<name;if(plugin){// Register the loaded pluginautoplugin_handle=std::make_shared<constnixlPluginHandle>(nullptr,plugin);loaded_plugins_[name]=plugin_handle;}}
std::shared_ptr<constnixlPluginHandle>nixlPluginManager::loadPluginFromPath(conststd::string&plugin_path){// Open the plugin filevoid*handle=dlopen(plugin_path.c_str(),RTLD_NOW|RTLD_LOCAL);if(!handle){NIXL_ERROR<<"Failed to load plugin from "<<plugin_path<<": "<<dlerror();returnnullptr;}// Get the initialization functiontypedefnixlBackendPlugin*(*init_func_t)();init_func_tinit=(init_func_t)dlsym(handle,"nixl_plugin_init");if(!init){NIXL_ERROR<<"Failed to find nixl_plugin_init in "<<plugin_path<<": "<<dlerror();dlclose(handle);returnnullptr;}// Call the initialization functionnixlBackendPlugin*plugin=init();if(!plugin){NIXL_ERROR<<"Plugin initialization failed for "<<plugin_path;dlclose(handle);returnnullptr;}// Check API versionif(plugin->api_version!=NIXL_PLUGIN_API_VERSION){NIXL_ERROR<<"Plugin API version mismatch for "<<plugin_path<<": expected "<<NIXL_PLUGIN_API_VERSION<<", got "<<plugin->api_version;dlclose(handle);returnnullptr;}// Create and store the plugin handleautoplugin_handle=std::make_shared<constnixlPluginHandle>(handle,plugin);returnplugin_handle;}
// Function to create a new POSIX backend engine instancestaticnixlBackendEngine*create_posix_engine(constnixlBackendInitParams*init_params){returnnewnixlPosixEngine(init_params);}// 资源的正确释放staticvoiddestroy_posix_engine(nixlBackendEngine*engine){deleteengine;}// Function to get the plugin name,插件名称staticconstchar*get_plugin_name(){return"POSIX";}// Function to get the plugin version,插件版本staticconstchar*get_plugin_version(){return"0.1.0";}// Function to get backend optionsstaticnixl_b_params_tget_backend_options(){nixl_b_params_tparams;returnparams;}// Function to get supported backend mem typesstaticnixl_mem_list_tget_backend_mems(){return{DRAM_SEG,FILE_SEG};}