]>
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() :
99 ping_reply_callback(nullptr),
104 #if URL_CHECKSUM_DEBUG
107 vary_headers(nullptr)
109 debugs(20, 3, "new MemObject " << this);
110 memset(&start_ping
, 0, sizeof(start_ping
));
111 memset(&abort
, 0, sizeof(abort
));
112 _reply
= new HttpReply
;
116 MemObject::~MemObject()
118 debugs(20, 3, "del MemObject " << this);
119 const Ctx ctx
= ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
121 #if URL_CHECKSUM_DEBUG
125 if (!shutting_down
) { // Store::Root() is FATALly missing during shutdown
126 assert(xitTable
.index
< 0);
127 assert(memCache
.index
< 0);
128 assert(swapout
.sio
== NULL
);
131 data_hdr
.freeContent();
135 * There is no way to abort FD-less clients, so they might
136 * still have mem->clients set.
138 assert(clients
.head
== NULL
);
142 HTTPMSGUNLOCK(_reply
);
144 ctx_exit(ctx
); /* must exit before we free mem->url */
148 MemObject::write(const StoreIOBuffer
&writeBuffer
)
150 PROF_start(MemObject_write
);
151 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
153 /* We don't separate out mime headers yet, so ensure that the first
154 * write is at offset 0 - where they start
156 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
158 assert (data_hdr
.write (writeBuffer
));
159 PROF_stop(MemObject_write
);
163 MemObject::dump() const
167 /* do we want this one? */
168 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
171 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
172 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
173 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
174 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
175 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << _reply
);
176 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
177 debugs(20, DBG_IMPORTANT
, "MemObject->logUri: " << logUri_
);
178 debugs(20, DBG_IMPORTANT
, "MemObject->storeId: " << storeId_
);
182 MemObject::getReply() const
188 MemObject::replaceHttpReply(HttpReply
*newrep
)
190 HTTPMSGUNLOCK(_reply
);
195 struct LowestMemReader
: public unary_function
<store_client
, void> {
196 LowestMemReader(int64_t seed
):current(seed
) {}
198 void operator() (store_client
const &x
) {
199 if (x
.memReaderHasLowerOffset(current
))
200 current
= x
.copyInto
.offset
;
206 struct StoreClientStats
: public unary_function
<store_client
, void> {
207 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
209 void operator()(store_client
const &x
) {
210 x
.dumpStats(where
, index
);
219 MemObject::stat(MemBuf
* mb
) const
221 mb
->appendf("\t" SQUIDSBUFPH
" %s\n", SQUIDSBUFPRINT(method
.image()), logUri());
222 if (!vary_headers
.isEmpty())
223 mb
->appendf("\tvary_headers: " SQUIDSBUFPH
"\n", SQUIDSBUFPRINT(vary_headers
));
224 mb
->appendf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
225 mb
->appendf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
226 mb
->appendf("\tswapout: %" PRId64
" bytes queued\n", swapout
.queue_offset
);
228 if (swapout
.sio
.getRaw())
229 mb
->appendf("\tswapout: %" PRId64
" bytes written\n", (int64_t) swapout
.sio
->offset());
231 if (xitTable
.index
>= 0)
232 mb
->appendf("\ttransient index: %d state: %d\n", xitTable
.index
, xitTable
.io
);
233 if (memCache
.index
>= 0)
234 mb
->appendf("\tmem-cache index: %d state: %d offset: %" PRId64
"\n", memCache
.index
, memCache
.io
, memCache
.offset
);
236 mb
->appendf("\tobject_sz: %" PRId64
"\n", object_sz
);
238 mb
->appendf("\tsmp-collapsed\n");
240 StoreClientStats
statsVisitor(mb
);
242 for_each
<StoreClientStats
>(clients
, statsVisitor
);
246 MemObject::endOffset () const
248 return data_hdr
.endOffset();
252 MemObject::markEndOfReplyHeaders()
254 const int hdr_sz
= endOffset();
257 _reply
->hdr_sz
= hdr_sz
;
261 MemObject::size() const
270 MemObject::expectedReplySize() const
272 debugs(20, 7, HERE
<< "object_sz: " << object_sz
);
273 if (object_sz
>= 0) // complete() has been called; we know the exact answer
277 const int64_t clen
= _reply
->bodySize(method
);
278 debugs(20, 7, HERE
<< "clen: " << clen
);
279 if (clen
>= 0 && _reply
->hdr_sz
> 0) // yuck: Http::Message sets hdr_sz to 0
280 return clen
+ _reply
->hdr_sz
;
283 return -1; // not enough information to predict
289 assert(swapout
.sio
== NULL
);
290 data_hdr
.freeContent();
292 /* Should we check for clients? */
296 MemObject::lowestMemReaderOffset() const
298 LowestMemReader
lowest (endOffset() + 1);
300 for_each
<LowestMemReader
>(clients
, lowest
);
302 return lowest
.current
;
305 /* XXX: This is wrong. It breaks *badly* on range combining */
307 MemObject::readAheadPolicyCanRead() const
309 const bool canRead
= endOffset() - getReply()->hdr_sz
<
310 lowestMemReaderOffset() + Config
.readAheadGap
;
313 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz
<<
314 " < " << lowestMemReaderOffset() << '+' << Config
.readAheadGap
);
321 MemObject::addClient(store_client
*aClient
)
324 dlinkAdd(aClient
, &aClient
->node
, &clients
);
327 #if URL_CHECKSUM_DEBUG
329 MemObject::checkUrlChecksum () const
331 assert(chksum
== url_checksum(urlXXX()));
337 * How much of the object data is on the disk?
340 MemObject::objectBytesOnDisk() const
343 * NOTE: storeOffset() represents the disk file size,
344 * not the amount of object data on disk.
346 * If we don't have at least 'swap_hdr_sz' bytes
347 * then none of the object data is on disk.
349 * This should still be safe if swap_hdr_sz == 0,
350 * meaning we haven't even opened the swapout file
354 if (swapout
.sio
.getRaw() == NULL
)
357 int64_t nwritten
= swapout
.sio
->offset();
359 if (nwritten
<= (int64_t)swap_hdr_sz
)
362 return (nwritten
- swap_hdr_sz
);
366 MemObject::policyLowestOffsetToKeep(bool swap
) const
369 * Careful. lowest_offset can be greater than endOffset(), such
370 * as in the case of a range request.
372 int64_t lowest_offset
= lowestMemReaderOffset();
374 if (endOffset() < lowest_offset
||
375 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
376 (swap
&& !Config
.onoff
.memory_cache_first
))
377 return lowest_offset
;
383 MemObject::trimSwappable()
385 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
387 * We should only free up to what we know has been written
388 * to disk, not what has been queued for writing. Otherwise
389 * there will be a chunk of the data which is not in memory
390 * and is not yet on disk.
391 * The -1 makes sure the page isn't freed until storeSwapOut has
392 * walked to the next page.
396 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
397 new_mem_lo
= on_disk
- 1;
399 if (new_mem_lo
== -1)
400 new_mem_lo
= 0; /* the above might become -1 */
402 data_hdr
.freeDataUpto(new_mem_lo
);
404 inmem_lo
= new_mem_lo
;
408 MemObject::trimUnSwappable()
410 if (const int64_t new_mem_lo
= policyLowestOffsetToKeep(false)) {
411 assert (new_mem_lo
> 0);
412 data_hdr
.freeDataUpto(new_mem_lo
);
413 inmem_lo
= new_mem_lo
;
414 } // else we should not trim anything at this time
418 MemObject::isContiguous() const
420 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
421 /* XXX : make this higher level */
422 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
427 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
430 if (!ignoreDelayPools
) {
431 /* identify delay id with largest allowance */
432 DelayId largestAllowance
= mostBytesAllowed ();
433 return largestAllowance
.bytesWanted(0, max
);
441 MemObject::setNoDelay(bool const newValue
)
445 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
446 store_client
*sc
= (store_client
*) node
->data
;
447 sc
->delayId
.setNoDelay(newValue
);
454 MemObject::delayRead(DeferredRead
const &aRead
)
456 deferredReads
.delayRead(aRead
);
460 MemObject::kickReads()
462 deferredReads
.kickReads(-1);
467 MemObject::mostBytesAllowed() const
473 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
474 store_client
*sc
= (store_client
*) node
->data
;
476 /* This test is invalid because the client may be writing data
477 * and thus will want data immediately.
478 * If we include the test, there is a race condition when too much
479 * data is read - if all sc's are writing when a read is scheduled.
483 if (!sc
->callbackPending())
484 /* not waiting for more data */
489 j
= sc
->delayId
.bytesWanted(0, sc
->copyInto
.length
);
493 result
= sc
->delayId
;
503 MemObject::availableForSwapOut() const
505 return endOffset() - swapout
.queue_offset
;