diff options
Diffstat (limited to 'tools/perf/scripts/python/export-to-postgresql.py')
| -rw-r--r-- | tools/perf/scripts/python/export-to-postgresql.py | 444 |
1 files changed, 444 insertions, 0 deletions
diff --git a/tools/perf/scripts/python/export-to-postgresql.py b/tools/perf/scripts/python/export-to-postgresql.py new file mode 100644 index 000000000000..4cdafd880074 --- /dev/null +++ b/tools/perf/scripts/python/export-to-postgresql.py | |||
| @@ -0,0 +1,444 @@ | |||
| 1 | # export-to-postgresql.py: export perf data to a postgresql database | ||
| 2 | # Copyright (c) 2014, Intel Corporation. | ||
| 3 | # | ||
| 4 | # This program is free software; you can redistribute it and/or modify it | ||
| 5 | # under the terms and conditions of the GNU General Public License, | ||
| 6 | # version 2, as published by the Free Software Foundation. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope it will be useful, but WITHOUT | ||
| 9 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 10 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 11 | # more details. | ||
| 12 | |||
| 13 | import os | ||
| 14 | import sys | ||
| 15 | import struct | ||
| 16 | import datetime | ||
| 17 | |||
| 18 | from PySide.QtSql import * | ||
| 19 | |||
| 20 | # Need to access PostgreSQL C library directly to use COPY FROM STDIN | ||
| 21 | from ctypes import * | ||
| 22 | libpq = CDLL("libpq.so.5") | ||
| 23 | PQconnectdb = libpq.PQconnectdb | ||
| 24 | PQconnectdb.restype = c_void_p | ||
| 25 | PQfinish = libpq.PQfinish | ||
| 26 | PQstatus = libpq.PQstatus | ||
| 27 | PQexec = libpq.PQexec | ||
| 28 | PQexec.restype = c_void_p | ||
| 29 | PQresultStatus = libpq.PQresultStatus | ||
| 30 | PQputCopyData = libpq.PQputCopyData | ||
| 31 | PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ] | ||
| 32 | PQputCopyEnd = libpq.PQputCopyEnd | ||
| 33 | PQputCopyEnd.argtypes = [ c_void_p, c_void_p ] | ||
| 34 | |||
| 35 | sys.path.append(os.environ['PERF_EXEC_PATH'] + \ | ||
| 36 | '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') | ||
| 37 | |||
| 38 | # These perf imports are not used at present | ||
| 39 | #from perf_trace_context import * | ||
| 40 | #from Core import * | ||
| 41 | |||
| 42 | perf_db_export_mode = True | ||
| 43 | perf_db_export_calls = False | ||
| 44 | |||
| 45 | def usage(): | ||
| 46 | print >> sys.stderr, "Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>]" | ||
| 47 | print >> sys.stderr, "where: columns 'all' or 'branches'" | ||
| 48 | print >> sys.stderr, " calls 'calls' => create calls table" | ||
| 49 | raise Exception("Too few arguments") | ||
| 50 | |||
| 51 | if (len(sys.argv) < 2): | ||
| 52 | usage() | ||
| 53 | |||
| 54 | dbname = sys.argv[1] | ||
| 55 | |||
| 56 | if (len(sys.argv) >= 3): | ||
| 57 | columns = sys.argv[2] | ||
| 58 | else: | ||
| 59 | columns = "all" | ||
| 60 | |||
| 61 | if columns not in ("all", "branches"): | ||
| 62 | usage() | ||
| 63 | |||
| 64 | branches = (columns == "branches") | ||
| 65 | |||
| 66 | if (len(sys.argv) >= 4): | ||
| 67 | if (sys.argv[3] == "calls"): | ||
| 68 | perf_db_export_calls = True | ||
| 69 | else: | ||
| 70 | usage() | ||
| 71 | |||
| 72 | output_dir_name = os.getcwd() + "/" + dbname + "-perf-data" | ||
| 73 | os.mkdir(output_dir_name) | ||
| 74 | |||
| 75 | def do_query(q, s): | ||
| 76 | if (q.exec_(s)): | ||
| 77 | return | ||
| 78 | raise Exception("Query failed: " + q.lastError().text()) | ||
| 79 | |||
| 80 | print datetime.datetime.today(), "Creating database..." | ||
| 81 | |||
| 82 | db = QSqlDatabase.addDatabase('QPSQL') | ||
| 83 | query = QSqlQuery(db) | ||
| 84 | db.setDatabaseName('postgres') | ||
| 85 | db.open() | ||
| 86 | try: | ||
| 87 | do_query(query, 'CREATE DATABASE ' + dbname) | ||
| 88 | except: | ||
| 89 | os.rmdir(output_dir_name) | ||
| 90 | raise | ||
| 91 | query.finish() | ||
| 92 | query.clear() | ||
| 93 | db.close() | ||
| 94 | |||
| 95 | db.setDatabaseName(dbname) | ||
| 96 | db.open() | ||
| 97 | |||
| 98 | query = QSqlQuery(db) | ||
| 99 | do_query(query, 'SET client_min_messages TO WARNING') | ||
| 100 | |||
| 101 | do_query(query, 'CREATE TABLE selected_events (' | ||
| 102 | 'id bigint NOT NULL,' | ||
| 103 | 'name varchar(80))') | ||
| 104 | do_query(query, 'CREATE TABLE machines (' | ||
| 105 | 'id bigint NOT NULL,' | ||
| 106 | 'pid integer,' | ||
| 107 | 'root_dir varchar(4096))') | ||
| 108 | do_query(query, 'CREATE TABLE threads (' | ||
| 109 | 'id bigint NOT NULL,' | ||
| 110 | 'machine_id bigint,' | ||
| 111 | 'process_id bigint,' | ||
| 112 | 'pid integer,' | ||
| 113 | 'tid integer)') | ||
| 114 | do_query(query, 'CREATE TABLE comms (' | ||
| 115 | 'id bigint NOT NULL,' | ||
| 116 | 'comm varchar(16))') | ||
| 117 | do_query(query, 'CREATE TABLE comm_threads (' | ||
| 118 | 'id bigint NOT NULL,' | ||
| 119 | 'comm_id bigint,' | ||
| 120 | 'thread_id bigint)') | ||
| 121 | do_query(query, 'CREATE TABLE dsos (' | ||
| 122 | 'id bigint NOT NULL,' | ||
| 123 | 'machine_id bigint,' | ||
| 124 | 'short_name varchar(256),' | ||
| 125 | 'long_name varchar(4096),' | ||
| 126 | 'build_id varchar(64))') | ||
| 127 | do_query(query, 'CREATE TABLE symbols (' | ||
| 128 | 'id bigint NOT NULL,' | ||
| 129 | 'dso_id bigint,' | ||
| 130 | 'sym_start bigint,' | ||
| 131 | 'sym_end bigint,' | ||
| 132 | 'binding integer,' | ||
| 133 | 'name varchar(2048))') | ||
| 134 | do_query(query, 'CREATE TABLE branch_types (' | ||
| 135 | 'id integer NOT NULL,' | ||
| 136 | 'name varchar(80))') | ||
| 137 | |||
| 138 | if branches: | ||
| 139 | do_query(query, 'CREATE TABLE samples (' | ||
| 140 | 'id bigint NOT NULL,' | ||
| 141 | 'evsel_id bigint,' | ||
| 142 | 'machine_id bigint,' | ||
| 143 | 'thread_id bigint,' | ||
| 144 | 'comm_id bigint,' | ||
| 145 | 'dso_id bigint,' | ||
| 146 | 'symbol_id bigint,' | ||
| 147 | 'sym_offset bigint,' | ||
| 148 | 'ip bigint,' | ||
| 149 | 'time bigint,' | ||
| 150 | 'cpu integer,' | ||
| 151 | 'to_dso_id bigint,' | ||
| 152 | 'to_symbol_id bigint,' | ||
| 153 | 'to_sym_offset bigint,' | ||
| 154 | 'to_ip bigint,' | ||
| 155 | 'branch_type integer,' | ||
| 156 | 'in_tx boolean)') | ||
| 157 | else: | ||
| 158 | do_query(query, 'CREATE TABLE samples (' | ||
| 159 | 'id bigint NOT NULL,' | ||
| 160 | 'evsel_id bigint,' | ||
| 161 | 'machine_id bigint,' | ||
| 162 | 'thread_id bigint,' | ||
| 163 | 'comm_id bigint,' | ||
| 164 | 'dso_id bigint,' | ||
| 165 | 'symbol_id bigint,' | ||
| 166 | 'sym_offset bigint,' | ||
| 167 | 'ip bigint,' | ||
| 168 | 'time bigint,' | ||
| 169 | 'cpu integer,' | ||
| 170 | 'to_dso_id bigint,' | ||
| 171 | 'to_symbol_id bigint,' | ||
| 172 | 'to_sym_offset bigint,' | ||
| 173 | 'to_ip bigint,' | ||
| 174 | 'period bigint,' | ||
| 175 | 'weight bigint,' | ||
| 176 | 'transaction bigint,' | ||
| 177 | 'data_src bigint,' | ||
| 178 | 'branch_type integer,' | ||
| 179 | 'in_tx boolean)') | ||
| 180 | |||
| 181 | if perf_db_export_calls: | ||
| 182 | do_query(query, 'CREATE TABLE call_paths (' | ||
| 183 | 'id bigint NOT NULL,' | ||
| 184 | 'parent_id bigint,' | ||
| 185 | 'symbol_id bigint,' | ||
| 186 | 'ip bigint)') | ||
| 187 | do_query(query, 'CREATE TABLE calls (' | ||
| 188 | 'id bigint NOT NULL,' | ||
| 189 | 'thread_id bigint,' | ||
| 190 | 'comm_id bigint,' | ||
| 191 | 'call_path_id bigint,' | ||
| 192 | 'call_time bigint,' | ||
| 193 | 'return_time bigint,' | ||
| 194 | 'branch_count bigint,' | ||
| 195 | 'call_id bigint,' | ||
