3 * DEBUG: section 39 Peer source hash based selection
4 * AUTHOR: Henrik Nordstrom
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
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.
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.
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.
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.
36 #include "CachePeer.h"
37 #include "HttpRequest.h"
38 #include "mgr/Registration.h"
39 #include "neighbors.h"
40 #include "SquidConfig.h"
47 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
49 static int n_sourcehash_peers
= 0;
50 static CachePeer
**sourcehash_peers
= NULL
;
51 static OBJH peerSourceHashCachemgr
;
52 static void peerSourceHashRegisterWithCacheManager(void);
55 peerSortWeight(const void *a
, const void *b
)
57 const CachePeer
*const *p1
= (const CachePeer
*const *)a
;
58 const CachePeer
*const *p2
= (const CachePeer
*const *)b
;
59 return (*p1
)->weight
- (*p2
)->weight
;
63 peerSourceHashInit(void)
68 double P_last
, X_last
, Xn
;
74 for (k
= 0; k
< n_sourcehash_peers
; ++k
) {
75 cbdataReferenceDone(sourcehash_peers
[k
]);
78 safe_free(sourcehash_peers
);
79 n_sourcehash_peers
= 0;
80 /* find out which peers we have */
82 for (p
= Config
.peers
; p
; p
= p
->next
) {
83 if (!p
->options
.sourcehash
)
86 assert(p
->type
== PEER_PARENT
);
96 peerSourceHashRegisterWithCacheManager();
98 if (n_sourcehash_peers
== 0)
101 sourcehash_peers
= (CachePeer
**)xcalloc(n_sourcehash_peers
, sizeof(*sourcehash_peers
));
103 /* Build a list of the found peers and calculate hashes and load factors */
104 for (P
= sourcehash_peers
, p
= Config
.peers
; p
; p
= p
->next
) {
105 if (!p
->options
.sourcehash
)
111 /* calculate this peers hash */
112 p
->sourcehash
.hash
= 0;
114 for (t
= p
->name
; *t
!= 0; ++t
)
115 p
->sourcehash
.hash
+= ROTATE_LEFT(p
->sourcehash
.hash
, 19) + (unsigned int) *t
;
117 p
->sourcehash
.hash
+= p
->sourcehash
.hash
* 0x62531965;
119 p
->sourcehash
.hash
= ROTATE_LEFT(p
->sourcehash
.hash
, 21);
121 /* and load factor */
122 p
->sourcehash
.load_factor
= ((double) p
->weight
) / (double) W
;
124 if (floor(p
->sourcehash
.load_factor
* 1000.0) == 0.0)
125 p
->sourcehash
.load_factor
= 0.0;
127 /* add it to our list of peers */
128 *P
++ = cbdataReference(p
);
131 /* Sort our list on weight */
132 qsort(sourcehash_peers
, n_sourcehash_peers
, sizeof(*sourcehash_peers
), peerSortWeight
);
134 /* Calculate the load factor multipliers X_k
136 * X_1 = pow ((K*p_1), (1/K))
137 * X_k = ([K-k+1] * [P_k - P_{k-1}])/(X_1 * X_2 * ... * X_{k-1})
138 * X_k += pow ((X_{k-1}, {K-k+1})
139 * X_k = pow (X_k, {1/(K-k+1)})
140 * simplified to have X_1 part of the loop
142 K
= n_sourcehash_peers
;
144 P_last
= 0.0; /* Empty P_0 */
146 Xn
= 1.0; /* Empty starting point of X_1 * X_2 * ... * X_{x-1} */
148 X_last
= 0.0; /* Empty X_0, nullifies the first pow statement */
150 for (k
= 1; k
<= K
; ++k
) {
151 double Kk1
= (double) (K
- k
+ 1);
152 p
= sourcehash_peers
[k
- 1];
153 p
->sourcehash
.load_multiplier
= (Kk1
* (p
->sourcehash
.load_factor
- P_last
)) / Xn
;
154 p
->sourcehash
.load_multiplier
+= pow(X_last
, Kk1
);
155 p
->sourcehash
.load_multiplier
= pow(p
->sourcehash
.load_multiplier
, 1.0 / Kk1
);
156 Xn
*= p
->sourcehash
.load_multiplier
;
157 X_last
= p
->sourcehash
.load_multiplier
;
158 P_last
= p
->sourcehash
.load_factor
;
163 peerSourceHashRegisterWithCacheManager(void)
165 Mgr::RegisterAction("sourcehash", "peer sourcehash information",
166 peerSourceHashCachemgr
, 0, 1);
170 peerSourceHashSelectParent(HttpRequest
* request
)
176 unsigned int user_hash
= 0;
177 unsigned int combined_hash
;
179 double high_score
= 0;
180 const char *key
= NULL
;
181 char ntoabuf
[MAX_IPSTRLEN
];
183 if (n_sourcehash_peers
== 0)
186 key
= request
->client_addr
.NtoA(ntoabuf
, sizeof(ntoabuf
));
188 /* calculate hash key */
189 debugs(39, 2, "peerSourceHashSelectParent: Calculating hash for " << key
);
191 for (c
= key
; *c
!= 0; ++c
)
192 user_hash
+= ROTATE_LEFT(user_hash
, 19) + *c
;
194 /* select CachePeer */
195 for (k
= 0; k
< n_sourcehash_peers
; ++k
) {
196 tp
= sourcehash_peers
[k
];
197 combined_hash
= (user_hash
^ tp
->sourcehash
.hash
);
198 combined_hash
+= combined_hash
* 0x62531965;
199 combined_hash
= ROTATE_LEFT(combined_hash
, 21);
200 score
= combined_hash
* tp
->sourcehash
.load_multiplier
;
201 debugs(39, 3, "peerSourceHashSelectParent: " << tp
->name
<< " combined_hash " << combined_hash
<<
202 " score " << std::setprecision(0) << score
);
204 if ((score
> high_score
) && peerHTTPOkay(tp
, request
)) {
211 debugs(39, 2, "peerSourceHashSelectParent: selected " << p
->name
);
217 peerSourceHashCachemgr(StoreEntry
* sentry
)
221 storeAppendPrintf(sentry
, "%24s %10s %10s %10s %10s\n",
228 for (p
= Config
.peers
; p
; p
= p
->next
)
229 sumfetches
+= p
->stats
.fetches
;
231 for (p
= Config
.peers
; p
; p
= p
->next
) {
232 storeAppendPrintf(sentry
, "%24s %10x %10f %10f %10f\n",
233 p
->name
, p
->sourcehash
.hash
,
234 p
->sourcehash
.load_multiplier
,
235 p
->sourcehash
.load_factor
,
236 sumfetches
? (double) p
->stats
.fetches
/ sumfetches
: -1.0);