UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 6.06 kB
import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{t as decodeMountInfoPath}from"./mountinfo-path-TifqQAkt.mjs";import{a as resolveGatewayServiceCgroupDirectory,i as prepareAgentExecCgroupScope}from"./linux-exec-cgroup-kO5YhBoe.mjs";import fs from"node:fs";import path from"node:path";import os from"node:os"; //!src/process/linux-exec-cgroup-device.ts function readDevice(e){const t=fs.readFileSync(e,`utf8`).trim();if(!/^\d+:\d+$/u.test(t))throw new Error(`Invalid block device identity in ${e}`);return t}function wholeDevice(e){if(!/^\d+:\d+$/u.test(e))throw new Error(`State mount has no block device identity`);let t=fs.realpathSync(`/sys/dev/block/${e}`);try{fs.readFileSync(path.join(t,`partition`),`utf8`);t=path.dirname(t)}catch(e){if(!hasErrnoCode(e,`ENOENT`))throw e}return readDevice(path.join(t,`dev`))}function btrfsDevices(e){const t=fs.statSync(e,{bigint:true});if(!t.isBlockDevice())throw new Error(`Btrfs state mount source is not a block device`);const n=`${t.rdev>>8n&4095n|t.rdev>>32n&4294963200n}:${t.rdev&255n|t.rdev>>12n&4294967040n}`;for(const e of fs.readdirSync(`/sys/fs/btrfs`)){if(!/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/iu.test(e))continue;const t=`/sys/fs/btrfs/${e}/devices`;const r=fs.readdirSync(t).map(e=>readDevice(`${t}/${e}/dev`));if(r.includes(n))return r}throw new Error(`Cannot identify every backing device of the btrfs state mount`)}function resolveExecIoDevices(e){const t=fs.openSync(e,fs.constants.O_RDONLY|fs.constants.O_DIRECTORY);try{const e=/^mnt_id:\s+(\d+)$/mu.exec(fs.readFileSync(`/proc/self/fdinfo/${t}`,`utf8`))?.[1];const n=fs.readFileSync(`/proc/self/mountinfo`,`utf8`).split(` `).find(t=>e&&t.startsWith(`${e} `))?.split(` `);const r=n?.indexOf(`-`)??-1;const i=n?.[2];const a=n?.[r+1];const o=n?.[r+2];if(r<6||!i||!a||!o)throw new Error(`Cannot identify the state directory's mounted filesystem`);if(a!==`btrfs`&&i.startsWith(`0:`))throw new Error(`State filesystem ${a} has no block device for IO throttling`);const s=a===`btrfs`?btrfsDevices(decodeMountInfoPath(o)):[i];return[...new Set(s.map(wholeDevice))].toSorted()}finally{fs.closeSync(t)}} //!src/process/linux-exec-cgroup-startup.ts const cgroupMount=`/sys/fs/cgroup`;const budgetKeys=[`rbps`,`wbps`,`riops`,`wiops`];function readOptional(e){try{return fs.readFileSync(e,`utf8`).trim()}catch(e){if(hasErrnoCode(e,`ENOENT`))return``;throw e}}function ioBudget(e,t,n){for(let r=e;r!==cgroupMount;r=path.dirname(r)){const e=readOptional(path.join(r,`io.max`)).split(` `).find(e=>e.startsWith(`${t} `));for(const t of e?.split(/\s+/u).slice(1)??[]){const[e,r]=t.split(`=`);for(const t of budgetKeys)if(e===t&&r!==`max`&&Number(r)>0)n[t]=Math.min(n[t],Math.max(1,Math.floor(Number(r)/2)))}}return n}function resolveIoLimits(e,t){const n=process.constrainedMemory();const r=n>0?Math.min(os.totalmem(),n):os.totalmem();const i=Math.max(1,Math.floor(Math.min(r/4,(r-process.memoryUsage.rss())/2)));const a=resolveExecIoDevices(e);return a.map(e=>{const n=readOptional(`/sys/dev/block/${e}/queue/rotational`)===`0`;const r=n?250*1024**2:Math.max(1,Math.floor(i/600/a.length));const o=ioBudget(t,e,{rbps:r,wbps:r,riops:n?40960:Math.max(1,Math.floor(r/4096)),wiops:n?40960:Math.max(1,Math.floor(r/4096))});return`${e} ${budgetKeys.map(e=>`${e}=${o[e]}`).join(` `)}`})}function initializeDelegatedExecIo(e,t){const n=path.join(e,`agent-exec`);if(readOptional(path.join(n,`cgroup.procs`)))return`Existing commands retain their shared exec IO policy; memory protection uses the shared sampler.`;if(!t.length)throw new Error(`the state-device IO budget is unavailable`);fs.writeFileSync(path.join(e,`cgroup.subtree_control`),`+cpu +io +memory`);fs.mkdirSync(n,{recursive:true});fs.writeFileSync(path.join(n,`memory.high`),`max`);fs.writeFileSync(path.join(n,`cpu.weight`),`100`);for(const e of t)fs.writeFileSync(path.join(n,`io.max`),e);fs.writeFileSync(path.join(n,`cgroup.subtree_control`),`+cpu +io +memory`);prepareAgentExecCgroupScope(n);fs.writeFileSync(path.join(n,`cpu.weight`),`10`);return`Agent exec IO isolation active: cpu.weight=10; io.max: ${t.join(`; `)}. Each session shares half the aggregate IO ceiling. Memory protection uses the shared sampler.`}async function initializeGatewayExecCgroup(e){if(process.platform!==`linux`)return`Agent exec cgroup isolation is only available on Linux.`;let t=``;try{const n=/^0::(.+)$/mu.exec(fs.readFileSync(`/proc/self/cgroup`,`utf8`))?.[1];const r=n&&/^(.*\.service)(?:\/.*)?$/u.exec(n)?.[1];const i=r&&path.join(cgroupMount,r,`agent-exec`);let a=``;const o=n===`${r}/agent-exec`||n?.startsWith(`${r}/agent-exec/`);if(i&&!o){a=readOptional(path.join(i,`io.max`));try{const e=Number(readOptional(path.join(i,`memory.high`)));if(e>0&&Number.isFinite(e))fs.writeFileSync(path.join(i,`memory.high`),`max`)}catch(e){t=` The earlier memory.high throttle could not be removed: ${String(e)}.`}}const s=resolveGatewayServiceCgroupDirectory();if(!s||path.basename(n??``)!==`gateway`)throw new Error(`requires Delegate=yes and DelegateSubgroup=gateway (systemd 254+)`);const c=path.join(s,`agent-exec`);try{fs.accessSync(c,fs.constants.W_OK|fs.constants.X_OK);fs.accessSync(path.join(c,`cgroup.procs`),fs.constants.W_OK)}catch(e){if(!hasErrnoCode(e,`ENOENT`))throw e}const l=/(?:rbps|wbps|riops|wiops)=[1-9]\d*/u.test(a)&&readOptional(path.join(c,`cpu.weight`))===`10`;if(l&&t)return`Agent exec IO isolation active: retaining existing aggregate and session IO policies. Memory protection uses the shared sampler.${t}`;let u;try{u=resolveIoLimits(e,s);if(!u.length)throw new Error(`the state-device IO budget is unavailable`)}catch(e){if(!l)throw e;return`Agent exec IO isolation active: retaining existing aggregate and session IO policies because refresh is unavailable (${String(e)}). Memory protection uses the shared sampler.${t}`}return`${initializeDelegatedExecIo(s,u)}${t}`}catch(e){return`Agent exec isolation unavailable: ${String(e)}. Continuing with shared sampled memory protection. Check Gateway service delegation with openclaw doctor.${t}`}}export{initializeGatewayExecCgroup};