forked from lei.chen/myxce
128 lines
3.3 KiB
C
128 lines
3.3 KiB
C
#include <errno.h>
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "decision.h"
|
|
#include "plugin.h"
|
|
#include "preason.h"
|
|
#include "scheduler.h"
|
|
#include "utils.h"
|
|
#include "xce/types.h"
|
|
#include "xcealloc.h"
|
|
|
|
static uint32_t maxslots = 8;
|
|
static uint32_t slotsInUse = 0;
|
|
|
|
static int default_allocator_init(xce_sched_plugin_t* plugin) { return 1; }
|
|
|
|
static void default_allocator_fini(xce_sched_plugin_t* plugin)
|
|
{
|
|
if (plugin == NULL) {
|
|
return;
|
|
}
|
|
FREEUP(plugin);
|
|
}
|
|
|
|
static int my_update_job(xce_sched_plugin_t* plugin, xce_sched_job_t* jold,
|
|
xce_sched_job_t* j)
|
|
{
|
|
/* FIXME: we need to add logic to check whether the job should be handled by
|
|
* me */
|
|
j->allocator = plugin;
|
|
switch (j->state) {
|
|
case JOB_STAT_PEND:
|
|
break;
|
|
case JOB_STAT_RUN:
|
|
break;
|
|
case JOB_STAT_DONE:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static xce_job_alloc_t* my_alloc(xce_sched_plugin_t* plugin, int64_t jobid,
|
|
xce_pendreason_t** preason)
|
|
{
|
|
xce_job_alloc_t* jalloc = NULL;
|
|
|
|
*preason = NULL;
|
|
|
|
if (slotsInUse < maxslots) {
|
|
xce_alloc_t* alloc = NULL;
|
|
xce_alloc_t** allocs = NULL;
|
|
xce_host_alloc_t* halloc = NULL;
|
|
xce_host_alloc_t** hallocs = NULL;
|
|
|
|
alloc = make_alloc("slots", "", -1, 1.0, -1);
|
|
allocs = make_allocs(1, alloc);
|
|
halloc = make_host_alloc("pc13v", 1, allocs);
|
|
hallocs = make_host_allocs(1, halloc);
|
|
|
|
jalloc = make_job_alloc(jobid, 1, hallocs);
|
|
} else {
|
|
xce_pendreason_t* reason = NULL;
|
|
|
|
reason = make_pendreason(jobid);
|
|
add_job_preason(reason->reasons, PREASON_NO_RESOURCE, "pc13v", "slots");
|
|
|
|
*preason = reason;
|
|
}
|
|
|
|
return jalloc;
|
|
}
|
|
|
|
static int my_commit(xce_sched_plugin_t* plugin, xce_decision_t* decision)
|
|
{
|
|
int rc = 0;
|
|
uint64_t jobid = 0;
|
|
xce_sched_job_t* job = NULL;
|
|
|
|
switch (decision->op) {
|
|
case XCE_DECISION_DISPATCH:
|
|
/* FIXME: */
|
|
jobid = decision->u.alloc->jobid;
|
|
job = sched_get_job(plugin->sched, jobid);
|
|
if (job != NULL) {
|
|
slotsInUse++;
|
|
job->starting = 1;
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
int register_default_allocator(void* sched)
|
|
{
|
|
static xce_sched_plugin_t default_allocator_plugin = {
|
|
.name = "xce_sched_default_allocator",
|
|
.hooks = { .config_hook = NULL,
|
|
.job_hook = (plugin_update_job_hook_t)my_update_job,
|
|
.commit_hook = (plugin_commit_decision_hook_t)my_commit },
|
|
.init_fn = (plugin_init_fn_t)default_allocator_init,
|
|
.fini_fn = (plugin_fini_fn_t)default_allocator_fini,
|
|
.begin_session_fn = NULL,
|
|
.end_session_fn = NULL,
|
|
.job_elect_fn = NULL,
|
|
.job_alloc_fn = (plugin_job_allocate_fn_t)my_alloc,
|
|
.data = NULL
|
|
};
|
|
|
|
xce_sched_plugin_t* plugin = NULL;
|
|
|
|
plugin = (xce_sched_plugin_t*)calloc(1, sizeof(*plugin));
|
|
if (plugin == NULL) {
|
|
return -ENOMEM;
|
|
}
|
|
|
|
memcpy(plugin, &default_allocator_plugin, sizeof(*plugin));
|
|
|
|
return plugin_register(sched, plugin);
|
|
}
|