Add helper functions for a few common cases.
Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all
Unified · Split
| @@ -52,6 +52,28 @@ from PySide.QtGui import * · 51 unmodified lines | ||
| 52 | 52 | from PySide.QtSql import * |
| 53 | 53 | from decimal import * |
| 54 | 54 | |
| 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 | + | |
| 55 | 77 | class TreeItem(): |
| 56 | 78 | |
| 57 | 79 | def __init__(self, db, row, parent_item): |
| @@ -73,9 +95,7 @@ class TreeItem(): · 15 unmodified lines | ||
| 73 | 95 | def setUpRoot(self): |
| 74 | 96 | self.query_done = True |
| 75 | 97 | 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') | |
| 79 | 99 | while query.next(): |
| 80 | 100 | if not query.value(0): |
| 81 | 101 | continue |
| @@ -91,9 +111,7 @@ class TreeItem(): · 9 unmodified lines | ||
| 91 | 111 | self.child_items = [] |
| 92 | 112 | self.child_count = 0 |
| 93 | 113 | 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)) | |
| 97 | 115 | while query.next(): |
| 98 | 116 | child_item = TreeItem(self.db, self.child_count, self) |
| 99 | 117 | self.child_items.append(child_item) |
| @@ -114,18 +132,6 @@ class TreeItem(): · 14 unmodified lines | ||
| 114 | 132 | def getRow(self): |
| 115 | 133 | return self.row |
| 116 | 134 | |
| 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 | − | |
| 129 | 135 | def addChild(self, call_path_id, name, dso, count, time, branch_count): |
| 130 | 136 | child_item = TreeItem(self.db, self.child_count, self) |
| 131 | 137 | child_item.comm_id = self.comm_id |
| @@ -134,14 +140,12 @@ class TreeItem(): · 2 unmodified lines | ||
| 134 | 140 | child_item.branch_count = branch_count |
| 135 | 141 | child_item.time = time |
| 136 | 142 | 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) | |
| 140 | 144 | child_item.data[2] = str(count) |
| 141 | 145 | child_item.data[3] = str(time) |
| 142 | − child_item.data[4] = self.timePercent(time) | |
| 146 | + child_item.data[4] = PercentToOneDP(time, self.time) | |
| 143 | 147 | 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) | |
| 145 | 149 | self.child_items.append(child_item) |
| 146 | 150 | self.child_count += 1 |
| 147 | 151 | |
| @@ -189,12 +193,12 @@ class TreeItem(): · 41 unmodified lines | ||
| 189 | 193 | self.branch_count = total_branch_count |
| 190 | 194 | if self.branch_count: |
| 191 | 195 | 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) | |
| 193 | 197 | if total_time > self.time: |
| 194 | 198 | self.time = total_time |
| 195 | 199 | if self.time: |
| 196 | 200 | 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) | |
| 198 | 202 | |
| 199 | 203 | def childCount(self): |
| 200 | 204 | if not self.query_done: |