Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
2613 lines (2483 loc) · 112 KB
/
Copy pathflake.nix
File metadata and controls
2613 lines (2483 loc) · 112 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
description = "beastdb — high-assurance embedded KV store (Lean 4 + Nix; freestanding libbeastdb)";
inputs = {
nixpkgs.follows = "lean4-nix/nixpkgs";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
lean4-nix.url = "github:lenianiva/lean4-nix";
systems-lean.url = "github:cryptoquick/lean4/systems";
};
outputs = inputs @ {
nixpkgs,
flake-parts,
lean4-nix,
systems-lean,
systems,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = import systems;
perSystem = {
system,
...
}: let
pkgs = import nixpkgs {
inherit system;
overlays = [(lean4-nix.readToolchainFile ./lean-toolchain)];
};
productSrc = pkgs.lib.cleanSourceWith {
src = ./.;
filter = path: type: let
base = baseNameOf path;
in
!(base == "ref" && type == "directory")
&& pkgs.lib.cleanSourceFilter path type;
};
systemsLeanPkg =
let
sysPkgs = systems-lean.packages.${system} or {};
in
sysPkgs.systems-lean or null;
freestandingLibbeastdb =
if systemsLeanPkg != null
then
import ./nix/libbeastdb.nix {
inherit pkgs;
systemsLean = systemsLeanPkg;
freestandingSrc = ./freestanding;
}
else null;
# First-party package SPDX: Unlicense (see LICENSE.md). Not for ref/ upstream.
beastdbLicenseMeta = description: {
inherit description;
license = pkgs.lib.licenses.unlicense;
platforms = pkgs.lib.platforms.unix;
};
leanPkg = pkgs.lean.buildLeanPackage {
name = "beastdb";
roots = ["beastdb.Main"];
src = productSrc;
debug = false;
leancFlags = ["-O3" "-DNDEBUG"];
};
leanPkgExport = pkgs.lean.buildLeanPackage {
name = "beastdb_export";
roots = ["beastdb.FfiExport"];
src = productSrc;
debug = false;
leancFlags = ["-O3" "-DNDEBUG"];
};
beastdbFsync =
pkgs.stdenv.mkDerivation {
name = "beastdb-fsync";
dontUnpack = true;
buildInputs = [];
buildPhase = ''
cat > fsync_path.c <<'EOF'
#define _POSIX_C_SOURCE 200809L
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv) {
int dir = 0;
const char *path = NULL;
if (argc == 2) { path = argv[1]; }
else if (argc == 3 && strcmp(argv[1], "--dir") == 0) {
dir = 1; path = argv[2];
} else {
fprintf(stderr, "usage: beastdb-fsync [--dir] PATH\n");
return 2;
}
int fd = open(path, dir ? O_RDONLY : O_RDWR);
if (fd < 0) { perror("open"); return 1; }
if (fsync(fd) != 0) { perror("fsync"); close(fd); return 1; }
if (close(fd) != 0) { perror("close"); return 1; }
return 0;
}
EOF
$CC -O2 -o beastdb-fsync fsync_path.c
'';
installPhase = ''
mkdir -p $out/bin
cp beastdb-fsync $out/bin/
'';
meta = beastdbLicenseMeta "beastdb optional path fsync helper";
};
beastdbWrapped =
pkgs.runCommand "beastdb" {
nativeBuildInputs = [pkgs.makeWrapper];
meta = beastdbLicenseMeta "beastdb host CLI (lean4-nix pin; multi-file durable store)";
} ''
mkdir -p $out/bin
makeWrapper ${leanPkg.executable}/bin/beastdb $out/bin/beastdb \
--prefix PATH : ${beastdbFsync}/bin
ln -s ${beastdbFsync}/bin/beastdb-fsync $out/bin/beastdb-fsync
'';
beastdbRelease =
pkgs.runCommand "beastdb-release" {
nativeBuildInputs = [pkgs.makeWrapper pkgs.binutils];
meta = beastdbLicenseMeta "beastdb stripped release CLI wrapper";
} ''
mkdir -p $out/bin
cp ${leanPkg.executable}/bin/beastdb $out/bin/beastdb
chmod u+w $out/bin/beastdb
strip --strip-unneeded $out/bin/beastdb
mv $out/bin/beastdb $out/bin/beastdb.unwrapped
makeWrapper $out/bin/beastdb.unwrapped $out/bin/beastdb \
--prefix PATH : ${beastdbFsync}/bin
ln -s ${beastdbFsync}/bin/beastdb-fsync $out/bin/beastdb-fsync
'';
lmdbMicrobench =
pkgs.stdenv.mkDerivation {
name = "beastdb-lmdb-bench";
dontUnpack = true;
buildInputs = [pkgs.lmdb];
buildPhase = ''
cat > lmdb_bench.c <<'EOF'
#define _POSIX_C_SOURCE 200809L
#include <lmdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <errno.h>
static long long now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
}
static int put_kv(MDB_txn *txn, MDB_dbi dbi, int i) {
char kbuf[64], vbuf[64];
snprintf(kbuf, sizeof kbuf, "k%d", i);
snprintf(vbuf, sizeof vbuf, "v%d", i);
MDB_val key = { .mv_size = strlen(kbuf), .mv_data = kbuf };
MDB_val val = { .mv_size = strlen(vbuf), .mv_data = vbuf };
return mdb_put(txn, dbi, &key, &val, 0);
}
static int get_kv(MDB_txn *txn, MDB_dbi dbi, int i) {
char kbuf[64];
snprintf(kbuf, sizeof kbuf, "k%d", i);
MDB_val key = { .mv_size = strlen(kbuf), .mv_data = kbuf };
MDB_val val;
return mdb_get(txn, dbi, &key, &val);
}
int main(int argc, char **argv) {
const char *dir = argc >= 2 ? argv[1] : "/tmp/beastdb-lmdb-bench";
int n = argc >= 3 ? atoi(argv[2]) : 1000;
if (n < 1) { fprintf(stderr, "n must be >= 1\n"); return 2; }
if (mkdir(dir, 0755) != 0 && errno != EEXIST) {
perror("mkdir"); return 1;
}
MDB_env *env = NULL;
MDB_txn *txn = NULL;
MDB_dbi dbi;
int rc;
if ((rc = mdb_env_create(&env)) != 0) {
fprintf(stderr, "mdb_env_create: %s\n", mdb_strerror(rc)); return 1;
}
mdb_env_set_mapsize(env, 256UL * 1024UL * 1024UL);
if ((rc = mdb_env_open(env, dir, 0, 0644)) != 0) {
fprintf(stderr, "mdb_env_open: %s\n", mdb_strerror(rc)); return 1;
}
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0) {
fprintf(stderr, "txn: %s\n", mdb_strerror(rc)); return 1;
}
if ((rc = mdb_dbi_open(txn, NULL, 0, &dbi)) != 0) {
fprintf(stderr, "dbi: %s\n", mdb_strerror(rc)); return 1;
}
long long t0 = now_ms();
for (int i = 0; i < n; i++) {
if ((rc = put_kv(txn, dbi, i)) != 0) {
fprintf(stderr, "put %d: %s\n", i, mdb_strerror(rc)); return 1;
}
}
if ((rc = mdb_txn_commit(txn)) != 0) {
fprintf(stderr, "commit: %s\n", mdb_strerror(rc)); return 1;
}
long long t1 = now_ms();
long long put_ms = t1 - t0;
if ((rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)) != 0) {
fprintf(stderr, "ro txn: %s\n", mdb_strerror(rc)); return 1;
}
t0 = now_ms();
for (int i = 0; i < n; i++) {
if ((rc = get_kv(txn, dbi, i)) != 0) {
fprintf(stderr, "get %d: %s\n", i, mdb_strerror(rc)); return 1;
}
}
mdb_txn_abort(txn);
t1 = now_ms();
long long get_ms = t1 - t0;
mdb_env_sync(env, 1);
mdb_dbi_close(env, dbi);
mdb_env_close(env);
long long put_ops = put_ms <= 0 ? (long long)n * 1000 : ((long long)n * 1000) / put_ms;
long long get_ops = get_ms <= 0 ? (long long)n * 1000 : ((long long)n * 1000) / get_ms;
printf("lmdb bench n=%d dir=%s\n", n, dir);
printf("put: n=%d wall_ms=%lld ops_per_s=%lld\n", n, put_ms, put_ops);
printf("get: n=%d wall_ms=%lld ops_per_s=%lld\n", n, get_ms, get_ops);
printf("beastdb-lmdb-bench complete\n");
return 0;
}
EOF
$CC -O2 -o beastdb-lmdb-bench lmdb_bench.c -llmdb
'';
installPhase = ''
mkdir -p $out/bin
cp beastdb-lmdb-bench $out/bin/
'';
meta = beastdbLicenseMeta "beastdb LMDB microbench harness (measure only; first-party)";
};
hostProbeSrc = pkgs.writeText "beastdb-host-probe.c" ''
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int flag_present(const char *flags, const char *name) {
const char *p = flags;
size_t n = strlen(name);
while ((p = strstr(p, name)) != NULL) {
char before = (p == flags) ? ' ' : p[-1];
char after = p[n];
if ((before == ' ' || before == '\t' || before == '\n') &&
(after == ' ' || after == '\t' || after == '\n' || after == '\0'))
return 1;
p += n;
}
return 0;
}
int main(void) {
long online = sysconf(_SC_NPROCESSORS_ONLN);
long conf = sysconf(_SC_NPROCESSORS_CONF);
printf("beastdb-host-probe\n");
printf("note: local planning only; do not commit unique machine dumps\n");
printf("logical_cpus_online=%ld\n", online);
printf("logical_cpus_configured=%ld\n", conf);
#ifdef __linux__
{
FILE *f = fopen("/proc/cpuinfo", "r");
char line[4096];
char flags[8192];
flags[0] = '\0';
if (f) {
/* First flags/Features line only (local planning; not hybrid-CPU inventory). */
while (fgets(line, sizeof line, f)) {
if (strncmp(line, "flags", 5) == 0 || strncmp(line, "Features", 8) == 0) {
char *colon = strchr(line, ':');
if (colon) {
strncpy(flags, colon + 1, sizeof flags - 1);
flags[sizeof flags - 1] = '\0';
break;
}
}
}
fclose(f);
}
if (flags[0] != '\0') {
printf("vector_source=first_cpuinfo_flags_line\n");
printf("vector_sse=%s\n", flag_present(flags, "sse") ? "yes" : "no");
printf("vector_sse2=%s\n", flag_present(flags, "sse2") ? "yes" : "no");
printf("vector_avx=%s\n", flag_present(flags, "avx") ? "yes" : "no");
printf("vector_avx2=%s\n", flag_present(flags, "avx2") ? "yes" : "no");
printf("vector_avx512f=%s\n", flag_present(flags, "avx512f") ? "yes" : "no");
printf("vector_asimd=%s\n", flag_present(flags, "asimd") ? "yes" : "no");
} else {
printf("vector_flags=unavailable\n");
}
}
#else
printf("vector_flags=probe_linux_only\n");
#endif
printf("beastdb-host-probe complete\n");
return 0;
}
'';
hostProbe =
pkgs.stdenv.mkDerivation {
name = "beastdb-host-probe";
dontUnpack = true;
buildPhase = ''
$CC -O2 -o beastdb-host-probe ${hostProbeSrc}
'';
installPhase = ''
mkdir -p $out/bin
cp beastdb-host-probe $out/bin/
'';
meta = beastdbLicenseMeta "beastdb local host capability probe (no identity fields)";
};
linuxPerf =
if !pkgs.stdenv.isLinux
then null
else if pkgs ? perf
then pkgs.perf
else if pkgs ? linuxPackages && pkgs.linuxPackages ? perf
then pkgs.linuxPackages.perf
else if pkgs ? linuxPackages_latest && pkgs.linuxPackages_latest ? perf
then pkgs.linuxPackages_latest.perf
else null;
linuxValgrind =
if pkgs.stdenv.isLinux && pkgs ? valgrind then pkgs.valgrind else null;
linuxRr =
if pkgs.stdenv.isLinux && pkgs ? rr then pkgs.rr else null;
namecheck =
import ./nix/namecheck.nix {
inherit (pkgs) lib;
inherit pkgs;
root = ./.;
};
whitespaceTools =
import ./nix/whitespace.nix {
inherit pkgs;
inherit (pkgs) lib;
checkSrc = productSrc;
};
whitespaceBin = whitespaceTools.whitespaceBin;
whitespaceCheck = whitespaceTools.whitespaceCheck;
noSorry =
pkgs.runCommand "beastdb-no-sorry" {
src = productSrc;
} ''
if grep -R --include='*.lean' -nE '(^|[^a-zA-Z_])sorry([^a-zA-Z_]|$)' "$src/beastdb"; then
echo "beastdb: sorry found in product Lean sources" >&2
exit 1
fi
mkdir -p $out
echo ok > $out/result
'';
gitPolicy =
import ./nix/git-policy.nix {
inherit pkgs;
inherit (pkgs) lib;
inherit whitespaceBin;
};
preCommitHook = gitPolicy.preCommitHook;
verifySignatures = gitPolicy.verifySignatures;
prePushHook = gitPolicy.prePushHook;
installHooks = gitPolicy.installHooks;
# User-facing flake apps: store Python (writePython3Bin), not writeShellApplication.
flakeApps =
import ./nix/flake-apps.nix {
inherit pkgs;
inherit (pkgs) lib;
inherit beastdbWrapped lmdbMicrobench freestandingLibbeastdb linuxPerf linuxValgrind whitespaceBin;
inherit beastdbFsync;
beastdbFfiCAsan = null;
};
freestandingGateApp = flakeApps.freestandingGateApp;
benchApp = flakeApps.benchApp;
profileCpuApp = flakeApps.profileCpuApp;
profileMemApp = flakeApps.profileMemApp;
testSuiteApp = flakeApps.testSuiteApp;
namecheckApp = flakeApps.namecheckApp;
whitespaceFixApp = flakeApps.whitespaceFixApp;
beastdbLib =
pkgs.stdenv.mkDerivation {
name = "beastdb-lib";
src = productSrc;
dontStrip = true;
nativeBuildInputs = [
pkgs.pkg-config
pkgs.lean.leanc
pkgs.lean.lean-all
pkgs.stdenv.cc
pkgs.binutils
];
propagatedBuildInputs = [pkgs.lean.leanshared];
BEASTDB_BIN = "${beastdbWrapped}/bin/beastdb";
buildPhase = ''
set -euo pipefail
mkdir -p build
export LEAN_CC="$CC"
LEANSHARED="${pkgs.lean.leanshared}"
STATIC_A="${leanPkgExport.staticLib}/libbeastdb_export.a"
test -f "$STATIC_A"
$CC -O2 -fPIC -pthread \
-I"$src/ffi/include" \
-I${pkgs.lean.lean-all}/include \
-DBEASTDB_BIN=\"$BEASTDB_BIN\" \
-DBEASTDB_HAS_EXPORT=1 \
-c "$src/ffi/src/beastdb_bridge.c" -o build/beastdb_bridge.o
leanc -shared -fPIC -pthread \
build/beastdb_bridge.o \
-Wl,--whole-archive "$STATIC_A" -Wl,--no-whole-archive \
$LEANSHARED/* \
-Wl,-rpath,"$LEANSHARED" \
-o build/libbeastdb.so
$CC -O2 -fPIC -pthread \
-I"$src/ffi/include" \
-I${pkgs.lean.lean-all}/include \
-DBEASTDB_BIN=\"$BEASTDB_BIN\" \
-DBEASTDB_HAS_EXPORT=0 \
-c "$src/ffi/src/beastdb_bridge.c" -o build/beastdb_bridge_static.o
$AR rcs build/libbeastdb.a build/beastdb_bridge_static.o
'';
installPhase = ''
mkdir -p $out/lib $out/include $out/lib/pkgconfig
cp build/libbeastdb.so $out/lib/
cp build/libbeastdb.a $out/lib/
cp "$src/ffi/include/beastdb.h" $out/include/
cat > $out/lib/pkgconfig/beastdb.pc <<EOF
prefix=$out
libdir=$out/lib
includedir=$out/include
Name: beastdb
Description: beastdb foreign C ABI (dual-backend: export default + CLI emergency)
Version: 0.1.0-core
Libs: -L$out/lib -lbeastdb -pthread
Cflags: -I$out/include
EOF
'';
meta = beastdbLicenseMeta "beastdb temporary export+runtime C ABI library (libbeastdb.so; not freestanding)";
};
libbeastdbThr =
if freestandingLibbeastdb != null
then
import ./nix/libbeastdb-thr.nix {
inherit pkgs freestandingLibbeastdb beastdbLib beastdbFsync;
}
else null;
libbeastdbThrApp =
if libbeastdbThr != null
then libbeastdbThr.thrApp
else null;
valueAssess =
import ./nix/value-assess.nix {
inherit pkgs beastdbWrapped beastdbLib beastdbFsync lmdbMicrobench;
};
valueAssessHarness = valueAssess.valueAssessHarness;
valueAssessApp = valueAssess.valueAssessApp;
compareLmdb =
import ./nix/compare-lmdb.nix {
inherit pkgs beastdbWrapped;
};
lmdbCompareBench = compareLmdb.lmdbCompareBench;
compareLmdbApp = compareLmdb.compareLmdbApp;
ycsbCSrcPath =
if builtins.pathExists ./ref/ycsb-c/Makefile
then ./ref/ycsb-c
else
pkgs.fetchFromGitHub {
owner = "basicthinker";
repo = "YCSB-C";
rev = "6475c7a5c3c7b229224ce5ca3332a58c994b8a09";
hash = "sha256-2p1BHgGcFuPBmQ29JSjAksIaeG05YEByuQqDKeFeAMo=";
};
ycsbBench =
import ./nix/ycsb-bench.nix {
inherit pkgs beastdbWrapped beastdbLib beastdbFsync;
ycsbCSrc = ycsbCSrcPath;
};
beastdbYcsbHarness = ycsbBench.beastdbYcsbHarness;
ycsbCRef = ycsbBench.ycsbCRef;
ycsbBenchApp = ycsbBench.ycsbBenchApp;
ycsbCLinked =
if ycsbCSrcPath == null
then null
else
import ./nix/ycsb-c-beastdb.nix {
inherit pkgs beastdbLib beastdbFsync beastdbWrapped;
ycsbCSrc = ycsbCSrcPath;
beastdbYcsbDriverSrc = ./bench/ycsb;
};
beastdbYcsbc =
if ycsbCLinked == null
then null
else ycsbCLinked.beastdbYcsbc;
ycsbcRunApp =
if ycsbCLinked == null
then null
else ycsbCLinked.ycsbcApp;
ycsbcCompareApp =
if ycsbCLinked == null
then null
else ycsbCLinked.ycsbcCompareApp;
measureChartsApp =
import ./nix/measure-charts/package.nix {
inherit pkgs;
};
suiteReport =
import ./nix/suite-report.nix {
inherit pkgs beastdbWrapped hostProbe compareLmdbApp ycsbBenchApp valueAssessApp;
ycsbcRunApp = ycsbcRunApp;
ycsbcCompareApp = ycsbcCompareApp;
libbeastdbThrApp = libbeastdbThrApp;
measureChartsApp = measureChartsApp;
};
systemClassApp = suiteReport.systemClassApp;
suiteReportApp = suiteReport.suiteReportApp;
measureFormatSmoke =
import ./nix/measure-format-smoke.nix {
inherit pkgs;
thrApp = libbeastdbThrApp;
suiteReportApp = suiteReportApp;
};
measureFormatSmokeApp = measureFormatSmoke.smokeBin;
measureFormatSmokeCheck = measureFormatSmoke.formatSmokeCheck;
deepMeasure =
import ./nix/deep-measure.nix {
inherit pkgs beastdbWrapped linuxPerf linuxValgrind linuxRr valueAssessHarness;
thrFs =
if libbeastdbThr != null
then libbeastdbThr.thrFs
else null;
};
deepCpuApp = deepMeasure.deepCpuApp;
deepMemApp = deepMeasure.deepMemApp;
deepRrApp = deepMeasure.deepRrApp;
linuxFlamegraph = deepMeasure.linuxFlamegraph;
beastdbExportSpike =
pkgs.stdenv.mkDerivation {
name = "beastdb-export-spike";
dontUnpack = true;
dontStrip = true;
nativeBuildInputs = [pkgs.lean.leanc pkgs.lean.lean-all pkgs.binutils pkgs.stdenv.cc];
buildPhase = ''
set -euo pipefail
mkdir -p build
export LEAN_CC="$CC"
STATIC_A="${leanPkgExport.staticLib}/libbeastdb_export.a"
test -f "$STATIC_A"
LEANSHARED="${pkgs.lean.leanshared}"
leanc -shared -fPIC \
-Wl,--whole-archive "$STATIC_A" -Wl,--no-whole-archive \
"$LEANSHARED"/* \
-Wl,-rpath,"$LEANSHARED" \
-o build/libbeastdb_export_spike.so
cat > build/spike_main.c <<'EOF'
#include <lean/lean.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/* Runtime init lives in libleanshared; not always prototyped in lean.h (v4.30 pin). */
void lean_initialize_runtime_module(void);
/*
* Pure UInt32 constant `@[export]` from beastdb.FfiExport becomes a *data*
* symbol (BSS/global), not a function — confirmed via nm on the .o (type B).
* Module init populates it.
*/
extern uint32_t beastdb_lean_api_version;
extern lean_object *initialize_beastdb_FfiExport(uint8_t builtin, lean_object *w);
int main(void) {
lean_initialize_runtime_module();
lean_object *res = initialize_beastdb_FfiExport(/*builtin=*/1, lean_io_mk_world());
if (lean_io_result_is_error(res)) {
lean_io_result_show_error(res);
return 1;
}
lean_dec_ref(res);
lean_io_mark_end_initialization();
uint32_t v = beastdb_lean_api_version;
if (v != 9u) {
fprintf(stderr,
"beastdb export spike FAIL: beastdb_lean_api_version=%u expected 9\n",
(unsigned)v);
return 1;
}
printf("beastdb export spike: beastdb_lean_api_version=%u ok\n", (unsigned)v);
printf("beastdb-export-spike link ok\n");
return 0;
}
EOF
leanc build/spike_main.c \
"$STATIC_A" \
"$LEANSHARED"/* \
-Wl,-rpath,"$LEANSHARED" \
-o build/beastdb-export-spike
nm -D --defined-only build/libbeastdb_export_spike.so \
| grep -E -q ' [TDBR] beastdb_lean_api_version$'
./build/beastdb-export-spike
'';
installPhase = ''
set -euo pipefail
mkdir -p $out/bin $out/lib $out/share/beastdb
cp build/beastdb-export-spike $out/bin/
cp build/libbeastdb_export_spike.so $out/lib/
{
echo "beastdb-export-spike package"
echo "static_lib=libbeastdb_export.a"
echo "shared_lib=libbeastdb_export_spike.so"
echo "export_symbol=beastdb_lean_api_version"
echo "init_symbol=initialize_beastdb_FfiExport"
echo "runtime_init=lean_initialize_runtime_module"
echo "leanshared=${pkgs.lean.leanshared}"
echo "platform=linux (flake systems also list darwin; spike validated on linux)"
echo "static_a_decision=harness runtime-proves static .a + leanshared; .so built+nm-checked; runtime shared embed validated in PR1 when linked into libbeastdb"
} > $out/share/beastdb/export-spike-link-facts.txt
$out/bin/beastdb-export-spike | tee $out/share/beastdb/export-spike-run.txt
grep -q 'beastdb_lean_api_version=9 ok' $out/share/beastdb/export-spike-run.txt
grep -q 'beastdb-export-spike link ok' $out/share/beastdb/export-spike-run.txt
'';
meta = beastdbLicenseMeta "beastdb Lean @[export] link spike (measure/CI harness)";
};
beastdbFfiC =
pkgs.stdenv.mkDerivation {
name = "beastdb-ffi-c";
src = productSrc;
buildInputs = [beastdbLib];
nativeBuildInputs = [];
buildPhase = ''
$CC -O2 -I${beastdbLib}/include \
"$src/ffi/demos/c/main.c" \
-L${beastdbLib}/lib -lbeastdb -Wl,-rpath,${beastdbLib}/lib \
-o beastdb-ffi-c
'';
installPhase = ''
mkdir -p $out/bin
cp beastdb-ffi-c $out/bin/
'';
meta = beastdbLicenseMeta "beastdb C ABI smoke demo";
};
beastdbFfiCAsan =
pkgs.stdenv.mkDerivation {
name = "beastdb-ffi-c-asan";
src = productSrc;
buildInputs = [beastdbLib];
nativeBuildInputs = [pkgs.makeWrapper pkgs.stdenv.cc];
dontStrip = true;
buildPhase = ''
set -euo pipefail
$CC -O1 -g -fno-omit-frame-pointer \
-fsanitize=address,undefined \
-I${beastdbLib}/include \
"$src/ffi/demos/c/main.c" \
-L${beastdbLib}/lib -lbeastdb -Wl,-rpath,${beastdbLib}/lib \
-fsanitize=address,undefined \
-o beastdb-ffi-c-asan
'';
installPhase = ''
set -euo pipefail
mkdir -p $out/bin
cp beastdb-ffi-c-asan $out/bin/beastdb-ffi-c-asan.unwrapped
makeWrapper $out/bin/beastdb-ffi-c-asan.unwrapped $out/bin/beastdb-ffi-c-asan \
--prefix PATH : ${beastdbFsync}/bin \
--run 'export ASAN_OPTIONS="''${ASAN_OPTIONS:-detect_leaks=0:halt_on_error=1:abort_on_error=1}"' \
--run 'export UBSAN_OPTIONS="''${UBSAN_OPTIONS:-halt_on_error=1:print_stacktrace=1}"'
'';
meta = beastdbLicenseMeta "beastdb C ABI ASan smoke (export path)";
};
beastdbFfiCAsanCli =
pkgs.stdenv.mkDerivation {
name = "beastdb-ffi-c-asan-cli";
src = productSrc;
nativeBuildInputs = [pkgs.makeWrapper pkgs.stdenv.cc];
dontStrip = true;
BEASTDB_BIN = "${beastdbWrapped}/bin/beastdb";
buildPhase = ''
set -euo pipefail
$CC -O1 -g -fPIC -pthread \
-fsanitize=address,undefined -fno-omit-frame-pointer \
-I"$src/ffi/include" \
-I${pkgs.lean.lean-all}/include \
-DBEASTDB_BIN=\"$BEASTDB_BIN\" \
-DBEASTDB_HAS_EXPORT=0 \
-c "$src/ffi/src/beastdb_bridge.c" -o beastdb_bridge_asan.o
$CC -O1 -g -fno-omit-frame-pointer \
-fsanitize=address,undefined \
-I"$src/ffi/include" \
-c "$src/ffi/demos/c/main.c" -o main_asan.o
$CC -fsanitize=address,undefined -fno-omit-frame-pointer \
-o beastdb-ffi-c-asan-cli main_asan.o beastdb_bridge_asan.o -pthread
'';
installPhase = ''
set -euo pipefail
mkdir -p $out/bin
cp beastdb-ffi-c-asan-cli $out/bin/beastdb-ffi-c-asan-cli.unwrapped
makeWrapper $out/bin/beastdb-ffi-c-asan-cli.unwrapped $out/bin/beastdb-ffi-c-asan-cli \
--prefix PATH : ${beastdbFsync}/bin:${beastdbWrapped}/bin \
--run 'export ASAN_OPTIONS="''${ASAN_OPTIONS:-halt_on_error=1:abort_on_error=1}"' \
--run 'export UBSAN_OPTIONS="''${UBSAN_OPTIONS:-halt_on_error=1:print_stacktrace=1}"'
'';
meta = beastdbLicenseMeta "beastdb C ABI ASan smoke (CLI emergency path)";
};
asanSmokeApp =
(import ./nix/flake-apps.nix {
inherit pkgs;
inherit (pkgs) lib;
inherit beastdbWrapped lmdbMicrobench freestandingLibbeastdb linuxPerf linuxValgrind whitespaceBin;
inherit beastdbFsync beastdbFfiCAsan;
}).asanSmokeApp;
beastdbFfiCpp =
pkgs.stdenv.mkDerivation {
name = "beastdb-ffi-cpp";
src = productSrc;
buildInputs = [beastdbLib];
buildPhase = ''
$CXX -O2 -I${beastdbLib}/include \
"$src/ffi/demos/cpp/main.cpp" \
-L${beastdbLib}/lib -lbeastdb -Wl,-rpath,${beastdbLib}/lib \
-o beastdb-ffi-cpp
'';
installPhase = ''
mkdir -p $out/bin
cp beastdb-ffi-cpp $out/bin/
'';
meta = beastdbLicenseMeta "beastdb C++ ABI smoke demo";
};
beastdbFfiRust =
pkgs.stdenv.mkDerivation {
name = "beastdb-ffi-rust";
src = productSrc;
nativeBuildInputs = [pkgs.rustc pkgs.gcc];
buildInputs = [beastdbLib];
buildPhase = ''
export HOME=$TMPDIR
rustc -O "$src/ffi/demos/rust/main.rs" \
-L native=${beastdbLib}/lib \
-l beastdb -C link-arg=-Wl,-rpath,${beastdbLib}/lib \
-o beastdb-ffi-rust
'';
installPhase = ''
mkdir -p $out/bin
cp beastdb-ffi-rust $out/bin/
'';
meta = beastdbLicenseMeta "beastdb thin Rust FFI smoke demo";
};
beastdbRust =
pkgs.stdenv.mkDerivation {
name = "beastdb-rust";
src = productSrc;
nativeBuildInputs = [pkgs.rustc pkgs.gcc];
buildInputs = [beastdbLib];
dontStrip = true;
buildPhase = ''
set -euo pipefail
export HOME=$TMPDIR
crate="$src/ffi/rust/beastdb"
rustc --edition 2021 -O --crate-type rlib --crate-name beastdb \
"$crate/src/lib.rs" \
-L native=${beastdbLib}/lib -l beastdb -l pthread \
-o libbeastdb.rlib
rustc --edition 2021 -O --crate-type bin --crate-name beastdb_rust_smoke \
"$crate/examples/smoke.rs" \
--extern beastdb=libbeastdb.rlib \
-L native=${beastdbLib}/lib -l beastdb -l pthread \
-C link-arg=-Wl,-rpath,${beastdbLib}/lib \
-o beastdb-rust-smoke
rustc --edition 2021 -O --test --crate-name beastdb_rust_t0 \
"$crate/tests/t0.rs" \
--extern beastdb=libbeastdb.rlib \
-L native=${beastdbLib}/lib -l beastdb -l pthread \
-C link-arg=-Wl,-rpath,${beastdbLib}/lib \
-o beastdb-rust-t0
rustc --edition 2021 -O --test --crate-name beastdb_rust_t1 \
"$crate/tests/t1.rs" \
--extern beastdb=libbeastdb.rlib \
-L native=${beastdbLib}/lib -l beastdb -l pthread \
-C link-arg=-Wl,-rpath,${beastdbLib}/lib \
-o beastdb-rust-t1
rustc --edition 2021 -O --test --crate-name beastdb_rust_t2 \
"$crate/tests/t2.rs" \
--extern beastdb=libbeastdb.rlib \
-L native=${beastdbLib}/lib -l beastdb -l pthread \
-C link-arg=-Wl,-rpath,${beastdbLib}/lib \
-o beastdb-rust-t2
'';
installPhase = ''
mkdir -p $out/bin $out/lib
cp beastdb-rust-smoke $out/bin/
cp beastdb-rust-t0 $out/bin/
cp beastdb-rust-t1 $out/bin/
cp beastdb-rust-t2 $out/bin/
cp libbeastdb.rlib $out/lib/
'';
meta = beastdbLicenseMeta "beastdb heed-inspired Rust crate (Unlicense; links beastdb-lib)";
};
beastdbRustSmoke =
pkgs.runCommand "beastdb-rust-smoke" {
nativeBuildInputs = [beastdbRust beastdbLib beastdbWrapped pkgs.coreutils];
} ''
set -euo pipefail
work=$(mktemp -d)
echo "=== beastdb-rust-smoke ===" | tee $out
beastdb-rust-smoke "$work" | tee -a $out
grep -q "mode_a immediate put+commit ok" $out
grep -q "mode_b batch put+commit ok" $out
grep -q "mode_b flush_policy put+commit ok" $out
grep -q "mode_b time_flush_policy put+commit ok" $out
grep -q "get_imm=imm-value" $out
grep -q "get_batch_a=1 get_batch_b=2" $out
grep -q "get_policy_a=1 get_policy_b=2 get_policy_c=3" $out
grep -q "missing_key=none" $out
grep -q "contains_present=true" $out
grep -q "contains_missing=false" $out
grep -q "delete_get_missing=none" $out
grep -q "delete_contains_missing=false" $out
grep -q "iter_live_pairs=" $out
grep -q "prefix_iter_pairs=2" $out
grep -q "rev_iter_pairs=" $out
grep -q "first_last_ok=1" $out
grep -q "range_pairs=" $out
grep -q "acid_commit_a=1" $out
grep -q "acid_abort_a_kept=1" $out
grep -q "acid_abort_ghost_missing=none" $out
grep -q "get_bytes_ok" $out
grep -q "clear_len_ok=1" $out
grep -q "corrupt_open_fail_closed" $out
grep -q "beastdb-rust-smoke durable immediate+batch put/get roundtrip ok" $out
test -f ${beastdbRust}/lib/libbeastdb.rlib
echo "beastdb-rust-smoke complete" | tee -a $out
'';
beastdbRustT0 =
pkgs.runCommand "beastdb-rust-t0" {
nativeBuildInputs = [beastdbRust beastdbLib beastdbWrapped pkgs.coreutils];
} ''
set -euo pipefail
echo "=== beastdb-rust-t0 ===" | tee $out
beastdb-rust-t0 --test-threads=1 2>&1 | tee -a $out
grep -q "test result: ok" $out
grep -q "8 passed" $out
echo "beastdb-rust-t0 complete" | tee -a $out
'';
beastdbRustT1 =
pkgs.runCommand "beastdb-rust-t1" {
nativeBuildInputs = [beastdbRust beastdbLib beastdbWrapped pkgs.coreutils];
} ''
set -euo pipefail
echo "=== beastdb-rust-t1 ===" | tee $out
beastdb-rust-t1 --test-threads=1 2>&1 | tee -a $out
grep -q "test result: ok" $out
grep -q "26 passed" $out
echo "beastdb-rust-t1 complete" | tee -a $out
'';
beastdbRustT2 =
pkgs.runCommand "beastdb-rust-t2" {
nativeBuildInputs = [beastdbRust beastdbLib beastdbWrapped pkgs.coreutils];
} ''
set -euo pipefail
echo "=== beastdb-rust-t2 ===" | tee $out
beastdb-rust-t2 --test-threads=1 2>&1 | tee -a $out
grep -q "test result: ok" $out
grep -q "8 passed" $out
echo "beastdb-rust-t2 complete" | tee -a $out
'';
ffiSmoke =
pkgs.runCommand "beastdb-ffi-smoke" {
nativeBuildInputs = [
beastdbFfiC
beastdbFfiCpp
beastdbFfiRust
beastdbLib
beastdbWrapped
pkgs.coreutils
pkgs.stdenv.cc
];
} ''
set -euo pipefail
work=$(mktemp -d)
mkdir -p "$work/c" "$work/cpp" "$work/rust" "$work/corrupt"
echo "=== FFI C ===" | tee $out
beastdb-ffi-c "$work/c" | tee -a $out
grep -q "get=ffi-c-value" $out
grep -q "beastdb-ffi-c durable put/get roundtrip ok" $out
echo "=== FFI C++ ===" | tee -a $out
beastdb-ffi-cpp "$work/cpp" | tee -a $out
grep -q "get=ffi-cpp-value" $out
grep -q "beastdb-ffi-cpp durable put/get roundtrip ok" $out
echo "=== FFI Rust ===" | tee -a $out
beastdb-ffi-rust "$work/rust" | tee -a $out
grep -q "get=ffi-rust-value" $out
grep -q "beastdb-ffi-rust durable put/get roundtrip ok" $out
echo "=== FFI fail-closed corrupt ===" | tee -a $out
beastdb init "$work/corrupt"
beastdb put "$work/corrupt" k v
beastdb sync "$work/corrupt"
printf 'torn' > "$work/corrupt/MANIFEST"
cat > "$work/probe.c" <<'CEOF'
#include "beastdb.h"
#include <stdio.h>
int main(int argc, char **argv) {
beastdb_env *e = 0;
int rc = beastdb_env_open(&e, argv[1], 0);
if (rc == 0) { printf("unexpected ok\n"); return 1; }
printf("ffi-open-corrupt rc=%d\n", rc);
return 0;
}
CEOF
$CC -O2 -I${beastdbLib}/include "$work/probe.c" \
-L${beastdbLib}/lib -lbeastdb -Wl,-rpath,${beastdbLib}/lib \
-o "$work/probe"
"$work/probe" "$work/corrupt" | tee -a $out
grep -q "ffi-open-corrupt rc=" $out
echo "=== FFI value fidelity (newline + large) ===" | tee -a $out