aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/scripts/python/export-to-postgresql.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/scripts/python/export-to-postgresql.py')
-rw-r--r--tools/perf/scripts/python/export-to-postgresql.py444
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
13import os
14import sys
15import struct
16import datetime
17
18from PySide.QtSql import *
19
20# Need to access PostgreSQL C library directly to use COPY FROM STDIN
21from ctypes import *
22libpq = CDLL("libpq.so.5")
23PQconnectdb = libpq.PQconnectdb
24PQconnectdb.restype = c_void_p
25PQfinish = libpq.PQfinish
26PQstatus = libpq.PQstatus
27PQexec = libpq.PQexec
28PQexec.restype = c_void_p
29PQresultStatus = libpq.PQresultStatus
30PQputCopyData = libpq.PQputCopyData
31PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ]
32PQputCopyEnd = libpq.PQputCopyEnd
33PQputCopyEnd.argtypes = [ c_void_p, c_void_p ]
34
35sys.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
42perf_db_export_mode = True
43perf_db_export_calls = False
44
45def 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
51if (len(sys.argv) < 2):
52 usage()
53
54dbname = sys.argv[1]
55
56if (len(sys.argv) >= 3):
57 columns = sys.argv[2]
58else:
59 columns = "all"
60
61if columns not in ("all", "branches"):
62 usage()
63
64branches = (columns == "branches")
65
66if (len(sys.argv) >= 4):
67 if (sys.argv[3] == "calls"):
68 perf_db_export_calls = True
69 else:
70 usage()
71
72output_dir_name = os.getcwd() + "/" + dbname + "-perf-data"
73os.mkdir(output_dir_name)
74
75def do_query(q, s):
76 if (q.exec_(s)):
77 return
78 raise Exception("Query failed: " + q.lastError().text())
79
80print datetime.datetime.today(), "Creating database..."
81
82db = QSqlDatabase.addDatabase('QPSQL')
83query = QSqlQuery(db)
84db.setDatabaseName('postgres')
85db.open()
86try:
87 do_query(query, 'CREATE DATABASE ' + dbname)
88except:
89 os.rmdir(output_dir_name)
90 raise
91query.finish()
92query.clear()
93db.close()
94
95db.setDatabaseName(dbname)
96db.open()
97
98query = QSqlQuery(db)
99do_query(query, 'SET client_min_messages TO WARNING')
100
101do_query(query, 'CREATE TABLE selected_events ('
102 'id bigint NOT NULL,'
103 'name varchar(80))')
104do_query(query, 'CREATE TABLE machines ('
105 'id bigint NOT NULL,'
106 'pid integer,'
107 'root_dir varchar(4096))')
108do_query(query, 'CREATE TABLE threads ('
109 'id bigint NOT NULL,'
110 'machine_id bigint,'
111 'process_id bigint,'
112 'pid integer,'
113 'tid integer)')
114do_query(query, 'CREATE TABLE comms ('
115 'id bigint NOT NULL,'
116 'comm varchar(16))')
117do_query(query, 'CREATE TABLE comm_threads ('
118 'id bigint NOT NULL,'
119 'comm_id bigint,'
120 'thread_id bigint)')
121do_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))')
127do_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))')
134do_query(query, 'CREATE TABLE branch_types ('
135 'id integer NOT NULL,'
136 'name varchar(80))')
137
138if 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)')
157else:
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
181if 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,'