]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
2 * Copyright (C) 1996-2015 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() :
100 ping_reply_callback(NULL
),
105 #if URL_CHECKSUM_DEBUG
110 debugs(20, 3, "new MemObject " << this);
111 memset(&start_ping
, 0, sizeof(start_ping
));
112 memset(&abort
, 0, sizeof(abort
));
113 _reply
= new HttpReply
;
117 MemObject::~MemObject()
119 debugs(20, 3, "del MemObject " << this);
120 const Ctx ctx
= ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
122 #if URL_CHECKSUM_DEBUG
126 if (!shutting_down
) { // Store::Root() is FATALly missing during shutdown
127 assert(xitTable
.index
< 0);
128 assert(memCache
.index
< 0);
129 assert(swapout
.sio
== NULL
);
132 data_hdr
.freeContent();
136 * There is no way to abort FD-less clients, so they might
137 * still have mem->clients set.
139 assert(clients
.head
== NULL
);
143 HTTPMSGUNLOCK(_reply
);
145 HTTPMSGUNLOCK(request
);
147 ctx_exit(ctx
); /* must exit before we free mem->url */
149 safe_free(vary_headers
);
153 MemObject::unlinkRequest()
155 HTTPMSGUNLOCK(request
);
159 MemObject::write(const StoreIOBuffer
&writeBuffer
)
161 PROF_start(MemObject_write
);
162 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
164 /* We don't separate out mime headers yet, so ensure that the first
165 * write is at offset 0 - where they start
167 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
169 assert (data_hdr
.write (writeBuffer
));
170 PROF_stop(MemObject_write
);
174 MemObject::dump() const
178 /* do we want this one? */
179 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
182 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
183 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
184 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
185 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
186 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << _reply
);
187 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
188 debugs(20, DBG_IMPORTANT
, "MemObject->logUri: " << logUri_
);
189 debugs(20, DBG_IMPORTANT
, "MemObject->storeId: " << storeId_
);
193 MemObject::getReply() const
199 MemObject::replaceHttpReply(HttpReply
*newrep
)
201 HTTPMSGUNLOCK(_reply
);
206 struct LowestMemReader
: public unary_function
<store_client
, void> {
207 LowestMemReader(int64_t seed
):current(seed
) {}
209 void operator() (store_client
const &x
) {
210 if (x
.memReaderHasLowerOffset(current
))
211 current
= x
.copyInto
.offset
;
217 struct StoreClientStats
: public unary_function
<store_client
, void> {
218 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
220 void operator()(store_client
const &x
) {
221 x
.dumpStats(where
, index
);
230 MemObject::stat(MemBuf
* mb
) const
232 mb
->appendf("\t" SQUIDSBUFPH
" %s\n", SQUIDSBUFPRINT(method
.image()), logUri());
234 mb
->appendf("\tvary_headers: %s\n", vary_headers
);
235 mb
->appendf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
236 mb
->appendf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
237 mb
->appendf("\tswapout: %" PRId64
" bytes queued\n", swapout
.queue_offset
);
239 if (swapout
.sio
.getRaw())
240 mb
->appendf("\tswapout: %" PRId64
" bytes written\n", (int64_t) swapout
.sio
->offset());
242 if (xitTable
.index
>= 0)
243 mb
->appendf("\ttransient index: %d state: %d\n", xitTable
.index
, xitTable
.io
);
244 if (memCache
.index
>= 0)
245 mb
->appendf("\tmem-cache index: %d state: %d offset: %" PRId64
"\n", memCache
.index
, memCache
.io
, memCache
.offset
);
247 mb
->appendf("\tobject_sz: %" PRId64
"\n", object_sz
);
249 mb
->appendf("\tsmp-collapsed\n");
251 StoreClientStats
statsVisitor(mb
);
253 for_each
<StoreClientStats
>(clients
, statsVisitor
);
257 MemObject::endOffset () const
259 return data_hdr
.endOffset();
263 MemObject::markEndOfReplyHeaders()
265 const int hdr_sz
= endOffset();
268 _reply
->hdr_sz
= hdr_sz
;
272 MemObject::size() const
281 MemObject::expectedReplySize() const
283 debugs(20, 7, HERE
<< "object_sz: " << object_sz
);
284 if (object_sz
>= 0) // complete() has been called; we know the exact answer
288 const int64_t clen
= _reply
->bodySize(method
);
289 debugs(20, 7, HERE
<< "clen: " << clen
);
290 if (clen
>= 0 && _reply
->hdr_sz
> 0) // yuck: HttpMsg sets hdr_sz to 0
291 return clen
+ _reply
->hdr_sz
;
294 return -1; // not enough information to predict
300 assert(swapout
.sio
== NULL
);
301 data_hdr
.freeContent();
303 /* Should we check for clients? */
307 MemObject::lowestMemReaderOffset() const
309 LowestMemReader
lowest (endOffset() + 1);
311 for_each
<LowestMemReader
>(clients
, lowest
);
313 return lowest
.current
;
316 /* XXX: This is wrong. It breaks *badly* on range combining */
318 MemObject::readAheadPolicyCanRead() const
320 const bool canRead
= endOffset() - getReply()->hdr_sz
<
321 lowestMemReaderOffset() + Config
.readAheadGap
;
324 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz
<<
325 " < " << lowestMemReaderOffset() << '+' << Config
.readAheadGap
);
332 MemObject::addClient(store_client
*aClient
)
335 dlinkAdd(aClient
, &aClient
->node
, &clients
);
338 #if URL_CHECKSUM_DEBUG
340 MemObject::checkUrlChecksum () const
342 assert(chksum
== url_checksum(urlXXX()));
348 * How much of the object data is on the disk?
351 MemObject::objectBytesOnDisk() const
354 * NOTE: storeOffset() represents the disk file size,
355 * not the amount of object data on disk.
357 * If we don't have at least 'swap_hdr_sz' bytes
358 * then none of the object data is on disk.
360 * This should still be safe if swap_hdr_sz == 0,
361 * meaning we haven't even opened the swapout file
365 if (swapout
.sio
.getRaw() == NULL
)
368 int64_t nwritten
= swapout
.sio
->offset();
370 if (nwritten
<= (int64_t)swap_hdr_sz
)
373 return (nwritten
- swap_hdr_sz
);
377 MemObject::policyLowestOffsetToKeep(bool swap
) const
380 * Careful. lowest_offset can be greater than endOffset(), such
381 * as in the case of a range request.
383 int64_t lowest_offset
= lowestMemReaderOffset();
385 if (endOffset() < lowest_offset
||
386 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
387 (swap
&& !Config
.onoff
.memory_cache_first
))
388 return lowest_offset
;
394 MemObject::trimSwappable()
396 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
398 * We should only free up to what we know has been written
399 * to disk, not what has been queued for writing. Otherwise
400 * there will be a chunk of the data which is not in memory
401 * and is not yet on disk.
402 * The -1 makes sure the page isn't freed until storeSwapOut has
403 * walked to the next page.
407 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
408 new_mem_lo
= on_disk
- 1;
410 if (new_mem_lo
== -1)
411 new_mem_lo
= 0; /* the above might become -1 */
413 data_hdr
.freeDataUpto(new_mem_lo
);
415 inmem_lo
= new_mem_lo
;
419 MemObject::trimUnSwappable()
421 if (const int64_t new_mem_lo
= policyLowestOffsetToKeep(false)) {
422 assert (new_mem_lo
> 0);
423 data_hdr
.freeDataUpto(new_mem_lo
);
424 inmem_lo
= new_mem_lo
;
425 } // else we should not trim anything at this time
429 MemObject::isContiguous() const
431 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
432 /* XXX : make this higher level */
433 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
438 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
441 if (!ignoreDelayPools
) {
442 /* identify delay id with largest allowance */
443 DelayId largestAllowance
= mostBytesAllowed ();
444 return largestAllowance
.bytesWanted(0, max
);
452 MemObject::setNoDelay(bool const newValue
)
456 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
457 store_client
*sc
= (store_client
*) node
->data
;
458 sc
->delayId
.setNoDelay(newValue
);
465 MemObject::delayRead(DeferredRead
const &aRead
)
467 deferredReads
.delayRead(aRead
);
471 MemObject::kickReads()
473 deferredReads
.kickReads(-1);
478 MemObject::mostBytesAllowed() const
484 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
485 store_client
*sc
= (store_client
*) node
->data
;
487 /* This test is invalid because the client may be writing data
488 * and thus will want data immediately.
489 * If we include the test, there is a race condition when too much
490 * data is read - if all sc's are writing when a read is scheduled.
494 if (!sc
->callbackPending())
495 /* not waiting for more data */
500 j
= sc
->delayId
.bytesWanted(0, sc
->copyInto
.length
);
504 result
= sc
->delayId
;
514 MemObject::availableForSwapOut() const
516 return endOffset() - swapout
.queue_offset
;