store_swapout.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "globals.h"
14 #include "Store.h"
15 #include "StoreClient.h"
16 /* FIXME: Abstract the use of this more */
17 #include "mem_node.h"
18 #include "MemObject.h"
19 #include "SquidConfig.h"
20 #include "StatCounters.h"
21 #include "store/Disk.h"
22 #include "store/Disks.h"
23 #include "store_log.h"
24 #include "swap_log_op.h"
25 
26 static void storeSwapOutStart(StoreEntry * e);
29 
30 // wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
31 static void xfree_cppwrapper(void *x)
32 {
33  xfree(x);
34 }
35 
36 /* start swapping object to disk */
37 static void
39 {
40  MemObject *mem = e->mem_obj;
42  assert(mem);
43  /* Build the swap metadata, so the filesystem will know how much
44  * metadata there is to store
45  */
46  debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e->url() << "' to dirno " <<
47  e->swap_dirn << ", fileno " << std::hex << std::setw(8) << std::setfill('0') <<
48  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) {
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 */
82  e->swap_filen = mem->swapout.sio->swap_filen;
83 
84  e->swap_dirn = mem->swapout.sio->swap_dirn;
85 
86  /* write out the swap metadata */
88 }
89 
90 static void
92 {
93  StoreEntry *e;
94  static_cast<generic_cbdata *>(data)->unwrap(&e);
95 
96  MemObject *mem = e->mem_obj;
98  assert(mem);
99  assert(mem->swapout.sio == self);
100  assert(errflag == 0);
101  assert(e->swap_filen < 0); // if this fails, call SwapDir::disconnect(e)
102  e->swap_filen = mem->swapout.sio->swap_filen;
103  e->swap_dirn = mem->swapout.sio->swap_dirn;
104 }
105 
106 static bool
108 {
109  MemObject *mem = anEntry->mem_obj;
110 
111  do {
112  // find the page containing the first byte we have not swapped out yet
113  mem_node *page =
115 
116  if (!page)
117  break; // wait for more data to become available
118 
119  // memNodeWriteComplete() and absence of buffer offset math below
120  // imply that we always write from the very beginning of the page
121  assert(page->start() == mem->swapout.queue_offset);
122 
123  /*
124  * Get the length of this buffer. We are assuming(!) that the buffer
125  * length won't change on this buffer, or things are going to be very
126  * strange. I think that after the copy to a buffer is done, the buffer
127  * size should stay fixed regardless so that this code isn't confused,
128  * but we can look at this at a later date or whenever the code results
129  * in bad swapouts, whichever happens first. :-)
130  */
131  ssize_t swap_buf_len = page->nodeBuffer.length;
132 
133  debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
134 
135  assert(swap_buf_len > 0);
136 
137  debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
138 
139  mem->swapout.queue_offset += swap_buf_len;
140 
141  // Quit if write() fails. Sio is going to call our callback, and that
142  // will cleanup, but, depending on the fs, that call may be async.
143  const bool ok = mem->swapout.sio->write(
144  mem->data_hdr.NodeGet(page),
145  swap_buf_len,
146  -1,
148 
149  if (!ok || anEntry->swap_status != SWAPOUT_WRITING)
150  return false;
151 
152  int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
153 
154  if (anEntry->store_status == STORE_PENDING)
155  if (swapout_size < SM_PAGE_SIZE)
156  break;
157 
158  if (swapout_size <= 0)
159  break;
160  } while (true);
161 
162  // either wait for more data or call swapOutFileClose()
163  return true;
164 }
165 
166 /* This routine is called every time data is sent to the client side.
167  * It's overhead is therefor, significant.
168  */
169 void
171 {
172  if (!mem_obj)
173  return;
174 
175  // this flag may change so we must check even if we are swappingOut
176  if (EBIT_TEST(flags, ENTRY_ABORTED)) {
178  // StoreEntry::abort() already closed the swap out file, if any
179  // no trimming: data producer must stop production if ENTRY_ABORTED
180  return;
181  }
182 
183  const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
184 
185  Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut);
186 
188  return; // nothing else to do
189 
190  // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
191  // store_status == STORE_OK below means we got everything we wanted.
192 
193  debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
194  debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
195  debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
196 
197  if (mem_obj->swapout.sio != NULL)
198  debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
199 
200  int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
201 
202  debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
203 
204 #if SIZEOF_OFF_T <= 4
205 
206  if (mem_obj->endOffset() > 0x7FFF0000) {
207  debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
208  abort();
209  return;
210  }
211 
212 #endif
215 
216  // buffered bytes we have not swapped out yet
217  const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
218  assert(swapout_maxsize >= 0);
219  debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
220 
221  if (swapout_maxsize == 0) { // swapped everything we got
222  if (store_status == STORE_OK) { // got everything we wanted
223  assert(mem_obj->object_sz >= 0);
225  }
226  // else need more data to swap out
227  return;
228  }
229 
230  if (store_status == STORE_PENDING) {
231  /* wait for a full block to write */
232 
233  if (swapout_maxsize < SM_PAGE_SIZE)
234  return;
235 
236  /*
237  * Wait until we are below the disk FD limit, only if the
238  * next read won't be deferred.
239  */
241  return;
242  }
243 
244  /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
245  if (swap_status == SWAPOUT_NONE) {
247  assert(mem_obj->inmem_lo == 0);
248  storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
249  }
250 
251  if (mem_obj->swapout.sio == NULL)
252  return;
253 
254  if (!doPages(this))
255  /* oops, we're not swapping out any more */
256  return;
257 
258  if (store_status == STORE_OK) {
259  /*
260  * If the state is STORE_OK, then all data must have been given
261  * to the filesystem at this point because storeSwapOut() is
262  * not going to be called again for this entry.
263  */
264  assert(mem_obj->object_sz >= 0);
267  }
268 }
269 
270 void
272 {
273  assert(mem_obj != NULL);
274  debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
275  debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
276 
277  if (mem_obj->swapout.sio == NULL)
278  return;
279 
280  storeClose(mem_obj->swapout.sio, how);
281 }
282 
283 static void
285 {
286  StoreEntry *e;
287  static_cast<generic_cbdata *>(data)->unwrap(&e);
288 
289  MemObject *mem = e->mem_obj;
290  assert(mem->swapout.sio == self);
292 
293  // if object_size is still unknown, the entry was probably aborted
294  if (errflag || e->objectLen() < 0) {
295  debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
296  std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
297  e->swap_filen << ", errflag=" << errflag);
298 
299  if (errflag == DISK_NO_SPACE_LEFT) {
300  /* FIXME: this should be handle by the link from store IO to
301  * Store, rather than being a top level API call.
302  */
303  e->disk().diskFull();
304  storeConfigure();
305  }
306 
307  if (e->swap_filen >= 0)
308  e->disk().unlink(*e);
309 
311 
312  e->releaseRequest();
313  } else {
314  /* swapping complete */
315  debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
316  e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
317  std::uppercase << e->swap_filen);
318  debugs(20, 5, HERE << "swap_file_sz = " <<
319  e->objectLen() << " + " << mem->swap_hdr_sz);
320 
321  e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
323  e->disk().swappedOut(*e);
324 
325  // XXX: For some Stores, it is pointless to re-check cachability here
326  // and it leads to double counts in store_check_cachable_hist. We need
327  // another way to signal a completed but failed swapout. Or, better,
328  // each Store should handle its own logging and LOG state setting.
329  if (e->checkCachable()) {
332  }
333 
335  }
336 
337  debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
338  mem->swapout.sio = NULL;
339  e->unlock("storeSwapOutFileClosed");
340 }
341 
342 bool
344 {
345  // must be checked in the caller
347  assert(!swappingOut());
348 
350  return false;
351 
352  assert(mem_obj);
354 
355  // if we decided that starting is not possible, do not repeat same checks
356  if (decision == MemObject::SwapOut::swImpossible) {
357  debugs(20, 3, HERE << " already rejected");
358  return false;
359  }
360 
361  // if we swapped out already, do not start over
362  if (swap_status == SWAPOUT_DONE) {
363  debugs(20, 3, "already did");
365  return false;
366  }
367 
368  // if we stared swapping out already, do not start over
369  if (decision == MemObject::SwapOut::swStarted) {
370  debugs(20, 3, "already started");
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 
sdirno swap_dirn
Definition: Store.h:179
static StoreIOState::STFNCB storeSwapOutFileNotify
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:485
size_t swap_hdr_sz
Definition: MemObject.h:170
void storeDirSwapLog(const StoreEntry *e, int op)
Definition: Disks.cc:712
void storeLog(int tag, const StoreEntry *e)
Definition: store_log.cc:38
static bool doPages(StoreEntry *anEntry)
void memNodeWriteComplete(void *d)
Definition: mem_node.cc:37
void storeClose(StoreIOState::Pointer sio, int how)
Definition: store_io.cc:68
int64_t endOffset() const
Definition: MemObject.cc:232
static StoreIOState::STIOCB storeSwapOutFileClosed
virtual void swappedOut(const StoreEntry &e)=0
called when entry swap out is complete
int64_t store_maxobjsize
off_t offset() const
Definition: StoreIOState.h:60
bool swappingOut() const
whether we are in the process of writing this entry to disk
Definition: Store.h:106
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:177
#define DBG_CRITICAL
Definition: Debug.h:44
Controller & Root()
safely access controller singleton
Definition: Controller.cc:619
static void storeSwapOutStart(StoreEntry *e)
int storeTooManyDiskFilesOpen(void)
Definition: store.cc:940
int64_t lowestMemReaderOffset() const
Definition: MemObject.cc:284
StoreIOState::Pointer sio
Definition: MemObject.h:117
struct StatCounters::@137 swap
void storeIOWrite(StoreIOState::Pointer sio, char const *buf, size_t size, off_t offset, FREE *free_func)
Definition: store_io.cc:88
virtual char const * getSerialisedMetaData()
Definition: store.cc:1892
uint64_t swap_file_sz
Definition: Store.h:171
void const char HLPCB void * data
Definition: stub_helper.cc:16
bool checkDeferRead(int fd) const
Definition: store.cc:244
int64_t object_sz
Definition: MemObject.h:169
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
virtual int64_t objectLen() const
Definition: store.cc:1727
success: caller supplied all data it wanted to swap out
Definition: StoreIOState.h:70
void abort()
Definition: store.cc:1134
bool checkCachable()
Definition: store.cc:981
void STFNCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:41
int64_t objectBytesOnDisk() const
Definition: MemObject.cc:328
void releaseRequest(const bool shareable=false)
Definition: store.cc:474
uint16_t flags
Definition: Store.h:173
int64_t accumulateMore(StoreEntry &) const
Definition: Controller.cc:348
MemObject * mem_obj
Definition: Store.h:162
#define SM_PAGE_SIZE
Definition: defines.h:102
const char * url() const
Definition: store.cc:1672
void const char * buf
Definition: stub_helper.cc:16
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
static void xfree_cppwrapper(void *x)
SwapOut swapout
Definition: MemObject.h:124
Definition: enums.h:46
virtual void diskFull()
Definition: Disk.cc:145
void STIOCB(void *their_data, int errflag, StoreIOState::Pointer self)
Definition: StoreIOState.h:51
virtual const char * getMD5Text() const
Definition: store.cc:181
StoreIOBuffer nodeBuffer
Definition: mem_node.h:35
store_status_t store_status
Definition: Store.h:185
void swapOutDecision(const MemObject::SwapOut::Decision &decision)
Definition: store.cc:1929
void swapOutFileClose(int how)
Store::Disk & disk() const
the disk this entry is [being] cached on; asserts for entries w/o a disk
Definition: store.cc:2055
int64_t queue_offset
number of bytes sent to SwapDir for writing
Definition: MemObject.h:116
virtual bool mayStartSwapOut()
whether we may start writing to disk (now or in the future)
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:418
virtual bool write(char const *buf, size_t size, off_t offset, FREE *free_func)=0
void storeConfigure(void)
Definition: store.cc:1404
sdirno swap_dirn
Definition: StoreIOState.h:83
swap_status_t swap_status
Definition: Store.h:187
Decision
Decision states for StoreEntry::swapoutPossible() and related code.
Definition: MemObject.h:120
void memoryOut(StoreEntry &, const bool preserveSwappable)
called to get rid of no longer needed entry data in RAM, if any
Definition: Controller.cc:393
int64_t start() const
Definition: mem_node.cc:67
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:101
void swapOut()
bool isContiguous() const
Definition: MemObject.cc:406
#define xfree
mem_node * getBlockContainingLocation(int64_t location) const
Definition: stmem.cc:176
int64_t availableForSwapOut() const
buffered bytes we have not swapped out yet
Definition: MemObject.cc:499
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
C * getRaw() const
Definition: RefCount.h:74
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
int64_t expectedReplySize() const
Definition: MemObject.cc:256
int64_t inmem_lo
Definition: MemObject.h:102
virtual void unlink(StoreEntry &e)=0
remove the entry from the store
sfileno swap_filen
Definition: StoreIOState.h:84
void lock(const char *context)
Definition: store.cc:448

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors