]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
2 * Copyright (C) 1996-2017 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"
17 #include "MemObject.h"
18 #include "profiler/Profiler.h"
19 #include "SquidConfig.h"
21 #include "StoreClient.h"
24 #include "DelayPools.h"
27 /* TODO: make this global or private */
28 #if URL_CHECKSUM_DEBUG
29 static unsigned int url_checksum(const char *url
);
31 url_checksum(const char *url
)
35 static unsigned char digest
[16];
37 SquidMD5Update(&M
, (unsigned char *) url
, strlen(url
));
38 SquidMD5Final(digest
, &M
);
39 memcpy(&ck
, digest
, sizeof(ck
));
45 RemovalPolicy
* mem_policy
= NULL
;
48 MemObject::inUseCount()
50 return Pool().inUseCount();
54 MemObject::storeId() const
56 if (!storeId_
.size()) {
57 debugs(20, DBG_IMPORTANT
, "Bug: Missing MemObject::storeId value");
59 storeId_
= "[unknown_URI]";
61 return storeId_
.termedBuf();
65 MemObject::logUri() const
67 return logUri_
.size() ? logUri_
.termedBuf() : storeId();
71 MemObject::hasUris() const
73 return storeId_
.size();
77 MemObject::setUris(char const *aStoreId
, char const *aLogUri
, const HttpRequestMethod
&aMethod
)
81 // fast pointer comparison for a common storeCreateEntry(url,url,...) case
82 if (!aLogUri
|| aLogUri
== aStoreId
)
83 logUri_
.clean(); // use storeId_ by default to minimize copying
89 #if URL_CHECKSUM_DEBUG
90 chksum
= url_checksum(urlXXX());
94 MemObject::MemObject() :
98 ping_reply_callback(nullptr),
103 #if URL_CHECKSUM_DEBUG
106 vary_headers(nullptr)
108 debugs(20, 3, "new MemObject " << this);
109 memset(&start_ping
, 0, sizeof(start_ping
));
110 memset(&abort
, 0, sizeof(abort
));
111 reply_
= new HttpReply
;
114 MemObject::~MemObject()
116 debugs(20, 3, "del MemObject " << this);
117 const Ctx ctx
= ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
119 #if URL_CHECKSUM_DEBUG
123 if (!shutting_down
) { // Store::Root() is FATALly missing during shutdown
124 assert(xitTable
.index
< 0);
125 assert(memCache
.index
< 0);
126 assert(swapout
.sio
== NULL
);
129 data_hdr
.freeContent();
133 * There is no way to abort FD-less clients, so they might
134 * still have mem->clients set.
136 assert(clients
.head
== NULL
);
140 ctx_exit(ctx
); /* must exit before we free mem->url */
144 MemObject::write(const StoreIOBuffer
&writeBuffer
)
146 PROF_start(MemObject_write
);
147 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
149 /* We don't separate out mime headers yet, so ensure that the first
150 * write is at offset 0 - where they start
152 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
154 assert (data_hdr
.write (writeBuffer
));
155 PROF_stop(MemObject_write
);
159 MemObject::dump() const
163 /* do we want this one? */
164 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
167 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
168 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
169 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
170 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
171 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << reply_
);
172 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
173 debugs(20, DBG_IMPORTANT
, "MemObject->logUri: " << logUri_
);
174 debugs(20, DBG_IMPORTANT
, "MemObject->storeId: " << storeId_
);
177 struct LowestMemReader
: public unary_function
<store_client
, void> {
178 LowestMemReader(int64_t seed
):current(seed
) {}
180 void operator() (store_client
const &x
) {
181 if (x
.memReaderHasLowerOffset(current
))
182 current
= x
.copyInto
.offset
;
188 struct StoreClientStats
: public unary_function
<store_client
, void> {
189 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
191 void operator()(store_client
const &x
) {
192 x
.dumpStats(where
, index
);
201 MemObject::stat(MemBuf
* mb
) const
203 mb
->appendf("\t" SQUIDSBUFPH
" %s\n", SQUIDSBUFPRINT(method
.image()), logUri());
204 if (!vary_headers
.isEmpty())
205 mb
->appendf("\tvary_headers: " SQUIDSBUFPH
"\n", SQUIDSBUFPRINT(vary_headers
));
206 mb
->appendf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
207 mb
->appendf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
208 mb
->appendf("\tswapout: %" PRId64
" bytes queued\n", swapout
.queue_offset
);
210 if (swapout
.sio
.getRaw())
211 mb
->appendf("\tswapout: %" PRId64
" bytes written\n", (int64_t) swapout
.sio
->offset());
213 if (xitTable
.index
>= 0)
214 mb
->appendf("\ttransient index: %d state: %d\n", xitTable
.index
, xitTable
.io
);
215 if (memCache
.index
>= 0)
216 mb
->appendf("\tmem-cache index: %d state: %d offset: %" PRId64
"\n", memCache
.index
, memCache
.io
, memCache
.offset
);
218 mb
->appendf("\tobject_sz: %" PRId64
"\n", object_sz
);
220 mb
->appendf("\tsmp-collapsed\n");
222 StoreClientStats
statsVisitor(mb
);
224 for_each
<StoreClientStats
>(clients
, statsVisitor
);
228 MemObject::endOffset () const
230 return data_hdr
.endOffset();
234 MemObject::markEndOfReplyHeaders()
236 const int hdr_sz
= endOffset();
239 reply_
->hdr_sz
= hdr_sz
;
243 MemObject::size() const
252 MemObject::expectedReplySize() const
254 debugs(20, 7, "object_sz: " << object_sz
);
255 if (object_sz
>= 0) // complete() has been called; we know the exact answer
259 const int64_t clen
= reply_
->bodySize(method
);
260 debugs(20, 7, "clen: " << clen
);
261 if (clen
>= 0 && reply_
->hdr_sz
> 0) // yuck: Http::Message sets hdr_sz to 0
262 return clen
+ reply_
->hdr_sz
;
265 return -1; // not enough information to predict
271 assert(swapout
.sio
== NULL
);
272 data_hdr
.freeContent();
274 /* Should we check for clients? */
280 MemObject::lowestMemReaderOffset() const
282 LowestMemReader
lowest (endOffset() + 1);
284 for_each
<LowestMemReader
>(clients
, lowest
);
286 return lowest
.current
;
289 /* XXX: This is wrong. It breaks *badly* on range combining */
291 MemObject::readAheadPolicyCanRead() const
293 const bool canRead
= endOffset() - getReply()->hdr_sz
<
294 lowestMemReaderOffset() + Config
.readAheadGap
;
297 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz
<<
298 " < " << lowestMemReaderOffset() << '+' << Config
.readAheadGap
);
305 MemObject::addClient(store_client
*aClient
)
308 dlinkAdd(aClient
, &aClient
->node
, &clients
);
311 #if URL_CHECKSUM_DEBUG
313 MemObject::checkUrlChecksum () const
315 assert(chksum
== url_checksum(urlXXX()));
321 * How much of the object data is on the disk?
324 MemObject::objectBytesOnDisk() const
327 * NOTE: storeOffset() represents the disk file size,
328 * not the amount of object data on disk.
330 * If we don't have at least 'swap_hdr_sz' bytes
331 * then none of the object data is on disk.
333 * This should still be safe if swap_hdr_sz == 0,
334 * meaning we haven't even opened the swapout file
338 if (swapout
.sio
.getRaw() == NULL
)
341 int64_t nwritten
= swapout
.sio
->offset();
343 if (nwritten
<= (int64_t)swap_hdr_sz
)
346 return (nwritten
- swap_hdr_sz
);
350 MemObject::policyLowestOffsetToKeep(bool swap
) const
353 * Careful. lowest_offset can be greater than endOffset(), such
354 * as in the case of a range request.
356 int64_t lowest_offset
= lowestMemReaderOffset();
358 if (endOffset() < lowest_offset
||
359 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
360 (swap
&& !Config
.onoff
.memory_cache_first
))
361 return lowest_offset
;
367 MemObject::trimSwappable()
369 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
371 * We should only free up to what we know has been written
372 * to disk, not what has been queued for writing. Otherwise
373 * there will be a chunk of the data which is not in memory
374 * and is not yet on disk.
375 * The -1 makes sure the page isn't freed until storeSwapOut has
376 * walked to the next page.
380 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
381 new_mem_lo
= on_disk
- 1;
383 if (new_mem_lo
== -1)
384 new_mem_lo
= 0; /* the above might become -1 */
386 data_hdr
.freeDataUpto(new_mem_lo
);
388 inmem_lo
= new_mem_lo
;
392 MemObject::trimUnSwappable()
394 if (const int64_t new_mem_lo
= policyLowestOffsetToKeep(false)) {
395 assert (new_mem_lo
> 0);
396 data_hdr
.freeDataUpto(new_mem_lo
);
397 inmem_lo
= new_mem_lo
;
398 } // else we should not trim anything at this time
402 MemObject::isContiguous() const
404 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
405 /* XXX : make this higher level */
406 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
411 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
414 if (!ignoreDelayPools
) {
415 /* identify delay id with largest allowance */
416 DelayId largestAllowance
= mostBytesAllowed ();
417 return largestAllowance
.bytesWanted(0, max
);
425 MemObject::setNoDelay(bool const newValue
)
429 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
430 store_client
*sc
= (store_client
*) node
->data
;
431 sc
->delayId
.setNoDelay(newValue
);
438 MemObject::delayRead(DeferredRead
const &aRead
)
441 if (readAheadPolicyCanRead()) {
442 if (DelayId mostAllowedId
= mostBytesAllowed()) {
443 mostAllowedId
.delayRead(aRead
);
448 deferredReads
.delayRead(aRead
);
452 MemObject::kickReads()
454 deferredReads
.kickReads(-1);
459 MemObject::mostBytesAllowed() const
465 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
466 store_client
*sc
= (store_client
*) node
->data
;
468 /* This test is invalid because the client may be writing data
469 * and thus will want data immediately.
470 * If we include the test, there is a race condition when too much
471 * data is read - if all sc's are writing when a read is scheduled.
475 if (!sc
->callbackPending())
476 /* not waiting for more data */
481 j
= sc
->delayId
.bytesWanted(0, sc
->copyInto
.length
);
485 result
= sc
->delayId
;
495 MemObject::availableForSwapOut() const
497 return endOffset() - swapout
.queue_offset
;