]>
Commit | Line | Data |
---|---|---|
254912f3 | 1 | /* |
ef57eb7b | 2 | * Copyright (C) 1996-2016 The Squid Software Foundation and contributors |
bbc27441 AJ |
3 | * |
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. | |
254912f3 AR |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 47 Store Directory Routines */ |
10 | ||
f7f3304a | 11 | #include "squid.h" |
f5591061 | 12 | #include "base/RunnersRegistry.h" |
254912f3 AR |
13 | #include "base/TextException.h" |
14 | #include "DiskIO/IORequestor.h" | |
15 | #include "DiskIO/IpcIo/IpcIoFile.h" | |
16 | #include "DiskIO/ReadRequest.h" | |
17 | #include "DiskIO/WriteRequest.h" | |
c4ad1349 | 18 | #include "fd.h" |
b3f7fd88 | 19 | #include "fs_io.h" |
582c2af2 | 20 | #include "globals.h" |
21d845b1 | 21 | #include "ipc/mem/Pages.h" |
254912f3 AR |
22 | #include "ipc/Messages.h" |
23 | #include "ipc/Port.h" | |
f5591061 | 24 | #include "ipc/Queue.h" |
9a51593d | 25 | #include "ipc/StrandSearch.h" |
254912f3 | 26 | #include "ipc/UdsOp.h" |
7015a149 | 27 | #include "SBuf.h" |
4d5904f7 | 28 | #include "SquidConfig.h" |
21d845b1 FC |
29 | #include "SquidTime.h" |
30 | #include "StatCounters.h" | |
5bed43d6 | 31 | #include "tools.h" |
254912f3 | 32 | |
1a30fdf5 AJ |
33 | #include <cerrno> |
34 | ||
254912f3 AR |
35 | CBDATA_CLASS_INIT(IpcIoFile); |
36 | ||
f5591061 DK |
37 | /// shared memory segment path to use for IpcIoFile maps |
38 | static const char *const ShmLabel = "io_file"; | |
ea2cdeb6 DK |
39 | /// a single worker-to-disker or disker-to-worker queue capacity; up |
40 | /// to 2*QueueCapacity I/O requests queued between a single worker and | |
41 | /// a single disker | |
42 | // TODO: make configurable or compute from squid.conf settings if possible | |
43 | static const int QueueCapacity = 1024; | |
f5591061 | 44 | |
fa61cefe | 45 | const double IpcIoFile::Timeout = 7; // seconds; XXX: ALL,9 may require more |
b2aa0934 DK |
46 | IpcIoFile::IpcIoFileList IpcIoFile::WaitingForOpen; |
47 | IpcIoFile::IpcIoFilesMap IpcIoFile::IpcIoFiles; | |
829030b5 | 48 | std::unique_ptr<IpcIoFile::Queue> IpcIoFile::queue; |
254912f3 | 49 | |
c792f7fc AR |
50 | bool IpcIoFile::DiskerHandleMoreRequestsScheduled = false; |
51 | ||
7015a149 AR |
52 | static bool DiskerOpen(const SBuf &path, int flags, mode_t mode); |
53 | static void DiskerClose(const SBuf &path); | |
254912f3 | 54 | |
fa61cefe AR |
55 | /// IpcIo wrapper for debugs() streams; XXX: find a better class name |
56 | struct SipcIo { | |
57 | SipcIo(int aWorker, const IpcIoMsg &aMsg, int aDisker): | |
f53969cc | 58 | worker(aWorker), msg(aMsg), disker(aDisker) {} |
fa61cefe AR |
59 | |
60 | int worker; | |
61 | const IpcIoMsg &msg; | |
62 | int disker; | |
63 | }; | |
64 | ||
65 | std::ostream & | |
66 | operator <<(std::ostream &os, const SipcIo &sio) | |
67 | { | |
68 | return os << "ipcIo" << sio.worker << '.' << sio.msg.requestId << | |
9199139f | 69 | (sio.msg.command == IpcIo::cmdRead ? 'r' : 'w') << sio.disker; |
fa61cefe AR |
70 | } |
71 | ||
254912f3 | 72 | IpcIoFile::IpcIoFile(char const *aDb): |
f53969cc SM |
73 | dbName(aDb), diskId(-1), error_(false), lastRequestId(0), |
74 | olderRequests(&requestMap1), newerRequests(&requestMap2), | |
75 | timeoutCheckScheduled(false) | |
254912f3 AR |
76 | { |
77 | } | |
78 | ||
79 | IpcIoFile::~IpcIoFile() | |
80 | { | |
b2aa0934 DK |
81 | if (diskId >= 0) { |
82 | const IpcIoFilesMap::iterator i = IpcIoFiles.find(diskId); | |
83 | // XXX: warn and continue? | |
84 | Must(i != IpcIoFiles.end()); | |
85 | Must(i->second == this); | |
86 | IpcIoFiles.erase(i); | |
87 | } | |
254912f3 AR |
88 | } |
89 | ||
43ebbac3 AR |
90 | void |
91 | IpcIoFile::configure(const Config &cfg) | |
92 | { | |
93 | DiskFile::configure(cfg); | |
94 | config = cfg; | |
95 | } | |
96 | ||
254912f3 AR |
97 | void |
98 | IpcIoFile::open(int flags, mode_t mode, RefCount<IORequestor> callback) | |
99 | { | |
100 | ioRequestor = callback; | |
101 | Must(diskId < 0); // we do not know our disker yet | |
f5591061 DK |
102 | |
103 | if (!queue.get()) | |
104 | queue.reset(new Queue(ShmLabel, IamWorkerProcess() ? Queue::groupA : Queue::groupB, KidIdentifier)); | |
254912f3 AR |
105 | |
106 | if (IamDiskProcess()) { | |
7015a149 | 107 | error_ = !DiskerOpen(SBuf(dbName.termedBuf()), flags, mode); |
f7091279 DK |
108 | if (error_) |
109 | return; | |
110 | ||
b2aa0934 DK |
111 | diskId = KidIdentifier; |
112 | const bool inserted = | |
113 | IpcIoFiles.insert(std::make_pair(diskId, this)).second; | |
114 | Must(inserted); | |
9a51593d | 115 | |
6c6a656a | 116 | queue->localRateLimit().store(config.ioRate); |
df881a0f | 117 | |
f7091279 DK |
118 | Ipc::HereIamMessage ann(Ipc::StrandCoord(KidIdentifier, getpid())); |
119 | ann.strand.tag = dbName; | |
120 | Ipc::TypedMsgHdr message; | |
121 | ann.pack(message); | |
1ee292b7 | 122 | SendMessage(Ipc::Port::CoordinatorAddr(), message); |
f7091279 | 123 | |
e528e570 | 124 | ioRequestor->ioCompletedNotification(); |
254912f3 | 125 | return; |
9a51593d | 126 | } |
254912f3 | 127 | |
9a51593d DK |
128 | Ipc::StrandSearchRequest request; |
129 | request.requestorId = KidIdentifier; | |
130 | request.tag = dbName; | |
254912f3 | 131 | |
9a51593d DK |
132 | Ipc::TypedMsgHdr msg; |
133 | request.pack(msg); | |
1ee292b7 | 134 | Ipc::SendMessage(Ipc::Port::CoordinatorAddr(), msg); |
9a51593d | 135 | |
b2aa0934 DK |
136 | WaitingForOpen.push_back(this); |
137 | ||
138 | eventAdd("IpcIoFile::OpenTimeout", &IpcIoFile::OpenTimeout, | |
139 | this, Timeout, 0, false); // "this" pointer is used as id | |
254912f3 AR |
140 | } |
141 | ||
142 | void | |
9199139f AR |
143 | IpcIoFile::openCompleted(const Ipc::StrandSearchResponse *const response) |
144 | { | |
9a51593d | 145 | Must(diskId < 0); // we do not know our disker yet |
9a51593d DK |
146 | |
147 | if (!response) { | |
82ed74dc AR |
148 | debugs(79, DBG_IMPORTANT, "ERROR: " << dbName << " communication " << |
149 | "channel establishment timeout"); | |
caca86d7 | 150 | error_ = true; |
9a51593d DK |
151 | } else { |
152 | diskId = response->strand.kidId; | |
153 | if (diskId >= 0) { | |
b2aa0934 DK |
154 | const bool inserted = |
155 | IpcIoFiles.insert(std::make_pair(diskId, this)).second; | |
156 | Must(inserted); | |
9a51593d | 157 | } else { |
254912f3 | 158 | error_ = true; |
82ed74dc AR |
159 | debugs(79, DBG_IMPORTANT, "ERROR: no disker claimed " << |
160 | "responsibility for " << dbName); | |
9a51593d DK |
161 | } |
162 | } | |
254912f3 AR |
163 | |
164 | ioRequestor->ioCompletedNotification(); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Alias for IpcIoFile::open(...) | |
169 | \copydoc IpcIoFile::open(int flags, mode_t mode, RefCount<IORequestor> callback) | |
170 | */ | |
171 | void | |
172 | IpcIoFile::create(int flags, mode_t mode, RefCount<IORequestor> callback) | |
173 | { | |
174 | assert(false); // check | |
175 | /* We use the same logic path for open */ | |
176 | open(flags, mode, callback); | |
177 | } | |
178 | ||
179 | void | |
180 | IpcIoFile::close() | |
181 | { | |
182 | assert(ioRequestor != NULL); | |
183 | ||
184 | if (IamDiskProcess()) | |
7015a149 | 185 | DiskerClose(SBuf(dbName.termedBuf())); |
9a51593d | 186 | // XXX: else nothing to do? |
254912f3 AR |
187 | |
188 | ioRequestor->closeCompleted(); | |
189 | } | |
190 | ||
191 | bool | |
192 | IpcIoFile::canRead() const | |
193 | { | |
7e2d5ef6 | 194 | return diskId >= 0 && !error_ && canWait(); |
254912f3 AR |
195 | } |
196 | ||
197 | bool | |
198 | IpcIoFile::canWrite() const | |
199 | { | |
7e2d5ef6 | 200 | return diskId >= 0 && !error_ && canWait(); |
254912f3 AR |
201 | } |
202 | ||
203 | bool | |
204 | IpcIoFile::error() const | |
205 | { | |
206 | return error_; | |
207 | } | |
208 | ||
209 | void | |
210 | IpcIoFile::read(ReadRequest *readRequest) | |
211 | { | |
212 | debugs(79,3, HERE << "(disker" << diskId << ", " << readRequest->len << ", " << | |
9199139f | 213 | readRequest->offset << ")"); |
254912f3 AR |
214 | |
215 | assert(ioRequestor != NULL); | |
254912f3 AR |
216 | assert(readRequest->offset >= 0); |
217 | Must(!error_); | |
218 | ||
219 | //assert(minOffset < 0 || minOffset <= readRequest->offset); | |
220 | //assert(maxOffset < 0 || readRequest->offset + readRequest->len <= (uint64_t)maxOffset); | |
221 | ||
9a51593d | 222 | IpcIoPendingRequest *const pending = new IpcIoPendingRequest(this); |
254912f3 | 223 | pending->readRequest = readRequest; |
7a907247 | 224 | push(pending); |
254912f3 AR |
225 | } |
226 | ||
227 | void | |
228 | IpcIoFile::readCompleted(ReadRequest *readRequest, | |
8ed94021 | 229 | IpcIoMsg *const response) |
254912f3 | 230 | { |
caca86d7 | 231 | bool ioError = false; |
9a51593d | 232 | if (!response) { |
0ef0509e | 233 | debugs(79, 3, HERE << "error: timeout"); |
caca86d7 | 234 | ioError = true; // I/O timeout does not warrant setting error_? |
9a51593d | 235 | } else { |
70e3f706 | 236 | if (response->xerrno) { |
82ed74dc AR |
237 | debugs(79, DBG_IMPORTANT, "ERROR: " << dbName << " read: " << |
238 | xstrerr(response->xerrno)); | |
70e3f706 | 239 | ioError = error_ = true; |
9199139f | 240 | } else if (!response->page) { |
82ed74dc AR |
241 | debugs(79, DBG_IMPORTANT, "ERROR: " << dbName << " read ran " << |
242 | "out of shared memory pages"); | |
70e3f706 DK |
243 | ioError = true; |
244 | } else { | |
245 | const char *const buf = Ipc::Mem::PagePointer(response->page); | |
246 | memcpy(readRequest->buf, buf, response->len); | |
247 | } | |
254912f3 | 248 | |
70e3f706 DK |
249 | Ipc::Mem::PutPage(response->page); |
250 | } | |
8ed94021 | 251 | |
caca86d7 | 252 | const ssize_t rlen = ioError ? -1 : (ssize_t)readRequest->len; |
9199139f | 253 | const int errflag = ioError ? DISK_ERROR :DISK_OK; |
20b0e1fe | 254 | ioRequestor->readCompleted(readRequest->buf, rlen, errflag, readRequest); |
254912f3 AR |
255 | } |
256 | ||
257 | void | |
258 | IpcIoFile::write(WriteRequest *writeRequest) | |
259 | { | |
260 | debugs(79,3, HERE << "(disker" << diskId << ", " << writeRequest->len << ", " << | |
9199139f | 261 | writeRequest->offset << ")"); |
254912f3 AR |
262 | |
263 | assert(ioRequestor != NULL); | |
254912f3 AR |
264 | assert(writeRequest->len > 0); // TODO: work around mmap failures on zero-len? |
265 | assert(writeRequest->offset >= 0); | |
266 | Must(!error_); | |
267 | ||
268 | //assert(minOffset < 0 || minOffset <= writeRequest->offset); | |
269 | //assert(maxOffset < 0 || writeRequest->offset + writeRequest->len <= (uint64_t)maxOffset); | |
270 | ||
9a51593d | 271 | IpcIoPendingRequest *const pending = new IpcIoPendingRequest(this); |
254912f3 | 272 | pending->writeRequest = writeRequest; |
7a907247 | 273 | push(pending); |
254912f3 AR |
274 | } |
275 | ||
276 | void | |
277 | IpcIoFile::writeCompleted(WriteRequest *writeRequest, | |
9a51593d | 278 | const IpcIoMsg *const response) |
254912f3 | 279 | { |
caca86d7 | 280 | bool ioError = false; |
9a51593d | 281 | if (!response) { |
4e4f7fd9 | 282 | debugs(79, 3, "disker " << diskId << " timeout"); |
caca86d7 | 283 | ioError = true; // I/O timeout does not warrant setting error_? |
9199139f | 284 | } else if (response->xerrno) { |
4e4f7fd9 AR |
285 | debugs(79, DBG_IMPORTANT, "ERROR: disker " << diskId << |
286 | " error writing " << writeRequest->len << " bytes at " << | |
287 | writeRequest->offset << ": " << xstrerr(response->xerrno) << | |
288 | "; this worker will stop using " << dbName); | |
9a51593d | 289 | ioError = error_ = true; |
9199139f | 290 | } else if (response->len != writeRequest->len) { |
4e4f7fd9 AR |
291 | debugs(79, DBG_IMPORTANT, "ERROR: disker " << diskId << " wrote " << |
292 | response->len << " instead of " << writeRequest->len << | |
293 | " bytes (offset " << writeRequest->offset << "); " << | |
294 | "this worker will stop using " << dbName); | |
254912f3 AR |
295 | error_ = true; |
296 | } | |
297 | ||
298 | if (writeRequest->free_func) | |
299 | (writeRequest->free_func)(const_cast<char*>(writeRequest->buf)); // broken API? | |
300 | ||
caca86d7 | 301 | if (!ioError) { |
254912f3 | 302 | debugs(79,5, HERE << "wrote " << writeRequest->len << " to disker" << |
9199139f AR |
303 | diskId << " at " << writeRequest->offset); |
304 | } | |
254912f3 | 305 | |
caca86d7 | 306 | const ssize_t rlen = ioError ? 0 : (ssize_t)writeRequest->len; |
9199139f | 307 | const int errflag = ioError ? DISK_ERROR :DISK_OK; |
254912f3 AR |
308 | ioRequestor->writeCompleted(errflag, rlen, writeRequest); |
309 | } | |
310 | ||
311 | bool | |
312 | IpcIoFile::ioInProgress() const | |
313 | { | |
9a51593d | 314 | return !olderRequests->empty() || !newerRequests->empty(); |
254912f3 AR |
315 | } |
316 | ||
9a51593d | 317 | /// track a new pending request |
254912f3 | 318 | void |
28bd45ba | 319 | IpcIoFile::trackPendingRequest(const unsigned int id, IpcIoPendingRequest *const pending) |
254912f3 | 320 | { |
28bd45ba AR |
321 | const std::pair<RequestMap::iterator,bool> result = |
322 | newerRequests->insert(std::make_pair(id, pending)); | |
323 | Must(result.second); // failures means that id was not unique | |
9a51593d DK |
324 | if (!timeoutCheckScheduled) |
325 | scheduleTimeoutCheck(); | |
326 | } | |
254912f3 | 327 | |
9a51593d DK |
328 | /// push an I/O request to disker |
329 | void | |
7a907247 | 330 | IpcIoFile::push(IpcIoPendingRequest *const pending) |
9a51593d | 331 | { |
fa61cefe | 332 | // prevent queue overflows: check for responses to earlier requests |
28bd45ba | 333 | // warning: this call may result in indirect push() recursion |
f5591061 | 334 | HandleResponses("before push"); |
fa61cefe | 335 | |
a1c98830 | 336 | debugs(47, 7, HERE); |
9a51593d | 337 | Must(diskId >= 0); |
7a907247 DK |
338 | Must(pending); |
339 | Must(pending->readRequest || pending->writeRequest); | |
9a51593d DK |
340 | |
341 | IpcIoMsg ipcIo; | |
8ed94021 | 342 | try { |
28bd45ba AR |
343 | if (++lastRequestId == 0) // don't use zero value as requestId |
344 | ++lastRequestId; | |
8ed94021 | 345 | ipcIo.requestId = lastRequestId; |
0a11e039 | 346 | ipcIo.start = current_time; |
8ed94021 DK |
347 | if (pending->readRequest) { |
348 | ipcIo.command = IpcIo::cmdRead; | |
349 | ipcIo.offset = pending->readRequest->offset; | |
350 | ipcIo.len = pending->readRequest->len; | |
351 | } else { // pending->writeRequest | |
352 | Must(pending->writeRequest->len <= Ipc::Mem::PageSize()); | |
551f8a18 | 353 | if (!Ipc::Mem::GetPage(Ipc::Mem::PageId::ioPage, ipcIo.page)) { |
8ed94021 | 354 | ipcIo.len = 0; |
551f8a18 | 355 | throw TexcHere("run out of shared memory pages for IPC I/O"); |
8ed94021 DK |
356 | } |
357 | ipcIo.command = IpcIo::cmdWrite; | |
358 | ipcIo.offset = pending->writeRequest->offset; | |
359 | ipcIo.len = pending->writeRequest->len; | |
360 | char *const buf = Ipc::Mem::PagePointer(ipcIo.page); | |
361 | memcpy(buf, pending->writeRequest->buf, ipcIo.len); // optimize away | |
362 | } | |
254912f3 | 363 | |
f5591061 | 364 | debugs(47, 7, HERE << "pushing " << SipcIo(KidIdentifier, ipcIo, diskId)); |
fa61cefe | 365 | |
f5591061 | 366 | if (queue->push(diskId, ipcIo)) |
fa61cefe | 367 | Notify(diskId); // must notify disker |
28bd45ba | 368 | trackPendingRequest(ipcIo.requestId, pending); |
f5591061 | 369 | } catch (const Queue::Full &) { |
82ed74dc AR |
370 | debugs(47, DBG_IMPORTANT, "ERROR: worker I/O push queue for " << |
371 | dbName << " overflow: " << | |
fa61cefe AR |
372 | SipcIo(KidIdentifier, ipcIo, diskId)); // TODO: report queue len |
373 | // TODO: grow queue size | |
9199139f | 374 | |
0ef0509e | 375 | pending->completeIo(NULL); |
8ed94021 DK |
376 | delete pending; |
377 | } catch (const TextException &e) { | |
82ed74dc | 378 | debugs(47, DBG_IMPORTANT, "ERROR: " << dbName << " exception: " << e.what()); |
0ef0509e | 379 | pending->completeIo(NULL); |
7a907247 | 380 | delete pending; |
9a51593d | 381 | } |
254912f3 AR |
382 | } |
383 | ||
0a11e039 AR |
384 | /// whether we think there is enough time to complete the I/O |
385 | bool | |
9199139f AR |
386 | IpcIoFile::canWait() const |
387 | { | |
43ebbac3 | 388 | if (!config.ioTimeout) |
0a11e039 AR |
389 | return true; // no timeout specified |
390 | ||
391 | IpcIoMsg oldestIo; | |
5aac671b | 392 | if (!queue->findOldest(diskId, oldestIo) || oldestIo.start.tv_sec <= 0) |
0a11e039 AR |
393 | return true; // we cannot estimate expected wait time; assume it is OK |
394 | ||
55939a01 AR |
395 | const int oldestWait = tvSubMsec(oldestIo.start, current_time); |
396 | ||
397 | int rateWait = -1; // time in millisecons | |
6c6a656a | 398 | const int ioRate = queue->rateLimit(diskId).load(); |
55939a01 AR |
399 | if (ioRate > 0) { |
400 | // if there are N requests pending, the new one will wait at | |
401 | // least N/max-swap-rate seconds | |
75017bc9 | 402 | rateWait = static_cast<int>(1e3 * queue->outSize(diskId) / ioRate); |
55939a01 AR |
403 | // adjust N/max-swap-rate value based on the queue "balance" |
404 | // member, in case we have been borrowing time against future | |
405 | // I/O already | |
406 | rateWait += queue->balance(diskId); | |
407 | } | |
408 | ||
409 | const int expectedWait = max(oldestWait, rateWait); | |
0a11e039 | 410 | if (expectedWait < 0 || |
43ebbac3 | 411 | static_cast<time_msec_t>(expectedWait) < config.ioTimeout) |
0a11e039 AR |
412 | return true; // expected wait time is acceptible |
413 | ||
c792f7fc AR |
414 | debugs(47,2, HERE << "cannot wait: " << expectedWait << |
415 | " oldest: " << SipcIo(KidIdentifier, oldestIo, diskId)); | |
0a11e039 AR |
416 | return false; // do not want to wait that long |
417 | } | |
418 | ||
9a51593d | 419 | /// called when coordinator responds to worker open request |
254912f3 | 420 | void |
9a51593d | 421 | IpcIoFile::HandleOpenResponse(const Ipc::StrandSearchResponse &response) |
254912f3 | 422 | { |
9a51593d | 423 | debugs(47, 7, HERE << "coordinator response to open request"); |
b2aa0934 | 424 | for (IpcIoFileList::iterator i = WaitingForOpen.begin(); |
9199139f | 425 | i != WaitingForOpen.end(); ++i) { |
b2aa0934 DK |
426 | if (response.strand.tag == (*i)->dbName) { |
427 | (*i)->openCompleted(&response); | |
428 | WaitingForOpen.erase(i); | |
429 | return; | |
430 | } | |
431 | } | |
432 | ||
433 | debugs(47, 4, HERE << "LATE disker response to open for " << | |
434 | response.strand.tag); | |
435 | // nothing we can do about it; completeIo() has been called already | |
9a51593d | 436 | } |
254912f3 | 437 | |
9a51593d | 438 | void |
f5591061 | 439 | IpcIoFile::HandleResponses(const char *const when) |
fa61cefe AR |
440 | { |
441 | debugs(47, 4, HERE << "popping all " << when); | |
fa61cefe AR |
442 | IpcIoMsg ipcIo; |
443 | // get all responses we can: since we are not pushing, this will stop | |
f5591061 DK |
444 | int diskId; |
445 | while (queue->pop(diskId, ipcIo)) { | |
446 | const IpcIoFilesMap::const_iterator i = IpcIoFiles.find(diskId); | |
447 | Must(i != IpcIoFiles.end()); // TODO: warn but continue | |
448 | i->second->handleResponse(ipcIo); | |
449 | } | |
9a51593d | 450 | } |
254912f3 | 451 | |
9a51593d | 452 | void |
8ed94021 | 453 | IpcIoFile::handleResponse(IpcIoMsg &ipcIo) |
9a51593d DK |
454 | { |
455 | const int requestId = ipcIo.requestId; | |
fa61cefe | 456 | debugs(47, 7, HERE << "popped disker response: " << |
9199139f | 457 | SipcIo(KidIdentifier, ipcIo, diskId)); |
fa61cefe | 458 | |
9a51593d DK |
459 | Must(requestId); |
460 | if (IpcIoPendingRequest *const pending = dequeueRequest(requestId)) { | |
461 | pending->completeIo(&ipcIo); | |
caca86d7 AR |
462 | delete pending; // XXX: leaking if throwing |
463 | } else { | |
9a51593d DK |
464 | debugs(47, 4, HERE << "LATE disker response to " << ipcIo.command << |
465 | "; ipcIo" << KidIdentifier << '.' << requestId); | |
caca86d7 AR |
466 | // nothing we can do about it; completeIo() has been called already |
467 | } | |
254912f3 AR |
468 | } |
469 | ||
254912f3 | 470 | void |
9a51593d | 471 | IpcIoFile::Notify(const int peerId) |
254912f3 | 472 | { |
fa61cefe | 473 | // TODO: Count and report the total number of notifications, pops, pushes. |
a1c98830 | 474 | debugs(47, 7, HERE << "kid" << peerId); |
9a51593d DK |
475 | Ipc::TypedMsgHdr msg; |
476 | msg.setType(Ipc::mtIpcIoNotification); // TODO: add proper message type? | |
477 | msg.putInt(KidIdentifier); | |
1ee292b7 | 478 | const String addr = Ipc::Port::MakeAddr(Ipc::strandAddrLabel, peerId); |
9a51593d DK |
479 | Ipc::SendMessage(addr, msg); |
480 | } | |
481 | ||
482 | void | |
483 | IpcIoFile::HandleNotification(const Ipc::TypedMsgHdr &msg) | |
484 | { | |
fa61cefe AR |
485 | const int from = msg.getInt(); |
486 | debugs(47, 7, HERE << "from " << from); | |
f5591061 DK |
487 | queue->clearReaderSignal(from); |
488 | if (IamDiskProcess()) | |
489 | DiskerHandleRequests(); | |
490 | else | |
491 | HandleResponses("after notification"); | |
9a51593d DK |
492 | } |
493 | ||
b2aa0934 DK |
494 | /// handles open request timeout |
495 | void | |
496 | IpcIoFile::OpenTimeout(void *const param) | |
497 | { | |
498 | Must(param); | |
499 | // the pointer is used for comparison only and not dereferenced | |
500 | const IpcIoFile *const ipcIoFile = | |
501 | reinterpret_cast<const IpcIoFile *>(param); | |
502 | for (IpcIoFileList::iterator i = WaitingForOpen.begin(); | |
9199139f | 503 | i != WaitingForOpen.end(); ++i) { |
b2aa0934 DK |
504 | if (*i == ipcIoFile) { |
505 | (*i)->openCompleted(NULL); | |
506 | WaitingForOpen.erase(i); | |
507 | break; | |
508 | } | |
509 | } | |
510 | } | |
511 | ||
9a51593d DK |
512 | /// IpcIoFile::checkTimeouts wrapper |
513 | void | |
514 | IpcIoFile::CheckTimeouts(void *const param) | |
515 | { | |
9a51593d | 516 | Must(param); |
b2aa0934 DK |
517 | const int diskId = reinterpret_cast<uintptr_t>(param); |
518 | debugs(47, 7, HERE << "diskId=" << diskId); | |
519 | const IpcIoFilesMap::const_iterator i = IpcIoFiles.find(diskId); | |
520 | if (i != IpcIoFiles.end()) | |
521 | i->second->checkTimeouts(); | |
9a51593d DK |
522 | } |
523 | ||
524 | void | |
525 | IpcIoFile::checkTimeouts() | |
526 | { | |
9a51593d | 527 | timeoutCheckScheduled = false; |
254912f3 | 528 | |
0ef0509e AR |
529 | // last chance to recover in case a notification message was lost, etc. |
530 | const RequestMap::size_type timeoutsBefore = olderRequests->size(); | |
531 | HandleResponses("before timeout"); | |
532 | const RequestMap::size_type timeoutsNow = olderRequests->size(); | |
533 | ||
534 | if (timeoutsBefore > timeoutsNow) { // some requests were rescued | |
535 | // notification message lost or significantly delayed? | |
82ed74dc AR |
536 | debugs(47, DBG_IMPORTANT, "WARNING: communication with " << dbName << |
537 | " may be too slow or disrupted for about " << | |
0ef0509e AR |
538 | Timeout << "s; rescued " << (timeoutsBefore - timeoutsNow) << |
539 | " out of " << timeoutsBefore << " I/Os"); | |
540 | } | |
541 | ||
542 | if (timeoutsNow) { | |
543 | debugs(47, DBG_IMPORTANT, "WARNING: abandoning " << | |
82ed74dc | 544 | timeoutsNow << ' ' << dbName << " I/Os after at least " << |
0ef0509e AR |
545 | Timeout << "s timeout"); |
546 | } | |
547 | ||
caca86d7 AR |
548 | // any old request would have timed out by now |
549 | typedef RequestMap::const_iterator RMCI; | |
9a51593d DK |
550 | for (RMCI i = olderRequests->begin(); i != olderRequests->end(); ++i) { |
551 | IpcIoPendingRequest *const pending = i->second; | |
254912f3 | 552 | |
b2aa0934 | 553 | const unsigned int requestId = i->first; |
caca86d7 | 554 | debugs(47, 7, HERE << "disker timeout; ipcIo" << |
b2aa0934 | 555 | KidIdentifier << '.' << requestId); |
254912f3 | 556 | |
caca86d7 AR |
557 | pending->completeIo(NULL); // no response |
558 | delete pending; // XXX: leaking if throwing | |
9a51593d DK |
559 | } |
560 | olderRequests->clear(); | |
caca86d7 | 561 | |
9a51593d | 562 | swap(olderRequests, newerRequests); // switches pointers around |
0ef0509e | 563 | if (!olderRequests->empty() && !timeoutCheckScheduled) |
9a51593d | 564 | scheduleTimeoutCheck(); |
254912f3 AR |
565 | } |
566 | ||
caca86d7 | 567 | /// prepare to check for timeouts in a little while |
254912f3 | 568 | void |
9a51593d | 569 | IpcIoFile::scheduleTimeoutCheck() |
254912f3 | 570 | { |
b2aa0934 | 571 | // we check all older requests at once so some may be wait for 2*Timeout |
caca86d7 | 572 | eventAdd("IpcIoFile::CheckTimeouts", &IpcIoFile::CheckTimeouts, |
b2aa0934 | 573 | reinterpret_cast<void *>(diskId), Timeout, 0, false); |
9a51593d | 574 | timeoutCheckScheduled = true; |
254912f3 AR |
575 | } |
576 | ||
577 | /// returns and forgets the right IpcIoFile pending request | |
578 | IpcIoPendingRequest * | |
9a51593d | 579 | IpcIoFile::dequeueRequest(const unsigned int requestId) |
254912f3 | 580 | { |
254912f3 | 581 | Must(requestId != 0); |
caca86d7 | 582 | |
9a51593d DK |
583 | RequestMap *map = NULL; |
584 | RequestMap::iterator i = requestMap1.find(requestId); | |
caca86d7 | 585 | |
9a51593d DK |
586 | if (i != requestMap1.end()) |
587 | map = &requestMap1; | |
caca86d7 | 588 | else { |
9a51593d DK |
589 | i = requestMap2.find(requestId); |
590 | if (i != requestMap2.end()) | |
591 | map = &requestMap2; | |
caca86d7 AR |
592 | } |
593 | ||
594 | if (!map) // not found in both maps | |
595 | return NULL; | |
596 | ||
597 | IpcIoPendingRequest *pending = i->second; | |
598 | map->erase(i); | |
599 | return pending; | |
254912f3 AR |
600 | } |
601 | ||
602 | int | |
9a51593d | 603 | IpcIoFile::getFD() const |
254912f3 AR |
604 | { |
605 | assert(false); // not supported; TODO: remove this method from API | |
606 | return -1; | |
607 | } | |
608 | ||
9a51593d | 609 | /* IpcIoMsg */ |
254912f3 | 610 | |
9a51593d | 611 | IpcIoMsg::IpcIoMsg(): |
f53969cc SM |
612 | requestId(0), |
613 | offset(0), | |
614 | len(0), | |
615 | command(IpcIo::cmdNone), | |
616 | xerrno(0) | |
254912f3 | 617 | { |
0a11e039 | 618 | start.tv_sec = 0; |
e19994df | 619 | start.tv_usec = 0; |
254912f3 AR |
620 | } |
621 | ||
caca86d7 | 622 | /* IpcIoPendingRequest */ |
254912f3 AR |
623 | |
624 | IpcIoPendingRequest::IpcIoPendingRequest(const IpcIoFile::Pointer &aFile): | |
f53969cc | 625 | file(aFile), readRequest(NULL), writeRequest(NULL) |
254912f3 AR |
626 | { |
627 | } | |
628 | ||
caca86d7 | 629 | void |
8ed94021 | 630 | IpcIoPendingRequest::completeIo(IpcIoMsg *const response) |
caca86d7 | 631 | { |
caca86d7 AR |
632 | if (readRequest) |
633 | file->readCompleted(readRequest, response); | |
9199139f | 634 | else if (writeRequest) |
caca86d7 | 635 | file->writeCompleted(writeRequest, response); |
9a51593d DK |
636 | else { |
637 | Must(!response); // only timeouts are handled here | |
638 | file->openCompleted(NULL); | |
639 | } | |
caca86d7 AR |
640 | } |
641 | ||
254912f3 AR |
642 | /* XXX: disker code that should probably be moved elsewhere */ |
643 | ||
7015a149 | 644 | static SBuf DbName; ///< full db file name |
254912f3 AR |
645 | static int TheFile = -1; ///< db file descriptor |
646 | ||
9a51593d DK |
647 | static void |
648 | diskerRead(IpcIoMsg &ipcIo) | |
254912f3 | 649 | { |
551f8a18 | 650 | if (!Ipc::Mem::GetPage(Ipc::Mem::PageId::ioPage, ipcIo.page)) { |
8ed94021 | 651 | ipcIo.len = 0; |
c792f7fc | 652 | debugs(47,2, HERE << "run out of shared memory pages for IPC I/O"); |
8ed94021 DK |
653 | return; |
654 | } | |
655 | ||
656 | char *const buf = Ipc::Mem::PagePointer(ipcIo.page); | |
657 | const ssize_t read = pread(TheFile, buf, min(ipcIo.len, Ipc::Mem::PageSize()), ipcIo.offset); | |
e4f1fdae | 658 | ++statCounter.syscalls.disk.reads; |
2d338731 AR |
659 | fd_bytes(TheFile, read, FD_READ); |
660 | ||
254912f3 | 661 | if (read >= 0) { |
9a51593d DK |
662 | ipcIo.xerrno = 0; |
663 | const size_t len = static_cast<size_t>(read); // safe because read > 0 | |
254912f3 | 664 | debugs(47,8, HERE << "disker" << KidIdentifier << " read " << |
9199139f | 665 | (len == ipcIo.len ? "all " : "just ") << read); |
9a51593d DK |
666 | ipcIo.len = len; |
667 | } else { | |
668 | ipcIo.xerrno = errno; | |
669 | ipcIo.len = 0; | |
254912f3 | 670 | debugs(47,5, HERE << "disker" << KidIdentifier << " read error: " << |
9199139f | 671 | ipcIo.xerrno); |
9a51593d | 672 | } |
254912f3 AR |
673 | } |
674 | ||
9d4e9cfb | 675 | /// Tries to write buffer to disk (a few times if needed); |
4e4f7fd9 | 676 | /// sets ipcIo results, but does no cleanup. The caller must cleanup. |
9a51593d | 677 | static void |
4e4f7fd9 AR |
678 | diskerWriteAttempts(IpcIoMsg &ipcIo) |
679 | { | |
680 | const char *buf = Ipc::Mem::PagePointer(ipcIo.page); | |
681 | size_t toWrite = min(ipcIo.len, Ipc::Mem::PageSize()); | |
682 | size_t wroteSoFar = 0; | |
683 | off_t offset = ipcIo.offset; | |
684 | // Partial writes to disk do happen. It is unlikely that the caller can | |
685 | // handle partial writes by doing something other than writing leftovers | |
686 | // again, so we try to write them ourselves to minimize overheads. | |
687 | const int attemptLimit = 10; | |
688 | for (int attempts = 1; attempts <= attemptLimit; ++attempts) { | |
689 | const ssize_t result = pwrite(TheFile, buf, toWrite, offset); | |
690 | ++statCounter.syscalls.disk.writes; | |
691 | fd_bytes(TheFile, result, FD_WRITE); | |
692 | ||
693 | if (result < 0) { | |
694 | ipcIo.xerrno = errno; | |
695 | assert(ipcIo.xerrno); | |
82ed74dc AR |
696 | debugs(47, DBG_IMPORTANT, "ERROR: " << DbName << " failure" << |
697 | " writing " << toWrite << '/' << ipcIo.len << | |
4e4f7fd9 AR |
698 | " at " << ipcIo.offset << '+' << wroteSoFar << |
699 | " on " << attempts << " try: " << xstrerr(ipcIo.xerrno)); | |
700 | ipcIo.len = wroteSoFar; | |
701 | return; // bail on error | |
702 | } | |
2d338731 | 703 | |
4e4f7fd9 | 704 | const size_t wroteNow = static_cast<size_t>(result); // result >= 0 |
9a51593d | 705 | ipcIo.xerrno = 0; |
4e4f7fd9 AR |
706 | |
707 | debugs(47,3, "disker" << KidIdentifier << " wrote " << | |
708 | (wroteNow >= toWrite ? "all " : "just ") << wroteNow << | |
709 | " out of " << toWrite << '/' << ipcIo.len << " at " << | |
710 | ipcIo.offset << '+' << wroteSoFar << " on " << attempts << | |
711 | " try"); | |
712 | ||
713 | wroteSoFar += wroteNow; | |
714 | ||
715 | if (wroteNow >= toWrite) { | |
716 | ipcIo.xerrno = 0; | |
717 | ipcIo.len = wroteSoFar; | |
718 | return; // wrote everything there was to write | |
719 | } | |
720 | ||
721 | buf += wroteNow; | |
722 | offset += wroteNow; | |
723 | toWrite -= wroteNow; | |
9a51593d | 724 | } |
8ed94021 | 725 | |
82ed74dc AR |
726 | debugs(47, DBG_IMPORTANT, "ERROR: " << DbName << " exhausted all " << |
727 | attemptLimit << " attempts while writing " << | |
4e4f7fd9 AR |
728 | toWrite << '/' << ipcIo.len << " at " << ipcIo.offset << '+' << |
729 | wroteSoFar); | |
730 | return; // not a fatal I/O error, unless the caller treats it as such | |
731 | } | |
732 | ||
733 | static void | |
734 | diskerWrite(IpcIoMsg &ipcIo) | |
735 | { | |
736 | diskerWriteAttempts(ipcIo); // may fail | |
8ed94021 | 737 | Ipc::Mem::PutPage(ipcIo.page); |
254912f3 AR |
738 | } |
739 | ||
c792f7fc | 740 | void |
df881a0f | 741 | IpcIoFile::DiskerHandleMoreRequests(void *source) |
c792f7fc | 742 | { |
df881a0f AR |
743 | debugs(47, 7, HERE << "resuming handling requests after " << |
744 | static_cast<const char *>(source)); | |
c792f7fc AR |
745 | DiskerHandleMoreRequestsScheduled = false; |
746 | IpcIoFile::DiskerHandleRequests(); | |
747 | } | |
748 | ||
df881a0f AR |
749 | bool |
750 | IpcIoFile::WaitBeforePop() | |
751 | { | |
6c6a656a | 752 | const int ioRate = queue->localRateLimit().load(); |
df881a0f AR |
753 | const double maxRate = ioRate/1e3; // req/ms |
754 | ||
755 | // do we need to enforce configured I/O rate? | |
756 | if (maxRate <= 0) | |
757 | return false; | |
758 | ||
759 | // is there an I/O request we could potentially delay? | |
1e614370 DK |
760 | int processId; |
761 | IpcIoMsg ipcIo; | |
762 | if (!queue->peek(processId, ipcIo)) { | |
763 | // unlike pop(), peek() is not reliable and does not block reader | |
df881a0f AR |
764 | // so we must proceed with pop() even if it is likely to fail |
765 | return false; | |
766 | } | |
767 | ||
9a585a30 | 768 | static timeval LastIo = current_time; |
df881a0f AR |
769 | |
770 | const double ioDuration = 1.0 / maxRate; // ideal distance between two I/Os | |
771 | // do not accumulate more than 100ms or 100 I/Os, whichever is smaller | |
772 | const int64_t maxImbalance = min(static_cast<int64_t>(100), static_cast<int64_t>(100 * ioDuration)); | |
773 | ||
774 | const double credit = ioDuration; // what the last I/O should have cost us | |
775 | const double debit = tvSubMsec(LastIo, current_time); // actual distance from the last I/O | |
776 | LastIo = current_time; | |
777 | ||
778 | Ipc::QueueReader::Balance &balance = queue->localBalance(); | |
779 | balance += static_cast<int64_t>(credit - debit); | |
780 | ||
781 | debugs(47, 7, HERE << "rate limiting balance: " << balance << " after +" << credit << " -" << debit); | |
782 | ||
1e614370 DK |
783 | if (ipcIo.command == IpcIo::cmdWrite && balance > maxImbalance) { |
784 | // if the next request is (likely) write and we accumulated | |
785 | // too much time for future slow I/Os, then shed accumulated | |
786 | // time to keep just half of the excess | |
df881a0f | 787 | const int64_t toSpend = balance - maxImbalance/2; |
69321ae9 AR |
788 | |
789 | if (toSpend/1e3 > Timeout) | |
82ed74dc AR |
790 | debugs(47, DBG_IMPORTANT, "WARNING: " << DbName << " delays " << |
791 | "I/O requests for " << (toSpend/1e3) << " seconds " << | |
792 | "to obey " << ioRate << "/sec rate limit"); | |
69321ae9 | 793 | |
df881a0f AR |
794 | debugs(47, 3, HERE << "rate limiting by " << toSpend << " ms to get" << |
795 | (1e3*maxRate) << "/sec rate"); | |
796 | eventAdd("IpcIoFile::DiskerHandleMoreRequests", | |
797 | &IpcIoFile::DiskerHandleMoreRequests, | |
798 | const_cast<char*>("rate limiting"), | |
799 | toSpend/1e3, 0, false); | |
800 | DiskerHandleMoreRequestsScheduled = true; | |
801 | return true; | |
e29ccb57 | 802 | } else if (balance < -maxImbalance) { |
df881a0f AR |
803 | // do not owe "too much" to avoid "too large" bursts of I/O |
804 | balance = -maxImbalance; | |
805 | } | |
806 | ||
807 | return false; | |
808 | } | |
809 | ||
254912f3 | 810 | void |
f5591061 | 811 | IpcIoFile::DiskerHandleRequests() |
254912f3 | 812 | { |
c792f7fc | 813 | // Balance our desire to maximize the number of concurrent I/O requests |
fdb3059b | 814 | // (reordred by OS to minimize seek time) with a requirement to |
c792f7fc | 815 | // send 1st-I/O notification messages, process Coordinator events, etc. |
fdb3059b AR |
816 | const int maxSpentMsec = 10; // keep small: most RAM I/Os are under 1ms |
817 | const timeval loopStart = current_time; | |
818 | ||
c792f7fc | 819 | int popped = 0; |
fa61cefe AR |
820 | int workerId = 0; |
821 | IpcIoMsg ipcIo; | |
df881a0f | 822 | while (!WaitBeforePop() && queue->pop(workerId, ipcIo)) { |
fdb3059b AR |
823 | ++popped; |
824 | ||
825 | // at least one I/O per call is guaranteed if the queue is not empty | |
fa61cefe AR |
826 | DiskerHandleRequest(workerId, ipcIo); |
827 | ||
fdb3059b AR |
828 | getCurrentTime(); |
829 | const double elapsedMsec = tvSubMsec(loopStart, current_time); | |
830 | if (elapsedMsec > maxSpentMsec || elapsedMsec < 0) { | |
831 | if (!DiskerHandleMoreRequestsScheduled) { | |
832 | // the gap must be positive for select(2) to be given a chance | |
833 | const double minBreakSecs = 0.001; | |
834 | eventAdd("IpcIoFile::DiskerHandleMoreRequests", | |
9199139f | 835 | &IpcIoFile::DiskerHandleMoreRequests, |
df881a0f AR |
836 | const_cast<char*>("long I/O loop"), |
837 | minBreakSecs, 0, false); | |
fdb3059b AR |
838 | DiskerHandleMoreRequestsScheduled = true; |
839 | } | |
840 | debugs(47, 3, HERE << "pausing after " << popped << " I/Os in " << | |
9199139f | 841 | elapsedMsec << "ms; " << (elapsedMsec/popped) << "ms per I/O"); |
fdb3059b AR |
842 | break; |
843 | } | |
c792f7fc AR |
844 | } |
845 | ||
fdb3059b | 846 | // TODO: consider using O_DIRECT with "elevator" optimization where we pop |
fa61cefe AR |
847 | // requests first, then reorder the popped requests to optimize seek time, |
848 | // then do I/O, then take a break, and come back for the next set of I/O | |
849 | // requests. | |
9a51593d | 850 | } |
254912f3 | 851 | |
9a51593d DK |
852 | /// called when disker receives an I/O request |
853 | void | |
854 | IpcIoFile::DiskerHandleRequest(const int workerId, IpcIoMsg &ipcIo) | |
855 | { | |
9a51593d | 856 | if (ipcIo.command != IpcIo::cmdRead && ipcIo.command != IpcIo::cmdWrite) { |
82ed74dc | 857 | debugs(0, DBG_CRITICAL, "ERROR: " << DbName << |
9a51593d DK |
858 | " should not receive " << ipcIo.command << |
859 | " ipcIo" << workerId << '.' << ipcIo.requestId); | |
860 | return; | |
861 | } | |
862 | ||
863 | debugs(47,5, HERE << "disker" << KidIdentifier << | |
864 | (ipcIo.command == IpcIo::cmdRead ? " reads " : " writes ") << | |
865 | ipcIo.len << " at " << ipcIo.offset << | |
866 | " ipcIo" << workerId << '.' << ipcIo.requestId); | |
867 | ||
868 | if (ipcIo.command == IpcIo::cmdRead) | |
869 | diskerRead(ipcIo); | |
870 | else // ipcIo.command == IpcIo::cmdWrite | |
871 | diskerWrite(ipcIo); | |
872 | ||
f5591061 | 873 | debugs(47, 7, HERE << "pushing " << SipcIo(workerId, ipcIo, KidIdentifier)); |
fa61cefe | 874 | |
7a907247 | 875 | try { |
f5591061 | 876 | if (queue->push(workerId, ipcIo)) |
5e44782e | 877 | Notify(workerId); // must notify worker |
f5591061 | 878 | } catch (const Queue::Full &) { |
fa61cefe AR |
879 | // The worker queue should not overflow because the worker should pop() |
880 | // before push()ing and because if disker pops N requests at a time, | |
881 | // we should make sure the worker pop() queue length is the worker | |
882 | // push queue length plus N+1. XXX: implement the N+1 difference. | |
82ed74dc AR |
883 | debugs(47, DBG_IMPORTANT, "BUG: Worker I/O pop queue for " << |
884 | DbName << " overflow: " << | |
5e44782e | 885 | SipcIo(workerId, ipcIo, KidIdentifier)); // TODO: report queue len |
fa61cefe AR |
886 | |
887 | // the I/O request we could not push will timeout | |
7a907247 | 888 | } |
254912f3 AR |
889 | } |
890 | ||
891 | static bool | |
ced8def3 | 892 | DiskerOpen(const SBuf &path, int flags, mode_t) |
254912f3 AR |
893 | { |
894 | assert(TheFile < 0); | |
895 | ||
82ed74dc | 896 | DbName = path; |
7015a149 | 897 | TheFile = file_open(DbName.c_str(), flags); |
254912f3 AR |
898 | |
899 | if (TheFile < 0) { | |
900 | const int xerrno = errno; | |
82ed74dc | 901 | debugs(47, DBG_CRITICAL, "ERROR: cannot open " << DbName << ": " << |
254912f3 AR |
902 | xstrerr(xerrno)); |
903 | return false; | |
9a51593d | 904 | } |
254912f3 | 905 | |
cb4185f1 | 906 | ++store_open_disk_fd; |
7015a149 | 907 | debugs(79,3, "rock db opened " << DbName << ": FD " << TheFile); |
9a51593d | 908 | return true; |
254912f3 AR |
909 | } |
910 | ||
911 | static void | |
7015a149 | 912 | DiskerClose(const SBuf &path) |
254912f3 AR |
913 | { |
914 | if (TheFile >= 0) { | |
915 | file_close(TheFile); | |
916 | debugs(79,3, HERE << "rock db closed " << path << ": FD " << TheFile); | |
917 | TheFile = -1; | |
5e263176 | 918 | --store_open_disk_fd; |
a1c98830 | 919 | } |
7015a149 | 920 | DbName.clear(); |
254912f3 | 921 | } |
f5591061 | 922 | |
ea2cdeb6 | 923 | /// reports our needs for shared memory pages to Ipc::Mem::Pages |
21b7990f AR |
924 | /// and initializes shared memory segments used by IpcIoFile |
925 | class IpcIoRr: public Ipc::Mem::RegisteredRunner | |
ea2cdeb6 DK |
926 | { |
927 | public: | |
928 | /* RegisteredRunner API */ | |
21b7990f AR |
929 | IpcIoRr(): owner(NULL) {} |
930 | virtual ~IpcIoRr(); | |
931 | virtual void claimMemoryNeeds(); | |
932 | ||
933 | protected: | |
934 | /* Ipc::Mem::RegisteredRunner API */ | |
935 | virtual void create(); | |
936 | ||
937 | private: | |
938 | Ipc::FewToFewBiQueue::Owner *owner; | |
ea2cdeb6 DK |
939 | }; |
940 | ||
21b7990f | 941 | RunnerRegistrationEntry(IpcIoRr); |
ea2cdeb6 | 942 | |
ea2cdeb6 | 943 | void |
21b7990f | 944 | IpcIoRr::claimMemoryNeeds() |
ea2cdeb6 DK |
945 | { |
946 | const int itemsCount = Ipc::FewToFewBiQueue::MaxItemsCount( | |
c11211d9 | 947 | ::Config.workers, ::Config.cacheSwap.n_strands, QueueCapacity); |
ea2cdeb6 DK |
948 | // the maximum number of shared I/O pages is approximately the |
949 | // number of queue slots, we add a fudge factor to that to account | |
950 | // for corner cases where I/O pages are created before queue | |
951 | // limits are checked or destroyed long after the I/O is dequeued | |
29a9ff4e DK |
952 | Ipc::Mem::NotePageNeed(Ipc::Mem::PageId::ioPage, |
953 | static_cast<int>(itemsCount * 1.1)); | |
ea2cdeb6 DK |
954 | } |
955 | ||
21b7990f AR |
956 | void |
957 | IpcIoRr::create() | |
f5591061 | 958 | { |
e9f68413 | 959 | if (Config.cacheSwap.n_strands <= 0) |
f5591061 DK |
960 | return; |
961 | ||
4404f1c5 | 962 | Must(!owner); |
4404f1c5 | 963 | owner = Ipc::FewToFewBiQueue::Init(ShmLabel, Config.workers, 1, |
3b581957 | 964 | Config.cacheSwap.n_strands, |
4404f1c5 | 965 | 1 + Config.workers, sizeof(IpcIoMsg), |
ea2cdeb6 | 966 | QueueCapacity); |
f5591061 DK |
967 | } |
968 | ||
969 | IpcIoRr::~IpcIoRr() | |
970 | { | |
971 | delete owner; | |
972 | } | |
f53969cc | 973 |