2022-01-31 13:56:44 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2022-01-31 13:56:44 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2022-01-28 13:50:59 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/algorithm.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/Container.h"
|
2023-07-27 13:03:39 +02:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2022-03-11 19:38:48 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/List.h"
|
2022-02-01 16:25:02 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/optional.h"
|
|
|
|
#include "td/utils/Span.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2022-01-28 13:50:59 +01:00
|
|
|
#include "td/utils/VectorQueue.h"
|
|
|
|
|
2022-01-31 13:56:44 +01:00
|
|
|
#include <functional>
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
struct ChainSchedulerBase {
|
|
|
|
struct TaskWithParents {
|
|
|
|
uint64 task_id{};
|
|
|
|
vector<uint64> parents;
|
|
|
|
};
|
2022-01-28 17:12:20 +01:00
|
|
|
};
|
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT = Unit>
|
2022-02-01 16:25:02 +01:00
|
|
|
class ChainScheduler final : public ChainSchedulerBase {
|
2022-01-28 13:50:59 +01:00
|
|
|
public:
|
|
|
|
using TaskId = uint64;
|
|
|
|
using ChainId = uint64;
|
2022-02-03 20:01:09 +01:00
|
|
|
|
2022-01-31 13:56:44 +01:00
|
|
|
TaskId create_task(Span<ChainId> chains, ExtraT extra = {});
|
2022-02-03 20:01:09 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
ExtraT *get_task_extra(TaskId task_id);
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
optional<TaskWithParents> start_next_task();
|
2022-02-03 20:01:09 +01:00
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
void pause_task(TaskId task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
void finish_task(TaskId task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
void reset_task(TaskId task_id);
|
|
|
|
|
2022-01-28 15:43:22 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each(F &&f) {
|
2023-07-29 16:55:05 +02:00
|
|
|
tasks_.for_each([&f](uint64, Task &task) { f(task.extra); });
|
2022-01-28 15:43:22 +01:00
|
|
|
}
|
2022-02-03 20:01:09 +01:00
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each_dependent(TaskId task_id, F &&f) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<TaskId> visited;
|
2022-02-01 14:01:17 +01:00
|
|
|
bool check_for_collisions = task->chains.size() > 1;
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
2022-02-03 20:01:09 +01:00
|
|
|
chain_info.chain.foreach_child(&task_chain_info.chain_node, [&](TaskId task_id, uint64) {
|
|
|
|
if (check_for_collisions && !visited.insert(task_id).second) {
|
|
|
|
return;
|
2022-02-01 14:01:17 +01:00
|
|
|
}
|
|
|
|
f(task_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 15:43:22 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
private:
|
|
|
|
struct ChainNode : ListNode {
|
|
|
|
TaskId task_id{};
|
2022-02-01 09:13:59 +01:00
|
|
|
uint64 generation{};
|
2022-01-28 13:50:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Chain {
|
|
|
|
public:
|
|
|
|
void add_task(ChainNode *node) {
|
|
|
|
head_.put_back(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<TaskId> get_first() {
|
|
|
|
if (head_.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return static_cast<ChainNode &>(*head_.get_next()).task_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<TaskId> get_child(ChainNode *chain_node) {
|
|
|
|
if (chain_node->get_next() == head_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return static_cast<ChainNode &>(*chain_node->get_next()).task_id;
|
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
optional<ChainNode *> get_parent(ChainNode *chain_node) {
|
2022-01-28 13:50:59 +01:00
|
|
|
if (chain_node->get_prev() == head_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
return static_cast<ChainNode *>(chain_node->get_prev());
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void finish_task(ChainNode *node) {
|
|
|
|
node->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return head_.empty();
|
|
|
|
}
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
void foreach(std::function<void(TaskId, uint64)> f) const {
|
2022-01-28 13:50:59 +01:00
|
|
|
for (auto it = head_.begin(); it != head_.end(); it = it->get_next()) {
|
2022-02-01 09:13:59 +01:00
|
|
|
auto &node = static_cast<const ChainNode &>(*it);
|
|
|
|
f(node.task_id, node.generation);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
void foreach_child(ListNode *start_node, std::function<void(TaskId, uint64)> f) const {
|
|
|
|
for (auto it = start_node; it != head_.end(); it = it->get_next()) {
|
2022-02-01 14:01:17 +01:00
|
|
|
auto &node = static_cast<const ChainNode &>(*it);
|
|
|
|
f(node.task_id, node.generation);
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
ListNode head_;
|
|
|
|
};
|
|
|
|
struct ChainInfo {
|
|
|
|
Chain chain;
|
|
|
|
uint32 active_tasks{};
|
2022-02-01 09:13:59 +01:00
|
|
|
uint64 generation{1};
|
2022-01-28 13:50:59 +01:00
|
|
|
};
|
|
|
|
struct TaskChainInfo {
|
|
|
|
ChainNode chain_node;
|
|
|
|
ChainId chain_id{};
|
|
|
|
ChainInfo *chain_info{};
|
|
|
|
};
|
|
|
|
struct Task {
|
2022-02-01 14:01:17 +01:00
|
|
|
enum class State { Pending, Active, Paused } state{State::Pending};
|
2022-01-31 13:56:44 +01:00
|
|
|
vector<TaskChainInfo> chains;
|
2022-01-28 13:50:59 +01:00
|
|
|
ExtraT extra;
|
|
|
|
};
|
2023-07-27 13:18:39 +02:00
|
|
|
FlatHashMap<ChainId, unique_ptr<ChainInfo>> chains_;
|
2023-07-27 13:03:39 +02:00
|
|
|
FlatHashMap<ChainId, TaskId> limited_tasks_;
|
2022-01-28 13:50:59 +01:00
|
|
|
Container<Task> tasks_;
|
|
|
|
VectorQueue<TaskId> pending_tasks_;
|
|
|
|
|
2023-07-27 13:18:39 +02:00
|
|
|
ChainInfo &get_chain_info(ChainId chain_id) {
|
|
|
|
auto &chain = chains_[chain_id];
|
|
|
|
if (chain == nullptr) {
|
|
|
|
chain = make_unique<ChainInfo>();
|
|
|
|
}
|
|
|
|
return *chain;
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
void try_start_task(TaskId task_id) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-01-28 13:50:59 +01:00
|
|
|
if (task->state != Task::State::Pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
auto o_parent = task_chain_info.chain_info->chain.get_parent(&task_chain_info.chain_node);
|
|
|
|
|
|
|
|
if (o_parent) {
|
|
|
|
if (o_parent.value()->generation != task_chain_info.chain_info->generation) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
|
|
|
|
if (task_chain_info.chain_info->active_tasks >= 10) {
|
2022-01-28 13:50:59 +01:00
|
|
|
limited_tasks_[task_chain_info.chain_id] = task_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do_start_task(task_id, task);
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_start_task(TaskId task_id, Task *task) {
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2023-07-27 13:18:39 +02:00
|
|
|
ChainInfo &chain_info = get_chain_info(task_chain_info.chain_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
chain_info.active_tasks++;
|
2022-02-01 09:13:59 +01:00
|
|
|
task_chain_info.chain_node.generation = chain_info.generation;
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
task->state = Task::State::Active;
|
|
|
|
|
|
|
|
pending_tasks_.push(task_id);
|
2022-02-09 16:41:38 +01:00
|
|
|
for_each_child(task, [&](TaskId task_id) { try_start_task(task_id); });
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
template <class F>
|
|
|
|
void for_each_child(Task *task, F &&f) {
|
2022-01-28 13:50:59 +01:00
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
2022-01-28 13:50:59 +01:00
|
|
|
auto o_child = chain_info.chain.get_child(&task_chain_info.chain_node);
|
|
|
|
if (o_child) {
|
2022-02-01 09:13:59 +01:00
|
|
|
f(o_child.value());
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
void inactivate_task(TaskId task_id, bool failed) {
|
2022-02-01 16:25:02 +01:00
|
|
|
LOG(DEBUG) << "Inactivate " << task_id << " " << (failed ? "failed" : "finished");
|
2022-02-01 14:01:17 +01:00
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-02-01 09:13:59 +01:00
|
|
|
bool was_active = task->state == Task::State::Active;
|
2022-01-28 13:50:59 +01:00
|
|
|
task->state = Task::State::Pending;
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
|
|
|
if (was_active) {
|
|
|
|
chain_info.active_tasks--;
|
|
|
|
}
|
2022-02-01 14:01:17 +01:00
|
|
|
if (was_active && failed) {
|
2022-02-09 01:08:18 +01:00
|
|
|
chain_info.generation = td::max(chain_info.generation, task_chain_info.chain_node.generation + 1);
|
2022-02-01 14:01:17 +01:00
|
|
|
}
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
auto it = limited_tasks_.find(task_chain_info.chain_id);
|
|
|
|
if (it != limited_tasks_.end()) {
|
|
|
|
auto limited_task_id = it->second;
|
|
|
|
limited_tasks_.erase(it);
|
|
|
|
if (limited_task_id != task_id) {
|
2022-02-01 09:13:59 +01:00
|
|
|
try_start_task_later(limited_task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 14:01:17 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
auto o_first = chain_info.chain.get_first();
|
|
|
|
if (o_first) {
|
|
|
|
auto first_task_id = o_first.unwrap();
|
|
|
|
if (first_task_id != task_id) {
|
2022-02-01 09:13:59 +01:00
|
|
|
try_start_task_later(first_task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void finish_chain_task(TaskChainInfo &task_chain_info) {
|
|
|
|
auto &chain = task_chain_info.chain_info->chain;
|
|
|
|
chain.finish_task(&task_chain_info.chain_node);
|
|
|
|
if (chain.empty()) {
|
|
|
|
chains_.erase(task_chain_info.chain_id);
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
|
2022-02-01 16:25:02 +01:00
|
|
|
vector<TaskId> to_start_;
|
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
void try_start_task_later(TaskId task_id) {
|
2022-02-07 22:42:53 +01:00
|
|
|
LOG(DEBUG) << "Start later " << task_id;
|
2022-02-01 16:25:02 +01:00
|
|
|
to_start_.push_back(task_id);
|
2022-02-01 09:13:59 +01:00
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
|
2022-02-01 09:13:59 +01:00
|
|
|
void flush_try_start_task() {
|
2022-02-01 16:25:02 +01:00
|
|
|
auto moved_to_start = std::move(to_start_);
|
2022-02-01 09:13:59 +01:00
|
|
|
for (auto task_id : moved_to_start) {
|
2022-02-01 14:01:17 +01:00
|
|
|
try_start_task(task_id);
|
2022-02-01 09:13:59 +01:00
|
|
|
}
|
2022-02-01 16:25:02 +01:00
|
|
|
CHECK(to_start_.empty());
|
2022-02-01 09:13:59 +01:00
|
|
|
}
|
2022-02-03 20:01:09 +01:00
|
|
|
|
|
|
|
template <class ExtraTT>
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, ChainScheduler<ExtraTT> &scheduler);
|
2022-01-28 13:50:59 +01:00
|
|
|
};
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2023-07-29 01:41:29 +02:00
|
|
|
typename ChainScheduler<ExtraT>::TaskId ChainScheduler<ExtraT>::create_task(Span<ChainId> chains, ExtraT extra) {
|
2022-01-28 13:50:59 +01:00
|
|
|
auto task_id = tasks_.create();
|
|
|
|
Task &task = *tasks_.get(task_id);
|
|
|
|
task.extra = std::move(extra);
|
2023-07-29 01:41:29 +02:00
|
|
|
task.chains = transform(chains, [&](ChainId chain_id) {
|
2023-07-27 13:18:39 +02:00
|
|
|
CHECK(chain_id != 0);
|
2022-01-28 13:50:59 +01:00
|
|
|
TaskChainInfo task_chain_info;
|
2023-07-27 13:18:39 +02:00
|
|
|
ChainInfo &chain_info = get_chain_info(chain_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
task_chain_info.chain_id = chain_id;
|
|
|
|
task_chain_info.chain_info = &chain_info;
|
|
|
|
task_chain_info.chain_node.task_id = task_id;
|
2022-02-01 14:01:17 +01:00
|
|
|
task_chain_info.chain_node.generation = 0;
|
2022-01-28 13:50:59 +01:00
|
|
|
return task_chain_info;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (TaskChainInfo &task_chain_info : task.chains) {
|
2022-02-01 09:13:59 +01:00
|
|
|
ChainInfo &chain_info = *task_chain_info.chain_info;
|
|
|
|
chain_info.chain.add_task(&task_chain_info.chain_node);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
try_start_task(task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
return task_id;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
// TODO: return reference
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2023-07-29 01:41:29 +02:00
|
|
|
ExtraT *ChainScheduler<ExtraT>::get_task_extra(TaskId task_id) { // may return nullptr
|
2022-01-28 13:50:59 +01:00
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
if (task == nullptr) {
|
2022-01-28 13:50:59 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &task->extra;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2022-02-01 09:13:59 +01:00
|
|
|
optional<ChainSchedulerBase::TaskWithParents> ChainScheduler<ExtraT>::start_next_task() {
|
2022-01-28 13:50:59 +01:00
|
|
|
if (pending_tasks_.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto task_id = pending_tasks_.pop();
|
2022-02-01 09:13:59 +01:00
|
|
|
TaskWithParents res;
|
2022-01-28 13:50:59 +01:00
|
|
|
res.task_id = task_id;
|
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-01-28 13:50:59 +01:00
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
Chain &chain = task_chain_info.chain_info->chain;
|
|
|
|
auto o_parent = chain.get_parent(&task_chain_info.chain_node);
|
|
|
|
if (o_parent) {
|
2022-02-01 09:13:59 +01:00
|
|
|
res.parents.push_back(o_parent.value()->task_id);
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2023-07-29 01:41:29 +02:00
|
|
|
void ChainScheduler<ExtraT>::finish_task(TaskId task_id) {
|
2022-01-28 13:50:59 +01:00
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-02-01 16:25:02 +01:00
|
|
|
CHECK(to_start_.empty());
|
2022-02-01 14:01:17 +01:00
|
|
|
|
|
|
|
inactivate_task(task_id, false);
|
|
|
|
|
2022-02-09 16:41:38 +01:00
|
|
|
for_each_child(task, [&](TaskId task_id) { try_start_task_later(task_id); });
|
2022-01-28 13:50:59 +01:00
|
|
|
|
|
|
|
for (TaskChainInfo &task_chain_info : task->chains) {
|
|
|
|
finish_chain_task(task_chain_info);
|
|
|
|
}
|
2022-02-01 09:13:59 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
tasks_.erase(task_id);
|
2022-02-01 09:13:59 +01:00
|
|
|
flush_try_start_task();
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2023-07-29 01:41:29 +02:00
|
|
|
void ChainScheduler<ExtraT>::reset_task(TaskId task_id) {
|
2022-02-01 16:25:02 +01:00
|
|
|
CHECK(to_start_.empty());
|
2022-01-28 13:50:59 +01:00
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-02-01 14:01:17 +01:00
|
|
|
inactivate_task(task_id, true);
|
2022-02-01 09:13:59 +01:00
|
|
|
try_start_task_later(task_id);
|
|
|
|
flush_try_start_task();
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-02-01 14:01:17 +01:00
|
|
|
template <class ExtraT>
|
|
|
|
void ChainScheduler<ExtraT>::pause_task(TaskId task_id) {
|
|
|
|
auto *task = tasks_.get(task_id);
|
2022-02-03 20:01:09 +01:00
|
|
|
CHECK(task != nullptr);
|
2022-02-01 14:01:17 +01:00
|
|
|
inactivate_task(task_id, true);
|
|
|
|
task->state = Task::State::Paused;
|
2022-02-01 15:05:23 +01:00
|
|
|
flush_try_start_task();
|
2022-02-01 14:01:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
template <class ExtraT>
|
2022-01-28 17:12:20 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &sb, ChainScheduler<ExtraT> &scheduler) {
|
2022-01-28 13:50:59 +01:00
|
|
|
// 1 print chains
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << '\n';
|
2022-01-28 13:50:59 +01:00
|
|
|
for (auto &it : scheduler.chains_) {
|
2023-07-27 13:18:39 +02:00
|
|
|
CHECK(it.second != nullptr);
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << "ChainId{" << it.first << "}";
|
2023-07-27 13:18:39 +02:00
|
|
|
sb << " active_cnt = " << it.second->active_tasks;
|
|
|
|
sb << " g = " << it.second->generation;
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << ':';
|
2023-07-29 16:55:05 +02:00
|
|
|
it.second->chain.foreach([&](typename ChainScheduler<ExtraT>::TaskId task_id, uint64 generation) {
|
2023-07-29 01:41:29 +02:00
|
|
|
sb << ' ' << *scheduler.get_task_extra(task_id) << ':' << generation;
|
|
|
|
});
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << '\n';
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
2023-07-29 16:55:05 +02:00
|
|
|
scheduler.tasks_.for_each([&](uint64, typename ChainScheduler<ExtraT>::Task &task) {
|
2022-01-28 13:50:59 +01:00
|
|
|
sb << "Task: " << task.extra;
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << " state = " << static_cast<int>(task.state);
|
2022-02-01 16:25:02 +01:00
|
|
|
for (auto &task_chain_info : task.chains) {
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << " g = " << task_chain_info.chain_node.generation;
|
2022-02-01 09:13:59 +01:00
|
|
|
if (task_chain_info.chain_info->generation != task_chain_info.chain_node.generation) {
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << " chain_g = " << task_chain_info.chain_info->generation;
|
2022-01-28 13:50:59 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-03 20:01:09 +01:00
|
|
|
sb << '\n';
|
2022-01-28 13:50:59 +01:00
|
|
|
});
|
|
|
|
return sb;
|
|
|
|
}
|
2022-01-31 13:56:44 +01:00
|
|
|
|
2022-01-28 13:50:59 +01:00
|
|
|
} // namespace td
|