3 * $Id: MemObject.cc,v 1.29 2007/08/13 17:20:51 hno Exp $
5 * DEBUG: section 19 Store Memory Primitives
6 * AUTHOR: Robert Collins
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "MemObject.h"
38 #include "HttpRequest.h"
39 #include "HttpReply.h"
41 #include "StoreClient.h"
44 #include "DelayPools.h"
48 /* TODO: make this global or private */
49 #if URL_CHECKSUM_DEBUG
50 static unsigned int url_checksum(const char *url
);
52 url_checksum(const char *url
)
56 static unsigned char digest
[16];
58 MD5Update(&M
, (unsigned char *) url
, strlen(url
));
60 xmemcpy(&ck
, digest
, sizeof(ck
));
66 RemovalPolicy
* mem_policy
= NULL
;
69 MemObject::inUseCount()
71 return Pool().inUseCount();
74 MemObject::MemObject(char const *aUrl
, char const *aLog_url
)
76 debugs(20, 3, HERE
<< "new MemObject " << this);
77 HttpReply
*rep
= new HttpReply
;
79 _reply
= HTTPMSGLOCK(rep
);
82 #if URL_CHECKSUM_DEBUG
84 chksum
= url_checksum(url
);
88 log_url
= xstrdup(aLog_url
);
92 /* XXX account log_url */
95 MemObject::~MemObject()
97 debugs(20, 3, HERE
<< "del MemObject " << this);
98 const Ctx ctx
= ctx_enter(url
);
99 #if URL_CHECKSUM_DEBUG
101 assert(chksum
== url_checksum(url
));
105 assert(swapout
.sio
== NULL
);
107 data_hdr
.freeContent();
111 * There is no way to abort FD-less clients, so they might
112 * still have mem->clients set.
114 assert(clients
.head
== NULL
);
118 HTTPMSGUNLOCK(_reply
);
120 HTTPMSGUNLOCK(request
);
122 ctx_exit(ctx
); /* must exit before we free mem->url */
126 safe_free(log_url
); /* XXX account log_url */
128 safe_free(vary_headers
);
132 MemObject::unlinkRequest()
134 HTTPMSGUNLOCK(request
);
138 MemObject::write ( StoreIOBuffer writeBuffer
, STMCB
*callback
, void *callbackData
)
140 PROF_start(MemObject_write
);
141 debugs(19, 6, "memWrite: offset " << writeBuffer
.offset
<< " len " << writeBuffer
.length
);
143 /* the offset is into the content, not the headers */
144 writeBuffer
.offset
+= (_reply
? _reply
->hdr_sz
: 0);
146 /* We don't separate out mime headers yet, so ensure that the first
147 * write is at offset 0 - where they start
149 assert (data_hdr
.endOffset() || writeBuffer
.offset
== 0);
151 assert (data_hdr
.write (writeBuffer
));
152 callback (callbackData
, writeBuffer
);
153 PROF_stop(MemObject_write
);
157 MemObject::dump() const
161 /* do we want this one? */
162 debugs(20, 1, "MemObject->data.origin_offset: " << (data_hdr
.head
? data_hdr
.head
->nodeBuffer
.offset
: 0));
165 debugs(20, 1, "MemObject->start_ping: " << start_ping
.tv_sec
<< "."<< std::setfill('0') << std::setw(6) << start_ping
.tv_usec
);
166 debugs(20, 1, "MemObject->inmem_hi: " << data_hdr
.endOffset());
167 debugs(20, 1, "MemObject->inmem_lo: " << inmem_lo
);
168 debugs(20, 1, "MemObject->nclients: " << nclients
);
169 debugs(20, 1, "MemObject->reply: " << _reply
);
170 debugs(20, 1, "MemObject->request: " << request
);
171 debugs(20, 1, "MemObject->log_url: " << log_url
<< " " << checkNullString(log_url
));
175 MemObject::getReply() const
181 MemObject::replaceHttpReply(HttpReply
*newrep
)
183 HTTPMSGUNLOCK(_reply
);
184 _reply
= HTTPMSGLOCK(newrep
);
187 struct LowestMemReader
: public unary_function
<store_client
, void>
189 LowestMemReader(int64_t seed
):current(seed
){}
191 void operator() (store_client
const &x
)
193 if (x
.memReaderHasLowerOffset(current
))
194 current
= x
.copyInto
.offset
;
200 struct StoreClientStats
: public unary_function
<store_client
, void>
202 StoreClientStats(MemBuf
*anEntry
):where(anEntry
),index(0){}
204 void operator()(store_client
const &x
)
206 x
.dumpStats(where
, index
++);
214 MemObject::stat (MemBuf
* mb
) const
216 mb
->Printf("\t%s %s\n",
217 RequestMethodStr
[method
], log_url
);
218 mb
->Printf("\tinmem_lo: %"PRId64
"\n", inmem_lo
);
219 mb
->Printf("\tinmem_hi: %"PRId64
"\n", data_hdr
.endOffset());
220 mb
->Printf("\tswapout: %"PRId64
" bytes queued\n",
221 swapout
.queue_offset
);
223 if (swapout
.sio
.getRaw())
224 mb
->Printf("\tswapout: %"PRId64
" bytes written\n",
225 (int64_t) swapout
.sio
->offset());
227 StoreClientStats
statsVisitor(mb
);
229 for_each
<StoreClientStats
>(clients
, statsVisitor
);
233 MemObject::endOffset () const
235 return data_hdr
.endOffset();
239 MemObject::size() const
250 assert(swapout
.sio
== NULL
);
251 data_hdr
.freeContent();
253 /* Should we check for clients? */
258 MemObject::lowestMemReaderOffset() const
260 LowestMemReader
lowest (endOffset() + 1);
262 for_each
<LowestMemReader
>(clients
, lowest
);
264 return lowest
.current
;
267 /* XXX: This is wrong. It breaks *badly* on range combining */
269 MemObject::readAheadPolicyCanRead() const
271 return endOffset() - getReply()->hdr_sz
< lowestMemReaderOffset() + Config
.readAheadGap
;
275 MemObject::addClient(store_client
*aClient
)
278 dlinkAdd(aClient
, &aClient
->node
, &clients
);
281 #if URL_CHECKSUM_DEBUG
283 MemObject::checkUrlChecksum () const
285 assert(chksum
== url_checksum(url
));
291 * How much of the object data is on the disk?
294 MemObject::objectBytesOnDisk() const
297 * NOTE: storeOffset() represents the disk file size,
298 * not the amount of object data on disk.
300 * If we don't have at least 'swap_hdr_sz' bytes
301 * then none of the object data is on disk.
303 * This should still be safe if swap_hdr_sz == 0,
304 * meaning we haven't even opened the swapout file
308 if (swapout
.sio
.getRaw() == NULL
)
311 int64_t nwritten
= swapout
.sio
->offset();
313 if (nwritten
<= swap_hdr_sz
)
316 return (nwritten
- swap_hdr_sz
);
320 MemObject::policyLowestOffsetToKeep() const
323 * Careful. lowest_offset can be greater than endOffset(), such
324 * as in the case of a range request.
326 int64_t lowest_offset
= lowestMemReaderOffset();
328 if (endOffset() < lowest_offset
||
329 endOffset() - inmem_lo
> Config
.Store
.maxInMemObjSize
)
330 return lowest_offset
;
336 MemObject::trimSwappable()
338 int64_t new_mem_lo
= policyLowestOffsetToKeep();
340 * We should only free up to what we know has been written
341 * to disk, not what has been queued for writing. Otherwise
342 * there will be a chunk of the data which is not in memory
343 * and is not yet on disk.
344 * The -1 makes sure the page isn't freed until storeSwapOut has
345 * walked to the next page. (mem->swapout.memnode)
349 if ((on_disk
= objectBytesOnDisk()) - 1 < new_mem_lo
)
350 new_mem_lo
= on_disk
- 1;
352 if (new_mem_lo
== -1)
353 new_mem_lo
= 0; /* the above might become -1 */
355 data_hdr
.freeDataUpto(new_mem_lo
);
357 inmem_lo
= new_mem_lo
;
361 MemObject::trimUnSwappable()
363 int64_t new_mem_lo
= policyLowestOffsetToKeep();
364 assert (new_mem_lo
> 0);
366 data_hdr
.freeDataUpto(new_mem_lo
);
367 inmem_lo
= new_mem_lo
;
372 MemObject::isContiguous() const
374 bool result
= data_hdr
.hasContigousContentRange (Range
<int64_t>(inmem_lo
, endOffset()));
375 /* XXX : make this higher level */
376 debugs (19, result
? 4 :3, "MemObject::isContiguous: Returning " << (result
? "true" : "false"));
381 MemObject::mostBytesWanted(int max
) const
384 /* identify delay id with largest allowance */
385 DelayId largestAllowance
= mostBytesAllowed ();
386 return largestAllowance
.bytesWanted(0, max
);
394 MemObject::setNoDelay(bool const newValue
)
398 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
399 store_client
*sc
= (store_client
*) node
->data
;
400 sc
->delayId
.setNoDelay(newValue
);
407 MemObject::delayRead(DeferredRead
const &aRead
)
409 deferredReads
.delayRead(aRead
);
413 MemObject::kickReads()
415 deferredReads
.kickReads(-1);
420 MemObject::mostBytesAllowed() const
426 for (dlink_node
*node
= clients
.head
; node
; node
= node
->next
) {
427 store_client
*sc
= (store_client
*) node
->data
;
429 /* This test is invalid because the client may be writing data
430 * and thus will want data immediately.
431 * If we include the test, there is a race condition when too much
432 * data is read - if all sc's are writing when a read is scheduled.
436 if (!sc
->callbackPending())
437 /* not waiting for more data */
442 if (sc
->getType() != STORE_MEM_CLIENT
)
443 /* reading off disk */
446 j
= sc
->delayId
.bytesWanted(0, sc
->copyInto
.length
);
450 result
= sc
->delayId
;