Debian Codegraph @

test/low_level/test_timing_thread.py

raw symbols SCIP
1 import ctypes
2 import os
3 import sys
4 import time
5
6 import pyinstrument.low_level.stat_profile as native_module
7
8 from ..util import busy_wait, flaky_in_ci
9
10 lib = ctypes.CDLL(native_module.__file__)
11
12 pyi_timing_thread_subscribe = lib.pyi_timing_thread_subscribe
13 pyi_timing_thread_subscribe.argtypes = [ctypes.c_double]
14 pyi_timing_thread_subscribe.restype = ctypes.c_int
15
16 pyi_timing_thread_get_time = lib.pyi_timing_thread_get_time
17 pyi_timing_thread_get_time.argtypes = []
18 pyi_timing_thread_get_time.restype = ctypes.c_double
19
20 pyi_timing_thread_get_interval = lib.pyi_timing_thread_get_interval
21 pyi_timing_thread_get_interval.argtypes = []
22 pyi_timing_thread_get_interval.restype = ctypes.c_double
23
24 pyi_timing_thread_unsubscribe = lib.pyi_timing_thread_unsubscribe
25 pyi_timing_thread_unsubscribe.argtypes = [ctypes.c_int]
26 pyi_timing_thread_unsubscribe.restype = ctypes.c_int
27
28 PYI_TIMING_THREAD_UNKNOWN_ERROR = -1
29 PYI_TIMING_THREAD_TOO_MANY_SUBSCRIBERS = -2
30
31
32 if sys.platform == "win32":
33 # on windows, the thread scheduling 'quanta', the time that a thread can run
34 # before potentially being pre-empted, is 20-30ms. This means that the
35 # worst-case, we have to wait 30ms before the timing thread gets a chance to
36 # run. This isn't really a huge problem in practice, because thread-based
37 # timing isn't much use on windows, since the synchronous timing functions are
38 # so fast.
39 WAIT_TIME = 0.03
40 elif os.environ.get("QEMU_EMULATED"):
41 # the scheduler seems slower under emulation
42 WAIT_TIME = 0.2
43 else:
44 WAIT_TIME = 0.015
45
46
47 @flaky_in_ci
48 def test():
49 # check the thread isn't running to begin with
50 assert pyi_timing_thread_get_interval() == -1
51
52 time_before = pyi_timing_thread_get_time()
53 time.sleep(WAIT_TIME)
54 assert pyi_timing_thread_get_time() == time_before
55
56 # subscribe
57 subscription_id = pyi_timing_thread_subscribe(0.001)
58 try:
59 assert subscription_id >= 0
60
61 assert pyi_timing_thread_get_interval() == 0.001
62
63 # check it's updating
64 busy_wait(WAIT_TIME)
65 time_a = pyi_timing_thread_get_time()
66 assert time_a > time_before
67 busy_wait(WAIT_TIME)
68 time_b = pyi_timing_thread_get_time()
69 assert time_b > time_a
70
71 # unsubscribe
72 assert pyi_timing_thread_unsubscribe(subscription_id) == 0
73
74 assert pyi_timing_thread_get_interval() == -1
75
76 # check it's stopped updating
77 time.sleep(WAIT_TIME)
78 time_c = pyi_timing_thread_get_time()
79 time.sleep(WAIT_TIME)
80 time_d = pyi_timing_thread_get_time()
81 assert time_c == time_d
82 finally:
83 # ensure the subscriber is removed even if the test fails
84 pyi_timing_thread_unsubscribe(subscription_id)
85
86
87 def test_max_subscribers():
88 subscription_ids = []
89
90 try:
91 for i in range(1000):
92 subscription_id = pyi_timing_thread_subscribe(0.001)
93 assert subscription_id >= 0
94 subscription_ids.append(subscription_id)
95
96 # the next one should fail
97 assert pyi_timing_thread_subscribe(0.001) == PYI_TIMING_THREAD_TOO_MANY_SUBSCRIBERS
98
99 # unsubscribe them in FIFO order
100 for subscription_id in subscription_ids:
101 assert pyi_timing_thread_get_interval() == 0.001
102 assert pyi_timing_thread_unsubscribe(subscription_id) == 0
103
104 # check there are no subscribers left
105 assert pyi_timing_thread_get_interval() == -1
106 finally:
107 # ensure all subscription ids are removed even if the test fails
108 while subscription_ids:
109 subscription_id = subscription_ids.pop()
110 pyi_timing_thread_unsubscribe(subscription_id)