Skip to content

feat(ktuner): add deterministic kernel-tuning engine#1278

Merged
kongche-jbw merged 4 commits into
alibaba:mainfrom
jfeng18:feat/ktuner-engine
Jul 4, 2026
Merged

feat(ktuner): add deterministic kernel-tuning engine#1278
kongche-jbw merged 4 commits into
alibaba:mainfrom
jfeng18:feat/ktuner-engine

Conversation

@jfeng18

@jfeng18 jfeng18 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

为 anolisa 新增内核参数自动调优组件 ktuner。cosh 的 agent 可以通过它诊断系统内核配置、给出调优建议、一键应用并支持回滚。

动机

当前 cosh 缺少系统性的内核调优能力。agent 虽然能读取单个内核参数,但没有确定性的规则基线——不知道该检查哪些参数、合理值是什么、改完怎么安全回滚。ktuner 提供这套基线:207 条规则系统性评估当前配置,输出 JSON 格式的诊断和建议,由 agent 调用。规则引擎不依赖 LLM,零 token 成本;LLM 只在解释层加价值("为什么建议改 swappiness")。

什么改了

  • src/ktuner/:新 Rust crate(lib + bin),~10k 行
    • 5 个子命令:check(诊断)、tune(应用)、fix(单条)、why(解释)、rollback(撤销)
    • 所有输出 stdout JSON,错误 stderr JSON,结构化退出码(0/1/2)
    • tune/fix/rollback 需要 root;check/why 不需要
  • src/os-skills/system-admin/ktuner/SKILL.md:注册为 cosh skill,agent 自动发现
  • .github/workflows/ci.yaml:新增 Step 11 test-ktuner job(fmt + clippy + test)
  • .github/commitlint.config.json:新增 ktuner scope

什么没改

anolisa 其他组件零改动。

安全设计

ktuner 以 root 身份写 /proc/sys 和 /sys。安全措施:

  • 代码执行参数禁写:kernel.core_pattern / kernel.modprobe / kernel.hotplug / kernel.poweroff_cmd / kernel.modules_disabled / kernel.kexec_load_disabled / kernel.usermodehelper.* / fs.binfmt_misc.* 全部禁止写入。基于解析后绝对路径匹配,任何拼写变体(斜杠/双斜杠/点分/traversal)都拦截。deny-list 同时覆盖 tune/fix 正向写入路径和 rollback 反向恢复路径。
  • 回滚安全:部分失败或路径缺失时保留 rollback ledger,不丢原值;三态状态报告(Full/Partial/Nothing),只有全部恢复才报"系统恢复原状"
  • readback 验证:每次写入后回读验证,支持 bracket-format sysfs 文件(scheduler/THP)和 write-only 参数(vm.drop_caches)
  • JSON 输出纯净:quiet 模式抑制所有 println 和 systemctl stdout,agent 拿到的 stdout 只有 JSON
  • cosh 权限链路:非 yolo 模式下 agent 执行 sudo 命令需用户确认;ktuner 自身不提权,只检查 euid==0 并报错

验证

层级 状态
单测 191 passed,clippy -D warnings 干净,fmt 干净
ECS E2E check/tune/fix/why/rollback 全链路,root 环境,真实内核参数读写+回滚验证
cosh E2E 模糊 prompt("帮我优化下内核参数"),agent 自主发现 ktuner 并完成 check→tune 全流程;rollback 单独验证通过
对抗式 review 通过

后续规划

本 PR 是第一步:确定性规则引擎,cosh 按需调用,覆盖 80% 常见调优场景。

第二步(agentic-doctor):ktuner 将依靠 agentsight 可观测机制做持续 profiling——利用 BPF 可观测数据做动态调优,智能处理规则引擎覆盖不到的 edge cases。

@github-actions github-actions Bot added component:skill src/os-skills/ scope:ci ./.github/ scope:documentation ./docs/|./*.md|./NOTICE labels Jul 2, 2026
@jfeng18 jfeng18 force-pushed the feat/ktuner-engine branch 2 times, most recently from 4fccd95 to 5051fe6 Compare July 2, 2026 16:11
@casparant

Copy link
Copy Markdown
Collaborator

@jfeng18 代码看过了,非常可以,感谢贡献。组件集成的时候,有一些文档和分发格式上的要求,后续我们会做到 github-ci & AGENTS.md 里,不过很遗憾目前还没有完善,请 @kongche-jbw @ikunkun-sys 后续给一些指引。

@kongche-jbw

kongche-jbw commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

整体看下来 ktuner 这部分已经很完整了,代码、README、CHANGELOG、CI scope 和 commitlint scope 都已经补上,感谢这个大改动。

有两个很小的仓库治理同步点,麻烦再考虑一下:

  1. 现在 ktuner 已经作为新的 component/scope 接入了 CI 和 commitlint,是否也可以顺手同步一下根目录 AGENTS.md?比如组件表、开发命令、scope inference 里加上 ktuner,这样后续 agent 和贡献者看到的入口规则会和 CI 保持一致。
  2. ktuner 是用户可直接使用的 CLI/skill,而且涉及 sudo 和内核参数修改。是否可以考虑补一份 docs/user-guide 入口说明,哪怕先是简短版,说明诊断、dry-run、应用、回滚和权限边界;完整细节可以继续放在 src/ktuner/README.md

这两个不是代码实现问题,主要是让新组件在仓库文档入口上更容易被发现和维护。

jfeng18 added 4 commits July 4, 2026 16:04
Add ktuner as a new component for automated kernel parameter tuning.
The agent diagnoses system configuration against 207 rules, outputs
JSON recommendations, and can safely apply/rollback changes.

Subcommands: check, tune, fix, why, rollback. All stdout is JSON.
Registered as a cosh skill for automatic agent discovery.
- Add ktuner row to root README component table
- Add ktuner scope to prelint validScopes (title + branch checks)
- Add src/ktuner/CHANGELOG.md ([Unreleased] stub)
- test-ktuner: cargo test --lib -> cargo test (parity, no-op today)
Address review feedback on alibaba#1278 from @kongche-jbw:

- AGENTS.md: add ktuner to component table, dev commands, Rust
  conventions list, and Scope Inference table
- Add docs/user-guide user-entrypoint/ktuner.md (en + zh) covering
  diagnose / dry-run / apply / rollback and the permission boundary
- Link ktuner from the user-guide index (en + zh)
@jfeng18 jfeng18 force-pushed the feat/ktuner-engine branch from a807e3d to 8bf35d6 Compare July 4, 2026 08:22

@casparant casparant left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kongche-jbw kongche-jbw merged commit 7a8da0f into alibaba:main Jul 4, 2026
16 checks passed
@jfeng18

jfeng18 commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

感谢 review 和合入!🎉 AGENTS.md 和 user-guide 两处建议都已按你的意见补上。first-run auto-check(#1279)和打包分发路线我会继续跟进,到时按你和 @ikunkun-sys 的指引补齐。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:skill src/os-skills/ scope:ci ./.github/ scope:documentation ./docs/|./*.md|./NOTICE

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants