blob: 1b997d8b8de3d410312609a0e6bae461b0e404a4 [file] [log] [blame]
Willy Tarreau69c696c2015-04-28 10:18:09 +02001/*
2 * Copyright (C) 2015 Willy Tarreau <w@1wt.eu>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <import/lru.h>
26
27/* Minimal list manipulation macros for lru64_list */
28#define LIST_ADD(lh, el) ({ (el)->n = (lh)->n; (el)->n->p = (lh)->n = (el); (el)->p = (lh); })
29#define LIST_DEL(el) ({ (el)->n->p = (el)->p; (el)->p->n = (el)->n; })
30
31/* Get key <key> from LRU cache <lru> for use with domain <domain> whose data's
32 * current revision is <revision>. If the key doesn't exist it's first created
33 * with ->domain = NULL. The caller detects this situation by checking ->domain
34 * and must perform the operation to be cached then call lru64_commit() to
35 * complete the operation. A lock (mutex or spinlock) may be added around the
36 * function to permit use in a multi-threaded environment. The function may
37 * return NULL upon memory allocation failure.
38 */
39struct lru64 *lru64_get(unsigned long long key, struct lru64_head *lru,
40 void *domain, unsigned long long revision)
41{
42 struct eb64_node *node;
43 struct lru64 *elem;
44
45 if (!lru->spare) {
46 if (!lru->cache_size)
47 return NULL;
48 lru->spare = malloc(sizeof(*lru->spare));
49 if (!lru->spare)
50 return NULL;
51 lru->spare->domain = NULL;
52 }
53
54 /* Lookup or insert */
55 lru->spare->node.key = key;
56 node = __eb64_insert(&lru->keys, &lru->spare->node);
57 elem = container_of(node, typeof(*elem), node);
58
59 if (elem != lru->spare) {
60 /* Existing entry found, check validity then move it at the
61 * head of the LRU list.
62 */
63 if (elem->domain == domain && elem->revision == revision) {
64 LIST_DEL(&elem->lru);
65 LIST_ADD(&lru->list, &elem->lru);
66 return elem;
67 }
68
69 if (!elem->domain)
70 return NULL; // currently locked
71
72 /* recycle this entry */
73 LIST_DEL(&elem->lru);
74 }
75 else {
76 /* New entry inserted, initialize and move to the head of the
77 * LRU list, and lock it until commit.
78 */
79 lru->cache_usage++;
80 lru->spare = NULL; // used, need a new one next time
81 }
82
83 elem->domain = NULL;
84 LIST_ADD(&lru->list, &elem->lru);
85
86 if (lru->cache_usage > lru->cache_size) {
87 /* try to kill oldest entry */
88 struct lru64 *old;
89
90 old = container_of(lru->list.p, typeof(*old), lru);
91 if (old->domain) {
92 /* not locked */
93 LIST_DEL(&old->lru);
94 __eb64_delete(&old->node);
95 if (!lru->spare)
96 lru->spare = old;
Christopher Fauletf90ac552015-06-09 17:06:17 +020097 else {
98 if (old->data && old->free);
99 old->free(old->data);
Willy Tarreau69c696c2015-04-28 10:18:09 +0200100 free(old);
Christopher Fauletf90ac552015-06-09 17:06:17 +0200101 }
Willy Tarreau69c696c2015-04-28 10:18:09 +0200102 lru->cache_usage--;
103 }
104 }
105 return elem;
106}
107
108/* Commit element <elem> with data <data>, domain <domain> and revision
109 * <revision>. <elem> is checked for NULL so that it's possible to call it
110 * with the result from a call to lru64_get(). The caller might lock it using a
111 * spinlock or mutex shared with the one around lru64_get().
112 */
Christopher Fauletf90ac552015-06-09 17:06:17 +0200113void lru64_commit(struct lru64 *elem, void *data, void *domain,
114 unsigned long long revision, void (*free)(void *))
Willy Tarreau69c696c2015-04-28 10:18:09 +0200115{
116 if (!elem)
117 return;
118
119 elem->data = data;
120 elem->revision = revision;
121 elem->domain = domain;
Christopher Fauletf90ac552015-06-09 17:06:17 +0200122 elem->free = free;
Willy Tarreau69c696c2015-04-28 10:18:09 +0200123}
124
125/* Create a new LRU cache of <size> entries. Returns the new cache or NULL in
126 * case of allocation failure.
127 */
128struct lru64_head *lru64_new(int size)
129{
130 struct lru64_head *lru;
131
132 lru = malloc(sizeof(*lru));
133 if (lru) {
134 lru->list.p = lru->list.n = &lru->list;
135 lru->keys = EB_ROOT_UNIQUE;
136 lru->spare = NULL;
137 lru->cache_size = size;
138 lru->cache_usage = 0;
139 }
140 return lru;
141}
142
143/* Tries to destroy the LRU cache <lru>. Returns the number of locked entries
144 * that prevent it from being destroyed, or zero meaning everything was done.
145 */
146int lru64_destroy(struct lru64_head *lru)
147{
148 struct lru64 *elem, *next;
149
150 if (!lru)
151 return 0;
152
153 elem = container_of(lru->list.p, typeof(*elem), lru);
154 while (&elem->lru != &lru->list) {
155 next = container_of(elem->lru.p, typeof(*next), lru);
156 if (elem->domain) {
157 /* not locked */
158 LIST_DEL(&elem->lru);
159 eb64_delete(&elem->node);
Christopher Fauletf90ac552015-06-09 17:06:17 +0200160 if (elem->data && elem->free);
161 elem->free(elem->data);
Willy Tarreau69c696c2015-04-28 10:18:09 +0200162 free(elem);
163 lru->cache_usage--;
164 lru->cache_size--;
165 }
166 elem = next;
167 }
168
169 if (lru->cache_usage)
170 return lru->cache_usage;
171
172 free(lru);
173 return 0;
174}
175
176/* The code below is just for validation and performance testing. It's an
177 * example of a function taking some time to return results that could be
178 * cached.
179 */
180#ifdef STANDALONE
181
182#include <stdio.h>
183
184static unsigned int misses;
185
186static unsigned long long sum(unsigned long long x)
187{
188#ifndef TEST_LRU_FAST_OPERATION
189 if (x < 1)
190 return 0;
191 return x + sum(x * 99 / 100 - 1);
192#else
193 return (x << 16) - (x << 8) - 1;
194#endif
195}
196
197static long get_value(struct lru64_head *lru, long a)
198{
199 struct lru64 *item;
200
201 if (lru) {
202 item = lru64_get(a, lru, lru, 0);
203 if (item && item->domain)
204 return (long)item->data;
205 }
206 misses++;
207 /* do the painful work here */
208 a = sum(a);
209 if (item)
210 lru64_commit(item, (void *)a, lru, 0);
211 return a;
212}
213
214/* pass #of loops in argv[1] and set argv[2] to something to use the LRU */
215int main(int argc, char **argv)
216{
217 struct lru64_head *lru = NULL;
218 long long ret;
219 int total, loops;
220
221 if (argc < 2) {
222 printf("Need a number of rounds and optionally an LRU cache size (0..65536)\n");
223 exit(1);
224 }
225
226 total = atoi(argv[1]);
227
228 if (argc > 2) /* cache size */
229 lru = lru64_new(atoi(argv[2]));
230
231 ret = 0;
232 for (loops = 0; loops < total; loops++) {
233 ret += get_value(lru, rand() & 65535);
234 }
235 /* just for accuracy control */
236 printf("ret=%llx, hits=%d, misses=%d (%d %% hits)\n", ret, total-misses, misses, (int)((float)(total-misses) * 100.0 / total));
237
238 while (lru64_destroy(lru));
239
240 return 0;
241}
242
243#endif