Skip to content

Commit

Permalink
[Runtime] Remove RuntimeFunctionCounters in no-assert builds.
Browse files Browse the repository at this point in the history
A build option to include or exclude RuntimeFunctionCounters. By default, it's enabled when assertions are enabled.

rdar://problem/35864525
  • Loading branch information
mikeash committed Jan 25, 2018
1 parent 92a2f9c commit f48bd29
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ option(SWIFT_ENABLE_GUARANTEED_NORMAL_ARGUMENTS
"Build the standard libraries, overlays, and runtime with normal arguments at +0"
FALSE)

option(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS
"Enable runtime function counters and expose the API. Defaults to on if assertions are on."
${SWIFT_STDLIB_ASSERTIONS})

#
# End of user-configurable options.
#
Expand Down
8 changes: 8 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ function(_add_variant_c_compile_flags)
else()
list(APPEND result "-DNDEBUG")
endif()

if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
list(APPEND result "-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
endif()

if(CFLAGS_ANALYZE_CODE_COVERAGE)
list(APPEND result "-fprofile-instr-generate"
Expand Down Expand Up @@ -316,6 +320,10 @@ function(_add_variant_swift_compile_flags
if (SWIFT_ENABLE_GUARANTEED_NORMAL_ARGUMENTS)
list(APPEND result "-Xfrontend" "-enable-guaranteed-normal-arguments")
endif()

if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
endif()

set("${result_var_name}" "${result}" PARENT_SCOPE)
endfunction()
Expand Down
9 changes: 9 additions & 0 deletions stdlib/public/core/RuntimeFunctionCounters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
// number of invocations, or per-object counters, which represent the
// number of runtime functions calls for a specific object.

// By default, this feature is enabled only when assertions are enabled. To enable when
// assertions are off, use this option with build-script:
// --extra-cmake-options='-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS=TRUE'
// Or to disable in debug builds:
// --extra-cmake-options='-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS=FALSE'
#if SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS

/// Collect all references inside the object using Mirrors.
/// - Parameter value: the value to be inspected
/// - Parameter references: the array which should contain the collected
Expand Down Expand Up @@ -541,3 +548,5 @@ func _measureRuntimeFunctionCountersDiffs(
mode: savedMode)
return (globalCountersBefore.diff(globalCountersAfter), objectsCountersDiff)
}

#endif
7 changes: 3 additions & 4 deletions stdlib/public/runtime/RuntimeInvocationsTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Mutex.h"

// This file is compiled always, even if assertions are disabled and no runtime
// functions are being tracked. This is done to avoid recompiling Swift clients
// using these APIs. They should be able to link against the standard library
// independent of the fact whether assertions are enabled or not.
#if defined(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)

#define SWIFT_RT_FUNCTION_INVOCATION_COUNTER_NAME(RT_FUNCTION) \
invocationCounter_##RT_FUNCTION
Expand Down Expand Up @@ -238,3 +235,5 @@ _swift_setGlobalRuntimeFunctionCountersUpdateHandler(
/// TODO: Provide an API to remove any counters releated to a specific object
/// or all objects.
/// This is useful if you want to reset the stats for some/all objects.

#endif
22 changes: 11 additions & 11 deletions stdlib/public/runtime/RuntimeInvocationsTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include "swift/Runtime/Config.h"

/// This API is only enabled if this define is set.
#if defined(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)

#if defined(__cplusplus)

namespace swift {
Expand All @@ -39,10 +42,6 @@ struct RuntimeFunctionCountersState;
#define SWIFT_RT_TRACK_INVOCATION_NAME(RT_FUNCTION) \
swift_trackRuntimeInvocation_##RT_FUNCTION

/// Instrument the runtime functions only if we are building with
/// assertions enabled.
#if !defined(NDEBUG)

/// Invoke a helper function for tracking the calls of a runtime function.
#define SWIFT_RT_TRACK_INVOCATION(OBJ, RT_FUNCTION) \
SWIFT_RT_TRACK_INVOCATION_NAME(RT_FUNCTION)(OBJ)
Expand All @@ -52,13 +51,6 @@ struct RuntimeFunctionCountersState;
/// Declarations of external functions for invocations tracking.
#include "RuntimeInvocationsTracking.def"

#else

/// It is just a NOP if assertions are not enabled.
#define SWIFT_RT_TRACK_INVOCATION(OBJ, RT_FUNCTION)

#endif // NDEBUG

/// This type defines a callback to be called on any intercepted runtime
/// function.
using RuntimeFunctionCountersUpdateHandler =
Expand Down Expand Up @@ -118,4 +110,12 @@ SWIFT_RUNTIME_EXPORT RuntimeFunctionCountersUpdateHandler
_swift_setGlobalRuntimeFunctionCountersUpdateHandler(
RuntimeFunctionCountersUpdateHandler handler);

#else

/// Let runtime functions uncoditionally use this define by making it a NOP if
/// counters are not enabled.
#define SWIFT_RT_TRACK_INVOCATION(OBJ, RT_FUNCTION)

#endif // SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS

#endif

0 comments on commit f48bd29

Please sign in to comment.