store_swapout.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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 
27 static void storeSwapOutStart(StoreEntry * e);
30 
31 // wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
32 static void xfree_cppwrapper(void *x)
33 {
34  xfree(x);
35 }
36 
37 /* start swapping object to disk */
38 static 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);
51  /* If we start swapping out objects with OutOfBand Metadata,
52  * then this code needs changing
53  */
54 
55  /* TODO: make some sort of data,size refcounted immutable buffer
56  * and stop fooling ourselves with "const char*" buffers.
57  */
58 
59  // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
60  const auto buf = e->getSerialisedMetaData(mem->swap_hdr_sz);
61  assert(buf);
62 
63  /* Create the swap file */
64  generic_cbdata *c = new generic_cbdata(e);
66 
67  if (sio == NULL) {
68  assert(!e->hasDisk());
71  delete c;
72  xfree((char*)buf);
74  return;
75  }
76 
77  mem->swapout.sio = sio;
78  /* Don't lock until after create, or the replacement
79  * code might get confused */
80 
81  e->lock("storeSwapOutStart");
82  /* Pick up the file number if it was assigned immediately */
84 
85  /* write out the swap metadata */
87 }
88 
90 static void
92 {
93  assert(false);
94 }
95 
96 static bool
98 {
99  MemObject *mem = anEntry->mem_obj;
100 
101  do {
102  // find the page containing the first byte we have not swapped out yet
103  mem_node *page =
105 
106  if (!page)
107  break; // wait for more data to become available
108 
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);
112 
113  /*
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. :-)
120  */
121  ssize_t swap_buf_len = page->nodeBuffer.length;
122 
123  debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
124 
125  assert(swap_buf_len > 0);
126 
127  debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
128 
129  mem->swapout.queue_offset += swap_buf_len;
130 
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),
135  swap_buf_len,
136  -1,
138 
139  if (!ok || !anEntry->swappingOut())
140  return false;
141 
142  int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
143 
144  if (anEntry->store_status == STORE_PENDING)
145  if (swapout_size < SM_PAGE_SIZE)
146  break;
147 
148  if (swapout_size <= 0)
149  break;
150  } while (true);
151 
152  // either wait for more data or call swapOutFileClose()
153  return true;
154 }
155 
156 /* This routine is called every time data is sent to the client side.
157  * It's overhead is therefor, significant.
158  */
159 void
161 {
162  if (!mem_obj)
163  return;
164 
165  // this flag may change so we must check even if we are swappingOut
166  if (EBIT_TEST(flags, ENTRY_ABORTED)) {
168  // StoreEntry::abort() already closed the swap out file, if any
169  // no trimming: data producer must stop production if ENTRY_ABORTED
170  return;
171  }
172 
173  const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
174 
175  Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut);
176 
178  return; // decided not to write to disk (at least for now)
179 
180  if (!weAreOrMayBeSwappingOut)
181  return; // finished writing to disk after an earlier swStarted decision
182 
183  // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
184  // store_status == STORE_OK below means we got everything we wanted.
185 
186  debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
187  debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
188  debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
189 
190  if (mem_obj->swapout.sio != NULL)
191  debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
192 
193  int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
194 
195  debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
196 
197 #if SIZEOF_OFF_T <= 4
198 
199  if (mem_obj->endOffset() > 0x7FFF0000) {
200  debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
201  abort();
202  return;
203  }
204 
205 #endif
206  if (swappingOut())
208 
209  // buffered bytes we have not swapped out yet
210  const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
211  assert(swapout_maxsize >= 0);
212  debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
213 
214  if (swapout_maxsize == 0) { // swapped everything we got
215  if (store_status == STORE_OK) { // got everything we wanted
216  assert(mem_obj->object_sz >= 0);
218  }
219  // else need more data to swap out
220  return;
221  }
222 
223  if (store_status == STORE_PENDING) {
224  /* wait for a full block to write */
225 
226  if (swapout_maxsize < SM_PAGE_SIZE)
227  return;
228 
229  /*
230  * Wait until we are below the disk FD limit, only if the
231  * next read won't be deferred.
232  */
234  return;
235  }
236 
237  /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
238  if (!hasDisk()) {
240  assert(mem_obj->inmem_lo == 0);
241  storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
242  }
243 
244  if (mem_obj->swapout.sio == NULL)
245  return;
246 
247  if (!doPages(this))
248  /* oops, we're not swapping out any more */
249  return;
250 
251  if (store_status == STORE_OK) {
252  /*
253  * If the state is STORE_OK, then all data must have been given
254  * to the filesystem at this point because storeSwapOut() is
255  * not going to be called again for this entry.
256  */
257  assert(mem_obj->object_sz >= 0);
260  }
261 }
262 
263 void
265 {
266  assert(mem_obj != NULL);
267  debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
268  debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
269 
270  if (mem_obj->swapout.sio == NULL)
271  return;
272 
273  storeClose(mem_obj->swapout.sio, how);
274 }
275 
276 static void
277 storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
278 {
279  StoreEntry *e;
280  static_cast<generic_cbdata *>(data)->unwrap(&e);
281 
282  MemObject *mem = e->mem_obj;
283  assert(mem->swapout.sio == self);
284  assert(e->swappingOut());
285 
286  // if object_size is still unknown, the entry was probably aborted
287  if (errflag || e->objectLen() < 0) {
288  debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
289  std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
290  e->swap_filen << ", errflag=" << errflag);
291 
292  if (errflag == DISK_NO_SPACE_LEFT) {
293  /* TODO: this should be handle by the link from store IO to
294  * Store, rather than being a top level API call.
295  */
296  e->disk().diskFull();
297  storeConfigure();
298  }
299 
300  // mark the locked entry for deletion
301  // TODO: Keep the memory entry (if any)
302  e->releaseRequest();
304  e->disk().finalizeSwapoutFailure(*e);
305  } else {
306  /* swapping complete */
307  debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
308  e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
309  std::uppercase << e->swap_filen);
310  debugs(20, 5, HERE << "swap_file_sz = " <<
311  e->objectLen() << " + " << mem->swap_hdr_sz);
312 
313  e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
315  e->disk().finalizeSwapoutSuccess(*e);
316 
317  // XXX: For some Stores, it is pointless to re-check cachability here
318  // and it leads to double counts in store_check_cachable_hist. We need
319  // another way to signal a completed but failed swapout. Or, better,
320  // each Store should handle its own logging and LOG state setting.
321  if (e->checkCachable()) {
324  }
325 
327  }
328 
330  debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
331  mem->swapout.sio = NULL;
332  e->unlock("storeSwapOutFileClosed");
333 }
334 
335 bool
337 {
338  // must be checked in the caller
340  assert(!swappingOut());
341 
343  return false;
344 
345  assert(mem_obj);
347 
348  // if we decided that starting is not possible, do not repeat same checks
349  if (decision == MemObject::SwapOut::swImpossible) {
350  debugs(20, 3, HERE << " already rejected");
351  return false;
352  }
353 
354  // if we are swapping out or swapped out already, do not start over
355  if (hasDisk() || Store::Root().hasReadableDiskEntry(*this)) {
356  debugs(20, 3, "already did");
358  return false;
359  }
360 
361  // if we have just stared swapping out (attachToDisk() has not been
362  // called), do not start over
363  if (decision == MemObject::SwapOut::swStarted) {
364  debugs(20, 3, "already started");
366  return false;
367  }
368 
369  if (Store::Root().markedForDeletionAndAbandoned(*this)) {
370  debugs(20, 3, "marked for deletion and abandoned");
372  return false;
373  }
374 
375  // if we decided that swapout is possible, do not repeat same checks
376  if (decision == MemObject::SwapOut::swPossible) {
377  debugs(20, 3, "already allowed");
378  return true;
379  }
380 
381  if (!checkCachable()) {
382  debugs(20, 3, HERE << "not cachable");
384  return false;
385  }
386 
387  if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
388  debugs(20, 3, HERE << url() << " SPECIAL");
390  return false;
391  }
392 
393  if (mem_obj->inmem_lo > 0) {
394  debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
396  return false;
397  }
398 
399  if (!mem_obj->isContiguous()) {
400  debugs(20, 3, "storeSwapOut: not Contiguous");
402  return false;
403  }
404 
405  // handle store_maxobjsize limit
406  {
407  // TODO: add estimated store metadata size to be conservative
408 
409  // use guaranteed maximum if it is known
410  const int64_t expectedEnd = mem_obj->expectedReplySize();
411  debugs(20, 7, HERE << "expectedEnd = " << expectedEnd);
412  if (expectedEnd > store_maxobjsize) {
413  debugs(20, 3, HERE << "will not fit: " << expectedEnd <<
414  " > " << store_maxobjsize);
416  return false; // known to outgrow the limit eventually
417  }
418 
419  // use current minimum (always known)
420  const int64_t currentEnd = mem_obj->endOffset();
421  if (currentEnd > store_maxobjsize) {
422  debugs(20, 3, HERE << "does not fit: " << currentEnd <<
423  " > " << store_maxobjsize);
425  return false; // already does not fit and may only get bigger
426  }
427 
428  // prevent final default swPossible answer for yet unknown length
429  if (expectedEnd < 0 && store_status != STORE_OK) {
430  const int64_t more = Store::Root().accumulateMore(*this);
431  if (more > 0) {
432  debugs(20, 5, "got " << currentEnd << "; defer decision for " << more << " more bytes");
433  return true; // may still fit, but no final decision yet
434  }
435  }
436  }
437 
439  return true;
440 }
441 
int64_t queue_offset
number of bytes sent to SwapDir for writing
Definition: MemObject.h:151
#define SM_PAGE_SIZE
Definition: defines.h:65
void memoryOut(StoreEntry &, const bool preserveSwappable)
called to get rid of no longer needed entry data in RAM, if any
Definition: Controller.cc:594
StoreIOBuffer nodeBuffer
Definition: mem_node.h:35
void releaseRequest(const bool shareable=false)
Definition: store.cc:472
virtual void finalizeSwapoutFailure(StoreEntry &)=0
abort the failed swapout that has been already noticed by Store
MemObject * mem_obj
Definition: Store.h:222
mem_hdr data_hdr
Definition: MemObject.h:138
const char * url() const
Definition: store.cc:1583
void lock(const char *context)
Definition: store.cc:459
static void storeSwapOutStart(StoreEntry *e)
char * NodeGet(mem_node *aNode)
Definition: stmem.cc:26
void storeIOWrite(StoreIOState::Pointer sio, char const *buf, size_t size, off_t offset, FREE *free_func)
Definition: store_io.cc:85
StoreIOState::Pointer sio
Definition: MemObject.h:152
int64_t expectedReplySize() const
Definition: MemObject.cc:240
bool isContiguous() const
Definition: MemObject.cc:402
@ SWAPOUT_WRITING
Definition: enums.h:61
C * getRaw() const
Definition: RefCount.h:80
uint16_t flags
Definition: Store.h:233
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:430
int64_t endOffset() const
Definition: MemObject.cc:216
int64_t objectLen() const
Definition: Store.h:258
@ SWAPOUT_DONE
Definition: enums.h:64
#define DBG_CRITICAL
Definition: Debug.h:40
int64_t availableForSwapOut() const
buffered bytes we have not swapped out yet
Definition: MemObject.cc:485
@ SWAP_LOG_ADD
Definition: swap_log_op.h:14
@ STORE_LOG_SWAPOUT
Definition: enums.h:158
void transientsCompleteWriting(StoreEntry &)
marks the entry completed for collapsed requests
Definition: Controller.cc:640
@ wroteAll
success: caller supplied all data it wanted to swap out
Definition: StoreIOState.h:70
bool mayStartSwapOut()
whether we may start writing to disk (now or in the future)
int64_t inmem_lo
Definition: MemObject.h:139
int64_t accumulateMore(StoreEntry &) const
Definition: Controller.cc:485
@ ENTRY_ABORTED
Definition: enums.h:115
static StoreIOState::STFNCB storeSwapOutFileNotify
Decision decision
current decision state
Definition: MemObject.h:156
#define NULL
Definition: types.h:166
void STFNCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:41
#define DISK_NO_SPACE_LEFT
Definition: defines.h:30
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
struct StatCounters::@137 swap
void abort()
Definition: store.cc:1091
static void xfree_cppwrapper(void *x)
bool hasDisk(const sdirno dirn=-1, const sfileno filen=-1) const
Definition: store.cc:1930
sdirno swap_dirn
Definition: Store.h:239
#define EBIT_TEST(flag, bit)
Definition: defines.h:69
@ RELEASE_REQUEST
prohibits making the key public
Definition: enums.h:98
mem_node * getBlockContainingLocation(int64_t location) const
Definition: stmem.cc:175
virtual void diskFull()
Definition: Disk.cc:145
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:152
StoreIOState::Pointer storeCreate(StoreEntry *e, StoreIOState::STFNCB *file_callback, StoreIOState::STIOCB *close_callback, void *callback_data)
Definition: store_io.cc:25
const char * getSerialisedMetaData(size_t &length) const
Definition: store.cc:1769
int unlock(const char *context)
Definition: store.cc:483
off_t offset() const
Definition: StoreIOState.h:60
store_status_t store_status
Definition: Store.h:245
#define assert(EX)
Definition: assert.h:19
void swapOutDecision(const MemObject::SwapOut::Decision &decision)
Definition: store.cc:1806
@ ENTRY_SPECIAL
Definition: enums.h:84
bool checkCachable()
Definition: store.cc:943
@ STORE_OK
Definition: enums.h:50
void swapOut()
virtual void finalizeSwapoutSuccess(const StoreEntry &)=0
finalize the successful swapout that has been already noticed by Store
#define xfree
int64_t store_maxobjsize
void storeDirSwapLog(const StoreEntry *e, int op)
Definition: Disks.cc:836
static StoreIOState::STIOCB storeSwapOutFileClosed
void storeConfigure(void)
Definition: store.cc:1286
swap_status_t swap_status
Definition: Store.h:247
static bool doPages(StoreEntry *anEntry)
void STIOCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:51
Decision
Decision states for StoreEntry::swapoutPossible() and related code.
Definition: MemObject.h:155
bool checkDeferRead(int fd) const
Definition: store.cc:267
void swapOutFileClose(int how)
int storeTooManyDiskFilesOpen(void)
Definition: store.cc:903
void storeClose(StoreIOState::Pointer sio, int how)
Definition: store_io.cc:65
int64_t object_sz
Definition: MemObject.h:198
@ SWAPOUT_FAILED
Definition: enums.h:68
virtual bool write(char const *buf, size_t size, off_t offset, FREE *free_func)=0
void storeLog(int tag, const StoreEntry *e)
Definition: store_log.cc:39
uint64_t swap_file_sz
Definition: Store.h:231
Store::Disk & disk() const
the disk this entry is [being] cached on; asserts for entries w/o a disk
Definition: store.cc:1921
sdirno swap_dirn
Definition: StoreIOState.h:83
void attachToDisk(const sdirno, const sfileno, const swap_status_t)
Definition: store.cc:1941
int64_t lowestMemReaderOffset() const
Definition: MemObject.cc:279
bool swappingOut() const
whether we are in the process of writing this entry to disk
Definition: Store.h:133
sfileno swap_filen
Definition: StoreIOState.h:84
int64_t objectBytesOnDisk() const
Definition: MemObject.cc:324
void memNodeWriteComplete(void *d)
Definition: mem_node.cc:37
@ SWAPOUT_NONE
Definition: enums.h:58
size_t swap_hdr_sz
Definition: MemObject.h:199
const char * getMD5Text() const
Definition: store.cc:205
int64_t start() const
Definition: mem_node.cc:67
class SquidConfig Config
Definition: SquidConfig.cc:12
@ STORE_LOG_SWAPOUTFAIL
Definition: enums.h:160
StatCounters statCounter
Definition: StatCounters.cc:12
@ STORE_PENDING
Definition: enums.h:51
SwapOut swapout
Definition: MemObject.h:159
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:237
Controller & Root()
safely access controller singleton
Definition: Controller.cc:934

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors