Skip to contentSkip to conversation

tools: New 'spl-stackusage' script

U-Boot Siarhei Siamashka 2 participants 4 messages Jan 31, 2015 – Feb 11, 2015

lore
Share & tools
lore
#0 lore raw

This is a script, which tries to provide a pessimistic estimate of the stack usage in the SPL binary.

A more detailed description about how it works and some example pictures are available in an earlier e-mail:

    http://lists.denx.de/pipermail/u-boot/2015-January/201713.html

In fact, I have done nothing with it since that old status update and the code is still very much just at the proof of concept stage and can be only treated as a somewhat working prototype. It only supports 32-bit ARM so far.

An example of trying it:

1. First compile u-boot for some ARM board. For example:

make CROSS_COMPILE=arm-none-linux-gnueabi- Cubieboard_defconfig make CROSS_COMPILE=arm-none-linux-gnueabi- -j8

2. Run the script:

tools/spl-stackusage/spl-stackusage

3. Look at the (slightly reformatted) output:

Execution path with maximized stack usage (3152 bytes):
entry > _main > board_init_f > board_init_r > spl_mmc_load_image > mmc_init > mmc_startup > mmc_switch.isra.1 > mmc_send_status > printf > puts > console_puts ? mmc_bread > mmc_set_blocklen > mmc_send_cmd ? sunxi_mmc_getcd > gpio_get_value > axp_gpio_get_value > axp209_read > i2c_read ? twsi_i2c_read > i2c_begin > twsi_wait > udelay > __udelay

The '>' symbols indicate normal calls. The '?' symbols indicate a suspected indirect call. Now we can see that the 'console_puts ? mmc_bread' indirect call does not make any sense. So we can run the script again, now indicating that a call from 'console_puts' to 'mmc_bread' is impossible:

4. Run the script again as:

   tools/spl-stackusage/spl-stackusage \
       remove_arrow[console_puts,mmc_bread]

5. Look at the new output:

Execution path with maximized stack usage (3152 bytes):
entry > _main > board_init_f > board_init_r > spl_mmc_load_image > mmc_init > mmc_startup > mmc_switch.isra.1 > mmc_send_status > printf > puts > console_puts ? sunxi_mmc_getcd > gpio_get_value > axp_gpio_get_value > axp209_read > i2c_read ? mmc_bread > mmc_set_blocklen > mmc_send_cmd ? twsi_i2c_read > i2c_begin > twsi_wait > udelay > __udelay

Now it tries to guess that there is an indirect call from 'console_puts' to 'sunxi_mmc_getcd'. This also does not make sense. We add it to the black list too. And repreat the whole process iteratively. Eventually we end up with something like

6. Run the script as:

   tools/spl-stackusage/spl-stackusage \
       remove_arrow[console_puts,mmc_bread] \
       remove_arrow[console_puts,sunxi_mmc_getcd] \
       remove_arrow[i2c_read,sunxi_mmc_set_ios] \
       remove_arrow[console_puts,twsi_i2c_read] \
       remove_arrow[i2c_read,mmc_bread] \
       remove_arrow[serial_puts,mmc_bread] \
       remove_arrow[serial_puts,sunxi_mmc_getcd] \
       remove_arrow[console_puts,twsi_i2c_write] \
       remove_arrow[console_puts,sunxi_mmc_send_cmd] \
       remove_arrow[console_puts,twsi_i2c_probe] \
       remove_arrow[serial_puts,twsi_i2c_read] \
       remove_arrow[console_puts,twsi_i2c_init] \
       remove_arrow[serial_puts,twsi_i2c_write] \
       remove_arrow[serial_puts,sunxi_mmc_send_cmd] \
       remove_arrow[serial_puts,twsi_i2c_probe] \
       remove_arrow[console_puts,sunxi_mmc_set_ios] \
       remove_arrow[serial_puts,twsi_i2c_init]

7. And look at the final report:

Execution path with maximized stack usage (3064 bytes):
entry > _main > board_init_f > board_init_r > spl_mmc_load_image > mmc_init > mmc_startup > mmc_set_clock > mmc_set_ios ? mmc_bread > mmc_set_blocklen > mmc_send_cmd ? sunxi_mmc_getcd > gpio_get_value > axp_gpio_get_value > axp209_read > i2c_read > i2c_get_adapter > printf > vsprintf > vsnprintf_internal.isra.6 > number.isra.4 > put_dec > __div64_32 > __aeabi_uidiv > __aeabi_idiv0

The process of blacklisting impossible caller->callee pairs made the stack usage report less pessimistic (from 3152 to 3064 bytes) and the execution path makes a little bit more sense. But that's the whole point. Even the initial stack usage number is supposed to be usable, because it is just overestimating the stack usage and should never underestimate it (unless confused by tricks in the assembly code).

