omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / estimation / est_load.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* EST-3c (protocol v3) declared CPU load schedule. |
| 18 | * |
| 19 | * est_load <seed> <seconds> <marks-file> |
| 20 | * |
| 21 | * Generates a deterministic schedule of segments from <seed> (splitmix64): |
| 22 | * level in {0, 6, 12, 18} busy threads (uniform), duration uniform in |
| 23 | * [20, 120] s. A level-0 segment is "idle"; a segment with level > 0 is a |
| 24 | * load trial and is written to <marks-file> in the R15 marks shape: |
| 25 | * <t s.ns> begin load-L<level>-<index> |
| 26 | * <t s.ns> end load-L<level>-<index> exit 0 |
| 27 | * Busy threads spin on integer arithmetic only (no memory traffic of note), |
| 28 | * so the load is CPU heat, nothing else. The schedule is printed to stdout |
| 29 | * before it starts ("schedule <index> <level> <seconds>"), so it is part of |
| 30 | * the record and can be re-derived from the seed alone. |
| 31 | * |
| 32 | * No Python, no root, no files besides <marks-file> and stdout. Signals: on |
| 33 | * SIGTERM/SIGINT the current trial gets its end mark and the program exits. */ |
| 34 | #define _POSIX_C_SOURCE 200809L |
| 35 | #include <pthread.h> |
| 36 | #include <signal.h> |
| 37 | #include <stdatomic.h> |
| 38 | #include <stdint.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <time.h> |
| 42 | |
| 43 | #define MAX_THREADS 18 |
| 44 | #define MAX_SEGS 4096 |
| 45 | |
| 46 | static atomic_int g_level; /* threads with index < level spin */ |
| 47 | static atomic_int g_quit; |
| 48 | static volatile sig_atomic_t g_stop; |
| 49 | |
| 50 | static uint64_t splitmix64(uint64_t *s) |
| 51 | { |
| 52 | uint64_t z = (*s += 0x9e3779b97f4a7c15ull); |
| 53 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; |
| 54 | z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; |
| 55 | return z ^ (z >> 31); |
| 56 | } |
| 57 | |
| 58 | static void *spin(void *arg) |
| 59 | { |
| 60 | int idx = (int)(intptr_t)arg; |
| 61 | volatile uint64_t x = (uint64_t)idx + 1u; |
| 62 | struct timespec nap = {0, 20000000}; /* 20 ms when parked */ |
| 63 | while (!atomic_load(&g_quit)) { |
| 64 | if (idx < atomic_load(&g_level)) { |
| 65 | for (int i = 0; i < 1000000; i++) |
| 66 | x = x * 6364136223846793005ull + 1442695040888963407ull; |
| 67 | } else { |
| 68 | nanosleep(&nap, NULL); |
| 69 | } |
| 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | static void on_sig(int s) { (void)s; g_stop = 1; } |
| 75 | |
| 76 | static void now(char *buf, size_t n) |
| 77 | { |
| 78 | struct timespec ts; |
| 79 | clock_gettime(CLOCK_REALTIME, &ts); |
| 80 | snprintf(buf, n, "%lld.%09ld", (long long)ts.tv_sec, ts.tv_nsec); |
| 81 | } |
| 82 | |
| 83 | int main(int argc, char **argv) |
| 84 | { |
| 85 | if (argc != 4) { |
| 86 | fprintf(stderr, "usage: est_load <seed> <seconds> <marks-file>\n"); |
| 87 | return 2; |
| 88 | } |
| 89 | uint64_t seed = strtoull(argv[1], NULL, 0); |
| 90 | long total = strtol(argv[2], NULL, 10); |
| 91 | if (total <= 0) { fprintf(stderr, "est_load: seconds must be > 0\n"); return 2; } |
| 92 | static const int levels[4] = {0, 6, 12, 18}; |
| 93 | int seg_level[MAX_SEGS]; |
| 94 | long seg_secs[MAX_SEGS]; |
| 95 | int nseg = 0; |
| 96 | long acc = 0; |
| 97 | uint64_t s = seed; |
| 98 | while (acc < total && nseg < MAX_SEGS) { |
| 99 | int lv = levels[splitmix64(&s) % 4u]; |
| 100 | long d = 20 + (long)(splitmix64(&s) % 101u); |
| 101 | if (acc + d > total) d = total - acc; |
| 102 | seg_level[nseg] = lv; |
| 103 | seg_secs[nseg] = d; |
| 104 | acc += d; |
| 105 | nseg++; |
| 106 | } |
| 107 | printf("est_load seed %llu seconds %ld segments %d\n", (unsigned long long)seed, total, nseg); |
| 108 | for (int i = 0; i < nseg; i++) |
| 109 | printf("schedule %d %d %ld\n", i, seg_level[i], seg_secs[i]); |
| 110 | fflush(stdout); |
| 111 | |
| 112 | FILE *m = fopen(argv[3], "a"); |
| 113 | if (!m) { perror("est_load: marks"); return 1; } |
| 114 | struct sigaction sa = {0}; |
| 115 | sa.sa_handler = on_sig; |
| 116 | sigaction(SIGTERM, &sa, NULL); |
| 117 | sigaction(SIGINT, &sa, NULL); |
| 118 | |
| 119 | pthread_t th[MAX_THREADS]; |
| 120 | for (int i = 0; i < MAX_THREADS; i++) |
| 121 | if (pthread_create(&th[i], NULL, spin, (void *)(intptr_t)i) != 0) { |
| 122 | fprintf(stderr, "est_load: pthread_create failed\n"); |
| 123 | return 1; |
| 124 | } |
| 125 | char tb[64]; |
| 126 | for (int i = 0; i < nseg && !g_stop; i++) { |
| 127 | if (seg_level[i] > 0) { |
| 128 | now(tb, sizeof tb); |
| 129 | fprintf(m, "%s begin load-L%d-%03d\n", tb, seg_level[i], i); |
| 130 | fflush(m); |
| 131 | } |
| 132 | atomic_store(&g_level, seg_level[i]); |
| 133 | struct timespec end, cur; |
| 134 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 135 | end.tv_sec += seg_secs[i]; |
| 136 | for (;;) { |
| 137 | if (g_stop) break; |
| 138 | clock_gettime(CLOCK_MONOTONIC, &cur); |
| 139 | if (cur.tv_sec > end.tv_sec || (cur.tv_sec == end.tv_sec && cur.tv_nsec >= end.tv_nsec)) |
| 140 | break; |
| 141 | struct timespec nap = {0, 100000000}; |
| 142 | nanosleep(&nap, NULL); |
| 143 | } |
| 144 | atomic_store(&g_level, 0); |
| 145 | if (seg_level[i] > 0) { |
| 146 | now(tb, sizeof tb); |
| 147 | fprintf(m, "%s end load-L%d-%03d exit 0\n", tb, seg_level[i], i); |
| 148 | fflush(m); |
| 149 | } |
| 150 | } |
| 151 | atomic_store(&g_quit, 1); |
| 152 | for (int i = 0; i < MAX_THREADS; i++) pthread_join(th[i], NULL); |
| 153 | fclose(m); |
| 154 | printf("est_load done%s\n", g_stop ? " (stopped by signal)" : ""); |
| 155 | return 0; |
| 156 | } |