3 * $Id: store_repl_heap.cc,v 1.16 2004/12/20 14:52:32 robertc Exp $
5 * DEBUG: section ? HEAP based removal policies
6 * AUTHOR: Henrik Nordstrom
8 * Based on the ideas of the heap policy implemented by John Dilley of
9 * Hewlett Packard. Rewritten from scratch when modularizing the removal
10 * policy implementation of Squid.
12 * For details on the original heap policy work and the thinking behind see
13 * http://www.hpl.hp.com/techreports/1999/HPL-1999-69.html
16 * SQUID Web Proxy Cache http://www.squid-cache.org/
17 * ----------------------------------------------------------
19 * Squid is the result of efforts by numerous individuals from
20 * the Internet community; see the CONTRIBUTORS file for full
21 * details. Many organizations have provided support for Squid's
22 * development; see the SPONSORS file for full details. Squid is
23 * Copyrighted (C) 2001 by the Regents of the University of
24 * California; see the COPYRIGHT file for full details. Squid
25 * incorporates software developed and/or copyrighted by other
26 * sources; see the CREDITS file for full details.
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
46 #include "store_heap_replacement.h"
48 #include "MemObject.h"
50 REMOVALPOLICYCREATE createRemovalPolicy_heap
;
52 static int nr_heap_policies
= 0;
56 void setPolicyNode (StoreEntry
*, void *) const;
57 RemovalPolicy
*policy
;
59 heap_key_func
*keyfunc
;
62 enum heap_entry_type
{
63 TYPE_UNKNOWN
= 0, TYPE_STORE_ENTRY
, TYPE_STORE_MEM
67 /* Hack to avoid having to remember the RemovalPolicyNode location.
68 * Needed by the purge walker.
70 static enum HeapPolicyData::heap_entry_type
71 heap_guessType(StoreEntry
* entry
, RemovalPolicyNode
* node
)
73 if (node
== &entry
->repl
)
74 return HeapPolicyData::TYPE_STORE_ENTRY
;
76 if (entry
->mem_obj
&& node
== &entry
->mem_obj
->repl
)
77 return HeapPolicyData::TYPE_STORE_MEM
;
79 fatal("Heap Replacement: Unknown StoreEntry node type");
81 return HeapPolicyData::TYPE_UNKNOWN
;
85 HeapPolicyData::setPolicyNode (StoreEntry
*entry
, void *value
) const
89 case TYPE_STORE_ENTRY
:
90 entry
->repl
.data
= value
;
94 entry
->mem_obj
->repl
.data
= value
;
103 heap_add(RemovalPolicy
* policy
, StoreEntry
* entry
, RemovalPolicyNode
* node
)
105 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
108 if (EBIT_TEST(entry
->flags
, ENTRY_SPECIAL
))
109 return; /* We won't manage these.. they messes things up */
111 node
->data
= heap_insert(heap
->theHeap
, entry
);
116 heap
->type
= heap_guessType(entry
, node
);
118 /* Add a little more variance to the aging factor */
119 heap
->theHeap
->age
+= heap
->theHeap
->age
/ 100000000;
123 heap_remove(RemovalPolicy
* policy
, StoreEntry
* entry
,
124 RemovalPolicyNode
* node
)
126 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
127 heap_node
*hnode
= (heap_node
*)node
->data
;
132 heap_delete(heap
->theHeap
, hnode
);
140 heap_referenced(RemovalPolicy
* policy
, const StoreEntry
* entry
,
141 RemovalPolicyNode
* node
)
143 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
144 heap_node
*hnode
= (heap_node
*)node
->data
;
149 heap_update(heap
->theHeap
, hnode
, (StoreEntry
*) entry
);
152 /** RemovalPolicyWalker **/
154 typedef struct _HeapWalkData HeapWalkData
;
161 static const StoreEntry
*
162 heap_walkNext(RemovalPolicyWalker
* walker
)
164 HeapWalkData
*heap_walk
= (HeapWalkData
*)walker
->_data
;
165 RemovalPolicy
*policy
= walker
->_policy
;
166 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
169 if (heap_walk
->current
>= heap_nodes(heap
->theHeap
))
170 return NULL
; /* done */
172 entry
= (StoreEntry
*) heap_peep(heap
->theHeap
, heap_walk
->current
++);
178 heap_walkDone(RemovalPolicyWalker
* walker
)
180 RemovalPolicy
*policy
= walker
->_policy
;
181 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
182 assert(strcmp(policy
->_type
, "heap") == 0);
183 assert(heap
->nwalkers
> 0);
185 safe_free(walker
->_data
);
189 static RemovalPolicyWalker
*
190 heap_walkInit(RemovalPolicy
* policy
)
192 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
193 RemovalPolicyWalker
*walker
;
194 HeapWalkData
*heap_walk
;
196 walker
= cbdataAlloc(RemovalPolicyWalker
);
197 heap_walk
= (HeapWalkData
*)xcalloc(1, sizeof(*heap_walk
));
198 heap_walk
->current
= 0;
199 walker
->_policy
= policy
;
200 walker
->_data
= heap_walk
;
201 walker
->Next
= heap_walkNext
;
202 walker
->Done
= heap_walkDone
;
206 /** RemovalPurgeWalker **/
208 typedef struct _HeapPurgeData HeapPurgeData
;
210 struct _HeapPurgeData
212 link_list
*locked_entries
;
217 heap_purgeNext(RemovalPurgeWalker
* walker
)
219 HeapPurgeData
*heap_walker
= (HeapPurgeData
*)walker
->_data
;
220 RemovalPolicy
*policy
= walker
->_policy
;
221 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
227 if (!heap_nodes(heap
->theHeap
) > 0)
228 return NULL
; /* done */
230 age
= heap_peepminkey(heap
->theHeap
);
232 entry
= (StoreEntry
*)heap_extractmin(heap
->theHeap
);
234 if (storeEntryLocked(entry
)) {
235 storeLockObject(entry
);
236 linklistPush(&heap_walker
->locked_entries
, entry
);
240 heap_walker
->min_age
= age
;
241 heap
->setPolicyNode(entry
, NULL
);
246 heap_purgeDone(RemovalPurgeWalker
* walker
)
248 HeapPurgeData
*heap_walker
= (HeapPurgeData
*)walker
->_data
;
249 RemovalPolicy
*policy
= walker
->_policy
;
250 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
252 assert(strcmp(policy
->_type
, "heap") == 0);
253 assert(heap
->nwalkers
> 0);
256 if (heap_walker
->min_age
> 0) {
257 heap
->theHeap
->age
= heap_walker
->min_age
;
258 debug(81, 3) ("heap_purgeDone: Heap age set to %f\n",
259 (double) heap
->theHeap
->age
);
263 * Reinsert the locked entries
265 while ((entry
= (StoreEntry
*)linklistShift(&heap_walker
->locked_entries
))) {
266 heap_node
*node
= heap_insert(heap
->theHeap
, entry
);
267 heap
->setPolicyNode(entry
, node
);
268 storeUnlockObject(entry
);
271 safe_free(walker
->_data
);
275 static RemovalPurgeWalker
*
276 heap_purgeInit(RemovalPolicy
* policy
, int max_scan
)
278 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
279 RemovalPurgeWalker
*walker
;
280 HeapPurgeData
*heap_walk
;
282 walker
= cbdataAlloc(RemovalPurgeWalker
);
283 heap_walk
= (HeapPurgeData
*)xcalloc(1, sizeof(*heap_walk
));
284 heap_walk
->min_age
= 0.0;
285 heap_walk
->locked_entries
= NULL
;
286 walker
->_policy
= policy
;
287 walker
->_data
= heap_walk
;
288 walker
->max_scan
= max_scan
;
289 walker
->Next
= heap_purgeNext
;
290 walker
->Done
= heap_purgeDone
;
295 heap_free(RemovalPolicy
* policy
)
297 HeapPolicyData
*heap
= (HeapPolicyData
*)policy
->_data
;
298 /* Make some verification of the policy state */
299 assert(strcmp(policy
->_type
, "heap") == 0);
300 assert(heap
->nwalkers
);
302 /* Ok, time to destroy this policy */
304 memset(policy
, 0, sizeof(*policy
));
309 createRemovalPolicy_heap(wordlist
* args
)
311 RemovalPolicy
*policy
;
312 HeapPolicyData
*heap_data
;
314 /* Allocate the needed structures */
315 policy
= cbdataAlloc(RemovalPolicy
);
316 heap_data
= (HeapPolicyData
*)xcalloc(1, sizeof(*heap_data
));
317 /* Initialize the policy data */
318 heap_data
->policy
= policy
;
324 debug(81, 1) ("createRemovalPolicy_heap: No key type specified. Using LRU\n");
328 if (!strcmp(keytype
, "GDSF"))
329 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_GDSF
;
330 else if (!strcmp(keytype
, "LFUDA"))
331 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_LFUDA
;
332 else if (!strcmp(keytype
, "LRU"))
333 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_LRU
;
335 debug(81, 0) ("createRemovalPolicy_heap: Unknown key type \"%s\". Using LRU\n",
337 heap_data
->keyfunc
= HeapKeyGen_StoreEntry_LRU
;
340 /* No additional arguments expected */
343 heap_data
->theHeap
= new_heap(1000, heap_data
->keyfunc
);
345 heap_data
->theHeap
->age
= 1.0;
347 /* Populate the policy structure */
348 policy
->_type
= "heap";
350 policy
->_data
= heap_data
;
352 policy
->Free
= heap_free
;
354 policy
->Add
= heap_add
;
356 policy
->Remove
= heap_remove
;
358 policy
->Referenced
= NULL
;
360 policy
->Dereferenced
= heap_referenced
;
362 policy
->WalkInit
= heap_walkInit
;
364 policy
->PurgeInit
= heap_purgeInit
;
366 /* Increase policy usage count */
367 nr_heap_policies
+= 0;