myxce/src/scheduler/src/plugin.c
2026-01-15 17:56:53 +08:00

399 lines
8.5 KiB
C

#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "plugin.h"
#include "scheduler.h"
#include "xlist.h"
int plugin_register(void* arg1, void* arg2)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg1;
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
int rc = 0;
if ((sched == NULL) && (plugin == NULL)) {
return -EINVAL;
}
plugin->sched = sched;
if (plugin->init_fn != NULL) {
rc = plugin->init_fn(plugin);
}
if (rc < 0) {
return rc;
}
rc = kvmgr_add(&(sched->plugins), (void*)plugin->name, (void*)plugin);
if (rc >= 0) {
plugin->plugin_id = rc;
}
return rc;
}
void plugin_unregister(void* arg)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg;
xce_scheduler_t* sched = NULL;
if (plugin == NULL) {
return;
}
sched = (xce_scheduler_t*)plugin->sched;
if (sched == NULL) {
return;
}
kvmgr_del(&(sched->plugins), plugin->name);
if (plugin->fini_fn != NULL) {
plugin->fini_fn(plugin);
}
}
static int plugin_config_update_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
xce_conf4sched_t* config = (xce_conf4sched_t*)arg1;
if ((arg1 == NULL) || (plugin == NULL)) {
return 0;
}
if (plugin->hooks.config_hook != NULL) {
return plugin->hooks.config_hook(plugin, config);
}
return 0;
}
int plugin_config_update(void* arg, xce_conf4sched_t* config)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_config_update_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, config);
xlist_fini(&lst);
return rc;
}
static int plugin_job_update_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
xce_sched_job_t* job = (xce_sched_job_t*)arg1;
if ((arg1 == NULL) || (plugin == NULL)) {
return 0;
}
if (plugin->hooks.job_hook != NULL) {
return plugin->hooks.job_hook(plugin, job);
}
return 0;
}
int plugin_job_update(void* arg, xce_sched_job_t* job)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_job_update_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, job);
xlist_fini(&lst);
return rc;
}
static int plugin_queue_update_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
xce_sched_queue_t* queue = (xce_sched_queue_t*)arg1;
if ((arg1 == NULL) || (plugin == NULL)) {
return 0;
}
if (plugin->hooks.queue_hook != NULL) {
return plugin->hooks.queue_hook(plugin, queue);
}
return 0;
}
int plugin_queue_update(void* arg, xce_sched_queue_t* queue)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_queue_update_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, queue);
xlist_fini(&lst);
return rc;
}
static int plugin_worker_update_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
xce_sched_worker_t* worker = (xce_sched_worker_t*)arg1;
if ((arg1 == NULL) || (plugin == NULL)) {
return 0;
}
if (plugin->hooks.worker_hook != NULL) {
return plugin->hooks.worker_hook(plugin, worker);
}
return 0;
}
int plugin_worker_update(void* arg, xce_sched_worker_t* worker)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_worker_update_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, worker);
xlist_fini(&lst);
return rc;
}
static int plugin_begin_session_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
if (plugin == NULL) {
return 0;
}
if (plugin->begin_session_fn != NULL) {
return plugin->begin_session_fn(plugin);
}
return 0;
}
int plugin_begin_session(void* arg)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_begin_session_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, NULL);
xlist_fini(&lst);
return rc;
}
static int plugin_end_session_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
if (plugin == NULL) {
return 0;
}
if (plugin->end_session_fn != NULL) {
return plugin->end_session_fn(plugin);
}
return 0;
}
int plugin_end_session(void* arg)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_end_session_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, NULL);
xlist_fini(&lst);
return rc;
}
static int plugin_commit_decision_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
if (plugin == NULL) {
return 0;
}
if (plugin->hooks.commit_hook != NULL) {
return plugin->hooks.commit_hook(plugin, arg1);
}
return 0;
}
int plugin_commit_decision(void* arg1, xce_decision_t* decision)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg1;
xlist_t lst;
int rc;
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_commit_decision_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, decision);
xlist_fini(&lst);
return rc;
}
typedef struct {
xce_sched_job_t* job;
uint32_t state;
} job_state_change_t;
static int plugin_job_state_change_(void* arg1, void* arg2)
{
xce_sched_plugin_t* plugin = (xce_sched_plugin_t*)arg2;
job_state_change_t* args = (job_state_change_t*)arg1;
if ((arg1 == NULL) || (plugin == NULL)) {
return 0;
}
if (plugin->hooks.jstate_hook != NULL) {
return plugin->hooks.jstate_hook(plugin, args->job, args->state);
}
return 0;
}
int plugin_job_state_change(void* arg, xce_sched_job_t* job, uint32_t state)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
job_state_change_t args = { .job = job, .state = state };
if (sched == NULL) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_job_state_change_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, &args);
xlist_fini(&lst);
return rc;
}
static int plugin_job_election_(xce_sched_ctx_t* ctx,
xce_sched_plugin_t* plugin)
{
if ((plugin == NULL) || (ctx == NULL)) {
return 0;
}
if (plugin->hooks.job_election_hook != NULL) {
return plugin->hooks.job_election_hook(plugin, ctx);
}
return 0;
}
int plugin_job_election(void* arg, xce_sched_ctx_t* ctx)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if ((sched == NULL) || (ctx == NULL)) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_job_election_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, ctx);
xlist_fini(&lst);
return rc;
}
static int plugin_prepare_allocation_(xce_sched_ctx_t* ctx,
xce_sched_plugin_t* plugin)
{
if ((plugin == NULL) || (ctx == NULL)) {
return 0;
}
if (plugin->hooks.job_election_hook != NULL) {
return plugin->hooks.prepare_allocation_hook(plugin, ctx);
}
return 0;
}
int plugin_prepare_allocation(void* arg, xce_sched_ctx_t* ctx)
{
xce_scheduler_t* sched = (xce_scheduler_t*)arg;
xlist_t lst;
int rc;
if ((sched == NULL) || (ctx == NULL)) {
return -EINVAL;
}
xlist_init(&lst, 3);
xlist_store(&lst, plugin_prepare_allocation_);
rc = kvmgr_iter(&(sched->plugins), NULL, &lst, ctx);
xlist_fini(&lst);
return rc;
}