2 * DEBUG: section 79 Squid-side DISKD I/O functions.
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 #include "comm/Loops.h"
36 #include "ConfigOption.h"
37 #include "DiskdIOStrategy.h"
38 #include "DiskIO/DiskFile.h"
39 #include "DiskdFile.h"
43 #include "StatCounters.h"
44 #include "SquidConfig.h"
46 #include "SquidTime.h"
56 diskd_stats_t diskd_stats
;
58 size_t DiskdIOStrategy::nextInstanceID (0);
59 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
62 DiskdIOStrategy::newInstance()
64 return ++nextInstanceID
;
68 DiskdIOStrategy::shedLoad()
71 * Fail on open() if there are too many requests queued.
75 debugs(79, 3, "storeDiskdIO::shedLoad: Shedding, too many requests away");
84 DiskdIOStrategy::load()
86 /* Calculate the storedir load relative to magic2 on a scale of 0 .. 1000 */
87 /* the parse function guarantees magic2 is positivie */
88 return away
* 1000 / magic2
;
92 DiskdIOStrategy::openFailed()
94 ++diskd_stats
.open_fail_queue_len
;
98 DiskdIOStrategy::newFile(char const *path
)
105 return new DiskdFile (path
, this);
108 DiskdIOStrategy::DiskdIOStrategy() : magic1(64), magic2(72), away(0) , smsgid(-1), rmsgid(-1), wfd(-1) , instanceID(newInstance())
112 DiskdIOStrategy::unlinkdUseful() const
118 DiskdIOStrategy::unlinkFile(char const *path
)
121 /* Damn, we need to issue a sync unlink here :( */
122 debugs(79, 2, "storeDiskUnlink: Out of queue space, sync unlink");
127 /* We can attempt a diskd unlink */
134 buf
= (char *)shm
.get(&shm_offset
);
136 xstrncpy(buf
, path
, SHMBUF_BLKSZ
);
138 x
= send(_MQD_UNLINK
,
140 (StoreIOState::Pointer
)NULL
,
146 debugs(79, DBG_IMPORTANT
, "storeDiskdSend UNLINK: " << xstrerror());
147 ::unlink(buf
); /* XXX EWW! */
148 // shm.put (shm_offset);
151 ++diskd_stats
.unlink
.ops
;
155 DiskdIOStrategy::init()
165 Ip::Address localhost
;
167 ikey
= (getpid() << 10) + (instanceID
<< 2);
169 smsgid
= msgget((key_t
) ikey
, 0700 | IPC_CREAT
);
172 debugs(50, DBG_CRITICAL
, "storeDiskdInit: msgget: " << xstrerror());
173 fatal("msgget failed");
176 rmsgid
= msgget((key_t
) (ikey
+ 1), 0700 | IPC_CREAT
);
179 debugs(50, DBG_CRITICAL
, "storeDiskdInit: msgget: " << xstrerror());
180 fatal("msgget failed");
183 shm
.init(ikey
, magic2
);
184 snprintf(skey1
, 32, "%d", ikey
);
185 snprintf(skey2
, 32, "%d", ikey
+ 1);
186 snprintf(skey3
, 32, "%d", ikey
+ 2);
192 localhost
.SetLocalhost();
193 pid
= ipcCreate(IPC_STREAM
,
194 Config
.Program
.diskd
,
203 fatalf("execl: %s", Config
.Program
.diskd
);
208 fd_note(wfd
, "squid -> diskd");
210 commUnsetFdTimeout(wfd
);
211 commSetNonBlocking(wfd
);
212 Comm::QuickPollRequired();
216 * SHM manipulation routines
219 SharedMemory::put(ssize_t offset
)
223 assert(offset
< nbufs
* SHMBUF_BLKSZ
);
224 i
= offset
/ SHMBUF_BLKSZ
;
226 assert(CBIT_TEST(inuse_map
, i
));
227 CBIT_CLR(inuse_map
, i
);
228 --diskd_stats
.shmbuf_count
;
233 SharedMemory::get(ssize_t
* shm_offset
)
238 for (i
= 0; i
< nbufs
; ++i
) {
239 if (CBIT_TEST(inuse_map
, i
))
242 CBIT_SET(inuse_map
, i
);
244 *shm_offset
= i
* SHMBUF_BLKSZ
;
246 aBuf
= buf
+ (*shm_offset
);
253 assert(aBuf
< buf
+ (nbufs
* SHMBUF_BLKSZ
));
254 ++diskd_stats
.shmbuf_count
;
256 if (diskd_stats
.max_shmuse
< diskd_stats
.shmbuf_count
)
257 diskd_stats
.max_shmuse
= diskd_stats
.shmbuf_count
;
263 SharedMemory::init(int ikey
, int magic2
)
265 nbufs
= (int)(magic2
* 1.3);
266 id
= shmget((key_t
) (ikey
+ 2),
267 nbufs
* SHMBUF_BLKSZ
, 0600 | IPC_CREAT
);
270 debugs(50, DBG_CRITICAL
, "storeDiskdInit: shmget: " << xstrerror());
271 fatal("shmget failed");
274 buf
= (char *)shmat(id
, NULL
, 0);
276 if (buf
== (void *) -1) {
277 debugs(50, DBG_CRITICAL
, "storeDiskdInit: shmat: " << xstrerror());
278 fatal("shmat failed");
281 inuse_map
= (char *)xcalloc((nbufs
+ 7) / 8, 1);
282 diskd_stats
.shmbuf_count
+= nbufs
;
284 for (int i
= 0; i
< nbufs
; ++i
) {
285 CBIT_SET(inuse_map
, i
);
286 put (i
* SHMBUF_BLKSZ
);
291 DiskdIOStrategy::unlinkDone(diomsg
* M
)
293 debugs(79, 3, "storeDiskdUnlinkDone: file " << shm
.buf
+ M
->shm_offset
<< " status " << M
->status
);
294 ++statCounter
.syscalls
.disk
.unlinks
;
297 ++diskd_stats
.unlink
.fail
;
299 ++diskd_stats
.unlink
.success
;
303 DiskdIOStrategy::handle(diomsg
* M
)
305 if (!cbdataReferenceValid (M
->callback_data
)) {
306 /* I.e. already closed file
307 * - say when we have a error opening after
308 * a read was already queued
310 debugs(79, 3, "storeDiskdHandle: Invalid callback_data " << M
->callback_data
);
311 cbdataReferenceDone (M
->callback_data
);
315 /* set errno passed from diskd. makes debugging more meaningful */
320 DiskdFile
*theFile
= (DiskdFile
*)M
->callback_data
;
321 theFile
->RefCountDereference();
322 theFile
->completed (M
);
347 cbdataReferenceDone (M
->callback_data
);
351 DiskdIOStrategy::send(int mtype
, int id
, DiskdFile
*theFile
, size_t size
, off_t offset
, ssize_t shm_offset
, RefCountable_
*requestor
)
354 M
.callback_data
= cbdataReference(theFile
);
355 theFile
->RefCountReference();
356 M
.requestor
= requestor
;
360 requestor
->RefCountReference();
362 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
366 DiskdIOStrategy::send(int mtype
, int id
, RefCount
<StoreIOState
> sio
, size_t size
, off_t offset
, ssize_t shm_offset
)
369 M
.callback_data
= cbdataReference(sio
.getRaw());
372 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
376 DiskdIOStrategy::SEND(diomsg
*M
, int mtype
, int id
, size_t size
, off_t offset
, ssize_t shm_offset
)
378 static int send_errors
= 0;
379 static int last_seq_no
= 0;
380 static int seq_no
= 0;
387 M
->shm_offset
= (int) shm_offset
;
389 M
->seq_no
= ++seq_no
;
391 if (M
->seq_no
< last_seq_no
)
392 debugs(79, DBG_IMPORTANT
, "WARNING: sequencing out of order");
394 x
= msgsnd(smsgid
, M
, diomsg::msg_snd_rcv_sz
, IPC_NOWAIT
);
396 last_seq_no
= M
->seq_no
;
399 ++diskd_stats
.sent_count
;
402 debugs(79, DBG_IMPORTANT
, "storeDiskdSend: msgsnd: " << xstrerror());
403 cbdataReferenceDone(M
->callback_data
);
404 assert(++send_errors
< 100);
410 * We have to drain the queue here if necessary. If we don't,
411 * then we can have a lot of messages in the queue (probably
412 * up to 2*magic1) and we can run out of shared memory buffers.
415 * Note that we call Store::Root().callbackk (for all SDs), rather
416 * than callback for just this SD, so that while
417 * we're "blocking" on this SD we can also handle callbacks
418 * from other SDs that might be ready.
421 struct timeval delay
= {0, 1};
423 while (away
> magic2
) {
424 select(0, NULL
, NULL
, NULL
, &delay
);
425 Store::Root().callback();
427 if (delay
.tv_usec
< 1000000)
435 DiskdIOStrategy::getOptionTree() const
437 ConfigOptionVector
*result
= new ConfigOptionVector
;
438 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ1Parse
, &DiskdIOStrategy::optionQ1Dump
));
439 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ2Parse
, &DiskdIOStrategy::optionQ2Dump
));
444 DiskdIOStrategy::optionQ1Parse(const char *name
, const char *value
, int isaReconfig
)
446 if (strcmp(name
, "Q1") != 0)
449 int old_magic1
= magic1
;
451 magic1
= atoi(value
);
456 if (old_magic1
< magic1
) {
458 * This is because shm.nbufs is computed at startup, when
459 * we call shmget(). We can't increase the Q1/Q2 parameters
460 * beyond their initial values because then we might have
461 * more "Q2 messages" than shared memory chunks, and this
462 * will cause an assertion in storeDiskdShmGet().
464 /* TODO: have DiskdIO hold a link to the swapdir, to allow detailed reporting again */
465 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q1 value while Squid is running.");
470 if (old_magic1
!= magic1
)
471 debugs(3, DBG_IMPORTANT
, "cache_dir new Q1 value '" << magic1
<< "'");
477 DiskdIOStrategy::optionQ1Dump(StoreEntry
* e
) const
479 storeAppendPrintf(e
, " Q1=%d", magic1
);
483 DiskdIOStrategy::optionQ2Parse(const char *name
, const char *value
, int isaReconfig
)
485 if (strcmp(name
, "Q2") != 0)
488 int old_magic2
= magic2
;
490 magic2
= atoi(value
);
495 if (old_magic2
< magic2
) {
496 /* See comments in Q1 function above */
497 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q2 value while Squid is running.");
502 if (old_magic2
!= magic2
)
503 debugs(3, DBG_IMPORTANT
, "cache_dir new Q2 value '" << magic2
<< "'");
509 DiskdIOStrategy::optionQ2Dump(StoreEntry
* e
) const
511 storeAppendPrintf(e
, " Q2=%d", magic2
);
515 * Sync any pending data. We just sit around and read the queue
516 * until the data has finished writing.
519 DiskdIOStrategy::sync()
521 static time_t lastmsg
= 0;
524 if (squid_curtime
> lastmsg
) {
525 debugs(47, DBG_IMPORTANT
, "storeDiskdDirSync: " << away
<< " messages away");
526 lastmsg
= squid_curtime
;
534 * Handle callbacks. If we have more than magic2 requests away, we block
535 * until the queue is below magic2. Otherwise, we simply return when we
536 * don't get a message.
540 DiskdIOStrategy::callback()
546 if (away
>= magic2
) {
547 ++diskd_stats
.block_queue_len
;
549 /* We might not have anything to do, but our queue
553 if (diskd_stats
.sent_count
- diskd_stats
.recv_count
>
554 diskd_stats
.max_away
) {
555 diskd_stats
.max_away
= diskd_stats
.sent_count
- diskd_stats
.recv_count
;
559 #ifdef ALWAYS_ZERO_BUFFERS
560 memset(&M
, '\0', sizeof(M
));
563 x
= msgrcv(rmsgid
, &M
, diomsg::msg_snd_rcv_sz
, 0, IPC_NOWAIT
);
567 else if (x
!= diomsg::msg_snd_rcv_sz
) {
568 debugs(47, DBG_IMPORTANT
, "storeDiskdDirCallback: msgget returns " << x
);
572 ++diskd_stats
.recv_count
;
575 retval
= 1; /* Return that we've actually done some work */
577 if (M
.shm_offset
> -1)
578 shm
.put ((off_t
) M
.shm_offset
);
585 DiskdIOStrategy::statfs(StoreEntry
& sentry
)const
587 storeAppendPrintf(&sentry
, "Pending operations: %d\n", away
);