1/* @ajax/mesh-jobs-rate: a member's measured rate for a program, as the leader keeps it (jobs.rs
2 * `Rate`, `lease_time` and the lease-sizing constants): items/s as an EWMA over windows of at least
3 * 400 ms, and a lease target that grows by slow start. */
4#ifndef MESH_JOBS_RATE_H
5#define MESH_JOBS_RATE_H
67#include <stdbool.h>
8#include <stdint.h>
910/* A member without work longer than this was idle (its rate window restarts). */
11#define MESH_JOBS_IDLE_MS 50u
12/* A lease's target duration at the member's measured rate. */
13#define MESH_JOBS_LEASE_MS 250u
14/* At most this many items in one lease. */
15#define MESH_JOBS_LEASE_MAX ((uint64_t)1 << 20)
1617typedef struct {
18 double ips; /* items/s */
19 uint64_t t0, k, idle_at;
20 uint64_t target; /* slow start's lease size */
21} mj_rate;
2223/* n items done at now (ms). */
24void mj_rate_count(mj_rate *r, uint64_t n, uint64_t now);
25/* A lease for a member with none outstanding: if it sat idle (more than a link's round trip), the
26 * next window starts now. */
27void mj_rate_busy(mj_rate *r, uint64_t now);
28/* A lease of n items issued at `issued` is done at now; `last`: the member has no other lease of
29 * the program. The first rate is its turnaround; slow start doubles or halves the target. */
30void mj_rate_lease_done(mj_rate *r, uint64_t n, uint64_t issued, uint64_t now, bool last);
31/* A lease of n items at `rate` items/s takes this long (ms; an unknown rate: 10 s). */
32uint64_t mj_lease_time(uint64_t n, double rate);
3334#endif