]> git.ipfire.org Git - thirdparty/squid.git/blob - src/pconn.cc
C++ conversion
[thirdparty/squid.git] / src / pconn.cc
1
2 /*
3 * $Id: pconn.cc,v 1.33 2002/10/13 20:35:02 robertc Exp $
4 *
5 * DEBUG: section 48 Persistent Connections
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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"
37 #include "Store.h"
38
39 struct _pconn {
40 hash_link hash; /* must be first */
41 int *fds;
42 int nfds_alloc;
43 int nfds;
44 };
45 typedef struct _pconn pconn;
46
47 #define PCONN_FDS_SZ 8 /* pconn set size, increase for better memcache hit rate */
48 #define PCONN_HIST_SZ (1<<16)
49 int client_pconn_hist[PCONN_HIST_SZ];
50 int server_pconn_hist[PCONN_HIST_SZ];
51
52 static PF pconnRead;
53 static PF pconnTimeout;
54 static const char *pconnKey(const char *host, u_short port);
55 static hash_table *table = NULL;
56 static struct _pconn *pconnNew(const char *key);
57 static void pconnDelete(struct _pconn *p);
58 static void pconnRemoveFD(struct _pconn *p, int fd);
59 static OBJH pconnHistDump;
60 static MemPool *pconn_fds_pool = NULL;
61 CBDATA_TYPE(pconn);
62
63
64
65 static const char *
66 pconnKey(const char *host, u_short port)
67 {
68 LOCAL_ARRAY(char, buf, SQUIDHOSTNAMELEN + 10);
69 snprintf(buf, SQUIDHOSTNAMELEN + 10, "%s.%d", host, (int) port);
70 return buf;
71 }
72
73 static struct _pconn *
74 pconnNew(const char *key)
75 {
76 pconn *p;
77 CBDATA_INIT_TYPE(pconn);
78 p = cbdataAlloc(pconn);
79 p->hash.key = xstrdup(key);
80 p->nfds_alloc = PCONN_FDS_SZ;
81 p->fds = (int *)memPoolAlloc(pconn_fds_pool);
82 debug(48, 3) ("pconnNew: adding %s\n", hashKeyStr(&p->hash));
83 hash_join(table, &p->hash);
84 return p;
85 }
86
87 static void
88 pconnDelete(struct _pconn *p)
89 {
90 debug(48, 3) ("pconnDelete: deleting %s\n", hashKeyStr(&p->hash));
91 hash_remove_link(table, (hash_link *) p);
92 if (p->nfds_alloc == PCONN_FDS_SZ)
93 memPoolFree(pconn_fds_pool, p->fds);
94 else
95 xfree(p->fds);
96 xfree(p->hash.key);
97 cbdataFree(p);
98 }
99
100 static void
101 pconnRemoveFD(struct _pconn *p, int fd)
102 {
103 int i;
104 for (i = 0; i < p->nfds; i++) {
105 if (p->fds[i] == fd)
106 break;
107 }
108 assert(i < p->nfds);
109 debug(48, 3) ("pconnRemoveFD: found FD %d at index %d\n", fd, i);
110 for (; i < p->nfds - 1; i++)
111 p->fds[i] = p->fds[i + 1];
112 if (--p->nfds == 0)
113 pconnDelete(p);
114 }
115
116 static void
117 pconnTimeout(int fd, void *data)
118 {
119 struct _pconn *p = (_pconn *)data;
120 assert(table != NULL);
121 debug(48, 3) ("pconnTimeout: FD %d %s\n", fd, hashKeyStr(&p->hash));
122 pconnRemoveFD(p, fd);
123 comm_close(fd);
124 }
125
126 static void
127 pconnRead(int fd, void *data)
128 {
129 LOCAL_ARRAY(char, buf, 256);
130 struct _pconn *p = (_pconn *)data;
131 int n;
132 assert(table != NULL);
133 statCounter.syscalls.sock.reads++;
134 n = FD_READ_METHOD(fd, buf, 256);
135 debug(48, 3) ("pconnRead: %d bytes from FD %d, %s\n", n, fd,
136 hashKeyStr(&p->hash));
137 pconnRemoveFD(p, fd);
138 comm_close(fd);
139 }
140
141 static void
142 pconnHistDump(StoreEntry * e)
143 {
144 int i;
145 storeAppendPrintf(e,
146 "Client-side persistent connection counts:\n"
147 "\n"
148 "\treq/\n"
149 "\tconn count\n"
150 "\t---- ---------\n");
151 for (i = 0; i < PCONN_HIST_SZ; i++) {
152 if (client_pconn_hist[i] == 0)
153 continue;
154 storeAppendPrintf(e, "\t%4d %9d\n", i, client_pconn_hist[i]);
155 }
156 storeAppendPrintf(e,
157 "\n"
158 "Server-side persistent connection counts:\n"
159 "\n"
160 "\treq/\n"
161 "\tconn count\n"
162 "\t---- ---------\n");
163 for (i = 0; i < PCONN_HIST_SZ; i++) {
164 if (server_pconn_hist[i] == 0)
165 continue;
166 storeAppendPrintf(e, "\t%4d %9d\n", i, server_pconn_hist[i]);
167 }
168 }
169
170 /* ========== PUBLIC FUNCTIONS ============================================ */
171
172
173 void
174 pconnInit(void)
175 {
176 int i;
177 assert(table == NULL);
178 table = hash_create((HASHCMP *) strcmp, 229, hash_string);
179 for (i = 0; i < PCONN_HIST_SZ; i++) {
180 client_pconn_hist[i] = 0;
181 server_pconn_hist[i] = 0;
182 }
183 pconn_fds_pool = memPoolCreate("pconn_fds", PCONN_FDS_SZ * sizeof(int));
184
185 cachemgrRegister("pconn",
186 "Persistent Connection Utilization Histograms",
187 pconnHistDump, 0, 1);
188 debug(48, 3) ("persistent connection module initialized\n");
189 }
190
191 void
192 pconnPush(int fd, const char *host, u_short port)
193 {
194 struct _pconn *p;
195 int *old;
196 LOCAL_ARRAY(char, key, SQUIDHOSTNAMELEN + 10);
197 LOCAL_ARRAY(char, desc, FD_DESC_SZ);
198 if (fdNFree() < (RESERVED_FD << 1)) {
199 debug(48, 3) ("pconnPush: Not many unused FDs\n");
200 comm_close(fd);
201 return;
202 } else if (shutting_down) {
203 comm_close(fd);
204 return;
205 }
206 assert(table != NULL);
207 strcpy(key, pconnKey(host, port));
208 p = (struct _pconn *) hash_lookup(table, key);
209 if (p == NULL)
210 p = pconnNew(key);
211 if (p->nfds == p->nfds_alloc) {
212 debug(48, 3) ("pconnPush: growing FD array\n");
213 p->nfds_alloc <<= 1;
214 old = p->fds;
215 p->fds = (int *)xmalloc(p->nfds_alloc * sizeof(int));
216 xmemcpy(p->fds, old, p->nfds * sizeof(int));
217 if (p->nfds == PCONN_FDS_SZ)
218 memPoolFree(pconn_fds_pool, old);
219 else
220 xfree(old);
221 }
222 p->fds[p->nfds++] = fd;
223 commSetSelect(fd, COMM_SELECT_READ, pconnRead, p, 0);
224 commSetTimeout(fd, Config.Timeout.pconn, pconnTimeout, p);
225 snprintf(desc, FD_DESC_SZ, "%s idle connection", host);
226 fd_note(fd, desc);
227 debug(48, 3) ("pconnPush: pushed FD %d for %s\n", fd, key);
228 }
229
230 int
231 pconnPop(const char *host, u_short port)
232 {
233 struct _pconn *p;
234 hash_link *hptr;
235 int fd = -1;
236 LOCAL_ARRAY(char, key, SQUIDHOSTNAMELEN + 10);
237 assert(table != NULL);
238 strcpy(key, pconnKey(host, port));
239 hptr = (hash_link *)hash_lookup(table, key);
240 if (hptr != NULL) {
241 p = (struct _pconn *) hptr;
242 assert(p->nfds > 0);
243 fd = p->fds[0];
244 pconnRemoveFD(p, fd);
245 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
246 commSetTimeout(fd, -1, NULL, NULL);
247 }
248 return fd;
249 }
250
251 void
252 pconnHistCount(int what, int i)
253 {
254 if (i >= PCONN_HIST_SZ)
255 i = PCONN_HIST_SZ - 1;
256 /* what == 0 for client, 1 for server */
257 if (what == 0)
258 client_pconn_hist[i]++;
259 else if (what == 1)
260 server_pconn_hist[i]++;
261 else
262 assert(0);
263 }