Add smoke test and benchmarks

smoke links crt0.o + libc.a with -nostdlib -static and proves the whole chain runs without the dynamic linker; bench_strlen and bench_syscall measure the string core and raw syscall round-trip cost via make bench.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <[email protected]>
This commit is contained in:
2026-08-30 04:09:45 -04:00
co-authored by Sisyphus
parent b9f8d9bf19
commit 370e64c3a6
4 changed files with 131 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
/*
* bench.h — tiny shared helpers for nulsl-libc benchmarks.
*
* Deliberately dependency-free beyond the libc itself: timing goes
* through a raw clock_gettime syscall, and numbers are printed with
* putchar/write because printf() is still a stub. If you write a new
* benchmark, include this and stay this lean.
*/
#ifndef _NULSL_BENCH_H
#define _NULSL_BENCH_H
#include <sys/syscall.h>
#include <unistd.h>
#define BENCH_CLOCK_MONOTONIC 1
/* Monotonic time in nanoseconds, via raw clock_gettime(2). */
static inline long long bench_now_ns(void)
{
struct {
long tv_sec;
long tv_nsec;
} t;
syscall(SYS_clock_gettime, BENCH_CLOCK_MONOTONIC, &t);
return t.tv_sec * 1000000000LL + t.tv_nsec;
}
/* Print an unsigned integer plus a newline, without printf(). */
static inline void bench_print_u64(unsigned long long v)
{
char buf[24];
int i = sizeof buf;
buf[--i] = '\n';
do {
buf[--i] = (char)('0' + v % 10);
v /= 10;
} while (v);
write(STDOUT_FILENO, buf + i, sizeof buf - i);
}
#endif /* _NULSL_BENCH_H */
+28
View File
@@ -0,0 +1,28 @@
/*
* bench_strlen.c — how fast is our string core?
*
* Measures strlen() over a typical short string. 10M iterations keeps
* the loop overhead negligible; results are printed as ns per call.
*/
#include <string.h>
#include "bench.h"
#define ITERS 10000000UL
int main(void)
{
static const char s[] = "the quick brown fox jumps over the lazy dog";
volatile size_t sink = 0;
long long t0, t1;
t0 = bench_now_ns();
for (unsigned long i = 0; i < ITERS; i++)
sink += strlen(s);
t1 = bench_now_ns();
(void)sink; /* keep the loop observable */
bench_print_u64((unsigned long long)((t1 - t0) / ITERS));
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
/*
* bench_syscall.c — raw syscall round-trip cost.
*
* This is the number that justifies the whole project: for a kernel-first
* libc (project guideline #6), every wrapper is one `syscall` instruction
* away from the kernel. getpid() is the cheapest syscall there is, so
* this measures the floor. 1M iterations; results in ns per call.
*/
#include <unistd.h>
#include "bench.h"
#define ITERS 1000000UL
int main(void)
{
volatile pid_t sink = 0;
long long t0, t1;
t0 = bench_now_ns();
for (unsigned long i = 0; i < ITERS; i++)
sink += getpid();
t1 = bench_now_ns();
(void)sink;
bench_print_u64((unsigned long long)((t1 - t0) / ITERS));
return 0;
}