Home /Modules/vm/SLVmTelemetry.sleela

SLVmTelemetry.sleela

lib/vm/src/sleela/SLVmTelemetry.sleela · 137 line(s) · sleela

<< lib/vm/src/sleela/SLVmTelemetry.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLVmTelemetry — the OOD equivalent of the native
     observability layer (virtual-machine/vm-version/004/vm_telemetry.{h,c},
     v004). It holds VM-wide monotonic counters (steps, allocs, bridge calls,
     errors) and a saturation gauge per bounded handle table (best-of 32,
     synchro 64, munction 64, ai, canvas), with a high-water mark and an
     edge-triggered saturation watch that raises an EXHAUSTED result through
     SLVmResult/SLVmReport when a table crosses its watermark. The native layer
     is lock-free C11 atomics; on the Sleela surface (no arrays) the four
     counters and the watched tables are expanded into numbered fields.
   
   Mirrors: enum VmCounter, enum VmTable, vm_telemetry_add/get/gauge/report.
   
   Family: vm. Composes SLVmResult, SLVmReport. Tables use numbered fields in
     place of arrays, matching the SLVM binding-table convention. >>
#sleela 2.5
material;
class SLVmTelemetry {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11293;
  String metaAuthor = "unknown";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "unknown";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  < counters (mirror VmCounter) >
  int steps; int allocs; int bridge; int errors;

  < per-table gauges, numbered (mirror VmTable: bestof, synchro, munction, ai, canvas) >
  int capBestof;  int liveBestof;  int peakBestof;  int warnedBestof;
  int capSynchro; int liveSynchro; int peakSynchro; int warnedSynchro;
  int capMunction;int liveMunction;int peakMunction;int warnedMunction;
  int capAi;      int liveAi;      int peakAi;      int warnedAi;
  int capCanvas;  int liveCanvas;  int peakCanvas;  int warnedCanvas;

  < default 80 >
  int watermarkPct;

  void init() {
    steps = 0; allocs = 0; bridge = 0; errors = 0;
    capBestof = 0; liveBestof = 0; peakBestof = 0; warnedBestof = 0;
    capSynchro = 0; liveSynchro = 0; peakSynchro = 0; warnedSynchro = 0;
    capMunction = 0; liveMunction = 0; peakMunction = 0; warnedMunction = 0;
    capAi = 0; liveAi = 0; peakAi = 0; warnedAi = 0;
    capCanvas = 0; liveCanvas = 0; peakCanvas = 0; warnedCanvas = 0;
    watermarkPct = 80;
  }

  void setWatermark(int pct) {
    if (pct < 0) { pct = 0; }
    if (pct > 100) { pct = 100; }
    watermarkPct = pct;
  }

  < counter bumps (mirror vm_telemetry_add) >
  void addSteps(int n)  { steps = steps + n; }
  void addAllocs(int n) { allocs = allocs + n; }
  void addBridge(int n) { bridge = bridge + n; }
  void addErrors(int n) { errors = errors + n; }

  < register a table capacity (0=bestof 1=synchro 2=munction 3=ai 4=canvas) >
  void setCapacity(int table, int cap) {
    if (table == 0) { capBestof = cap; }
    else if (table == 1) { capSynchro = cap; }
    else if (table == 2) { capMunction = cap; }
    else if (table == 3) { capAi = cap; }
    else if (table == 4) { capCanvas = cap; }
  }

  < update a gauge; edge-trigger an EXHAUSTED result on crossing the watermark. >
  void gauge(int table, int live, SLVmResult res, SLVmReport rep) {
    if (live < 0) { live = 0; }
    int cap = 0; int peak = 0; int warned = 0; String name = "table";
    if (table == 0) { liveBestof = live; cap = capBestof; if (live > peakBestof) { peakBestof = live; } peak = peakBestof; warned = warnedBestof; name = "best-of"; }
    else if (table == 1) { liveSynchro = live; cap = capSynchro; if (live > peakSynchro) { peakSynchro = live; } peak = peakSynchro; warned = warnedSynchro; name = "synchro"; }
    else if (table == 2) { liveMunction = live; cap = capMunction; if (live > peakMunction) { peakMunction = live; } peak = peakMunction; warned = warnedMunction; name = "munction"; }
    else if (table == 3) { liveAi = live; cap = capAi; if (live > peakAi) { peakAi = live; } peak = peakAi; warned = warnedAi; name = "ai"; }
    else if (table == 4) { liveCanvas = live; cap = capCanvas; if (live > peakCanvas) { peakCanvas = live; } peak = peakCanvas; warned = warnedCanvas; name = "canvas"; }
    if (cap <= 0) { return; }
    int pct = (live * 100) / cap;
    if (pct >= watermarkPct && warned == 0) {
      if (res != null) {
        < 6 = 6 (literal: a sibling class static cannot be read through an instance) >
        res.fail(6, "telemetry", name + " table near capacity: " + live + "/" + cap);
        res.report(rep);
      }
      errors = errors + 1;
      setWarned(table, 1);
    } else if (pct < watermarkPct && warned == 1) {
      setWarned(table, 0);
    }
  }

  void setWarned(int table, int v) {
    if (table == 0) { warnedBestof = v; }
    else if (table == 1) { warnedSynchro = v; }
    else if (table == 2) { warnedMunction = v; }
    else if (table == 3) { warnedAi = v; }
    else if (table == 4) { warnedCanvas = v; }
  }
}