mbed-os
Dependents: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
tools/size.py@0:b74591d5ab33, 2017-12-11 (annotated)
- Committer:
- be_bryan
- Date:
- Mon Dec 11 17:54:04 2017 +0000
- Revision:
- 0:b74591d5ab33
motor ++
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
be_bryan | 0:b74591d5ab33 | 1 | """ |
be_bryan | 0:b74591d5ab33 | 2 | mbed SDK |
be_bryan | 0:b74591d5ab33 | 3 | Copyright (c) 2011-2013 ARM Limited |
be_bryan | 0:b74591d5ab33 | 4 | |
be_bryan | 0:b74591d5ab33 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
be_bryan | 0:b74591d5ab33 | 6 | you may not use this file except in compliance with the License. |
be_bryan | 0:b74591d5ab33 | 7 | You may obtain a copy of the License at |
be_bryan | 0:b74591d5ab33 | 8 | |
be_bryan | 0:b74591d5ab33 | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
be_bryan | 0:b74591d5ab33 | 10 | |
be_bryan | 0:b74591d5ab33 | 11 | Unless required by applicable law or agreed to in writing, software |
be_bryan | 0:b74591d5ab33 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
be_bryan | 0:b74591d5ab33 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
be_bryan | 0:b74591d5ab33 | 14 | See the License for the specific language governing permissions and |
be_bryan | 0:b74591d5ab33 | 15 | limitations under the License. |
be_bryan | 0:b74591d5ab33 | 16 | """ |
be_bryan | 0:b74591d5ab33 | 17 | import sys |
be_bryan | 0:b74591d5ab33 | 18 | from os.path import join, abspath, dirname, exists, splitext |
be_bryan | 0:b74591d5ab33 | 19 | from subprocess import Popen, PIPE |
be_bryan | 0:b74591d5ab33 | 20 | import csv |
be_bryan | 0:b74591d5ab33 | 21 | from collections import defaultdict |
be_bryan | 0:b74591d5ab33 | 22 | |
be_bryan | 0:b74591d5ab33 | 23 | ROOT = abspath(join(dirname(__file__), "..")) |
be_bryan | 0:b74591d5ab33 | 24 | sys.path.insert(0, ROOT) |
be_bryan | 0:b74591d5ab33 | 25 | |
be_bryan | 0:b74591d5ab33 | 26 | from tools.paths import BUILD_DIR, TOOLS_DATA |
be_bryan | 0:b74591d5ab33 | 27 | from tools.settings import GCC_ARM_PATH |
be_bryan | 0:b74591d5ab33 | 28 | from tools.tests import TEST_MAP |
be_bryan | 0:b74591d5ab33 | 29 | from tools.build_api import build_mbed_libs, build_project |
be_bryan | 0:b74591d5ab33 | 30 | |
be_bryan | 0:b74591d5ab33 | 31 | SIZE = join(GCC_ARM_PATH, 'arm-none-eabi-size') |
be_bryan | 0:b74591d5ab33 | 32 | |
be_bryan | 0:b74591d5ab33 | 33 | def get_size(path): |
be_bryan | 0:b74591d5ab33 | 34 | out = Popen([SIZE, path], stdout=PIPE).communicate()[0] |
be_bryan | 0:b74591d5ab33 | 35 | return map(int, out.splitlines()[1].split()[:4]) |
be_bryan | 0:b74591d5ab33 | 36 | |
be_bryan | 0:b74591d5ab33 | 37 | def get_percentage(before, after): |
be_bryan | 0:b74591d5ab33 | 38 | if before == 0: |
be_bryan | 0:b74591d5ab33 | 39 | return 0 if after == 0 else 100.0 |
be_bryan | 0:b74591d5ab33 | 40 | return float(after - before) / float(before) * 100.0 |
be_bryan | 0:b74591d5ab33 | 41 | |
be_bryan | 0:b74591d5ab33 | 42 | def human_size(val): |
be_bryan | 0:b74591d5ab33 | 43 | if val>1024: |
be_bryan | 0:b74591d5ab33 | 44 | return "%.0fKb" % (float(val)/1024.0) |
be_bryan | 0:b74591d5ab33 | 45 | return "%d" % val |
be_bryan | 0:b74591d5ab33 | 46 | |
be_bryan | 0:b74591d5ab33 | 47 | def print_diff(name, before, after): |
be_bryan | 0:b74591d5ab33 | 48 | print "%s: (%s -> %s) %.2f%%" % (name, human_size(before) , human_size(after) , get_percentage(before , after)) |
be_bryan | 0:b74591d5ab33 | 49 | |
be_bryan | 0:b74591d5ab33 | 50 | BENCHMARKS = [ |
be_bryan | 0:b74591d5ab33 | 51 | ("BENCHMARK_1", "CENV"), |
be_bryan | 0:b74591d5ab33 | 52 | ("BENCHMARK_2", "PRINTF"), |
be_bryan | 0:b74591d5ab33 | 53 | ("BENCHMARK_3", "FP"), |
be_bryan | 0:b74591d5ab33 | 54 | ("BENCHMARK_4", "MBED"), |
be_bryan | 0:b74591d5ab33 | 55 | ("BENCHMARK_5", "ALL"), |
be_bryan | 0:b74591d5ab33 | 56 | ] |
be_bryan | 0:b74591d5ab33 | 57 | BENCHMARK_DATA_PATH = join(TOOLS_DATA, 'benchmarks.csv') |
be_bryan | 0:b74591d5ab33 | 58 | |
be_bryan | 0:b74591d5ab33 | 59 | |
be_bryan | 0:b74591d5ab33 | 60 | def benchmarks(): |
be_bryan | 0:b74591d5ab33 | 61 | # CSV Data |
be_bryan | 0:b74591d5ab33 | 62 | csv_data = csv.writer(open(BENCHMARK_DATA_PATH, 'wb')) |
be_bryan | 0:b74591d5ab33 | 63 | csv_data.writerow(['Toolchain', "Target", "Benchmark", "code", "data", "bss", "flash"]) |
be_bryan | 0:b74591d5ab33 | 64 | |
be_bryan | 0:b74591d5ab33 | 65 | # Build |
be_bryan | 0:b74591d5ab33 | 66 | for toolchain in ['ARM', 'uARM', 'GCC_CR', 'GCC_ARM']: |
be_bryan | 0:b74591d5ab33 | 67 | for mcu in ["LPC1768", "LPC11U24"]: |
be_bryan | 0:b74591d5ab33 | 68 | # Build Libraries |
be_bryan | 0:b74591d5ab33 | 69 | build_mbed_libs(mcu, toolchain) |
be_bryan | 0:b74591d5ab33 | 70 | |
be_bryan | 0:b74591d5ab33 | 71 | # Build benchmarks |
be_bryan | 0:b74591d5ab33 | 72 | build_dir = join(BUILD_DIR, "benchmarks", mcu, toolchain) |
be_bryan | 0:b74591d5ab33 | 73 | for test_id, title in BENCHMARKS: |
be_bryan | 0:b74591d5ab33 | 74 | # Build Benchmark |
be_bryan | 0:b74591d5ab33 | 75 | try: |
be_bryan | 0:b74591d5ab33 | 76 | test = TEST_MAP[test_id] |
be_bryan | 0:b74591d5ab33 | 77 | path = build_project(test.source_dir, join(build_dir, test_id), |
be_bryan | 0:b74591d5ab33 | 78 | mcu, toolchain, test.dependencies) |
be_bryan | 0:b74591d5ab33 | 79 | base, ext = splitext(path) |
be_bryan | 0:b74591d5ab33 | 80 | # Check Size |
be_bryan | 0:b74591d5ab33 | 81 | code, data, bss, flash = get_size(base+'.elf') |
be_bryan | 0:b74591d5ab33 | 82 | csv_data.writerow([toolchain, mcu, title, code, data, bss, flash]) |
be_bryan | 0:b74591d5ab33 | 83 | except Exception, e: |
be_bryan | 0:b74591d5ab33 | 84 | print "Unable to build %s for toolchain %s targeting %s" % (test_id, toolchain, mcu) |
be_bryan | 0:b74591d5ab33 | 85 | print e |
be_bryan | 0:b74591d5ab33 | 86 | |
be_bryan | 0:b74591d5ab33 | 87 | |
be_bryan | 0:b74591d5ab33 | 88 | def compare(t1, t2, target): |
be_bryan | 0:b74591d5ab33 | 89 | if not exists(BENCHMARK_DATA_PATH): |
be_bryan | 0:b74591d5ab33 | 90 | benchmarks() |
be_bryan | 0:b74591d5ab33 | 91 | else: |
be_bryan | 0:b74591d5ab33 | 92 | print "Loading: %s" % BENCHMARK_DATA_PATH |
be_bryan | 0:b74591d5ab33 | 93 | |
be_bryan | 0:b74591d5ab33 | 94 | data = csv.reader(open(BENCHMARK_DATA_PATH, 'rb')) |
be_bryan | 0:b74591d5ab33 | 95 | |
be_bryan | 0:b74591d5ab33 | 96 | benchmarks_data = defaultdict(dict) |
be_bryan | 0:b74591d5ab33 | 97 | for (toolchain, mcu, name, code, data, bss, flash) in data: |
be_bryan | 0:b74591d5ab33 | 98 | if target == mcu: |
be_bryan | 0:b74591d5ab33 | 99 | for t in [t1, t2]: |
be_bryan | 0:b74591d5ab33 | 100 | if toolchain == t: |
be_bryan | 0:b74591d5ab33 | 101 | benchmarks_data[name][t] = map(int, (code, data, bss, flash)) |
be_bryan | 0:b74591d5ab33 | 102 | |
be_bryan | 0:b74591d5ab33 | 103 | print "%s vs %s for %s" % (t1, t2, target) |
be_bryan | 0:b74591d5ab33 | 104 | for name, data in benchmarks_data.iteritems(): |
be_bryan | 0:b74591d5ab33 | 105 | try: |
be_bryan | 0:b74591d5ab33 | 106 | # Check Size |
be_bryan | 0:b74591d5ab33 | 107 | code_a, data_a, bss_a, flash_a = data[t1] |
be_bryan | 0:b74591d5ab33 | 108 | code_u, data_u, bss_u, flash_u = data[t2] |
be_bryan | 0:b74591d5ab33 | 109 | |
be_bryan | 0:b74591d5ab33 | 110 | print "\n=== %s ===" % name |
be_bryan | 0:b74591d5ab33 | 111 | print_diff("code", code_a , code_u) |
be_bryan | 0:b74591d5ab33 | 112 | print_diff("data", data_a , data_u) |
be_bryan | 0:b74591d5ab33 | 113 | print_diff("bss", bss_a , bss_u) |
be_bryan | 0:b74591d5ab33 | 114 | print_diff("flash", flash_a , flash_u) |
be_bryan | 0:b74591d5ab33 | 115 | except Exception, e: |
be_bryan | 0:b74591d5ab33 | 116 | print "No data for benchmark %s" % (name) |
be_bryan | 0:b74591d5ab33 | 117 | print e |
be_bryan | 0:b74591d5ab33 | 118 | |
be_bryan | 0:b74591d5ab33 | 119 | |
be_bryan | 0:b74591d5ab33 | 120 | if __name__ == '__main__': |
be_bryan | 0:b74591d5ab33 | 121 | compare("GCC_CR", "LPC1768") |