skip to content
==> include/mesh/cpuuse.h <==
1/* @ajax/mesh-cpuuse: what the CPU executors cost, per slot (mesh-host
2 * cpuuse.rs): CPU time and resident bytes of a slot's wasm lanes (each lane
3 * adds its own thread's CPU time and its linear memory) or native worker
4 * processes (the driver stores their sampled counters).  The load sampler
5 * turns them into PL_HOST fields and the machine's S_EXEC_* totals.
6 * Counters are cumulative and never reset. */
7#ifndef MESH_CPUUSE_H
8#define MESH_CPUUSE_H
9
10#include "mesh/abi/program_table.h"
11
12#include <stdatomic.h>
13#include <stdint.h>
14
15typedef struct {
16    _Atomic uint64_t cpu_ns; /* CPU ns, summed over the slot's lanes or processes, monotonic */
17    _Atomic uint64_t rss;    /* resident bytes now */
18    _Atomic uint32_t units;  /* lanes running (wasm) / live worker processes (native) */
19} mesh_slot_use;
20
21extern mesh_slot_use mesh_cpuuse_slots[MESH_PROG_SLOTS];
22/* Native workers of this meshd, machine-wide: live processes, spawns, crashes. */
23extern _Atomic uint32_t mesh_cpuuse_workers, mesh_cpuuse_worker_spawns, mesh_cpuuse_worker_crashes;
24
25void mesh_slot_use_add_cpu(mesh_slot_use *u, uint64_t ns);
26/* Adds d (which may be negative) to rss, never below 0. */
27void mesh_slot_use_add_rss(mesh_slot_use *u, int64_t d);
28void mesh_slot_use_get(mesh_slot_use *u, uint64_t *cpu_ns, uint64_t *rss, uint32_t *units);
29
30/* The calling thread's CPU time, ns (CLOCK_THREAD_CPUTIME_ID). */
31uint64_t mesh_thread_cpu_ns(void);
32
33/* A lane's meter: adds its thread's CPU time since the last tick (and its
34 * memory's growth) to its slot.  Ended: its memory leaves the slot's RSS. */
35typedef struct {
36    uint32_t slot;
37    uint64_t cpu, rss;
38} mesh_meter;
39
40void mesh_meter_start(mesh_meter *m, uint32_t slot); /* on the lane's own thread */
41void mesh_meter_tick(mesh_meter *m, uint64_t rss);   /* rss: the lane's bytes now */
42void mesh_meter_end(mesh_meter *m);                  /* Rust's Drop */
43
44#endif