5 * DEBUG: none LRU Removal Policy
6 * AUTHOR: Henrik Nordstrom
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
36 #include "squid-old.h"
38 #include "MemObject.h"
39 #include "SquidTime.h"
41 REMOVALPOLICYCREATE createRemovalPolicy_lru
;
43 struct LruPolicyData
{
44 void setPolicyNode (StoreEntry
*, void *) const;
45 RemovalPolicy
*policy
;
49 enum heap_entry_type
{
50 TYPE_UNKNOWN
= 0, TYPE_STORE_ENTRY
, TYPE_STORE_MEM
54 /* Hack to avoid having to remember the RemovalPolicyNode location.
55 * Needed by the purge walker to clear the policy information
57 static enum LruPolicyData::heap_entry_type
58 repl_guessType(StoreEntry
* entry
, RemovalPolicyNode
* node
)
60 if (node
== &entry
->repl
)
61 return LruPolicyData::TYPE_STORE_ENTRY
;
63 if (entry
->mem_obj
&& node
== &entry
->mem_obj
->repl
)
64 return LruPolicyData::TYPE_STORE_MEM
;
66 fatal("Heap Replacement: Unknown StoreEntry node type");
68 return LruPolicyData::TYPE_UNKNOWN
;
72 LruPolicyData::setPolicyNode (StoreEntry
*entry
, void *value
) const
76 case TYPE_STORE_ENTRY
:
77 entry
->repl
.data
= value
;
81 entry
->mem_obj
->repl
.data
= value
;
89 typedef struct _LruNode LruNode
;
92 /* Note: the dlink_node MUST be the first member of the LruNode
93 * structure. This member is later pointer typecasted to LruNode *.
98 static MemAllocator
*lru_node_pool
= NULL
;
99 static int nr_lru_policies
= 0;
102 lru_add(RemovalPolicy
* policy
, StoreEntry
* entry
, RemovalPolicyNode
* node
)
104 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
107 node
->data
= lru_node
= (LruNode
*)lru_node_pool
->alloc();
108 dlinkAddTail(entry
, &lru_node
->node
, &lru
->list
);
112 lru
->type
= repl_guessType(entry
, node
);
116 lru_remove(RemovalPolicy
* policy
, StoreEntry
* entry
, RemovalPolicyNode
* node
)
118 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
119 LruNode
*lru_node
= (LruNode
*)node
->data
;
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.
129 if (NULL
== lru_node
->node
.data
)
132 assert(lru_node
->node
.data
== entry
);
136 dlinkDelete(&lru_node
->node
, &lru
->list
);
138 lru_node_pool
->freeOne(lru_node
);
144 lru_referenced(RemovalPolicy
* policy
, const StoreEntry
* entry
,
145 RemovalPolicyNode
* node
)
147 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
148 LruNode
*lru_node
= (LruNode
*)node
->data
;
153 dlinkDelete(&lru_node
->node
, &lru
->list
);
155 dlinkAddTail((void *) entry
, &lru_node
->node
, &lru
->list
);
158 /** RemovalPolicyWalker **/
160 typedef struct _LruWalkData LruWalkData
;
162 struct _LruWalkData
{
166 static const StoreEntry
*
167 lru_walkNext(RemovalPolicyWalker
* walker
)
169 LruWalkData
*lru_walk
= (LruWalkData
*)walker
->_data
;
170 LruNode
*lru_node
= lru_walk
->current
;
175 lru_walk
->current
= (LruNode
*) lru_node
->node
.next
;
177 return (StoreEntry
*) lru_node
->node
.data
;
181 lru_walkDone(RemovalPolicyWalker
* walker
)
183 RemovalPolicy
*policy
= walker
->_policy
;
184 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
185 assert(strcmp(policy
->_type
, "lru") == 0);
186 assert(lru
->nwalkers
> 0);
188 safe_free(walker
->_data
);
192 static RemovalPolicyWalker
*
193 lru_walkInit(RemovalPolicy
* policy
)
195 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
196 RemovalPolicyWalker
*walker
;
197 LruWalkData
*lru_walk
;
199 walker
= new RemovalPolicyWalker
;
200 lru_walk
= (LruWalkData
*)xcalloc(1, sizeof(*lru_walk
));
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
;
209 /** RemovalPurgeWalker **/
211 typedef struct _LruPurgeData LruPurgeData
;
213 struct _LruPurgeData
{
219 lru_purgeNext(RemovalPurgeWalker
* walker
)
221 LruPurgeData
*lru_walker
= (LruPurgeData
*)walker
->_data
;
222 RemovalPolicy
*policy
= walker
->_policy
;
223 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
228 lru_node
= lru_walker
->current
;
230 if (!lru_node
|| walker
->scanned
>= walker
->max_scan
)
233 walker
->scanned
+= 1;
235 lru_walker
->current
= (LruNode
*) lru_node
->node
.next
;
237 if (lru_walker
->current
== lru_walker
->start
) {
238 /* Last node found */
239 lru_walker
->current
= NULL
;
242 entry
= (StoreEntry
*) lru_node
->node
.data
;
243 dlinkDelete(&lru_node
->node
, &lru
->list
);
245 if (entry
->locked()) {
246 /* Shit, it is locked. we can't return this one */
248 dlinkAddTail(entry
, &lru_node
->node
, &lru
->list
);
252 lru_node_pool
->freeOne(lru_node
);
254 lru
->setPolicyNode(entry
, NULL
);
259 lru_purgeDone(RemovalPurgeWalker
* walker
)
261 RemovalPolicy
*policy
= walker
->_policy
;
262 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
263 assert(strcmp(policy
->_type
, "lru") == 0);
264 assert(lru
->nwalkers
> 0);
266 safe_free(walker
->_data
);
270 static RemovalPurgeWalker
*
271 lru_purgeInit(RemovalPolicy
* policy
, int max_scan
)
273 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
274 RemovalPurgeWalker
*walker
;
275 LruPurgeData
*lru_walk
;
277 walker
= new RemovalPurgeWalker
;
278 lru_walk
= (LruPurgeData
*)xcalloc(1, sizeof(*lru_walk
));
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
;
289 lru_stats(RemovalPolicy
* policy
, StoreEntry
* sentry
)
291 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
292 LruNode
*lru_node
= (LruNode
*) lru
->list
.head
;
297 StoreEntry
*entry
= (StoreEntry
*) lru_node
->node
.data
;
299 if (entry
->locked()) {
300 lru_node
= (LruNode
*) lru_node
->node
.next
;
304 storeAppendPrintf(sentry
, "LRU reference age: %.2f days\n", (double) (squid_curtime
- entry
->lastref
) / (double) (24 * 60 * 60));
309 lru_free(RemovalPolicy
* policy
)
311 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
312 /* Make some verification of the policy state */
313 assert(strcmp(policy
->_type
, "lru") == 0);
314 assert(lru
->nwalkers
);
316 /* Ok, time to destroy this policy */
318 memset(policy
, 0, sizeof(*policy
));
323 createRemovalPolicy_lru(wordlist
* args
)
325 RemovalPolicy
*policy
;
326 LruPolicyData
*lru_data
;
327 /* no arguments expected or understood */
331 if (!lru_node_pool
) {
332 /* Must be chunked */
333 lru_node_pool
= memPoolCreate("LRU policy node", sizeof(LruNode
));
334 lru_node_pool
->setChunkSize(512 * 1024);
337 /* Allocate the needed structures */
338 lru_data
= (LruPolicyData
*)xcalloc(1, sizeof(*lru_data
));
340 policy
= new RemovalPolicy
;
342 /* Initialize the URL data */
343 lru_data
->policy
= policy
;
345 /* Populate the policy structure */
346 policy
->_type
= "lru";
348 policy
->_data
= lru_data
;
350 policy
->Free
= lru_free
;
352 policy
->Add
= lru_add
;
354 policy
->Remove
= lru_remove
;
356 policy
->Referenced
= lru_referenced
;
358 policy
->Dereferenced
= lru_referenced
;
360 policy
->WalkInit
= lru_walkInit
;
362 policy
->PurgeInit
= lru_purgeInit
;
364 policy
->Stats
= lru_stats
;
366 /* Increase policy usage count */
367 nr_lru_policies
+= 0;