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
|
#include <errno.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include "evanix.h"
#include "queue.h"
#include "solver_conformity.h"
#include "util.h"
#define MAX_NIX_PKG_COUNT 200000
static int queue_push(struct queue *queue, struct job *job);
static int queue_dag_isolate(struct job *job, struct job *keep_parent,
struct job_clist *jobs, struct job **htab);
static int queue_dag_isolate(struct job *job, struct job *keep_parent,
struct job_clist *jobs, struct job **htab)
{
int ret;
for (size_t i = 0; i < job->deps_filled; i++) {
ret = queue_dag_isolate(job->deps[i], job, jobs, htab);
if (ret < 0)
return ret;
}
for (size_t i = 0; i < job->parents_filled; i++) {
if (job->parents[i] == keep_parent)
continue;
job_deps_list_rm(job->parents[i], job);
}
if (keep_parent != NULL) {
job->parents[0] = keep_parent;
job->parents_filled = 1;
} else {
/* it must be tha parent */
job->parents_filled = 0;
}
if (job->requested)
CIRCLEQ_REMOVE(jobs, job, clist);
HASH_DEL(*htab, job);
return 0;
}
int queue_isempty(struct job_clist *jobs)
{
struct job *j;
CIRCLEQ_FOREACH (j, jobs, clist) {
if (j->stale == false)
return false;
}
return true;
}
void *queue_thread_entry(void *queue_thread)
{
struct queue_thread *qt = queue_thread;
struct job *job = NULL;
int ret = 0;
while (true) {
ret = job_read(qt->stream, &job);
if (ret == JOB_READ_EOF) {
qt->queue->state = Q_ITS_OVER;
sem_post(&qt->queue->sem);
ret = 0;
break;
} else if (ret == JOB_READ_EVAL_ERR ||
ret == JOB_READ_JSON_INVAL ||
ret == JOB_READ_SYS_MISMATCH ||
ret == JOB_READ_CACHED) {
continue;
} else if (ret == JOB_READ_SUCCESS) {
queue_push(qt->queue, job);
} else {
break;
}
}
pthread_exit(NULL);
}
int queue_pop(struct queue *queue, struct job **job)
{
int ret;
struct job *j;
if (CIRCLEQ_EMPTY(&queue->jobs)) {
print_err("%s", "Empty queue");
return -EPERM;
}
pthread_mutex_lock(&queue->mutex);
if (evanix_opts.max_builds || evanix_opts.max_time) {
ret = evanix_opts.solver(&j, &queue->jobs, queue->resources);
if (ret < 0)
goto out_mutex_unlock;
queue->resources -= ret;
} else {
j = CIRCLEQ_FIRST(&queue->jobs);
}
ret = queue_dag_isolate(j, NULL, &queue->jobs, &queue->htab);
if (ret < 0)
goto out_mutex_unlock;
out_mutex_unlock:
pthread_mutex_unlock(&queue->mutex);
if (ret >= 0)
*job = j;
return ret;
}
/* this merge functions are closely tied to the output characteristics of
* nix-eval-jobs, that is
* - only two level of nodes (root and childrens or dependencies)
* - only childrens or dependencies have parent node
* - only root node have dependencies
*/
int queue_htab_job_merge(struct job **job, struct job **htab)
{
int ret;
struct job *jtab = NULL;
struct job *j = *job;
HASH_FIND_STR(*htab, j->drv_path, jtab);
if (jtab == NULL) {
HASH_ADD_STR(*htab, drv_path, j);
for (size_t i = 0; i < j->deps_filled; i++) {
ret = queue_htab_job_merge(&j->deps[i], htab);
if (ret < 0)
return ret;
}
return 0;
}
/* if it's already inside htab, it's deps should also be in htab, hence
* not merging deps */
if (jtab->name == NULL) {
/* steal name from new job struct */
jtab->name = j->name;
j->name = NULL;
}
/* only recursive calls with childrens or dependencies can enter this
* for a recursive call to happen the parent was just entered into htab
* so,
* - update parent's reference to point to jtab (done by *job = jtab)
* - insert the parent to jtab
*/
if (j->parents_filled > 0) {
ret = job_parents_list_insert(jtab, j->parents[0]);
if (ret < 0)
return ret;
j->parents_filled = 0;
}
job_free(*job);
*job = jtab;
return 0;
}
static int queue_push(struct queue *queue, struct job *job)
{
int ret;
pthread_mutex_lock(&queue->mutex);
ret = queue_htab_job_merge(&job, &queue->htab);
if (ret < 0) {
pthread_mutex_unlock(&queue->mutex);
return ret;
}
/* no duplicate entries in queue */
if (!job->requested) {
job->requested = true;
CIRCLEQ_INSERT_TAIL(&queue->jobs, job, clist);
}
pthread_mutex_unlock(&queue->mutex);
sem_post(&queue->sem);
return 0;
}
void queue_thread_free(struct queue_thread *queue_thread)
{
struct job *j;
int ret;
if (queue_thread == NULL)
return;
while (!CIRCLEQ_EMPTY(&queue_thread->queue->jobs)) {
j = CIRCLEQ_FIRST(&queue_thread->queue->jobs);
ret = queue_dag_isolate(j, NULL, &queue_thread->queue->jobs,
&queue_thread->queue->htab);
if (ret < 0)
return;
job_free(j);
}
ret = sem_destroy(&queue_thread->queue->sem);
if (ret < 0)
print_err("%s", strerror(errno));
ret = pthread_mutex_destroy(&queue_thread->queue->mutex);
if (ret < 0)
print_err("%s", strerror(errno));
free(queue_thread->queue);
free(queue_thread);
}
int queue_thread_new(struct queue_thread **queue_thread, FILE *stream)
{
int ret = 0;
struct queue_thread *qt = NULL;
qt = malloc(sizeof(*qt));
if (qt == NULL) {
print_err("%s", strerror(errno));
return -errno;
}
qt->stream = stream;
qt->queue = malloc(sizeof(*qt->queue));
if (qt->queue == NULL) {
print_err("%s", strerror(errno));
ret = -errno;
goto out_free_qt;
}
if (evanix_opts.max_builds)
qt->queue->resources = evanix_opts.max_builds;
else if (evanix_opts.max_time)
qt->queue->resources = evanix_opts.max_time;
else
qt->queue->resources = 0;
qt->queue->htab = NULL;
qt->queue->jobid = NULL;
qt->queue->state = Q_SEM_WAIT;
ret = sem_init(&qt->queue->sem, 0, 0);
if (ret < 0) {
print_err("%s", strerror(errno));
ret = -errno;
goto out_free_queue;
}
CIRCLEQ_INIT(&qt->queue->jobs);
pthread_mutex_init(&qt->queue->mutex, NULL);
out_free_queue:
if (ret < 0)
free(qt->queue);
out_free_qt:
if (ret < 0)
free(qt);
else
*queue_thread = qt;
return ret;
}
|