From aac016f8e2f9beeb3730256ae5c0f8f77f2efbfa Mon Sep 17 00:00:00 2001
From: NamelessOIer <70872016+NamelessOIer@users.noreply.github.com>
Date: Fri, 18 Oct 2024 15:22:53 +0800
Subject: [PATCH] Refactor

---
 src/CraneCtld/TaskScheduler.cpp | 26 ++++++++++++++++++++------
 src/CraneCtld/TaskScheduler.h   |  4 +---
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/CraneCtld/TaskScheduler.cpp b/src/CraneCtld/TaskScheduler.cpp
index d16e4c6c1..67678b6e5 100644
--- a/src/CraneCtld/TaskScheduler.cpp
+++ b/src/CraneCtld/TaskScheduler.cpp
@@ -1855,7 +1855,6 @@ void MinLoadFirst::CalculateNodeSelectionInfoOfPartition_(
     // Sort all running task in this node by ending time.
     std::vector<std::pair<absl::Time, uint32_t>> end_time_task_id_vec;
 
-    std::vector<std::string> running_task_ids_str;
     for (const auto& [task_id, res] : craned_meta->running_task_resource_map) {
       const auto& task = running_tasks.at(task_id);
 
@@ -1869,8 +1868,14 @@ void MinLoadFirst::CalculateNodeSelectionInfoOfPartition_(
       absl::Time end_time = std::max(task->StartTime() + task->time_limit,
                                      now + absl::Seconds(1));
       end_time_task_id_vec.emplace_back(end_time, task_id);
+    }
 
-      running_task_ids_str.emplace_back(std::to_string(task_id));
+    if constexpr (kAlgoTraceOutput) {
+      std::string running_task_ids_str;
+      for (const auto& [end_time, task_id] : end_time_task_id_vec)
+        running_task_ids_str.append(fmt::format("{}, ", task_id));
+      CRANE_TRACE("Craned node {} has running tasks: {}", craned_id,
+                  running_task_ids_str);
     }
 
     std::sort(
@@ -1890,8 +1895,8 @@ void MinLoadFirst::CalculateNodeSelectionInfoOfPartition_(
       }
     }
 
-    // Calculate delta resources at [now, first task end,
-    //  second task end, ...] in this node.
+    // Calculate how many resources are available at [now, first task end,
+    // second task end, ...] in this node.
     auto& time_avail_res_map =
         node_selection_info_ref.node_time_avail_res_map[craned_id];
     node_selection_info_ref.node_res_total_map[craned_id] =
@@ -1929,6 +1934,16 @@ void MinLoadFirst::CalculateNodeSelectionInfoOfPartition_(
            */
           std::tie(cur_time_iter, ok) =
               time_avail_res_map.emplace(end_time, cur_time_iter->second);
+
+          if constexpr (kAlgoTraceOutput) {
+            CRANE_TRACE(
+                "Insert duration [now+{}s, inf) with resource: "
+                "cpu: {}, mem: {}, gres: {}",
+                absl::ToInt64Seconds(end_time - now),
+                craned_meta->res_avail.allocatable_res.cpu_count,
+                craned_meta->res_avail.allocatable_res.memory_bytes,
+                util::ReadableDresInNode(craned_meta->res_avail));
+          }
         }
 
         /**
@@ -2456,8 +2471,7 @@ CraneErr TaskScheduler::AcquireTaskAttributes(TaskInCtld* task) {
     task_mem_per_cpu = part_meta.default_mem_per_cpu;
   } else if (part_meta.max_mem_per_cpu != 0) {
     // If a task sets its memory bytes,
-    // check if memory/core ratio is greater than the partition's maximum
-    // value.
+    // check if memory/core ratio is greater than the partition's maximum value.
     task_mem_per_cpu =
         std::min(task_mem_per_cpu, (double)part_meta.max_mem_per_cpu);
   }
diff --git a/src/CraneCtld/TaskScheduler.h b/src/CraneCtld/TaskScheduler.h
index f7287cbba..603091ea8 100644
--- a/src/CraneCtld/TaskScheduler.h
+++ b/src/CraneCtld/TaskScheduler.h
@@ -155,7 +155,6 @@ class MinLoadFirst : public INodeSelectionAlgo {
    * In time interval [y, z-1], the amount of available resources is b.
    * In time interval [z, ...], the amount of available resources is c.
    */
-
   using TimeAvailResMap = std::map<absl::Time, ResourceInNode>;
   struct TimeAvailResTracker {
     const CranedId craned_id;
@@ -339,8 +338,7 @@ class TaskScheduler {
 
   void TerminateTasksOnCraned(const CranedId& craned_id, uint32_t exit_code);
 
-  // Temporary inconsistency may happen. If 'false' is returned, just ignore
-  // it.
+  // Temporary inconsistency may happen. If 'false' is returned, just ignore it.
   void QueryTasksInRam(const crane::grpc::QueryTasksInfoRequest* request,
                        crane::grpc::QueryTasksInfoReply* response);