diffsyms.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2021 Project CHIP Authors
  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. """Report differences in size of individual symbols between two files."""
  18. import sys
  19. import memdf.collect
  20. import memdf.report
  21. import memdf.select
  22. import pandas as pd
  23. from memdf import Config, ConfigDescription, SymbolDF
  24. CONFIG: ConfigDescription = {
  25. **memdf.util.config.CONFIG,
  26. **memdf.collect.CONFIG,
  27. **memdf.select.CONFIG,
  28. **memdf.report.REPORT_CONFIG,
  29. **memdf.report.OUTPUT_CONFIG,
  30. }
  31. def main(argv):
  32. status = 0
  33. try:
  34. config = Config().init(CONFIG)
  35. config.argparse.add_argument('inputs', metavar='FILE', nargs=2)
  36. config.parse(argv)
  37. config['args.fill_holes'] = False
  38. inputs = config.get('args.inputs')
  39. a_dfs = memdf.collect.collect_files(config, files=[inputs[0]])
  40. b_dfs = memdf.collect.collect_files(config, files=[inputs[1]])
  41. a_syms = a_dfs[SymbolDF.name].sort_values(by='symbol',
  42. ignore_index=True)
  43. b_syms = b_dfs[SymbolDF.name].sort_values(by='symbol',
  44. ignore_index=True)
  45. # TBD: Differences other than size, configurably.
  46. differences = []
  47. ai = a_syms.itertuples()
  48. bi = b_syms.itertuples()
  49. a = next(ai, None)
  50. b = next(bi, None)
  51. while a and b:
  52. if a.symbol < b.symbol:
  53. differences.append((-a.size, a.size, 0, a.symbol))
  54. a = next(ai, None)
  55. continue
  56. if a.symbol > b.symbol:
  57. differences.append((b.size, 0, b.size, b.symbol))
  58. b = next(bi, None)
  59. continue
  60. if a.size != b.size:
  61. differences.append((b.size - a.size, a.size, b.size, a.symbol))
  62. a = next(ai, None)
  63. b = next(bi, None)
  64. for a in ai:
  65. differences.append((-a.size, a.size, 0, a.symbol))
  66. for b in bi:
  67. differences.append((b.size, 0, b.size, b.symbol))
  68. df = pd.DataFrame(differences,
  69. columns=['change', 'a-size', 'b-size', 'symbol'])
  70. if config['report.demangle']:
  71. # Demangle early to sort by demangled name.
  72. df['symbol'] = df['symbol'].apply(memdf.report.demangle)
  73. config['report.demangle'] = False
  74. df.sort_values(by=['change', 'symbol'], ascending=[False, True],
  75. inplace=True)
  76. memdf.report.write_dfs(config, {'Differences': df})
  77. except Exception as exception:
  78. raise exception
  79. return status
  80. if __name__ == '__main__':
  81. sys.exit(main(sys.argv))