]> git.ipfire.org Git - thirdparty/squid.git/blame - src/repl/lru/store_repl_lru.cc
Document the 'carp' cache_peer option
[thirdparty/squid.git] / src / repl / lru / store_repl_lru.cc
CommitLineData
6a566b9c 1
2/*
528b2c61 3 * $Id: store_repl_lru.cc,v 1.13 2003/01/23 00:38:29 robertc Exp $
6a566b9c 4 *
5 * DEBUG: section ? LRU Removal policy
6 * AUTHOR: Henrik Nordstrom
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
6a566b9c 9 * ----------------------------------------------------------
10 *
2b6662ba 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.
6a566b9c 19 *
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.
24 *
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.
29 *
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.
33 *
34 */
35
36#include "squid.h"
e6ccf245 37#include "Store.h"
528b2c61 38#include "MemObject.h"
6a566b9c 39
40REMOVALPOLICYCREATE createRemovalPolicy_lru;
41
e6ccf245 42struct LruPolicyData {
43 void setPolicyNode (StoreEntry *, void *) const;
6a566b9c 44 RemovalPolicy *policy;
45 dlink_list list;
46 int count;
47 int nwalkers;
9f913cf1 48 enum heap_entry_type {
49 TYPE_UNKNOWN = 0, TYPE_STORE_ENTRY, TYPE_STORE_MEM
50 } type;
6a566b9c 51};
52
53/* Hack to avoid having to remember the RemovalPolicyNode location.
54 * Needed by the purge walker to clear the policy information
55 */
e6ccf245 56static enum LruPolicyData::heap_entry_type
6a566b9c 57repl_guessType(StoreEntry * entry, RemovalPolicyNode * node)
58{
59 if (node == &entry->repl)
e6ccf245 60 return LruPolicyData::TYPE_STORE_ENTRY;
6a566b9c 61 if (entry->mem_obj && node == &entry->mem_obj->repl)
e6ccf245 62 return LruPolicyData::TYPE_STORE_MEM;
6a566b9c 63 fatal("Heap Replacement: Unknown StoreEntry node type");
e6ccf245 64 return LruPolicyData::TYPE_UNKNOWN;
6a566b9c 65}
e6ccf245 66
67void
68LruPolicyData::setPolicyNode (StoreEntry *entry, void *value) const
69{
70 switch (type) {
71 case TYPE_STORE_ENTRY: entry->repl.data = value; break ;
72 case TYPE_STORE_MEM: entry->mem_obj->repl.data = value ; break ;
73 default: break;
6a566b9c 74 }
e6ccf245 75}
6a566b9c 76
77typedef struct _LruNode LruNode;
9f913cf1 78struct _LruNode {
6a566b9c 79 /* Note: the dlink_node MUST be the first member of the LruNode
80 * structure. This member is later pointer typecasted to LruNode *.
81 */
82 dlink_node node;
83};
84
85static MemPool *lru_node_pool = NULL;
86static int nr_lru_policies = 0;
87
88static void
89lru_add(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node)
90{
e6ccf245 91 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 92 LruNode *lru_node;
93 assert(!node->data);
e6ccf245 94 node->data = lru_node = (LruNode *)memPoolAlloc(lru_node_pool);
6a566b9c 95 dlinkAddTail(entry, &lru_node->node, &lru->list);
96 lru->count += 1;
97 if (!lru->type)
98 lru->type = repl_guessType(entry, node);
99}
100
101static void
102lru_remove(RemovalPolicy * policy, StoreEntry * entry, RemovalPolicyNode * node)
103{
e6ccf245 104 LruPolicyData *lru = (LruPolicyData *)policy->_data;
105 LruNode *lru_node = (LruNode *)node->data;
6a566b9c 106 if (!lru_node)
107 return;
75240ca9 108 /*
109 * It seems to be possible for an entry to exist in the hash
110 * but not be in the LRU list, so check for that case rather
111 * than suffer a NULL pointer access.
112 */
113 if (NULL == lru_node->node.data)
114 return;
6a566b9c 115 assert(lru_node->node.data == entry);
116 node->data = NULL;
117 dlinkDelete(&lru_node->node, &lru->list);
118 memPoolFree(lru_node_pool, lru_node);
119 lru->count -= 1;
120}
121
122static void
123lru_referenced(RemovalPolicy * policy, const StoreEntry * entry,
124 RemovalPolicyNode * node)
125{
e6ccf245 126 LruPolicyData *lru = (LruPolicyData *)policy->_data;
127 LruNode *lru_node = (LruNode *)node->data;
6a566b9c 128 if (!lru_node)
129 return;
130 dlinkDelete(&lru_node->node, &lru->list);
131 dlinkAddTail((void *) entry, &lru_node->node, &lru->list);
132}
133
134/** RemovalPolicyWalker **/
135
136typedef struct _LruWalkData LruWalkData;
9f913cf1 137struct _LruWalkData {
6a566b9c 138 LruNode *current;
139};
140
2d72d4fd 141static const StoreEntry *
6a566b9c 142lru_walkNext(RemovalPolicyWalker * walker)
143{
e6ccf245 144 LruWalkData *lru_walk = (LruWalkData *)walker->_data;
6a566b9c 145 LruNode *lru_node = lru_walk->current;
146 if (!lru_node)
147 return NULL;
148 lru_walk->current = (LruNode *) lru_node->node.next;
149 return (StoreEntry *) lru_node->node.data;
150}
151
152static void
153lru_walkDone(RemovalPolicyWalker * walker)
154{
155 RemovalPolicy *policy = walker->_policy;
e6ccf245 156 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 157 assert(strcmp(policy->_type, "lru") == 0);
158 assert(lru->nwalkers > 0);
159 lru->nwalkers -= 1;
160 safe_free(walker->_data);
161 cbdataFree(walker);
162}
163
164static RemovalPolicyWalker *
165lru_walkInit(RemovalPolicy * policy)
166{
e6ccf245 167 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 168 RemovalPolicyWalker *walker;
169 LruWalkData *lru_walk;
170 lru->nwalkers += 1;
72711e31 171 walker = cbdataAlloc(RemovalPolicyWalker);
e6ccf245 172 lru_walk = (LruWalkData *)xcalloc(1, sizeof(*lru_walk));
6a566b9c 173 walker->_policy = policy;
174 walker->_data = lru_walk;
175 walker->Next = lru_walkNext;
176 walker->Done = lru_walkDone;
177 lru_walk->current = (LruNode *) lru->list.head;
6a566b9c 178 return walker;
179}
180
181/** RemovalPurgeWalker **/
182
183typedef struct _LruPurgeData LruPurgeData;
9f913cf1 184struct _LruPurgeData {
6a566b9c 185 LruNode *current;
186 LruNode *start;
187};
188
189static StoreEntry *
190lru_purgeNext(RemovalPurgeWalker * walker)
191{
e6ccf245 192 LruPurgeData *lru_walker = (LruPurgeData *)walker->_data;
6a566b9c 193 RemovalPolicy *policy = walker->_policy;
e6ccf245 194 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 195 LruNode *lru_node;
196 StoreEntry *entry;
197 try_again:
198 lru_node = lru_walker->current;
199 if (!lru_node || walker->scanned >= walker->max_scan)
200 return NULL;
201 walker->scanned += 1;
202 lru_walker->current = (LruNode *) lru_node->node.next;
203 if (lru_walker->current == lru_walker->start) {
204 /* Last node found */
205 lru_walker->current = NULL;
206 }
207 entry = (StoreEntry *) lru_node->node.data;
208 dlinkDelete(&lru_node->node, &lru->list);
209 if (storeEntryLocked(entry)) {
210 /* Shit, it is locked. we can't return this one */
211 walker->locked++;
212 dlinkAddTail(entry, &lru_node->node, &lru->list);
213 goto try_again;
214 }
215 memPoolFree(lru_node_pool, lru_node);
216 lru->count -= 1;
e6ccf245 217 lru->setPolicyNode(entry, NULL);
6a566b9c 218 return entry;
219}
220
221static void
222lru_purgeDone(RemovalPurgeWalker * walker)
223{
224 RemovalPolicy *policy = walker->_policy;
e6ccf245 225 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 226 assert(strcmp(policy->_type, "lru") == 0);
227 assert(lru->nwalkers > 0);
228 lru->nwalkers -= 1;
229 safe_free(walker->_data);
230 cbdataFree(walker);
231}
232
233static RemovalPurgeWalker *
234lru_purgeInit(RemovalPolicy * policy, int max_scan)
235{
e6ccf245 236 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 237 RemovalPurgeWalker *walker;
238 LruPurgeData *lru_walk;
239 lru->nwalkers += 1;
72711e31 240 walker = cbdataAlloc(RemovalPurgeWalker);
e6ccf245 241 lru_walk = (LruPurgeData *)xcalloc(1, sizeof(*lru_walk));
6a566b9c 242 walker->_policy = policy;
243 walker->_data = lru_walk;
244 walker->max_scan = max_scan;
245 walker->Next = lru_purgeNext;
246 walker->Done = lru_purgeDone;
247 lru_walk->start = lru_walk->current = (LruNode *) lru->list.head;
6a566b9c 248 return walker;
249}
250
8a3e7d9e 251static void
252lru_stats(RemovalPolicy * policy, StoreEntry * sentry)
253{
e6ccf245 254 LruPolicyData *lru = (LruPolicyData *)policy->_data;
b671cc68 255 LruNode *lru_node = (LruNode *) lru->list.head;
8a3e7d9e 256
b671cc68 257 again:
8a3e7d9e 258 if (lru_node) {
b671cc68 259 StoreEntry *entry = (StoreEntry *) lru_node->node.data;
8a3e7d9e 260 if (storeEntryLocked(entry)) {
b671cc68 261 lru_node = (LruNode *) lru_node->node.next;
8a3e7d9e 262 goto again;
263 }
264 storeAppendPrintf(sentry, "LRU reference age: %.2f days\n", (double) (squid_curtime - entry->lastref) / (double) (24 * 60 * 60));
265 }
266}
267
6a566b9c 268static void
269lru_free(RemovalPolicy * policy)
270{
e6ccf245 271 LruPolicyData *lru = (LruPolicyData *)policy->_data;
6a566b9c 272 /* Make some verification of the policy state */
273 assert(strcmp(policy->_type, "lru") == 0);
274 assert(lru->nwalkers);
275 assert(lru->count);
276 /* Ok, time to destroy this policy */
277 safe_free(policy->_data);
278 memset(policy, 0, sizeof(*policy));
279 cbdataFree(policy);
280}
281
282RemovalPolicy *
9f913cf1 283createRemovalPolicy_lru(wordlist * args)
6a566b9c 284{
285 RemovalPolicy *policy;
286 LruPolicyData *lru_data;
287 /* no arguments expected or understood */
288 assert(!args);
289 /* Initialize */
d96ceb8e 290 if (!lru_node_pool) {
6a566b9c 291 lru_node_pool = memPoolCreate("LRU policy node", sizeof(LruNode));
d96ceb8e 292 memPoolSetChunkSize(lru_node_pool, 512 * 1024);
293 }
6a566b9c 294 /* Allocate the needed structures */
e6ccf245 295 lru_data = (LruPolicyData *)xcalloc(1, sizeof(*lru_data));
72711e31 296 policy = cbdataAlloc(RemovalPolicy);
6a566b9c 297 /* Initialize the URL data */
298 lru_data->policy = policy;
299 /* Populate the policy structure */
300 policy->_type = "lru";
301 policy->_data = lru_data;
302 policy->Free = lru_free;
303 policy->Add = lru_add;
304 policy->Remove = lru_remove;
305 policy->Referenced = lru_referenced;
306 policy->Dereferenced = lru_referenced;
307 policy->WalkInit = lru_walkInit;
308 policy->PurgeInit = lru_purgeInit;
8a3e7d9e 309 policy->Stats = lru_stats;
6a566b9c 310 /* Increase policy usage count */
311 nr_lru_policies += 0;
312 return policy;
313}