History

perf scripts python: call-graph-from-sql.py: Add data helper functions

Add helper functions for a few common cases.

Adrian Hunter committed 8 years ago

Commit 4be9ace1 parent 70d831e1 file · +29 −25Browse files

Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all

Unified · Split

tools/perf/scripts/python/call-graph-from-sql.py+29−25
@@ -52,6 +52,28 @@ from PySide.QtGui import * · 51 unmodified lines
5252 from PySide.QtSql import *
5353 from decimal import *
5454
55+# Data formatting helpers
56+
57+def dsoname(name):
58+ if name == "[kernel.kallsyms]":
59+ return "[kernel]"
60+ return name
61+
62+# Percent to one decimal place
63+
64+def PercentToOneDP(n, d):
65+ if not d:
66+ return "0.0"
67+ x = (n * Decimal(100)) / d
68+ return str(x.quantize(Decimal(".1"), rounding=ROUND_HALF_UP))
69+
70+# Helper for queries that must not fail
71+
72+def QueryExec(query, stmt):
73+ ret = query.exec_(stmt)
74+ if not ret:
75+ raise Exception("Query failed: " + query.lastError().text())
76+
5577 class TreeItem():
5678
5779 def __init__(self, db, row, parent_item):
@@ -73,9 +95,7 @@ class TreeItem(): · 15 unmodified lines
7395 def setUpRoot(self):
7496 self.query_done = True
7597 query = QSqlQuery(self.db)
76− ret = query.exec_('SELECT id, comm FROM comms')
77− if not ret:
78− raise Exception("Query failed: " + query.lastError().text())
98+ QueryExec(query, 'SELECT id, comm FROM comms')
7999 while query.next():
80100 if not query.value(0):
81101 continue
@@ -91,9 +111,7 @@ class TreeItem(): · 9 unmodified lines
91111 self.child_items = []
92112 self.child_count = 0
93113 query = QSqlQuery(self.db)
94− ret = query.exec_('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))
95− if not ret:
96− raise Exception("Query failed: " + query.lastError().text())
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))
97115 while query.next():
98116 child_item = TreeItem(self.db, self.child_count, self)
99117 self.child_items.append(child_item)
@@ -114,18 +132,6 @@ class TreeItem(): · 14 unmodified lines
114132 def getRow(self):
115133 return self.row
116134
117− def timePercent(self, b):
118− if not self.time:
119− return "0.0"
120− x = (b * Decimal(100)) / self.time
121− return str(x.quantize(Decimal('.1'), rounding=ROUND_HALF_UP))
122−
123− def branchPercent(self, b):
124− if not self.branch_count:
125− return "0.0"
126− x = (b * Decimal(100)) / self.branch_count
127− return str(x.quantize(Decimal('.1'), rounding=ROUND_HALF_UP))
128−
129135 def addChild(self, call_path_id, name, dso, count, time, branch_count):
130136 child_item = TreeItem(self.db, self.child_count, self)
131137 child_item.comm_id = self.comm_id
@@ -134,14 +140,12 @@ class TreeItem(): · 2 unmodified lines
134140 child_item.branch_count = branch_count
135141 child_item.time = time
136142 child_item.data[0] = name
137− if dso == "[kernel.kallsyms]":
138− dso = "[kernel]"
139− child_item.data[1] = dso
143+ child_item.data[1] = dsoname(dso)
140144 child_item.data[2] = str(count)
141145 child_item.data[3] = str(time)
142− child_item.data[4] = self.timePercent(time)
146+ child_item.data[4] = PercentToOneDP(time, self.time)
143147 child_item.data[5] = str(branch_count)
144− child_item.data[6] = self.branchPercent(branch_count)
148+ child_item.data[6] = PercentToOneDP(branch_count, self.branch_count)
145149 self.child_items.append(child_item)
146150 self.child_count += 1
147151
@@ -189,12 +193,12 @@ class TreeItem(): · 41 unmodified lines
189193 self.branch_count = total_branch_count
190194 if self.branch_count:
191195 for child_item in self.child_items:
192− child_item.data[6] = self.branchPercent(child_item.branch_count)
196+ child_item.data[6] = PercentToOneDP(child_item.branch_count, self.branch_count)
193197 if total_time > self.time:
194198 self.time = total_time
195199 if self.time:
196200 for child_item in self.child_items:
197− child_item.data[4] = self.timePercent(child_item.time)
201+ child_item.data[4] = PercentToOneDP(child_item.time, self.time)
198202
199203 def childCount(self):
200204 if not self.query_done: