Skip to content

Commit

Permalink
chore: add script for memory measures
Browse files Browse the repository at this point in the history
  • Loading branch information
funbiscuit committed Jan 27, 2024
1 parent b6ee61f commit 334794d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 42 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,17 @@ Memory usage depends on version of crate, enabled features and complexity of you
Below is memory usage of arduino [example](examples/arduino/README.md) when different features are enabled.
Memory usage might change in future versions, but I'll try to keep this table up to date.

| Features | ROM, bytes | Static RAM, bytes |
|---------------------------|:----------:|:-----------------:|
| - | 7494 | 141 |
| autocomplete | 9066 | 161 |
| history | 9168 | 173 |
| help | 10446 | 458 |
| autocomplete+history | 10772 | 193 |
| autocomplete+help | 12052 | 474 |
| help+history | 12154 | 490 |
| autocomplete+help+history | 13968 | 506 |

Commands used to calculate memory usage are given in example [description](examples/arduino/README.md#memory-usage).
| Features | ROM, bytes | Static RAM, bytes |
|---------------------------------|:----------:|:-----------------:|
| | 9388 | 161 |
| `autocomplete` | 11126 | 181 |
| `history` | 11248 | 193 |
| `autocomplete` `history` | 12748 | 213 |
| `help` | 12204 | 557 |
| `autocomplete` `help` | 13916 | 573 |
| `history` `help` | 14456 | 589 |
| `autocomplete` `history` `help` | 15538 | 605 |

This table is generated using this [script](examples/arduino/memory.sh).
As table shows, enabling help adds quite a lot to memory usage since help usually requires a lot of text to be stored.
Also enabling all features almost doubles ROM usage comparing to all features disabled.
1 change: 1 addition & 0 deletions demo.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
# This script emulates predefined input to showcase usage of CLI
# Size of screen: 65x15
# Dependencies: xdotool
# To run demo you need to flash arduino example to real arduino nano
# Then run script in background:
Expand Down
1 change: 1 addition & 0 deletions embedded-cli-macros/src/command/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum CommandArgType {
}

impl CommandArgType {
#[cfg(feature = "help")]
pub fn is_positional(&self) -> bool {
self == &CommandArgType::Positional
}
Expand Down
36 changes: 6 additions & 30 deletions examples/arduino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This example shows how to build cli with Arduino Nano.
Another Arduino can also be used, but you will have to tweak configs.
Example uses ~14KiB of ROM and ~0.5KiB of static RAM.
Example uses ~15KiB of ROM and ~0.6KiB of static RAM.
Most of RAM is taken by derived implementations for help and autocomplete
that don't use progmem. In future this should be fixed.

Expand All @@ -24,40 +24,16 @@ tio /dev/ttyUSB0 --map ODELBS
# Memory usage

Memory usage might vary depending on compiler version, build environment and library version.
To find out total ROM usage run:
You can run `memory.sh` script to calculate memory usage of this arduino example with different activated features
of cli library.

To analyze ROM usage:

```shell
cargo bloat --release
```

Example output:
```
File .text Size Crate Name
1.6% 56.9% 8.1KiB arduino_cli arduino_cli::try_run
0.1% 3.7% 538B embedded_cli embedded_cli::token::Tokens::new
0.1% 3.6% 530B embedded_cli embedded_cli::help::HelpRequest::from_tokens
0.1% 3.2% 472B embedded_cli embedded_cli::token::Tokens::remove
0.1% 3.2% 468B embedded_cli embedded_cli::cli::Cli<W,E,CommandBuffer,HistoryBuffer>::navigate_history
0.7% 26.0% 3.7KiB And 34 smaller methods. Use -n N to show more.
2.9% 100.0% 14.2KiB .text section size, the file size is 493.3KiB
```

To find total static memory usage:

To analyze static RAM:
```shell
cargo build --release && \
avr-nm -Crtd --size-sort \
target/avr-atmega328p/release/arduino-cli.elf \
| grep -i ' [dbvr] ' \
| awk -F " " '{Total=Total+$1} END{print "RAM usage: " Total}' -
```

Example output:
```
RAM usage: 506
```

To further analyze used space:
```
avr-objdump -s -j .data target/avr-atmega328p/release/arduino-cli.elf
```
Binary file modified examples/arduino/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions examples/arduino/memory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
# This script measures ROM and static RAM memory usage of Arduino example with different features enabled
# Memory info should be updated at following places:
# /README.md in Demo section
# /README.md in Memory section
# /examples/arduino/README.md in example description

memory_file="target/MEMORY.md"

echo "## Memory usage" >$memory_file

echo "| Features | ROM, bytes | Static RAM, bytes |" >>$memory_file
echo "|----------|:----------:|:-----------------:|" >>$memory_file

array=("autocomplete" "history" "help")
n=${#array[@]}
for ((i = 0; i < (1 << n); i++)); do
list=()
list_md=()
for ((j = 0; j < n; j++)); do
if (((1 << j) & i)); then
list+=("${array[j]}")
list_md+=("\`${array[j]}\`")
fi
done
features=$(
IFS=,
echo "${list[*]}"
)
features_md=$(
IFS=' '
echo "${list_md[*]}"
)
echo "Measuring features: $features"

cp Cargo.toml Cargo.toml.bak

cargo remove embedded-cli
cargo add embedded-cli --path "../../embedded-cli" \
--no-default-features \
--features "macros, $features"

cargo build --release

ram_usage=$(avr-nm -Crtd --size-sort \
target/avr-atmega328p/release/arduino-cli.elf |
grep -i ' [dbvr] ' |
awk -F " " '{Total=Total+$1} END{print Total}' -)

rom_usage=$(cargo bloat --release --message-format json | jq -cs '.[0]["text-section-size"]')

echo "| $features_md | $rom_usage | $ram_usage |" >>$memory_file
echo "$features: ROM=$rom_usage RAM=$ram_usage"

mv Cargo.toml.bak Cargo.toml
done

0 comments on commit 334794d

Please sign in to comment.