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