-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtuneit
executable file
·148 lines (130 loc) · 3.37 KB
/
tuneit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/python
# -*- python -*-
# -*- coding: utf-8 -*-
import ethtool, getopt, schedutils, sys, time, procfs
from dbstats import dbstats
from procfs import sysctl
def replay_sysctl(knob, value, parms):
s = sysctl()
s["%s/%s" % (parms[0], knob)] = value
del s
def replay_nic_affinity(knob, value, parms):
entries=value.split(";")
print entries
knob_replayers = {
"tcp_congestion_control": [ replay_sysctl, "net/ipv4" ],
"vsyscall64": [ replay_sysctl, "kernel" ],
"nic_kthread_affinities": [ replay_nic_affinity ],
}
def dbquery(db, id):
tunings = db.get_system_tunings_by_id(id)
if not tunings:
return None
dict = {}
columns = [column[0] for column in db.cursor.description]
for i in range(len(columns)):
dict[columns[i]] = tunings[i]
return dict
def replay(knobs):
for knob in knobs.keys():
value = knobs[knob]
if value:
if knob_replayers.has_key(knob):
replayer = knob_replayers[knob]
replayer[0](knob, value, replayer[1:])
else:
print "no replayer for %s=%s" % (knob, value)
def show(knobs):
for knob in knobs.keys():
if knobs[knob]:
print "%s: %s" % (knob, knobs[knob])
def query(db, q):
try:
ids = db.get_system_tunings_ids_for_query(q)
except:
print "Invalid query"
return
print ",".join([str(id) for id in ids])
def dicts_common_key_values(dicts):
common_keys = []
for key in dicts[0].keys():
values = [ dict[key] for dict in dicts ]
if len(list(set(values))) == 1:
common_keys.append(key)
return common_keys
def get_tunings_for_id_list(db, ids):
tunings = []
for id in ids:
t = dbquery(db, int(id))
t['tuning_id'] = id
tunings.append(t)
return tunings
def common(db, ids):
tunings = get_tunings_for_id_list(db, ids)
common_keys = dicts_common_key_values(tunings)
for key in common_keys:
print "%s: %s" % (key, tunings[0][key])
def diff(db, ids):
tunings = get_tunings_for_id_list(db, ids)
common_keys = dicts_common_key_values(tunings)
common_keys.append('tuning_id')
for tuning in tunings:
print "Tunings#: %s" % tuning['tuning_id']
for key in tunings[0].keys():
if key not in common_keys:
print " %s: %s" % (key, tuning[key])
def usage():
print "tuneit: [--help|h] [--query|q QUERY] [--replay|r TUNING_ID] [--show|s TUNING_ID]"
def main(args):
try:
opts, args = getopt.getopt(args, "d:D:c:hq:r:s:",
[ "db", "diff=", "common=", "help",
"query=", "replay=",
"show="], )
except getopt.GetoptError, err:
usage()
print str(err)
sys.exit(2)
if not opts:
usage()
sys.exit()
dbname = "tuna"
tuning_id = -1
query_str = None
common_str = None
diff_str = None
for o, a in opts:
if o in ( "-h", "--help" ):
usage()
sys.exit()
elif o in ( "-q", "--query" ):
query_str = a
elif o in ( "-c", "--common" ):
common_str = a
elif o in ( "-D", "--diff" ):
diff_str = a
elif o in ( "-r", "--replay", "-s", "--show" ):
tuning_id = int(a)
if o in ("-r", "--replay"):
dboperation = replay
else:
dboperation = show
elif o in ( "-d", "--db" ):
dbname = a
else:
assert False, "unhandled option"
db = dbstats(dbname)
if tuning_id != -1:
knobs = dbquery(db, tuning_id)
if not knobs:
print "%d not found" % tuning_id
sys.exit(2)
dboperation(knobs)
elif query_str:
query(db, query_str)
elif common_str:
common(db, common_str.split(","))
elif diff_str:
diff(db, diff_str.split(","))
if __name__ == '__main__':
main(sys.argv[1:])