]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
2 * Copyright (C) 1996-2020 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
)
83 debugs(88, 3, this << " storeId: " << storeId_
);
85 // fast pointer comparison for a common storeCreateEntry(url,url,...) case
86 if (!aLogUri
|| aLogUri
== aStoreId
)
87 logUri_
.clean(); // use storeId_ by default to minimize copying
93 #if URL_CHECKSUM_DEBUG
94 chksum
= url_checksum(urlXXX());
98 MemObject::MemObject()
100 debugs(20, 3, "MemObject constructed, this=" << this);
101 ping_reply_callback
= nullptr;
102 memset(&start_ping
, 0, sizeof(start_ping
));
103 reply_
= new HttpReply
;
106 MemObject::~MemObject()
108 debugs(20, 3, "MemObject destructed, this=" << this);
109 const Ctx ctx
= ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
111 #if URL_CHECKSUM_DEBUG
115 if (!shutting_down
) { // Store::Root() is FATALly missing during shutdown
116 assert(xitTable
.index
< 0);
117 assert(memCache
.index
< 0);
118 assert(swapout
.sio
== NULL
);
121 data_hdr
.freeContent();
125 * There is no way to abort FD-less clients, so they might
126 * still have mem->clients set.
128 assert(clients
.head
== NULL
);
132 ctx_exit(ctx
); /* must exit before we free mem->url */
136 MemObject::adjustableBaseReply()
138 assert(!updatedReply_
);
143 MemObject::replaceBaseReply(const HttpReplyPointer
&r
)
147 updatedReply_
= nullptr;
151 MemObject::write(const StoreIOBuffer
&writeBuffer
)
153 PROF_start(MemObject_write
);
154 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
156 /* We don't separate out mime headers yet, so ensure that the first
157 * write is at offset 0 - where they start
159 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
161 assert (data_hdr
.write (writeBuffer
));
162 PROF_stop(MemObject_write
);
166 MemObject::dump() const
170 /* do we want this one? */
171 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
174 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
175 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
176 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
177 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
178 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << reply_
);
179 debugs(20, DBG_IMPORTANT
, "MemObject->updatedReply: " << updatedReply_
);
180 debugs(20, DBG_IMPORTANT
, "MemObject->appliedUpdates: " << appliedUpdates
);
181 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
182 debugs(20, DBG_IMPORTANT
, "MemObject->logUri: " << logUri_
);
183 debugs(20, DBG_IMPORTANT
, "MemObject->storeId: " << storeId_
);
186 struct LowestMemReader
: public unary_function
<store_client
, void> {
187 LowestMemReader(int64_t seed
):current(seed
) {}
189 void operator() (store_client
const &x
) {
190 if (x
.memReaderHasLowerOffset(current
))
191 current
= x
.copyInto
.offset
;
197 struct StoreClientStats
: public unary_function
<store_client
, void> {
198 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
200 void operator()(store_client
const &x
) {
201 x
.dumpStats(where
, index
);
210 MemObject::stat(MemBuf
* mb
) const
212 mb
->appendf("\t" SQUIDSBUFPH
" %s\n", SQUIDSBUFPRINT(method
.image()), logUri());
213 if (!vary_headers
.isEmpty())
214 mb
->appendf("\tvary_headers: " SQUIDSBUFPH
"\n", SQUIDSBUFPRINT(vary_headers
));
215 mb
->appendf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
216 mb
->appendf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
217 mb
->appendf("\tswapout: %" PRId64
" bytes queued\n", swapout
.queue_offset
);
219 if (swapout
.sio
.getRaw())
220 mb
->appendf("\tswapout: %" PRId64
" bytes written\n", (int64_t) swapout
.sio
->offset());
222 if (xitTable
.index
>= 0)
223 mb
->appendf("\ttransient index: %d state: %d\n", xitTable
.index
, xitTable
.io
);
224 if (memCache
.index
>= 0)
225 mb
->appendf("\tmem-cache index: %d state: %d offset: %" PRId64
"\n", memCache
.index
, memCache
.io
, memCache
.offset
);
227 mb
->appendf("\tobject_sz: %" PRId64
"\n", object_sz
);
229 StoreClientStats
statsVisitor(mb
);
231 for_each
<StoreClientStats
>(clients
, statsVisitor
);
235 MemObject::endOffset () const
237 return data_hdr
.endOffset();
241 MemObject::markEndOfReplyHeaders()
243 const int hdr_sz
= endOffset();
246 reply_
->hdr_sz
= hdr_sz
;
250 MemObject::size() const
259 MemObject::expectedReplySize() const
261 if (object_sz
>= 0) {
262 debugs(20, 7, object_sz
<< " frozen by complete()");
266 const auto hdr_sz
= baseReply().hdr_sz
;
268 // Cannot predict future length using an empty/unset or HTTP/0 reply.
269 // For any HTTP/1 reply, hdr_sz is positive -- status-line cannot be empty.
273 const auto clen
= baseReply().bodySize(method
);
275 debugs(20, 7, "unknown; hdr: " << hdr_sz
);
279 const auto messageSize
= clen
+ hdr_sz
;
280 debugs(20, 7, messageSize
<< " hdr: " << hdr_sz
<< " clen: " << clen
);
287 assert(swapout
.sio
== NULL
);
288 data_hdr
.freeContent();
290 /* Should we check for clients? */
293 updatedReply_
= nullptr;
294 appliedUpdates
= false;
298 MemObject::lowestMemReaderOffset() const
300 LowestMemReader
lowest (endOffset() + 1);
302 for_each
<LowestMemReader
>(clients
, lowest
);
304 return lowest
.current
;
307 /* XXX: This is wrong. It breaks *badly* on range combining */
309 MemObject::readAheadPolicyCanRead() const
311 const auto savedHttpHeaders
= baseReply().hdr_sz
;
312 const bool canRead
= endOffset() - savedHttpHeaders
<
313 lowestMemReaderOffset() + Config
.readAheadGap
;
316 debugs(19, 5, "no: " << endOffset() << '-' << savedHttpHeaders
<<
317 " < " << lowestMemReaderOffset() << '+' << Config
.readAheadGap
);
324 MemObject::addClient(store_client
*aClient
)
327 dlinkAdd(aClient
, &aClient
->node
, &clients
);
330 #if URL_CHECKSUM_DEBUG
332 MemObject::checkUrlChecksum () const
334 assert(chksum
== url_checksum(urlXXX()));
340 * How much of the object data is on the disk?
343 MemObject::objectBytesOnDisk() const
346 * NOTE: storeOffset() represents the disk file size,
347 * not the amount of object data on disk.
349 * If we don't have at least 'swap_hdr_sz' bytes
350 * then none of the object data is on disk.
352 * This should still be safe if swap_hdr_sz == 0,
353 * meaning we haven't even opened the swapout file
357 if (swapout
.sio
.getRaw() == NULL
)
360 int64_t nwritten
= swapout
.sio
->offset();
362 if (nwritten
<= (int64_t)swap_hdr_sz
)
365 return (nwritten
- swap_hdr_sz
);
369 MemObject::policyLowestOffsetToKeep(bool swap
) const
372 * Careful. lowest_offset can be greater than endOffset(), such
373 * as in the case of a range request.
375 int64_t lowest_offset
= lowestMemReaderOffset();
377 if (endOffset() < lowest_offset
||
378 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
379 (swap
&& !Config
.onoff
.memory_cache_first
))
380 return lowest_offset
;
386 MemObject::trimSwappable()
388 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
390 * We should only free up to what we know has been written
391 * to disk, not what has been queued for writing. Otherwise
392 * there will be a chunk of the data which is not in memory
393 * and is not yet on disk.
394 * The -1 makes sure the page isn't freed until storeSwapOut has
395 * walked to the next page.
399 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
400 new_mem_lo
= on_disk
- 1;
402 if (new_mem_lo
== -1)
403 new_mem_lo
= 0; /* the above might become -1 */
405 data_hdr
.freeDataUpto(new_mem_lo
);
407 inmem_lo
= new_mem_lo
;
411 MemObject::trimUnSwappable()
413 if (const int64_t new_mem_lo
= policyLowestOffsetToKeep(false)) {
414 assert (new_mem_lo
> 0);
415 data_hdr
.freeDataUpto(new_mem_lo
);
416 inmem_lo
= new_mem_lo
;
417 } // else we should not trim anything at this time
421 MemObject::isContiguous() const
423 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
424 /* XXX : make this higher level */
425 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
430 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
433 if (!ignoreDelayPools
) {
434 /* identify delay id with largest allowance */
435 DelayId largestAllowance
= mostBytesAllowed ();
436 return largestAllowance
.bytesWanted(0, max
);
444 MemObject::setNoDelay(bool const newValue
)
448 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
449 store_client
*sc
= (store_client
*) node
->data
;
450 sc
->delayId
.setNoDelay(newValue
);
457 MemObject::delayRead(DeferredRead
const &aRead
)
460 if (readAheadPolicyCanRead()) {
461 if (DelayId mostAllowedId
= mostBytesAllowed()) {
462 mostAllowedId
.delayRead(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
;