hujun260 opened a new pull request, #18239:
URL: https://github.com/apache/nuttx/pull/18239
## Summary
Refactor the CPU load monitoring module to improve code quality by moving
the static `g_cpuload_wdog` watchdog timer from file scope to block scope
within the `cpuload_init()` function. This change addresses a MISRA C 2012 Rule
8.9 violation and enhances code maintainability through proper variable scope
discipline.
## Motivation and Problem
MISRA C 2012 Rule 8.9 states that "An object should be defined at block
scope if its identifier only appears in a single function." The static
`g_cpuload_wdog` watchdog structure was defined at file scope but is only used
within the `cpuload_init()` function, creating an unnecessary global scope
binding.
### Coding Standard Compliance
- **MISRA C 2012 Rule 8.9**: Ensures proper variable scope discipline for
embedded systems
- **Purpose**: Reduces the scope of static data to the minimum necessary
level
- **Benefit**: Improves code modularity, reduces potential variable
interactions
- **Safety**: Enhances code clarity and verifiability for safety-critical
systems
## Changes Made
1. **Removed**: Static `g_cpuload_wdog` declaration from file scope (lines
in Pre-processor Data section)
- Eliminates unnecessary global scope binding
- Reduces global namespace pollution
2. **Added**: Static `g_cpuload_wdog` declaration moved to block scope
- Declared within `cpuload_init()` function body
- Local scope limits visibility and potential misuse
- Still maintains static storage duration (initialized once)
3. **Preserved**: All functionality and behavior unchanged
- Watchdog timer initialization semantics identical
- Callback mechanism unchanged
- Timer period and expiration behavior preserved
### File Statistics
- **File Modified**: `sched/sched/sched_cpuload.c`
- **Lines Changed**: 6 (2 insertions, 4 deletions)
- **Type**: Code reorganization (no functional changes)
## Impact Analysis
✅ **Backward Compatibility**: MAINTAINED - Function behavior identical
✅ **Code Quality**: IMPROVED - Enhanced MISRA C 2012 compliance
✅ **Performance**: NO IMPACT - Static initialization still occurs once
✅ **Memory**: NO CHANGE - Variable location optimized but size identical
✅ **Scope Management**: IMPROVED - Proper variable scope discipline
## Verification Checklist
- [x] Compilation successful with all warning flags enabled
- [x] CPU load sampling continues normally after cpuload_init()
- [x] Watchdog timer correctly configured and started
- [x] Block-scope static variable initialized on first function call
- [x] No functional changes to cpuload_init() behavior
- [x] No impact on CPU load calculation logic
- [x] MISRA C 2012 Rule 8.9 compliance verified
- [x] Code review completed
## Testing Scenarios
1. **Initialization**: Verify cpuload_init() correctly initializes watchdog
2. **Watchdog Operation**: Confirm periodic cpuload_callback() execution
3. **CPU Load Calculation**: Verify correct CPU load values computed
4. **Multiple Calls**: Test cpuload_init() called multiple times (static
value reused)
5. **Integration**: Test CPU load statistics work correctly with system
operation
6. **Architecture Coverage**: Test on ARM platforms with/without
CONFIG_SCHED_CPULOAD_SYSCLK
## Technical Notes
- Static storage duration preserved - watchdog initialized once per program
lifetime
- Block-scope static variables have same lifetime as file-scope statics
- First call to cpuload_init() initializes the static watchdog
- Subsequent calls reuse the already-initialized watchdog
- No threading issues since watchdog configuration is stable
## Related Issues
- **Category**: Code Quality & Safety Standard Compliance
- **Standard**: MISRA C 2012 Rule 8.9 (Variable Scope)
- **Subsystem**: Kernel scheduler - CPU load monitoring (sched/sched/)
- **Configuration**: Requires CONFIG_SCHED_CPULOAD_SYSCLK enabled
## Build Information
- **Compiler**: ARM GCC 10.x (primary test environment)
- **Architectures**: ARMv7-A, ARMv7-R, x86_64
- **Target**: NuttX kernel - scheduler subsystem (sched/)
- **Build Flags**: `-Wall -Wextra -Werror`
- **Configuration**: CONFIG_SCHED_CPULOAD_SYSCLK
## Files Changed
--
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]