How do I measure stack length for cog 0?
dbpage
Posts: 217
in Propeller 1
I use the stack length object to measure the stack length of cogs that I launch. I would like to use the stack length object to measure how much free memory that cog 0 uses. It seems that by the time the stack length code executes, some free memory is already in use. I don't know where to point the stack length object. Should I just add code to look for the first unused portion of free memory and point the stack length object there? I'm not getting good results doing this. Any ideas?
Comments
If you try to use the stack length object on the main stack, you'll have to allow room above the stack pointer for the stack length object's local and temporary variables since it'll overwrite them during initialization otherwise. You'll have to limit the length of the stack area that's checked because the default stack runs to the end of memory and that would slow things down badly.
I'd add 64 to the address from WORD[ 10 ] before passing it to Init and limit the stack area tested to at most 128 longs more than you expect to use.
The bottom of cog 0's stack is at the boundary from yellow to blue. However, cog 0's stack pointer begins at the end of the stuff in free memory (that's what WORD[10] is). The stuff between the beginning of free/stack RAM and WORD[10] is a stack frame (the stuff that gets pushed when you call a method) so that if your main method returns, it returns to a function in ROM that does cogstop(cogid). However, as Mike Green said, you'll clobber the stack if you give it that pointer, so give it a pointer somewhat after that.
This pushes the return address and a long for the result variable and the local variable "aVariable", then returns the address of "aVariable" in the stack. Subtract the initial value of the stack pointer to get the amount of stack used including the 8 bytes for the return address and result variable.
This seems to work IF I put pst.dec(stacksize) in the BEST location.
I would prefer to go back after a period of time and determine how much stack space WAS used so I don't have to place these statements in the exact right method that uses the most stack space.
What do you think will work?
p.s. For those worried about other cogs, the stack length method works great. This is only about cog 0.
Help me understand word[10]. Let's say word[10] = $4848. That gives a stack/free space of ($7FFF - $4848)/4 = 3565 (decimal) longs. View Info displays 3,568 longs.
What's up with that?
($8000 - $4848) / 4 + 2= 3568
I wouldn't recommend using unless you want to play stack tricks. There are many better things you can do if you need more space, like BST's or OpenSpin's unused method removal features and manual optimization.
I am trying to write to Long[StackAddr] the same value that already exists at Long[StackAddr]. This causes irratic behavior.
Before the repeat statement, my intent is to avoid pushing values on the stack for method calling, parameter passing, and expression evaluation. I have used as many as 64 Long[StackAddr][i++] := Seed, but erratic behavior continues.
My goal is to better understand the Cog 0 stack.
What am I doing wrong?
then you know how much stack will be used.
alas you need a free cog
Mike
Doing that will clobber Start_up's stack. Try initializing Seed to at least word[10] + 24. That might not even be big enough - make it bigger if it doesn't work.
You can't not use the stack at all, even by avoiding function calls. The Spin Interpreter is a stack-based machine - all intermediate calculation results and local variables (including result, which you can't get rid of) live on the stack.
Try using the Stack Length object. All you should need to do is to offset the initial pointer by the smallest value that doesn't crash the program.
The stack length object is not designed for Cog 0. The stack length object works great for other cogs. There is no guidance about how to use the stack length object for Cog 0. This is where my investigation began.
I should have mentioned that if I omit the repeat statement, there is no issue. Everything works fine without the repeat statement.
There is something about the repeat statement that clobbers the system, even after 64 Long[StackAddr][i++] := Seed statements. Remove the repeat statement, and the system works as expected.
I like your idea. I will try that. I'm not sure it will help me understand what's going on with the real cog 0, but all I need is predictable and stable behavior.
Thanks.
If that doesn't crash it, what does stacklength.GetLength return?
Terminal output:
I find it interesting that @result is the same address as word[10]. What's up with that? Shouldn't word[10] allow space for @result?