{"version":3,"file":"common.cjs","names":["cpus","totalmem","freemem"],"sources":["../../src/stats/common.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { cpus, totalmem, freemem } from 'os';\nimport type { CpuInfo } from 'os';\nimport type { CpuUsageData, MemoryData, ProcessCpuUsageData } from '../types';\n\nconst MILLISECOND = 1 / 1e3;\nconst MICROSECOND = 1 / 1e6;\n\n/**\n * We get data as soon as we load the module so the 1st collect\n * of the metric already has valuable data to be sent.\n */\nlet prevOsData: { time: number; cpus: CpuInfo[] } = {\n  time: Date.now(),\n  cpus: cpus(),\n};\n\n/**\n * For each CPU returned by `os.cpus()` it returns\n * - the CPU times in each state (user, sys, ...) in seconds\n * - the % of time the CPU was in each state since last measurement\n */\nexport function getCpuUsageData(): CpuUsageData[] {\n  const currentTime = Date.now();\n  const timeElapsed = currentTime - prevOsData.time;\n  const currentOsData = { time: currentTime, cpus: cpus() };\n\n  const usageData = currentOsData.cpus.map((cpu, cpuNumber) => {\n    const prevTimes = prevOsData.cpus[cpuNumber].times;\n    const currTimes = cpu.times;\n\n    const idle = currTimes.idle * MILLISECOND;\n    const user = currTimes.user * MILLISECOND;\n    const system = currTimes.sys * MILLISECOND;\n    const interrupt = currTimes.irq * MILLISECOND;\n    const nice = currTimes.nice * MILLISECOND;\n\n    const idleP = (currTimes.idle - prevTimes.idle) / timeElapsed;\n    const userP = (currTimes.user - prevTimes.user) / timeElapsed;\n    const systemP = (currTimes.sys - prevTimes.sys) / timeElapsed;\n    const interruptP = (currTimes.irq - prevTimes.irq) / timeElapsed;\n    const niceP = (currTimes.nice - prevTimes.nice) / timeElapsed;\n\n    return {\n      cpuNumber: String(cpuNumber),\n      idle,\n      user,\n      system,\n      interrupt,\n      nice,\n      userP,\n      systemP,\n      idleP,\n      interruptP,\n      niceP,\n    };\n  });\n\n  prevOsData = currentOsData;\n\n  return usageData;\n}\n\n/**\n * We get data as soon as we load the module so the 1st collect\n * of the metric already has valuable data to be sent.\n */\nlet prevProcData: { time: number; usage: NodeJS.CpuUsage } = {\n  time: Date.now(),\n  usage: process.cpuUsage(),\n};\n\n/**\n * Gets the process CPU usage and returns\n * - the time spent in `user` state\n * - the time spent in `system` state\n * - the % of time in `user` state since last measurement\n * - the % of time in `system` state since last measurement\n */\nexport function getProcessCpuUsageData(): ProcessCpuUsageData {\n  const currentTime = Date.now();\n  const currentUsage = process.cpuUsage();\n  const prevUsage = prevProcData.usage;\n  // According to semantic conventions we need to divide by\n  // - time elapsed (in microseconds to match `process.cpuUsage()` units)\n  // - number of CPUs\n  const timeElapsed = (currentTime - prevProcData.time) * 1000;\n  const cpusTimeElapsed = timeElapsed * prevOsData.cpus.length;\n\n  const user = currentUsage.user * MICROSECOND;\n  const system = currentUsage.system * MICROSECOND;\n  const userP = (currentUsage.user - prevUsage.user) / cpusTimeElapsed;\n  const systemP = (currentUsage.system - prevUsage.system) / cpusTimeElapsed;\n\n  prevProcData = { time: currentTime, usage: currentUsage };\n\n  return {\n    user,\n    system,\n    userP,\n    systemP,\n  };\n}\n\n/**\n * Returns memory data as absolute values\n */\nexport function getMemoryData(): MemoryData {\n  const total = totalmem();\n  const free = freemem();\n\n  const used = total - free;\n\n  const freeP = free / total;\n  const usedP = used / total;\n\n  return {\n    used: used,\n    free: free,\n    usedP: usedP, // this is frac part (0-1)\n    freeP: freeP, // this is frac part (0-1)\n  };\n}\n\n/**\n * Returns process memory RSS\n * The Resident Set Size, is the amount of space occupied in the main memory device (that is a subset of the total allocated memory) for the process,\n * including all C++ and JavaScript objects and code.\n */\nexport function getProcessMemoryData(): number {\n  // `process.memoryUsage.rss` is a faster alternative introduced in v14.18.0.\n  // Prefer it if available.\n  if (process.memoryUsage.rss) {\n    return process.memoryUsage.rss();\n  }\n  return process.memoryUsage().rss;\n}\n"],"mappings":";;AASA,MAAM,cAAc,IAAI;AACxB,MAAM,cAAc,IAAI;;;;;AAMxB,IAAI,aAAgD;CAClD,MAAM,KAA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