Also there is a 'spl-stackusage.png' file with the whole callgraph generated by the graphviz 'dot' tool. It may looks like this (a different picture, not generated by following the steps above):

    http://people.freedesktop.org/~siamashka/files/20150116-spl-stackgraph/spl-stackgraph-v2015.01-cubieboard2-fel.png

This is surely not intended to be pushed as-is yet. And still needs some cleanups. However I would like to get some feedback, just to get a better sense of direction where it should be moving.

Also, formally, the merge window is still open. So, I guess, this tool might have a very slim theoretical chance to get into v2015.04 :-)

---
 tools/spl-stackusage/spl-stackusage    |   1 +
 tools/spl-stackusage/spl-stackusage.rb | 471 +++++++++++++++++++++++++++++++++
 2 files changed, 472 insertions(+)
 create mode 120000 tools/spl-stackusage/spl-stackusage
 create mode 100755 tools/spl-stackusage/spl-stackusage.rb
tools/spl-stackusage/spl-stackusage new file +1 -0
new file mode 120000
index 0000000..eb10b9e
@@ -0,0 +1 @@
+spl-stackusage.rb
\ No newline at end of file
tools/spl-stackusage/spl-stackusage.rb new file +471 -0

471 lines not on this page · raw message

Signature · 1 line
2.0.5
#1 lore raw

Dear Siarhei Siamashka,

Quoted 12 lines · Siarhei Siamashka

This is a script, which tries to provide a pessimistic estimate of the stack usage in the SPL binary.

A more detailed description about how it works and some example pictures are available in an earlier e-mail:

    http://lists.denx.de/pipermail/u-boot/2015-January/201713.html

In fact, I have done nothing with it since that old status update and the code is still very much just at the proof of concept stage and can be only treated as a somewhat working prototype. It only supports 32-bit ARM so far.

Quoted 6 lines

An example of trying it:

1. First compile u-boot for some ARM board. For example:

make CROSS_COMPILE=arm-none-linux-gnueabi- Cubieboard_defconfig make CROSS_COMPILE=arm-none-linux-gnueabi- -j8

unfortunately out-of-tree build is not directly supported with this script. I mean some environment (CROSS_COMPILE) is re-used/required by the script but others not (O). It would be fine to just replace the make in cmdline with this tool to run it.

Quoted 20 lines

2. Run the script:

tools/spl-stackusage/spl-stackusage

3. Look at the (slightly reformatted) output:

Execution path with maximized stack usage (3152 bytes):
entry > _main > board_init_f > board_init_r > spl_mmc_load_image > mmc_init > mmc_startup > mmc_switch.isra.1 > mmc_send_status > printf > puts > console_puts ? mmc_bread > mmc_set_blocklen > mmc_send_cmd ? sunxi_mmc_getcd > gpio_get_value > axp_gpio_get_value > axp209_read > i2c_read ? twsi_i2c_read > i2c_begin > twsi_wait > udelay > __udelay

The '>' symbols indicate normal calls. The '?' symbols indicate a suspected indirect call. Now we can see that the 'console_puts ? mmc_bread' indirect call does not make any sense. So we can run the script again, now indicating that a call from 'console_puts' to 'mmc_bread' is impossible:

Quoted 4 lines

4. Run the script again as:

   tools/spl-stackusage/spl-stackusage \
       remove_arrow[console_puts,mmc_bread]

Unfortunately dynamic stack allocation is not considered. So adding FAT to SPL will result in an empty list here. I only get 'dynamic stack size in get_cluster'. I think the most important thing is to always print out the summary and the caller stack, mark the dynamic parts of the stack and mention that there is some more required.
Some interface to declare the maximum amount of dynamic stack usage for a specific symbol by command line would be really great. So one can guess the relevant parts and calculate the maximum size.

Another point ... I really appreciate your efforts to bring this tool in. So don't get me wrong, but ... why ruby?
A while ago some guys started to introduce python for some complex scripts, before that u-boot only had shell scripts.
I personally think one interpreter in u-boot world is enough. I'm curious how others think about that.

Best regards

Andreas Bie?mann

#2 lore raw
Quoted 1 line · On Mon, 02 Feb 2015 14:15:33 +0100 "Andreas Bie?mann"…

Dear Siarhei Siamashka,

Hi Andreas,

Thanks for your feedback.

Quoted 17 lines · Siarhei Siamashka
Quoted 16 lines

This is a script, which tries to provide a pessimistic estimate of the stack usage in the SPL binary.

A more detailed description about how it works and some example pictures are available in an earlier e-mail:

    http://lists.denx.de/pipermail/u-boot/2015-January/201713.html

In fact, I have done nothing with it since that old status update and the code is still very much just at the proof of concept stage and can be only treated as a somewhat working prototype. It only supports 32-bit ARM so far.

