File: test_runtime_function_counters.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (281 lines) | stat: -rw-r--r-- 9,560 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/test_runtime_function_counters
// RUN: %target-codesign %t/test_runtime_function_counters
// RUN: %target-run %t/test_runtime_function_counters 2>&1 | %FileCheck %s
// REQUIRES: runtime_function_counters
// REQUIRES: executable_test
// REQUIRES: rdar48995133

/// Test functionality related to the runtime function counters.

class C {
  var next: C? = nil
  func test(_ c: C) {
  }
}

struct MyStruct {
  var ref1: AnyObject? = C()
  var ref2: AnyObject = C()
  var str: String = ""
}

public final class List<T> {
  var value: T
  var next: List<T>?

  init(_ value: T) {
    self.value = value
    self.next = nil
  }

  init(_ value: T, _ tail: List<T>) {
    self.value = value
    self.next = tail
  }
}

public func length<T>(_ l: List<T>) -> Int {
  var ll: List<T>? = l
  var len = 0
  while ll != nil {
    len = len + 1
    ll = ll?.next
  }
  return len
}

/// CHECK-LABEL: TEST: Collect references inside objects

// Constant strings don't really have a reference, but BridgeObject
// still counts as one.
//
// FIXME(TODO: JIRA): On 32-bit, we use AnyObject? instead of BridgeObject. If
// we get back onto a real reference (or if 64-bit gets off of a real
// reference), then drop adjust the optionality of the following check.
//
/// CHECK: Constant string: [{{([0-9a-fA-Fx]+)?}}]

/// An array has one reference
/// CHECK: Array<Int>: [{{[0-9a-fA-Fx]+}}]

/// MyStruct has two references plus a String with a third
//
// FIXME(TODO: JIRA): On 32-bit, we use AnyObject? instead of BridgeObject. If
// we get back onto a real reference (or if 64-bit gets off of a real
// reference), then drop adjust the optionality of the following check.
//
/// CHECK: MyStruct: [{{[0-9a-fA-Fx]+}}, {{[0-9a-fA-Fx]+}}{{(, [0-9a-fA-Fx]+)?}}]

/// Dictionary has one reference
/// CHECK: Dictionary<Int, Int>: [{{[0-9a-fA-Fx]+}}]
/// Set has one reference
/// CHECK: Set<Int>: [{{[0-9a-fA-Fx]+}}]
/// Test collection of references inside different types of objects.
@inline(never)
func testCollectReferencesInsideObject() {
  print("TEST: Collect references inside objects")
  let s = "MyString"
  let aint = [1,2,3,4]
  let dint = [1:1, 2:2]
  let sint: Set<Int> = [1,2,3,4]

  print("Constant string: \(_collectReferencesInsideObject(s))")
  print("Array<Int>: \(_collectReferencesInsideObject(aint))")
  print("MyStruct: \(_collectReferencesInsideObject(MyStruct()))")
  print("Dictionary<Int, Int>: \(_collectReferencesInsideObject(dint))")
  print("Set<Int>: \(_collectReferencesInsideObject(sint))")

  var mystring = "MyString"
  mystring.append("End")
  testString(mystring)
  testDict(dint)
  testObjectCycle()
}


/// CHECK-LABEL: TEST: APIs from _RuntimeFunctionCounters
/// CHECK: Number of runtime function pointers:
/// Test some APIs from _RuntimeFunctionCounters
func testRuntimeCounters() {
  print("TEST: APIs from _RuntimeFunctionCounters")
  let numRuntimeFunctionPointer =
    Int(_RuntimeFunctionCounters.getNumRuntimeFunctionCounters())

  print("Number of runtime function pointers: \(numRuntimeFunctionPointer)")

  let names = _RuntimeFunctionCounters.getRuntimeFunctionNames()
  let offsets = _RuntimeFunctionCounters.getRuntimeFunctionCountersOffsets()

  for i in 0..<numRuntimeFunctionPointer {
    print("Runtime function \(i) : \(names[i]) at offset: \(offsets[i])")
  }

  var d: [Int : Int] = [:]
  let globalCounters1 = _GlobalRuntimeFunctionCountersState()

  for i in 0..<50 {
    let k = i
    let v = i*i
    d[k] = v
  }

  let globalCounters2 = _GlobalRuntimeFunctionCountersState()

  globalCounters1.dumpDiff(globalCounters2, skipUnchanged: true)
}

/// Test finding references inside a String object.
@inline(never)
func testString(_ s: String) {
  print("TEST: Collect references for strings")
  let refs = _collectReferencesInsideObject(s)
  print("References are: \(refs)")
  let objectCounters1 = _ObjectRuntimeFunctionCountersState(refs[0])
  let _ = [String](repeating: s, count: 4)
  let objectCounters2 = _ObjectRuntimeFunctionCountersState(refs[0])
  objectCounters1.dumpDiff(objectCounters2, skipUnchanged: true)
}

/// Test finding references inside a Dictionary object.
@inline(never)
func testDict(_ _dint: [Int : Int]) {
  print("TEST: Collect references for dictionaries")
  var dint = _dint
  dint[3] = 3
  let refs = _collectReferencesInsideObject(dint)
  print("References are: \(refs)")
  let objectCounters1 = _ObjectRuntimeFunctionCountersState(refs[0])
  dint[222] = 222
  dint[2222] = 2222
  let objectCounters2 = _ObjectRuntimeFunctionCountersState(refs[0])
  objectCounters1.dumpDiff(objectCounters2, skipUnchanged: true)
}

