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