From dc0b4bd2c93c6a58002ca9c0b0b36b07006cf7d3 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Fri, 4 Nov 2016 09:47:41 -0400 Subject: [PATCH] Add debugging info about .so size to build output (#477) * Add debugging info about so size to build output This adds a small python script to tools that captures before-and-after .so sizes between builds and outputs this in the build output via a string such as: ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 924696 (decrease of 73680 bytes = 7.38%) ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 (increase of 73680 bytes = 7.97%) ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 (no change) Or, if there was no .so during the build, just the .so size by itself: ------ pybind11_tests.cpython-35m-x86_64-linux-gnu.so file size: 998376 This allows you to, for example, build, checkout a different branch, rebuild, and easily see exactly the change in the pybind11_tests.so size. It also allows looking at the travis and appveyor build logs to get an idea of .so/.dll sizes across different build systems. * Minor libsize.py script changes - Use RAII open - Remove unused libsize=-1 - Report change as [+-]xyz bytes = [+-]a.bc% --- .gitignore | 1 + tests/CMakeLists.txt | 5 +++++ tools/libsize.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tools/libsize.py diff --git a/.gitignore b/.gitignore index ab667585..4df6229a 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ MANIFEST /build /cmake/ .cache/ +sosize-*.txt diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7470c5d9..0bc0cd08 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -89,3 +89,8 @@ endif() # A single command to compile and run the tests add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES} DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir}) + +# And another to show the .so size and, if a previous size, compare it: +add_custom_command(TARGET pybind11_tests POST_BUILD + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/libsize.py + $ ${CMAKE_CURRENT_BINARY_DIR}/sosize-$.txt) diff --git a/tools/libsize.py b/tools/libsize.py new file mode 100644 index 00000000..70b811d4 --- /dev/null +++ b/tools/libsize.py @@ -0,0 +1,38 @@ +from __future__ import print_function +import os +import sys + +# Internal build script for generating debugging test .so size. +# Usage: +# python libsize.py file.so save.txt -- displays the size of file.so and, if save.txt exists, compares it to the +# size in it, then overwrites save.txt with the new size for future runs. + +if len(sys.argv) != 3: + sys.exit("Invalid arguments: usage: python libsize.py file.so save.txt") + +lib = sys.argv[1] +save = sys.argv[2] + +if not os.path.exists(lib): + sys.exit("Error: requested file ({}) does not exist".format(lib)) + +libsize = os.path.getsize(lib) + +print("------", os.path.basename(lib), "file size:", libsize, end='') + +if os.path.exists(save): + with open(save) as sf: + oldsize = int(sf.readline()) + + if oldsize > 0: + change = libsize - oldsize + if change == 0: + print(" (no change)") + else: + print(" (change of {:+} bytes = {:+.2%})".format(change, change / oldsize)) +else: + print() + +with open(save, 'w') as sf: + sf.write(str(libsize)) +