OILS / regtest / aports-run.sh View on Github | oils.pub

750 lines, 347 significant
1#!/usr/bin/env bash
2#
3# Build Alpine Linux packages: baseline, OSH as /bin/sh, OSH as /bin/bash
4# See regtest/aports.md
5#
6# Usage:
7# regtest/aports-run.sh <function name>
8#
9# Common usage:
10#
11# export APORTS_EPOCH=2025-08-04-foo # optional override
12# $0 build-many-shards-overlayfs shard{0..16} # build all 17 shards in 2 configs
13#
14# Also useful:
15#
16# $0 fetch-packages fetch $pkg_filter $a_repo # alpine repo is 'main' or 'community'
17#
18# $0 fetch-packages fetch 100,300p # packages 100-300
19# $0 fetch-packages fetch '.*' # all packages
20#
21# Look for results in _tmp/aports-build/
22#
23# Build many packages:
24#
25# $0 build-packages-overlayfs osh-as-sh shard9 community
26# $0 build-packages-overlayfs osh-as-sh shardA # main is default $a_repo
27#
28# Build a single package:
29#
30# $0 build-package-overlayfs osh-as-sh userspace-rcu
31# $0 build-package-overlayfs osh-as-sh xterm community # community repo
32#
33# Drop into a shell:
34# INTERACTIVE=1 $0 build-package-overlayfs osh-as-sh userspace-rcu
35#
36# PKG_FILTER
37# shard[0-9]+ - shard3 is packages 301 to 400
38# [0-9]+ - 42 means build the first 42 packages
39# [0-9]+,[0-9]+p - 100,300p packages 100 to 300 (sed syntax)
40# ALL - all packages
41# .* - egrep pattern matching all packages
42# curl - egrep pattern matching 'curl'
43#
44# Preview packages:
45#
46# $0 package-dirs shard9 community
47
48: ${LIB_OSH=stdlib/osh}
49source $LIB_OSH/bash-strict.sh
50source $LIB_OSH/task-five.sh
51
52source regtest/aports-common.sh
53
54#
55# Config
56#
57
58show-config() {
59 enter-rootfs sh -c '
60 ls -l /bin/sh /bin/ash /bin/bash
61 '
62}
63
64set-baseline() {
65 # ensure we have the default config
66 enter-rootfs sh -c '
67 set -x
68 ln -s -f /bin/busybox /bin/sh
69 ln -s -f /bin/busybox /bin/ash
70 cp /bin/bash.ORIG /bin/bash
71 '
72 show-config
73}
74
75set-osh-as-X() {
76 local x=$1
77
78 enter-rootfs sh -c '
79 x=$1
80 set -x
81 if ! test -f /usr/local/bin/oils-for-unix; then
82 echo "Build Oils first"
83 exit
84 fi
85 ln -s -f /usr/local/bin/oils-for-unix /bin/$x
86 ' dummy0 "$x"
87 show-config
88}
89
90set-osh-as-sh() {
91 set-osh-as-X sh
92}
93
94set-osh-as-ash() {
95 set-osh-as-X ash
96}
97
98set-osh-as-bash() {
99 set-osh-as-X bash
100}
101
102#
103# Run
104#
105
106package-dirs() {
107 # lz gives 5 packages: some fail at baseline
108 # lzip: a single fast package
109 # mpfr4: OSH bug, and big log
110 # yash: make sure it doesn't hang
111 local package_filter=${1:-'lz|mpfr|yash'}
112 local a_repo=${2:-main} # or 'community'
113
114 local -a prefix
115
116 if [[ $package_filter = 'ALL' ]]; then
117 prefix=( cat )
118
119 # 100 means 0 to 100
120 elif [[ $package_filter =~ ^[0-9]+$ ]]; then
121 prefix=( head -n $package_filter )
122
123 # 100,300p means lines 100 to 300
124 elif [[ $package_filter =~ ^[0-9]+,[0-9]+p$ ]]; then
125 prefix=( sed -n $package_filter )
126
127 elif [[ $package_filter =~ ^shard([0-9]+)$ ]]; then
128 # shards of 100 packages
129
130 local shard_num=${BASH_REMATCH[1]}
131 #echo shard=$shard_num
132
133 local range
134 # shard 0 is 0-99
135 # shard 9 is 900 to 999
136 # shard 10 is 1000 to 1099
137 case $shard_num in
138 # sed doesn't like 000,099
139 0) range='1,100p' ;;
140 *) range="${shard_num}01,$(( shard_num + 1))00p" ;;
141 esac
142
143 prefix=( sed -n "$range" )
144
145 # shardA, shardB For testing the combined report
146 elif [[ $package_filter =~ ^shard([A-Z]+)$ ]]; then
147 local shard_name=${BASH_REMATCH[1]}
148 case $a_repo in
149 main)
150 case $shard_name in
151 A) package_filter='^gzip' ;; # failure
152 B) package_filter='^xz' ;; # failure
153 C) package_filter='^lz' ;; # 3 packages
154 D) package_filter='^jq$' ;; # produces autotools test-suite.log
155 E) package_filter='^py3-p' ;; # many packages in parallel
156 F) package_filter='^py3-pathspec' ;; # very fast package
157 P) package_filter='^xz$|^shorewall' ;; # patches
158 *) package_filter='^perl-http-daemon' ;; # test out perl
159 esac
160 ;;
161 community)
162 case $shard_name in
163 A) package_filter='^py3-zulip' ;; # one Python package
164 B) package_filter='^xterm' ;; # one C package
165 C) package_filter='^shfmt' ;; # one Go package
166 D) package_filter='^shellspec' ;; # OSH disagreement because of 'var'
167 *) package_filter='^shell' ;; # a bunch of packages
168 esac
169 ;;
170 *)
171 die "Invalid a_repo $a_repo"
172 ;;
173 esac
174
175 prefix=( egrep "$package_filter" )
176
177 elif [[ $package_filter =~ ^disagree-(.*)+$ ]]; then
178 local filename=${BASH_REMATCH[1]}
179 # A file of EXACT package names, not patterns
180 # See copy-disagree
181 local package_file="_tmp/$package_filter.txt"
182 comm -1 -2 <(sort $package_file) <(sort _tmp/apk-${a_repo}-manifest.txt)
183 return
184
185 else
186 prefix=( egrep "$package_filter" )
187
188 fi
189
190 "${prefix[@]}" _tmp/apk-${a_repo}-manifest.txt
191}
192
193copy-disagree() {
194 ### Determine what to run
195
196 local epoch=${1:-2025-09-18-bash}
197 cp -v \
198 _tmp/aports-report/$epoch/disagree-packages.txt \
199 _tmp/disagree-$epoch.txt
200}
201
202do-packages() {
203 ### Download sources - abuild puts it in /var/cahe/distfiles
204 local action=${1:-fetch}
205 local package_filter=${2:-}
206 local a_repo=${3:-main}
207 # flags to pass to the inner shell
208 local sh_flags=${4:-'-e -u'} # -u to disable -e
209
210 # 6 seconds for 10 packages
211 # There are ~1600 packages
212 # So if there are 20 shards, each shard could have 10?
213
214 local -a package_dirs
215 package_dirs=( $(package-dirs "$package_filter" "$a_repo") )
216
217 echo "${dirs[@]}"
218 #return
219
220 time enter-rootfs-user sh $sh_flags -c '
221
222 action=$1
223 a_repo=$2
224 shift 2
225 for dir in "$@"; do
226 time abuild -r -C aports/$a_repo/$dir "$action"
227 done
228 ' dummy0 "$action" "$a_repo" "${package_dirs[@]}"
229}
230
231fetch-packages() {
232 local package_filter=${1:-}
233 local a_repo=${2:-main}
234
235 # -u means we don't pass -e (and it's non-empty)
236 do-packages fetch "$package_filter" "$a_repo" '-u'
237}
238
239banner() {
240 echo
241 echo "=== $@"
242 echo
243}
244
245build-package-overlayfs() {
246 local config=${1:-baseline}
247 local pkg=${2:-lua5.4}
248 local a_repo=${3:-main}
249
250 # baseline stack:
251 # _chroot/aports-build
252 # _chroot/package-upper/baseline/gzip # upper dir / layer dir
253 #
254 # osh-as-sh stack:
255 # _chroot/aports-build
256 # _chroot/osh-as-sh.overlay/layer # this has the symlink
257 # _chroot/package-upper/osh-as-sh/gzip # upper dir / layer dir
258
259 # allow concurrency
260 local xargs_slot="${XARGS_SLOT:-99}"
261 local ov_base_dir=_chroot/package-slot${xargs_slot}.overlay
262
263 local merged=$ov_base_dir/merged
264 local work=$ov_base_dir/work
265
266 local layer_dir=_chroot/package-layers/$config/$pkg
267 mkdir -p $merged $work $layer_dir
268
269 local overlay_opts
270 case $config in
271 baseline)
272 overlay_opts="lowerdir=$CHROOT_DIR,upperdir=$layer_dir,workdir=$work"
273 ;;
274 osh-as-sh)
275 local osh_as_sh=_chroot/osh-as-sh.overlay/layer
276 overlay_opts="lowerdir=$osh_as_sh:$CHROOT_DIR,upperdir=$layer_dir,workdir=$work"
277 ;;
278 *)
279 die "Invalid config $config"
280 ;;
281 esac
282
283 sudo mount \
284 -t overlay \
285 aports-package \
286 -o "$overlay_opts" \
287 $merged
288
289 local -a prefix
290 if test -n "${XARGS_SLOT:-}"; then
291 local x=$XARGS_SLOT
292
293 # run slot 0 on cores 0 and 1
294 # run slot 9 on cores 18 and 19
295 local cores="$(( x*2 )),$(( x*2 + 1 ))"
296
297 # oversubscribe
298 # run slot 0 on cores 0 and 1
299 # run slot 19 on cores 19 and 0
300 #local cores="$(( x )),$(( (x + 1) % NUM_CORES ))"
301 prefix=( taskset -c "$cores" )
302 fi
303
304 "${prefix[@]}" $merged/enter-chroot -u udu sh -c '
305 cd oils
306
307 # show the effect of the overlay
308 #ls -l /bin/sh
309
310 regtest/aports-guest.sh build-one-package "$@"
311 ' dummy0 "$pkg" "$a_repo" "$xargs_slot"
312
313 if test -n "$INTERACTIVE"; then
314 echo "Starting interactive shell in overlayfs environment for package $a_repo/$pkg"
315 echo "Rebuild: abuild -f -r -C ~/aports/$a_repo/$pkg -k -K"
316 echo " Help: abuild -h"
317 # If the last command in the child shell exited non-zero then ctrl-d/exit
318 # will report that error code to the parent. If we don't ignore that error
319 # we will exit early and leave the package overlay mounted.
320 set +o errexit
321 $merged/enter-chroot -u udu
322 set -o errexit
323 fi
324
325 unmount-loop $merged
326}
327
328build-pkg() {
329 ### trivial wrapper around build-package-overlayfs - change arg order for xargs
330 local config=${1:-baseline}
331 local a_repo=${2:-main}
332 local pkg=${3:-lua5.4}
333
334 build-package-overlayfs "$config" "$pkg" "$a_repo"
335
336 # TODO:
337 # - we should only do this after we've done BOTH configs, so it appears
338 # atomically
339 save-package-files $config $a_repo $pkg
340
341 # TODO: blow away the layer dir, since we saved the "tombstone".
342 # We're not doing this now because we're still reporting off DEPRECATED shard
343 # files.
344}
345
346LOG_SIZE_THRESHOLD=$(( 500 * 1000 )) # 500 KB
347#LOG_SIZE_THRESHOLD=$(( 1 * 1000 ))
348
349abridge-one-log() {
350 local src=$1
351 local dest=$2
352
353 local size
354 size=$(stat --format '%s' $src)
355 if test $size -lt $LOG_SIZE_THRESHOLD; then
356 #cp --verbose $src $dest
357 cp $src $dest
358 else
359 # Bug fix: abridging to 1000 lines isn't sufficient. We got some logs
360 # that were hundreds of MB, with less than 1000 lines!
361 { echo "*** This log is abridged to its last $LOG_SIZE_THRESHOLD bytes"
362 echo
363 tail --bytes $LOG_SIZE_THRESHOLD $src
364 } > $dest
365 fi
366}
367
368# save-package-files creates a tree we can rsync
369# For EACH PACKAGE, without shards
370#
371# TODO: Both baseline and osh-as-sh should appear atomically?
372
373# Source tree:
374#
375# _chroot/package-layers/
376# baseline/
377# jq/
378# home/udu/
379# oils/_tmp/aports-guest/
380# jq.log.txt
381# jq.task.tsv
382# packages/main/x86_64
383# jq-*.apk
384# aports/main/jq/
385# src/jq-1.8.0/test-suite.log
386#
387# _tmp/aports-build/
388# 2025-11-12/
389# shard0/ # TODO: remove shards
390# baseline/
391# NEW:
392# apk/
393# jq.apk.txt # md5sum
394# layer/
395# jq.tombstone.txt # find '%s %P\n'
396# task/
397# jq.task.tsv
398# EXISTING:
399# apk.txt
400# tasks.tsv
401# log/
402# jq.log.txt
403# test-suite/
404# jq/ TODO: support multiple logs
405# test-suite.log
406
407save-package-files() {
408 ### Copy some files from _chroot/package-layers/ -> _tmp/aports-build
409
410 local config=${1:-baseline}
411 local a_repo=${2:-main}
412 local pkg=${3:-jq}
413
414 local layer_dir=_chroot/package-layers/$config/$pkg
415 local dest_dir=$BASE_DIR/$APORTS_EPOCH/$config
416
417 # 5 directories
418 mkdir -p $dest_dir/{apk,layer,task,log,test-suite}
419
420 cp \
421 $layer_dir/home/udu/oils/_tmp/aports-guest/$pkg.task.tsv \
422 $dest_dir/task
423
424 abridge-one-log \
425 $layer_dir/home/udu/oils/_tmp/aports-guest/$pkg.log.txt \
426 $dest_dir/log/$pkg.log.txt
427
428 # Abridge this log too
429 { find $layer_dir/home/udu/aports/$a_repo/$pkg -name 'test-suite.log' 2> /dev/null || true; } |
430 while read -r log_src; do
431 local test_suite_dest_dir=$dest_dir/test-suite/$pkg
432 mkdir -p $test_suite_dest_dir
433 abridge-one-log \
434 $log_src \
435 $test_suite_dest_dir/test-suite.log.txt
436 done
437
438 md5sum $layer_dir/home/udu/packages/$a_repo/x86_64/*.apk \
439 > $dest_dir/apk/$pkg.apk.txt 2> /dev/null || true # allow failure if nothing built
440
441 # Truncate large listings - e.g. clang packages have over 120K files
442 { find $layer_dir -printf '%s %P\n' 2> /dev/null || true; } |
443 head -n 1000 > $dest_dir/layer/$pkg.tombstone.txt
444
445 #tree $dest_dir
446
447 # log.txt
448 # log.txt
449}
450
451NUM_CORES=$(( $(nproc) ))
452
453# 2 cores per package build
454NUM_PAR=$(( NUM_CORES / 2 ))
455
456# over-subscribe - allow 20 processes to see 2 cores each
457# Note: this causes more timeouts. TODO: get rid of shards to get rid of
458# stragglers, and then raise the timeout to 20 minutes or more.
459# NUM_PAR=$(( NUM_CORES ))
460
461# TODO: we ran into the env.sh race condition in the enter-chroot script
462# generated by alpine-chroot-install
463build-many-packages-overlayfs() {
464 local package_filter=${1:-}
465 local config=${2:-baseline}
466 local a_repo=${3:-main}
467 local parallel=${4:-T}
468
469 banner "Building packages (filter=$package_filter a_repo=$a_repo)"
470
471 local -a flags
472 if test -n "$parallel"; then
473 log "(with $NUM_PAR jobs in parallel)"
474 flags=( -P $NUM_PAR )
475 else
476 log '(serially)'
477 fi
478
479 package-dirs "$package_filter" $a_repo |
480 xargs "${flags[@]}" -n 1 --process-slot-var=XARGS_SLOT -- \
481 $0 build-pkg $config $a_repo
482}
483
484
485clean-host-and-guest() {
486 # host dir _tmp/aports-build
487 rm -r -f -v $BASE_DIR
488}
489
490clean-guest() {
491 # clean guest chroot
492 sudo rm -r -f -v $CHROOT_HOME_DIR/oils/_tmp
493}
494
495readonly -a CONFIGS=( baseline osh-as-sh )
496
497APORTS_EPOCH="${APORTS_EPOCH:-}"
498# default epoch
499if test -z "$APORTS_EPOCH"; then
500 APORTS_EPOCH=$(date '+%Y-%m-%d')
501fi
502
503_build-many-configs-overlayfs() {
504 local package_filter=${1:-}
505 local epoch=${2:-$APORTS_EPOCH}
506 local a_repo=${3:-main}
507
508 if test -z "$package_filter"; then
509 die "Package filter is required (e.g. shard3, ALL)"
510 fi
511
512 clean-guest
513
514 # See note about /etc/sudoers.d at top of file
515
516 local dest_dir="$BASE_DIR/$epoch/$package_filter" # e.g. shard10
517
518 for config in "${CONFIGS[@]}"; do
519 banner "$epoch: Using config $config"
520
521 build-many-packages-overlayfs "$package_filter" "$config" "$a_repo"
522 done
523}
524
525remove-shard-files() {
526 local shard_dir=${1:-_chroot/shardC}
527
528 # For all packages packages, for baseline and osh-as-sh, clean up the aports source dir
529 # For linux, clang, etc. it becomes MANY GIGABYTES
530 #
531 # 2025-09-12: ignore errors from rm; I think there was a race condition -
532 # processes could still be running and creating files
533 #
534 # rm: cannot remove '_chroot/shard6/baseline/llvm19/home/udu/aports/main/llvm19/src/llvm-project-19.1.7.src/build/lib': Directory not empty
535 # real 1041m46.464s
536
537 #log "Removing big files in shard $shard_dir"
538 #sudo rm -r -f $shard_dir/*/*/home/udu/aports/ || true
539
540 log "Removing all files in $shard_dir"
541 sudo rm -r -f $shard_dir || true
542}
543
544build-many-shards-overlayfs() {
545 sudo -k
546
547 local a_repo=${A_REPO:-main} # env var like $APORTS_EPOCH
548
549 # Clean up old runs
550 sudo rm -r -f _chroot/package-layers _chroot/shard* _chroot/disagree*
551
552 banner "$APORTS_EPOCH $a_repo: building shards: $*"
553
554 time for shard_name in "$@"; do
555 _build-many-configs-overlayfs "$shard_name" "$APORTS_EPOCH" "$a_repo"
556
557 # Move layer files to _chroot/shard10/{baseline,osh}/...
558 mv -v --no-target-directory _chroot/package-layers _chroot/$shard_name
559
560 # Make it rsync-able in _tmp/aports-build ($BASE_DIR)
561 make-shard-tree $shard_name $a_repo
562
563 # Remove big files
564 remove-shard-files _chroot/$shard_name
565
566 # TODO: we should publish and clean up after every PACKAGE, rather than
567 # each shard
568 done
569}
570
571build-and-stat() {
572 # Measure resource utilization
573 local proc_dir="$BASE_DIR/$APORTS_EPOCH/proc-log"
574 mkdir -v -p $proc_dir
575 regtest/proc_log.py --out-dir $proc_dir --sleep-secs 5 &
576 local proc_log_pid=$!
577
578 sleep 0.05 # prevent overlapping sudo prompt
579
580 build-many-shards-overlayfs "$@"
581
582 kill -s TERM $proc_log_pid
583 wc -l $proc_dir/*.txt
584}
585
586make-shard-tree() {
587 ### Put outputs in rsync-able format, for a SINGLE shard
588
589 # The dir structure is like this:
590 #
591 # _tmp/aports-build/
592 # 2025-09-10-overlayfs/
593 # shard0/
594 # baseline/
595 # apk.txt
596 # tasks.tsv
597 # log/
598 # gzip.log.txt
599 # xz.log.txt
600 # test-suite/ # autotools dir
601 # gzip/
602 # test-suite.log.txt
603 # osh-as-sh/
604 # apk.txt
605 # tasks.tsv
606 # log/
607 # gzip.log.txt
608 # xz.log.txt
609 # test-suite/
610 # gzip/
611 # test-suite.log.txt
612 # shard1/
613 # ...
614 # shard16/
615 # ...
616
617 local shard_name=$1
618 local a_repo=${2:-main}
619 local epoch=${3:-$APORTS_EPOCH}
620
621 local shard_dir=_chroot/$shard_name
622
623 for config in baseline osh-as-sh; do
624 local dest_dir=$BASE_DIR/$epoch/$shard_name/$config
625 mkdir -p $dest_dir
626 #ls -l $shard_dir/$config
627
628 # Four outputs
629 # 1) log.txt for each package
630 # 2) Optional test-suite.txt for each package
631 # 3) merged tasks.tsv
632 # - comes from .task.tsv
633 # 4) merged apk.txt
634 #
635 # So 3 and 4 should not be merged yet
636 #
637 # _tmp/aports-build/
638 # 2025-11-12/
639 # shardP/
640 # baseline/
641 # log/
642 # test-suite/
643 # apk.txt
644 # tasks.tsv
645 #
646 # We want to
647
648 time python3 devtools/tsv_concat.py \
649 $shard_dir/$config/*/home/udu/oils/_tmp/aports-guest/*.task.tsv > $dest_dir/tasks.tsv
650
651 # Allowed to fail if zero .apk are built
652 time md5sum $shard_dir/$config/*/home/udu/packages/$a_repo/x86_64/*.apk \
653 > $dest_dir/apk.txt 2> /dev/null || true
654
655 abridge-logs $shard_dir/$config $dest_dir
656
657 done
658}
659
660abridge-logs() {
661 local config_src_dir=${1:-_chroot/shardD/osh-as-sh}
662 local dest_dir=${2:-$BASE_DIR/shardD/osh-as-sh}
663
664 local log_dest_dir=$dest_dir/log
665 local test_suite_dest_dir=$dest_dir/test-suite
666 mkdir -p $log_dest_dir $test_suite_dest_dir
667
668 local threshold=$(( 500 * 1000 )) # 500 KB
669
670 # this assumes the build process doesn't create *.log.txt
671 # test-suite.log is the name used by the autotools test runner - we want to save those too
672 # ignore permission errors with || true
673 { find $config_src_dir -name '*.log.txt' -a -printf '%s\t%P\n' 2> /dev/null || true; } |
674 while read -r size path; do
675 local src=$config_src_dir/$path
676 # Remove text until last slash (shortest match)
677 # like $(basename $path) but in bash, for speed
678 local filename=${path##*/}
679 local dest=$log_dest_dir/$filename
680
681 if test "$size" -lt "$threshold"; then
682 cp -v $src $dest
683 else
684 # Bug fix: abriding to 1000 lines isn't sufficient. We got some logs
685 # that were hundreds of MB, with less than 1000 lines!
686 { echo "*** This log is abridged to its last 500 KB:"
687 echo
688 tail --bytes 500000 $src
689 } > $dest
690 fi
691 done
692
693 { find $config_src_dir -name 'test-suite.log' -a -printf '%P\n' 2> /dev/null || true; } |
694 while read -r path; do
695 local src=$config_src_dir/$path
696
697 # Remove text after the first slash (shortest match)
698 local package_name=${path%%/*}
699 local dest=$test_suite_dest_dir/$package_name/test-suite.log.txt
700
701 mkdir -p "$(dirname $dest)"
702 cp -v --no-target-directory $src $dest
703 done
704
705 # 500K threshold: 76 MB
706 du --si -s $log_dest_dir
707}
708
709demo-build() {
710 local pkg=${1:-gzip} # in shardA, uses many cores
711 local do_pin=${2:-}
712
713 local -a prefix
714 if test -n "$do_pin"; then
715 echo "*** Pinning to CPU 0 ***"
716 prefix=( taskset -c 0 )
717 fi
718
719 "${prefix[@]}" $CHROOT_DIR/enter-chroot -u udu sh -c '
720 pkg=$1
721
722 echo "nproc = $(nproc)"
723
724 cd oils
725 set -x
726
727 # Note the user / real ratio! How many cores did we use?
728 time regtest/aports-guest.sh build-one-package $pkg
729 ' dummy0 $pkg
730}
731
732test-taskset() {
733 local pkg=${1:-gzip} # in shardA, uses many cores
734
735 demo-build $pkg ''
736 demo-build $pkg T
737}
738
739test-proc-log() {
740 local out_dir=_tmp/proc-log
741 mkdir -p $out_dir
742
743 regtest/proc_log.py --out-dir $out_dir &
744 local pid=$!
745 sleep 3.1 # should get 3 entries
746 kill $pid
747 wc -l $out_dir/*.txt
748}
749
750task-five "$@"