==> include/mesh/top_net.h <==1/* @ajax/mesh-top-net: network rates from meshd's cumulative counters (crates/mesh-top src/net.rs).
2 * Rates are taken on meshd's own clock, never the viewer's: the live counters (links, control
3 * port, HTTP relay) against the snapshot's uptime, the NIC counters against their sample's time,
4 * so polling jitter and the once-a-second NIC sampling don't show as noise. */
5#ifndef MESH_TOP_NET_H
6#define MESH_TOP_NET_H
7
8#include "mesh/top_snap.h"
9
10#define MT_NET_WINDOW_MS 1000u
11#define MT_NET_HISTORY 120u
12
13/* One cumulative counter sampled at meshd times (ms), and its rate over about a second. */
14typedef struct {
15 uint64_t (*s)[2]; /* (t, v) samples, oldest first */
16 size_t n, cap;
17} mt_meter;
18
19void mt_meter_push(mt_meter *m, uint64_t t, uint64_t v);
20double mt_meter_per_s(const mt_meter *m);
21void mt_meter_free(mt_meter *m);
22
23typedef struct {
24 mt_meter rx, tx, rx_frames[MESH_NET_KINDS], tx_frames[MESH_NET_KINDS];
25} mt_link_track;
26void mt_link_frames_per_s(const mt_link_track *l, size_t kind, double *rx, double *tx);
27
28typedef struct {
29 mt_meter v[8]; /* MT_NIC_COUNTERS order */
30} mt_nic_track;
31double mt_nic_rx(const mt_nic_track *t);
32double mt_nic_tx(const mt_nic_track *t);
33void mt_nic_packets(const mt_nic_track *t, double *rx, double *tx);
34double mt_nic_errors(const mt_nic_track *t);
35double mt_nic_drops(const mt_nic_track *t);
36
37typedef struct {
38 char name[49];
39 mt_nic_track t;
40} mt_named_nic;
41
42typedef struct {
43 uint64_t uptime, nic_at;
44 bool has_links;
45 mt_link_track links[MESH_MAX_SYSTEMS];
46 mt_named_nic *nics; /* sorted by name, as a BTreeMap */
47 size_t nnics;
48 mt_nic_track total;
49 mt_meter ctl[3], http[3];
50 uint64_t *hist, *mesh_hist; /* rings of MT_NET_HISTORY, oldest first */
51 size_t nhist;
52} mt_net_track;
53
54void mt_net_update(mt_net_track *n, const mt_snapshot *s);
55void mt_net_free(mt_net_track *n);
56bool mt_net_nic_age_ms(const mt_net_track *n, uint64_t *age);
57const mt_link_track *mt_net_link(const mt_net_track *n, uint32_t k); /* NULL before a snapshot */
58const mt_nic_track *mt_net_nic(const mt_net_track *n, const char *name);
59double mt_net_mesh_bytes(const mt_net_track *n);
60bool mt_net_mesh_share(const mt_net_track *n, double *share);
61
62/* Bytes per second in decimal units: "870 B/s", "12.3 kB/s", "1.2 MB/s". */
63void mt_human(double bps, char *out, size_t cap);
64/* The same, short: "870", "12.3k", "154k", "1.2M". */
65void mt_short(double x, char *out, size_t cap);
66/* A text sparkline of the last width values, scaled to their maximum, padded left. out holds
67 * 3 * width + 1. */
68void mt_spark(const uint64_t *v, size_t n, size_t width, char *out);
69
70#endif