Skip to content

Commit 68d7778

Browse files
committed
Address post-commit review comments on PR swiftlang#11910
- Use SWIFT_RUNTIME_EXPORT instead of SWIFT_RT_ENTRY_VISIBILITY for exposed functions - Use `_swift_` prefixes on the names of exposed functions - Make the global counters and per-object counters cache thread-safe by using locks
1 parent 36e6c29 commit 68d7778

File tree

3 files changed

+85
-72
lines changed

3 files changed

+85
-72
lines changed

stdlib/public/core/RuntimeFunctionCounters.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct _RuntimeFunctionCounters {
110110

111111
/// Get the names of all runtime functions whose calls are being
112112
/// tracked.
113-
@_silgen_name("getRuntimeFunctionNames")
113+
@_silgen_name("_swift_getRuntimeFunctionNames")
114114
static public func _getRuntimeFunctionNames() ->
115115
UnsafePointer<UnsafePointer<CChar>>
116116

@@ -129,21 +129,21 @@ struct _RuntimeFunctionCounters {
129129

130130
/// Get the offsets of the collected runtime function counters inside
131131
/// the state.
132-
@_silgen_name("getRuntimeFunctionCountersOffsets")
132+
@_silgen_name("_swift_getRuntimeFunctionCountersOffsets")
133133
static public func getRuntimeFunctionCountersOffsets() ->
134134
UnsafePointer<UInt16>
135135

136136
/// Get the number of different runtime functions whose calls are being
137137
/// tracked.
138-
@_silgen_name("getNumRuntimeFunctionCounters")
138+
@_silgen_name("_swift_getNumRuntimeFunctionCounters")
139139
static public func getNumRuntimeFunctionCounters() -> Int
140140

141141
/// Dump all per-object runtime function counters.
142-
@_silgen_name("dumpObjectsRuntimeFunctionPointers")
142+
@_silgen_name("_swift_dumpObjectsRuntimeFunctionPointers")
143143
static public func dumpObjectsRuntimeFunctionPointers()
144144

145145
@discardableResult
146-
@_silgen_name("setGlobalRuntimeFunctionCountersUpdateHandler")
146+
@_silgen_name("_swift_setGlobalRuntimeFunctionCountersUpdateHandler")
147147
static public func setGlobalRuntimeFunctionCountersUpdateHandler(
148148
handler: RuntimeFunctionCountersUpdateHandler?
149149
) -> RuntimeFunctionCountersUpdateHandler?
@@ -312,31 +312,31 @@ internal struct _RuntimeFunctionCountersState: _RuntimeFunctionCountersStats {
312312
}
313313

314314
extension _RuntimeFunctionCounters {
315-
@_silgen_name("getObjectRuntimeFunctionCounters")
315+
@_silgen_name("_swift_getObjectRuntimeFunctionCounters")
316316
static internal func getObjectRuntimeFunctionCounters(
317317
_ object: UnsafeRawPointer, _ result: inout _RuntimeFunctionCountersState)
318318

319-
@_silgen_name("getGlobalRuntimeFunctionCounters")
319+
@_silgen_name("_swift_getGlobalRuntimeFunctionCounters")
320320
static internal func getGlobalRuntimeFunctionCounters(
321321
_ result: inout _RuntimeFunctionCountersState)
322322

323-
@_silgen_name("setGlobalRuntimeFunctionCounters")
323+
@_silgen_name("_swift_setGlobalRuntimeFunctionCounters")
324324
static internal func setGlobalRuntimeFunctionCounters(
325325
_ state: inout _RuntimeFunctionCountersState)
326326

327-
@_silgen_name("setObjectRuntimeFunctionCounters")
327+
@_silgen_name("_swift_setObjectRuntimeFunctionCounters")
328328
static internal func setObjectRuntimeFunctionCounters(
329329
_ object: UnsafeRawPointer,
330330
_ state: inout _RuntimeFunctionCountersState)
331331

332332
@discardableResult
333-
@_silgen_name("setGlobalRuntimeFunctionCountersMode")
333+
@_silgen_name("_swift_setGlobalRuntimeFunctionCountersMode")
334334
static
335335
public // @testable
336336
func setGlobalRuntimeFunctionCountersMode(enable: Bool) -> Bool
337337

338338
@discardableResult
339-
@_silgen_name("setPerObjectRuntimeFunctionCountersMode")
339+
@_silgen_name("_swift_setPerObjectRuntimeFunctionCountersMode")
340340
static
341341
public // @testable
342342
func setPerObjectRuntimeFunctionCountersMode(enable: Bool) -> Bool

stdlib/public/runtime/RuntimeInvocationsTracking.cpp

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
#include "llvm/ADT/DenseMap.h"
1919
#include "RuntimeInvocationsTracking.h"
20+
#include "swift/Basic/Lazy.h"
2021
#include "swift/Runtime/HeapObject.h"
22+
#include "swift/Runtime/Mutex.h"
2123

2224
// This file is compiled always, even if assertions are disabled and no runtime
2325
// functions are being tracked. This is done to avoid recompiling Swift clients
@@ -48,12 +50,19 @@ static bool UpdateGlobalRuntimeFunctionCounters = false;
4850
/// TODO: Add support for enabling/disabling counters on a per object basis?
4951

5052
/// Global set of counters tracking the total number of runtime invocations.
51-
static RuntimeFunctionCountersState RuntimeGlobalFunctionCountersState;
53+
struct RuntimeFunctionCountersStateSentinel {
54+
RuntimeFunctionCountersState State;
55+
StaticReadWriteLock Lock;
56+
};
57+
static RuntimeFunctionCountersStateSentinel RuntimeGlobalFunctionCountersState;
5258

5359
/// The object state cache mapping objects to the collected state associated with
5460
/// them.
55-
/// TODO: Do we need to make it thread-safe?
56-
static llvm::DenseMap<HeapObject *, RuntimeFunctionCountersState> RuntimeObjectStateCache;
61+
struct RuntimeObjectCacheSentinel {
62+
llvm::DenseMap<HeapObject *, RuntimeFunctionCountersState> Cache;
63+
StaticReadWriteLock Lock;
64+
};
65+
static Lazy<RuntimeObjectCacheSentinel> RuntimeObjectStateCache;
5766

5867
static const char *RuntimeFunctionNames[] {
5968
/// Define names of runtime functions.
@@ -95,79 +104,84 @@ static uint16_t RuntimeFunctionCountersOffsets[] = {
95104
void SWIFT_RT_TRACK_INVOCATION_NAME(RT_FUNCTION)(HeapObject * object) { \
96105
/* Update global counters. */ \
97106
if (UpdateGlobalRuntimeFunctionCounters) { \
98-
RuntimeGlobalFunctionCountersState \
107+
StaticScopedWriteLock lock(RuntimeGlobalFunctionCountersState.Lock); \
108+
RuntimeGlobalFunctionCountersState.State \
99109
.SWIFT_RT_FUNCTION_INVOCATION_COUNTER_NAME(RT_FUNCTION)++; \
100110
if (GlobalRuntimeFunctionCountersUpdateHandler) { \
101-
auto oldGlobalMode = setGlobalRuntimeFunctionCountersMode(0); \
102-
auto oldPerObjectMode = setPerObjectRuntimeFunctionCountersMode(0); \
111+
auto oldGlobalMode = _swift_setGlobalRuntimeFunctionCountersMode(0); \
112+
auto oldPerObjectMode = \
113+
_swift_setPerObjectRuntimeFunctionCountersMode(0); \
103114
GlobalRuntimeFunctionCountersUpdateHandler( \
104115
object, RT_FUNCTION_ID(RT_FUNCTION)); \
105-
setGlobalRuntimeFunctionCountersMode(oldGlobalMode); \
106-
setPerObjectRuntimeFunctionCountersMode(oldPerObjectMode); \
116+
_swift_setGlobalRuntimeFunctionCountersMode(oldGlobalMode); \
117+
_swift_setPerObjectRuntimeFunctionCountersMode(oldPerObjectMode); \
107118
} \
108119
} \
109120
/* Update per object counters. */ \
110121
if (UpdatePerObjectRuntimeFunctionCounters && object) { \
111-
RuntimeObjectStateCache[object] \
112-
.SWIFT_RT_FUNCTION_INVOCATION_COUNTER_NAME(RT_FUNCTION)++; \
113-
/* TODO: Remember the order/history of operations? */ \
122+
auto &theSentinel = RuntimeObjectStateCache.get(); \
123+
StaticScopedWriteLock lock(theSentinel.Lock); \
124+
theSentinel.Cache[object].SWIFT_RT_FUNCTION_INVOCATION_COUNTER_NAME( \
125+
RT_FUNCTION)++; \
126+
/* TODO: Remember the order/history of operations? */ \
114127
} \
115128
}
116129
#include "RuntimeInvocationsTracking.def"
117130

118131
/// Public APIs
119132

120133
/// Get the runtime object state associated with an object.
121-
SWIFT_RT_ENTRY_VISIBILITY
122-
void getObjectRuntimeFunctionCounters(HeapObject *object,
123-
RuntimeFunctionCountersState *result) {
124-
*result = RuntimeObjectStateCache[object];
134+
void _swift_getObjectRuntimeFunctionCounters(
135+
HeapObject *object, RuntimeFunctionCountersState *result) {
136+
auto &theSentinel = RuntimeObjectStateCache.get();
137+
StaticScopedReadLock lock(theSentinel.Lock);
138+
*result = theSentinel.Cache[object];
125139
}
126140

127141
/// Set the runtime object state associated with an object from a provided
128142
/// state.
129-
SWIFT_RT_ENTRY_VISIBILITY
130-
void setObjectRuntimeFunctionCounters(HeapObject *object,
131-
RuntimeFunctionCountersState *state) {
132-
RuntimeObjectStateCache[object] = *state;
143+
void _swift_setObjectRuntimeFunctionCounters(
144+
HeapObject *object, RuntimeFunctionCountersState *state) {
145+
auto &theSentinel = RuntimeObjectStateCache.get();
146+
StaticScopedWriteLock lock(theSentinel.Lock);
147+
theSentinel.Cache[object] = *state;
133148
}
134149

135150
/// Get the global runtime state containing the total numbers of invocations for
136151
/// each runtime function of interest.
137-
SWIFT_RT_ENTRY_VISIBILITY
138-
void getGlobalRuntimeFunctionCounters(RuntimeFunctionCountersState *result) {
139-
*result = RuntimeGlobalFunctionCountersState;
152+
void _swift_getGlobalRuntimeFunctionCounters(
153+
RuntimeFunctionCountersState *result) {
154+
StaticScopedReadLock lock(RuntimeGlobalFunctionCountersState.Lock);
155+
*result = RuntimeGlobalFunctionCountersState.State;
140156
}
141157

142158
/// Set the global runtime state of function pointers from a provided state.
143-
SWIFT_RT_ENTRY_VISIBILITY
144-
void setGlobalRuntimeFunctionCounters(RuntimeFunctionCountersState *state) {
145-
RuntimeGlobalFunctionCountersState = *state;
159+
void _swift_setGlobalRuntimeFunctionCounters(
160+
RuntimeFunctionCountersState *state) {
161+
StaticScopedWriteLock lock(RuntimeGlobalFunctionCountersState.Lock);
162+
RuntimeGlobalFunctionCountersState.State = *state;
146163
}
147164

148165
/// Return the names of the runtime functions being tracked.
149166
/// Their order is the same as the order of the counters in the
150167
/// RuntimeObjectState structure. All these strings are null terminated.
151-
SWIFT_RT_ENTRY_VISIBILITY
152-
const char **getRuntimeFunctionNames() {
168+
const char **_swift_getRuntimeFunctionNames() {
153169
return RuntimeFunctionNames;
154170
}
155171

156172
/// Return the offsets of the runtime function counters being tracked.
157173
/// Their order is the same as the order of the counters in the
158174
/// RuntimeObjectState structure.
159-
SWIFT_RT_ENTRY_VISIBILITY
160-
const uint16_t *getRuntimeFunctionCountersOffsets() {
175+
const uint16_t *_swift_getRuntimeFunctionCountersOffsets() {
161176
return RuntimeFunctionCountersOffsets;
162177
}
163178

164179
/// Return the number of runtime functions being tracked.
165-
SWIFT_RT_ENTRY_VISIBILITY
166-
uint64_t getNumRuntimeFunctionCounters() {
180+
uint64_t _swift_getNumRuntimeFunctionCounters() {
167181
return ID_LastRuntimeFunctionName;
168182
}
169183

170-
static void dumpRuntimeCounters(RuntimeFunctionCountersState *State) {
184+
static void _swift_dumpRuntimeCounters(RuntimeFunctionCountersState *State) {
171185
uint32_t tmp;
172186
/// Define how to dump the counter for a given runtime function.
173187
#define FUNCTION_TO_TRACK(RT_FUNCTION) \
@@ -179,28 +193,27 @@ static void dumpRuntimeCounters(RuntimeFunctionCountersState *State) {
179193
}
180194

181195
/// Dump all per-object runtime function pointers.
182-
SWIFT_RT_ENTRY_VISIBILITY
183-
void dumpObjectsRuntimeFunctionPointers() {
184-
for (auto &Pair : RuntimeObjectStateCache) {
196+
void _swift_dumpObjectsRuntimeFunctionPointers() {
197+
auto &theSentinel = RuntimeObjectStateCache.get();
198+
StaticScopedReadLock lock(theSentinel.Lock);
199+
for (auto &Pair : theSentinel.Cache) {
185200
printf("\n\nRuntime counters for object at address %p:\n", Pair.getFirst());
186-
dumpRuntimeCounters(&Pair.getSecond());
201+
_swift_dumpRuntimeCounters(&Pair.getSecond());
187202
printf("\n");
188203
}
189204
}
190205

191206
/// Set mode for global runtime function counters.
192207
/// Return the old value of this flag.
193-
SWIFT_RT_ENTRY_VISIBILITY
194-
int setGlobalRuntimeFunctionCountersMode(int mode) {
208+
int _swift_setGlobalRuntimeFunctionCountersMode(int mode) {
195209
int oldMode = UpdateGlobalRuntimeFunctionCounters;
196210
UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0;
197211
return oldMode;
198212
}
199213

200214
/// Set mode for per object runtime function counters.
201215
/// Return the old value of this flag.
202-
SWIFT_RT_ENTRY_VISIBILITY
203-
int setPerObjectRuntimeFunctionCountersMode(int mode) {
216+
int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) {
204217
int oldMode = UpdatePerObjectRuntimeFunctionCounters;
205218
UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0;
206219
return oldMode;
@@ -210,13 +223,12 @@ int setPerObjectRuntimeFunctionCountersMode(int mode) {
210223
/// is being updated. The handler should take the object and may be
211224
/// the name of the runtime function as parameters. And this handler
212225
/// could e.g. check some conditions and stop the program under
213-
/// a debuggger if a certain condition is met, like a refcount has
226+
/// a debugger if a certain condition is met, like a refcount has
214227
/// reached a certain value.
215228
/// We could allow for setting global handlers or even per-object
216229
/// handlers.
217-
SWIFT_RT_ENTRY_VISIBILITY
218230
RuntimeFunctionCountersUpdateHandler
219-
setGlobalRuntimeFunctionCountersUpdateHandler(
231+
_swift_setGlobalRuntimeFunctionCountersUpdateHandler(
220232
RuntimeFunctionCountersUpdateHandler handler) {
221233
auto oldHandler = GlobalRuntimeFunctionCountersUpdateHandler;
222234
GlobalRuntimeFunctionCountersUpdateHandler = handler;

stdlib/public/runtime/RuntimeInvocationsTracking.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,52 +69,53 @@ using RuntimeFunctionCountersUpdateHandler =
6969

7070
/// Get the runtime object state associated with an object and store it
7171
/// into the result.
72-
SWIFT_RT_ENTRY_VISIBILITY void
73-
getObjectRuntimeFunctionCounters(HeapObject *object,
74-
RuntimeFunctionCountersState *result);
72+
SWIFT_RUNTIME_EXPORT void
73+
_swift_getObjectRuntimeFunctionCounters(HeapObject *object,
74+
RuntimeFunctionCountersState *result);
7575

7676
/// Get the global runtime state containing the total numbers of invocations for
7777
/// each runtime function of interest and store it into the result.
78-
SWIFT_RT_ENTRY_VISIBILITY void
79-
getGlobalRuntimeFunctionCounters(swift::RuntimeFunctionCountersState *result);
78+
SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters(
79+
swift::RuntimeFunctionCountersState *result);
8080

8181
/// Return the names of the runtime functions being tracked.
8282
/// Their order is the same as the order of the counters in the
8383
/// RuntimeObjectState structure.
84-
SWIFT_RT_ENTRY_VISIBILITY const char **getRuntimeFunctionNames();
84+
SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames();
8585

8686
/// Return the offsets of the runtime function counters being tracked.
8787
/// Their order is the same as the order of the counters in the
8888
/// RuntimeFunctionCountersState structure.
89-
SWIFT_RT_ENTRY_VISIBILITY const uint16_t *getRuntimeFunctionCountersOffsets();
89+
SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets();
9090

9191
/// Return the number of runtime functions being tracked.
92-
SWIFT_RT_ENTRY_VISIBILITY uint64_t getNumRuntimeFunctionCounters();
92+
SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters();
9393

9494
/// Dump all per-object runtime function pointers.
95-
SWIFT_RT_ENTRY_VISIBILITY void dumpObjectsRuntimeFunctionPointers();
95+
SWIFT_RUNTIME_EXPORT void _swift_dumpObjectsRuntimeFunctionPointers();
9696

9797
/// Set mode for global runtime function counters.
9898
/// Return the old value of this flag.
99-
SWIFT_RT_ENTRY_VISIBILITY int setPerObjectRuntimeFunctionCountersMode(int mode);
99+
SWIFT_RUNTIME_EXPORT int
100+
_swift_setPerObjectRuntimeFunctionCountersMode(int mode);
100101

101102
/// Set mode for per object runtime function counters.
102103
/// Return the old value of this flag.
103-
SWIFT_RT_ENTRY_VISIBILITY int setGlobalRuntimeFunctionCountersMode(int mode);
104+
SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode);
104105

105106
/// Set the global runtime state of function pointers from a provided state.
106-
SWIFT_RT_ENTRY_VISIBILITY void
107-
setGlobalRuntimeFunctionCounters(swift::RuntimeFunctionCountersState *state);
107+
SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters(
108+
swift::RuntimeFunctionCountersState *state);
108109

109110
/// Set the runtime object state associated with an object from a provided
110111
/// state.
111-
SWIFT_RT_ENTRY_VISIBILITY void
112-
setObjectRuntimeFunctionCounters(HeapObject *object,
113-
RuntimeFunctionCountersState *state);
112+
SWIFT_RUNTIME_EXPORT void
113+
_swift_setObjectRuntimeFunctionCounters(HeapObject *object,
114+
RuntimeFunctionCountersState *state);
114115

115116
/// Set the global runtime function counters update handler.
116-
SWIFT_RT_ENTRY_VISIBILITY RuntimeFunctionCountersUpdateHandler
117-
setGlobalRuntimeFunctionCountersUpdateHandler(
117+
SWIFT_RUNTIME_EXPORT RuntimeFunctionCountersUpdateHandler
118+
_swift_setGlobalRuntimeFunctionCountersUpdateHandler(
118119
RuntimeFunctionCountersUpdateHandler handler);
119120

120121
#endif

0 commit comments

Comments
 (0)