-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Unified clock_time subsystem replacing hwtimer/ktime/cputime #11101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
|
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2025-12-29 17:31 CST)
📝 Review Instructions
|
|
@copilot 另外,关于三项: |
Co-authored-by: BernardXiong <1241087+BernardXiong@users.noreply.github.com>
... Fixed in commit e9f911c:
Created compatibility header |
Problem
RT-Thread has three overlapping time subsystems (hwtimer, ktime, cputime) with inconsistent APIs, duplicated functionality, and fragmented BSP implementations.
Solution
Introduced unified
clock_timesubsystem with C-OOP device abstraction and completely removed the old subsystems:Core Implementation
rt_clock_time_devicewithrt_clock_time_opsfor unified timer hardware interfaceOld Subsystems Removed
components/drivers/hwtimer/: 4 files deleted (Kconfig, SConscript, 2 C files)components/drivers/ktime/: 8 files deleted (Kconfig, README, SConscript, header, 4 C files)components/drivers/cputime/: 6 files deleted (Kconfig, SConscript, 4 C files)hwtimer.h,cputime.h,cputimer.hBackward Compatibility
rt_ktime_*APIs mapped tort_clock_*via macroscomponents/drivers/include/ktime.hredirects to clock_time#include <ktime.h>automatically uses the new subsystemSecurity Hardening
Fixed integer overflow vulnerabilities in time calculations:
getcnt() * getres()overflowCode Quality
BSP Porting
Three implementation levels documented:
rt_tick_get())get_freq/get_counter/set_timeoutops)rt_clock_cputimer_*functions)Files Changed
Added (1,880 lines):
components/drivers/clock_time/src/*.c- Core implementationcomponents/drivers/include/drivers/clock_time.h- Public APIcomponents/drivers/clock_time/README.md- Bilingual documentation (EN/ZH)components/drivers/include/ktime.h- Compatibility redirect headerRemoved (2,616 lines):
components/drivers/hwtimer/- Complete directory removedcomponents/drivers/ktime/- Complete directory removedcomponents/drivers/cputime/- Complete directory removedcomponents/drivers/include/drivers/Modified:
components/drivers/Kconfig- Removed old subsystem referencescomponents/drivers/include/rtdevice.h- Removed old includes, added clock_timeNet Result: Cleaner codebase with -736 lines overall, better architecture, and full backward compatibility.
Original prompt
This section details on the original issue you should resolve
<issue_title>[Feature] 对 hwtimer/ktime/cputime 进行整体重构</issue_title>
<issue_description>### Describe problem solved by the proposed feature
目前 hwtimer/ktime/cputime 整体非常乱,应该从整体维度上进行重构,让它有一个更好的架构:
对于这部分的重构,应该先给出整体设计,涉及到的文件评估,完成重构,ci涉及到的每个bsp构建,qemu测试通过。
Describe your preferred solution
No response
Describe possible alternatives
No response</issue_description>
<agent_instructions>目前 hwtimer/ktime/cputime 整体非常乱,应该从整体维度上进行重构,让它有一个更好的架构:
对于这部分的重构,应该先给出整体设计,涉及到的文件评估,完成重构,ci涉及到的每个bsp构建,qemu测试通过。</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@BernardXiong # RT-Thread 时间子系统(ktime / cputime / hwtimer)分析OS 时间子系统包含的主要部分
现状情况
ktime
boottime.c(弱符号,默认用 tick 计数换算 ns/us/s)、cputimer.c(弱符号,默认用 tick;AArch64/virt64 覆写为硬件计数器)、hrtimer.c(链表管理,spinlock 保护)。rt_ktime_hrtimer_settimeout作为硬件回调入口,不实现时退化为软定时器;_cnt_convert做 cputimer 周期到 hrtimer 周期的换算,防止大跨度溢出;_set_next_timeout_locked尝试“消化”已过期并立即重新编程下一个超时。rt_timer类似,易迁移。cputime
rt_clock_cputime_ops(getres/gettime/settimeout),clock_cpu_*工具 API,cputimer.c实现一个基于clock_cpu_settimeout的定时器链表(带信号量 sleep),cputime_cortexm/riscv提供 ops 示例。clock_cpu_settimeout允许硬件 oneshot;rt_cputimer_start/stop使用全局中断屏蔽保护链表;rt_cputime_sleep/ndelay/udelay/mdelay提供高精度延时。udelay类需求。hwtimer
rt_hwtimer_ops/info/device,实现文件hwtimer.c,以及典型驱动hwtimer-arm_arch.c。timeout_calc根据 sec/usec 和 freq/maxcnt 计算合适的 cycles/reload,支持上下计数模式;rt_device_hwtimer_isr维护 overflow/cycles,并通过rx_indicate通知上层;control 支持设频率/模式/获取 info。总结:ktime 和 cputime 都在提供“高精度计数 + oneshot/定时”能力,但各自一套接口(weak vs ops),应该是ktime是cputime的后期期望改善版本(但改善不彻底)。hrtimer/cputimer 都是链表调度,未复用 hwtimer。
@BernardXiong cputime - 是一份简洁版本的cpu time功能,提供对POSIX time的支持(clock_gettime/clock_settime/clock_getres)。而其中又被塞入了一份 定时器 的实现(以链表的方式) hwtimer - 是一份提供硬件外设定时器的设备操作接口的实现; ktime - 期望是一份容纳包括 cputime / bootime,再加上 hrtimer (高分辨率定时器)的实现。但这份实现加入后,又没把之前的给移除……然后就开始堆山了。POSIX中的clockid_t,常见的包括:(取决于实现,POSIX vs Linux 扩展):
- CLOCK_REALTIME:可被 settimeofday 等调整的墙钟时间。
- CLOCK_MONOTONIC:单调递增,不受手动改时影响(可能受 NTP 微调)。
- CLOCK_MONOTONIC_RAW(Linux):硬件单调计数原始值,不做 NTP 调整。
- CLOCK_BOOTTIME(Linux):含挂起/休眠时间的单调计数。
- CLOCK_REALTIME_COARSE / CLOCK_MONOTONIC_COARSE(Linux):低精度、低开销版本。
- CLOCK_PROCESS_CPUTIME_ID:当前进程占用的 CPU 时间。
- CLOCK_THREAD_CPUTIME_ID:当前线程占用的 CPU 时间。
- CLOCK_TAI(Linux):国际原子时基准,不受闰秒调整。
- 其他平台可能有:CLOCK_VIRTUAL、CLOCK_PROF(部分 Unix 旧实现),以及实现自定义的专用时钟。 Availability 需查各平台的 <time.h>/man page。
@BernardXiong ## RT-Thread 时间子系统 C-OOP 设计方案引入统一时间设备抽象
clock_time:作为模块目录(如components/drivers/clock_time),聚合时间基准/事件源能力,显式表意且不与rt_timer冲突。rt_clock_time:作为对象/接口前缀,含义是“时间基准/时钟事件的统一抽象”。rt_clock_time_default(由 BSP 绑定:可直接封装 hwtimer 的 oneshot + count_get,或架构计数器),作为系统时基。res_scale固定用RT_KTIME_RESMUL,兼容当前换算逻辑。目录与功能覆盖情况(clock_time)
components/drivers/clock_time,集中放置抽象、公共实现与适配器。clock_gettime/clock_settime支撑、hrtimer(红黑树优先,极简版可链表降级),并保留定时器对象/设备化接口(可由 hwtimer/DM/OFW 实例化)。rt_clock_time_*前缀的对象/函数,避免与rt_timer冲突且表意清晰。clock_time 引入后,移除 cputime, hwtimer, ktime,都统一收归到 clock_time 中。如原有API有引用,做兼容处理或代码调整。</comment_new>
<comment_new>@BernardXiong<b...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.