rogue_machines 0.1.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +56 -0
- data/Cargo.lock +22 -2
- data/Cargo.toml +2 -0
- data/README.md +147 -36
- data/ext/rogue_machines_native/core/Cargo.toml +4 -6
- data/ext/rogue_machines_native/core/src/experiment.rs +50 -16
- data/ext/rogue_machines_native/core/src/lib.rs +21 -8
- data/ext/rogue_machines_native/core/src/rogue.rs +152 -63
- data/ext/rogue_machines_native/core/src/slot.rs +34 -0
- data/ext/rogue_machines_native/core/src/test_support.rs +27 -0
- data/ext/rogue_machines_native/ffi/Cargo.toml +5 -9
- data/ext/rogue_machines_native/ffi/src/init.rs +15 -0
- data/ext/rogue_machines_native/ffi/src/lib.rs +12 -164
- data/ext/rogue_machines_native/ffi/src/rogue.rs +194 -0
- data/ext/rogue_machines_native/ffi/src/values.rs +288 -0
- data/lib/rogue_machines/runtime.rb +312 -0
- data/lib/rogue_machines/version.rb +1 -1
- data/lib/rogue_machines.rb +94 -264
- data/sig/rogue_machines.rbs +210 -104
- metadata +9 -6
- data/ext/rogue_machines_native/core/src/armed.rs +0 -78
- data/ext/rogue_machines_native/ffi/src/injection.rs +0 -44
- data/ext/rogue_machines_native/ffi/src/registry.rs +0 -283
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
//! The experiments a process is running.
|
|
2
2
|
|
|
3
|
-
use
|
|
3
|
+
use core::time::Duration;
|
|
4
|
+
|
|
5
|
+
use crate::{Draw, EXPERIMENTS, Experiment, Fault, Slot};
|
|
4
6
|
|
|
5
7
|
/// What to do with one call.
|
|
6
8
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
@@ -11,28 +13,61 @@ pub enum Decision {
|
|
|
11
13
|
Inject(Fault),
|
|
12
14
|
}
|
|
13
15
|
|
|
16
|
+
/// What an honoured [`Rogue::arm`] did.
|
|
17
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
18
|
+
pub enum Arming {
|
|
19
|
+
/// The slot was empty and now holds the experiment.
|
|
20
|
+
Armed,
|
|
21
|
+
/// The slot held this other experiment, which the new one replaced.
|
|
22
|
+
Replaced(Experiment),
|
|
23
|
+
/// The slot already holds exactly this experiment. Nothing changed.
|
|
24
|
+
AlreadyArmed,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// What a [`Rogue::disarm`] did.
|
|
28
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
29
|
+
pub enum Disarming {
|
|
30
|
+
/// The slot held this experiment and is now empty.
|
|
31
|
+
Disarmed(Experiment),
|
|
32
|
+
/// The slot was already empty. Nothing changed.
|
|
33
|
+
NotArmed,
|
|
34
|
+
}
|
|
35
|
+
|
|
14
36
|
/// A slot table and the mask over it.
|
|
15
37
|
///
|
|
38
|
+
/// A slot is *armed* while it holds an experiment, and that experiment is
|
|
39
|
+
/// *live* at an instant before its expiry. Expiry is never applied behind the
|
|
40
|
+
/// caller's back: [`active`](Self::active), [`decide`](Self::decide) and
|
|
41
|
+
/// [`live_count`](Self::live_count) judge it against the instant they are
|
|
42
|
+
/// given and change nothing, and only [`sweep`](Self::sweep) empties the slots
|
|
43
|
+
/// of experiments that have expired.
|
|
44
|
+
///
|
|
16
45
|
/// ```
|
|
17
|
-
/// use
|
|
46
|
+
/// use core::time::Duration;
|
|
47
|
+
/// use rogue_machines::{Arming, Decision, Draw, Experiment, Fault, Fraction, Rogue, Slot};
|
|
18
48
|
///
|
|
19
|
-
/// const SECOND: u64 = 1_000_000_000;
|
|
20
49
|
/// let Some(slot) = Slot::new(0) else { unreachable!() };
|
|
50
|
+
/// let Some(draw) = Draw::new(0) else { unreachable!() };
|
|
51
|
+
/// let second = Duration::from_secs(1);
|
|
21
52
|
/// let mut rogue = Rogue::new();
|
|
22
53
|
///
|
|
23
54
|
/// // Disarmed, which is where every process starts.
|
|
24
|
-
/// assert_eq!(rogue.decide(slot,
|
|
55
|
+
/// assert_eq!(rogue.decide(slot, Duration::ZERO, draw), Decision::Pass);
|
|
25
56
|
///
|
|
26
|
-
///
|
|
27
|
-
/// assert_eq!(rogue.
|
|
57
|
+
/// let experiment = Experiment::new(Fault::Raise, Fraction::ALL, 5 * second);
|
|
58
|
+
/// assert_eq!(rogue.arm(slot, experiment), Arming::Armed);
|
|
59
|
+
/// assert_eq!(rogue.decide(slot, second, draw), Decision::Inject(Fault::Raise));
|
|
28
60
|
///
|
|
29
|
-
/// // Past its expiry it
|
|
30
|
-
/// assert_eq!(rogue.decide(slot, 5 *
|
|
61
|
+
/// // Past its expiry it injects nothing, and a sweep gives the slot back.
|
|
62
|
+
/// assert_eq!(rogue.decide(slot, 5 * second, draw), Decision::Pass);
|
|
63
|
+
/// assert!(rogue.any_armed());
|
|
64
|
+
/// assert_eq!(rogue.sweep(5 * second), 1);
|
|
31
65
|
/// assert!(!rogue.any_armed());
|
|
32
66
|
/// ```
|
|
33
|
-
#[derive(Debug)]
|
|
67
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
34
68
|
pub struct Rogue {
|
|
35
|
-
|
|
69
|
+
/// One bit per slot, set exactly while `experiments` holds one there.
|
|
70
|
+
armed: u32,
|
|
36
71
|
experiments: [Option<Experiment>; EXPERIMENTS],
|
|
37
72
|
}
|
|
38
73
|
|
|
@@ -43,92 +78,146 @@ impl Default for Rogue {
|
|
|
43
78
|
}
|
|
44
79
|
|
|
45
80
|
impl Rogue {
|
|
81
|
+
/// Nothing armed.
|
|
46
82
|
#[must_use]
|
|
47
83
|
pub const fn new() -> Self {
|
|
48
84
|
Self {
|
|
49
|
-
armed:
|
|
85
|
+
armed: 0,
|
|
50
86
|
experiments: [None; EXPERIMENTS],
|
|
51
87
|
}
|
|
52
88
|
}
|
|
53
89
|
|
|
54
|
-
/// Arms `slot`, replacing whatever it held
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
90
|
+
/// Arms `slot` with `experiment`, replacing whatever it held, expired or
|
|
91
|
+
/// not. Never refuses: the slot is the caller's to pick, and an
|
|
92
|
+
/// experiment already spent is armed and injects nothing.
|
|
93
|
+
///
|
|
94
|
+
/// Arming the identical experiment again is an honoured repeat: the slot
|
|
95
|
+
/// already holds exactly that value, so [`Arming::AlreadyArmed`] is
|
|
96
|
+
/// provable and nothing changes.
|
|
97
|
+
pub fn arm(&mut self, slot: Slot, experiment: Experiment) -> Arming {
|
|
98
|
+
let held = self.held_mut(slot);
|
|
99
|
+
let arming = match *held {
|
|
100
|
+
None => Arming::Armed,
|
|
101
|
+
Some(previous) if previous == experiment => return Arming::AlreadyArmed,
|
|
102
|
+
Some(previous) => Arming::Replaced(previous),
|
|
103
|
+
};
|
|
104
|
+
*held = Some(experiment);
|
|
105
|
+
self.armed |= slot.bit();
|
|
106
|
+
arming
|
|
58
107
|
}
|
|
59
108
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
self.
|
|
109
|
+
/// Empties `slot`, whether or not what it held had expired. Never refuses.
|
|
110
|
+
pub const fn disarm(&mut self, slot: Slot) -> Disarming {
|
|
111
|
+
match self.held_mut(slot).take() {
|
|
112
|
+
Some(experiment) => {
|
|
113
|
+
self.armed &= !slot.bit();
|
|
114
|
+
Disarming::Disarmed(experiment)
|
|
115
|
+
}
|
|
116
|
+
None => Disarming::NotArmed,
|
|
117
|
+
}
|
|
63
118
|
}
|
|
64
119
|
|
|
65
|
-
|
|
120
|
+
/// Empties every slot and answers how many held an experiment, expired or
|
|
121
|
+
/// not. Never refuses; zero means nothing was armed and nothing changed.
|
|
122
|
+
pub const fn disarm_all(&mut self) -> u32 {
|
|
123
|
+
let held = self.armed.count_ones();
|
|
66
124
|
self.experiments = [None; EXPERIMENTS];
|
|
67
|
-
self.armed
|
|
125
|
+
self.armed = 0;
|
|
126
|
+
held
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// Empties every slot whose experiment has expired by `now` and answers
|
|
130
|
+
/// how many. Never refuses; zero means nothing changed.
|
|
131
|
+
///
|
|
132
|
+
/// The only call that applies an expiry. Without it an expired experiment
|
|
133
|
+
/// injects nothing but keeps its slot, and keeps
|
|
134
|
+
/// [`any_armed`](Self::any_armed) answering `true`.
|
|
135
|
+
pub fn sweep(&mut self, now: Duration) -> u32 {
|
|
136
|
+
let mut retired = 0;
|
|
137
|
+
for (index, held) in self.experiments.iter_mut().enumerate() {
|
|
138
|
+
if held.is_some_and(|experiment| experiment.spent(now)) {
|
|
139
|
+
*held = None;
|
|
140
|
+
self.armed &= !(1_u32 << index);
|
|
141
|
+
// At most `EXPERIMENTS`.
|
|
142
|
+
retired += 1;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
retired
|
|
68
146
|
}
|
|
69
147
|
|
|
70
|
-
/// Whether any experiment
|
|
71
|
-
///
|
|
148
|
+
/// Whether any slot holds an experiment, expired or not: the one word a
|
|
149
|
+
/// call reads when nothing is armed. `false` means every decision passes.
|
|
72
150
|
#[must_use]
|
|
73
|
-
pub fn any_armed(&self) -> bool {
|
|
74
|
-
self.armed
|
|
151
|
+
pub const fn any_armed(&self) -> bool {
|
|
152
|
+
self.armed != 0
|
|
75
153
|
}
|
|
76
154
|
|
|
77
|
-
/// How many
|
|
155
|
+
/// How many slots hold an experiment, expired or not.
|
|
78
156
|
#[must_use]
|
|
79
|
-
pub fn armed_count(&self) -> u32 {
|
|
80
|
-
self.armed.
|
|
157
|
+
pub const fn armed_count(&self) -> u32 {
|
|
158
|
+
self.armed.count_ones()
|
|
81
159
|
}
|
|
82
160
|
|
|
83
|
-
///
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
for index in 0..EXPERIMENTS {
|
|
94
|
-
let Some(slot) = Slot::new(index) else {
|
|
95
|
-
continue;
|
|
96
|
-
};
|
|
97
|
-
if self.armed.holds(slot) && self.spent(slot, now_ns) {
|
|
98
|
-
self.armed.release(slot);
|
|
161
|
+
/// How many slots hold an experiment that is live at `now`.
|
|
162
|
+
#[must_use]
|
|
163
|
+
pub fn live_count(&self, now: Duration) -> u32 {
|
|
164
|
+
let mut live = 0;
|
|
165
|
+
for experiment in self.experiments.iter().flatten() {
|
|
166
|
+
if !experiment.spent(now) {
|
|
167
|
+
// At most `EXPERIMENTS`.
|
|
168
|
+
live += 1;
|
|
99
169
|
}
|
|
100
170
|
}
|
|
171
|
+
live
|
|
101
172
|
}
|
|
102
173
|
|
|
103
|
-
/// The
|
|
174
|
+
/// The lowest slot holding nothing, or `None` when every slot is armed.
|
|
104
175
|
#[must_use]
|
|
105
|
-
pub fn
|
|
106
|
-
self.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
176
|
+
pub const fn vacant(&self) -> Option<Slot> {
|
|
177
|
+
Slot::new(self.armed.trailing_ones() as usize)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// The experiment `slot` holds, expired or not.
|
|
181
|
+
#[must_use]
|
|
182
|
+
pub const fn experiment(&self, slot: Slot) -> Option<Experiment> {
|
|
183
|
+
self.held(slot)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// The experiment `slot` holds, if it is live at `now`.
|
|
187
|
+
#[must_use]
|
|
188
|
+
pub fn active(&self, slot: Slot, now: Duration) -> Option<Experiment> {
|
|
189
|
+
self.held(slot).filter(|experiment| !experiment.spent(now))
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// The entry `slot` names.
|
|
193
|
+
#[expect(
|
|
194
|
+
clippy::indexing_slicing,
|
|
195
|
+
reason = "a `Slot` is below `EXPERIMENTS` by construction, the table's length"
|
|
196
|
+
)]
|
|
197
|
+
const fn held(&self, slot: Slot) -> Option<Experiment> {
|
|
198
|
+
self.experiments[slot.index()]
|
|
111
199
|
}
|
|
112
200
|
|
|
113
|
-
///
|
|
114
|
-
|
|
115
|
-
|
|
201
|
+
/// The entry `slot` names, to change.
|
|
202
|
+
#[expect(
|
|
203
|
+
clippy::indexing_slicing,
|
|
204
|
+
reason = "a `Slot` is below `EXPERIMENTS` by construction, the table's length"
|
|
205
|
+
)]
|
|
206
|
+
const fn held_mut(&mut self, slot: Slot) -> &mut Option<Experiment> {
|
|
207
|
+
&mut self.experiments[slot.index()]
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// What to do with one call: inject when `slot` holds an experiment that
|
|
211
|
+
/// is live at `now` and `draw` falls in the share it covers.
|
|
116
212
|
#[must_use]
|
|
117
|
-
pub fn decide(&self, slot: Slot,
|
|
118
|
-
|
|
119
|
-
match self.active(slot, now_ns) {
|
|
213
|
+
pub fn decide(&self, slot: Slot, now: Duration, draw: Draw) -> Decision {
|
|
214
|
+
match self.active(slot, now) {
|
|
120
215
|
Some(experiment) if experiment.fraction.covers(draw) => {
|
|
121
216
|
Decision::Inject(experiment.fault)
|
|
122
217
|
}
|
|
123
|
-
_ => Decision::Pass,
|
|
218
|
+
Some(_) | None => Decision::Pass,
|
|
124
219
|
}
|
|
125
220
|
}
|
|
126
|
-
|
|
127
|
-
/// Whether `slot` holds nothing, or holds something already expired. An
|
|
128
|
-
/// empty slot counts as spent so a bit can never outlive its experiment.
|
|
129
|
-
fn spent(&self, slot: Slot, now_ns: u64) -> bool {
|
|
130
|
-
self.experiments[slot.index()].is_none_or(|experiment| experiment.spent(now_ns))
|
|
131
|
-
}
|
|
132
221
|
}
|
|
133
222
|
|
|
134
223
|
#[cfg(test)]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//! Which experiment a call is asking about.
|
|
2
|
+
|
|
3
|
+
use crate::EXPERIMENTS;
|
|
4
|
+
|
|
5
|
+
/// A slot index a [`Rogue`](crate::Rogue) can hold. The caller picks it; the
|
|
6
|
+
/// core issues none.
|
|
7
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
8
|
+
pub struct Slot(usize);
|
|
9
|
+
|
|
10
|
+
impl Slot {
|
|
11
|
+
/// Rejects an index at or above [`EXPERIMENTS`].
|
|
12
|
+
#[must_use]
|
|
13
|
+
pub const fn new(index: usize) -> Option<Self> {
|
|
14
|
+
if index < EXPERIMENTS {
|
|
15
|
+
Some(Self(index))
|
|
16
|
+
} else {
|
|
17
|
+
None
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// The index this slot was built from.
|
|
22
|
+
#[must_use]
|
|
23
|
+
pub const fn index(self) -> usize {
|
|
24
|
+
self.0
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/// This slot's bit in the armed mask.
|
|
28
|
+
pub(crate) const fn bit(self) -> u32 {
|
|
29
|
+
1_u32 << self.0
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[cfg(test)]
|
|
34
|
+
mod tests;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//! Fixtures the unit tests share.
|
|
2
|
+
|
|
3
|
+
use core::time::Duration;
|
|
4
|
+
|
|
5
|
+
use crate::{Draw, Experiment, Fault, Fraction, Slot};
|
|
6
|
+
|
|
7
|
+
/// An instant `nanoseconds` after the epoch.
|
|
8
|
+
pub const fn at(nanoseconds: u64) -> Duration {
|
|
9
|
+
Duration::from_nanos(nanoseconds)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
pub fn slot(index: usize) -> Slot {
|
|
13
|
+
Slot::new(index).expect("the index is inside the mask")
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
pub fn fraction(millionths: u32) -> Fraction {
|
|
17
|
+
Fraction::new(millionths).expect("the share is a whole at most")
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
pub fn draw(millionths: u32) -> Draw {
|
|
21
|
+
Draw::new(millionths).expect("the draw is below a whole")
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// Raises on every call until `expires_at`.
|
|
25
|
+
pub const fn raising(expires_at: Duration) -> Experiment {
|
|
26
|
+
Experiment::new(Fault::Raise, Fraction::ALL, expires_at)
|
|
27
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "rogue_machines_native"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.5"
|
|
4
4
|
edition.workspace = true
|
|
5
5
|
rust-version.workspace = true
|
|
6
6
|
authors = ["Abdelkader Boudih <oss@seuros.com>"]
|
|
@@ -15,17 +15,13 @@ crate-type = ["cdylib"]
|
|
|
15
15
|
workspace = true
|
|
16
16
|
|
|
17
17
|
[dependencies]
|
|
18
|
-
rogue-machines = { workspace = true
|
|
19
|
-
magnus = { version = "
|
|
20
|
-
|
|
21
|
-
# implements the stable API for a Ruby rb-sys has no pre-generated bindings
|
|
22
|
-
# for, such as a preview. magnus asks for "stable-api" without it, and
|
|
23
|
-
# default-features = false drops the default that would have supplied it, so
|
|
24
|
-
# removing this line breaks the build on every future Ruby.
|
|
18
|
+
rogue-machines = { workspace = true }
|
|
19
|
+
magnus = { version = "0.9.3", default-features = false, features = ["io"] }
|
|
20
|
+
matryona-machines = { workspace = true, features = ["ruby"] }
|
|
25
21
|
# Nothing here calls rb-sys directly: this is the compiled fallback that
|
|
26
22
|
# implements the stable API for a Ruby rb-sys ships no pre-generated bindings
|
|
27
23
|
# for, such as a preview. magnus asks for "stable-api" without it, and its
|
|
28
24
|
# default-features = false drops the default that would have supplied it, so
|
|
29
25
|
# dropping this line breaks the build on every Ruby newer than rb-sys.
|
|
30
|
-
rb-sys = { version = "
|
|
26
|
+
rb-sys = { version = "0.9.130", default-features = false, features = ["stable-api-compiled-fallback"] }
|
|
31
27
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//! Private native entry point.
|
|
2
|
+
use magnus::{Error, Ruby, prelude::*};
|
|
3
|
+
use rogue_machines::PER_MILLION;
|
|
4
|
+
|
|
5
|
+
use crate::{define_errors, rogue, values};
|
|
6
|
+
|
|
7
|
+
#[magnus::init]
|
|
8
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
9
|
+
let module = ruby.define_module("RogueMachines")?;
|
|
10
|
+
module.const_set("PER_MILLION", PER_MILLION)?;
|
|
11
|
+
module.const_set("EXPERIMENTS", rogue_machines::EXPERIMENTS)?;
|
|
12
|
+
define_errors(ruby, module)?;
|
|
13
|
+
values::define(ruby, module)?;
|
|
14
|
+
rogue::define(ruby, module)
|
|
15
|
+
}
|
|
@@ -1,171 +1,19 @@
|
|
|
1
1
|
//! Ruby bindings for the `rogue-machines` fault injector.
|
|
2
2
|
//!
|
|
3
|
-
//!
|
|
4
|
-
//! the
|
|
5
|
-
//!
|
|
6
|
-
//!
|
|
3
|
+
//! A plain mirror of the core: `RogueMachines::Rogue` wraps one `Rogue`, and
|
|
4
|
+
//! the `Data` values `Slot`, `Fraction`, `Draw`, `Delay`, `Experiment`,
|
|
5
|
+
//! `Inject`, `Replaced` and `Disarmed` (defined in Ruby, validated here) carry
|
|
6
|
+
//! the core's types. Time is Integer nanoseconds in and out. The process-wide
|
|
7
|
+
//! runtime (`RogueMachines.arm`, `RogueMachines.run`) is Ruby, on top of this.
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
mod init;
|
|
10
|
+
mod rogue;
|
|
11
|
+
mod values;
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
mod registry;
|
|
13
|
+
use magnus::{Error, RModule, Ruby, prelude::*};
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
use std::fmt::Display;
|
|
15
|
-
use std::sync::OnceLock;
|
|
16
|
-
|
|
17
|
-
use magnus::value::Opaque;
|
|
18
|
-
use magnus::{Error, ExceptionClass, Integer, RModule, Ruby, Value, prelude::*};
|
|
19
|
-
use rogue_machines::{Fraction, PER_MILLION};
|
|
20
|
-
|
|
21
|
-
static ERROR: OnceLock<Opaque<ExceptionClass>> = OnceLock::new();
|
|
22
|
-
static INJECTED: OnceLock<Opaque<ExceptionClass>> = OnceLock::new();
|
|
23
|
-
|
|
24
|
-
/// Nanoseconds in a second.
|
|
25
|
-
pub(crate) const NS_PER_SECOND: u32 = 1_000_000_000;
|
|
26
|
-
|
|
27
|
-
/// Longest an experiment may run for. An experiment is something a person is
|
|
28
|
-
/// waiting on, so a day is already generous; past that it is configuration,
|
|
29
|
-
/// and it must be re-armed deliberately.
|
|
30
|
-
pub(crate) const LONGEST_EXPERIMENT: f64 = 24.0 * 60.0 * 60.0;
|
|
31
|
-
|
|
32
|
-
/// Longest a single delay may wait. Bounds how long a stall can hold a caller
|
|
33
|
-
/// that has no deadline of its own.
|
|
34
|
-
pub(crate) const LONGEST_DELAY: f64 = 5.0 * 60.0;
|
|
35
|
-
|
|
36
|
-
/// Seconds as nanoseconds, rejecting a duration no experiment should run for.
|
|
37
|
-
///
|
|
38
|
-
/// An unbounded duration is not merely untidy: `as u64` saturates rather than
|
|
39
|
-
/// wrapping, so without `longest` a large `delay` becomes a stall of centuries.
|
|
40
|
-
#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
|
41
|
-
pub(crate) fn duration(ruby: &Ruby, name: &str, seconds: f64, longest: f64) -> Result<u64, Error> {
|
|
42
|
-
if !seconds.is_finite() || seconds <= 0.0 || seconds > longest {
|
|
43
|
-
return Err(arg_error(
|
|
44
|
-
ruby,
|
|
45
|
-
format!("{name} must be above 0 and at most {longest} seconds, got {seconds}"),
|
|
46
|
-
));
|
|
47
|
-
}
|
|
48
|
-
let nanoseconds = seconds * f64::from(NS_PER_SECOND);
|
|
49
|
-
if nanoseconds < 1.0 {
|
|
50
|
-
return Err(arg_error(
|
|
51
|
-
ruby,
|
|
52
|
-
format!("{name} of {seconds} is below the one nanosecond resolution"),
|
|
53
|
-
));
|
|
54
|
-
}
|
|
55
|
-
Ok(nanoseconds.round() as u64)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/// A reading in nanoseconds.
|
|
59
|
-
///
|
|
60
|
-
/// A Float is refused rather than truncated, and so is a value outside the
|
|
61
|
-
/// range: magnus would reject that with its own `RangeError`, which would
|
|
62
|
-
/// leak out of a protected call site as something the gem never documents.
|
|
63
|
-
pub(crate) fn reading(ruby: &Ruby, name: &str, value: Value) -> Result<u64, Error> {
|
|
64
|
-
whole(ruby, name, value)?
|
|
65
|
-
.to_u64()
|
|
66
|
-
.map_err(|_| above(ruby, name, u64::MAX))
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/// A draw below [`PER_MILLION`].
|
|
70
|
-
///
|
|
71
|
-
/// A truncated draw becomes 0, which every share covers, so a caller whose
|
|
72
|
-
/// `rand` returns a Float would inject on every call instead of none.
|
|
73
|
-
pub(crate) fn draw(ruby: &Ruby, value: Value) -> Result<u32, Error> {
|
|
74
|
-
let drawn = whole(ruby, "draw", value)?.to_u32().map_err(|_| {
|
|
75
|
-
arg_error(
|
|
76
|
-
ruby,
|
|
77
|
-
format!("draw must be a non-negative Integer below {PER_MILLION}"),
|
|
78
|
-
)
|
|
79
|
-
})?;
|
|
80
|
-
if drawn >= PER_MILLION {
|
|
81
|
-
return Err(arg_error(
|
|
82
|
-
ruby,
|
|
83
|
-
format!("draw must be below {PER_MILLION}, got {drawn}"),
|
|
84
|
-
));
|
|
85
|
-
}
|
|
86
|
-
Ok(drawn)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/// A generation tag.
|
|
90
|
-
pub(crate) fn counted(ruby: &Ruby, name: &str, value: Value) -> Result<u32, Error> {
|
|
91
|
-
whole(ruby, name, value)?
|
|
92
|
-
.to_u32()
|
|
93
|
-
.map_err(|_| above(ruby, name, u32::MAX))
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/// Refuses an Integer that is negative or larger than `max`.
|
|
97
|
-
fn above(ruby: &Ruby, name: &str, max: impl Display) -> Error {
|
|
98
|
-
arg_error(
|
|
99
|
-
ruby,
|
|
100
|
-
format!("{name} must be a non-negative Integer of at most {max}"),
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
fn whole(ruby: &Ruby, name: &str, value: Value) -> Result<Integer, Error> {
|
|
105
|
-
Integer::from_value(value).ok_or_else(|| arg_error(ruby, format!("{name} must be an Integer")))
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/// A share of calls, rejecting one outside a whole or too small to express.
|
|
109
|
-
#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
|
110
|
-
pub(crate) fn share(ruby: &Ruby, given: f64) -> Result<Fraction, Error> {
|
|
111
|
-
if !given.is_finite() || !(0.0..=1.0).contains(&given) {
|
|
112
|
-
return Err(arg_error(
|
|
113
|
-
ruby,
|
|
114
|
-
format!("share must be between 0 and 1, got {given}"),
|
|
115
|
-
));
|
|
116
|
-
}
|
|
117
|
-
let millionths = given * f64::from(PER_MILLION);
|
|
118
|
-
if given > 0.0 && millionths < 1.0 {
|
|
119
|
-
return Err(arg_error(
|
|
120
|
-
ruby,
|
|
121
|
-
format!("share of {given} is below the one millionth resolution"),
|
|
122
|
-
));
|
|
123
|
-
}
|
|
124
|
-
let millionths = millionths.round() as u32;
|
|
125
|
-
Fraction::new(millionths).ok_or_else(|| {
|
|
126
|
-
arg_error(
|
|
127
|
-
ruby,
|
|
128
|
-
format!("share of {millionths} millionths is not a whole"),
|
|
129
|
-
)
|
|
130
|
-
})
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/// An `ArgumentError` carrying `message`.
|
|
134
|
-
pub(crate) fn arg_error(ruby: &Ruby, message: impl Into<Cow<'static, str>>) -> Error {
|
|
135
|
-
Error::new(ruby.exception_arg_error(), message)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
pub(crate) fn error(ruby: &Ruby, message: String) -> Error {
|
|
139
|
-
ERROR.get().map_or_else(
|
|
140
|
-
|| {
|
|
141
|
-
Error::new(
|
|
142
|
-
ruby.exception_runtime_error(),
|
|
143
|
-
"RogueMachines was not initialised",
|
|
144
|
-
)
|
|
145
|
-
},
|
|
146
|
-
|class| Error::new(ruby.get_inner(*class), message),
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
fn define_errors(ruby: &Ruby, module: RModule) -> Result<(), Error> {
|
|
15
|
+
pub(crate) fn define_errors(ruby: &Ruby, module: RModule) -> Result<(), Error> {
|
|
151
16
|
let base = module.define_error("Error", ruby.exception_standard_error())?;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
.map_err(|_| error(ruby, "Error was defined twice".to_owned()))?;
|
|
155
|
-
|
|
156
|
-
let injected = module.define_error("Injected", base)?;
|
|
157
|
-
INJECTED
|
|
158
|
-
.set(injected.into())
|
|
159
|
-
.map_err(|_| error(ruby, "Injected was defined twice".to_owned()))
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
#[magnus::init]
|
|
163
|
-
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
164
|
-
let module = ruby.define_module("RogueMachines")?;
|
|
165
|
-
module.const_set("NS_PER_SECOND", NS_PER_SECOND)?;
|
|
166
|
-
module.const_set("PER_MILLION", PER_MILLION)?;
|
|
167
|
-
module.const_set("EXPERIMENTS", rogue_machines::EXPERIMENTS)?;
|
|
168
|
-
define_errors(ruby, module)?;
|
|
169
|
-
injection::define(ruby, module)?;
|
|
170
|
-
registry::define(ruby, module)
|
|
17
|
+
module.define_error("Injected", base)?;
|
|
18
|
+
Ok(())
|
|
171
19
|
}
|