Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

How to know limit for $hwstack, $swstack, $framesize, is it

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1048

usa.gif
PostPosted: Tue Sep 15, 2020 12:06 am    Post subject: How to know limit for $hwstack, $swstack, $framesize, is it Reply with quote

Hello all,

I am using an ATMEGA 1284.


How to find the maximum value to use to set
$hwstack, $swstack and $framesize ?

Is it bad to over allocate these?

My app cannot be simulated since it needs to use both UARTs connected and it is using many ISR's and the SPI bus. I can however print results to a terminal emulator.

Thanks,
Tim

(BASCOM-AVR version : 2.0.8.3 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Wed Sep 16, 2020 4:29 pm    Post subject: Re: How to know limit for $hwstack, $swstack, $framesize, is Reply with quote

TSEYFARTH wrote:
How to find the maximum value to use to set
$hwstack, $swstack and $framesize ?
Call the report(Ctrl+W) and look for 'Space left', which is the difference between available SRam size, minus variables, minus stacks/frame.
Quote:
Is it bad to over allocate these?
You shalt not allocate more than is free.

Every byte of SRam not being used by variables, which start on the lower address range or by stack on SRamend, going down, is wasted memory.
The big advantage of allocating and subdividing any free space into hardstack, softstack and frame is: it becomes less likely, that they overwrite each other.

If for example hwstack = 64 and you use heavy recursion in code, then each recursion takes some stack, which moves from SRamend downwards.
At a certain point it will overwrite the soft-stack and then the frame.

If you have 12kByte SRam left and assign each 4k to hardstack, softstack and frame, then recursion has much more space till things go mad.
The exact distribution is not necessarily same for each hw, sw, frame, instead it depends on how your program works.
If the program relies heavily on recursion, you want a big hardware stack, if you use tons of locals, a bigger frame size is your friend.
See the help how and for what each of the stack/frame memory ranges is used.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 696

poland.gif
PostPosted: Wed Sep 16, 2020 7:16 pm    Post subject: Reply with quote

Some advice about free SRAM you will find in the CodeExplorer - just enable it.
Please also check Help topic - $hwcheck - that give you opportunity to print stacks use values.

Back to top
View user's profile Visit poster's website
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Wed Sep 23, 2020 4:15 am    Post subject: Reply with quote

So what do these numbers represent?

Bytes used?
bytes needed?
_lowest
_highest??

I'm confused.

_________________
Neil
Back to top
View user's profile
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Wed Sep 23, 2020 5:38 am    Post subject: Reply with quote

Still confused. I included (copied with cut & paste) the sample program from help section on $HWCHECK and I run it every 30 secs in a program that runs successfully, has been running for several years and is 8351 lines long. Processor is atmega 1284p.
The program takes 15 minutes to exercise all the subs & functions, communicate with the net, peripherals etc and after 15 minutes, the $HWcheck tells me things I don't believe.




Code:


$regfile = "m1284pdef.dat"
$crystal = 9830400
$framesize =800
$hwstack = 800                                              '
$swstack =  800
$frameprotect = 1
config submode = old  
$PROG &HFF,&HC7,&HD0,&HFE.                                    
$include "config_mmcsd_hc.bas"                          
$include "config_AVR-DOS.BAS"
$lib "stackcheck.lib"


'later on
            'do this stuff every 30 sec
            '**************************

       
                    if _sec = 30 or _sec = 0  then

                        If Dn12 = 0 then
                           Dn12 = 1
                           'do lots of other things
                                             
                           Print #1, "stack test"
                           Dim G As Byte , W As Word
                           $hwcheck                                                   'hw stack check on
                           $framecheck
                           $softcheck
                           Print #1,""
                           Print #1, _hw_lowest
                           W = _hwstackstart - _hw_lowest
                           Print #1, "HW stack needed : " ; W

                           Print #1, _fw_highest
                           If _fw_highest > 0 Then
                             W = _frame_high - _fw_highest
                             Print #1, "Frame space needed : " ; W
                           End If


                           Print #1, _sw_lowest
                           W = _hwstack_low - _sw_lowest
                           Print #1, "SW stack needed : " ; W
                           Print #1,""
                         
                        end if
                    else

                        dn12 = 0



                    end if
              end if



'*************************************

 

Quote:

stack test

_hw_lowest= 16634
HW stack needed : 5
_fw_highest= 0
_sw_lowest = 65535
SW stack needed : 15840


_________________
Neil
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Wed Sep 23, 2020 2:40 pm    Post subject: Reply with quote

njepsen wrote:
Still confused.

What happens, if you place the stackcheck directives where they belong to: in front of any code start?
The sample in the help is pretty clear about it, I'm wondering why you need to put it differently.

You're aware that your questions are only loosely connected with the original question?
Instead of hijacking this one, it may make sense to start a new thread.
Back to top
View user's profile
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Wed Sep 23, 2020 9:22 pm    Post subject: Reply with quote

Thanks for the tip Rudolf. Here is what happens:
Code:


$regfile = "m1284pdef.dat"
$crystal = 9830400
$framesize =800
$hwstack = 800                                              '
$swstack =  800
$frameprotect = 1
config submode = old                                      
$include "config_mmcsd_hc.bas"                          
$include "config_AVR-DOS.BAS"
$lib "stackcheck.lib"
$hwcheck                                                  
$framecheck
$softcheck
$PROG &HFF,&HC7,&HD0,&HFE'



            'do this stuff every 30 sec
            '**************************

         
                    if _sec = 30 or _sec = 0  then

                        If Dn12 = 0 then
                           Dn12 = 1
                         
                           Print #1, "stack test"

                           Dim G As Byte , W As Word




                           print #1,""
                           Print #1,  "_hw_lowest= "; _hw_lowest
                           W = _hwstackstart - _hw_lowest
                           Print #1, "HW stack needed : " ; W

                           Print #1, "_fw_highest= ";_fw_highest
                           If _fw_highest > 0 Then
                             W = _frame_high - _fw_highest
                             Print #1, "Frame space needed : " ; W
                           End If

                           Print #1, "_sw_lowest = ";_sw_lowest
                           W = _hwstack_low - _sw_lowest
                           Print #1, "SW stack needed : " ; W
                           print #1,""


                        end if
                    else

                        dn12 = 0


                    end if




         
Quote:

stack test

_hw_lowest= 16626
HW stack needed : 13
_fw_highest= 14748
Frame space needed : 293
_sw_lowest = 15800
SW stack needed : 39


These numbers are much more believable but there are now errors in code explorer saying that all the _frame & stack variable are undeclared.



_________________
Neil
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Sep 24, 2020 7:48 am    Post subject: Reply with quote

njepsen wrote:
Thanks for the tip Rudolf.
My pedigree does not include reindeer, nor am I called Rudolf.
Quote:
...that all the _frame & stack variable are undeclared.
If they would actually be undeclared, your code would not run and use them.
It's some issue with the code explorer, which does parsing and checking independently from the compiler.
Back to top
View user's profile
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Thu Sep 24, 2020 7:59 am    Post subject: Reply with quote

My sincere apologies mws - for some reason I thought that was your name. I wasn't being cheeky or flippant.

Yes obviously these var must be declared in the .lib. Anyway - the values seem reasonable, but they are considerably different from the info values.


Quote:
stack test

_hw_lowest= 16626
HW stack needed : 13
_fw_highest= 14748
Frame space needed : 293
_sw_lowest = 15800
SW stack needed : 39

>> pi=on 24/09/20 18:36:30

_________________
Neil
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Sep 24, 2020 8:53 am    Post subject: Reply with quote

njepsen wrote:
Anyway - the values seem reasonable, but they are considerably different from the info values.

I would rather verify the values from stackcheck against the declared values for stack/frame, additionally I'd have a look what the report says.

About the code explorer's info: everything therein can be only info which the compiler provides at end of compilation, thus it's static info.
I do not know how these values are derived in detail, but for example the compiler can count local variables going to frame, or it can assume if there's a print instruction, then it needs a minimum of swstack, but the compiler can not guess from a statical picture, how it works dynamically.

That's what stackcheck does, it does a check under (nearly) real conditions. With the disadvantage of 'nearly', which means that in case your code works a week and does not call all the week a special memory-hungry function, you still do not know what happens in one week and one second.

If in contrary you are sure that within this week every function, or chain of function calls and every combination thereof was executed one time and no memory leaks exist, then stackcheck gives you the correct representation of memory usage.
Back to top
View user's profile
njepsen

Bascom Member



Joined: 13 Aug 2007
Posts: 412

newzealand.gif
PostPosted: Mon Sep 28, 2020 5:56 am    Post subject: Reply with quote

Hi MWS,
Thanks for your reply.
Quote:
About the code explorer's info: everything therein can be only info which the compiler provides at end of compilation, thus it's static info.
I do not know how these values are derived in detail, but for example the compiler can count local variables going to frame, or it can assume if there's a print instruction, then it needs a minimum of swstack, but the compiler can not guess from a statical picture, how it works dynamically.

This is obvious, and therein lies the rub - statics test vs dynamic.
I was hoping the little test routine shown in the help would give sensible results, but it appears not - I should re-phrase that - I haven't been able to get sensible numbers.
A few minutes ago - I coded a small loop immediately after the declarations and before any real code. This is with the intention of running none of the main code ( which takes several hours/days before all subs get called.)
Here is the test code outline:
Code:


$regfile = "m1284pdef.dat"
$crystal = 9830400
$framesize =1500
$hwstack = 400                                              '
$swstack = 400
$frameprotect = 1
config submode = old                                        'old =  need to declare subs or functions
$include "config_mmcsd_hc.bas"                              'set up the high capacity SD card comms
$include "config_AVR-DOS.BAS"
$lib "stackcheck.lib"
$hwcheck                                                   'hw stack check on
$framecheck
$softcheck
$PROG &HFF,&HC7,&HD0,&HFE' generated. Take care that the chip supports all fuse bytes.

'declarations
'configurations

'-------------------------------------------------------------------------------

Open "comc.3:38400,8,n,1" For Output As #1                  '
Open "comc.2:38400,8,n,1" For Input As #5
Open "comc.5:9600,8,n,1" For Output As #7
Open "comc.6:9600,8,n,1" For Input As #8
Open "comc.0:9600,8,n,1" For Output As #2
Open "comc.1:9600,8,n,1" For Input As #4

$baud1 = ModemBaudrate
Open "com2:" For Binary As #6                               '
Config Serialin1 = Buffered , Size = 250 , Cts = Pind.6 , Rts = Pind.5 , Threshold_full = 30 , Threshold_empty = 20
Config Serialout1 = Buffered , Size = 50
Enable Interrupts
 'test code

        do

                           Print #1, "stack test"
                           Dim G As Byte , W As Word
                           print #1,""
                           Print #1,  "_hw_lowest= "; _hw_lowest
                           W = _hwstackstart - _hw_lowest
                           Print #1, "HW stack needed : " ; W

                           Print #1, "_fw_highest= ";_fw_highest
                           If _fw_highest > 0 Then
                             W = _frame_high - _fw_highest
                             Print #1, "Frame space needed : " ; W
                           End If


                           Print #1, "_sw_lowest = ";_sw_lowest
                           W = _hwstack_low - _sw_lowest
                           Print #1, "SW stack needed : " ; W
                           print #1,""
                           wait 5



        loop
'line 883
'7000 more lines

 

this code results in this printout.

Quote:


_hw_lowest= 16595
HW stack needed : 44
_fw_highest= 14468
Frame space needed : 1373
_sw_lowest = 16230
SW stack needed : 9

These numbers don't make sense - at least to me.
If I move the "test loop" further down the main body of the code (no subs or functions are called up to that point) i get a totally different result.

Quote:


stack test

_hw_lowest= 16626
HW stack needed : 13
_fw_highest= 14684
Frame space needed : 1157
_sw_lowest = 16218
SW stack needed : 21

As a further test - I embedded the "test loop" into a sub in my main code that only gets called once an hour. Clearly after an hour most modem coms, serial coms and HTTP, sms and email coms have been exercised at least once.
Here is that result:
Quote:

stack test

_hw_lowest= 16626
HW stack needed : 13
_fw_highest= 14848
Frame space needed : 993
_sw_lowest = 16200
SW stack needed : 39

This last result is the most likely, and is reasonable close to an earlier test, but still way different to the code explorer numbers., especially WRT the frame size estimate.

_________________
Neil
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5077
Location: Holland

blank.gif
PostPosted: Mon Sep 28, 2020 9:43 am    Post subject: Reply with quote

the Code Explorer looks at the BASIC code. it does not take into account the space required by the asm libs.
Also when you have code like this :

Code:
if mytest=0 then
  call param10(1,2,3,4,5,6,7,8,9,9,10)
else
  call param0
end if
 


it will take the worse case : param10. so if your code during run always execute the else, you get different values.

So the Code Explorer returns the deepest nesting space estimate. But not taking into account(yet) the space required by the asm libs. these do not take much in general. so personally i add 40 to hw stack.
Code like this :
Code:
dim s as string * 100
s="test"
s="abc"+ s
 

requires a temp copy. the Code Explorer does not take this into account.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum