Debian Codegraph @

Lib/test/test_free_threading/test_monitoring.py

raw
1 """Tests monitoring, sys.settrace, and sys.setprofile in a multi-threaded
2 environment to verify things are thread-safe in a free-threaded build"""
3
4 import sys
5 import threading
6 import time
7 import unittest
8 import weakref
9
10 from contextlib import contextmanager
11 from sys import monitoring
12 from test.support import threading_helper
13 from threading import Thread, _PyRLock, Barrier
14 from unittest import TestCase
15
16
17 class InstrumentationMultiThreadedMixin:
18 thread_count = 10
19 func_count = 50
20 fib = 12
21
22 def after_threads(self):
23 """Runs once after all the threads have started"""
24 pass
25
26 def during_threads(self):
27 """Runs repeatedly while the threads are still running"""
28 pass
29
30 def work(self, n, funcs):
31 """Fibonacci function which also calls a bunch of random functions"""
32 for func in funcs:
33 func()
34 if n < 2:
35 return n
36 return self.work(n - 1, funcs) + self.work(n - 2, funcs)
37
38 def start_work(self, n, funcs, barrier):
39 # With the GIL builds we need to make sure that the hooks have
40 # a chance to run as it's possible to run w/o releasing the GIL.
41 barrier.wait()
42 self.work(n, funcs)
43
44 def after_test(self):
45 """Runs once after the test is done"""
46 pass
47
48 def test_instrumentation(self):
49 # Setup a bunch of functions which will need instrumentation...
50 funcs = []
51 for i in range(self.func_count):
52 x = {}
53 exec("def f(): pass", x)
54 funcs.append(x["f"])
55
56 barrier = Barrier(self.thread_count + 1)
57 threads = []
58 for i in range(self.thread_count):
59 # Each thread gets a copy of the func list to avoid contention
60 t = Thread(target=self.start_work, args=(self.fib, list(funcs), barrier))
61 t.start()
62 threads.append(t)
63
64 self.after_threads()
65 barrier.wait()
66
67 while True:
68 any_alive = False
69 for t in threads:
70 if t.is_alive():
71 any_alive = True
72 break
73
74 if not any_alive:
75 break
76
77 self.during_threads()
78 # Sleep to avoid setting monitoring events too rapidly and
79 # overflowing the global version counter
80 time.sleep(0.0001)
81
82 self.after_test()
83
84
85 class MonitoringTestMixin:
86 def setUp(self):
87 for i in range(6):
88 if monitoring.get_tool(i) is None:
89 self.tool_id = i
90 monitoring.use_tool_id(i, self.__class__.__name__)
91 break
92
93 def tearDown(self):
94 monitoring.free_tool_id(self.tool_id)
95
96
97 @threading_helper.requires_working_threading()
98 class SetPreTraceMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
99 """Sets tracing one time after the threads have started"""
100
101 def setUp(self):
102 super().setUp()
103 self.called = False
104
105 def after_test(self):
106 self.assertTrue(self.called)
107
108 def trace_func(self, frame, event, arg):
109 self.called = True
110 return self.trace_func
111
112 def after_threads(self):
113 sys.settrace(self.trace_func)
114
115
116 @threading_helper.requires_working_threading()
117 class MonitoringMultiThreaded(
118 MonitoringTestMixin, InstrumentationMultiThreadedMixin, TestCase
119 ):
120 """Uses sys.monitoring and repeatedly toggles instrumentation on and off"""
121
122 def setUp(self):
123 super().setUp()
124 self.set = False
125 monitoring.register_callback(
126 self.tool_id, monitoring.events.LINE, self.callback
127 )
128
129 def tearDown(self):
130 monitoring.set_events(self.tool_id, 0)
131 super().tearDown()
132
133 def callback(self, *args):
134 pass
135
136 def during_threads(self):
137 if self.set:
138 monitoring.set_events(
139 self.tool_id, monitoring.events.CALL | monitoring.events.LINE
140 )
141 else:
142 monitoring.set_events(self.tool_id, 0)
143 self.set = not self.set
144
145
146 @threading_helper.requires_working_threading()
147 class SetTraceMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
148 """Uses sys.settrace and repeatedly toggles instrumentation on and off"""
149
150 def setUp(self):
151 self.set = False
152
153 def tearDown(self):
154 sys.settrace(None)
155
156 def trace_func(self, frame, event, arg):
157 return self.trace_func
158
159 def during_threads(self):
160 if self.set:
161 sys.settrace(self.trace_func)
162 else:
163 sys.settrace(None)
164 self.set = not self.set
165
166
167 @threading_helper.requires_working_threading()
168 class SetProfileMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
169 """Uses sys.setprofile and repeatedly toggles instrumentation on and off"""
170
171 def setUp(self):
172 self.set = False
173
174 def tearDown(self):
175 sys.setprofile(None)
176
177 def trace_func(self, frame, event, arg):
178 return self.trace_func
179
180 def during_threads(self):
181 if self.set:
182 sys.setprofile(self.trace_func)
183 else:
184 sys.setprofile(None)
185 self.set = not self.set
186
187
188 @threading_helper.requires_working_threading()
189 class SetProfileAllThreadsMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
190 """Uses threading.setprofile_all_threads and repeatedly toggles instrumentation on and off"""
191
192 def setUp(self):
193 self.set = False
194
195 def tearDown(self):
196 threading.setprofile_all_threads(None)
197
198 def trace_func(self, frame, event, arg):
199 return self.trace_func
200
201 def during_threads(self):
202 if self.set:
203 threading.setprofile_all_threads(self.trace_func)
204 else:
205 threading.setprofile_all_threads(None)
206 self.set = not self.set
207
208
209 class SetProfileAllMultiThreaded(TestCase):
210 def test_profile_all_threads(self):
211 done = threading.Event()
212
213 def func():
214 pass
215
216 def bg_thread():
217 while not done.is_set():
218 func()
219 func()
220 func()
221 func()
222 func()
223
224 def my_profile(frame, event, arg):
225 return None
226
227 bg_threads = []
228 for i in range(10):
229 t = threading.Thread(target=bg_thread)
230 t.start()
231 bg_threads.append(t)
232
233 for i in range(100):
234 threading.setprofile_all_threads(my_profile)
235 threading.setprofile_all_threads(None)
236
237 done.set()
238 for t in bg_threads:
239 t.join()
240
241
242 class TraceBuf:
243 def __init__(self):
244 self.traces = []
245 self.traces_lock = threading.Lock()
246
247 def append(self, trace):
248 with self.traces_lock:
249 self.traces.append(trace)
250
251
252 @threading_helper.requires_working_threading()
253 class MonitoringMisc(MonitoringTestMixin, TestCase):
254 def register_callback(self, barrier):
255 barrier.wait()
256
257 def callback(*args):
258 pass
259
260 for i in range(200):
261 monitoring.register_callback(self.tool_id, monitoring.events.LINE, callback)
262
263 self.refs.append(weakref.ref(callback))
264
265 def test_register_callback(self):
266 self.refs = []
267 threads = []
268 barrier = Barrier(5)
269 for i in range(5):
270 t = Thread(target=self.register_callback, args=(barrier,))
271 t.start()
272 threads.append(t)
273
274 for thread in threads:
275 thread.join()
276
277 monitoring.register_callback(self.tool_id, monitoring.events.LINE, None)
278 for ref in self.refs:
279 self.assertEqual(ref(), None)
280
281 def test_set_local_trace_opcodes(self):
282 def trace(frame, event, arg):
283 frame.f_trace_opcodes = True
284 return trace
285
286 loops = 1_000
287
288 sys.settrace(trace)
289 try:
290 l = _PyRLock()
291
292 def f():
293 for i in range(loops):
294 with l:
295 pass
296
297 t = Thread(target=f)
298 t.start()
299 for i in range(loops):
300 with l:
301 pass
302 t.join()
303 finally:
304 sys.settrace(None)
305
306 def test_toggle_setprofile_no_new_events(self):
307 # gh-136396: Make sure that profile functions are called for newly
308 # created threads when profiling is toggled but the set of monitoring
309 # events doesn't change
310 traces = []
311
312 def profiler(frame, event, arg):
313 traces.append((frame.f_code.co_name, event, arg))
314
315 def a(x, y):
316 return b(x, y)
317
318 def b(x, y):
319 return max(x, y)
320
321 sys.setprofile(profiler)
322 try:
323 a(1, 2)
324 finally:
325 sys.setprofile(None)
326 traces.clear()
327
328 def thread_main(x, y):
329 sys.setprofile(profiler)
330 try:
331 a(x, y)
332 finally:
333 sys.setprofile(None)
334 t = Thread(target=thread_main, args=(100, 200))
335 t.start()
336 t.join()
337
338 expected = [
339 ("a", "call", None),
340 ("b", "call", None),
341 ("b", "c_call", max),
342 ("b", "c_return", max),
343 ("b", "return", 200),
344 ("a", "return", 200),
345 ("thread_main", "c_call", sys.setprofile),
346 ]
347 self.assertEqual(traces, expected)
348
349 def observe_threads(self, observer, buf):
350 def in_child(ident):
351 return ident
352
353 def child(ident):
354 with observer():
355 in_child(ident)
356
357 def in_parent(ident):
358 return ident
359
360 def parent(barrier, ident):
361 barrier.wait()
362 with observer():
363 t = Thread(target=child, args=(ident,))
364 t.start()
365 t.join()
366 in_parent(ident)
367
368 num_threads = 5
369 barrier = Barrier(num_threads)
370 threads = []
371 for i in range(num_threads):
372 t = Thread(target=parent, args=(barrier, i))
373 t.start()
374 threads.append(t)
375 for t in threads:
376 t.join()
377
378 for i in range(num_threads):
379 self.assertIn(("in_parent", "return", i), buf.traces)
380 self.assertIn(("in_child", "return", i), buf.traces)
381
382 def test_profile_threads(self):
383 buf = TraceBuf()
384
385 def profiler(frame, event, arg):
386 buf.append((frame.f_code.co_name, event, arg))
387
388 @contextmanager
389 def profile():
390 sys.setprofile(profiler)
391 try:
392 yield
393 finally:
394 sys.setprofile(None)
395
396 self.observe_threads(profile, buf)
397
398 def test_trace_threads(self):
399 buf = TraceBuf()
400
401 def tracer(frame, event, arg):
402 buf.append((frame.f_code.co_name, event, arg))
403 return tracer
404
405 @contextmanager
406 def trace():
407 sys.settrace(tracer)
408 try:
409 yield
410 finally:
411 sys.settrace(None)
412
413 self.observe_threads(trace, buf)
414
415 def test_monitor_threads(self):
416 buf = TraceBuf()
417
418 def monitor_py_return(code, off, retval):
419 buf.append((code.co_name, "return", retval))
420
421 monitoring.register_callback(
422 self.tool_id, monitoring.events.PY_RETURN, monitor_py_return
423 )
424
425 monitoring.set_events(
426 self.tool_id, monitoring.events.PY_RETURN
427 )
428
429 @contextmanager
430 def noop():
431 yield
432
433 self.observe_threads(noop, buf)
434
435 def test_trace_concurrent(self):
436 # Test calling a function concurrently from a tracing and a non-tracing
437 # thread
438 b = threading.Barrier(2)
439
440 def func():
441 for _ in range(100):
442 pass
443
444 def noop():
445 pass
446
447 def bg_thread():
448 b.wait()
449 func() # this may instrument `func`
450
451 def tracefunc(frame, event, arg):
452 # These calls run under tracing can race with the background thread
453 for _ in range(10):
454 func()
455 return tracefunc
456
457 t = Thread(target=bg_thread)
458 t.start()
459 try:
460 sys.settrace(tracefunc)
461 b.wait()
462 noop()
463 finally:
464 sys.settrace(None)
465 t.join()
466
467
468 if __name__ == "__main__":
469 unittest.main()