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