3 * $Id: store_swapout.cc,v 1.84 2001/11/13 17:30:45 hno Exp $
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 static off_t
storeSwapOutObjectBytesOnDisk(const MemObject
*);
39 static void storeSwapOutStart(StoreEntry
* e
);
40 static STIOCB storeSwapOutFileClosed
;
41 static STIOCB storeSwapOutFileNotify
;
43 /* start swapping object to disk */
45 storeSwapOutStart(StoreEntry
* e
)
48 MemObject
*mem
= e
->mem_obj
;
53 /* Build the swap metadata, so the filesystem will know how much
54 * metadata there is to store
56 debug(20, 5) ("storeSwapOutStart: Begin SwapOut '%s' to dirno %d, fileno %08X\n",
57 storeUrl(e
), e
->swap_dirn
, e
->swap_filen
);
58 e
->swap_status
= SWAPOUT_WRITING
;
59 tlv_list
= storeSwapMetaBuild(e
);
60 buf
= storeSwapMetaPack(tlv_list
, &swap_hdr_sz
);
61 storeSwapTLVFree(tlv_list
);
62 mem
->swap_hdr_sz
= (size_t) swap_hdr_sz
;
63 /* Create the swap file */
64 c
= cbdataAlloc(generic_cbdata
);
66 mem
->swapout
.sio
= storeCreate(e
, storeSwapOutFileNotify
, storeSwapOutFileClosed
, c
);
67 if (NULL
== mem
->swapout
.sio
) {
68 e
->swap_status
= SWAPOUT_NONE
;
71 storeLog(STORE_LOG_SWAPOUTFAIL
, e
);
74 storeLockObject(e
); /* Don't lock until after create, or the replacement
75 * code might get confused */
76 /* Pick up the file number if it was assigned immediately */
77 e
->swap_filen
= mem
->swapout
.sio
->swap_filen
;
78 e
->swap_dirn
= mem
->swapout
.sio
->swap_dirn
;
79 /* write out the swap metadata */
80 cbdataLock(mem
->swapout
.sio
);
81 storeWrite(mem
->swapout
.sio
, buf
, mem
->swap_hdr_sz
, 0, xfree
);
85 storeSwapOutFileNotify(void *data
, int errflag
, storeIOState
* sio
)
87 generic_cbdata
*c
= data
;
88 StoreEntry
*e
= c
->data
;
89 MemObject
*mem
= e
->mem_obj
;
90 assert(e
->swap_status
== SWAPOUT_WRITING
);
92 assert(mem
->swapout
.sio
== sio
);
94 e
->swap_filen
= mem
->swapout
.sio
->swap_filen
;
95 e
->swap_dirn
= mem
->swapout
.sio
->swap_dirn
;
99 storeSwapOut(StoreEntry
* e
)
101 MemObject
*mem
= e
->mem_obj
;
105 ssize_t swapout_size
;
106 ssize_t swap_buf_len
;
109 /* should we swap something out to disk? */
110 debug(20, 7) ("storeSwapOut: %s\n", storeUrl(e
));
111 debug(20, 7) ("storeSwapOut: store_status = %s\n",
112 storeStatusStr
[e
->store_status
]);
113 if (EBIT_TEST(e
->flags
, ENTRY_ABORTED
)) {
114 assert(EBIT_TEST(e
->flags
, RELEASE_REQUEST
));
115 storeSwapOutFileClose(e
);
118 debug(20, 7) ("storeSwapOut: mem->inmem_lo = %d\n",
119 (int) mem
->inmem_lo
);
120 debug(20, 7) ("storeSwapOut: mem->inmem_hi = %d\n",
121 (int) mem
->inmem_hi
);
122 debug(20, 7) ("storeSwapOut: swapout.queue_offset = %d\n",
123 (int) mem
->swapout
.queue_offset
);
124 if (mem
->swapout
.sio
)
125 debug(20, 7) ("storeSwapOut: storeOffset() = %d\n",
126 (int) storeOffset(mem
->swapout
.sio
));
127 assert(mem
->inmem_hi
>= mem
->swapout
.queue_offset
);
128 lowest_offset
= storeLowestMemReaderOffset(e
);
129 debug(20, 7) ("storeSwapOut: lowest_offset = %d\n",
130 (int) lowest_offset
);
132 * Grab the swapout_size and check to see whether we're going to defer
133 * the swapout based upon size
135 swapout_size
= (ssize_t
) (mem
->inmem_hi
- mem
->swapout
.queue_offset
);
136 if ((e
->store_status
!= STORE_OK
) && (swapout_size
< store_maxobjsize
)) {
138 * NOTE: the store_maxobjsize here is the max of optional
139 * max-size values from 'cache_dir' lines. It is not the
140 * same as 'maximum_object_size'. By default, store_maxobjsize
141 * will be set to -1. However, I am worried that this
142 * deferance may consume a lot of memory in some cases.
143 * It would be good to make this decision based on reply
144 * content-length, rather than wait to accumulate huge
145 * amounts of object data in memory.
147 debug(20, 5) ("storeSwapOut: Deferring starting swapping out\n");
151 * Careful. lowest_offset can be greater than inmem_hi, such
152 * as in the case of a range request.
154 if (mem
->inmem_hi
< lowest_offset
)
155 new_mem_lo
= lowest_offset
;
156 else if (mem
->inmem_hi
- mem
->inmem_lo
> Config
.Store
.maxInMemObjSize
)
157 new_mem_lo
= lowest_offset
;
159 new_mem_lo
= mem
->inmem_lo
;
160 assert(new_mem_lo
>= mem
->inmem_lo
);
161 if (storeSwapOutAble(e
)) {
163 * We should only free up to what we know has been written
164 * to disk, not what has been queued for writing. Otherwise
165 * there will be a chunk of the data which is not in memory
166 * and is not yet on disk.
167 * The -1 makes sure the page isn't freed until storeSwapOut has
168 * walked to the next page. (mem->swapout.memnode)
170 if ((on_disk
= storeSwapOutObjectBytesOnDisk(mem
)) - 1 < new_mem_lo
)
171 new_mem_lo
= on_disk
- 1;
172 if (new_mem_lo
== -1)
173 new_mem_lo
= 0; /* the above might become -1 */
174 } else if (new_mem_lo
> 0) {
176 * Its not swap-able, and we're about to delete a chunk,
177 * so we must make it PRIVATE. This is tricky/ugly because
178 * for the most part, we treat swapable == cachable here.
180 storeReleaseRequest(e
);
182 stmemFreeDataUpto(&mem
->data_hdr
, new_mem_lo
);
183 mem
->inmem_lo
= new_mem_lo
;
184 if (e
->swap_status
== SWAPOUT_WRITING
)
185 assert(mem
->inmem_lo
<= on_disk
);
186 if (!storeSwapOutAble(e
))
188 debug(20, 7) ("storeSwapOut: swapout_size = %d\n",
190 if (swapout_size
== 0) {
191 if (e
->store_status
== STORE_OK
)
192 storeSwapOutFileClose(e
);
193 return; /* Nevermore! */
195 if (e
->store_status
== STORE_PENDING
) {
196 /* wait for a full block to write */
197 if (swapout_size
< SM_PAGE_SIZE
)
200 * Wait until we are below the disk FD limit, only if the
201 * next server-side read won't be deferred.
203 if (storeTooManyDiskFilesOpen() && !fwdCheckDeferRead(-1, e
))
206 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
207 if (e
->swap_status
== SWAPOUT_NONE
) {
208 assert(mem
->swapout
.sio
== NULL
);
209 assert(mem
->inmem_lo
== 0);
210 if (storeCheckCachable(e
))
211 storeSwapOutStart(e
);
214 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
216 if (NULL
== mem
->swapout
.sio
)
221 * We are paging out to disk in page size chunks. however, later on when
222 * we update the queue position, we might not have a page (I *think*),
223 * so we do the actual page update here.
226 if (mem
->swapout
.memnode
== NULL
) {
227 /* We need to swap out the first page */
228 mem
->swapout
.memnode
= mem
->data_hdr
.head
;
230 /* We need to swap out the next page */
231 mem
->swapout
.memnode
= mem
->swapout
.memnode
->next
;
234 * Get the length of this buffer. We are assuming(!) that the buffer
235 * length won't change on this buffer, or things are going to be very
236 * strange. I think that after the copy to a buffer is done, the buffer
237 * size should stay fixed regardless so that this code isn't confused,
238 * but we can look at this at a later date or whenever the code results
239 * in bad swapouts, whichever happens first. :-)
241 swap_buf_len
= mem
->swapout
.memnode
->len
;
243 debug(20, 3) ("storeSwapOut: swap_buf_len = %d\n", (int) swap_buf_len
);
244 assert(swap_buf_len
> 0);
245 debug(20, 3) ("storeSwapOut: swapping out %ld bytes from %ld\n",
246 (long int) swap_buf_len
, (long int) mem
->swapout
.queue_offset
);
247 mem
->swapout
.queue_offset
+= swap_buf_len
;
248 storeWrite(mem
->swapout
.sio
, mem
->swapout
.memnode
->data
, swap_buf_len
, -1, NULL
);
249 /* the storeWrite() call might generate an error */
250 if (e
->swap_status
!= SWAPOUT_WRITING
)
252 swapout_size
= (ssize_t
) (mem
->inmem_hi
- mem
->swapout
.queue_offset
);
253 if (e
->store_status
== STORE_PENDING
)
254 if (swapout_size
< SM_PAGE_SIZE
)
256 } while (swapout_size
> 0);
257 if (NULL
== mem
->swapout
.sio
)
258 /* oops, we're not swapping out any more */
260 if (e
->store_status
== STORE_OK
) {
262 * If the state is STORE_OK, then all data must have been given
263 * to the filesystem at this point because storeSwapOut() is
264 * not going to be called again for this entry.
266 assert(mem
->inmem_hi
== mem
->swapout
.queue_offset
);
267 storeSwapOutFileClose(e
);
272 storeSwapOutFileClose(StoreEntry
* e
)
274 MemObject
*mem
= e
->mem_obj
;
276 debug(20, 3) ("storeSwapOutFileClose: %s\n", storeKeyText(e
->hash
.key
));
277 debug(20, 3) ("storeSwapOutFileClose: sio = %p\n", mem
->swapout
.sio
);
278 if (mem
->swapout
.sio
== NULL
)
280 storeClose(mem
->swapout
.sio
);
284 storeSwapOutFileClosed(void *data
, int errflag
, storeIOState
* sio
)
286 generic_cbdata
*c
= data
;
287 StoreEntry
*e
= c
->data
;
288 MemObject
*mem
= e
->mem_obj
;
289 assert(e
->swap_status
== SWAPOUT_WRITING
);
292 debug(20, 1) ("storeSwapOutFileClosed: dirno %d, swapfile %08X, errflag=%d\n\t%s\n",
293 e
->swap_dirn
, e
->swap_filen
, errflag
, xstrerror());
294 if (errflag
== DISK_NO_SPACE_LEFT
) {
295 storeDirDiskFull(e
->swap_dirn
);
299 if (e
->swap_filen
> 0)
303 e
->swap_status
= SWAPOUT_NONE
;
304 storeReleaseRequest(e
);
306 /* swapping complete */
307 debug(20, 3) ("storeSwapOutFileClosed: SwapOut complete: '%s' to %d, %08X\n",
308 storeUrl(e
), e
->swap_dirn
, e
->swap_filen
);
309 e
->swap_file_sz
= objectLen(e
) + mem
->swap_hdr_sz
;
310 e
->swap_status
= SWAPOUT_DONE
;
311 storeDirUpdateSwapSize(&Config
.cacheSwap
.swapDirs
[e
->swap_dirn
], e
->swap_file_sz
, 1);
312 if (storeCheckCachable(e
)) {
313 storeLog(STORE_LOG_SWAPOUT
, e
);
314 storeDirSwapLog(e
, SWAP_LOG_ADD
);
316 statCounter
.swap
.outs
++;
318 debug(20, 3) ("storeSwapOutFileClosed: %s:%d\n", __FILE__
, __LINE__
);
319 mem
->swapout
.sio
= NULL
;
321 storeUnlockObject(e
);
325 * How much of the object data is on the disk?
328 storeSwapOutObjectBytesOnDisk(const MemObject
* mem
)
331 * NOTE: storeOffset() represents the disk file size,
332 * not the amount of object data on disk.
334 * If we don't have at least 'swap_hdr_sz' bytes
335 * then none of the object data is on disk.
337 * This should still be safe if swap_hdr_sz == 0,
338 * meaning we haven't even opened the swapout file
342 if (mem
->swapout
.sio
== NULL
)
344 nwritten
= storeOffset(mem
->swapout
.sio
);
345 if (nwritten
<= mem
->swap_hdr_sz
)
347 return nwritten
- mem
->swap_hdr_sz
;
351 * Is this entry a candidate for writing to disk?
354 storeSwapOutAble(const StoreEntry
* e
)
357 if (e
->mem_obj
->swapout
.sio
!= NULL
)
359 if (e
->mem_obj
->inmem_lo
> 0)
362 * If there are DISK clients, we must write to disk
363 * even if its not cachable
365 for (node
= e
->mem_obj
->clients
.head
; node
; node
= node
->next
) {
366 if (((store_client
*) node
->data
)->type
== STORE_DISK_CLIENT
)
369 /* Don't pollute the disk with icons and other special entries */
370 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
372 return EBIT_TEST(e
->flags
, ENTRY_CACHABLE
);