]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 19 Store Memory Primitives */
12 #include "comm/Connection.h"
15 #include "HttpReply.h"
16 #include "HttpRequest.h"
18 #include "MemObject.h"
19 #include "profiler/Profiler.h"
20 #include "SquidConfig.h"
22 #include "StoreClient.h"
25 #include "DelayPools.h"
28 /* TODO: make this global or private */
29 #if URL_CHECKSUM_DEBUG
30 static unsigned int url_checksum(const char *url
);
32 url_checksum(const char *url
)
36 static unsigned char digest
[16];
38 SquidMD5Update(&M
, (unsigned char *) url
, strlen(url
));
39 SquidMD5Final(digest
, &M
);
40 memcpy(&ck
, digest
, sizeof(ck
));
46 RemovalPolicy
* mem_policy
= NULL
;
49 MemObject::inUseCount()
51 return Pool().inUseCount();
55 MemObject::storeId() const
57 if (!storeId_
.size()) {
58 debugs(20, DBG_IMPORTANT
, "Bug: Missing MemObject::storeId value");
60 storeId_
= "[unknown_URI]";
62 return storeId_
.termedBuf();
66 MemObject::logUri() const
68 return logUri_
.size() ? logUri_
.termedBuf() : storeId();
72 MemObject::hasUris() const
74 return storeId_
.size();
78 MemObject::setUris(char const *aStoreId
, char const *aLogUri
, const HttpRequestMethod
&aMethod
)
82 // fast pointer comparison for a common storeCreateEntry(url,url,...) case
83 if (!aLogUri
|| aLogUri
== aStoreId
)
84 logUri_
.clean(); // use storeId_ by default to minimize copying
90 #if URL_CHECKSUM_DEBUG
91 chksum
= url_checksum(urlXXX());
95 MemObject::MemObject(): smpCollapsed(false)
97 debugs(20, 3, HERE
<< "new MemObject " << this);
98 _reply
= new HttpReply
;
103 /* XXX account log_url */
105 swapout
.decision
= SwapOut::swNeedsCheck
;
108 MemObject::~MemObject()
110 debugs(20, 3, HERE
<< "del MemObject " << this);
111 const Ctx ctx
= ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
113 #if URL_CHECKSUM_DEBUG
117 if (!shutting_down
) { // Store::Root() is FATALly missing during shutdown
118 assert(xitTable
.index
< 0);
119 assert(memCache
.index
< 0);
120 assert(swapout
.sio
== NULL
);
123 data_hdr
.freeContent();
127 * There is no way to abort FD-less clients, so they might
128 * still have mem->clients set.
130 assert(clients
.head
== NULL
);
134 HTTPMSGUNLOCK(_reply
);
136 HTTPMSGUNLOCK(request
);
138 ctx_exit(ctx
); /* must exit before we free mem->url */
140 safe_free(vary_headers
);
144 MemObject::unlinkRequest()
146 HTTPMSGUNLOCK(request
);
150 MemObject::write(const StoreIOBuffer
&writeBuffer
)
152 PROF_start(MemObject_write
);
153 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
155 /* We don't separate out mime headers yet, so ensure that the first
156 * write is at offset 0 - where they start
158 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
160 assert (data_hdr
.write (writeBuffer
));
161 PROF_stop(MemObject_write
);
165 MemObject::dump() const
169 /* do we want this one? */
170 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
173 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
174 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
175 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
176 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
177 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << _reply
);
178 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
179 debugs(20, DBG_IMPORTANT
, "MemObject->logUri: " << logUri_
);
180 debugs(20, DBG_IMPORTANT
, "MemObject->storeId: " << storeId_
);
184 MemObject::getReply() const
190 MemObject::replaceHttpReply(HttpReply
*newrep
)
192 HTTPMSGUNLOCK(_reply
);
197 struct LowestMemReader
: public unary_function
<store_client
, void> {
198 LowestMemReader(int64_t seed
):current(seed
) {}
200 void operator() (store_client
const &x
) {
201 if (x
.memReaderHasLowerOffset(current
))
202 current
= x
.copyInto
.offset
;
208 struct StoreClientStats
: public unary_function
<store_client
, void> {
209 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
211 void operator()(store_client
const &x
) {
212 x
.dumpStats(where
, index
);
221 MemObject::stat(MemBuf
* mb
) const
223 mb
->Printf("\t" SQUIDSBUFPH
" %s\n", SQUIDSBUFPRINT(method
.image()), logUri());
225 mb
->Printf("\tvary_headers: %s\n", vary_headers
);
226 mb
->Printf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
227 mb
->Printf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
228 mb
->Printf("\tswapout: %" PRId64
" bytes queued\n",
229 swapout
.queue_offset
);
231 if (swapout
.sio
.getRaw())
232 mb
->Printf("\tswapout: %" PRId64
" bytes written\n",
233 (int64_t) swapout
.sio
->offset());
235 if (xitTable
.index
>= 0)
236 mb
->Printf("\ttransient index: %d state: %d\n",
237 xitTable
.index
, xitTable
.io
);
238 if (memCache
.index
>= 0)
239 mb
->Printf("\tmem-cache index: %d state: %d offset: %" PRId64
"\n",
240 memCache
.index
, memCache
.io
, memCache
.offset
);
242 mb
->Printf("\tobject_sz: %" PRId64
"\n", object_sz
);
244 mb
->Printf("\tsmp-collapsed\n");
246 StoreClientStats
statsVisitor(mb
);
248 for_each
<StoreClientStats
>(clients
, statsVisitor
);
252 MemObject::endOffset () const
254 return data_hdr
.endOffset();
258 MemObject::markEndOfReplyHeaders()
260 const int hdr_sz
= endOffset();
263 _reply
->hdr_sz
= hdr_sz
;
267 MemObject::size() const
276 MemObject::expectedReplySize() const
278 debugs(20, 7, HERE
<< "object_sz: " << object_sz
);
279 if (object_sz
>= 0) // complete() has been called; we know the exact answer
283 const int64_t clen
= _reply
->bodySize(method
);
284 debugs(20, 7, HERE
<< "clen: " << clen
);
285 if (clen
>= 0 && _reply
->hdr_sz
> 0) // yuck: HttpMsg sets hdr_sz to 0
286 return clen
+ _reply
->hdr_sz
;
289 return -1; // not enough information to predict
295 assert(swapout
.sio
== NULL
);
296 data_hdr
.freeContent();
298 /* Should we check for clients? */
302 MemObject::lowestMemReaderOffset() const
304 LowestMemReader
lowest (endOffset() + 1);
306 for_each
<LowestMemReader
>(clients
, lowest
);
308 return lowest
.current
;
311 /* XXX: This is wrong. It breaks *badly* on range combining */
313 MemObject::readAheadPolicyCanRead() const
315 const bool canRead
= endOffset() - getReply()->hdr_sz
<
316 lowestMemReaderOffset() + Config
.readAheadGap
;
319 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz
<<
320 " < " << lowestMemReaderOffset() << '+' << Config
.readAheadGap
);
327 MemObject::addClient(store_client
*aClient
)
330 dlinkAdd(aClient
, &aClient
->node
, &clients
);
333 #if URL_CHECKSUM_DEBUG
335 MemObject::checkUrlChecksum () const
337 assert(chksum
== url_checksum(urlXXX()));
343 * How much of the object data is on the disk?
346 MemObject::objectBytesOnDisk() const
349 * NOTE: storeOffset() represents the disk file size,
350 * not the amount of object data on disk.
352 * If we don't have at least 'swap_hdr_sz' bytes
353 * then none of the object data is on disk.
355 * This should still be safe if swap_hdr_sz == 0,
356 * meaning we haven't even opened the swapout file
360 if (swapout
.sio
.getRaw() == NULL
)
363 int64_t nwritten
= swapout
.sio
->offset();
365 if (nwritten
<= (int64_t)swap_hdr_sz
)
368 return (nwritten
- swap_hdr_sz
);
372 MemObject::policyLowestOffsetToKeep(bool swap
) const
375 * Careful. lowest_offset can be greater than endOffset(), such
376 * as in the case of a range request.
378 int64_t lowest_offset
= lowestMemReaderOffset();
380 if (endOffset() < lowest_offset
||
381 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
382 (swap
&& !Config
.onoff
.memory_cache_first
))
383 return lowest_offset
;
389 MemObject::trimSwappable()
391 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
393 * We should only free up to what we know has been written
394 * to disk, not what has been queued for writing. Otherwise
395 * there will be a chunk of the data which is not in memory
396 * and is not yet on disk.
397 * The -1 makes sure the page isn't freed until storeSwapOut has
398 * walked to the next page.
402 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
403 new_mem_lo
= on_disk
- 1;
405 if (new_mem_lo
== -1)
406 new_mem_lo
= 0; /* the above might become -1 */
408 data_hdr
.freeDataUpto(new_mem_lo
);
410 inmem_lo
= new_mem_lo
;
414 MemObject::trimUnSwappable()
416 if (const int64_t new_mem_lo
= policyLowestOffsetToKeep(false)) {
417 assert (new_mem_lo
> 0);
418 data_hdr
.freeDataUpto(new_mem_lo
);
419 inmem_lo
= new_mem_lo
;
420 } // else we should not trim anything at this time
424 MemObject::isContiguous() const
426 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
427 /* XXX : make this higher level */
428 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
433 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
436 if (!ignoreDelayPools
) {
437 /* identify delay id with largest allowance */
438 DelayId largestAllowance
= mostBytesAllowed ();
439 return largestAllowance
.bytesWanted(0, max
);
447 MemObject::setNoDelay(bool const newValue
)
451 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
452 store_client
*sc
= (store_client
*) node
->data
;
453 sc
->delayId
.setNoDelay(newValue
);
460 MemObject::delayRead(DeferredRead
const &aRead
)
462 deferredReads
.delayRead(aRead
);
466 MemObject::kickReads()
468 deferredReads
.kickReads(-1);
473 MemObject::mostBytesAllowed() const
479 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
480 store_client
*sc
= (store_client
*) node
->data
;
482 /* This test is invalid because the client may be writing data
483 * and thus will want data immediately.
484 * If we include the test, there is a race condition when too much
485 * data is read - if all sc's are writing when a read is scheduled.
489 if (!sc
->callbackPending())
490 /* not waiting for more data */
495 j
= sc
->delayId
.bytesWanted(0, sc
->copyInto
.length
);
499 result
= sc
->delayId
;
509 MemObject::availableForSwapOut() const
511 return endOffset() - swapout
.queue_offset
;