Skip to content

Commit 8a278c3

Browse files
committed
project/info: cgroup v2 compatibility
1 parent 7b579fe commit 8a278c3

File tree

4 files changed

+364
-21
lines changed

4 files changed

+364
-21
lines changed

src/CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This file provides guidance to Claude Code (claude.ai/code) and also Gemini CLI
2020
- Some older code is JavaScript or CoffeeScript, which will be translated to TypeScript
2121
- Use ES modules (import/export) syntax, not CommonJS (require)
2222
- Organize the list of imports in such a way: installed npm packages are on top, newline, then are imports from @cocalc's code base. Sorted alphabetically.
23+
- **Backend Logging**: Use `getLogger` from `@cocalc/project/logger` for logging in backend code. Do NOT use `console.log`. Example: `const L = getLogger("module:name").debug;`
2324

2425
## Development Commands
2526

src/packages/comm/project-status/utils.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@ import type {
88
DiskUsageInfo,
99
} from "@cocalc/util/types/project-info/types";
1010

11-
// DiskUsage for /tmp !
11+
/**
12+
* Calculate resource usage statistics from cgroup data.
13+
*
14+
* This function processes cgroup resource information and calculates usage percentages
15+
* for both memory and CPU. It works with both cgroup v1 and v2 data structures,
16+
* handling the unified CGroup interface that abstracts the differences between versions.
17+
*/
1218
export function cgroup_stats(cg: CGroup, du?: DiskUsageInfo) {
13-
// why? /tmp is a memory disk in kucalc
19+
// DiskUsage for /tmp – add to memory usage since it's already been
20+
// calculated appropriately by the backend based on whether /tmp is tmpfs
1421
const mem_rss = cg.mem_stat.total_rss + (du?.usage ?? 0);
1522
const mem_tot = cg.mem_stat.hierarchical_memory_limit;
16-
const mem_pct = 100 * Math.min(1, mem_rss / mem_tot);
17-
const cpu_pct = 100 * Math.min(1, cg.cpu_usage_rate / cg.cpu_cores_limit);
23+
24+
// Handle unlimited (-1) and zero memory limits to avoid division by zero
25+
const mem_pct = mem_tot <= 0 ? 0 : 100 * Math.min(1, mem_rss / mem_tot);
26+
27+
// Handle unlimited (-1) and zero CPU limits to avoid division by zero
28+
const cpu_pct =
29+
cg.cpu_cores_limit <= 0
30+
? 0
31+
: 100 * Math.min(1, cg.cpu_usage_rate / cg.cpu_cores_limit);
32+
1833
const cpu_tot = cg.cpu_usage; // seconds
1934
return { mem_rss, mem_tot, mem_pct, cpu_pct, cpu_tot };
2035
}

0 commit comments

Comments
 (0)