Repository navigation
fix(process): CPU/GPU 占用至少按 1 秒的窗口计算 - #81
Merged
Merged
Conversation
processMemory.summarize 以前只要两次读取的间隔大于 0 就计算占用率。桌面端 打开内存面板时会紧挨着读两次进程表(打开时一次,插件名称读完后又一次), 相隔只有几十毫秒,这一瞬间的读取开销和面板打开的开销被当成持续占用, 主进程 CPU 显示到 166%、GPU 到 28%,过几秒才恢复。 - 新增 MIN_USAGE_WINDOW(1,000,000 µs):相隔不足 1 秒的两张表不做比较。 - 基准新增可选的 earlier:正常读取返回的基准把上一个基准的计数记作 earlier。 - 距基准不足 1 秒的读取改按 earlier 计算(earlier 也必须早于它至少 1 秒), 没有 earlier 就不给占用率;返回的基准原样不变,下一次仍按同一组计数比较。 - 时钟回退(sampledAt 早于基准)照旧不给占用率,并重新开始一个不带 earlier 的基准。 - 契约文档和 memory-summary-v1.json 同步:已有用例的期望基准带上 earlier, 新增“按 earlier 计算并保留基准”“没有 earlier 时不给占用率并保留基准”两例。 earlier 是可选字段,schemaVersion 不变。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue
类型
fix:
processMemory.summarize的 CPU/GPU 占用至少按 1 秒的窗口计算改动
根因
占用率是两张进程表之间累计时间的差除以间隔。
Usage::since以前只要求间隔大于 0(#45,40711b1)。桌面端打开内存详情时会紧挨着读两次进程表:打开时读一次,插件名称读完后又读一次,相隔只有几十毫秒。这段时间里主要是读取本身和面板打开的开销,Core 却按持续占用计算,于是出现瞬时高值。规则(
rust/crates/process/src/memory.rs)新增常量
MIN_USAGE_WINDOW = 1_000_000(微秒)。基线UsageBaseline新增可选字段earlier: { sampledAt, processes },记录这个基线自己的占用率是从哪组计数算出来的。earlier= 上一个基线的sampledAt和processesearlier计算;earlier也必须早于读数至少 1 秒,否则不给earliersampledAt时开始新基线,不带earlier读数过近时保留原基线,下一次读数仍和同一组计数比较,所以连续的快速读取不会把窗口越缩越短。按
earlier计算让过近的读数仍有数值,详情里的 CPU/GPU 列不会在打开时消失再出现。宿主不需要判断间隔,只需把返回的基线原样发回。UsageWindow,负责选择这次按哪组计数计算、是否保留基线。Rates.interval取实际使用的窗口长度。UsageFigures.cpu_percent/gpu_percent、MemorySummary.baseline、Rates.interval、Usage::since的注释。测试
a_table_read_too_soon_after_the_baseline_keeps_it_and_has_no_percentages:读数比基线晚 0、50 ms、999.999 ms。修复前得到 100% 并开始新基线,修复后没有占用率,基线原样返回。a_table_read_too_soon_is_measured_from_the_earlier_counters:earlier早 2 秒时按它算出 50.0%;earlier只早 0.5 秒时不给占用率。a_table_read_a_window_later_keeps_the_previous_counters_as_earlier:恰好晚 1 秒时按基线计算,新基线的earlier是上一个基线的计数。a_table_read_before_the_baseline_starts_a_new_one_without_percentages:替换原来的a_table_read_no_later_than_the_baseline_has_no_percentages,只覆盖时钟回退。契约与 fixture
rust/contracts/rust-core-api.md:基线写作{ sampledAt, processes, earlier? },原样发回;新增 1 秒最短窗口和earlier的说明;“没有早于读数至少 1 秒的计数”和“时钟回退重新开始不带earlier的基线”写进最后一条。rust/fixtures/process-lifecycle/memory-summary-v1.json:已有三个计算占用率的用例,期望基线补上earlier。新增两例:“不足 1 秒按earlier计算并保留基线”(主进程 CPU 50.0 / GPU 20.0、子进程 20.0 / 0.0、合计 70.0 / 20.0),和“不足 1 秒且没有earlier,不给占用率并保留基线”。earlier是可选字段,schemaVersion保持 1。插件契约:没有改
shared/plugins/、能力版本或plugin-rpc,不需要更新协议更新记录,也不需要宿主方法表。验证
cargo test --locked -p lithe-processcargo test --locked -p lithe-core process_memorymemory-summary-v1.json)cargo clippy --locked -p lithe-process -p lithe-core --all-targets -- -D warnings(macOS 与--target x86_64-pc-windows-msvc)cargo fmt --all -- --checkscripts/rust-core/verify-rust-core-comments.sh processnode scripts/rust-core/verify-platform-tests.mjs./scripts/desktop/desktop-go.sh test ./internal/core ./internal/workbench(链接本分支 8dd6c70)后续
PINNED_CORE_REVISION升到合并提交。