chiark / gitweb /
manager: fix GC algorithm
[elogind.git] / manager.c
index f10345b43a9b83563156fa2c4723b5960fb77343..200bbfa006e38c6e0e1632713d3fe2b8e9ace24e 100644 (file)
--- a/manager.c
+++ b/manager.c
@@ -395,42 +395,70 @@ static unsigned manager_dispatch_cleanup_queue(Manager *m) {
         return n;
 }
 
         return n;
 }
 
-static void unit_gc_sweep(Unit *u, int gc_marker) {
+enum {
+        GC_OFFSET_IN_PATH,  /* This one is on the path we were travelling */
+        GC_OFFSET_UNSURE,   /* No clue */
+        GC_OFFSET_GOOD,     /* We still need this unit */
+        GC_OFFSET_BAD,      /* We don't need this unit anymore */
+        _GC_OFFSET_MAX
+};
+
+static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
         Iterator i;
         Unit *other;
         Iterator i;
         Unit *other;
+        bool is_bad;
 
         assert(u);
 
 
         assert(u);
 
-        if (u->meta.gc_marker == gc_marker ||
-            u->meta.gc_marker == -gc_marker)
+        if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD ||
+            u->meta.gc_marker == gc_marker + GC_OFFSET_BAD ||
+            u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH)
                 return;
 
                 return;
 
-        if (!u->meta.in_cleanup_queue)
+        if (u->meta.in_cleanup_queue)
                 goto bad;
 
         if (unit_check_gc(u))
                 goto good;
 
                 goto bad;
 
         if (unit_check_gc(u))
                 goto good;
 
+        u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH;
+
+        is_bad = true;
+
         SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
                 unit_gc_sweep(other, gc_marker);
 
         SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
                 unit_gc_sweep(other, gc_marker);
 
-                if (other->meta.gc_marker == gc_marker)
+                if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD)
                         goto good;
                         goto good;
+
+                if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD)
+                        is_bad = false;
         }
 
         }
 
+        if (is_bad)
+                goto bad;
+
+        /* We were unable to find anything out about this entry, so
+         * let's investigate it later */
+        u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE;
+        unit_add_to_gc_queue(u);
+        return;
+
 bad:
 bad:
-        /* So there is no reason to keep this unit around, hence let's get rid of it */
-        u->meta.gc_marker = -gc_marker;
+        /* We definitely know that this one is not useful anymore, so
+         * let's mark it for deletion */
+        u->meta.gc_marker = gc_marker + GC_OFFSET_BAD;
+        unit_add_to_cleanup_queue(u);
         return;
 
 good:
         return;
 
 good:
-        u->meta.gc_marker = gc_marker;
+        u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD;
 }
 
 static unsigned manager_dispatch_gc_queue(Manager *m) {
         Meta *meta;
         unsigned n = 0;
 }
 
 static unsigned manager_dispatch_gc_queue(Manager *m) {
         Meta *meta;
         unsigned n = 0;
-        int gc_marker;
+        unsigned gc_marker;
 
         assert(m);
 
 
         assert(m);
 
@@ -441,21 +469,26 @@ static unsigned manager_dispatch_gc_queue(Manager *m) {
 
         log_debug("Running GC...");
 
 
         log_debug("Running GC...");
 
+        m->gc_marker += _GC_OFFSET_MAX;
+        if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
+                m->gc_marker = 1;
+
         gc_marker = m->gc_marker;
         gc_marker = m->gc_marker;
-        m->gc_marker = MIN(0, m->gc_marker + 1);
 
         while ((meta = m->gc_queue)) {
                 assert(meta->in_gc_queue);
 
 
         while ((meta = m->gc_queue)) {
                 assert(meta->in_gc_queue);
 
+                unit_gc_sweep(UNIT(meta), gc_marker);
+
                 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
                 meta->in_gc_queue = false;
 
                 n++;
 
                 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
                 meta->in_gc_queue = false;
 
                 n++;
 
-                unit_gc_sweep(UNIT(meta), gc_marker);
-
-                if (meta->gc_marker == -gc_marker) {
+                if (meta->gc_marker == gc_marker + GC_OFFSET_BAD ||
+                    meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
                         log_debug("Collecting %s", meta->id);
                         log_debug("Collecting %s", meta->id);
+                        meta->gc_marker = gc_marker + GC_OFFSET_BAD;
                         unit_add_to_cleanup_queue(UNIT(meta));
                 }
         }
                         unit_add_to_cleanup_queue(UNIT(meta));
                 }
         }
@@ -1002,7 +1035,7 @@ static void transaction_collect_garbage(Manager *m) {
         } while (again);
 }
 
         } while (again);
 }
 
