Skip to content

Commit

Permalink
Update profiling tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Davinco authored Sep 9, 2020
1 parent 211ebcc commit abfa56d
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 265 deletions.
95 changes: 56 additions & 39 deletions Bloaty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,59 @@
# swift_add_bloaty(<target>
# [OPTIONS]
# [WORKING_DIRECTORY working_directory]
# [REPORT_DIRECTORY report_directory]
# )
#
# Call this function to create a new cmake target which runs the `target`'s
# executable binary with bloaty applied. All created cmake targets can be
# invoked by calling the common target 'do-all-bloaty'.
# executable binary with bloaty applied. All targets created with
# `swift_add_bloaty` can be invoked by calling the common target 'do-all-bloaty'.
#
# BLOATY OPTIONS
#
# * SEGMENTS: Outputs what the run-time loader uses to determine what
# parts of the binary needs to be loaded/mapped into memory.
# * SECTIONS: Outputs the binary in finer details structured in different sections.
# * SYMBOLS: Outputs individual functions or variables.
# * COMPILEUNITS: Outputs what compile unit (and corresponding source file)
# each bit of the binary came from.
# * SEGMENTS: Outputs what the run-time loader uses to determine what
# parts of the binary needs to be loaded/mapped into memory.
# * SECTIONS: Outputs the binary in finer details structured in different sections.
# * SYMBOLS: Outputs individual functions or variables.
# * COMPILEUNITS: Outputs what compile unit (and corresponding source file)
# each bit of the binary came from.
#
# * NUM: Set how many rows to show per level before collapsing into '[other]'.
# Set to '0' for unlimited, default: '20'.
# * NUM: Set how many rows to show per level before collapsing into '[other]'.
# Set to '0' for unlimited, default: '20'.
#
# * SORT:
# vm - Sort the vm size column from largest to smallest and tells you
# how much space the binary will take when loaded into memory.
# file - Sort the file size column from largest to smallest and tells you
# how much space the binary is taking on disk.
# both - Default, sorts by max(vm, file).
# * SORT:
# vm - Sort the vm size column from largest to smallest and tells you
# how much space the binary will take when loaded into memory.
# file - Sort the file size column from largest to smallest and tells you
# how much space the binary is taking on disk.
# both - Default, sorts by max(vm, file).
#
# WORKING_DIRECTORY
# This variable changes the execution directory for the tool from the default
# folder `${CMAKE_CURRENT_BINARY_DIR}` to the given argument. For instance, if
# a user wants to utilize files located in a specific folder.
#
# REPORT_DIRECTORY
# This variable changes the output directory for the tool from the default folder
# `${CMAKE_BINARY_DIR}/profiling/bloaty-reports` to the given argument.
# Example, using argument `/tmp`, outputs the results to `/tmp/bloaty-reports/
# Example, using argument `/tmp`, outputs the result to `/tmp`.
#
# NOTE
#
# * A cmake option is available to control whether targets should be built,
# with the name ${PROJECT_NAME}_ENABLE_MEMORY_PROFILING.
# * A cmake option is available to control whether targets should be built,
# with the name ${PROJECT_NAME}_ENABLE_PROFILING.
#
# Running
#
# cmake -D<project>_ENABLE_MEMORY_PROFILING=ON ..
# cmake -D<project>_ENABLE_PROFILING=ON ..
#
# will explicitly enable these targets from the command line at configure time
# will explicitly enable these targets from the command line at configure time.
#

option(${PROJECT_NAME}_ENABLE_MEMORY_PROFILING "Builds targets with memory profiling" OFF)
option(${PROJECT_NAME}_ENABLE_PROFILING "Builds targets with profiling applied" OFF)

find_package(Bloaty)

if (NOT Bloaty_FOUND AND ${PROJECT_NAME}_ENABLE_MEMORY_PROFILING)
if (NOT Bloaty_FOUND AND ${PROJECT_NAME}_ENABLE_PROFILING)
message(STATUS "Bloaty is not installed on system, will fetch content from source")

cmake_minimum_required(VERSION 3.14.0)
Expand All @@ -77,23 +83,24 @@ macro(eval_bloaty_target target)

get_target_property(target_type ${target} TYPE)
if (NOT target_type STREQUAL EXECUTABLE)
message(FATAL_ERROR "Specified target \"${target}\" must be an executable type to register for profiling with bloaty")
message(FATAL_ERROR "Specified target \"${target}\" must be an executable type to register for profiling with Bloaty")
endif()

