]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
Maintenance: Remove FIXME and \todo labels (#647)
[thirdparty/squid.git] / src / store_swapout.cc
1 /*
2 * Copyright (C) 1996-2020 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);
28 static StoreIOState::STIOCB storeSwapOutFileClosed;
29 static StoreIOState::STFNCB storeSwapOutFileNotify;
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
39 storeSwapOutStart(StoreEntry * e)
40 {
41 MemObject *mem = e->mem_obj;
42 StoreIOState::Pointer sio;
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 e->swapOutDecision(MemObject::SwapOut::swStarted);
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);
65 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
66
67 if (sio == NULL) {
68 assert(!e->hasDisk());
69 e->swap_status = SWAPOUT_NONE;
70 e->swapOutDecision(MemObject::SwapOut::swImpossible);
71 delete c;
72 xfree((char*)buf);
73 storeLog(STORE_LOG_SWAPOUTFAIL, e);
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 */
83 e->attachToDisk(mem->swapout.sio->swap_dirn, mem->swapout.sio->swap_filen, SWAPOUT_WRITING);
84
85 /* write out the swap metadata */
86 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree_cppwrapper);
87 }
88
89 /// XXX: unused, see a related StoreIOState::file_callback
90 static void
91 storeSwapOutFileNotify(void *, int, StoreIOState::Pointer)
92 {
93 assert(false);
94 }
95
96 static bool
97 doPages(StoreEntry *anEntry)
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 =
104 mem->data_hdr.getBlockContainingLocation(mem->swapout.queue_offset);
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,
137 memNodeWriteComplete);
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
160 StoreEntry::swapOut()
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)) {
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
170 return;
171 }
172
173 const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
174
175 Store::Root().memoryOut(*this, weAreOrMayBeSwappingOut);
176
177 if (mem_obj->swapout.decision < MemObject::SwapOut::swPossible)
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())
204 assert(mem_obj->inmem_lo <= mem_obj->objectBytesOnDisk() );
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);
214 swapOutFileClose(StoreIOState::wroteAll);
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 */
230 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
231 return;
232 }
233
234 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
235 if (!hasDisk()) {
236 assert(mem_obj->swapout.sio == NULL);
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);
255 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
256 swapOutFileClose(StoreIOState::wroteAll);
257 }
258 }
259
260 void
261 StoreEntry::swapOutFileClose(int how)
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
274 storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
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 /* TODO: 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();
300 e->swap_status = SWAPOUT_FAILED;
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;
311 e->swap_status = SWAPOUT_DONE;
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()) {
319 storeLog(STORE_LOG_SWAPOUT, e);
320 storeDirSwapLog(e, SWAP_LOG_ADD);
321 }
322
323 ++statCounter.swap.outs;
324 }
325
326 Store::Root().transientsCompleteWriting(*e);
327 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
328 mem->swapout.sio = NULL;
329 e->unlock("storeSwapOutFileClosed");
330 }
331
332 bool
333 StoreEntry::mayStartSwapOut()
334 {
335 // must be checked in the caller
336 assert(!EBIT_TEST(flags, ENTRY_ABORTED));
337 assert(!swappingOut());
338
339 if (!Config.cacheSwap.n_configured)
340 return false;
341
342 assert(mem_obj);
343 const MemObject::SwapOut::Decision &decision = mem_obj->swapout.decision;
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");
354 swapOutDecision(MemObject::SwapOut::swImpossible);
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");
362 swapOutDecision(MemObject::SwapOut::swImpossible);
363 return false;
364 }
365
366 if (Store::Root().markedForDeletionAndAbandoned(*this)) {
367 debugs(20, 3, "marked for deletion and abandoned");
368 swapOutDecision(MemObject::SwapOut::swImpossible);
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");
380 swapOutDecision(MemObject::SwapOut::swImpossible);
381 return false;
382 }
383
384 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
385 debugs(20, 3, HERE << url() << " SPECIAL");
386 swapOutDecision(MemObject::SwapOut::swImpossible);
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);
392 swapOutDecision(MemObject::SwapOut::swImpossible);
393 return false;
394 }
395
396 if (!mem_obj->isContiguous()) {
397 debugs(20, 3, "storeSwapOut: not Contiguous");
398 swapOutDecision(MemObject::SwapOut::swImpossible);
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);
412 swapOutDecision(MemObject::SwapOut::swImpossible);
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);
421 swapOutDecision(MemObject::SwapOut::swImpossible);
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
435 swapOutDecision(MemObject::SwapOut::swPossible);
436 return true;
437 }
438