Skip to content

Commit 67e1349

Browse files
committed
chore: fixes - move yield points
1 parent c097924 commit 67e1349

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/server/rdb_save.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ size_t SerializerBase::SerializedLen() const {
910910
io::Bytes SerializerBase::PrepareFlush(SerializerBase::FlushState flush_state) {
911911
size_t sz = mem_buf_.InputLen();
912912
if (sz == 0)
913-
return mem_buf_.InputBuffer();
913+
return {};
914914

915915
bool is_last_chunk = flush_state == FlushState::kFlushEndEntry;
916916
VLOG(2) << "PrepareFlush:" << is_last_chunk << " " << number_of_chunks_;

src/server/snapshot.cc

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void SliceSnapshot::IterateBucketsFb(bool send_full_sync_cut) {
154154
stats_.keys_total += db_slice_->DbSize(db_indx);
155155
}
156156

157-
const uint64_t kCyclesPerYield = base::CycleClock::Frequency() >> 16; // ~15usec.
157+
const uint64_t kCyclesPerJiffy = base::CycleClock::Frequency() >> 16; // ~15usec.
158158

159159
for (DbIndex db_indx = 0; db_indx < db_array_.size(); ++db_indx) {
160160
if (!cntx_->IsRunning())
@@ -166,7 +166,6 @@ void SliceSnapshot::IterateBucketsFb(bool send_full_sync_cut) {
166166
PrimeTable* pt = &db_array_[db_indx]->prime;
167167
VLOG(1) << "Start traversing " << pt->size() << " items for index " << db_indx;
168168

169-
uint64_t start_cycles = base::CycleClock::Now();
170169
do {
171170
if (!cntx_->IsRunning()) {
172171
return;
@@ -175,13 +174,14 @@ void SliceSnapshot::IterateBucketsFb(bool send_full_sync_cut) {
175174
PrimeTable::Cursor next = pt->TraverseBuckets(
176175
cursor, [this, &db_indx](auto it) { return BucketSaveCb(db_indx, it); });
177176
cursor = next;
178-
uint64_t now = base::CycleClock::Now();
179-
if (now > start_cycles + kCyclesPerYield) {
180-
ThisFiber::Yield();
181-
start_cycles = base::CycleClock::Now();
182-
}
183177

184-
PushSerialized(false);
178+
// If we do not flush the data, and have not preempted,
179+
// we may need to yield to other fibers to avoid grabbind the CPU for too long.
180+
if (!PushSerialized(false)) {
181+
if (ThisFiber::GetRunningTimeCycles() > kCyclesPerJiffy) {
182+
ThisFiber::Yield();
183+
}
184+
}
185185
} while (cursor);
186186

187187
DVLOG(2) << "after loop " << ThisFiber::GetName();
@@ -309,7 +309,6 @@ void SliceSnapshot::SerializeEntry(DbIndex db_indx, const PrimeKey& pk, const Pr
309309

310310
size_t SliceSnapshot::FlushSerialized(SerializerBase::FlushState flush_state) {
311311
io::StringFile sfile;
312-
uint64_t start = base::CycleClock::Now();
313312
error_code ec = serializer_->FlushToSink(&sfile, flush_state);
314313
CHECK(!ec); // always succeeds
315314

@@ -320,16 +319,8 @@ size_t SliceSnapshot::FlushSerialized(SerializerBase::FlushState flush_state) {
320319
uint64_t id = rec_id_++;
321320
DVLOG(2) << "Pushing " << id;
322321

323-
// FlushToSink can be quite slow for large values or due compression.
324-
// In this case we sleep to avoid starvation of other fibers.
325-
// Please note that we allocate the record id before we preempt in order to preserve ordering
326-
// guarantees.
327-
const uint64_t kSleepThreshold = base::CycleClock::Frequency() >> 15; // ~30 usec
328-
uint64_t elapsed = base::CycleClock::Now() - start;
329-
if (elapsed > kSleepThreshold) {
330-
// Balance the load between fibers.
331-
ThisFiber::SleepFor(chrono::microseconds(200));
332-
}
322+
const uint64_t kLongRunThresholdCycles = base::CycleClock::Frequency() >> 15; // ~30 usec
323+
uint64_t running_cycles = ThisFiber::GetRunningTimeCycles();
333324

334325
fb2::NoOpLock lk;
335326

@@ -349,6 +340,14 @@ size_t SliceSnapshot::FlushSerialized(SerializerBase::FlushState flush_state) {
349340

350341
VLOG(2) << "Pushed with Serialize() " << serialized;
351342

343+
// FlushToSink can be quite slow for large values or due compression.
344+
// In this case we counter-balance CPU over-usage by forcing sleep.
345+
// We measure running_cycles before the preemption points because they reset the counter.
346+
if (running_cycles > kLongRunThresholdCycles) {
347+
// Balance the load between fibers.
348+
ThisFiber::SleepFor(chrono::microseconds(200));
349+
}
350+
352351
return serialized;
353352
}
354353

0 commit comments

Comments
 (0)