Browse Source

Add profiling functionality

Yentl Van Tendeloo 3 years ago
parent
commit
3834f6e351
2 changed files with 25 additions and 0 deletions
  1. 8 0
      hybrid_server/classes/task.xml
  2. 17 0
      kernel/modelverse_kernel/main.py

+ 8 - 0
hybrid_server/classes/task.xml

@@ -112,14 +112,22 @@
                             <state id="processing">
                                 <onentry>
                                     <script>
+                                        if self.mvk.profiling:
+                                            try:
+                                                self.mvk.prev_timers[self.taskname] += (time.time() - self.mvk.end_timers[self.taskname])
+                                            except KeyError:
+                                                self.mvk.prev_timers[self.taskname] = time.time()
                                         start_time = time.time()
                                         # Grant each task some milliseconds of execution
+                                        timeout = (0.0, False)
                                         while (time.time() - start_time &lt; 0.05):
                                             timeout = self.execute_modelverse(self.taskname, "execute_rule", [])
                                             if timeout[0] > 0.0:
                                                 # We should not continue immediately
                                                 break
                                         self.timeout = timeout
+                                        if self.mvk.profiling:
+                                            self.mvk.end_timers[self.taskname] = time.time()
                                     </script>
                                 </onentry>
 

+ 17 - 0
kernel/modelverse_kernel/main.py

@@ -5,12 +5,15 @@ import modelverse_kernel.jit as jit
 from collections import defaultdict
 import sys
 import time
+import json
 
 if sys.version > '3': # pragma: no cover
     string_types = (str,)
 else:
     string_types = (str, unicode)
 
+PROFILE = False
+
 class ModelverseKernel(object):
     
     counter = 0
@@ -37,6 +40,14 @@ class ModelverseKernel(object):
 
         self.debug_info = defaultdict(list)
 
+        if PROFILE:
+            self.prev_timers = {}
+            self.end_timers = {}
+            self.profiling = True
+            self.profile_file = open("../profiling_results", 'w')
+        else:
+            self.profiling = False
+
     def try_to_protect(self, var):
         if isinstance(var, dict) and "id" in var and var['id'] is not None:
             return set([var['id']])
@@ -451,6 +462,9 @@ class ModelverseKernel(object):
                 self.jit.register_compiled(inst, compiled_func, suggested_name)
 
         # Run the compiled function.
+        if self.profiling:
+            self.profile_file.write(json.dumps([gen.__name__ for gen in self.request_handlers[taskname]['execute_rule'].generator_stack if gen is not None and gen.__name__ not in ['execute_rule', 'execute_jit']] + [time.time() - self.prev_timers[taskname]]) + "\n")
+            self.prev_timers[taskname] = time.time()
         if compiled_func == primitive_functions.dict_read:
             if "value" not in params['b']:
                 params['b']['value'], = yield [("RV", [params['b']['id']])]
@@ -464,6 +478,9 @@ class ModelverseKernel(object):
                 result, = results
             if result is None:
                 result = {'id': None, 'value': None}
+        if self.profiling:
+            self.profile_file.write(json.dumps([gen.__name__ for gen in self.request_handlers[taskname]['execute_rule'].generator_stack if gen is not None and gen.__name__ not in ['execute_rule', 'execute_jit']] + [compiled_func.__name__, time.time() - self.prev_timers[taskname]]) + "\n")
+            self.prev_timers[taskname] = time.time()
 
         if store:
             # Clean up the current stack, as if a return happened