]> git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.cc
Transition pconn handling from FD to Comm::Connection
[thirdparty/squid.git] / src / pconn.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 48 Persistent Connections
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid.h"
36 #include "CacheManager.h"
37 #include "comm.h"
38 #include "comm/Connection.h"
39 #include "fde.h"
40 #include "pconn.h"
41 #include "Store.h"
42
43 #define PCONN_FDS_SZ 8 /* pconn set size, increase for better memcache hit rate */
44
45 //TODO: re-attach to MemPools. WAS: static MemAllocator *pconn_fds_pool = NULL;
46 PconnModule * PconnModule::instance = NULL;
47 CBDATA_CLASS_INIT(IdleConnList);
48
49 /* ========== IdleConnList ============================================ */
50
51 IdleConnList::IdleConnList(const char *key, PconnPool *thePool) :
52 nfds_alloc(PCONN_FDS_SZ),
53 nfds(0),
54 parent(thePool)
55 {
56 hash.key = xstrdup(key);
57 theList = new Comm::ConnectionPointer[nfds_alloc];
58 // TODO: re-attach to MemPools. WAS: fds = (int *)pconn_fds_pool->alloc();
59 }
60
61 IdleConnList::~IdleConnList()
62 {
63 parent->unlinkList(this);
64
65 /* TODO: re-attach to MemPools.
66 if (nfds_alloc == PCONN_FDS_SZ)
67 pconn_fds_pool->freeOne(theList);
68 else
69 */
70 delete[] theList;
71
72 xfree(hash.key);
73 }
74
75 int
76 IdleConnList::findIndex(const Comm::ConnectionPointer &conn)
77 {
78 for (int index = nfds - 1; index >= 0; --index) {
79 if (theList[index]->fd == conn->fd)
80 return index;
81 }
82
83 return -1;
84 }
85
86 bool
87 IdleConnList::remove(const Comm::ConnectionPointer &conn)
88 {
89 int index = findIndex(conn);
90 if (index < 0) {
91 debugs(48, 2, HERE << conn << " NOT FOUND!");
92 return false;
93 }
94 debugs(48, 3, HERE << "found " << conn << " at index " << index);
95
96 for (; index < nfds - 1; index++)
97 theList[index] = theList[index + 1];
98
99 if (--nfds == 0) {
100 debugs(48, 3, "IdleConnList::removeFD: deleting " << hashKeyStr(&hash));
101 delete this;
102 }
103 return true;
104 }
105
106 void
107 IdleConnList::clearHandlers(const Comm::ConnectionPointer &conn)
108 {
109 comm_read_cancel(conn->fd, IdleConnList::read, this);
110 commSetTimeout(conn->fd, -1, NULL, NULL);
111 }
112
113 void
114 IdleConnList::push(const Comm::ConnectionPointer &conn)
115 {
116 if (nfds == nfds_alloc) {
117 debugs(48, 3, "IdleConnList::push: growing FD array");
118 nfds_alloc <<= 1;
119 const Comm::ConnectionPointer *oldList = theList;
120 theList = new Comm::ConnectionPointer[nfds_alloc];
121 for (int index = 0; index < nfds; index++)
122 theList[index] = oldList[index];
123
124 /* TODO: re-attach to MemPools.
125 if (nfds == PCONN_FDS_SZ)
126 pconn_fds_pool->freeOne(oldList);
127 else
128 */
129 delete[] oldList;
130 }
131
132 theList[nfds++] = conn;
133 comm_read(conn, fakeReadBuf, sizeof(fakeReadBuf), IdleConnList::read, this);
134 commSetTimeout(conn->fd, Config.Timeout.pconn, IdleConnList::timeout, this);
135 }
136
137 /*
138 * XXX this routine isn't terribly efficient - if there's a pending
139 * read event (which signifies the fd will close in the next IO loop!)
140 * we ignore the FD and move onto the next one. This means, as an example,
141 * if we have a lot of FDs open to a very popular server and we get a bunch
142 * of requests JUST as they timeout (say, it shuts down) we'll be wasting
143 * quite a bit of CPU. Just keep it in mind.
144 */
145 Comm::ConnectionPointer
146 IdleConnList::findUseable()
147 {
148 assert(nfds);
149
150 for (int i=nfds-1; i>=0; i--) {
151 if (!comm_has_pending_read_callback(theList[i]->fd)) {
152 return theList[i];
153 }
154 }
155
156 return Comm::ConnectionPointer();
157 }
158
159 void
160 IdleConnList::read(const Comm::ConnectionPointer &conn, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
161 {
162 debugs(48, 3, HERE << len << " bytes from " << conn);
163
164 if (flag == COMM_ERR_CLOSING) {
165 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us */
166 return;
167 }
168
169 IdleConnList *list = (IdleConnList *) data;
170 /* might delete list */
171 if (list && list->remove(conn)) {
172 Comm::ConnectionPointer nonConst = conn;
173 nonConst->close();
174 }
175 }
176
177 void
178 IdleConnList::timeout(int fd, void *data)
179 {
180 debugs(48, 3, "IdleConnList::timeout: FD " << fd);
181 IdleConnList *list = (IdleConnList *) data;
182 Comm::ConnectionPointer temp = new Comm::Connection; // XXX: transition. make timeouts pass conn in
183 temp->fd = fd;
184 if (list->remove(temp)) {
185 temp->close();
186 } else
187 temp->fd = -1; // XXX: transition. prevent temp erasure double-closing FD until timeout CB passess conn in.
188 }
189
190 /* ========== PconnPool PRIVATE FUNCTIONS ============================================ */
191
192 const char *
193 PconnPool::key(const Comm::ConnectionPointer &destLink, const char *domain)
194 {
195 LOCAL_ARRAY(char, buf, SQUIDHOSTNAMELEN * 3 + 10);
196
197 destLink->remote.ToURL(buf, SQUIDHOSTNAMELEN * 3 + 10);
198 if (domain) {
199 int used = strlen(buf);
200 snprintf(buf+used, SQUIDHOSTNAMELEN * 3 + 10-used, "/%s", domain);
201 }
202
203 debugs(48,6,"PconnPool::key(" << destLink << ", " << (domain?domain:"[no domain]") << ") is {" << buf << "}" );
204 return buf;
205 }
206
207 void
208 PconnPool::dumpHist(StoreEntry * e) const
209 {
210 storeAppendPrintf(e,
211 "%s persistent connection counts:\n"
212 "\n"
213 "\treq/\n"
214 "\tconn count\n"
215 "\t---- ---------\n",
216 descr);
217
218 for (int i = 0; i < PCONN_HIST_SZ; i++) {
219 if (hist[i] == 0)
220 continue;
221
222 storeAppendPrintf(e, "\t%4d %9d\n", i, hist[i]);
223 }
224 }
225
226 void
227 PconnPool::dumpHash(StoreEntry *e) const
228 {
229 hash_table *hid = table;
230 hash_first(hid);
231
232 int i = 0;
233 for (hash_link *walker = hid->next; walker; walker = hash_next(hid)) {
234 storeAppendPrintf(e, "\t item %5d: %s\n", i++, (char *)(walker->key));
235 }
236 }
237
238 /* ========== PconnPool PUBLIC FUNCTIONS ============================================ */
239
240 PconnPool::PconnPool(const char *aDescr) : table(NULL), descr(aDescr)
241 {
242 table = hash_create((HASHCMP *) strcmp, 229, hash_string);
243
244 for (int i = 0; i < PCONN_HIST_SZ; i++)
245 hist[i] = 0;
246
247 PconnModule::GetInstance()->add(this);
248 }
249
250 PconnPool::~PconnPool()
251 {
252 descr = NULL;
253 hashFreeMemory(table);
254 }
255
256 void
257 PconnPool::push(const Comm::ConnectionPointer &conn, const char *domain)
258 {
259 if (fdUsageHigh()) {
260 debugs(48, 3, "PconnPool::push: Not many unused FDs");
261 Comm::ConnectionPointer nonConst = conn;
262 nonConst->close();
263 return;
264 } else if (shutting_down) {
265 Comm::ConnectionPointer nonConst = conn;
266 nonConst->close();
267 debugs(48, 3, "PconnPool::push: Squid is shutting down. Refusing to do anything");
268 return;
269 }
270
271 const char *aKey = key(conn, domain);
272 IdleConnList *list = (IdleConnList *) hash_lookup(table, aKey);
273
274 if (list == NULL) {
275 list = new IdleConnList(aKey, this);
276 debugs(48, 3, "PconnPool::push: new IdleConnList for {" << hashKeyStr(&list->hash) << "}" );
277 hash_join(table, &list->hash);
278 } else {
279 debugs(48, 3, "PconnPool::push: found IdleConnList for {" << hashKeyStr(&list->hash) << "}" );
280 }
281
282 list->push(conn);
283 assert(!comm_has_incomplete_write(conn->fd));
284
285 LOCAL_ARRAY(char, desc, FD_DESC_SZ);
286 snprintf(desc, FD_DESC_SZ, "Idle: %s", aKey);
287 fd_note(conn->fd, desc);
288 debugs(48, 3, HERE << "pushed " << conn << " for " << aKey);
289 }
290
291 bool
292 PconnPool::pop(Comm::ConnectionPointer &destLink, const char *domain, bool isRetriable)
293 {
294 const char * aKey = key(destLink, domain);
295
296 IdleConnList *list = (IdleConnList *)hash_lookup(table, aKey);
297 if (list == NULL) {
298 debugs(48, 3, "PconnPool::pop: lookup for key {" << aKey << "} failed.");
299 return false;
300 } else {
301 debugs(48, 3, "PconnPool::pop: found " << hashKeyStr(&list->hash) << (isRetriable?"(to use)":"(to kill)") );
302 }
303
304 Comm::ConnectionPointer temp = list->findUseable(); // search from the end. skip pending reads.
305
306 if (Comm::IsConnOpen(temp)) {
307 list->clearHandlers(temp);
308
309 /* might delete list */
310 if (list->remove(temp) && !isRetriable)
311 temp->close();
312 else
313 destLink = temp;
314 }
315
316 return true;
317 }
318
319 void
320 PconnPool::unlinkList(IdleConnList *list) const
321 {
322 hash_remove_link(table, &list->hash);
323 }
324
325 void
326 PconnPool::count(int uses)
327 {
328 if (uses >= PCONN_HIST_SZ)
329 uses = PCONN_HIST_SZ - 1;
330
331 hist[uses]++;
332 }
333
334 /* ========== PconnModule ============================================ */
335
336 /*
337 * This simple class exists only for the cache manager
338 */
339
340 PconnModule::PconnModule() : pools(NULL), poolCount(0)
341 {
342 pools = (PconnPool **) xcalloc(MAX_NUM_PCONN_POOLS, sizeof(*pools));
343 //TODO: re-link to MemPools. WAS: pconn_fds_pool = memPoolCreate("pconn_fds", PCONN_FDS_SZ * sizeof(int));
344 debugs(48, 0, "persistent connection module initialized");
345 registerWithCacheManager();
346 }
347
348 PconnModule *
349 PconnModule::GetInstance()
350 {
351 if (instance == NULL)
352 instance = new PconnModule;
353
354 return instance;
355 }
356
357 void
358 PconnModule::registerWithCacheManager(void)
359 {
360 CacheManager::GetInstance()->
361 registerAction("pconn",
362 "Persistent Connection Utilization Histograms",
363 DumpWrapper, 0, 1);
364 }
365
366 void
367
368 PconnModule::add(PconnPool *aPool)
369 {
370 assert(poolCount < MAX_NUM_PCONN_POOLS);
371 *(pools+poolCount) = aPool;
372 poolCount++;
373 }
374
375 void
376 PconnModule::dump(StoreEntry *e)
377 {
378 for (int i = 0; i < poolCount; i++) {
379 storeAppendPrintf(e, "\n Pool %d Stats\n", i);
380 (*(pools+i))->dumpHist(e);
381 storeAppendPrintf(e, "\n Pool %d Hash Table\n",i);
382 (*(pools+i))->dumpHash(e);
383 }
384 }
385
386 void
387 PconnModule::DumpWrapper(StoreEntry *e)
388 {
389 PconnModule::GetInstance()->dump(e);
390 }