omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / v2 / measure.c
| 1 | /* Native ABI v2 caller cost (tests/native/v2/run.sh measure): what a C caller pays per call, split into |
| 2 | * the argument checks, the entry (callee-saved registers, stack switch, runtime table), the out-buffer |
| 3 | * copies and the Omega code itself, for a small and a large view. Each figure is the lowest of 7 runs of |
| 4 | * N calls (ns per call, CLOCK_MONOTONIC). The code is what OMEGA_NATIVE_OMC2 compiled from vec.om, lay.om |
| 5 | * and madd.om. HOST evidence. */ |
| 6 | #define _GNU_SOURCE |
| 7 | #include "omega_native.h" |
| 8 | #include <pthread.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | static double now(void) |
| 16 | { |
| 17 | struct timespec t; |
| 18 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 19 | return t.tv_sec * 1e9 + t.tv_nsec; |
| 20 | } |
| 21 | |
| 22 | static const uint8_t *g_d; |
| 23 | static uint64_t g_n; |
| 24 | static volatile uint64_t sink; |
| 25 | |
| 26 | __attribute__((noinline)) static uint64_t c_sum(const uint8_t *d, uint64_t n) |
| 27 | { |
| 28 | uint64_t s = 0; |
| 29 | for (uint64_t i = 0; i < n; i++) { |
| 30 | s += d[i]; |
| 31 | __asm__ volatile("" : "+r"(s)); /* keep the loop scalar, as the Omega code is */ |
| 32 | } |
| 33 | return s; |
| 34 | } |
| 35 | __attribute__((noinline)) static uint64_t c_empty(const uint8_t *d, uint64_t n) |
| 36 | { |
| 37 | __asm__ volatile("" ::"r"(d), "r"(n)); |
| 38 | return n; |
| 39 | } |
| 40 | |
| 41 | typedef void (*op)(void); |
| 42 | static uint64_t r; |
| 43 | static uint8_t o8[8]; |
| 44 | static const uint8_t d8[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 45 | static void m_refuse(void) { sink = omega2_bsum(&r, NULL, 1); } |
| 46 | static void m_empty(void) { sink = omega2_bsum(&r, g_d, 0); } |
| 47 | static void m_vlen(void) { sink = omega2_vlen(&r, (const uint64_t *)(const void *)g_d, 1); } |
| 48 | static void m_v1(void) { sink = omega_madd(&r, 3, 5, 7); } |
| 49 | static void m_bsum(void) { sink = omega2_bsum(&r, g_d, g_n); } |
| 50 | static void m_swap8(void) { sink = omega2_swap8(&r, d8, 8, o8); } |
| 51 | static void m_cempty(void) { sink = c_empty(g_d, g_n); } |
| 52 | static void m_csum(void) { sink = c_sum(g_d, g_n); } |
| 53 | |
| 54 | static double best(op f, long n) |
| 55 | { |
| 56 | double b = 1e300; |
| 57 | for (int k = 0; k < 7; k++) { |
| 58 | double t0 = now(); |
| 59 | for (long i = 0; i < n; i++) |
| 60 | f(); |
| 61 | double t = (now() - t0) / n; |
| 62 | if (t < b) |
| 63 | b = t; |
| 64 | } |
| 65 | return b; |
| 66 | } |
| 67 | |
| 68 | static long rss_kib(void) |
| 69 | { |
| 70 | long pages = 0, res = 0; |
| 71 | FILE *f = fopen("/proc/self/statm", "r"); |
| 72 | if (f) { |
| 73 | if (fscanf(f, "%ld %ld", &pages, &res) != 2) |
| 74 | res = 0; |
| 75 | fclose(f); |
| 76 | } |
| 77 | return res * (sysconf(_SC_PAGESIZE) / 1024); |
| 78 | } |
| 79 | |
| 80 | static double first_ns, second_ns; |
| 81 | static long rss_before, rss_after; |
| 82 | static void *fresh(void *a) |
| 83 | { |
| 84 | (void)a; |
| 85 | rss_before = rss_kib(); |
| 86 | double t0 = now(); |
| 87 | omega2_vlen(&r, (const uint64_t *)(const void *)g_d, 1); |
| 88 | double t1 = now(); |
| 89 | omega2_vlen(&r, (const uint64_t *)(const void *)g_d, 1); |
| 90 | double t2 = now(); |
| 91 | rss_after = rss_kib(); |
| 92 | first_ns = t1 - t0; |
| 93 | second_ns = t2 - t1; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int main(void) |
| 98 | { |
| 99 | uint64_t big = 16777216; |
| 100 | uint8_t *d = malloc(big); |
| 101 | for (uint64_t i = 0; i < big; i++) |
| 102 | d[i] = (uint8_t)(i * 2654435761u >> 24); |
| 103 | g_d = d; |
| 104 | m_vlen(); /* map this thread's Omega stack before timing */ |
| 105 | printf("MEASURE refused-call (checks only, no entry) %.1f ns\n", best(m_refuse, 2000000)); |
| 106 | printf("MEASURE empty-view-call (checks + entry + stack switch, 0 elements) %.1f ns\n", best(m_empty, 2000000)); |
| 107 | printf("MEASURE vlen-call (one view, code returns its len) %.1f ns\n", best(m_vlen, 2000000)); |
| 108 | printf("MEASURE v1-madd-call (ABI v1 baseline, three scalars) %.1f ns\n", best(m_v1, 2000000)); |
| 109 | printf("MEASURE swap8-call (view + 8-byte out: zero, code, memmove) %.1f ns\n", best(m_swap8, 2000000)); |
| 110 | g_n = 0; |
| 111 | printf("MEASURE c-direct-call (a plain noinline C call, for scale) %.1f ns\n", best(m_cempty, 2000000)); |
| 112 | g_n = 16; |
| 113 | double sb = best(m_bsum, 1000000), sc = best(m_csum, 1000000); |
| 114 | printf("MEASURE small-view bsum 16 B: omega %.1f ns, scalar C loop %.1f ns\n", sb, sc); |
| 115 | g_n = 4096; |
| 116 | sb = best(m_bsum, 20000), sc = best(m_csum, 20000); |
| 117 | printf("MEASURE view bsum 4 KiB: omega %.1f ns (%.3f ns/B), scalar C loop %.1f ns (%.3f ns/B)\n", sb, sb / 4096, sc, |
| 118 | sc / 4096); |
| 119 | g_n = big; |
| 120 | sb = best(m_bsum, 5), sc = best(m_csum, 5); |
| 121 | printf("MEASURE large-view bsum 16 MiB: omega %.0f ns (%.3f ns/B), scalar C loop %.0f ns (%.3f ns/B)\n", sb, sb / big, |
| 122 | sc, sc / big); |
| 123 | pthread_t t; |
| 124 | pthread_create(&t, 0, fresh, 0); |
| 125 | pthread_join(t, 0); |
| 126 | printf("MEASURE first call on a new thread (maps 8 MiB stack + 8 MiB guard) %.0f ns, second %.0f ns\n", first_ns, |
| 127 | second_ns); |
| 128 | printf("MEASURE RSS around a new thread's first two calls: %ld KiB -> %ld KiB\n", rss_before, rss_after); |
| 129 | free(d); |
| 130 | return 0; |
| 131 | } |