]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/loop.py
Fix configuration generation
[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 # Multithread #
52 __conflist__ = set()
53 __listlock__ = Lock()
54
55 class prepareThread(Thread):
56         def __init__(self, name='prepare'):
57                 Thread.__init__(self, name=name)
58         def run(self):
59                 __listlock__.aquire()
60                 while not __terminate__ and len(__conflist__) <= conf.multithread_buffer:
61                         __listlock__.release()
62                         try:
63                                 img, config = prepare()
64                         except exceptions.NoApplicableConfiguration:
65                                 return
66                         __listlock__.aquire()
67                         __conflist__.add((img, config))
68                         if not __measurethread__.isActive():
69                                 __measurethread__.start()
70                 __listlock__.release()
71
72 class measureThread(Thread):
73         def __init__(self, name='measure'):
74                 Thread.__init__(self, name=name)
75         def run(self):
76                 __listlock__.aquire()
77                 while not __terminate__ and len(__conflist__) > 0:
78                         img, config = __conflist__.pop()
79                         __listlock__.release()
80                         if not __preparethread__.isActive():
81                                 __preparethread__.start()
82                         measure(img, config)
83                         __listlock__.aquire()
84                 __listlock__.release()
85
86 __preparethread__ = prepareThread()
87 __measurethread__ = measureThread()
88
89 # Start and sigterm handler #
90 def sigterm_handler(_signo, _stack_frame):
91         __terminate__ = True
92
93 # Main loop and single thread #
94 def loop():
95         utils.dirtycheck()
96         initialize.all()
97         if conf.multithread:
98                 __preparethread__.start()
99                 __measurethread__.start()
100         else:
101                 if conf.single_loop:
102                         img, config = prepare()
103                         measure(img, config)
104                 else:
105                         while not __terminate__:
106                                 img, config = prepare()
107                                 measure(img, config)
108
109 #################################################################################
110
111 if __name__ == '__main__':
112         signal.signal(signal.SIGTERM, sigterm_handler)
113         signal.signal(signal.SIGINT, sigterm_handler)
114         loop()