Skip to contentSkip to conversation

block-copy: make helper APIs thread safe

PATCH In review Emanuele Giuseppe Esposito 1 participant 4 messages Jun 14, 2021 Block layer

lore
Share & tools
lore
#1 lore raw

PATCH v3 1/5 ratelimit: treat zero speed as unlimited

In reply to Emanuele Giuseppe Esposito

From: Paolo Bonzini <[email protected]>

Both users of RateLimit, block-copy.c and blockjob.c, treat a speed of zero as unlimited, while RateLimit treats it as "as slow as possible". The latter is nicer from the code point of view but pretty useless, so disable rate limiting if a speed of zero is provided.

---
 include/qemu/ratelimit.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/qemu/ratelimit.h +10 -2
index 003ea6d5a3..48bf59e857 100644
@@ -43,7 +43,11 @@ static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
     double delay_slices;
 
     QEMU_LOCK_GUARD(&limit->lock);
-    assert(limit->slice_quota && limit->slice_ns);
+    if (!limit->slice_quota) {
+        /* Throttling disabled.  */
+        return 0;
+    }
+    assert(limit->slice_ns);
 
     if (limit->slice_end_time < now) {
         /* Previous, possibly extended, time slice finished; reset the
@@ -83,7 +87,11 @@ static inline void ratelimit_set_speed(RateLimit *limit, uint64_t speed,
 {
     QEMU_LOCK_GUARD(&limit->lock);
     limit->slice_ns = slice_ns;
-    limit->slice_quota = MAX(((double)speed * slice_ns) / 1000000000ULL, 1);
+    if (speed == 0) {
+        limit->slice_quota = 0;
+    } else {
+        limit->slice_quota = MAX(((double)speed * slice_ns) / 1000000000ULL, 1);
+    }
 }
 
 #endif
Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/5 block-copy: let ratelimit handle a speed of 0

In reply to Emanuele Giuseppe Esposito

From: Paolo Bonzini <[email protected]>

---
 block/block-copy.c | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)
block/block-copy.c +11 -17
index 5808cfe657..943e30b7e6 100644
@@ -114,7 +114,6 @@ typedef struct BlockCopyState {
 
     SharedResource *mem;
 
-    uint64_t speed;
     RateLimit rate_limit;
 } BlockCopyState;
 
@@ -647,21 +646,19 @@ block_copy_dirty_clusters(BlockCopyCallState *call_state)
             task->copy_range = false;
         }
 
-        if (s->speed) {
-            if (!call_state->ignore_ratelimit) {
-                uint64_t ns = ratelimit_calculate_delay(&s->rate_limit, 0);
-                if (ns > 0) {
-                    block_copy_task_end(task, -EAGAIN);
-                    g_free(task);
-                    qemu_co_sleep_ns_wakeable(&call_state->sleep,
-                                              QEMU_CLOCK_REALTIME, ns);
-                    continue;
-                }
+        if (!call_state->ignore_ratelimit) {
+            uint64_t ns = ratelimit_calculate_delay(&s->rate_limit, 0);
+            if (ns > 0) {
+                block_copy_task_end(task, -EAGAIN);
+                g_free(task);
+                qemu_co_sleep_ns_wakeable(&call_state->sleep,
+                                            QEMU_CLOCK_REALTIME, ns);
+                continue;
             }
-
-            ratelimit_calculate_delay(&s->rate_limit, task->bytes);
         }
 
+        ratelimit_calculate_delay(&s->rate_limit, task->bytes);
+
         trace_block_copy_process(s, task->offset);
 
         co_get_from_shres(s->mem, task->bytes);
@@ -853,10 +850,7 @@ void block_copy_set_skip_unallocated(BlockCopyState *s, bool skip)
 
 void block_copy_set_speed(BlockCopyState *s, uint64_t speed)
 {
-    s->speed = speed;
-    if (speed > 0) {
-        ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME);
-    }
+    ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME);
 
     /*
      * Note: it's good to kick all call states from here, but it should be done
Signature · 1 line
2.31.1
#3 lore raw
In reply to Emanuele Giuseppe Esposito

Please discard this thread, I had an issue with git send-email and patch 3-5 are missing.

Thank you,
Emanuele

Quoted 44 lines · Emanuele Giuseppe Esposito

This serie of patches bring thread safety to the smaller APIs used by block-copy, namely ratelimit, progressmeter, co-shared-resource and aiotask.
The end goal is to reduce the usage of AioContexlock in block-copy, by introducing smaller granularity locks thus on making the block layer thread safe.

What's missing for block-copy to be fully thread-safe is fixing the CoSleep API to allow cross-thread sleep and wakeup. Paolo is working on it and will post the patches once his new CoSleep API is accepted.

Patches 1-3 work on ratelimit, 4 covers progressmeter and 5 co-shared-resources.

---
v3:
* Rebase on current master (had conflicts in block-copy), remove based-on in cover letter

Emanuele Giuseppe Esposito (2):
progressmeter: protect with a mutex
co-shared-resource: protect with a mutex

Paolo Bonzini (3):
ratelimit: treat zero speed as unlimited
block-copy: let ratelimit handle a speed of 0
blockjob: let ratelimit handle a speed of 0

  block/block-copy.c                | 28 ++++++--------
  block/meson.build                 |  1 +
  block/progress_meter.c            | 64 +++++++++++++++++++++++++++++++
  blockjob.c                        | 46 +++++++++++++---------
  include/qemu/co-shared-resource.h |  4 +-
  include/qemu/progress_meter.h     | 34 ++++++++--------
  include/qemu/ratelimit.h          | 12 +++++-
  job-qmp.c                         |  8 +++-
  job.c                             |  3 ++
  qemu-img.c                        |  9 +++--
  util/qemu-co-shared-resource.c    | 24 +++++++++---
  11 files changed, 168 insertions(+), 65 deletions(-)

create mode 100644 block/progress_meter.c

#0 lore raw

v3 cover block-copy: make helper APIs thread safe

This serie of patches bring thread safety to the smaller APIs used by block-copy, namely ratelimit, progressmeter, co-shared-resource and aiotask.
The end goal is to reduce the usage of AioContexlock in block-copy, by introducing smaller granularity locks thus on making the block layer thread safe.

What's missing for block-copy to be fully thread-safe is fixing the CoSleep API to allow cross-thread sleep and wakeup. Paolo is working on it and will post the patches once his new CoSleep API is accepted.

Patches 1-3 work on ratelimit, 4 covers progressmeter and 5 co-shared-resources.

---
v3:
* Rebase on current master (had conflicts in block-copy), remove based-on in cover letter

Emanuele Giuseppe Esposito (2):
progressmeter: protect with a mutex
co-shared-resource: protect with a mutex

Paolo Bonzini (3):
ratelimit: treat zero speed as unlimited
block-copy: let ratelimit handle a speed of 0
blockjob: let ratelimit handle a speed of 0

 block/block-copy.c                | 28 ++++++--------
 block/meson.build                 |  1 +
 block/progress_meter.c            | 64 +++++++++++++++++++++++++++++++
 blockjob.c                        | 46 +++++++++++++---------
 include/qemu/co-shared-resource.h |  4 +-
 include/qemu/progress_meter.h     | 34 ++++++++--------
 include/qemu/ratelimit.h          | 12 +++++-
 job-qmp.c                         |  8 +++-
 job.c                             |  3 ++
 qemu-img.c                        |  9 +++--
 util/qemu-co-shared-resource.c    | 24 +++++++++---
 11 files changed, 168 insertions(+), 65 deletions(-)
 create mode 100644 block/progress_meter.c
Signature · 1 line
2.31.1