-
Notifications
You must be signed in to change notification settings - Fork 26
/
test.py
276 lines (187 loc) · 7.76 KB
/
test.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python3
import argparse, yaml, tempfile, os, subprocess, json, jinja2, datetime, copy, re, dnf, pprint, urllib.request, sys, koji
import concurrent.futures
import rpm_showme as showme
from functools import lru_cache
import multiprocessing, time
import asyncio
class AnalysisError(Exception):
pass
# Highlight subprocesses
first_log_pid = os.getpid()
def log(msg):
if first_log_pid == os.getpid():
print(" {} : {}".format(os.getpid(), msg), file=sys.stderr)
else:
print("* {} : {}".format(os.getpid(), msg), file=sys.stderr)
log("")
class Analyzer():
def __init__(self):
self.workload_queue = {}
self.workload_queue_counter_total = 0
self.workload_queue_counter_current = 0
self.current_subprocesses = 0
self.data = {}
self.data["envs"] = {}
self.data["envs"]["env_id"] = {}
self.data["envs"]["env_id"]["succeeded"] = True
def _return_failed_workload_env_err(self, workload_conf, env_conf, repo, arch):
workload = {}
workload["workload_conf_id"] = workload_conf["id"]
workload["env_conf_id"] = env_conf["id"]
workload["repo_id"] = repo["id"]
workload["arch"] = arch
workload["pkg_env_ids"] = []
workload["pkg_added_ids"] = []
workload["pkg_placeholder_ids"] = []
workload["pkg_relations"] = []
workload["errors"] = {}
workload["errors"]["non_existing_pkgs"] = []
workload["succeeded"] = False
workload["env_succeeded"] = False
workload["errors"]["message"] = """
Failed to analyze this workload because of an error while analyzing the environment.
Please see the associated environment results for a detailed error message.
"""
return workload
def _analyze_workload(self, workload_conf, env_conf, repo, arch):
workload = {}
workload["workload_conf_id"] = workload_conf["id"]
workload["env_conf_id"] = env_conf["id"]
workload["repo_id"] = repo["id"]
workload["arch"] = arch
log(workload)
time.sleep(2)
if workload_conf["id"] == 22 and arch == "x86_64":
fuck
return workload
def _analyze_workload_process(self, queue_result, workload_conf, env_conf, repo, arch):
workload = self._analyze_workload(workload_conf, env_conf, repo, arch)
queue_result.put(workload)
async def _analyze_workloads_subset_async(self, task_queue, results):
for task in task_queue:
workload_conf = task["workload_conf"]
env_conf = task["env_conf"]
repo = task["repo"]
arch = task["arch"]
workload_id = "{workload_conf_id}:{env_conf_id}:{repo_id}:{arch}".format(
workload_conf_id=workload_conf["id"],
env_conf_id=env_conf["id"],
repo_id=repo["id"],
arch=arch
)
# Max processes
while True:
if self.current_subprocesses < 4: # <<------ subprocesses
self.current_subprocesses += 1
break
else:
await asyncio.sleep(.1)
# Log progress
self.workload_queue_counter_current += 1
log("[{} of {}]".format(self.workload_queue_counter_current, self.workload_queue_counter_total))
log("Analyzing workload: {}".format(workload_id))
log("")
queue_result = multiprocessing.Queue()
process = multiprocessing.Process(target=self._analyze_workload_process, args=(queue_result, workload_conf, env_conf, repo, arch), daemon=True)
process.start()
# Now wait a bit for the result.
# This is a terrible way to implement an async way to
# wait for the result with a 222 seconds timeout.
# 2 seconds
for _ in range(1, 20):
if queue_result.empty():
await asyncio.sleep(.1)
else:
break
# 20 seconds
for _ in range(1, 20):
if queue_result.empty():
await asyncio.sleep(1)
else:
break
# 200 seconds
for _ in range(1, 20):
if queue_result.empty():
await asyncio.sleep(10)
else:
break
#while True:
# if process.is_alive():
# log(" waiting on {}".format(workload_id))
# await asyncio.sleep(1)
# else:
# break
self.current_subprocesses -= 1
# This basically means there was an exception in the processing and the process crashed
if queue_result.empty():
log("")
log("")
log("--------------------------------------------------------------------------")
log("")
log("ERROR: Workload analysis failed")
log("")
log("Details:")
log(" workload_conf: {}".format(workload_conf["id"]))
log(" env_conf: {}".format(env_conf["id"]))
log(" repo: {}".format(repo["id"]))
log(" arch: {}".format(arch))
log("")
log("More details somewhere above.")
log("")
log("--------------------------------------------------------------------------")
log("")
log("")
sys.exit(1)
workload = queue_result.get()
results[workload_id] = workload
async def _analyze_workloads_async(self, results):
log("_analyze_workloads_async")
tasks = []
for repo in self.workload_queue:
for arch in self.workload_queue[repo]:
task_queue = self.workload_queue[repo][arch]
tasks.append(asyncio.create_task(self._analyze_workloads_subset_async(task_queue, results)))
for task in tasks:
await task
await asyncio.sleep(.1)
log("DONE!")
def _queue_workload_processing(self, workload_conf, env_conf, repo, arch):
#log("_queue_workload_processing")
repo_id = repo["id"]
if repo_id not in self.workload_queue:
self.workload_queue[repo_id] = {}
if arch not in self.workload_queue[repo_id]:
self.workload_queue[repo_id][arch] = []
workload_task = {
"workload_conf": workload_conf,
"env_conf" : env_conf,
"repo" : repo,
"arch" : arch
}
self.workload_queue[repo_id][arch].append(workload_task)
def _analyze_workloads(self):
#log("_analyze_workloads")
for repo_id in ["fedora", "rhel"]:
for arch in ["aarch64", "x86_64"]:
for n in range(1,10):
workload_conf = {
"id": n
}
env_conf = {
"id": "env_id"
}
repo = {
"id": repo_id
}
self._queue_workload_processing(workload_conf, env_conf, repo, arch)
self.workload_queue_counter_total += 1
self.data["workloads"] = {}
log("Analyzing {} workloads.".format(self.workload_queue_counter_total))
log("")
log("")
asyncio.run(self._analyze_workloads_async(self.data["workloads"]))
log("")
a = Analyzer()
a._analyze_workloads()
print(a.data["workloads"])