2 * Copyright (C) 1996-2018 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.
10 * DEBUG: section 81 Store HEAP Removal Policies
12 * Based on the ideas of the heap policy implemented by John Dilley of
13 * Hewlett Packard. Rewritten from scratch when modularizing the removal
14 * policy implementation of Squid.
16 * For details on the original heap policy work and the thinking behind see
17 * http://www.hpl.hp.com/techreports/1999/HPL-1999-69.html
22 #include "MemObject.h"
24 #include "store_heap_replacement.h"
29 REMOVALPOLICYCREATE createRemovalPolicy_heap
;
31 static int nr_heap_policies
= 0;
33 struct HeapPolicyData
{
34 void setPolicyNode (StoreEntry
*, void *) const;
35 RemovalPolicy
*policy
;
37 heap_key_func
*keyfunc
;
40 enum heap_entry_type
{
41 TYPE_UNKNOWN
= 0, TYPE_STORE_ENTRY
, TYPE_STORE_MEM
45 /* Hack to avoid having to remember the RemovalPolicyNode location.
46 * Needed by the purge walker.
48 static enum HeapPolicyData::heap_entry_type
49 heap_guessType(StoreEntry
* entry
, RemovalPolicyNode
* node
)
51 if (node
== &entry
->repl
)
52 return HeapPolicyData::TYPE_STORE_ENTRY
;
54 if (entry
->mem_obj
&& node
== &entry
->mem_obj
->repl
)
55 return HeapPolicyData::TYPE_STORE_MEM
;
57 fatal("Heap Replacement: Unknown StoreEntry node type");
59 return HeapPolicyData::TYPE_UNKNOWN
;
63 HeapPolicyData::setPolicyNode (StoreEntry
*entry
, void *value
) const
67 case TYPE_STORE_ENTRY
:
68 entry
->repl
.data
= value
;
72 entry
->mem_obj
->repl
.data
= value
;
81 heap_add(RemovalPolicy
* policy
, StoreEntry
* entry
, RemovalPolicyNode
* node
)
83 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
86 if (EBIT_TEST(entry
->flags
, ENTRY_SPECIAL
))
87 return; /* We won't manage these.. they messes things up */
89 node
->data
= heap_insert(h
->theHeap
, entry
);
94 h
->type
= heap_guessType(entry
, node
);
96 /* Add a little more variance to the aging factor */
97 h
->theHeap
->age
+= h
->theHeap
->age
/ 100000000;
101 heap_remove(RemovalPolicy
* policy
, StoreEntry
* entry
,
102 RemovalPolicyNode
* node
)
104 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
105 heap_node
*hnode
= (heap_node
*)node
->data
;
110 heap_delete(h
->theHeap
, hnode
);
118 heap_referenced(RemovalPolicy
* policy
, const StoreEntry
* entry
,
119 RemovalPolicyNode
* node
)
121 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
122 heap_node
*hnode
= (heap_node
*)node
->data
;
127 heap_update(h
->theHeap
, hnode
, (StoreEntry
*) entry
);
130 /** RemovalPolicyWalker **/
132 typedef struct _HeapWalkData HeapWalkData
;
134 struct _HeapWalkData
{
138 static const StoreEntry
*
139 heap_walkNext(RemovalPolicyWalker
* walker
)
141 HeapWalkData
*heap_walk
= (HeapWalkData
*)walker
->_data
;
142 RemovalPolicy
*policy
= walker
->_policy
;
143 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
146 if (heap_walk
->current
>= heap_nodes(h
->theHeap
))
147 return NULL
; /* done */
149 entry
= (StoreEntry
*) heap_peep(h
->theHeap
, heap_walk
->current
++);
155 heap_walkDone(RemovalPolicyWalker
* walker
)
157 RemovalPolicy
*policy
= walker
->_policy
;
158 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
159 assert(strcmp(policy
->_type
, "heap") == 0);
160 assert(h
->nwalkers
> 0);
162 safe_free(walker
->_data
);
166 static RemovalPolicyWalker
*
167 heap_walkInit(RemovalPolicy
* policy
)
169 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
170 RemovalPolicyWalker
*walker
;
171 HeapWalkData
*heap_walk
;
173 walker
= new RemovalPolicyWalker
;
174 heap_walk
= (HeapWalkData
*)xcalloc(1, sizeof(*heap_walk
));
175 heap_walk
->current
= 0;
176 walker
->_policy
= policy
;
177 walker
->_data
= heap_walk
;
178 walker
->Next
= heap_walkNext
;
179 walker
->Done
= heap_walkDone
;
183 /** RemovalPurgeWalker **/
188 std::queue
<StoreEntry
*> locked_entries
;
189 heap_key min_age
= 0.0;
193 heap_purgeNext(RemovalPurgeWalker
* walker
)
195 HeapPurgeData
*heap_walker
= (HeapPurgeData
*)walker
->_data
;
196 RemovalPolicy
*policy
= walker
->_policy
;
197 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
203 if (heap_empty(h
->theHeap
))
204 return NULL
; /* done */
206 age
= heap_peepminkey(h
->theHeap
);
208 entry
= (StoreEntry
*)heap_extractmin(h
->theHeap
);
210 if (entry
->locked()) {
212 entry
->lock("heap_purgeNext");
213 heap_walker
->locked_entries
.push(entry
);
218 heap_walker
->min_age
= age
;
219 h
->setPolicyNode(entry
, NULL
);
224 heap_purgeDone(RemovalPurgeWalker
* walker
)
226 HeapPurgeData
*heap_walker
= (HeapPurgeData
*)walker
->_data
;
227 RemovalPolicy
*policy
= walker
->_policy
;
228 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
229 assert(strcmp(policy
->_type
, "heap") == 0);
230 assert(h
->nwalkers
> 0);
233 if (heap_walker
->min_age
> 0) {
234 h
->theHeap
->age
= heap_walker
->min_age
;
235 debugs(81, 3, "Heap age set to " << h
->theHeap
->age
);
238 // Reinsert the locked entries
239 while (!heap_walker
->locked_entries
.empty()) {
240 StoreEntry
*entry
= heap_walker
->locked_entries
.front();
241 heap_node
*node
= heap_insert(h
->theHeap
, entry
);
242 h
->setPolicyNode(entry
, node
);
243 entry
->unlock("heap_purgeDone");
244 heap_walker
->locked_entries
.pop();
251 static RemovalPurgeWalker
*
252 heap_purgeInit(RemovalPolicy
* policy
, int max_scan
)
254 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
255 RemovalPurgeWalker
*walker
;
256 HeapPurgeData
*heap_walk
;
258 walker
= new RemovalPurgeWalker
;
259 heap_walk
= new HeapPurgeData
;
260 walker
->_policy
= policy
;
261 walker
->_data
= heap_walk
;
262 walker
->max_scan
= max_scan
;
263 walker
->Next
= heap_purgeNext
;
264 walker
->Done
= heap_purgeDone
;
269 heap_free(RemovalPolicy
* policy
)
271 HeapPolicyData
*h
= (HeapPolicyData
*)policy
->_data
;
272 /* Make some verification of the policy state */
273 assert(strcmp(policy
->_type
, "heap") == 0);
276 /* Ok, time to destroy this policy */
278 memset(policy
, 0, sizeof(*policy
));
283 createRemovalPolicy_heap(wordlist
* args
)
285 RemovalPolicy
*policy
;
286 HeapPolicyData
*heap_data
;
288 /* Allocate the needed structures */
289 policy
= new RemovalPolicy
;
290 heap_data
= (HeapPolicyData
*)xcalloc(1, sizeof(*heap_data
));
291 /* Initialize the policy data */
292 heap_data
->policy
= policy
;
298 debugs(81, DBG_IMPORTANT
, "createRemovalPolicy_heap: No key type specified. Using LRU");
302 if (!strcmp(keytype
, "GDSF"))
303 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_GDSF
;
304 else if (!strcmp(keytype
, "LFUDA"))
305 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_LFUDA
;
306 else if (!strcmp(keytype
, "LRU"))
307 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_LRU
;
309 debugs(81, DBG_CRITICAL
, "createRemovalPolicy_heap: Unknown key type \"" << keytype
<< "\". Using LRU");
310 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_LRU
;
313 /* No additional arguments expected */
315 debugs(81, DBG_IMPORTANT
, "WARNING: discarding unknown removal policy '" << args
->key
<< "'");
319 heap_data
->theHeap
= new_heap(1000, heap_data
->keyfunc
);
321 heap_data
->theHeap
->age
= 1.0;
323 /* Populate the policy structure */
324 policy
->_type
= "heap";
326 policy
->_data
= heap_data
;
328 policy
->Free
= heap_free
;
330 policy
->Add
= heap_add
;
332 policy
->Remove
= heap_remove
;
334 policy
->Referenced
= NULL
;
336 policy
->Dereferenced
= heap_referenced
;
338 policy
->WalkInit
= heap_walkInit
;
340 policy
->PurgeInit
= heap_purgeInit
;
342 /* Increase policy usage count */
343 nr_heap_policies
+= 0;