upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

size.py 4.1KB

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