repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

wezterm: add scoped build domain

f0d1c760c8a7b88b77082c1433b4896268b65831

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (7)

wezterm/bugabinga/exec_domains.lua

Added
new file mode 100644
1 +local wez = require 'wezterm'
3 +local function append_all(dst, src)
4 + for _, value in ipairs(src) do
5 + table.insert(dst, value)
6 + end
7 +end
9 +local function scoped_domain(name, label, properties)
10 + return wez.exec_domain(name, function(cmd)
11 + local args = cmd.args or { 'nu', '--login' }
12 + local env = cmd.set_environment_variables or {}
13 + env.WEZTERM_EXEC_DOMAIN = name
14 + cmd.set_environment_variables = env
15 + local unit = 'wezterm-' .. name:gsub('[^%w_.-]', '-') .. '-' .. tostring(env.WEZTERM_PANE or os.time())
16 + local wrapped = {
17 + 'systemd-run',
18 + '--user',
19 + '--scope',
20 + '--same-dir',
21 + '--collect',
22 + '--quiet',
23 + '--unit=' .. unit,
24 + '--description=WezTerm ' .. name,
25 + }
27 + for _, property in ipairs(properties) do
28 + table.insert(wrapped, '-p')
29 + table.insert(wrapped, property)
30 + end
32 + append_all(wrapped, args)
33 + cmd.args = wrapped
34 + return cmd
35 + end, label)
36 +end
38 +return function(cfg)
39 + cfg.exec_domains = {
40 + scoped_domain('scope:build', 'scope:build CPU 25% MEM max 256M', {
41 + 'CPUQuota=25%',
42 + 'MemoryMax=256M',
43 + 'MemorySwapMax=0',
44 + 'OOMPolicy=stop',
45 + }),
46 + }
47 +end

wezterm/bugabinga/key_binds.lua

Modified
146 146 mods = 'LEADER',
147 147 action = wez.action.SpawnTab 'CurrentPaneDomain',
150 + key = 'b',
151 + mods = 'LEADER',
152 + action = wez.action.SpawnCommandInNewTab { domain = { DomainName = 'scope:build', }, },
153 + },
155 + key = 'B',
156 + mods = 'LEADER|SHIFT',
157 + action = wez.action.SpawnCommandInNewTab { domain = 'DefaultDomain', },
158 + },
150 160 key = 'PageUp',
151 161 action = wez.action.ScrollByLine( -1 ),

wezterm/bugabinga/launch.lua

