Selaa lähdekoodia

added performace, changed timing

Simon Van Mierlo 9 vuotta sitten
vanhempi
commit
8a52c84748

+ 2 - 9
examples/bouncingballs/python/runner_performance_cpu_time.py

@@ -4,17 +4,10 @@ Created on 27-jul.-2014
 @author: Simon
 '''
 
-import Tkinter as tk
-import target_py.target_performance_threads as target
+import target_py.target_performance_cpu_time as target
 import sys
 from sccd.runtime.statecharts_core import Event
 
 if __name__ == '__main__':
-    def callback(ctrl, behind_schedule):
-        if behind_schedule > 2000:
-            print len(ctrl.object_manager.instances)
-            ctrl.stop()
-            sys.exit()
-    controller = target.Controller(behind_schedule_callback=callback)
+    controller = target.Controller(int(sys.argv[1]), int(sys.argv[2]))
     controller.start()
-    ui.window.mainloop()

+ 0 - 2
examples/bouncingballs/python/runner_performance_threads.py

@@ -4,7 +4,6 @@ Created on 27-jul.-2014
 @author: Simon
 '''
 
-import Tkinter as tk
 import target_py.target_performance_threads as target
 import sys
 from sccd.runtime.statecharts_core import Event
@@ -17,4 +16,3 @@ if __name__ == '__main__':
             sys.exit()
     controller = target.Controller(behind_schedule_callback=callback)
     controller.start()
-    ui.window.mainloop()

+ 39 - 3
examples/bouncingballs/python/sccd_performance_cpu_time.xml

@@ -4,15 +4,19 @@
         Tkinter frame with bouncing balls in it.
     </description>
     <top>
-        import random, sys
+        import random, sys, os
     </top>
     <class name="MainApp" default="true">
         <relationships>
             <association name="fields" class="Field" />
         </relationships>
         <constructor>
+            <parameter name="fields_to_create" />
+            <parameter name="balls_to_create" />
             <body>
                 <![CDATA[
+                self.fields_to_create = fields_to_create
+                self.balls_to_create = balls_to_create
                 self.nr_of_fields = 0
                 ]]>
             </body>
@@ -25,6 +29,8 @@
                             <transition event="create_field" target="../creating">
                                 <raise scope="cd" event="create_instance">
                                     <parameter expr='"fields"' />
+                                    <parameter expr='"Field"' />
+                                    <parameter expr="self.balls_to_create" />
                                 </raise>
                             </transition>
                         </state>
@@ -34,14 +40,29 @@
                                 <raise scope="cd" event="start_instance">
                                     <parameter expr="association_name" />
                                 </raise>
+                                <script>
+                                    self.nr_of_fields += 1
+                                </script>
                             </transition>
                         </state>
                     </state>
                     <state id="spawn_windows" initial="spawning">
                         <state id="spawning">
-                            <transition target="." after="(1000 - self.getSimulatedTime() % 1000) / 1000.0" cond="self.nr_of_fields &lt; 10">
+                            <transition target="." after="0.1" cond="self.nr_of_fields &lt; self.fields_to_create">
                                 <raise event="create_field" />
                             </transition>
+                            <transition target="../stopped_spawning" cond="self.nr_of_fields &gt;= self.fields_to_create" />
+                        </state>
+                        <state id="stopped_spawning" />
+                    </state>
+                    <state id="killer" initial="killing">
+                        <state id="killing">
+                            <transition after="5" target=".">
+                                <script>
+                                    print "cpu usage for %i fields with %i balls: %.5f" % (self.fields_to_create, self.balls_to_create, os.times()[0] / 5.0)
+                                    sys.exit()
+                                </script>
+                            </transition>
                         </state>
                     </state>
                 </parallel>
@@ -55,6 +76,15 @@
             <association name="balls" class="Ball" />
             <association name="parent" class="MainApp" min="1" max="1" />
         </relationships>
+        <constructor>
+            <parameter name="balls_to_create" />
+            <body>
+                <![CDATA[
+                self.balls_to_create = balls_to_create
+                self.nr_of_balls = 0
+                ]]>
+            </body>
+        </constructor>
         <scxml initial="root">
             <state id="root" initial="running">
                 <parallel id="running">
@@ -77,18 +107,23 @@
                                 <raise scope="cd" event="start_instance">
                                     <parameter expr="association_name" />
                                 </raise>
+                                <script>
+                                    self.nr_of_balls += 1
+                                </script>
                             </transition>
                         </state>
                     </state>
                     <state id="spawn_balls" initial="spawning">
                         <state id="spawning">
