/[svn]/linuxsampler/trunk/src/scriptvm/ScriptVM.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/scriptvm/ScriptVM.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2945 by schoenebeck, Thu Jul 14 00:22:26 2016 UTC revision 3034 by schoenebeck, Mon Oct 31 00:05:00 2016 UTC
# Line 19  Line 19 
19    
20  #define DEBUG_SCRIPTVM_CORE 0  #define DEBUG_SCRIPTVM_CORE 0
21    
22    /**
23     * Maximum amount of VM instructions to be executed per ScriptVM::exec() call
24     * in case loops are involved, before the script got automatically suspended
25     * for a certain amount of time to avoid any RT instability issues.
26     *
27     * The following value takes a max. execution time of 300 microseconds as aimed
28     * target, assuming an execution time of approximately 5 microseconds per
29     * instruction this leads to the very approximate value set below.
30     */
31    #define SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT 70
32    
33    /**
34     * Absolute maximum amount of VM instructions to be executed per
35     * ScriptVM::exec() call (even if no loops are involved), before the script got
36     * automatically suspended for a certain amount of time to avoid any RT
37     * instability issues.
38     *
39     * A distinction between "soft" and "hard" limit is done here ATM because a
40     * script author typically expects that his script might be interrupted
41     * automatically if he is using while() loops, however he might not be
42     * prepared that his script might also be interrupted if no loop is involved
43     * (i.e. on very large scripts).
44     *
45     * The following value takes a max. execution time of 1000 microseconds as
46     * aimed target, assuming an execution time of approximately 5 microseconds per
47     * instruction this leads to the very approximate value set below.
48     */
49    #define SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD 210
50    
51    /**
52     * In case either SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT or
53     * SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD was exceeded when calling
54     * ScriptVM::exec() : the amount of microseconds the respective script
55     * execution instance should be automatically suspended by the VM.
56     */
57    #define SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS 1000
58    
59  int InstrScript_parse(LinuxSampler::ParserContext*);  int InstrScript_parse(LinuxSampler::ParserContext*);
60    
61  namespace LinuxSampler {  namespace LinuxSampler {
62    
63        #if DEBUG_SCRIPTVM_CORE
64      static void _printIndents(int n) {      static void _printIndents(int n) {
65          for (int i = 0; i < n; ++i) printf("  ");          for (int i = 0; i < n; ++i) printf("  ");
66          fflush(stdout);          fflush(stdout);
67      }      }
68        #endif
69    
70      static int _requiredMaxStackSizeFor(Statement* statement, int depth = 0) {      static int _requiredMaxStackSizeFor(Statement* statement, int depth = 0) {
71          if (!statement) return 1;          if (!statement) return 1;
# Line 92  namespace LinuxSampler { Line 131  namespace LinuxSampler {
131          return max;          return max;
132      }      }
133    
134      ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) {      ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true) {
135          m_fnMessage = new CoreVMFunction_message;          m_fnMessage = new CoreVMFunction_message;
136          m_fnExit = new CoreVMFunction_exit;          m_fnExit = new CoreVMFunction_exit;
137          m_fnWait = new CoreVMFunction_wait(this);          m_fnWait = new CoreVMFunction_wait(this);
# Line 103  namespace LinuxSampler { Line 142  namespace LinuxSampler {
142          m_fnDec = new CoreVMFunction_dec;          m_fnDec = new CoreVMFunction_dec;
143          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;          m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER;
144          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;          m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER;
145            m_fnShLeft = new CoreVMFunction_sh_left;
146            m_fnShRight = new CoreVMFunction_sh_right;
147            m_fnMin = new CoreVMFunction_min;
148            m_fnMax = new CoreVMFunction_max;
149      }      }
150    
151      ScriptVM::~ScriptVM() {      ScriptVM::~ScriptVM() {
# Line 114  namespace LinuxSampler { Line 157  namespace LinuxSampler {
157          delete m_fnNumElements;          delete m_fnNumElements;
158          delete m_fnInc;          delete m_fnInc;
159          delete m_fnDec;          delete m_fnDec;
160            delete m_fnShLeft;
161            delete m_fnShRight;
162            delete m_fnMin;
163            delete m_fnMax;
164          delete m_varRealTimer;          delete m_varRealTimer;
165          delete m_varPerfTimer;          delete m_varPerfTimer;
166      }      }
# Line 215  namespace LinuxSampler { Line 262  namespace LinuxSampler {
262          else if (name == "num_elements") return m_fnNumElements;          else if (name == "num_elements") return m_fnNumElements;
263          else if (name == "inc") return m_fnInc;          else if (name == "inc") return m_fnInc;
264          else if (name == "dec") return m_fnDec;          else if (name == "dec") return m_fnDec;
265            else if (name == "sh_left") return m_fnShLeft;
266            else if (name == "sh_right") return m_fnShRight;
267            else if (name == "min") return m_fnMin;
268            else if (name == "max") return m_fnMax;
269          return NULL;          return NULL;
270      }      }
271    
# Line 237  namespace LinuxSampler { Line 288  namespace LinuxSampler {
288      }      }
289    
290      std::map<String,int> ScriptVM::builtInConstIntVariables() {      std::map<String,int> ScriptVM::builtInConstIntVariables() {
291          return std::map<String,int>();          std::map<String,int> m;
292    
293            m["$NI_CB_TYPE_INIT"] = VM_EVENT_HANDLER_INIT;
294            m["$NI_CB_TYPE_NOTE"] = VM_EVENT_HANDLER_NOTE;
295            m["$NI_CB_TYPE_RELEASE"] = VM_EVENT_HANDLER_RELEASE;
296            m["$NI_CB_TYPE_CONTROLLER"] = VM_EVENT_HANDLER_CONTROLLER;
297    
298            return m;
299      }      }
300    
301      VMEventHandler* ScriptVM::currentVMEventHandler() {      VMEventHandler* ScriptVM::currentVMEventHandler() {
# Line 253  namespace LinuxSampler { Line 311  namespace LinuxSampler {
311          return m_parserContext->execContext;          return m_parserContext->execContext;
312      }      }
313    
314        void ScriptVM::setAutoSuspendEnabled(bool b) {
315            m_autoSuspend = b;
316        }
317    
318        bool ScriptVM::isAutoSuspendEnabled() const {
319            return m_autoSuspend;
320        }
321    
322      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
323          m_parserContext = dynamic_cast<ParserContext*>(parserContext);          m_parserContext = dynamic_cast<ParserContext*>(parserContext);
324          if (!m_parserContext) {          if (!m_parserContext) {
# Line 276  namespace LinuxSampler { Line 342  namespace LinuxSampler {
342    
343          ctx->status = VM_EXEC_RUNNING;          ctx->status = VM_EXEC_RUNNING;
344          StmtFlags_t flags = STMT_SUCCESS;          StmtFlags_t flags = STMT_SUCCESS;
345            int instructionsCounter = 0;
346    
347          int& frameIdx = ctx->stackFrame;          int& frameIdx = ctx->stackFrame;
348          if (frameIdx < 0) { // start condition ...          if (frameIdx < 0) { // start condition ...
# Line 352  namespace LinuxSampler { Line 419  namespace LinuxSampler {
419                          ctx->pushStack(                          ctx->pushStack(
420                              whileStmt->statements()                              whileStmt->statements()
421                          );                          );
422                            if (flags == STMT_SUCCESS && m_autoSuspend &&
423                                instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT)
424                            {
425                                flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
426                                ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
427                            }
428                      } else ctx->popStack();                      } else ctx->popStack();
429                        break;
430                  }                  }
431              }              }
432    
433                if (flags == STMT_SUCCESS && m_autoSuspend &&
434                    instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD)
435                {
436                    flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED);
437                    ctx->suspendMicroseconds = SCRIPT_VM_FORCE_SUSPENSION_MICROSECONDS;
438                }
439    
440                ++instructionsCounter;
441          }          }
442    
443          if (flags & STMT_SUSPEND_SIGNALLED) {          if (flags & STMT_SUSPEND_SIGNALLED) {

Legend:
Removed from v.2945  
changed lines
  Added in v.3034

  ViewVC Help
Powered by ViewVC