Skip to content

Commit 2fa4168

Browse files
TrappMkarelzak
authored andcommitted
libuuid: fix lib internal cache size
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.
1 parent 1120837 commit 2fa4168

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

libuuid/src/gen_uuid.c

+22-8
Original file line numberDiff line numberDiff line change
@@ -528,25 +528,35 @@ int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
528528
*/
529529
static int uuid_generate_time_generic(uuid_t out) {
530530
#ifdef HAVE_TLS
531+
/* thread local cache for uuidd based requests */
532+
const int cs_min = (1<<6);
533+
const int cs_max = (1<<18);
534+
const int cs_factor = 2;
531535
THREAD_LOCAL int num = 0;
532-
THREAD_LOCAL int cache_size = 1;
536+
THREAD_LOCAL int cache_size = cs_min;
537+
THREAD_LOCAL int last_used = 0;
533538
THREAD_LOCAL struct uuid uu;
534539
THREAD_LOCAL time_t last_time = 0;
535540
time_t now;
536541

537-
if (num > 0) {
542+
if (num > 0) { /* expire cache */
538543
now = time(NULL);
539-
if (now > last_time+1)
544+
if (now > last_time+1) {
545+
last_used = cache_size - num;
540546
num = 0;
547+
}
541548
}
542-
if (num <= 0) {
549+
if (num <= 0) { /* fill cache */
543550
/*
544551
* num + OP_BULK provides a local cache in each application.
545552
* Start with a small cache size to cover short running applications
546-
* and increment the cache size over the runntime.
553+
* and adjust the cache size over the runntime.
547554
*/
548-
if (cache_size < 1000000)
549-
cache_size *= 10;
555+
if ((last_used == cache_size) && (cache_size < cs_max))
556+
cache_size *= cs_factor;
557+
else if ((last_used < (cache_size / cs_factor)) && (cache_size > cs_min))
558+
cache_size /= cs_factor;
559+
550560
num = cache_size;
551561

552562
if (get_uuid_via_daemon(UUIDD_OP_BULK_TIME_UUID,
@@ -556,9 +566,11 @@ static int uuid_generate_time_generic(uuid_t out) {
556566
num--;
557567
return 0;
558568
}
569+
/* request to daemon failed, reset cache */
559570
num = 0;
571+
cache_size = cs_min;
560572
}
561-
if (num > 0) {
573+
if (num > 0) { /* serve uuid from cache */
562574
uu.time_low++;
563575
if (uu.time_low == 0) {
564576
uu.time_mid++;
@@ -567,6 +579,8 @@ static int uuid_generate_time_generic(uuid_t out) {
567579
}
568580
num--;
569581
uuid_pack(&uu, out);
582+
if (num == 0)
583+
last_used = cache_size;
570584
return 0;
571585
}
572586
#else

0 commit comments

Comments
 (0)