]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
Don't swap out "SPECIAL" entries such as icons. We don't log these anyway
[thirdparty/squid.git] / src / store_swapout.cc
1
2 /*
3 * $Id: store_swapout.cc,v 1.84 2001/11/13 17:30:45 hno Exp $
4 *
5 * DEBUG: section 20 Storage Manager Swapout Functions
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
19 *
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.
24 *
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.
29 *
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.
33 *
34 */
35
36 #include "squid.h"
37
38 static off_t storeSwapOutObjectBytesOnDisk(const MemObject *);
39 static void storeSwapOutStart(StoreEntry * e);
40 static STIOCB storeSwapOutFileClosed;
41 static STIOCB storeSwapOutFileNotify;
42
43 /* start swapping object to disk */
44 static void
45 storeSwapOutStart(StoreEntry * e)
46 {
47 generic_cbdata *c;
48 MemObject *mem = e->mem_obj;
49 int swap_hdr_sz = 0;
50 tlv *tlv_list;
51 char *buf;
52 assert(mem);
53 /* Build the swap metadata, so the filesystem will know how much
54 * metadata there is to store
55 */
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);
65 c->data = e;
66 mem->swapout.sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
67 if (NULL == mem->swapout.sio) {
68 e->swap_status = SWAPOUT_NONE;
69 cbdataFree(c);
70 xfree(buf);
71 storeLog(STORE_LOG_SWAPOUTFAIL, e);
72 return;
73 }
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);
82 }
83
84 static void
85 storeSwapOutFileNotify(void *data, int errflag, storeIOState * sio)
86 {
87 generic_cbdata *c = data;
88 StoreEntry *e = c->data;
89 MemObject *mem = e->mem_obj;
90 assert(e->swap_status == SWAPOUT_WRITING);
91 assert(mem);
92 assert(mem->swapout.sio == sio);
93 assert(errflag == 0);
94 e->swap_filen = mem->swapout.sio->swap_filen;
95 e->swap_dirn = mem->swapout.sio->swap_dirn;
96 }
97
98 void
99 storeSwapOut(StoreEntry * e)
100 {
101 MemObject *mem = e->mem_obj;
102 off_t lowest_offset;
103 off_t new_mem_lo;
104 off_t on_disk = 0;
105 ssize_t swapout_size;
106 ssize_t swap_buf_len;
107 if (mem == NULL)
108 return;
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);
116 return;
117 }
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);
131 /*
132 * Grab the swapout_size and check to see whether we're going to defer
133 * the swapout based upon size
134 */
135 swapout_size = (ssize_t) (mem->inmem_hi - mem->swapout.queue_offset);
136 if ((e->store_status != STORE_OK) && (swapout_size < store_maxobjsize)) {
137 /*
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.
146 */
147 debug(20, 5) ("storeSwapOut: Deferring starting swapping out\n");
148 return;
149 }
150 /*
151 * Careful. lowest_offset can be greater than inmem_hi, such
152 * as in the case of a range request.
153 */
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;
158 else
159 new_mem_lo = mem->inmem_lo;
160 assert(new_mem_lo >= mem->inmem_lo);
161 if (storeSwapOutAble(e)) {
162 /*
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)
169 */
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) {
175 /*
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.
179 */
180 storeReleaseRequest(e);
181 }
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))
187 return;
188 debug(20, 7) ("storeSwapOut: swapout_size = %d\n",
189 (int) swapout_size);
190 if (swapout_size == 0) {
191 if (e->store_status == STORE_OK)
192 storeSwapOutFileClose(e);
193 return; /* Nevermore! */
194 }
195 if (e->store_status == STORE_PENDING) {
196 /* wait for a full block to write */
197 if (swapout_size < SM_PAGE_SIZE)
198 return;
199 /*
200 * Wait until we are below the disk FD limit, only if the
201 * next server-side read won't be deferred.
202 */
203 if (storeTooManyDiskFilesOpen() && !fwdCheckDeferRead(-1, e))
204 return;
205 }
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);
212 else
213 return;
214 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
215 }
216 if (NULL == mem->swapout.sio)
217 return;
218 do {
219 /*
220 * Evil hack time.
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.
224 */
225
226 if (mem->swapout.memnode == NULL) {
227 /* We need to swap out the first page */
228 mem->swapout.memnode = mem->data_hdr.head;
229 } else {
230 /* We need to swap out the next page */
231 mem->swapout.memnode = mem->swapout.memnode->next;
232 }
233 /*
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. :-)
240 */
241 swap_buf_len = mem->swapout.memnode->len;
242
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)
251 break;
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)
255 break;
256 } while (swapout_size > 0);
257 if (NULL == mem->swapout.sio)
258 /* oops, we're not swapping out any more */
259 return;
260 if (e->store_status == STORE_OK) {
261 /*
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.
265 */
266 assert(mem->inmem_hi == mem->swapout.queue_offset);
267 storeSwapOutFileClose(e);
268 }
269 }
270
271 void
272 storeSwapOutFileClose(StoreEntry * e)
273 {
274 MemObject *mem = e->mem_obj;
275 assert(mem != NULL);
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)
279 return;
280 storeClose(mem->swapout.sio);
281 }
282
283 static void
284 storeSwapOutFileClosed(void *data, int errflag, storeIOState * sio)
285 {
286 generic_cbdata *c = data;
287 StoreEntry *e = c->data;
288 MemObject *mem = e->mem_obj;
289 assert(e->swap_status == SWAPOUT_WRITING);
290 cbdataFree(c);
291 if (errflag) {
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);
296 storeDirConfigure();
297 storeConfigure();
298 }
299 if (e->swap_filen > 0)
300 storeUnlink(e);
301 e->swap_filen = -1;
302 e->swap_dirn = -1;
303 e->swap_status = SWAPOUT_NONE;
304 storeReleaseRequest(e);
305 } else {
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);
315 }
316 statCounter.swap.outs++;
317 }
318 debug(20, 3) ("storeSwapOutFileClosed: %s:%d\n", __FILE__, __LINE__);
319 mem->swapout.sio = NULL;
320 cbdataUnlock(sio);
321 storeUnlockObject(e);
322 }
323
324 /*
325 * How much of the object data is on the disk?
326 */
327 static off_t
328 storeSwapOutObjectBytesOnDisk(const MemObject * mem)
329 {
330 /*
331 * NOTE: storeOffset() represents the disk file size,
332 * not the amount of object data on disk.
333 *
334 * If we don't have at least 'swap_hdr_sz' bytes
335 * then none of the object data is on disk.
336 *
337 * This should still be safe if swap_hdr_sz == 0,
338 * meaning we haven't even opened the swapout file
339 * yet.
340 */
341 off_t nwritten;
342 if (mem->swapout.sio == NULL)
343 return 0;
344 nwritten = storeOffset(mem->swapout.sio);
345 if (nwritten <= mem->swap_hdr_sz)
346 return 0;
347 return nwritten - mem->swap_hdr_sz;
348 }
349
350 /*
351 * Is this entry a candidate for writing to disk?
352 */
353 int
354 storeSwapOutAble(const StoreEntry * e)
355 {
356 dlink_node *node;
357 if (e->mem_obj->swapout.sio != NULL)
358 return 1;
359 if (e->mem_obj->inmem_lo > 0)
360 return 0;
361 /*
362 * If there are DISK clients, we must write to disk
363 * even if its not cachable
364 */
365 for (node = e->mem_obj->clients.head; node; node = node->next) {
366 if (((store_client *) node->data)->type == STORE_DISK_CLIENT)
367 return 1;
368 }
369 /* Don't pollute the disk with icons and other special entries */
370 if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
371 return 0;
372 return EBIT_TEST(e->flags, ENTRY_CACHABLE);
373 }