diff options
author | Adrian Hunter <adrian.hunter@intel.com> | 2018-10-01 02:28:45 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2018-10-23 13:26:06 -0400 |
commit | 341e73cbd3019d350d1271803b45d84af88f2408 (patch) | |
tree | e15524ae20116722e22858e2f4c97703ec503707 /tools/perf/scripts/python/call-graph-from-sql.py | |
parent | 4be9ace7e1cdcb44c1fba1fb41ec2b92dda06732 (diff) |
perf scripts python: call-graph-from-sql.py: Refactor TreeItem class
class TreeItem represents items at all levels of the call-graph tree.
However, not all the levels represent the same data i.e. the top-level is
comms, the next level is threads, and subsequent levels are functions.
Consequently it is simpler to have separate classes for different levels
with commonality in a base class. Refactor TreeItem class accordingly.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/20181001062853.28285-12-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/scripts/python/call-graph-from-sql.py')
-rw-r--r-- | tools/perf/scripts/python/call-graph-from-sql.py | 273 |
1 files changed, 133 insertions, 140 deletions
diff --git a/tools/perf/scripts/python/call-graph-from-sql.py b/tools/perf/scripts/python/call-graph-from-sql.py index 7f2eabe7dacd..ee1085169a3e 100644 --- a/tools/perf/scripts/python/call-graph-from-sql.py +++ b/tools/perf/scripts/python/call-graph-from-sql.py | |||
@@ -74,145 +74,6 @@ def QueryExec(query, stmt): | |||
74 | if not ret: | 74 | if not ret: |
75 | raise Exception("Query failed: " + query.lastError().text()) | 75 | raise Exception("Query failed: " + query.lastError().text()) |
76 | 76 | ||
77 | class TreeItem(): | ||
78 | |||
79 | def __init__(self, db, row, parent_item): | ||
80 | self.db = db | ||
81 | self.row = row | ||
82 | self.parent_item = parent_item | ||
83 | self.query_done = False; | ||
84 | self.child_count = 0 | ||
85 | self.child_items = [] | ||
86 | self.data = ["", "", "", "", "", "", ""] | ||
87 | self.comm_id = 0 | ||
88 | self.thread_id = 0 | ||
89 | self.call_path_id = 1 | ||
90 | self.branch_count = 0 | ||
91 | self.time = 0 | ||
92 | if not parent_item: | ||
93 | self.setUpRoot() | ||
94 | |||
95 | def setUpRoot(self): | ||
96 | self.query_done = True | ||
97 | query = QSqlQuery(self.db) | ||
98 | QueryExec(query, 'SELECT id, comm FROM comms') | ||
99 | while query.next(): | ||
100 | if not query.value(0): | ||
101 | continue | ||
102 | child_item = TreeItem(self.db, self.child_count, self) | ||
103 | self.child_items.append(child_item) | ||
104 | self.child_count += 1 | ||
105 | child_item.setUpLevel1(query.value(0), query.value(1)) | ||
106 | |||
107 | def setUpLevel1(self, comm_id, comm): | ||
108 | self.query_done = True; | ||
109 | self.comm_id = comm_id | ||
110 | self.data[0] = comm | ||
111 | self.child_items = [] | ||
112 | self.child_count = 0 | ||
113 | query = QSqlQuery(self.db) | ||
114 | QueryExec(query, 'SELECT thread_id, ( SELECT pid FROM threads WHERE id = thread_id ), ( SELECT tid FROM threads WHERE id = thread_id ) FROM comm_threads WHERE comm_id = ' + str(comm_id)) | ||
115 | while query.next(): | ||
116 | child_item = TreeItem(self.db, self.child_count, self) | ||
117 | self.child_items.append(child_item) | ||
118 | self.child_count += 1 | ||
119 | child_item.setUpLevel2(comm_id, query.value(0), query.value(1), query.value(2)) | ||
120 | |||
121 | def setUpLevel2(self, comm_id, thread_id, pid, tid): | ||
122 | self.comm_id = comm_id | ||
123 | self.thread_id = thread_id | ||
124 | self.data[0] = str(pid) + ":" + str(tid) | ||
125 | |||
126 | def getChildItem(self, row): | ||
127 | return self.child_items[row] | ||
128 | |||
129 | def getParentItem(self): | ||
130 | return self.parent_item | ||
131 | |||
132 | def getRow(self): | ||
133 | return self.row | ||
134 | |||
135 | def addChild(self, call_path_id, name, dso, count, time, branch_count): | ||
136 | child_item = TreeItem(self.db, self.child_count, self) | ||
137 | child_item.comm_id = self.comm_id | ||
138 | child_item.thread_id = self.thread_id | ||
139 | child_item.call_path_id = call_path_id | ||
140 | child_item.branch_count = branch_count | ||
141 | child_item.time = time | ||
142 | child_item.data[0] = name | ||
143 | child_item.data[1] = dsoname(dso) | ||
144 | child_item.data[2] = str(count) | ||
145 | child_item.data[3] = str(time) | ||
146 | child_item.data[4] = PercentToOneDP(time, self.time) | ||
147 | child_item.data[5] = str(branch_count) | ||
148 | child_item.data[6] = PercentToOneDP(branch_count, self.branch_count) | ||
149 | self.child_items.append(child_item) | ||
150 | self.child_count += 1 | ||
151 | |||
152 | def selectCalls(self): | ||
153 | self.query_done = True; | ||
154 | query = QSqlQuery(self.db) | ||
155 | ret = query.exec_('SELECT id, call_path_id, branch_count, call_time, return_time, ' | ||
156 | '( SELECT name FROM symbols WHERE id = ( SELECT symbol_id FROM call_paths WHERE id = call_path_id ) ), ' | ||
157 | '( SELECT short_name FROM dsos WHERE id = ( SELECT dso_id FROM symbols WHERE id = ( SELECT symbol_id FROM call_paths WHERE id = call_path_id ) ) ), ' | ||
158 | '( SELECT ip FROM call_paths where id = call_path_id ) ' | ||
159 | 'FROM calls WHERE parent_call_path_id = ' + str(self.call_path_id) + ' AND comm_id = ' + str(self.comm_id) + ' AND thread_id = ' + str(self.thread_id) + | ||
160 | ' ORDER BY call_path_id') | ||
161 | if not ret: | ||
162 | raise Exception("Query failed: " + query.lastError().text()) | ||
163 | last_call_path_id = 0 | ||
164 | name = "" | ||
165 | dso = "" | ||
166 | count = 0 | ||
167 | branch_count = 0 | ||
168 | total_branch_count = 0 | ||
169 | time = 0 | ||
170 | total_time = 0 | ||
171 | while query.next(): | ||
172 | if query.value(1) == last_call_path_id: | ||
173 | count += 1 | ||
174 | branch_count += query.value(2) | ||
175 | time += query.value(4) - query.value(3) | ||
176 | else: | ||
177 | if count: | ||
178 | self.addChild(last_call_path_id, name, dso, count, time, branch_count) | ||
179 | last_call_path_id = query.value(1) | ||
180 | name = query.value(5) | ||
181 | dso = query.value(6) | ||
182 | count = 1 | ||
183 | total_branch_count += branch_count | ||
184 | total_time += time | ||
185 | branch_count = query.value(2) | ||
186 | time = query.value(4) - query.value(3) | ||
187 | if count: | ||
188 | self.addChild(last_call_path_id, name, dso, count, time, branch_count) | ||
189 | total_branch_count += branch_count | ||
190 | total_time += time | ||
191 | # Top level does not have time or branch count, so fix that here | ||
192 | if total_branch_count > self.branch_count: | ||
193 | self.branch_count = total_branch_count | ||
194 | if self.branch_count: | ||
195 | for child_item in self.child_items: | ||
196 | child_item.data[6] = PercentToOneDP(child_item.branch_count, self.branch_count) | ||
197 | if total_time > self.time: | ||
198 | self.time = total_time | ||
199 | if self.time: | ||
200 | for child_item in self.child_items: | ||
201 | child_item.data[4] = PercentToOneDP(child_item.time, self.time) | ||
202 | |||
203 | def childCount(self): | ||
204 | if not self.query_done: | ||
205 | self.selectCalls() | ||
206 | return self.child_count | ||
207 | |||
208 | def hasChildren(self): | ||
209 | if not self.query_done: | ||
210 | return True | ||
211 | return self.child_count > 0 | ||
212 | |||
213 | def getData(self, column): | ||
214 | return self.data[column] | ||
215 | |||
216 | # Tree data model | 77 | # Tree data model |
217 | 78 | ||
218 | class TreeModel(QAbstractItemModel): | 79 | class TreeModel(QAbstractItemModel): |
@@ -277,12 +138,144 @@ class TreeModel(QAbstractItemModel): | |||
277 | item = index.internalPointer() | 138 | item = index.internalPointer() |
278 | return self.DisplayData(item, index) | 139 | return self.DisplayData(item, index) |
279 | 140 | ||
141 | # Context-sensitive call graph data model item base | ||
142 | |||
143 | class CallGraphLevelItemBase(object): | ||
144 | |||
145 | def __init__(self, glb, row, parent_item): | ||
146 | self.glb = glb | ||
147 | self.row = row | ||
148 | self.parent_item = parent_item | ||
149 | self.query_done = False; | ||
150 | self.child_count = 0 | ||
151 | self.child_items = [] | ||
152 | |||
153 | def getChildItem(self, row): | ||
154 | return self.child_items[row] | ||
155 | |||
156 | def getParentItem(self): | ||
157 | return self.parent_item | ||
158 | |||
159 | def getRow(self): | ||
160 | return self.row | ||
161 | |||
162 | def childCount(self): | ||
163 | if not self.query_done: | ||
164 | self.Select() | ||
165 | if not self.child_count: | ||
166 | return -1 | ||
167 | return self.child_count | ||
168 | |||
169 | def hasChildren(self): | ||
170 | if not self.query_done: | ||
171 | return True | ||
172 | return self.child_count > 0 | ||
173 | |||
174 | def getData(self, column): | ||
175 | return self.data[column] | ||
176 | |||
177 | # Context-sensitive call graph data model level 2+ item base | ||
178 | |||
179 | class CallGraphLevelTwoPlusItemBase(CallGraphLevelItemBase): | ||
180 | |||
181 | def __init__(self, glb, row, comm_id, thread_id, call_path_id, time, branch_count, parent_item): | ||
182 | super(CallGraphLevelTwoPlusItemBase, self).__init__(glb, row, parent_item) | ||
183 | self.comm_id = comm_id | ||
184 | self.thread_id = thread_id | ||
185 | self.call_path_id = call_path_id | ||
186 | self.branch_count = branch_count | ||
187 | self.time = time | ||
188 | |||
189 | def Select(self): | ||
190 | self.query_done = True; | ||
191 | query = QSqlQuery(self.glb.db) | ||
192 | QueryExec(query, "SELECT call_path_id, name, short_name, COUNT(calls.id), SUM(return_time - call_time), SUM(branch_count)" | ||
193 | " FROM calls" | ||
194 | " INNER JOIN call_paths ON calls.call_path_id = call_paths.id" | ||
195 | " INNER JOIN symbols ON call_paths.symbol_id = symbols.id" | ||
196 | " INNER JOIN dsos ON symbols.dso_id = dsos.id" | ||
197 | " WHERE parent_call_path_id = " + str(self.call_path_id) + | ||
198 | " AND comm_id = " + str(self.comm_id) + | ||
199 | " AND thread_id = " + str(self.thread_id) + | ||
200 | " GROUP BY call_path_id, name, short_name" | ||
201 | " ORDER BY call_path_id") | ||
202 | while query.next(): | ||
203 | child_item = CallGraphLevelThreeItem(self.glb, self.child_count, self.comm_id, self.thread_id, query.value(0), query.value(1), query.value(2), query.value(3), int(query.value(4)), int(query.value(5)), self) | ||
204 | self.child_items.append(child_item) | ||
205 | self.child_count += 1 | ||
206 | |||
207 | # Context-sensitive call graph data model level three item | ||
208 | |||
209 | class CallGraphLevelThreeItem(CallGraphLevelTwoPlusItemBase): | ||
210 | |||
211 | def __init__(self, glb, row, comm_id, thread_id, call_path_id, name, dso, count, time, branch_count, parent_item): | ||
212 | super(CallGraphLevelThreeItem, self).__init__(glb, row, comm_id, thread_id, call_path_id, time, branch_count, parent_item) | ||
213 | dso = dsoname(dso) | ||
214 | self.data = [ name, dso, str(count), str(time), PercentToOneDP(time, parent_item.time), str(branch_count), PercentToOneDP(branch_count, parent_item.branch_count) ] | ||
215 | self.dbid = call_path_id | ||
216 | |||
217 | # Context-sensitive call graph data model level two item | ||
218 | |||
219 | class CallGraphLevelTwoItem(CallGraphLevelTwoPlusItemBase): | ||
220 | |||
221 | def __init__(self, glb, row, comm_id, thread_id, pid, tid, parent_item): | ||
222 | super(CallGraphLevelTwoItem, self).__init__(glb, row, comm_id, thread_id, 1, 0, 0, parent_item) | ||
223 | self.data = [str(pid) + ":" + str(tid), "", "", "", "", "", ""] | ||
224 | self.dbid = thread_id | ||
225 | |||
226 | def Select(self): | ||
227 | super(CallGraphLevelTwoItem, self).Select() | ||
228 | for child_item in self.child_items: | ||
229 | self.time += child_item.time | ||
230 | self.branch_count += child_item.branch_count | ||
231 | for child_item in self.child_items: | ||
232 | child_item.data[4] = PercentToOneDP(child_item.time, self.time) | ||
233 | child_item.data[6] = PercentToOneDP(child_item.branch_count, self.branch_count) | ||
234 | |||
235 | # Context-sensitive call graph data model level one item | ||
236 | |||
237 | class CallGraphLevelOneItem(CallGraphLevelItemBase): | ||
238 | |||
239 | def __init__(self, glb, row, comm_id, comm, parent_item): | ||
240 | super(CallGraphLevelOneItem, self).__init__(glb, row, parent_item) | ||
241 | self.data = [comm, "", "", "", "", "", ""] | ||
242 | self.dbid = comm_id | ||
243 | |||
244 | def Select(self): | ||
245 | self.query_done = True; | ||
246 | query = QSqlQuery(self.glb.db) | ||
247 | QueryExec(query, "SELECT thread_id, pid, tid" | ||
248 | " FROM comm_threads" | ||
249 | " INNER JOIN threads ON thread_id = threads.id" | ||
250 | " WHERE comm_id = " + str(self.dbid)) | ||
251 | while query.next(): | ||
252 | child_item = CallGraphLevelTwoItem(self.glb, self.child_count, self.dbid, query.value(0), query.value(1), query.value(2), self) | ||
253 | self.child_items.append(child_item) | ||
254 | self.child_count += 1 | ||
255 | |||
256 | # Context-sensitive call graph data model root item | ||
257 | |||
258 | class CallGraphRootItem(CallGraphLevelItemBase): | ||
259 | |||
260 | def __init__(self, glb): | ||
261 | super(CallGraphRootItem, self).__init__(glb, 0, None) | ||
262 | self.dbid = 0 | ||
263 | self.query_done = True; | ||
264 | query = QSqlQuery(glb.db) | ||
265 | QueryExec(query, "SELECT id, comm FROM comms") | ||
266 | while query.next(): | ||
267 | if not query.value(0): | ||
268 | continue | ||
269 | child_item = CallGraphLevelOneItem(glb, self.child_count, query.value(0), query.value(1), self) | ||
270 | self.child_items.append(child_item) | ||
271 | self.child_count += 1 | ||
272 | |||
280 | # Context-sensitive call graph data model | 273 | # Context-sensitive call graph data model |
281 | 274 | ||
282 | class CallGraphModel(TreeModel): | 275 | class CallGraphModel(TreeModel): |
283 | 276 | ||
284 | def __init__(self, glb, parent=None): | 277 | def __init__(self, glb, parent=None): |
285 | super(CallGraphModel, self).__init__(TreeItem(glb.db, 0, None), parent) | 278 | super(CallGraphModel, self).__init__(CallGraphRootItem(glb), parent) |
286 | self.glb = glb | 279 | self.glb = glb |
287 | 280 | ||
288 | def columnCount(self, parent=None): | 281 | def columnCount(self, parent=None): |