-                            <transition target="." after="(50 - self.getSimulatedTime() % 50) / 1000.0">
+                            <transition target="." after="0" cond="self.nr_of_balls &lt; self.balls_to_create">
                                 <raise event="spawn_ball">
                                     <parameter expr="150" />
                                     <parameter expr="150" />
                                 </raise>
                             </transition>
+                            <transition target="../stopped_spawning" cond="self.nr_of_balls &gt;= self.balls_to_create" />
                         </state>
+                        <state id="stopped_spawning" />
                     </state>
                 </parallel>
                 <state id="deleting">
@@ -135,6 +170,7 @@
                             self.pos['y'] += self.vel['y']
                             ]]>                            
                         </script>
+                        <raise event="test" scope="narrow" target="'parent[0]'" />
                     </transition>
                 </state>
             </state>

+ 3 - 0
examples/bouncingballs/python/sccd_performance_threads.xml

@@ -34,6 +34,9 @@
                                 <raise scope="cd" event="start_instance">
                                     <parameter expr="association_name" />
                                 </raise>
+                                <script>
+                                    self.nr_of_fields += 1
+                                </script>
                             </transition>
                         </state>
                     </state>

+ 11 - 0
examples/external-input/python/runner.py

@@ -0,0 +1,11 @@
+import test, threading
+from sccd.runtime.statecharts_core import Event
+
+controller = test.Controller(False)
+def raw_inputter():
+    while 1:
+        controller.addInput(Event(raw_input(), "input", []))
+thread = threading.Thread(target=raw_inputter)
+thread.daemon = True
+thread.start()
+controller.start()

+ 180 - 0
examples/external-input/python/test.py

