==> tests/samplers.c <==1/* Both samplers on this machine for a few seconds: the host block fills (load samples, CPUs, memory,
2 * a program's record; NIC samples and the total), then they stop on quit. */
3#define _XOPEN_SOURCE 700 /* sexc compiles tests without the package's cflags (reported) */
4#include "mesh/check.h"
5#include "mesh/log.h"
6#include "mesh/samplers.h"
7
8#include <pthread.h>
9#include <stdlib.h>
10#include <string.h>
11
12static mesh_shared *SH;
13static void *load(void *a) { (void)a; mesh_load_sampler(SH); return NULL; }
14static void *net(void *a) { (void)a; mesh_net_sampler(SH); return NULL; }
15
16int main(void) {
17 mesh_log_set_fd(-1);
18 static _Atomic bool quit;
19 mesh_mailbox mb = mesh_mailbox_test();
20 SH = mesh_shared_new(mb, ".", false, 0, 1, &quit);
21 mesh_mailbox_set(mb, mesh_prog_entry(1) + MESH_PT_STATE, MESH_ST_RUNNING);
22 mesh_mailbox_set(mb, mesh_prog_entry(1) + MESH_PT_NODES, 1);
23 mesh_mailbox_set(mb, mesh_prog_entry(1) + MESH_PT_LOADS, 1);
24 mesh_mailbox_set(mb, mesh_prog_ext(1) + MESH_PX_EXEC, MESH_BACKEND_WASM);
25 pthread_t a, b;
26 pthread_create(&a, NULL, load, NULL);
27 pthread_create(&b, NULL, net, NULL);
28 mesh_sleep_us(2600000);
29 atomic_store(&quit, true);
30 pthread_join(a, NULL);
31 pthread_join(b, NULL);
32 uint8_t *h = malloc(MESH_SNAP_HOST);
33 mesh_host_block_bytes(&SH->host, h);
34 uint32_t samples, cpus, mem, nets, flags, pflags;
35 memcpy(&samples, h + MESH_S_LOAD_SAMPLES, 4);
36 memcpy(&cpus, h + MESH_S_CPUS, 4);
37 memcpy(&mem, h + MESH_S_MEM_TOTAL_MB, 4);
38 memcpy(&nets, h + MESH_S_NET_SAMPLES, 4);
39 memcpy(&flags, h + MESH_S_NET_TOTAL + MESH_NI_COUNT, 4);
40 memcpy(&pflags, h + mesh_pload(1) + MESH_PL_FLAGS, 4);
41 CHECK(samples >= 1);
42 CHECK(cpus >= 1 && mem > 0);
43 CHECK(nets >= 2);
44 CHECK(memcmp(h + MESH_S_NET_TOTAL, "total", 5) == 0);
45 CHECK(pflags & MESH_PLF_LOADED);
46 CHECK(pflags & MESH_PLF_HOST);
47 CHECK(mesh_mailbox_get(mb, MESH_H_LOAD_WIN) >= 1);
48 /* The load page mirrors the machine region. */
49 uint8_t lp[16];
50 mesh_mailbox_read(mb, MESH_LOAD_PAGE, lp, 16);
51 CHECK(memcmp(lp, h + MESH_S_LOAD, 16) == 0);
52 free(h);
53 mesh_shared_free(SH);
54 free(mb.base);
55 return mesh_check_done();
56}