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