File size: 3,010 Bytes
29f097e
32b4893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* SPDX-License-Identifier: LicenseRef-Sovereign-Leviathan-SL-AGPL3-001
 * Copyright (C) 2026 SNAPKITTYWEST / Sovereign Kernel Project
 * CLONE_GATE: sparse-router-formal::main
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "router.h"

static void init_state(router_state_t *s, uint32_t num_workers) {
    memset(s, 0, sizeof(*s));
    s->num_workers = num_workers;
    for (uint32_t i = 0; i < num_workers; i++) {
        s->workers[i].id = i;
    }
}

static void seed_sparse_edges(router_state_t *s, uint32_t num_regions,
                               uint32_t edges_per_region) {
    for (uint32_t r = 0; r < num_regions; r++) {
        uint32_t count = edges_per_region;
        if (count > s->num_workers) count = s->num_workers;
        if (count > MAX_SPARSE_EDGES) count = MAX_SPARSE_EDGES;
        s->sparse_edges[r].count = count;
        for (uint32_t i = 0; i < count; i++) {
            s->sparse_edges[r].workers[i] = (r + i) % s->num_workers;
        }
    }
}

static void bench_routing(uint32_t num_workers, uint32_t num_tasks,
                           uint32_t num_regions, uint32_t edges) {
    router_state_t *state = calloc(1, sizeof(router_state_t));
    if (!state) { perror("calloc"); return; }

    init_state(state, num_workers);
    seed_sparse_edges(state, num_regions, edges);

    struct timespec t0, t1;
    timespec_get(&t0, TIME_UTC);

    for (uint32_t i = 0; i < num_tasks; i++) {
        task_t t = {
            .memory_region  = i % num_regions,
            .footprint_bytes = 4096 + (i % 16) * 4096,
            .task_id        = i,
            .cache_lines_hot = 4 + (i % 12),
            .priority       = i % 4,
        };
        state->global_tick = i;
        raw_route(state, &t);
    }

    timespec_get(&t1, TIME_UTC);
    double elapsed = (t1.tv_sec - t0.tv_sec) +
                     (t1.tv_nsec - t0.tv_nsec) / 1e9;

    printf("workers=%u tasks=%u regions=%u edges=%u\n",
           num_workers, num_tasks, num_regions, edges);
    printf("  elapsed: %.6f s  (%.0f routes/sec)\n",
           elapsed, num_tasks / elapsed);

    uint32_t total = 0, max_q = 0, min_q = UINT32_MAX;
    for (uint32_t i = 0; i < num_workers; i++) {
        uint32_t c = state->workers[i].deque.count;
        total += c;
        if (c > max_q) max_q = c;
        if (c < min_q) min_q = c;
    }
    printf("  total queued: %u  min/max: %u/%u  balance: %.2f%%\n",
           total, min_q, max_q,
           num_workers > 0 ? 100.0 * min_q / (max_q ? max_q : 1) : 0.0);

    uint32_t owned = 0;
    for (uint32_t r = 0; r < num_regions; r++) {
        if (state->region_table[r].has_owner) owned++;
    }
    printf("  regions owned: %u/%u\n\n", owned, num_regions);

    free(state);
}

int main(void) {
    printf("=== Sparse Router Benchmark ===\n\n");
    bench_routing(8, 100000, 64, 4);
    bench_routing(16, 100000, 256, 4);
    bench_routing(64, 1000000, 1024, 8);
    bench_routing(256, 1000000, 4096, 16);
    return 0;
}