]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemObject.cc
Fix various uninitialized class members
[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(NULL),
100 ping_reply_callback(NULL),
101 ircb_data(NULL),
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(NULL)
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->Printf("\t" SQUIDSBUFPH " %s\n", SQUIDSBUFPRINT(method.image()), logUri());
233 if (vary_headers)
234 mb->Printf("\tvary_headers: %s\n", vary_headers);
235 mb->Printf("\tinmem_lo: %" PRId64 "\n", inmem_lo);
236 mb->Printf("\tinmem_hi: %" PRId64 "\n", data_hdr.endOffset());
237 mb->Printf("\tswapout: %" PRId64 " bytes queued\n",
238 swapout.queue_offset);
239
240 if (swapout.sio.getRaw())
241 mb->Printf("\tswapout: %" PRId64 " bytes written\n",
242 (int64_t) swapout.sio->offset());
243
244 if (xitTable.index >= 0)
245 mb->Printf("\ttransient index: %d state: %d\n",
246 xitTable.index, xitTable.io);
247 if (memCache.index >= 0)
248 mb->Printf("\tmem-cache index: %d state: %d offset: %" PRId64 "\n",
249 memCache.index, memCache.io, memCache.offset);
250 if (object_sz >= 0)
251 mb->Printf("\tobject_sz: %" PRId64 "\n", object_sz);
252 if (smpCollapsed)
253 mb->Printf("\tsmp-collapsed\n");
254
255 StoreClientStats statsVisitor(mb);
256
257 for_each<StoreClientStats>(clients, statsVisitor);
258 }
259
260 int64_t
261 MemObject::endOffset () const
262 {
263 return data_hdr.endOffset();
264 }
265
266 void
267 MemObject::markEndOfReplyHeaders()
268 {
269 const int hdr_sz = endOffset();
270 assert(hdr_sz >= 0);
271 assert(_reply);
272 _reply->hdr_sz = hdr_sz;
273 }
274
275 int64_t
276 MemObject::size() const
277 {
278 if (object_sz < 0)
279 return endOffset();
280
281 return object_sz;
282 }
283
284 int64_t
285 MemObject::expectedReplySize() const
286 {
287 debugs(20, 7, HERE << "object_sz: " << object_sz);
288 if (object_sz >= 0) // complete() has been called; we know the exact answer
289 return object_sz;
290
291 if (_reply) {
292 const int64_t clen = _reply->bodySize(method);
293 debugs(20, 7, HERE << "clen: " << clen);
294 if (clen >= 0 && _reply->hdr_sz > 0) // yuck: HttpMsg sets hdr_sz to 0
295 return clen + _reply->hdr_sz;
296 }
297
298 return -1; // not enough information to predict
299 }
300
301 void
302 MemObject::reset()
303 {
304 assert(swapout.sio == NULL);
305 data_hdr.freeContent();
306 inmem_lo = 0;
307 /* Should we check for clients? */
308 }
309
310 int64_t
311 MemObject::lowestMemReaderOffset() const
312 {
313 LowestMemReader lowest (endOffset() + 1);
314
315 for_each <LowestMemReader>(clients, lowest);
316
317 return lowest.current;
318 }
319
320 /* XXX: This is wrong. It breaks *badly* on range combining */
321 bool
322 MemObject::readAheadPolicyCanRead() const
323 {
324 const bool canRead = endOffset() - getReply()->hdr_sz <
325 lowestMemReaderOffset() + Config.readAheadGap;
326
327 if (!canRead) {
328 debugs(19, 9, "no: " << endOffset() << '-' << getReply()->hdr_sz <<
329 " < " << lowestMemReaderOffset() << '+' << Config.readAheadGap);
330 }
331
332 return canRead;
333 }
334
335 void
336 MemObject::addClient(store_client *aClient)
337 {
338 ++nclients;
339 dlinkAdd(aClient, &aClient->node, &clients);
340 }
341
342 #if URL_CHECKSUM_DEBUG
343 void
344 MemObject::checkUrlChecksum () const
345 {
346 assert(chksum == url_checksum(urlXXX()));
347 }
348
349 #endif
350
351 /*
352 * How much of the object data is on the disk?
353 */
354 int64_t
355 MemObject::objectBytesOnDisk() const
356 {
357 /*
358 * NOTE: storeOffset() represents the disk file size,
359 * not the amount of object data on disk.
360 *
361 * If we don't have at least 'swap_hdr_sz' bytes
362 * then none of the object data is on disk.
363 *
364 * This should still be safe if swap_hdr_sz == 0,
365 * meaning we haven't even opened the swapout file
366 * yet.
367 */
368
369 if (swapout.sio.getRaw() == NULL)
370 return 0;
371
372 int64_t nwritten = swapout.sio->offset();
373
374 if (nwritten <= (int64_t)swap_hdr_sz)
375 return 0;
376
377 return (nwritten - swap_hdr_sz);
378 }
379
380 int64_t
381 MemObject::policyLowestOffsetToKeep(bool swap) const
382 {
383 /*
384 * Careful. lowest_offset can be greater than endOffset(), such
385 * as in the case of a range request.
386 */
387 int64_t lowest_offset = lowestMemReaderOffset();
388
389 if (endOffset() < lowest_offset ||
390 endOffset() - inmem_lo > (int64_t)Config.Store.maxInMemObjSize ||
391 (swap && !Config.onoff.memory_cache_first))
392 return lowest_offset;
393
394 return inmem_lo;
395 }
396
397 void
398 MemObject::trimSwappable()
399 {
400 int64_t new_mem_lo = policyLowestOffsetToKeep(1);
401 /*
402 * We should only free up to what we know has been written
403 * to disk, not what has been queued for writing. Otherwise
404 * there will be a chunk of the data which is not in memory
405 * and is not yet on disk.
406 * The -1 makes sure the page isn't freed until storeSwapOut has
407 * walked to the next page.
408 */
409 int64_t on_disk;
410
411 if ((on_disk = objectBytesOnDisk()) - 1 < new_mem_lo)
412 new_mem_lo = on_disk - 1;
413
414 if (new_mem_lo == -1)
415 new_mem_lo = 0; /* the above might become -1 */
416
417 data_hdr.freeDataUpto(new_mem_lo);
418
419 inmem_lo = new_mem_lo;
420 }
421
422 void
423 MemObject::trimUnSwappable()
424 {
425 if (const int64_t new_mem_lo = policyLowestOffsetToKeep(false)) {
426 assert (new_mem_lo > 0);
427 data_hdr.freeDataUpto(new_mem_lo);
428 inmem_lo = new_mem_lo;
429 } // else we should not trim anything at this time
430 }
431
432 bool
433 MemObject::isContiguous() const
434 {
435 bool result = data_hdr.hasContigousContentRange (Range<int64_t>(inmem_lo, endOffset()));
436 /* XXX : make this higher level */
437 debugs (19, result ? 4 :3, "MemObject::isContiguous: Returning " << (result ? "true" : "false"));
438 return result;
439 }
440
441 int
442 MemObject::mostBytesWanted(int max, bool ignoreDelayPools) const
443 {
444 #if USE_DELAY_POOLS
445 if (!ignoreDelayPools) {
446 /* identify delay id with largest allowance */
447 DelayId largestAllowance = mostBytesAllowed ();
448 return largestAllowance.bytesWanted(0, max);
449 }
450 #endif
451
452 return max;
453 }
454
455 void
456 MemObject::setNoDelay(bool const newValue)
457 {
458 #if USE_DELAY_POOLS
459
460 for (dlink_node *node = clients.head; node; node = node->next) {
461 store_client *sc = (store_client *) node->data;
462 sc->delayId.setNoDelay(newValue);
463 }
464
465 #endif
466 }
467
468 void
469 MemObject::delayRead(DeferredRead const &aRead)
470 {
471 deferredReads.delayRead(aRead);
472 }
473
474 void
475 MemObject::kickReads()
476 {
477 deferredReads.kickReads(-1);
478 }
479
480 #if USE_DELAY_POOLS
481 DelayId
482 MemObject::mostBytesAllowed() const
483 {
484 int j;
485 int jmax = -1;
486 DelayId result;
487
488 for (dlink_node *node = clients.head; node; node = node->next) {
489 store_client *sc = (store_client *) node->data;
490 #if 0
491 /* This test is invalid because the client may be writing data
492 * and thus will want data immediately.
493 * If we include the test, there is a race condition when too much
494 * data is read - if all sc's are writing when a read is scheduled.
495 * XXX: fixme.
496 */
497
498 if (!sc->callbackPending())
499 /* not waiting for more data */
500 continue;
501
502 #endif
503
504 j = sc->delayId.bytesWanted(0, sc->copyInto.length);
505
506 if (j > jmax) {
507 jmax = j;
508 result = sc->delayId;
509 }
510 }
511
512 return result;
513 }
514
515 #endif
516
517 int64_t
518 MemObject::availableForSwapOut() const
519 {
520 return endOffset() - swapout.queue_offset;
521 }
522