myxce/src/lib/xcealloc.c

207 lines
4.7 KiB
C

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "xce/types.h"
#include "xcealloc.h"
xce_alloc_t* make_alloc(char* resname, char* reserve, uint64_t instid,
float value, int duration)
{
xce_alloc_t* alloc = NULL;
alloc = (xce_alloc_t*)calloc(1, sizeof(*alloc));
if (alloc == NULL) {
return NULL;
}
alloc->resname = strdup(resname);
if (reserve != NULL) {
alloc->reserve = strdup(reserve);
} else {
alloc->reserve = strdup("");
}
alloc->instid = instid;
alloc->value = value;
alloc->from = time(NULL);
alloc->duration = duration;
return alloc;
}
static xce_res_account_t* make_res_account(xce_alloc_t* alloc)
{
xce_res_account_t* account = NULL;
account = (xce_res_account_t*)calloc(1, sizeof(*account));
if (account == NULL) {
return NULL;
}
account->instid = alloc->instid;
account->resname = strdup(alloc->resname);
account->residx = -1;
account->used = 0.0;
account->reserved = alloc->value;
account->allocated = alloc->value;
return account;
}
xce_alloc_t** make_allocs(int nallocs, ...)
{
xce_alloc_t** allocs = NULL;
int i;
va_list ap;
if (nallocs <= 0) {
return NULL;
}
allocs = (xce_alloc_t**)calloc(nallocs, sizeof(*allocs));
if (allocs == NULL) {
return NULL;
}
va_start(ap, nallocs);
for (i = 0; i < nallocs; i++) {
xce_alloc_t* alloc = va_arg(ap, xce_alloc_t*);
allocs[i] = alloc;
}
va_end(ap);
return allocs;
}
xce_host_alloc_t* make_host_alloc(char* host, int nalloc, xce_alloc_t** allocs)
{
xce_host_alloc_t* halloc = NULL;
halloc = (xce_host_alloc_t*)calloc(1, sizeof(*halloc));
if (halloc == NULL) {
return NULL;
}
halloc->nallocs = nalloc;
halloc->allocs = allocs;
halloc->host = strdup(host);
return halloc;
}
static xce_host_account_t* make_host_account(xce_host_alloc_t* halloc)
{
xce_host_account_t* haccount = NULL;
int i;
haccount = (xce_host_account_t*)calloc(1, sizeof(*haccount));
if (haccount == NULL) {
return NULL;
}
haccount->accounts = (xce_res_account_t**)calloc(
halloc->nallocs, sizeof(xce_res_account_t*));
if (haccount->accounts == NULL) {
FREEUP(haccount);
return NULL;
}
for (i = 0; i < halloc->nallocs; i++) {
haccount->accounts[i] = make_res_account(halloc->allocs[i]);
}
haccount->naccount = halloc->nallocs;
haccount->host = strdup(halloc->host);
return haccount;
}
xce_host_alloc_t** make_host_allocs(int nhosts, ...)
{
xce_host_alloc_t** hallocs = NULL;
int i;
va_list ap;
if (nhosts <= 0) {
return NULL;
}
hallocs = (xce_host_alloc_t**)calloc(1, sizeof(*hallocs));
if (hallocs == NULL) {
return NULL;
}
va_start(ap, nhosts);
for (i = 0; i < nhosts; i++) {
xce_host_alloc_t* halloc = va_arg(ap, xce_host_alloc_t*);
hallocs[i] = halloc;
}
va_end(ap);
return hallocs;
}
xce_job_alloc_t* make_job_alloc(uint64_t jobid, int nhost,
xce_host_alloc_t** hallocs)
{
xce_job_alloc_t* jalloc = NULL;
jalloc = (xce_job_alloc_t*)calloc(1, sizeof(*jalloc));
if (jalloc == NULL) {
return NULL;
}
jalloc->jobid = jobid;
jalloc->nhosts = nhost;
jalloc->allocs = hallocs;
jalloc->nres_shared = 0;
jalloc->allocs_shared = NULL;
return jalloc;
}
xce_job_account_t* make_job_account(xce_job_alloc_t* jalloc)
{
xce_job_account_t* account = NULL;
int i;
account = (xce_job_account_t*)calloc(1, sizeof(*account));
if (account == NULL) {
return NULL;
}
account->accounts
= (xce_host_account_t**)calloc(1, sizeof(xce_host_account_t*));
if (account->accounts == NULL) {
FREEUP(account);
return NULL;
}
for (i = 0; i < jalloc->nhosts; i++) {
account->accounts[i] = make_host_account(jalloc->allocs[i]);
}
account->nhosts = jalloc->nhosts;
account->accounts_shared = NULL;
if (jalloc->nres_shared) {
account->accounts_shared = (xce_res_account_t**)calloc(
jalloc->nres_shared, sizeof(xce_res_account_t*));
for (i = 0; i < jalloc->nres_shared; i++) {
account->accounts_shared[i]
= make_res_account(jalloc->allocs_shared[i]);
}
account->nres_shared = jalloc->nres_shared;
}
return account;
}