<commit_before>from rsqueakvm.util import system
if "profiler_plugin" not in system.optional_plugins:
    raise LookupError

from rsqueakvm.error import PrimitiveFailedError
from rsqueakvm.model.compiled_methods import W_CompiledMethod
from rsqueakvm.plugins.plugin import Plugin, PluginPatchScripts

from rpython.rlib import rvmprof


ProfilerPlugin = Plugin()

# ____________________________________________________________

def patch_interpreter():
    from rsqueakvm.interpreter import Interpreter
    def _get_code(interp, s_frame, s_sender, may_context_switch=True):
        return s_frame.w_method()
    _decorator = rvmprof.vmprof_execute_code("rsqueak", _get_code)
    _my_stack_frame = _decorator(Interpreter.stack_frame)
    Interpreter.stack_frame = _my_stack_frame
    print "Interpreter was patched for vmprof"
# XXX: We cannot patch the interpreter here, so we use this hacked
PluginPatchScripts.append(patch_interpreter)

def _safe(s):
    if len(s) > 210:
        s = s[:207] + '...'
    return s.replace(':', ';')

def _get_full_name(w_cm):
    # must not be longer than 255 chars
    return "py:%s:0:img" % _safe(w_cm.safe_identifier_string())

rvmprof.register_code_object_class(W_CompiledMethod, _get_full_name)

def patch_compiled_method():
    def _my_post_init(self):
        rvmprof.register_code(self, _get_full_name)
    W_CompiledMethod.post_init = _my_post_init
patch_compiled_method()

# ____________________________________________________________

@ProfilerPlugin.expose_primitive(unwrap_spec=[object, int, float])
def enable(interp, s_frame, w_rcvr, fileno, period):
    try:
        rvmprof.enable(fileno, period)
    except rvmprof.VMProfError as e:
        print e.msg
        raise PrimitiveFailedError

@ProfilerPlugin.expose_primitive()
def disable(interp, s_frame, argcount):
    try:
        rvmprof.disable()
    except rvmprof.VMProfError as e:
        print e.msg
        raise PrimitiveFailedError<commit_msg>[vmprof] we are not py, we are st. also, shorten our identifier<commit_after>from rsqueakvm.util import system
if "profiler_plugin" not in system.optional_plugins:
    raise LookupError

from rsqueakvm.error import PrimitiveFailedError
from rsqueakvm.model.compiled_methods import W_CompiledMethod
from rsqueakvm.plugins.plugin import Plugin, PluginPatchScripts

from rpython.rlib import rvmprof


ProfilerPlugin = Plugin()

# ____________________________________________________________

def patch_interpreter():
    from rsqueakvm.interpreter import Interpreter
    def _get_code(interp, s_frame, s_sender, may_context_switch=True):
        return s_frame.w_method()
    _decorator = rvmprof.vmprof_execute_code("rsqueak", _get_code)
    _my_stack_frame = _decorator(Interpreter.stack_frame)
    Interpreter.stack_frame = _my_stack_frame
    print "Interpreter was patched for vmprof"
# XXX: We cannot patch the interpreter here, so we use this hacked
PluginPatchScripts.append(patch_interpreter)

def _safe(s):
    if len(s) > 200:
        s = s[:197] + '...'
    return s.replace(':', ';')

def _get_full_name(w_cm):
    # must not be longer than 255 chars
    return "st:%s:0:/img" % _safe(w_cm.safe_identifier_string())

rvmprof.register_code_object_class(W_CompiledMethod, _get_full_name)

def patch_compiled_method():
    def _my_post_init(self):
        rvmprof.register_code(self, _get_full_name)
    W_CompiledMethod.post_init = _my_post_init
patch_compiled_method()

# ____________________________________________________________

@ProfilerPlugin.expose_primitive(unwrap_spec=[object, int, float])
def enable(interp, s_frame, w_rcvr, fileno, period):
    try:
        rvmprof.enable(fileno, period)
    except rvmprof.VMProfError as e:
        print e.msg
        raise PrimitiveFailedError

@ProfilerPlugin.expose_primitive()
def disable(interp, s_frame, argcount):
    try:
        rvmprof.disable()
    except rvmprof.VMProfError as e:
        print e.msg
        raise PrimitiveFailedError
{'fraction_non_alphanumeric': 0.08032331396817378, 'fraction_numerical': 0.005051780752715332, 'mean_word_length': 4.7894736842105265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '957583', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1266, 'n_words': 284}