Skip to content
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

implement EspClass::getFreeSysStack method #5153

Closed
wants to merge 3 commits into from

Conversation

toomasz
Copy link

@toomasz toomasz commented Sep 19, 2018

Not sure if implemetation is correct.

Did simple test:


void StackTest()
{
	char cc[100] = "sdfsdfsfsf";
	auto freeSysStack1 = ESP.getFreeSysStack();
	Serial.printf("free sys stack in submethod: %d\n", freeSysStack1);
	Serial.println(cc);
}

void onWifiConnect(const WiFiEventStationModeGotIP& event) 
{
	auto freeSysStack = ESP.getFreeSysStack();
	Serial.printf("free sys stack: %d\n", freeSysStack);
	StackTest();

	Serial.println("Connected to Wi-Fi.");
	DebugFreeStack();
}

And output was:

Connecting to Wi-Fi...
free sys stack: 12000
free sys stack in submethod: 11872

See comments in #5148

Copy link
Collaborator

@devyte devyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calculation gets the current stack usage at the time of call.
The getFreeContStack() gets the maximum stack usage from boot time by checking the stack watermark. The value returned can never decrease over time.
The meanings are fundamentally diifferent.

I would suggest figuring out a way to get the max sys stack usage in the same way that the max cont stack usage is done, because that is most useful, and can detect if a stack overflow has happened in the past.

@toomasz
Copy link
Author

toomasz commented Sep 19, 2018

I didn't realize getFreeContStack works that way. For now have two problems with implementing same approach for sys stack.

  • Don't know region of sys stack. Need this to fill it with magic pattern. @earlephilhower said is starts 0x3FFFC000 but what would be the stack size?
  • No idea where to put code that fills SYS stack with watermark pattern. Is that even possible?

BTW, whats the meaning of cont abbreviation?

@earlephilhower
Copy link
Collaborator

Actually, there's a new API that makes more sense than trying to find the high water mark. Look at #5133 where the "get free stack" is added to the ESP class. You can't really fill the OS stack unless you patch the startup code, which is probably not worth the effort.

@devyte
Copy link
Collaborator

devyte commented Sep 20, 2018

@earlephilhower #5133 is a thin wrapper around cont_get_free_stack(), which looks at the cont stack watermark. The cont stack is filled with a guard value on init, and usage overwrites those values. The watermark is checked by starting at the end and counting the guard values while moving inwards. My previous effort is aimed at doing something similar for the sys stack.
About where to init, we can do it in a naive place as a first approach, given that what we really care about is the stack usage during the application callbacks. We can figure out an earlier place later.
About how to init, I'd calculate the current stack usage with the approach given here, then I'd allocate an array of size with that value, then I'd fill that array with the guard value. That's just an abstract, you could do the same with just a pointer startingto at the end and moving inwards while assigning to it.
About how to calculatenthe free stack, I'd do the same thing as with the cont stack: start at the end and count guard values moving inwards.

@earlephilhower
Copy link
Collaborator

Did not see that, @devyte . While I can see its use, from my own experience the answer to the question "How much stack do I have now?" is much more interesting and given the name of the ESP function that's actually what I would expect...

For sanity this should match the existing behavior, though. There is no task stack structure on startup, so @toomasz would need to find the current stack and then somehow safely fill from end-of-stack to a1 with the guard bytes...

@devyte
Copy link
Collaborator

devyte commented Sep 20, 2018

Thinking back, I've actually found the need for both. Maybe the current method in #5133 should be renamed to getMaxContStackUse(), then a new method should be implemented for the current use, and then do the same for the sys stack. What do you think?

@d-a-v
Copy link
Collaborator

d-a-v commented Sep 20, 2018

Cont stack is initialized here (within sys stack) or here (within user ram).
This might also help understanding addresses (link)

@d-a-v
Copy link
Collaborator

d-a-v commented Sep 20, 2018

We may have both views:
how much do we have left right now,
and how far/deep did my app go inside stack until now.

@devyte
Copy link
Collaborator

devyte commented Sep 21, 2018

Reference: sys stack area and painting by @eriksl
From that same discussion, this commit with painting code from @eriksl 's universal io bridge repo.

@toomasz
Copy link
Author

toomasz commented Sep 22, 2018

I think we should have 4 methods then:
ESP.getMaxContStackUse() - rename method from #5133
ESP.getMaxSysStackUse() - to implement, tricky

ESP.getFreeContStack() - to implement, easy
ESP.getFreeSysStack() - method from this PR

@earlephilhower earlephilhower added the merge-conflict PR has a merge conflict that needs manual correction label Dec 10, 2019
@toomasz toomasz closed this Jan 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-conflict PR has a merge conflict that needs manual correction
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants