implement properties of Process
Stefan Schuermans

Stefan Schuermans commited on 2020-05-24 10:00:17
Showing 1 changed files, with 74 additions and 4 deletions.

... ...
@@ -45,6 +45,24 @@ class Process():
45 45
             return None
46 46
         return self._begin.cmdline
47 47
 
48
+    @property
49
+    def cpu_time(self) -> float:
50
+        """
51
+        CPU time of process (in s).
52
+        """
53
+        if self._end is None:
54
+            return None
55
+        return self._end.cpu_time
56
+
57
+    @property
58
+    def cwd(self) -> str:
59
+        """
60
+        Working directory of process.
61
+        """
62
+        if self._begin is None:
63
+            return None
64
+        return self._begin.cwd
65
+
48 66
     @property
49 67
     def end_timestamp(self) -> list:
50 68
         """
... ...
@@ -54,6 +72,33 @@ class Process():
54 72
             return None
55 73
         return self._end.timestamp
56 74
 
75
+    @property
76
+    def environ(self) -> list:
77
+        """
78
+        Environment of process.
79
+        """
80
+        if self._begin is None:
81
+            return None
82
+        return self._begin.environ
83
+
84
+    @property
85
+    def exe(self) -> str:
86
+        """
87
+        Executable of process.
88
+        """
89
+        if self._begin is None:
90
+            return None
91
+        return self._begin.exe
92
+
93
+    @property
94
+    def max_rss_kb(self) -> int:
95
+        """
96
+        Maximum resident set size of process (in KiB).
97
+        """
98
+        if self._end is None:
99
+            return None
100
+        return self._end.max_rss_kb
101
+
57 102
     @property
58 103
     def proc_id(self):
59 104
         """
... ...
@@ -68,6 +113,24 @@ class Process():
68 113
         """
69 114
         return self._parent
70 115
 
116
+    @property
117
+    def sys_time(self) -> float:
118
+        """
119
+        System CPU time of process (in s).
120
+        """
121
+        if self._end is None:
122
+            return None
123
+        return self._end.sys_time
124
+
125
+    @property
126
+    def user_time(self) -> float:
127
+        """
128
+        User CPU time of process (in s).
129
+        """
130
+        if self._end is None:
131
+            return None
132
+        return self._end.user_time
133
+
71 134
     def addChild(self, child):
72 135
         """
73 136
         Add a child process.
... ...
@@ -103,8 +166,8 @@ class Processes(uproctrace.parse.Visitor):
103 166
         """
104 167
         super().__init__()
105 168
         self._timeline = dict()  # time -> list(parse.BaseEvent)
106
-        self._all_processes = list()  # list of all processess
107
-        self._current_processes = dict()  # pid -> process
169
+        self._all_processes = dict()  # proc_id -> process
170
+        self._current_processes = dict()  # pid -> process (while pid alive)
108 171
         self._toplevel_processes = list()  # list of processes without parent
109 172
         self._readTrace(proto_file)
110 173
 
... ...
@@ -112,8 +175,9 @@ class Processes(uproctrace.parse.Visitor):
112 175
         """
113 176
         Create new process, set its PID, store it and return it.
114 177
         """
115
-        proc = Process(len(self._all_processes), pid)
116
-        self._all_processes.append(proc)
178
+        proc_id = len(self._all_processes)
179
+        proc = Process(proc_id, pid)
180
+        self._all_processes[proc_id] = proc
117 181
         return proc
118 182
 
119 183
     def _readTrace(self, proto_file):
... ...
@@ -137,6 +201,12 @@ class Processes(uproctrace.parse.Visitor):
137 201
         """
138 202
         return self._toplevel_processes.copy()
139 203
 
204
+    def getProcess(self, proc_id: int):
205
+        """
206
+        Return process with proc_id, or None if not found.
207
+        """
208
+        return self._all_processes.get(proc_id)
209
+
140 210
     def visitProcBegin(self, proc_begin: uproctrace.parse.ProcBegin):
141 211
         """
142 212
         Process a process begin event.
143 213