]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_swapout.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / store_swapout.cc
CommitLineData
9cef6668 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
9cef6668 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9cef6668 7 */
8
bbc27441
AJ
9/* DEBUG: section 20 Storage Manager Swapout Functions */
10
582c2af2 11#include "squid.h"
aa839030 12#include "cbdata.h"
af69c635 13#include "globals.h"
e6ccf245 14#include "Store.h"
602d9612 15#include "StoreClient.h"
528b2c61 16/* FIXME: Abstract the use of this more */
17#include "mem_node.h"
18#include "MemObject.h"
4d5904f7 19#include "SquidConfig.h"
e4f1fdae 20#include "StatCounters.h"
10818c0a 21#include "store_log.h"
4b981814 22#include "swap_log_op.h"
602d9612 23#include "SwapDir.h"
f09f5b26 24
d54f0ab3 25static void storeSwapOutStart(StoreEntry * e);
4fcc8876 26static StoreIOState::STIOCB storeSwapOutFileClosed;
27static StoreIOState::STFNCB storeSwapOutFileNotify;
f09f5b26 28
daacd51f
AJ
29// wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
30static void xfree_cppwrapper(void *x)
31{
32 xfree(x);
33}
34
f09f5b26 35/* start swapping object to disk */
d54f0ab3 36static void
f09f5b26 37storeSwapOutStart(StoreEntry * e)
38{
2391a162 39 MemObject *mem = e->mem_obj;
d3b3ab85 40 StoreIOState::Pointer sio;
2391a162 41 assert(mem);
cd748f27 42 /* Build the swap metadata, so the filesystem will know how much
43 * metadata there is to store
44 */
26ac0430
AJ
45 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e->url() << "' to dirno " <<
46 e->swap_dirn << ", fileno " << std::hex << std::setw(8) << std::setfill('0') <<
bf8fe701 47 std::uppercase << e->swap_filen);
cd748f27 48 e->swap_status = SWAPOUT_WRITING;
0cdcf3d7 49 e->swapOutDecision(MemObject::SwapOut::swStarted);
528b2c61 50 /* If we start swapping out objects with OutOfBand Metadata,
51 * then this code needs changing
52 */
246e6cc1
AJ
53
54 /* TODO: make some sort of data,size refcounted immutable buffer
55 * and stop fooling ourselves with "const char*" buffers.
56 */
57
58 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
59 const char *buf = e->getSerialisedMetaData ();
60 assert(buf);
61
cd748f27 62 /* Create the swap file */
aa839030 63 generic_cbdata *c = new generic_cbdata(e);
fa80a8ef 64 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
62e76326 65
85a4b153 66 if (sio == NULL) {
62e76326 67 e->swap_status = SWAPOUT_NONE;
0cdcf3d7 68 e->swapOutDecision(MemObject::SwapOut::swImpossible);
aa839030 69 delete c;
246e6cc1 70 xfree((char*)buf);
62e76326 71 storeLog(STORE_LOG_SWAPOUTFAIL, e);
72 return;
3fc29499 73 }
62e76326 74
528b2c61 75 mem->swapout.sio = sio;
76 /* Don't lock until after create, or the replacement
77 * code might get confused */
34266cde 78
1bfe9ade 79 e->lock("storeSwapOutStart");
cd748f27 80 /* Pick up the file number if it was assigned immediately */
81 e->swap_filen = mem->swapout.sio->swap_filen;
34266cde 82
cd748f27 83 e->swap_dirn = mem->swapout.sio->swap_dirn;
34266cde 84
cd748f27 85 /* write out the swap metadata */
daacd51f 86 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree_cppwrapper);
f09f5b26 87}
88
cd748f27 89static void
e5de8b13 90storeSwapOutFileNotify(void *data, int errflag, StoreIOState::Pointer self)
cd748f27 91{
e6ccf245 92 generic_cbdata *c = (generic_cbdata *)data;
93 StoreEntry *e = (StoreEntry *)c->data;
cd748f27 94 MemObject *mem = e->mem_obj;
95 assert(e->swap_status == SWAPOUT_WRITING);
96 assert(mem);
84177444 97 assert(mem->swapout.sio == self);
cd748f27 98 assert(errflag == 0);
f58bb2f4 99 assert(e->swap_filen < 0); // if this fails, call SwapDir::disconnect(e)
cd748f27 100 e->swap_filen = mem->swapout.sio->swap_filen;
101 e->swap_dirn = mem->swapout.sio->swap_dirn;
102}
103
90e8b325 104static bool
528b2c61 105doPages(StoreEntry *anEntry)
106{
107 MemObject *mem = anEntry->mem_obj;
62e76326 108
528b2c61 109 do {
aa1a691e
AR
110 // find the page containing the first byte we have not swapped out yet
111 mem_node *page =
112 mem->data_hdr.getBlockContainingLocation(mem->swapout.queue_offset);
62e76326 113
aa1a691e 114 if (!page)
90e8b325 115 break; // wait for more data to become available
aa1a691e
AR
116
117 // memNodeWriteComplete() and absence of buffer offset math below
118 // imply that we always write from the very beginning of the page
119 assert(page->start() == mem->swapout.queue_offset);
62e76326 120
121 /*
122 * Get the length of this buffer. We are assuming(!) that the buffer
123 * length won't change on this buffer, or things are going to be very
124 * strange. I think that after the copy to a buffer is done, the buffer
125 * size should stay fixed regardless so that this code isn't confused,
126 * but we can look at this at a later date or whenever the code results
127 * in bad swapouts, whichever happens first. :-)
128 */
aa1a691e 129 ssize_t swap_buf_len = page->nodeBuffer.length;
62e76326 130
4a7a3d56 131 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
62e76326 132
133 assert(swap_buf_len > 0);
134
4a7a3d56 135 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
62e76326 136
137 mem->swapout.queue_offset += swap_buf_len;
138
90e8b325
AR
139 // Quit if write() fails. Sio is going to call our callback, and that
140 // will cleanup, but, depending on the fs, that call may be async.
141 const bool ok = mem->swapout.sio->write(
9d4e9cfb
AR
142 mem->data_hdr.NodeGet(page),
143 swap_buf_len,
144 -1,
145 memNodeWriteComplete);
62e76326 146
90e8b325
AR
147 if (!ok || anEntry->swap_status != SWAPOUT_WRITING)
148 return false;
62e76326 149
47f6e231 150 int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
62e76326 151
152 if (anEntry->store_status == STORE_PENDING)
153 if (swapout_size < SM_PAGE_SIZE)
154 break;
155
156 if (swapout_size <= 0)
90e8b325 157 break;
528b2c61 158 } while (true);
90e8b325
AR
159
160 // either wait for more data or call swapOutFileClose()
161 return true;
528b2c61 162}
163
528b2c61 164/* This routine is called every time data is sent to the client side.
165 * It's overhead is therefor, significant.
166 */
f09f5b26 167void
c07cbbf4 168StoreEntry::swapOut()
f09f5b26 169{
c07cbbf4 170 if (!mem_obj)
62e76326 171 return;
172
5b55f1f1
CT
173 // this flag may change so we must check even if we are swappingOut
174 if (EBIT_TEST(flags, ENTRY_ABORTED)) {
175 assert(EBIT_TEST(flags, RELEASE_REQUEST));
176 // StoreEntry::abort() already closed the swap out file, if any
177 // no trimming: data producer must stop production if ENTRY_ABORTED
62e76326 178 return;
5b55f1f1
CT
179 }
180
181 const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
55bfc9fc 182
4475555f 183 Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut);
5b55f1f1 184
2d4dea47 185 if (mem_obj->swapout.decision < MemObject::SwapOut::swPossible)
5b55f1f1 186 return; // nothing else to do
528b2c61 187
aa1a691e
AR
188 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
189 // store_status == STORE_OK below means we got everything we wanted.
190
47f6e231 191 debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
192 debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
193 debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
62e76326 194
85a4b153 195 if (mem_obj->swapout.sio != NULL)
26ac0430 196 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
62e76326 197
47f6e231 198 int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
62e76326 199
47f6e231 200 debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
62e76326 201
5aecb102 202#if SIZEOF_OFF_T <= 4
62e76326 203
c07cbbf4 204 if (mem_obj->endOffset() > 0x7FFF0000) {
fa84c01d 205 debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
bfb55b6f 206 abort();
62e76326 207 return;
adcceb47 208 }
62e76326 209
adcceb47 210#endif
c07cbbf4 211 if (swap_status == SWAPOUT_WRITING)
47f6e231 212 assert(mem_obj->inmem_lo <= mem_obj->objectBytesOnDisk() );
62e76326 213
5b55f1f1
CT
214 // buffered bytes we have not swapped out yet
215 const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
216 assert(swapout_maxsize >= 0);
4a7a3d56 217 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
62e76326 218
aa1a691e
AR
219 if (swapout_maxsize == 0) { // swapped everything we got
220 if (store_status == STORE_OK) { // got everything we wanted
221 assert(mem_obj->object_sz >= 0);
222 swapOutFileClose(StoreIOState::wroteAll);
223 }
224 // else need more data to swap out
225 return;
1b72bda1 226 }
62e76326 227
c07cbbf4 228 if (store_status == STORE_PENDING) {
62e76326 229 /* wait for a full block to write */
230
231 if (swapout_maxsize < SM_PAGE_SIZE)
232 return;
233
234 /*
235 * Wait until we are below the disk FD limit, only if the
d5430dc8 236 * next read won't be deferred.
62e76326 237 */
c07cbbf4 238 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
62e76326 239 return;
c47511fd 240 }
62e76326 241
2391a162 242 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
c07cbbf4 243 if (swap_status == SWAPOUT_NONE) {
244 assert(mem_obj->swapout.sio == NULL);
245 assert(mem_obj->inmem_lo == 0);
ddc9b32c 246 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
f09f5b26 247 }
62e76326 248
c07cbbf4 249 if (mem_obj->swapout.sio == NULL)
62e76326 250 return;
251
90e8b325 252 if (!doPages(this))
62e76326 253 /* oops, we're not swapping out any more */
254 return;
255
c07cbbf4 256 if (store_status == STORE_OK) {
62e76326 257 /*
258 * If the state is STORE_OK, then all data must have been given
259 * to the filesystem at this point because storeSwapOut() is
260 * not going to be called again for this entry.
261 */
aa1a691e 262 assert(mem_obj->object_sz >= 0);
c07cbbf4 263 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
aa1a691e 264 swapOutFileClose(StoreIOState::wroteAll);
1792fbdb 265 }
f09f5b26 266}
267
268void
aa1a691e 269StoreEntry::swapOutFileClose(int how)
f09f5b26 270{
c07cbbf4 271 assert(mem_obj != NULL);
aa1a691e 272 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
bf8fe701 273 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
62e76326 274
c07cbbf4 275 if (mem_obj->swapout.sio == NULL)
62e76326 276 return;
277
aa1a691e 278 storeClose(mem_obj->swapout.sio, how);
f09f5b26 279}
280
281static void
e5de8b13 282storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
f09f5b26 283{
e6ccf245 284 generic_cbdata *c = (generic_cbdata *)data;
285 StoreEntry *e = (StoreEntry *)c->data;
25354045 286 MemObject *mem = e->mem_obj;
84177444 287 assert(mem->swapout.sio == self);
2391a162 288 assert(e->swap_status == SWAPOUT_WRITING);
289 cbdataFree(c);
62e76326 290
aa1a691e
AR
291 // if object_size is still unknown, the entry was probably aborted
292 if (errflag || e->objectLen() < 0) {
de4bc0ea 293 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
26ac0430 294 std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
bf8fe701 295 e->swap_filen << ", errflag=" << errflag);
62e76326 296
297 if (errflag == DISK_NO_SPACE_LEFT) {
c8f4eac4 298 /* FIXME: this should be handle by the link from store IO to
299 * Store, rather than being a top level API call.
300 */
301 e->store()->diskFull();
62e76326 302 storeConfigure();
303 }
304
fa192c71 305 if (e->swap_filen >= 0)
c8f4eac4 306 e->unlink();
62e76326 307
f58bb2f4 308 assert(e->swap_status == SWAPOUT_NONE);
62e76326 309
d88e3c49 310 e->releaseRequest();
2391a162 311 } else {
62e76326 312 /* swapping complete */
26ac0430
AJ
313 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
314 e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
bf8fe701 315 std::uppercase << e->swap_filen);
aa1a691e
AR
316 debugs(20, 5, HERE << "swap_file_sz = " <<
317 e->objectLen() << " + " << mem->swap_hdr_sz);
8061c9b2 318
707fdc47 319 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
62e76326 320 e->swap_status = SWAPOUT_DONE;
da9d3191 321 e->store()->swappedOut(*e);
62e76326 322
ddc9b32c
AR
323 // XXX: For some Stores, it is pointless to re-check cachability here
324 // and it leads to double counts in store_check_cachable_hist. We need
325 // another way to signal a completed but failed swapout. Or, better,
326 // each Store should handle its own logging and LOG state setting.
3900307b 327 if (e->checkCachable()) {
62e76326 328 storeLog(STORE_LOG_SWAPOUT, e);
329 storeDirSwapLog(e, SWAP_LOG_ADD);
330 }
331
e4f1fdae 332 ++statCounter.swap.outs;
f09f5b26 333 }
62e76326 334
bf8fe701 335 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
d3b3ab85 336 mem->swapout.sio = NULL;
1bfe9ade 337 e->unlock("storeSwapOutFileClosed");
f09f5b26 338}
61038223 339
c07cbbf4 340bool
5b55f1f1 341StoreEntry::mayStartSwapOut()
c2725718 342{
5b55f1f1
CT
343 // must be checked in the caller
344 assert(!EBIT_TEST(flags, ENTRY_ABORTED));
4094b311 345 assert(!swappingOut());
5b55f1f1
CT
346
347 if (!Config.cacheSwap.n_configured)
348 return false;
349
350 assert(mem_obj);
0cdcf3d7 351 const MemObject::SwapOut::Decision &decision = mem_obj->swapout.decision;
5b55f1f1 352
0e3b2ff0 353 // if we decided that starting is not possible, do not repeat same checks
5b55f1f1
CT
354 if (decision == MemObject::SwapOut::swImpossible) {
355 debugs(20, 3, HERE << " already rejected");
356 return false;
357 }
358
4094b311
AR
359 // if we swapped out already, do not start over
360 if (swap_status == SWAPOUT_DONE) {
539283df 361 debugs(20, 3, "already did");
0cdcf3d7 362 swapOutDecision(MemObject::SwapOut::swImpossible);
4094b311 363 return false;
5b55f1f1
CT
364 }
365
0e3b2ff0
AR
366 // if we stared swapping out already, do not start over
367 if (decision == MemObject::SwapOut::swStarted) {
368 debugs(20, 3, "already started");
0cdcf3d7 369 swapOutDecision(MemObject::SwapOut::swImpossible);
0e3b2ff0
AR
370 return false;
371 }
372
373 // if we decided that swapout is possible, do not repeat same checks
374 if (decision == MemObject::SwapOut::swPossible) {
375 debugs(20, 3, "already allowed");
376 return true;
377 }
378
5b55f1f1
CT
379 if (!checkCachable()) {
380 debugs(20, 3, HERE << "not cachable");
0cdcf3d7 381 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
382 return false;
383 }
384
385 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
386 debugs(20, 3, HERE << url() << " SPECIAL");
0cdcf3d7 387 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
388 return false;
389 }
390
e0397786
AR
391 if (mem_obj->inmem_lo > 0) {
392 debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
0cdcf3d7 393 swapOutDecision(MemObject::SwapOut::swImpossible);
e0397786
AR
394 return false;
395 }
396
397 if (!mem_obj->isContiguous()) {
398 debugs(20, 3, "storeSwapOut: not Contiguous");
0cdcf3d7 399 swapOutDecision(MemObject::SwapOut::swImpossible);
e0397786
AR
400 return false;
401 }
402
3c856e95
AR
403 // handle store_maxobjsize limit
404 {
5b55f1f1
CT
405 // TODO: add estimated store metadata size to be conservative
406
407 // use guaranteed maximum if it is known
408 const int64_t expectedEnd = mem_obj->expectedReplySize();
409 debugs(20, 7, HERE << "expectedEnd = " << expectedEnd);
410 if (expectedEnd > store_maxobjsize) {
411 debugs(20, 3, HERE << "will not fit: " << expectedEnd <<
412 " > " << store_maxobjsize);
0cdcf3d7 413 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
414 return false; // known to outgrow the limit eventually
415 }
416
417 // use current minimum (always known)
418 const int64_t currentEnd = mem_obj->endOffset();
419 if (currentEnd > store_maxobjsize) {
420 debugs(20, 3, HERE << "does not fit: " << currentEnd <<
421 " > " << store_maxobjsize);
0cdcf3d7 422 swapOutDecision(MemObject::SwapOut::swImpossible);
5b55f1f1
CT
423 return false; // already does not fit and may only get bigger
424 }
425
e0397786
AR
426 // prevent final default swPossible answer for yet unknown length
427 if (expectedEnd < 0 && store_status != STORE_OK) {
428 const int64_t maxKnownSize = mem_obj->availableForSwapOut();
5b55f1f1 429 debugs(20, 7, HERE << "maxKnownSize= " << maxKnownSize);
2ee34428 430 /*
b51ec8c8
AJ
431 * NOTE: the store_maxobjsize here is the global maximum
432 * size of object cacheable in any of Squid cache stores
433 * both disk and memory stores.
434 *
435 * However, I am worried that this
2ee34428
A
436 * deferance may consume a lot of memory in some cases.
437 * Should we add an option to limit this memory consumption?
438 */
439 debugs(20, 5, HERE << "Deferring swapout start for " <<
440 (store_maxobjsize - maxKnownSize) << " bytes");
e0397786 441 return true; // may still fit, but no final decision yet
5b55f1f1 442 }
06d2839d 443 }
62e76326 444
0cdcf3d7 445 swapOutDecision(MemObject::SwapOut::swPossible);
c07cbbf4 446 return true;
c2725718 447}
f53969cc 448