| 1 |
"""Tests for thread-local bytecode.""" |
| 2 |
import textwrap |
| 3 |
import unittest |
| 4 |
|
| 5 |
from test import support |
| 6 |
from test.support import cpython_only, import_helper, requires_specialization_ft |
| 7 |
from test.support.script_helper import assert_python_ok |
| 8 |
from test.support.threading_helper import requires_working_threading |
| 9 |
|
| 10 |
# Skip this test if the _testinternalcapi module isn't available |
| 11 |
_testinternalcapi = import_helper.import_module("_testinternalcapi") |
| 12 |
|
| 13 |
|
| 14 |
@cpython_only |
| 15 |
@requires_working_threading() |
| 16 |
@unittest.skipUnless(support.Py_GIL_DISABLED, "only in free-threaded builds") |
| 17 |
class TLBCTests(unittest.TestCase): |
| 18 |
@requires_specialization_ft |
| 19 |
def test_new_threads_start_with_unspecialized_code(self): |
| 20 |
code = textwrap.dedent(""" |
| 21 |
import dis |
| 22 |
import queue |
| 23 |
import threading |
| 24 |
|
| 25 |
from _testinternalcapi import get_tlbc |
| 26 |
|
| 27 |
def all_opnames(bc): |
| 28 |
return {i.opname for i in dis._get_instructions_bytes(bc)} |
| 29 |
|
| 30 |
def f(a, b, q=None): |
| 31 |
if q is not None: |
| 32 |
q.put(get_tlbc(f)) |
| 33 |
return a + b |
| 34 |
|
| 35 |
for _ in range(100): |
| 36 |
# specialize |
| 37 |
f(1, 2) |
| 38 |
|
| 39 |
q = queue.Queue() |
| 40 |
t = threading.Thread(target=f, args=('a', 'b', q)) |
| 41 |
t.start() |
| 42 |
t.join() |
| 43 |
|
| 44 |
assert "BINARY_OP_ADD_INT" in all_opnames(get_tlbc(f)) |
| 45 |
assert "BINARY_OP_ADD_INT" not in all_opnames(q.get()) |
| 46 |
""") |
| 47 |
assert_python_ok("-X", "tlbc=1", "-c", code) |
| 48 |
|
| 49 |
@requires_specialization_ft |
| 50 |
def test_threads_specialize_independently(self): |
| 51 |
code = textwrap.dedent(""" |
| 52 |
import dis |
| 53 |
import queue |
| 54 |
import threading |
| 55 |
|
| 56 |
from _testinternalcapi import get_tlbc |
| 57 |
|
| 58 |
def all_opnames(bc): |
| 59 |
return {i.opname for i in dis._get_instructions_bytes(bc)} |
| 60 |
|
| 61 |
def f(a, b): |
| 62 |
return a + b |
| 63 |
|
| 64 |
def g(a, b, q=None): |
| 65 |
for _ in range(100): |
| 66 |
f(a, b) |
| 67 |
if q is not None: |
| 68 |
q.put(get_tlbc(f)) |
| 69 |
|
| 70 |
# specialize in main thread |
| 71 |
g(1, 2) |
| 72 |
|
| 73 |
# specialize in other thread |
| 74 |
q = queue.Queue() |
| 75 |
t = threading.Thread(target=g, args=('a', 'b', q)) |
| 76 |
t.start() |
| 77 |
t.join() |
| 78 |
|
| 79 |
assert "BINARY_OP_ADD_INT" in all_opnames(get_tlbc(f)) |
| 80 |
t_opnames = all_opnames(q.get()) |
| 81 |
assert "BINARY_OP_ADD_INT" not in t_opnames |
| 82 |
assert "BINARY_OP_ADD_UNICODE" in t_opnames |
| 83 |
""") |
| 84 |
assert_python_ok("-X", "tlbc=1", "-c", code) |
| 85 |
|
| 86 |
def test_reuse_tlbc_across_threads_different_lifetimes(self): |
| 87 |
code = textwrap.dedent(""" |
| 88 |
import queue |
| 89 |
import threading |
| 90 |
|
| 91 |
from _testinternalcapi import get_tlbc_id |
| 92 |
|
| 93 |
def f(a, b, q=None): |
| 94 |
if q is not None: |
| 95 |
q.put(get_tlbc_id(f)) |
| 96 |
return a + b |
| 97 |
|
| 98 |
q = queue.Queue() |
| 99 |
tlbc_ids = [] |
| 100 |
for _ in range(3): |
| 101 |
t = threading.Thread(target=f, args=('a', 'b', q)) |
| 102 |
t.start() |
| 103 |
t.join() |
| 104 |
tlbc_ids.append(q.get()) |
| 105 |
|
| 106 |
assert tlbc_ids[0] == tlbc_ids[1] |
| 107 |
assert tlbc_ids[1] == tlbc_ids[2] |
| 108 |
""") |
| 109 |
assert_python_ok("-X", "tlbc=1", "-c", code) |
| 110 |
|
| 111 |
@support.skip_if_sanitizer("gh-129752: data race on adaptive counter", thread=True) |
| 112 |
def test_no_copies_if_tlbc_disabled(self): |
| 113 |
code = textwrap.dedent(""" |
| 114 |
import queue |
| 115 |
import threading |
| 116 |
|
| 117 |
from _testinternalcapi import get_tlbc_id |
| 118 |
|
| 119 |
def f(a, b, q=None): |
| 120 |
if q is not None: |
| 121 |
q.put(get_tlbc_id(f)) |
| 122 |
return a + b |
| 123 |
|
| 124 |
q = queue.Queue() |
| 125 |
threads = [] |
| 126 |
for _ in range(3): |
| 127 |
t = threading.Thread(target=f, args=('a', 'b', q)) |
| 128 |
t.start() |
| 129 |
threads.append(t) |
| 130 |
|
| 131 |
tlbc_ids = [] |
| 132 |
for t in threads: |
| 133 |
t.join() |
| 134 |
tlbc_ids.append(q.get()) |
| 135 |
|
| 136 |
main_tlbc_id = get_tlbc_id(f) |
| 137 |
assert main_tlbc_id is not None |
| 138 |
assert tlbc_ids[0] == main_tlbc_id |
| 139 |
assert tlbc_ids[1] == main_tlbc_id |
| 140 |
assert tlbc_ids[2] == main_tlbc_id |
| 141 |
""") |
| 142 |
assert_python_ok("-X", "tlbc=0", "-c", code) |
| 143 |
|
| 144 |
def test_no_specialization_if_tlbc_disabled(self): |
| 145 |
code = textwrap.dedent(""" |
| 146 |
import dis |
| 147 |
import queue |
| 148 |
import threading |
| 149 |
|
| 150 |
from _testinternalcapi import get_tlbc |
| 151 |
|
| 152 |
def all_opnames(f): |
| 153 |
bc = get_tlbc(f) |
| 154 |
return {i.opname for i in dis._get_instructions_bytes(bc)} |
| 155 |
|
| 156 |
def f(a, b): |
| 157 |
return a + b |
| 158 |
|
| 159 |
for _ in range(100): |
| 160 |
f(1, 2) |
| 161 |
|
| 162 |
assert "BINARY_OP_ADD_INT" not in all_opnames(f) |
| 163 |
""") |
| 164 |
assert_python_ok("-X", "tlbc=0", "-c", code) |
| 165 |
|
| 166 |
def test_generator_throw(self): |
| 167 |
code = textwrap.dedent(""" |
| 168 |
import queue |
| 169 |
import threading |
| 170 |
|
| 171 |
from _testinternalcapi import get_tlbc_id |
| 172 |
|
| 173 |
def g(): |
| 174 |
try: |
| 175 |
yield |
| 176 |
except: |
| 177 |
yield get_tlbc_id(g) |
| 178 |
|
| 179 |
def f(q): |
| 180 |
gen = g() |
| 181 |
next(gen) |
| 182 |
q.put(gen.throw(ValueError)) |
| 183 |
|
| 184 |
q = queue.Queue() |
| 185 |
t = threading.Thread(target=f, args=(q,)) |
| 186 |
t.start() |
| 187 |
t.join() |
| 188 |
|
| 189 |
gen = g() |
| 190 |
next(gen) |
| 191 |
main_id = gen.throw(ValueError) |
| 192 |
assert main_id != q.get() |
| 193 |
""") |
| 194 |
assert_python_ok("-X", "tlbc=1", "-c", code) |
| 195 |
|
| 196 |
|
| 197 |
if __name__ == "__main__": |
| 198 |
unittest.main() |