組み込み系

Arduinoで作ったスケッチのメモリ使用量を確認する

Arduino IDEの設定とコマンドで、ビルドしたスケッチのメモリ使用量の確認ができます。

メモリ使用量確認の準備

Arduino IDEの環境設定を変更します。

  1. Arduino IDEのメニュー[ファイル]-[環境設定]をクリック
  2. 「環境設定」ダイアログの[設定]タブにある「より詳細な情報を表示する」の「コンパイル」のチェックボックスをONにします。
  3. [OK]ボタンを押し、「環境設定」ダイアログを閉じます。
  4. 対象のスケッチをビルドします。

※この設定は、スケッチのメモリ使用量チェックでも使います。

ビルドが終わると、終了部分はこのような画面になります。

arduino%e3%83%93%e3%83%ab%e3%83%89%e7%b5%90%e6%9e%9c

この表示に出ている「最大32,256バイトのフラッシュメモリのうち、スケッチが1,236バイト(3%)を使っています。」という部分がフラッシュメモリの使用量です。

 

スケッチのメモリ使用量チェック

スケッチのメモリ使用量チェックは、フラッシュメモリの使用量よりちょっと面倒です。

まず必要な作業は、

  1. 上記の環境設定を行う
  2. 調べたいスケッチのビルドを行い、ビルド結果が表示された状態にする。

です。

ビルド結果のうち、赤の四角で囲った行の中に、拡張子が”.elf”のファイルをフルパスで指定している箇所があります。

arduino%e3%83%93%e3%83%ab%e3%83%89%e7%b5%90%e6%9e%9c_%e8%aa%ac%e6%98%8e

横長の行なので、横スクロールで探すことになります。
(この画面でも、「最大○○バイトのRAMのうち・・・」という行で概算は表示されています)
elfファイルは、バイナリファイルなので情報を取得するためにコマンド(avr-objdump)を使用します。
Windowsの場合、avr-objdumpコマンドは、「<Arduino IDEをインストールしたパス>\hardware\tools\avr\bin\」にあります。

avr-objdumpコマンドのヘルプ

Usage: C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objdump.exe <option(s)> <file(s)>
 Display information from object <file(s)>.
 At least one of the following switches must be given:
  -a, --archive-headers    Display archive header information
  -f, --file-headers       Display the contents of the overall file header
  -p, --private-headers    Display object format specific file header contents
  -P, --private=OPT,OPT... Display object format specific contents
  -h, --[section-]headers  Display the contents of the section headers
  -x, --all-headers        Display the contents of all headers
  -d, --disassemble        Display assembler contents of executable sections
  -D, --disassemble-all    Display assembler contents of all sections
  -S, --source             Intermix source code with disassembly
  -s, --full-contents      Display the full contents of all sections requested
  -g, --debugging          Display debug information in object file
  -e, --debugging-tags     Display debug information using ctags style
  -G, --stabs              Display (in raw form) any STABS info in the file
  -W[lLiaprmfFsoRt] or
  --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,
          =frames-interp,=str,=loc,=Ranges,=pubtypes,
          =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,
          =addr,=cu_index]
                           Display DWARF info in the file
  -t, --syms               Display the contents of the symbol table(s)
  -T, --dynamic-syms       Display the contents of the dynamic symbol table
  -r, --reloc              Display the relocation entries in the file
  -R, --dynamic-reloc      Display the dynamic relocation entries in the file
  @<file>                  Read options from <file>
  -v, --version            Display this program's version number
  -i, --info               List object formats and architectures supported
  -H, --help               Display this information

 The following switches are optional:
  -b, --target=BFDNAME           Specify the target object format as BFDNAME
  -m, --architecture=MACHINE     Specify the target architecture as MACHINE
  -j, --section=NAME             Only display information for section NAME
  -M, --disassembler-options=OPT Pass text OPT on to the disassembler
  -EB --endian=big               Assume big endian format when disassembling
  -EL --endian=little            Assume little endian format when disassembling
      --file-start-context       Include context from start of file (with -S)
  -I, --include=DIR              Add DIR to search list for source files
  -l, --line-numbers             Include line numbers and filenames in output
  -F, --file-offsets             Include file offsets when displaying information
  -C, --demangle[=STYLE]         Decode mangled/processed symbol names
                                  The STYLE, if specified, can be `auto', `gnu',
                                  `lucid', `arm', `hp', `edg', `gnu-v3', `java'
                                  or `gnat'
  -w, --wide                     Format output for more than 80 columns
  -z, --disassemble-zeroes       Do not skip blocks of zeroes when disassembling
      --start-address=ADDR       Only process data whose address is >= ADDR
      --stop-address=ADDR        Only process data whose address is <= ADDR
      --prefix-addresses         Print complete address alongside disassembly
      --[no-]show-raw-insn       Display hex alongside symbolic disassembly
      --insn-width=WIDTH         Display WIDTH bytes on a single line for -d
      --adjust-vma=OFFSET        Add OFFSET to all displayed section addresses
      --special-syms             Include special symbols in symbol dumps
      --prefix=PREFIX            Add PREFIX to absolute paths for -S
      --prefix-strip=LEVEL       Strip initial directory names for -S
      --dwarf-depth=N        Do not display DIEs at depth N or greater
      --dwarf-start=N        Display DIEs starting with N, at the same depth
                             or deeper
      --dwarf-check          Make additional dwarf internal consistency checks.      

C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objdump.exe: supported targets: elf32-avr elf32-little elf32-big plugin srec symbolsrec verilog tekhex binary ihex
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objdump.exe: supported architectures: avr avr:1 avr:2 avr:25 avr:3 avr:31 avr:35 avr:4 avr:5 avr:51 avr:6 avr:100 avr:101 avr:102 avr:103 avr:104 avr:105 avr:106 avr:107 plugin

Options supported for -P/--private switch:
For AVR ELF files:
  mem-usage   Display memory usage
  avr-prop    Display contents of .avr.prop section
Report bugs to <http://www.sourceware.org/bugzilla/>.

elfファイルを、avr-objdumpコマンドにかけます。

ちょっと長めですが、コマンドは下記のような感じ(ユーザー名部分を変えていますので、コピペでは使えません)です。

"C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objdump.exe" -h "C:\Users\<ユーザ名>\AppData\Local\Temp\arduino_build_205112\sketch_oct04b.ino.elf"

avr-objdumpコマンドの’-h’オプションを使用します。

実行した結果は、こんな感じで表示されます。

C:\Users\<ユーザー名>\AppData\Local\Temp\arduino_build_205112/sketch_oct04b.ino.elf:     file format elf32-avr

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         00000000  00800100  000004d4  00000568  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  1 .text         000004d4  00000000  00000000  00000094  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .bss          0000001c  00800100  00800100  00000568  2**0
                  ALLOC
  3 .comment      00000011  00000000  00000000  00000568  2**0
                  CONTENTS, READONLY
  4 .note.gnu.avr.deviceinfo 00000040  00000000  00000000  0000057c  2**2
                  CONTENTS, READONLY
  5 .debug_info   000005f4  00000000  00000000  000005bc  2**0
                  CONTENTS, READONLY, DEBUGGING
  6 .debug_abbrev 000005a2  00000000  00000000  00000bb0  2**0
                  CONTENTS, READONLY, DEBUGGING
  7 .debug_line   0000001a  00000000  00000000  00001152  2**0
                  CONTENTS, READONLY, DEBUGGING
  8 .debug_str    00000208  00000000  00000000  0000116c  2**0
                  CONTENTS, READONLY, DEBUGGING

 

タイトルとURLをコピーしました