Skip to content

Add rt_scheduler_critical_switch_flag to reduce scheduling on UP and fix bug on rzn2l #10581

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

Merged
merged 2 commits into from
Aug 6, 2025

Conversation

PillarShi
Copy link
Contributor

@PillarShi PillarShi commented Aug 4, 2025

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

  1. rzn2l 中断重复调用rt_interrupt_enter以及rt_interrupt_leave
  2. rzn2l定时器驱动在默认直接编译运行是有问题的
  3. rzn2l无法开启高优化,工具生成的代码有问题
  4. 优化UP下的性能

你的解决方案是什么 (what is your solution)

  1. 删除重复
  2. 更改驱动
  3. 加volatile
  4. 加入rt_scheduler_critical_switch_flag,使单核和多核策略同步

更改前

no

更改后

2025-08-04_17-38

请提供验证的bsp和config (provide the config and bsp)

  • BSP: bsp\renesas\rzn2l_etherkit

]

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

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

  • 本拉取/合并请求是一个草稿版本 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:

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

@PillarShi PillarShi requested a review from Rbb666 as a code owner August 4, 2025 13:47
@CLAassistant
Copy link

CLAassistant commented Aug 4, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

github-actions bot commented Aug 4, 2025

📌 Code Review Assignment

🏷️ Tag: bsp_renesas

Reviewers: kurisaW

Changed Files (Click to expand)
  • bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h
  • bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c
  • bsp/renesas/rzn2l_etherkit/rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c

🏷️ Tag: kernel

Reviewers: GorrayLi ReviewSun hamburger-os lianux-mm wdfk-prog xu18838022837

Changed Files (Click to expand)
  • src/cpu_up.c
  • src/scheduler_up.c

📊 Current Review Status (Last Updated: 2025-08-06 15:24 CST)

  • GorrayLi Pending Review
  • ReviewSun Pending Review
  • hamburger-os Pending Review
  • kurisaW Pending Review
  • lianux-mm Pending Review
  • wdfk-prog Pending Review
  • xu18838022837 Pending Review

📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@github-actions github-actions bot added BSP Kernel PR has src relate code BSP: Renesas BSP related with Renesas labels Aug 4, 2025
@@ -685,9 +694,10 @@ void rt_exit_critical(void)
/* enable interrupt */
rt_hw_interrupt_enable(level);

if (rt_current_thread)
if (rt_scheduler_critical_switch_flag == 1)
Copy link
Member

Choose a reason for hiding this comment

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

rt_schedule里面开始就会进行是否锁调度器的判断,没看出来这么修改后如何提升性能的?
麻烦举例说明下,谢谢

Copy link
Member

Choose a reason for hiding this comment

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

更新:这个位置的修改是为了减少调度的次数吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是的,针对重复的调度进入,这是改动最小的方法吧,更改前后的对比可以看描述中的图(-O0时的数据)

@Rbb666
Copy link
Member

Rbb666 commented Aug 5, 2025

CLA assistant check Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.You have signed the CLA already but the status is still pending? Let us recheck it.

麻烦签署下CLA,PR的标题也修改下吧,可以说明清晰点解决了什么问题

@PillarShi PillarShi changed the title Pillar rzn2l Add rt_scheduler_critical_switch_flag to reduce scheduling on UP and fix bug on rzn2l Aug 5, 2025
@Rbb666 Rbb666 requested a review from Copilot August 5, 2025 10:53
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a critical switch flag mechanism to optimize scheduler performance on single-core systems and fixes multiple issues in the rzn2l BSP, including duplicate interrupt handling and timer driver problems.

  • Introduces rt_scheduler_critical_switch_flag to track deferred scheduling needs and optimize performance on UP systems
  • Removes duplicate rt_interrupt_enter/leave calls in rzn2l timer callbacks that were causing issues
  • Fixes timer configuration and adds volatile keyword to prevent optimization issues

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/scheduler_up.c Adds critical switch flag mechanism to defer scheduling until safe to execute
src/cpu_up.c Updates documentation to reflect deferred scheduling behavior
bsp/renesas/rzn2l_etherkit/rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c Adds volatile keyword to counter variables to prevent optimization issues
bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c Removes duplicate interrupt handling for rzn2l and adds missing prescaler values
bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h Increases prescaler array size to accommodate new values
Comments suppressed due to low confidence (2)

src/scheduler_up.c:53

  • [nitpick] The variable name rt_scheduler_critical_switch_flag is overly verbose. Consider a shorter, clearer name like rt_sched_deferred_flag or rt_critical_sched_flag.
static rt_int8_t rt_scheduler_critical_switch_flag = -1;

bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c:22

  • The array size increase from 8 to 9 in timer_config.h doesn't match the addition of only 2 new prescaler values (lines 41-42). This could lead to array bounds issues or uninitialized elements.
#ifdef BSP_USING_TIM0

@@ -49,6 +50,7 @@ rt_uint8_t rt_thread_ready_table[32];

extern volatile rt_atomic_t rt_interrupt_nest;
static rt_int16_t rt_scheduler_lock_nest;
static rt_int8_t rt_scheduler_critical_switch_flag = -1;
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

