26ba25
From 5da39ce2527a4c7e64543f1b0a3733a30cedbc9d Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Tue, 26 Jun 2018 09:48:10 +0200
26ba25
Subject: [PATCH 102/268] job: Add job_sleep_ns()
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20180626094856.6924-28-kwolf@redhat.com>
26ba25
Patchwork-id: 81117
26ba25
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 27/73] job: Add job_sleep_ns()
26ba25
Bugzilla: 1513543
26ba25
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Fam Zheng <famz@redhat.com>
26ba25
26ba25
There is nothing block layer specific about block_job_sleep_ns(), so
26ba25
move the function to Job.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: John Snow <jsnow@redhat.com>
26ba25
Reviewed-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 5d43e86e11f488fda7956b13160e0c0105a84845)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block/backup.c               |  2 +-
26ba25
 block/commit.c               |  2 +-
26ba25
 block/mirror.c               |  4 ++--
26ba25
 block/stream.c               |  2 +-
26ba25
 blockjob.c                   | 27 ---------------------------
26ba25
 include/block/blockjob_int.h | 11 -----------
26ba25
 include/qemu/job.h           | 19 ++++++++++++++++++-
26ba25
 job.c                        | 32 ++++++++++++++++++++++++++++++++
26ba25
 tests/test-bdrv-drain.c      |  8 ++++----
26ba25
 tests/test-blockjob-txn.c    |  2 +-
26ba25
 tests/test-blockjob.c        |  2 +-
26ba25
 11 files changed, 61 insertions(+), 50 deletions(-)
26ba25
26ba25
diff --git a/block/backup.c b/block/backup.c
26ba25
index 7d9aad9..f3a4f7c 100644
26ba25
--- a/block/backup.c
26ba25
+++ b/block/backup.c
26ba25
@@ -338,7 +338,7 @@ static bool coroutine_fn yield_and_check(BackupBlockJob *job)
26ba25
      * return. Without a yield, the VM would not reboot. */
26ba25
     delay_ns = block_job_ratelimit_get_delay(&job->common, job->bytes_read);
26ba25
     job->bytes_read = 0;
26ba25
-    block_job_sleep_ns(&job->common, delay_ns);
26ba25
+    job_sleep_ns(&job->common.job, delay_ns);
26ba25
 
26ba25
     if (job_is_cancelled(&job->common.job)) {
26ba25
         return true;
26ba25
diff --git a/block/commit.c b/block/commit.c
26ba25
index 2fbc310..1c6cb6c 100644
26ba25
--- a/block/commit.c
26ba25
+++ b/block/commit.c
26ba25
@@ -172,7 +172,7 @@ static void coroutine_fn commit_run(void *opaque)
26ba25
         /* Note that even when no rate limit is applied we need to yield
26ba25
          * with no pending I/O here so that bdrv_drain_all() returns.
26ba25
          */
26ba25
-        block_job_sleep_ns(&s->common, delay_ns);
26ba25
+        job_sleep_ns(&s->common.job, delay_ns);
26ba25
         if (job_is_cancelled(&s->common.job)) {
26ba25
             break;
26ba25
         }
26ba25
diff --git a/block/mirror.c b/block/mirror.c
26ba25
index 95fc807..5d8f75c 100644
26ba25
--- a/block/mirror.c
26ba25
+++ b/block/mirror.c
26ba25
@@ -595,7 +595,7 @@ static void mirror_throttle(MirrorBlockJob *s)
26ba25
 
26ba25
     if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) {
26ba25
         s->last_pause_ns = now;
26ba25
-        block_job_sleep_ns(&s->common, 0);
26ba25
+        job_sleep_ns(&s->common.job, 0);
26ba25
     } else {
26ba25
         job_pause_point(&s->common.job);
26ba25
     }
26ba25
@@ -869,7 +869,7 @@ static void coroutine_fn mirror_run(void *opaque)
26ba25
                         cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0);
26ba25
         }
26ba25
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
26ba25
-        block_job_sleep_ns(&s->common, delay_ns);
26ba25
+        job_sleep_ns(&s->common.job, delay_ns);
26ba25
         if (job_is_cancelled(&s->common.job) &&
26ba25
             (!s->synced || s->common.force))
