Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 1 | ###################### |
| 2 | Code size analyze tool |
| 3 | ###################### |
| 4 | |
| 5 | .. contents:: Table of Contents |
| 6 | |
| 7 | These are scripts to dump ARMCLANG complie map file detail information and get |
| 8 | the change between two different build results. The features are: |
| 9 | |
| 10 | * Get a database to implement data tables from ARMCLANG or GNUARM map file. |
| 11 | * Supply an UI for developers to scan sorted information of sections, libraries, |
| 12 | object files, functions and data. It can help analyze the biggest or smallest |
| 13 | targets in certain image. It can also help search functions or data to locate |
| 14 | their address and detail information. |
| 15 | * Diff two databases to see the code size increasement or decreasement. |
| 16 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 17 | ****************** |
| 18 | Install dependency |
| 19 | ****************** |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 20 | |
| 21 | Linux |
| 22 | ===== |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 23 | Install curse and database. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 24 | |
| 25 | .. code-block:: bash |
| 26 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 27 | sudo apt-get install python3-dev libmysqlclient-dev libncursesw6 libncurses6 libsqlite3-dev |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 28 | pip install mysqlclient XlsxWriter |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 29 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 30 | Windows |
| 31 | ======= |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 32 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 33 | In power shell, use pip to install packages. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 34 | |
| 35 | .. code-block:: bash |
| 36 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 37 | pip install mysqlclient XlsxWriter windows-curses |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 38 | |
Elena Uziunaite | b90a340 | 2023-11-13 16:24:28 +0000 | [diff] [blame] | 39 | ***** |
| 40 | Usage |
| 41 | ***** |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 42 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 43 | The commands of code size analyze tool usage are: |
| 44 | |
| 45 | .. code-block:: bash |
| 46 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 47 | usage: code_size_analyze.py [-h] [--gnuarm | --armcc] [--db-name <data.db>] |
| 48 | [-u | -S | -s | -l | -o | -f | -d | |
| 49 | --dump-sec <sec> | --dump-lib <lib> | |
| 50 | --dump-obj <obj> | --dump-func <func> | |
| 51 | --dump-data <data> | --search-func <func> | |
| 52 | --search-data <data>] |
| 53 | [--sort-by-size | --sort-by-name | |
| 54 | --sort-by-obj | --sort-by-lib | |
| 55 | --sort-by-sec] [--desc | --asc] |
| 56 | file_input |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 57 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 58 | positional arguments: |
| 59 | file_input map or database file |
| 60 | |
| 61 | optional arguments: |
| 62 | -h, --help show this help message and exit |
| 63 | --gnuarm GNUARM model |
| 64 | --armcc ARMCLANG model |
| 65 | --db-name <data.db> database name to save |
| 66 | -u, --ui start UI to analyze |
| 67 | -S, --Summary show summary message |
| 68 | -s, --list-section list section |
| 69 | -l, --list-library list library |
| 70 | -o, --list-obj list object file |
| 71 | -f, --list-function list function |
| 72 | -d, --list-data list data |
| 73 | --dump-sec <sec> dump section |
| 74 | --dump-lib <lib> dump library |
| 75 | --dump-obj <obj> dump object file |
| 76 | --dump-func <func> dump function |
| 77 | --dump-data <data> dump data |
| 78 | --search-func <func> search function |
| 79 | --search-data <data> search data |
| 80 | --sort-by-size list by size order |
| 81 | --sort-by-name list by name order |
| 82 | --sort-by-obj list by object file name order |
| 83 | --sort-by-lib list by library file name order |
| 84 | --sort-by-sec list by section name order |
| 85 | --desc sort with desc order |
| 86 | --asc sort with asc order |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 87 | |
| 88 | Create database |
| 89 | =============== |
| 90 | |
| 91 | It is required to input map file path for the script before show the UI. One of |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 92 | the options like ``--gnuarm`` or ``--armcc`` is required. |
| 93 | |
| 94 | The default database name created is ``data.db``. Use ``--db-name`` to name the |
| 95 | output file if necessary. For example, saving two different databases to compare |
| 96 | later. |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 97 | |
| 98 | .. code-block:: bash |
| 99 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 100 | $: python code_size_analyze.py tfm_s.map <--gnuarm|--armcc> --db-name tfm_s.db |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 101 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 102 | UI mode |
| 103 | ======= |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 104 | |
| 105 | The script ui.py supplies a menu to choose what developers may be interested. |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 106 | You can enter UI mode by analyzing map file directly or by importing database |
| 107 | file path. The latter way is suggested as it runs more quickly. |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 108 | |
| 109 | .. code-block:: bash |
| 110 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 111 | $: python code_size_analyze.py tfm_s.map <--gnuarm|--armcc> -u |
| 112 | $: python code_size_analyze.py tfm_s.db -u |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 113 | |
| 114 | There are several keys to use UI. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 115 | |
| 116 | * UP: Move UP, mouse scrolling up is same. |
| 117 | * DOWN: Move down, mouse scrolling down is same. |
| 118 | * RIGHT: Move right. |
| 119 | * LEFT: Move left. |
| 120 | * Enter: Move to next page if it can be unfolded. |
| 121 | * ``Q`` or ``q``: Escape to previous page or close script if it in top menu. |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 122 | * ``s`` or ``S``: Enter output file name to save the content of current page. |
| 123 | * ``:`` : Start search and enter the function or data name. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 124 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 125 | Terminal mode |
| 126 | ============= |
| 127 | |
| 128 | In terminal mode, it is better to analyze database file rather than map file. |
| 129 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 130 | Dump detail information |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 131 | ----------------------- |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 132 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 133 | You can get the list of all sections, libraries, object files, functions or |
| 134 | data. You can also dump the specific symbol with the name. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 135 | |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 136 | .. code-block:: bash |
| 137 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 138 | $: python code_size_analyze.py tfm_s.map --armcc --db-name test.db -S |
| 139 | ─────────────────────────────────────────────── |
| 140 | Code size : 56676 55.35 KB |
| 141 | ----------------------------------------------- |
| 142 | RO data : 3732 3.64 KB |
| 143 | RW data : 204 0.20 KB |
| 144 | ZI data : 24588 24.01 KB |
| 145 | Flash size : 60612 59.19 KB = Code + RO + RW |
| 146 | RAM size : 24792 24.21 KB = RW + ZI |
| 147 | ─────────────────────────────────────────────── |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 148 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 149 | $: python code_size_analyze.py tfm_s.db -s |
| 150 | $: python code_size_analyze.py tfm_s.db --dump-sec <sec> |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 151 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 152 | Search specific function or data |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 153 | -------------------------------- |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 154 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 155 | You can search the target with keyword in command line. For example: |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 156 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 157 | .. code-block:: bash |
| 158 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 159 | $: python code_size_analyze.py tfm_s.db --search-func <func> |
| 160 | $: python code_size_analyze.py tfm_s.db --search-data <data> |
| 161 | |
| 162 | Sort Table |
| 163 | ---------- |
| 164 | |
| 165 | You can sort the messages in terminal mode. The script supplies five options and |
| 166 | two orders. For example: |
| 167 | |
| 168 | .. code-block:: bash |
| 169 | |
| 170 | $: python code_size_analyze.py tfm_s.db -l --sort-by-size --asc |
| 171 | ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
| 172 | Name Flash size RAM size Code RO data RW data ZI data Inc. data Debug |
| 173 | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| 174 | libtfm_qcbor_s.a 758 0 758 0 0 0 4 17046 |
| 175 | libtfm_sprt.a 1016 0 1016 0 0 0 0 41004 |
| 176 | c_w.l 1248 96 1248 0 0 96 86 1892 |
| 177 | libtfm_psa_rot_partition_attestation.a 2497 557 2492 5 0 557 68 51865 |
| 178 | libtfm_spm.a 4112 657 3932 136 44 613 168 52958 |
| 179 | libtfm_psa_rot_partition_its.a 5090 116 5030 32 28 88 28 49804 |
| 180 | libtfm_psa_rot_partition_crypto.a 6062 3232 6062 0 0 3232 36 92472 |
| 181 | libplatform_s.a 6486 316 5582 780 124 192 404 94887 |
| 182 | libmbedcrypto.a 28408 2292 26138 2262 8 2284 1066 226489 |
| 183 | ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
| 184 | |
| 185 | Not all symbols support to be sorted by the whole five options, refer to the |
| 186 | table to get more information. |
| 187 | |
| 188 | +----------------+---------+---------+--------------+----------+------+ |
| 189 | | Options | Section | Library | Object files | Function | Data | |
| 190 | +================+=========+=========+==============+==========+======+ |
| 191 | | --sort-by-size | √ | √ | √ | √ | √ | |
| 192 | +----------------+---------+---------+--------------+----------+------+ |
| 193 | | --sort-by-name | √ | √ | √ | √ | √ | |
| 194 | +----------------+---------+---------+--------------+----------+------+ |
| 195 | | --sort-by-sec | √ | | | √ | √ | |
| 196 | +----------------+---------+---------+--------------+----------+------+ |
| 197 | | --sort-by-lib | | √ | √ | √ | √ | |
| 198 | +----------------+---------+---------+--------------+----------+------+ |
| 199 | | --sort-by-obj | | | √ | √ | √ | |
| 200 | +----------------+---------+---------+--------------+----------+------+ |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 201 | |
| 202 | ******************* |
| 203 | Code size diff tool |
| 204 | ******************* |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 205 | |
| 206 | Use ``code_size_diff.py`` to diff two diffrent build results with same compiler. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 207 | |
| 208 | .. code-block:: bash |
| 209 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 210 | usage: code_size_diff.py [-h] (-S | -f | -d | -o | -l) based_db compared_db |
| 211 | |
| 212 | positional arguments: |
| 213 | based_db based databse |
| 214 | compared_db compared databse |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 215 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 216 | optional arguments: |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 217 | -h, --help show this help message and exit |
| 218 | -S, --diff-Summary diff summary |
| 219 | -f, --diff-function diff function |
| 220 | -d, --diff-data diff data |
| 221 | -o, --diff-obj diff object file |
| 222 | -l, --diff-lib diff library |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 223 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 224 | Firstly, use ``code_size_analyze.py`` to prepare two different databases. Then |
| 225 | compare two database with the diff tool, the branch1 is base. |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 226 | |
| 227 | .. code-block:: bash |
| 228 | |
Jianliang Shen | 28cce57 | 2022-05-02 21:44:03 +0800 | [diff] [blame] | 229 | $: python code_size_diff.py output/branch1.db output/branch2.db -S |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 230 | Code size: +++ 48928 B 47.78 KB |
| 231 | RO data: +++ 29440 B 28.75 KB |
| 232 | RW data: --- 64 B 0.06 KB |
| 233 | ZI data: --- 500 B 0.49 KB |
| 234 | Flash size: +++ 78304 B 76.47 KB |
| 235 | RAM size: --- 564 B 0.55 KB |
| 236 | |
Jianliang Shen | abf0041 | 2021-11-12 10:58:52 +0800 | [diff] [blame] | 237 | -------------- |
| 238 | |
Jianliang Shen | 9b4ba11 | 2022-01-19 14:57:04 +0800 | [diff] [blame] | 239 | *Copyright (c) 2021-2022, Arm Limited. All rights reserved.* |