]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/initialize.py
e476f1d10bad01f3106ea1c825223546d0eb9fdc
[linux-conf-perf.git] / scripts / initialize.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import subprocess
5 import shutil
6
7 import utils
8 import database
9 from conf import conf
10 from conf import sf
11 import exceptions
12 import configurations
13
14 def all():
15         try:
16                 utils.dirtycheck()
17         except exceptions.DirtyRepository as e:
18                 print("Warning: " + str(e))
19         base()
20         parse_kconfig()
21         gen_fixed()
22         checkmeasure()
23         database.database() # check if database is initialized
24
25 def base():
26         print('Initialize base...')
27         try:
28                 os.mkdir(sf(conf.build_folder))
29         except FileExistsError:
30                 pass
31         try:
32                 os.mkdir(sf(conf.log_folder))
33         except FileExistsError:
34                 pass
35
36 def parse_kconfig():
37         "Execute parse_kconfig in linux_sources directory."
38         if os.path.isfile(sf(conf.symbol_map_file)) and \
39                         os.path.isfile(sf(conf.rules_file)) and \
40                         os.path.isfile(sf(conf.variable_count_file)):
41                 print('Warning: parse_kconfig not executed. Files already exists.')
42                 return
43         print('Executing parse_kconfig...')
44         wd = os.getcwd()
45         os.chdir(sf(conf.linux_sources))
46         parse_kconfig_cmd = [sf(conf.parse_kconfig)]
47         parse_kconfig_cmd += [sf(conf.linux_kconfig_head), sf(conf.build_folder)]
48         parse_kconfig_cmd += ['-v', '-v']
49         utils.callsubprocess("parse_kconfig", parse_kconfig_cmd,
50                         conf.parse_kconfig_output, env=utils.get_kernel_env())
51         os.chdir(wd)
52
53
54 def __gen_allconfig_fixed__():
55         wd = os.getcwd()
56         os.chdir(sf(conf.linux_sources))
57         allconfig_cmd = [sf(conf.allconfig)]
58         allconfig_cmd += ['Kconfig', sf(conf.dot_config), sf(conf.dot_measure_file)]
59         allconfig_cmd += ['--inv']
60         utils.callsubprocess("allconfig_fixed", allconfig_cmd, False,
61                         env = utils.get_kernel_env())
62         os.chdir(wd)
63
64 def gen_fixed():
65         "Generates fixed depenpency from dot_config file."
66         print('Generating required configuration...')
67
68         if not os.path.isfile(sf(conf.dot_config)):
69                 raise exceptions.MissingFile(sf(conf.dot_config),
70                                 'Generate fixed configuration. Use make dot_config.')
71
72         utils.build_symbol_map() # Ensure smap existence
73         srmap = {value:key for key, value in utils.smap.items()} # swap dictionary
74
75         shutil.copy(sf(conf.dot_config), sf(conf.dot_config_back_file))
76         __gen_allconfig_fixed__()
77
78         with open(sf(conf.dot_config), 'r') as f:
79                 with open(sf(conf.fixed_file), 'w') as ffix:
80                         for line in f:
81                                 if (line[0] == '#') or (not '=' in line):
82                                         continue
83                                 indx = line.index('=')
84                                 if (line[indx + 1] == 'y'):
85                                         if line[7:indx] == "MODULES": # exception if modules set
86                                                 raise exceptions.ConfigurationError("Fixed kernel configuration must have MODULES disabled.")
87                                         ffix.write(str(srmap[line[7:indx]]) + "\n")
88                                 elif (line[indx + 1] == 'n' or line[indx + 1] == 'm'):
89                                         ffix.write("-" + str(srmap[line[7:indx]]) + "\n")
90         with open(sf(conf.dot_measure_file), 'r') as f:
91                 with open(sf(conf.measure_file), 'w') as fmes:
92                         for line in f:
93                                 if (line[0] == '#') or (not '=' in line):
94                                         continue
95                                 indx = line.index('=')
96                                 if line[7:indx] == "MODULES":
97                                         raise exceptions.ConfigurationError("Can't measure configuraion option MODULES. Not supported.")
98                                 fmes.write(str(srmap[line[7:indx]]) + "\n")
99
100 def checkmeasure():
101         print("Checking if all configurations can be measured...")
102         utils.build_symbol_map()
103         measure_list = set()
104         with open(sf(conf.variable_count_file)) as f:
105                 var_num = f.readline().rstrip()
106                 conf_num = f.readline().rstrip()
107         with open(sf(conf.measure_file), 'r') as fi:
108                 for ln in fi:
109                         measure_list.add(int(ln))
110         for measure in measure_list:
111                 tfile1 = configurations.__buildtempcnf__(var_num, (sf(conf.rules_file),
112                         sf(conf.fixed_file)), [str(measure)])
113                 tfile2 = configurations.__buildtempcnf__(var_num, (sf(conf.rules_file),
114                         sf(conf.fixed_file)), [str(-1 * measure)])
115                 try:
116                         configurations.__exec_sat__(tfile1, [], conf_num)
117                         configurations.__exec_sat__(tfile2, [], conf_num)
118                 except exceptions.NoSolution:
119                         print("W: " + utils.smap[measure] + " won't be measured!")
120
121 #################################################################################
122
123 if __name__ == '__main__':
124         all()