]> git.ipfire.org Git - thirdparty/squid.git/blame - src/repl/lru/store_repl_lru.cc
regenerated HTML to be in synch with SGML code
[thirdparty/squid.git] / src / repl / lru / store_repl_lru.cc
CommitLineData
6a566b9c 1
2/*
62e76326 3 * $Id: store_repl_lru.cc,v 1.14 2003/02/21 22:50:50 robertc Exp $
6a566b9c 4 *
5 * DEBUG: section ? LRU Removal policy
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.
24 *
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.
29 *
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"
6a566b9c 39
40REMOVALPOLICYCREATE createRemovalPolicy_lru;
41
62e76326 42struct LruPolicyData
43{
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 57static enum LruPolicyData::heap_entry_type
6a566b9c 58repl_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
71void
72LruPolicyData::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
89typedef struct _LruNode LruNode;
62e76326 90
91struct _LruNode
92{
6a566b9c 93 /* Note: the dlink_node MUST be the first member of the LruNode
94 * structure. This member is later pointer typecasted to LruNode *.
95 */
96 dlink_node node;
97};
98
99static MemPool *lru_node_pool = NULL;
100static int nr_lru_policies = 0;
101
102static void
103lru_add(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node)
104{
e6ccf245 105 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 106 LruNode *lru_node;
107 assert(!node->data);
e6ccf245 108 node->data = lru_node = (LruNode *)memPoolAlloc(lru_node_pool);
6a566b9c 109 dlinkAddTail(entry, &lru_node->node, &lru->list);
110 lru->count += 1;
62e76326 111
6a566b9c 112 if (!lru->type)
62e76326 113 lru->type = repl_guessType(entry, node);
6a566b9c 114}
115
116static void
117lru_remove(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node)
118{
e6ccf245 119 LruPolicyData *lru = (LruPolicyData *)policy->_data;
120 LruNode *lru_node = (LruNode *)node->data;
62e76326 121
6a566b9c 122 if (!lru_node)
62e76326 123 return;
124
75240ca9 125 /*
126 * It seems to be possible for an entry to exist in the hash
127 * but not be in the LRU list, so check for that case rather
128 * than suffer a NULL pointer access.
129 */
130 if (NULL == lru_node->node.data)
62e76326 131 return;
132
6a566b9c 133 assert(lru_node->node.data == entry);
62e76326 134
6a566b9c 135 node->data = NULL;
62e76326 136
6a566b9c 137 dlinkDelete(&lru_node->node, &lru->list);
62e76326 138
6a566b9c 139 memPoolFree(lru_node_pool, lru_node);
62e76326 140
6a566b9c 141 lru->count -= 1;
142}
143
144static void
145lru_referenced(RemovalPolicy * policy, const StoreEntry * entry,
62e76326 146 RemovalPolicyNode * node)
6a566b9c 147{
e6ccf245 148 LruPolicyData *lru = (LruPolicyData *)policy->_data;
149 LruNode *lru_node = (LruNode *)node->data;
62e76326 150
6a566b9c 151 if (!lru_node)
62e76326 152 return;
153
6a566b9c 154 dlinkDelete(&lru_node->node, &lru->list);
62e76326 155
6a566b9c 156 dlinkAddTail((void *) entry, &lru_node->node, &lru->list);
157}
158
159/** RemovalPolicyWalker **/
160
161typedef struct _LruWalkData LruWalkData;
62e76326 162
163struct _LruWalkData
164{
6a566b9c 165 LruNode *current;
166};
167
2d72d4fd 168static const StoreEntry *
6a566b9c 169lru_walkNext(RemovalPolicyWalker * walker)
170{
e6ccf245 171 LruWalkData *lru_walk = (LruWalkData *)walker->_data;
6a566b9c 172 LruNode *lru_node = lru_walk->current;
62e76326 173
6a566b9c 174 if (!lru_node)
62e76326 175 return NULL;
176
6a566b9c 177 lru_walk->current = (LruNode *) lru_node->node.next;
62e76326 178
6a566b9c 179 return (StoreEntry *) lru_node->node.data;
180}
181
182static void
183lru_walkDone(RemovalPolicyWalker * walker)
184{
185 RemovalPolicy *policy = walker->_policy;
e6ccf245 186 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 187 assert(strcmp(policy->_type, "lru") == 0);
188 assert(lru->nwalkers > 0);
189 lru->nwalkers -= 1;
190 safe_free(walker->_data);
191 cbdataFree(walker);
192}
193
194static RemovalPolicyWalker *
195lru_walkInit(RemovalPolicy * policy)
196{
e6ccf245 197 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 198 RemovalPolicyWalker *walker;
199 LruWalkData *lru_walk;
200 lru->nwalkers += 1;
72711e31 201 walker = cbdataAlloc(RemovalPolicyWalker);
e6ccf245 202 lru_walk = (LruWalkData *)xcalloc(1, sizeof(*lru_walk));
6a566b9c 203 walker->_policy = policy;
204 walker->_data = lru_walk;
205 walker->Next = lru_walkNext;
206 walker->Done = lru_walkDone;
207 lru_walk->current = (LruNode *) lru->list.head;
6a566b9c 208 return walker;
209}
210
211/** RemovalPurgeWalker **/
212
213typedef struct _LruPurgeData LruPurgeData;
62e76326 214
215struct _LruPurgeData
216{
6a566b9c 217 LruNode *current;
218 LruNode *start;
219};
220
221static StoreEntry *
222lru_purgeNext(RemovalPurgeWalker * walker)
223{
e6ccf245 224 LruPurgeData *lru_walker = (LruPurgeData *)walker->_data;
6a566b9c 225 RemovalPolicy *policy = walker->_policy;
e6ccf245 226 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 227 LruNode *lru_node;
228 StoreEntry *entry;
62e76326 229
230try_again:
6a566b9c 231 lru_node = lru_walker->current;
62e76326 232
6a566b9c 233 if (!lru_node || walker->scanned >= walker->max_scan)
62e76326 234 return NULL;
235
6a566b9c 236 walker->scanned += 1;
62e76326 237
6a566b9c 238 lru_walker->current = (LruNode *) lru_node->node.next;
62e76326 239
6a566b9c 240 if (lru_walker->current == lru_walker->start) {
62e76326 241 /* Last node found */
242 lru_walker->current = NULL;
6a566b9c 243 }
62e76326 244
6a566b9c 245 entry = (StoreEntry *) lru_node->node.data;
246 dlinkDelete(&lru_node->node, &lru->list);
62e76326 247
6a566b9c 248 if (storeEntryLocked(entry)) {
62e76326 249 /* Shit, it is locked. we can't return this one */
250 walker->locked++;
251 dlinkAddTail(entry, &lru_node->node, &lru->list);
252 goto try_again;
6a566b9c 253 }
62e76326 254
6a566b9c 255 memPoolFree(lru_node_pool, lru_node);
256 lru->count -= 1;
e6ccf245 257 lru->setPolicyNode(entry, NULL);
6a566b9c 258 return entry;
259}
260
261static void
262lru_purgeDone(RemovalPurgeWalker * walker)
263{
264 RemovalPolicy *policy = walker->_policy;
e6ccf245 265 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 266 assert(strcmp(policy->_type, "lru") == 0);
267 assert(lru->nwalkers > 0);
268 lru->nwalkers -= 1;
269 safe_free(walker->_data);
270 cbdataFree(walker);
271}
272
273static RemovalPurgeWalker *
274lru_purgeInit(RemovalPolicy * policy, int max_scan)
275{
e6ccf245 276 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 277 RemovalPurgeWalker *walker;
278 LruPurgeData *lru_walk;
279 lru->nwalkers += 1;
72711e31 280 walker = cbdataAlloc(RemovalPurgeWalker);
e6ccf245 281 lru_walk = (LruPurgeData *)xcalloc(1, sizeof(*lru_walk));
6a566b9c 282 walker->_policy = policy;
283 walker->_data = lru_walk;
284 walker->max_scan = max_scan;
285 walker->Next = lru_purgeNext;
286 walker->Done = lru_purgeDone;
287 lru_walk->start = lru_walk->current = (LruNode *) lru->list.head;
6a566b9c 288 return walker;
289}
290
8a3e7d9e 291static void
292lru_stats(RemovalPolicy * policy, StoreEntry * sentry)
293{
e6ccf245 294 LruPolicyData *lru = (LruPolicyData *)policy->_data;
b671cc68 295 LruNode *lru_node = (LruNode *) lru->list.head;
8a3e7d9e 296
62e76326 297again:
298
8a3e7d9e 299 if (lru_node) {
62e76326 300 StoreEntry *entry = (StoreEntry *) lru_node->node.data;
301
302 if (storeEntryLocked(entry)) {
303 lru_node = (LruNode *) lru_node->node.next;
304 goto again;
305 }
306
307 storeAppendPrintf(sentry, "LRU reference age: %.2f days\n", (double) (squid_curtime - entry->lastref) / (double) (24 * 60 * 60));
8a3e7d9e 308 }
309}
310
6a566b9c 311static void
312lru_free(RemovalPolicy * policy)
313{
e6ccf245 314 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 315 /* Make some verification of the policy state */
316 assert(strcmp(policy->_type, "lru") == 0);
317 assert(lru->nwalkers);
318 assert(lru->count);
319 /* Ok, time to destroy this policy */
320 safe_free(policy->_data);
321 memset(policy, 0, sizeof(*policy));
322 cbdataFree(policy);
323}
324
325RemovalPolicy *
9f913cf1 326createRemovalPolicy_lru(wordlist * args)
6a566b9c 327{
328 RemovalPolicy *policy;
329 LruPolicyData *lru_data;
330 /* no arguments expected or understood */
331 assert(!args);
332 /* Initialize */
62e76326 333
d96ceb8e 334 if (!lru_node_pool) {
62e76326 335 lru_node_pool = memPoolCreate("LRU policy node", sizeof(LruNode));
336 memPoolSetChunkSize(lru_node_pool, 512 * 1024);
d96ceb8e 337 }
62e76326 338
6a566b9c 339 /* Allocate the needed structures */
e6ccf245 340 lru_data = (LruPolicyData *)xcalloc(1, sizeof(*lru_data));
62e76326 341
72711e31 342 policy = cbdataAlloc(RemovalPolicy);
62e76326 343
6a566b9c 344 /* Initialize the URL data */
345 lru_data->policy = policy;
62e76326 346
6a566b9c 347 /* Populate the policy structure */
348 policy->_type = "lru";
62e76326 349
6a566b9c 350 policy->_data = lru_data;
62e76326 351
6a566b9c 352 policy->Free = lru_free;
62e76326 353
6a566b9c 354 policy->Add = lru_add;
62e76326 355
6a566b9c 356 policy->Remove = lru_remove;
62e76326 357
6a566b9c 358 policy->Referenced = lru_referenced;
62e76326 359
6a566b9c 360 policy->Dereferenced = lru_referenced;
62e76326 361
6a566b9c 362 policy->WalkInit = lru_walkInit;
62e76326 363
6a566b9c 364 policy->PurgeInit = lru_purgeInit;
62e76326 365
8a3e7d9e 366 policy->Stats = lru_stats;
62e76326 367
6a566b9c 368 /* Increase policy usage count */
369 nr_lru_policies += 0;
62e76326 370
6a566b9c 371 return policy;
372}