2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 20 Storage Manager Swapout Functions */
13 #include "CollapsedForwarding.h"
16 #include "StoreClient.h"
17 // TODO: Abstract the use of this more
19 #include "MemObject.h"
20 #include "SquidConfig.h"
21 #include "StatCounters.h"
22 #include "store/Disk.h"
23 #include "store/Disks.h"
24 #include "store_log.h"
25 #include "swap_log_op.h"
27 static void storeSwapOutStart(StoreEntry
* e
);
28 static StoreIOState::STIOCB storeSwapOutFileClosed
;
29 static StoreIOState::STFNCB storeSwapOutFileNotify
;
31 // wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
32 static void xfree_cppwrapper(void *x
)
37 /* start swapping object to disk */
39 storeSwapOutStart(StoreEntry
* e
)
41 MemObject
*mem
= e
->mem_obj
;
42 StoreIOState::Pointer sio
;
44 /* Build the swap metadata, so the filesystem will know how much
45 * metadata there is to store
47 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e
->url() << "' to dirno " <<
48 e
->swap_dirn
<< ", fileno " << std::hex
<< std::setw(8) << std::setfill('0') <<
49 std::uppercase
<< e
->swap_filen
);
50 e
->swapOutDecision(MemObject::SwapOut::swStarted
);
51 /* If we start swapping out objects with OutOfBand Metadata,
52 * then this code needs changing
55 /* TODO: make some sort of data,size refcounted immutable buffer
56 * and stop fooling ourselves with "const char*" buffers.
59 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
60 const auto buf
= e
->getSerialisedMetaData(mem
->swap_hdr_sz
);
63 /* Create the swap file */
64 generic_cbdata
*c
= new generic_cbdata(e
);
65 sio
= storeCreate(e
, storeSwapOutFileNotify
, storeSwapOutFileClosed
, c
);
68 assert(!e
->hasDisk());
69 e
->swap_status
= SWAPOUT_NONE
;
70 e
->swapOutDecision(MemObject::SwapOut::swImpossible
);
73 storeLog(STORE_LOG_SWAPOUTFAIL
, e
);
77 mem
->swapout
.sio
= sio
;
78 /* Don't lock until after create, or the replacement
79 * code might get confused */
81 e
->lock("storeSwapOutStart");
82 /* Pick up the file number if it was assigned immediately */
83 e
->attachToDisk(mem
->swapout
.sio
->swap_dirn
, mem
->swapout
.sio
->swap_filen
, SWAPOUT_WRITING
);
85 /* write out the swap metadata */
86 storeIOWrite(mem
->swapout
.sio
, buf
, mem
->swap_hdr_sz
, 0, xfree_cppwrapper
);
89 /// XXX: unused, see a related StoreIOState::file_callback
91 storeSwapOutFileNotify(void *, int, StoreIOState::Pointer
)
97 doPages(StoreEntry
*anEntry
)
99 MemObject
*mem
= anEntry
->mem_obj
;
102 // find the page containing the first byte we have not swapped out yet
104 mem
->data_hdr
.getBlockContainingLocation(mem
->swapout
.queue_offset
);
107 break; // wait for more data to become available
109 // memNodeWriteComplete() and absence of buffer offset math below
110 // imply that we always write from the very beginning of the page
111 assert(page
->start() == mem
->swapout
.queue_offset
);
114 * Get the length of this buffer. We are assuming(!) that the buffer
115 * length won't change on this buffer, or things are going to be very
116 * strange. I think that after the copy to a buffer is done, the buffer
117 * size should stay fixed regardless so that this code isn't confused,
118 * but we can look at this at a later date or whenever the code results
119 * in bad swapouts, whichever happens first. :-)
121 ssize_t swap_buf_len
= page
->nodeBuffer
.length
;
123 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len
);
125 assert(swap_buf_len
> 0);
127 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len
<< " bytes from " << mem
->swapout
.queue_offset
);
129 mem
->swapout
.queue_offset
+= swap_buf_len
;
131 // Quit if write() fails. Sio is going to call our callback, and that
132 // will cleanup, but, depending on the fs, that call may be async.
133 const bool ok
= mem
->swapout
.sio
->write(
134 mem
->data_hdr
.NodeGet(page
),
137 memNodeWriteComplete
);
139 if (!ok
|| !anEntry
->swappingOut())
142 int64_t swapout_size
= mem
->endOffset() - mem
->swapout
.queue_offset
;
144 if (anEntry
->store_status
== STORE_PENDING
)
145 if (swapout_size
< SM_PAGE_SIZE
)
148 if (swapout_size
<= 0)
152 // either wait for more data or call swapOutFileClose()
156 /* This routine is called every time data is sent to the client side.
157 * It's overhead is therefor, significant.
160 StoreEntry::swapOut()
165 // this flag may change so we must check even if we are swappingOut
166 if (EBIT_TEST(flags
, ENTRY_ABORTED
)) {
167 assert(EBIT_TEST(flags
, RELEASE_REQUEST
));
168 // StoreEntry::abort() already closed the swap out file, if any
169 // no trimming: data producer must stop production if ENTRY_ABORTED
173 const bool weAreOrMayBeSwappingOut
= swappingOut() || mayStartSwapOut();
175 Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut
);
177 if (mem_obj
->swapout
.decision
< MemObject::SwapOut::swPossible
)
178 return; // nothing else to do
180 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
181 // store_status == STORE_OK below means we got everything we wanted.
183 debugs(20, 7, HERE
<< "storeSwapOut: mem->inmem_lo = " << mem_obj
->inmem_lo
);
184 debugs(20, 7, HERE
<< "storeSwapOut: mem->endOffset() = " << mem_obj
->endOffset());
185 debugs(20, 7, HERE
<< "storeSwapOut: swapout.queue_offset = " << mem_obj
->swapout
.queue_offset
);
187 if (mem_obj
->swapout
.sio
!= NULL
)
188 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj
->swapout
.sio
->offset() );
190 int64_t const lowest_offset
= mem_obj
->lowestMemReaderOffset();
192 debugs(20, 7, HERE
<< "storeSwapOut: lowest_offset = " << lowest_offset
);
194 #if SIZEOF_OFF_T <= 4
196 if (mem_obj
->endOffset() > 0x7FFF0000) {
197 debugs(20, DBG_CRITICAL
, "WARNING: preventing off_t overflow for " << url());
204 assert(mem_obj
->inmem_lo
<= mem_obj
->objectBytesOnDisk() );
206 // buffered bytes we have not swapped out yet
207 const int64_t swapout_maxsize
= mem_obj
->availableForSwapOut();
208 assert(swapout_maxsize
>= 0);
209 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize
);
211 if (swapout_maxsize
== 0) { // swapped everything we got
212 if (store_status
== STORE_OK
) { // got everything we wanted
213 assert(mem_obj
->object_sz
>= 0);
214 swapOutFileClose(StoreIOState::wroteAll
);
216 // else need more data to swap out
220 if (store_status
== STORE_PENDING
) {
221 /* wait for a full block to write */
223 if (swapout_maxsize
< SM_PAGE_SIZE
)
227 * Wait until we are below the disk FD limit, only if the
228 * next read won't be deferred.
230 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
234 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
236 assert(mem_obj
->swapout
.sio
== NULL
);
237 assert(mem_obj
->inmem_lo
== 0);
238 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
241 if (mem_obj
->swapout
.sio
== NULL
)
245 /* oops, we're not swapping out any more */
248 if (store_status
== STORE_OK
) {
250 * If the state is STORE_OK, then all data must have been given
251 * to the filesystem at this point because storeSwapOut() is
252 * not going to be called again for this entry.
254 assert(mem_obj
->object_sz
>= 0);
255 assert(mem_obj
->endOffset() == mem_obj
->swapout
.queue_offset
);
256 swapOutFileClose(StoreIOState::wroteAll
);
261 StoreEntry::swapOutFileClose(int how
)
263 assert(mem_obj
!= NULL
);
264 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how
);
265 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj
->swapout
.sio
.getRaw());
267 if (mem_obj
->swapout
.sio
== NULL
)
270 storeClose(mem_obj
->swapout
.sio
, how
);
274 storeSwapOutFileClosed(void *data
, int errflag
, StoreIOState::Pointer self
)
277 static_cast<generic_cbdata
*>(data
)->unwrap(&e
);
279 MemObject
*mem
= e
->mem_obj
;
280 assert(mem
->swapout
.sio
== self
);
281 assert(e
->swappingOut());
283 // if object_size is still unknown, the entry was probably aborted
284 if (errflag
|| e
->objectLen() < 0) {
285 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e
->swap_dirn
<< ", swapfile " <<
286 std::hex
<< std::setw(8) << std::setfill('0') << std::uppercase
<<
287 e
->swap_filen
<< ", errflag=" << errflag
);
289 if (errflag
== DISK_NO_SPACE_LEFT
) {
290 /* TODO: this should be handle by the link from store IO to
291 * Store, rather than being a top level API call.
293 e
->disk().diskFull();
297 // mark the locked entry for deletion
298 // TODO: Keep the memory entry (if any)
300 e
->swap_status
= SWAPOUT_FAILED
;
301 e
->disk().finalizeSwapoutFailure(*e
);
303 /* swapping complete */
304 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e
->url() << "' to " <<
305 e
->swap_dirn
<< ", " << std::hex
<< std::setw(8) << std::setfill('0') <<
306 std::uppercase
<< e
->swap_filen
);
307 debugs(20, 5, HERE
<< "swap_file_sz = " <<
308 e
->objectLen() << " + " << mem
->swap_hdr_sz
);
310 e
->swap_file_sz
= e
->objectLen() + mem
->swap_hdr_sz
;
311 e
->swap_status
= SWAPOUT_DONE
;
312 e
->disk().finalizeSwapoutSuccess(*e
);
314 // XXX: For some Stores, it is pointless to re-check cachability here
315 // and it leads to double counts in store_check_cachable_hist. We need
316 // another way to signal a completed but failed swapout. Or, better,
317 // each Store should handle its own logging and LOG state setting.
318 if (e
->checkCachable()) {
319 storeLog(STORE_LOG_SWAPOUT
, e
);
320 storeDirSwapLog(e
, SWAP_LOG_ADD
);
323 ++statCounter
.swap
.outs
;
326 Store::Root().transientsCompleteWriting(*e
);
327 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__
<< ":" << __LINE__
);
328 mem
->swapout
.sio
= NULL
;
329 e
->unlock("storeSwapOutFileClosed");
333 StoreEntry::mayStartSwapOut()
335 // must be checked in the caller
336 assert(!EBIT_TEST(flags
, ENTRY_ABORTED
));
337 assert(!swappingOut());
339 if (!Config
.cacheSwap
.n_configured
)
343 const MemObject::SwapOut::Decision
&decision
= mem_obj
->swapout
.decision
;
345 // if we decided that starting is not possible, do not repeat same checks
346 if (decision
== MemObject::SwapOut::swImpossible
) {
347 debugs(20, 3, HERE
<< " already rejected");
351 // if we are swapping out or swapped out already, do not start over
352 if (hasDisk() || Store::Root().hasReadableDiskEntry(*this)) {
353 debugs(20, 3, "already did");
354 swapOutDecision(MemObject::SwapOut::swImpossible
);
358 // if we have just stared swapping out (attachToDisk() has not been
359 // called), do not start over
360 if (decision
== MemObject::SwapOut::swStarted
) {
361 debugs(20, 3, "already started");
362 swapOutDecision(MemObject::SwapOut::swImpossible
);
366 if (Store::Root().markedForDeletionAndAbandoned(*this)) {
367 debugs(20, 3, "marked for deletion and abandoned");
368 swapOutDecision(MemObject::SwapOut::swImpossible
);
372 // if we decided that swapout is possible, do not repeat same checks
373 if (decision
== MemObject::SwapOut::swPossible
) {
374 debugs(20, 3, "already allowed");
378 if (!checkCachable()) {
379 debugs(20, 3, HERE
<< "not cachable");
380 swapOutDecision(MemObject::SwapOut::swImpossible
);
384 if (EBIT_TEST(flags
, ENTRY_SPECIAL
)) {
385 debugs(20, 3, HERE
<< url() << " SPECIAL");
386 swapOutDecision(MemObject::SwapOut::swImpossible
);
390 if (mem_obj
->inmem_lo
> 0) {
391 debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj
->inmem_lo
);
392 swapOutDecision(MemObject::SwapOut::swImpossible
);
396 if (!mem_obj
->isContiguous()) {
397 debugs(20, 3, "storeSwapOut: not Contiguous");
398 swapOutDecision(MemObject::SwapOut::swImpossible
);
402 // handle store_maxobjsize limit
404 // TODO: add estimated store metadata size to be conservative
406 // use guaranteed maximum if it is known
407 const int64_t expectedEnd
= mem_obj
->expectedReplySize();
408 debugs(20, 7, HERE
<< "expectedEnd = " << expectedEnd
);
409 if (expectedEnd
> store_maxobjsize
) {
410 debugs(20, 3, HERE
<< "will not fit: " << expectedEnd
<<
411 " > " << store_maxobjsize
);
412 swapOutDecision(MemObject::SwapOut::swImpossible
);
413 return false; // known to outgrow the limit eventually
416 // use current minimum (always known)
417 const int64_t currentEnd
= mem_obj
->endOffset();
418 if (currentEnd
> store_maxobjsize
) {
419 debugs(20, 3, HERE
<< "does not fit: " << currentEnd
<<
420 " > " << store_maxobjsize
);
421 swapOutDecision(MemObject::SwapOut::swImpossible
);
422 return false; // already does not fit and may only get bigger
425 // prevent final default swPossible answer for yet unknown length
426 if (expectedEnd
< 0 && store_status
!= STORE_OK
) {
427 const int64_t more
= Store::Root().accumulateMore(*this);
429 debugs(20, 5, "got " << currentEnd
<< "; defer decision for " << more
<< " more bytes");
430 return true; // may still fit, but no final decision yet
435 swapOutDecision(MemObject::SwapOut::swPossible
);