[09a8006] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Michal Koutny
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[dda2602] | 29 | #include <adt/list.h>
|
---|
[694253c] | 30 | #include <assert.h>
|
---|
| 31 | #include <errno.h>
|
---|
[dda2602] | 32 | #include <stdlib.h>
|
---|
[694253c] | 33 |
|
---|
[dda2602] | 34 | #include "configuration.h"
|
---|
[3f7e1f24] | 35 | #include "dep.h"
|
---|
[694253c] | 36 | #include "job.h"
|
---|
[6efec7e3] | 37 | #include "log.h"
|
---|
[3f7e1f24] | 38 | #include "sysman.h"
|
---|
[694253c] | 39 |
|
---|
| 40 | static list_t job_queue;
|
---|
[c0c388d2] | 41 |
|
---|
[3f7e1f24] | 42 | /*
|
---|
| 43 | * Static functions
|
---|
| 44 | */
|
---|
[c0c388d2] | 45 |
|
---|
[dda2602] | 46 | static int job_add_blocked_job(job_t *blocking_job, job_t *blocked_job)
|
---|
[694253c] | 47 | {
|
---|
[dda2602] | 48 | int rc = dyn_array_append(&blocking_job->blocked_jobs, job_ptr_t,
|
---|
| 49 | blocked_job);
|
---|
[694253c] | 50 | if (rc != EOK) {
|
---|
[3f7e1f24] | 51 | return ENOMEM;
|
---|
[694253c] | 52 | }
|
---|
[3f7e1f24] | 53 | job_add_ref(blocked_job);
|
---|
[8432ae1] | 54 |
|
---|
[3f7e1f24] | 55 | blocked_job->blocking_jobs += 1;
|
---|
[694253c] | 56 |
|
---|
| 57 | return EOK;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[8432ae1] | 60 | /** Remove blocking_job from blocked job structure
|
---|
| 61 | *
|
---|
| 62 | * @note Caller must remove blocked_job from collection of blocked_jobs
|
---|
| 63 | */
|
---|
| 64 | static void job_unblock(job_t *blocked_job, job_t *blocking_job)
|
---|
| 65 | {
|
---|
| 66 | if (blocking_job->retval == JOB_FAILED) {
|
---|
| 67 | blocked_job->blocking_job_failed = true;
|
---|
| 68 | }
|
---|
| 69 | blocked_job->blocking_jobs -= 1;
|
---|
| 70 |
|
---|
| 71 | job_del_ref(&blocked_job);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[3f7e1f24] | 74 | static void job_init(job_t *job, unit_t *u, unit_state_t target_state)
|
---|
[694253c] | 75 | {
|
---|
[3f7e1f24] | 76 | assert(job);
|
---|
| 77 | assert(u);
|
---|
[dda2602] | 78 | assert(u->job == NULL);
|
---|
[5559712] | 79 | memset(job, 0, sizeof(*job));
|
---|
[694253c] | 80 |
|
---|
[3f7e1f24] | 81 | link_initialize(&job->job_queue);
|
---|
[694253c] | 82 |
|
---|
[8432ae1] | 83 | atomic_set(&job->refcnt, 0);
|
---|
[694253c] | 84 |
|
---|
[3f7e1f24] | 85 | job->target_state = target_state;
|
---|
| 86 | job->unit = u;
|
---|
[694253c] | 87 |
|
---|
[dda2602] | 88 | u->job = job;
|
---|
| 89 | job_add_ref(job);
|
---|
| 90 |
|
---|
[dd5c623] | 91 | dyn_array_initialize(&job->blocked_jobs, job_ptr_t);
|
---|
[3f7e1f24] | 92 | job->blocking_jobs = 0;
|
---|
| 93 | job->blocking_job_failed = false;
|
---|
[694253c] | 94 |
|
---|
[3f7e1f24] | 95 | job->state = JOB_UNQUEUED;
|
---|
| 96 | job->retval = JOB_UNDEFINED_;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | static bool job_eval_retval(job_t *job)
|
---|
| 100 | {
|
---|
| 101 | unit_t *u = job->unit;
|
---|
| 102 | if (u->state == job->target_state) {
|
---|
| 103 | job->retval = JOB_OK;
|
---|
| 104 | return true;
|
---|
| 105 | } else if (u->state == STATE_FAILED) {
|
---|
| 106 | job->retval = JOB_FAILED;
|
---|
| 107 | return true;
|
---|
| 108 | } else {
|
---|
| 109 | return false;
|
---|
[694253c] | 110 | }
|
---|
[3f7e1f24] | 111 | }
|
---|
[694253c] | 112 |
|
---|
[3f7e1f24] | 113 | static void job_check(void *object, void *data)
|
---|
| 114 | {
|
---|
| 115 | unit_t *u = object;
|
---|
| 116 | job_t *job = data;
|
---|
| 117 |
|
---|
[8432ae1] | 118 | /*
|
---|
| 119 | * We have one reference from caller for our disposal, *
|
---|
| 120 | * if needed, pass it to observer.
|
---|
| 121 | */
|
---|
[3f7e1f24] | 122 | if (job_eval_retval(job)) {
|
---|
| 123 | job_finish(job);
|
---|
[8432ae1] | 124 | job_del_ref(&job);
|
---|
[3f7e1f24] | 125 | } else {
|
---|
| 126 | // TODO place for timeout
|
---|
| 127 | sysman_object_observer(u, &job_check, job);
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
[694253c] | 130 |
|
---|
[3f7e1f24] | 131 | static void job_destroy(job_t **job_ptr)
|
---|
[694253c] | 132 | {
|
---|
[3f7e1f24] | 133 | job_t *job = *job_ptr;
|
---|
| 134 | if (job == NULL) {
|
---|
| 135 | return;
|
---|
[694253c] | 136 | }
|
---|
| 137 |
|
---|
[3f7e1f24] | 138 | assert(!link_used(&job->job_queue));
|
---|
[8432ae1] | 139 |
|
---|
| 140 | dyn_array_foreach(job->blocked_jobs, job_ptr_t, job_it) {
|
---|
| 141 | job_del_ref(&(*job_it));
|
---|
| 142 | }
|
---|
[3f7e1f24] | 143 | dyn_array_destroy(&job->blocked_jobs);
|
---|
| 144 |
|
---|
| 145 | free(job);
|
---|
| 146 | *job_ptr = NULL;
|
---|
[694253c] | 147 | }
|
---|
| 148 |
|
---|
[5559712] | 149 | static bool job_is_runnable(job_t *job)
|
---|
| 150 | {
|
---|
| 151 | return job->state == JOB_QUEUED && job->blocking_jobs == 0;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Pop next runnable job
|
---|
| 155 | *
|
---|
| 156 | * @return runnable job or NULL when there's none
|
---|
| 157 | */
|
---|
| 158 | static job_t *job_queue_pop_runnable(void)
|
---|
| 159 | {
|
---|
| 160 | job_t *result = NULL;
|
---|
| 161 |
|
---|
| 162 | /* Select first runnable job */
|
---|
| 163 | list_foreach(job_queue, job_queue, job_t, candidate) {
|
---|
| 164 | if (job_is_runnable(candidate)) {
|
---|
| 165 | result = candidate;
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | if (result) {
|
---|
| 170 | /* Remove job from queue and pass reference to caller */
|
---|
| 171 | list_remove(&result->job_queue);
|
---|
| 172 | result->state = JOB_DEQUEUED;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | return result;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[3f7e1f24] | 178 | /*
|
---|
| 179 | * Non-static functions
|
---|
| 180 | */
|
---|
| 181 |
|
---|
[694253c] | 182 | void job_queue_init()
|
---|
| 183 | {
|
---|
| 184 | list_initialize(&job_queue);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[3f7e1f24] | 187 | int job_queue_add_jobs(dyn_array_t *jobs)
|
---|
[694253c] | 188 | {
|
---|
| 189 | /* Check consistency with queue. */
|
---|
[3f7e1f24] | 190 | dyn_array_foreach(*jobs, job_ptr_t, new_job_it) {
|
---|
| 191 | list_foreach(job_queue, job_queue, job_t, queued_job) {
|
---|
[694253c] | 192 | /*
|
---|
| 193 | * Currently we have strict strategy not permitting
|
---|
[5559712] | 194 | * multiple jobs for one unit in the queue at a time.
|
---|
[694253c] | 195 | */
|
---|
[3f7e1f24] | 196 | if ((*new_job_it)->unit == queued_job->unit) {
|
---|
| 197 | sysman_log(LVL_ERROR,
|
---|
[5559712] | 198 | "Cannot queue multiple jobs for unit '%s'",
|
---|
[3f7e1f24] | 199 | unit_name((*new_job_it)->unit));
|
---|
[694253c] | 200 | return EEXIST;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /* Enqueue jobs */
|
---|
[3f7e1f24] | 206 | dyn_array_foreach(*jobs, job_ptr_t, job_it) {
|
---|
| 207 | (*job_it)->state = JOB_QUEUED;
|
---|
| 208 | list_append(&(*job_it)->job_queue, &job_queue);
|
---|
[8432ae1] | 209 | /* We pass reference from the closure to the queue */
|
---|
[694253c] | 210 | }
|
---|
| 211 |
|
---|
| 212 | return EOK;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[5559712] | 215 | /** Process all jobs that aren't transitively blocked
|
---|
[694253c] | 216 | *
|
---|
[5559712] | 217 | * Job can be blocked either by another job or by an incoming event, that will
|
---|
| 218 | * be queued after this job_queue_process call.
|
---|
| 219 | *
|
---|
| 220 | * TODO Write down rules from where this function can be called, to avoid stack
|
---|
| 221 | * overflow.
|
---|
[694253c] | 222 | */
|
---|
[5559712] | 223 | void job_queue_process(void)
|
---|
[694253c] | 224 | {
|
---|
[5559712] | 225 | job_t *job;
|
---|
| 226 | while ((job = job_queue_pop_runnable())) {
|
---|
| 227 | job_run(job);
|
---|
| 228 | job_del_ref(&job);
|
---|
[3f7e1f24] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | int job_create_closure(job_t *main_job, dyn_array_t *job_closure)
|
---|
| 233 | {
|
---|
| 234 | int rc;
|
---|
[dda2602] | 235 | list_t units_fifo;
|
---|
| 236 | list_initialize(&units_fifo);
|
---|
[3f7e1f24] | 237 |
|
---|
[dda2602] | 238 | /* Zero BFS tags before use */
|
---|
| 239 | list_foreach(units, units, unit_t, u) {
|
---|
| 240 | u->bfs_tag = false;
|
---|
| 241 | }
|
---|
[5559712] | 242 |
|
---|
[dda2602] | 243 | unit_t *unit = main_job->unit;
|
---|
| 244 | list_append(&unit->bfs_link, &units_fifo);
|
---|
| 245 | unit->bfs_tag = true;
|
---|
| 246 |
|
---|
| 247 | while (!list_empty(&units_fifo)) {
|
---|
| 248 | unit = list_get_instance(list_first(&units_fifo), unit_t,
|
---|
| 249 | bfs_link);
|
---|
| 250 | assert(unit->job);
|
---|
| 251 | list_remove(&unit->bfs_link);
|
---|
| 252 | job_t *job = unit->job;
|
---|
| 253 |
|
---|
| 254 |
|
---|
[5559712] | 255 | // TODO more sophisticated check? (unit that is in transitional
|
---|
| 256 | // state cannot have currently multiple jobs queued)
|
---|
[dda2602] | 257 | if (job->target_state == unit->state) {
|
---|
[5559712] | 258 | /*
|
---|
| 259 | * Job would do nothing, finish it on spot.
|
---|
| 260 | * No need to continue BFS search from it.
|
---|
| 261 | */
|
---|
| 262 | job->retval = JOB_OK;
|
---|
| 263 | job_finish(job);
|
---|
| 264 | continue;
|
---|
| 265 | }
|
---|
[3f7e1f24] | 266 |
|
---|
[dda2602] | 267 | job_add_ref(job);
|
---|
| 268 | dyn_array_append(job_closure, job_ptr_t, job);
|
---|
| 269 |
|
---|
| 270 | /*
|
---|
| 271 | * Traverse dependencies edges
|
---|
| 272 | * Depending on dependency type and edge direction create
|
---|
| 273 | * appropriate jobs.
|
---|
| 274 | */
|
---|
| 275 | list_foreach(unit->dependencies, dependencies, unit_dependency_t, dep) {
|
---|
| 276 | unit_t *u = dep->dependency;
|
---|
| 277 | job_t *blocking_job;
|
---|
| 278 | if (u->bfs_tag) {
|
---|
| 279 | assert(u->job);
|
---|
| 280 | blocking_job = u->job;
|
---|
| 281 | } else {
|
---|
| 282 | u->bfs_tag = true;
|
---|
| 283 | blocking_job = job_create(u, job->target_state);
|
---|
| 284 | if (blocking_job == NULL) {
|
---|
| 285 | rc = ENOMEM;
|
---|
| 286 | goto finish;
|
---|
| 287 | }
|
---|
| 288 | /* Reference to job is kept in unit */
|
---|
| 289 | job_del_ref(&blocking_job);
|
---|
| 290 | list_append(&u->bfs_link, &units_fifo);
|
---|
[3f7e1f24] | 291 | }
|
---|
[dda2602] | 292 |
|
---|
| 293 | job_add_blocked_job(blocking_job, job);
|
---|
[3f7e1f24] | 294 | }
|
---|
| 295 | }
|
---|
[dda2602] | 296 | sysman_log(LVL_DEBUG2, "%s(%s):", __func__, unit_name(main_job->unit));
|
---|
| 297 | dyn_array_foreach(*job_closure, job_ptr_t, job_it) {
|
---|
| 298 | sysman_log(LVL_DEBUG2, "%s\t%s", __func__, unit_name((*job_it)->unit));
|
---|
| 299 | }
|
---|
[3f7e1f24] | 300 | rc = EOK;
|
---|
| 301 |
|
---|
| 302 | finish:
|
---|
[dda2602] | 303 | /* Any unprocessed jobs may be referenced by units */
|
---|
| 304 | list_foreach(units_fifo, bfs_link, unit_t, u) {
|
---|
| 305 | job_del_ref(&u->job);
|
---|
| 306 | }
|
---|
[694253c] | 307 | return rc;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[dda2602] | 310 | /** Create job assigned to the unit
|
---|
| 311 | *
|
---|
| 312 | * @param[in] unit unit to be modified, its job must be empty
|
---|
| 313 | * @param[in] target_state
|
---|
| 314 | *
|
---|
| 315 | * @return NULL or newly created job
|
---|
| 316 | * There are two references to the job, one set in the unit and second
|
---|
| 317 | * is the return value.
|
---|
| 318 | */
|
---|
[3f7e1f24] | 319 | job_t *job_create(unit_t *u, unit_state_t target_state)
|
---|
| 320 | {
|
---|
| 321 | job_t *job = malloc(sizeof(job_t));
|
---|
| 322 | if (job != NULL) {
|
---|
| 323 | job_init(job, u, target_state);
|
---|
[8432ae1] | 324 |
|
---|
[dda2602] | 325 | /* Add one reference for the creator */
|
---|
[8432ae1] | 326 | job_add_ref(job);
|
---|
[3f7e1f24] | 327 | }
|
---|
| 328 |
|
---|
| 329 | return job;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[8432ae1] | 332 | /** Add one reference to job
|
---|
| 333 | *
|
---|
| 334 | * Usage:
|
---|
| 335 | * - adding observer which references the job,
|
---|
| 336 | * - raising and event that references the job,
|
---|
| 337 | * - anytime any other new reference is made.
|
---|
| 338 | */
|
---|
[c0c388d2] | 339 | void job_add_ref(job_t *job)
|
---|
| 340 | {
|
---|
| 341 | atomic_inc(&job->refcnt);
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[8432ae1] | 344 | /** Remove one reference from job, last remover destroys the job
|
---|
| 345 | *
|
---|
| 346 | * Usage:
|
---|
| 347 | * - inside observer callback that references the job,
|
---|
| 348 | * - inside event handler that references the job,
|
---|
| 349 | * - anytime you dispose a reference to the job.
|
---|
| 350 | */
|
---|
[c0c388d2] | 351 | void job_del_ref(job_t **job_ptr)
|
---|
| 352 | {
|
---|
| 353 | job_t *job = *job_ptr;
|
---|
| 354 |
|
---|
[8432ae1] | 355 | assert(job != NULL);
|
---|
[c0c388d2] | 356 | assert(atomic_get(&job->refcnt) > 0);
|
---|
| 357 | if (atomic_predec(&job->refcnt) == 0) {
|
---|
| 358 | job_destroy(job_ptr);
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[3f7e1f24] | 362 | void job_run(job_t *job)
|
---|
[694253c] | 363 | {
|
---|
[3f7e1f24] | 364 | assert(job->state != JOB_RUNNING);
|
---|
| 365 | assert(job->state != JOB_FINISHED);
|
---|
[694253c] | 366 |
|
---|
[3f7e1f24] | 367 | unit_t *u = job->unit;
|
---|
[8432ae1] | 368 | sysman_log(LVL_DEBUG, "%s(%p), %s -> %i",
|
---|
| 369 | __func__, job, unit_name(u), job->target_state);
|
---|
[694253c] | 370 |
|
---|
[3f7e1f24] | 371 | /* Propagate failure */
|
---|
| 372 | if (job->blocking_job_failed) {
|
---|
| 373 | goto fail;
|
---|
[694253c] | 374 | }
|
---|
| 375 |
|
---|
[3f7e1f24] | 376 | int rc;
|
---|
| 377 | switch (job->target_state) {
|
---|
| 378 | case STATE_STARTED:
|
---|
| 379 | rc = unit_start(u);
|
---|
| 380 | break;
|
---|
| 381 | default:
|
---|
| 382 | // TODO implement other states
|
---|
| 383 | assert(false);
|
---|
| 384 | }
|
---|
| 385 | if (rc != EOK) {
|
---|
[dda2602] | 386 | sysman_log(LVL_DEBUG, "%s(%p), %s -> %i, error: %i",
|
---|
| 387 | __func__, job, unit_name(u), job->target_state, rc);
|
---|
[3f7e1f24] | 388 | goto fail;
|
---|
[4fe7fcb] | 389 | }
|
---|
| 390 |
|
---|
[8432ae1] | 391 | /*
|
---|
| 392 | * job_check deletes reference, we want job to remain to caller, thus
|
---|
| 393 | * add one dummy ref
|
---|
| 394 | */
|
---|
| 395 | job_add_ref(job);
|
---|
[3f7e1f24] | 396 | job_check(job->unit, job);
|
---|
| 397 | return;
|
---|
[4fe7fcb] | 398 |
|
---|
[3f7e1f24] | 399 | fail:
|
---|
| 400 | job->retval = JOB_FAILED;
|
---|
| 401 | job_finish(job);
|
---|
[4fe7fcb] | 402 | }
|
---|
| 403 |
|
---|
[3f7e1f24] | 404 | /** Unblocks blocked jobs and notify observers
|
---|
| 405 | *
|
---|
| 406 | * @param[in] job job with defined return value
|
---|
| 407 | */
|
---|
| 408 | void job_finish(job_t *job)
|
---|
[694253c] | 409 | {
|
---|
[3f7e1f24] | 410 | assert(job->state != JOB_FINISHED);
|
---|
| 411 | assert(job->retval != JOB_UNDEFINED_);
|
---|
[dda2602] | 412 | assert(job->unit->job == job);
|
---|
[3f7e1f24] | 413 |
|
---|
[8432ae1] | 414 | sysman_log(LVL_DEBUG2, "%s(%p) %s -> %i",
|
---|
| 415 | __func__, job, unit_name(job->unit), job->retval);
|
---|
[3f7e1f24] | 416 |
|
---|
| 417 | job->state = JOB_FINISHED;
|
---|
[694253c] | 418 |
|
---|
[8432ae1] | 419 | /* First remove references, then clear the array */
|
---|
[3f7e1f24] | 420 | dyn_array_foreach(job->blocked_jobs, job_ptr_t, job_it) {
|
---|
| 421 | job_unblock(*job_it, job);
|
---|
[694253c] | 422 | }
|
---|
[8432ae1] | 423 | dyn_array_clear(&job->blocked_jobs);
|
---|
[694253c] | 424 |
|
---|
[dda2602] | 425 | /*
|
---|
| 426 | * Remove job from unit and pass the reference from the unit to the
|
---|
| 427 | * event.
|
---|
| 428 | */
|
---|
| 429 | job->unit->job = NULL;
|
---|
[5559712] | 430 | sysman_raise_event(&sysman_event_job_finished, job);
|
---|
[694253c] | 431 | }
|
---|
[3f7e1f24] | 432 |
|
---|