Skip to content

Commit

Permalink
queue: switch to uthash
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanmohd committed Jul 18, 2024
1 parent b74181d commit 961b325
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 203 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
jq
highs
cjson
uthash
nix-eval-jobs

pkg-config
Expand Down Expand Up @@ -59,6 +60,7 @@

src = ./.;
nativeBuildInputs = with pkgs; [
uthash
meson
ninja
pkg-config
Expand Down
18 changes: 0 additions & 18 deletions include/htab.h

This file was deleted.

2 changes: 2 additions & 0 deletions include/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdint.h>
#include <stdio.h>
#include <sys/queue.h>
#include <uthash.h>

#ifndef JOBS_H

Expand All @@ -21,6 +22,7 @@ struct job {
struct job **deps;
size_t parents_size, parents_filled;
struct job **parents;
UT_hash_handle hh;

/* queue */
CIRCLEQ_ENTRY(job) clist;
Expand Down
5 changes: 2 additions & 3 deletions include/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdint.h>
#include <sys/queue.h>

#include "htab.h"
#include "jobs.h"
#include "solver_util.h"

Expand All @@ -20,7 +19,7 @@ struct queue {
sem_t sem;
queue_state_t state;
pthread_mutex_t mutex;
struct htab *htab;
struct job *htab;

/* solver */
struct jobid *jobid;
Expand All @@ -36,7 +35,7 @@ struct queue_thread {
int queue_thread_new(struct queue_thread **queue_thread, FILE *stream);
void queue_thread_free(struct queue_thread *queue_thread);
void *queue_thread_entry(void *queue_thread);
int queue_pop(struct queue *queue, struct job **job, struct htab *htab);
int queue_pop(struct queue *queue, struct job **job);
int queue_isempty(struct job_clist *jobs);

#define QUEUE_H
Expand Down
2 changes: 1 addition & 1 deletion src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static int build(struct queue *queue)

char out_link[NAME_MAX] = "result";

ret = queue_pop(queue, &job, queue->htab);
ret = queue_pop(queue, &job);
if (ret == -ESRCH)
return EAGAIN;
if (ret < 0)
Expand Down
149 changes: 0 additions & 149 deletions src/htab.c

This file was deleted.

1 change: 0 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ e = executable(
'util.c',
'queue.c',
'build.c',
'htab.c',
'solver_util.c',
'solver_greedy.c',
],
Expand Down
44 changes: 15 additions & 29 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#define MAX_NIX_PKG_COUNT 200000

static int queue_push(struct queue *queue, struct job *job);
static int queue_htab_job_merge(struct job **job, struct htab *htab);
static int queue_htab_job_merge(struct job **job, struct job **htab);
static int queue_dag_isolate(struct job *job, struct job *keep_parent,
struct job_clist *jobs, struct htab *htab);
struct job_clist *jobs, struct job **htab);

static int queue_dag_isolate(struct job *job, struct job *keep_parent,
struct job_clist *jobs, struct htab *htab)
struct job_clist *jobs, struct job **htab)
{
int ret;

Expand Down Expand Up @@ -46,9 +46,7 @@ static int queue_dag_isolate(struct job *job, struct job *keep_parent,
if (job->scheduled)
CIRCLEQ_REMOVE(jobs, job, clist);

ret = htab_delete(htab, job->drv_path);
if (ret < 0)
return ret;
HASH_DEL(*htab, job);

return 0;
}
Expand Down Expand Up @@ -94,7 +92,7 @@ void *queue_thread_entry(void *queue_thread)
pthread_exit(NULL);
}

int queue_pop(struct queue *queue, struct job **job, struct htab *htab)
int queue_pop(struct queue *queue, struct job **job)
{
int ret;
struct job *j;
Expand All @@ -112,7 +110,7 @@ int queue_pop(struct queue *queue, struct job **job, struct htab *htab)
} else {
j = CIRCLEQ_FIRST(&queue->jobs);
}
ret = queue_dag_isolate(j, NULL, &queue->jobs, htab);
ret = queue_dag_isolate(j, NULL, &queue->jobs, &queue->htab);
if (ret < 0)
goto out_mutex_unlock;

Expand All @@ -130,19 +128,15 @@ int queue_pop(struct queue *queue, struct job **job, struct htab *htab)
* - only childrens or dependencies have parent node
* - only root node have dependencies
*/
static int queue_htab_job_merge(struct job **job, struct htab *htab)
static int queue_htab_job_merge(struct job **job, struct job **htab)
{
struct job *jtab;
ENTRY *ep;
int ret;
struct job *jtab = NULL;
struct job *j = *job;

ret = htab_search(htab, (*job)->drv_path, &ep);
if (ret < 0) {
return ret;
} else if (ret == ESRCH) {
ret = htab_enter(htab, (*job)->drv_path, *job);
if (ret < 0)
return ret;
HASH_FIND_STR(*htab, j->drv_path, jtab);
if (jtab == NULL) {
HASH_ADD_STR(*htab, drv_path, j);

for (size_t i = 0; i < (*job)->deps_filled; i++) {
ret = queue_htab_job_merge(&(*job)->deps[i], htab);
Expand All @@ -155,7 +149,6 @@ static int queue_htab_job_merge(struct job **job, struct htab *htab)

/* if it's already inside htab, it's deps should also be in htab, hence
* not merging deps */
jtab = ep->data;
if (jtab->name == NULL) {
/* steal name from new job struct */
jtab->name = (*job)->name;
Expand Down Expand Up @@ -185,7 +178,7 @@ 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);
ret = queue_htab_job_merge(&job, &queue->htab);
if (ret < 0) {
pthread_mutex_unlock(&queue->mutex);
return ret;
Expand Down Expand Up @@ -213,13 +206,12 @@ void queue_thread_free(struct queue_thread *queue_thread)
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);
&queue_thread->queue->htab);
if (ret < 0)
return;
job_free(j);
}

htab_free(queue_thread->queue->htab);
ret = sem_destroy(&queue_thread->queue->sem);
if (ret < 0)
print_err("%s", strerror(errno));
Expand Down Expand Up @@ -250,6 +242,7 @@ int queue_thread_new(struct queue_thread **queue_thread, FILE *stream)
ret = -errno;
goto out_free_qt;
}
qt->queue->htab = NULL;
qt->queue->resources = evanix_opts.max_build;
qt->queue->jobid = NULL;
qt->queue->state = Q_SEM_WAIT;
Expand All @@ -260,16 +253,9 @@ int queue_thread_new(struct queue_thread **queue_thread, FILE *stream)
goto out_free_queue;
}

ret = htab_init(MAX_NIX_PKG_COUNT, &qt->queue->htab);
if (ret < 0)
goto out_free_sem;

CIRCLEQ_INIT(&qt->queue->jobs);
pthread_mutex_init(&qt->queue->mutex, NULL);

out_free_sem:
if (ret < 0)
sem_destroy(&qt->queue->sem);
out_free_queue:
if (ret < 0)
free(qt->queue);
Expand Down
4 changes: 2 additions & 2 deletions src/solver_greedy.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ int solver_greedy(struct job_clist *q, int32_t *max_build, struct job **job)

if (!evanix_opts.solver_report)
continue;
printf("ℹ️ cost: %2d, conformity: %.2f -> %s\n",
job_cost(j), conformity_cur, j->drv_path);
printf("ℹ️ cost: %2d, conformity: %.2f -> %s\n", job_cost(j),
conformity_cur, j->drv_path);
}

if (selected == NULL)
Expand Down

0 comments on commit 961b325

Please sign in to comment.