]>
Commit | Line | Data |
---|---|---|
6a566b9c | 1 | |
2 | /* | |
262a0e14 | 3 | * $Id$ |
6a566b9c | 4 | * |
507d0a78 | 5 | * DEBUG: section 81 Store HEAP Removal Policies |
6a566b9c | 6 | * AUTHOR: Henrik Nordstrom |
7 | * | |
b6a7f52c | 8 | * Based on the ideas of the heap policy implemented by John Dilley of |
6095abfe | 9 | * Hewlett Packard. Rewritten from scratch when modularizing the removal |
10 | * policy implementation of Squid. | |
b6a7f52c | 11 | * |
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 | |
14 | * | |
26ac0430 | 15 | * |
2b6662ba | 16 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
6a566b9c | 17 | * ---------------------------------------------------------- |
18 | * | |
2b6662ba | 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. | |
6a566b9c | 27 | * |
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. | |
26ac0430 | 32 | * |
6a566b9c | 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. | |
26ac0430 | 37 | * |
6a566b9c | 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. | |
41 | * | |
42 | */ | |
43 | ||
44 | #include "squid.h" | |
45 | #include "heap.h" | |
46 | #include "store_heap_replacement.h" | |
e6ccf245 | 47 | #include "Store.h" |
528b2c61 | 48 | #include "MemObject.h" |
d295d770 | 49 | #include "wordlist.h" |
6a566b9c | 50 | |
51 | REMOVALPOLICYCREATE createRemovalPolicy_heap; | |
52 | ||
53 | static int nr_heap_policies = 0; | |
54 | ||
26ac0430 | 55 | struct HeapPolicyData { |
e6ccf245 | 56 | void setPolicyNode (StoreEntry *, void *) const; |
6a566b9c | 57 | RemovalPolicy *policy; |
e6ccf245 | 58 | heap *theHeap; |
6a566b9c | 59 | heap_key_func *keyfunc; |
60 | int count; | |
61 | int nwalkers; | |
be1ed2a4 | 62 | enum heap_entry_type { |
62e76326 | 63 | TYPE_UNKNOWN = 0, TYPE_STORE_ENTRY, TYPE_STORE_MEM |
be1ed2a4 | 64 | } type; |
6a566b9c | 65 | }; |
66 | ||
67 | /* Hack to avoid having to remember the RemovalPolicyNode location. | |
68 | * Needed by the purge walker. | |
69 | */ | |
e6ccf245 | 70 | static enum HeapPolicyData::heap_entry_type |
6a566b9c | 71 | heap_guessType(StoreEntry * entry, RemovalPolicyNode * node) |
72 | { | |
73 | if (node == &entry->repl) | |
62e76326 | 74 | return HeapPolicyData::TYPE_STORE_ENTRY; |
75 | ||
6a566b9c | 76 | if (entry->mem_obj && node == &entry->mem_obj->repl) |
62e76326 | 77 | return HeapPolicyData::TYPE_STORE_MEM; |
78 | ||
6a566b9c | 79 | fatal("Heap Replacement: Unknown StoreEntry node type"); |
62e76326 | 80 | |
e6ccf245 | 81 | return HeapPolicyData::TYPE_UNKNOWN; |
6a566b9c | 82 | } |
e6ccf245 | 83 | |
84 | void | |
85 | HeapPolicyData::setPolicyNode (StoreEntry *entry, void *value) const | |
86 | { | |
87 | switch (type) { | |
62e76326 | 88 | |
89 | case TYPE_STORE_ENTRY: | |
90 | entry->repl.data = value; | |
91 | break ; | |
92 | ||
93 | case TYPE_STORE_MEM: | |
94 | entry->mem_obj->repl.data = value ; | |
95 | break ; | |
96 | ||
97 | default: | |
98 | break; | |
6a566b9c | 99 | } |
e6ccf245 | 100 | } |
6a566b9c | 101 | |
102 | static void | |
103 | heap_add(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node) | |
104 | { | |
e6ccf245 | 105 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 106 | assert(!node->data); |
62e76326 | 107 | |
6a566b9c | 108 | if (EBIT_TEST(entry->flags, ENTRY_SPECIAL)) |
62e76326 | 109 | return; /* We won't manage these.. they messes things up */ |
110 | ||
e6ccf245 | 111 | node->data = heap_insert(heap->theHeap, entry); |
62e76326 | 112 | |
6a566b9c | 113 | heap->count += 1; |
62e76326 | 114 | |
6a566b9c | 115 | if (!heap->type) |
62e76326 | 116 | heap->type = heap_guessType(entry, node); |
117 | ||
6a566b9c | 118 | /* Add a little more variance to the aging factor */ |
e6ccf245 | 119 | heap->theHeap->age += heap->theHeap->age / 100000000; |
6a566b9c | 120 | } |
121 | ||
122 | static void | |
123 | heap_remove(RemovalPolicy * policy, StoreEntry * entry, | |
62e76326 | 124 | RemovalPolicyNode * node) |
6a566b9c | 125 | { |
e6ccf245 | 126 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
127 | heap_node *hnode = (heap_node *)node->data; | |
62e76326 | 128 | |
6a566b9c | 129 | if (!hnode) |
62e76326 | 130 | return; |
131 | ||
e6ccf245 | 132 | heap_delete(heap->theHeap, hnode); |
62e76326 | 133 | |
6a566b9c | 134 | node->data = NULL; |
62e76326 | 135 | |
6a566b9c | 136 | heap->count -= 1; |
137 | } | |
138 | ||
139 | static void | |
140 | heap_referenced(RemovalPolicy * policy, const StoreEntry * entry, | |
62e76326 | 141 | RemovalPolicyNode * node) |
6a566b9c | 142 | { |
e6ccf245 | 143 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
144 | heap_node *hnode = (heap_node *)node->data; | |
62e76326 | 145 | |
6a566b9c | 146 | if (!hnode) |
62e76326 | 147 | return; |
148 | ||
e6ccf245 | 149 | heap_update(heap->theHeap, hnode, (StoreEntry *) entry); |
6a566b9c | 150 | } |
151 | ||
152 | /** RemovalPolicyWalker **/ | |
153 | ||
154 | typedef struct _HeapWalkData HeapWalkData; | |
62e76326 | 155 | |
26ac0430 | 156 | struct _HeapWalkData { |
e6ccf245 | 157 | size_t current; |
6a566b9c | 158 | }; |
159 | ||
2d72d4fd | 160 | static const StoreEntry * |
6a566b9c | 161 | heap_walkNext(RemovalPolicyWalker * walker) |
162 | { | |
e6ccf245 | 163 | HeapWalkData *heap_walk = (HeapWalkData *)walker->_data; |
6a566b9c | 164 | RemovalPolicy *policy = walker->_policy; |
e6ccf245 | 165 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 166 | StoreEntry *entry; |
62e76326 | 167 | |
e6ccf245 | 168 | if (heap_walk->current >= heap_nodes(heap->theHeap)) |
62e76326 | 169 | return NULL; /* done */ |
170 | ||
e6ccf245 | 171 | entry = (StoreEntry *) heap_peep(heap->theHeap, heap_walk->current++); |
62e76326 | 172 | |
6a566b9c | 173 | return entry; |
174 | } | |
175 | ||
176 | static void | |
177 | heap_walkDone(RemovalPolicyWalker * walker) | |
178 | { | |
179 | RemovalPolicy *policy = walker->_policy; | |
e6ccf245 | 180 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 181 | assert(strcmp(policy->_type, "heap") == 0); |
182 | assert(heap->nwalkers > 0); | |
183 | heap->nwalkers -= 1; | |
184 | safe_free(walker->_data); | |
aa839030 | 185 | delete walker; |
6a566b9c | 186 | } |
187 | ||
188 | static RemovalPolicyWalker * | |
189 | heap_walkInit(RemovalPolicy * policy) | |
190 | { | |
e6ccf245 | 191 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 192 | RemovalPolicyWalker *walker; |
193 | HeapWalkData *heap_walk; | |
194 | heap->nwalkers += 1; | |
aa839030 | 195 | walker = new RemovalPolicyWalker; |
e6ccf245 | 196 | heap_walk = (HeapWalkData *)xcalloc(1, sizeof(*heap_walk)); |
6a566b9c | 197 | heap_walk->current = 0; |
198 | walker->_policy = policy; | |
199 | walker->_data = heap_walk; | |
200 | walker->Next = heap_walkNext; | |
201 | walker->Done = heap_walkDone; | |
6a566b9c | 202 | return walker; |
203 | } | |
204 | ||
205 | /** RemovalPurgeWalker **/ | |
206 | ||
207 | typedef struct _HeapPurgeData HeapPurgeData; | |
62e76326 | 208 | |
26ac0430 | 209 | struct _HeapPurgeData { |
6a566b9c | 210 | link_list *locked_entries; |
211 | heap_key min_age; | |
212 | }; | |
213 | ||
214 | static StoreEntry * | |
215 | heap_purgeNext(RemovalPurgeWalker * walker) | |
216 | { | |
e6ccf245 | 217 | HeapPurgeData *heap_walker = (HeapPurgeData *)walker->_data; |
6a566b9c | 218 | RemovalPolicy *policy = walker->_policy; |
e6ccf245 | 219 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 220 | StoreEntry *entry; |
221 | heap_key age; | |
62e76326 | 222 | |
223 | try_again: | |
224 | ||
e6ccf245 | 225 | if (!heap_nodes(heap->theHeap) > 0) |
62e76326 | 226 | return NULL; /* done */ |
227 | ||
e6ccf245 | 228 | age = heap_peepminkey(heap->theHeap); |
62e76326 | 229 | |
e6ccf245 | 230 | entry = (StoreEntry *)heap_extractmin(heap->theHeap); |
62e76326 | 231 | |
3900307b | 232 | if (entry->locked()) { |
34266cde | 233 | |
3d0ac046 | 234 | entry->lock(); |
62e76326 | 235 | linklistPush(&heap_walker->locked_entries, entry); |
34266cde | 236 | |
62e76326 | 237 | goto try_again; |
6a566b9c | 238 | } |
62e76326 | 239 | |
6a566b9c | 240 | heap_walker->min_age = age; |
e6ccf245 | 241 | heap->setPolicyNode(entry, NULL); |
6a566b9c | 242 | return entry; |
243 | } | |
244 | ||
245 | static void | |
246 | heap_purgeDone(RemovalPurgeWalker * walker) | |
247 | { | |
e6ccf245 | 248 | HeapPurgeData *heap_walker = (HeapPurgeData *)walker->_data; |
6a566b9c | 249 | RemovalPolicy *policy = walker->_policy; |
e6ccf245 | 250 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 251 | StoreEntry *entry; |
252 | assert(strcmp(policy->_type, "heap") == 0); | |
253 | assert(heap->nwalkers > 0); | |
254 | heap->nwalkers -= 1; | |
62e76326 | 255 | |
6a566b9c | 256 | if (heap_walker->min_age > 0) { |
62e76326 | 257 | heap->theHeap->age = heap_walker->min_age; |
4a7a3d56 | 258 | debugs(81, 3, "heap_purgeDone: Heap age set to " << heap->theHeap->age ); |
6a566b9c | 259 | } |
62e76326 | 260 | |
6a566b9c | 261 | /* |
262 | * Reinsert the locked entries | |
263 | */ | |
e6ccf245 | 264 | while ((entry = (StoreEntry *)linklistShift(&heap_walker->locked_entries))) { |
62e76326 | 265 | heap_node *node = heap_insert(heap->theHeap, entry); |
266 | heap->setPolicyNode(entry, node); | |
97b5e68f | 267 | entry->unlock(); |
6a566b9c | 268 | } |
62e76326 | 269 | |
6a566b9c | 270 | safe_free(walker->_data); |
aa839030 | 271 | delete walker; |
6a566b9c | 272 | } |
273 | ||
274 | static RemovalPurgeWalker * | |
275 | heap_purgeInit(RemovalPolicy * policy, int max_scan) | |
276 | { | |
e6ccf245 | 277 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 278 | RemovalPurgeWalker *walker; |
279 | HeapPurgeData *heap_walk; | |
280 | heap->nwalkers += 1; | |
aa839030 | 281 | walker = new RemovalPurgeWalker; |
e6ccf245 | 282 | heap_walk = (HeapPurgeData *)xcalloc(1, sizeof(*heap_walk)); |
6a566b9c | 283 | heap_walk->min_age = 0.0; |
284 | heap_walk->locked_entries = NULL; | |
285 | walker->_policy = policy; | |
286 | walker->_data = heap_walk; | |
287 | walker->max_scan = max_scan; | |
288 | walker->Next = heap_purgeNext; | |
289 | walker->Done = heap_purgeDone; | |
6a566b9c | 290 | return walker; |
291 | } | |
292 | ||
293 | static void | |
294 | heap_free(RemovalPolicy * policy) | |
295 | { | |
e6ccf245 | 296 | HeapPolicyData *heap = (HeapPolicyData *)policy->_data; |
6a566b9c | 297 | /* Make some verification of the policy state */ |
298 | assert(strcmp(policy->_type, "heap") == 0); | |
299 | assert(heap->nwalkers); | |
300 | assert(heap->count); | |
301 | /* Ok, time to destroy this policy */ | |
e4a67a80 | 302 | safe_free(heap); |
6a566b9c | 303 | memset(policy, 0, sizeof(*policy)); |
aa839030 | 304 | delete policy; |
6a566b9c | 305 | } |
306 | ||
307 | RemovalPolicy * | |
308 | createRemovalPolicy_heap(wordlist * args) | |
309 | { | |
310 | RemovalPolicy *policy; | |
311 | HeapPolicyData *heap_data; | |
c193c972 | 312 | const char *keytype; |
6a566b9c | 313 | /* Allocate the needed structures */ |
aa839030 | 314 | policy = new RemovalPolicy; |
e6ccf245 | 315 | heap_data = (HeapPolicyData *)xcalloc(1, sizeof(*heap_data)); |
6a566b9c | 316 | /* Initialize the policy data */ |
317 | heap_data->policy = policy; | |
62e76326 | 318 | |
6a566b9c | 319 | if (args) { |
62e76326 | 320 | keytype = args->key; |
321 | args = args->next; | |
6a566b9c | 322 | } else { |
bf8fe701 | 323 | debugs(81, 1, "createRemovalPolicy_heap: No key type specified. Using LRU"); |
62e76326 | 324 | keytype = "LRU"; |
6a566b9c | 325 | } |
62e76326 | 326 | |
6a566b9c | 327 | if (!strcmp(keytype, "GDSF")) |
62e76326 | 328 | heap_data->keyfunc = HeapKeyGen_StoreEntry_GDSF; |
6a566b9c | 329 | else if (!strcmp(keytype, "LFUDA")) |
62e76326 | 330 | heap_data->keyfunc = HeapKeyGen_StoreEntry_LFUDA; |
6a566b9c | 331 | else if (!strcmp(keytype, "LRU")) |
62e76326 | 332 | heap_data->keyfunc = HeapKeyGen_StoreEntry_LRU; |
6a566b9c | 333 | else { |
bf8fe701 | 334 | debugs(81, 0, "createRemovalPolicy_heap: Unknown key type \"" << keytype << "\". Using LRU"); |
62e76326 | 335 | heap_data->keyfunc = HeapKeyGen_StoreEntry_LRU; |
6a566b9c | 336 | } |
62e76326 | 337 | |
6a566b9c | 338 | /* No additional arguments expected */ |
339 | assert(!args); | |
62e76326 | 340 | |
e6ccf245 | 341 | heap_data->theHeap = new_heap(1000, heap_data->keyfunc); |
62e76326 | 342 | |
e6ccf245 | 343 | heap_data->theHeap->age = 1.0; |
62e76326 | 344 | |
6a566b9c | 345 | /* Populate the policy structure */ |
346 | policy->_type = "heap"; | |
62e76326 | 347 | |
6a566b9c | 348 | policy->_data = heap_data; |
62e76326 | 349 | |
6a566b9c | 350 | policy->Free = heap_free; |
62e76326 | 351 | |
6a566b9c | 352 | policy->Add = heap_add; |
62e76326 | 353 | |
6a566b9c | 354 | policy->Remove = heap_remove; |
62e76326 | 355 | |
6a566b9c | 356 | policy->Referenced = NULL; |
62e76326 | 357 | |
6a566b9c | 358 | policy->Dereferenced = heap_referenced; |
62e76326 | 359 | |
6a566b9c | 360 | policy->WalkInit = heap_walkInit; |
62e76326 | 361 | |
6a566b9c | 362 | policy->PurgeInit = heap_purgeInit; |
62e76326 | 363 | |
6a566b9c | 364 | /* Increase policy usage count */ |
365 | nr_heap_policies += 0; | |
62e76326 | 366 | |
6a566b9c | 367 | return policy; |
368 | } |