2 * Background jobs (long-running operations)
4 * Copyright (c) 2011 IBM Corp.
5 * Copyright (c) 2012, 2018 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "qapi/error.h"
31 #include "qemu/main-loop.h"
32 #include "trace-root.h"
34 static QLIST_HEAD(, Job
) jobs
= QLIST_HEAD_INITIALIZER(jobs
);
36 /* Job State Transition Table */
37 bool JobSTT
[JOB_STATUS__MAX
][JOB_STATUS__MAX
] = {
38 /* U, C, R, P, Y, S, W, D, X, E, N */
39 /* U: */ [JOB_STATUS_UNDEFINED
] = {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
40 /* C: */ [JOB_STATUS_CREATED
] = {0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
41 /* R: */ [JOB_STATUS_RUNNING
] = {0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0},
42 /* P: */ [JOB_STATUS_PAUSED
] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
43 /* Y: */ [JOB_STATUS_READY
] = {0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0},
44 /* S: */ [JOB_STATUS_STANDBY
] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
45 /* W: */ [JOB_STATUS_WAITING
] = {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
46 /* D: */ [JOB_STATUS_PENDING
] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
47 /* X: */ [JOB_STATUS_ABORTING
] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
48 /* E: */ [JOB_STATUS_CONCLUDED
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
49 /* N: */ [JOB_STATUS_NULL
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
52 bool JobVerbTable
[JOB_VERB__MAX
][JOB_STATUS__MAX
] = {
53 /* U, C, R, P, Y, S, W, D, X, E, N */
54 [JOB_VERB_CANCEL
] = {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
55 [JOB_VERB_PAUSE
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
56 [JOB_VERB_RESUME
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
57 [JOB_VERB_SET_SPEED
] = {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
58 [JOB_VERB_COMPLETE
] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
59 [JOB_VERB_FINALIZE
] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
60 [JOB_VERB_DISMISS
] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
63 /* Right now, this mutex is only needed to synchronize accesses to job->busy
64 * and job->sleep_timer, such as concurrent calls to job_do_yield and
66 static QemuMutex job_mutex
;
68 static void job_lock(void)
70 qemu_mutex_lock(&job_mutex
);
73 static void job_unlock(void)
75 qemu_mutex_unlock(&job_mutex
);
78 static void __attribute__((__constructor__
)) job_init(void)
80 qemu_mutex_init(&job_mutex
);
83 /* TODO Make static once the whole state machine is in job.c */
84 void job_state_transition(Job
*job
, JobStatus s1
)
86 JobStatus s0
= job
->status
;
87 assert(s1
>= 0 && s1
<= JOB_STATUS__MAX
);
88 trace_job_state_transition(job
, /* TODO re-enable: job->ret */ 0,
89 JobSTT
[s0
][s1
] ? "allowed" : "disallowed",
90 JobStatus_str(s0
), JobStatus_str(s1
));
91 assert(JobSTT
[s0
][s1
]);
95 int job_apply_verb(Job
*job
, JobVerb verb
, Error
**errp
)
97 JobStatus s0
= job
->status
;
98 assert(verb
>= 0 && verb
<= JOB_VERB__MAX
);
99 trace_job_apply_verb(job
, JobStatus_str(s0
), JobVerb_str(verb
),
100 JobVerbTable
[verb
][s0
] ? "allowed" : "prohibited");
101 if (JobVerbTable
[verb
][s0
]) {
104 error_setg(errp
, "Job '%s' in state '%s' cannot accept command verb '%s'",
105 job
->id
, JobStatus_str(s0
), JobVerb_str(verb
));
109 JobType
job_type(const Job
*job
)
111 return job
->driver
->job_type
;
114 const char *job_type_str(const Job
*job
)
116 return JobType_str(job_type(job
));
119 bool job_is_cancelled(Job
*job
)
121 return job
->cancelled
;
124 bool job_is_completed(Job
*job
)
126 switch (job
->status
) {
127 case JOB_STATUS_UNDEFINED
:
128 case JOB_STATUS_CREATED
:
129 case JOB_STATUS_RUNNING
:
130 case JOB_STATUS_PAUSED
:
131 case JOB_STATUS_READY
:
132 case JOB_STATUS_STANDBY
:
134 case JOB_STATUS_WAITING
:
135 case JOB_STATUS_PENDING
:
136 case JOB_STATUS_ABORTING
:
137 case JOB_STATUS_CONCLUDED
:
138 case JOB_STATUS_NULL
:
141 g_assert_not_reached();
146 bool job_started(Job
*job
)
151 bool job_should_pause(Job
*job
)
153 return job
->pause_count
> 0;
156 Job
*job_next(Job
*job
)
159 return QLIST_FIRST(&jobs
);
161 return QLIST_NEXT(job
, job_list
);
164 Job
*job_get(const char *id
)
168 QLIST_FOREACH(job
, &jobs
, job_list
) {
169 if (job
->id
&& !strcmp(id
, job
->id
)) {
177 static void job_sleep_timer_cb(void *opaque
)
184 void *job_create(const char *job_id
, const JobDriver
*driver
, AioContext
*ctx
,
190 if (!id_wellformed(job_id
)) {
191 error_setg(errp
, "Invalid job ID '%s'", job_id
);
194 if (job_get(job_id
)) {
195 error_setg(errp
, "Job ID '%s' already in use", job_id
);
200 job
= g_malloc0(driver
->instance_size
);
201 job
->driver
= driver
;
202 job
->id
= g_strdup(job_id
);
204 job
->aio_context
= ctx
;
207 job
->pause_count
= 1;
209 job_state_transition(job
, JOB_STATUS_CREATED
);
210 aio_timer_init(qemu_get_aio_context(), &job
->sleep_timer
,
211 QEMU_CLOCK_REALTIME
, SCALE_NS
,
212 job_sleep_timer_cb
, job
);
214 QLIST_INSERT_HEAD(&jobs
, job
, job_list
);
219 void job_ref(Job
*job
)
224 void job_unref(Job
*job
)
226 if (--job
->refcnt
== 0) {
227 assert(job
->status
== JOB_STATUS_NULL
);
228 assert(!timer_pending(&job
->sleep_timer
));
230 if (job
->driver
->free
) {
231 job
->driver
->free(job
);
234 QLIST_REMOVE(job
, job_list
);
241 void job_enter_cond(Job
*job
, bool(*fn
)(Job
*job
))
243 if (!job_started(job
)) {
246 if (job
->deferred_to_main_loop
) {
256 if (fn
&& !fn(job
)) {
261 assert(!job
->deferred_to_main_loop
);
262 timer_del(&job
->sleep_timer
);
265 aio_co_wake(job
->co
);
268 void job_enter(Job
*job
)
270 job_enter_cond(job
, NULL
);
273 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
274 * Reentering the job coroutine with block_job_enter() before the timer has
275 * expired is allowed and cancels the timer.
277 * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
278 * called explicitly. */
279 void coroutine_fn
job_do_yield(Job
*job
, uint64_t ns
)
283 timer_mod(&job
->sleep_timer
, ns
);
287 qemu_coroutine_yield();
289 /* Set by job_enter_cond() before re-entering the coroutine. */
293 void coroutine_fn
job_pause_point(Job
*job
)
295 assert(job
&& job_started(job
));
297 if (!job_should_pause(job
)) {
300 if (job_is_cancelled(job
)) {
304 if (job
->driver
->pause
) {
305 job
->driver
->pause(job
);
308 if (job_should_pause(job
) && !job_is_cancelled(job
)) {
309 JobStatus status
= job
->status
;
310 job_state_transition(job
, status
== JOB_STATUS_READY
312 : JOB_STATUS_PAUSED
);
314 job_do_yield(job
, -1);
316 job_state_transition(job
, status
);
319 if (job
->driver
->resume
) {
320 job
->driver
->resume(job
);
324 void coroutine_fn
job_sleep_ns(Job
*job
, int64_t ns
)
328 /* Check cancellation *before* setting busy = false, too! */
329 if (job_is_cancelled(job
)) {
333 if (!job_should_pause(job
)) {
334 job_do_yield(job
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + ns
);
337 job_pause_point(job
);
341 * All jobs must allow a pause point before entering their job proper. This
342 * ensures that jobs can be paused prior to being started, then resumed later.
344 static void coroutine_fn
job_co_entry(void *opaque
)
348 assert(job
&& job
->driver
&& job
->driver
->start
);
349 job_pause_point(job
);
350 job
->driver
->start(job
);
354 void job_start(Job
*job
)
356 assert(job
&& !job_started(job
) && job
->paused
&&
357 job
->driver
&& job
->driver
->start
);
358 job
->co
= qemu_coroutine_create(job_co_entry
, job
);
362 job_state_transition(job
, JOB_STATUS_RUNNING
);
363 aio_co_enter(job
->aio_context
, job
->co
);
366 /* Assumes the block_job_mutex is held */
367 static bool job_timer_not_pending(Job
*job
)
369 return !timer_pending(&job
->sleep_timer
);
372 void job_pause(Job
*job
)
377 void job_resume(Job
*job
)
379 assert(job
->pause_count
> 0);
381 if (job
->pause_count
) {
385 /* kick only if no timer is pending */
386 job_enter_cond(job
, job_timer_not_pending
);
389 void job_user_pause(Job
*job
, Error
**errp
)
391 if (job_apply_verb(job
, JOB_VERB_PAUSE
, errp
)) {
394 if (job
->user_paused
) {
395 error_setg(errp
, "Job is already paused");
398 job
->user_paused
= true;
402 bool job_user_paused(Job
*job
)
404 return job
->user_paused
;
407 void job_user_resume(Job
*job
, Error
**errp
)
410 if (!job
->user_paused
|| job
->pause_count
<= 0) {
411 error_setg(errp
, "Can't resume a job that was not paused");
414 if (job_apply_verb(job
, JOB_VERB_RESUME
, errp
)) {
417 if (job
->driver
->user_resume
) {
418 job
->driver
->user_resume(job
);
420 job
->user_paused
= false;
427 JobDeferToMainLoopFn
*fn
;
429 } JobDeferToMainLoopData
;
431 static void job_defer_to_main_loop_bh(void *opaque
)
433 JobDeferToMainLoopData
*data
= opaque
;
434 Job
*job
= data
->job
;
435 AioContext
*aio_context
= job
->aio_context
;
437 aio_context_acquire(aio_context
);
438 data
->fn(data
->job
, data
->opaque
);
439 aio_context_release(aio_context
);
444 void job_defer_to_main_loop(Job
*job
, JobDeferToMainLoopFn
*fn
, void *opaque
)
446 JobDeferToMainLoopData
*data
= g_malloc(sizeof(*data
));
449 data
->opaque
= opaque
;
450 job
->deferred_to_main_loop
= true;
452 aio_bh_schedule_oneshot(qemu_get_aio_context(),
453 job_defer_to_main_loop_bh
, data
);