/// Test finding references inside an object graph with a cycle.
/// It should not result in a stack overflow.
@inline(never)
func testObjectCycle() {
  print("TEST: Collect references on object graph with cycles")
  print("testObjectCycle")
  let c1 = C()
  let c2 = C()
  c1.next = c1
  c2.next = c1
  let refs = _collectReferencesInsideObject(c1)
  print("References are: \(refs)")
  let objectCounters1 = _ObjectRuntimeFunctionCountersState(refs[0])
  c1.next = nil
  c2.next = nil
  let objectCounters2 = _ObjectRuntimeFunctionCountersState(refs[0])
  objectCounters1.dumpDiff(objectCounters2, skipUnchanged: true)
}

/// Test runtime function counters for a List object.
@inline(never)
func testLists() {
  print("TEST: Runtime function counters for Lists")
  print("testLists")
  let globalCounters1 = _GlobalRuntimeFunctionCountersState()
  var l: List<Int>? = List(1, List(2, List(3, List(4, List(5)))))
  let refs = _collectReferencesInsideObject(l!)
  let globalCounters11 = _GlobalRuntimeFunctionCountersState()
  let _ = _collectReferencesInsideObject(l!)
  let globalCounters111 = _GlobalRuntimeFunctionCountersState()

  print("Global counters diff for 11")
  globalCounters1.dumpDiff(globalCounters11, skipUnchanged: true)
  print("Global counters diff for 111")
  globalCounters1.dumpDiff(globalCounters111, skipUnchanged: true)

  let len = length(l!)
  let globalCounters2 = _GlobalRuntimeFunctionCountersState()
  print("Length of the list is \(len)")
  print("Global counters diff after constructing a list and computing its length")
  globalCounters1.dumpDiff(globalCounters2, skipUnchanged: true)
  let objectCounters1 = _ObjectRuntimeFunctionCountersState(refs[0])
  l = nil
  let objectCounters2 = _ObjectRuntimeFunctionCountersState(refs[0])
  print("List head counters after list becomes unreferenced")
  objectCounters1.dumpDiff(objectCounters2, skipUnchanged: true)
}

/// Test the _measureRuntimeFunctionCountersDiffs API.
@inline(never)
func testMeasureRuntimeFunctionCountersDiffs() {
  print("TEST: Measure runtime function counters diff")
  let l: List<Int>? = List(1, List(2, List(3, List(4, List(5)))))
  let refs = _collectReferencesInsideObject(l!)
  var len = 0
  let (globalCounters, objectsCountersDiffs) =
    _measureRuntimeFunctionCountersDiffs(objects: [refs[0]]) {
    len = length(l!)
  }
  print("List length is: \(len)")
  print("Global counters changes")
  globalCounters.dump(skipUnchanged: true)
  print("Objects counters changes")
  for (i, objectCounters) in objectsCountersDiffs.enumerated() {
    print("Object counters diff for \(refs[i])")
    objectCounters.dump(skipUnchanged: true)
  }
}

/// This is a handler that is invoked on each runtime functions counters update.
@inline(never)
func updatesHandler(object: UnsafeRawPointer, functionId: Int64) {
  let savedMode = _RuntimeFunctionCounters.disableRuntimeFunctionCountersUpdates()
  print("Start handler")
  let functionName = _RuntimeFunctionCounters.runtimeFunctionNames[Int(functionId)]
  print("Function \(functionName) was invoked on object \(object)")
  print("End handler")
  _RuntimeFunctionCounters.enableRuntimeFunctionCountersUpdates(mode: savedMode)
}

/// Check that it is possible to set your own runtime functions counters
/// updates handler and this handler is invoked at runtime.
/// CHECK-LABEL: TEST: Provide runtime function counters update handler
/// CHECK: Start handler
/// Check that allocations and deallocations are intercepted too.
/// CHECK: swift_allocObject
/// CHECK: swift_deallocObject
/// CHECK: End handler
/// Test that you can provide custom handlers for runtime functions counters
/// updates.
var globalC: C? = nil
@inline(never)
func testFunctionRuntimeCountersUpdateHandler() {
  print("TEST: Provide runtime function counters update handler")
  let l: List<Int>? = List(1, List(2, List(3, List(4, List(5)))))
  let oldHandler =
    _RuntimeFunctionCounters.setGlobalRuntimeFunctionCountersUpdateHandler(
      handler: updatesHandler)
  globalC = C()
  globalC = nil
  let len = length(l!)
  _ = _RuntimeFunctionCounters.setGlobalRuntimeFunctionCountersUpdateHandler(
    handler: oldHandler)
  print("Restored old handler")
  print(len)
}

/// Enable runtime function counters stats collection.
_RuntimeFunctionCounters.enableRuntimeFunctionCountersUpdates()

/// Test collection of references inside different types of objects.
testCollectReferencesInsideObject()

/// Test some APIs from _RuntimeFunctionCounters.
testRuntimeCounters()

/// Test dumping of counters for all objects.
_RuntimeFunctionCounters.dumpObjectsRuntimeFunctionPointers()

/// Test runtime function counters for a List object.
testLists()

/// Test the _measureRuntimeFunctionCountersDiffs API.
testMeasureRuntimeFunctionCountersDiffs()

/// Test that you can provide custom handlers for runtime functions counters updates.
testFunctionRuntimeCountersUpdateHandler()