5 * DEBUG: section 20 Storage Manager Swapout Functions
6 * AUTHOR: Duane Wessels
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.
38 #include "StoreClient.h"
40 /* FIXME: Abstract the use of this more */
42 #include "MemObject.h"
44 #include "swap_log_op.h"
46 static void storeSwapOutStart(StoreEntry
* e
);
47 static StoreIOState::STIOCB storeSwapOutFileClosed
;
48 static StoreIOState::STFNCB storeSwapOutFileNotify
;
50 // wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
51 static void xfree_cppwrapper(void *x
)
56 /* start swapping object to disk */
58 storeSwapOutStart(StoreEntry
* e
)
60 MemObject
*mem
= e
->mem_obj
;
61 StoreIOState::Pointer sio
;
63 /* Build the swap metadata, so the filesystem will know how much
64 * metadata there is to store
66 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e
->url() << "' to dirno " <<
67 e
->swap_dirn
<< ", fileno " << std::hex
<< std::setw(8) << std::setfill('0') <<
68 std::uppercase
<< e
->swap_filen
);
69 e
->swap_status
= SWAPOUT_WRITING
;
70 /* If we start swapping out objects with OutOfBand Metadata,
71 * then this code needs changing
74 /* TODO: make some sort of data,size refcounted immutable buffer
75 * and stop fooling ourselves with "const char*" buffers.
78 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
79 const char *buf
= e
->getSerialisedMetaData ();
82 /* Create the swap file */
83 generic_cbdata
*c
= new generic_cbdata(e
);
84 sio
= storeCreate(e
, storeSwapOutFileNotify
, storeSwapOutFileClosed
, c
);
87 e
->swap_status
= SWAPOUT_NONE
;
88 mem
->swapout
.decision
= MemObject::SwapOut::swImpossible
;
91 storeLog(STORE_LOG_SWAPOUTFAIL
, e
);
95 mem
->swapout
.sio
= sio
;
96 /* Don't lock until after create, or the replacement
97 * code might get confused */
100 /* Pick up the file number if it was assigned immediately */
101 e
->swap_filen
= mem
->swapout
.sio
->swap_filen
;
103 e
->swap_dirn
= mem
->swapout
.sio
->swap_dirn
;
105 /* write out the swap metadata */
106 storeIOWrite(mem
->swapout
.sio
, buf
, mem
->swap_hdr_sz
, 0, xfree_cppwrapper
);
110 storeSwapOutFileNotify(void *data
, int errflag
, StoreIOState::Pointer self
)
112 generic_cbdata
*c
= (generic_cbdata
*)data
;
113 StoreEntry
*e
= (StoreEntry
*)c
->data
;
114 MemObject
*mem
= e
->mem_obj
;
115 assert(e
->swap_status
== SWAPOUT_WRITING
);
117 assert(mem
->swapout
.sio
== self
);
118 assert(errflag
== 0);
119 assert(e
->swap_filen
< 0); // if this fails, call SwapDir::disconnect(e)
120 e
->swap_filen
= mem
->swapout
.sio
->swap_filen
;
121 e
->swap_dirn
= mem
->swapout
.sio
->swap_dirn
;
125 doPages(StoreEntry
*anEntry
)
127 MemObject
*mem
= anEntry
->mem_obj
;
130 // find the page containing the first byte we have not swapped out yet
132 mem
->data_hdr
.getBlockContainingLocation(mem
->swapout
.queue_offset
);
135 return; // wait for more data to become available
137 // memNodeWriteComplete() and absence of buffer offset math below
138 // imply that we always write from the very beginning of the page
139 assert(page
->start() == mem
->swapout
.queue_offset
);
142 * Get the length of this buffer. We are assuming(!) that the buffer
143 * length won't change on this buffer, or things are going to be very
144 * strange. I think that after the copy to a buffer is done, the buffer
145 * size should stay fixed regardless so that this code isn't confused,
146 * but we can look at this at a later date or whenever the code results
147 * in bad swapouts, whichever happens first. :-)
149 ssize_t swap_buf_len
= page
->nodeBuffer
.length
;
151 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len
);
153 assert(swap_buf_len
> 0);
155 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len
<< " bytes from " << mem
->swapout
.queue_offset
);
157 mem
->swapout
.queue_offset
+= swap_buf_len
;
159 storeIOWrite(mem
->swapout
.sio
,
160 mem
->data_hdr
.NodeGet(page
),
163 memNodeWriteComplete
);
165 /* the storeWrite() call might generate an error */
166 if (anEntry
->swap_status
!= SWAPOUT_WRITING
)
169 int64_t swapout_size
= mem
->endOffset() - mem
->swapout
.queue_offset
;
171 if (anEntry
->store_status
== STORE_PENDING
)
172 if (swapout_size
< SM_PAGE_SIZE
)
175 if (swapout_size
<= 0)
181 /* This routine is called every time data is sent to the client side.
182 * It's overhead is therefor, significant.
185 StoreEntry::swapOut()
190 if (!swapoutPossible())
193 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
194 // store_status == STORE_OK below means we got everything we wanted.
196 debugs(20, 7, HERE
<< "storeSwapOut: mem->inmem_lo = " << mem_obj
->inmem_lo
);
197 debugs(20, 7, HERE
<< "storeSwapOut: mem->endOffset() = " << mem_obj
->endOffset());
198 debugs(20, 7, HERE
<< "storeSwapOut: swapout.queue_offset = " << mem_obj
->swapout
.queue_offset
);
200 if (mem_obj
->swapout
.sio
!= NULL
)
201 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj
->swapout
.sio
->offset() );
203 // buffered bytes we have not swapped out yet
204 int64_t swapout_maxsize
= mem_obj
->endOffset() - mem_obj
->swapout
.queue_offset
;
206 assert(swapout_maxsize
>= 0);
208 int64_t const lowest_offset
= mem_obj
->lowestMemReaderOffset();
210 debugs(20, 7, HERE
<< "storeSwapOut: lowest_offset = " << lowest_offset
);
212 // Check to see whether we're going to defer the swapout based upon size
213 if (store_status
!= STORE_OK
) {
214 const int64_t expectedSize
= mem_obj
->expectedReplySize();
215 const int64_t maxKnownSize
= expectedSize
< 0 ?
216 swapout_maxsize
: expectedSize
;
217 debugs(20, 7, HERE
<< "storeSwapOut: maxKnownSize= " << maxKnownSize
);
219 if (maxKnownSize
< store_maxobjsize
) {
221 * NOTE: the store_maxobjsize here is the max of optional
222 * max-size values from 'cache_dir' lines. It is not the
223 * same as 'maximum_object_size'. By default, store_maxobjsize
224 * will be set to -1. However, I am worried that this
225 * deferance may consume a lot of memory in some cases.
226 * Should we add an option to limit this memory consumption?
228 debugs(20, 5, "storeSwapOut: Deferring swapout start for " <<
229 (store_maxobjsize
- maxKnownSize
) << " bytes");
234 // TODO: it is better to trim as soon as we swap something out, not before
236 #if SIZEOF_OFF_T <= 4
238 if (mem_obj
->endOffset() > 0x7FFF0000) {
239 debugs(20, 0, "WARNING: preventing off_t overflow for " << url());
245 if (swap_status
== SWAPOUT_WRITING
)
246 assert(mem_obj
->inmem_lo
<= mem_obj
->objectBytesOnDisk() );
251 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize
);
253 if (swapout_maxsize
== 0) { // swapped everything we got
254 if (store_status
== STORE_OK
) { // got everything we wanted
255 assert(mem_obj
->object_sz
>= 0);
256 swapOutFileClose(StoreIOState::wroteAll
);
258 // else need more data to swap out
262 if (store_status
== STORE_PENDING
) {
263 /* wait for a full block to write */
265 if (swapout_maxsize
< SM_PAGE_SIZE
)
269 * Wait until we are below the disk FD limit, only if the
270 * next server-side read won't be deferred.
272 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
276 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
277 if (swap_status
== SWAPOUT_NONE
) {
278 assert(mem_obj
->swapout
.sio
== NULL
);
279 assert(mem_obj
->inmem_lo
== 0);
280 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
283 if (mem_obj
->swapout
.sio
== NULL
)
288 if (mem_obj
->swapout
.sio
== NULL
)
289 /* oops, we're not swapping out any more */
292 if (store_status
== STORE_OK
) {
294 * If the state is STORE_OK, then all data must have been given
295 * to the filesystem at this point because storeSwapOut() is
296 * not going to be called again for this entry.
298 assert(mem_obj
->object_sz
>= 0);
299 assert(mem_obj
->endOffset() == mem_obj
->swapout
.queue_offset
);
300 swapOutFileClose(StoreIOState::wroteAll
);
305 StoreEntry::swapOutFileClose(int how
)
307 assert(mem_obj
!= NULL
);
308 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how
);
309 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj
->swapout
.sio
.getRaw());
311 if (mem_obj
->swapout
.sio
== NULL
)
314 storeClose(mem_obj
->swapout
.sio
, how
);
318 storeSwapOutFileClosed(void *data
, int errflag
, StoreIOState::Pointer self
)
320 generic_cbdata
*c
= (generic_cbdata
*)data
;
321 StoreEntry
*e
= (StoreEntry
*)c
->data
;
322 MemObject
*mem
= e
->mem_obj
;
323 assert(mem
->swapout
.sio
== self
);
324 assert(e
->swap_status
== SWAPOUT_WRITING
);
327 // if object_size is still unknown, the entry was probably aborted
328 if (errflag
|| e
->objectLen() < 0) {
329 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e
->swap_dirn
<< ", swapfile " <<
330 std::hex
<< std::setw(8) << std::setfill('0') << std::uppercase
<<
331 e
->swap_filen
<< ", errflag=" << errflag
);
333 if (errflag
== DISK_NO_SPACE_LEFT
) {
334 /* FIXME: this should be handle by the link from store IO to
335 * Store, rather than being a top level API call.
337 e
->store()->diskFull();
341 if (e
->swap_filen
>= 0)
344 assert(e
->swap_status
== SWAPOUT_NONE
);
348 /* swapping complete */
349 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e
->url() << "' to " <<
350 e
->swap_dirn
<< ", " << std::hex
<< std::setw(8) << std::setfill('0') <<
351 std::uppercase
<< e
->swap_filen
);
352 debugs(20, 5, HERE
<< "swap_file_sz = " <<
353 e
->objectLen() << " + " << mem
->swap_hdr_sz
);
355 e
->swap_file_sz
= e
->objectLen() + mem
->swap_hdr_sz
;
356 e
->swap_status
= SWAPOUT_DONE
;
357 e
->store()->swappedOut(*e
);
359 // XXX: For some Stores, it is pointless to re-check cachability here
360 // and it leads to double counts in store_check_cachable_hist. We need
361 // another way to signal a completed but failed swapout. Or, better,
362 // each Store should handle its own logging and LOG state setting.
363 if (e
->checkCachable()) {
364 storeLog(STORE_LOG_SWAPOUT
, e
);
365 storeDirSwapLog(e
, SWAP_LOG_ADD
);
368 statCounter
.swap
.outs
++;
371 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__
<< ":" << __LINE__
);
372 mem
->swapout
.sio
= NULL
;
377 * Is this entry a candidate for writing to disk?
380 StoreEntry::swapOutAble() const
384 if (mem_obj
->swapout
.sio
!= NULL
)
387 if (mem_obj
->inmem_lo
> 0)
391 * If there are DISK clients, we must write to disk
392 * even if its not cachable
393 * RBC: Surely we should not create disk client on non cacheable objects?
394 * therefore this should be an assert?
395 * RBC 20030708: We can use disk to avoid mem races, so this shouldn't be
398 for (node
= mem_obj
->clients
.head
; node
; node
= node
->next
) {
399 if (((store_client
*) node
->data
)->getType() == STORE_DISK_CLIENT
)
403 /* Don't pollute the disk with icons and other special entries */
404 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
407 if (!EBIT_TEST(flags
, ENTRY_CACHABLE
))
410 if (!mem_obj
->isContiguous())