Skip to content

Commit

Permalink
evanix: add --solver flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanmohd committed Jul 21, 2024
1 parent 15a55c7 commit c3c9830
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
3 changes: 3 additions & 0 deletions include/evanix.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdbool.h>
#include <stdint.h>

#include "jobs.h"

#ifndef EVANIX_H

struct evanix_opts_t {
Expand All @@ -12,6 +14,7 @@ struct evanix_opts_t {
bool check_cache_status;
char *system;
uint32_t max_build;
int (*solver)(struct job **, struct job_clist *, int32_t);
};

extern struct evanix_opts_t evanix_opts;
Expand Down
2 changes: 1 addition & 1 deletion include/solver_greedy.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "jobs.h"

int solver_greedy(struct job_clist *q, int32_t *max_build, struct job **job);
int solver_greedy(struct job **job, struct job_clist *q, int32_t resources);
2 changes: 2 additions & 0 deletions include/solver_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct jobid {

void jobid_free(struct jobid *jid);
int jobid_init(struct job_clist *q, struct jobid **job_ids);
int solver_fcfs(struct job **job, struct job_clist *q,
__attribute__((unused)) int32_t resources);

#define SOLVER_UTIL_H
#endif
22 changes: 21 additions & 1 deletion src/evanix.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "build.h"
#include "evanix.h"
#include "queue.h"
#include "solver_greedy.h"
#include "solver_util.h"
#include "util.h"

static const char usage[] =
Expand All @@ -20,6 +22,7 @@ static const char usage[] =
" -p, --pipelined <bool> Use evanix build pipeline.\n"
" -l, --check_cache-status <bool> Perform cache locality check.\n"
" -c, --close-unused-fd <bool> Close stderr on exec.\n"
" -k, --solver fcfs|greedy Solver to use.\n"
"\n";

struct evanix_opts_t evanix_opts = {
Expand All @@ -31,6 +34,7 @@ struct evanix_opts_t evanix_opts = {
.system = NULL,
.solver_report = false,
.check_cache_status = true,
.solver = solver_fcfs,
};

static int evanix_build_thread_create(struct build_thread *build_thread);
Expand Down Expand Up @@ -129,6 +133,7 @@ int main(int argc, char *argv[])
{"help", no_argument, NULL, 'h'},
{"flake", no_argument, NULL, 'f'},
{"dry-run", no_argument, NULL, 'd'},
{"solver", required_argument, NULL, 'k'},
{"system", required_argument, NULL, 's'},
{"solver-report", no_argument, NULL, 'r'},
{"max-build", required_argument, NULL, 'm'},
Expand All @@ -138,7 +143,7 @@ int main(int argc, char *argv[])
{NULL, 0, NULL, 0},
};

while ((c = getopt_long(argc, argv, "hfds:r::m:p:c:l:", longopts,
while ((c = getopt_long(argc, argv, "hfds:r::m:p:c:l:k:", longopts,
&longindex)) != -1) {
switch (c) {
case 'h':
Expand All @@ -157,6 +162,21 @@ int main(int argc, char *argv[])
case 'r':
evanix_opts.solver_report = true;
break;
case 'k':
if (!strcmp(optarg, "greedy")) {
evanix_opts.solver = solver_greedy;
} else if (!strcmp(optarg, "fcfs")) {
evanix_opts.solver = solver_fcfs;
} else {
fprintf(stderr,
"option -%c has an invalid solver "
"argument\n"
"Try 'evanix --help' for more "
"information.\n",
c);
exit(EXIT_FAILURE);
}
break;
case 'm':
ret = atoi(optarg);
if (ret <= 0) {
Expand Down
12 changes: 5 additions & 7 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,11 @@ int queue_pop(struct queue *queue, struct job **job)
}

pthread_mutex_lock(&queue->mutex);
if (evanix_opts.max_build) {
ret = solver_greedy(&queue->jobs, &queue->resources, &j);
if (ret < 0)
goto out_mutex_unlock;
} else {
j = CIRCLEQ_FIRST(&queue->jobs);
}
ret = evanix_opts.solver(&j, &queue->jobs, queue->resources);
if (ret < 0)
goto out_mutex_unlock;
else if (evanix_opts.max_build)
queue->resources -= ret;
ret = queue_dag_isolate(j, NULL, &queue->jobs, &queue->htab);
if (ret < 0)
goto out_mutex_unlock;
Expand Down
15 changes: 7 additions & 8 deletions src/solver_greedy.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ static float conformity(struct job *job)
return conformity;
}

static void solver_report(struct job *job, int32_t max_build)
static void solver_report(struct job *job, int32_t resources)
{
if (!evanix_opts.solver_report)
return;
else if (job->reported)
return;

job->reported = true;
printf("❌ cost: %2d > %2d <-> %s -> %s\n", job_cost(job), max_build,
printf("❌ cost: %2d > %2d <-> %s -> %s\n", job_cost(job), resources,
job->name, job->drv_path);

for (size_t i = 0; i < job->parents_filled; i++)
solver_report(job->parents[i], max_build);
solver_report(job->parents[i], resources);
}

int solver_greedy(struct job_clist *q, int32_t *max_build, struct job **job)
int solver_greedy(struct job **job, struct job_clist *q, int32_t resources)
{
struct job *j;
float conformity_cur;
Expand All @@ -61,9 +61,9 @@ int solver_greedy(struct job_clist *q, int32_t *max_build, struct job **job)
CIRCLEQ_FOREACH (j, q, clist) {
if (j->stale) {
continue;
} else if (job_cost(j) > *max_build) {
} else if (job_cost(j) > resources) {
job_stale_set(j);
solver_report(j, *max_build);
solver_report(j, resources);
continue;
}
}
Expand All @@ -90,7 +90,6 @@ int solver_greedy(struct job_clist *q, int32_t *max_build, struct job **job)
if (selected == NULL)
return -ESRCH;

*max_build -= job_cost(selected);
*job = selected;
return 0;
return job_cost(selected);
}
12 changes: 12 additions & 0 deletions src/solver_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ int jobid_init(struct job_clist *q, struct jobid **job_ids)

return ret;
}

int solver_fcfs(struct job **job, struct job_clist *q,
__attribute__((unused)) int32_t resources)
{
if (CIRCLEQ_EMPTY(q)) {
print_err("%s", "Trying to pop from empty queue");
return -ESRCH;
}

*job = CIRCLEQ_FIRST(q);
return 0;
}

0 comments on commit c3c9830

Please sign in to comment.