blob: fe9c40bc40d8cfca5c8a73a3889622281a61ba8e [file] [log] [blame]
Anas Nashiff026b472021-06-04 13:09:47 -04001#!/usr/bin/env python3
2# Copyright (c) 2021 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5
6# A script to diff between two ram or rom reports generated by
7# size_report. When you call call the ram_report or rom_report targets you
8# end up with a json file in the build directory that can be used as input
9# for this script.
10
Nazar Kazakovf483b1b2022-03-16 21:07:43 +000011# The output shows which symbols increased and which decreased in size and
Anas Nashiff026b472021-06-04 13:09:47 -040012# also tracked added/remove symbols as well.
13
14# Example:
15# ./scripts/footprint/fpdiff.py ram1.json ram2.json
16
17from anytree.importer import DictImporter
18from anytree import PreOrderIter
19from anytree.search import find
20
Stephanos Ioannidis2ee02f92022-05-23 19:15:23 +090021import colorama
Anas Nashiff026b472021-06-04 13:09:47 -040022from colorama import Fore
23import json
24import argparse
25
26importer = DictImporter()
27
28def parse_args():
29 parser = argparse.ArgumentParser(
Jamie McCraeec704442023-01-04 16:08:36 +000030 description="Compare footprint sizes of two builds.", allow_abbrev=False)
Anas Nashiff026b472021-06-04 13:09:47 -040031 parser.add_argument("file1", help="First file")
32 parser.add_argument("file2", help="Second file")
33
34 return parser.parse_args()
35
36def main():
Stephanos Ioannidis2ee02f92022-05-23 19:15:23 +090037 colorama.init()
38
Anas Nashiff026b472021-06-04 13:09:47 -040039 args = parse_args()
40
41 with open(args.file1, "r") as f:
Anas Nashif88d42992022-05-04 13:43:51 -040042 data1 = json.load(f)
Anas Nashiff026b472021-06-04 13:09:47 -040043
44 with open(args.file2, "r") as f:
Anas Nashif88d42992022-05-04 13:43:51 -040045 data2 = json.load(f)
Anas Nashiff026b472021-06-04 13:09:47 -040046
Anas Nashif88d42992022-05-04 13:43:51 -040047 for idx, ch in enumerate(data1['symbols']['children']):
48 root1 = importer.import_(ch)
49 root2 = importer.import_(data2['symbols']['children'][idx])
50 print(f"{root1.name}\n+++++++++++++++++++++")
Anas Nashiff026b472021-06-04 13:09:47 -040051
Anas Nashif88d42992022-05-04 13:43:51 -040052 for node in PreOrderIter(root1):
53 # pylint: disable=undefined-loop-variable
54 n = find(root2, lambda node2: node2.identifier == node.identifier)
55 if n:
56 if n.size != node.size:
57 diff = n.size - node.size
58 if diff == 0:
59 continue
60 if not n.children or not n.parent:
61 if diff < 0:
62 print(f"{n.identifier} -> {Fore.GREEN}{diff}{Fore.RESET}")
63 else:
64 print(f"{n.identifier} -> {Fore.RED}+{diff}{Fore.RESET}")
Anas Nashiff026b472021-06-04 13:09:47 -040065
Anas Nashif88d42992022-05-04 13:43:51 -040066 else:
67 if not node.children:
68 print(f"{node.identifier} ({Fore.GREEN}-{node.size}{Fore.RESET}) disappeared.")
69
70 for node in PreOrderIter(root2):
71 n = find(root1, lambda node2: node2.identifier == node.identifier)
72 if not n:
73 if not node.children and node.size != 0:
74 print(f"{node.identifier} ({Fore.RED}+{node.size}{Fore.RESET}) is new.")
Anas Nashiff026b472021-06-04 13:09:47 -040075
76
77if __name__ == "__main__":
78 main()