Skip to content

fix: two-phase ps sampling for space-safe command parsing (issue #29)#38

Merged
ShadyUnderLight merged 2 commits into
mainfrom
fix/issue29-ps-parse-spaces
Apr 21, 2026
Merged

fix: two-phase ps sampling for space-safe command parsing (issue #29)#38
ShadyUnderLight merged 2 commits into
mainfrom
fix/issue29-ps-parse-spaces

Conversation

@ShadyUnderLight
Copy link
Copy Markdown
Owner

修复内容

根因:原代码 ps -axo pid=,comm=,%cpu=,rss= 按空格 split 解析,command/path 含空格时字段错位导致整行被丢弃,top apps 列表缺少真实高负载进程。

方案:两阶段采样,完全避开空格问题:

  1. Phase 1ps -axo pid=,%cpu=,rss= 用正则稳定解析 pid/cpu/rss,command 置空
  2. Phase 2 — 对每个高优先级 PID 并发拉 ps -p <pid> -o comm=(comm= 格式无空格),填回 command

改动

文件 改动
ProcessSnapshotProvider.swift 改为两阶段采样;移除 comm= 字段;新增 fetchCommand() / fetchCommands()RawProcess.command 改为可选
ProcessSnapshotProviderTests.swift 新增 6 个 regression tests(常规/多余空格/非法行过滤/全非法/负数CPU/可变间距)
Package.swift 新增 ProcessBarMonitorTests test target

swift build && swift test

待你 review / 合并

按流程,等你确认后 merge。

LMZ added 2 commits April 21, 2026 11:53
…pace-sensitive field splitting (issue #29)

Phase 1: parse pid/cpu/rss via regex from `ps -axo pid=,%cpu=,rss=` (no comm=)
Phase 2: fetch `ps -p <pid> -o comm=` concurrently for top-priority PIDs only
Covered by 6 regression tests in Tests/ProcessBarMonitorTests/
…ilable command (PR #38 review fix)

The pid-based fallback key ("pid:<n>") is now used when command is
nil and no metadata cache entry exists, instead of the previous ""
fallback that caused unrelated processes to collapse into one aggregate.

Also adds:
- aggregateKeyForTest() test helper (static, nonisolated)
- 4 new aggregation regression tests:
  • non-prioritized nil-command processes each get unique key
  • prioritized PID with failed fetchCommand avoids empty-key collision
  • resolved commands still group correctly by command string
  • spaced command paths resolve correctly without pid fallback

Fixes review blocking issue on PR #38.
@ShadyUnderLight ShadyUnderLight merged commit 6046b81 into main Apr 21, 2026
2 checks passed
@ShadyUnderLight ShadyUnderLight deleted the fix/issue29-ps-parse-spaces branch April 21, 2026 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant