rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

[schedule] reduce unnecessary schedule by comparing new ready thread's priority and current

Open blta opened this issue 3 years ago • 0 comments

[ 当前因为超时,或者资源唤醒就会主动发起一次rt_schedule, 但有时新ready的任务的优先级并没有当前的高 这样就会进入rt_schedule: 关中断,获取highest_ready_priority,rt_list_entry得到highest_priority_thread,再一堆比较 一顿操作最后发现做了无用功,降低了系统的实时性和效率。

新的优化策略是修改rt_thread_resume: 恢复thread状态后,再比较优先级大小:

rt_err_t rt_thread_resume(rt_thread_t thread)
{
    rt_base_t level;
    rt_err_t result = RT_EOK;
    extern rt_uint8_t rt_current_priority;

     .........

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    /* remove from suspend list */
    rt_list_remove(&(thread->tlist));

    rt_timer_stop(&thread->thread_timer);

    /* insert to schedule ready list */
    rt_schedule_insert_thread(thread);

    /* compare the priority with rt_current_priority*/
    if(thread->current_priority < rt_current_priority)
    {
        result = RT_EOK;
    }
    else
    {
        result = RT_EBUSY;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
    return result;
}

外面的timer_check 和 资源类 api根据rt_thread_resume返回结果,决定是否有必要发起一次rt_schedule 这个和freertos的策略一致

					/* If there was a task waiting for data to arrive on the
					queue then unblock it now. */
					if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
					{
						if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
						{
							/* The unblocked task has a priority higher than
							our own so yield immediately.  Yes it is ok to do
							this from within the critical section - the kernel
							takes care of that. */
							queueYIELD_IF_USING_PREEMPTION();
						}
						else
						{
							mtCOVERAGE_TEST_MARKER();
						}
					}

初始版本使用的是rt_schedule_insert_thread返回值,虽然不需要extern rt_uint8_t rt_current_priority,但改动太多,放弃了

测试 已在stm32f4disc1上测试,目前正常 还需要测试更多的ipc api 和其他改动验证

先提上来看看有没有问题!

]

以下的内容不应该在提交PR时的message修改,修改下述message,PR会被直接关闭。请在提交PR后,浏览器查看PR并对以下检查项逐项check,没问题后逐条在页面上打钩。 The following content must not be changed in the submitted PR message. Otherwise, the PR will be closed immediately. After submitted PR, please use a web browser to visit PR, and check items one by one, and ticked them if no problem.

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • [x] 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • [ ] 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • [x] 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • [x] 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • [x] 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • [x] 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • [x] 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • [x] 本拉取/合并请求代码是高质量的 Code in this PR is of high quality
  • [x] 本拉取/合并使用formatting等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification

blta avatar May 17 '22 09:05 blta