myxce/src/lib/resobj.c
Rongsong Shen e3b3ffaa97
fix(controller,xceadmin): fix zero-slot scheduling block, misleading resval logs, byte-unit display
Three bugs found while diagnosing a live controller log, all in the
worker resource-value path:

1. Every host relying on the documented hosts.yaml auto-detect default
   (no max_slot, meaning "use CPU count") got 0 scheduling slots,
   permanently blocking the scheduler from placing jobs on it. mxj is
   UInt32 on the wire and can't carry the schema's -1 sentinel; mapping.py
   worked around this by leaving it unset, which defaults to 0 -- and 0 is
   never < 0, so apply_join_state()'s auto-detect fallback never fired.
   Fixed by encoding the sentinel as 0xFFFFFFFF, which the controller's
   `int mxj` field reinterprets back to -1 via its existing implicit
   uint32_t->int conversion. No C-side or wire schema change needed for
   this part; the fallback itself was already correct but had zero test
   coverage for a negative mxj, now added.

2. update_resvals()/update_resval_list() logged the failure reason via
   PERROR, which reads process-global errno -- but update_resval()'s
   failure paths return raw negative codes directly and never touch
   errno, so the controller log filled with misleading "update_resval:
   Success" / "update_resval: Resource temporarily unavailable" lines
   instead of the real reason. Fixed to decode the actual rc via
   xce_strerror_r and name the failing resource.

3. maxmem is canonically stored/wired as raw bytes but had no unit
   metadata, so resval/workerlist printed it as a bare number that read
   as garbage (e.g. workerlist's fixed-width columns visually smeared a
   multi-GB byte count across the MaxCPU/MaxMem headers). Added an
   optional `unit` field to the Resource/ResourceVal capnp structs (and
   xce_resource_t/xce_resval_t/resources.yaml) so resval and workerlist
   can render a human-readable value (e.g. "30.90 GB") while the stored
   value stays raw bytes -- display-only, every other resource unaffected.

Regenerated the xceadmin golden fixture (fixtures/full/expected.bin)
twice: once for the mxj sentinel encoding change, once for the new
Resource.unit field growing every resource's wire size by one pointer
slot. Both diffs verified byte-for-byte isolated to exactly the expected
change before regenerating.

Verified: 1095/1095 controller_unit_test (stable across repeated runs),
229/229 xceadmin pytest, clean rebuild of lib/controller/xceadmin/xce_test.
Two related findings logged to .wolf/buglog.json but deliberately not
fixed here (out of scope per discussion): a pre-existing flaky segfault
in the combined controller_unit_test binary, and maxswap/maxtmp being
hardcoded stub values on the worker side.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-13 08:12:09 +00:00

153 lines
3.3 KiB
C

#include <errno.h>
#include <rtti.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include "mylock.h"
#include "resobj.h"
#include "utils.h"
#include "xstring.h"
typedef struct {
atomic_ulong refcount;
} resobj_private_t;
static size_t resobj_datasize(va_list ap)
{
size_t sz = sizeof(xce_resource_t);
return sz;
}
static int resobj_init(meta_obj_t* obj, va_list ap)
{
xce_resource_t* dst = RTTI_OBJ_TO_DATA(xce_resource_t, obj);
resobj_private_t* priv = (resobj_private_t*)RTTI_OBJ_TO_PRIV(obj);
xce_resource_t* src = va_arg(ap, xce_resource_t*);
if (src == NULL) {
return -EINVAL;
}
memcpy(dst, src, sizeof(*dst));
dst->name = makeString(src->name);
dst->desc = makeString(src->desc);
dst->unit = makeString(src->unit);
#if 0
if (src->distributions != NULL) {
MOVE_PTR(dst->distributions, src->distributions);
}
#else
/* FIXME: */
dst->distributions = NULL;
#endif
#if 0
MOVE_PTR(dst->hbased_defined, src->hbased_defined);
#else
/* FIXME: */
dst->hbased_defined = NULL;
#endif
priv->refcount = 1;
return 1;
}
static void resobj_destroy(meta_obj_t* obj)
{
xce_resource_t* r = RTTI_OBJ_TO_DATA(xce_resource_t, obj);
resobj_private_t* priv = (resobj_private_t*)RTTI_OBJ_TO_PRIV(obj);
dereference(r->name);
dereference(r->desc);
dereference(r->unit);
memset(priv, 0x0, sizeof(*priv));
return;
}
static int resobj_reference(meta_obj_t* obj)
{
resobj_private_t* priv = (resobj_private_t*)RTTI_OBJ_TO_PRIV(obj);
atomic_fetch_add(&(priv->refcount), 1);
return 1;
}
static void resobj_dereference(meta_obj_t* obj)
{
resobj_private_t* priv = (resobj_private_t*)RTTI_OBJ_TO_PRIV(obj);
xce_resource_t* w = RTTI_OBJ_TO_DATA(xce_resource_t, obj);
uint64_t count = 0;
count = atomic_fetch_sub(&(priv->refcount), 1);
if (count == 1) {
destroy_(w, (obj_destroy_fn_t)resobj_destroy);
}
return;
}
static typeinfo_t resobj_type
= { .name = "xce resobj",
.privateSize = sizeof(resobj_private_t),
.dataSize = (datasize_fn_t)resobj_datasize,
.init = (obj_init_fn_t)resobj_init,
.destroy = NULL,
.interfaces
= { .reference = &(intf_reference_t) {
.reference = (reference_fn_t)resobj_reference,
.dereference = (dereference_fn_t)resobj_dereference } } };
static xce_resource_t* makeResource_(var resmgr, xce_resource_t* r)
{
xce_resource_t* resobj = NULL;
int rc;
rc = key_find(resmgr, (void*)r->name, (void**)&resobj);
if ((rc < 0) && (rc != -ENOENT)) {
errno = -rc;
return NULL;
}
if (rc > 0) {
errno = EEXIST;
return NULL;
}
resobj = makeInstance(&resobj_type, r);
if (resobj != NULL) {
rc = key_add(resmgr, (void*)resobj->name, (void*)resobj);
if (rc < 0) {
destroy(resobj);
errno = -rc;
return NULL;
}
resobj->idx = rc;
}
return resobj;
}
xce_resource_t* makeResource(var resmgr, xce_resource_t* r)
{
xce_resource_t* r1 = NULL;
lock(resmgr, RTTI_LOCK_WRITE);
r1 = makeResource_(resmgr, r);
unlock(resmgr);
return r1;
}