]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
3 * DEBUG: section 19 Store Memory Primitives
4 * AUTHOR: Robert Collins
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "comm/Connection.h"
38 #include "HttpReply.h"
39 #include "HttpRequest.h"
41 #include "MemObject.h"
42 #include "profiler/Profiler.h"
43 #include "SquidConfig.h"
45 #include "StoreClient.h"
48 #include "DelayPools.h"
51 /* TODO: make this global or private */
52 #if URL_CHECKSUM_DEBUG
53 static unsigned int url_checksum(const char *url
);
55 url_checksum(const char *url
)
59 static unsigned char digest
[16];
61 SquidMD5Update(&M
, (unsigned char *) url
, strlen(url
));
62 SquidMD5Final(digest
, &M
);
63 memcpy(&ck
, digest
, sizeof(ck
));
69 RemovalPolicy
* mem_policy
= NULL
;
72 MemObject::inUseCount()
74 return Pool().inUseCount();
78 MemObject::storeId() const
80 if (!storeId_
.size()) {
81 debugs(20, DBG_IMPORTANT
, "Bug: Missing MemObject::storeId value");
83 storeId_
= "[unknown_URI]";
85 return storeId_
.termedBuf();
89 MemObject::logUri() const
91 return logUri_
.size() ? logUri_
.termedBuf() : storeId();
95 MemObject::hasUris() const
97 return storeId_
.size();
101 MemObject::setUris(char const *aStoreId
, char const *aLogUri
, const HttpRequestMethod
&aMethod
)
105 // fast pointer comparison for a common storeCreateEntry(url,url,...) case
106 if (!aLogUri
|| aLogUri
== aStoreId
)
107 logUri_
.clean(); // use storeId_ by default to minimize copying
113 #if URL_CHECKSUM_DEBUG
114 chksum
= url_checksum(urlXXX());
118 MemObject::MemObject(): smpCollapsed(false)
120 debugs(20, 3, HERE
<< "new MemObject " << this);
121 _reply
= new HttpReply
;
126 /* XXX account log_url */
128 swapout
.decision
= SwapOut::swNeedsCheck
;
131 MemObject::~MemObject()
133 debugs(20, 3, HERE
<< "del MemObject " << this);
134 const Ctx ctx
= ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
136 #if URL_CHECKSUM_DEBUG
140 if (!shutting_down
) { // Store::Root() is FATALly missing during shutdown
141 assert(xitTable
.index
< 0);
142 assert(memCache
.index
< 0);
143 assert(swapout
.sio
== NULL
);
146 data_hdr
.freeContent();
150 * There is no way to abort FD-less clients, so they might
151 * still have mem->clients set.
153 assert(clients
.head
== NULL
);
157 HTTPMSGUNLOCK(_reply
);
159 HTTPMSGUNLOCK(request
);
161 ctx_exit(ctx
); /* must exit before we free mem->url */
163 safe_free(vary_headers
);
167 MemObject::unlinkRequest()
169 HTTPMSGUNLOCK(request
);
173 MemObject::write(const StoreIOBuffer
&writeBuffer
)
175 PROF_start(MemObject_write
);
176 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
178 /* We don't separate out mime headers yet, so ensure that the first
179 * write is at offset 0 - where they start
181 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
183 assert (data_hdr
.write (writeBuffer
));
184 PROF_stop(MemObject_write
);
188 MemObject::dump() const
192 /* do we want this one? */
193 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
196 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
197 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
198 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
199 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
200 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << _reply
);
201 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
202 debugs(20, DBG_IMPORTANT
, "MemObject->logUri: " << logUri_
);
203 debugs(20, DBG_IMPORTANT
, "MemObject->storeId: " << storeId_
);
207 MemObject::getReply() const
213 MemObject::replaceHttpReply(HttpReply
*newrep
)
215 HTTPMSGUNLOCK(_reply
);
220 struct LowestMemReader
: public unary_function
<store_client
, void> {
221 LowestMemReader(int64_t seed
):current(seed
) {}
223 void operator() (store_client
const &x
) {
224 if (x
.memReaderHasLowerOffset(current
))
225 current
= x
.copyInto
.offset
;
231 struct StoreClientStats
: public unary_function
<store_client
, void> {
232 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
234 void operator()(store_client
const &x
) {
235 x
.dumpStats(where
, index
);
244 MemObject::stat(MemBuf
* mb
) const
246 mb
->Printf("\t" SQUIDSBUFPH
" %s\n", SQUIDSBUFPRINT(method
.image()), logUri());
248 mb
->Printf("\tvary_headers: %s\n", vary_headers
);
249 mb
->Printf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
250 mb
->Printf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
251 mb
->Printf("\tswapout: %" PRId64
" bytes queued\n",
252 swapout
.queue_offset
);
254 if (swapout
.sio
.getRaw())
255 mb
->Printf("\tswapout: %" PRId64
" bytes written\n",
256 (int64_t) swapout
.sio
->offset());
258 if (xitTable
.index
>= 0)
259 mb
->Printf("\ttransient index: %d state: %d\n",
260 xitTable
.index
, xitTable
.io
);
261 if (memCache
.index
>= 0)
262 mb
->Printf("\tmem-cache index: %d state: %d offset: %" PRId64
"\n",
263 memCache
.index
, memCache
.io
, memCache
.offset
);
265 mb
->Printf("\tobject_sz: %" PRId64
"\n", object_sz
);
267 mb
->Printf("\tsmp-collapsed\n");
269 StoreClientStats
statsVisitor(mb
);
271 for_each
<StoreClientStats
>(clients
, statsVisitor
);
275 MemObject::endOffset () const
277 return data_hdr
.endOffset();
281 MemObject::markEndOfReplyHeaders()
283 const int hdr_sz
= endOffset();
286 _reply
->hdr_sz
= hdr_sz
;
290 MemObject::size() const
299 MemObject::expectedReplySize() const
301 debugs(20, 7, HERE
<< "object_sz: " << object_sz
);
302 if (object_sz
>= 0) // complete() has been called; we know the exact answer
306 const int64_t clen
= _reply
->bodySize(method
);
307 debugs(20, 7, HERE
<< "clen: " << clen
);
308 if (clen
>= 0 && _reply
->hdr_sz
> 0) // yuck: HttpMsg sets hdr_sz to 0
309 return clen
+ _reply
->hdr_sz
;
312 return -1; // not enough information to predict
318 assert(swapout
.sio
== NULL
);
319 data_hdr
.freeContent();
321 /* Should we check for clients? */
325 MemObject::lowestMemReaderOffset() const
327 LowestMemReader
lowest (endOffset() + 1);
329 for_each
<LowestMemReader
>(clients
, lowest
);
331 return lowest
.current
;
334 /* XXX: This is wrong. It breaks *badly* on range combining */
336 MemObject::readAheadPolicyCanRead() const
338 const bool canRead
= endOffset() - getReply()->hdr_sz
<
339 lowestMemReaderOffset() + Config
.readAheadGap
;
342 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz
<<
343 " < " << lowestMemReaderOffset() << '+' << Config
.readAheadGap
);
350 MemObject::addClient(store_client
*aClient
)
353 dlinkAdd(aClient
, &aClient
->node
, &clients
);
356 #if URL_CHECKSUM_DEBUG
358 MemObject::checkUrlChecksum () const
360 assert(chksum
== url_checksum(urlXXX()));
366 * How much of the object data is on the disk?
369 MemObject::objectBytesOnDisk() const
372 * NOTE: storeOffset() represents the disk file size,
373 * not the amount of object data on disk.
375 * If we don't have at least 'swap_hdr_sz' bytes
376 * then none of the object data is on disk.
378 * This should still be safe if swap_hdr_sz == 0,
379 * meaning we haven't even opened the swapout file
383 if (swapout
.sio
.getRaw() == NULL
)
386 int64_t nwritten
= swapout
.sio
->offset();
388 if (nwritten
<= (int64_t)swap_hdr_sz
)
391 return (nwritten
- swap_hdr_sz
);
395 MemObject::policyLowestOffsetToKeep(bool swap
) const
398 * Careful. lowest_offset can be greater than endOffset(), such
399 * as in the case of a range request.
401 int64_t lowest_offset
= lowestMemReaderOffset();
403 if (endOffset() < lowest_offset
||
404 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
405 (swap
&& !Config
.onoff
.memory_cache_first
))
406 return lowest_offset
;
412 MemObject::trimSwappable()
414 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
416 * We should only free up to what we know has been written
417 * to disk, not what has been queued for writing. Otherwise
418 * there will be a chunk of the data which is not in memory
419 * and is not yet on disk.
420 * The -1 makes sure the page isn't freed until storeSwapOut has
421 * walked to the next page.
425 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
426 new_mem_lo
= on_disk
- 1;
428 if (new_mem_lo
== -1)
429 new_mem_lo
= 0; /* the above might become -1 */
431 data_hdr
.freeDataUpto(new_mem_lo
);
433 inmem_lo
= new_mem_lo
;
437 MemObject::trimUnSwappable()
439 if (const int64_t new_mem_lo
= policyLowestOffsetToKeep(false)) {
440 assert (new_mem_lo
> 0);
441 data_hdr
.freeDataUpto(new_mem_lo
);
442 inmem_lo
= new_mem_lo
;
443 } // else we should not trim anything at this time
447 MemObject::isContiguous() const
449 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
450 /* XXX : make this higher level */
451 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
456 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
459 if (!ignoreDelayPools
) {
460 /* identify delay id with largest allowance */
461 DelayId largestAllowance
= mostBytesAllowed ();
462 return largestAllowance
.bytesWanted(0, max
);
470 MemObject::setNoDelay(bool const newValue
)
474 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
475 store_client
*sc
= (store_client
*) node
->data
;
476 sc
->delayId
.setNoDelay(newValue
);
483 MemObject::delayRead(DeferredRead
const &aRead
)
485 deferredReads
.delayRead(aRead
);
489 MemObject::kickReads()
491 deferredReads
.kickReads(-1);
496 MemObject::mostBytesAllowed() const
502 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
503 store_client
*sc
= (store_client
*) node
->data
;
505 /* This test is invalid because the client may be writing data
506 * and thus will want data immediately.
507 * If we include the test, there is a race condition when too much
508 * data is read - if all sc's are writing when a read is scheduled.
512 if (!sc
->callbackPending())
513 /* not waiting for more data */
518 j
= sc
->delayId
.bytesWanted(0, sc
->copyInto
.length
);
522 result
= sc
->delayId
;
532 MemObject::availableForSwapOut() const
534 return endOffset() - swapout
.queue_offset
;