Skip to content

sched/critmonitor: Fix context switch state tracking - #20066

Open
yushuailong wants to merge 4 commits into
apache:masterfrom
yushuailong:fix/critmonitor-state-tracking
Open

sched/critmonitor: Fix context switch state tracking#20066
yushuailong wants to merge 4 commits into
apache:masterfrom
yushuailong:fix/critmonitor-state-tracking

Conversation

@yushuailong

Copy link
Copy Markdown
Contributor

Summary

Commit b2a69ba merged the critical monitor suspend and resume paths into nxsched_switch_critmon(), but introduced two state tracking problems:

  • It reintroduced the obsolete premp_start field name. The field was previously renamed to preemp_start, causing configurations with preemption monitoring enabled to fail to build.
  • It assigned the current timestamp to to->run_time instead of to->run_start. This overwrote the accumulated runtime and left a stale start timestamp for the next context-switch calculation when CONFIG_SCHED_CPULOAD_CRITMONITOR was disabled.

This PR corrects the preemption field reference, restores the target thread's run_start timestamp, and updates the GDB TCB protocol annotations to match the current struct tcb_s fields.

Impact

Bug fix only; no new features.

Testing

Host: macOS 25.6 (arm64), Apple clang version 21.0.0
Target: sim:ostest

Build: clean, no warnings/errors introduced by this change.

ostest run (5 loops, CONFIG_TESTING_OSTEST_LOOPS=5, auto poweroff):

$ ./nuttx
stdio_test: write fd=1
stdio_test: Standard I/O Check: printf
stdio_test: write fd=2
stdio_test: Standard I/O Check: fprintf to stderr
ostest_main: putenv(Variable1=BadValue3)
ostest_main: setenv(Variable1, GoodValue1, TRUE)
ostest_main: setenv(Variable2, BadValue1, FALSE)
ostest_main: setenv(Variable2, GoodValue2, TRUE)
ostest_main: setenv(Variable3, GoodValue3, FALSE)
ostest_main: setenv(Variable3, BadValue2, FALSE)
show_variable: Variable=Variable1 has value=GoodValue1
show_variable: Variable=Variable2 has value=GoodValue2
show_variable: Variable=Variable3 has value=GoodValue3
ostest_main: Started user_main at PID=5
...
Final memory usage:
VARIABLE  BEFORE   AFTER
======== ======== ========
arena     4000000  4000000
ordblks         2        7
mxordblk  3faaaa0  3faaaa0
uordblks    452e0    457c0
fordblks  3fbad20  3fba840
user_main: Exiting
ostest_main: Exiting with status 0

Full log: 52364 lines, no FAILED / ASSERT / PANIC; final heap statistics
match the baseline (no leaks introduced).

Add the blank lines required between local declarations and statements so sched_critmonitor.c passes nxstyle.

Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
The premp_start member was renamed to preemp_start, but the old name was reintroduced when the critical monitor switch paths were merged.

Use the current struct tcb_s field name so configurations with preemption monitoring enabled build successfully.

Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
The struct tcb_s premp_* members were renamed to preemp_*, but the GDB TCB protocol retained the old annotations.

Update the annotations to match the current structure field names.

Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
The context-switch merge assigned the current timestamp to run_time instead of run_start. This overwrote the accumulated runtime and left the next elapsed-time calculation with a stale start value when critical-monitor CPU load accounting was disabled.

Store the timestamp in run_start under the thread runtime monitor configuration, matching the former resume path.

Fixes: b2a69ba ("sched: merge nxsched_suspend/resume_critmon")
Assisted-by: OpenAI Codex
Signed-off-by: yushuailong <yyyusl@qq.com>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

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

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant