3 * $Id: store_swapout.cc,v 1.65 2000/05/03 17:15:44 adrian Exp $
5 * DEBUG: section 20 Storage Manager Swapout Functions
6 * AUTHOR: Duane Wessels
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * 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
= xcalloc(1, sizeof(*c
));
66 cbdataAdd(c
, cbdataXfree
, 0);
67 mem
->swapout
.sio
= storeCreate(e
, storeSwapOutFileNotify
, storeSwapOutFileClosed
, c
);
68 if (NULL
== mem
->swapout
.sio
) {
69 e
->swap_status
= SWAPOUT_NONE
;
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
;
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
= (size_t) (mem
->inmem_hi
- mem
->swapout
.queue_offset
);
136 if ((e
->store_status
!= STORE_OK
) && (swapout_size
< store_maxobjsize
)) {
137 debug (20, 5) ("storeSwapOut: Deferring starting swapping out\n");
141 * Careful. lowest_offset can be greater than inmem_hi, such
142 * as in the case of a range request.
144 if (mem
->inmem_hi
< lowest_offset
)
145 new_mem_lo
= lowest_offset
;
146 else if (mem
->inmem_hi
- lowest_offset
> SM_PAGE_SIZE
)
147 new_mem_lo
= lowest_offset
;
149 new_mem_lo
= mem
->inmem_lo
;
150 assert(new_mem_lo
>= mem
->inmem_lo
);
151 if (storeSwapOutAble(e
)) {
153 * We should only free up to what we know has been written
154 * to disk, not what has been queued for writing. Otherwise
155 * there will be a chunk of the data which is not in memory
156 * and is not yet on disk.
158 if ((on_disk
= storeSwapOutObjectBytesOnDisk(mem
)) < new_mem_lo
)
159 new_mem_lo
= on_disk
;
160 } else if (new_mem_lo
> 0) {
162 * Its not swap-able, and we're about to delete a chunk,
163 * so we must make it PRIVATE. This is tricky/ugly because
164 * for the most part, we treat swapable == cachable here.
166 storeReleaseRequest(e
);
168 stmemFreeDataUpto(&mem
->data_hdr
, new_mem_lo
);
169 mem
->inmem_lo
= new_mem_lo
;
170 if (e
->swap_status
== SWAPOUT_WRITING
)
171 assert(mem
->inmem_lo
<= on_disk
);
172 if (!storeSwapOutAble(e
))
174 debug(20, 7) ("storeSwapOut: swapout_size = %d\n",
176 if (swapout_size
== 0) {
177 if (e
->store_status
== STORE_OK
)
178 storeSwapOutFileClose(e
);
179 return; /* Nevermore! */
181 if (e
->store_status
== STORE_PENDING
) {
182 /* wait for a full block to write */
183 if (swapout_size
< SM_PAGE_SIZE
)
186 * Wait until we are below the disk FD limit, only if the
187 * next server-side read won't be deferred.
189 if (storeTooManyDiskFilesOpen() && !fwdCheckDeferRead(-1, e
))
193 * Don't start swapping out until its either all in memory, or bigger
194 * than the maximum object size (so we pick a -1 maxobjsize fs)
196 if ((e
->store_status
!= STORE_OK
) && (swapout_size
< store_maxobjsize
)) {
197 debug (20, 5) ("storeSwapOut: Deferring starting swapping out\n");
200 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
201 if (e
->swap_status
== SWAPOUT_NONE
) {
202 assert(mem
->swapout
.sio
== NULL
);
203 assert(mem
->inmem_lo
== 0);
204 if (storeCheckCachable(e
))
205 storeSwapOutStart(e
);
208 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
210 if (NULL
== mem
->swapout
.sio
)
215 * We are paging out to disk in page size chunks. however, later on when
216 * we update the queue position, we might not have a page (I *think*),
217 * so we do the actual page update here.
220 if (mem
->swapout
.memnode
== NULL
) {
221 /* We need to swap out the first page */
222 mem
->swapout
.memnode
= mem
->data_hdr
.head
;
224 /* We need to swap out the next page */
225 mem
->swapout
.memnode
= mem
->swapout
.memnode
->next
;
228 * Get the length of this buffer. We are assuming(!) that the buffer
229 * length won't change on this buffer, or things are going to be very
230 * strange. I think that after the copy to a buffer is done, the buffer
231 * size should stay fixed regardless so that this code isn't confused,
232 * but we can look at this at a later date or whenever the code results
233 * in bad swapouts, whichever happens first. :-)
235 swap_buf_len
= mem
->swapout
.memnode
->len
;
237 debug(20, 3) ("storeSwapOut: swap_buf_len = %d\n", (int) swap_buf_len
);
238 assert(swap_buf_len
> 0);
239 debug(20, 3) ("storeSwapOut: swapping out %d bytes from %d\n",
240 swap_buf_len
, (int) mem
->swapout
.queue_offset
);
241 mem
->swapout
.queue_offset
+= swap_buf_len
;
242 storeWrite(mem
->swapout
.sio
, mem
->swapout
.memnode
->data
, swap_buf_len
, -1, NULL
);
243 /* the storeWrite() call might generate an error */
244 if (e
->swap_status
!= SWAPOUT_WRITING
)
246 swapout_size
= (size_t) (mem
->inmem_hi
- mem
->swapout
.queue_offset
);
247 if (e
->store_status
== STORE_PENDING
)
248 if (swapout_size
< SM_PAGE_SIZE
)
250 } while (swapout_size
> 0);
251 if (NULL
== mem
->swapout
.sio
)
252 /* oops, we're not swapping out any more */
254 if (e
->store_status
== STORE_OK
) {
256 * If the state is STORE_OK, then all data must have been given
257 * to the filesystem at this point because storeSwapOut() is
258 * not going to be called again for this entry.
260 assert(mem
->inmem_hi
== mem
->swapout
.queue_offset
);
261 storeSwapOutFileClose(e
);
266 storeSwapOutFileClose(StoreEntry
* e
)
268 MemObject
*mem
= e
->mem_obj
;
270 debug(20, 3) ("storeSwapOutFileClose: %s\n", storeKeyText(e
->key
));
271 debug(20, 3) ("storeSwapOutFileClose: sio = %p\n", mem
->swapout
.sio
);
272 if (mem
->swapout
.sio
== NULL
)
274 storeClose(mem
->swapout
.sio
);
278 storeSwapOutFileClosed(void *data
, int errflag
, storeIOState
* sio
)
280 generic_cbdata
*c
= data
;
281 StoreEntry
*e
= c
->data
;
282 MemObject
*mem
= e
->mem_obj
;
283 assert(e
->swap_status
== SWAPOUT_WRITING
);
286 debug(20, 1) ("storeSwapOutFileClosed: dirno %d, swapfile %08X, errflag=%d\n\t%s\n",
287 e
->swap_dirn
, e
->swap_filen
, errflag
, xstrerror());
288 if (errflag
== DISK_NO_SPACE_LEFT
) {
289 storeDirDiskFull(e
->swap_dirn
);
293 if (e
->swap_filen
> 0)
297 e
->swap_status
= SWAPOUT_NONE
;
298 storeReleaseRequest(e
);
300 /* swapping complete */
301 debug(20, 3) ("storeSwapOutFileClosed: SwapOut complete: '%s' to %d, %08X\n",
302 storeUrl(e
), e
->swap_dirn
, e
->swap_filen
);
303 e
->swap_file_sz
= objectLen(e
) + mem
->swap_hdr_sz
;
304 e
->swap_status
= SWAPOUT_DONE
;
305 storeDirUpdateSwapSize(&Config
.cacheSwap
.swapDirs
[e
->swap_dirn
], e
->swap_file_sz
, 1);
306 if (storeCheckCachable(e
)) {
307 storeLog(STORE_LOG_SWAPOUT
, e
);
308 storeDirSwapLog(e
, SWAP_LOG_ADD
);
311 debug(20, 3) ("storeSwapOutFileClosed: %s:%d\n", __FILE__
, __LINE__
);
312 mem
->swapout
.sio
= NULL
;
314 storeUnlockObject(e
);
318 * How much of the object data is on the disk?
321 storeSwapOutObjectBytesOnDisk(const MemObject
* mem
)
324 * NOTE: storeOffset() represents the disk file size,
325 * not the amount of object data on disk.
327 * If we don't have at least 'swap_hdr_sz' bytes
328 * then none of the object data is on disk.
330 * This should still be safe if swap_hdr_sz == 0,
331 * meaning we haven't even opened the swapout file
335 if (mem
->swapout
.sio
== NULL
)
337 nwritten
= storeOffset(mem
->swapout
.sio
);
338 if (nwritten
<= mem
->swap_hdr_sz
)
340 return nwritten
- mem
->swap_hdr_sz
;
344 * Is this entry a candidate for writing to disk?
347 storeSwapOutAble(const StoreEntry
* e
)
350 if (e
->mem_obj
->swapout
.sio
!= NULL
)
352 if (e
->mem_obj
->inmem_lo
> 0)
355 * If there are DISK clients, we must write to disk
356 * even if its not cachable
358 for (sc
= e
->mem_obj
->clients
; sc
; sc
= sc
->next
)
359 if (sc
->type
== STORE_DISK_CLIENT
)
361 if (store_dirs_rebuilding
)
362 if (!EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
364 return EBIT_TEST(e
->flags
, ENTRY_CACHABLE
);