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