]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/loop.py
Export int values to conf.mk
[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 from threading import Event
9
10 from conf import conf
11 from conf import sf
12 import initialize
13 import configurations
14 import kernel
15 import boot
16 import exceptions
17 import database
18 import utils
19
20 __confs_unmeasured__ = []
21
22 def prepare():
23         """Prepare for measuring
24         Outcome is Linux image for generated configuration."""
25         print("Preparing new image.")
26         global __confs_unmeasured__
27         if len(__confs_unmeasured__) == 0:
28                 dtb = database.database()
29                 confs = dtb.get_unmeasured()
30                 if len(confs) == 0:
31                         configurations.generate()
32                         confs = dtb.get_unmeasured()
33                         if len(confs) == 0:
34                                 raise exceptions.NoApplicableConfiguration()
35                 __confs_unmeasured__ = list(confs)
36         con = __confs_unmeasured__.pop()
37         kernel.config(con.config)
38         img = kernel.make(con.hash)
39         print("Prepared image: " + img)
40         return img, con
41
42 def measure(kernelimg, con):
43         print("Measuring " + con.hash)
44         try:
45                 os.remove(sf(conf.jobfolder_linux_image))
46         except FileNotFoundError:
47                 pass
48         os.symlink(kernelimg, sf(conf.jobfolder_linux_image))
49         boot.boot(con)
50         print("Configuration '" + con.hash + "' measured.")
51
52 # Multithread #
53 __conflist__ = []
54 __listlock__ = Lock()
55 __preparethreadEvent__ = Event()
56 __measurethreadEvent__ = Event()
57
58 class prepareThread(Thread):
59         global __preparethread__
60         global __measurethread__
61         def __init__(self, name='prepare'):
62                 Thread.__init__(self, name=name)
63         def run(self):
64                 print('Prepare thread start')
65                 while not __terminate__:
66                         try:
67                                 img, config = prepare()
68                         except exceptions.NoApplicableConfiguration:
69                                 return
70                         __listlock__.acquire()
71                         __conflist__.append((img, config))
72                         __preparethreadEvent__.set()
73                         if len(__conflist__) > conf.multithread_buffer:
74                                 __listlock__.release()
75                                 print('Prepare thread suspended')
76                                 __measurethreadEvent__.wait()
77                                 print('Prepare thread waken')
78                         else:
79                                 __listlock__.release()
80                         __measurethreadEvent__.clear()
81                 print('Prepare thread stop')
82
83 class measureThread(Thread):
84         global __preparethread__
85         global __measurethread__
86         def __init__(self, name='measure'):
87                 Thread.__init__(self, name=name)
88         def run(self):
89                 print('Measure thread start')
90                 while not __terminate__:
91                         __listlock__.acquire()
92                         if len(__conflist__) <= 0:
93                                 __listlock__.release()
94                                 print('Measure thread suspended')
95                                 __preparethreadEvent__.wait()
96                                 print('Measure thread waken')
97                                 __listlock__.acquire()
98                         __preparethreadEvent__.clear()
99                         img, config = __conflist__.pop()
100                         __listlock__.release()
101                         __measurethreadEvent__.set()
102                         measure(img, config)
103                 print('Measure thread stop')
104
105 __preparethread__ = prepareThread()
106 __measurethread__ = measureThread()
107
108 # Start and sigterm handler #
109 def sigterm_handler(_signo, _stack_frame):
110         global __terminate__
111         __terminate__ = True
112         if conf.multithread:
113                 __measurethreadEvent__.set()
114                 __preparethreadEvent__.set()
115
116 # Main loop and single thread #
117 __terminate__ = False
118 def loop():
119         utils.dirtycheck()
120         initialize.all()
121         if conf.multithread:
122                 __preparethread__.start()
123                 __measurethread__.start()
124         else:
125                 if conf.single_loop:
126                         img, config = prepare()
127                         measure(img, config)
128                 else:
129                         while not __terminate__:
130                                 img, config = prepare()
131                                 measure(img, config)
132
133 #################################################################################
134
135 if __name__ == '__main__':
136         signal.signal(signal.SIGTERM, sigterm_handler)
137         signal.signal(signal.SIGINT, sigterm_handler)
138         loop()