2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 79 Squid-side DISKD I/O functions. */
12 #include "comm/Loops.h"
13 #include "ConfigOption.h"
15 #include "DiskdFile.h"
16 #include "DiskdIOStrategy.h"
17 #include "DiskIO/DiskFile.h"
19 #include "SquidConfig.h"
21 #include "SquidTime.h"
22 #include "StatCounters.h"
37 diskd_stats_t diskd_stats
;
39 size_t DiskdIOStrategy::nextInstanceID (0);
40 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
43 DiskdIOStrategy::newInstance()
45 return ++nextInstanceID
;
49 DiskdIOStrategy::shedLoad()
52 * Fail on open() if there are too many requests queued.
56 debugs(79, 3, "storeDiskdIO::shedLoad: Shedding, too many requests away");
65 DiskdIOStrategy::load()
67 /* Calculate the storedir load relative to magic2 on a scale of 0 .. 1000 */
68 /* the parse function guarantees magic2 is positivie */
69 return away
* 1000 / magic2
;
73 DiskdIOStrategy::openFailed()
75 ++diskd_stats
.open_fail_queue_len
;
79 DiskdIOStrategy::newFile(char const *path
)
86 return new DiskdFile (path
, this);
89 DiskdIOStrategy::DiskdIOStrategy() : magic1(64), magic2(72), away(0) , smsgid(-1), rmsgid(-1), wfd(-1) , instanceID(newInstance())
93 DiskdIOStrategy::unlinkdUseful() const
99 DiskdIOStrategy::unlinkFile(char const *path
)
102 /* Damn, we need to issue a sync unlink here :( */
103 debugs(79, 2, "storeDiskUnlink: Out of queue space, sync unlink");
108 /* We can attempt a diskd unlink */
115 buf
= (char *)shm
.get(&shm_offset
);
117 xstrncpy(buf
, path
, SHMBUF_BLKSZ
);
119 x
= send(_MQD_UNLINK
,
121 (StoreIOState::Pointer
)NULL
,
127 debugs(79, DBG_IMPORTANT
, "storeDiskdSend UNLINK: " << xstrerror());
128 ::unlink(buf
); /* XXX EWW! */
129 // shm.put (shm_offset);
132 ++diskd_stats
.unlink
.ops
;
136 DiskdIOStrategy::init()
146 Ip::Address localhost
;
148 ikey
= (getpid() << 10) + (instanceID
<< 2);
150 smsgid
= msgget((key_t
) ikey
, 0700 | IPC_CREAT
);
153 debugs(50, DBG_CRITICAL
, "storeDiskdInit: msgget: " << xstrerror());
154 fatal("msgget failed");
157 rmsgid
= msgget((key_t
) (ikey
+ 1), 0700 | IPC_CREAT
);
160 debugs(50, DBG_CRITICAL
, "storeDiskdInit: msgget: " << xstrerror());
161 fatal("msgget failed");
164 shm
.init(ikey
, magic2
);
165 snprintf(skey1
, 32, "%d", ikey
);
166 snprintf(skey2
, 32, "%d", ikey
+ 1);
167 snprintf(skey3
, 32, "%d", ikey
+ 2);
173 localhost
.setLocalhost();
174 pid
= ipcCreate(IPC_STREAM
,
175 Config
.Program
.diskd
,
184 fatalf("execl: %s", Config
.Program
.diskd
);
189 fd_note(wfd
, "squid -> diskd");
191 commUnsetFdTimeout(wfd
);
192 commSetNonBlocking(wfd
);
193 Comm::QuickPollRequired();
197 * SHM manipulation routines
200 SharedMemory::put(ssize_t offset
)
204 assert(offset
< nbufs
* SHMBUF_BLKSZ
);
205 i
= offset
/ SHMBUF_BLKSZ
;
207 assert(CBIT_TEST(inuse_map
, i
));
208 CBIT_CLR(inuse_map
, i
);
209 --diskd_stats
.shmbuf_count
;
214 SharedMemory::get(ssize_t
* shm_offset
)
219 for (i
= 0; i
< nbufs
; ++i
) {
220 if (CBIT_TEST(inuse_map
, i
))
223 CBIT_SET(inuse_map
, i
);
225 *shm_offset
= i
* SHMBUF_BLKSZ
;
227 aBuf
= buf
+ (*shm_offset
);
234 assert(aBuf
< buf
+ (nbufs
* SHMBUF_BLKSZ
));
235 ++diskd_stats
.shmbuf_count
;
237 if (diskd_stats
.max_shmuse
< diskd_stats
.shmbuf_count
)
238 diskd_stats
.max_shmuse
= diskd_stats
.shmbuf_count
;
244 SharedMemory::init(int ikey
, int magic2
)
246 nbufs
= (int)(magic2
* 1.3);
247 id
= shmget((key_t
) (ikey
+ 2),
248 nbufs
* SHMBUF_BLKSZ
, 0600 | IPC_CREAT
);
251 debugs(50, DBG_CRITICAL
, "storeDiskdInit: shmget: " << xstrerror());
252 fatal("shmget failed");
255 buf
= (char *)shmat(id
, NULL
, 0);
257 if (buf
== (void *) -1) {
258 debugs(50, DBG_CRITICAL
, "storeDiskdInit: shmat: " << xstrerror());
259 fatal("shmat failed");
262 inuse_map
= (char *)xcalloc((nbufs
+ 7) / 8, 1);
263 diskd_stats
.shmbuf_count
+= nbufs
;
265 for (int i
= 0; i
< nbufs
; ++i
) {
266 CBIT_SET(inuse_map
, i
);
267 put (i
* SHMBUF_BLKSZ
);
272 DiskdIOStrategy::unlinkDone(diomsg
* M
)
274 debugs(79, 3, "storeDiskdUnlinkDone: file " << shm
.buf
+ M
->shm_offset
<< " status " << M
->status
);
275 ++statCounter
.syscalls
.disk
.unlinks
;
278 ++diskd_stats
.unlink
.fail
;
280 ++diskd_stats
.unlink
.success
;
284 DiskdIOStrategy::handle(diomsg
* M
)
286 if (!cbdataReferenceValid (M
->callback_data
)) {
287 /* I.e. already closed file
288 * - say when we have a error opening after
289 * a read was already queued
291 debugs(79, 3, "storeDiskdHandle: Invalid callback_data " << M
->callback_data
);
292 cbdataReferenceDone (M
->callback_data
);
296 /* set errno passed from diskd. makes debugging more meaningful */
301 DiskdFile
*theFile
= (DiskdFile
*)M
->callback_data
;
303 theFile
->completed (M
);
328 cbdataReferenceDone (M
->callback_data
);
332 DiskdIOStrategy::send(int mtype
, int id
, DiskdFile
*theFile
, size_t size
, off_t offset
, ssize_t shm_offset
, Lock
*requestor
)
335 M
.callback_data
= cbdataReference(theFile
);
337 M
.requestor
= requestor
;
343 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
347 DiskdIOStrategy::send(int mtype
, int id
, RefCount
<StoreIOState
> sio
, size_t size
, off_t offset
, ssize_t shm_offset
)
350 M
.callback_data
= cbdataReference(sio
.getRaw());
353 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
357 DiskdIOStrategy::SEND(diomsg
*M
, int mtype
, int id
, size_t size
, off_t offset
, ssize_t shm_offset
)
359 static int send_errors
= 0;
360 static int last_seq_no
= 0;
361 static int seq_no
= 0;
368 M
->shm_offset
= (int) shm_offset
;
370 M
->seq_no
= ++seq_no
;
372 if (M
->seq_no
< last_seq_no
)
373 debugs(79, DBG_IMPORTANT
, "WARNING: sequencing out of order");
375 x
= msgsnd(smsgid
, M
, diomsg::msg_snd_rcv_sz
, IPC_NOWAIT
);
377 last_seq_no
= M
->seq_no
;
380 ++diskd_stats
.sent_count
;
383 debugs(79, DBG_IMPORTANT
, "storeDiskdSend: msgsnd: " << xstrerror());
384 cbdataReferenceDone(M
->callback_data
);
386 assert(send_errors
< 100);
392 * We have to drain the queue here if necessary. If we don't,
393 * then we can have a lot of messages in the queue (probably
394 * up to 2*magic1) and we can run out of shared memory buffers.
397 * Note that we call Store::Root().callbackk (for all SDs), rather
398 * than callback for just this SD, so that while
399 * we're "blocking" on this SD we can also handle callbacks
400 * from other SDs that might be ready.
403 struct timeval delay
= {0, 1};
405 while (away
> magic2
) {
406 select(0, NULL
, NULL
, NULL
, &delay
);
407 Store::Root().callback();
409 if (delay
.tv_usec
< 1000000)
417 DiskdIOStrategy::getOptionTree() const
419 ConfigOptionVector
*result
= new ConfigOptionVector
;
420 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ1Parse
, &DiskdIOStrategy::optionQ1Dump
));
421 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ2Parse
, &DiskdIOStrategy::optionQ2Dump
));
426 DiskdIOStrategy::optionQ1Parse(const char *name
, const char *value
, int isaReconfig
)
428 if (strcmp(name
, "Q1") != 0)
431 int old_magic1
= magic1
;
433 magic1
= atoi(value
);
438 if (old_magic1
< magic1
) {
440 * This is because shm.nbufs is computed at startup, when
441 * we call shmget(). We can't increase the Q1/Q2 parameters
442 * beyond their initial values because then we might have
443 * more "Q2 messages" than shared memory chunks, and this
444 * will cause an assertion in storeDiskdShmGet().
446 /* TODO: have DiskdIO hold a link to the swapdir, to allow detailed reporting again */
447 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q1 value while Squid is running.");
452 if (old_magic1
!= magic1
)
453 debugs(3, DBG_IMPORTANT
, "cache_dir new Q1 value '" << magic1
<< "'");
459 DiskdIOStrategy::optionQ1Dump(StoreEntry
* e
) const
461 storeAppendPrintf(e
, " Q1=%d", magic1
);
465 DiskdIOStrategy::optionQ2Parse(const char *name
, const char *value
, int isaReconfig
)
467 if (strcmp(name
, "Q2") != 0)
470 int old_magic2
= magic2
;
472 magic2
= atoi(value
);
477 if (old_magic2
< magic2
) {
478 /* See comments in Q1 function above */
479 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q2 value while Squid is running.");
484 if (old_magic2
!= magic2
)
485 debugs(3, DBG_IMPORTANT
, "cache_dir new Q2 value '" << magic2
<< "'");
491 DiskdIOStrategy::optionQ2Dump(StoreEntry
* e
) const
493 storeAppendPrintf(e
, " Q2=%d", magic2
);
497 * Sync any pending data. We just sit around and read the queue
498 * until the data has finished writing.
501 DiskdIOStrategy::sync()
503 static time_t lastmsg
= 0;
506 if (squid_curtime
> lastmsg
) {
507 debugs(47, DBG_IMPORTANT
, "storeDiskdDirSync: " << away
<< " messages away");
508 lastmsg
= squid_curtime
;
516 * Handle callbacks. If we have more than magic2 requests away, we block
517 * until the queue is below magic2. Otherwise, we simply return when we
518 * don't get a message.
522 DiskdIOStrategy::callback()
528 if (away
>= magic2
) {
529 ++diskd_stats
.block_queue_len
;
531 /* We might not have anything to do, but our queue
535 if (diskd_stats
.sent_count
- diskd_stats
.recv_count
>
536 diskd_stats
.max_away
) {
537 diskd_stats
.max_away
= diskd_stats
.sent_count
- diskd_stats
.recv_count
;
541 #ifdef ALWAYS_ZERO_BUFFERS
542 memset(&M
, '\0', sizeof(M
));
545 x
= msgrcv(rmsgid
, &M
, diomsg::msg_snd_rcv_sz
, 0, IPC_NOWAIT
);
549 else if (x
!= diomsg::msg_snd_rcv_sz
) {
550 debugs(47, DBG_IMPORTANT
, "storeDiskdDirCallback: msgget returns " << x
);
554 ++diskd_stats
.recv_count
;
557 retval
= 1; /* Return that we've actually done some work */
559 if (M
.shm_offset
> -1)
560 shm
.put ((off_t
) M
.shm_offset
);
567 DiskdIOStrategy::statfs(StoreEntry
& sentry
)const
569 storeAppendPrintf(&sentry
, "Pending operations: %d\n", away
);