#include "fs/rock/RockRebuild.h"
#include "fs/rock/RockSwapDir.h"
#include "fs/rock/RockFile.h"
+#include "SquidTime.h"
CBDATA_NAMESPACED_CLASS_INIT(Rock, Rebuild);
debugs(47,5, HERE << sd->index << " filen " << filen << " at " <<
dbOffset << " <= " << dbSize);
- const int maxCount = dbEntryLimit;
- const int wantedCount = opt_foreground_rebuild ? maxCount : 50;
- const int stepCount = min(wantedCount, maxCount);
- for (int i = 0; i < stepCount && dbOffset < dbSize; ++i, ++filen) {
+ // Balance our desire to maximize the number of entries processed at once
+ // (and, hence, minimize overheads and total rebuild time) with a
+ // requirement to also process Coordinator events, disk I/Os, etc.
+ const int maxSpentMsec = 50; // keep small: most RAM I/Os are under 1ms
+ const timeval loopStart = current_time;
+
+ int loaded = 0;
+ while (loaded < dbEntryLimit && dbOffset < dbSize) {
doOneEntry();
dbOffset += dbEntrySize;
+ ++filen;
+ ++loaded;
if (counts.scancount % 1000 == 0)
- storeRebuildProgress(sd->index, maxCount, counts.scancount);
- }
+ storeRebuildProgress(sd->index, dbEntryLimit, counts.scancount);
+
+ if (opt_foreground_rebuild)
+ continue; // skip "few entries at a time" check below
+
+ getCurrentTime();
+ const double elapsedMsec = tvSubMsec(loopStart, current_time);
+ if (elapsedMsec > maxSpentMsec || elapsedMsec < 0) {
+ debugs(47, 5, HERE << "pausing after " << loaded << " entries in " <<
+ elapsedMsec << "ms; " << (elapsedMsec/loaded) << "ms per entry");
+ break;
+ }
+ }
checkpoint();
}