Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Add more documentation to export-to-postgresql.py script

Add some comments to the script and some 'views' to the created database
that better illustrate the database structure and how it can be used.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/1443186956-18718-8-git-send-email-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Adrian Hunter and committed by
Arnaldo Carvalho de Melo
35ca01c1 a38f48e3

+221
+221
tools/perf/scripts/python/export-to-postgresql.py
··· 61 61 # 62 62 # An example of using the database is provided by the script 63 63 # call-graph-from-postgresql.py. Refer to that script for details. 64 + # 65 + # Tables: 66 + # 67 + # The tables largely correspond to perf tools' data structures. They are largely self-explanatory. 68 + # 69 + # samples 70 + # 71 + # 'samples' is the main table. It represents what instruction was executing at a point in time 72 + # when something (a selected event) happened. The memory address is the instruction pointer or 'ip'. 73 + # 74 + # calls 75 + # 76 + # 'calls' represents function calls and is related to 'samples' by 'call_id' and 'return_id'. 77 + # 'calls' is only created when the 'calls' option to this script is specified. 78 + # 79 + # call_paths 80 + # 81 + # 'call_paths' represents all the call stacks. Each 'call' has an associated record in 'call_paths'. 82 + # 'calls_paths' is only created when the 'calls' option to this script is specified. 83 + # 84 + # branch_types 85 + # 86 + # 'branch_types' provides descriptions for each type of branch. 87 + # 88 + # comm_threads 89 + # 90 + # 'comm_threads' shows how 'comms' relates to 'threads'. 91 + # 92 + # comms 93 + # 94 + # 'comms' contains a record for each 'comm' - the name given to the executable that is running. 95 + # 96 + # dsos 97 + # 98 + # 'dsos' contains a record for each executable file or library. 99 + # 100 + # machines 101 + # 102 + # 'machines' can be used to distinguish virtual machines if virtualization is supported. 103 + # 104 + # selected_events 105 + # 106 + # 'selected_events' contains a record for each kind of event that has been sampled. 107 + # 108 + # symbols 109 + # 110 + # 'symbols' contains a record for each symbol. Only symbols that have samples are present. 111 + # 112 + # threads 113 + # 114 + # 'threads' contains a record for each thread. 115 + # 116 + # Views: 117 + # 118 + # Most of the tables have views for more friendly display. The views are: 119 + # 120 + # calls_view 121 + # call_paths_view 122 + # comm_threads_view 123 + # dsos_view 124 + # machines_view 125 + # samples_view 126 + # symbols_view 127 + # threads_view 128 + # 129 + # More examples of browsing the database with psql: 130 + # Note that some of the examples are not the most optimal SQL query. 131 + # Note that call information is only available if the script's 'calls' option has been used. 132 + # 133 + # Top 10 function calls (not aggregated by symbol): 134 + # 135 + # SELECT * FROM calls_view ORDER BY elapsed_time DESC LIMIT 10; 136 + # 137 + # Top 10 function calls (aggregated by symbol): 138 + # 139 + # SELECT symbol_id,(SELECT name FROM symbols WHERE id = symbol_id) AS symbol, 140 + # SUM(elapsed_time) AS tot_elapsed_time,SUM(branch_count) AS tot_branch_count 141 + # FROM calls_view GROUP BY symbol_id ORDER BY tot_elapsed_time DESC LIMIT 10; 142 + # 143 + # Note that the branch count gives a rough estimation of cpu usage, so functions 144 + # that took a long time but have a relatively low branch count must have spent time 145 + # waiting. 146 + # 147 + # Find symbols by pattern matching on part of the name (e.g. names containing 'alloc'): 148 + # 149 + # SELECT * FROM symbols_view WHERE name LIKE '%alloc%'; 150 + # 151 + # Top 10 function calls for a specific symbol (e.g. whose symbol_id is 187): 152 + # 153 + # SELECT * FROM calls_view WHERE symbol_id = 187 ORDER BY elapsed_time DESC LIMIT 10; 154 + # 155 + # Show function calls made by function in the same context (i.e. same call path) (e.g. one with call_path_id 254): 156 + # 157 + # SELECT * FROM calls_view WHERE parent_call_path_id = 254; 158 + # 159 + # Show branches made during a function call (e.g. where call_id is 29357 and return_id is 29370 and tid is 29670) 160 + # 161 + # SELECT * FROM samples_view WHERE id >= 29357 AND id <= 29370 AND tid = 29670 AND event LIKE 'branches%'; 162 + # 163 + # Show transactions: 164 + # 165 + # SELECT * FROM samples_view WHERE event = 'transactions'; 166 + # 167 + # Note transaction start has 'in_tx' true whereas, transaction end has 'in_tx' false. 168 + # Transaction aborts have branch_type_name 'transaction abort' 169 + # 170 + # Show transaction aborts: 171 + # 172 + # SELECT * FROM samples_view WHERE event = 'transactions' AND branch_type_name = 'transaction abort'; 173 + # 174 + # To print a call stack requires walking the call_paths table. For example this python script: 175 + # #!/usr/bin/python2 176 + # 177 + # import sys 178 + # from PySide.QtSql import * 179 + # 180 + # if __name__ == '__main__': 181 + # if (len(sys.argv) < 3): 182 + # print >> sys.stderr, "Usage is: printcallstack.py <database name> <call_path_id>" 183 + # raise Exception("Too few arguments") 184 + # dbname = sys.argv[1] 185 + # call_path_id = sys.argv[2] 186 + # db = QSqlDatabase.addDatabase('QPSQL') 187 + # db.setDatabaseName(dbname) 188 + # if not db.open(): 189 + # raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text()) 190 + # query = QSqlQuery(db) 191 + # print " id ip symbol_id symbol dso_id dso_short_name" 192 + # while call_path_id != 0 and call_path_id != 1: 193 + # ret = query.exec_('SELECT * FROM call_paths_view WHERE id = ' + str(call_path_id)) 194 + # if not ret: 195 + # raise Exception("Query failed: " + query.lastError().text()) 196 + # if not query.next(): 197 + # raise Exception("Query failed") 198 + # print "{0:>6} {1:>10} {2:>9} {3:<30} {4:>6} {5:<30}".format(query.value(0), query.value(1), query.value(2), query.value(3), query.value(4), query.value(5)) 199 + # call_path_id = query.value(6) 64 200 65 201 from PySide.QtSql import * 66 202 ··· 379 243 'return_id bigint,' 380 244 'parent_call_path_id bigint,' 381 245 'flags integer)') 246 + 247 + do_query(query, 'CREATE VIEW machines_view AS ' 248 + 'SELECT ' 249 + 'id,' 250 + 'pid,' 251 + 'root_dir,' 252 + 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest' 253 + ' FROM machines') 254 + 255 + do_query(query, 'CREATE VIEW dsos_view AS ' 256 + 'SELECT ' 257 + 'id,' 258 + 'machine_id,' 259 + '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,' 260 + 'short_name,' 261 + 'long_name,' 262 + 'build_id' 263 + ' FROM dsos') 264 + 265 + do_query(query, 'CREATE VIEW symbols_view AS ' 266 + 'SELECT ' 267 + 'id,' 268 + 'name,' 269 + '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,' 270 + 'dso_id,' 271 + 'sym_start,' 272 + 'sym_end,' 273 + 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding' 274 + ' FROM symbols') 275 + 276 + do_query(query, 'CREATE VIEW threads_view AS ' 277 + 'SELECT ' 278 + 'id,' 279 + 'machine_id,' 280 + '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,' 281 + 'process_id,' 282 + 'pid,' 283 + 'tid' 284 + ' FROM threads') 285 + 286 + do_query(query, 'CREATE VIEW comm_threads_view AS ' 287 + 'SELECT ' 288 + 'comm_id,' 289 + '(SELECT comm FROM comms WHERE id = comm_id) AS command,' 290 + 'thread_id,' 291 + '(SELECT pid FROM threads WHERE id = thread_id) AS pid,' 292 + '(SELECT tid FROM threads WHERE id = thread_id) AS tid' 293 + ' FROM comm_threads') 294 + 295 + if perf_db_export_calls: 296 + do_query(query, 'CREATE VIEW call_paths_view AS ' 297 + 'SELECT ' 298 + 'c.id,' 299 + 'to_hex(c.ip) AS ip,' 300 + 'c.symbol_id,' 301 + '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,' 302 + '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,' 303 + '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,' 304 + 'c.parent_id,' 305 + 'to_hex(p.ip) AS parent_ip,' 306 + 'p.symbol_id AS parent_symbol_id,' 307 + '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,' 308 + '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,' 309 + '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name' 310 + ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id') 311 + do_query(query, 'CREATE VIEW calls_view AS ' 312 + 'SELECT ' 313 + 'calls.id,' 314 + 'thread_id,' 315 + '(SELECT pid FROM threads WHERE id = thread_id) AS pid,' 316 + '(SELECT tid FROM threads WHERE id = thread_id) AS tid,' 317 + '(SELECT comm FROM comms WHERE id = comm_id) AS command,' 318 + 'call_path_id,' 319 + 'to_hex(ip) AS ip,' 320 + 'symbol_id,' 321 + '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,' 322 + 'call_time,' 323 + 'return_time,' 324 + 'return_time - call_time AS elapsed_time,' 325 + 'branch_count,' 326 + 'call_id,' 327 + 'return_id,' 328 + 'CASE WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' ELSE \'\' END AS flags,' 329 + 'parent_call_path_id' 330 + ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id') 382 331 383 332 do_query(query, 'CREATE VIEW samples_view AS ' 384 333 'SELECT '