if (NOT ${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})
if (NOT (${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME} AND
${PROJECT_NAME}_ENABLE_PROFILING))
return()
endif()

if (NOT ${PROJECT_NAME}_ENABLE_MEMORY_PROFILING)
return()
if (NOT TARGET do-all-memory-profiling)
add_custom_target(do-all-memory-profiling)
endif()
endmacro()

function(swift_add_bloaty target)
eval_bloaty_target(${target})

set(argOption SEGMENTS SECTIONS SYMBOLS COMPILEUNITS)
set(argSingle NUM SORT WORKING_DIRECTORY)
set(argSingle NUM SORT WORKING_DIRECTORY REPORT_DIRECTORY)
set(argMulti "")

cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
Expand All @@ -103,12 +110,17 @@ function(swift_add_bloaty target)
endif()

set(target_name bloaty-${target})
set(working_directory ${CMAKE_BINARY_DIR}/profiling)
set(output_file ${target_name}.txt)

set(working_directory ${CMAKE_CURRENT_BINARY_DIR})
if (x_WORKING_DIRECTORY)
set(working_directory ${x_WORKING_DIRECTORY})
endif()
set(reports_directory ${working_directory}/bloaty-reports)
set(output_file ${target_name}.txt)

set(report_directory ${CMAKE_BINARY_DIR}/profiling/bloaty-reports)
if (x_REPORT_DIRECTORY)
set(report_directory ${x_REPORT_DIRECTORY})
endif()

unset(resource_options)
if (x_SEGMENTS)
Expand Down Expand Up @@ -142,24 +154,29 @@ function(swift_add_bloaty target)

if (NOT Bloaty_FOUND)
add_custom_target(${target_name}
COMMENT "bloaty is running on ${target}\ (output: \"${reports_directory}/${output_file}\")"
COMMAND ${CMAKE_COMMAND} -E make_directory ${reports_directory}
COMMAND ${CMAKE_COMMAND} -E chdir ${reports_directory} echo \"bloaty with options: ${resource_options}\" > ${reports_directory}/${output_file}
COMMAND ${CMAKE_COMMAND} -E env $<TARGET_FILE:bloaty> ${resource_options} $<TARGET_FILE:${target}> >> ${reports_directory}/${output_file}
COMMAND $(MAKE) --directory=${bloaty_BINARY_DIR}
COMMENT "Bloaty is running on ${target}\ (output: \"${report_directory}/${output_file}\")"
COMMAND ${CMAKE_COMMAND} -E make_directory ${report_directory}
COMMAND ${CMAKE_COMMAND} -E echo \"bloaty with options: ${resource_options}\" > ${report_directory}/${output_file}
COMMAND ${bloaty_BINARY_DIR}/bloaty ${resource_options} $<TARGET_FILE:${target}> >> ${report_directory}/${output_file}
WORKING_DIRECTORY ${working_directory}
DEPENDS ${target}
)
else()
add_custom_target(${target_name}
COMMENT "bloaty is running on ${target}\ (output: \"${reports_directory}/${output_file}\")"
COMMAND ${CMAKE_COMMAND} -E make_directory ${reports_directory}
COMMAND ${CMAKE_COMMAND} -E chdir ${reports_directory} echo \"bloaty with options: ${resource_options}\" > ${reports_directory}/${output_file}
COMMAND ${CMAKE_COMMAND} -E env ${Bloaty_EXECUTABLE} ${resource_options} $<TARGET_FILE:${target}> >> ${reports_directory}/${output_file}
COMMENT "Bloaty is running on ${target}\ (output: \"${report_directory}/${output_file}\")"
COMMAND ${CMAKE_COMMAND} -E make_directory ${report_directory}
COMMAND ${CMAKE_COMMAND} -E echo \"bloaty with options: ${resource_options}\" > ${report_directory}/${output_file}
COMMAND ${Bloaty_EXECUTABLE} ${resource_options} $<TARGET_FILE:${target}> >> ${report_directory}/${output_file}
WORKING_DIRECTORY ${working_directory}
DEPENDS ${target}
)
endif()

if (NOT TARGET do-all-bloaty)
add_custom_target(do-all-bloaty)
endif()

add_dependencies(do-all-bloaty ${target_name})
add_dependencies(do-all-memory-profiling do-all-bloaty)
endfunction()
Loading

0 comments on commit abfa56d

Please sign in to comment.