]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/loop.py
f4a37a29fdbf156260b041d4eec494776221fb1b
[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__.acquire()
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__.acquire()
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__.acquire()
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__.acquire()
84                 __listlock__.release()
85
86 __preparethread__ = prepareThread()
87 __measurethread__ = measureThread()
88
89 # Start and sigterm handler #
90 __terminate__ = False
91 def sigterm_handler(_signo, _stack_frame):
92         global __terminate__
93         __terminate__ = True
94
95 # Main loop and single thread #
96 def loop():
97         utils.dirtycheck()
98         initialize.all()
99         if conf.multithread:
100                 __preparethread__.start()
101                 __measurethread__.start()
102         else:
103                 if conf.single_loop:
104                         img, config = prepare()
105                         measure(img, config)
106                 else:
107                         while not __terminate__:
108                                 img, config = prepare()
109                                 measure(img, config)
110
111 #################################################################################
112
113 if __name__ == '__main__':
114         signal.signal(signal.SIGTERM, sigterm_handler)
115         signal.signal(signal.SIGINT, sigterm_handler)
116         loop()