1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= <thomas@t-8ch.de>
Date: Tue, 7 May 2024 13:33:40 +0200
Subject: libuuid: split uuidd cache into dedicated struct
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
To clear the struct we need to be able to refer to it by name.
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---
libuuid/src/gen_uuid.c | 74 +++++++++++++++++++++++++++-----------------------
1 file changed, 40 insertions(+), 34 deletions(-)
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
index 0984e24..c40f9d6 100644
--- a/libuuid/src/gen_uuid.c
+++ b/libuuid/src/gen_uuid.c
@@ -579,57 +579,63 @@ int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
* If neither of these is possible (e.g. because of insufficient permissions), it generates
* the UUID anyway, but returns -1. Otherwise, returns 0.
*/
+
+/* thread local cache for uuidd based requests */
+THREAD_LOCAL struct {
+ int num;
+ int cache_size;
+ int last_used;
+ struct uuid uu;
+ time_t last_time;
+} uuidd_cache = {
+ .cache_size = CS_MIN,
+};
+
static int uuid_generate_time_generic(uuid_t out) {
- /* thread local cache for uuidd based requests */
- THREAD_LOCAL int num = 0;
- 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) { /* expire cache */
+ time_t now;
+
+ if (uuidd_cache.num > 0) { /* expire cache */
now = time(NULL);
- if (now > last_time+1) {
- last_used = cache_size - num;
- num = 0;
+ if (now > uuidd_cache.last_time+1) {
+ uuidd_cache.last_used = uuidd_cache.cache_size - uuidd_cache.num;
+ uuidd_cache.num = 0;
}
}
- if (num <= 0) { /* fill cache */
+ if (uuidd_cache.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 adjust the cache size over the runntime.
*/
- 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;
+ if ((uuidd_cache.last_used == uuidd_cache.cache_size) && (uuidd_cache.cache_size < CS_MAX))
+ uuidd_cache.cache_size *= CS_FACTOR;
+ else if ((uuidd_cache.last_used < (uuidd_cache.cache_size / CS_FACTOR)) && (uuidd_cache.cache_size > CS_MIN))
+ uuidd_cache.cache_size /= CS_FACTOR;
- num = cache_size;
+ uuidd_cache.num = uuidd_cache.cache_size;
if (get_uuid_via_daemon(UUIDD_OP_BULK_TIME_UUID,
- out, &num) == 0) {
- last_time = time(NULL);
- uuid_unpack(out, &uu);
- num--;
+ out, &uuidd_cache.num) == 0) {
+ uuidd_cache.last_time = time(NULL);
+ uuid_unpack(out, &uuidd_cache.uu);
+ uuidd_cache.num--;
return 0;
}
/* request to daemon failed, reset cache */
- num = 0;
- cache_size = CS_MIN;
+ uuidd_cache.num = 0;
+ uuidd_cache.cache_size = CS_MIN;
}
- if (num > 0) { /* serve uuid from cache */
- uu.time_low++;
- if (uu.time_low == 0) {
- uu.time_mid++;
- if (uu.time_mid == 0)
- uu.time_hi_and_version++;
+ if (uuidd_cache.num > 0) { /* serve uuid from cache */
+ uuidd_cache.uu.time_low++;
+ if (uuidd_cache.uu.time_low == 0) {
+ uuidd_cache.uu.time_mid++;
+ if (uuidd_cache.uu.time_mid == 0)
+ uuidd_cache.uu.time_hi_and_version++;
}
- num--;
- uuid_pack(&uu, out);
- if (num == 0)
- last_used = cache_size;
+ uuidd_cache.num--;
+ uuid_pack(&uuidd_cache.uu, out);
+ if (uuidd_cache.num == 0)
+ uuidd_cache.last_used = uuidd_cache.cache_size;
return 0;
}
|