aboutsummaryrefslogtreecommitdiff
path: root/src/solver_sjf.c
blob: 4cbdbc1ae06db0ed532245c4ac97e469fb252f91 (plain) (blame)
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
#include <errno.h>
#include <queue.h>

#include "evanix.h"
#include "jobs.h"
#include "solver_sjf.h"

int solver_sjf(struct job **job, struct job_clist *q, int32_t resources)
{
	struct job *j;
	int cost_cur;

	struct job *selected = NULL;
	int cost_min = -1;

	CIRCLEQ_FOREACH (j, q, clist) {
		if (j->stale)
			continue;

		cost_cur = job_cost_recursive(j);
		if (cost_cur < 0)
			return cost_cur;

		if (cost_cur > resources) {
			job_stale_set(j);
			if (evanix_opts.solver_report) {
				printf("❌ refusing to build %s, cost: %d\n",
				       j->drv_path, cost_cur);
			}
		}

		if (cost_min < 0 || cost_min > cost_cur) {
			selected = j;
			cost_min = cost_cur;
		}
	}

	*job = selected;
	return (cost_min < 0) ? -ESRCH : cost_min;
}