3 * DEBUG: section 19 Store Memory Primitives
4 * AUTHOR: Robert Collins
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "comm/Connection.h"
38 #include "HttpReply.h"
39 #include "HttpRequest.h"
41 #include "MemObject.h"
42 #include "profiler/Profiler.h"
43 #include "SquidConfig.h"
45 #include "StoreClient.h"
48 #include "DelayPools.h"
51 /* TODO: make this global or private */
52 #if URL_CHECKSUM_DEBUG
53 static unsigned int url_checksum(const char *url
);
55 url_checksum(const char *url
)
59 static unsigned char digest
[16];
61 SquidMD5Update(&M
, (unsigned char *) url
, strlen(url
));
62 SquidMD5Final(digest
, &M
);
63 memcpy(&ck
, digest
, sizeof(ck
));
69 RemovalPolicy
* mem_policy
= NULL
;
72 MemObject::inUseCount()
74 return Pool().inUseCount();
78 MemObject::resetUrls(char const *aUrl
, char const *aLog_url
)
81 safe_free(log_url
); /* XXX account log_url */
82 log_url
= xstrdup(aLog_url
);
86 MemObject::MemObject(char const *aUrl
, char const *aLog_url
)
88 debugs(20, 3, HERE
<< "new MemObject " << this);
89 HttpReply
*rep
= new HttpReply
;
91 _reply
= HTTPMSGLOCK(rep
);
94 #if URL_CHECKSUM_DEBUG
96 chksum
= url_checksum(url
);
100 log_url
= xstrdup(aLog_url
);
104 /* XXX account log_url */
106 swapout
.decision
= SwapOut::swNeedsCheck
;
109 MemObject::~MemObject()
111 debugs(20, 3, HERE
<< "del MemObject " << this);
112 const Ctx ctx
= ctx_enter(url
);
113 #if URL_CHECKSUM_DEBUG
115 assert(chksum
== url_checksum(url
));
119 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 HTTPMSGUNLOCK(_reply
);
134 HTTPMSGUNLOCK(request
);
136 ctx_exit(ctx
); /* must exit before we free mem->url */
140 safe_free(log_url
); /* XXX account log_url */
142 safe_free(vary_headers
);
146 MemObject::unlinkRequest()
148 HTTPMSGUNLOCK(request
);
152 MemObject::write ( StoreIOBuffer writeBuffer
, STMCB
*callback
, void *callbackData
)
154 PROF_start(MemObject_write
);
155 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
157 /* the offset is into the content, not the headers */
158 writeBuffer
.offset
+= (_reply
? _reply
->hdr_sz
: 0);
160 /* We don't separate out mime headers yet, so ensure that the first
161 * write is at offset 0 - where they start
163 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
165 assert (data_hdr
.write (writeBuffer
));
166 callback (callbackData
, writeBuffer
);
167 PROF_stop(MemObject_write
);
171 MemObject::dump() const
175 /* do we want this one? */
176 debugs(20, DBG_IMPORTANT
, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
179 debugs(20, DBG_IMPORTANT
, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
180 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_hi: " << data_hdr
.endOffset());
181 debugs(20, DBG_IMPORTANT
, "MemObject->inmem_lo: " << inmem_lo
);
182 debugs(20, DBG_IMPORTANT
, "MemObject->nclients: " << nclients
);
183 debugs(20, DBG_IMPORTANT
, "MemObject->reply: " << _reply
);
184 debugs(20, DBG_IMPORTANT
, "MemObject->request: " << request
);
185 debugs(20, DBG_IMPORTANT
, "MemObject->log_url: " << log_url
<< " " << checkNullString(log_url
));
189 MemObject::getReply() const
195 MemObject::replaceHttpReply(HttpReply
*newrep
)
197 HTTPMSGUNLOCK(_reply
);
198 _reply
= HTTPMSGLOCK(newrep
);
201 struct LowestMemReader
: public unary_function
<store_client
, void> {
202 LowestMemReader(int64_t seed
):current(seed
) {}
204 void operator() (store_client
const &x
) {
205 if (x
.memReaderHasLowerOffset(current
))
206 current
= x
.copyInto
.offset
;
212 struct StoreClientStats
: public unary_function
<store_client
, void> {
213 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0) {}
215 void operator()(store_client
const &x
) {
216 x
.dumpStats(where
, index
);
225 MemObject::stat(MemBuf
* mb
) const
227 mb
->Printf("\t%s %s\n",
228 RequestMethodStr(method
), log_url
);
230 mb
->Printf("\tvary_headers: %s\n", vary_headers
);
231 mb
->Printf("\tinmem_lo: %" PRId64
"\n", inmem_lo
);
232 mb
->Printf("\tinmem_hi: %" PRId64
"\n", data_hdr
.endOffset());
233 mb
->Printf("\tswapout: %" PRId64
" bytes queued\n",
234 swapout
.queue_offset
);
236 if (swapout
.sio
.getRaw())
237 mb
->Printf("\tswapout: %" PRId64
" bytes written\n",
238 (int64_t) swapout
.sio
->offset());
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: HttpMsg 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 return endOffset() - getReply()->hdr_sz
< lowestMemReaderOffset() + Config
.readAheadGap
;
313 MemObject::addClient(store_client
*aClient
)
316 dlinkAdd(aClient
, &aClient
->node
, &clients
);
319 #if URL_CHECKSUM_DEBUG
321 MemObject::checkUrlChecksum () const
323 assert(chksum
== url_checksum(url
));
329 * How much of the object data is on the disk?
332 MemObject::objectBytesOnDisk() const
335 * NOTE: storeOffset() represents the disk file size,
336 * not the amount of object data on disk.
338 * If we don't have at least 'swap_hdr_sz' bytes
339 * then none of the object data is on disk.
341 * This should still be safe if swap_hdr_sz == 0,
342 * meaning we haven't even opened the swapout file
346 if (swapout
.sio
.getRaw() == NULL
)
349 int64_t nwritten
= swapout
.sio
->offset();
351 if (nwritten
<= (int64_t)swap_hdr_sz
)
354 return (nwritten
- swap_hdr_sz
);
358 MemObject::policyLowestOffsetToKeep(bool swap
) const
361 * Careful. lowest_offset can be greater than endOffset(), such
362 * as in the case of a range request.
364 int64_t lowest_offset
= lowestMemReaderOffset();
366 if (endOffset() < lowest_offset
||
367 endOffset() - inmem_lo
> (int64_t)Config
.Store
.maxInMemObjSize
||
368 (swap
&& !Config
.onoff
.memory_cache_first
))
369 return lowest_offset
;
375 MemObject::trimSwappable()
377 int64_t new_mem_lo
= policyLowestOffsetToKeep(1);
379 * We should only free up to what we know has been written
380 * to disk, not what has been queued for writing. Otherwise
381 * there will be a chunk of the data which is not in memory
382 * and is not yet on disk.
383 * The -1 makes sure the page isn't freed until storeSwapOut has
384 * walked to the next page.
388 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
389 new_mem_lo
= on_disk
- 1;
391 if (new_mem_lo
== -1)
392 new_mem_lo
= 0; /* the above might become -1 */
394 data_hdr
.freeDataUpto(new_mem_lo
);
396 inmem_lo
= new_mem_lo
;
400 MemObject::trimUnSwappable()
402 int64_t new_mem_lo
= policyLowestOffsetToKeep(0);
403 assert (new_mem_lo
> 0);
405 data_hdr
.freeDataUpto(new_mem_lo
);
406 inmem_lo
= new_mem_lo
;
410 MemObject::isContiguous() const
412 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
413 /* XXX : make this higher level */
414 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
419 MemObject::mostBytesWanted(int max
, bool ignoreDelayPools
) const
422 if (!ignoreDelayPools
) {
423 /* identify delay id with largest allowance */
424 DelayId largestAllowance
= mostBytesAllowed ();
425 return largestAllowance
.bytesWanted(0, max
);
433 MemObject::setNoDelay(bool const newValue
)
437 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
438 store_client
*sc
= (store_client
*) node
->data
;
439 sc
->delayId
.setNoDelay(newValue
);
446 MemObject::delayRead(DeferredRead
const &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
;