-static int transaction_is_destructive(Manager *m, JobMode mode) {
+static int transaction_is_destructive(Manager *m) {
         Iterator i;
         Job *j;
 
         Iterator i;
         Job *j;
 
@@ -1081,7 +1114,7 @@ static void transaction_minimize_impact(Manager *m) {
         } while (again);
 }
 
         } while (again);
 }
 
-static int transaction_apply(Manager *m, JobMode mode) {
+static int transaction_apply(Manager *m) {
         Iterator i;
         Job *j;
         int r;
         Iterator i;
         Job *j;
         int r;
@@ -1201,13 +1234,13 @@ static int transaction_activate(Manager *m, JobMode mode) {
 
         /* Ninth step: check whether we can actually apply this */
         if (mode == JOB_FAIL)
 
         /* Ninth step: check whether we can actually apply this */
         if (mode == JOB_FAIL)
-                if ((r = transaction_is_destructive(m, mode)) < 0) {
+                if ((r = transaction_is_destructive(m)) < 0) {
                         log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
                         goto rollback;
                 }
 
         /* Tenth step: apply changes */
                         log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
                         goto rollback;
                 }
 
         /* Tenth step: apply changes */
-        if ((r = transaction_apply(m, mode)) < 0) {
+        if ((r = transaction_apply(m)) < 0) {
                 log_debug("Failed to apply transaction: %s", strerror(-r));
                 goto rollback;
         }
                 log_debug("Failed to apply transaction: %s", strerror(-r));
                 goto rollback;
         }
@@ -1381,6 +1414,38 @@ fail:
         return r;
 }
 
         return r;
 }
 
+static int transaction_add_isolate_jobs(Manager *m) {
+        Iterator i;
+        Unit *u;
+        char *k;
+        int r;
+
+        assert(m);
+
+        HASHMAP_FOREACH_KEY(u, k, m->units, i) {
+
+                /* ignore aliases */
+                if (u->meta.id != k)
+                        continue;
+
+                if (UNIT_VTABLE(u)->no_isolate)
+                        continue;
+
+                /* No need to stop inactive jobs */
+                if (unit_active_state(u) == UNIT_INACTIVE)
+                        continue;
+
+                /* Is there already something listed for this? */
+                if (hashmap_get(m->transaction_jobs, u))
+                        continue;
+
+                if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, NULL)) < 0)
+                        log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
+        }
+
+        return 0;
+}
+
 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, Job **_ret) {
         int r;
         Job *ret;
 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, Job **_ret) {
         int r;
         Job *ret;
@@ -1390,6 +1455,9 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove
         assert(unit);
         assert(mode < _JOB_MODE_MAX);
 
         assert(unit);
         assert(mode < _JOB_MODE_MAX);
 
+        if (mode == JOB_ISOLATE && type != JOB_START)
+                return -EINVAL;
+
         log_debug("Trying to enqueue job %s/%s", unit->meta.id, job_type_to_string(type));
 
         if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, &ret)) < 0) {
         log_debug("Trying to enqueue job %s/%s", unit->meta.id, job_type_to_string(type));
 
         if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, &ret)) < 0) {
@@ -1397,6 +1465,12 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove
                 return r;
         }
 
                 return r;
         }
 
+        if (mode == JOB_ISOLATE)
+                if ((r = transaction_add_isolate_jobs(m)) < 0) {
+                        transaction_abort(m);
+                        return r;
+                }
+
         if ((r = transaction_activate(m, mode)) < 0)
                 return r;
 
         if ((r = transaction_activate(m, mode)) < 0)
                 return r;