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

Open
wants to merge 3 commits into
base: master
from

Conversation

@toomasz
Copy link

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 left a comment

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Collaborator

earlephilhower commented Sep 20, 2018

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Collaborator

earlephilhower commented Sep 20, 2018

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Collaborator

devyte commented Sep 21, 2018

@toomasz

This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.