Skip to content

Commit

Permalink
Remove redundant call for absl::Now(). (#270)
Browse files Browse the repository at this point in the history
* Remove redundant call for absl::Now().

* Refactor, fix bugs and add comments in config.yaml.

Signed-off-by: RileyW <wrllrwwrllrw@gmail.com>

---------

Signed-off-by: RileyW <wrllrwwrllrw@gmail.com>
Co-authored-by: NamelessOIer <namelessoier@protonmail.com>
  • Loading branch information
RileyWen and NamelessOIer authored Jun 12, 2024
1 parent 0f28bec commit 1227f07
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 2 additions & 0 deletions etc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ CranedMutexFilePath: craned/craned.lock
CranedForeground: true

# Scheduling settings
# Current implemented scheduling algorithms are:
# priority/basic, priority/multifactor
PriorityType: priority/multifactor

# Default value is true
Expand Down
23 changes: 12 additions & 11 deletions src/CraneCtld/TaskScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2436,22 +2436,23 @@ void TaskScheduler::TerminateTasksOnCraned(const CranedId& craned_id,
std::vector<task_id_t> MultiFactorPriority::GetOrderedTaskIdList(
const OrderedTaskMap& pending_task_map,
const UnorderedTaskMap& running_task_map, size_t limit_num) {
CalculateFactorBound_(pending_task_map, running_task_map);
absl::Time now = absl::Now();
CalculateFactorBound_(pending_task_map, running_task_map, now);

std::vector<std::pair<task_id_t, double>> task_priority_vec;
for (const auto& [task_id, task] : pending_task_map) {
// Admin may manually specify the priority of a task.
// In this case, MultiFactorPriority will not calculate the priority.
double priority = (task->mandated_priority == 0.0)
? CalculatePriority_(task.get())
? CalculatePriority_(task.get(), now)
: task->mandated_priority;
task->cached_priority = priority;
task_priority_vec.emplace_back(task->TaskId(), priority);
}

std::sort(task_priority_vec.begin(), task_priority_vec.end(),
[](const std::pair<task_id_t, uint32_t>& a,
const std::pair<task_id_t, uint32_t>& b) {
[](const std::pair<task_id_t, double>& a,
const std::pair<task_id_t, double>& b) {
return a.second > b.second;
});

Expand All @@ -2468,7 +2469,7 @@ std::vector<task_id_t> MultiFactorPriority::GetOrderedTaskIdList(

void MultiFactorPriority::CalculateFactorBound_(
const OrderedTaskMap& pending_task_map,
const UnorderedTaskMap& running_task_map) {
const UnorderedTaskMap& running_task_map, absl::Time now) {
FactorBound& bound = m_factor_bound_;

// Initialize the values of each max and min
Expand Down Expand Up @@ -2496,7 +2497,7 @@ void MultiFactorPriority::CalculateFactorBound_(
bound.acc_service_val_map.clear();

for (const auto& [task_id, task] : pending_task_map) {
uint64_t age = ToInt64Seconds((absl::Now() - task->SubmitTime()));
uint64_t age = ToInt64Seconds(now - task->SubmitTime());
age = std::min(age, g_config.PriorityConfig.MaxAge);

bound.acc_service_val_map[task->account] = 0.0;
Expand Down Expand Up @@ -2556,7 +2557,7 @@ void MultiFactorPriority::CalculateFactorBound_(
else
service_val += 1.0;

uint64_t run_time = ToInt64Seconds(absl::Now() - task->StartTime());
uint64_t run_time = ToInt64Seconds(now - task->StartTime());
bound.acc_service_val_map[task->account] +=
service_val * static_cast<double>(run_time);
}
Expand All @@ -2567,11 +2568,11 @@ void MultiFactorPriority::CalculateFactorBound_(
}
}

double MultiFactorPriority::CalculatePriority_(Ctld::TaskInCtld* task) {
FactorBound& bound = m_factor_bound_;
double MultiFactorPriority::CalculatePriority_(Ctld::TaskInCtld* task,
absl::Time now) const {
FactorBound const& bound = m_factor_bound_;

uint64_t task_age =
ToUnixSeconds(absl::Now()) - task->SubmitTimeInUnixSecond();
uint64_t task_age = ToUnixSeconds(now) - task->SubmitTimeInUnixSecond();
task_age = std::min(task_age, g_config.PriorityConfig.MaxAge);

uint32_t task_qos_priority = task->qos_priority;
Expand Down
5 changes: 3 additions & 2 deletions src/CraneCtld/TaskScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ class MultiFactorPriority : public IPrioritySorter {
};

void CalculateFactorBound_(const OrderedTaskMap& pending_task_map,
const UnorderedTaskMap& running_task_map);
double CalculatePriority_(Ctld::TaskInCtld* task);
const UnorderedTaskMap& running_task_map,
absl::Time now);
double CalculatePriority_(Ctld::TaskInCtld* task, absl::Time now) const;

FactorBound m_factor_bound_;
};
Expand Down

0 comments on commit 1227f07

Please sign in to comment.