2 * DEBUG: section 43 Windows AIOPS
3 * AUTHOR: Stewart Forster <slf@connect.com.au>
4 * AUTHOR: Robert Collins <robertc@squid-cache.org>
5 * AUTHOR: Guido Serassio <serassio@squid-cache.org>
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.
36 #include "DiskIO/DiskThreads/CommIO.h"
37 #include "DiskThreads.h"
39 #include "SquidConfig.h"
40 #include "SquidTime.h"
50 #define RIDICULOUS_LENGTH 4096
52 enum _squidaio_thread_status
{
59 typedef enum _squidaio_thread_status squidaio_thread_status
;
61 typedef struct squidaio_request_t
{
63 struct squidaio_request_t
*next
;
64 squidaio_request_type request_type
;
78 struct stat
*tmpstatp
;
81 squidaio_result_t
*resultp
;
84 typedef struct squidaio_request_queue_t
{
86 HANDLE cond
; /* See Event objects */
87 squidaio_request_t
*volatile head
;
88 squidaio_request_t
*volatile *volatile tailp
;
89 unsigned long requests
;
90 unsigned long blocked
; /* main failed to lock the queue */
91 } squidaio_request_queue_t
;
93 typedef struct squidaio_thread_t squidaio_thread_t
;
95 struct squidaio_thread_t
{
96 squidaio_thread_t
*next
;
98 DWORD dwThreadId
; /* thread ID */
99 squidaio_thread_status status
;
101 struct squidaio_request_t
*current_req
;
102 unsigned long requests
;
106 static void squidaio_queue_request(squidaio_request_t
*);
107 static void squidaio_cleanup_request(squidaio_request_t
*);
108 static DWORD WINAPI
squidaio_thread_loop( LPVOID lpParam
);
109 static void squidaio_do_open(squidaio_request_t
*);
110 static void squidaio_do_read(squidaio_request_t
*);
111 static void squidaio_do_write(squidaio_request_t
*);
112 static void squidaio_do_close(squidaio_request_t
*);
113 static void squidaio_do_stat(squidaio_request_t
*);
114 static void squidaio_do_unlink(squidaio_request_t
*);
116 static void *squidaio_do_opendir(squidaio_request_t
*);
118 static void squidaio_debug(squidaio_request_t
*);
119 static void squidaio_poll_queues(void);
121 static squidaio_thread_t
*threads
= NULL
;
122 static int squidaio_initialised
= 0;
124 #define AIO_LARGE_BUFS 16384
125 #define AIO_MEDIUM_BUFS AIO_LARGE_BUFS >> 1
126 #define AIO_SMALL_BUFS AIO_LARGE_BUFS >> 2
127 #define AIO_TINY_BUFS AIO_LARGE_BUFS >> 3
128 #define AIO_MICRO_BUFS 128
130 static MemAllocator
*squidaio_large_bufs
= NULL
; /* 16K */
131 static MemAllocator
*squidaio_medium_bufs
= NULL
; /* 8K */
132 static MemAllocator
*squidaio_small_bufs
= NULL
; /* 4K */
133 static MemAllocator
*squidaio_tiny_bufs
= NULL
; /* 2K */
134 static MemAllocator
*squidaio_micro_bufs
= NULL
; /* 128K */
136 static int request_queue_len
= 0;
137 static MemAllocator
*squidaio_request_pool
= NULL
;
138 static MemAllocator
*squidaio_thread_pool
= NULL
;
139 static squidaio_request_queue_t request_queue
;
142 squidaio_request_t
*head
, **tailp
;
147 NULL
, &request_queue2
.head
149 static squidaio_request_queue_t done_queue
;
152 squidaio_request_t
*head
, **tailp
;
157 NULL
, &done_requests
.head
160 static HANDLE main_thread
;
162 static MemAllocator
*
163 squidaio_get_pool(int size
)
165 if (size
<= AIO_LARGE_BUFS
) {
166 if (size
<= AIO_MICRO_BUFS
)
167 return squidaio_micro_bufs
;
168 else if (size
<= AIO_TINY_BUFS
)
169 return squidaio_tiny_bufs
;
170 else if (size
<= AIO_SMALL_BUFS
)
171 return squidaio_small_bufs
;
172 else if (size
<= AIO_MEDIUM_BUFS
)
173 return squidaio_medium_bufs
;
175 return squidaio_large_bufs
;
182 squidaio_xmalloc(int size
)
187 if ((pool
= squidaio_get_pool(size
)) != NULL
) {
196 squidaio_xstrdup(const char *str
)
199 int len
= strlen(str
) + 1;
201 p
= (char *)squidaio_xmalloc(len
);
202 strncpy(p
, str
, len
);
208 squidaio_xfree(void *p
, int size
)
212 if ((pool
= squidaio_get_pool(size
)) != NULL
) {
219 squidaio_xstrfree(char *str
)
222 int len
= strlen(str
) + 1;
224 if ((pool
= squidaio_get_pool(len
)) != NULL
) {
234 squidaio_thread_t
*threadp
;
236 if (squidaio_initialised
)
239 if (!DuplicateHandle(GetCurrentProcess(), /* pseudo handle, don't close */
240 GetCurrentThread(), /* pseudo handle to copy */
241 GetCurrentProcess(), /* pseudo handle, don't close */
243 0, /* required access */
244 FALSE
, /* child process's don't inherit the handle */
245 DUPLICATE_SAME_ACCESS
)) {
247 fatal("Couldn't get current thread handle");
250 /* Initialize request queue */
251 if ((request_queue
.mutex
= CreateMutex(NULL
, /* no inheritance */
252 FALSE
, /* start unowned (as per mutex_init) */
255 fatal("Failed to create mutex");
258 if ((request_queue
.cond
= CreateEvent(NULL
, /* no inheritance */
259 FALSE
, /* auto signal reset - which I think is pthreads like ? */
260 FALSE
, /* start non signaled */
263 fatal("Failed to create condition variable");
266 request_queue
.head
= NULL
;
268 request_queue
.tailp
= &request_queue
.head
;
270 request_queue
.requests
= 0;
272 request_queue
.blocked
= 0;
274 /* Initialize done queue */
276 if ((done_queue
.mutex
= CreateMutex(NULL
, /* no inheritance */
277 FALSE
, /* start unowned (as per mutex_init) */
280 fatal("Failed to create mutex");
283 if ((done_queue
.cond
= CreateEvent(NULL
, /* no inheritance */
284 TRUE
, /* manually signaled - which I think is pthreads like ? */
285 FALSE
, /* start non signaled */
288 fatal("Failed to create condition variable");
291 done_queue
.head
= NULL
;
293 done_queue
.tailp
= &done_queue
.head
;
295 done_queue
.requests
= 0;
297 done_queue
.blocked
= 0;
299 // Initialize the thread I/O pipes before creating any threads
300 // see bug 3189 comment 5 about race conditions.
301 CommIO::Initialize();
303 /* Create threads and get them to sit in their wait loop */
304 squidaio_thread_pool
= memPoolCreate("aio_thread", sizeof(squidaio_thread_t
));
308 for (i
= 0; i
< NUMTHREADS
; ++i
) {
309 threadp
= (squidaio_thread_t
*)squidaio_thread_pool
->alloc();
310 threadp
->status
= _THREAD_STARTING
;
311 threadp
->current_req
= NULL
;
312 threadp
->requests
= 0;
313 threadp
->next
= threads
;
316 if ((threadp
->thread
= CreateThread(NULL
, /* no security attributes */
317 0, /* use default stack size */
318 squidaio_thread_loop
, /* thread function */
319 threadp
, /* argument to thread function */
320 0, /* use default creation flags */
321 &(threadp
->dwThreadId
)) /* returns the thread identifier */
323 fprintf(stderr
, "Thread creation failed\n");
324 threadp
->status
= _THREAD_FAILED
;
328 /* Set the new thread priority above parent process */
329 SetThreadPriority(threadp
->thread
,THREAD_PRIORITY_ABOVE_NORMAL
);
332 /* Create request pool */
333 squidaio_request_pool
= memPoolCreate("aio_request", sizeof(squidaio_request_t
));
335 squidaio_large_bufs
= memPoolCreate("squidaio_large_bufs", AIO_LARGE_BUFS
);
337 squidaio_medium_bufs
= memPoolCreate("squidaio_medium_bufs", AIO_MEDIUM_BUFS
);
339 squidaio_small_bufs
= memPoolCreate("squidaio_small_bufs", AIO_SMALL_BUFS
);
341 squidaio_tiny_bufs
= memPoolCreate("squidaio_tiny_bufs", AIO_TINY_BUFS
);
343 squidaio_micro_bufs
= memPoolCreate("squidaio_micro_bufs", AIO_MICRO_BUFS
);
345 squidaio_initialised
= 1;
349 squidaio_shutdown(void)
351 squidaio_thread_t
*threadp
;
355 if (!squidaio_initialised
)
358 /* This is the same as in squidaio_sync */
360 squidaio_poll_queues();
361 } while (request_queue_len
> 0);
363 hthreads
= (HANDLE
*) xcalloc (NUMTHREADS
, sizeof (HANDLE
));
367 for (i
= 0; i
< NUMTHREADS
; ++i
) {
369 hthreads
[i
] = threadp
->thread
;
370 threadp
= threadp
->next
;
373 ReleaseMutex(request_queue
.mutex
);
374 ResetEvent(request_queue
.cond
);
375 ReleaseMutex(done_queue
.mutex
);
376 ResetEvent(done_queue
.cond
);
379 WaitForMultipleObjects(NUMTHREADS
, hthreads
, TRUE
, 2000);
381 for (i
= 0; i
< NUMTHREADS
; ++i
) {
382 CloseHandle(hthreads
[i
]);
385 CloseHandle(main_thread
);
386 CommIO::NotifyIOClose();
388 squidaio_initialised
= 0;
393 squidaio_thread_loop(LPVOID lpParam
)
395 squidaio_thread_t
*threadp
= (squidaio_thread_t
*)lpParam
;
396 squidaio_request_t
*request
;
397 HANDLE cond
; /* local copy of the event queue because win32 event handles
398 * don't atomically release the mutex as cond variables do. */
400 /* lock the thread info */
402 if (WAIT_FAILED
== WaitForSingleObject(request_queue
.mutex
, INFINITE
)) {
403 fatal("Can't get ownership of mutex\n");
406 /* duplicate the handle */
407 if (!DuplicateHandle(GetCurrentProcess(), /* pseudo handle, don't close */
408 request_queue
.cond
, /* handle to copy */
409 GetCurrentProcess(), /* pseudo handle, don't close */
411 0, /* required access */
412 FALSE
, /* child process's don't inherit the handle */
413 DUPLICATE_SAME_ACCESS
))
414 fatal("Can't duplicate mutex handle\n");
416 if (!ReleaseMutex(request_queue
.mutex
)) {
418 fatal("Can't release mutex\n");
425 threadp
->current_req
= request
= NULL
;
427 /* Get a request to process */
428 threadp
->status
= _THREAD_WAITING
;
431 CloseHandle(request_queue
.mutex
);
436 rv
= WaitForSingleObject(request_queue
.mutex
, INFINITE
);
438 if (rv
== WAIT_FAILED
) {
443 while (!request_queue
.head
) {
444 if (!ReleaseMutex(request_queue
.mutex
)) {
446 threadp
->status
= _THREAD_FAILED
;
451 rv
= WaitForSingleObject(cond
, INFINITE
);
453 if (rv
== WAIT_FAILED
) {
458 rv
= WaitForSingleObject(request_queue
.mutex
, INFINITE
);
460 if (rv
== WAIT_FAILED
) {
466 request
= request_queue
.head
;
469 request_queue
.head
= request
->next
;
471 if (!request_queue
.head
)
472 request_queue
.tailp
= &request_queue
.head
;
474 if (!ReleaseMutex(request_queue
.mutex
)) {
481 /* process the request */
482 threadp
->status
= _THREAD_BUSY
;
484 request
->next
= NULL
;
486 threadp
->current_req
= request
;
490 if (!request
->cancelled
) {
491 switch (request
->request_type
) {
494 squidaio_do_open(request
);
498 squidaio_do_read(request
);
502 squidaio_do_write(request
);
506 squidaio_do_close(request
);
510 squidaio_do_unlink(request
);
513 #if AIO_OPENDIR /* Opendir not implemented yet */
515 case _AIO_OP_OPENDIR
:
516 squidaio_do_opendir(request
);
521 squidaio_do_stat(request
);
526 request
->err
= EINVAL
;
529 } else { /* cancelled */
531 request
->err
= EINTR
;
534 threadp
->status
= _THREAD_DONE
;
535 /* put the request in the done queue */
536 rv
= WaitForSingleObject(done_queue
.mutex
, INFINITE
);
538 if (rv
== WAIT_FAILED
) {
543 *done_queue
.tailp
= request
;
544 done_queue
.tailp
= &request
->next
;
546 if (!ReleaseMutex(done_queue
.mutex
)) {
551 CommIO::NotifyIOCompleted();
553 ++ threadp
->requests
;
554 } /* while forever */
559 } /* squidaio_thread_loop */
562 squidaio_queue_request(squidaio_request_t
* request
)
564 static int high_start
= 0;
565 debugs(43, 9, "squidaio_queue_request: " << request
<< " type=" << request
->request_type
<< " result=" << request
->resultp
);
566 /* Mark it as not executed (failing result, no error) */
569 /* Internal housekeeping */
570 request_queue_len
+= 1;
571 request
->resultp
->_data
= request
;
572 /* Play some tricks with the request_queue2 queue */
573 request
->next
= NULL
;
575 if (WaitForSingleObject(request_queue
.mutex
, 0) == WAIT_OBJECT_0
) {
576 if (request_queue2
.head
) {
577 /* Grab blocked requests */
578 *request_queue
.tailp
= request_queue2
.head
;
579 request_queue
.tailp
= request_queue2
.tailp
;
582 /* Enqueue request */
583 *request_queue
.tailp
= request
;
585 request_queue
.tailp
= &request
->next
;
587 if (!SetEvent(request_queue
.cond
))
588 fatal("Couldn't push queue");
590 if (!ReleaseMutex(request_queue
.mutex
)) {
591 /* unexpected error */
592 fatal("Couldn't push queue");
597 if (request_queue2
.head
) {
598 /* Clear queue of blocked requests */
599 request_queue2
.head
= NULL
;
600 request_queue2
.tailp
= &request_queue2
.head
;
603 /* Oops, the request queue is blocked, use request_queue2 */
604 *request_queue2
.tailp
= request
;
605 request_queue2
.tailp
= &request
->next
;
608 if (request_queue2
.head
) {
609 static int filter
= 0;
610 static int filter_limit
= 8;
612 if (++filter
>= filter_limit
) {
613 filter_limit
+= filter
;
615 debugs(43, DBG_IMPORTANT
, "squidaio_queue_request: WARNING - Queue congestion");
619 /* Warn if out of threads */
620 if (request_queue_len
> MAGIC1
) {
621 static int last_warn
= 0;
622 static int queue_high
, queue_low
;
624 if (high_start
== 0) {
625 high_start
= (int)squid_curtime
;
626 queue_high
= request_queue_len
;
627 queue_low
= request_queue_len
;
630 if (request_queue_len
> queue_high
)
631 queue_high
= request_queue_len
;
633 if (request_queue_len
< queue_low
)
634 queue_low
= request_queue_len
;
636 if (squid_curtime
>= (last_warn
+ 15) &&
637 squid_curtime
>= (high_start
+ 5)) {
638 debugs(43, DBG_IMPORTANT
, "squidaio_queue_request: WARNING - Disk I/O overloading");
640 if (squid_curtime
>= (high_start
+ 15))
641 debugs(43, DBG_IMPORTANT
, "squidaio_queue_request: Queue Length: current=" <<
642 request_queue_len
<< ", high=" << queue_high
<<
643 ", low=" << queue_low
<< ", duration=" <<
644 (long int) (squid_curtime
- high_start
));
646 last_warn
= (int)squid_curtime
;
652 /* Warn if seriously overloaded */
653 if (request_queue_len
> RIDICULOUS_LENGTH
) {
654 debugs(43, DBG_CRITICAL
, "squidaio_queue_request: Async request queue growing uncontrollably!");
655 debugs(43, DBG_CRITICAL
, "squidaio_queue_request: Syncing pending I/O operations.. (blocking)");
657 debugs(43, DBG_CRITICAL
, "squidaio_queue_request: Synced");
659 } /* squidaio_queue_request */
662 squidaio_cleanup_request(squidaio_request_t
* requestp
)
664 squidaio_result_t
*resultp
= requestp
->resultp
;
665 int cancelled
= requestp
->cancelled
;
667 /* Free allocated structures and copy data back to user space if the */
668 /* request hasn't been cancelled */
670 switch (requestp
->request_type
) {
674 if (!cancelled
&& requestp
->ret
== 0)
675 memcpy(requestp
->statp
, requestp
->tmpstatp
, sizeof(struct stat
));
677 squidaio_xfree(requestp
->tmpstatp
, sizeof(struct stat
));
679 squidaio_xstrfree(requestp
->path
);
684 if (cancelled
&& requestp
->ret
>= 0)
685 /* The open() was cancelled but completed */
686 close(requestp
->ret
);
688 squidaio_xstrfree(requestp
->path
);
693 if (cancelled
&& requestp
->ret
< 0)
694 /* The close() was cancelled and never got executed */
701 case _AIO_OP_OPENDIR
:
702 squidaio_xstrfree(requestp
->path
);
716 if (resultp
!= NULL
&& !cancelled
) {
717 resultp
->aio_return
= requestp
->ret
;
718 resultp
->aio_errno
= requestp
->err
;
721 squidaio_request_pool
->freeOne(requestp
);
722 } /* squidaio_cleanup_request */
725 squidaio_cancel(squidaio_result_t
* resultp
)
727 squidaio_request_t
*request
= (squidaio_request_t
*)resultp
->_data
;
729 if (request
&& request
->resultp
== resultp
) {
730 debugs(43, 9, "squidaio_cancel: " << request
<< " type=" << request
->request_type
<< " result=" << request
->resultp
);
731 request
->cancelled
= 1;
732 request
->resultp
= NULL
;
733 resultp
->_data
= NULL
;
734 resultp
->result_type
= _AIO_OP_NONE
;
739 } /* squidaio_cancel */
742 squidaio_open(const char *path
, int oflag
, mode_t mode
, squidaio_result_t
* resultp
)
745 squidaio_request_t
*requestp
;
747 requestp
= (squidaio_request_t
*)squidaio_request_pool
->alloc();
749 requestp
->path
= (char *) squidaio_xstrdup(path
);
751 requestp
->oflag
= oflag
;
753 requestp
->mode
= mode
;
755 requestp
->resultp
= resultp
;
757 requestp
->request_type
= _AIO_OP_OPEN
;
759 requestp
->cancelled
= 0;
761 resultp
->result_type
= _AIO_OP_OPEN
;
763 squidaio_queue_request(requestp
);
769 squidaio_do_open(squidaio_request_t
* requestp
)
771 requestp
->ret
= open(requestp
->path
, requestp
->oflag
, requestp
->mode
);
772 requestp
->err
= errno
;
776 squidaio_read(int fd
, char *bufp
, size_t bufs
, off_t offset
, int whence
, squidaio_result_t
* resultp
)
778 squidaio_request_t
*requestp
;
780 requestp
= (squidaio_request_t
*)squidaio_request_pool
->alloc();
784 requestp
->bufferp
= bufp
;
786 requestp
->buflen
= bufs
;
788 requestp
->offset
= offset
;
790 requestp
->whence
= whence
;
792 requestp
->resultp
= resultp
;
794 requestp
->request_type
= _AIO_OP_READ
;
796 requestp
->cancelled
= 0;
798 resultp
->result_type
= _AIO_OP_READ
;
800 squidaio_queue_request(requestp
);
806 squidaio_do_read(squidaio_request_t
* requestp
)
808 lseek(requestp
->fd
, requestp
->offset
, requestp
->whence
);
810 if (!ReadFile((HANDLE
)_get_osfhandle(requestp
->fd
), requestp
->bufferp
,
811 requestp
->buflen
, (LPDWORD
)&requestp
->ret
, NULL
)) {
812 WIN32_maperror(GetLastError());
816 requestp
->err
= errno
;
820 squidaio_write(int fd
, char *bufp
, size_t bufs
, off_t offset
, int whence
, squidaio_result_t
* resultp
)
822 squidaio_request_t
*requestp
;
824 requestp
= (squidaio_request_t
*)squidaio_request_pool
->alloc();
828 requestp
->bufferp
= bufp
;
830 requestp
->buflen
= bufs
;
832 requestp
->offset
= offset
;
834 requestp
->whence
= whence
;
836 requestp
->resultp
= resultp
;
838 requestp
->request_type
= _AIO_OP_WRITE
;
840 requestp
->cancelled
= 0;
842 resultp
->result_type
= _AIO_OP_WRITE
;
844 squidaio_queue_request(requestp
);
850 squidaio_do_write(squidaio_request_t
* requestp
)
852 if (!WriteFile((HANDLE
)_get_osfhandle(requestp
->fd
), requestp
->bufferp
,
853 requestp
->buflen
, (LPDWORD
)&requestp
->ret
, NULL
)) {
854 WIN32_maperror(GetLastError());
858 requestp
->err
= errno
;
862 squidaio_close(int fd
, squidaio_result_t
* resultp
)
864 squidaio_request_t
*requestp
;
866 requestp
= (squidaio_request_t
*)squidaio_request_pool
->alloc();
870 requestp
->resultp
= resultp
;
872 requestp
->request_type
= _AIO_OP_CLOSE
;
874 requestp
->cancelled
= 0;
876 resultp
->result_type
= _AIO_OP_CLOSE
;
878 squidaio_queue_request(requestp
);
884 squidaio_do_close(squidaio_request_t
* requestp
)
886 if ((requestp
->ret
= close(requestp
->fd
)) < 0) {
887 debugs(43, DBG_CRITICAL
, "squidaio_do_close: FD " << requestp
->fd
<< ", errno " << errno
);
891 requestp
->err
= errno
;
896 squidaio_stat(const char *path
, struct stat
*sb
, squidaio_result_t
* resultp
)
899 squidaio_request_t
*requestp
;
901 requestp
= (squidaio_request_t
*)squidaio_request_pool
->alloc();
903 requestp
->path
= (char *) squidaio_xstrdup(path
);
905 requestp
->statp
= sb
;
907 requestp
->tmpstatp
= (struct stat
*) squidaio_xmalloc(sizeof(struct stat
));
909 requestp
->resultp
= resultp
;
911 requestp
->request_type
= _AIO_OP_STAT
;
913 requestp
->cancelled
= 0;
915 resultp
->result_type
= _AIO_OP_STAT
;
917 squidaio_queue_request(requestp
);
923 squidaio_do_stat(squidaio_request_t
* requestp
)
925 requestp
->ret
= stat(requestp
->path
, requestp
->tmpstatp
);
926 requestp
->err
= errno
;
930 squidaio_unlink(const char *path
, squidaio_result_t
* resultp
)
933 squidaio_request_t
*requestp
;
935 requestp
= (squidaio_request_t
*)squidaio_request_pool
->alloc();
937 requestp
->path
= squidaio_xstrdup(path
);
939 requestp
->resultp
= resultp
;
941 requestp
->request_type
= _AIO_OP_UNLINK
;
943 requestp
->cancelled
= 0;
945 resultp
->result_type
= _AIO_OP_UNLINK
;
947 squidaio_queue_request(requestp
);
953 squidaio_do_unlink(squidaio_request_t
* requestp
)
955 requestp
->ret
= unlink(requestp
->path
);
956 requestp
->err
= errno
;
960 /* XXX squidaio_opendir NOT implemented yet.. */
963 squidaio_opendir(const char *path
, squidaio_result_t
* resultp
)
965 squidaio_request_t
*requestp
;
968 requestp
= squidaio_request_pool
->alloc();
970 resultp
->result_type
= _AIO_OP_OPENDIR
;
976 squidaio_do_opendir(squidaio_request_t
* requestp
)
978 /* NOT IMPLEMENTED */
984 squidaio_poll_queues(void)
986 /* kick "overflow" request queue */
988 if (request_queue2
.head
&&
989 (WaitForSingleObject(request_queue
.mutex
, 0 )== WAIT_OBJECT_0
)) {
990 *request_queue
.tailp
= request_queue2
.head
;
991 request_queue
.tailp
= request_queue2
.tailp
;
993 if (!SetEvent(request_queue
.cond
))
994 fatal("couldn't push queue\n");
996 if (!ReleaseMutex(request_queue
.mutex
)) {
997 /* unexpected error */
1001 request_queue2
.head
= NULL
;
1002 request_queue2
.tailp
= &request_queue2
.head
;
1005 /* poll done queue */
1006 if (done_queue
.head
&&
1007 (WaitForSingleObject(done_queue
.mutex
, 0)==WAIT_OBJECT_0
)) {
1009 struct squidaio_request_t
*requests
= done_queue
.head
;
1010 done_queue
.head
= NULL
;
1011 done_queue
.tailp
= &done_queue
.head
;
1013 if (!ReleaseMutex(done_queue
.mutex
)) {
1014 /* unexpected error */
1018 *done_requests
.tailp
= requests
;
1019 request_queue_len
-= 1;
1021 while (requests
->next
) {
1022 requests
= requests
->next
;
1023 request_queue_len
-= 1;
1026 done_requests
.tailp
= &requests
->next
;
1031 squidaio_poll_done(void)
1033 squidaio_request_t
*request
;
1034 squidaio_result_t
*resultp
;
1039 request
= done_requests
.head
;
1041 if (request
== NULL
&& !polled
) {
1042 CommIO::ResetNotifications();
1043 squidaio_poll_queues();
1045 request
= done_requests
.head
;
1052 debugs(43, 9, "squidaio_poll_done: " << request
<< " type=" << request
->request_type
<< " result=" << request
->resultp
);
1053 done_requests
.head
= request
->next
;
1055 if (!done_requests
.head
)
1056 done_requests
.tailp
= &done_requests
.head
;
1058 resultp
= request
->resultp
;
1060 cancelled
= request
->cancelled
;
1062 squidaio_debug(request
);
1064 debugs(43, 5, "DONE: " << request
->ret
<< " -> " << request
->err
);
1066 squidaio_cleanup_request(request
);
1072 } /* squidaio_poll_done */
1075 squidaio_operations_pending(void)
1077 return request_queue_len
+ (done_requests
.head
? 1 : 0);
1083 /* XXX This might take a while if the queue is large.. */
1086 squidaio_poll_queues();
1087 } while (request_queue_len
> 0);
1089 return squidaio_operations_pending();
1093 squidaio_get_queue_len(void)
1095 return request_queue_len
;
1099 squidaio_debug(squidaio_request_t
* request
)
1101 switch (request
->request_type
) {
1104 debugs(43, 5, "OPEN of " << request
->path
<< " to FD " << request
->ret
);
1108 debugs(43, 5, "READ on fd: " << request
->fd
);
1112 debugs(43, 5, "WRITE on fd: " << request
->fd
);
1116 debugs(43, 5, "CLOSE of fd: " << request
->fd
);
1119 case _AIO_OP_UNLINK
:
1120 debugs(43, 5, "UNLINK of " << request
->path
);
1129 squidaio_stats(StoreEntry
* sentry
)
1131 squidaio_thread_t
*threadp
;
1134 if (!squidaio_initialised
)
1137 storeAppendPrintf(sentry
, "\n\nThreads Status:\n");
1139 storeAppendPrintf(sentry
, "#\tID\t# Requests\n");
1143 for (i
= 0; i
< NUMTHREADS
; ++i
) {
1144 storeAppendPrintf(sentry
, "%i\t0x%lx\t%ld\n", i
+ 1, threadp
->dwThreadId
, threadp
->requests
);
1145 threadp
= threadp
->next
;