TestQueues.py
16.6 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""Test queues inspection SB APIs."""
from __future__ import print_function
import unittest2
import os
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class TestQueues(TestBase):
mydir = TestBase.compute_mydir(__file__)
@skipUnlessDarwin
@add_test_categories(['pyapi'])
def test_with_python_api_queues(self):
"""Test queues inspection SB APIs."""
self.build()
self.queues()
@skipUnlessDarwin
@add_test_categories(['pyapi'])
def test_with_python_api_queues_with_backtrace(self):
"""Test queues inspection SB APIs."""
self.build()
self.queues_with_libBacktraceRecording()
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line numbers that we will step to in main:
self.main_source = "main.c"
def check_queue_for_valid_queue_id(self, queue):
self.assertTrue(
queue.GetQueueID() != 0, "Check queue %s for valid QueueID (got 0x%x)" %
(queue.GetName(), queue.GetQueueID()))
def check_running_and_pending_items_on_queue(
self, queue, expected_running, expected_pending):
self.assertTrue(
queue.GetNumPendingItems() == expected_pending,
"queue %s should have %d pending items, instead has %d pending items" %
(queue.GetName(),
expected_pending,
(queue.GetNumPendingItems())))
self.assertTrue(
queue.GetNumRunningItems() == expected_running,
"queue %s should have %d running items, instead has %d running items" %
(queue.GetName(),
expected_running,
(queue.GetNumRunningItems())))
def describe_threads(self):
desc = []
for x in self.inferior_process:
id = x.GetIndexID()
reason_str = lldbutil.stop_reason_to_str(x.GetStopReason())
location = "\t".join([lldbutil.get_description(
x.GetFrameAtIndex(i)) for i in range(x.GetNumFrames())])
desc.append(
"thread %d: %s (queue id: %s) at\n\t%s" %
(id, reason_str, x.GetQueueID(), location))
print('\n'.join(desc))
def check_number_of_threads_owned_by_queue(self, queue, number_threads):
if (queue.GetNumThreads() != number_threads):
self.describe_threads()
self.assertTrue(
queue.GetNumThreads() == number_threads,
"queue %s should have %d thread executing, but has %d" %
(queue.GetName(),
number_threads,
queue.GetNumThreads()))
def check_queue_kind(self, queue, kind):
expected_kind_string = "Unknown"
if kind == lldb.eQueueKindSerial:
expected_kind_string = "Serial queue"
if kind == lldb.eQueueKindConcurrent:
expected_kind_string = "Concurrent queue"
actual_kind_string = "Unknown"
if queue.GetKind() == lldb.eQueueKindSerial:
actual_kind_string = "Serial queue"
if queue.GetKind() == lldb.eQueueKindConcurrent:
actual_kind_string = "Concurrent queue"
self.assertTrue(
queue.GetKind() == kind,
"queue %s is expected to be a %s but it is actually a %s" %
(queue.GetName(),
expected_kind_string,
actual_kind_string))
def check_queues_threads_match_queue(self, queue):
for idx in range(0, queue.GetNumThreads()):
t = queue.GetThreadAtIndex(idx)
self.assertTrue(
t.IsValid(), "Queue %s's thread #%d must be valid" %
(queue.GetName(), idx))
self.assertTrue(
t.GetQueueID() == queue.GetQueueID(),
"Queue %s has a QueueID of %d but its thread #%d has a QueueID of %d" %
(queue.GetName(),
queue.GetQueueID(),
idx,
t.GetQueueID()))
self.assertTrue(
t.GetQueueName() == queue.GetName(),
"Queue %s has a QueueName of %s but its thread #%d has a QueueName of %s" %
(queue.GetName(),
queue.GetName(),
idx,
t.GetQueueName()))
self.assertTrue(
t.GetQueue().GetQueueID() == queue.GetQueueID(),
"Thread #%d's Queue's QueueID of %d is not the same as the QueueID of its owning queue %d" %
(idx,
t.GetQueue().GetQueueID(),
queue.GetQueueID()))
def queues(self):
"""Test queues inspection SB APIs without libBacktraceRecording."""
exe = self.getBuildArtifact("a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
self.main_source_spec = lldb.SBFileSpec(self.main_source)
break1 = target.BreakpointCreateByName("stopper", 'a.out')
self.assertTrue(break1, VALID_BREAKPOINT)
process = target.LaunchSimple(
[], None, self.get_process_working_directory())
self.assertTrue(process, PROCESS_IS_VALID)
threads = lldbutil.get_threads_stopped_at_breakpoint(process, break1)
if len(threads) != 1:
self.fail("Failed to stop at breakpoint 1.")
self.inferior_process = process
queue_submittor_1 = lldb.SBQueue()
queue_performer_1 = lldb.SBQueue()
queue_performer_2 = lldb.SBQueue()
queue_performer_3 = lldb.SBQueue()
for idx in range(0, process.GetNumQueues()):
q = process.GetQueueAtIndex(idx)
if q.GetName() == "com.apple.work_submittor_1":
queue_submittor_1 = q
if q.GetName() == "com.apple.work_performer_1":
queue_performer_1 = q
if q.GetName() == "com.apple.work_performer_2":
queue_performer_2 = q
if q.GetName() == "com.apple.work_performer_3":
queue_performer_3 = q
self.assertTrue(
queue_submittor_1.IsValid() and queue_performer_1.IsValid() and queue_performer_2.IsValid() and queue_performer_3.IsValid(),
"Got all four expected queues: %s %s %s %s" %
(queue_submittor_1.IsValid(),
queue_performer_1.IsValid(),
queue_performer_2.IsValid(),
queue_performer_3.IsValid()))
self.check_queue_for_valid_queue_id(queue_submittor_1)
self.check_queue_for_valid_queue_id(queue_performer_1)
self.check_queue_for_valid_queue_id(queue_performer_2)
self.check_queue_for_valid_queue_id(queue_performer_3)
self.check_number_of_threads_owned_by_queue(queue_submittor_1, 1)
self.check_number_of_threads_owned_by_queue(queue_performer_1, 1)
self.check_number_of_threads_owned_by_queue(queue_performer_2, 1)
self.check_number_of_threads_owned_by_queue(queue_performer_3, 4)
self.check_queue_kind(queue_submittor_1, lldb.eQueueKindSerial)
self.check_queue_kind(queue_performer_1, lldb.eQueueKindSerial)
self.check_queue_kind(queue_performer_2, lldb.eQueueKindSerial)
self.check_queue_kind(queue_performer_3, lldb.eQueueKindConcurrent)
self.check_queues_threads_match_queue(queue_submittor_1)
self.check_queues_threads_match_queue(queue_performer_1)
self.check_queues_threads_match_queue(queue_performer_2)
self.check_queues_threads_match_queue(queue_performer_3)
# We have threads running with all the different dispatch QoS service
# levels - find those threads and check that we can get the correct
# QoS name for each of them.
user_initiated_thread = lldb.SBThread()
user_interactive_thread = lldb.SBThread()
utility_thread = lldb.SBThread()
unspecified_thread = lldb.SBThread()
background_thread = lldb.SBThread()
for th in process.threads:
if th.GetName() == "user initiated QoS":
user_initiated_thread = th
if th.GetName() == "user interactive QoS":
user_interactive_thread = th
if th.GetName() == "utility QoS":
utility_thread = th
if th.GetName() == "unspecified QoS":
unspecified_thread = th
if th.GetName() == "background QoS":
background_thread = th
self.assertTrue(
user_initiated_thread.IsValid(),
"Found user initiated QoS thread")
self.assertTrue(
user_interactive_thread.IsValid(),
"Found user interactive QoS thread")
self.assertTrue(utility_thread.IsValid(), "Found utility QoS thread")
self.assertTrue(
unspecified_thread.IsValid(),
"Found unspecified QoS thread")
self.assertTrue(
background_thread.IsValid(),
"Found background QoS thread")
stream = lldb.SBStream()
self.assertTrue(
user_initiated_thread.GetInfoItemByPathAsString(
"requested_qos.printable_name",
stream),
"Get QoS printable string for user initiated QoS thread")
self.assertTrue(
stream.GetData() == "User Initiated",
"user initiated QoS thread name is valid")
stream.Clear()
self.assertTrue(
user_interactive_thread.GetInfoItemByPathAsString(
"requested_qos.printable_name",
stream),
"Get QoS printable string for user interactive QoS thread")
self.assertTrue(
stream.GetData() == "User Interactive",
"user interactive QoS thread name is valid")
stream.Clear()
self.assertTrue(
utility_thread.GetInfoItemByPathAsString(
"requested_qos.printable_name",
stream),
"Get QoS printable string for utility QoS thread")
self.assertTrue(
stream.GetData() == "Utility",
"utility QoS thread name is valid")
stream.Clear()
self.assertTrue(
unspecified_thread.GetInfoItemByPathAsString(
"requested_qos.printable_name",
stream),
"Get QoS printable string for unspecified QoS thread")
qosName = stream.GetData()
self.assertTrue(
qosName == "User Initiated" or qosName == "Default",
"unspecified QoS thread name is valid")
stream.Clear()
self.assertTrue(
background_thread.GetInfoItemByPathAsString(
"requested_qos.printable_name",
stream),
"Get QoS printable string for background QoS thread")
self.assertTrue(
stream.GetData() == "Background",
"background QoS thread name is valid")
@skipIfDarwin # rdar://50379398
def queues_with_libBacktraceRecording(self):
"""Test queues inspection SB APIs with libBacktraceRecording present."""
exe = self.getBuildArtifact("a.out")
if not os.path.isfile(
'/Applications/Xcode.app/Contents/Developer/usr/lib/libBacktraceRecording.dylib'):
self.skipTest(
"Skipped because libBacktraceRecording.dylib was present on the system.")
if not os.path.isfile(
'/usr/lib/system/introspection/libdispatch.dylib'):
self.skipTest(
"Skipped because introspection libdispatch dylib is not present.")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
self.main_source_spec = lldb.SBFileSpec(self.main_source)
break1 = target.BreakpointCreateByName("stopper", 'a.out')
self.assertTrue(break1, VALID_BREAKPOINT)
# Now launch the process, and do not stop at entry point.
libbtr_path = "/Applications/Xcode.app/Contents/Developer/usr/lib/libBacktraceRecording.dylib"
if self.getArchitecture() in ['arm', 'arm64', 'arm64e', 'arm64_32', 'armv7', 'armv7k']:
libbtr_path = "/Developer/usr/lib/libBacktraceRecording.dylib"
process = target.LaunchSimple(
[],
[
'DYLD_INSERT_LIBRARIES=%s' % (libbtr_path),
'DYLD_LIBRARY_PATH=/usr/lib/system/introspection'],
self.get_process_working_directory())
self.assertTrue(process, PROCESS_IS_VALID)
# The stop reason of the thread should be breakpoint.
threads = lldbutil.get_threads_stopped_at_breakpoint(process, break1)
if len(threads) != 1:
self.fail("Failed to stop at breakpoint 1.")
self.inferior_process = process
libbtr_module_filespec = lldb.SBFileSpec("libBacktraceRecording.dylib")
libbtr_module = target.FindModule(libbtr_module_filespec)
if not libbtr_module.IsValid():
self.skipTest(
"Skipped because libBacktraceRecording.dylib was not loaded into the process.")
self.assertTrue(
process.GetNumQueues() >= 4,
"Found the correct number of queues.")
queue_submittor_1 = lldb.SBQueue()
queue_performer_1 = lldb.SBQueue()
queue_performer_2 = lldb.SBQueue()
queue_performer_3 = lldb.SBQueue()
for idx in range(0, process.GetNumQueues()):
q = process.GetQueueAtIndex(idx)
if "LLDB_COMMAND_TRACE" in os.environ:
print("Queue with id %s has name %s" % (q.GetQueueID(), q.GetName()))
if q.GetName() == "com.apple.work_submittor_1":
queue_submittor_1 = q
if q.GetName() == "com.apple.work_performer_1":
queue_performer_1 = q
if q.GetName() == "com.apple.work_performer_2":
queue_performer_2 = q
if q.GetName() == "com.apple.work_performer_3":
queue_performer_3 = q
if q.GetName() == "com.apple.main-thread":
if q.GetNumThreads() == 0:
print("Cannot get thread <=> queue associations")
return
self.assertTrue(
queue_submittor_1.IsValid() and queue_performer_1.IsValid() and queue_performer_2.IsValid() and queue_performer_3.IsValid(),
"Got all four expected queues: %s %s %s %s" %
(queue_submittor_1.IsValid(),
queue_performer_1.IsValid(),
queue_performer_2.IsValid(),
queue_performer_3.IsValid()))
self.check_queue_for_valid_queue_id(queue_submittor_1)
self.check_queue_for_valid_queue_id(queue_performer_1)
self.check_queue_for_valid_queue_id(queue_performer_2)
self.check_queue_for_valid_queue_id(queue_performer_3)
self.check_running_and_pending_items_on_queue(queue_submittor_1, 1, 0)
self.check_running_and_pending_items_on_queue(queue_performer_1, 1, 3)
self.check_running_and_pending_items_on_queue(
queue_performer_2, 1, 9999)
self.check_running_and_pending_items_on_queue(queue_performer_3, 4, 0)
self.check_number_of_threads_owned_by_queue(queue_submittor_1, 1)
self.check_number_of_threads_owned_by_queue(queue_performer_1, 1)
self.check_number_of_threads_owned_by_queue(queue_performer_2, 1)
self.check_number_of_threads_owned_by_queue(queue_performer_3, 4)
self.check_queue_kind(queue_submittor_1, lldb.eQueueKindSerial)
self.check_queue_kind(queue_performer_1, lldb.eQueueKindSerial)
self.check_queue_kind(queue_performer_2, lldb.eQueueKindSerial)
self.check_queue_kind(queue_performer_3, lldb.eQueueKindConcurrent)
self.check_queues_threads_match_queue(queue_submittor_1)
self.check_queues_threads_match_queue(queue_performer_1)
self.check_queues_threads_match_queue(queue_performer_2)
self.check_queues_threads_match_queue(queue_performer_3)
self.assertTrue(queue_performer_2.GetPendingItemAtIndex(
0).IsValid(), "queue 2's pending item #0 is valid")
self.assertTrue(queue_performer_2.GetPendingItemAtIndex(0).GetAddress().GetSymbol(
).GetName() == "doing_the_work_2", "queue 2's pending item #0 should be doing_the_work_2")
self.assertTrue(
queue_performer_2.GetNumPendingItems() == 9999,
"verify that queue 2 still has 9999 pending items")
self.assertTrue(queue_performer_2.GetPendingItemAtIndex(
9998).IsValid(), "queue 2's pending item #9998 is valid")
self.assertTrue(queue_performer_2.GetPendingItemAtIndex(9998).GetAddress().GetSymbol(
).GetName() == "doing_the_work_2", "queue 2's pending item #0 should be doing_the_work_2")
self.assertTrue(queue_performer_2.GetPendingItemAtIndex(
9999).IsValid() == False, "queue 2's pending item #9999 is invalid")