repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

wezterm: add scoped build domain

b972dc429a2026b83e533272a37fe1b699918f42

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (7)

+201 / −4 text lines

Diff layout

wezterm/bugabinga/exec_domains.lua

Added
new file mode 100644

+47 / −0 text lines

1local wez = require 'wezterm'
2
3local function append_all(dst, src)
4 for _, value in ipairs(src) do
5 table.insert(dst, value)
6 end
7end
8
9local 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)
36end
38return 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 }
47end

wezterm/bugabinga/key_binds.lua

Modified

+10 / −0 text lines

146146 mods = 'LEADER',
147147 action = wez.action.SpawnTab 'CurrentPaneDomain',
148148 },
149 {
150 key = 'b',
151 mods = 'LEADER',
152 action = wez.action.SpawnCommandInNewTab { domain = { DomainName = 'scope:build', }, },
153 },
154 {
155 key = 'B',
156 mods = 'LEADER|SHIFT',
157 action = wez.action.SpawnCommandInNewTab { domain = 'DefaultDomain', },
158 },
149159 {
150160 key = 'PageUp',
151161 action = wez.action.ScrollByLine( -1 ),

wezterm/bugabinga/launch.lua

Modified

+6 / −0 text lines

33return function ( cfg )
44 cfg.launch_menu = {
5 {
6 label = 'OOM Test (scope:build)',
7 args = { 'nu', wez.home_dir .. '/Workspace/dotfiles/wezterm/scripts/leak-memory.nu', },
8 cwd = wez.home_dir .. '/Workspace/dotfiles/wezterm',
9 domain = { DomainName = 'scope:build', },
10 },
511 {
612 label = 'Anime (new)',
713 args = { 'ani-cli', },

wezterm/bugabinga/status.lua

Modified

+17 / −4 text lines

239239 return elements
240240end
242local 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' } },
253 }
254end
242256local tab_title = function ( tab )
243257 if tab.tab_title and #tab.tab_title > 0 then
244258 local explicit_program = detect_program_from_title( tab.tab_title )
506520 table.insert( status, zoomed_state )
507521 end
509 if pane_domain and pane_domain ~= 'local' and pane_domain ~= 'Local' then
510 table.insert( status, '@' .. pane_domain )
511 end
513523 local status_parts = {}
514524 for _, value in ipairs( status ) do
515525 if value and value ~= '' then table.insert( status_parts, value ) end
552562 for _, element in ipairs( workspace_hud( current_workspace ) ) do
553563 table.insert( left_elements, element )
554564 end
565 for _, element in ipairs( domain_hud( pane_domain ) ) do
566 table.insert( left_elements, element )
567 end
555568 end
557570 window:set_left_status( wez.format( left_elements ) )

wezterm/scripts/leak-memory.nu

Added
new file mode 100755

+79 / −0 text lines

1#!/usr/bin/env nu
2
3def 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)'
11}
13def read-cgroup-file [cg: string, name: string] {
14 let path = ($cg | path join $name)
15 try { open --raw $path | str trim } catch { $'<missing: ($name)>' }
16}
18def 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 }
43}
45let chunk_mib = ($env.LEAK_CHUNK_MIB? | default '16' | into int)
46let sleep_ms = (($env.LEAK_SLEEP_MS? | default '50' | into int) * 1ms)
47let force = (($env.LEAK_FORCE? | default '0') == '1')
48let info = (scope-info)
49let memory_max = $info.memory_max
50let limited = if $memory_max == 'max' {
51 false
52} else {
53 try { ($memory_max | into int) <= 1073741824 } catch { false }
54}
56print ($info | table --expand)
58if (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
64}
66print ''
67print $'leaking ($chunk_mib) MiB every ($sleep_ms); expect cgroup OOM kill inside scope:build'
69mut chunks = []
70mut allocated = 0
71let chunk_size = ($chunk_mib * 1024 * 1024)
73loop {
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
79}

wezterm/scripts/scope-info.nu

Added
new file mode 100755

+41 / −0 text lines

1#!/usr/bin/env nu
2
3def 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)'
11}
13def read-cgroup-file [cg: string, name: string] {
14 let path = ($cg | path join $name)
15 try { open --raw $path | str trim } catch { $'<missing: ($name)>' }
16}
18let cg = (cgroup-path)
19let sensitive_env_names = (
20 $env
21 | transpose name value
22 | where name =~ '(?i)(KEY|TOKEN|SECRET|PASSWORD)'
23 | get name
24 | sort
25)
27{
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
41}

wezterm/wezterm.lua

Modified

+1 / −0 text lines

99require 'bugabinga.window' ( cfg )
1010require 'bugabinga.status' ( cfg )
1111require 'bugabinga.launch' ( cfg )
12require 'bugabinga.exec_domains' ( cfg )
1213require 'bugabinga.hosts' ( cfg )
1415-- logs key presses