Blame SOURCES/0019-rate-Allow-burstiness-to-be-controlled.patch

3cdd4c
From c1a7c87fb9710fb29d699d1f39d0da19caf98da0 Mon Sep 17 00:00:00 2001
3cdd4c
From: "Richard W.M. Jones" <rjones@redhat.com>
3cdd4c
Date: Sat, 11 Jun 2022 12:34:02 +0100
3cdd4c
Subject: [PATCH] rate: Allow burstiness to be controlled
3cdd4c
3cdd4c
Previously it was fixed at 2.0 seconds.  Allowing it to be adjusted
3cdd4c
upwards could help with large, lumpy requests.
3cdd4c
3cdd4c
(cherry picked from commit f79e951c20510381d5cd83c203c670874a4978f4)
3cdd4c
---
3cdd4c
 filters/rate/nbdkit-rate-filter.pod | 12 ++++++++++--
3cdd4c
 filters/rate/rate.c                 | 20 +++++++++++++-------
3cdd4c
 tests/test-rate.sh                  |  2 +-
3cdd4c
 3 files changed, 24 insertions(+), 10 deletions(-)
3cdd4c
3cdd4c
diff --git a/filters/rate/nbdkit-rate-filter.pod b/filters/rate/nbdkit-rate-filter.pod
3cdd4c
index 8956e641..09ce7dbc 100644
3cdd4c
--- a/filters/rate/nbdkit-rate-filter.pod
3cdd4c
+++ b/filters/rate/nbdkit-rate-filter.pod
3cdd4c
@@ -9,6 +9,7 @@ nbdkit-rate-filter - limit bandwidth by connection or server
3cdd4c
                       [connection-rate=BITSPERSEC]
3cdd4c
                       [rate-file=FILENAME]
3cdd4c
                       [connection-rate-file=FILENAME]
3cdd4c
+                      [burstiness=SECS]
3cdd4c
 
3cdd4c
 =head1 DESCRIPTION
3cdd4c
 
3cdd4c
@@ -63,6 +64,13 @@ Limit total bandwidth across all connections to C<BITSPERSEC>.
3cdd4c
 Adjust the per-connection or total bandwidth dynamically by writing
3cdd4c
 C<BITSPERSEC> into C<FILENAME>.  See L</DYNAMIC ADJUSTMENT> below.
3cdd4c
 
3cdd4c
+=item B<burstiness=>SECS
3cdd4c
+
3cdd4c
+Control the bucket capacity, expressed as a length of time in
3cdd4c
+"rate-equivalent seconds" that the client is allowed to burst for
3cdd4c
+after a period of inactivity.  The default is 2.0 seconds.  It's not
3cdd4c
+recommended to set this smaller than the default.
3cdd4c
+
3cdd4c
 =back
3cdd4c
 
3cdd4c
 C<BITSPERSEC> can be specified as a simple number, or you can use a
3cdd4c
@@ -105,8 +113,8 @@ If the size of requests made by your client is much larger than the
3cdd4c
 rate limit then you can see long, lumpy sleeps in this filter.  In the
3cdd4c
 future we may modify the filter to break up large requests
3cdd4c
 automatically in order to limit the length of sleeps.  Placing the
3cdd4c
-L<nbdkit-blocksize-filter(1)> in front of this filter may help in the
3cdd4c
-meantime.
3cdd4c
+L<nbdkit-blocksize-filter(1)> in front of this filter, or adjusting
3cdd4c
+C<burstiness> upwards may help.
3cdd4c
 
3cdd4c
 =head1 FILES
3cdd4c
 
3cdd4c
diff --git a/filters/rate/rate.c b/filters/rate/rate.c
3cdd4c
index 1a70d212..26082f8c 100644
3cdd4c
--- a/filters/rate/rate.c
3cdd4c
+++ b/filters/rate/rate.c
3cdd4c
@@ -68,10 +68,9 @@ static char *rate_file = NULL;
3cdd4c
 
3cdd4c
 /* Bucket capacity controls the burst rate.  It is expressed as the
3cdd4c
  * length of time in "rate-equivalent seconds" that the client can
3cdd4c
- * burst for after a period of inactivity.  This could be adjustable
3cdd4c
- * in future.
3cdd4c
+ * burst for after a period of inactivity.
3cdd4c
  */
3cdd4c
-#define BUCKET_CAPACITY 2.0
3cdd4c
+static double bucket_capacity = 2.0 /* seconds */;
3cdd4c
 
3cdd4c
 /* Global read and write buckets. */
3cdd4c
 static struct bucket read_bucket;
3cdd4c
@@ -142,6 +141,13 @@ rate_config (nbdkit_next_config *next, nbdkit_backend *nxdata,
3cdd4c
       return -1;
3cdd4c
     return 0;
3cdd4c
   }
3cdd4c
+  else if (strcmp (key, "burstiness") == 0) {
3cdd4c
+    if (sscanf (value, "%lg", &bucket_capacity) != 1) {
3cdd4c
+      nbdkit_error ("burstiness must be a floating point number (seconds)");
3cdd4c
+      return -1;
3cdd4c
+    }
3cdd4c
+    return 0;
3cdd4c
+  }
3cdd4c
   else
3cdd4c
     return next (nxdata, key, value);
3cdd4c
 }
3cdd4c
@@ -150,8 +156,8 @@ static int
3cdd4c
 rate_get_ready (int thread_model)
3cdd4c
 {
3cdd4c
   /* Initialize the global buckets. */
3cdd4c
-  bucket_init (&read_bucket, rate, BUCKET_CAPACITY);
3cdd4c
-  bucket_init (&write_bucket, rate, BUCKET_CAPACITY);
3cdd4c
+  bucket_init (&read_bucket, rate, bucket_capacity);
3cdd4c
+  bucket_init (&write_bucket, rate, bucket_capacity);
3cdd4c
 
3cdd4c
   return 0;
3cdd4c
 }
3cdd4c
@@ -178,8 +184,8 @@ rate_open (nbdkit_next_open *next, nbdkit_context *nxdata,
3cdd4c
     return NULL;
3cdd4c
   }
3cdd4c
 
3cdd4c
-  bucket_init (&h->read_bucket, connection_rate, BUCKET_CAPACITY);
3cdd4c
-  bucket_init (&h->write_bucket, connection_rate, BUCKET_CAPACITY);
3cdd4c
+  bucket_init (&h->read_bucket, connection_rate, bucket_capacity);
3cdd4c
+  bucket_init (&h->write_bucket, connection_rate, bucket_capacity);
3cdd4c
   pthread_mutex_init (&h->read_bucket_lock, NULL);
3cdd4c
   pthread_mutex_init (&h->write_bucket_lock, NULL);
3cdd4c
 
3cdd4c
diff --git a/tests/test-rate.sh b/tests/test-rate.sh
3cdd4c
index 7305c928..ff781c21 100755
3cdd4c
--- a/tests/test-rate.sh
3cdd4c
+++ b/tests/test-rate.sh
3cdd4c
@@ -56,7 +56,7 @@ nbdkit -U - \
3cdd4c
        --filter=blocksize --filter=rate \
3cdd4c
        pattern 25M \
3cdd4c
        maxdata=65536 \
3cdd4c
-       rate=10M \
3cdd4c
+       rate=10M burstiness=2.0 \
3cdd4c
        --run 'nbdcopy "$uri" rate.img'
3cdd4c
 end_t=$SECONDS
3cdd4c
 
3cdd4c
-- 
3cdd4c
2.31.1
3cdd4c