]> git.ipfire.org Git - thirdparty/squid.git/blame - src/MemObject.cc
Author: Alex Rousskov <rousskov@measurement-factory.com>
[thirdparty/squid.git] / src / MemObject.cc
CommitLineData
528b2c61 1
2/*
262a0e14 3 * $Id$
528b2c61 4 *
5 * DEBUG: section 19 Store Memory Primitives
6 * AUTHOR: Robert Collins
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.
26ac0430 24 *
528b2c61 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.
26ac0430 29 *
528b2c61 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 "MemObject.h"
38#include "HttpRequest.h"
39#include "HttpReply.h"
40#include "Store.h"
41#include "StoreClient.h"
42#include "Generic.h"
9a0a18de 43#if USE_DELAY_POOLS
b67e2c8c 44#include "DelayPools.h"
45#endif
0eb49b6d 46#include "MemBuf.h"
528b2c61 47
48/* TODO: make this global or private */
49#if URL_CHECKSUM_DEBUG
50static unsigned int url_checksum(const char *url);
51unsigned int
52url_checksum(const char *url)
53{
54 unsigned int ck;
c3031d67 55 SquidMD5_CTX M;
528b2c61 56 static unsigned char digest[16];
c3031d67 57 SquidMD5Init(&M);
58 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
59 SquidMD5Final(digest, &M);
41d00cd3 60 memcpy(&ck, digest, sizeof(ck));
528b2c61 61 return ck;
62}
62e76326 63
528b2c61 64#endif
65
aa839030 66RemovalPolicy * mem_policy = NULL;
67
528b2c61 68size_t
69MemObject::inUseCount()
70{
9f9e06f3 71 return Pool().inUseCount();
528b2c61 72}
73
06a5ae20 74MemObject::MemObject(char const *aUrl, char const *aLog_url)
528b2c61 75{
e85137f1 76 debugs(20, 3, HERE << "new MemObject " << this);
4a56ee8d 77 HttpReply *rep = new HttpReply;
78
6dd9f4bd 79 _reply = HTTPMSGLOCK(rep);
528b2c61 80 url = xstrdup(aUrl);
4a56ee8d 81
528b2c61 82#if URL_CHECKSUM_DEBUG
62e76326 83
528b2c61 84 chksum = url_checksum(url);
4a56ee8d 85
528b2c61 86#endif
62e76326 87
528b2c61 88 log_url = xstrdup(aLog_url);
4a56ee8d 89
528b2c61 90 object_sz = -1;
4a56ee8d 91
528b2c61 92 /* XXX account log_url */
528b2c61 93}
94
95MemObject::~MemObject()
96{
e85137f1 97 debugs(20, 3, HERE << "del MemObject " << this);
528b2c61 98 const Ctx ctx = ctx_enter(url);
528b2c61 99#if URL_CHECKSUM_DEBUG
62e76326 100
528b2c61 101 assert(chksum == url_checksum(url));
102#endif
62e76326 103
528b2c61 104 if (!shutting_down)
105 assert(swapout.sio == NULL);
62e76326 106
528b2c61 107 data_hdr.freeContent();
62e76326 108
9cdee68d 109#if 0
528b2c61 110 /*
111 * There is no way to abort FD-less clients, so they might
9cdee68d 112 * still have mem->clients set.
528b2c61 113 */
9cdee68d 114 assert(clients.head == NULL);
115
116#endif
62e76326 117
6dd9f4bd 118 HTTPMSGUNLOCK(_reply);
62e76326 119
6dd9f4bd 120 HTTPMSGUNLOCK(request);
62e76326 121
528b2c61 122 ctx_exit(ctx); /* must exit before we free mem->url */
62e76326 123
528b2c61 124 safe_free(url);
62e76326 125
528b2c61 126 safe_free(log_url); /* XXX account log_url */
62e76326 127
528b2c61 128 safe_free(vary_headers);
129}
130
131void
132MemObject::unlinkRequest()
133{
6dd9f4bd 134 HTTPMSGUNLOCK(request);
528b2c61 135}
136
137void
138MemObject::write ( StoreIOBuffer writeBuffer, STMCB *callback, void *callbackData)
139{
1d5161bd 140 PROF_start(MemObject_write);
4a7a3d56 141 debugs(19, 6, "memWrite: offset " << writeBuffer.offset << " len " << writeBuffer.length);
528b2c61 142
143 /* the offset is into the content, not the headers */
144 writeBuffer.offset += (_reply ? _reply->hdr_sz : 0);
145
146 /* We don't separate out mime headers yet, so ensure that the first
26ac0430 147 * write is at offset 0 - where they start
528b2c61 148 */
149 assert (data_hdr.endOffset() || writeBuffer.offset == 0);
150
151 assert (data_hdr.write (writeBuffer));
152 callback (callbackData, writeBuffer);
1d5161bd 153 PROF_stop(MemObject_write);
528b2c61 154}
155
156void
157MemObject::dump() const
158{
42a503bd 159 data_hdr.dump();
528b2c61 160#if 0
161 /* do we want this one? */
bf8fe701 162 debugs(20, 1, "MemObject->data.origin_offset: " << (data_hdr.head ? data_hdr.head->nodeBuffer.offset : 0));
528b2c61 163#endif
62e76326 164
4a7a3d56 165 debugs(20, 1, "MemObject->start_ping: " << start_ping.tv_sec << "."<< std::setfill('0') << std::setw(6) << start_ping.tv_usec);
166 debugs(20, 1, "MemObject->inmem_hi: " << data_hdr.endOffset());
167 debugs(20, 1, "MemObject->inmem_lo: " << inmem_lo);
bf8fe701 168 debugs(20, 1, "MemObject->nclients: " << nclients);
169 debugs(20, 1, "MemObject->reply: " << _reply);
170 debugs(20, 1, "MemObject->request: " << request);
30abd221 171 debugs(20, 1, "MemObject->log_url: " << log_url << " " << checkNullString(log_url));
528b2c61 172}
173
174HttpReply const *
175MemObject::getReply() const
176{
177 return _reply;
178}
179
4a56ee8d 180void
181MemObject::replaceHttpReply(HttpReply *newrep)
182{
6dd9f4bd 183 HTTPMSGUNLOCK(_reply);
184 _reply = HTTPMSGLOCK(newrep);
4a56ee8d 185}
186
26ac0430
AJ
187struct LowestMemReader : public unary_function<store_client, void> {
188 LowestMemReader(int64_t seed):current(seed) {}
62e76326 189
26ac0430 190 void operator() (store_client const &x) {
62e76326 191 if (x.memReaderHasLowerOffset(current))
192 current = x.copyInto.offset;
193 }
194
47f6e231 195 int64_t current;
528b2c61 196};
197
26ac0430
AJ
198struct StoreClientStats : public unary_function<store_client, void> {
199 StoreClientStats(MemBuf *anEntry):where(anEntry),index(0) {}
62e76326 200
26ac0430 201 void operator()(store_client const &x) {
62e76326 202 x.dumpStats(where, index++);
528b2c61 203 }
62e76326 204
fcc35180 205 MemBuf *where;
528b2c61 206 size_t index;
207};
208
209void
fcc35180 210MemObject::stat (MemBuf * mb) const
528b2c61 211{
2fe7eff9 212 mb->Printf("\t%s %s\n",
60745f24 213 RequestMethodStr(method), log_url);
47f6e231 214 mb->Printf("\tinmem_lo: %"PRId64"\n", inmem_lo);
215 mb->Printf("\tinmem_hi: %"PRId64"\n", data_hdr.endOffset());
216 mb->Printf("\tswapout: %"PRId64" bytes queued\n",
217 swapout.queue_offset);
62e76326 218
528b2c61 219 if (swapout.sio.getRaw())
47f6e231 220 mb->Printf("\tswapout: %"PRId64" bytes written\n",
221 (int64_t) swapout.sio->offset());
62e76326 222
fcc35180 223 StoreClientStats statsVisitor(mb);
62e76326 224
4cbb7fa8 225 for_each<StoreClientStats>(clients, statsVisitor);
528b2c61 226}
227
47f6e231 228int64_t
528b2c61 229MemObject::endOffset () const
230{
231 return data_hdr.endOffset();
232}
233
47f6e231 234int64_t
528b2c61 235MemObject::size() const
236{
62e76326 237 if (object_sz < 0)
238 return endOffset();
239
528b2c61 240 return object_sz;
241}
242
243void
244MemObject::reset()
245{
246 assert(swapout.sio == NULL);
247 data_hdr.freeContent();
248 inmem_lo = 0;
249 /* Should we check for clients? */
250}
251
252
47f6e231 253int64_t
528b2c61 254MemObject::lowestMemReaderOffset() const
255{
256 LowestMemReader lowest (endOffset() + 1);
257
4cbb7fa8 258 for_each <LowestMemReader>(clients, lowest);
62e76326 259
528b2c61 260 return lowest.current;
261}
262
263/* XXX: This is wrong. It breaks *badly* on range combining */
264bool
265MemObject::readAheadPolicyCanRead() const
266{
47f6e231 267 return endOffset() - getReply()->hdr_sz < lowestMemReaderOffset() + Config.readAheadGap;
528b2c61 268}
269
270void
271MemObject::addClient(store_client *aClient)
272{
273 ++nclients;
274 dlinkAdd(aClient, &aClient->node, &clients);
275}
276
277#if URL_CHECKSUM_DEBUG
278void
279MemObject::checkUrlChecksum () const
280{
281 assert(chksum == url_checksum(url));
282}
62e76326 283
528b2c61 284#endif
285
286/*
287 * How much of the object data is on the disk?
288 */
47f6e231 289int64_t
528b2c61 290MemObject::objectBytesOnDisk() const
291{
292 /*
293 * NOTE: storeOffset() represents the disk file size,
294 * not the amount of object data on disk.
26ac0430 295 *
528b2c61 296 * If we don't have at least 'swap_hdr_sz' bytes
297 * then none of the object data is on disk.
298 *
299 * This should still be safe if swap_hdr_sz == 0,
300 * meaning we haven't even opened the swapout file
301 * yet.
302 */
62e76326 303
528b2c61 304 if (swapout.sio.getRaw() == NULL)
62e76326 305 return 0;
306
47f6e231 307 int64_t nwritten = swapout.sio->offset();
62e76326 308
ed013b6c 309 if (nwritten <= (int64_t)swap_hdr_sz)
62e76326 310 return 0;
311
47f6e231 312 return (nwritten - swap_hdr_sz);
528b2c61 313}
314
47f6e231 315int64_t
10aeba1d 316MemObject::policyLowestOffsetToKeep(bool swap) const
528b2c61 317{
318 /*
319 * Careful. lowest_offset can be greater than endOffset(), such
320 * as in the case of a range request.
321 */
47f6e231 322 int64_t lowest_offset = lowestMemReaderOffset();
62e76326 323
528b2c61 324 if (endOffset() < lowest_offset ||
ff4b33f4 325 endOffset() - inmem_lo > (int64_t)Config.Store.maxInMemObjSize ||
10aeba1d 326 (swap && !Config.onoff.memory_cache_first))
62e76326 327 return lowest_offset;
328
528b2c61 329 return inmem_lo;
330}
331
332void
333MemObject::trimSwappable()
334{
10aeba1d 335 int64_t new_mem_lo = policyLowestOffsetToKeep(1);
528b2c61 336 /*
337 * We should only free up to what we know has been written
338 * to disk, not what has been queued for writing. Otherwise
339 * there will be a chunk of the data which is not in memory
340 * and is not yet on disk.
341 * The -1 makes sure the page isn't freed until storeSwapOut has
342 * walked to the next page. (mem->swapout.memnode)
343 */
47f6e231 344 int64_t on_disk;
62e76326 345
528b2c61 346 if ((on_disk = objectBytesOnDisk()) - 1 < new_mem_lo)
62e76326 347 new_mem_lo = on_disk - 1;
348
528b2c61 349 if (new_mem_lo == -1)
62e76326 350 new_mem_lo = 0; /* the above might become -1 */
351
528b2c61 352 data_hdr.freeDataUpto(new_mem_lo);
62e76326 353
528b2c61 354 inmem_lo = new_mem_lo;
355}
356
357void
358MemObject::trimUnSwappable()
359{
10aeba1d 360 int64_t new_mem_lo = policyLowestOffsetToKeep(0);
528b2c61 361 assert (new_mem_lo > 0);
362
363 data_hdr.freeDataUpto(new_mem_lo);
364 inmem_lo = new_mem_lo;
365}
366
367
368bool
369MemObject::isContiguous() const
370{
47f6e231 371 bool result = data_hdr.hasContigousContentRange (Range<int64_t>(inmem_lo, endOffset()));
528b2c61 372 /* XXX : make this higher level */
bf8fe701 373 debugs (19, result ? 4 :3, "MemObject::isContiguous: Returning " << (result ? "true" : "false"));
528b2c61 374 return result;
375}
b67e2c8c 376
377int
378MemObject::mostBytesWanted(int max) const
379{
9a0a18de 380#if USE_DELAY_POOLS
b67e2c8c 381 /* identify delay id with largest allowance */
382 DelayId largestAllowance = mostBytesAllowed ();
383 return largestAllowance.bytesWanted(0, max);
384#else
62e76326 385
b67e2c8c 386 return max;
387#endif
388}
389
a46d2c0e 390void
391MemObject::setNoDelay(bool const newValue)
392{
9a0a18de 393#if USE_DELAY_POOLS
a46d2c0e 394
395 for (dlink_node *node = clients.head; node; node = node->next) {
396 store_client *sc = (store_client *) node->data;
397 sc->delayId.setNoDelay(newValue);
398 }
399
400#endif
401}
402
403void
404MemObject::delayRead(DeferredRead const &aRead)
405{
406 deferredReads.delayRead(aRead);
407}
408
409void
410MemObject::kickReads()
411{
412 deferredReads.kickReads(-1);
413}
414
9a0a18de 415#if USE_DELAY_POOLS
b67e2c8c 416DelayId
417MemObject::mostBytesAllowed() const
418{
419 int j;
420 int jmax = -1;
421 DelayId result;
62e76326 422
b67e2c8c 423 for (dlink_node *node = clients.head; node; node = node->next) {
62e76326 424 store_client *sc = (store_client *) node->data;
d576a6a6 425#if 0
62e76326 426 /* This test is invalid because the client may be writing data
427 * and thus will want data immediately.
428 * If we include the test, there is a race condition when too much
429 * data is read - if all sc's are writing when a read is scheduled.
430 * XXX: fixme.
431 */
432
433 if (!sc->callbackPending())
434 /* not waiting for more data */
435 continue;
436
d576a6a6 437#endif
62e76326 438
439 if (sc->getType() != STORE_MEM_CLIENT)
440 /* reading off disk */
441 continue;
442
443 j = sc->delayId.bytesWanted(0, sc->copyInto.length);
444
445 if (j > jmax) {
446 jmax = j;
447 result = sc->delayId;
448 }
b67e2c8c 449 }
62e76326 450
b67e2c8c 451 return result;
452}
62e76326 453
b67e2c8c 454#endif