| View previous topic :: View next topic |
| Author |
Message |
jaka
Joined: 04 May 2014 Posts: 51 Location: Finland
|
| Worst case stack usage |
Posted: Mon Mar 02, 2026 4:49 am |
|
|
I need to conduct worst case stack / heap usage analysis for a CCS project and prove that an overflow can't occur. I am using PIC18F26K83.
Normally, I check the list or .STA file but for this project they show that stack is not used at all. Can that be true? Is there some other way to analyze stack usage?
The top of the .lst file says:
| Code: | CCS PCH C Compiler, Version 5.116, 63210 02-maalis-26 12.26
Filename: (filename removed)
ROM used: 18472 bytes (28%)
Largest free fragment is 21840
RAM used: 690 (17%) at main() level
752 (18%) worst case
Stack used: 0 locations
Stack size: 31 |
And some snippets from .STA file:
| Code: | ROM used: 18472/65536 (28%)
21884/65536 (33%) including unused fragments
1.1 Average locations per line
9.9 Average locations per statement
RAM used: 690/4095 (17%) at main() level
752/4095 (18%) worst case
Stack used: 0 worst case (0 in main + 0 for interrupts)
Stack size: 31
...
Program metrics:
Functions 76
Statements 1868
Comments 8458
Volume (V) 145135
Difficulty (D) 411.9
Effort to implement (E) 59779021
Time to implement (T) 922 hours, 31 minutes
Est Delivered Bugs (B) 51
Cyclomatic Complexity 384
Maintainability (MI) 49
Segment Used Free
----------- ---- ----
00000-00002 4 0
00004-00006 0 4
00008-00060 90 0
00062-05550 18332 3412
05552-05552 2 0
05554-05554 2 0
05556-05556 2 0
05558-0AAA6 0 21840
0AAA8-0AAA8 2 0
0AAAA-0AAAA 2 0
0AAAC-0AAAC 2 0
0AAAE-0FFBE 0 21778
0FFC0-0FFDE 32 0
0FFE0-0FFFC 0 30
0FFFE-0FFFE 2 0 |
My code uses only one interrupt, I2C1 interrupt. The code flow is also tried to be kept deterministic, with all functions always running at specific order. But still, zero stack usage seems odd? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20048
|
|
Posted: Mon Mar 02, 2026 8:57 am |
|
|
Show the lines where you declare the interrupt handler.
For 'straight line' code, it is possible to have no stack usage, but the
interrupt if the handler is properly declared, will be using stack space.
So show how this is declared. |
|
 |
jaka
Joined: 04 May 2014 Posts: 51 Location: Finland
|
|
Posted: Mon Mar 02, 2026 11:32 am |
|
|
My interrupt is declared like this:
| Code: | #INT_I2C1
void i2c1_interrupt(void) {
code removed, actual code is very similar
as in ex_i2c_slave_k42.c
} |
|
|
 |
jaka
Joined: 04 May 2014 Posts: 51 Location: Finland
|
|
Posted: Mon Mar 02, 2026 11:38 am |
|
|
Hmm.. just tried to compile the ex_i2c_slave_k42.c and that is showing:
Stack used: 2 locations (0 in main + 2 for interrupts)
I need to do some experimenting which is causing the stack analysis to fail on my main project. I suspect the ClassB tests..
edit:
It was the ClassB tests, now I am getting this on my main project:
Stack used: 6 locations (5 in main + 1 for interrupts)
which seems plausible to me |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20048
|
|
Posted: Tue Mar 03, 2026 11:29 pm |
|
|
Makes a lot more sense.
Hope you can now move forwards. Perhaps looking at the tests a bit more
carefully, to see what is stopping the stack being used. |
|
 |
jaka
Joined: 04 May 2014 Posts: 51 Location: Finland
|
|
Posted: Wed Mar 04, 2026 5:46 am |
|
|
There is something weird going on. I am getting varying results for the stack usage, which are not consistent at all.
I tried to strip down the ClassB code, and found that the culprit was #indlucde <stdlib.h>. It was possible to omit the classB code completely, just include <stdlib.h> and the stack usage dropped to zero.
Then I tried to a simpler project, the aforementioned ex_i2c_slave_k42.c. Inclusion of stdlib had no effect (still 2 stack locations for interrupts, 0 in main)
Then I tried another project for 18F24K50 using USB. The stack usage dropped from 7+7 to 6+7 by including stdlib.
Then I was thinking this is perhaps compiler version dependant. Dropped from 5.116 to 5.097. Now even ex_i2c_slave_k42.c is showing zero stack usage. Back to 5.116, still showing zero stack usage. Tried to clean and rebuild project, still zero stack. |
|
 |
jaka
Joined: 04 May 2014 Posts: 51 Location: Finland
|
|
Posted: Wed Mar 04, 2026 7:25 am |
|
|
Tried to reboot PC, recreate the ex_i2c_slave_k42.c project from scratch, still showing zero stack usage.
Since the PIC18F24K50 based USB CDC project was showing repeatable results with regard to including stdlib.h, I used that to narrow down what is causing the variation in stack usage. I copied the stdlib.h local to project to modify it.
1. Without including stdlib, stack usage is 7 main + 7 interrupt.
2. With stdlib included after USB stuff, stack usage is 7 main + 7 interrupt.
3. With stdlib included before USB stuff, stack usage is 6 main + 7 interrupt.
4. With stdlib included before USB stuff, and qsort() and *bsearch() functions commented out, stack usage is 7 main + 7 interrupt.
In the cases 3 and 4 above, the compiled binaries are identical, although compiler reports different stack usage.
edit: all testing on this post done at v5.116. |
|
 |
jaka
Joined: 04 May 2014 Posts: 51 Location: Finland
|
|
Posted: Thu Mar 05, 2026 4:00 am |
|
|
I was able reproduce the problem in some CCS example projects, for example ex_usb_serial.c and some others. Including stdlib.h reduces the reported stack usage from 0+5 to 0 when compiled for 4550. If compiled for 45K50 the stack usage goes down from 6+7 to 0+7.
I reported the problem to CCS. |
|
 |
|