store_swapout.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/* DEBUG: section 20 Storage Manager Swapout Functions */
10
11#include "squid.h"
12#include "cbdata.h"
13#include "CollapsedForwarding.h"
14#include "globals.h"
15#include "Store.h"
16#include "StoreClient.h"
17// TODO: Abstract the use of this more
18#include "mem_node.h"
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"
26
27static void storeSwapOutStart(StoreEntry * e);
30
31// wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
32static void xfree_cppwrapper(void *x)
33{
34 xfree(x);
35}
36
37/* start swapping object to disk */
38static void
40{
41 MemObject *mem = e->mem_obj;
43 assert(mem);
44 /* Build the swap metadata, so the filesystem will know how much
45 * metadata there is to store
46 */
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 /* If we start swapping out objects with OutOfBand Metadata,
51 * then this code needs changing
52 */
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 auto buf = e->getSerialisedMetaData(mem->swap_hdr_sz);
60 assert(buf);
61
62 /* Create the swap file */
65
66 if (sio == nullptr) {
67 assert(!e->hasDisk());
70 delete c;
71 xfree((char*)buf);
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("storeSwapOutStart");
81 /* Pick up the file number if it was assigned immediately */
83
84 e->swapOutDecision(MemObject::SwapOut::swStarted); // after SWAPOUT_WRITING
85
86 /* write out the swap metadata */
88}
89
91static void
93{
94 assert(false);
95}
96
97static bool
99{
100 MemObject *mem = anEntry->mem_obj;
101
102 do {
103 // find the page containing the first byte we have not swapped out yet
104 mem_node *page =
106
107 if (!page)
108 break; // wait for more data to become available
109
110 // memNodeWriteComplete() and absence of buffer offset math below
111 // imply that we always write from the very beginning of the page
112 assert(page->start() == mem->swapout.queue_offset);
113
114 /*
115 * Get the length of this buffer. We are assuming(!) that the buffer
116 * length won't change on this buffer, or things are going to be very
117 * strange. I think that after the copy to a buffer is done, the buffer
118 * size should stay fixed regardless so that this code isn't confused,
119 * but we can look at this at a later date or whenever the code results
120 * in bad swapouts, whichever happens first. :-)
121 */
122 ssize_t swap_buf_len = page->nodeBuffer.length;
123
124 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
125
126 assert(swap_buf_len > 0);
127
128 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
129
130 mem->swapout.queue_offset += swap_buf_len;
131
132 // Quit if write() fails. Sio is going to call our callback, and that
133 // will cleanup, but, depending on the fs, that call may be async.
134 const bool ok = mem->swapout.sio->write(
135 mem->data_hdr.NodeGet(page),
136 swap_buf_len,
137 -1,
139
140 if (!ok || !anEntry->swappingOut())
141 return false;
142
143 int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
144
145 if (anEntry->store_status == STORE_PENDING)
146 if (swapout_size < SM_PAGE_SIZE)
147 break;
148
149 if (swapout_size <= 0)
150 break;
151 } while (true);
152
153 // either wait for more data or call swapOutFileClose()
154 return true;
155}
156
157/* This routine is called every time data is sent to the client side.
158 * It's overhead is therefor, significant.
159 */
160void
162{
163 if (!mem_obj)
164 return;
165
166 // this flag may change so we must check even if we are swappingOut
169 // StoreEntry::abort() already closed the swap out file, if any
170 // no trimming: data producer must stop production if ENTRY_ABORTED
171 return;
172 }
173
174 const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
175
176 Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut);
177
179 return; // decided not to write to disk (at least for now)
180
181 if (!weAreOrMayBeSwappingOut)
182 return; // finished writing to disk after an earlier swStarted decision
183
184 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
185 // store_status == STORE_OK below means we got everything we wanted.
186
187 debugs(20, 7, "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
188 debugs(20, 7, "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
189 debugs(20, 7, "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
190
191 if (mem_obj->swapout.sio != nullptr)
192 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
193
194 int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
195
196 debugs(20, 7, "storeSwapOut: lowest_offset = " << lowest_offset);
197
198#if SIZEOF_OFF_T <= 4
199
200 if (mem_obj->endOffset() > 0x7FFF0000) {
201 debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
202 abort();
203 return;
204 }
205
206#endif
207 if (swappingOut())
209
210 // buffered bytes we have not swapped out yet
211 const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
212 assert(swapout_maxsize >= 0);
213 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
214
215 if (swapout_maxsize == 0) { // swapped everything we got
216 if (store_status == STORE_OK) { // got everything we wanted
217 assert(mem_obj->object_sz >= 0);
219 }
220 // else need more data to swap out
221 return;
222 }
223
225 /* wait for a full block to write */
226
227 if (swapout_maxsize < SM_PAGE_SIZE)
228 return;
229
230 /*
231 * Wait until we are below the disk FD limit, only if the
232 * next read won't be deferred.
233 */
235 return;
236 }
237
238 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
239 if (!hasDisk()) {
240 assert(mem_obj->swapout.sio == nullptr);
241 assert(mem_obj->inmem_lo == 0);
242 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
243 }
244
245 if (mem_obj->swapout.sio == nullptr)
246 return;
247
248 if (!doPages(this))
249 /* oops, we're not swapping out any more */
250 return;
251
252 if (store_status == STORE_OK) {
253 /*
254 * If the state is STORE_OK, then all data must have been given
255 * to the filesystem at this point because storeSwapOut() is
256 * not going to be called again for this entry.
257 */
258 assert(mem_obj->object_sz >= 0);
261 }
262}
263
264void
266{
267 assert(mem_obj != nullptr);
268 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
269 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
270
271 if (mem_obj->swapout.sio == nullptr)
272 return;
273
275}
276
277static void
278storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
279{
280 StoreEntry *e;
281 static_cast<generic_cbdata *>(data)->unwrap(&e);
282
283 MemObject *mem = e->mem_obj;
284 assert(mem->swapout.sio == self);
285 assert(e->swappingOut());
286
287 // if object_size is still unknown, the entry was probably aborted
288 if (errflag || e->objectLen() < 0) {
289 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
290 std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
291 e->swap_filen << ", errflag=" << errflag);
292
293 if (errflag == DISK_NO_SPACE_LEFT) {
294 /* TODO: this should be handle by the link from store IO to
295 * Store, rather than being a top level API call.
296 */
297 e->disk().diskFull();
299 }
300
301 // mark the locked entry for deletion
302 // TODO: Keep the memory entry (if any)
303 e->releaseRequest();
306 } else {
307 /* swapping complete */
308 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
309 e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
310 std::uppercase << e->swap_filen);
311 debugs(20, 5, "swap_file_sz = " <<
312 e->objectLen() << " + " << mem->swap_hdr_sz);
313
314 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
317
318 // XXX: For some Stores, it is pointless to re-check cachability here
319 // and it leads to double counts in store_check_cachable_hist. We need
320 // another way to signal a completed but failed swapout. Or, better,
321 // each Store should handle its own logging and LOG state setting.
322 if (e->checkCachable()) {
325 }
326
328 }
329
330 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
331 mem->swapout.sio = nullptr;
332 e->storeWriterDone(); // after updating swap_status
333 e->unlock("storeSwapOutFileClosed");
334}
335
336bool
338{
339 // must be checked in the caller
342
344 return false;
345
348
349 // if we decided that starting is not possible, do not repeat same checks
350 if (decision == MemObject::SwapOut::swImpossible) {
351 debugs(20, 3, " already rejected");
352 return false;
353 }
354
355 // If we have started swapping out, do not start over. Most likely, we have
356 // finished swapping out by now because we are not currently swappingOut().
357 if (decision == MemObject::SwapOut::swStarted) {
358 debugs(20, 3, "already started");
359 return false;
360 }
361
362 if (shutting_down) {
363 debugs(20, 3, "avoid heavy optional work during shutdown");
365 return false;
366 }
367
368 // if there is a usable disk entry already, do not start over
369 if (hasDisk() || Store::Root().hasReadableDiskEntry(*this)) {
370 debugs(20, 3, "already did"); // we or somebody else created that entry
372 return false;
373 }
374
375 if (Store::Root().markedForDeletionAndAbandoned(*this)) {
376 debugs(20, 3, "marked for deletion and abandoned");
378 return false;
379 }
380
381 // if we decided that swapout is possible, do not repeat same checks
382 if (decision == MemObject::SwapOut::swPossible) {
383 debugs(20, 3, "already allowed");
384 return true;
385 }
386
387 // To avoid SMP workers releasing each other caching attempts, restrict disk
388 // caching to StoreEntry publisher. This check goes before checkCachable()
389 // that may incorrectly release() publisher's entry.
390 if (Store::Root().transientsReader(*this)) {
391 debugs(20, 5, "yield to entry publisher");
393 return false;
394 }
395
396 if (!checkCachable()) {
397 debugs(20, 3, "not cachable");
399 return false;
400 }
401
403 debugs(20, 3, url() << " SPECIAL");
405 return false;
406 }
407
408 if (mem_obj->inmem_lo > 0) {
409 debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
411 return false;
412 }
413
414 if (!mem_obj->isContiguous()) {
415 debugs(20, 3, "storeSwapOut: not Contiguous");
417 return false;
418 }
419
420 // handle store_maxobjsize limit
421 {
422 // TODO: add estimated store metadata size to be conservative
423
424 // use guaranteed maximum if it is known
425 const int64_t expectedEnd = mem_obj->expectedReplySize();
426 debugs(20, 7, "expectedEnd = " << expectedEnd);
427 if (expectedEnd > store_maxobjsize) {
428 debugs(20, 3, "will not fit: " << expectedEnd <<
429 " > " << store_maxobjsize);
431 return false; // known to outgrow the limit eventually
432 }
433
434 // use current minimum (always known)
435 const int64_t currentEnd = mem_obj->endOffset();
436 if (currentEnd > store_maxobjsize) {
437 debugs(20, 3, "does not fit: " << currentEnd <<
438 " > " << store_maxobjsize);
440 return false; // already does not fit and may only get bigger
441 }
442
443 // prevent final default swPossible answer for yet unknown length
444 if (expectedEnd < 0 && store_status != STORE_OK) {
445 const int64_t more = Store::Root().accumulateMore(*this);
446 if (more > 0) {
447 debugs(20, 5, "got " << currentEnd << "; defer decision for " << more << " more bytes");
448 return true; // may still fit, but no final decision yet
449 }
450 }
451 }
452
454 return true;
455}
456
void storeDirSwapLog(const StoreEntry *e, int op)
Definition: Disks.cc:835
class SquidConfig Config
Definition: SquidConfig.cc:12
StatCounters statCounter
Definition: StatCounters.cc:12
#define assert(EX)
Definition: assert.h:19
int64_t queue_offset
number of bytes sent to SwapDir for writing
Definition: MemObject.h:151
Decision decision
current decision state
Definition: MemObject.h:156
StoreIOState::Pointer sio
Definition: MemObject.h:152
Decision
Decision states for StoreEntry::swapoutPossible() and related code.
Definition: MemObject.h:155
int64_t lowestMemReaderOffset() const
Definition: MemObject.cc:277
SwapOut swapout
Definition: MemObject.h:159
int64_t availableForSwapOut() const
buffered bytes we have not swapped out yet
Definition: MemObject.cc:483
int64_t objectBytesOnDisk() const
Definition: MemObject.cc:322
int64_t expectedReplySize() const
Definition: MemObject.cc:238
mem_hdr data_hdr
Definition: MemObject.h:138
int64_t inmem_lo
Definition: MemObject.h:139
size_t swap_hdr_sz
Definition: MemObject.h:199
int64_t endOffset() const
Definition: MemObject.cc:214
bool isContiguous() const
Definition: MemObject.cc:400
int64_t object_sz
Definition: MemObject.h:198
C * getRaw() const
Definition: RefCount.h:80
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:421
struct StatCounters::@137 swap
uint16_t flags
Definition: Store.h:232
sdirno swap_dirn
Definition: Store.h:238
const char * getSerialisedMetaData(size_t &length) const
Definition: store.cc:1717
int unlock(const char *context)
Definition: store.cc:442
const char * url() const
Definition: store.cc:1533
bool hasDisk(const sdirno dirn=-1, const sfileno filen=-1) const
Definition: store.cc:1896
swap_status_t swap_status
Definition: Store.h:246
void lock(const char *context)
Definition: store.cc:418
bool checkDeferRead(int fd) const
Definition: store.cc:228
void swapOutDecision(const MemObject::SwapOut::Decision &decision)
Definition: store.cc:1767
bool swappingOut() const
whether we are in the process of writing this entry to disk
Definition: Store.h:134
Store::Disk & disk() const
the disk this entry is [being] cached on; asserts for entries w/o a disk
Definition: store.cc:1887
bool mayStartSwapOut()
whether we may start writing to disk (now or in the future)
const char * getMD5Text() const
Definition: store.cc:204
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:236
void storeWriterDone()
called when a store writer ends its work (successfully or not)
Definition: store.cc:1775
void attachToDisk(const sdirno, const sfileno, const swap_status_t)
Definition: store.cc:1907
MemObject * mem_obj
Definition: Store.h:221
void abort()
Definition: store.cc:1050
int64_t objectLen() const
Definition: Store.h:257
store_status_t store_status
Definition: Store.h:244
void releaseRequest(const bool shareable=false)
Definition: store.cc:431
uint64_t swap_file_sz
Definition: Store.h:230
void swapOut()
bool checkCachable()
Definition: store.cc:902
void swapOutFileClose(int how)
@ wroteAll
success: caller supplied all data it wanted to swap out
Definition: StoreIOState.h:70
off_t offset() const
Definition: StoreIOState.h:60
void STFNCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:41
sfileno swap_filen
Definition: StoreIOState.h:84
virtual bool write(char const *buf, size_t size, off_t offset, FREE *free_func)=0
sdirno swap_dirn
Definition: StoreIOState.h:83
void STIOCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:51
void memoryOut(StoreEntry &, const bool preserveSwappable)
called to get rid of no longer needed entry data in RAM, if any
Definition: Controller.cc:594
int64_t accumulateMore(StoreEntry &) const
Definition: Controller.cc:485
virtual void diskFull()
Definition: Disk.cc:145
virtual void finalizeSwapoutSuccess(const StoreEntry &)=0
finalize the successful swapout that has been already noticed by Store
virtual void finalizeSwapoutFailure(StoreEntry &)=0
abort the failed swapout that has been already noticed by Store
char * NodeGet(mem_node *aNode)
Definition: stmem.cc:26
mem_node * getBlockContainingLocation(int64_t location) const
Definition: stmem.cc:175
StoreIOBuffer nodeBuffer
Definition: mem_node.h:35
int64_t start() const
Definition: mem_node.cc:67
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
#define DBG_CRITICAL
Definition: Stream.h:40
#define SM_PAGE_SIZE
Definition: defines.h:65
#define EBIT_TEST(flag, bit)
Definition: defines.h:69
#define DISK_NO_SPACE_LEFT
Definition: defines.h:30
@ STORE_LOG_SWAPOUTFAIL
Definition: enums.h:160
@ STORE_LOG_SWAPOUT
Definition: enums.h:158
@ SWAPOUT_FAILED
Definition: enums.h:68
@ SWAPOUT_NONE
Definition: enums.h:58
@ SWAPOUT_WRITING
Definition: enums.h:61
@ SWAPOUT_DONE
Definition: enums.h:64
@ ENTRY_SPECIAL
Definition: enums.h:84
@ RELEASE_REQUEST
prohibits making the key public
Definition: enums.h:98
@ ENTRY_ABORTED
Definition: enums.h:115
@ STORE_PENDING
Definition: enums.h:51
@ STORE_OK
Definition: enums.h:50
int shutting_down
int64_t store_maxobjsize
void memNodeWriteComplete(void *d)
Definition: mem_node.cc:37
Controller & Root()
safely access controller singleton
Definition: Controller.cc:926
#define xfree
void storeConfigure(void)
Definition: store.cc:1243
int storeTooManyDiskFilesOpen(void)
Definition: store.cc:862
void storeClose(StoreIOState::Pointer sio, int how)
Definition: store_io.cc:65
StoreIOState::Pointer storeCreate(StoreEntry *e, StoreIOState::STFNCB *file_callback, StoreIOState::STIOCB *close_callback, void *callback_data)
Definition: store_io.cc:25
void storeIOWrite(StoreIOState::Pointer sio, char const *buf, size_t size, off_t offset, FREE *free_func)
Definition: store_io.cc:85
void storeLog(int tag, const StoreEntry *e)
Definition: store_log.cc:38
static StoreIOState::STFNCB storeSwapOutFileNotify
static StoreIOState::STIOCB storeSwapOutFileClosed
static void xfree_cppwrapper(void *x)
static bool doPages(StoreEntry *anEntry)
static void storeSwapOutStart(StoreEntry *e)
@ SWAP_LOG_ADD
Definition: swap_log_op.h:14

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors