Further work
This commit is contained in:
@@ -25,6 +25,14 @@ import logger from '../../utils/logger.js'
|
|||||||
|
|
||||||
const log = logger.child('cgroups')
|
const log = logger.child('cgroups')
|
||||||
|
|
||||||
|
const HOST_CPUS = (() => {
|
||||||
|
try {
|
||||||
|
return os.cpus().length || 1
|
||||||
|
} catch {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
export function isCgroupsEnabled() {
|
export function isCgroupsEnabled() {
|
||||||
const v = process.env.PEARDATA_CGROUPS
|
const v = process.env.PEARDATA_CGROUPS
|
||||||
if (v === '0' || v === 'off' || v === 'false') return false
|
if (v === '0' || v === 'off' || v === 'false') return false
|
||||||
@@ -241,7 +249,7 @@ export class CgroupsCollector extends EventEmitter {
|
|||||||
const ts = Date.now()
|
const ts = Date.now()
|
||||||
const dtSec = this.lastTs ? (ts - this.lastTs) / 1000 : this.intervalMs / 1000
|
const dtSec = this.lastTs ? (ts - this.lastTs) / 1000 : this.intervalMs / 1000
|
||||||
this.lastTs = ts
|
this.lastTs = ts
|
||||||
const ncpu = os.cpus().length || 1
|
const ncpu = HOST_CPUS
|
||||||
/** @type {Array<{ chart: string, context: string, ts: number, values: object }>} */
|
/** @type {Array<{ chart: string, context: string, ts: number, values: object }>} */
|
||||||
const batch = []
|
const batch = []
|
||||||
|
|
||||||
|
|||||||
@@ -30,18 +30,18 @@ export function isProcessCollectorEnabled() {
|
|||||||
return os.platform() === 'linux'
|
return os.platform() === 'linux'
|
||||||
}
|
}
|
||||||
|
|
||||||
function topN() {
|
const HOST_CPUS = (() => {
|
||||||
const n = Number(process.env.PEARDATA_PROCESSES_TOP)
|
|
||||||
return Number.isFinite(n) && n > 0 ? Math.min(32, Math.floor(n)) : 8
|
|
||||||
}
|
|
||||||
|
|
||||||
function hostCpus() {
|
|
||||||
try {
|
try {
|
||||||
return os.cpus().length || 1
|
return os.cpus().length || 1
|
||||||
} catch {
|
} catch {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
}
|
})()
|
||||||
|
|
||||||
|
const PROC_LIMIT = (() => {
|
||||||
|
const n = Number(process.env.PEARDATA_PROCESSES_TOP)
|
||||||
|
return Number.isFinite(n) && n > 0 ? Math.min(32, Math.floor(n)) : 8
|
||||||
|
})()
|
||||||
|
|
||||||
function sanitizeDim(name) {
|
function sanitizeDim(name) {
|
||||||
const s = String(name || 'unknown')
|
const s = String(name || 'unknown')
|
||||||
@@ -68,6 +68,7 @@ export function listProcStats() {
|
|||||||
}
|
}
|
||||||
procTickCount = (procTickCount + 1) % PROC_EXTRA_TICK
|
procTickCount = (procTickCount + 1) % PROC_EXTRA_TICK
|
||||||
const readExtra = procTickCount === 0
|
const readExtra = procTickCount === 0
|
||||||
|
if (readExtra) PROC_EXTRA_CACHE.clear()
|
||||||
|
|
||||||
/** @type {Array<{ pid: number, name: string, utime: number, stime: number, rssPages: number, threads: number, readBytes: number|null, writeBytes: number|null }>} */
|
/** @type {Array<{ pid: number, name: string, utime: number, stime: number, rssPages: number, threads: number, readBytes: number|null, writeBytes: number|null }>} */
|
||||||
const out = []
|
const out = []
|
||||||
@@ -141,7 +142,7 @@ function pageSize() {
|
|||||||
* @param {'cpu'|'rss'|'io'|'threads'} kind
|
* @param {'cpu'|'rss'|'io'|'threads'} kind
|
||||||
*/
|
*/
|
||||||
function registerTopChart(names, kind) {
|
function registerTopChart(names, kind) {
|
||||||
const dims = [...new Set(names)].slice(0, topN()).map((id) => ({
|
const dims = [...new Set(names)].slice(0, PROC_LIMIT).map((id) => ({
|
||||||
id,
|
id,
|
||||||
name: id,
|
name: id,
|
||||||
algorithm: 'absolute',
|
algorithm: 'absolute',
|
||||||
@@ -211,7 +212,7 @@ function registerTopChart(names, kind) {
|
|||||||
export class ProcessCollector extends EventEmitter {
|
export class ProcessCollector extends EventEmitter {
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
super()
|
super()
|
||||||
this.intervalMs = opts.intervalMs || Number(process.env.PEARDATA_SAMPLE_MS) || SAMPLE_INTERVAL_MS
|
this.intervalMs = opts.intervalMs || Number(process.env.PEARDATA_PROCESSES_MS) || Number(process.env.PEARDATA_SAMPLE_MS) || SAMPLE_INTERVAL_MS
|
||||||
this._timer = null
|
this._timer = null
|
||||||
/** @type {Map<number, { ticks: number, wallMs: number, name: string, readBytes: number, writeBytes: number }>|null} */
|
/** @type {Map<number, { ticks: number, wallMs: number, name: string, readBytes: number, writeBytes: number }>|null} */
|
||||||
this._prev = null
|
this._prev = null
|
||||||
@@ -221,7 +222,7 @@ export class ProcessCollector extends EventEmitter {
|
|||||||
start() {
|
start() {
|
||||||
if (this._timer) return
|
if (this._timer) return
|
||||||
this._pageBytes = pageSize()
|
this._pageBytes = pageSize()
|
||||||
log.info('Process top-N collector started', { top: topN() })
|
log.info('Process top-N collector started', { top: PROC_LIMIT })
|
||||||
this._tick()
|
this._tick()
|
||||||
this._timer = setInterval(() => this._tick(), this.intervalMs)
|
this._timer = setInterval(() => this._tick(), this.intervalMs)
|
||||||
if (typeof this._timer.unref === 'function') this._timer.unref()
|
if (typeof this._timer.unref === 'function') this._timer.unref()
|
||||||
@@ -240,8 +241,8 @@ export class ProcessCollector extends EventEmitter {
|
|||||||
if (!procs) return
|
if (!procs) return
|
||||||
const ts = Date.now()
|
const ts = Date.now()
|
||||||
const wallMs = ts
|
const wallMs = ts
|
||||||
const ncpu = hostCpus()
|
const ncpu = HOST_CPUS
|
||||||
const limit = topN()
|
const limit = PROC_LIMIT
|
||||||
|
|
||||||
/** @type {Map<string, number>} */
|
/** @type {Map<string, number>} */
|
||||||
const cpuByName = new Map()
|
const cpuByName = new Map()
|
||||||
|
|||||||
Reference in New Issue
Block a user