2 * Copyright (C) 1996-2022 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 "StatCounters.h"
36 diskd_stats_t diskd_stats
;
38 size_t DiskdIOStrategy::nextInstanceID (0);
39 const int diomsg::msg_snd_rcv_sz
= sizeof(diomsg
) - sizeof(mtyp_t
);
42 DiskdIOStrategy::newInstance()
44 return ++nextInstanceID
;
48 DiskdIOStrategy::shedLoad()
51 * Fail on open() if there are too many requests queued.
55 debugs(79, 3, "storeDiskdIO::shedLoad: Shedding, too many requests away");
64 DiskdIOStrategy::load()
66 /* Calculate the storedir load relative to magic2 on a scale of 0 .. 1000 */
67 /* the parse function guarantees magic2 is positivie */
68 return away
* 1000 / magic2
;
72 DiskdIOStrategy::openFailed()
74 ++diskd_stats
.open_fail_queue_len
;
78 DiskdIOStrategy::newFile(char const *path
)
85 return new DiskdFile (path
, this);
88 DiskdIOStrategy::DiskdIOStrategy() : magic1(64), magic2(72), away(0), smsgid(-1), rmsgid(-1), wfd(-1), instanceID(newInstance())
92 DiskdIOStrategy::unlinkdUseful() const
98 DiskdIOStrategy::unlinkFile(char const *path
)
101 /* Damn, we need to issue a sync unlink here :( */
102 debugs(79, 2, "storeDiskUnlink: Out of queue space, sync unlink");
107 /* We can attempt a diskd unlink */
114 buf
= (char *)shm
.get(&shm_offset
);
116 xstrncpy(buf
, path
, SHMBUF_BLKSZ
);
118 x
= send(_MQD_UNLINK
,
120 (StoreIOState::Pointer
)NULL
,
127 debugs(79, DBG_IMPORTANT
, "storeDiskdSend UNLINK: " << xstrerr(xerrno
));
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
);
154 debugs(50, DBG_CRITICAL
, MYNAME
<< "msgget: " << xstrerr(xerrno
));
155 fatal("msgget failed");
158 rmsgid
= msgget((key_t
) (ikey
+ 1), 0700 | IPC_CREAT
);
162 debugs(50, DBG_CRITICAL
, MYNAME
<< "msgget: " << xstrerr(xerrno
));
163 fatal("msgget failed");
166 shm
.init(ikey
, magic2
);
167 snprintf(skey1
, 32, "%d", ikey
);
168 snprintf(skey2
, 32, "%d", ikey
+ 1);
169 snprintf(skey3
, 32, "%d", ikey
+ 2);
175 localhost
.setLocalhost();
176 pid
= ipcCreate(IPC_STREAM
,
177 Config
.Program
.diskd
,
186 fatalf("execl: %s", Config
.Program
.diskd
);
191 fd_note(wfd
, "squid -> diskd");
193 commUnsetFdTimeout(wfd
);
194 commSetNonBlocking(wfd
);
195 Comm::QuickPollRequired();
199 * SHM manipulation routines
202 SharedMemory::put(ssize_t offset
)
206 assert(offset
< nbufs
* SHMBUF_BLKSZ
);
207 i
= offset
/ SHMBUF_BLKSZ
;
209 assert(CBIT_TEST(inuse_map
, i
));
210 CBIT_CLR(inuse_map
, i
);
211 --diskd_stats
.shmbuf_count
;
216 SharedMemory::get(ssize_t
* shm_offset
)
221 for (i
= 0; i
< nbufs
; ++i
) {
222 if (CBIT_TEST(inuse_map
, i
))
225 CBIT_SET(inuse_map
, i
);
227 *shm_offset
= i
* SHMBUF_BLKSZ
;
229 aBuf
= buf
+ (*shm_offset
);
236 assert(aBuf
< buf
+ (nbufs
* SHMBUF_BLKSZ
));
237 ++diskd_stats
.shmbuf_count
;
239 if (diskd_stats
.max_shmuse
< diskd_stats
.shmbuf_count
)
240 diskd_stats
.max_shmuse
= diskd_stats
.shmbuf_count
;
246 SharedMemory::init(int ikey
, int magic2
)
248 nbufs
= (int)(magic2
* 1.3);
249 id
= shmget((key_t
) (ikey
+ 2),
250 nbufs
* SHMBUF_BLKSZ
, 0600 | IPC_CREAT
);
254 debugs(50, DBG_CRITICAL
, MYNAME
<< "shmget: " << xstrerr(xerrno
));
255 fatal("shmget failed");
258 buf
= (char *)shmat(id
, NULL
, 0);
260 if (buf
== (void *) -1) {
262 debugs(50, DBG_CRITICAL
, MYNAME
<< "shmat: " << xstrerr(xerrno
));
263 fatal("shmat failed");
266 inuse_map
= (char *)xcalloc((nbufs
+ 7) / 8, 1);
267 diskd_stats
.shmbuf_count
+= nbufs
;
269 for (int i
= 0; i
< nbufs
; ++i
) {
270 CBIT_SET(inuse_map
, i
);
271 put (i
* SHMBUF_BLKSZ
);
276 DiskdIOStrategy::unlinkDone(diomsg
* M
)
278 debugs(79, 3, "storeDiskdUnlinkDone: file " << shm
.buf
+ M
->shm_offset
<< " status " << M
->status
);
279 ++statCounter
.syscalls
.disk
.unlinks
;
282 ++diskd_stats
.unlink
.fail
;
284 ++diskd_stats
.unlink
.success
;
288 DiskdIOStrategy::handle(diomsg
* M
)
290 if (!cbdataReferenceValid (M
->callback_data
)) {
291 /* I.e. already closed file
292 * - say when we have a error opening after
293 * a read was already queued
295 debugs(79, 3, "storeDiskdHandle: Invalid callback_data " << M
->callback_data
);
296 cbdataReferenceDone (M
->callback_data
);
300 /* set errno passed from diskd. makes debugging more meaningful */
305 DiskdFile
*theFile
= (DiskdFile
*)M
->callback_data
;
307 theFile
->completed (M
);
332 cbdataReferenceDone (M
->callback_data
);
336 DiskdIOStrategy::send(int mtype
, int id
, DiskdFile
*theFile
, size_t size
, off_t offset
, ssize_t shm_offset
, Lock
*requestor
)
339 M
.callback_data
= cbdataReference(theFile
);
341 M
.requestor
= requestor
;
347 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
351 DiskdIOStrategy::send(int mtype
, int id
, RefCount
<StoreIOState
> sio
, size_t size
, off_t offset
, ssize_t shm_offset
)
354 M
.callback_data
= cbdataReference(sio
.getRaw());
357 return SEND(&M
, mtype
, id
, size
, offset
, shm_offset
);
361 DiskdIOStrategy::SEND(diomsg
*M
, int mtype
, int id
, size_t size
, off_t offset
, ssize_t shm_offset
)
363 static int send_errors
= 0;
364 static int last_seq_no
= 0;
365 static int seq_no
= 0;
372 M
->shm_offset
= (int) shm_offset
;
374 M
->seq_no
= ++seq_no
;
376 if (M
->seq_no
< last_seq_no
)
377 debugs(79, DBG_IMPORTANT
, "WARNING: sequencing out of order");
379 x
= msgsnd(smsgid
, M
, diomsg::msg_snd_rcv_sz
, IPC_NOWAIT
);
381 last_seq_no
= M
->seq_no
;
384 ++diskd_stats
.sent_count
;
388 debugs(79, DBG_IMPORTANT
, MYNAME
<< "msgsnd: " << xstrerr(xerrno
));
389 cbdataReferenceDone(M
->callback_data
);
391 assert(send_errors
< 100);
397 * We have to drain the queue here if necessary. If we don't,
398 * then we can have a lot of messages in the queue (probably
399 * up to 2*magic1) and we can run out of shared memory buffers.
402 * Note that we call Store::Root().callbackk (for all SDs), rather
403 * than callback for just this SD, so that while
404 * we're "blocking" on this SD we can also handle callbacks
405 * from other SDs that might be ready.
408 struct timeval delay
= {0, 1};
410 while (away
> magic2
) {
411 select(0, NULL
, NULL
, NULL
, &delay
);
412 Store::Root().callback();
414 if (delay
.tv_usec
< 1000000)
422 DiskdIOStrategy::getOptionTree() const
424 ConfigOptionVector
*result
= new ConfigOptionVector
;
425 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ1Parse
, &DiskdIOStrategy::optionQ1Dump
));
426 result
->options
.push_back(new ConfigOptionAdapter
<DiskdIOStrategy
>(*const_cast<DiskdIOStrategy
*>(this), &DiskdIOStrategy::optionQ2Parse
, &DiskdIOStrategy::optionQ2Dump
));
431 DiskdIOStrategy::optionQ1Parse(const char *name
, const char *value
, int isaReconfig
)
433 if (strcmp(name
, "Q1") != 0)
436 int old_magic1
= magic1
;
438 magic1
= atoi(value
);
443 if (old_magic1
< magic1
) {
445 * This is because shm.nbufs is computed at startup, when
446 * we call shmget(). We can't increase the Q1/Q2 parameters
447 * beyond their initial values because then we might have
448 * more "Q2 messages" than shared memory chunks, and this
449 * will cause an assertion in storeDiskdShmGet().
451 /* TODO: have DiskdIO hold a link to the swapdir, to allow detailed reporting again */
452 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q1 value while Squid is running.");
457 if (old_magic1
!= magic1
)
458 debugs(3, DBG_IMPORTANT
, "cache_dir new Q1 value '" << magic1
<< "'");
464 DiskdIOStrategy::optionQ1Dump(StoreEntry
* e
) const
466 storeAppendPrintf(e
, " Q1=%d", magic1
);
470 DiskdIOStrategy::optionQ2Parse(const char *name
, const char *value
, int isaReconfig
)
472 if (strcmp(name
, "Q2") != 0)
475 int old_magic2
= magic2
;
477 magic2
= atoi(value
);
482 if (old_magic2
< magic2
) {
483 /* See comments in Q1 function above */
484 debugs(3, DBG_IMPORTANT
, "WARNING: cannot increase cache_dir Q2 value while Squid is running.");
489 if (old_magic2
!= magic2
)
490 debugs(3, DBG_IMPORTANT
, "cache_dir new Q2 value '" << magic2
<< "'");
496 DiskdIOStrategy::optionQ2Dump(StoreEntry
* e
) const
498 storeAppendPrintf(e
, " Q2=%d", magic2
);
502 * Sync any pending data. We just sit around and read the queue
503 * until the data has finished writing.
506 DiskdIOStrategy::sync()
508 static time_t lastmsg
= 0;
511 if (squid_curtime
> lastmsg
) {
512 debugs(47, DBG_IMPORTANT
, "storeDiskdDirSync: " << away
<< " messages away");
513 lastmsg
= squid_curtime
;
521 * Handle callbacks. If we have more than magic2 requests away, we block
522 * until the queue is below magic2. Otherwise, we simply return when we
523 * don't get a message.
527 DiskdIOStrategy::callback()
533 if (away
>= magic2
) {
534 ++diskd_stats
.block_queue_len
;
536 /* We might not have anything to do, but our queue
540 if (diskd_stats
.sent_count
- diskd_stats
.recv_count
>
541 diskd_stats
.max_away
) {
542 diskd_stats
.max_away
= diskd_stats
.sent_count
- diskd_stats
.recv_count
;
546 x
= msgrcv(rmsgid
, &M
, diomsg::msg_snd_rcv_sz
, 0, IPC_NOWAIT
);
550 else if (x
!= diomsg::msg_snd_rcv_sz
) {
551 debugs(47, DBG_IMPORTANT
, "storeDiskdDirCallback: msgget returns " << x
);
555 ++diskd_stats
.recv_count
;
558 retval
= 1; /* Return that we've actually done some work */
560 if (M
.shm_offset
> -1)
561 shm
.put ((off_t
) M
.shm_offset
);
568 DiskdIOStrategy::statfs(StoreEntry
& sentry
)const
570 storeAppendPrintf(&sentry
, "Pending operations: %d\n", away
);