build event timeline
Stefan Schuermans

Stefan Schuermans commited on 2020-05-23 15:50:39
Showing 2 changed files, with 49 additions and 11 deletions.

... ...
@@ -68,7 +68,40 @@ class BaseEvent():
68 68
         return self._timestamp
69 69
 
70 70
 
71
-class ProcBegin(BaseEvent):
71
+class ProcBeginOrEnd(BaseEvent):
72
+    """
73
+    Process begin or end event.
74
+    """
75
+    def __init__(self, pb2_ev: pb2.event):
76
+        """
77
+        Initialize process begin or end event from PB2 event.
78
+        """
79
+        super().__init__(pb2_ev)
80
+        self._process = None
81
+        self._pid = None
82
+
83
+    @property
84
+    def pid(self) -> int:
85
+        """
86
+        ID of process.
87
+        """
88
+        return self._pid
89
+
90
+    @property
91
+    def process(self):
92
+        """
93
+        Process object.
94
+        """
95
+        return self._process
96
+
97
+    def setProcess(self, process):
98
+        """
99
+        Set process object.
100
+        """
101
+        self._process = process
102
+
103
+
104
+class ProcBegin(ProcBeginOrEnd):
72 105
     """
73 106
     Process begin event.
74 107
     """
... ...
@@ -87,13 +120,6 @@ class ProcBegin(BaseEvent):
87 120
         self._environ = self._pb2GetStringList(
88 121
             p_b.environ) if p_b.hasField('environ') else None
89 122
 
90
-    @property
91
-    def pid(self) -> int:
92
-        """
93
-        ID of process.
94
-        """
95
-        return self._pid
96
-
97 123
     @property
98 124
     def ppid(self) -> int:
99 125
         """
... ...
@@ -130,7 +156,7 @@ class ProcBegin(BaseEvent):
130 156
         return self._environ.copy()
131 157
 
132 158
 
133
-class ProcEnd(BaseEvent):
159
+class ProcEnd(ProcBeginOrEnd):
134 160
     """
135 161
     Process end event.
136 162
     """
... ...
@@ -55,6 +53,7 @@ class Processes(uproctrace.parse.Visitor):
55 53
         Initialize processes from a trace file (f).
56 54
         """
57 55
         super().__init__()
56
+        self._timeline = dict()  # time -> list(parse.BaseEvent)
58 57
         self._current_processes = dict()  # pid -> Process
59 58
         self._readTrace(proto_file)
60 59
 
... ...
@@ -65,16 +64,25 @@ class Processes(uproctrace.parse.Visitor):
65 64
         while uproctrace.parse.parse_event(proto_file, self):
66 65
             pass
67 66
 
67
+    def _visitBaseEvent(self, event: uproctrace.parse.BaseEvent):
68
+        """
69
+        Common processing for all events.
70
+        """
71
+        # store event in timeline
72
+        self._timeline.setdefault(event.time, list()).append(event)
73
+
68 74
     def visitProcBegin(self, proc_begin: uproctrace.parse.ProcBegin):
69 75
         """
70 76
         Process a process begin event.
71 77
         """
78
+        self._visitBaseEvent(proc_begin)
72 79
         # new process
73 80
         proc = Process(proc_begin.pid)
74 81
         # add process to dict of current processes
75 82
         self._current_processes[proc_begin.pid] = proc
76
-        # set begin event of process
83
+        # set begin event of process and process of begin event
77 84
         proc.setBegin(proc_begin)
85
+        proc_begin.setProcess(proc)
78 86
         # connect to parent
79 87
         if proc_begin.ppid in self._current_processes:
80 88
             parent = self._current_processes[proc_begin.ppid]
... ...
@@ -85,12 +93,14 @@ class Processes(uproctrace.parse.Visitor):
85 93
         """
86 94
         Process a process end event.
87 95
         """
96
+        self._visitBaseEvent(proc_end)
88 97
         # get process (or create it it is not known)
89 98
         if proc_end.pid in self._current_processes:
90 99
             proc = self._current_processes[proc_end.pid]
91 100
         else:
92 101
             proc = Process(proc_end.pid)
93
-        # set end event of process
102
+        # set end event of process and process of end event
94 103
         proc.setEnd(proc_end)
104
+        proc_end.setProcess(proc)
95 105
         # remove process from dict of current processes (it ended)
96 106
         self._current_processes[proc_end.pid] = None
97 107