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 /* start swapping object to disk */
52 storeSwapOutStart(StoreEntry
* e
)
54 MemObject
*mem
= e
->mem_obj
;
55 StoreIOState::Pointer sio
;
57 /* Build the swap metadata, so the filesystem will know how much
58 * metadata there is to store
60 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e
->url() << "' to dirno " <<
61 e
->swap_dirn
<< ", fileno " << std::hex
<< std::setw(8) << std::setfill('0') <<
62 std::uppercase
<< e
->swap_filen
);
63 e
->swap_status
= SWAPOUT_WRITING
;
64 /* If we start swapping out objects with OutOfBand Metadata,
65 * then this code needs changing
68 /* TODO: make some sort of data,size refcounted immutable buffer
69 * and stop fooling ourselves with "const char*" buffers.
72 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
73 const char *buf
= e
->getSerialisedMetaData ();
76 /* Create the swap file */
77 generic_cbdata
*c
= new generic_cbdata(e
);
78 sio
= storeCreate(e
, storeSwapOutFileNotify
, storeSwapOutFileClosed
, c
);
81 e
->swap_status
= SWAPOUT_NONE
;
84 storeLog(STORE_LOG_SWAPOUTFAIL
, e
);
88 mem
->swapout
.sio
= sio
;
89 /* Don't lock until after create, or the replacement
90 * code might get confused */
93 /* Pick up the file number if it was assigned immediately */
94 e
->swap_filen
= mem
->swapout
.sio
->swap_filen
;
96 e
->swap_dirn
= mem
->swapout
.sio
->swap_dirn
;
98 /* write out the swap metadata */
99 storeIOWrite(mem
->swapout
.sio
, buf
, mem
->swap_hdr_sz
, 0, xfree
);
103 storeSwapOutFileNotify(void *data
, int errflag
, StoreIOState::Pointer self
)
105 generic_cbdata
*c
= (generic_cbdata
*)data
;
106 StoreEntry
*e
= (StoreEntry
*)c
->data
;
107 MemObject
*mem
= e
->mem_obj
;
108 assert(e
->swap_status
== SWAPOUT_WRITING
);
110 assert(mem
->swapout
.sio
== self
);
111 assert(errflag
== 0);
112 e
->swap_filen
= mem
->swapout
.sio
->swap_filen
;
113 e
->swap_dirn
= mem
->swapout
.sio
->swap_dirn
;
117 doPages(StoreEntry
*anEntry
)
119 MemObject
*mem
= anEntry
->mem_obj
;
124 * We are paging out to disk in page size chunks. however, later on when
125 * we update the queue position, we might not have a page (I *think*),
126 * so we do the actual page update here.
129 if (mem
->swapout
.memnode
== NULL
) {
130 /* We need to swap out the first page */
131 mem
->swapout
.memnode
= const_cast<mem_node
*>(mem
->data_hdr
.start());
133 /* We need to swap out the next page */
134 /* 20030636 RBC - we don't have ->next anymore.
135 * But we do have the next location */
136 mem
->swapout
.memnode
= mem
->data_hdr
.getBlockContainingLocation (mem
->swapout
.memnode
->end());
140 * Get the length of this buffer. We are assuming(!) that the buffer
141 * length won't change on this buffer, or things are going to be very
142 * strange. I think that after the copy to a buffer is done, the buffer
143 * size should stay fixed regardless so that this code isn't confused,
144 * but we can look at this at a later date or whenever the code results
145 * in bad swapouts, whichever happens first. :-)
147 ssize_t swap_buf_len
= mem
->swapout
.memnode
->nodeBuffer
.length
;
149 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len
);
151 assert(swap_buf_len
> 0);
153 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len
<< " bytes from " << mem
->swapout
.queue_offset
);
155 mem
->swapout
.queue_offset
+= swap_buf_len
;
157 storeIOWrite(mem
->swapout
.sio
,
158 mem
->data_hdr
.NodeGet(mem
->swapout
.memnode
),
161 memNodeWriteComplete
);
163 /* the storeWrite() call might generate an error */
164 if (anEntry
->swap_status
!= SWAPOUT_WRITING
)
167 int64_t swapout_size
= mem
->endOffset() - mem
->swapout
.queue_offset
;
169 if (anEntry
->store_status
== STORE_PENDING
)
170 if (swapout_size
< SM_PAGE_SIZE
)
173 if (swapout_size
<= 0)
179 /* This routine is called every time data is sent to the client side.
180 * It's overhead is therefor, significant.
183 StoreEntry::swapOut()
188 if (!swapoutPossible())
191 debugs(20, 7, HERE
<< "storeSwapOut: mem->inmem_lo = " << mem_obj
->inmem_lo
);
192 debugs(20, 7, HERE
<< "storeSwapOut: mem->endOffset() = " << mem_obj
->endOffset());
193 debugs(20, 7, HERE
<< "storeSwapOut: swapout.queue_offset = " << mem_obj
->swapout
.queue_offset
);
195 if (mem_obj
->swapout
.sio
!= NULL
)
196 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj
->swapout
.sio
->offset() );
198 int64_t swapout_maxsize
= mem_obj
->endOffset() - mem_obj
->swapout
.queue_offset
;
200 assert(swapout_maxsize
>= 0);
202 int64_t const lowest_offset
= mem_obj
->lowestMemReaderOffset();
204 debugs(20, 7, HERE
<< "storeSwapOut: lowest_offset = " << lowest_offset
);
207 * Grab the swapout_size and check to see whether we're going to defer
208 * the swapout based upon size
210 if ((store_status
!= STORE_OK
) && (swapout_maxsize
< store_maxobjsize
)) {
212 * NOTE: the store_maxobjsize here is the max of optional
213 * max-size values from 'cache_dir' lines. It is not the
214 * same as 'maximum_object_size'. By default, store_maxobjsize
215 * will be set to -1. However, I am worried that this
216 * deferance may consume a lot of memory in some cases.
217 * It would be good to make this decision based on reply
218 * content-length, rather than wait to accumulate huge
219 * amounts of object data in memory.
221 debugs(20, 5, "storeSwapOut: Deferring starting swapping out");
226 #if SIZEOF_OFF_T <= 4
228 if (mem_obj
->endOffset() > 0x7FFF0000) {
229 debugs(20, 0, "WARNING: preventing off_t overflow for " << url());
235 if (swap_status
== SWAPOUT_WRITING
)
236 assert(mem_obj
->inmem_lo
<= mem_obj
->objectBytesOnDisk() );
241 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize
);
243 if (swapout_maxsize
== 0) {
244 if (store_status
== STORE_OK
)
247 return; /* Nevermore! */
250 if (store_status
== STORE_PENDING
) {
251 /* wait for a full block to write */
253 if (swapout_maxsize
< SM_PAGE_SIZE
)
257 * Wait until we are below the disk FD limit, only if the
258 * next server-side read won't be deferred.
260 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
264 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
265 if (swap_status
== SWAPOUT_NONE
) {
266 assert(mem_obj
->swapout
.sio
== NULL
);
267 assert(mem_obj
->inmem_lo
== 0);
270 storeSwapOutStart(this);
274 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
277 if (mem_obj
->swapout
.sio
== NULL
)
282 if (mem_obj
->swapout
.sio
== NULL
)
283 /* oops, we're not swapping out any more */
286 if (store_status
== STORE_OK
) {
288 * If the state is STORE_OK, then all data must have been given
289 * to the filesystem at this point because storeSwapOut() is
290 * not going to be called again for this entry.
292 assert(mem_obj
->endOffset() == mem_obj
->swapout
.queue_offset
);
298 StoreEntry::swapOutFileClose()
300 assert(mem_obj
!= NULL
);
301 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text());
302 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj
->swapout
.sio
.getRaw());
304 if (mem_obj
->swapout
.sio
== NULL
)
307 storeClose(mem_obj
->swapout
.sio
);
311 storeSwapOutFileClosed(void *data
, int errflag
, StoreIOState::Pointer self
)
313 generic_cbdata
*c
= (generic_cbdata
*)data
;
314 StoreEntry
*e
= (StoreEntry
*)c
->data
;
315 MemObject
*mem
= e
->mem_obj
;
316 assert(mem
->swapout
.sio
== self
);
317 assert(e
->swap_status
== SWAPOUT_WRITING
);
321 debugs(20, 1, "storeSwapOutFileClosed: dirno " << e
->swap_dirn
<< ", swapfile " <<
322 std::hex
<< std::setw(8) << std::setfill('0') << std::uppercase
<<
323 e
->swap_filen
<< ", errflag=" << errflag
);
324 debugs(20, 1, "\t" << xstrerror());
326 if (errflag
== DISK_NO_SPACE_LEFT
) {
327 /* FIXME: this should be handle by the link from store IO to
328 * Store, rather than being a top level API call.
330 e
->store()->diskFull();
334 if (e
->swap_filen
> 0)
341 e
->swap_status
= SWAPOUT_NONE
;
345 /* swapping complete */
346 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e
->url() << "' to " <<
347 e
->swap_dirn
<< ", " << std::hex
<< std::setw(8) << std::setfill('0') <<
348 std::uppercase
<< e
->swap_filen
);
349 e
->swap_file_sz
= e
->objectLen() + mem
->swap_hdr_sz
;
350 e
->swap_status
= SWAPOUT_DONE
;
351 e
->store()->updateSize(e
->swap_file_sz
, 1);
353 if (e
->checkCachable()) {
354 storeLog(STORE_LOG_SWAPOUT
, e
);
355 storeDirSwapLog(e
, SWAP_LOG_ADD
);
358 statCounter
.swap
.outs
++;
361 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__
<< ":" << __LINE__
);
362 mem
->swapout
.sio
= NULL
;
367 * Is this entry a candidate for writing to disk?
370 StoreEntry::swapOutAble() const
374 if (mem_obj
->swapout
.sio
!= NULL
)
377 if (mem_obj
->inmem_lo
> 0)
381 * If there are DISK clients, we must write to disk
382 * even if its not cachable
383 * RBC: Surely we should not create disk client on non cacheable objects?
384 * therefore this should be an assert?
385 * RBC 20030708: We can use disk to avoid mem races, so this shouldn't be
388 for (node
= mem_obj
->clients
.head
; node
; node
= node
->next
) {
389 if (((store_client
*) node
->data
)->getType() == STORE_DISK_CLIENT
)
393 /* Don't pollute the disk with icons and other special entries */
394 if (EBIT_TEST(flags
, ENTRY_SPECIAL
))
397 if (!EBIT_TEST(flags
, ENTRY_CACHABLE
))
400 if (!mem_obj
->isContiguous())