Util-Linux package development
 help / color / mirror / Atom feed
From: Michael Trapp <michael.trapp@sap.com>
To: util-linux@vger.kernel.org
Cc: kzak@redhat.com
Subject: [PATCH] libuuid: fix lib internal cache size
Date: Mon,  6 Mar 2023 10:40:20 +0000	[thread overview]
Message-ID: <20230306104020.46754-1-michael.trapp@sap.com> (raw)

The lib internal cache improves throughput in high load
scenarios but for applications with a low request rate,
the cache size must be adapted to this situation.
Therefore the cache size should be changed to the current
requirements of the application during runtime.
---
 libuuid/src/gen_uuid.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
index e1ba3c3d0..619ef0131 100644
--- a/libuuid/src/gen_uuid.c
+++ b/libuuid/src/gen_uuid.c
@@ -528,25 +528,35 @@ int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
  */
 static int uuid_generate_time_generic(uuid_t out) {
 #ifdef HAVE_TLS
+	/* thread local cache for uuidd based requests */
+	const int			cs_min = (1<<6);
+	const int			cs_max = (1<<18);
+	const int			cs_factor = 2;
 	THREAD_LOCAL int		num = 0;
-	THREAD_LOCAL int		cache_size = 1;
+	THREAD_LOCAL int		cache_size = cs_min;
+	THREAD_LOCAL int		last_used = 0;
 	THREAD_LOCAL struct uuid	uu;
 	THREAD_LOCAL time_t		last_time = 0;
 	time_t				now;
 
-	if (num > 0) {
+	if (num > 0) { /* expire cache */
 		now = time(NULL);
-		if (now > last_time+1)
+		if (now > last_time+1) {
+			last_used = cache_size - num;
 			num = 0;
+		}
 	}
-	if (num <= 0) {
+	if (num <= 0) { /* fill cache */
 		/*
 		 * num + OP_BULK provides a local cache in each application.
 		 * Start with a small cache size to cover short running applications
-		 * and increment the cache size over the runntime.
+		 * and adjust the cache size over the runntime.
 		 */
-		if (cache_size < 1000000)
-			cache_size *= 10;
+		if ((last_used == cache_size) && (cache_size < cs_max))
+			cache_size *= cs_factor;
+		else if ((last_used < (cache_size / cs_factor)) && (cache_size > cs_min))
+			cache_size /= cs_factor;
+
 		num = cache_size;
 
 		if (get_uuid_via_daemon(UUIDD_OP_BULK_TIME_UUID,
@@ -556,9 +566,11 @@ static int uuid_generate_time_generic(uuid_t out) {
 			num--;
 			return 0;
 		}
+		/* request to daemon failed, reset cache */
 		num = 0;
+		cache_size = cs_min;
 	}
-	if (num > 0) {
+	if (num > 0) { /* serve uuid from cache */
 		uu.time_low++;
 		if (uu.time_low == 0) {
 			uu.time_mid++;
@@ -567,6 +579,8 @@ static int uuid_generate_time_generic(uuid_t out) {
 		}
 		num--;
 		uuid_pack(&uu, out);
+		if (num == 0)
+			last_used = cache_size;
 		return 0;
 	}
 #else
-- 
2.37.1


             reply	other threads:[~2023-03-06 10:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-06 10:40 Michael Trapp [this message]
2023-03-09 12:19 ` [PATCH] libuuid: fix lib internal cache size Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230306104020.46754-1-michael.trapp@sap.com \
    --to=michael.trapp@sap.com \
    --cc=kzak@redhat.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox