py_control.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "hooks_py.h"
  2. #include "simulator.h"
  3. #include "magic_server.h"
  4. #include "sim_api.h"
  5. static PyObject *
  6. setROI(PyObject *self, PyObject *args)
  7. {
  8. bool inRoi = false;
  9. if (!PyArg_ParseTuple(args, "b", &inRoi))
  10. return NULL;
  11. Sim()->getMagicServer()->setPerformance(inRoi);
  12. Py_RETURN_NONE;
  13. }
  14. static PyObject *
  15. setInstrumentationMode(PyObject *self, PyObject *args)
  16. {
  17. long int mode = 999;
  18. if (!PyArg_ParseTuple(args, "l", &mode))
  19. return NULL;
  20. switch (mode)
  21. {
  22. case SIM_OPT_INSTRUMENT_DETAILED:
  23. case SIM_OPT_INSTRUMENT_WARMUP:
  24. case SIM_OPT_INSTRUMENT_FASTFORWARD:
  25. Sim()->getMagicServer()->Magic_unlocked(INVALID_CORE_ID, INVALID_THREAD_ID, SIM_CMD_INSTRUMENT_MODE, mode, 0);
  26. break;
  27. default:
  28. LOG_PRINT_ERROR("Unexpected instrumentation mode from python: %lx.", mode);
  29. return NULL;
  30. }
  31. Py_RETURN_NONE;
  32. }
  33. static PyObject *
  34. setProgress(PyObject *self, PyObject *args)
  35. {
  36. float progress = 0;
  37. if (!PyArg_ParseTuple(args, "f", &progress))
  38. return NULL;
  39. Sim()->getMagicServer()->setProgress(progress);
  40. Py_RETURN_NONE;
  41. }
  42. static PyObject *
  43. simulatorAbort(PyObject *self, PyObject *args)
  44. {
  45. // Exit now, cleaning up as best as possible
  46. // For benchmarks where, after ROI, functionally simulating until the end takes too long.
  47. // If we're still in ROI, make sure we end it properly
  48. Sim()->getMagicServer()->setPerformance(false);
  49. LOG_PRINT("Application exit.");
  50. Simulator::release();
  51. exit(0);
  52. }
  53. static PyMethodDef PyControlMethods[] = {
  54. { "set_roi", setROI, METH_VARARGS, "Set whether or not we are in the ROI" },
  55. { "set_instrumentation_mode", setInstrumentationMode, METH_VARARGS, "Set instrumentation mode" },
  56. { "set_progress", setProgress, METH_VARARGS, "Set simulation progress indicator (0..1)" },
  57. { "abort", simulatorAbort, METH_VARARGS, "Stop simulation now" },
  58. { NULL, NULL, 0, NULL } /* Sentinel */
  59. };
  60. void HooksPy::PyControl::setup(void)
  61. {
  62. PyObject *pModule = Py_InitModule("sim_control", PyControlMethods);
  63. {
  64. PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_DETAILED);
  65. PyObject_SetAttrString(pModule, "DETAILED", pGlobalConst);
  66. Py_DECREF(pGlobalConst);
  67. }
  68. {
  69. PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_WARMUP);
  70. PyObject_SetAttrString(pModule, "WARMUP", pGlobalConst);
  71. Py_DECREF(pGlobalConst);
  72. }
  73. {
  74. PyObject *pGlobalConst = PyInt_FromLong(SIM_OPT_INSTRUMENT_FASTFORWARD);
  75. PyObject_SetAttrString(pModule, "FASTFORWARD", pGlobalConst);
  76. Py_DECREF(pGlobalConst);
  77. }
  78. }