26ba25
         {
26ba25
diff --git a/block/stream.c b/block/stream.c
26ba25
index 6d8b7b6..1faab02 100644
26ba25
--- a/block/stream.c
26ba25
+++ b/block/stream.c
26ba25
@@ -140,7 +140,7 @@ static void coroutine_fn stream_run(void *opaque)
26ba25
         /* Note that even when no rate limit is applied we need to yield
26ba25
          * with no pending I/O here so that bdrv_drain_all() returns.
26ba25
          */
26ba25
-        block_job_sleep_ns(&s->common, delay_ns);
26ba25
+        job_sleep_ns(&s->common.job, delay_ns);
26ba25
         if (job_is_cancelled(&s->common.job)) {
26ba25
             break;
26ba25
         }
26ba25
diff --git a/blockjob.c b/blockjob.c
26ba25
index 313b1ff..4dc360c 100644
26ba25
--- a/blockjob.c
26ba25
+++ b/blockjob.c
26ba25
@@ -181,7 +181,6 @@ void block_job_free(Job *job)
26ba25
                                     block_job_detach_aio_context, bjob);
26ba25
     blk_unref(bjob->blk);
26ba25
     error_free(bjob->blocker);
26ba25
-    assert(!timer_pending(&bjob->job.sleep_timer));
26ba25
 }
26ba25
 
26ba25
 static void block_job_attached_aio_context(AioContext *new_context,
26ba25
@@ -290,13 +289,6 @@ const BlockJobDriver *block_job_driver(BlockJob *job)
26ba25
     return job->driver;
26ba25
 }
26ba25
 
26ba25
-static void block_job_sleep_timer_cb(void *opaque)
26ba25
-{
26ba25
-    BlockJob *job = opaque;
26ba25
-
26ba25
-    block_job_enter(job);
26ba25
-}
26ba25
-
26ba25
 static void block_job_decommission(BlockJob *job)
26ba25
 {
26ba25
     assert(job);
26ba25
@@ -866,9 +858,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
     job->opaque        = opaque;
26ba25
     job->auto_finalize = !(flags & BLOCK_JOB_MANUAL_FINALIZE);
26ba25
     job->auto_dismiss  = !(flags & BLOCK_JOB_MANUAL_DISMISS);
26ba25
-    aio_timer_init(qemu_get_aio_context(), &job->job.sleep_timer,
26ba25
-                   QEMU_CLOCK_REALTIME, SCALE_NS,
26ba25
-                   block_job_sleep_timer_cb, job);
26ba25
 
26ba25
     error_setg(&job->blocker, "block device is in use by block job: %s",
26ba25
                job_type_str(&job->job));
26ba25
@@ -931,22 +920,6 @@ void block_job_enter(BlockJob *job)
26ba25
     job_enter_cond(&job->job, NULL);
26ba25
 }
26ba25
 
26ba25
-void block_job_sleep_ns(BlockJob *job, int64_t ns)
26ba25
-{
26ba25
-    assert(job->job.busy);
26ba25
-
26ba25
-    /* Check cancellation *before* setting busy = false, too!  */
26ba25
-    if (job_is_cancelled(&job->job)) {
26ba25
-        return;
26ba25
-    }
26ba25
-
26ba25
-    if (!job_should_pause(&job->job)) {
26ba25
-        job_do_yield(&job->job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
26ba25
-    }
26ba25
-
26ba25
-    job_pause_point(&job->job);
26ba25
-}
26ba25
-
26ba25
 void block_job_yield(BlockJob *job)
26ba25
 {
26ba25
     assert(job->job.busy);
26ba25
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
26ba25
index 0a614a8..8937f5b 100644
26ba25
--- a/include/block/blockjob_int.h
26ba25
+++ b/include/block/blockjob_int.h
26ba25
@@ -134,17 +134,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
 void block_job_free(Job *job);
26ba25
 
26ba25
 /**
26ba25
- * block_job_sleep_ns:
26ba25
- * @job: The job that calls the function.
26ba25
- * @ns: How many nanoseconds to stop for.
26ba25
- *
26ba25
- * Put the job to sleep (assuming that it wasn't canceled) for @ns
26ba25
- * %QEMU_CLOCK_REALTIME nanoseconds.  Canceling the job will immediately
26ba25
- * interrupt the wait.
26ba25
- */
26ba25
-void block_job_sleep_ns(BlockJob *job, int64_t ns);
26ba25
-
26ba25
-/**
26ba25
  * block_job_yield:
26ba25
  * @job: The job that calls the function.
26ba25
  *
26ba25
diff --git a/include/qemu/job.h b/include/qemu/job.h
26ba25
index 9dcff12..509408f 100644
26ba25
--- a/include/qemu/job.h
26ba25
+++ b/include/qemu/job.h
26ba25
@@ -58,7 +58,7 @@ typedef struct Job {
26ba25
     Coroutine *co;
26ba25
 
26ba25
     /**
26ba25
-     * Timer that is used by @block_job_sleep_ns. Accessed under job_mutex (in
26ba25
+     * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
26ba25
      * job.c).
26ba25
      */
26ba25
     QEMUTimer sleep_timer;
26ba25
@@ -168,6 +168,13 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job));
26ba25
 void job_start(Job *job);
26ba25
 
26ba25
 /**
26ba25
+ * @job: The job to enter.
26ba25
+ *
26ba25
+ * Continue the specified job by entering the coroutine.
26ba25
+ */
26ba25
+void job_enter(Job *job);
26ba25
+
26ba25
+/**
26ba25
  * @job: The job that is ready to pause.
26ba25
  *
26ba25
  * Pause now if job_pause() has been called. Jobs that perform lots of I/O
26ba25
@@ -175,6 +182,16 @@ void job_start(Job *job);
26ba25
  */
26ba25
 void coroutine_fn job_pause_point(Job *job);
26ba25
 
26ba25
+/**
26ba25
+ * @job: The job that calls the function.
26ba25
+ * @ns: How many nanoseconds to stop for.
26ba25
+ *
26ba25
+ * Put the job to sleep (assuming that it wasn't canceled) for @ns
26ba25
+ * %QEMU_CLOCK_REALTIME nanoseconds.  Canceling the job will immediately
26ba25
+ * interrupt the wait.
26ba25
+ */
26ba25
+void coroutine_fn job_sleep_ns(Job *job, int64_t ns);
26ba25
+
26ba25
 
26ba25
 /** Returns the JobType of a given Job. */
26ba25
 JobType job_type(const Job *job);
26ba25
diff --git a/job.c b/job.c
26ba25
index 78497fd..1b8cba1 100644
26ba25
--- a/job.c
26ba25
+++ b/job.c
26ba25
@@ -152,6 +152,13 @@ Job *job_get(const char *id)
26ba25
     return NULL;
26ba25
 }
26ba25
 
26ba25
+static void job_sleep_timer_cb(void *opaque)
26ba25
+{
26ba25
+    Job *job = opaque;
26ba25
+
26ba25
+    job_enter(job);
26ba25
+}
26ba25
+
26ba25
 void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
26ba25
                  Error **errp)
26ba25
 {
26ba25
@@ -178,6 +185,9 @@ void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
26ba25
     job->pause_count   = 1;
26ba25
 
26ba25
     job_state_transition(job, JOB_STATUS_CREATED);
26ba25
+    aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
26ba25
+                   QEMU_CLOCK_REALTIME, SCALE_NS,
26ba25
+                   job_sleep_timer_cb, job);
26ba25
 
26ba25
     QLIST_INSERT_HEAD(&jobs, job, job_list);
26ba25
 
26ba25
@@ -193,6 +203,7 @@ void job_unref(Job *job)
26ba25
 {
26ba25
     if (--job->refcnt == 0) {
26ba25
         assert(job->status == JOB_STATUS_NULL);
26ba25
+        assert(!timer_pending(&job->sleep_timer));
26ba25
 
26ba25
         if (job->driver->free) {
26ba25
             job->driver->free(job);
26ba25
@@ -232,6 +243,11 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job))
26ba25
     aio_co_wake(job->co);
26ba25
 }
26ba25
 
26ba25
+void job_enter(Job *job)
26ba25
+{
26ba25
+    job_enter_cond(job, NULL);
26ba25
+}
26ba25
+
26ba25
 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
26ba25
  * Reentering the job coroutine with block_job_enter() before the timer has
26ba25
  * expired is allowed and cancels the timer.
26ba25
@@ -283,6 +299,22 @@ void coroutine_fn job_pause_point(Job *job)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
+void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
26ba25
+{
26ba25
+    assert(job->busy);
26ba25
+
26ba25
+    /* Check cancellation *before* setting busy = false, too!  */
26ba25
+    if (job_is_cancelled(job)) {
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    if (!job_should_pause(job)) {
26ba25
+        job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
26ba25
+    }
26ba25
+
26ba25
+    job_pause_point(job);
26ba25
+}
26ba25
+
26ba25
 /**
26ba25
  * All jobs must allow a pause point before entering their job proper. This
26ba25
  * ensures that jobs can be paused prior to being started, then resumed later.
26ba25
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
26ba25
index c9f2f9b..50232f5 100644
26ba25
--- a/tests/test-bdrv-drain.c
26ba25
+++ b/tests/test-bdrv-drain.c
26ba25
@@ -508,7 +508,7 @@ static void coroutine_fn test_job_start(void *opaque)
26ba25
 
26ba25
     block_job_event_ready(&s->common);
26ba25
     while (!s->should_complete) {
26ba25
-        block_job_sleep_ns(&s->common, 100000);
26ba25
+        job_sleep_ns(&s->common.job, 100000);
26ba25
     }
26ba25
 
26ba25
     job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
26ba25
@@ -553,7 +553,7 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
 
26ba25
     g_assert_cmpint(job->job.pause_count, ==, 0);
26ba25
     g_assert_false(job->job.paused);
26ba25
-    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
26ba25
+    g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
26ba25
 
26ba25
     do_drain_begin(drain_type, src);
26ba25
 
26ba25
@@ -571,7 +571,7 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
 
26ba25
     g_assert_cmpint(job->job.pause_count, ==, 0);
26ba25
     g_assert_false(job->job.paused);
26ba25
-    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
26ba25
+    g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
26ba25
 
26ba25
     do_drain_begin(drain_type, target);
26ba25
 
26ba25
@@ -589,7 +589,7 @@ static void test_blockjob_common(enum drain_type drain_type)
26ba25
 
26ba25
     g_assert_cmpint(job->job.pause_count, ==, 0);
26ba25
     g_assert_false(job->job.paused);
26ba25
-    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
26ba25
+    g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
26ba25
 
26ba25
     ret = block_job_complete_sync(job, &error_abort);
26ba25
     g_assert_cmpint(ret, ==, 0);
26ba25
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
26ba25
index 323e154..0e6162b 100644
26ba25
--- a/tests/test-blockjob-txn.c
26ba25
+++ b/tests/test-blockjob-txn.c
26ba25
@@ -45,7 +45,7 @@ static void coroutine_fn test_block_job_run(void *opaque)
26ba25
 
26ba25
     while (s->iterations--) {
26ba25
         if (s->use_timer) {
26ba25
-            block_job_sleep_ns(job, 0);
26ba25
+            job_sleep_ns(&job->job, 0);
26ba25
         } else {
26ba25
             block_job_yield(job);
26ba25
         }
26ba25
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
26ba25
index 1d18325..b329bd5 100644
26ba25
--- a/tests/test-blockjob.c
26ba25
+++ b/tests/test-blockjob.c
26ba25
@@ -188,7 +188,7 @@ static void coroutine_fn cancel_job_start(void *opaque)
26ba25
             block_job_event_ready(&s->common);
26ba25
         }
26ba25
 
26ba25
-        block_job_sleep_ns(&s->common, 100000);
26ba25
+        job_sleep_ns(&s->common.job, 100000);
26ba25
     }
26ba25
 
26ba25
  defer:
26ba25
-- 
26ba25
1.8.3.1
26ba25