]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/loop.py
Fix missing utils include in loop.py
[linux-conf-perf.git] / scripts / loop.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import subprocess
5 import signal
6 from threading import Thread
7 from threading import Lock
8
9 from conf import conf
10 from conf import sf
11 import initialize
12 import configurations
13 import kernel
14 import boot
15 import exceptions
16 import database
17 import utils
18
19 __confs_unmeasured__ = []
20
21 def prepare():
22         """Prepare for measuring
23         Outcome is Linux image for generated configuration."""
24         print("Preparing new image.")
25         global __confs_unmeasured__
26         if len(__confs_unmeasured__) == 0:
27                 dtb = database.database()
28                 confs = dtb.get_unmeasured()
29                 if len(confs) == 0:
30                         configurations.generate()
31                         confs = dtb.get_unmeasured()
32                         if len(confs) == 0:
33                                 raise exceptions.NoApplicableConfiguration()
34                 __confs_unmeasured__ = list(confs)
35         con = __confs_unmeasured__.pop()
36         kernel.config(con.config)
37         img = kernel.make(con.hash)
38         print("Prepared image: " + img)
39         return img, con
40
41 def measure(kernelimg, con):
42         print("Measuring " + con.hash)
43         try:
44                 os.remove(sf(conf.jobfolder_linux_image))
45         except FileNotFoundError:
46                 pass
47         os.symlink(kernelimg, sf(conf.jobfolder_linux_image))
48         boot.boot(con)
49         print("Configuration '" + con.hash + "' measured.")
50
51 # Threads #
52 __terminate__ = False
53 class mainThread(Thread):
54         def run(self):
55                 if conf.single_loop:
56                         img, config = prepare()
57                         measure(img, config)
58                 else:
59                         while not __terminate__:
60                                 img, config = prepare()
61                                 measure(img, config)
62
63 # Multithread section #
64 __conflist__ = set()
65 __listlock__ = Lock()
66
67 class prepareThread(Thread):
68         def __init__(self, name='prepare'):
69                 Thread.__init__(self, name=name)
70         def run(self):
71                 __listlock__.aquire()
72                 while not __terminate__ and len(__conflist__) <= conf.multithread_buffer:
73                         __listlock__.release()
74                         try:
75                                 img, config = prepare()
76                         except exceptions.NoApplicableConfiguration:
77                                 return
78                         __listlock__.aquire()
79                         __conflist__.add((img, config))
80                         if not __measurethread__.isActive():
81                                 __measurethread__.start()
82                 __listlock__.release()
83
84 class measureThread(Thread):
85         def __init__(self, name='measure'):
86                 Thread.__init__(self, name=name)
87         def run(self):
88                 __listlock__.aquire()
89                 while not __terminate__ and len(__conflist__) > 0:
90                         img, config = __conflist__.pop()
91                         __listlock__.release()
92                         if not __preparethread__.isActive():
93                                 __preparethread__.start()
94                         measure(img, config)
95                         __listlock__.aquire()
96                 __listlock__.release()
97
98 __preparethread__ = prepareThread()
99 __measurethread__ = measureThread()
100
101 # Start and sigterm handler #
102 def sigterm_handler(_signo, _stack_frame):
103         __terminate__ = True
104
105 def loop():
106         utils.dirtycheck()
107         initialize.all()
108         global thr
109         thr = mainThread()
110         thr.start()
111         try:
112                 thr.join()
113         except KeyboardInterrupt:
114                 __terminate__ = True
115
116 #################################################################################
117
118 if __name__ == '__main__':
119         signal.signal(signal.SIGTERM, sigterm_handler)
120         loop()