| 1 |
|
| 2 |
import os, py |
| 3 |
from rpython.jit.backend.test.support import CCompiledMixin |
| 4 |
from rpython.rlib.jit import JitDriver |
| 5 |
from rpython.tool.udir import udir |
| 6 |
from rpython.rlib import rthread |
| 7 |
from rpython.translator.translator import TranslationContext |
| 8 |
from rpython.jit.backend.detect_cpu import getcpuclass |
| 9 |
from rpython.rlib.rweaklist import RWeakListMixin |
| 10 |
|
| 11 |
class CompiledVmprofTest(CCompiledMixin): |
| 12 |
CPUClass = getcpuclass() |
| 13 |
|
| 14 |
def _get_TranslationContext(self): |
| 15 |
t = TranslationContext() |
| 16 |
t.config.translation.gc = 'incminimark' |
| 17 |
t.config.translation.list_comprehension_operations = True |
| 18 |
return t |
| 19 |
|
| 20 |
def test_vmprof(self): |
| 21 |
from rpython.rlib import rvmprof |
| 22 |
|
| 23 |
class MyCode: |
| 24 |
_vmprof_unique_id = 0 |
| 25 |
_vmprof_weak_list = RWeakListMixin() ; _vmprof_weak_list.initialize() |
| 26 |
def __init__(self, name): |
| 27 |
self.name = name |
| 28 |
|
| 29 |
def get_name(code): |
| 30 |
return code.name |
| 31 |
|
| 32 |
code2 = MyCode("py:y:foo:4") |
| 33 |
rvmprof.register_code(code2, get_name) |
| 34 |
|
| 35 |
try: |
| 36 |
rvmprof.register_code_object_class(MyCode, get_name) |
| 37 |
except rvmprof.VMProfPlatformUnsupported as e: |
| 38 |
py.test.skip(str(e)) |
| 39 |
|
| 40 |
def get_unique_id(code): |
| 41 |
return rvmprof.get_unique_id(code) |
| 42 |
|
| 43 |
driver = JitDriver(greens = ['code'], reds = ['i', 's', 'num'], |
| 44 |
is_recursive=True, get_unique_id=get_unique_id) |
| 45 |
|
| 46 |
@rvmprof.vmprof_execute_code("xcode13", lambda code, num: code) |
| 47 |
def main(code, num): |
| 48 |
return main_jitted(code, num) |
| 49 |
|
| 50 |
def main_jitted(code, num): |
| 51 |
s = 0 |
| 52 |
i = 0 |
| 53 |
while i < num: |
| 54 |
driver.jit_merge_point(code=code, i=i, s=s, num=num) |
| 55 |
s += (i << 1) |
| 56 |
if i % 3 == 0 and code is not code2: |
| 57 |
main(code2, 100) |
| 58 |
i += 1 |
| 59 |
return s |
| 60 |
|
| 61 |
tmpfilename = str(udir.join('test_rvmprof')) |
| 62 |
|
| 63 |
def f(num): |
| 64 |
rthread.get_ident() |
| 65 |
code = MyCode("py:x:foo:3") |
| 66 |
rvmprof.register_code(code, get_name) |
| 67 |
fd = os.open(tmpfilename, os.O_WRONLY | os.O_CREAT, 0666) |
| 68 |
period = 0.0001 |
| 69 |
rvmprof.enable(fd, period) |
| 70 |
res = main(code, num) |
| 71 |
|
| 72 |
rvmprof.disable() |
| 73 |
os.close(fd) |
| 74 |
return 0 |
| 75 |
|
| 76 |
def check_vmprof_output(): |
| 77 |
from vmprof import read_profile |
| 78 |
tmpfile = str(udir.join('test_rvmprof')) |
| 79 |
stats = read_profile(tmpfile) |
| 80 |
t = stats.get_tree() |
| 81 |
assert t.name == 'py:x:foo:3' |
| 82 |
assert len(t.children) == 1 |
| 83 |
|
| 84 |
self.meta_interp(f, [1000000], inline=True) |
| 85 |
try: |
| 86 |
import vmprof |
| 87 |
except ImportError: |
| 88 |
pass |
| 89 |
else: |
| 90 |
check_vmprof_output() |