[components][ktime]support period ktime timeout and multiple ktimer_hrtimer (#8972)

* support period time

* enable multiple ktimer

* mv set delay_cnt to hrtimer_start

* add ktime debug info

* change current_irq_begin to local var

* fix bug: setting current timer and setting timeout in driver aren't atomicly

* create->init

* refactoring ktime
This commit is contained in:
zms123456 2024-06-23 22:08:54 +08:00 committed by GitHub
parent 5c1e5d2fca
commit 22b5e5fd7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 132 additions and 139 deletions

View File

@ -104,7 +104,6 @@ struct rt_pic_isr
#ifdef RT_USING_PIC_STATISTICS
struct rt_pic_irq_statistics
{
rt_ubase_t current_irq_begin[RT_CPUS_NR];
rt_ubase_t max_irq_time_ns;
rt_ubase_t min_irq_time_ns;
rt_ubase_t sum_irq_time_ns;

View File

@ -22,10 +22,11 @@
struct rt_ktime_hrtimer
{
struct rt_object parent; /**< inherit from rt_object */
rt_list_t row;
rt_uint8_t flag; /**< compatible to tick timer's flag */
char name[RT_NAME_MAX];
rt_list_t node;
void *parameter;
unsigned long init_cnt;
unsigned long delay_cnt;
unsigned long timeout_cnt;
rt_err_t error;
struct rt_completion completion;
@ -113,22 +114,24 @@ unsigned long rt_ktime_hrtimer_getfrq(void);
unsigned long rt_ktime_hrtimer_getcnt(void);
/**
* @brief set hrtimer timeout, when timeout, the timer callback will call timeout
* @brief set hrtimer interrupt timeout count (cnt), you should re-implemented it in hrtimer device driver
*
* @param cnt: hrtimer requires a timing cnt value
* @param timeout: timeout callback
* @param param: parameter
* @return rt_err_t
*/
rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt, void (*timeout)(void *param), void *param);
rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt);
/**
* @brief called in hrtimer device driver isr routinue, it will process the timeouts
*/
void rt_ktime_hrtimer_process(void);
void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
const char *name,
unsigned long cnt,
rt_uint8_t flag,
void (*timeout)(void *parameter),
void *parameter);
rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer);
rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long cnt);
rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer);
rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg);
rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer);
@ -143,6 +146,7 @@ rt_inline void rt_ktime_hrtimer_keep_errno(rt_ktime_hrtimer_t timer, rt_err_t er
void rt_ktime_hrtimer_delay_init(struct rt_ktime_hrtimer *timer);
void rt_ktime_hrtimer_delay_detach(struct rt_ktime_hrtimer *timer);
void rt_ktime_hrtimer_process(void);
/**
* @brief sleep by the cputimer cnt value

View File

@ -13,6 +13,10 @@
#include <rthw.h>
#include <rtthread.h>
#define DBG_SECTION_NAME "drv.ktime"
#define DBG_LEVEL DBG_INFO
#include <rtdbg.h>
#include "ktime.h"
#ifdef ARCH_CPU_64BIT
@ -22,9 +26,13 @@
#endif
static rt_list_t _timer_list = RT_LIST_OBJECT_INIT(_timer_list);
static rt_ktime_hrtimer_t _nowtimer = RT_NULL;
static RT_DEFINE_SPINLOCK(_spinlock);
rt_inline rt_ktime_hrtimer_t _first_hrtimer(void)
{
return rt_list_isempty(&_timer_list) ? RT_NULL : rt_list_first_entry(&_timer_list, struct rt_ktime_hrtimer, node);
}
rt_weak rt_uint64_t rt_ktime_hrtimer_getres(void)
{
return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND;
@ -40,50 +48,30 @@ rt_weak unsigned long rt_ktime_hrtimer_getcnt(void)
return rt_tick_get();
}
static void (*_outcb)(void *param) = RT_NULL;
static void _hrtimer_timeout(void *parameter)
{
if (_outcb)
_outcb(parameter);
}
rt_weak rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt, void (*timeout)(void *param), void *param)
rt_weak rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt)
{
static rt_timer_t timer = RT_NULL;
static struct rt_timer _sh_rtimer;
_outcb = timeout;
if (cnt == 0)
{
if (timer != RT_NULL)
{
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
rt_timer_stop(timer);
}
}
if (_outcb)
_outcb(param);
return RT_EOK;
}
RT_ASSERT(cnt > 0);
if (timer == RT_NULL)
{
timer = rt_timer_create("shrtimer", _hrtimer_timeout, param, cnt, RT_TIMER_FLAG_ONE_SHOT);
timer = &_sh_rtimer;
rt_timer_init(timer, "shrtimer", (void (*)(void *))rt_ktime_hrtimer_process, RT_NULL, cnt, RT_TIMER_FLAG_ONE_SHOT);
}
else
{
rt_tick_t tick = cnt;
rt_timer_control(timer, RT_TIMER_CTRL_SET_TIME, &tick);
rt_timer_control(timer, RT_TIMER_CTRL_SET_PARM, param);
rt_timer_control(timer, RT_TIMER_CTRL_SET_PARM, RT_NULL);
}
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
{
rt_timer_stop(timer);
}
rt_timer_start(timer);
return RT_EOK;
}
@ -111,63 +99,81 @@ static void _sleep_timeout(void *parameter)
rt_completion_done(&timer->completion);
}
static void _set_next_timeout_n_unlock(rt_base_t level);
static void _timeout_callback(void *parameter)
static void _insert_timer_to_list_locked(rt_ktime_hrtimer_t timer)
{
rt_ktime_hrtimer_t iter;
rt_list_for_each_entry(iter, &_timer_list, node)
{
if (iter->timeout_cnt > timer->timeout_cnt)
{
break;
}
}
rt_list_insert_before(&iter->node, &(timer->node));
timer->flag |= RT_TIMER_FLAG_ACTIVATED;
}
static void _hrtimer_process_locked(void)
{
rt_ktime_hrtimer_t timer;
timer = (rt_ktime_hrtimer_t)parameter;
rt_base_t level;
level = rt_spin_lock_irqsave(&_spinlock);
_nowtimer = RT_NULL;
rt_list_remove(&(timer->row));
for (timer = _first_hrtimer();
(timer != RT_NULL) && (timer->timeout_cnt <= rt_ktime_cputimer_getcnt());
timer = _first_hrtimer())
{
rt_list_remove(&(timer->node));
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
if (timer->flag & RT_TIMER_FLAG_PERIODIC)
{
timer->timeout_cnt = timer->delay_cnt + rt_ktime_cputimer_getcnt();
_insert_timer_to_list_locked(timer);
}
else
{
timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
}
if (timer->timeout_func)
{
timer->timeout_func(timer->parameter);
}
_set_next_timeout_n_unlock(level);
}
}
static void _set_next_timeout_n_unlock(rt_base_t level)
static void _set_next_timeout_locked(void)
{
rt_ktime_hrtimer_t t;
rt_ktime_hrtimer_t timer;
rt_ubase_t next_timeout_hrtimer_cnt;
if (&_timer_list != _timer_list.prev)
if ((timer = _first_hrtimer()) != RT_NULL)
{
t = rt_list_entry((&_timer_list)->next, struct rt_ktime_hrtimer, row);
if (_nowtimer != RT_NULL)
next_timeout_hrtimer_cnt = _cnt_convert(timer->timeout_cnt);
if (next_timeout_hrtimer_cnt > 0)
{
if (t != _nowtimer && t->timeout_cnt < _nowtimer->timeout_cnt)
{
_nowtimer = t;
rt_spin_unlock_irqrestore(&_spinlock, level);
rt_ktime_hrtimer_settimeout(_cnt_convert(t->timeout_cnt), _timeout_callback, t);
rt_ktime_hrtimer_settimeout(next_timeout_hrtimer_cnt);
}
else
{
_hrtimer_process_locked();
_set_next_timeout_locked();
}
}
}
void rt_ktime_hrtimer_process(void)
{
rt_base_t level = rt_spin_lock_irqsave(&_spinlock);
_hrtimer_process_locked();
_set_next_timeout_locked();
rt_spin_unlock_irqrestore(&_spinlock, level);
}
}
else
{
_nowtimer = t;
rt_spin_unlock_irqrestore(&_spinlock, level);
rt_ktime_hrtimer_settimeout(_cnt_convert(t->timeout_cnt), _timeout_callback, t);
}
}
else
{
_nowtimer = RT_NULL;
rt_spin_unlock_irqrestore(&_spinlock, level);
rt_ktime_hrtimer_settimeout(0, RT_NULL, RT_NULL);
}
}
void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
const char *name,
unsigned long cnt,
rt_uint8_t flag,
void (*timeout)(void *parameter),
void *parameter)
@ -175,55 +181,40 @@ void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer,
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(timeout != RT_NULL);
RT_ASSERT(cnt < (_HRTIMER_MAX_CNT / 2));
/* set flag */
timer->parent.flag = flag;
rt_memset(timer, 0, sizeof(struct rt_ktime_hrtimer));
/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->flag = flag & ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt;
rt_list_init(&(timer->row));
rt_strncpy(timer->name, name, RT_NAME_MAX - 1);
rt_list_init(&(timer->node));
rt_completion_init(&timer->completion);
}
rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer)
rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long delay_cnt)
{
rt_list_t *timer_list;
rt_base_t level;
/* parameter check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(delay_cnt < (_HRTIMER_MAX_CNT / 2));
timer->delay_cnt = delay_cnt;
timer->timeout_cnt = timer->delay_cnt + rt_ktime_cputimer_getcnt();
level = rt_spin_lock_irqsave(&_spinlock);
rt_list_remove(&timer->row); /* remove timer from list */
/* change status of timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer_list = &_timer_list;
for (; timer_list != _timer_list.prev; timer_list = timer_list->next)
{
rt_ktime_hrtimer_t t;
rt_list_t *p = timer_list->next;
t = rt_list_entry(p, struct rt_ktime_hrtimer, row);
if ((t->timeout_cnt - timer->timeout_cnt) == 0)
if (timer->flag & RT_TIMER_FLAG_ACTIVATED)
{
continue;
rt_spin_unlock_irqrestore(&_spinlock, level);
return -RT_ERROR;
}
else if ((t->timeout_cnt - timer->timeout_cnt) < (_HRTIMER_MAX_CNT / 2))
{
break;
}
}
rt_list_insert_after(timer_list, &(timer->row));
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
_set_next_timeout_n_unlock(level);
_insert_timer_to_list_locked(timer);
_set_next_timeout_locked();
rt_spin_unlock_irqrestore(&_spinlock, level);
return RT_EOK;
}
@ -235,16 +226,18 @@ rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer)
RT_ASSERT(timer != RT_NULL); /* timer check */
level = rt_spin_lock_irqsave(&_spinlock);
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
if (!(timer->flag & RT_TIMER_FLAG_ACTIVATED))
{
rt_spin_unlock_irqrestore(&_spinlock, level);
return -RT_ERROR;
}
_nowtimer = RT_NULL;
rt_list_remove(&timer->row);
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; /* change status */
_set_next_timeout_n_unlock(level);
rt_list_remove(&timer->node);
timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
_set_next_timeout_locked();
rt_spin_unlock_irqrestore(&_spinlock, level);
return RT_EOK;
}
@ -259,26 +252,27 @@ rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg)
level = rt_spin_lock_irqsave(&_spinlock);
switch (cmd)
{
case RT_TIMER_CTRL_GET_TIME:
*(unsigned long *)arg = timer->init_cnt;
*(unsigned long *)arg = timer->delay_cnt;
break;
case RT_TIMER_CTRL_SET_TIME:
RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2));
timer->init_cnt = *(unsigned long *)arg;
timer->delay_cnt = *(unsigned long *)arg;
timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt();
break;
case RT_TIMER_CTRL_SET_ONESHOT:
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
timer->flag &= ~RT_TIMER_FLAG_PERIODIC;
break;
case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
timer->flag |= RT_TIMER_FLAG_PERIODIC;
break;
case RT_TIMER_CTRL_GET_STATE:
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
if (timer->flag & RT_TIMER_FLAG_ACTIVATED)
{
/*timer is start and run*/
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
@ -330,18 +324,15 @@ rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer)
level = rt_spin_lock_irqsave(&_spinlock);
/* stop timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->flag &= ~RT_TIMER_FLAG_ACTIVATED;
/* when interrupted */
if (timer->error == -RT_EINTR || timer->error == RT_EINTR)
{
_nowtimer = RT_NULL;
rt_list_remove(&timer->row);
_set_next_timeout_n_unlock(level);
rt_list_remove(&timer->node);
_set_next_timeout_locked();
}
else
{
rt_spin_unlock_irqrestore(&_spinlock, level);
}
return RT_EOK;
}
@ -350,7 +341,7 @@ rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer)
void rt_ktime_hrtimer_delay_init(struct rt_ktime_hrtimer *timer)
{
rt_ktime_hrtimer_init(timer, "hrtimer_sleep", 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
rt_ktime_hrtimer_init(timer, "hrtimer_sleep", RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
_sleep_timeout, timer);
}
@ -366,15 +357,15 @@ rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cn
if (cnt == 0)
return -RT_EINVAL;
timer->timeout_cnt = cnt + rt_ktime_cputimer_getcnt();
timer->init_cnt = cnt;
err = rt_ktime_hrtimer_start(timer, cnt);
if (err)
return err;
rt_ktime_hrtimer_start(timer); /* reset the timeout of thread timer and start it */
err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER,
RT_INTERRUPTIBLE);
rt_ktime_hrtimer_keep_errno(timer, err);
return RT_EOK;
return err;
}
rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns)

