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 "StatCounters.h"
50 #include "SquidTime.h"
52 diskd_stats_t diskd_stats
;
54 size_t DiskdIOStrategy::nextInstanceID (0);
55 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
58 DiskdIOStrategy::newInstance()
60 return ++nextInstanceID
;
64 DiskdIOStrategy::shedLoad()
67 * Fail on open() if there are too many requests queued.
71 debugs(79, 3, "storeDiskdIO::shedLoad: Shedding, too many requests away");
80 DiskdIOStrategy::load()
82 /* Calculate the storedir load relative to magic2 on a scale of 0 .. 1000 */
83 /* the parse function guarantees magic2 is positivie */
84 return away
* 1000 / magic2
;
88 DiskdIOStrategy::openFailed()
90 diskd_stats
.open_fail_queue_len
++;
94 DiskdIOStrategy::newFile(char const *path
)
101 return new DiskdFile (path
, this);
104 DiskdIOStrategy::DiskdIOStrategy() : magic1(64), magic2(72), away(0) , smsgid(-1), rmsgid(-1), wfd(-1) , instanceID(newInstance())
108 DiskdIOStrategy::unlinkdUseful() const
114 DiskdIOStrategy::unlinkFile(char const *path
)
117 /* Damn, we need to issue a sync unlink here :( */
118 debugs(79, 2, "storeDiskUnlink: Out of queue space, sync unlink");
130 /* We can attempt a diskd unlink */
137 buf
= (char *)shm
.get(&shm_offset
);
139 xstrncpy(buf
, path
, SHMBUF_BLKSZ
);
141 x
= send(_MQD_UNLINK
,
143 (StoreIOState::Pointer
)NULL
,
149 debugs(79, 1, "storeDiskdSend UNLINK: " << xstrerror());
150 ::unlink(buf
); /* XXX EWW! */
151 // shm.put (shm_offset);
154 diskd_stats
.unlink
.ops
++;
158 DiskdIOStrategy::init()
168 Ip::Address localhost
;
170 ikey
= (getpid() << 10) + (instanceID
<< 2);
172 smsgid
= msgget((key_t
) ikey
, 0700 | IPC_CREAT
);
175 debugs(50, 0, "storeDiskdInit: msgget: " << xstrerror());
176 fatal("msgget failed");
179 rmsgid
= msgget((key_t
) (ikey
+ 1), 0700 | IPC_CREAT
);
182 debugs(50, 0, "storeDiskdInit: msgget: " << xstrerror());
183 fatal("msgget failed");
186 shm
.init(ikey
, magic2
);
187 snprintf(skey1
, 32, "%d", ikey
);
188 snprintf(skey2
, 32, "%d", ikey
+ 1);
189 snprintf(skey3
, 32, "%d", ikey
+ 2);
195 localhost
.SetLocalhost();
196 pid
= ipcCreate(IPC_STREAM
,
197 Config
.Program
.diskd
,
206 fatalf("execl: %s", Config
.Program
.diskd
);
211 fd_note(wfd
, "squid -> diskd");
213 commUnsetFdTimeout(wfd
);
214 commSetNonBlocking(wfd
);
215 Comm::QuickPollRequired();
219 * SHM manipulation routines
222 SharedMemory::put(ssize_t offset
)
226 assert(offset
< nbufs
* SHMBUF_BLKSZ
);
227 i
= offset
/ SHMBUF_BLKSZ
;
229 assert(CBIT_TEST(inuse_map
, i
));
230 CBIT_CLR(inuse_map
, i
);
231 --diskd_stats
.shmbuf_count
;
236 SharedMemory::get(ssize_t
* shm_offset
)
241 for (i
= 0; i
< nbufs
; i
++) {
242 if (CBIT_TEST(inuse_map
, i
))
245 CBIT_SET(inuse_map
, i
);
247 *shm_offset
= i
* SHMBUF_BLKSZ
;
249 aBuf
= buf
+ (*shm_offset
);
256 assert(aBuf
< buf
+ (nbufs
* SHMBUF_BLKSZ
));
257 diskd_stats
.shmbuf_count
++;
259 if (diskd_stats
.max_shmuse
< diskd_stats
.shmbuf_count
)
260 diskd_stats
.max_shmuse
= diskd_stats
.shmbuf_count
;
266 SharedMemory::init(int ikey
, int magic2
)
268 nbufs
= (int)(magic2
* 1.3);
269 id
= shmget((key_t
) (ikey
+ 2),
270 nbufs
* SHMBUF_BLKSZ
, 0600 | IPC_CREAT
);
273 debugs(50, 0, "storeDiskdInit: shmget: " << xstrerror());
274 fatal("shmget failed");
277 buf
= (char *)shmat(id
, NULL
, 0);
279 if (buf
== (void *) -1) {
280 debugs(50, 0, "storeDiskdInit: shmat: " << xstrerror());
281 fatal("shmat failed");
284 inuse_map
= (char *)xcalloc((nbufs
+ 7) / 8, 1);
285 diskd_stats
.shmbuf_count
+= nbufs
;
287 for (int i
= 0; i
< nbufs
; i
++) {
288 CBIT_SET(inuse_map
, i
);
289 put (i
* SHMBUF_BLKSZ
);
294 DiskdIOStrategy::unlinkDone(diomsg
* M
)
296 debugs(79, 3, "storeDiskdUnlinkDone: file " << shm
.buf
+ M
->shm_offset
<< " status " << M
->status
);
297 ++statCounter
.syscalls
.disk
.unlinks
;
300 diskd_stats
.unlink
.fail
++;
302 diskd_stats
.unlink
.success
++;
306 DiskdIOStrategy::handle(diomsg
* M
)
308 if (!cbdataReferenceValid (M
->callback_data
)) {
309 /* I.e. already closed file
310 * - say when we have a error opening after
311 * a read was already queued
313 debugs(79, 3, "storeDiskdHandle: Invalid callback_data " << M
->callback_data
);
314 cbdataReferenceDone (M
->callback_data
);
319 /* set errno passed from diskd. makes debugging more meaningful */
324 DiskdFile
*theFile
= (DiskdFile
*)M
->callback_data
;
325 theFile
->RefCountDereference();
326 theFile
->completed (M
);
351 cbdataReferenceDone (M
->callback_data
);
355 DiskdIOStrategy::send(int mtype
, int id
, DiskdFile
*theFile
, size_t size
, off_t offset
, ssize_t shm_offset
, RefCountable_
*requestor
)
358 M
.callback_data
= cbdataReference(theFile
);
359 theFile
->RefCountReference();
360 M
.requestor
= requestor
;
364 requestor
->RefCountReference();
366 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
370 DiskdIOStrategy::send(int mtype
, int id
, RefCount
<StoreIOState
> sio
, size_t size
, off_t offset
, ssize_t shm_offset
)
373 M
.callback_data
= cbdataReference(sio
.getRaw());
376 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
380 DiskdIOStrategy::SEND(diomsg
*M
, int mtype
, int id
, size_t size
, off_t offset
, ssize_t shm_offset
)
382 static int send_errors
= 0;
383 static int last_seq_no
= 0;
384 static int seq_no
= 0;
391 M
->shm_offset
= (int) shm_offset
;
393 M
->seq_no
= ++seq_no
;
395 if (M
->seq_no
< last_seq_no
)
396 debugs(79, 1, "WARNING: sequencing out of order");
398 x
= msgsnd(smsgid
, M
, diomsg::msg_snd_rcv_sz
, IPC_NOWAIT
);
400 last_seq_no
= M
->seq_no
;
403 diskd_stats
.sent_count
++;
406 debugs(79, 1, "storeDiskdSend: msgsnd: " << xstrerror());
407 cbdataReferenceDone(M
->callback_data
);
408 assert(++send_errors
< 100);
414 * We have to drain the queue here if necessary. If we don't,
415 * then we can have a lot of messages in the queue (probably
416 * up to 2*magic1) and we can run out of shared memory buffers.
419 * Note that we call Store::Root().callbackk (for all SDs), rather
420 * than callback for just this SD, so that while
421 * we're "blocking" on this SD we can also handle callbacks
422 * from other SDs that might be ready.
425 struct timeval delay
= {0, 1};
427 while (away
> magic2
) {
428 select(0, NULL
, NULL
, NULL
, &delay
);
429 Store::Root().callback();
431 if (delay
.tv_usec
< 1000000)
439 DiskdIOStrategy::getOptionTree() const
441 ConfigOptionVector
*result
= new ConfigOptionVector
;
442 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ1Parse
, &DiskdIOStrategy::optionQ1Dump
));
443 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ2Parse
, &DiskdIOStrategy::optionQ2Dump
));
448 DiskdIOStrategy::optionQ1Parse(const char *name
, const char *value
, int isaReconfig
)
450 if (strcmp(name
, "Q1") != 0)
453 int old_magic1
= magic1
;
455 magic1
= atoi(value
);
460 if (old_magic1
< magic1
) {
462 * This is because shm.nbufs is computed at startup, when
463 * we call shmget(). We can't increase the Q1/Q2 parameters
464 * beyond their initial values because then we might have
465 * more "Q2 messages" than shared memory chunks, and this
466 * will cause an assertion in storeDiskdShmGet().
468 /* TODO: have DiskdIO hold a link to the swapdir, to allow detailed reporting again */
469 debugs(3, 1, "WARNING: cannot increase cache_dir Q1 value while Squid is running.");
474 if (old_magic1
!= magic1
)
475 debugs(3, 1, "cache_dir new Q1 value '" << magic1
<< "'");
481 DiskdIOStrategy::optionQ1Dump(StoreEntry
* e
) const
483 storeAppendPrintf(e
, " Q1=%d", magic1
);
487 DiskdIOStrategy::optionQ2Parse(const char *name
, const char *value
, int isaReconfig
)
489 if (strcmp(name
, "Q2") != 0)
492 int old_magic2
= magic2
;
494 magic2
= atoi(value
);
499 if (old_magic2
< magic2
) {
500 /* See comments in Q1 function above */
501 debugs(3, 1, "WARNING: cannot increase cache_dir Q2 value while Squid is running.");
506 if (old_magic2
!= magic2
)
507 debugs(3, 1, "cache_dir new Q2 value '" << magic2
<< "'");
513 DiskdIOStrategy::optionQ2Dump(StoreEntry
* e
) const
515 storeAppendPrintf(e
, " Q2=%d", magic2
);
519 * Sync any pending data. We just sit around and read the queue
520 * until the data has finished writing.
523 DiskdIOStrategy::sync()
525 static time_t lastmsg
= 0;
528 if (squid_curtime
> lastmsg
) {
529 debugs(47, 1, "storeDiskdDirSync: " << away
<< " messages away");
530 lastmsg
= squid_curtime
;
539 * Handle callbacks. If we have more than magic2 requests away, we block
540 * until the queue is below magic2. Otherwise, we simply return when we
541 * don't get a message.
545 DiskdIOStrategy::callback()
551 if (away
>= magic2
) {
552 diskd_stats
.block_queue_len
++;
554 /* We might not have anything to do, but our queue
558 if (diskd_stats
.sent_count
- diskd_stats
.recv_count
>
559 diskd_stats
.max_away
) {
560 diskd_stats
.max_away
= diskd_stats
.sent_count
- diskd_stats
.recv_count
;
564 #ifdef ALWAYS_ZERO_BUFFERS
565 memset(&M
, '\0', sizeof(M
));
568 x
= msgrcv(rmsgid
, &M
, diomsg::msg_snd_rcv_sz
, 0, IPC_NOWAIT
);
572 else if (x
!= diomsg::msg_snd_rcv_sz
) {
573 debugs(47, 1, "storeDiskdDirCallback: msgget returns " << x
);
577 diskd_stats
.recv_count
++;
580 retval
= 1; /* Return that we've actually done some work */
582 if (M
.shm_offset
> -1)
583 shm
.put ((off_t
) M
.shm_offset
);
590 DiskdIOStrategy::statfs(StoreEntry
& sentry
)const
592 storeAppendPrintf(&sentry
, "Pending operations: %d\n", away
);