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"
45 #include "SquidTime.h"
55 diskd_stats_t diskd_stats
;
57 size_t DiskdIOStrategy::nextInstanceID (0);
58 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
61 DiskdIOStrategy::newInstance()
63 return ++nextInstanceID
;
67 DiskdIOStrategy::shedLoad()
70 * Fail on open() if there are too many requests queued.
74 debugs(79, 3, "storeDiskdIO::shedLoad: Shedding, too many requests away");
83 DiskdIOStrategy::load()
85 /* Calculate the storedir load relative to magic2 on a scale of 0 .. 1000 */
86 /* the parse function guarantees magic2 is positivie */
87 return away
* 1000 / magic2
;
91 DiskdIOStrategy::openFailed()
93 ++diskd_stats
.open_fail_queue_len
;
97 DiskdIOStrategy::newFile(char const *path
)
104 return new DiskdFile (path
, this);
107 DiskdIOStrategy::DiskdIOStrategy() : magic1(64), magic2(72), away(0) , smsgid(-1), rmsgid(-1), wfd(-1) , instanceID(newInstance())
111 DiskdIOStrategy::unlinkdUseful() const
117 DiskdIOStrategy::unlinkFile(char const *path
)
120 /* Damn, we need to issue a sync unlink here :( */
121 debugs(79, 2, "storeDiskUnlink: Out of queue space, sync unlink");
126 /* We can attempt a diskd unlink */
133 buf
= (char *)shm
.get(&shm_offset
);
135 xstrncpy(buf
, path
, SHMBUF_BLKSZ
);
137 x
= send(_MQD_UNLINK
,
139 (StoreIOState::Pointer
)NULL
,
145 debugs(79, DBG_IMPORTANT
, "storeDiskdSend UNLINK: " << xstrerror());
146 ::unlink(buf
); /* XXX EWW! */
147 // shm.put (shm_offset);
150 ++diskd_stats
.unlink
.ops
;
154 DiskdIOStrategy::init()
164 Ip::Address localhost
;
166 ikey
= (getpid() << 10) + (instanceID
<< 2);
168 smsgid
= msgget((key_t
) ikey
, 0700 | IPC_CREAT
);
171 debugs(50, DBG_CRITICAL
, "storeDiskdInit: msgget: " << xstrerror());
172 fatal("msgget failed");
175 rmsgid
= msgget((key_t
) (ikey
+ 1), 0700 | IPC_CREAT
);
178 debugs(50, DBG_CRITICAL
, "storeDiskdInit: msgget: " << xstrerror());
179 fatal("msgget failed");
182 shm
.init(ikey
, magic2
);
183 snprintf(skey1
, 32, "%d", ikey
);
184 snprintf(skey2
, 32, "%d", ikey
+ 1);
185 snprintf(skey3
, 32, "%d", ikey
+ 2);
191 localhost
.SetLocalhost();
192 pid
= ipcCreate(IPC_STREAM
,
193 Config
.Program
.diskd
,
202 fatalf("execl: %s", Config
.Program
.diskd
);
207 fd_note(wfd
, "squid -> diskd");
209 commUnsetFdTimeout(wfd
);
210 commSetNonBlocking(wfd
);
211 Comm::QuickPollRequired();
215 * SHM manipulation routines
218 SharedMemory::put(ssize_t offset
)
222 assert(offset
< nbufs
* SHMBUF_BLKSZ
);
223 i
= offset
/ SHMBUF_BLKSZ
;
225 assert(CBIT_TEST(inuse_map
, i
));
226 CBIT_CLR(inuse_map
, i
);
227 --diskd_stats
.shmbuf_count
;
232 SharedMemory::get(ssize_t
* shm_offset
)
237 for (i
= 0; i
< nbufs
; ++i
) {
238 if (CBIT_TEST(inuse_map
, i
))
241 CBIT_SET(inuse_map
, i
);
243 *shm_offset
= i
* SHMBUF_BLKSZ
;
245 aBuf
= buf
+ (*shm_offset
);
252 assert(aBuf
< buf
+ (nbufs
* SHMBUF_BLKSZ
));
253 ++diskd_stats
.shmbuf_count
;
255 if (diskd_stats
.max_shmuse
< diskd_stats
.shmbuf_count
)
256 diskd_stats
.max_shmuse
= diskd_stats
.shmbuf_count
;
262 SharedMemory::init(int ikey
, int magic2
)
264 nbufs
= (int)(magic2
* 1.3);
265 id
= shmget((key_t
) (ikey
+ 2),
266 nbufs
* SHMBUF_BLKSZ
, 0600 | IPC_CREAT
);
269 debugs(50, DBG_CRITICAL
, "storeDiskdInit: shmget: " << xstrerror());
270 fatal("shmget failed");
273 buf
= (char *)shmat(id
, NULL
, 0);
275 if (buf
== (void *) -1) {
276 debugs(50, DBG_CRITICAL
, "storeDiskdInit: shmat: " << xstrerror());
277 fatal("shmat failed");
280 inuse_map
= (char *)xcalloc((nbufs
+ 7) / 8, 1);
281 diskd_stats
.shmbuf_count
+= nbufs
;
283 for (int i
= 0; i
< nbufs
; ++i
) {
284 CBIT_SET(inuse_map
, i
);
285 put (i
* SHMBUF_BLKSZ
);
290 DiskdIOStrategy::unlinkDone(diomsg
* M
)
292 debugs(79, 3, "storeDiskdUnlinkDone: file " << shm
.buf
+ M
->shm_offset
<< " status " << M
->status
);
293 ++statCounter
.syscalls
.disk
.unlinks
;
296 ++diskd_stats
.unlink
.fail
;
298 ++diskd_stats
.unlink
.success
;
302 DiskdIOStrategy::handle(diomsg
* M
)
304 if (!cbdataReferenceValid (M
->callback_data
)) {
305 /* I.e. already closed file
306 * - say when we have a error opening after
307 * a read was already queued
309 debugs(79, 3, "storeDiskdHandle: Invalid callback_data " << M
->callback_data
);
310 cbdataReferenceDone (M
->callback_data
);
314 /* set errno passed from diskd. makes debugging more meaningful */
319 DiskdFile
*theFile
= (DiskdFile
*)M
->callback_data
;
320 theFile
->RefCountDereference();
321 theFile
->completed (M
);
346 cbdataReferenceDone (M
->callback_data
);
350 DiskdIOStrategy::send(int mtype
, int id
, DiskdFile
*theFile
, size_t size
, off_t offset
, ssize_t shm_offset
, RefCountable_
*requestor
)
353 M
.callback_data
= cbdataReference(theFile
);
354 theFile
->RefCountReference();
355 M
.requestor
= requestor
;
359 requestor
->RefCountReference();
361 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
365 DiskdIOStrategy::send(int mtype
, int id
, RefCount
<StoreIOState
> sio
, size_t size
, off_t offset
, ssize_t shm_offset
)
368 M
.callback_data
= cbdataReference(sio
.getRaw());
371 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
375 DiskdIOStrategy::SEND(diomsg
*M
, int mtype
, int id
, size_t size
, off_t offset
, ssize_t shm_offset
)
377 static int send_errors
= 0;
378 static int last_seq_no
= 0;
379 static int seq_no
= 0;
386 M
->shm_offset
= (int) shm_offset
;
388 M
->seq_no
= ++seq_no
;
390 if (M
->seq_no
< last_seq_no
)
391 debugs(79, DBG_IMPORTANT
, "WARNING: sequencing out of order");
393 x
= msgsnd(smsgid
, M
, diomsg::msg_snd_rcv_sz
, IPC_NOWAIT
);
395 last_seq_no
= M
->seq_no
;
398 ++diskd_stats
.sent_count
;
401 debugs(79, DBG_IMPORTANT
, "storeDiskdSend: msgsnd: " << xstrerror());
402 cbdataReferenceDone(M
->callback_data
);
403 assert(++send_errors
< 100);
409 * We have to drain the queue here if necessary. If we don't,
410 * then we can have a lot of messages in the queue (probably
411 * up to 2*magic1) and we can run out of shared memory buffers.
414 * Note that we call Store::Root().callbackk (for all SDs), rather
415 * than callback for just this SD, so that while
416 * we're "blocking" on this SD we can also handle callbacks
417 * from other SDs that might be ready.
420 struct timeval delay
= {0, 1};
422 while (away
> magic2
) {
423 select(0, NULL
, NULL
, NULL
, &delay
);
424 Store::Root().callback();
426 if (delay
.tv_usec
< 1000000)
434 DiskdIOStrategy::getOptionTree() const
436 ConfigOptionVector
*result
= new ConfigOptionVector
;
437 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ1Parse
, &DiskdIOStrategy::optionQ1Dump
));
438 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ2Parse
, &DiskdIOStrategy::optionQ2Dump
));
443 DiskdIOStrategy::optionQ1Parse(const char *name
, const char *value
, int isaReconfig
)
445 if (strcmp(name
, "Q1") != 0)
448 int old_magic1
= magic1
;
450 magic1
= atoi(value
);
455 if (old_magic1
< magic1
) {
457 * This is because shm.nbufs is computed at startup, when
458 * we call shmget(). We can't increase the Q1/Q2 parameters
459 * beyond their initial values because then we might have
460 * more "Q2 messages" than shared memory chunks, and this
461 * will cause an assertion in storeDiskdShmGet().
463 /* TODO: have DiskdIO hold a link to the swapdir, to allow detailed reporting again */
464 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q1 value while Squid is running.");
469 if (old_magic1
!= magic1
)
470 debugs(3, DBG_IMPORTANT
, "cache_dir new Q1 value '" << magic1
<< "'");
476 DiskdIOStrategy::optionQ1Dump(StoreEntry
* e
) const
478 storeAppendPrintf(e
, " Q1=%d", magic1
);
482 DiskdIOStrategy::optionQ2Parse(const char *name
, const char *value
, int isaReconfig
)
484 if (strcmp(name
, "Q2") != 0)
487 int old_magic2
= magic2
;
489 magic2
= atoi(value
);
494 if (old_magic2
< magic2
) {
495 /* See comments in Q1 function above */
496 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q2 value while Squid is running.");
501 if (old_magic2
!= magic2
)
502 debugs(3, DBG_IMPORTANT
, "cache_dir new Q2 value '" << magic2
<< "'");
508 DiskdIOStrategy::optionQ2Dump(StoreEntry
* e
) const
510 storeAppendPrintf(e
, " Q2=%d", magic2
);
514 * Sync any pending data. We just sit around and read the queue
515 * until the data has finished writing.
518 DiskdIOStrategy::sync()
520 static time_t lastmsg
= 0;
523 if (squid_curtime
> lastmsg
) {
524 debugs(47, DBG_IMPORTANT
, "storeDiskdDirSync: " << away
<< " messages away");
525 lastmsg
= squid_curtime
;
533 * Handle callbacks. If we have more than magic2 requests away, we block
534 * until the queue is below magic2. Otherwise, we simply return when we
535 * don't get a message.
539 DiskdIOStrategy::callback()
545 if (away
>= magic2
) {
546 ++diskd_stats
.block_queue_len
;
548 /* We might not have anything to do, but our queue
552 if (diskd_stats
.sent_count
- diskd_stats
.recv_count
>
553 diskd_stats
.max_away
) {
554 diskd_stats
.max_away
= diskd_stats
.sent_count
- diskd_stats
.recv_count
;
558 #ifdef ALWAYS_ZERO_BUFFERS
559 memset(&M
, '\0', sizeof(M
));
562 x
= msgrcv(rmsgid
, &M
, diomsg::msg_snd_rcv_sz
, 0, IPC_NOWAIT
);
566 else if (x
!= diomsg::msg_snd_rcv_sz
) {
567 debugs(47, DBG_IMPORTANT
, "storeDiskdDirCallback: msgget returns " << x
);
571 ++diskd_stats
.recv_count
;
574 retval
= 1; /* Return that we've actually done some work */
576 if (M
.shm_offset
> -1)
577 shm
.put ((off_t
) M
.shm_offset
);
584 DiskdIOStrategy::statfs(StoreEntry
& sentry
)const
586 storeAppendPrintf(&sentry
, "Pending operations: %d\n", away
);