]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_swapout.cc
c8b700686e50abca39941ac86bf055e2253ea91a
[thirdparty/squid.git] / src / store_swapout.cc
1
2 /*
3 * DEBUG: section 20 Storage Manager Swapout Functions
4 * AUTHOR: Duane Wessels
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 #include "squid.h"
35 #include "cbdata.h"
36 #include "StoreClient.h"
37 #include "globals.h"
38 #include "Store.h"
39 /* FIXME: Abstract the use of this more */
40 #include "mem_node.h"
41 #include "MemObject.h"
42 #include "SquidConfig.h"
43 #include "SwapDir.h"
44 #include "StatCounters.h"
45 #include "store_log.h"
46 #include "swap_log_op.h"
47
48 static void storeSwapOutStart(StoreEntry * e);
49 static StoreIOState::STIOCB storeSwapOutFileClosed;
50 static StoreIOState::STFNCB storeSwapOutFileNotify;
51
52 // wrapper to cross C/C++ ABI boundary. xfree is extern "C" for libraries.
53 static void xfree_cppwrapper(void *x)
54 {
55 xfree(x);
56 }
57
58 /* start swapping object to disk */
59 static void
60 storeSwapOutStart(StoreEntry * e)
61 {
62 MemObject *mem = e->mem_obj;
63 StoreIOState::Pointer sio;
64 assert(mem);
65 /* Build the swap metadata, so the filesystem will know how much
66 * metadata there is to store
67 */
68 debugs(20, 5, "storeSwapOutStart: Begin SwapOut '" << e->url() << "' to dirno " <<
69 e->swap_dirn << ", fileno " << std::hex << std::setw(8) << std::setfill('0') <<
70 std::uppercase << e->swap_filen);
71 e->swap_status = SWAPOUT_WRITING;
72 /* If we start swapping out objects with OutOfBand Metadata,
73 * then this code needs changing
74 */
75
76 /* TODO: make some sort of data,size refcounted immutable buffer
77 * and stop fooling ourselves with "const char*" buffers.
78 */
79
80 // Create metadata now, possibly in vain: storeCreate needs swap_hdr_sz.
81 const char *buf = e->getSerialisedMetaData ();
82 assert(buf);
83
84 /* Create the swap file */
85 generic_cbdata *c = new generic_cbdata(e);
86 sio = storeCreate(e, storeSwapOutFileNotify, storeSwapOutFileClosed, c);
87
88 if (sio == NULL) {
89 e->swap_status = SWAPOUT_NONE;
90 mem->swapout.decision = MemObject::SwapOut::swImpossible;
91 delete c;
92 xfree((char*)buf);
93 storeLog(STORE_LOG_SWAPOUTFAIL, e);
94 return;
95 }
96
97 mem->swapout.sio = sio;
98 /* Don't lock until after create, or the replacement
99 * code might get confused */
100
101 e->lock();
102 /* Pick up the file number if it was assigned immediately */
103 e->swap_filen = mem->swapout.sio->swap_filen;
104
105 e->swap_dirn = mem->swapout.sio->swap_dirn;
106
107 /* write out the swap metadata */
108 storeIOWrite(mem->swapout.sio, buf, mem->swap_hdr_sz, 0, xfree_cppwrapper);
109 }
110
111 static void
112 storeSwapOutFileNotify(void *data, int errflag, StoreIOState::Pointer self)
113 {
114 generic_cbdata *c = (generic_cbdata *)data;
115 StoreEntry *e = (StoreEntry *)c->data;
116 MemObject *mem = e->mem_obj;
117 assert(e->swap_status == SWAPOUT_WRITING);
118 assert(mem);
119 assert(mem->swapout.sio == self);
120 assert(errflag == 0);
121 assert(e->swap_filen < 0); // if this fails, call SwapDir::disconnect(e)
122 e->swap_filen = mem->swapout.sio->swap_filen;
123 e->swap_dirn = mem->swapout.sio->swap_dirn;
124 }
125
126 static bool
127 doPages(StoreEntry *anEntry)
128 {
129 MemObject *mem = anEntry->mem_obj;
130
131 do {
132 // find the page containing the first byte we have not swapped out yet
133 mem_node *page =
134 mem->data_hdr.getBlockContainingLocation(mem->swapout.queue_offset);
135
136 if (!page)
137 break; // wait for more data to become available
138
139 // memNodeWriteComplete() and absence of buffer offset math below
140 // imply that we always write from the very beginning of the page
141 assert(page->start() == mem->swapout.queue_offset);
142
143 /*
144 * Get the length of this buffer. We are assuming(!) that the buffer
145 * length won't change on this buffer, or things are going to be very
146 * strange. I think that after the copy to a buffer is done, the buffer
147 * size should stay fixed regardless so that this code isn't confused,
148 * but we can look at this at a later date or whenever the code results
149 * in bad swapouts, whichever happens first. :-)
150 */
151 ssize_t swap_buf_len = page->nodeBuffer.length;
152
153 debugs(20, 3, "storeSwapOut: swap_buf_len = " << swap_buf_len);
154
155 assert(swap_buf_len > 0);
156
157 debugs(20, 3, "storeSwapOut: swapping out " << swap_buf_len << " bytes from " << mem->swapout.queue_offset);
158
159 mem->swapout.queue_offset += swap_buf_len;
160
161 // Quit if write() fails. Sio is going to call our callback, and that
162 // will cleanup, but, depending on the fs, that call may be async.
163 const bool ok = mem->swapout.sio->write(
164 mem->data_hdr.NodeGet(page),
165 swap_buf_len,
166 -1,
167 memNodeWriteComplete);
168
169 if (!ok || anEntry->swap_status != SWAPOUT_WRITING)
170 return false;
171
172 int64_t swapout_size = mem->endOffset() - mem->swapout.queue_offset;
173
174 if (anEntry->store_status == STORE_PENDING)
175 if (swapout_size < SM_PAGE_SIZE)
176 break;
177
178 if (swapout_size <= 0)
179 break;
180 } while (true);
181
182 // either wait for more data or call swapOutFileClose()
183 return true;
184 }
185
186 /* This routine is called every time data is sent to the client side.
187 * It's overhead is therefor, significant.
188 */
189 void
190 StoreEntry::swapOut()
191 {
192 if (!mem_obj)
193 return;
194
195 // this flag may change so we must check even if we are swappingOut
196 if (EBIT_TEST(flags, ENTRY_ABORTED)) {
197 assert(EBIT_TEST(flags, RELEASE_REQUEST));
198 // StoreEntry::abort() already closed the swap out file, if any
199 // no trimming: data producer must stop production if ENTRY_ABORTED
200 return;
201 }
202
203 const bool weAreOrMayBeSwappingOut = swappingOut() || mayStartSwapOut();
204
205 Store::Root().maybeTrimMemory(*this, weAreOrMayBeSwappingOut);
206
207 if (mem_obj->swapout.decision != MemObject::SwapOut::swPossible)
208 return; // nothing else to do
209
210 // Aborted entries have STORE_OK, but swapoutPossible rejects them. Thus,
211 // store_status == STORE_OK below means we got everything we wanted.
212
213 debugs(20, 7, HERE << "storeSwapOut: mem->inmem_lo = " << mem_obj->inmem_lo);
214 debugs(20, 7, HERE << "storeSwapOut: mem->endOffset() = " << mem_obj->endOffset());
215 debugs(20, 7, HERE << "storeSwapOut: swapout.queue_offset = " << mem_obj->swapout.queue_offset);
216
217 if (mem_obj->swapout.sio != NULL)
218 debugs(20, 7, "storeSwapOut: storeOffset() = " << mem_obj->swapout.sio->offset() );
219
220 int64_t const lowest_offset = mem_obj->lowestMemReaderOffset();
221
222 debugs(20, 7, HERE << "storeSwapOut: lowest_offset = " << lowest_offset);
223
224 #if SIZEOF_OFF_T <= 4
225
226 if (mem_obj->endOffset() > 0x7FFF0000) {
227 debugs(20, DBG_CRITICAL, "WARNING: preventing off_t overflow for " << url());
228 abort();
229 return;
230 }
231
232 #endif
233 if (swap_status == SWAPOUT_WRITING)
234 assert(mem_obj->inmem_lo <= mem_obj->objectBytesOnDisk() );
235
236 // buffered bytes we have not swapped out yet
237 const int64_t swapout_maxsize = mem_obj->availableForSwapOut();
238 assert(swapout_maxsize >= 0);
239 debugs(20, 7, "storeSwapOut: swapout_size = " << swapout_maxsize);
240
241 if (swapout_maxsize == 0) { // swapped everything we got
242 if (store_status == STORE_OK) { // got everything we wanted
243 assert(mem_obj->object_sz >= 0);
244 swapOutFileClose(StoreIOState::wroteAll);
245 }
246 // else need more data to swap out
247 return;
248 }
249
250 if (store_status == STORE_PENDING) {
251 /* wait for a full block to write */
252
253 if (swapout_maxsize < SM_PAGE_SIZE)
254 return;
255
256 /*
257 * Wait until we are below the disk FD limit, only if the
258 * next server-side read won't be deferred.
259 */
260 if (storeTooManyDiskFilesOpen() && !checkDeferRead(-1))
261 return;
262 }
263
264 /* Ok, we have stuff to swap out. Is there a swapout.sio open? */
265 if (swap_status == SWAPOUT_NONE) {
266 assert(mem_obj->swapout.sio == NULL);
267 assert(mem_obj->inmem_lo == 0);
268 storeSwapOutStart(this); // sets SwapOut::swImpossible on failures
269 }
270
271 if (mem_obj->swapout.sio == NULL)
272 return;
273
274 if (!doPages(this))
275 /* oops, we're not swapping out any more */
276 return;
277
278 if (store_status == STORE_OK) {
279 /*
280 * If the state is STORE_OK, then all data must have been given
281 * to the filesystem at this point because storeSwapOut() is
282 * not going to be called again for this entry.
283 */
284 assert(mem_obj->object_sz >= 0);
285 assert(mem_obj->endOffset() == mem_obj->swapout.queue_offset);
286 swapOutFileClose(StoreIOState::wroteAll);
287 }
288 }
289
290 void
291 StoreEntry::swapOutFileClose(int how)
292 {
293 assert(mem_obj != NULL);
294 debugs(20, 3, "storeSwapOutFileClose: " << getMD5Text() << " how=" << how);
295 debugs(20, 3, "storeSwapOutFileClose: sio = " << mem_obj->swapout.sio.getRaw());
296
297 if (mem_obj->swapout.sio == NULL)
298 return;
299
300 storeClose(mem_obj->swapout.sio, how);
301 }
302
303 static void
304 storeSwapOutFileClosed(void *data, int errflag, StoreIOState::Pointer self)
305 {
306 generic_cbdata *c = (generic_cbdata *)data;
307 StoreEntry *e = (StoreEntry *)c->data;
308 MemObject *mem = e->mem_obj;
309 assert(mem->swapout.sio == self);
310 assert(e->swap_status == SWAPOUT_WRITING);
311 cbdataFree(c);
312
313 // if object_size is still unknown, the entry was probably aborted
314 if (errflag || e->objectLen() < 0) {
315 debugs(20, 2, "storeSwapOutFileClosed: dirno " << e->swap_dirn << ", swapfile " <<
316 std::hex << std::setw(8) << std::setfill('0') << std::uppercase <<
317 e->swap_filen << ", errflag=" << errflag);
318
319 if (errflag == DISK_NO_SPACE_LEFT) {
320 /* FIXME: this should be handle by the link from store IO to
321 * Store, rather than being a top level API call.
322 */
323 e->store()->diskFull();
324 storeConfigure();
325 }
326
327 if (e->swap_filen >= 0)
328 e->unlink();
329
330 assert(e->swap_status == SWAPOUT_NONE);
331
332 e->releaseRequest();
333 } else {
334 /* swapping complete */
335 debugs(20, 3, "storeSwapOutFileClosed: SwapOut complete: '" << e->url() << "' to " <<
336 e->swap_dirn << ", " << std::hex << std::setw(8) << std::setfill('0') <<
337 std::uppercase << e->swap_filen);
338 debugs(20, 5, HERE << "swap_file_sz = " <<
339 e->objectLen() << " + " << mem->swap_hdr_sz);
340
341 e->swap_file_sz = e->objectLen() + mem->swap_hdr_sz;
342 e->swap_status = SWAPOUT_DONE;
343 e->store()->swappedOut(*e);
344
345 // XXX: For some Stores, it is pointless to re-check cachability here
346 // and it leads to double counts in store_check_cachable_hist. We need
347 // another way to signal a completed but failed swapout. Or, better,
348 // each Store should handle its own logging and LOG state setting.
349 if (e->checkCachable()) {
350 storeLog(STORE_LOG_SWAPOUT, e);
351 storeDirSwapLog(e, SWAP_LOG_ADD);
352 }
353
354 ++statCounter.swap.outs;
355 }
356
357 debugs(20, 3, "storeSwapOutFileClosed: " << __FILE__ << ":" << __LINE__);
358 mem->swapout.sio = NULL;
359 e->unlock();
360 }
361
362 bool
363 StoreEntry::mayStartSwapOut()
364 {
365 // must be checked in the caller
366 assert(!EBIT_TEST(flags, ENTRY_ABORTED));
367 assert(!swappingOut());
368
369 if (!Config.cacheSwap.n_configured)
370 return false;
371
372 assert(mem_obj);
373 MemObject::SwapOut::Decision &decision = mem_obj->swapout.decision;
374
375 // if we decided that swapout is not possible, do not repeat same checks
376 if (decision == MemObject::SwapOut::swImpossible) {
377 debugs(20, 3, HERE << " already rejected");
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, HERE << "already allowed");
384 return true;
385 }
386
387 // if we swapped out already, do not start over
388 if (swap_status == SWAPOUT_DONE) {
389 debugs(20, 3, HERE << "already did");
390 decision = MemObject::SwapOut::swImpossible;
391 return false;
392 }
393
394 if (!checkCachable()) {
395 debugs(20, 3, HERE << "not cachable");
396 decision = MemObject::SwapOut::swImpossible;
397 return false;
398 }
399
400 if (EBIT_TEST(flags, ENTRY_SPECIAL)) {
401 debugs(20, 3, HERE << url() << " SPECIAL");
402 decision = MemObject::SwapOut::swImpossible;
403 return false;
404 }
405
406 if (mem_obj->inmem_lo > 0) {
407 debugs(20, 3, "storeSwapOut: (inmem_lo > 0) imem_lo:" << mem_obj->inmem_lo);
408 decision = MemObject::SwapOut::swImpossible;
409 return false;
410 }
411
412 if (!mem_obj->isContiguous()) {
413 debugs(20, 3, "storeSwapOut: not Contiguous");
414 decision = MemObject::SwapOut::swImpossible;
415 return false;
416 }
417
418 // check cache_dir max-size limit if all cache_dirs have it
419 if (store_maxobjsize >= 0) {
420 // TODO: add estimated store metadata size to be conservative
421
422 // use guaranteed maximum if it is known
423 const int64_t expectedEnd = mem_obj->expectedReplySize();
424 debugs(20, 7, HERE << "expectedEnd = " << expectedEnd);
425 if (expectedEnd > store_maxobjsize) {
426 debugs(20, 3, HERE << "will not fit: " << expectedEnd <<
427 " > " << store_maxobjsize);
428 decision = MemObject::SwapOut::swImpossible;
429 return false; // known to outgrow the limit eventually
430 }
431
432 // use current minimum (always known)
433 const int64_t currentEnd = mem_obj->endOffset();
434 if (currentEnd > store_maxobjsize) {
435 debugs(20, 3, HERE << "does not fit: " << currentEnd <<
436 " > " << store_maxobjsize);
437 decision = MemObject::SwapOut::swImpossible;
438 return false; // already does not fit and may only get bigger
439 }
440
441 // prevent final default swPossible answer for yet unknown length
442 if (expectedEnd < 0 && store_status != STORE_OK) {
443 const int64_t maxKnownSize = mem_obj->availableForSwapOut();
444 debugs(20, 7, HERE << "maxKnownSize= " << maxKnownSize);
445 /*
446 * NOTE: the store_maxobjsize here is the global maximum
447 * size of object cacheable in any of Squid cache stores
448 * both disk and memory stores.
449 *
450 * However, I am worried that this
451 * deferance may consume a lot of memory in some cases.
452 * Should we add an option to limit this memory consumption?
453 */
454 debugs(20, 5, HERE << "Deferring swapout start for " <<
455 (store_maxobjsize - maxKnownSize) << " bytes");
456 return true; // may still fit, but no final decision yet
457 }
458 }
459
460 decision = MemObject::SwapOut::swPossible;
461 return true;
462 }