Modified
2 2 local wez = require 'wezterm'
4 4 return function ( cfg )
5 5 cfg.launch_menu = {
6 + {
7 + label = 'OOM Test (scope:build)',
8 + args = { 'nu', wez.home_dir .. '/Workspace/dotfiles/wezterm/scripts/leak-memory.nu', },
9 + cwd = wez.home_dir .. '/Workspace/dotfiles/wezterm',
10 + domain = { DomainName = 'scope:build', },
11 + },
7 13 label = 'Anime (new)',
8 14 args = { 'ani-cli', },

wezterm/bugabinga/status.lua

Modified
239 239 return elements
242 +local domain_hud = function ( domain )
243 + if not domain or domain == 'local' or domain == 'Local' then return {} end
245 + local color = palette.ui.important_local
246 + if domain:find( '^scope:', 1, false ) then color = palette.warning end
248 + return {
249 + { Foreground = { Color = color } },
250 + { Attribute = { Intensity = 'Bold' } },
251 + { Text = '@' .. domain .. ' ' },
252 + { Attribute = { Intensity = 'Normal' } },
254 +end
242 256 local tab_title = function ( tab )
243 257 if tab.tab_title and #tab.tab_title > 0 then
244 258 local explicit_program = detect_program_from_title( tab.tab_title )
506 520 table.insert( status, zoomed_state )
509 - if pane_domain and pane_domain ~= 'local' and pane_domain ~= 'Local' then
510 - table.insert( status, '@' .. pane_domain )
511 - end
513 523 local status_parts = {}
514 524 for _, value in ipairs( status ) do
515 525 if value and value ~= '' then table.insert( status_parts, value ) end
552 562 for _, element in ipairs( workspace_hud( current_workspace ) ) do
553 563 table.insert( left_elements, element )
565 + for _, element in ipairs( domain_hud( pane_domain ) ) do
566 + table.insert( left_elements, element )
567 + end
557 570 window:set_left_status( wez.format( left_elements ) )

wezterm/scripts/leak-memory.nu

Added
new file mode 100755
1 +#!/usr/bin/env nu
3 +def cgroup-path [] {
4 + open --raw /proc/self/cgroup
5 + | lines
6 + | parse --regex '^(?P<id>[^:]*):(?P<controllers>[^:]*):(?P<path>.*)$'
7 + | where id == '0'
8 + | get path.0
9 + | str trim --left --char '/'
10 + | $'/sys/fs/cgroup/($in)'
13 +def read-cgroup-file [cg: string, name: string] {
14 + let path = ($cg | path join $name)
15 + try { open --raw $path | str trim } catch { $'<missing: ($name)>' }
18 +def scope-info [] {
19 + let cg = (cgroup-path)
20 + let sensitive_env_names = (
21 + $env
22 + | transpose name value
23 + | where name =~ '(?i)(KEY|TOKEN|SECRET|PASSWORD)'
24 + | get name
25 + | sort
26 + )
28 + {
29 + pid: $nu.pid
30 + cwd: (pwd)
31 + shell: ($env.SHELL? | default '')
32 + wezterm_pane: ($env.WEZTERM_PANE? | default '')
33 + wezterm_exec_domain: ($env.WEZTERM_EXEC_DOMAIN? | default '')
34 + cgroup: $cg
35 + memory_current: (read-cgroup-file $cg memory.current)
36 + memory_high: (read-cgroup-file $cg memory.high)
37 + memory_max: (read-cgroup-file $cg memory.max)
38 + memory_swap_max: (read-cgroup-file $cg memory.swap.max)
39 + cpu_max: (read-cgroup-file $cg cpu.max)
40 + env_count: ($env | transpose name value | length)
41 + sensitive_env_names: $sensitive_env_names
42 + }
45 +let chunk_mib = ($env.LEAK_CHUNK_MIB? | default '16' | into int)
46 +let sleep_ms = (($env.LEAK_SLEEP_MS? | default '50' | into int) * 1ms)
47 +let force = (($env.LEAK_FORCE? | default '0') == '1')
48 +let info = (scope-info)
49 +let memory_max = $info.memory_max
50 +let limited = if $memory_max == 'max' {
51 + false
52 +} else {
53 + try { ($memory_max | into int) <= 1073741824 } catch { false }
56 +print ($info | table --expand)
58 +if (not $limited) and (not $force) {
59 + print ''
60 + print 'REFUSING: memory.max is not <= 1GiB. You are not in the tiny scope.'
61 + print 'Open scope first: LEADER b, then run this script again.'
62 + print 'Override only if intentional: LEAK_FORCE=1 nu scripts/leak-memory.nu'
63 + exit 2
66 +print ''
67 +print $'leaking ($chunk_mib) MiB every ($sleep_ms); expect cgroup OOM kill inside scope:build'
69 +mut chunks = []
70 +mut allocated = 0
71 +let chunk_size = ($chunk_mib * 1024 * 1024)
73 +loop {
74 + let chunk = (random binary $chunk_size)
75 + $chunks = ($chunks | append $chunk)
76 + $allocated = ($allocated + $chunk_mib)
77 + print $'allocated=($allocated) MiB'
78 + sleep $sleep_ms

wezterm/scripts/scope-info.nu

Added
new file mode 100755
1 +#!/usr/bin/env nu
3 +def cgroup-path [] {
4 + open --raw /proc/self/cgroup
5 + | lines
6 + | parse --regex '^(?P<id>[^:]*):(?P<controllers>[^:]*):(?P<path>.*)$'
7 + | where id == '0'
8 + | get path.0
9 + | str trim --left --char '/'
10 + | $'/sys/fs/cgroup/($in)'
13 +def read-cgroup-file [cg: string, name: string] {
14 + let path = ($cg | path join $name)
15 + try { open --raw $path | str trim } catch { $'<missing: ($name)>' }
18 +let cg = (cgroup-path)
19 +let sensitive_env_names = (
20 + $env
21 + | transpose name value
22 + | where name =~ '(?i)(KEY|TOKEN|SECRET|PASSWORD)'
23 + | get name
24 + | sort
28 + pid: $nu.pid
29 + cwd: (pwd)
30 + shell: ($env.SHELL? | default '')
31 + wezterm_pane: ($env.WEZTERM_PANE? | default '')
32 + wezterm_exec_domain: ($env.WEZTERM_EXEC_DOMAIN? | default '')
33 + cgroup: $cg
34 + memory_current: (read-cgroup-file $cg memory.current)
35 + memory_high: (read-cgroup-file $cg memory.high)
36 + memory_max: (read-cgroup-file $cg memory.max)
37 + memory_swap_max: (read-cgroup-file $cg memory.swap.max)
38 + cpu_max: (read-cgroup-file $cg cpu.max)
39 + env_count: ($env | transpose name value | length)
40 + sensitive_env_names: $sensitive_env_names

wezterm/wezterm.lua

Modified
9 9 require 'bugabinga.window' ( cfg )
10 10 require 'bugabinga.status' ( cfg )
11 11 require 'bugabinga.launch' ( cfg )
12 +require 'bugabinga.exec_domains' ( cfg )
12 13 require 'bugabinga.hosts' ( cfg )
14 15 -- logs key presses