View File

@ -515,6 +515,7 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
#ifdef RT_USING_PIC_STATISTICS
struct timespec ts;
rt_ubase_t irq_time_ns;
rt_ubase_t current_irq_begin;
#endif
RT_ASSERT(pirq != RT_NULL);
@ -522,7 +523,7 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
#ifdef RT_USING_PIC_STATISTICS
rt_ktime_boottime_get_ns(&ts);
pirq->stat.current_irq_begin[rt_hw_cpu_id()] = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec;
current_irq_begin = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec;
#endif
handler_nodes = &pirq->isr.list;
@ -577,7 +578,7 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
#ifdef RT_USING_PIC_STATISTICS
rt_ktime_boottime_get_ns(&ts);
irq_time_ns = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec - pirq->stat.current_irq_begin[rt_hw_cpu_id()];
irq_time_ns = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec - current_irq_begin;
pirq->stat.sum_irq_time_ns += irq_time_ns;
if (irq_time_ns < pirq->stat.min_irq_time_ns || pirq->stat.min_irq_time_ns == 0)
{

View File

@ -881,8 +881,7 @@ static void rtthread_timer_wrapper(void *timerobj)
rt_ktime_cputimer_getres();
if (timer->reload)
{
rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
rt_ktime_hrtimer_start(&timer->hrtimer);
rt_ktime_hrtimer_start(&timer->hrtimer, timer->reload);
}
#ifdef RT_USING_SMART
/* this field is named as tid in musl */
@ -1003,7 +1002,7 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
timer->status = NOT_ACTIVE;
timer->clockid = clockid;
rt_ktime_hrtimer_init(&timer->hrtimer, timername, 0, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
rt_ktime_hrtimer_init(&timer->hrtimer, timername, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER,
rtthread_timer_wrapper, timer);
_timerid = resource_id_get(&id_timer);
@ -1226,8 +1225,7 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
else
rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_PERIODIC, RT_NULL);
rt_ktime_hrtimer_control(&timer->hrtimer, RT_TIMER_CTRL_SET_TIME, &(timer->reload));
rt_ktime_hrtimer_start(&timer->hrtimer);
rt_ktime_hrtimer_start(&timer->hrtimer, timer->reload);
return 0;
}