Skip to content

Conversation

@hujun260
Copy link
Contributor

Summary

Move the static g_prof variable declaration to block scope within the profil()
function when CONFIG_SMP is disabled, to comply with MISRA C 2012 Rule 8.9 which
requires objects used by only one function to be declared within that function's scope.

Motivation and Problem

MISRA C 2012 Rule 8.9 enforces improved code locality by requiring that objects
with internal linkage be declared at the smallest possible scope. The original
code declared g_prof at file scope globally, even though it is only used within
the profil() function when CONFIG_SMP is not enabled. When CONFIG_SMP is enabled,
g_prof is used in SMP call data initialization, requiring file-scope visibility.
This change optimizes the scope based on build configuration, improving code clarity
and compliance with embedded systems coding standards.

Changes

  • Remove unconditional file-scope static struct profinfo_s g_prof declaration
  • Add conditional CONFIG_SMP block at file scope containing g_prof and g_call_data
  • Add local static struct profinfo_s g_prof declaration inside profil() function
    when CONFIG_SMP is disabled, restricting scope to only where it's used

Impact

  • MISRA Compliance: Achieves MISRA C 2012 Rule 8.9 compliance for scope management
  • Code Quality: Improves code locality and reduces global variable exposure
  • Maintainability: Clearer intent about variable usage within build configurations
  • Verifiability: Easier static analysis with properly scoped variables
  • Backward Compatibility: No functional changes; identical runtime behavior
  • Performance: No performance impact; scope changes are compile-time only

Verification

  • Code compiles without warnings on ARM GCC 10.x
  • Verified on QEMU ARMv7-A simulator with CONFIG_SMP disabled and enabled
  • Profiling functionality verified in both SMP and non-SMP configurations
  • Timer handler and profil function behavior unchanged
  • SMP-specific code paths verified in CONFIG_SMP builds
  • Static analysis shows improved compliance metrics

Testing

Tested with:

  • ARM GCC 10.x compiler
  • QEMU ARMv7-A simulation
  • Profiling scenarios:
    • Non-SMP configuration (g_prof in profil() function scope)
    • SMP configuration (g_prof at file scope with g_call_data)
    • Timer handler invocation and profiling data collection
    • Scale and offset parameter handling

Files Changed

  • sched/sched/sched_profil.c (6 lines: 4 insertions, 2 deletions)

Move static g_prof variable declaration into block scope for the profil()
function when CONFIG_SMP is disabled, following MISRA C 2012 Rule 8.9 which
requires objects used by only one function to be declared within that function's scope.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
@github-actions github-actions bot added Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small labels Jan 28, 2026
@xiaoxiang781216 xiaoxiang781216 merged commit c7bb7ec into apache:master Jan 28, 2026
40 checks passed
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: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants