Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug that starves all children except an unblocked child in weight_fair TC #729

Merged
merged 3 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions core/traffic_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ void PriorityTrafficClass::FinishAndAccountTowardsRoot(

WeightedFairTrafficClass::~WeightedFairTrafficClass() {
while (!runnable_children_.empty()) {
delete runnable_children_.top().c_;
delete runnable_children_.top().c;
runnable_children_.pop();
}
for (auto &c : blocked_children_) {
delete c.c_;
delete c.c;
}
TrafficClassBuilder::Clear(this);
}
Expand All @@ -187,21 +187,12 @@ std::vector<TrafficClass *> WeightedFairTrafficClass::Children() const {

bool WeightedFairTrafficClass::AddChild(TrafficClass *child,
resource_share_t share) {
if (child->parent_) {
return false;
}

int64_t pass = 0;
if (!runnable_children_.empty()) {
pass = runnable_children_.top().pass_;
}

if (!share) {
if (child->parent_ || share == 0) {
return false;
}

child->parent_ = this;
WeightedFairTrafficClass::ChildData child_data{STRIDE1 / share, pass, child};
ChildData child_data{STRIDE1 / share, {NextPass()}, child};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is initializing a struct with a union, for clarity would you be able to use designated initializers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wanted to do that, but g++5 refused with "non-trivial designated initializers are not supported" 😞

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm... I was hoping that that wouldn't happen with unions because it'd be smart enough to know that one option among the union is set.

if (child->blocked_) {
blocked_children_.push_back(child_data);
} else {
Expand All @@ -228,15 +219,15 @@ bool WeightedFairTrafficClass::RemoveChild(TrafficClass *child) {

for (auto it = blocked_children_.begin(); it != blocked_children_.end();
it++) {
if (it->c_ == child) {
if (it->c == child) {
blocked_children_.erase(it);
child->parent_ = nullptr;
return true;
}
}

bool ret = runnable_children_.delete_single_element(
[=](const ChildData &x) { return x.c_ == child; });
[=](const ChildData &x) { return x.c == child; });
if (ret) {
child->parent_ = nullptr;
BlockTowardsRoot();
Expand All @@ -247,14 +238,14 @@ bool WeightedFairTrafficClass::RemoveChild(TrafficClass *child) {
}

TrafficClass *WeightedFairTrafficClass::PickNextChild() {
return runnable_children_.top().c_;
return runnable_children_.top().c;
}

void WeightedFairTrafficClass::UnblockTowardsRoot(uint64_t tsc) {
// TODO(barath): Optimize this unblocking behavior.
for (auto it = blocked_children_.begin(); it != blocked_children_.end();) {
if (!it->c_->blocked_) {
it->pass_ = 0;
if (!it->c->blocked_) {
it->pass = NextPass() + it->remain;
runnable_children_.push(*it);
blocked_children_.erase(it++);
} else {
Expand All @@ -267,7 +258,7 @@ void WeightedFairTrafficClass::UnblockTowardsRoot(uint64_t tsc) {

void WeightedFairTrafficClass::BlockTowardsRoot() {
runnable_children_.delete_single_element([&](const ChildData &x) {
if (x.c_->blocked_) {
if (x.c->blocked_) {
blocked_children_.push_back(x);
return true;
}
Expand All @@ -282,17 +273,21 @@ void WeightedFairTrafficClass::FinishAndAccountTowardsRoot(
uint64_t tsc) {
ACCUMULATE(stats_.usage, usage);

// DCHECK_EQ(item.c_, child) << "Child that we picked should be at the front
auto &item = runnable_children_.mutable_top();
uint64_t consumed = usage[resource_];
uint64_t pass_delta = item.stride * consumed / QUANTUM;

// DCHECK_EQ(item.c, child) << "Child that we picked should be at the front
// of priority queue.";
if (child->blocked_) {
auto item = runnable_children_.top();
runnable_children_.pop();
// The blocked child will be penalized when unblocked, by the amount of the
// resource usage (pass_delta) not accounted for this round.
item.remain = pass_delta;
blocked_children_.emplace_back(std::move(item));
runnable_children_.pop();
blocked_ = runnable_children_.empty();
} else {
auto &item = runnable_children_.mutable_top();
uint64_t consumed = usage[resource_];
item.pass_ += item.stride_ * consumed / QUANTUM;
item.pass += pass_delta;
runnable_children_.decrease_key_top();
}

Expand Down
26 changes: 22 additions & 4 deletions core/traffic_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,21 @@ class WeightedFairTrafficClass final : public TrafficClass {
struct ChildData {
bool operator<(const ChildData &right) const {
// Reversed so that priority_queue is a min priority queue.
return right.pass_ < pass_;
return right.pass < pass;
}

int64_t stride_;
int64_t pass_;
int64_t stride;

TrafficClass *c_;
// NOTE: while in the code example in the original Stride Scheduler
// [Waldspurgger95] maintains "pass" and "remain" (penalty) separately,
// we can safely multiplex these variables in a union since they are never
// used at the same time.
union {
int64_t pass;
int64_t remain;
};

TrafficClass *c;
};

WeightedFairTrafficClass(const std::string &name, resource_t resource)
Expand Down Expand Up @@ -385,6 +393,16 @@ class WeightedFairTrafficClass final : public TrafficClass {
}

private:
// Returns the pass value of the first child to be scheduled next,
// or 0 if there is no runnable child (i.e., the priority queue is empty)
int64_t NextPass() const {
if (runnable_children_.empty()) {
return 0;
} else {
return runnable_children_.top().pass;
}
}

// The resource that we are sharing.
resource_t resource_;

Expand Down
4 changes: 2 additions & 2 deletions core/traffic_class_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ TEST(CreateTree, WeightedFairRootAndLeaf) {
ASSERT_EQ(0, c->blocked_children().size());

LeafTrafficClass *leaf = static_cast<LeafTrafficClass *>(
c->runnable_children().container().front().c_);
c->runnable_children().container().front().c);
ASSERT_NE(nullptr, leaf);
EXPECT_EQ(leaf->parent(), c);

Expand Down Expand Up @@ -245,7 +245,7 @@ TEST(DefaultSchedulerNext, BasicTreeWeightedFair) {
ASSERT_EQ(0, c->blocked_children().size());

LeafTrafficClass *leaf = static_cast<LeafTrafficClass *>(
c->runnable_children().container().front().c_);
c->runnable_children().container().front().c);
ASSERT_NE(nullptr, leaf);
EXPECT_EQ(leaf->parent(), c);

Expand Down