-
Notifications
You must be signed in to change notification settings - Fork 8.2k
subsys: samples: doc: arch: Make perf subsystem #72890
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
nashif
merged 7 commits into
zephyrproject-rtos:main
from
KushnerovMikhail:add_perf_x86_riscv
Aug 13, 2024
+1,343
−7
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
611ddae
profiling: Add perf tool
13156ad
arch: riscv: Implement perf stack thrace func
7343350
arch: x86: intel64: make perf stack thrace func
9136472
arch: x86: ia32: Implement perf stack thrace func
6654609
scripts: profiling: Add stackcollapse script
c8b2389
doc: profiling: Add doc for perf tool
bc9b550
samples: profiling: Add sample for perf tool
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright (c) 2023 KNS Group LLC (YADRO) | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <zephyr/kernel.h> | ||
|
|
||
| static bool valid_stack(uintptr_t addr, k_tid_t current) | ||
| { | ||
| return current->stack_info.start <= addr && | ||
| addr < current->stack_info.start + current->stack_info.size; | ||
| } | ||
|
|
||
| /* | ||
| * This function use frame pointers to unwind stack and get trace of return addresses. | ||
| * Return addresses are translated in corresponding function's names using .elf file. | ||
| * So we get function call trace | ||
| */ | ||
| size_t arch_perf_current_stack_trace(uintptr_t *buf, size_t size) | ||
| { | ||
| if (size < 2U) | ||
| return 0; | ||
|
|
||
| size_t idx = 0; | ||
|
|
||
| /* | ||
| * In riscv (arch/riscv/core/isr.S) ra, ip($mepc) and fp($s0) are saved | ||
| * at the beginning of _isr_wrapper in order, specified by z_arch_esf_t. | ||
| * Then, before calling interruption handler, core switch $sp to | ||
| * _current_cpu->irq_stack and save $sp with offset -16 on irq stack | ||
| * | ||
| * The following lines lines do the reverse things to get ra, ip anf fp | ||
| * from thread stack | ||
| */ | ||
| const struct arch_esf * const esf = | ||
| *((struct arch_esf **)(((uintptr_t)_current_cpu->irq_stack) - 16)); | ||
|
|
||
| /* | ||
| * $s0 is used as frame pointer. | ||
| * | ||
| * stack frame in memory (commonly): | ||
| * (addresses growth up) | ||
| * .... | ||
| * [-] <- $fp($s0) (curr) | ||
| * $ra | ||
| * $fp($s0) (next) | ||
| * .... | ||
| * | ||
| * If function do not call any other function, compiller may not save $ra, | ||
| * then stack frame will be: | ||
| * .... | ||
| * [-] <- $fp($s0) (curr) | ||
| * $fp($s0) (next) | ||
| * .... | ||
| * | ||
| */ | ||
| void **fp = (void **)esf->s0; | ||
| void **new_fp = (void **)fp[-1]; | ||
|
|
||
| buf[idx++] = (uintptr_t)esf->mepc; | ||
|
|
||
| /* | ||
| * During function prologue and epilogue fp is equal to fp of | ||
| * previous function stack frame, it looks like second function | ||
| * from top is missed. | ||
| * So saving $ra will help in case when irq occurred in | ||
| * function prologue or epilogue. | ||
| */ | ||
| buf[idx++] = (uintptr_t)esf->ra; | ||
| if (valid_stack((uintptr_t)new_fp, _current)) { | ||
| fp = new_fp; | ||
| } | ||
| while (valid_stack((uintptr_t)fp, _current)) { | ||
| if (idx >= size) | ||
| return 0; | ||
|
|
||
| buf[idx++] = (uintptr_t)fp[-1]; | ||
| new_fp = (void **)fp[-2]; | ||
|
|
||
| /* | ||
| * anti-infinity-loop if | ||
| * new_fp can't be smaller than fp, cause the stack is growing down | ||
| * and trace moves deeper into the stack | ||
| */ | ||
| if (new_fp <= fp) { | ||
| break; | ||
| } | ||
| fp = new_fp; | ||
| } | ||
|
|
||
| return idx; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,11 +112,12 @@ SECTION_FUNC(PINNED_TEXT, _interrupt_enter) | |
| * EAX = isr_param, EDX = isr | ||
| */ | ||
|
|
||
| /* Push EDI as we will use it for scratch space. | ||
| /* Push EBP as we will use it for scratch space. | ||
|
||
| * Also it helps in stack unwinding | ||
| * Rest of the callee-saved regs get saved by invocation of C | ||
| * functions (isr handler, arch_swap(), etc) | ||
| */ | ||
| pushl %edi | ||
| pushl %ebp | ||
|
|
||
| /* load %ecx with &_kernel */ | ||
|
|
||
|
|
@@ -131,17 +132,17 @@ SECTION_FUNC(PINNED_TEXT, _interrupt_enter) | |
| jne alreadyOnIntStack | ||
|
|
||
| /* | ||
| * switch to base of the interrupt stack: save esp in edi, then load | ||
| * switch to base of the interrupt stack: save esp in ebp, then load | ||
| * irq_stack pointer | ||
| */ | ||
|
|
||
| movl %esp, %edi | ||
| movl %esp, %ebp | ||
| movl _kernel_offset_to_irq_stack(%ecx), %esp | ||
|
|
||
|
|
||
| /* save thread's stack pointer onto base of interrupt stack */ | ||
|
|
||
| pushl %edi /* Save stack pointer */ | ||
| pushl %ebp /* Save stack pointer */ | ||
|
|
||
| #ifdef CONFIG_PM | ||
| cmpl $0, _kernel_offset_to_idle(%ecx) | ||
|
|
@@ -265,7 +266,7 @@ alreadyOnIntStack: | |
| #endif /* CONFIG_LAZY_FPU_SHARING */ | ||
|
|
||
| /* Restore volatile registers and return to the interrupted thread */ | ||
| popl %edi | ||
| popl %ebp | ||
| popl %ecx | ||
| popl %edx | ||
| popl %eax | ||
|
|
@@ -298,7 +299,7 @@ noReschedule: | |
| */ | ||
|
|
||
| nestedInterrupt: | ||
| popl %edi | ||
| popl %ebp | ||
| popl %ecx /* pop volatile registers in reverse order */ | ||
| popl %edx | ||
| popl %eax | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright (c) 2023 KNS Group LLC (YADRO) | ||
| * Copyright (c) 2020 Yonatan Goldschmidt <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <zephyr/kernel.h> | ||
|
|
||
| static bool valid_stack(uintptr_t addr, k_tid_t current) | ||
| { | ||
| return current->stack_info.start <= addr && | ||
| addr < current->stack_info.start + current->stack_info.size; | ||
| } | ||
|
|
||
| /* interruption stack frame */ | ||
| struct isf { | ||
| uint32_t ebp; | ||
| uint32_t ecx; | ||
| uint32_t edx; | ||
| uint32_t eax; | ||
| uint32_t eip; | ||
| }; | ||
|
|
||
| /* | ||
| * This function use frame pointers to unwind stack and get trace of return addresses. | ||
| * Return addresses are translated in corresponding function's names using .elf file. | ||
| * So we get function call trace | ||
| */ | ||
| size_t arch_perf_current_stack_trace(uintptr_t *buf, size_t size) | ||
| { | ||
| if (size < 1U) | ||
| return 0; | ||
|
|
||
| size_t idx = 0; | ||
|
|
||
| const struct isf * const isf = | ||
| *((struct isf **)(((void **)_current_cpu->irq_stack)-1)); | ||
| /* | ||
| * In x86 (arch/x86/core/ia32/intstub.S) %eip and %ebp | ||
| * are saved at the beginning of _interrupt_enter in order, that described | ||
| * in struct esf. Core switch %esp to | ||
| * _current_cpu->irq_stack and push %esp on irq stack | ||
| * | ||
| * The following lines lines do the reverse things to get %eip and %ebp | ||
| * from thread stack | ||
| */ | ||
| void **fp = (void **)isf->ebp; | ||
|
|
||
| /* | ||
| * %ebp is frame pointer. | ||
| * | ||
| * stack frame in memory: | ||
| * (addresses growth up) | ||
| * .... | ||
| * ra | ||
| * %ebp (next) <- %ebp (curr) | ||
| * .... | ||
| */ | ||
|
|
||
| buf[idx++] = (uintptr_t)isf->eip; | ||
| while (valid_stack((uintptr_t)fp, _current)) { | ||
| if (idx >= size) | ||
| return 0; | ||
|
|
||
| buf[idx++] = (uintptr_t)fp[1]; | ||
| void **new_fp = (void **)fp[0]; | ||
|
|
||
| /* | ||
| * anti-infinity-loop if | ||
| * new_fp can't be smaller than fp, cause the stack is growing down | ||
| * and trace moves deeper into the stack | ||
| */ | ||
| if (new_fp <= fp) { | ||
| break; | ||
| } | ||
| fp = new_fp; | ||
| } | ||
|
|
||
| return idx; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright (c) 2023 KNS Group LLC (YADRO) | ||
| * Copyright (c) 2020 Yonatan Goldschmidt <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <zephyr/kernel.h> | ||
|
|
||
| static bool valid_stack(uintptr_t addr, k_tid_t current) | ||
| { | ||
| return current->stack_info.start <= addr && | ||
| addr < current->stack_info.start + current->stack_info.size; | ||
| } | ||
|
|
||
| /* | ||
| * This function use frame pointers to unwind stack and get trace of return addresses. | ||
| * Return addresses are translated in corresponding function's names using .elf file. | ||
| * So we get function call trace | ||
| */ | ||
| size_t arch_perf_current_stack_trace(uintptr_t *buf, size_t size) | ||
| { | ||
| if (size < 1U) | ||
| return 0; | ||
|
|
||
| size_t idx = 0; | ||
|
|
||
| /* | ||
| * In x86_64 (arch/x86/core/intel64/locore.S) %rip and %rbp | ||
| * are always saved in _current->callee_saved before calling | ||
| * handler function if interrupt is not nested | ||
| * | ||
| * %rip points the location where interrupt was occurred | ||
| */ | ||
| buf[idx++] = (uintptr_t)_current->callee_saved.rip; | ||
| void **fp = (void **)_current->callee_saved.rbp; | ||
|
|
||
| /* | ||
| * %rbp is frame pointer. | ||
| * | ||
| * stack frame in memory: | ||
| * (addresses growth up) | ||
| * .... | ||
| * ra | ||
| * %rbp (next) <- %rbp (curr) | ||
| * .... | ||
| */ | ||
| while (valid_stack((uintptr_t)fp, _current)) { | ||
| if (idx >= size) | ||
| return 0; | ||
|
|
||
| buf[idx++] = (uintptr_t)fp[1]; | ||
| void **new_fp = (void **)fp[0]; | ||
|
|
||
| /* | ||
| * anti-infinity-loop if | ||
| * new_fp can't be smaller than fp, cause the stack is growing down | ||
| * and trace moves deeper into the stack | ||
| */ | ||
| if (new_fp <= fp) { | ||
| break; | ||
| } | ||
| fp = new_fp; | ||
| } | ||
|
|
||
| return idx; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| .. _profiling: | ||
|
|
||
| Profiling | ||
| ######### | ||
|
|
||
| Required Kconfig: :kconfig:option:`CONFIG_PROFILING` | ||
|
|
||
| .. toctree:: | ||
| :maxdepth: 1 | ||
|
|
||
| perf.rst |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance that this and https://github.com/zephyrproject-rtos/zephyr/blob/main/arch/riscv/core/stacktrace.c can share some of the unwinding logics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #73587