@@ -528,25 +528,35 @@ int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
528
528
*/
529
529
static int uuid_generate_time_generic (uuid_t out ) {
530
530
#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 ;
531
535
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 ;
533
538
THREAD_LOCAL struct uuid uu ;
534
539
THREAD_LOCAL time_t last_time = 0 ;
535
540
time_t now ;
536
541
537
- if (num > 0 ) {
542
+ if (num > 0 ) { /* expire cache */
538
543
now = time (NULL );
539
- if (now > last_time + 1 )
544
+ if (now > last_time + 1 ) {
545
+ last_used = cache_size - num ;
540
546
num = 0 ;
547
+ }
541
548
}
542
- if (num <= 0 ) {
549
+ if (num <= 0 ) { /* fill cache */
543
550
/*
544
551
* num + OP_BULK provides a local cache in each application.
545
552
* 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.
547
554
*/
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
+
550
560
num = cache_size ;
551
561
552
562
if (get_uuid_via_daemon (UUIDD_OP_BULK_TIME_UUID ,
@@ -556,9 +566,11 @@ static int uuid_generate_time_generic(uuid_t out) {
556
566
num -- ;
557
567
return 0 ;
558
568
}
569
+ /* request to daemon failed, reset cache */
559
570
num = 0 ;
571
+ cache_size = cs_min ;
560
572
}
561
- if (num > 0 ) {
573
+ if (num > 0 ) { /* serve uuid from cache */
562
574
uu .time_low ++ ;
563
575
if (uu .time_low == 0 ) {
564
576
uu .time_mid ++ ;
@@ -567,6 +579,8 @@ static int uuid_generate_time_generic(uuid_t out) {
567
579
}
568
580
num -- ;
569
581
uuid_pack (& uu , out );
582
+ if (num == 0 )
583
+ last_used = cache_size ;
570
584
return 0 ;
571
585
}
572
586
#else
0 commit comments