Skip to content

Commit 2074c00

Browse files
kernel: speed up z_stack_space_get
The z_stack_space_get call currently checks for free space at the top of the stack by checking each byte individually. This can introduce significant runtime overhead for threads which have large, mostly unused stacks. This change updates the check to first count the free space by word, and then check the sub-word unused bytes. Signed-off-by: Félix Turgeon <[email protected]>
1 parent 3d4f83a commit 2074c00

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

kernel/thread.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,26 @@ int z_stack_space_get(const uint8_t *stack_start, size_t size, size_t *unused_pt
853853
size -= 4;
854854
}
855855

856-
for (size_t i = 0; i < size; i++) {
856+
/* align to word boundary */
857+
const unsigned long *checked_stack_word =
858+
(const unsigned long *)ROUND_UP(checked_stack, sizeof(unsigned long));
859+
size -= ((const uint8_t *)checked_stack_word - checked_stack);
860+
861+
/* compare using native machine word size */
862+
size_t i;
863+
864+
for (i = 0; i < size / sizeof(unsigned long); i++) {
865+
if (checked_stack_word[i] != (unsigned long)0xaaaaaaaaaaaaaaaa) {
866+
break;
867+
}
868+
}
869+
unused = i * sizeof(unsigned long);
870+
871+
/* Continue checking from last used word to find remaining unused bytes*/
872+
size -= unused;
873+
checked_stack = (const uint8_t *)checked_stack_word + unused;
874+
875+
for (i = 0; i < size; i++) {
857876
if ((checked_stack[i]) == 0xaaU) {
858877
unused++;
859878
} else {

0 commit comments

Comments
 (0)