diff --git a/ansible/roles/developer-rust/defaults/main.yml b/ansible/roles/developer-rust/defaults/main.yml index 0c67b932..971ba48c 100644 --- a/ansible/roles/developer-rust/defaults/main.yml +++ b/ansible/roles/developer-rust/defaults/main.yml @@ -186,14 +186,25 @@ rust_governor_enabled: true # REFUSE the build outright rather than fall back to compiling. rust_governor_no_incremental: false +# Where Rust builds run: local, client (sent to hyperi-ai's remote.host), target +# (takes other hosts' builds) or auto (client when remote.host is set). +# `hyperi-build-slice raise ` lifts the slice for one case. +rust_build_role: auto + # Share of the host's RAM the whole build slice may use. High throttles and # reclaims, Max kills the build -- the build dies, never the host. Max is a # third of RAM so the desktop and its sessions keep the other two thirds. -# Raise both on a host dedicated to builds (a remote build target), e.g. 60 and -# 50, in its host or group vars. rust_governor_memory_high_pct: 27 rust_governor_memory_max_pct: 33 +# A client's build slice in MiB, sized for rust-analyzer. +rust_governor_client_memory_high_mb: 3584 +rust_governor_client_memory_max_mb: 4096 + +# A build target's shares of RAM. +rust_governor_target_memory_high_pct: 50 +rust_governor_target_memory_max_pct: 60 + # Smallest slice limits in MiB, so a small host can still build. Applied after # the percentage, and High never exceeds Max. rust_governor_memory_high_floor_mb: 3072 diff --git a/ansible/roles/developer-rust/files/hyperi-build-slice b/ansible/roles/developer-rust/files/hyperi-build-slice new file mode 100755 index 00000000..afea54d9 --- /dev/null +++ b/ansible/roles/developer-rust/files/hyperi-build-slice @@ -0,0 +1,171 @@ +#!/usr/bin/env python3 +"""Show or move the memory split between Rust builds and agent sessions, live. + + hyperi-build-slice show # both slices: use, High, Max, converged Max + hyperi-build-slice raise 8G # give rustbuild.slice 8 GiB until reset + hyperi-build-slice reset # back to the converged sizes + +rustbuild.slice and agents.slice share one RAM budget, sized at converge so +their maxima never add up to more than the host has. `raise` keeps that true: +whatever it gives the build slice it takes from agents.slice. It refuses when +that would leave agent sessions less than they use now plus 1 GiB. + +Changes are runtime only (`systemctl --user set-property --runtime`), so a +reboot or `reset` restores the converged sizes, and a converge rewrites them. +""" + +import argparse +import re +import shutil +import subprocess +import sys +from pathlib import Path + +BUILD = "rustbuild.slice" +AGENTS = "agents.slice" +UNIT_DIR = Path.home() / ".config" / "systemd" / "user" +MIB = 1024 * 1024 +GIB = 1024 * MIB +SYSTEMCTL = shutil.which("systemctl") or "/usr/bin/systemctl" +AGENTS_SPARE = GIB +AGENTS_FLOOR = 2 * GIB +BUILD_HIGH_RATIO = 7 / 8 +AGENTS_HIGH_RATIO = 9 / 10 +SIZE = re.compile(r"^(\d+(?:\.\d+)?)([KMGT]?)i?B?$", re.IGNORECASE) +UNITS = {"": 1, "K": 1024, "M": MIB, "G": GIB, "T": 1024 * GIB} + + +def parse_size(text: str) -> int: + """Return a size such as 8G, 6144M or 8GiB in bytes.""" + match = SIZE.match(text.strip()) + if match is None: + raise argparse.ArgumentTypeError(f"not a size: {text!r} (try 8G or 6144M)") + number, unit = match.groups() + return int(float(number) * UNITS[unit.upper()]) + + +def live(unit: str, prop: str) -> int | None: + """Return a slice property in bytes, or None when systemd reports no limit.""" + result = subprocess.run( # noqa: S603 - fixed argv, unit names are constants + [SYSTEMCTL, "--user", "show", unit, "-p", prop, "--value"], + capture_output=True, + check=True, + encoding="utf-8", + errors="replace", + text=True, + ) + value = result.stdout.strip() + return int(value) if value.isdigit() else None + + +def converged(unit: str, prop: str) -> int | None: + """Return the property the converged unit file sets, in bytes.""" + path = UNIT_DIR / unit + if not path.is_file(): + return None + for line in path.read_text(encoding="utf-8", errors="replace").splitlines(): + key, _, value = line.partition("=") + if key.strip() == prop: + return parse_size(value) + return None + + +def apply(unit: str, high: int, maximum: int) -> None: + """Set a slice's MemoryHigh and MemoryMax until reboot or reset.""" + subprocess.run( # noqa: S603 - fixed argv, unit names are constants + [ + SYSTEMCTL, + "--user", + "set-property", + "--runtime", + unit, + f"MemoryHigh={high // MIB}M", + f"MemoryMax={maximum // MIB}M", + ], + check=True, + ) + + +def gib(value: int | None) -> str: + """Format bytes as GiB for the table.""" + return "none" if value is None else f"{value / GIB:.1f}G" + + +def show() -> int: + """Print both slices' use and limits.""" + print(f"{'slice':16} {'used':>7} {'high':>7} {'max':>7} {'converged':>10}") + for unit in (BUILD, AGENTS): + print( + f"{unit:16} {gib(live(unit, 'MemoryCurrent')):>7} " + f"{gib(live(unit, 'MemoryHigh')):>7} {gib(live(unit, 'MemoryMax')):>7} " + f"{gib(converged(unit, 'MemoryMax')):>10}" + ) + return 0 + + +def raise_build(size: int) -> int: + """Give the build slice `size` and take the difference from the agents slice.""" + build_max = live(BUILD, "MemoryMax") + agents_max = live(AGENTS, "MemoryMax") + agents_used = live(AGENTS, "MemoryCurrent") or 0 + if build_max is None or agents_max is None: + print( + "hyperi-build-slice: both slices need a MemoryMax, " + "converge the developer-rust role first", + file=sys.stderr, + ) + return 1 + new_agents_max = agents_max - (size - build_max) + needed = max(agents_used + AGENTS_SPARE, AGENTS_FLOOR) + if new_agents_max < needed: + print( + f"hyperi-build-slice: {gib(size)} for builds leaves agent sessions " + f"{gib(new_agents_max)}, under the {gib(needed)} they need now. " + "Close a session or ask for less.", + file=sys.stderr, + ) + return 1 + apply(BUILD, int(size * BUILD_HIGH_RATIO), size) + apply(AGENTS, int(new_agents_max * AGENTS_HIGH_RATIO), new_agents_max) + return show() + + +def reset() -> int: + """Put both slices back to the sizes the converged unit files set.""" + for unit in (BUILD, AGENTS): + high = converged(unit, "MemoryHigh") + maximum = converged(unit, "MemoryMax") + if high is None or maximum is None: + print( + f"hyperi-build-slice: no converged limits in {UNIT_DIR / unit}", + file=sys.stderr, + ) + return 1 + apply(unit, high, maximum) + return show() + + +def main() -> int: + """Parse the command line and run the verb.""" + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + verbs = parser.add_subparsers(dest="verb", required=True) + verbs.add_parser("show", help="both slices' use and limits") + raise_parser = verbs.add_parser( + "raise", help="give the build slice SIZE until reset" + ) + raise_parser.add_argument( + "size", type=parse_size, help="the build slice's MemoryMax, e.g. 8G" + ) + verbs.add_parser("reset", help="back to the converged sizes") + args = parser.parse_args() + if args.verb == "raise": + return raise_build(args.size) + if args.verb == "reset": + return reset() + return show() + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/ansible/roles/developer-rust/tasks/build_role.yml b/ansible/roles/developer-rust/tasks/build_role.yml new file mode 100644 index 00000000..6bf56828 --- /dev/null +++ b/ansible/roles/developer-rust/tasks/build_role.yml @@ -0,0 +1,29 @@ +--- +# Sets developer_rust_build_role, which vars/main.yml sizes both slices from. + +- name: Check the Rust build role is one the role knows + ansible.builtin.assert: + that: + - rust_build_role in ['auto', 'local', 'client', 'target'] + fail_msg: "rust_build_role must be auto, local, client or target, not {{ rust_build_role }}" + quiet: true + +- name: Read hyperi-ai's remote build host + ansible.builtin.command: + cmd: "{{ user_home }}/.local/bin/hyperi-ai config get remote.host" + become: true + become_user: "{{ actual_user }}" + register: developer_rust_remote_host + changed_when: false + failed_when: false + check_mode: false + when: rust_build_role == 'auto' + +- name: Record where this host's Rust builds run + ansible.builtin.set_fact: + developer_rust_build_role: >- + {{ rust_build_role if rust_build_role != 'auto' + else ('client' + if developer_rust_remote_host.rc | default(1) == 0 + and (developer_rust_remote_host.stdout | default('') | trim) not in ['', 'None', 'null'] + else 'local') }} diff --git a/ansible/roles/developer-rust/tasks/governor.yml b/ansible/roles/developer-rust/tasks/governor.yml index ee76e488..0695675e 100644 --- a/ansible/roles/developer-rust/tasks/governor.yml +++ b/ansible/roles/developer-rust/tasks/governor.yml @@ -49,6 +49,15 @@ mode: '0755' become: true + - name: Install hyperi-build-slice + ansible.builtin.copy: + src: hyperi-build-slice + dest: /usr/local/bin/hyperi-build-slice + owner: root + mode: '0755' + become: true + when: ansible_facts['system'] == 'Linux' + - name: Create the hyperi config directory ansible.builtin.file: path: "{{ user_home }}/.config/hyperi" diff --git a/ansible/roles/developer-rust/tasks/main.yml b/ansible/roles/developer-rust/tasks/main.yml index 425880a8..3e53260f 100644 --- a/ansible/roles/developer-rust/tasks/main.yml +++ b/ansible/roles/developer-rust/tasks/main.yml @@ -14,6 +14,14 @@ tags: ['developer-rust', 'rust'] tags: ['developer-rust', 'rust', 'rust-cache', 'rust-governor', 'rust-llvm'] +# Both slices size from where builds run, so every tag that renders one sets it. +- name: Settle where this host's Rust builds run + ansible.builtin.include_tasks: + file: build_role.yml + apply: + tags: ['developer-rust', 'rust-governor', 'agents-slice'] + tags: ['developer-rust', 'rust-governor', 'agents-slice'] + # Its own tag so the governor can be applied or re-applied without a full # toolchain converge, the same bargain rust-cache makes. - name: Govern rust build resources diff --git a/ansible/roles/developer-rust/vars/main.yml b/ansible/roles/developer-rust/vars/main.yml index ee900252..0e647c86 100644 --- a/ansible/roles/developer-rust/vars/main.yml +++ b/ansible/roles/developer-rust/vars/main.yml @@ -5,13 +5,22 @@ developer_rust_ram_mb: "{{ ansible_facts['memtotal_mb'] | int }}" -# rustbuild.slice. High never exceeds Max. +# rustbuild.slice, sized by where builds run (tasks/build_role.yml sets the role). +# High never exceeds Max. +developer_rust_slice_max_pct: >- + {{ rust_governor_target_memory_max_pct if developer_rust_build_role == 'target' + else rust_governor_memory_max_pct }} +developer_rust_slice_high_pct: >- + {{ rust_governor_target_memory_high_pct if developer_rust_build_role == 'target' + else rust_governor_memory_high_pct }} developer_rust_slice_max_mb: >- - {{ [developer_rust_ram_mb | int * rust_governor_memory_max_pct | int // 100, - rust_governor_memory_max_floor_mb | int] | max }} + {{ rust_governor_client_memory_max_mb | int if developer_rust_build_role == 'client' + else [developer_rust_ram_mb | int * developer_rust_slice_max_pct | int // 100, + rust_governor_memory_max_floor_mb | int] | max }} developer_rust_slice_high_mb: >- - {{ [[developer_rust_ram_mb | int * rust_governor_memory_high_pct | int // 100, - rust_governor_memory_high_floor_mb | int] | max, + {{ [rust_governor_client_memory_high_mb | int if developer_rust_build_role == 'client' + else [developer_rust_ram_mb | int * developer_rust_slice_high_pct | int // 100, + rust_governor_memory_high_floor_mb | int] | max, developer_rust_slice_max_mb | int] | min }} # What the desktop keeps, and what is left for agent sessions once builds have