The magic number -1 for initialization is unclear. Consider using a named constant or enum to make the different states more explicit (e.g., SCHED_FLAG_UNINITIALIZED = -1, SCHED_FLAG_IDLE = 0, SCHED_FLAG_PENDING = 1).

Suggested change
static rt_int8_t rt_scheduler_critical_switch_flag = -1;
static rt_int8_t rt_scheduler_critical_switch_flag = SCHED_FLAG_UNINITIALIZED;

Copilot uses AI. Check for mistakes.

@@ -685,9 +694,10 @@ void rt_exit_critical(void)
/* enable interrupt */
rt_hw_interrupt_enable(level);

if (rt_current_thread)
if (rt_scheduler_critical_switch_flag == 1)
Copy link
Preview

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

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

The magic number 1 should be replaced with a named constant to improve code clarity and maintainability.

Suggested change
if (rt_scheduler_critical_switch_flag == 1)
if (rt_scheduler_critical_switch_flag == RT_SCHEDULER_CRITICAL_SWITCH_PENDING)

Copilot uses AI. Check for mistakes.

@PillarShi
Copy link
Contributor Author

AI审查真棒啊,那需要这样改吗,MP那里也是单纯的使用数字,两边同步个宏?

我想的是减少影响范围和差异,因此也直接使用数字

@Rbb666
Copy link
Member

Rbb666 commented Aug 5, 2025

AI审查真棒啊,那需要这样改吗,MP那里也是单纯的使用数字,两边同步个宏?

我想的是减少影响范围和差异,因此也直接使用数字

感觉宏好一些

@ReviewSun
Copy link
Contributor

请修改数字标志位为,方便管理、裁剪。

@ReviewSun
Copy link
Contributor

麻烦将commit修改为1个

@PillarShi
Copy link
Contributor Author

更新:

  1. 添加标志位宏
  2. 合并为一个commit

@@ -184,8 +186,10 @@ static void timer_one_shot_check(void)
#ifdef BSP_USING_TIM0
void timer0_callback(timer_callback_args_t *p_args)
{
#if !defined(SOC_SERIES_R9A07G0)
Copy link
Member

@kurisaW kurisaW Aug 6, 2025

Choose a reason for hiding this comment

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

我觉得如果你想在timer中断回调里去掉RTT的中断嵌套计数,以此来提升程序的性能,在自己的项目中应用就好了,但是这部分代码就不要提到主线了,而且你说的“重复调用rt_interrupt_enter以及rt_interrupt_leave”,没理解你意思

Copy link
Member

Choose a reason for hiding this comment

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

是因为在libc里面会隐式调用一次:

BL rt_interrupt_enter

Copy link
Member

Choose a reason for hiding this comment

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

那是不是建议直接修改:
rt-thread/libcpu/arm/cortex-r52/context_gcc.S

那上下文切换汇编里就不考虑此操作了,与其他libcpu统一一下规范,这么看也挺别扭的

@@ -49,6 +50,7 @@ rt_uint8_t rt_thread_ready_table[32];

extern volatile rt_atomic_t rt_interrupt_nest;
static rt_int16_t rt_scheduler_lock_nest;
static rt_int8_t rt_scheduler_critical_switch_flag = -1;
Copy link
Member

Choose a reason for hiding this comment

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

这块推荐这种写法,尽量不要使用魔法数字,语义不直观:

#define IS_CRITICAL_SWITCH_PEND(pcpu, curthr) (RT_SCHED_CTX(curthr).critical_switch_flag)

@@ -184,8 +186,10 @@ static void timer_one_shot_check(void)
#ifdef BSP_USING_TIM0
void timer0_callback(timer_callback_args_t *p_args)
{
#if !defined(SOC_SERIES_R9A07G0)
Copy link
Member

Choose a reason for hiding this comment

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

是因为在libc里面会隐式调用一次:

BL rt_interrupt_enter

@Rbb666
Copy link
Member

Rbb666 commented Aug 6, 2025

麻烦将commit修改为1个

多个commit也没啥问题,只要不冲突&每个commit都说明提交的内容即可

@Rbb666
Copy link
Member

Rbb666 commented Aug 6, 2025

那是不是建议直接修改: rt-thread/libcpu/arm/cortex-r52/context_gcc.S

那上下文切换汇编里就不考虑此操作了,与其他libcpu统一一下规范,这么看也挺别扭的

感觉问题不大,rv里面也是这么干的,rt_interrupt_enter 作用是为了让内核判断是否在中断中。
@PillarShi 问下作者,这个PR的主要优化点是在加入rt_scheduler_critical_switch_flag避免 rt_schedule 多次调用吧?

@PillarShi
Copy link
Contributor Author

那我归总一下吧。当前pr的关键点:

  1. 避免 rt_schedule 多次调用
  2. 修改定时器驱动上的bug

这两个明显影响使用,至于rt_interrupt_enter以及rt_interrupt_leave重复不在这个pr或者主线上更改。

我一会再调整一下:

  1. 2个关键点还是分开commit,方便以后review
  2. 调整魔数,用更直观的宏
  3. rt_interrupt_enter以及rt_interrupt_leave先还原不改动

@Rbb666 Rbb666 merged commit b084503 into RT-Thread:master Aug 6, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BSP: Renesas BSP related with Renesas BSP Kernel PR has src relate code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants