Blame SOURCES/kvm-job-Add-job_sleep_ns.patch

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