store_swapout.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 /* FIXME: 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 char *buf = e->getSerialisedMetaData ();
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; // nothing else to do
179 
180  // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
181  // store_status == STORE_OK below means we got everything we wanted.
182 
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);
186 
187  if (mem_obj->swapout.sio != NULL)
188  debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
189 
190  int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
191 
192  debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
193 
194 #if SIZEOF_OFF_T <= 4
195 
196  if (mem_obj->endOffset() > 0x7FFF0000) {
197  debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
198  abort();
199  return;
200  }
201 
202 #endif
203  if (swappingOut())
205 
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);
210 
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);
215  }
216  // else need more data to swap out
217  return;
218  }
219 
220  if (store_status == STORE_PENDING) {
221  /* wait for a full block to write */
222 
223  if (swapout_maxsize < SM_PAGE_SIZE)
224  return;
225 
226  /*
227  * Wait until we are below the disk FD limit, only if the
228  * next read won't be deferred.
229  */
231  return;
232  }
233 
234  /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
235  if (!hasDisk()) {
237  assert(mem_obj->inmem_lo == 0);
238  storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
239  }
240 
241  if (mem_obj->swapout.sio == NULL)
242  return;
243 
244  if (!doPages(this))
245  /* oops, we're not swapping out any more */
246  return;
247 
248  if (store_status == STORE_OK) {
249  /*
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.
253  */
254  assert(mem_obj->object_sz >= 0);
257  }
258 }
259 
260 void
262 {
263  assert(mem_obj != NULL);
264  debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
265  debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
266 
267  if (mem_obj->swapout.sio == NULL)
268  return;
269 
270  storeClose(mem_obj->swapout.sio, how);
271 }
272 
273 static void
275 {
276  StoreEntry *e;
277  static_cast<generic_cbdata *>(data)->unwrap(&e);
278 
279  MemObject *mem = e->mem_obj;
280  assert(mem->swapout.sio == self);
281  assert(e->swappingOut());
282 
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);
288 
289  if (errflag == DISK_NO_SPACE_LEFT) {
290  /* FIXME: this should be handle by the link from store IO to
291  * Store, rather than being a top level API call.
292  */
293  e->disk().diskFull();
294  storeConfigure();
295  }
296 
297  // mark the locked entry for deletion
298  // TODO: Keep the memory entry (if any)
299  e->releaseRequest();
301  e->disk().finalizeSwapoutFailure(*e);
302  } else {
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);
309 
310  e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
312  e->disk().finalizeSwapoutSuccess(*e);
313 
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()) {
321  }
322 
324  }
325 
327  debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
328  mem->swapout.sio = NULL;
329  e->unlock("storeSwapOutFileClosed");
330 }
331 
332 bool
334 {
335  // must be checked in the caller
337  assert(!swappingOut());
338 
340  return false;
341 
342  assert(mem_obj);
344 
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");
348  return false;
349  }
350 
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");
355  return false;
356  }
357 
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");
363  return false;
364  }
365 
366  if (Store::Root().markedForDeletionAndAbandoned(*this)) {
367  debugs(20, 3, "marked for deletion and abandoned");
369  return false;
370  }
371 
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");
375  return true;
376  }
377 
378  if (!checkCachable()) {
379  debugs(20, 3, HERE << "not cachable");
381  return false;
382  }
383 
384  if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
385  debugs(20, 3, HERE << url() << " SPECIAL");
387  return false;
388  }
389 
390  if (mem_obj->inmem_lo > 0) {
391  debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
393  return false;
394  }
395 
396  if (!mem_obj->isContiguous()) {
397  debugs(20, 3, "storeSwapOut: not Contiguous");
399  return false;
400  }
401 
402  // handle store_maxobjsize limit
403  {
404  // TODO: add estimated store metadata size to be conservative
405 
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);
413  return false; // known to outgrow the limit eventually
414  }
415 
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);
422  return false; // already does not fit and may only get bigger
423  }
424 
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);
428  if (more > 0) {
429  debugs(20, 5, "got " << currentEnd << "; defer decision for " << more << " more bytes");
430  return true; // may still fit, but no final decision yet
431  }
432  }
433  }
434 
436  return true;
437 }
438 
const char * getMD5Text() const
Definition: store.cc:185
sdirno swap_dirn
Definition: Store.h:216
static StoreIOState::STFNCB storeSwapOutFileNotify
struct StatCounters::@136 swap
StatCounters statCounter
Definition: StatCounters.cc:12
#define assert(EX)
Definition: assert.h:17
Decision decision
current decision state
Definition: MemObject.h:121
int unlock(const char *context)
Definition: store.cc:463
size_t swap_hdr_sz
Definition: MemObject.h:169
void storeDirSwapLog(const StoreEntry *e, int op)
Definition: Disks.cc:733
bool hasDisk(const sdirno dirn=-1, const sfileno filen=-1) const
Definition: store.cc:2003
void storeLog(int tag, const StoreEntry *e)
Definition: store_log.cc:38
static bool doPages(StoreEntry *anEntry)
int64_t endOffset() const
Definition: MemObject.cc:218
int64_t objectLen() const
Definition: store.cc:1661
char const * getSerialisedMetaData()
Definition: store.cc:1831
void memNodeWriteComplete(void *d)
Definition: mem_node.cc:37
void storeClose(StoreIOState::Pointer sio, int how)
Definition: store_io.cc:68
static StoreIOState::STIOCB storeSwapOutFileClosed
void attachToDisk(const sdirno, const sfileno, const swap_status_t)
Definition: store.cc:2014
int64_t store_maxobjsize
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:214
#define DBG_CRITICAL
Definition: Debug.h:45
Controller & Root()
safely access controller singleton
Definition: Controller.cc:877
bool isContiguous() const
Definition: MemObject.cc:392
static void storeSwapOutStart(StoreEntry *e)
int storeTooManyDiskFilesOpen(void)
Definition: store.cc:917
StoreIOState::Pointer sio
Definition: MemObject.h:117
int64_t start() const
Definition: mem_node.cc:67
void storeIOWrite(StoreIOState::Pointer sio, char const *buf, size_t size, off_t offset, FREE *free_func)
Definition: store_io.cc:88
int64_t accumulateMore(StoreEntry &) const
Definition: Controller.cc:447
bool swappingOut() const
whether we are in the process of writing this entry to disk
Definition: Store.h:119
uint64_t swap_file_sz
Definition: Store.h:208
void const char HLPCB void * data
Definition: stub_helper.cc:16
int64_t object_sz
Definition: MemObject.h:168
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
const char * url() const
Definition: store.cc:1606
success: caller supplied all data it wanted to swap out
Definition: StoreIOState.h:70
void abort()
Definition: store.cc:1113
bool checkCachable()
Definition: store.cc:958
void STFNCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:41
int64_t availableForSwapOut() const
buffered bytes we have not swapped out yet
Definition: MemObject.cc:485
void releaseRequest(const bool shareable=false)
Definition: store.cc:452
uint16_t flags
Definition: Store.h:210
MemObject * mem_obj
Definition: Store.h:199
off_t offset() const
Definition: StoreIOState.h:60
#define SM_PAGE_SIZE
Definition: defines.h:102
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:153
static void xfree_cppwrapper(void *x)
SwapOut swapout
Definition: MemObject.h:124
Definition: enums.h:46
mem_node * getBlockContainingLocation(int64_t location) const
Definition: stmem.cc:176
virtual void diskFull()
Definition: Disk.cc:145
void STIOCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:51
Store::Disk & disk() const
the disk this entry is [being] cached on; asserts for entries w/o a disk
Definition: store.cc:1994
StoreIOBuffer nodeBuffer
Definition: mem_node.h:35
void transientsCompleteWriting(StoreEntry &)
marks the entry completed for collapsed requests
Definition: Controller.cc:602
store_status_t store_status
Definition: Store.h:222
prohibits making the key public
Definition: enums.h:94
void swapOutDecision(const MemObject::SwapOut::Decision &decision)
Definition: store.cc:1868
void swapOutFileClose(int how)
int64_t queue_offset
number of bytes sent to SwapDir for writing
Definition: MemObject.h:116
int64_t lowestMemReaderOffset() const
Definition: MemObject.cc:270
bool mayStartSwapOut()
whether we may start writing to disk (now or in the future)
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:422
virtual bool write(char const *buf, size_t size, off_t offset, FREE *free_func)=0
void storeConfigure(void)
Definition: store.cc:1339
bool checkDeferRead(int fd) const
Definition: store.cc:247
C * getRaw() const
Definition: RefCount.h:74
sdirno swap_dirn
Definition: StoreIOState.h:83
swap_status_t swap_status
Definition: Store.h:224
Decision
Decision states for StoreEntry::swapoutPossible() and related code.
Definition: MemObject.h:120
virtual void finalizeSwapoutSuccess(const StoreEntry &)=0
finalize the successful swapout that has been already noticed by Store
void memoryOut(StoreEntry &, const bool preserveSwappable)
called to get rid of no longer needed entry data in RAM, if any
Definition: Controller.cc:556
char * NodeGet(mem_node *aNode)
Definition: stmem.cc:27
#define DISK_NO_SPACE_LEFT
Definition: defines.h:42
mem_hdr data_hdr
Definition: MemObject.h:103
void swapOut()
#define xfree
StoreIOState::Pointer storeCreate(StoreEntry *e, StoreIOState::STFNCB *file_callback, StoreIOState::STIOCB *close_callback, void *callback_data)
Definition: store_io.cc:25
#define EBIT_TEST(flag, bit)
Definition: defines.h:107
class SquidConfig Config
Definition: SquidConfig.cc:12
int64_t objectBytesOnDisk() const
Definition: MemObject.cc:314
#define NULL
Definition: types.h:166
virtual void finalizeSwapoutFailure(StoreEntry &)=0
abort the failed swapout that has been already noticed by Store
int64_t expectedReplySize() const
Definition: MemObject.cc:242
int64_t inmem_lo
Definition: MemObject.h:104
sfileno swap_filen
Definition: StoreIOState.h:84
void lock(const char *context)
Definition: store.cc:439

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors