Skip to content

Commit 60baf80

Browse files
committed
Add function to measure stack high water mark (thanks @g3gg0)
1 parent 66a88ac commit 60baf80

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

cores/esp8266/cont.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ void cont_yield(cont_t*);
6060
// return 1 if guard bytes were overwritten.
6161
int cont_check(cont_t* cont);
6262

63+
// Go through stack and check how many bytes are most probably still unchanged
64+
// and thus weren't used by the user code. i.e. that stack space is free. (high water mark)
65+
int cont_get_free_stack(cont_t* cont);
66+
6367
// Check if yield() may be called. Returns true if we are running inside
6468
// continuation stack
6569
bool cont_can_yield(cont_t* cont);

cores/esp8266/cont_util.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ void ICACHE_RAM_ATTR cont_init(cont_t* cont) {
3030
cont->stack_guard2 = CONT_STACKGUARD;
3131
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4);
3232
cont->struct_start = (unsigned*) cont;
33+
34+
// fill stack with magic values to check high water mark
35+
for(int pos = 0; pos < sizeof(cont->stack) / 4; pos++)
36+
{
37+
cont->stack[pos] = CONT_STACKGUARD;
38+
}
3339
}
3440

3541
int ICACHE_RAM_ATTR cont_check(cont_t* cont) {
@@ -38,6 +44,19 @@ int ICACHE_RAM_ATTR cont_check(cont_t* cont) {
3844
return 0;
3945
}
4046

47+
int ICACHE_RAM_ATTR cont_get_free_stack(cont_t* cont) {
48+
uint32_t *head = cont->stack;
49+
int freeWords = 0;
50+
51+
while(*head == CONT_STACKGUARD)
52+
{
53+
head++;
54+
freeWords++;
55+
}
56+
57+
return freeWords * 4;
58+
}
59+
4160
bool ICACHE_RAM_ATTR cont_can_yield(cont_t* cont) {
4261
return !ETS_INTR_WITHINISR() &&
4362
cont->pc_ret != 0 && cont->pc_yield == 0;

0 commit comments

Comments
 (0)