]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
Removed squid-old.h
[thirdparty/squid.git] / src / store_swapout.cc
1
2 /*
3 * $Id$
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 "protos.h"
44 #include "SwapDir.h"
45 #include "StatCounters.h"
46 #include "swap_log_op.h"
47
48 static void storeSwapOutStart(StoreEntry * e);
49 static StoreIOState::STIOCB storeSwapOutFileClosed;
50 static StoreIOState::STFNCB storeSwapOutFileNotify;
51
52 // wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
53 static void xfree_cppwrapper(void *x)
54 {
55 xfree(x);
56 }
57
58 /* start swapping object to disk */
59 static void
60 storeSwapOutStart(StoreEntry * e)
61 {
62 MemObject *mem = e->mem_obj;
63 StoreIOState::Pointer sio;
64 assert(mem);
65 /* Build the swap metadata, so the filesystem will know how much
66 * metadata there is to store
67 */
68 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e->url() << "' to dirno " <<
69 e->swap_dirn << ", fileno " << std::hex << std::setw(8) << std::setfill('0') <<
70 std::uppercase << e->swap_filen);
71 e->swap_status = SWAPOUT_WRITING;
72 /* If we start swapping out objects with OutOfBand Metadata,
73 * then this code needs changing
74 */
75
76 /* TODO: make some sort of data,size refcounted immutable buffer
77 * and stop fooling ourselves with "const char*" buffers.
78 */
79
80 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
81 const char *buf = e->getSerialisedMetaData ();
82 assert(buf);
83
84 /* Create the swap file */
85 generic_cbdata *c = new generic_cbdata(e);
86 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
87
88 if (sio == NULL) {
89 e->swap_status = SWAPOUT_NONE;
90 mem->swapout.decision = MemObject::SwapOut::swImpossible;
91 delete c;
92 xfree((char*)buf);
93 storeLog(STORE_LOG_SWAPOUTFAIL, e);
94 return;
95 }
96
97 mem->swapout.sio = sio;
98 /* Don't lock until after create, or the replacement
99 * code might get confused */
100
101 e->lock();
102 /* Pick up the file number if it was assigned immediately */
103 e->swap_filen = mem->swapout.sio->swap_filen;
104
105 e->swap_dirn = mem->swapout.sio->swap_dirn;
106
107 /* write out the swap metadata */
108 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree_cppwrapper);
109 }
110
111 static void
112 storeSwapOutFileNotify(void *data, int errflag, StoreIOState::Pointer self)
113 {
114 generic_cbdata *c = (generic_cbdata *)data;
115 StoreEntry *e = (StoreEntry *)c->data;
116 MemObject *mem = e->mem_obj;
117 assert(e->swap_status == SWAPOUT_WRITING);
118 assert(mem);
119 assert(mem->swapout.sio == self);
120 assert(errflag == 0);
121 assert(e->swap_filen < 0); // if this fails, call SwapDir::disconnect(e)
122 e->swap_filen = mem->swapout.sio->swap_filen;
123 e->swap_dirn = mem->swapout.sio->swap_dirn;
124 }
125
126 static void
127 doPages(StoreEntry *anEntry)
128 {
129 MemObject *mem = anEntry->mem_obj;
130
131 do {
132 // find the page containing the first byte we have not swapped out yet
133 mem_node *page =
134 mem->data_hdr.getBlockContainingLocation(mem->swapout.queue_offset);
135
136 if (!page)
137 return; // wait for more data to become available
138
139 // memNodeWriteComplete() and absence of buffer offset math below
140 // imply that we always write from the very beginning of the page
141 assert(page->start() == mem->swapout.queue_offset);
142
143 /*
144 * Get the length of this buffer. We are assuming(!) that the buffer
145 * length won't change on this buffer, or things are going to be very
146 * strange. I think that after the copy to a buffer is done, the buffer
147 * size should stay fixed regardless so that this code isn't confused,
148 * but we can look at this at a later date or whenever the code results
149 * in bad swapouts, whichever happens first. :-)
150 */
151 ssize_t swap_buf_len = page->nodeBuffer.length;
152
153 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
154
155 assert(swap_buf_len > 0);
156
157 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
158
159 mem->swapout.queue_offset += swap_buf_len;
160
161 storeIOWrite(mem->swapout.sio,
162 mem->data_hdr.NodeGet(page),
163 swap_buf_len,
164 -1,
165 memNodeWriteComplete);
166
167 /* the storeWrite() call might generate an error */
168 if (anEntry->swap_status != SWAPOUT_WRITING)
169 break;
170
171 int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
172
173 if (anEntry->store_status == STORE_PENDING)
174 if (swapout_size < SM_PAGE_SIZE)
175 break;
176
177 if (swapout_size <= 0)
178 return;
179 } while (true);
180 }
181
182
183 /* This routine is called every time data is sent to the client side.
184 * It's overhead is therefor, significant.
185 */
186 void
187 StoreEntry::swapOut()
188 {
189 if (!mem_obj)
190 return;
191
192 // this flag may change so we must check even if we are swappingOut
193 if (EBIT_TEST(flags, ENTRY_ABORTED)) {
194 assert(EBIT_TEST(flags, RELEASE_REQUEST));
195 // StoreEntry::abort() already closed the swap out file, if any
196 // no trimming: data producer must stop production if ENTRY_ABORTED
197 return;
198 }
199
200 const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
201
202 Store::Root().maybeTrimMemory(*this, weAreOrMayBeSwappingOut);
203
204 if (!weAreOrMayBeSwappingOut)
205 return; // nothing else to do
206
207 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
208 // store_status == STORE_OK below means we got everything we wanted.
209
210 debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
211 debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
212 debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
213
214 if (mem_obj->swapout.sio != NULL)
215 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
216
217 int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
218
219 debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
220
221 #if SIZEOF_OFF_T <= 4
222
223 if (mem_obj->endOffset() > 0x7FFF0000) {
224 debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
225 abort();
226 return;
227 }
228
229 #endif
230 if (swap_status == SWAPOUT_WRITING)
231 assert(mem_obj->inmem_lo <= mem_obj->objectBytesOnDisk() );
232
233 // buffered bytes we have not swapped out yet
234 const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
235 assert(swapout_maxsize >= 0);
236 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
237
238 if (swapout_maxsize == 0) { // swapped everything we got
239 if (store_status == STORE_OK) { // got everything we wanted
240 assert(mem_obj->object_sz >= 0);
241 swapOutFileClose(StoreIOState::wroteAll);
242 }
243 // else need more data to swap out
244 return;
245 }
246
247 if (store_status == STORE_PENDING) {
248 /* wait for a full block to write */
249
250 if (swapout_maxsize < SM_PAGE_SIZE)
251 return;
252
253 /*
254 * Wait until we are below the disk FD limit, only if the
255 * next server-side read won't be deferred.
256 */
257 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
258 return;
259 }
260
261 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
262 if (swap_status == SWAPOUT_NONE) {
263 assert(mem_obj->swapout.sio == NULL);
264 assert(mem_obj->inmem_lo == 0);
265 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
266 }
267
268 if (mem_obj->swapout.sio == NULL)
269 return;
270
271 doPages(this);
272
273 if (mem_obj->swapout.sio == NULL)
274 /* oops, we're not swapping out any more */
275 return;
276
277 if (store_status == STORE_OK) {
278 /*
279 * If the state is STORE_OK, then all data must have been given
280 * to the filesystem at this point because storeSwapOut() is
281 * not going to be called again for this entry.
282 */
283 assert(mem_obj->object_sz >= 0);
284 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
285 swapOutFileClose(StoreIOState::wroteAll);
286 }
287 }
288
289 void
290 StoreEntry::swapOutFileClose(int how)
291 {
292 assert(mem_obj != NULL);
293 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
294 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
295
296 if (mem_obj->swapout.sio == NULL)
297 return;
298
299 storeClose(mem_obj->swapout.sio, how);
300 }
301
302 static void
303 storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
304 {
305 generic_cbdata *c = (generic_cbdata *)data;
306 StoreEntry *e = (StoreEntry *)c->data;
307 MemObject *mem = e->mem_obj;
308 assert(mem->swapout.sio == self);
309 assert(e->swap_status == SWAPOUT_WRITING);
310 cbdataFree(c);
311
312 // if object_size is still unknown, the entry was probably aborted
313 if (errflag || e->objectLen() < 0) {
314 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
315 std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
316 e->swap_filen << ", errflag=" << errflag);
317
318 if (errflag == DISK_NO_SPACE_LEFT) {
319 /* FIXME: this should be handle by the link from store IO to
320 * Store, rather than being a top level API call.
321 */
322 e->store()->diskFull();
323 storeConfigure();
324 }
325
326 if (e->swap_filen >= 0)
327 e->unlink();
328
329 assert(e->swap_status == SWAPOUT_NONE);
330
331 e->releaseRequest();
332 } else {
333 /* swapping complete */
334 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
335 e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
336 std::uppercase << e->swap_filen);
337 debugs(20, 5, HERE << "swap_file_sz = " <<
338 e->objectLen() << " + " << mem->swap_hdr_sz);
339
340 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
341 e->swap_status = SWAPOUT_DONE;
342 e->store()->swappedOut(*e);
343
344 // XXX: For some Stores, it is pointless to re-check cachability here
345 // and it leads to double counts in store_check_cachable_hist. We need
346 // another way to signal a completed but failed swapout. Or, better,
347 // each Store should handle its own logging and LOG state setting.
348 if (e->checkCachable()) {
349 storeLog(STORE_LOG_SWAPOUT, e);
350 storeDirSwapLog(e, SWAP_LOG_ADD);
351 }
352
353 ++statCounter.swap.outs;
354 }
355
356 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
357 mem->swapout.sio = NULL;
358 e->unlock();
359 }
360
361 bool
362 StoreEntry::mayStartSwapOut()
363 {
364 dlink_node *node;
365
366 // must be checked in the caller
367 assert(!EBIT_TEST(flags, ENTRY_ABORTED));
368 assert(!swappingOut());
369
370 if (!Config.cacheSwap.n_configured)
371 return false;
372
373 assert(mem_obj);
374 MemObject::SwapOut::Decision &decision = mem_obj->swapout.decision;
375
376 // if we decided that swapout is not possible, do not repeat same checks
377 if (decision == MemObject::SwapOut::swImpossible) {
378 debugs(20, 3, HERE << " already rejected");
379 return false;
380 }
381
382 // if we decided that swapout is possible, do not repeat same checks
383 if (decision == MemObject::SwapOut::swPossible) {
384 debugs(20, 3, HERE << "already allowed");
385 return true;
386 }
387
388 // if we swapped out already, do not start over
389 if (swap_status == SWAPOUT_DONE) {
390 debugs(20, 3, HERE << "already did");
391 decision = MemObject::SwapOut::swImpossible;
392 return false;
393 }
394
395 if (!checkCachable()) {
396 debugs(20, 3, HERE << "not cachable");
397 decision = MemObject::SwapOut::swImpossible;
398 return false;
399 }
400
401 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
402 debugs(20, 3, HERE << url() << " SPECIAL");
403 decision = MemObject::SwapOut::swImpossible;
404 return false;
405 }
406
407 // check cache_dir max-size limit if all cache_dirs have it
408 if (store_maxobjsize >= 0) {
409 // TODO: add estimated store metadata size to be conservative
410
411 // use guaranteed maximum if it is known
412 const int64_t expectedEnd = mem_obj->expectedReplySize();
413 debugs(20, 7, HERE << "expectedEnd = " << expectedEnd);
414 if (expectedEnd > store_maxobjsize) {
415 debugs(20, 3, HERE << "will not fit: " << expectedEnd <<
416 " > " << store_maxobjsize);
417 decision = MemObject::SwapOut::swImpossible;
418 return false; // known to outgrow the limit eventually
419 }
420
421 // use current minimum (always known)
422 const int64_t currentEnd = mem_obj->endOffset();
423 if (currentEnd > store_maxobjsize) {
424 debugs(20, 3, HERE << "does not fit: " << currentEnd <<
425 " > " << store_maxobjsize);
426 decision = MemObject::SwapOut::swImpossible;
427 return false; // already does not fit and may only get bigger
428 }
429
430 // prevent default swPossible answer for yet unknown length
431 if (expectedEnd < 0) {
432 debugs(20, 3, HERE << "wait for more info: " <<
433 store_maxobjsize);
434 return false; // may fit later, but will be rejected now
435 }
436
437 if (store_status != STORE_OK) {
438 const int64_t maxKnownSize = expectedEnd < 0 ?
439 mem_obj->availableForSwapOut() : expectedEnd;
440 debugs(20, 7, HERE << "maxKnownSize= " << maxKnownSize);
441 if (maxKnownSize < store_maxobjsize) {
442 /*
443 * NOTE: the store_maxobjsize here is the max of optional
444 * max-size values from 'cache_dir' lines. It is not the
445 * same as 'maximum_object_size'. By default, store_maxobjsize
446 * will be set to -1. However, I am worried that this
447 * deferance may consume a lot of memory in some cases.
448 * Should we add an option to limit this memory consumption?
449 */
450 debugs(20, 5, HERE << "Deferring swapout start for " <<
451 (store_maxobjsize - maxKnownSize) << " bytes");
452 return false;
453 }
454 }
455 }
456
457 if (mem_obj->inmem_lo > 0) {
458 debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
459 decision = MemObject::SwapOut::swImpossible;
460 return false;
461 }
462
463 /*
464 * If there are DISK clients, we must write to disk
465 * even if its not cachable
466 * RBC: Surely we should not create disk client on non cacheable objects?
467 * therefore this should be an assert?
468 * RBC 20030708: We can use disk to avoid mem races, so this shouldn't be
469 * an assert.
470 *
471 * XXX: Not clear what "mem races" the above refers to, especially when
472 * dealing with non-cachable objects that cannot have multiple clients.
473 *
474 * XXX: If STORE_DISK_CLIENT needs SwapOut::swPossible, we have to check
475 * for that flag earlier, but forcing swapping may contradict max-size or
476 * other swapability restrictions. Change storeClientType() and/or its
477 * callers to take swap-in availability into account.
478 */
479 for (node = mem_obj->clients.head; node; node = node->next) {
480 if (((store_client *) node->data)->getType() == STORE_DISK_CLIENT) {
481 debugs(20, 3, HERE << "DISK client found");
482 decision = MemObject::SwapOut::swPossible;
483 return true;
484 }
485 }
486
487 if (!mem_obj->isContiguous()) {
488 debugs(20, 3, "storeSwapOut: not Contiguous");
489 decision = MemObject::SwapOut::swImpossible;
490 return false;
491 }
492
493 decision = MemObject::SwapOut::swPossible;
494 return true;
495 }