4 * DEBUG: section 79 Squid-side DISKD I/O functions.
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
37 #include "comm/Loops.h"
42 #include "DiskdIOStrategy.h"
43 #include "ConfigOption.h"
44 #include "DiskIO/DiskFile.h"
45 #include "DiskdFile.h"
49 #include "SquidTime.h"
51 diskd_stats_t diskd_stats
;
53 size_t DiskdIOStrategy::nextInstanceID (0);
54 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
57 DiskdIOStrategy::newInstance()
59 return ++nextInstanceID
;
63 DiskdIOStrategy::shedLoad()
66 * Fail on open() if there are too many requests queued.
70 debugs(79, 3, "storeDiskdIO::shedLoad: Shedding, too many requests away");
79 DiskdIOStrategy::load()
81 /* Calculate the storedir load relative to magic2 on a scale of 0 .. 1000 */
82 /* the parse function guarantees magic2 is positivie */
83 return away
* 1000 / magic2
;
87 DiskdIOStrategy::openFailed()
89 diskd_stats
.open_fail_queue_len
++;
93 DiskdIOStrategy::newFile(char const *path
)
100 return new DiskdFile (path
, this);
103 DiskdIOStrategy::DiskdIOStrategy() : magic1(64), magic2(72), away(0) , smsgid(-1), rmsgid(-1), wfd(-1) , instanceID(newInstance())
107 DiskdIOStrategy::unlinkFile(char const *path
)
110 /* Damn, we need to issue a sync unlink here :( */
111 debugs(79, 2, "storeDiskUnlink: Out of queue space, sync unlink");
123 /* We can attempt a diskd unlink */
130 buf
= (char *)shm
.get(&shm_offset
);
132 xstrncpy(buf
, path
, SHMBUF_BLKSZ
);
134 x
= send(_MQD_UNLINK
,
136 (StoreIOState::Pointer
)NULL
,
142 debugs(79, 1, "storeDiskdSend UNLINK: " << xstrerror());
143 ::unlink(buf
); /* XXX EWW! */
144 // shm.put (shm_offset);
147 diskd_stats
.unlink
.ops
++;
151 DiskdIOStrategy::init()
161 Ip::Address localhost
;
163 ikey
= (getpid() << 10) + (instanceID
<< 2);
165 smsgid
= msgget((key_t
) ikey
, 0700 | IPC_CREAT
);
168 debugs(50, 0, "storeDiskdInit: msgget: " << xstrerror());
169 fatal("msgget failed");
172 rmsgid
= msgget((key_t
) (ikey
+ 1), 0700 | IPC_CREAT
);
175 debugs(50, 0, "storeDiskdInit: msgget: " << xstrerror());
176 fatal("msgget failed");
179 shm
.init(ikey
, magic2
);
180 snprintf(skey1
, 32, "%d", ikey
);
181 snprintf(skey2
, 32, "%d", ikey
+ 1);
182 snprintf(skey3
, 32, "%d", ikey
+ 2);
188 localhost
.SetLocalhost();
189 pid
= ipcCreate(IPC_STREAM
,
190 Config
.Program
.diskd
,
199 fatalf("execl: %s", Config
.Program
.diskd
);
204 fd_note(wfd
, "squid -> diskd");
206 commSetTimeout(wfd
, -1, NULL
, NULL
);
207 commSetNonBlocking(wfd
);
208 Comm::QuickPollRequired();
212 * SHM manipulation routines
215 SharedMemory::put(ssize_t offset
)
219 assert(offset
< nbufs
* SHMBUF_BLKSZ
);
220 i
= offset
/ SHMBUF_BLKSZ
;
222 assert(CBIT_TEST(inuse_map
, i
));
223 CBIT_CLR(inuse_map
, i
);
224 --diskd_stats
.shmbuf_count
;
229 SharedMemory::get(ssize_t
* shm_offset
)
234 for (i
= 0; i
< nbufs
; i
++) {
235 if (CBIT_TEST(inuse_map
, i
))
238 CBIT_SET(inuse_map
, i
);
240 *shm_offset
= i
* SHMBUF_BLKSZ
;
242 aBuf
= buf
+ (*shm_offset
);
249 assert(aBuf
< buf
+ (nbufs
* SHMBUF_BLKSZ
));
250 diskd_stats
.shmbuf_count
++;
252 if (diskd_stats
.max_shmuse
< diskd_stats
.shmbuf_count
)
253 diskd_stats
.max_shmuse
= diskd_stats
.shmbuf_count
;
259 SharedMemory::init(int ikey
, int magic2
)
261 nbufs
= (int)(magic2
* 1.3);
262 id
= shmget((key_t
) (ikey
+ 2),
263 nbufs
* SHMBUF_BLKSZ
, 0600 | IPC_CREAT
);
266 debugs(50, 0, "storeDiskdInit: shmget: " << xstrerror());
267 fatal("shmget failed");
270 buf
= (char *)shmat(id
, NULL
, 0);
272 if (buf
== (void *) -1) {
273 debugs(50, 0, "storeDiskdInit: shmat: " << xstrerror());
274 fatal("shmat failed");
277 inuse_map
= (char *)xcalloc((nbufs
+ 7) / 8, 1);
278 diskd_stats
.shmbuf_count
+= nbufs
;
280 for (int i
= 0; i
< nbufs
; i
++) {
281 CBIT_SET(inuse_map
, i
);
282 put (i
* SHMBUF_BLKSZ
);
287 DiskdIOStrategy::unlinkDone(diomsg
* M
)
289 debugs(79, 3, "storeDiskdUnlinkDone: file " << shm
.buf
+ M
->shm_offset
<< " status " << M
->status
);
290 statCounter
.syscalls
.disk
.unlinks
++;
293 diskd_stats
.unlink
.fail
++;
295 diskd_stats
.unlink
.success
++;
299 DiskdIOStrategy::handle(diomsg
* M
)
301 if (!cbdataReferenceValid (M
->callback_data
)) {
302 /* I.e. already closed file
303 * - say when we have a error opening after
304 * a read was already queued
306 debugs(79, 3, "storeDiskdHandle: Invalid callback_data " << M
->callback_data
);
307 cbdataReferenceDone (M
->callback_data
);
312 /* set errno passed from diskd. makes debugging more meaningful */
317 DiskdFile
*theFile
= (DiskdFile
*)M
->callback_data
;
318 theFile
->RefCountDereference();
319 theFile
->completed (M
);
344 cbdataReferenceDone (M
->callback_data
);
348 DiskdIOStrategy::send(int mtype
, int id
, DiskdFile
*theFile
, size_t size
, off_t offset
, ssize_t shm_offset
, RefCountable_
*requestor
)
351 M
.callback_data
= cbdataReference(theFile
);
352 theFile
->RefCountReference();
353 M
.requestor
= requestor
;
357 requestor
->RefCountReference();
359 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
363 DiskdIOStrategy::send(int mtype
, int id
, RefCount
<StoreIOState
> sio
, size_t size
, off_t offset
, ssize_t shm_offset
)
366 M
.callback_data
= cbdataReference(sio
.getRaw());
369 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
373 DiskdIOStrategy::SEND(diomsg
*M
, int mtype
, int id
, size_t size
, off_t offset
, ssize_t shm_offset
)
375 static int send_errors
= 0;
376 static int last_seq_no
= 0;
377 static int seq_no
= 0;
384 M
->shm_offset
= (int) shm_offset
;
386 M
->seq_no
= ++seq_no
;
388 if (M
->seq_no
< last_seq_no
)
389 debugs(79, 1, "WARNING: sequencing out of order");
391 x
= msgsnd(smsgid
, M
, diomsg::msg_snd_rcv_sz
, IPC_NOWAIT
);
393 last_seq_no
= M
->seq_no
;
396 diskd_stats
.sent_count
++;
399 debugs(79, 1, "storeDiskdSend: msgsnd: " << xstrerror());
400 cbdataReferenceDone(M
->callback_data
);
401 assert(++send_errors
< 100);
407 * We have to drain the queue here if necessary. If we don't,
408 * then we can have a lot of messages in the queue (probably
409 * up to 2*magic1) and we can run out of shared memory buffers.
412 * Note that we call Store::Root().callbackk (for all SDs), rather
413 * than callback for just this SD, so that while
414 * we're "blocking" on this SD we can also handle callbacks
415 * from other SDs that might be ready.
418 struct timeval delay
= {0, 1};
420 while (away
> magic2
) {
421 select(0, NULL
, NULL
, NULL
, &delay
);
422 Store::Root().callback();
424 if (delay
.tv_usec
< 1000000)
432 DiskdIOStrategy::getOptionTree() const
434 ConfigOptionVector
*result
= new ConfigOptionVector
;
435 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ1Parse
, &DiskdIOStrategy::optionQ1Dump
));
436 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ2Parse
, &DiskdIOStrategy::optionQ2Dump
));
441 DiskdIOStrategy::optionQ1Parse(const char *name
, const char *value
, int isaReconfig
)
443 if (strcmp(name
, "Q1") != 0)
446 int old_magic1
= magic1
;
448 magic1
= atoi(value
);
453 if (old_magic1
< magic1
) {
455 * This is because shm.nbufs is computed at startup, when
456 * we call shmget(). We can't increase the Q1/Q2 parameters
457 * beyond their initial values because then we might have
458 * more "Q2 messages" than shared memory chunks, and this
459 * will cause an assertion in storeDiskdShmGet().
461 /* TODO: have DiskdIO hold a link to the swapdir, to allow detailed reporting again */
462 debugs(3, 1, "WARNING: cannot increase cache_dir Q1 value while Squid is running.");
467 if (old_magic1
!= magic1
)
468 debugs(3, 1, "cache_dir new Q1 value '" << magic1
<< "'");
474 DiskdIOStrategy::optionQ1Dump(StoreEntry
* e
) const
476 storeAppendPrintf(e
, " Q1=%d", magic1
);
480 DiskdIOStrategy::optionQ2Parse(const char *name
, const char *value
, int isaReconfig
)
482 if (strcmp(name
, "Q2") != 0)
485 int old_magic2
= magic2
;
487 magic2
= atoi(value
);
492 if (old_magic2
< magic2
) {
493 /* See comments in Q1 function above */
494 debugs(3, 1, "WARNING: cannot increase cache_dir Q2 value while Squid is running.");
499 if (old_magic2
!= magic2
)
500 debugs(3, 1, "cache_dir new Q2 value '" << magic2
<< "'");
506 DiskdIOStrategy::optionQ2Dump(StoreEntry
* e
) const
508 storeAppendPrintf(e
, " Q2=%d", magic2
);
512 * Sync any pending data. We just sit around and read the queue
513 * until the data has finished writing.
516 DiskdIOStrategy::sync()
518 static time_t lastmsg
= 0;
521 if (squid_curtime
> lastmsg
) {
522 debugs(47, 1, "storeDiskdDirSync: " << away
<< " messages away");
523 lastmsg
= squid_curtime
;
532 * Handle callbacks. If we have more than magic2 requests away, we block
533 * until the queue is below magic2. Otherwise, we simply return when we
534 * don't get a message.
538 DiskdIOStrategy::callback()
544 if (away
>= magic2
) {
545 diskd_stats
.block_queue_len
++;
547 /* We might not have anything to do, but our queue
551 if (diskd_stats
.sent_count
- diskd_stats
.recv_count
>
552 diskd_stats
.max_away
) {
553 diskd_stats
.max_away
= diskd_stats
.sent_count
- diskd_stats
.recv_count
;
557 #ifdef ALWAYS_ZERO_BUFFERS
558 memset(&M
, '\0', sizeof(M
));
561 x
= msgrcv(rmsgid
, &M
, diomsg::msg_snd_rcv_sz
, 0, IPC_NOWAIT
);
565 else if (x
!= diomsg::msg_snd_rcv_sz
) {
566 debugs(47, 1, "storeDiskdDirCallback: msgget returns " << x
);
570 diskd_stats
.recv_count
++;
573 retval
= 1; /* Return that we've actually done some work */
575 if (M
.shm_offset
> -1)
576 shm
.put ((off_t
) M
.shm_offset
);
583 DiskdIOStrategy::statfs(StoreEntry
& sentry
)const
585 storeAppendPrintf(&sentry
, "Pending operations: %d\n", away
);