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 / tests / runtime / rx_quiet_reps.h
| 1 | /* Speed checks on a shared machine: keep only the repetitions the machine did |
| 2 | * not disturb. |
| 3 | * |
| 4 | * A stopwatch check compares wall times. On the Spark other jobs share the |
| 5 | * cores, and a run whose threads sat runnable without a core (the kernel's |
| 6 | * run-queue delay) is longer by time the code under test never asked for. |
| 7 | * That delay is measured, not guessed: /proc/self/task/<tid>/schedstat field 2 |
| 8 | * is the time each thread of this process has spent runnable but waiting for |
| 9 | * a CPU (Documentation/scheduler/sched-stats.rst, "time spent waiting on a |
| 10 | * runqueue"). A repetition whose threads waited more than the allowance below |
| 11 | * is set aside and run again; the check then takes the same statistic (the |
| 12 | * median) with the same threshold over undisturbed repetitions only. |
| 13 | * |
| 14 | * Nothing is loosened: the work, the statistic and the threshold are the |
| 15 | * ones the check always had. A real slowdown is in every repetition, |
| 16 | * undisturbed ones included, so it still fails. When too few repetitions are |
| 17 | * undisturbed within the attempt budget the check is refused and says why |
| 18 | * (rx_busy_refuse below: NOT_RUN cpu_busy, never a pass). Where schedstat is missing every |
| 19 | * repetition counts (the old behaviour) and the output says so. |
| 20 | * |
| 21 | * The delay is summed over the whole process, so it cannot tell the |
| 22 | * machine's load from contention the code under test makes itself. A leg |
| 23 | * that intermittently makes its own threads wait (one run in three, say) |
| 24 | * would have exactly those slow runs set aside, and its slowdown would pass |
| 25 | * unseen. So each leg's disturbed repetitions are counted, and the check is |
| 26 | * refused (NOT_RUN leg_skew, never a pass) when the leg the check claims is |
| 27 | * the faster one was disturbed significantly more often than the other leg |
| 28 | * (rx_pairs_skew_p below) and the claim holds on the kept runs. A claim |
| 29 | * that fails on them is judged as it would be without the test: the skew |
| 30 | * only set aside more of the claimed-faster leg's runs, which favours the |
| 31 | * claim. Set-aside runs of the other leg only make that leg look faster, |
| 32 | * which works against the claim, so the test is one-sided. |
| 33 | * |
| 34 | * Known limit: the test assumes both legs offer the machine's load the same |
| 35 | * number of threads to delay. Where one leg keeps several workers on cores |
| 36 | * and the other one (a parallel graph against its serialized fusion), load |
| 37 | * alone disturbs the wider leg more often: on the loaded Spark the 4-worker |
| 38 | * leg was refused for skew in runs where nothing but load was at work, and |
| 39 | * dividing each repetition's delay by its mean number of threads on a core |
| 40 | * still left 7 of 25 such runs refused. Checks whose legs differ in |
| 41 | * parallelism therefore skip the skew test and say so in their output and |
| 42 | * receipt; for them, contention a leg makes itself only intermittently can |
| 43 | * still hide in its set-aside runs, as before this test existed. |
| 44 | */ |
| 45 | #ifndef RX_QUIET_REPS_H |
| 46 | #define RX_QUIET_REPS_H |
| 47 | |
| 48 | #include <dirent.h> |
| 49 | #include <stdint.h> |
| 50 | #include <stdio.h> |
| 51 | |
| 52 | /* Allowance: run-queue delay summed over all threads of the process during |
| 53 | * one repetition, at most RX_QUIET_FLOOR_NS plus RX_QUIET_PERMILLE of the |
| 54 | * repetition's wall time. The floor covers waking an idle worker (a few |
| 55 | * microseconds per hand-off on an idle core). */ |
| 56 | #define RX_QUIET_FLOOR_NS 50000ull |
| 57 | #define RX_QUIET_PERMILLE 20ull |
| 58 | |
| 59 | /* Sum of run-queue delay (ns) over every thread of this process; -1 when the |
| 60 | * kernel does not report it. */ |
| 61 | static inline int64_t rx_run_delay_ns(void) { |
| 62 | DIR *d = opendir("/proc/self/task"); |
| 63 | if (!d) return -1; |
| 64 | int64_t sum = 0; |
| 65 | int seen = 0; |
| 66 | struct dirent *de; |
| 67 | while ((de = readdir(d))) { |
| 68 | if (de->d_name[0] < '0' || de->d_name[0] > '9') continue; |
| 69 | char path[288]; |
| 70 | snprintf(path, sizeof path, "/proc/self/task/%s/schedstat", de->d_name); |
| 71 | FILE *fp = fopen(path, "r"); |
| 72 | if (!fp) continue; |
| 73 | unsigned long long on_cpu = 0, waited = 0; |
| 74 | if (fscanf(fp, "%llu %llu", &on_cpu, &waited) == 2) { |
| 75 | sum += (int64_t)waited; |
| 76 | seen = 1; |
| 77 | } |
| 78 | fclose(fp); |
| 79 | } |
| 80 | closedir(d); |
| 81 | return seen ? sum : -1; |
| 82 | } |
| 83 | |
| 84 | /* Whether a repetition of `wall_ns` whose threads waited `delay_ns` for a |
| 85 | * core counts. delay < 0 (not reported) always counts. */ |
| 86 | static inline int rx_quiet_rep(int64_t delay_ns, uint64_t wall_ns) { |
| 87 | if (delay_ns < 0) return 1; |
| 88 | return (uint64_t)delay_ns <= RX_QUIET_FLOOR_NS + wall_ns * RX_QUIET_PERMILLE / 1000ull; |
| 89 | } |
| 90 | |
| 91 | /* Leg skew. Under "the machine, not the leg, disturbs runs" a leg's |
| 92 | * disturbed repetitions are no more frequent than the other leg's. Legs are |
| 93 | * paired (each attempt times both legs back to back, as in |
| 94 | * rx_workflow_fusion.c), so the test is McNemar's exact test, one-sided at |
| 95 | * RX_SKEW_ALPHA: among the attempts where exactly one leg was disturbed, the |
| 96 | * count where it was the claimed-faster leg is Binomial(n, 1/2) under the |
| 97 | * null; p = P(X >= that count). |
| 98 | * |
| 99 | * A false refusal is a NOT_RUN, never a false FAIL. At RX_SKEW_ALPHA = 1/1000 |
| 100 | * the paired test cannot refuse with fewer than 10 one-leg-disturbed |
| 101 | * attempts, so a quiet machine (a handful of disturbed attempts) is never |
| 102 | * refused. Thresholds (WolframLanguageEvaluator, 2026-10-31, with |
| 103 | * up[n_, k_] := 1 - CDF[BinomialDistribution[n, 1/2], k - 1]): |
| 104 | * Min[Select[Range[1, 40], up[#, #] <= 1/1000 &]] -> 10 |
| 105 | * up[10, 10] -> 1/1024 (0.000977), up[10, 9] -> 0.0107 |
| 106 | * smallest refused count for n = 20, 50, 100, 300, 600, 2460: |
| 107 | * 18, 37, 66, 178, 339, 1308 |
| 108 | * rx_sign_upper is checked against these in rx_workflow_fusion.c |
| 109 | * (t_skew_math). */ |
| 110 | #define RX_SKEW_ALPHA 0.001 |
| 111 | |
| 112 | /* The tail sums pmf weights scaled to 1 at the mode, built by the pmf ratio |
| 113 | * of neighbours, so nothing overflows and no libm is needed; weights that |
| 114 | * underflow are below 1e-300 of the total. */ |
| 115 | |
| 116 | /* P(X >= k) for X ~ Binomial(n, 1/2). */ |
| 117 | static inline double rx_sign_upper(uint32_t n, uint32_t k) { |
| 118 | if (k == 0) return 1.0; |
| 119 | if (k > n) return 0.0; |
| 120 | uint32_t m = n / 2; |
| 121 | double w = 1.0, total = 1.0, tail = m >= k ? 1.0 : 0.0; |
| 122 | for (uint32_t i = m + 1; i <= n; i++) { /* w(i) = w(i-1) (n-i+1) / i */ |
| 123 | w *= (double)(n - i + 1) / (double)i; |
| 124 | total += w; |
| 125 | if (i >= k) tail += w; |
| 126 | } |
| 127 | w = 1.0; |
| 128 | for (uint32_t i = m; i-- > 0;) { /* w(i) = w(i+1) (i+1) / (n-i) */ |
| 129 | w *= (double)(i + 1) / (double)(n - i); |
| 130 | total += w; |
| 131 | if (i >= k) tail += w; |
| 132 | } |
| 133 | return tail / total; |
| 134 | } |
| 135 | |
| 136 | /* Paired legs: attempts where exactly one leg was disturbed, by leg. */ |
| 137 | typedef struct { |
| 138 | uint32_t only[2]; |
| 139 | } RxPairs; |
| 140 | |
| 141 | static inline void rx_pairs_note(RxPairs *s, int quiet0, int quiet1) { |
| 142 | if (quiet0 != quiet1) s->only[quiet0 ? 1 : 0]++; |
| 143 | } |
| 144 | |
| 145 | /* One-sided p that leg `fast` (the one the check claims is faster) was |
| 146 | * disturbed more often than the other leg. */ |
| 147 | static inline double rx_pairs_skew_p(const RxPairs *s, int fast) { |
| 148 | return rx_sign_upper(s->only[0] + s->only[1], s->only[fast ? 1 : 0]); |
| 149 | } |
| 150 | |
| 151 | /* A check that found the machine too busy to time is a refusal, not a |
| 152 | * failure: nothing was measured, so nothing is judged, and the checks that |
| 153 | * need that measurement are not judged either. The program then ends with |
| 154 | * the local judge's "could not run" contract (aien-test SELF_NOT_RUN_EXIT): |
| 155 | * stdout line `AIEN-OBS not_run_reason "<reason>"` and exit 77, a retryable |
| 156 | * NOT_RUN, never a pass. Any real failure anywhere in the run wins: the exit |
| 157 | * is then 1, whatever was refused. The reason is "cpu_busy", or "leg_skew" |
| 158 | * when a leg-skew refusal (rx_skew_refuse) happened: then the timing could |
| 159 | * not be trusted because of the code under test, not the machine. */ |
| 160 | #define RX_NOT_RUN_EXIT 77 |
| 161 | |
| 162 | static inline unsigned *rx_busy_refusals(void) { |
| 163 | static unsigned n; |
| 164 | return &n; |
| 165 | } |
| 166 | |
| 167 | static inline unsigned *rx_skew_refusals(void) { |
| 168 | static unsigned n; |
| 169 | return &n; |
| 170 | } |
| 171 | |
| 172 | /* Record one refusal; `what` names the measurement that was not taken. */ |
| 173 | static inline void rx_busy_refuse(const char *what) { |
| 174 | (*rx_busy_refusals())++; |
| 175 | fprintf(stderr, " NOT RUN %s (cpu_busy): checks that need this measurement are not judged\n", what); |
| 176 | } |
| 177 | |
| 178 | /* Record a leg-skew refusal: the leg the check claims is faster was disturbed |
| 179 | * in `fast_disturbed` attempts, the other leg in `other_disturbed`, one-sided |
| 180 | * p = `p` <= RX_SKEW_ALPHA. */ |
| 181 | static inline void rx_skew_refuse(const char *what, uint32_t fast_disturbed, uint32_t other_disturbed, double p) { |
| 182 | (*rx_busy_refusals())++; |
| 183 | (*rx_skew_refusals())++; |
| 184 | fprintf(stderr, |
| 185 | " NOT RUN %s (leg_skew): the leg claimed faster was disturbed in %u attempts, the other leg in %u " |
| 186 | "(one-sided p %.3g <= %g): its set-aside runs may hide a slowdown it causes itself; checks that need " |
| 187 | "this measurement are not judged\n", |
| 188 | what, fast_disturbed, other_disturbed, p, RX_SKEW_ALPHA); |
| 189 | } |
| 190 | |
| 191 | /* The not_run_reason of this run's refusals. */ |
| 192 | static inline const char *rx_busy_reason(void) { |
| 193 | return *rx_skew_refusals() ? "leg_skew" : "cpu_busy"; |
| 194 | } |
| 195 | |
| 196 | /* The program's exit status from its failure count and `pass`, everything |
| 197 | * else its receipt judges: a refusal never covers a check judged false. */ |
| 198 | static inline int rx_busy_exit(int failures, int pass) { |
| 199 | if (failures) return 1; |
| 200 | if (*rx_busy_refusals()) { |
| 201 | if (!pass) return 1; |
| 202 | printf("AIEN-OBS not_run_reason \"%s\"\n", rx_busy_reason()); |
| 203 | fflush(stdout); |
| 204 | return RX_NOT_RUN_EXIT; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* A receipt's gate word: FAIL when `pass` (everything judged) is false, |
| 210 | * NOT_RUN when a measurement was refused, else PASS. */ |
| 211 | static inline const char *rx_busy_verdict(int pass) { |
| 212 | if (!pass) return "FAIL"; |
| 213 | return *rx_busy_refusals() ? "NOT_RUN" : "PASS"; |
| 214 | } |
| 215 | |
| 216 | #endif |