huojianchaoyy opened a new pull request, #21654:
URL: https://github.com/apache/nuttx/pull/21654

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   This PR updates the critical section monitor 
(`sched/sched/sched_critmonitor.c`) and the cpuload accounting built on it:
   
   - **sched: divide nxsched_switch_critmon into two steps: suspend and 
resume**: after suspend and resume were unified into 
`nxsched_switch_critmon()`, it is no longer possible to guarantee that both 
`from` and `to` are valid at the call point, which changed the semantics of 
`nxsched_switch_context()`. Split it back into `nxsched_suspend_critmon()` and 
`nxsched_resume_critmon()`, and update 
`Documentation/os/interrupts/critical_sections.rst`.
   - **sched: add running time record tracepoint**: new 
`CONFIG_SCHED_INSTRUMENTATION_THREADTIME` records a `NOTE_DUMP_THREADTIME` note 
with the run time of the thread that is switched out.
   - **note: add runtime record threshold control**: new 
`CONFIG_SCHED_INSTRUMENTATION_THREAD_RUNTIME_DURATION` (us) filters out runs 
shorter than the threshold.
   - **sched: thread time recording ignores idle threads**.
   - **sched: Optimize threadtime statistics performance**: compare against a 
threshold converted to perf counts once, and only divide when a note is 
recorded.
   - **sched: optimize critmon_cpuload performance**: with 
`CONFIG_SCHED_CPULOAD_CRITMONITOR`, account the cpuload in raw perf counts 
instead of dividing on every context switch. The decay threshold is converted 
to perf counts once.
   - **sched/critmonitor: fix cpuload tick unit in nxsched_update_critmon**: 
`nxsched_update_critmon()` also passes raw perf counts, so both paths use the 
same unit.
   - **sched: fix critmonitor print format error**: `clock_t` is 64-bit, so the 
`CHECK_*` messages print it with `%ju` and a `uintmax_t` cast instead of 
`PRIu32`.
   - **sysinfo: Modify the types of time-related variables to meet MISRA-C2012 
10.3.**: explicit casts in `sysinfo()`. The decayed cpuload total is 
accumulated in `clock_t`, as perf counts can exceed 32 bits.
   
   ## Impact
   
   - New Kconfig options, both off by default:
     - `SCHED_INSTRUMENTATION_THREADTIME`, depends on 
`SCHED_INSTRUMENTATION_DUMP` and `SCHED_CRITMONITOR`. It uses the thread run 
time, which is only tracked with `SCHED_CRITMONITOR_MAXTIME_THREAD >= 0`; with 
`-1` no note is recorded.
     - `SCHED_INSTRUMENTATION_THREAD_RUNTIME_DURATION`, default 0.
   - `include/nuttx/sched_note.h`: new note type `NOTE_DUMP_THREADTIME`, 
appended after `NOTE_DUMP_COUNTER` so existing values do not change, plus 
`struct note_threadtime_s` and `sched_note_threadtime()`. The noteram text dump 
does not decode the new type and skips it like other unknown types.
   - `nxsched_switch_critmon()` is replaced by `nxsched_suspend_critmon()` and 
`nxsched_resume_critmon()`. Both are internal to `sched/`; the only caller is 
`nxsched_switch_context()`.
   - With `CONFIG_SCHED_CPULOAD_CRITMONITOR`, the `total` and `active` fields 
of `struct cpuload_s` are in perf counts instead of system ticks. `ps`, 
`/proc/cpuload`, `/proc/<pid>/loadavg` and `sysinfo()` use the ratio, so the 
reported load is unchanged. Other cpuload backends are not affected.
   - Without `CONFIG_SCHED_CRITMONITOR`, the only changes are the casts in 
