]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/database.py
58fdf9ecf0b525bb1d0e898382a6326287f7797a
[linux-conf-perf.git] / scripts / database.py
1 import os
2 import datetime
3 import postgresql
4 import collections
5
6 import utils
7 import exceptions
8 from conf import conf
9 from conf import sf
10
11 def __git_describe__():
12         return utils.callsubprocess('git_describe',
13                         conf.git_describe_cmd, False, True)[0]
14
15 def __git_commit__():
16         return utils.callsubprocess('git_rev_parse',
17                         conf.git_commit_cmd, False, True)[0]
18
19 def __timestamp__():
20         return datetime.datetime.now().strftime('%y-%m-%d-%H-%M-%S')
21
22 Config = collections.namedtuple('Config', 'id hash config') # Named tuple for configuration
23 Measure = collections.namedtuple('Measure', 'id conf_id output value') # Named tuple for measurement
24
25 class database:
26         "Class used for accessing PostgreSQL project database."
27         def __init__(self):
28                 self.db = postgresql.open(database = conf.db_database,
29                                                                   unix='/var/run/postgresql/.s.PGSQL.5432',
30                                                                   password='')
31                 # check if tables are present
32                 tables = ('toolsgit', 'configurations', 'measure')
33                 for tab in tables:
34                         val = self.db.prepare("""SELECT COUNT(*) FROM pg_class
35                                                            WHERE relname = $1""")(tab)[0][0]
36                         if val < 1:
37                                 raise exceptions.DatabaseUninitialized()
38
39         def check_toolsgit(self):
40                 "Return id of toolsgit row. If missing, it is inserted"
41                 ds = __git_describe__()
42                 cm = __git_commit__()
43                 ps = self.db.prepare("""SELECT id FROM toolsgit
44                                                           WHERE git_describe = $1 AND git_commit = $2
45                                                           """)
46                 id = ps(ds, cm)
47                 if id:
48                         return id[0][0]
49                 ps = self.db.prepare("""INSERT INTO toolsgit
50                                                    (git_describe, git_commit)
51                                                    VALUES
52                                                    ($1, $2);
53                                                    """)
54                 ps(ds, cm)
55                 return self.check_toolsgit()
56
57         def check_linuxgit(self):
58                 "Return id of linuxgit row. If missing, it is inserted."
59                 wd = os.getcwd()
60                 os.chdir(sf(conf.linux_sources))
61                 ds = __git_describe__()
62                 cm = __git_commit__()
63                 os.chdir(wd)
64                 ps = self.db.prepare("""SELECT id FROM linuxgit
65                                                           WHERE git_describe = $1 AND git_commit = $2
66                                                           """)
67                 id = ps(ds, cm)
68                 if id:
69                         return id[0][0]
70                 ps = self.db.prepare("""INSERT INTO linuxgit
71                                                    (git_describe, git_commit)
72                                                    VALUES
73                                                    ($1, $2);
74                                                    """)
75                 ps(ds, cm)
76                 return self.check_linuxgit()
77
78         def add_configuration(self, hash, txtconfig, generator):
79                 "Add configuration to database."
80                 ps = self.db.prepare("""INSERT INTO configurations
81                                                                 (hash, config, gtime, toolgit, linuxgit, generator)
82                                                                 VALUES
83                                                                 ($1, $2, $3, $4, $5, $6);
84                                                                 """)
85                 gt = self.check_toolsgit()
86                 lgt = self.check_linuxgit()
87                 tm = datetime.datetime.now()
88                 ps(hash, '\n'.join(txtconfig), tm, gt, lgt, generator)
89
90         def get_configration(self, hash):
91                 "Return configration id for inserted hash."
92                 ps = self.db.prepare("""SELECT id, config FROM configurations
93                                                                 WHERE hash = $1""")
94                 rtn = []
95                 for dt in ps(hash):
96                         rtn.append(Config(dt[0], hash, dt[1].split('\n')))
97                 return rtn
98
99         def add_measure(self, output, result, conf_id, value = None):
100                 "Add measurement."
101                 ps = self.db.prepare("""INSERT INTO measure
102                                                                 (conf, output, value, mtime, toolgit,
103                                                                 linuxgit, measurement, result)
104                                                                 VALUES
105                                                                 ($1, $2, $3, $4, $5, $6, $7, $8);
106                                                                 """)
107                 gt = self.check_toolsgit()
108                 lgt = self.check_linuxgit()
109                 tm = datetime.datetime.now()
110                 ps(conf_id, output, value, tm, gt, lgt, conf.measure_identifier, result)
111
112         def update_measure(self, measure_id, value):
113                 "Update measured value"
114                 ps = self.db.prepare("""UPDATE measure SET
115                                                                 (value) = ($2)
116                                                                 WHERE
117                                                                 id = $1;
118                                                                 """)
119                 ps(measure_id, value)
120
121         def get_measures(self, conf_id):
122                 "Get measures for configuration with conf_id id"
123                 ps = self.db.prepare("""SELECT id, output, value FROM measure
124                                                                 WHERE conf = $1;
125                                                                 """)
126                 rtn = []
127                 for dt in ps(conf_id):
128                         rtn.append(Measure(dt[0], conf_id, dt[1], dt[2]))
129                 return rtn
130
131         def get_unmeasured(self):
132                 "Returns list of all unmeasured configurations."
133                 # FIXME: Take into account case when we want the same
134                 # configuration for either different experiment or kernel
135                 # version or target.
136                 ps = self.db.prepare("""SELECT id, hash, config FROM configurations
137                                                                 WHERE id NOT IN
138                                                                 (SELECT conf FROM measure)
139                                                                 """)
140                 rtn = []
141                 for dt in ps():
142                         rtn.append(Config(dt[0], dt[1], dt[2].split('\n')))
143                 return rtn
144
145         def add_configsort(self, configopt):
146                 "Add configuration option to sorted list"
147                 ps = self.db.prepare("""INSERT INTO configopt
148                                                                 (configopt) VALUES ($1)
149                                                                 """)
150                 ps(configopt)
151
152         def get_configsort(self):
153                 "Returns sorted list of all configuration options"
154                 ps = self.db.prepare("""SELECT id, configopt FROM configopt
155                                                                 ORDER BY id ASC
156                                                                 """)
157                 rtn = []
158                 itms = ps()
159                 for id, config in itms:
160                         rtn.append(config)
161                 return rtn