From cfaedfc4d69859cc659709848feb5891340367c6 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Fri, 27 Nov 2026 00:09:10 +0200 Subject: [PATCH] docs: state the scheduler's pipeline and make the doc export incremental The scheduler page now records the design decided in conversation: an 18-thread cap (75% of cores) chosen for the machine rather than the frame, a four-stage pipeline (input and geometry, tiles 1 for the younger frame, tiles 2 for the older frame's tail, blit), the ordering rules, and what is deliberately excluded. Its "implemented today" section still says the budget is the only code, so the page does not claim more than exists. Tools/Update web site re-exports only pages whose .org (or first-level INCLUDE target) changed, keyed by sha256 in a gitignored .org-export-state, finds Module/*/Documentation as well as Documentation, and refuses to upload when an export failed. --- .gitignore | 4 + Modules/scheduler/Documentation/index.org | 148 ++++++++++++++++++---- Tools/Update web site | 104 +++++++++++---- 3 files changed, 209 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index 0aae896..8cb045d 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,10 @@ build/ .vscode/ .openidea/ +# documentation export cache (Tools/Update web site) +.org-export-state +.org-export-state.lock + # TLC scratch Modules/scheduler/Model/states/ Modules/scheduler/Model/*_TTrace_* diff --git a/Modules/scheduler/Documentation/index.org b/Modules/scheduler/Documentation/index.org index d222045..6c85ad4 100644 --- a/Modules/scheduler/Documentation/index.org +++ b/Modules/scheduler/Documentation/index.org @@ -15,50 +15,136 @@ :CUSTOM_ID: what-it-owns :END: -The scheduler is the only component that knows how many worker threads -the process may use, and the only one that decides which queued work -runs next. +Two things and nothing else: the number of worker threads this process may use, +and the threads themselves. It creates them once, parks them between frames, +and hands them work. + +It knows nothing about what the work is - no triangles, no tiles, no light, no +camera. It knows threads, and the order in which queued items run. It is the Java module =eu.svjatoslav.aukio.sched=, declared in =src/main/java/module-info.java=, and it exports exactly one package. Any other module that wants it must name it in its own descriptor, so the dependency is visible in the language, not only in the build file. -* The budget +* The budget is a cap, not a target :PROPERTIES: :CUSTOM_ID: the-budget :END: -- The default is 75% of available CPU cores, rounded down and never - below 1. -- The remainder is not waste: JVM threads (GC, JIT, compiler, - finalizers) and the OS need to run, and a pool that claims every - core starves itself exactly when it matters. -- It is a policy default, not a property of any machine. Any caller - can override it per scheduler with =setThreadCount(int)=. +- The default is 75% of available CPU cores, rounded down and never below 1. + On the development machine that is 18 threads out of 24. +- The cap exists for the machine, not for the frame. This application may not + take every core, because the same machine runs a desktop session, an editor + and a browser, and a compute-bound process that saturates all cores degrades + all of them. +- It is a throttle, not a fence. Eighteen busy threads are still scheduled + across all twenty-four cores, so they still compete for cache and memory + bandwidth. A hard fence would be =taskset= or a cgroup, and that is + deliberately not this module's job. +- It is a policy default, not a property of any machine. Any caller can + override it per scheduler with =setThreadCount(int)=. + +Two numbers justify the cap instead of a wider one. Measured on the +development machine with the same work at each width: 12 threads to 24 bought +about 11%, and 8 threads already reached 85% of the 24-thread figure. Thread +count is a weak lever on a memory-bound rasteriser, so the width is chosen for +the machine's comfort rather than squeezed for speed. + +* The frame loop +:PROPERTIES: +:CUSTOM_ID: the-pipeline +:END: -* Implemented today +The application runs a pipeline of four stages, not one frame start to finish: + +| Stage | Runs on | Purpose | +|------------------+-------------------------+---------------------------------------------| +| input, geometry | the loop thread | read the input, write node transforms, | +| | | settle the camera, emit world-space | +| | | triangles for one frame | +| tiles 1 | the pool | draw the pixels of the younger frame, the | +| | | one the geometry stage has just handed over | +| tiles 2 | the same pool | draw the pixels of the older frame, which | +| | | is down to its last few tiles | +| blit | the event dispatch | put the finished image on screen | +| | thread | | + +The two tile stages are not two copies of one job. They exist so the pool is +never idle: + +- Tiles 1 is the younger frame, fresh from the geometry stage, with all of its + tiles still to draw. +- Tiles 2 is the older frame, down to the thin tail of the picture - a handful + of tiles whose shape did not fit the others - and a tail cannot fill the + machine. +- So the workers serve the tail of the older frame first and take the younger + frame's tiles as the tail runs out, instead of going idle until the next + frame begins. +- Blit follows each of them in turn: a frame goes to the screen once every one + of its tiles is written, while the other frame keeps drawing. + +Why each rule: + +- Input is read in the first stage. A key, a pointer delta or a 3D device + deflection belongs to exactly one frame and cannot be counted twice. +- Geometry is settled before any tile is drawn. A tile must never read a pose + or a transform that is still moving, which is why the geometry stage is its + own stage rather than the first thing a tile worker happens to do. +- Two tile stages mean two frames of tiles in memory, so a frame keeps drawing + while the other is being finished and shown. +- The blit is a reference swap on the event dispatch thread. It overlaps the + next frame's tiles for free and occupies no worker. +- Memory is not the constraint. Two tile frames plus one geometry output are + cheap against this machine's memory, and the pipeline is worth more in frame + rate than those buffers cost. + +Every stage that runs in parallel draws on the same pool. There is exactly one +pool in the process, it is created once, and its width is the budget above. + +* Ordering rules :PROPERTIES: -:CUSTOM_ID: implemented-today +:CUSTOM_ID: ordering-rules :END: -=-= Scheduler holds the budget only. +- Frame work before background work. A background item is taken only when no + frame item is queued, which is what keeps an optional, minutes-long job such + as a light bake from delaying a frame. +- No pre-emption. An item that has started runs to completion; parallelism is + decided when an item is picked up, never while it runs. +- The frame is complete before it is shown. A half-filled image is never + visible to another thread. -| Member | Meaning | -|--------------------+------------------------------------------------| -| =Scheduler()= | Detects the cores, takes 75% of them. | -| =threadCount()= | The current budget, in worker threads. | -| =setThreadCount()= | Replaces the budget; refuses anything below 1. | +These rules are modelled as a state machine before they exist in code - see the +model section below. + +* Deliberately not here +:PROPERTIES: +:CUSTOM_ID: not-here +:END: + +- Deadlines, admission control and a quality ladder (render a smaller image, + drop leftover tiles, lower the target frame rate). They earn their place only + when frame work and background work actually compete for the pool, and the + first measurement decides their shape. Before that they are machinery with no + consumer. +- Thread pinning. The operating system places threads, and on a machine whose + cores are not equal it does that better than a fixed assignment. +- Per-subsystem thread counts. A second place deciding how much CPU the process + may use is the defect this module exists to prevent. +- Input device threads. A thread blocked on =read()= of a character device + spends no CPU budget, and scheduling it would let a frame starve the device + that steers it, so devices keep their own threads. * The model :PROPERTIES: :CUSTOM_ID: the-model :END: -=Model/FrameGate.tla= models the scheduler's two ordering rules as a state -machine, before either exists in code: a background item is taken only when no -frame item is queued, and the frame gate fires once, only after every frame -item has finished. +=Model/FrameGate.tla= models the ordering rules as a state machine: a +background item is taken only when no frame item is queued, the frame gate +fires once and only after every frame item has finished, and nothing is +pre-empted. | Config | What it shows | |-------------------------------+--------------------------------------------------| @@ -75,4 +161,20 @@ Modules/scheduler/Model/check-models.sh # gate passes, both probes fail as nam ./gradlew -Ptla :scheduler:check # the gate alone, wired into the build #+end_src +* Implemented today +:PROPERTIES: +:CUSTOM_ID: implemented-today +:END: + +=-= The budget only. The pool, the queues and the pipeline above are the design +this page states; they are not code yet. + +| Member | Meaning | +|--------------------+------------------------------------------------| +| =Scheduler()= | Detects the cores, takes 75% of them. | +| =threadCount()= | The current budget, in worker threads. | +| =setThreadCount()= | Replaces the budget; refuses anything below 1. | +The pool is the first piece to be built, because parallel tiles need it and +nothing else can supply it. Its width and its wake-up cost are to be measured +against the frame time at a fixed pose before the tile stage is called done. diff --git a/Tools/Update web site b/Tools/Update web site index 028c6e4..3e3c3c4 100755 --- a/Tools/Update web site +++ b/Tools/Update web site @@ -3,32 +3,77 @@ cd "${0%/*}"; if [ "$1" != "T" ]; then gnome-terminal -e "'$0' T"; exit; fi; cd .. +# Incremental org -> html export. A page is re-exported only when its .org file +# (or a first-level #+INCLUDE target) changed, or when its .html is missing. +# The cache is .org-export-state in the repo root, gitignored. +# +# One root per Documentation directory: "find" does not descend into symlinked +# directories, so a single-root walk would silently miss Modules/*/Documentation. +# A new module's Documentation directory is picked up automatically. + export_org_to_html() { local org_file=$1 local dir=$(dirname "$org_file") local base=$(basename "$org_file" .org) + local html_file="$dir/$base.html" + + local fingerprint + fingerprint=$( + { + sha256sum "$org_file" | awk '{print $1}' + grep -oE '^#\+INCLUDE:[[:space:]]+"[^"]+"' "$org_file" 2>/dev/null \ + | sed -E 's/^#\+INCLUDE:[[:space:]]+"([^"]+)"/\1/' \ + | while IFS= read -r include; do + if [ -f "$dir/$include" ]; then + sha256sum "$dir/$include" | awk '{print $1}' + else + echo "MISSING:$include" + fi + done + } | sha256sum | awk '{print $1}' + ) + + local recorded="" + [ -f .org-export-state ] && \ + recorded=$(awk -F'\t' -v f="$org_file" '$1 == "file" && $3 == f {print $2}' .org-export-state) + + if [ -f "$html_file" ] && [ -n "$recorded" ] && [ "$recorded" = "$fingerprint" ]; then + return 2 # unchanged + fi + + if [ -f "$html_file" ]; then + rm -f "$html_file" + fi + echo "Exporting: $org_file → $html_file" ( cd "$dir" || return 1 - local html_file="${base}.html" - if [ -f "$html_file" ]; then - rm -f "$html_file" - fi - echo "Exporting: $org_file → $dir/$html_file" emacs --batch -l ~/.emacs --visit="${base}.org" --funcall=org-html-export-to-html --kill - if [ $? -eq 0 ]; then - echo "✓ Successfully exported $org_file" - else - echo "✗ Failed to export $org_file" - return 1 - fi ) + if [ $? -ne 0 ] || [ ! -f "$html_file" ]; then + echo "✗ Failed to export $org_file" + return 1 + fi + + # record the successful export under the file lock the state rewrite takes + { + flock 9 + grep -v -F "$(printf 'file\t')" .org-export-state 2>/dev/null > .org-export-state.tmp + printf 'file\t%s\t%s\n' "$fingerprint" "$org_file" >> .org-export-state.tmp + sort -o .org-export-state.tmp .org-export-state.tmp + mv .org-export-state.tmp .org-export-state + } 9>>.org-export-state.lock + + echo "✓ Successfully exported $org_file" + return 0 } export_org_files_to_html() { echo "🔍 Searching for .org files in Documentation/ ..." echo "=======================================" - mapfile -t ORG_FILES < <(find Documentation -type f -name "*.org" | sort) + mapfile -t ORG_FILES < <( + find Documentation Modules -path '*/build' -prune -o -type f -name "*.org" -print | sort + ) if [ ${#ORG_FILES[@]} -eq 0 ]; then echo "❌ No .org files found!" @@ -39,27 +84,38 @@ export_org_files_to_html() { printf '%s\n' "${ORG_FILES[@]}" echo "=======================================" - SUCCESS_COUNT=0 - FAILED_COUNT=0 + local SUCCESS_COUNT=0 + local SKIPPED_COUNT=0 + local FAILED_COUNT=0 + local org_file result for org_file in "${ORG_FILES[@]}"; do export_org_to_html "$org_file" - if [ $? -eq 0 ]; then - ((SUCCESS_COUNT++)) - else - ((FAILED_COUNT++)) - fi + result=$? + case $result in + 0) ((SUCCESS_COUNT++)) ;; + 2) ((SKIPPED_COUNT++)) ;; + *) ((FAILED_COUNT++)) ;; + esac done echo "=======================================" echo "📊 SUMMARY:" - echo " ✓ Successful: $SUCCESS_COUNT" - echo " ✗ Failed: $FAILED_COUNT" - echo " Total: $((SUCCESS_COUNT + FAILED_COUNT))" + echo " ✓ Exported: $SUCCESS_COUNT" + echo " · Unchanged: $SKIPPED_COUNT" + echo " ✗ Failed: $FAILED_COUNT" + echo " Total: $((SUCCESS_COUNT + SKIPPED_COUNT + FAILED_COUNT))" echo "" + + [ $FAILED_COUNT -eq 0 ] } -export_org_files_to_html +if ! export_org_files_to_html; then + echo "✗ Export failed - not uploading." + echo "Press ENTER to close this window." + read + exit 1 +fi echo "📤 Uploading to server..." rsync -avz --delete -e 'ssh -p 10006' Documentation/ \ @@ -73,4 +129,4 @@ fi echo "" echo "Press ENTER to close this window." -read \ No newline at end of file +read -- 2.20.1