* [PATCH v2 1/5] throttle: introduce enum ThrottleType
2023-06-27 7:24 [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
@ 2023-06-27 7:24 ` zhenwei pi
2023-07-03 10:26 ` Alberto Garcia
2023-06-27 7:24 ` [PATCH v2 2/5] test-throttle: use " zhenwei pi
` (4 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: zhenwei pi @ 2023-06-27 7:24 UTC (permalink / raw)
To: berto; +Cc: arei.gonglei, qemu-devel, qemu-block, berrange, zhenwei pi
Use enum ThrottleType instead of number index.
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
include/qemu/throttle.h | 11 ++++++++---
util/throttle.c | 16 +++++++++-------
2 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/include/qemu/throttle.h b/include/qemu/throttle.h
index 05f6346137..ba6293eeef 100644
--- a/include/qemu/throttle.h
+++ b/include/qemu/throttle.h
@@ -99,13 +99,18 @@ typedef struct ThrottleState {
int64_t previous_leak; /* timestamp of the last leak done */
} ThrottleState;
+typedef enum {
+ THROTTLE_READ = 0,
+ THROTTLE_WRITE,
+ THROTTLE_MAX
+} ThrottleType;
+
typedef struct ThrottleTimers {
- QEMUTimer *timers[2]; /* timers used to do the throttling */
+ QEMUTimer *timers[THROTTLE_MAX]; /* timers used to do the throttling */
QEMUClockType clock_type; /* the clock used */
/* Callbacks */
- QEMUTimerCB *read_timer_cb;
- QEMUTimerCB *write_timer_cb;
+ QEMUTimerCB *timer_cb[THROTTLE_MAX];
void *timer_opaque;
} ThrottleTimers;
diff --git a/util/throttle.c b/util/throttle.c
index 81f247a8d1..5642e61763 100644
--- a/util/throttle.c
+++ b/util/throttle.c
@@ -199,10 +199,12 @@ static bool throttle_compute_timer(ThrottleState *ts,
void throttle_timers_attach_aio_context(ThrottleTimers *tt,
AioContext *new_context)
{
- tt->timers[0] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
- tt->read_timer_cb, tt->timer_opaque);
- tt->timers[1] = aio_timer_new(new_context, tt->clock_type, SCALE_NS,
- tt->write_timer_cb, tt->timer_opaque);
+ tt->timers[THROTTLE_READ] =
+ aio_timer_new(new_context, tt->clock_type, SCALE_NS,
+ tt->timer_cb[THROTTLE_READ], tt->timer_opaque);
+ tt->timers[THROTTLE_WRITE] =
+ aio_timer_new(new_context, tt->clock_type, SCALE_NS,
+ tt->timer_cb[THROTTLE_WRITE], tt->timer_opaque);
}
/*
@@ -236,8 +238,8 @@ void throttle_timers_init(ThrottleTimers *tt,
memset(tt, 0, sizeof(ThrottleTimers));
tt->clock_type = clock_type;
- tt->read_timer_cb = read_timer_cb;
- tt->write_timer_cb = write_timer_cb;
+ tt->timer_cb[THROTTLE_READ] = read_timer_cb;
+ tt->timer_cb[THROTTLE_WRITE] = write_timer_cb;
tt->timer_opaque = timer_opaque;
throttle_timers_attach_aio_context(tt, aio_context);
}
@@ -256,7 +258,7 @@ void throttle_timers_detach_aio_context(ThrottleTimers *tt)
{
int i;
- for (i = 0; i < 2; i++) {
+ for (i = 0; i < THROTTLE_MAX; i++) {
throttle_timer_destroy(&tt->timers[i]);
}
}
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v2 2/5] test-throttle: use enum ThrottleType
2023-06-27 7:24 [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
2023-06-27 7:24 ` [PATCH v2 1/5] throttle: introduce enum ThrottleType zhenwei pi
@ 2023-06-27 7:24 ` zhenwei pi
2023-07-03 10:26 ` Alberto Garcia
2023-06-27 7:24 ` [PATCH v2 3/5] throttle: support read-only and write-only zhenwei pi
` (3 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: zhenwei pi @ 2023-06-27 7:24 UTC (permalink / raw)
To: berto; +Cc: arei.gonglei, qemu-devel, qemu-block, berrange, zhenwei pi
Use enum ThrottleType instead in the throttle test codes.
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
tests/unit/test-throttle.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/tests/unit/test-throttle.c b/tests/unit/test-throttle.c
index 7adb5e6652..a60b5fe22e 100644
--- a/tests/unit/test-throttle.c
+++ b/tests/unit/test-throttle.c
@@ -169,8 +169,8 @@ static void test_init(void)
/* check initialized fields */
g_assert(tt->clock_type == QEMU_CLOCK_VIRTUAL);
- g_assert(tt->timers[0]);
- g_assert(tt->timers[1]);
+ g_assert(tt->timers[THROTTLE_READ]);
+ g_assert(tt->timers[THROTTLE_WRITE]);
/* check other fields where cleared */
g_assert(!ts.previous_leak);
@@ -191,7 +191,7 @@ static void test_destroy(void)
throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
read_timer_cb, write_timer_cb, &ts);
throttle_timers_destroy(tt);
- for (i = 0; i < 2; i++) {
+ for (i = 0; i < THROTTLE_MAX; i++) {
g_assert(!tt->timers[i]);
}
}
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v2 3/5] throttle: support read-only and write-only
2023-06-27 7:24 [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
2023-06-27 7:24 ` [PATCH v2 1/5] throttle: introduce enum ThrottleType zhenwei pi
2023-06-27 7:24 ` [PATCH v2 2/5] test-throttle: use " zhenwei pi
@ 2023-06-27 7:24 ` zhenwei pi
2023-07-03 10:28 ` Alberto Garcia
2023-06-27 7:24 ` [PATCH v2 4/5] test-throttle: test read only and write only zhenwei pi
` (2 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: zhenwei pi @ 2023-06-27 7:24 UTC (permalink / raw)
To: berto; +Cc: arei.gonglei, qemu-devel, qemu-block, berrange, zhenwei pi
Only one direction is necessary in several scenarios:
- a read-only disk
- operations on a device are considered as *write* only. For example,
encrypt/decrypt/sign/verify operations on a cryptodev use a single
*write* timer(read timer callback is defined, but never invoked).
Allow a single direction in throttle, this reduces memory, and uplayer
does not need a dummy callback any more.
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
util/throttle.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/util/throttle.c b/util/throttle.c
index 5642e61763..c0bd0c26c3 100644
--- a/util/throttle.c
+++ b/util/throttle.c
@@ -199,12 +199,17 @@ static bool throttle_compute_timer(ThrottleState *ts,
void throttle_timers_attach_aio_context(ThrottleTimers *tt,
AioContext *new_context)
{
- tt->timers[THROTTLE_READ] =
- aio_timer_new(new_context, tt->clock_type, SCALE_NS,
- tt->timer_cb[THROTTLE_READ], tt->timer_opaque);
- tt->timers[THROTTLE_WRITE] =
- aio_timer_new(new_context, tt->clock_type, SCALE_NS,
- tt->timer_cb[THROTTLE_WRITE], tt->timer_opaque);
+ if (tt->timer_cb[THROTTLE_READ]) {
+ tt->timers[THROTTLE_READ] =
+ aio_timer_new(new_context, tt->clock_type, SCALE_NS,
+ tt->timer_cb[THROTTLE_READ], tt->timer_opaque);
+ }
+
+ if (tt->timer_cb[THROTTLE_WRITE]) {
+ tt->timers[THROTTLE_WRITE] =
+ aio_timer_new(new_context, tt->clock_type, SCALE_NS,
+ tt->timer_cb[THROTTLE_WRITE], tt->timer_opaque);
+ }
}
/*
@@ -235,6 +240,7 @@ void throttle_timers_init(ThrottleTimers *tt,
QEMUTimerCB *write_timer_cb,
void *timer_opaque)
{
+ assert(read_timer_cb || write_timer_cb);
memset(tt, 0, sizeof(ThrottleTimers));
tt->clock_type = clock_type;
@@ -247,7 +253,9 @@ void throttle_timers_init(ThrottleTimers *tt,
/* destroy a timer */
static void throttle_timer_destroy(QEMUTimer **timer)
{
- assert(*timer != NULL);
+ if (*timer == NULL) {
+ return;
+ }
timer_free(*timer);
*timer = NULL;
@@ -272,7 +280,7 @@ void throttle_timers_destroy(ThrottleTimers *tt)
/* is any throttling timer configured */
bool throttle_timers_are_initialized(ThrottleTimers *tt)
{
- if (tt->timers[0]) {
+ if (tt->timers[THROTTLE_READ] || tt->timers[THROTTLE_WRITE]) {
return true;
}
@@ -424,8 +432,12 @@ bool throttle_schedule_timer(ThrottleState *ts,
{
int64_t now = qemu_clock_get_ns(tt->clock_type);
int64_t next_timestamp;
+ QEMUTimer *timer;
bool must_wait;
+ timer = is_write ? tt->timers[THROTTLE_WRITE] : tt->timers[THROTTLE_READ];
+ assert(timer);
+
must_wait = throttle_compute_timer(ts,
is_write,
now,
@@ -437,12 +449,12 @@ bool throttle_schedule_timer(ThrottleState *ts,
}
/* request throttled and timer pending -> do nothing */
- if (timer_pending(tt->timers[is_write])) {
+ if (timer_pending(timer)) {
return true;
}
/* request throttled and timer not pending -> arm timer */
- timer_mod(tt->timers[is_write], next_timestamp);
+ timer_mod(timer, next_timestamp);
return true;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v2 3/5] throttle: support read-only and write-only
2023-06-27 7:24 ` [PATCH v2 3/5] throttle: support read-only and write-only zhenwei pi
@ 2023-07-03 10:28 ` Alberto Garcia
0 siblings, 0 replies; 12+ messages in thread
From: Alberto Garcia @ 2023-07-03 10:28 UTC (permalink / raw)
To: zhenwei pi; +Cc: arei.gonglei, qemu-devel, qemu-block, berrange, zhenwei pi
On Tue 27 Jun 2023 03:24:29 PM +08, zhenwei pi wrote:
> Only one direction is necessary in several scenarios:
> - a read-only disk
> - operations on a device are considered as *write* only. For example,
> encrypt/decrypt/sign/verify operations on a cryptodev use a single
> *write* timer(read timer callback is defined, but never invoked).
>
> Allow a single direction in throttle, this reduces memory, and uplayer
> does not need a dummy callback any more.
>
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Berto
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2 4/5] test-throttle: test read only and write only
2023-06-27 7:24 [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
` (2 preceding siblings ...)
2023-06-27 7:24 ` [PATCH v2 3/5] throttle: support read-only and write-only zhenwei pi
@ 2023-06-27 7:24 ` zhenwei pi
2023-07-03 10:31 ` Alberto Garcia
2023-06-27 7:24 ` [PATCH v2 5/5] cryptodev: use NULL throttle timer cb for read direction zhenwei pi
2023-07-11 0:29 ` PING: [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
5 siblings, 1 reply; 12+ messages in thread
From: zhenwei pi @ 2023-06-27 7:24 UTC (permalink / raw)
To: berto; +Cc: arei.gonglei, qemu-devel, qemu-block, berrange, zhenwei pi
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
tests/unit/test-throttle.c | 66 ++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/tests/unit/test-throttle.c b/tests/unit/test-throttle.c
index a60b5fe22e..5547837a58 100644
--- a/tests/unit/test-throttle.c
+++ b/tests/unit/test-throttle.c
@@ -184,6 +184,70 @@ static void test_init(void)
throttle_timers_destroy(tt);
}
+static void test_init_readonly(void)
+{
+ int i;
+
+ tt = &tgm.throttle_timers;
+
+ /* fill the structures with crap */
+ memset(&ts, 1, sizeof(ts));
+ memset(tt, 1, sizeof(*tt));
+
+ /* init structures */
+ throttle_init(&ts);
+ throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
+ read_timer_cb, NULL, &ts);
+
+ /* check initialized fields */
+ g_assert(tt->clock_type == QEMU_CLOCK_VIRTUAL);
+ g_assert(tt->timers[THROTTLE_READ]);
+ g_assert(!tt->timers[THROTTLE_WRITE]);
+
+ /* check other fields where cleared */
+ g_assert(!ts.previous_leak);
+ g_assert(!ts.cfg.op_size);
+ for (i = 0; i < BUCKETS_COUNT; i++) {
+ g_assert(!ts.cfg.buckets[i].avg);
+ g_assert(!ts.cfg.buckets[i].max);
+ g_assert(!ts.cfg.buckets[i].level);
+ }
+
+ throttle_timers_destroy(tt);
+}
+
+static void test_init_writeonly(void)
+{
+ int i;
+
+ tt = &tgm.throttle_timers;
+
+ /* fill the structures with crap */
+ memset(&ts, 1, sizeof(ts));
+ memset(tt, 1, sizeof(*tt));
+
+ /* init structures */
+ throttle_init(&ts);
+ throttle_timers_init(tt, ctx, QEMU_CLOCK_VIRTUAL,
+ NULL, write_timer_cb, &ts);
+
+ /* check initialized fields */
+ g_assert(tt->clock_type == QEMU_CLOCK_VIRTUAL);
+ g_assert(!tt->timers[THROTTLE_READ]);
+ g_assert(tt->timers[THROTTLE_WRITE]);
+
+ /* check other fields where cleared */
+ g_assert(!ts.previous_leak);
+ g_assert(!ts.cfg.op_size);
+ for (i = 0; i < BUCKETS_COUNT; i++) {
+ g_assert(!ts.cfg.buckets[i].avg);
+ g_assert(!ts.cfg.buckets[i].max);
+ g_assert(!ts.cfg.buckets[i].level);
+ }
+
+ throttle_timers_destroy(tt);
+}
+
static void test_destroy(void)
{
int i;
@@ -752,6 +816,8 @@ int main(int argc, char **argv)
g_test_add_func("/throttle/leak_bucket", test_leak_bucket);
g_test_add_func("/throttle/compute_wait", test_compute_wait);
g_test_add_func("/throttle/init", test_init);
+ g_test_add_func("/throttle/init_readonly", test_init_readonly);
+ g_test_add_func("/throttle/init_writeonly", test_init_writeonly);
g_test_add_func("/throttle/destroy", test_destroy);
g_test_add_func("/throttle/have_timer", test_have_timer);
g_test_add_func("/throttle/detach_attach", test_detach_attach);
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v2 5/5] cryptodev: use NULL throttle timer cb for read direction
2023-06-27 7:24 [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
` (3 preceding siblings ...)
2023-06-27 7:24 ` [PATCH v2 4/5] test-throttle: test read only and write only zhenwei pi
@ 2023-06-27 7:24 ` zhenwei pi
2023-07-03 10:28 ` Alberto Garcia
2023-07-11 0:29 ` PING: [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
5 siblings, 1 reply; 12+ messages in thread
From: zhenwei pi @ 2023-06-27 7:24 UTC (permalink / raw)
To: berto; +Cc: arei.gonglei, qemu-devel, qemu-block, berrange, zhenwei pi
Operations on a crytpodev are considered as *write* only, the callback
of read direction is never invoked. Use NULL instead of an unreachable
path(cryptodev_backend_throttle_timer_cb on read direction).
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
backends/cryptodev.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/backends/cryptodev.c b/backends/cryptodev.c
index 7d29517843..5cfa25c61c 100644
--- a/backends/cryptodev.c
+++ b/backends/cryptodev.c
@@ -331,8 +331,7 @@ static void cryptodev_backend_set_throttle(CryptoDevBackend *backend, int field,
if (!enabled) {
throttle_init(&backend->ts);
throttle_timers_init(&backend->tt, qemu_get_aio_context(),
- QEMU_CLOCK_REALTIME,
- cryptodev_backend_throttle_timer_cb, /* FIXME */
+ QEMU_CLOCK_REALTIME, NULL,
cryptodev_backend_throttle_timer_cb, backend);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* PING: [PATCH v2 0/5] Misc fixes for throttle
2023-06-27 7:24 [PATCH v2 0/5] Misc fixes for throttle zhenwei pi
` (4 preceding siblings ...)
2023-06-27 7:24 ` [PATCH v2 5/5] cryptodev: use NULL throttle timer cb for read direction zhenwei pi
@ 2023-07-11 0:29 ` zhenwei pi
5 siblings, 0 replies; 12+ messages in thread
From: zhenwei pi @ 2023-07-11 0:29 UTC (permalink / raw)
To: berto, qemu-devel; +Cc: arei.gonglei, qemu-block, berrange
Hi,
This series has been reviewed by Alberto, can someone review / merge this?
On 6/27/23 15:24, zhenwei pi wrote:
> v1 -> v2:
> - rename 'ThrottleTimerType' to 'ThrottleType'
> - add assertion to throttle_schedule_timer
>
> Something remained:
> - 'bool is_write' is no longer appropriate, the related functions
> need to use 'ThrottleType throttle' instead. To avoid changes from
> other subsystems in this series, do this work in a followup series
> after there patches apply.
>
>
> v1:
> - introduce enum ThrottleTimerType instead of timers[0], timer[1]...
> - support read-only and write-only for throttle
> - adapt related test codes
> - cryptodev uses a write-only throttle timer
>
> Zhenwei Pi (5):
> throttle: introduce enum ThrottleType
> test-throttle: use enum ThrottleType
> throttle: support read-only and write-only
> test-throttle: test read only and write only
> cryptodev: use NULL throttle timer cb for read direction
>
> backends/cryptodev.c | 3 +-
> include/qemu/throttle.h | 11 ++++--
> tests/unit/test-throttle.c | 72 ++++++++++++++++++++++++++++++++++++--
> util/throttle.c | 36 +++++++++++++------
> 4 files changed, 103 insertions(+), 19 deletions(-)
>
--
zhenwei pi
^ permalink raw reply [flat|nested] 12+ messages in thread