]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
Converted a few more StoreEntry functions to class methods:
[thirdparty/squid.git] / src / store_swapout.cc
1
2 /*
3 * $Id: store_swapout.cc,v 1.114 2007/04/20 23:53:42 wessels 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 #include "cbdata.h"
38 #include "StoreClient.h"
39 #include "Store.h"
40 /* FIXME: Abstract the use of this more */
41 #include "mem_node.h"
42 #include "MemObject.h"
43 #include "SwapDir.h"
44
45 static void storeSwapOutStart(StoreEntry * e);
46 static StoreIOState::STIOCB storeSwapOutFileClosed;
47 static StoreIOState::STFNCB storeSwapOutFileNotify;
48
49 /* start swapping object to disk */
50 static void
51 storeSwapOutStart(StoreEntry * e)
52 {
53 MemObject *mem = e->mem_obj;
54 StoreIOState::Pointer sio;
55 assert(mem);
56 /* Build the swap metadata, so the filesystem will know how much
57 * metadata there is to store
58 */
59 debug(20, 5) ("storeSwapOutStart: Begin SwapOut '%s' to dirno %d, fileno %08X\n",
60 storeUrl(e), e->swap_dirn, e->swap_filen);
61 e->swap_status = SWAPOUT_WRITING;
62 /* If we start swapping out objects with OutOfBand Metadata,
63 * then this code needs changing
64 */
65 /* Create the swap file */
66 generic_cbdata *c = new generic_cbdata(e);
67 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
68
69 if (sio == NULL) {
70 e->swap_status = SWAPOUT_NONE;
71 delete c;
72 storeLog(STORE_LOG_SWAPOUTFAIL, e);
73 return;
74 }
75
76 mem->swapout.sio = sio;
77 /* Don't lock until after create, or the replacement
78 * code might get confused */
79
80 e->lock()
81
82 ;
83 /* Pick up the file number if it was assigned immediately */
84 e->swap_filen = mem->swapout.sio->swap_filen;
85
86 e->swap_dirn = mem->swapout.sio->swap_dirn;
87
88 /* write out the swap metadata */
89 /* TODO: make some sort of data,size refcounted immutable buffer
90 * for use by this sort of function.
91 */
92 char const *buf = e->getSerialisedMetaData ();
93
94 /* If we start swapping out with out of band metadata, this assert
95 * will catch it - this code needs to be adjusted if that happens
96 */
97 assert (buf);
98
99 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree);
100 }
101
102 static void
103 storeSwapOutFileNotify(void *data, int errflag, StoreIOState::Pointer self)
104 {
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);
109 assert(mem);
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;
114 }
115
116 static void
117 doPages(StoreEntry *anEntry)
118 {
119 MemObject *mem = anEntry->mem_obj;
120
121 do {
122 /*
123 * Evil hack time.
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.
127 */
128
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());
132 } else {
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());
137 }
138
139 /*
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. :-)
146 */
147 ssize_t swap_buf_len = mem->swapout.memnode->nodeBuffer.length;
148
149 debug(20, 3) ("storeSwapOut: swap_buf_len = %d\n", (int) swap_buf_len);
150
151 assert(swap_buf_len > 0);
152
153 debug(20, 3) ("storeSwapOut: swapping out %ld bytes from %ld\n",
154 (long int) swap_buf_len, (long int) mem->swapout.queue_offset);
155
156 mem->swapout.queue_offset += swap_buf_len;
157
158 storeIOWrite(mem->swapout.sio,
159 mem->data_hdr.NodeGet(mem->swapout.memnode),
160 swap_buf_len,
161 -1,
162 memNodeWriteComplete);
163
164 /* the storeWrite() call might generate an error */
165 if (anEntry->swap_status != SWAPOUT_WRITING)
166 break;
167
168 ssize_t swapout_size = (ssize_t) (mem->endOffset() - mem->swapout.queue_offset);
169
170 if (anEntry->store_status == STORE_PENDING)
171 if (swapout_size < SM_PAGE_SIZE)
172 break;
173
174 if (swapout_size <= 0)
175 return;
176 } while (true);
177 }
178
179
180 /* This routine is called every time data is sent to the client side.
181 * It's overhead is therefor, significant.
182 */
183 void
184 StoreEntry::swapOut()
185 {
186 if (!mem_obj)
187 return;
188
189 if (!swapoutPossible())
190 return;
191
192 debug(20, 7) ("storeSwapOut: mem_obj->inmem_lo = %d\n",
193 (int) mem_obj->inmem_lo);
194
195 debug(20, 7) ("storeSwapOut: mem_obj->endOffset() = %d\n",
196 (int) mem_obj->endOffset());
197
198 debug(20, 7) ("storeSwapOut: swapout.queue_offset = %d\n",
199 (int) mem_obj->swapout.queue_offset);
200
201 if (mem_obj->swapout.sio != NULL)
202 debug(20, 7) ("storeSwapOut: storeOffset() = %d\n",
203 (int) mem_obj->swapout.sio->offset());
204
205 ssize_t swapout_maxsize = (ssize_t) (mem_obj->endOffset() - mem_obj->swapout.queue_offset);
206
207 assert(swapout_maxsize >= 0);
208
209 off_t const lowest_offset = mem_obj->lowestMemReaderOffset();
210
211 debug(20, 7) ("storeSwapOut: lowest_offset = %d\n",
212 (int) lowest_offset);
213
214 /*
215 * Grab the swapout_size and check to see whether we're going to defer
216 * the swapout based upon size
217 */
218 if ((store_status != STORE_OK) && (swapout_maxsize < store_maxobjsize)) {
219 /*
220 * NOTE: the store_maxobjsize here is the max of optional
221 * max-size values from 'cache_dir' lines. It is not the
222 * same as 'maximum_object_size'. By default, store_maxobjsize
223 * will be set to -1. However, I am worried that this
224 * deferance may consume a lot of memory in some cases.
225 * It would be good to make this decision based on reply
226 * content-length, rather than wait to accumulate huge
227 * amounts of object data in memory.
228 */
229 debug(20, 5) ("storeSwapOut: Deferring starting swapping out\n");
230 return;
231 }
232
233 trimMemory();
234 #if SIZEOF_OFF_T == 4
235
236 if (mem_obj->endOffset() > 0x7FFF0000) {
237 debug(20, 0) ("WARNING: preventing off_t overflow for %s\n", storeUrl(this));
238 abort();
239 return;
240 }
241
242 #endif
243 if (swap_status == SWAPOUT_WRITING)
244 assert(mem_obj->inmem_lo <= (off_t)mem_obj->objectBytesOnDisk() );
245
246 if (!swapOutAble())
247 return;
248
249 debug(20, 7) ("storeSwapOut: swapout_size = %d\n",
250 (int) swapout_maxsize);
251
252 if (swapout_maxsize == 0) {
253 if (store_status == STORE_OK)
254 swapOutFileClose();
255
256 return; /* Nevermore! */
257 }
258
259 if (store_status == STORE_PENDING) {
260 /* wait for a full block to write */
261
262 if (swapout_maxsize < SM_PAGE_SIZE)
263 return;
264
265 /*
266 * Wait until we are below the disk FD limit, only if the
267 * next server-side read won't be deferred.
268 */
269 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
270 return;
271 }
272
273 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
274 if (swap_status == SWAPOUT_NONE) {
275 assert(mem_obj->swapout.sio == NULL);
276 assert(mem_obj->inmem_lo == 0);
277
278 if (storeCheckCachable(this))
279 storeSwapOutStart(this);
280 else
281 return;
282
283 /* ENTRY_CACHABLE will be cleared and we'll never get here again */
284 }
285
286 if (mem_obj->swapout.sio == NULL)
287 return;
288
289 doPages(this);
290
291 if (mem_obj->swapout.sio == NULL)
292 /* oops, we're not swapping out any more */
293 return;
294
295 if (store_status == STORE_OK) {
296 /*
297 * If the state is STORE_OK, then all data must have been given
298 * to the filesystem at this point because storeSwapOut() is
299 * not going to be called again for this entry.
300 */
301 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
302 swapOutFileClose();
303 }
304 }
305
306 void
307 StoreEntry::swapOutFileClose()
308 {
309 assert(mem_obj != NULL);
310 debug(20, 3) ("storeSwapOutFileClose: %s\n", getMD5Text());
311 debug(20, 3) ("storeSwapOutFileClose: sio = %p\n", mem_obj->swapout.sio.getRaw());
312
313 if (mem_obj->swapout.sio == NULL)
314 return;
315
316 storeClose(mem_obj->swapout.sio);
317 }
318
319 static void
320 storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
321 {
322 generic_cbdata *c = (generic_cbdata *)data;
323 StoreEntry *e = (StoreEntry *)c->data;
324 MemObject *mem = e->mem_obj;
325 assert(mem->swapout.sio == self);
326 assert(e->swap_status == SWAPOUT_WRITING);
327 cbdataFree(c);
328
329 if (errflag) {
330 debug(20, 1) ("storeSwapOutFileClosed: dirno %d, swapfile %08X, errflag=%d\n\t%s\n",
331 e->swap_dirn, e->swap_filen, errflag, xstrerror());
332
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.
336 */
337 e->store()->diskFull();
338 storeConfigure();
339 }
340
341 if (e->swap_filen > 0)
342 e->unlink();
343
344 e->swap_filen = -1;
345
346 e->swap_dirn = -1;
347
348 e->swap_status = SWAPOUT_NONE;
349
350 e->releaseRequest();
351 } else {
352 /* swapping complete */
353 debug(20, 3) ("storeSwapOutFileClosed: SwapOut complete: '%s' to %d, %08X\n",
354 storeUrl(e), e->swap_dirn, e->swap_filen);
355 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
356 e->swap_status = SWAPOUT_DONE;
357 e->store()->updateSize(e->swap_file_sz, 1);
358
359 if (storeCheckCachable(e)) {
360 storeLog(STORE_LOG_SWAPOUT, e);
361 storeDirSwapLog(e, SWAP_LOG_ADD);
362 }
363
364 statCounter.swap.outs++;
365 }
366
367 debug(20, 3) ("storeSwapOutFileClosed: %s:%d\n", __FILE__, __LINE__);
368 mem->swapout.sio = NULL;
369 e->unlock();
370 }
371
372 /*
373 * Is this entry a candidate for writing to disk?
374 */
375 bool
376 StoreEntry::swapOutAble() const
377 {
378 dlink_node *node;
379
380 if (mem_obj->swapout.sio != NULL)
381 return true;
382
383 if (mem_obj->inmem_lo > 0)
384 return false;
385
386 /*
387 * If there are DISK clients, we must write to disk
388 * even if its not cachable
389 * RBC: Surely we should not create disk client on non cacheable objects?
390 * therefore this should be an assert?
391 * RBC 20030708: We can use disk to avoid mem races, so this shouldn't be
392 * an assert.
393 */
394 for (node = mem_obj->clients.head; node; node = node->next) {
395 if (((store_client *) node->data)->getType() == STORE_DISK_CLIENT)
396 return true;
397 }
398
399 /* Don't pollute the disk with icons and other special entries */
400 if (EBIT_TEST(flags, ENTRY_SPECIAL))
401 return false;
402
403 if (!EBIT_TEST(flags, ENTRY_CACHABLE))
404 return false;
405
406 if (!mem_obj->isContiguous())
407 return false;
408
409 return true;
410 }