2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: none LRU Removal Policy */
12 #include "MemObject.h"
15 /* because LruNode use explicit memory alloc()/freeOne() calls.
16 * XXX: convert to MEMPROXY_CLASS() API
20 REMOVALPOLICYCREATE createRemovalPolicy_lru
;
22 struct LruPolicyData
{
23 void setPolicyNode (StoreEntry
*, void *) const;
24 RemovalPolicy
*policy
;
28 enum heap_entry_type
{
29 TYPE_UNKNOWN
= 0, TYPE_STORE_ENTRY
, TYPE_STORE_MEM
33 /* Hack to avoid having to remember the RemovalPolicyNode location.
34 * Needed by the purge walker to clear the policy information
36 static enum LruPolicyData::heap_entry_type
37 repl_guessType(StoreEntry
* entry
, RemovalPolicyNode
* node
)
39 if (node
== &entry
->repl
)
40 return LruPolicyData::TYPE_STORE_ENTRY
;
42 if (entry
->mem_obj
&& node
== &entry
->mem_obj
->repl
)
43 return LruPolicyData::TYPE_STORE_MEM
;
45 fatal("Heap Replacement: Unknown StoreEntry node type");
47 return LruPolicyData::TYPE_UNKNOWN
;
51 LruPolicyData::setPolicyNode (StoreEntry
*entry
, void *value
) const
55 case TYPE_STORE_ENTRY
:
56 entry
->repl
.data
= value
;
60 entry
->mem_obj
->repl
.data
= value
;
68 typedef struct _LruNode LruNode
;
71 /* Note: the dlink_node MUST be the first member of the LruNode
72 * structure. This member is later pointer typecasted to LruNode *.
77 static Mem::Allocator
*lru_node_pool
= nullptr;
78 static int nr_lru_policies
= 0;
81 lru_add(RemovalPolicy
* policy
, StoreEntry
* entry
, RemovalPolicyNode
* node
)
83 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
86 node
->data
= lru_node
= (LruNode
*)lru_node_pool
->alloc();
87 dlinkAddTail(entry
, &lru_node
->node
, &lru
->list
);
91 lru
->type
= repl_guessType(entry
, node
);
95 lru_remove(RemovalPolicy
* policy
, StoreEntry
* entry
, RemovalPolicyNode
* node
)
97 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
98 LruNode
*lru_node
= (LruNode
*)node
->data
;
104 * It seems to be possible for an entry to exist in the hash
105 * but not be in the LRU list, so check for that case rather
106 * than suffer a NULL pointer access.
108 if (nullptr == lru_node
->node
.data
)
111 assert(lru_node
->node
.data
== entry
);
113 node
->data
= nullptr;
115 dlinkDelete(&lru_node
->node
, &lru
->list
);
117 lru_node_pool
->freeOne(lru_node
);
123 lru_referenced(RemovalPolicy
* policy
, const StoreEntry
* entry
,
124 RemovalPolicyNode
* node
)
126 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
127 LruNode
*lru_node
= (LruNode
*)node
->data
;
132 dlinkDelete(&lru_node
->node
, &lru
->list
);
134 dlinkAddTail((void *) entry
, &lru_node
->node
, &lru
->list
);
137 /** RemovalPolicyWalker **/
139 typedef struct _LruWalkData LruWalkData
;
141 struct _LruWalkData
{
145 static const StoreEntry
*
146 lru_walkNext(RemovalPolicyWalker
* walker
)
148 LruWalkData
*lru_walk
= (LruWalkData
*)walker
->_data
;
149 LruNode
*lru_node
= lru_walk
->current
;
154 lru_walk
->current
= (LruNode
*) lru_node
->node
.next
;
156 return (StoreEntry
*) lru_node
->node
.data
;
160 lru_walkDone(RemovalPolicyWalker
* walker
)
162 RemovalPolicy
*policy
= walker
->_policy
;
163 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
164 assert(strcmp(policy
->_type
, "lru") == 0);
165 assert(lru
->nwalkers
> 0);
167 safe_free(walker
->_data
);
171 static RemovalPolicyWalker
*
172 lru_walkInit(RemovalPolicy
* policy
)
174 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
175 RemovalPolicyWalker
*walker
;
176 LruWalkData
*lru_walk
;
178 walker
= new RemovalPolicyWalker
;
179 lru_walk
= (LruWalkData
*)xcalloc(1, sizeof(*lru_walk
));
180 walker
->_policy
= policy
;
181 walker
->_data
= lru_walk
;
182 walker
->Next
= lru_walkNext
;
183 walker
->Done
= lru_walkDone
;
184 lru_walk
->current
= (LruNode
*) lru
->list
.head
;
188 /** RemovalPurgeWalker **/
190 typedef struct _LruPurgeData LruPurgeData
;
192 struct _LruPurgeData
{
198 lru_purgeNext(RemovalPurgeWalker
* walker
)
200 LruPurgeData
*lru_walker
= (LruPurgeData
*)walker
->_data
;
201 RemovalPolicy
*policy
= walker
->_policy
;
202 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
207 lru_node
= lru_walker
->current
;
209 if (!lru_node
|| walker
->scanned
>= walker
->max_scan
)
212 walker
->scanned
+= 1;
214 lru_walker
->current
= (LruNode
*) lru_node
->node
.next
;
216 if (lru_walker
->current
== lru_walker
->start
) {
217 /* Last node found */
218 lru_walker
->current
= nullptr;
221 entry
= (StoreEntry
*) lru_node
->node
.data
;
222 dlinkDelete(&lru_node
->node
, &lru
->list
);
224 if (entry
->locked()) {
225 /* Shit, it is locked. we can't return this one */
227 dlinkAddTail(entry
, &lru_node
->node
, &lru
->list
);
231 lru_node_pool
->freeOne(lru_node
);
233 lru
->setPolicyNode(entry
, nullptr);
238 lru_purgeDone(RemovalPurgeWalker
* walker
)
240 RemovalPolicy
*policy
= walker
->_policy
;
241 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
242 assert(strcmp(policy
->_type
, "lru") == 0);
243 assert(lru
->nwalkers
> 0);
245 safe_free(walker
->_data
);
249 static RemovalPurgeWalker
*
250 lru_purgeInit(RemovalPolicy
* policy
, int max_scan
)
252 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
253 RemovalPurgeWalker
*walker
;
254 LruPurgeData
*lru_walk
;
256 walker
= new RemovalPurgeWalker
;
257 lru_walk
= (LruPurgeData
*)xcalloc(1, sizeof(*lru_walk
));
258 walker
->_policy
= policy
;
259 walker
->_data
= lru_walk
;
260 walker
->max_scan
= max_scan
;
261 walker
->Next
= lru_purgeNext
;
262 walker
->Done
= lru_purgeDone
;
263 lru_walk
->start
= lru_walk
->current
= (LruNode
*) lru
->list
.head
;
268 lru_stats(RemovalPolicy
* policy
, StoreEntry
* sentry
)
270 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
271 LruNode
*lru_node
= (LruNode
*) lru
->list
.head
;
276 StoreEntry
*entry
= (StoreEntry
*) lru_node
->node
.data
;
278 if (entry
->locked()) {
279 lru_node
= (LruNode
*) lru_node
->node
.next
;
283 storeAppendPrintf(sentry
, "LRU reference age: %.2f days\n", (double) (squid_curtime
- entry
->lastref
) / (double) (24 * 60 * 60));
288 lru_free(RemovalPolicy
* policy
)
290 LruPolicyData
*lru
= (LruPolicyData
*)policy
->_data
;
291 /* Make some verification of the policy state */
292 assert(strcmp(policy
->_type
, "lru") == 0);
293 assert(lru
->nwalkers
);
295 /* Ok, time to destroy this policy */
297 memset(policy
, 0, sizeof(*policy
));
302 createRemovalPolicy_lru(wordlist
* args
)
304 RemovalPolicy
*policy
;
305 LruPolicyData
*lru_data
;
306 /* no arguments expected or understood */
310 if (!lru_node_pool
) {
311 /* Must be chunked */
312 lru_node_pool
= memPoolCreate("LRU policy node", sizeof(LruNode
));
313 lru_node_pool
->setChunkSize(512 * 1024);
316 /* Allocate the needed structures */
317 lru_data
= (LruPolicyData
*)xcalloc(1, sizeof(*lru_data
));
319 policy
= new RemovalPolicy
;
321 /* Initialize the URL data */
322 lru_data
->policy
= policy
;
324 /* Populate the policy structure */
325 policy
->_type
= "lru";
327 policy
->_data
= lru_data
;
329 policy
->Free
= lru_free
;
331 policy
->Add
= lru_add
;
333 policy
->Remove
= lru_remove
;
335 policy
->Referenced
= lru_referenced
;
337 policy
->Dereferenced
= lru_referenced
;
339 policy
->WalkInit
= lru_walkInit
;
341 policy
->PurgeInit
= lru_purgeInit
;
343 policy
->Stats
= lru_stats
;
345 /* Increase policy usage count */
346 nr_lru_policies
+= 0;