==> include/mesh/top_mock.h <==1/* @ajax/mesh-top-mock: a tiny mock of meshd's control port for mesh-top's tests and demo
2 * (crates/mesh-top tests/support/mock.rs). It is NOT meshd: the snapshot, trace and log are
3 * synthesised on the CPU (a toy raycaster, a scripted loss curve, a program table with a fake
4 * catalog) purely to exercise the viewer. It speaks mesh-abi's control protocol. Time comes from
5 * mesh-top-clock, so a test on the virtual clock gets the same bytes every run. */
6#ifndef MESH_TOP_MOCK_H
7#define MESH_TOP_MOCK_H
8
9#include "mesh/top_clock.h"
10#include "mesh/top_snap.h"
11#include "mesh/top_views.h"
12
13#include <stdbool.h>
14#include <stddef.h>
15#include <stdint.h>
16
17typedef struct {
18 const char *name;
19 uint32_t type;
20 size_t off;
21} mt_mock_field;
22
23/* One loaded program, as the mock's table keeps it. */
24typedef struct {
25 uint32_t slot;
26 char name[32];
27 uint32_t state, nodes, view_bytes, snap_bytes, policy, hz, fanout, peer;
28 const char *counters[4];
29 mt_mock_field fields[8];
30 size_t nfields;
31 uint64_t hash;
32 bool has_retained;
33 uint64_t retained;
34 uint32_t regs, loads;
35 float rate; /* tasks/s */
36 uint32_t task_ns;
37 int64_t offset; /* completions: offset + elapsed * rate, or frozen while stopped */
38 bool has_frozen;
39 uint32_t frozen;
40 bool has_native_pid;
41 uint32_t native_pid;
42} mt_mock_prog;
43
44#define MT_MOCK_CATALOG_LEN 7
45extern const char *const MT_MOCK_CATALOG[MT_MOCK_CATALOG_LEN];
46/* The manifest-equivalent of a catalog entry (false: not in the catalog). */
47bool mt_mock_catalog_prog(const char *name, uint32_t slot, mt_mock_prog *out);
48
49typedef struct {
50 char host[64];
51 uint32_t system;
52 mt_mock_prog progs[MESH_PROG_SLOTS];
53 size_t nprogs;
54 uint32_t epoch, prog_seq;
55 float pos[2], dir[2];
56 mt_instant step0;
57 bool has_frozen_step;
58 uint32_t frozen_step;
59 mt_instant started;
60 uint64_t snaps;
61 uint32_t http_served;
62 char last_http[96];
63 struct {
64 char name[32];
65 size_t node;
66 } stalled[8];
67 size_t nstalled;
68 char **log;
69 size_t nlog;
70 uint32_t hello_last[2];
71 uint32_t keys;
72 uint32_t place[2]; /* (PLACE_*, system) */
73 uint8_t assign[MESH_PLACE_PARTS_MAX];
74 uint32_t n;
75 char gpu[64];
76 uint32_t cc, blocks_per_sm;
77 char arch[32];
78 float waits[MESH_MAX_SYSTEMS];
79 uint32_t down[MESH_MAX_SYSTEMS];
80 size_t ndown;
81 bool has_clock_ms;
82 uint64_t clock_ms; /* meshd's uptime (the network counters' clock); else real time since started */
83} mt_mock_world;
84
85/* doom, train, infer, http, hello in slots 0..4, all running. */
86void mt_mock_world_init(mt_mock_world *w, const char *host, uint32_t system);
87void mt_mock_world_free(mt_mock_world *w);
88mt_mock_prog *mt_mock_prog_named(mt_mock_world *w, const char *name);
89void mt_mock_default_assign(uint32_t n, uint32_t fanout, uint8_t out[MESH_PLACE_PARTS_MAX]);
90uint32_t mt_mock_train_step(const mt_mock_world *w);
91void mt_mock_params_at(uint32_t step, float out[4]);
92
93/* The snapshot meshd would send now (it counts as a snapshot taken). */
94mt_snapshot mt_mock_synth(mt_mock_world *w);
95/* The `trace` reply (malloc'd). */
96uint8_t *mt_mock_trace(const mt_mock_world *w, size_t *len);
97/* One control line: a framed reply (*bytes set, malloc'd) or a text one (*text, malloc'd). */
98void mt_mock_request(mt_mock_world *w, const char *req, uint8_t **bytes, size_t *len, char **text);
99
100#define MT_MOCK_ROUNDS_PER_S 2000u
101#define MT_MOCK_ROUND_BYTES (1u + 12u + (uint64_t)MESH_EX_REGION_BYTES)
102const char *mt_mock_iface(uint32_t a, uint32_t b);
103
104/* The control-port server: a thread accepting connections, one thread each. */
105typedef struct mt_mock mt_mock;
106/* Listens on addr ("127.0.0.1:0" picks a port); NULL when it can't. */
107mt_mock *mt_mock_start(const char *addr, const char *host, uint32_t system);
108/* On an existing world (shared: the reconnect test reuses one); the world outlives the mock. */
109mt_mock *mt_mock_start_with(const char *addr, mt_mock_world *w);
110const char *mt_mock_addr(const mt_mock *m);
111mt_mock_world *mt_mock_world_of(mt_mock *m);
112void mt_mock_lock(mt_mock *m);
113void mt_mock_unlock(mt_mock *m);
114/* Whether a request line was received (the requests are logged). */
115bool mt_mock_saw(mt_mock *m, const char *req);
116/* Closes the listener and every connection, then frees the mock (not a shared world). */
117void mt_mock_stop(mt_mock *m);
118
119#endif