import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";var n=e(t(),1);function r(e){let t=/avg10=([\d.]+)/.exec(e),n=/avg60=([\d.]+)/.exec(e),r=/avg300=([\d.]+)/.exec(e),i=/total=(\d+)/.exec(e);return!t?.[1]||!n?.[1]||!r?.[1]||!i?.[1]?null:{avg10:parseFloat(t[1]),avg60:parseFloat(n[1]),avg300:parseFloat(r[1]),totalUs:parseInt(i[1],10)}}function i(e){let t=e.split(`
`).map(e=>e.trim()).filter(Boolean),n=null,i=null;for(let e of t)e.startsWith(`some `)?n=r(e):e.startsWith(`full `)&&(i=r(e));return n?{some:n,full:i}:null}function a(e){for(let t of e.split(`
`))if(t.startsWith(`MemAvailable:`)){let e=/MemAvailable:\s+(\d+)\s+kB/.exec(t);if(e?.[1])return parseInt(e[1],10)*1024}return null}function o(e){let t=null,n=null;for(let r of e.split(`
`))if(r.startsWith(`Rss:`)){let e=/Rss:\s+(\d+)\s+kB/.exec(r);e?.[1]&&(t=parseInt(e[1],10))}else if(r.startsWith(`Pss:`)){let e=/Pss:\s+(\d+)\s+kB/.exec(r);e?.[1]&&(n=parseInt(e[1],10))}return t===null||n===null?null:{rssBytes:t*1024,pssBytes:n*1024}}var s=(e,t)=>(0,n.readFile)(e,t),c=async e=>{try{return{size:(await(0,n.stat)(e)).size}}catch{return null}},l=class{globalPressurePath;cgroupSlicePressurePath;walPaths;readFileFn;statFileFn;constructor(e={}){this.globalPressurePath=e.globalPressurePath??`/proc/pressure/memory`,this.cgroupSlicePressurePath=e.cgroupSlicePressurePath??null,this.walPaths=e.walPaths??[],this.readFileFn=e.readFileFn??s,this.statFileFn=e.statFileFn??c}async sample(){let e=Date.now(),t=await this._readPsi(this.globalPressurePath),n=t!==null,r=null;this.cgroupSlicePressurePath&&(r=await this._readPsi(this.cgroupSlicePressurePath));let i=await this._readMemAvailable(),a=await this._readWalObservations();return{sampledAtMs:e,pressureAvailable:n,memAvailableBytes:i,globalPressure:t,slicePressure:r,walObservations:a}}async sweepChildRss(e){let t=Date.now(),n=[];for(let t of e)try{let e=o(await this.readFileFn(`/proc/${t}/smaps_rollup`,`utf-8`));e&&n.push({pid:t,...e})}catch{}return{sampledAtMs:t,entries:n}}async _readPsi(e){try{return i(await this.readFileFn(e,`utf-8`))}catch{return null}}async _readMemAvailable(){try{return a(await this.readFileFn(`/proc/meminfo`,`utf-8`))}catch{return null}}async _readWalObservations(){let e=[];for(let t of this.walPaths){let n=await this.statFileFn(t);e.push({walPath:t,sizeBytes:n?.size??null})}return e}},u={holdSomeAvg10:10,backoffSomeAvg10:20,holdFullAvg10:5,backoffFullAvg10:10,hysteresisPoints:3,headroomBytes:256*1024*1024,walWarnThresholdBytes:256*1024*1024,pollIntervalMs:1500};function d(e){return{holdSomeAvg10:e.psi?.holdSomeAvg10??u.holdSomeAvg10,backoffSomeAvg10:e.psi?.backoffSomeAvg10??u.backoffSomeAvg10,holdFullAvg10:e.psi?.holdFullAvg10??u.holdFullAvg10,backoffFullAvg10:e.psi?.backoffFullAvg10??u.backoffFullAvg10,hysteresisPoints:e.hysteresisPoints??u.hysteresisPoints,headroomBytes:e.headroomBytes??u.headroomBytes,walWarnThresholdBytes:e.walWarnThresholdBytes??u.walWarnThresholdBytes,pollIntervalMs:e.pollIntervalMs??u.pollIntervalMs}}function f(e,t,n){if(!e.pressureAvailable)return e.memAvailableBytes!==null&&e.memAvailableBytes<t.headroomBytes?{state:`hold`,reason:`degraded: memAvailable below headroom`}:{state:`ok`,reason:`degraded: PSI unavailable, memAvailable sufficient`};let r=e.globalPressure?.some??e.slicePressure?.some,i=e.globalPressure?.full??e.slicePressure?.full,a=r?.avg10??0,o=i?.avg10??0;if(a>t.backoffSomeAvg10||o>t.backoffFullAvg10)return{state:`backoff`,reason:`some avg10=${a.toFixed(2)} full avg10=${o.toFixed(2)} — above backoff threshold`};if(a>t.holdSomeAvg10||o>t.holdFullAvg10)return{state:`hold`,reason:`some avg10=${a.toFixed(2)} full avg10=${o.toFixed(2)} — above hold threshold`};if(n!==`ok`){let e=t.holdSomeAvg10-t.hysteresisPoints,r=t.holdFullAvg10-t.hysteresisPoints;if(a>e||o>r)return{state:n===`backoff`?`hold`:n,reason:`some avg10=${a.toFixed(2)} full avg10=${o.toFixed(2)} — hysteresis hold (floor: some ${e}, full ${r})`}}return{state:`ok`,reason:`some avg10=${a.toFixed(2)} full avg10=${o.toFixed(2)} — below thresholds`}}var p=class extends n.EventEmitter{thresholds;backend;_state=`ok`;_timer=null;_running=!1;constructor(e={}){super(),this.thresholds=d(e),this.backend=e.backend??new l}get state(){return this._state}get running(){return this._running}async sample(){return this.backend.sample()}startContinuous(){return this._running?()=>this._stopLoop():(this._running=!0,this._scheduleNext(),()=>this._stopLoop())}stop(){this._stopLoop()}_scheduleNext(){this._timer=setTimeout(()=>{this._tick()},this.thresholds.pollIntervalMs),typeof this._timer.unref==`function`&&this._timer.unref()}async _tick(){if(this._running){try{let e=await this.backend.sample();this.emit(`sample`,e);let{state:t,reason:n}=f(e,this.thresholds,this._state);if(t!==this._state){let r={from:this._state,to:t,sample:e,reason:n};this._state=t,this.emit(`transition`,r)}}catch(e){this.emit(`error`,e instanceof Error?e:Error(String(e)))}this._running&&this._scheduleNext()}}_stopLoop(){this._running=!1,this._timer!==null&&(clearTimeout(this._timer),this._timer=null)}};export{p as t};