-
Notifications
You must be signed in to change notification settings - Fork 493
/
DiskThreadsIOStrategy.cc
259 lines (205 loc) · 6.99 KB
/
DiskThreadsIOStrategy.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
/*
* $Id: DiskThreadsIOStrategy.cc,v 1.11 2007/04/12 23:51:57 wessels Exp $
*
* DEBUG: section 79 Squid-side Disk I/O functions.
* AUTHOR: Robert Collins
*
* 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.
*
* Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
*/
#include "squid.h"
#include "CacheManager.h"
#include "DiskThreadsIOStrategy.h"
#include "DiskThreadsDiskFile.h"
/* for statfs */
#include "Store.h"
#include "fde.h"
void
DiskThreadsIOStrategy::init(void)
{
if (initialised)
return;
squidaio_ctrl_pool = memPoolCreate("aio_ctrl", sizeof(squidaio_ctrl_t));
initialised = true;
/*
* We'd like to call squidaio_init() here, but the configuration
* hasn't been parsed yet and we don't know how many cache_dirs
* there are, which means we don't know how many threads to start.
*/
}
void
DiskThreadsIOStrategy::registerWithCacheManager(CacheManager & manager)
{
manager.registerAction("squidaio_counts", "Async IO Function Counters",
aioStats, 0, 1);
}
void
DiskThreadsIOStrategy::done(void)
{
if (!initialised)
return;
squidaio_shutdown();
delete squidaio_ctrl_pool;
squidaio_ctrl_pool = NULL;
initialised = false;
}
int
DiskThreadsIOStrategy::callback()
{
squidaio_result_t *resultp;
squidaio_ctrl_t *ctrlp;
int retval = 0;
assert(initialised);
squidaio_counts.check_callback++;
for (;;) {
if ((resultp = squidaio_poll_done()) == NULL)
break;
ctrlp = (squidaio_ctrl_t *) resultp->data;
switch (resultp->result_type) {
case _AIO_OP_NONE:
case _AIO_OP_OPENDIR:
break;
case _AIO_OP_OPEN:
++squidaio_counts.open_finish;
break;
case _AIO_OP_READ:
++squidaio_counts.read_finish;
break;
case _AIO_OP_WRITE:
++squidaio_counts.write_finish;
break;
case _AIO_OP_CLOSE:
++squidaio_counts.close_finish;
break;
case _AIO_OP_UNLINK:
++squidaio_counts.unlink_finish;
break;
case _AIO_OP_STAT:
++squidaio_counts.stat_finish;
break;
}
if (ctrlp == NULL)
continue; /* XXX Should not happen */
dlinkDelete(&ctrlp->node, &used_list);
if (ctrlp->done_handler) {
AIOCB *callback = ctrlp->done_handler;
void *cbdata;
ctrlp->done_handler = NULL;
if (cbdataReferenceValidDone(ctrlp->done_handler_data, &cbdata)) {
retval = 1; /* Return that we've actually done some work */
callback(ctrlp->fd, cbdata, ctrlp->bufp,
ctrlp->result.aio_return, ctrlp->result.aio_errno);
} else {
if (ctrlp->operation == _AIO_OPEN) {
/* The open operation was aborted.. */
int fd = ctrlp->result.aio_return;
if (fd >= 0)
aioClose(fd);
}
}
}
/* free data if requested to aioWrite() */
if (ctrlp->free_func)
ctrlp->free_func(ctrlp->bufp);
/* free temporary read buffer */
if (ctrlp->operation == _AIO_READ)
squidaio_xfree(ctrlp->bufp, ctrlp->len);
squidaio_ctrl_pool->free(ctrlp);
}
return retval;
}
/* Flush all pending I/O */
void
DiskThreadsIOStrategy::sync()
{
if (!initialised)
return; /* nothing to do then */
/* Flush all pending operations */
debug(32, 1) ("aioSync: flushing pending I/O operations\n");
do {
callback();
} while (squidaio_sync());
debug(32, 1) ("aioSync: done\n");
}
DiskThreadsIOStrategy::DiskThreadsIOStrategy() : initialised (false) {}
void
DiskThreadsIOStrategy::aioStats(StoreEntry * sentry)
{
storeAppendPrintf(sentry, "ASYNC IO Counters:\n");
storeAppendPrintf(sentry, "Operation\t# Requests\tNumber serviced\n");
storeAppendPrintf(sentry, "open\t%d\t%d\n", squidaio_counts.open_start, squidaio_counts.open_finish);
storeAppendPrintf(sentry, "close\t%d\t%d\n", squidaio_counts.close_start, squidaio_counts.close_finish);
storeAppendPrintf(sentry, "cancel\t%d\t-\n", squidaio_counts.cancel);
storeAppendPrintf(sentry, "write\t%d\t%d\n", squidaio_counts.write_start, squidaio_counts.write_finish);
storeAppendPrintf(sentry, "read\t%d\t%d\n", squidaio_counts.read_start, squidaio_counts.read_finish);
storeAppendPrintf(sentry, "stat\t%d\t%d\n", squidaio_counts.stat_start, squidaio_counts.stat_finish);
storeAppendPrintf(sentry, "unlink\t%d\t%d\n", squidaio_counts.unlink_start, squidaio_counts.unlink_finish);
storeAppendPrintf(sentry, "check_callback\t%d\t-\n", squidaio_counts.check_callback);
storeAppendPrintf(sentry, "queue\t%d\t-\n", squidaio_get_queue_len());
squidaio_stats(sentry);
}
DiskThreadsIOStrategy DiskThreadsIOStrategy::Instance;
bool
DiskThreadsIOStrategy::shedLoad()
{
/*
* we should detect some 'too many files open' condition and return
* NULL here.
*/
#ifdef MAGIC2
if (aioQueueSize() > MAGIC2)
return true;
#endif
return false;
}
int
DiskThreadsIOStrategy::load()
{
int loadav;
int ql;
ql = aioQueueSize();
if (ql == 0)
loadav = 0;
loadav = ql * 1000 / MAGIC1;
debug(47, 9) ("DiskThreadsIOStrategy::load: load=%d\n", loadav);
return loadav;
}
DiskFile::Pointer
DiskThreadsIOStrategy::newFile (char const *path)
{
if (shedLoad()) {
return NULL;
}
return new DiskThreadsDiskFile (path, this);
}
void
DiskThreadsIOStrategy::unlinkFile(char const *path)
{
statCounter.syscalls.disk.unlinks++;
aioUnlink(path, NULL, NULL);
}