interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / runtime / rx_regulate.h

126 lines · 6178 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* ADAPTIVE-M4: bounded resource regulation as a resident reaction.
18 *
19 * The World already prices admission against an RxResourceBudget
20 * (rx_world_set_resources: slots, memory, energy, fairness bound) and keeps
21 * reactions stable (RxStabilityBudget). Nothing in the runtime adapts that
22 * budget to what the machine is going through. This faculty closes that loop
23 * without touching the scheduler:
24 *
25 * condition object (written from outside: a sensor, a supervisor, a test)
26 * | trigger
27 * regulate.budget (one reaction: reads the condition and the current
28 * | policy, decides a regime with hysteresis, publishes
29 * v a bounded policy)
30 * policy object (written only by the regulator)
31 * | rx_regulate_apply (host step, never inside a reaction)
32 * v
33 * rx_world_set_resources
34 *
35 * Authority independence. The envelope (slots, memory, energy minimum and
36 * maximum, the nominal base budget, the fairness bound) is fixed when the
37 * objects are created and copied into the regulator. No capability, caller
38 * or object write can change it: a policy outside the envelope, however it
39 * got into the object, is refused at apply and the budget in force stays.
40 * The fields the regulator does not own (starvation_bound, locality,
41 * accelerator and compute offers, logical tick) are taken from the base
42 * budget on every apply, so fairness is never regulated away.
43 *
44 * Regimes. NOMINAL applies the base budget. EXPAND grants the envelope
45 * maximum when contention (deferred admissions in the window) reaches
46 * contention_hi and holds it until contention has fallen to contention_lo
47 * and the regime has been held for min_dwell samples. PROTECT applies the
48 * envelope minimum as soon as memory pressure reaches pressure_hi, takes
49 * precedence over EXPAND, and is left the same way (pressure_lo, dwell).
50 * Entering a regime is immediate; leaving needs the dead band and the dwell,
51 * so a signal that chatters around a threshold cannot flip the budget more
52 * than once per min_dwell + 1 samples and a signal inside the dead band
53 * never flips it.
54 *
55 * Emergency stop. The regulator runs under its own subject, so it can never
56 * stop or resume the world. While an operator stop is in force, outside
57 * condition publications are refused by the World and rx_regulate_apply
58 * refuses (RX_ERR_HALTED) without touching the budget.
59 *
60 * Objects. condition: 0 seq, 1 contention, 2 latency ns, 3 memory pressure
61 * (0..100), 4 deadline overdue, 5 window tick. policy: 0 version (changes
62 * only when the budget changes), 1 slots, 2 memory bytes, 3 energy budget,
63 * 4 regime, 5 condition seq decided on, 6 dwell (samples in this regime),
64 * 7 flips (regime changes so far). */
65#ifndef RX_REGULATE_H
66#define RX_REGULATE_H
67
68#include "runtime/rx_world.h"
69
70#define RX_REG_SUBJ 37u
71#define RX_REG_RES_BASE 0x5340000ull
72enum { RX_REG_RES_CONDITION = 0, RX_REG_RES_POLICY, RX_REG_RES_COUNT };
73enum { RX_OT_REG_CONDITION = 0x5340u, RX_OT_REG_POLICY };
74enum { RX_REG_NOMINAL = 0, RX_REG_EXPAND = 1, RX_REG_PROTECT = 2 };
75
76typedef struct {
77 uint32_t slots_min, slots_max;
78 uint64_t memory_min, memory_max;
79 uint64_t energy_min, energy_max;
80 RxResourceBudget base; /* NOMINAL; must lie inside the bounds above */
81 uint64_t contention_hi, contention_lo; /* lo < hi */
82 uint64_t pressure_hi, pressure_lo; /* lo < hi <= 100 */
83 uint64_t min_dwell; /* >= 1 */
84} RxRegulateEnvelope;
85
86typedef struct {
87 RxCapRef condition_read; /* RX_REG_SUBJ on condition, READ */
88 RxCapRef policy_rw; /* RX_REG_SUBJ on policy, READ|WRITE */
89} RxRegulateCaps;
90
91typedef struct {
92 RxWorld *w;
93 RxRegulateEnvelope env;
94 RxObjRef condition, policy;
95 uint32_t rid;
96 uint64_t applied_version; /* policy version in force (0: none applied yet) */
97 RxResourceBudget applied; /* the budget rx_regulate_apply last installed */
98 uint64_t refused; /* applies refused: outside the envelope or halted */
99} RxRegulator;
100
101/* RX_OK when every bound, the base budget and the thresholds are consistent. */
102int rx_regulate_envelope_ok(const RxRegulateEnvelope *e);
103
104/* Create the condition and policy objects (resources RX_REG_RES_BASE + i).
105 * The policy starts as NOMINAL, version 1. Copies the envelope. */
106int rx_regulate_create_objects(RxRegulator *r, RxWorld *w, const RxRegulateEnvelope *e);
107
108/* Register regulate.budget under RX_REG_SUBJ. `cred` is the subject's
109 * caller credential in a world that binds callers; null otherwise. */
110int rx_regulate_register(RxRegulator *r, const RxRegulateCaps *caps, const RxCallerCred *cred);
111
112/* Host step. Reads the policy and installs it with rx_world_set_resources
113 * when its version is new and it lies inside the envelope. RX_OK applied;
114 * 1 nothing new; RX_ERR_HALTED an operator stop is in force (budget
115 * untouched); RX_ERR_BOUNDS the policy is outside the envelope (budget
116 * untouched, counted in `refused`). Never call from a reaction or with the
117 * world lock held. */
118int rx_regulate_apply(RxRegulator *r);
119
120/* The pure decision, exposed so a test can drive it without a world. */
121uint32_t rx_regulate_decide(const RxRegulateEnvelope *e, uint32_t regime, uint64_t dwell,
122 uint64_t contention, uint64_t pressure);
123/* The budget a regime stands for inside `e`. */
124void rx_regulate_policy_for(const RxRegulateEnvelope *e, uint32_t regime, RxResourceBudget *out);
125
126#endif