Path: blob/master/src/packages/comm/project-status/utils.ts
5770 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import type {6CGroup,7DiskUsageInfo,8} from "@cocalc/util/types/project-info/types";910/**11* Calculate resource usage statistics from cgroup data.12*13* This function processes cgroup resource information and calculates usage percentages14* for both memory and CPU. It works with both cgroup v1 and v2 data structures,15* handling the unified CGroup interface that abstracts the differences between versions.16*/17export function cgroup_stats(cg: CGroup, du?: DiskUsageInfo) {18// DiskUsage for /tmp – add to memory usage since it's already been19// calculated appropriately by the backend based on whether /tmp is tmpfs20const mem_rss = cg.mem_stat.total_rss + (du?.usage ?? 0);21const mem_tot = cg.mem_stat.hierarchical_memory_limit;2223// Handle unlimited (-1) and zero memory limits to avoid division by zero24const mem_pct = mem_tot <= 0 ? 0 : 100 * Math.min(1, mem_rss / mem_tot);2526// Handle unlimited (-1) and zero CPU limits to avoid division by zero27const cpu_pct =28cg.cpu_cores_limit <= 029? 030: 100 * Math.min(1, cg.cpu_usage_rate / cg.cpu_cores_limit);3132const cpu_tot = cg.cpu_usage; // seconds33return { mem_rss, mem_tot, mem_pct, cpu_pct, cpu_tot };34}353637