Skip to main content

agentmux_srv/backend/blockcontroller/
pidregistry.rs

1// Copyright 2025, Command Line Inc.
2// Copyright 2025-2026, AgentMux Corp.
3// SPDX-License-Identifier: Apache-2.0
4
5//! Block PID registry: maps blockId → OS process ID for per-pane metrics collection.
6//! Used by the sysinfo loop to query per-process CPU/memory stats.
7
8use std::collections::HashMap;
9use std::sync::{LazyLock, RwLock};
10
11static BLOCK_PIDS: LazyLock<RwLock<HashMap<String, u32>>> =
12    LazyLock::new(|| RwLock::new(HashMap::new()));
13
14pub fn register(block_id: &str, pid: u32) {
15    BLOCK_PIDS.write().unwrap().insert(block_id.to_string(), pid);
16    tracing::debug!(block_id = %block_id, pid = pid, "pidregistry: registered");
17}
18
19pub fn unregister(block_id: &str) {
20    BLOCK_PIDS.write().unwrap().remove(block_id);
21    tracing::debug!(block_id = %block_id, "pidregistry: unregistered");
22}
23
24pub fn get_all() -> Vec<(String, u32)> {
25    BLOCK_PIDS
26        .read()
27        .unwrap()
28        .iter()
29        .map(|(k, v)| (k.clone(), *v))
30        .collect()
31}