]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/loop.py
Separate initialisation
[linux-conf-perf.git] / scripts / loop.py
1 #!/bin/env python3
2 import os
3 import sys
4 import subprocess
5 import signal
6 from threading import Thread
7
8 from conf import conf
9 from conf import sf
10 import initialize
11 import solution
12 import kernel
13 import boot
14 import exceptions
15
16 def step():
17         phs = phase_get()
18         if phs == 0 or phs == 1:
19                 phase_message(1)
20                 initialize.all()
21                 phase_set(2)
22         elif phs == 2:
23                 phase_message(2)
24                 phase_set(3)
25         elif phs == 3:
26                 phase_message(3)
27                 solution.generate()
28                 iteration_inc()
29                 phase_set(4)
30         elif phs == 4:
31                 phase_message(4)
32                 phase_set(5)
33         elif phs == 5:
34                 phase_message(5)
35                 solution.apply()
36                 phase_set(6)
37         elif phs == 6:
38                 phase_message(6)
39                 phase_set(7)
40         elif phs == 7:
41                 phase_message(7)
42                 try:
43                         kernel.config()
44                 except exceptions.ConfigurationError:
45                         if not conf.ignore_misconfig:
46                                 print("Configuration mismatch. Exiting.")
47                                 sys.exit(-2)
48                 phase_set(8)
49         elif phs == 8:
50                 phase_message(8)
51                 if conf.only_config:
52                         phase_set(2)
53                 else:
54                         phase_set(9)
55         elif phs == 9:
56                 phase_message(9)
57                 kernel.make()
58                 phase_set(10)
59         elif phs == 10:
60                 phase_message(10)
61                 phase_set(11)
62         elif phs == 11:
63                 phase_message(11)
64                 boot.boot()
65                 phase_set(12)
66         elif phs == 12:
67                 phase_message(12)
68                 phase_set(2)
69
70 # Phase #
71 phases = ("Not Initialized",            #0
72                   "Initializing",                       #1
73                   "Initialized",                        #2
74                   "Solution generating",        #3
75                   "Solution generated",         #4
76                   "Solution applying",          #5
77                   "Solution applied",           #6
78                   "Kernel configuration",       #7
79                   "Kernel configured",          #8
80                   "Kernel build",                       #9
81                   "Kernel built",                       #10
82                   "System boot",                        #11
83                   "Benchmark successful"        #12
84                   )
85
86 def phase_get():
87         try:
88                 with open(sf(conf.phase_file)) as f:
89                         txtPhase = f.readline().rstrip()
90                 if not txtPhase in phases:
91                         raise PhaseMismatch()
92                 return phases.index(txtPhase)
93         except FileNotFoundError:
94                 return 0
95
96 def phase_set(phs):
97         # TODO
98         global thr
99         if thr.term:
100                 return
101         with open(sf(conf.phase_file), 'w') as f:
102                 f.write(phases[phs])
103
104 def phase_message(phs):
105         "Prints message signaling running phase_"
106         print("-- " + phases[phs])
107
108 # Iteration #
109 def iteration_reset():
110         with open(sf(conf.iteration_file), 'w') as f:
111                 f.write('0')
112
113 def iteration_inc():
114         with open(sf(conf.iteration_file), 'r') as f:
115                 it = int(f.readline())
116         it += 1
117         with open(sf(conf.iteration_file), 'w') as f:
118                 f.write(str(it))
119
120 # Thread #
121 class mainThread(Thread):
122         def __init__(self, name):
123                 Thread.__init__(self, name=name)
124                 self.term = False
125         def run(self):
126                 if conf.step_by_step:
127                         step()
128                 elif conf.single_loop:
129                         while not phase_get() == 2:
130                                 step()
131                         step()
132                         while not phase_get() == 2:
133                                 step()
134                 else:
135                         while not self.term:
136                                 step()
137
138 def loop_term():
139         global thr
140         thr.term = True
141
142 def sigterm_handler(_signo, _stack_frame):
143         loop_term()
144
145 def loop():
146         global thr
147         thr = mainThread("thred")
148         thr.start()
149         try:
150                 thr.join()
151         except KeyboardInterrupt:
152                 loop_term()
153
154 #################################################################################
155
156 if __name__ == '__main__':
157         signal.signal(signal.SIGTERM, sigterm_handler)
158         loop()