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

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