@@ -0,0 +1,180 @@
+"""
+Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
+
+Date:   Tue Aug 09 14:27:20 2016
+
+Model author: Yentl Van Tendeloo
+Model name:   broken
+Model description:
+Broken!
+"""
+
+from sccd.runtime.statecharts_core import *
+
+# package "broken"
+
+class A(RuntimeClassBase):
+    def __init__(self, controller):
+        RuntimeClassBase.__init__(self, controller)
+        
+        self.semantics.big_step_maximality = StatechartSemantics.TakeMany
+        self.semantics.internal_event_lifeline = StatechartSemantics.Queue
+        self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
+        self.semantics.priority = StatechartSemantics.SourceParent
+        self.semantics.concurrency = StatechartSemantics.Single
+        
+        # build Statechart structure
+        self.build_statechart_structure()
+        
+        # call user defined constructor
+        A.user_defined_constructor(self)
+    
+    def user_defined_constructor(self):
+        pass
+    
+    def user_defined_destructor(self):
+        pass
+    
+    
+    # builds Statechart structure
+    def build_statechart_structure(self):
+        
+        # state <root>
+        self.states[""] = State(0, self)
+        
+        # state /x
+        self.states["/x"] = State(1, self)
+        self.states["/x"].setEnter(self._x_enter)
+        
+        # state /ready
+        self.states["/ready"] = State(2, self)
+        self.states["/ready"].setEnter(self._ready_enter)
+        self.states["/ready"].setExit(self._ready_exit)
+        
+        # state /done
+        self.states["/done"] = State(3, self)
+        
+        # add children
+        self.states[""].addChild(self.states["/x"])
+        self.states[""].addChild(self.states["/ready"])
+        self.states[""].addChild(self.states["/done"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/x"]
+        
+        # transition /x
+        _x_0 = Transition(self, self.states["/x"], [self.states["/ready"]])
+        _x_0.setAction(self._x_0_exec)
+        _x_0.setTrigger(Event("instance_created", None))
+        self.states["/x"].addTransition(_x_0)
+        
+        # transition /ready
+        _ready_0 = Transition(self, self.states["/ready"], [self.states["/ready"]])
+        _ready_0.setAction(self._ready_0_exec)
+        _ready_0.setTrigger(Event("_0after"))
+        self.states["/ready"].addTransition(_ready_0)
+        _ready_1 = Transition(self, self.states["/ready"], [self.states["/done"]])
+        _ready_1.setAction(self._ready_1_exec)
+        _ready_1.setTrigger(Event("close", None))
+        self.states["/ready"].addTransition(_ready_1)
+    
+    def _x_enter(self):
+        self.big_step.outputEventOM(Event("create_instance", None, [self, 'child', 'B']))
+    
+    def _ready_enter(self):
+        self.addTimer(0, 0.001)
+    
+    def _ready_exit(self):
+        self.removeTimer(0)
+    
+    def _x_0_exec(self, parameters):
+        instancename = parameters[0]
+        self.instancename = instancename
+        self.big_step.outputEventOM(Event("start_instance", None, [self, self.instancename]))
+    
+    def _ready_0_exec(self, parameters):
+        for _ in range(100000):
+            pass
+    
+    def _ready_1_exec(self, parameters):
+        self.big_step.outputEventOM(Event("delete_instance", None, [self, self.instancename]))
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/x"].getEffectiveTargetStates()
+        RuntimeClassBase.initializeStatechart(self)
+
+class B(RuntimeClassBase):
+    def __init__(self, controller):
+        RuntimeClassBase.__init__(self, controller)
+        
+        self.semantics.big_step_maximality = StatechartSemantics.TakeMany
+        self.semantics.internal_event_lifeline = StatechartSemantics.Queue
+        self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
+        self.semantics.priority = StatechartSemantics.SourceParent
+        self.semantics.concurrency = StatechartSemantics.Single
+        
+        # build Statechart structure
+        self.build_statechart_structure()
+        
+        # call user defined constructor
+        B.user_defined_constructor(self)
+    
+    def user_defined_constructor(self):
+        pass
+    
+    def user_defined_destructor(self):
+        pass
+    
+    
+    # builds Statechart structure
+    def build_statechart_structure(self):
+        
+        # state <root>
+        self.states[""] = State(0, self)
+        
+        # state /z
+        self.states["/z"] = State(1, self)
+        
+        # add children
+        self.states[""].addChild(self.states["/z"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/z"]
+        
+        # transition /z
+        _z_0 = Transition(self, self.states["/z"], [self.states["/z"]])
+        _z_0.setAction(self._z_0_exec)
+        _z_0.setTrigger(Event("stop", "input"))
+        self.states["/z"].addTransition(_z_0)
+    
+    def _z_0_exec(self, parameters):
+        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent[0]', Event("close", None, [])]))
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/z"].getEffectiveTargetStates()
+        RuntimeClassBase.initializeStatechart(self)
+
+class ObjectManager(ObjectManagerBase):
+    def __init__(self, controller):
+        ObjectManagerBase.__init__(self, controller)
+    
+    def instantiate(self, class_name, construct_params):
+        if class_name == "A":
+            instance = A(self.controller)
+            instance.associations = {}
+            instance.associations["child"] = Association("B", 0, 1)
+        elif class_name == "B":
+            instance = B(self.controller)
+            instance.associations = {}
+            instance.associations["parent"] = Association("A", 1, 1)
+        else:
+            raise Exception("Cannot instantiate class " + class_name)
+        return instance
+
+class Controller(ThreadsControllerBase):
+    def __init__(self, keep_running = None, behind_schedule_callback = None):
+        if keep_running == None: keep_running = True
+        if behind_schedule_callback == None: behind_schedule_callback = None
+        ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
+        self.addInputPort("input")
+        self.object_manager.createInstance("A", [])

+ 64 - 0
examples/external-input/python/test.xml

@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<diagram author="Yentl Van Tendeloo+Simon Van Mierlo" name="broken">
+    <description>
+        Broken!
+    </description>
+    
+    <inport name="input" />
+
+    <class name="A" default="true">
+        <relationships>
+            <association name="child" class="B" min="0" max="1"/>
+        </relationships>
+
+        <scxml initial="x">
+            <state id="x">
+                <onentry>
+                    <raise scope="cd" event="create_instance">
+                        <parameter expr="'child'"/>
+                        <parameter expr="'B'"/>
+                    </raise>
+                </onentry>
+
+                <transition event="instance_created" target="../ready">
+                    <parameter name="instancename"/>
+                    <script>
+                        self.instancename = instancename
+                    </script>
+                    <raise scope="cd" event="start_instance">
+                        <parameter expr="self.instancename"/>
+                    </raise>
+                </transition>
+            </state>
+            <state id="ready">
+                <transition after="0.001" target=".">
+                    <script>
+                        for _ in range(100000):
+                            pass
+                    </script>
+                </transition>
+                <transition event="close" target="../done">
+                    <raise scope="cd" event="delete_instance">
+                        <parameter expr="self.instancename"/>
+                    </raise>
+                </transition>
+            </state>
+            <state id="done"/>
+        </scxml>
+    </class>
+
+    <class name="B">
+        <relationships>
+            <association name="parent" class="A" min="1" max="1"/>
+        </relationships>
+
+        <scxml initial="z">
+            <state id="z">
+                <transition event="stop" port="input" target=".">
+                    <raise event="close" target="'parent[0]'" />
+                </transition>
+            </state>
+        </scxml>
+    </class>
+
+</diagram>

+ 3 - 1
src/python_sccd/python_sccd_runtime/accurate_time.py

@@ -6,10 +6,12 @@ def set_start_time():
     global start_time
     if os.name == 'posix':
         start_time = t.time()
+    else:
+         start_time = t.clock()
 
 if os.name == 'posix':
     def time():
         return int((t.time() - start_time) * 1000)
 elif os.name == 'nt':
     def time():
-        return int(t.clock() * 1000)
+        return int((t.clock() - start_time) * 1000)

+ 11 - 8
src/python_sccd/python_sccd_runtime/statecharts_core.py

@@ -9,7 +9,7 @@ from infinity import INFINITY
 from Queue import Queue, Empty 
 
 from sccd.runtime.event_queue import EventQueue
-from sccd.runtime.accurate_time import time, set_start_time
+import sccd.runtime.accurate_time as accurate_time
 
 class RuntimeException(Exception):
     def __init__(self, message):
@@ -81,6 +81,7 @@ class ObjectManagerBase(object):
         self.instances = set() # a set of RuntimeClassBase instances
         self.instance_times = []
         self.eventless = set()
+        self.regex_pattern = re.compile("^([a-zA-Z_]\w*)(?:\[(\d+)\])?$")
         
     def addEvent(self, event, time_offset = 0):
         self.events.add((self.controller.simulated_time + time_offset, event))
@@ -129,11 +130,10 @@ class ObjectManagerBase(object):
     def processAssociationReference(self, input_string):
         if len(input_string) == 0 :
             raise AssociationReferenceException("Empty association reference.")
-        regex_pattern = re.compile("^([a-zA-Z_]\w*)(?:\[(\d+)\])?$")
         path_string =  input_string.split("/")
         result = []
         for piece in path_string :
-            match = regex_pattern.match(piece)
+            match = self.regex_pattern.match(piece)
             if match :
                 name = match.group(1)
                 index = match.group(2)
@@ -363,7 +363,7 @@ class ControllerBase(object):
         self.object_manager.broadcast(new_event, time_offset)
         
     def start(self):
-        set_start_time()
+        accurate_time.set_start_time()
         self.simulated_time = 0
         self.object_manager.start()
     
@@ -381,7 +381,7 @@ class ControllerBase(object):
             if e.getPort() not in self.input_ports :
                 raise InputException("Input port mismatch, no such port: " + e.getPort() + ".")
             
-            self.input_queue.add(((0 if self.simulated_time is None else time()) + time_offset, e))
+            self.input_queue.add(((0 if self.simulated_time is None else accurate_time.time()) + time_offset, e))
 
     def getEarliestEventTime(self):
         return min(self.object_manager.getEarliestEventTime(), self.input_queue.getEarliestTime())
@@ -480,7 +480,7 @@ class EventLoopControllerBase(ControllerBase):
         ControllerBase.stop(self)
 
     def run(self):
-        start_time = time()
+        start_time = accurate_time.time()
         while 1:
             # clear existing timeout
             self.event_loop.clear()
@@ -492,7 +492,7 @@ class EventLoopControllerBase(ControllerBase):
             if earliest_event_time == INFINITY:
                 if self.finished_callback: self.finished_callback() # TODO: This is not necessarily correct (keep_running necessary?)
                 return
-            now = time()
+            accurate_time.time()
             if now - start_time > 10 or earliest_event_time - now > 0:
                 self.event_loop.schedule(self.run, earliest_event_time - now, now - start_time > 10)
                 if now - earliest_event_time > 10 and now - self.last_print_time >= 1000:
@@ -548,7 +548,7 @@ class ThreadsControllerBase(ControllerBase):
             earliest_event_time = self.getEarliestEventTime()
             if earliest_event_time == INFINITY and not self.keep_running:
                 return
-            now = time()
+            now = accurate_time.time()
             if earliest_event_time - now > 0:
                 if self.behind:                
                     print '\r' + ' ' * 80,
@@ -847,6 +847,9 @@ class RuntimeClassBase(object):
         self.active = False
         self.__set_stable(True)
         
+    def sccd_yield(self):
+        return max(0, (accurate_time.time() - self.controller.simulated_time) / 1000.0)
+        
     def getSimulatedTime(self):
         return self.controller.simulated_time