fpdiff.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 Intel Corporation
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. # A script to diff between two ram or rom reports generated by
  6. # size_report. When you call call the ram_report or rom_report targets you
  7. # end up with a json file in the build directory that can be used as input
  8. # for this script.
  9. # The output shows which symbols insreased and which decreased in size and
  10. # also tracked added/remove symbols as well.
  11. # Example:
  12. # ./scripts/footprint/fpdiff.py ram1.json ram2.json
  13. from anytree.importer import DictImporter
  14. from anytree import PreOrderIter
  15. from anytree.search import find
  16. from colorama import Fore
  17. import json
  18. import argparse
  19. importer = DictImporter()
  20. def parse_args():
  21. parser = argparse.ArgumentParser(
  22. description="Compare footprint sizes of two builds.")
  23. parser.add_argument("file1", help="First file")
  24. parser.add_argument("file2", help="Second file")
  25. return parser.parse_args()
  26. def main():
  27. args = parse_args()
  28. with open(args.file1, "r") as f:
  29. data = json.load(f)
  30. root1 = importer.import_(data['symbols'])
  31. with open(args.file2, "r") as f:
  32. data = json.load(f)
  33. root2 = importer.import_(data['symbols'])
  34. for node in PreOrderIter(root1):
  35. # pylint: disable=undefined-loop-variable
  36. n = find(root2, lambda node2: node2.identifier == node.identifier)
  37. if n:
  38. if n.size != node.size:
  39. diff = n.size - node.size
  40. if diff == 0:
  41. continue
  42. if not n.children or not n.parent:
  43. if diff < 0:
  44. print(f"{n.identifier} -> {Fore.GREEN}{diff}{Fore.RESET}")
  45. else:
  46. print(f"{n.identifier} -> {Fore.RED}+{diff}{Fore.RESET}")
  47. else:
  48. if not node.children:
  49. print(f"{node.identifier} ({Fore.GREEN}-{node.size}{Fore.RESET}) disappeared.")
  50. for node in PreOrderIter(root2):
  51. n = find(root1, lambda node2: node2.identifier == node.identifier)
  52. if not n:
  53. if not node.children and node.size != 0:
  54. print(f"{node.identifier} ({Fore.RED}+{node.size}{Fore.RESET}) is new.")
  55. if __name__ == "__main__":
  56. main()