`sysinfo()` and the wider accumulator in the cpuload decay. No defconfig 
changes.
   
   ## Testing
   
   Host: Ubuntu 22.04.5 LTS x86_64, gcc 13.4.0, CMake 3.23 + Ninja.
   
   Every commit of the series was built in three configurations:
   
   - `sim:nsh` (UP) and `sim:smp` (4 CPUs), with:
     ```
     CONFIG_SCHED_CRITMONITOR=y
     CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD=2000000000
     CONFIG_SCHED_CRITMONITOR_MAXTIME_PREEMPTION=2000000000
     CONFIG_SCHED_CRITMONITOR_MAXTIME_CSECTION=2000000000
     CONFIG_SCHED_CRITMONITOR_MAXTIME_BUSYWAIT=2000000000  # sim:smp only
     CONFIG_SCHED_CPULOAD_CRITMONITOR=y
     CONFIG_SCHED_INSTRUMENTATION=y
     CONFIG_SCHED_INSTRUMENTATION_DUMP=y
     CONFIG_SCHED_INSTRUMENTATION_THREADTIME=y
     CONFIG_SCHED_INSTRUMENTATION_THREAD_RUNTIME_DURATION=100
     CONFIG_DRIVERS_NOTE=y
     CONFIG_DRIVERS_NOTERAM=y
     ```
   - `sim:nsh` with `CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD=-1` and 
`CONFIG_SCHED_INSTRUMENTATION_THREADTIME=y`.
   
   `./tools/checkpatch.sh -c -m -g <base>..HEAD` passes.
   
   ostest passes on the last commit, on both sim:nsh and sim:smp:
   
   <details>
   <summary>sim:nsh</summary>
   
   ```
   user_main: Exiting
   ostest_main: Exiting with status 0
   nsh> ps
     TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK        
    STACK    CPU COMMAND
       0     0     9   0 FIFO     Kthread   - Ready              
0000000000000000 0069584 99.9% Idle_Task
       1     0     9 224 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0067448  0.0% sim_loop_wq 0x73ae4be003f0 0x73ae4be00478
       2     0     9 224 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0067472  0.0% hpwork 0x40193800 0x40193888
       4     4     0 100 FIFO     Task      - Waiting  Semaphore 
0000000000000000 0067504  0.0% init_main
       7     7     4 100 FIFO     Task      - Running            
0000000000000000 0067496  0.0% sh
   nsh> cat /proc/cpuload
     0.1%
   ```
   </details>
   
   <details>
   <summary>sim:smp</summary>
   
   ```
   user_main: Exiting
   ostest_main: Exiting with status 0
   nsh> ps
     TID   PID  PPID CPU PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK    
        STACK    USED FILLED    CPU COMMAND
       0     0    10   0   0 FIFO     Kthread   - Assigned           
0000000000000000 0066512 0005544   8.3%  22.1% CPU0 IDLE
       1     0    10   1   0 FIFO     Kthread   - Running            
0000000000000000 0066504 0009448  14.2%  22.1% CPU1 IDLE
       2     0    10   2   0 FIFO     Kthread   - Running            
0000000000000000 0066512 0009736  14.6%  27.6% CPU2 IDLE
       3     0    10   3   0 FIFO     Kthread   - Running            
0000000000000000 0066504 0009944  14.9%  28.1% CPU3 IDLE
       4     0    10 --- 224 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0067448 0000840   1.2%   0.0% sim_loop_wq 0x731792a30fe0 
0x731792a31068
       5     0    10 --- 224 FIFO     Kthread   - Waiting  Semaphore 
0000000000000000 0067472 0001640   2.4%   0.0% hpwork 0x400a0080 0x400a0108
       7     7     0 --- 100 FIFO     Task      - Waiting  Semaphore 
0000000000000000 0067504 0002728   4.0%   0.0% init_main
       8     8     7   0 100 FIFO     Task      - Running            
0000000000000000 0067496 0004088   6.0%   0.0% sh
   nsh> cat /proc/cpuload
     0.0%
   ```
   </details>
   
   The threadtime note was checked under GDB on sim:nsh, with breakpoints on 
the threshold check in `nxsched_suspend_critmon()` and on 
`sched_note_event_ip()`. Only non-idle threads that ran longer than 100 us 
record a `NOTE_DUMP_THREADTIME` note (`elapsed` is in perf counts, ns on sim):
   
   ```
   suspend#1: pid=0 elapsed=2976489 threshold=100000 record=False
   suspend#2: pid=3 elapsed=497038 threshold=100000 record=True
     -> NOTE_DUMP_THREADTIME recorded, elapsed_us=497
   suspend#3: pid=1 elapsed=51160 threshold=100000 record=False
   suspend#4: pid=2 elapsed=4727 threshold=100000 record=False
   suspend#5: pid=4 elapsed=234640 threshold=100000 record=True
     -> NOTE_DUMP_THREADTIME recorded, elapsed_us=234
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to