Shop OBEX P1 Docs P2 Docs Learn Events
calculate needed stack space — Parallax Forums

calculate needed stack space

Bobb FwedBobb Fwed Posts: 1,119
edited 2008-10-03 22:02 in Propeller 1
Is there any way to calculate needed stack space for a cog? Or is it just trial and error? It seems (if the stack work the way I think), that most programs allocated to a cog would use different amounts at different times, thus just testing it to see if works may be fine only if you are able to try all contingencies, which may not always be possible. I've been on the err on the side of way too large, just to make sure any problems I have are not caused by these finite stacks. Am I missing something obvious?
It seems even the most simple methods allocated to a cog require at least 6 longs, but at what point do we know it needs 7? There are other objects that I've seen allot 100 or more longs to stack space, why was this chosen? I did a quick search on the forums and found little to know information (at least with the searches I tried) on how to determine what is needed. Any help would be nice.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-03 20:45
    This question has come up many times before.

    The amount of stack space needed for a cog depends on the structure of the program, on the way various methods call each other, and on the complexity of the various expressions and statements that make up the program. Expressions and statement require varying amounts of temporary storage. When one method calls another, the call requires a minimum of 6 longs plus whatever is needed for the parameters and local variable declarations plus any temporaries in use at the time (particularly if the method is called as a function).

    You can make a very rough estimate based on the deepest call nesting that you're aware of, but, if the cog calls another object, then you have to do the same analysis of the called object.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2008-10-03 21:20
    It almost seems that this could be calculated at compile time, with a worst case scenario given as the value. But seeing as there is nothing like that, I guess I will fumble around with it a bit more.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-10-03 22:02
    Hi Bobb,
    · It actually isn't possible to concretely determine how much stack space is needed since it would require a full simulation first. Recursion is permitted (though not recommended), for instance consider the following code:
    PUB factorial(value)
      if value < 2
        return 1
      else
        return value * factorial(value - 1)
    

    This computes the factorial of a number recursively, each time the·method is called, additional space on the stack is consumed. How much stack space is used is dependent on the value given to the method, but frequently this number isn't known until runtime. So it's not possible to calculate stack space at compile time without having to do a major amount simulation.

    One of our most active forum members Phil Pilgrim (PhiPi) wrote a stack monitor a couple years ago to assist a programmer in determining how much stack spaced is used, but this is done at runtime. You then use the value to tweek the size of your stack in your program.

    The stack monitor is availible here: http://forums.parallax.com/showthread.php?p=577317
    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 10/3/2008 10:08:44 PM GMT
Sign In or Register to comment.