]>
Commit | Line | Data |
---|---|---|
6a566b9c | 1 | |
2 | /* | |
262a0e14 | 3 | * $Id$ |
6a566b9c | 4 | * |
507d0a78 | 5 | * DEBUG: none LRU Removal Policy |
6a566b9c | 6 | * AUTHOR: Henrik Nordstrom |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
6a566b9c | 9 | * ---------------------------------------------------------- |
10 | * | |
2b6662ba | 11 | * Squid is the result of efforts by numerous individuals from |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
6a566b9c | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
26ac0430 | 24 | * |
6a566b9c | 25 | * This program is distributed in the hope that it will be useful, |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
26ac0430 | 29 | * |
6a566b9c | 30 | * You should have received a copy of the GNU General Public License |
31 | * along with this program; if not, write to the Free Software | |
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
33 | * | |
34 | */ | |
35 | ||
36 | #include "squid.h" | |
e6ccf245 | 37 | #include "Store.h" |
528b2c61 | 38 | #include "MemObject.h" |
985c86bc | 39 | #include "SquidTime.h" |
6a566b9c | 40 | |
41 | REMOVALPOLICYCREATE createRemovalPolicy_lru; | |
42 | ||
26ac0430 | 43 | struct LruPolicyData { |
e6ccf245 | 44 | void setPolicyNode (StoreEntry *, void *) const; |
6a566b9c | 45 | RemovalPolicy *policy; |
46 | dlink_list list; | |
47 | int count; | |
48 | int nwalkers; | |
9f913cf1 | 49 | enum heap_entry_type { |
62e76326 | 50 | TYPE_UNKNOWN = 0, TYPE_STORE_ENTRY, TYPE_STORE_MEM |
9f913cf1 | 51 | } type; |
6a566b9c | 52 | }; |
53 | ||
54 | /* Hack to avoid having to remember the RemovalPolicyNode location. | |
55 | * Needed by the purge walker to clear the policy information | |
56 | */ | |
e6ccf245 | 57 | static enum LruPolicyData::heap_entry_type |
6a566b9c | 58 | repl_guessType(StoreEntry * entry, RemovalPolicyNode * node) |
59 | { | |
60 | if (node == &entry->repl) | |
62e76326 | 61 | return LruPolicyData::TYPE_STORE_ENTRY; |
62 | ||
6a566b9c | 63 | if (entry->mem_obj && node == &entry->mem_obj->repl) |
62e76326 | 64 | return LruPolicyData::TYPE_STORE_MEM; |
65 | ||
6a566b9c | 66 | fatal("Heap Replacement: Unknown StoreEntry node type"); |
62e76326 | 67 | |
e6ccf245 | 68 | return LruPolicyData::TYPE_UNKNOWN; |
6a566b9c | 69 | } |
e6ccf245 | 70 | |
71 | void | |
72 | LruPolicyData::setPolicyNode (StoreEntry *entry, void *value) const | |
73 | { | |
74 | switch (type) { | |
62e76326 | 75 | |
76 | case TYPE_STORE_ENTRY: | |
77 | entry->repl.data = value; | |
78 | break ; | |
79 | ||
80 | case TYPE_STORE_MEM: | |
81 | entry->mem_obj->repl.data = value ; | |
82 | break ; | |
83 | ||
84 | default: | |
85 | break; | |
6a566b9c | 86 | } |
e6ccf245 | 87 | } |
6a566b9c | 88 | |
89 | typedef struct _LruNode LruNode; | |
62e76326 | 90 | |
26ac0430 | 91 | struct _LruNode { |
6a566b9c | 92 | /* Note: the dlink_node MUST be the first member of the LruNode |
93 | * structure. This member is later pointer typecasted to LruNode *. | |
94 | */ | |
95 | dlink_node node; | |
96 | }; | |
97 | ||
04eb0689 | 98 | static MemAllocator *lru_node_pool = NULL; |
6a566b9c | 99 | static int nr_lru_policies = 0; |
100 | ||
101 | static void | |
102 | lru_add(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node) | |
103 | { | |
e6ccf245 | 104 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 105 | LruNode *lru_node; |
106 | assert(!node->data); | |
b001e822 | 107 | node->data = lru_node = (LruNode *)lru_node_pool->alloc(); |
6a566b9c | 108 | dlinkAddTail(entry, &lru_node->node, &lru->list); |
109 | lru->count += 1; | |
62e76326 | 110 | |
6a566b9c | 111 | if (!lru->type) |
62e76326 | 112 | lru->type = repl_guessType(entry, node); |
6a566b9c | 113 | } |
114 | ||
115 | static void | |
116 | lru_remove(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node) | |
117 | { | |
e6ccf245 | 118 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
119 | LruNode *lru_node = (LruNode *)node->data; | |
62e76326 | 120 | |
6a566b9c | 121 | if (!lru_node) |
62e76326 | 122 | return; |
123 | ||
75240ca9 | 124 | /* |
125 | * It seems to be possible for an entry to exist in the hash | |
126 | * but not be in the LRU list, so check for that case rather | |
127 | * than suffer a NULL pointer access. | |
128 | */ | |
129 | if (NULL == lru_node->node.data) | |
62e76326 | 130 | return; |
131 | ||
6a566b9c | 132 | assert(lru_node->node.data == entry); |
62e76326 | 133 | |
6a566b9c | 134 | node->data = NULL; |
62e76326 | 135 | |
6a566b9c | 136 | dlinkDelete(&lru_node->node, &lru->list); |
62e76326 | 137 | |
b001e822 | 138 | lru_node_pool->free(lru_node); |
62e76326 | 139 | |
6a566b9c | 140 | lru->count -= 1; |
141 | } | |
142 | ||
143 | static void | |
144 | lru_referenced(RemovalPolicy * policy, const StoreEntry * entry, | |
62e76326 | 145 | RemovalPolicyNode * node) |
6a566b9c | 146 | { |
e6ccf245 | 147 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
148 | LruNode *lru_node = (LruNode *)node->data; | |
62e76326 | 149 | |
6a566b9c | 150 | if (!lru_node) |
62e76326 | 151 | return; |
152 | ||
6a566b9c | 153 | dlinkDelete(&lru_node->node, &lru->list); |
62e76326 | 154 | |
6a566b9c | 155 | dlinkAddTail((void *) entry, &lru_node->node, &lru->list); |
156 | } | |
157 | ||
158 | /** RemovalPolicyWalker **/ | |
159 | ||
160 | typedef struct _LruWalkData LruWalkData; | |
62e76326 | 161 | |
26ac0430 | 162 | struct _LruWalkData { |
6a566b9c | 163 | LruNode *current; |
164 | }; | |
165 | ||
2d72d4fd | 166 | static const StoreEntry * |
6a566b9c | 167 | lru_walkNext(RemovalPolicyWalker * walker) |
168 | { | |
e6ccf245 | 169 | LruWalkData *lru_walk = (LruWalkData *)walker->_data; |
6a566b9c | 170 | LruNode *lru_node = lru_walk->current; |
62e76326 | 171 | |
6a566b9c | 172 | if (!lru_node) |
62e76326 | 173 | return NULL; |
174 | ||
6a566b9c | 175 | lru_walk->current = (LruNode *) lru_node->node.next; |
62e76326 | 176 | |
6a566b9c | 177 | return (StoreEntry *) lru_node->node.data; |
178 | } | |
179 | ||
180 | static void | |
181 | lru_walkDone(RemovalPolicyWalker * walker) | |
182 | { | |
183 | RemovalPolicy *policy = walker->_policy; | |
e6ccf245 | 184 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 185 | assert(strcmp(policy->_type, "lru") == 0); |
186 | assert(lru->nwalkers > 0); | |
187 | lru->nwalkers -= 1; | |
188 | safe_free(walker->_data); | |
aa839030 | 189 | delete walker; |
6a566b9c | 190 | } |
191 | ||
192 | static RemovalPolicyWalker * | |
193 | lru_walkInit(RemovalPolicy * policy) | |
194 | { | |
e6ccf245 | 195 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 196 | RemovalPolicyWalker *walker; |
197 | LruWalkData *lru_walk; | |
198 | lru->nwalkers += 1; | |
aa839030 | 199 | walker = new RemovalPolicyWalker; |
e6ccf245 | 200 | lru_walk = (LruWalkData *)xcalloc(1, sizeof(*lru_walk)); |
6a566b9c | 201 | walker->_policy = policy; |
202 | walker->_data = lru_walk; | |
203 | walker->Next = lru_walkNext; | |
204 | walker->Done = lru_walkDone; | |
205 | lru_walk->current = (LruNode *) lru->list.head; | |
6a566b9c | 206 | return walker; |
207 | } | |
208 | ||
209 | /** RemovalPurgeWalker **/ | |
210 | ||
211 | typedef struct _LruPurgeData LruPurgeData; | |
62e76326 | 212 | |
26ac0430 | 213 | struct _LruPurgeData { |
6a566b9c | 214 | LruNode *current; |
215 | LruNode *start; | |
216 | }; | |
217 | ||
218 | static StoreEntry * | |
219 | lru_purgeNext(RemovalPurgeWalker * walker) | |
220 | { | |
e6ccf245 | 221 | LruPurgeData *lru_walker = (LruPurgeData *)walker->_data; |
6a566b9c | 222 | RemovalPolicy *policy = walker->_policy; |
e6ccf245 | 223 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 224 | LruNode *lru_node; |
225 | StoreEntry *entry; | |
62e76326 | 226 | |
227 | try_again: | |
6a566b9c | 228 | lru_node = lru_walker->current; |
62e76326 | 229 | |
6a566b9c | 230 | if (!lru_node || walker->scanned >= walker->max_scan) |
62e76326 | 231 | return NULL; |
232 | ||
6a566b9c | 233 | walker->scanned += 1; |
62e76326 | 234 | |
6a566b9c | 235 | lru_walker->current = (LruNode *) lru_node->node.next; |
62e76326 | 236 | |
6a566b9c | 237 | if (lru_walker->current == lru_walker->start) { |
62e76326 | 238 | /* Last node found */ |
239 | lru_walker->current = NULL; | |
6a566b9c | 240 | } |
62e76326 | 241 | |
6a566b9c | 242 | entry = (StoreEntry *) lru_node->node.data; |
243 | dlinkDelete(&lru_node->node, &lru->list); | |
62e76326 | 244 | |
3900307b | 245 | if (entry->locked()) { |
62e76326 | 246 | /* Shit, it is locked. we can't return this one */ |
247 | walker->locked++; | |
248 | dlinkAddTail(entry, &lru_node->node, &lru->list); | |
249 | goto try_again; | |
6a566b9c | 250 | } |
62e76326 | 251 | |
b001e822 | 252 | lru_node_pool->free(lru_node); |
6a566b9c | 253 | lru->count -= 1; |
e6ccf245 | 254 | lru->setPolicyNode(entry, NULL); |
6a566b9c | 255 | return entry; |
256 | } | |
257 | ||
258 | static void | |
259 | lru_purgeDone(RemovalPurgeWalker * walker) | |
260 | { | |
261 | RemovalPolicy *policy = walker->_policy; | |
e6ccf245 | 262 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 263 | assert(strcmp(policy->_type, "lru") == 0); |
264 | assert(lru->nwalkers > 0); | |
265 | lru->nwalkers -= 1; | |
266 | safe_free(walker->_data); | |
aa839030 | 267 | delete walker; |
6a566b9c | 268 | } |
269 | ||
270 | static RemovalPurgeWalker * | |
271 | lru_purgeInit(RemovalPolicy * policy, int max_scan) | |
272 | { | |
e6ccf245 | 273 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 274 | RemovalPurgeWalker *walker; |
275 | LruPurgeData *lru_walk; | |
276 | lru->nwalkers += 1; | |
aa839030 | 277 | walker = new RemovalPurgeWalker; |
e6ccf245 | 278 | lru_walk = (LruPurgeData *)xcalloc(1, sizeof(*lru_walk)); |
6a566b9c | 279 | walker->_policy = policy; |
280 | walker->_data = lru_walk; | |
281 | walker->max_scan = max_scan; | |
282 | walker->Next = lru_purgeNext; | |
283 | walker->Done = lru_purgeDone; | |
284 | lru_walk->start = lru_walk->current = (LruNode *) lru->list.head; | |
6a566b9c | 285 | return walker; |
286 | } | |
287 | ||
8a3e7d9e | 288 | static void |
289 | lru_stats(RemovalPolicy * policy, StoreEntry * sentry) | |
290 | { | |
e6ccf245 | 291 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
b671cc68 | 292 | LruNode *lru_node = (LruNode *) lru->list.head; |
8a3e7d9e | 293 | |
62e76326 | 294 | again: |
295 | ||
8a3e7d9e | 296 | if (lru_node) { |
62e76326 | 297 | StoreEntry *entry = (StoreEntry *) lru_node->node.data; |
298 | ||
3900307b | 299 | if (entry->locked()) { |
62e76326 | 300 | lru_node = (LruNode *) lru_node->node.next; |
301 | goto again; | |
302 | } | |
303 | ||
304 | storeAppendPrintf(sentry, "LRU reference age: %.2f days\n", (double) (squid_curtime - entry->lastref) / (double) (24 * 60 * 60)); | |
8a3e7d9e | 305 | } |
306 | } | |
307 | ||
6a566b9c | 308 | static void |
309 | lru_free(RemovalPolicy * policy) | |
310 | { | |
e6ccf245 | 311 | LruPolicyData *lru = (LruPolicyData *)policy->_data; |
6a566b9c | 312 | /* Make some verification of the policy state */ |
313 | assert(strcmp(policy->_type, "lru") == 0); | |
314 | assert(lru->nwalkers); | |
315 | assert(lru->count); | |
316 | /* Ok, time to destroy this policy */ | |
e4a67a80 | 317 | safe_free(lru); |
6a566b9c | 318 | memset(policy, 0, sizeof(*policy)); |
aa839030 | 319 | delete policy; |
6a566b9c | 320 | } |
321 | ||
322 | RemovalPolicy * | |
9f913cf1 | 323 | createRemovalPolicy_lru(wordlist * args) |
6a566b9c | 324 | { |
325 | RemovalPolicy *policy; | |
326 | LruPolicyData *lru_data; | |
327 | /* no arguments expected or understood */ | |
328 | assert(!args); | |
329 | /* Initialize */ | |
62e76326 | 330 | |
d96ceb8e | 331 | if (!lru_node_pool) { |
b001e822 | 332 | /* Must be chunked */ |
04eb0689 | 333 | lru_node_pool = memPoolCreate("LRU policy node", sizeof(LruNode)); |
b001e822 | 334 | lru_node_pool->setChunkSize(512 * 1024); |
d96ceb8e | 335 | } |
62e76326 | 336 | |
6a566b9c | 337 | /* Allocate the needed structures */ |
e6ccf245 | 338 | lru_data = (LruPolicyData *)xcalloc(1, sizeof(*lru_data)); |
62e76326 | 339 | |
aa839030 | 340 | policy = new RemovalPolicy; |
62e76326 | 341 | |
6a566b9c | 342 | /* Initialize the URL data */ |
343 | lru_data->policy = policy; | |
62e76326 | 344 | |
6a566b9c | 345 | /* Populate the policy structure */ |
346 | policy->_type = "lru"; | |
62e76326 | 347 | |
6a566b9c | 348 | policy->_data = lru_data; |
62e76326 | 349 | |
6a566b9c | 350 | policy->Free = lru_free; |
62e76326 | 351 | |
6a566b9c | 352 | policy->Add = lru_add; |
62e76326 | 353 | |
6a566b9c | 354 | policy->Remove = lru_remove; |
62e76326 | 355 | |
6a566b9c | 356 | policy->Referenced = lru_referenced; |
62e76326 | 357 | |
6a566b9c | 358 | policy->Dereferenced = lru_referenced; |
62e76326 | 359 | |
6a566b9c | 360 | policy->WalkInit = lru_walkInit; |
62e76326 | 361 | |
6a566b9c | 362 | policy->PurgeInit = lru_purgeInit; |
62e76326 | 363 | |
8a3e7d9e | 364 | policy->Stats = lru_stats; |
62e76326 | 365 | |
6a566b9c | 366 | /* Increase policy usage count */ |
367 | nr_lru_policies += 0; | |
62e76326 | 368 | |
6a566b9c | 369 | return policy; |
370 | } |