-
Notifications
You must be signed in to change notification settings - Fork 493
/
aiops.cc
1059 lines (775 loc) · 24.8 KB
/
aiops.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: aiops.cc,v 1.13 2007/04/12 23:51:57 wessels Exp $
*
* DEBUG: section 43 AIOPS
* AUTHOR: Stewart Forster <slf@connect.com.au>
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
* Squid is the result of efforts by numerous individuals from
* the Internet community; see the CONTRIBUTORS file for full
* details. Many organizations have provided support for Squid's
* development; see the SPONSORS file for full details. Squid is
* Copyrighted (C) 2001 by the Regents of the University of
* California; see the COPYRIGHT file for full details. Squid
* incorporates software developed and/or copyrighted by other
* sources; see the CREDITS file for full details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#ifndef _REENTRANT
#error "_REENTRANT MUST be defined to build squid async io support."
#endif
#include "squid.h"
#include "DiskThreads.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <dirent.h>
#include <signal.h>
#if HAVE_SCHED_H
#include <sched.h>
#endif
#include "CommIO.h"
#include "SquidTime.h"
#include "Store.h"
#define RIDICULOUS_LENGTH 4096
enum _squidaio_thread_status {
_THREAD_STARTING = 0,
_THREAD_WAITING,
_THREAD_BUSY,
_THREAD_FAILED,
_THREAD_DONE
};
typedef enum _squidaio_thread_status squidaio_thread_status;
typedef struct squidaio_request_t
{
struct squidaio_request_t *next;
squidaio_request_type request_type;
int cancelled;
char *path;
int oflag;
mode_t mode;
int fd;
char *bufferp;
int buflen;
off_t offset;
int whence;
int ret;
int err;
struct stat *tmpstatp;
struct stat *statp;
squidaio_result_t *resultp;
}
squidaio_request_t;
typedef struct squidaio_request_queue_t
{
pthread_mutex_t mutex;
pthread_cond_t cond;
squidaio_request_t *volatile head;
squidaio_request_t *volatile *volatile tailp;
unsigned long requests;
unsigned long blocked; /* main failed to lock the queue */
}
squidaio_request_queue_t;
typedef struct squidaio_thread_t squidaio_thread_t;
struct squidaio_thread_t
{
squidaio_thread_t *next;
pthread_t thread;
squidaio_thread_status status;
struct squidaio_request_t *current_req;
unsigned long requests;
};
static void squidaio_queue_request(squidaio_request_t *);
static void squidaio_cleanup_request(squidaio_request_t *);
static void *squidaio_thread_loop(void *);
static void squidaio_do_open(squidaio_request_t *);
static void squidaio_do_read(squidaio_request_t *);
static void squidaio_do_write(squidaio_request_t *);
static void squidaio_do_close(squidaio_request_t *);
static void squidaio_do_stat(squidaio_request_t *);
static void squidaio_do_unlink(squidaio_request_t *);
#if AIO_OPENDIR
static void *squidaio_do_opendir(squidaio_request_t *);
#endif
static void squidaio_debug(squidaio_request_t *);
static void squidaio_poll_queues(void);
static squidaio_thread_t *threads = NULL;
static int squidaio_initialised = 0;
#define AIO_LARGE_BUFS 16384
#define AIO_MEDIUM_BUFS AIO_LARGE_BUFS >> 1
#define AIO_SMALL_BUFS AIO_LARGE_BUFS >> 2
#define AIO_TINY_BUFS AIO_LARGE_BUFS >> 3
#define AIO_MICRO_BUFS 128
static MemAllocator *squidaio_large_bufs = NULL; /* 16K */
static MemAllocator *squidaio_medium_bufs = NULL; /* 8K */
static MemAllocator *squidaio_small_bufs = NULL; /* 4K */
static MemAllocator *squidaio_tiny_bufs = NULL; /* 2K */
static MemAllocator *squidaio_micro_bufs = NULL; /* 128K */
static int request_queue_len = 0;
static MemAllocator *squidaio_request_pool = NULL;
static MemAllocator *squidaio_thread_pool = NULL;
static squidaio_request_queue_t request_queue;
static struct
{
squidaio_request_t *head, **tailp;
}
request_queue2 = {
NULL, &request_queue2.head
};
static squidaio_request_queue_t done_queue;
static struct
{
squidaio_request_t *head, **tailp;
}
done_requests = {
NULL, &done_requests.head
};
static pthread_attr_t globattr;
#if HAVE_SCHED_H
static struct sched_param globsched;
#endif
static pthread_t main_thread;
static MemAllocator *
squidaio_get_pool(int size)
{
if (size <= AIO_LARGE_BUFS) {
if (size <= AIO_MICRO_BUFS)
return squidaio_micro_bufs;
else if (size <= AIO_TINY_BUFS)
return squidaio_tiny_bufs;
else if (size <= AIO_SMALL_BUFS)
return squidaio_small_bufs;
else if (size <= AIO_MEDIUM_BUFS)
return squidaio_medium_bufs;
else
return squidaio_large_bufs;
}
return NULL;
}
void *
squidaio_xmalloc(int size)
{
void *p;
MemAllocator *pool;
if ((pool = squidaio_get_pool(size)) != NULL) {
p = pool->alloc();
} else
p = xmalloc(size);
return p;
}
static char *
squidaio_xstrdup(const char *str)
{
char *p;
int len = strlen(str) + 1;
p = (char *)squidaio_xmalloc(len);
strncpy(p, str, len);
return p;
}
void
squidaio_xfree(void *p, int size)
{
MemAllocator *pool;
if ((pool = squidaio_get_pool(size)) != NULL) {
pool->free(p);
} else
xfree(p);
}
static void
squidaio_xstrfree(char *str)
{
MemAllocator *pool;
int len = strlen(str) + 1;
if ((pool = squidaio_get_pool(len)) != NULL) {
pool->free(str);
} else
xfree(str);
}
void
squidaio_init(void)
{
int i;
squidaio_thread_t *threadp;
if (squidaio_initialised)
return;
pthread_attr_init(&globattr);
#if HAVE_PTHREAD_ATTR_SETSCOPE
pthread_attr_setscope(&globattr, PTHREAD_SCOPE_SYSTEM);
#endif
#if HAVE_SCHED_H
globsched.sched_priority = 1;
#endif
main_thread = pthread_self();
#if HAVE_SCHED_H && HAVE_PTHREAD_SETSCHEDPARAM
pthread_setschedparam(main_thread, SCHED_OTHER, &globsched);
#endif
#if HAVE_SCHED_H
globsched.sched_priority = 2;
#endif
#if HAVE_SCHED_H && HAVE_PTHREAD_ATTR_SETSCHEDPARAM
pthread_attr_setschedparam(&globattr, &globsched);
#endif
/* Give each thread a smaller 256KB stack, should be more than sufficient */
pthread_attr_setstacksize(&globattr, 256 * 1024);
/* Initialize request queue */
if (pthread_mutex_init(&(request_queue.mutex), NULL))
fatal("Failed to create mutex");
if (pthread_cond_init(&(request_queue.cond), NULL))
fatal("Failed to create condition variable");
request_queue.head = NULL;
request_queue.tailp = &request_queue.head;
request_queue.requests = 0;
request_queue.blocked = 0;
/* Initialize done queue */
if (pthread_mutex_init(&(done_queue.mutex), NULL))
fatal("Failed to create mutex");
if (pthread_cond_init(&(done_queue.cond), NULL))
fatal("Failed to create condition variable");
done_queue.head = NULL;
done_queue.tailp = &done_queue.head;
done_queue.requests = 0;
done_queue.blocked = 0;
/* Create threads and get them to sit in their wait loop */
squidaio_thread_pool = memPoolCreate("aio_thread", sizeof(squidaio_thread_t));
assert(NUMTHREADS);
for (i = 0; i < NUMTHREADS; i++) {
threadp = (squidaio_thread_t *)squidaio_thread_pool->alloc();
threadp->status = _THREAD_STARTING;
threadp->current_req = NULL;
threadp->requests = 0;
threadp->next = threads;
threads = threadp;
if (pthread_create(&threadp->thread, &globattr, squidaio_thread_loop, threadp)) {
fprintf(stderr, "Thread creation failed\n");
threadp->status = _THREAD_FAILED;
continue;
}
}
/* Create request pool */
squidaio_request_pool = memPoolCreate("aio_request", sizeof(squidaio_request_t));
squidaio_large_bufs = memPoolCreate("squidaio_large_bufs", AIO_LARGE_BUFS);
squidaio_medium_bufs = memPoolCreate("squidaio_medium_bufs", AIO_MEDIUM_BUFS);
squidaio_small_bufs = memPoolCreate("squidaio_small_bufs", AIO_SMALL_BUFS);
squidaio_tiny_bufs = memPoolCreate("squidaio_tiny_bufs", AIO_TINY_BUFS);
squidaio_micro_bufs = memPoolCreate("squidaio_micro_bufs", AIO_MICRO_BUFS);
squidaio_initialised = 1;
}
void
squidaio_shutdown(void)
{
if (!squidaio_initialised)
return;
/* This is the same as in squidaio_sync */
do {
squidaio_poll_queues();
} while (request_queue_len > 0);
CommIO::NotifyIOClose();
squidaio_initialised = 0;
}
static void *
squidaio_thread_loop(void *ptr)
{
squidaio_thread_t *threadp = (squidaio_thread_t *)ptr;
squidaio_request_t *request;
sigset_t newSig;
/*
* Make sure to ignore signals which may possibly get sent to
* the parent squid thread. Causes havoc with mutex's and
* condition waits otherwise
*/
sigemptyset(&newSig);
sigaddset(&newSig, SIGPIPE);
sigaddset(&newSig, SIGCHLD);
#ifdef _SQUID_LINUX_THREADS_
sigaddset(&newSig, SIGQUIT);
sigaddset(&newSig, SIGTRAP);
#else
sigaddset(&newSig, SIGUSR1);
sigaddset(&newSig, SIGUSR2);
#endif
sigaddset(&newSig, SIGHUP);
sigaddset(&newSig, SIGTERM);
sigaddset(&newSig, SIGINT);
sigaddset(&newSig, SIGALRM);
pthread_sigmask(SIG_BLOCK, &newSig, NULL);
while (1) {
threadp->current_req = request = NULL;
request = NULL;
/* Get a request to process */
threadp->status = _THREAD_WAITING;
pthread_mutex_lock(&request_queue.mutex);
while (!request_queue.head) {
pthread_cond_wait(&request_queue.cond, &request_queue.mutex);
}
request = request_queue.head;
if (request)
request_queue.head = request->next;
if (!request_queue.head)
request_queue.tailp = &request_queue.head;
pthread_mutex_unlock(&request_queue.mutex);
/* process the request */
threadp->status = _THREAD_BUSY;
request->next = NULL;
threadp->current_req = request;
errno = 0;
if (!request->cancelled) {
switch (request->request_type) {
case _AIO_OP_OPEN:
squidaio_do_open(request);
break;
case _AIO_OP_READ:
squidaio_do_read(request);
break;
case _AIO_OP_WRITE:
squidaio_do_write(request);
break;
case _AIO_OP_CLOSE:
squidaio_do_close(request);
break;
case _AIO_OP_UNLINK:
squidaio_do_unlink(request);
break;
#if AIO_OPENDIR /* Opendir not implemented yet */
case _AIO_OP_OPENDIR:
squidaio_do_opendir(request);
break;
#endif
case _AIO_OP_STAT:
squidaio_do_stat(request);
break;
default:
request->ret = -1;
request->err = EINVAL;
break;
}
} else { /* cancelled */
request->ret = -1;
request->err = EINTR;
}
threadp->status = _THREAD_DONE;
/* put the request in the done queue */
pthread_mutex_lock(&done_queue.mutex);
*done_queue.tailp = request;
done_queue.tailp = &request->next;
pthread_mutex_unlock(&done_queue.mutex);
CommIO::NotifyIOCompleted();
threadp->requests++;
} /* while forever */
return NULL;
} /* squidaio_thread_loop */
static void
squidaio_queue_request(squidaio_request_t * request)
{
static int high_start = 0;
debug(43, 9) ("squidaio_queue_request: %p type=%d result=%p\n",
request, request->request_type, request->resultp);
/* Mark it as not executed (failing result, no error) */
request->ret = -1;
request->err = 0;
/* Internal housekeeping */
request_queue_len += 1;
request->resultp->_data = request;
/* Play some tricks with the request_queue2 queue */
request->next = NULL;
if (pthread_mutex_trylock(&request_queue.mutex) == 0) {
if (request_queue2.head) {
/* Grab blocked requests */
*request_queue.tailp = request_queue2.head;
request_queue.tailp = request_queue2.tailp;
}
/* Enqueue request */
*request_queue.tailp = request;
request_queue.tailp = &request->next;
pthread_cond_signal(&request_queue.cond);
pthread_mutex_unlock(&request_queue.mutex);
if (request_queue2.head) {
/* Clear queue of blocked requests */
request_queue2.head = NULL;
request_queue2.tailp = &request_queue2.head;
}
} else {
/* Oops, the request queue is blocked, use request_queue2 */
*request_queue2.tailp = request;
request_queue2.tailp = &request->next;
}
if (request_queue2.head) {
static int filter = 0;
static int filter_limit = 8;
if (++filter >= filter_limit) {
filter_limit += filter;
filter = 0;
debug(43, 1) ("squidaio_queue_request: WARNING - Queue congestion\n");
}
}
/* Warn if out of threads */
if (request_queue_len > MAGIC1) {
static int last_warn = 0;
static int queue_high, queue_low;
if (high_start == 0) {
high_start = squid_curtime;
queue_high = request_queue_len;
queue_low = request_queue_len;
}
if (request_queue_len > queue_high)
queue_high = request_queue_len;
if (request_queue_len < queue_low)
queue_low = request_queue_len;
if (squid_curtime >= (last_warn + 15) &&
squid_curtime >= (high_start + 5)) {
debug(43, 1) ("squidaio_queue_request: WARNING - Disk I/O overloading\n");
if (squid_curtime >= (high_start + 15))
debug(43, 1) ("squidaio_queue_request: Queue Length: current=%d, high=%d, low=%d, duration=%ld\n",
request_queue_len, queue_high, queue_low, (long int) (squid_curtime - high_start));
last_warn = squid_curtime;
}
} else {
high_start = 0;
}
/* Warn if seriously overloaded */
if (request_queue_len > RIDICULOUS_LENGTH) {
debug(43, 0) ("squidaio_queue_request: Async request queue growing uncontrollably!\n");
debug(43, 0) ("squidaio_queue_request: Syncing pending I/O operations.. (blocking)\n");
squidaio_sync();
debug(43, 0) ("squidaio_queue_request: Synced\n");
}
} /* squidaio_queue_request */
static void
squidaio_cleanup_request(squidaio_request_t * requestp)
{
squidaio_result_t *resultp = requestp->resultp;
int cancelled = requestp->cancelled;
/* Free allocated structures and copy data back to user space if the */
/* request hasn't been cancelled */
switch (requestp->request_type) {
case _AIO_OP_STAT:
if (!cancelled && requestp->ret == 0)
xmemcpy(requestp->statp, requestp->tmpstatp, sizeof(struct stat));
squidaio_xfree(requestp->tmpstatp, sizeof(struct stat));
squidaio_xstrfree(requestp->path);
break;
case _AIO_OP_OPEN:
if (cancelled && requestp->ret >= 0)
/* The open() was cancelled but completed */
close(requestp->ret);
squidaio_xstrfree(requestp->path);
break;
case _AIO_OP_CLOSE:
if (cancelled && requestp->ret < 0)
/* The close() was cancelled and never got executed */
close(requestp->fd);
break;
case _AIO_OP_UNLINK:
case _AIO_OP_OPENDIR:
squidaio_xstrfree(requestp->path);
break;
case _AIO_OP_READ:
break;
case _AIO_OP_WRITE:
break;
default:
break;
}
if (resultp != NULL && !cancelled) {
resultp->aio_return = requestp->ret;
resultp->aio_errno = requestp->err;
}
squidaio_request_pool->free(requestp);
} /* squidaio_cleanup_request */
int
squidaio_cancel(squidaio_result_t * resultp)
{
squidaio_request_t *request = (squidaio_request_t *)resultp->_data;
if (request && request->resultp == resultp) {
debug(43, 9) ("squidaio_cancel: %p type=%d result=%p\n",
request, request->request_type, request->resultp);
request->cancelled = 1;
request->resultp = NULL;
resultp->_data = NULL;
resultp->result_type = _AIO_OP_NONE;
return 0;
}
return 1;
} /* squidaio_cancel */
int
squidaio_open(const char *path, int oflag, mode_t mode, squidaio_result_t * resultp)
{
squidaio_init();
squidaio_request_t *requestp;
requestp = (squidaio_request_t *)squidaio_request_pool->alloc();
requestp->path = (char *) squidaio_xstrdup(path);
requestp->oflag = oflag;
requestp->mode = mode;
requestp->resultp = resultp;
requestp->request_type = _AIO_OP_OPEN;
requestp->cancelled = 0;
resultp->result_type = _AIO_OP_OPEN;
squidaio_queue_request(requestp);
return 0;
}
static void
squidaio_do_open(squidaio_request_t * requestp)
{
requestp->ret = open(requestp->path, requestp->oflag, requestp->mode);
requestp->err = errno;
}
int
squidaio_read(int fd, char *bufp, int bufs, off_t offset, int whence, squidaio_result_t * resultp)
{
squidaio_request_t *requestp;
requestp = (squidaio_request_t *)squidaio_request_pool->alloc();
requestp->fd = fd;
requestp->bufferp = bufp;
requestp->buflen = bufs;
requestp->offset = offset;
requestp->whence = whence;
requestp->resultp = resultp;
requestp->request_type = _AIO_OP_READ;
requestp->cancelled = 0;
resultp->result_type = _AIO_OP_READ;
squidaio_queue_request(requestp);
return 0;
}
static void
squidaio_do_read(squidaio_request_t * requestp)
{
lseek(requestp->fd, requestp->offset, requestp->whence);
requestp->ret = read(requestp->fd, requestp->bufferp, requestp->buflen);
requestp->err = errno;
}
int
squidaio_write(int fd, char *bufp, int bufs, off_t offset, int whence, squidaio_result_t * resultp)
{
squidaio_request_t *requestp;
requestp = (squidaio_request_t *)squidaio_request_pool->alloc();
requestp->fd = fd;
requestp->bufferp = bufp;
requestp->buflen = bufs;
requestp->offset = offset;
requestp->whence = whence;
requestp->resultp = resultp;
requestp->request_type = _AIO_OP_WRITE;
requestp->cancelled = 0;
resultp->result_type = _AIO_OP_WRITE;
squidaio_queue_request(requestp);
return 0;
}
static void
squidaio_do_write(squidaio_request_t * requestp)
{
requestp->ret = write(requestp->fd, requestp->bufferp, requestp->buflen);
requestp->err = errno;
}
int
squidaio_close(int fd, squidaio_result_t * resultp)
{
squidaio_request_t *requestp;
requestp = (squidaio_request_t *)squidaio_request_pool->alloc();
requestp->fd = fd;
requestp->resultp = resultp;
requestp->request_type = _AIO_OP_CLOSE;
requestp->cancelled = 0;
resultp->result_type = _AIO_OP_CLOSE;
squidaio_queue_request(requestp);
return 0;
}
static void
squidaio_do_close(squidaio_request_t * requestp)
{
requestp->ret = close(requestp->fd);
requestp->err = errno;
}
int
squidaio_stat(const char *path, struct stat *sb, squidaio_result_t * resultp)
{
squidaio_init();
squidaio_request_t *requestp;
requestp = (squidaio_request_t *)squidaio_request_pool->alloc();
requestp->path = (char *) squidaio_xstrdup(path);
requestp->statp = sb;
requestp->tmpstatp = (struct stat *) squidaio_xmalloc(sizeof(struct stat));
requestp->resultp = resultp;
requestp->request_type = _AIO_OP_STAT;
requestp->cancelled = 0;
resultp->result_type = _AIO_OP_STAT;
squidaio_queue_request(requestp);
return 0;
}
static void
squidaio_do_stat(squidaio_request_t * requestp)
{
requestp->ret = stat(requestp->path, requestp->tmpstatp);
requestp->err = errno;
}
int
squidaio_unlink(const char *path, squidaio_result_t * resultp)
{
squidaio_init();
squidaio_request_t *requestp;
requestp = (squidaio_request_t *)squidaio_request_pool->alloc();
requestp->path = squidaio_xstrdup(path);
requestp->resultp = resultp;
requestp->request_type = _AIO_OP_UNLINK;
requestp->cancelled = 0;
resultp->result_type = _AIO_OP_UNLINK;
squidaio_queue_request(requestp);
return 0;
}
static void
squidaio_do_unlink(squidaio_request_t * requestp)
{
requestp->ret = unlink(requestp->path);
requestp->err = errno;
}
#if AIO_OPENDIR
/* XXX squidaio_opendir NOT implemented yet.. */
int
squidaio_opendir(const char *path, squidaio_result_t * resultp)
{
squidaio_request_t *requestp;
int len;
requestp = squidaio_request_pool->alloc();
resultp->result_type = _AIO_OP_OPENDIR;
return -1;
}
static void
squidaio_do_opendir(squidaio_request_t * requestp)
{
/* NOT IMPLEMENTED */
}
#endif
static void
squidaio_poll_queues(void)
{
/* kick "overflow" request queue */
if (request_queue2.head &&
pthread_mutex_trylock(&request_queue.mutex) == 0) {
*request_queue.tailp = request_queue2.head;
request_queue.tailp = request_queue2.tailp;
pthread_cond_signal(&request_queue.cond);
pthread_mutex_unlock(&request_queue.mutex);
request_queue2.head = NULL;
request_queue2.tailp = &request_queue2.head;
}
/* poll done queue */
if (done_queue.head && pthread_mutex_trylock(&done_queue.mutex) == 0) {
struct squidaio_request_t *requests = done_queue.head;
done_queue.head = NULL;
done_queue.tailp = &done_queue.head;
pthread_mutex_unlock(&done_queue.mutex);
*done_requests.tailp = requests;
request_queue_len -= 1;
while (requests->next) {
requests = requests->next;
request_queue_len -= 1;
}
done_requests.tailp = &requests->next;
}
}
squidaio_result_t *
squidaio_poll_done(void)
{
squidaio_request_t *request;
squidaio_result_t *resultp;
int cancelled;
int polled = 0;
AIO_REPOLL:
request = done_requests.head;
if (request == NULL && !polled) {
CommIO::ResetNotifications();
squidaio_poll_queues();
polled = 1;
request = done_requests.head;
}
if (!request) {
return NULL;
}
debug(43, 9) ("squidaio_poll_done: %p type=%d result=%p\n",
request, request->request_type, request->resultp);
done_requests.head = request->next;
if (!done_requests.head)
done_requests.tailp = &done_requests.head;
resultp = request->resultp;
cancelled = request->cancelled;
squidaio_debug(request);
debug(43, 5) ("DONE: %d -> %d\n", request->ret, request->err);
squidaio_cleanup_request(request);
if (cancelled)
goto AIO_REPOLL;
return resultp;
} /* squidaio_poll_done */
int
squidaio_operations_pending(void)
{
return request_queue_len + (done_requests.head ? 1 : 0);
}
int
squidaio_sync(void)
{
/* XXX This might take a while if the queue is large.. */
do {
squidaio_poll_queues();
} while (request_queue_len > 0);