<commit_before>#include "hooks_py.h"
#include "simulator.h"
#include "magic_server.h"
#include "sim_api.h"

static PyObject *
setROI(PyObject *self, PyObject *args)
{
   bool inRoi = false;

   if (!PyArg_ParseTuple(args, "b", &inRoi))
      return NULL;

   Sim()->getMagicServer()->Magic_unlocked(INVALID_CORE_ID, INVALID_THREAD_ID, inRoi ? SIM_CMD_ROI_START : SIM_CMD_ROI_END, 0, 0);

   Py_RETURN_NONE;
}

static PyObject *
setInstrumentationMode(PyObject *self, PyObject *args)
{
   long int mode = 999;

   if (!PyArg_ParseTuple(args, "l", &mode))
      return NULL;

   switch (mode)
   {
      case SIM_OPT_INSTRUMENT_DETAILED:
      case SIM_OPT_INSTRUMENT_WARMUP:
      case SIM_OPT_INSTRUMENT_FASTFORWARD:
         Sim()->getMagicServer()->Magic_unlocked(INVALID_CORE_ID, INVALID_THREAD_ID, SIM_CMD_INSTRUMENT_MODE, mode, 0);
         break;
      default:
         LOG_PRINT_ERROR("Unexpected instrumentation mode from python: %lx.", mode);
         return NULL;
   }

   Py_RETURN_NONE;
}

static PyObject *
simulatorAbort(PyObject *self, PyObject *args)
{
   // Exit now, cleaning up as best as possible
   // For benchmarks where, after ROI, functionally simulating until the end takes too long.

   // If we're still in ROI, make sure we end it properly
   Sim()->getMagicServer()->Magic_unlocked(INVALID_CORE_ID, INVALID_THREAD_ID, SIM_CMD_ROI_END, 0, 0);

   LOG_PRINT("Application exit.");
   Simulator::release();

   exit(0);
}

static PyMethodDef PyControlMethods[] = {
   { "set_roi", setROI, METH_VARARGS, "Set whether or not we are in the ROI" },
   { "set_instrumentation_mode", setInstrumentationMode, METH_VARARGS, "Set instrumentation mode" },
   { "abort", simulatorAbort, METH_VARARGS, "Stop simulation now" },
   { NULL, NULL, 0, NULL } /* Sentinel */
};

void HooksPy::PyControl::setup(void)
{
   PyObject *pModule = Py_InitModule("sim_control", PyControlMethods);

   {
      PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_DETAILED);
      PyObject_SetAttrString(pModule, "DETAILED", pGlobalConst);
      Py_DECREF(pGlobalConst);
   }
   {
      PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_WARMUP);
      PyObject_SetAttrString(pModule, "WARMUP", pGlobalConst);
      Py_DECREF(pGlobalConst);
   }
   {
      PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_FASTFORWARD);
      PyObject_SetAttrString(pModule, "FASTFORWARD", pGlobalConst);
      Py_DECREF(pGlobalConst);
   }
}
<commit_msg>[scripting] Fix ROI begin/end through sim.control.set_roi to not use magic instructions as those are ignored in --roi-script mode<commit_after>#include "hooks_py.h"
#include "simulator.h"
#include "magic_server.h"
#include "sim_api.h"

static PyObject *
setROI(PyObject *self, PyObject *args)
{
   bool inRoi = false;

   if (!PyArg_ParseTuple(args, "b", &inRoi))
      return NULL;

   Sim()->getMagicServer()->setPerformance(inRoi);

   Py_RETURN_NONE;
}

static PyObject *
setInstrumentationMode(PyObject *self, PyObject *args)
{
   long int mode = 999;

   if (!PyArg_ParseTuple(args, "l", &mode))
      return NULL;

   switch (mode)
   {
      case SIM_OPT_INSTRUMENT_DETAILED:
      case SIM_OPT_INSTRUMENT_WARMUP:
      case SIM_OPT_INSTRUMENT_FASTFORWARD:
         Sim()->getMagicServer()->Magic_unlocked(INVALID_CORE_ID, INVALID_THREAD_ID, SIM_CMD_INSTRUMENT_MODE, mode, 0);
         break;
      default:
         LOG_PRINT_ERROR("Unexpected instrumentation mode from python: %lx.", mode);
         return NULL;
   }

   Py_RETURN_NONE;
}

static PyObject *
simulatorAbort(PyObject *self, PyObject *args)
{
   // Exit now, cleaning up as best as possible
   // For benchmarks where, after ROI, functionally simulating until the end takes too long.

   // If we're still in ROI, make sure we end it properly
   Sim()->getMagicServer()->setPerformance(false);

   LOG_PRINT("Application exit.");
   Simulator::release();

   exit(0);
}

static PyMethodDef PyControlMethods[] = {
   { "set_roi", setROI, METH_VARARGS, "Set whether or not we are in the ROI" },
   { "set_instrumentation_mode", setInstrumentationMode, METH_VARARGS, "Set instrumentation mode" },
   { "abort", simulatorAbort, METH_VARARGS, "Stop simulation now" },
   { NULL, NULL, 0, NULL } /* Sentinel */
};

void HooksPy::PyControl::setup(void)
{
   PyObject *pModule = Py_InitModule("sim_control", PyControlMethods);

   {
      PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_DETAILED);
      PyObject_SetAttrString(pModule, "DETAILED", pGlobalConst);
      Py_DECREF(pGlobalConst);
   }
   {
      PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_WARMUP);
      PyObject_SetAttrString(pModule, "WARMUP", pGlobalConst);
      Py_DECREF(pGlobalConst);
   }
   {
      PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_FASTFORWARD);
      PyObject_SetAttrString(pModule, "FASTFORWARD", pGlobalConst);
      Py_DECREF(pGlobalConst);
   }
}

{'fraction_non_alphanumeric': 0.11169995923359152, 'fraction_numerical': 0.003261312678353037, 'mean_word_length': 4.187103594080338, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1184380', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1669, 'n_words': 363}