]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
Merge from trunk
[thirdparty/squid.git] / src / MemObject.cc
1 /*
2 * Copyright (C) 1996-2015 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 19 Store Memory Primitives */
10
11 #include "squid.h"
12 #include "comm/Connection.h"
13 #include "Generic.h"
14 #include "globals.h"
15 #include "HttpReply.h"
16 #include "HttpRequest.h"
17 #include "MemBuf.h"
18 #include "MemObject.h"
19 #include "profiler/Profiler.h"
20 #include "SquidConfig.h"
21 #include "Store.h"
22 #include "StoreClient.h"
23
24 #if USE_DELAY_POOLS
25 #include "DelayPools.h"
26 #endif
27
28 /* TODO: make this global or private */
29 #if URL_CHECKSUM_DEBUG
30 static unsigned int url_checksum(const char *url);
31 unsigned int
32 url_checksum(const char *url)
33 {
34 unsigned int ck;
35 SquidMD5_CTX M;
36 static unsigned char digest[16];
37 SquidMD5Init(&M);
38 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
39 SquidMD5Final(digest, &M);
40 memcpy(&ck, digest, sizeof(ck));
41 return ck;
42 }
43
44 #endif
45
46 RemovalPolicy * mem_policy = NULL;
47
48 size_t
49 MemObject::inUseCount()
50 {
51 return Pool().inUseCount();
52 }
53
54 const char *
55 MemObject::storeId() const
56 {
57 if (!storeId_.size()) {
58 debugs(20, DBG_IMPORTANT, "Bug: Missing MemObject::storeId value");
59 dump();
60 storeId_ = "[unknown_URI]";
61 }
62 return storeId_.termedBuf();
63 }
64
65 const char *
66 MemObject::logUri() const
67 {
68 return logUri_.size() ? logUri_.termedBuf() : storeId();
69 }
70
71 bool
72 MemObject::hasUris() const
73 {
74 return storeId_.size();
75 }
76
77 void
78 MemObject::setUris(char const *aStoreId, char const *aLogUri, const HttpRequestMethod &aMethod)
79 {
80 storeId_ = aStoreId;
81
82 // fast pointer comparison for a common storeCreateEntry(url,url,...) case
83 if (!aLogUri || aLogUri == aStoreId)
84 logUri_.clean(); // use storeId_ by default to minimize copying
85 else
86 logUri_ = aLogUri;
87
88 method = aMethod;
89
90 #if URL_CHECKSUM_DEBUG
91 chksum = url_checksum(urlXXX());
92 #endif
93 }
94
95 MemObject::MemObject() :
96 inmem_lo(0),
97 nclients(0),
98 smpCollapsed(false),
99 request(nullptr),
100 ping_reply_callback(nullptr),
101 ircb_data(nullptr),
102 id(0),
103 object_sz(-1),
104 swap_hdr_sz(0),
105 #if URL_CHECKSUM_DEBUG
106 chksum(0),
107 #endif
108 vary_headers(nullptr)
109 {
110 debugs(20, 3, "new MemObject " << this);
111 memset(&start_ping, 0, sizeof(start_ping));
112 memset(&abort, 0, sizeof(abort));
113 _reply = new HttpReply;
114 HTTPMSGLOCK(_reply);
115 }
116
117 MemObject::~MemObject()
118 {
119 debugs(20, 3, "del MemObject " << this);
120 const Ctx ctx = ctx_enter(hasUris() ? urlXXX() : "[unknown_ctx]");
121
122 #if URL_CHECKSUM_DEBUG
123 checkUrlChecksum();
124 #endif
125
126 if (!shutting_down) { // Store::Root() is FATALly missing during shutdown
127 assert(xitTable.index < 0);
128 assert(memCache.index < 0);
129 assert(swapout.sio == NULL);
130 }
131
132 data_hdr.freeContent();
133
134 #if 0
135 /*
136 * There is no way to abort FD-less clients, so they might
137 * still have mem->clients set.
138 */
139 assert(clients.head == NULL);
140
141 #endif
142
143 HTTPMSGUNLOCK(_reply);
144
145 HTTPMSGUNLOCK(request);
146
147 ctx_exit(ctx); /* must exit before we free mem->url */
148
149 safe_free(vary_headers);
150 }
151
152 void
153 MemObject::unlinkRequest()
154 {
155 HTTPMSGUNLOCK(request);
156 }
157
158 void
159 MemObject::write(const StoreIOBuffer &writeBuffer)
160 {
161 PROF_start(MemObject_write);
162 debugs(19, 6, "memWrite: offset " << writeBuffer.offset << " len " << writeBuffer.length);
163
164 /* We don't separate out mime headers yet, so ensure that the first
165 * write is at offset 0 - where they start
166 */
167 assert (data_hdr.endOffset() || writeBuffer.offset == 0);
168
169 assert (data_hdr.write (writeBuffer));
170 PROF_stop(MemObject_write);
171 }
172
173 void
174 MemObject::dump() const
175 {
176 data_hdr.dump();
177 #if 0
178 /* do we want this one? */
179 debugs(20, DBG_IMPORTANT, "MemObject->data.origin_offset: " << (data_hdr.head ? data_hdr.head->nodeBuffer.offset : 0));
180 #endif
181
182 debugs(20, DBG_IMPORTANT, "MemObject->start_ping: " << start_ping.tv_sec << "."<< std::setfill('0') << std::setw(6) << start_ping.tv_usec);
183 debugs(20, DBG_IMPORTANT, "MemObject->inmem_hi: " << data_hdr.endOffset());
184 debugs(20, DBG_IMPORTANT, "MemObject->inmem_lo: " << inmem_lo);
185 debugs(20, DBG_IMPORTANT, "MemObject->nclients: " << nclients);
186 debugs(20, DBG_IMPORTANT, "MemObject->reply: " << _reply);
187 debugs(20, DBG_IMPORTANT, "MemObject->request: " << request);
188 debugs(20, DBG_IMPORTANT, "MemObject->logUri: " << logUri_);
189 debugs(20, DBG_IMPORTANT, "MemObject->storeId: " << storeId_);
190 }
191
192 HttpReply const *
193 MemObject::getReply() const
194 {
195 return _reply;
196 }
197
198 void
199 MemObject::replaceHttpReply(HttpReply *newrep)
200 {
201 HTTPMSGUNLOCK(_reply);
202 _reply = newrep;
203 HTTPMSGLOCK(_reply);
204 }
205
206 struct LowestMemReader : public unary_function<store_client, void> {
207 LowestMemReader(int64_t seed):current(seed) {}
208
209 void operator() (store_client const &x) {
210 if (x.memReaderHasLowerOffset(current))
211 current = x.copyInto.offset;
212 }
213
214 int64_t current;
215 };
216
217 struct StoreClientStats : public unary_function<store_client, void> {
218 StoreClientStats(MemBuf *anEntry):where(anEntry),index(0) {}
219
220 void operator()(store_client const &x) {
221 x.dumpStats(where, index);
222 ++index;
223 }
224
225 MemBuf *where;
226 size_t index;
227 };
228
229 void
230 MemObject::stat(MemBuf * mb) const
231 {
232 mb->appendf("\t" SQUIDSBUFPH " %s\n", SQUIDSBUFPRINT(method.image()), logUri());
233 if (vary_headers)
234 mb->appendf("\tvary_headers: %s\n", vary_headers);
235 mb->appendf("\tinmem_lo: %" PRId64 "\n", inmem_lo);
236 mb->appendf("\tinmem_hi: %" PRId64 "\n", data_hdr.endOffset());
237 mb->appendf("\tswapout: %" PRId64 " bytes queued\n", swapout.queue_offset);
238
239 if (swapout.sio.getRaw())
240 mb->appendf("\tswapout: %" PRId64 " bytes written\n", (int64_t) swapout.sio->offset());
241
242 if (xitTable.index >= 0)
243 mb->appendf("\ttransient index: %d state: %d\n", xitTable.index, xitTable.io);
244 if (memCache.index >= 0)
245 mb->appendf("\tmem-cache index: %d state: %d offset: %" PRId64 "\n", memCache.index, memCache.io, memCache.offset);
246 if (object_sz >= 0)
247 mb->appendf("\tobject_sz: %" PRId64 "\n", object_sz);
248 if (smpCollapsed)
249 mb->appendf("\tsmp-collapsed\n");
250
251 StoreClientStats statsVisitor(mb);
252
253 for_each<StoreClientStats>(clients, statsVisitor);
254 }
255
256 int64_t
257 MemObject::endOffset () const
258 {
259 return data_hdr.endOffset();
260 }
261
262 void
263 MemObject::markEndOfReplyHeaders()
264 {
265 const int hdr_sz = endOffset();
266 assert(hdr_sz >= 0);
267 assert(_reply);
268 _reply->hdr_sz = hdr_sz;
269 }
270
271 int64_t
272 MemObject::size() const
273 {
274 if (object_sz < 0)
275 return endOffset();
276
277 return object_sz;
278 }
279
280 int64_t
281 MemObject::expectedReplySize() const
282 {
283 debugs(20, 7, HERE << "object_sz: " << object_sz);
284 if (object_sz >= 0) // complete() has been called; we know the exact answer
285 return object_sz;
286
287 if (_reply) {
288 const int64_t clen = _reply->bodySize(method);
289 debugs(20, 7, HERE << "clen: " << clen);
290 if (clen >= 0 && _reply->hdr_sz > 0) // yuck: HttpMsg sets hdr_sz to 0
291 return clen + _reply->hdr_sz;
292 }
293
294 return -1; // not enough information to predict
295 }
296
297 void
298 MemObject::reset()
299 {
300 assert(swapout.sio == NULL);
301 data_hdr.freeContent();
302 inmem_lo = 0;
303 /* Should we check for clients? */
304 }
305
306 int64_t
307 MemObject::lowestMemReaderOffset() const
308 {
309 LowestMemReader lowest (endOffset() + 1);
310
311 for_each <LowestMemReader>(clients, lowest);
312
313 return lowest.current;
314 }
315
316 /* XXX: This is wrong. It breaks *badly* on range combining */
317 bool
318 MemObject::readAheadPolicyCanRead() const
319 {
320 const bool canRead = endOffset() - getReply()->hdr_sz <
321 lowestMemReaderOffset() + Config.readAheadGap;
322
323 if (!canRead) {
324 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz <<
325 " < " << lowestMemReaderOffset() << '+' << Config.readAheadGap);
326 }
327
328 return canRead;
329 }
330
331 void
332 MemObject::addClient(store_client *aClient)
333 {
334 ++nclients;
335 dlinkAdd(aClient, &aClient->node, &clients);
336 }
337
338 #if URL_CHECKSUM_DEBUG
339 void
340 MemObject::checkUrlChecksum () const
341 {
342 assert(chksum == url_checksum(urlXXX()));
343 }
344
345 #endif
346
347 /*
348 * How much of the object data is on the disk?
349 */
350 int64_t
351 MemObject::objectBytesOnDisk() const
352 {
353 /*
354 * NOTE: storeOffset() represents the disk file size,
355 * not the amount of object data on disk.
356 *
357 * If we don't have at least 'swap_hdr_sz' bytes
358 * then none of the object data is on disk.
359 *
360 * This should still be safe if swap_hdr_sz == 0,
361 * meaning we haven't even opened the swapout file
362 * yet.
363 */
364
365 if (swapout.sio.getRaw() == NULL)
366 return 0;
367
368 int64_t nwritten = swapout.sio->offset();
369
370 if (nwritten <= (int64_t)swap_hdr_sz)
371 return 0;
372
373 return (nwritten - swap_hdr_sz);
374 }
375
376 int64_t
377 MemObject::policyLowestOffsetToKeep(bool swap) const
378 {
379 /*
380 * Careful. lowest_offset can be greater than endOffset(), such
381 * as in the case of a range request.
382 */
383 int64_t lowest_offset = lowestMemReaderOffset();
384
385 if (endOffset() < lowest_offset ||
386 endOffset() - inmem_lo > (int64_t)Config.Store.maxInMemObjSize ||
387 (swap && !Config.onoff.memory_cache_first))
388 return lowest_offset;
389
390 return inmem_lo;
391 }
392
393 void
394 MemObject::trimSwappable()
395 {
396 int64_t new_mem_lo = policyLowestOffsetToKeep(1);
397 /*
398 * We should only free up to what we know has been written
399 * to disk, not what has been queued for writing. Otherwise
400 * there will be a chunk of the data which is not in memory
401 * and is not yet on disk.
402 * The -1 makes sure the page isn't freed until storeSwapOut has
403 * walked to the next page.
404 */
405 int64_t on_disk;
406
407 if ((on_disk = objectBytesOnDisk()) - 1 < new_mem_lo)
408 new_mem_lo = on_disk - 1;
409
410 if (new_mem_lo == -1)
411 new_mem_lo = 0; /* the above might become -1 */
412
413 data_hdr.freeDataUpto(new_mem_lo);
414
415 inmem_lo = new_mem_lo;
416 }
417
418 void
419 MemObject::trimUnSwappable()
420 {
421 if (const int64_t new_mem_lo = policyLowestOffsetToKeep(false)) {
422 assert (new_mem_lo > 0);
423 data_hdr.freeDataUpto(new_mem_lo);
424 inmem_lo = new_mem_lo;
425 } // else we should not trim anything at this time
426 }
427
428 bool
429 MemObject::isContiguous() const
430 {
431 bool result = data_hdr.hasContigousContentRange (Range<int64_t>(inmem_lo, endOffset()));
432 /* XXX : make this higher level */
433 debugs (19, result ? 4 :3, "MemObject::isContiguous: Returning " << (result ? "true" : "false"));
434 return result;
435 }
436
437 int
438 MemObject::mostBytesWanted(int max, bool ignoreDelayPools) const
439 {
440 #if USE_DELAY_POOLS
441 if (!ignoreDelayPools) {
442 /* identify delay id with largest allowance */
443 DelayId largestAllowance = mostBytesAllowed ();
444 return largestAllowance.bytesWanted(0, max);
445 }
446 #endif
447
448 return max;
449 }
450
451 void
452 MemObject::setNoDelay(bool const newValue)
453 {
454 #if USE_DELAY_POOLS
455
456 for (dlink_node *node = clients.head; node; node = node->next) {
457 store_client *sc = (store_client *) node->data;
458 sc->delayId.setNoDelay(newValue);
459 }
460
461 #endif
462 }
463
464 void
465 MemObject::delayRead(DeferredRead const &aRead)
466 {
467 deferredReads.delayRead(aRead);
468 }
469
470 void
471 MemObject::kickReads()
472 {
473 deferredReads.kickReads(-1);
474 }
475
476 #if USE_DELAY_POOLS
477 DelayId
478 MemObject::mostBytesAllowed() const
479 {
480 int j;
481 int jmax = -1;
482 DelayId result;
483
484 for (dlink_node *node = clients.head; node; node = node->next) {
485 store_client *sc = (store_client *) node->data;
486 #if 0
487 /* This test is invalid because the client may be writing data
488 * and thus will want data immediately.
489 * If we include the test, there is a race condition when too much
490 * data is read - if all sc's are writing when a read is scheduled.
491 * XXX: fixme.
492 */
493
494 if (!sc->callbackPending())
495 /* not waiting for more data */
496 continue;
497
498 #endif
499
500 j = sc->delayId.bytesWanted(0, sc->copyInto.length);
501
502 if (j > jmax) {
503 jmax = j;
504 result = sc->delayId;
505 }
506 }
507
508 return result;
509 }
510
511 #endif
512
513 int64_t
514 MemObject::availableForSwapOut() const
515 {
516 return endOffset() - swapout.queue_offset;
517 }
518