aboutsummaryrefslogtreecommitdiff
path: root/scripts/loop.py
blob: ad87f18be09e8d5de14e713ddf24509778a24195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
import os
import sys
import subprocess
import signal
from threading import Thread
from threading import Lock

from conf import conf
from conf import sf
import initialize
import configurations
import kernel
import boot
import exceptions
import database
import utils

__confs_unmeasured__ = []

def prepare():
	"""Prepare for measuring
	Outcome is Linux image for generated configuration."""
	print("Preparing new image.")
	global __confs_unmeasured__
	if len(__confs_unmeasured__) == 0:
		dtb = database.database()
		confs = dtb.get_unmeasured()
		if len(confs) == 0:
			configurations.generate()
			confs = dtb.get_unmeasured()
			if len(confs) == 0:
				raise exceptions.NoApplicableConfiguration()
		__confs_unmeasured__ = list(confs)
	con = __confs_unmeasured__.pop()
	kernel.config(con.config)
	img = kernel.make(con.hash)
	print("Prepared image: " + img)
	return img, con

def measure(kernelimg, con):
	print("Measuring " + con.hash)
	try:
		os.remove(sf(conf.jobfolder_linux_image))
	except FileNotFoundError:
		pass
	os.symlink(kernelimg, sf(conf.jobfolder_linux_image))
	boot.boot(con)
	print("Configuration '" + con.hash + "' measured.")

# Multithread #
__conflist__ = set()
__listlock__ = Lock()

class prepareThread(Thread):
	def __init__(self, name='prepare'):
		Thread.__init__(self, name=name)
	def run(self):
		__listlock__.aquire()
		while not __terminate__ and len(__conflist__) <= conf.multithread_buffer:
			__listlock__.release()
			try:
				img, config = prepare()
			except exceptions.NoApplicableConfiguration:
				return
			__listlock__.aquire()
			__conflist__.add((img, config))
			if not __measurethread__.isActive():
				__measurethread__.start()
		__listlock__.release()

class measureThread(Thread):
	def __init__(self, name='measure'):
		Thread.__init__(self, name=name)
	def run(self):
		__listlock__.aquire()
		while not __terminate__ and len(__conflist__) > 0:
			img, config = __conflist__.pop()
			__listlock__.release()
			if not __preparethread__.isActive():
				__preparethread__.start()
			measure(img, config)
			__listlock__.aquire()
		__listlock__.release()

__preparethread__ = prepareThread()
__measurethread__ = measureThread()

# Start and sigterm handler #
def sigterm_handler(_signo, _stack_frame):
	__terminate__ = True

# Main loop and single thread #
def loop():
	utils.dirtycheck()
	initialize.all()
	if conf.multithread:
		__preparethread__.start()
		__measurethread__.start()
	else:
		if conf.single_loop:
			img, config = prepare()
			measure(img, config)
		else:
			while not __terminate__:
				img, config = prepare()
				measure(img, config)

#################################################################################

if __name__ == '__main__':
	signal.signal(signal.SIGTERM, sigterm_handler)
	signal.signal(signal.SIGINT, sigterm_handler)
	loop()