Linkgraph: Fix numeric overflow in cost estimate/duration multiplier.

Bump cost estimate type to 64 bits
This commit is contained in:
Jonathan G Rennison
2017-10-05 18:27:22 +01:00
parent a93cc441a9
commit 3f83546dfc
2 changed files with 10 additions and 10 deletions

View File

@@ -46,7 +46,7 @@ void LinkGraphSchedule::SpawnNext()
if (this->schedule.empty()) return;
GraphList schedule_to_back;
uint total_cost = 0;
uint64 total_cost = 0;
for (auto iter = this->schedule.begin(); iter != this->schedule.end();) {
auto current = iter;
++iter;
@@ -62,22 +62,22 @@ void LinkGraphSchedule::SpawnNext()
total_cost += it->Graph().CalculateCostEstimate();
}
uint scaling = 1 + FindLastBit(total_cost);
uint cost_budget = total_cost / scaling;
uint used_budget = 0;
uint64 cost_budget = total_cost / scaling;
uint64 used_budget = 0;
std::vector<LinkGraphJobGroup::JobInfo> jobs_to_execute;
while (used_budget < cost_budget && !this->schedule.empty()) {
LinkGraph *lg = this->schedule.front();
assert(lg == LinkGraph::Get(lg->index));
this->schedule.pop_front();
uint cost = lg->CalculateCostEstimate();
uint64 cost = lg->CalculateCostEstimate();
used_budget += cost;
if (LinkGraphJob::CanAllocateItem()) {
uint duration_multiplier = CeilDiv(scaling * cost, total_cost);
uint duration_multiplier = CeilDivT<uint64_t>(scaling * cost, total_cost);
std::unique_ptr<LinkGraphJob> job(new LinkGraphJob(*lg, duration_multiplier));
jobs_to_execute.emplace_back(job.get(), cost);
if (this->running.empty() || job->JoinDateTicks() >= this->running.back()->JoinDateTicks()) {
this->running.push_back(std::move(job));
DEBUG(linkgraph, 3, "LinkGraphSchedule::SpawnNext(): Running job: id: %u, nodes: %u, cost: %u, duration_multiplier: %u",
DEBUG(linkgraph, 3, "LinkGraphSchedule::SpawnNext(): Running job: id: %u, nodes: %u, cost: " OTTD_PRINTF64U ", duration_multiplier: %u",
lg->index, lg->Size(), cost, duration_multiplier);
} else {
// find right place to insert
@@ -85,7 +85,7 @@ void LinkGraphSchedule::SpawnNext()
return a < b->JoinDateTicks();
});
this->running.insert(iter, std::move(job));
DEBUG(linkgraph, 3, "LinkGraphSchedule::SpawnNext(): Running job (re-ordering): id: %u, nodes: %u, cost: %u, duration_multiplier: %u",
DEBUG(linkgraph, 3, "LinkGraphSchedule::SpawnNext(): Running job (re-ordering): id: %u, nodes: %u, cost: " OTTD_PRINTF64U ", duration_multiplier: %u",
lg->index, lg->Size(), cost, duration_multiplier);
}
} else {
@@ -97,7 +97,7 @@ void LinkGraphSchedule::SpawnNext()
LinkGraphJobGroup::ExecuteJobSet(std::move(jobs_to_execute));
DEBUG(linkgraph, 2, "LinkGraphSchedule::SpawnNext(): Linkgraph job totals: cost: %u, budget: %u, scaling: %u, scheduled: %zu, running: %zu",
DEBUG(linkgraph, 2, "LinkGraphSchedule::SpawnNext(): Linkgraph job totals: cost: " OTTD_PRINTF64U ", budget: " OTTD_PRINTF64U ", scaling: %u, scheduled: %zu, running: %zu",
total_cost, cost_budget, scaling, this->schedule.size(), this->running.size());
}