An example of trying it:

1. First compile u-boot for some ARM board. For example:

Quoted 7 lines
Quoted 2 lines

make CROSS_COMPILE=arm-none-linux-gnueabi- Cubieboard_defconfig make CROSS_COMPILE=arm-none-linux-gnueabi- -j8

unfortunately out-of-tree build is not directly supported with this script. I mean some environment (CROSS_COMPILE) is re-used/required by the script but others not (O). It would be fine to just replace the make in cmdline with this tool to run it.

I'm not so sure that replacing make is a very good idea because that's outside of the scope of this script. The script only needs the 'spl' directory with the SPL binary itself and also the *.su files generated by GCC.

The CROSS_COMPILE environment variable is only used to find the arm variant of the objdump tool. This variable is even only used as a fallback method, because the script contains the list of possible arm toolchain triplets and probes them. In many cases it can be run even without the CROSS_COMPILE variable defined.

Quoted 20 lines
Quoted 20 lines

2. Run the script:

tools/spl-stackusage/spl-stackusage

3. Look at the (slightly reformatted) output:

Execution path with maximized stack usage (3152 bytes):
entry > _main > board_init_f > board_init_r > spl_mmc_load_image > mmc_init > mmc_startup > mmc_switch.isra.1 > mmc_send_status > printf > puts > console_puts ? mmc_bread > mmc_set_blocklen > mmc_send_cmd ? sunxi_mmc_getcd > gpio_get_value > axp_gpio_get_value > axp209_read > i2c_read ? twsi_i2c_read > i2c_begin > twsi_wait > udelay > __udelay

The '>' symbols indicate normal calls. The '?' symbols indicate a suspected indirect call. Now we can see that the 'console_puts ? mmc_bread' indirect call does not make any sense. So we can run the script again, now indicating that a call from 'console_puts' to 'mmc_bread' is impossible:

Quoted 6 lines
Quoted 4 lines

4. Run the script again as:

   tools/spl-stackusage/spl-stackusage \
       remove_arrow[console_puts,mmc_bread]

Unfortunately dynamic stack allocation is not considered.

The use of dynamic stack size is at least detected and the script refuses to work. I wanted to get some feedback before deciding how to best handle it.

Quoted 4 lines

So adding FAT to SPL will result in an empty list here. I only get 'dynamic stack size in get_cluster'. I think the most important thing is to always print out the summary and the caller stack, mark the dynamic parts of the stack and mention that there is some more required.

Thanks for the suggestion. For now you can just find the relevant .su file and edit it fake some some arbitrary stack usage estimate there.

Quoted 3 lines

Some interface to declare the maximum amount of dynamic stack usage for a specific symbol by command line would be really great. So one can guess the relevant parts and calculate the maximum size.

Yes. Or alternatively it is perhaps possible to just fix the FAT code and use malloc instead of dynamic allocations on stack.

The large buffers in .bss in the FAT code is already responsible for the .bss section placement in DRAM. And these dynamic allocations on stack are yet another wacky thing in this code.

Quoted 2 lines

Another point ... I really appreciate your efforts to bring this tool in. So don't get me wrong, but ... why ruby?

That's because I know ruby, but don't know python at the moment.

Why I have picked ruby over python some years ago is another story. But I don't want to start a programming language holy war here :)

Quoted 4 lines

A while ago some guys started to introduce python for some complex scripts, before that u-boot only had shell scripts.
I personally think one interpreter in u-boot world is enough. I'm curious how others think about that.

If python is a requirement, then I will have to learn it first. Which is fair enough. But this requirement first needs to be explicitly communicated by somebody.

Signature · 2 lines
Best regards,
Siarhei Siamashka
#3 lore raw

Hi Siarhei,

Quoted 7 lines · Siarhei Siamashka
Quoted 2 lines · On Mon, 02 Feb 2015 14:15:33 +0100 "Andreas Bie?mann"…

Another point ... I really appreciate your efforts to bring this tool in. So don't get me wrong, but ... why ruby?

That's because I know ruby, but don't know python at the moment.

That's a good reason.

Quoted 11 lines

Why I have picked ruby over python some years ago is another story. But I don't want to start a programming language holy war here :)

Quoted 4 lines

A while ago some guys started to introduce python for some complex scripts, before that u-boot only had shell scripts.
I personally think one interpreter in u-boot world is enough. I'm curious how others think about that.

If python is a requirement, then I will have to learn it first. Which is fair enough. But this requirement first needs to be explicitly communicated by somebody.

For me it is not a real requirement, I'm fine with ruby when it works as expected. The dynamic stack allocation should be considered in the tool even though FAT would be converted to malloc(2) (some days ;).

Best regards

Andreas Bie?mann