From cb7360ae868987a43526b4994c20a7c51d971293 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Thu, 13 Mar 2025 20:47:20 +0300 Subject: [PATCH] improved planner --- src/files/Supervisor.h | 88 ++++++++++++++++++++++++++++-------------- src/files/version.h | 2 +- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/files/Supervisor.h b/src/files/Supervisor.h index 3a4ceb13..68490b30 100644 --- a/src/files/Supervisor.h +++ b/src/files/Supervisor.h @@ -132,7 +132,7 @@ public: } const int maxThreads = std::thread::hardware_concurrency(); - printf("total tasks count = %ld, active task count %ld, maxKernels %d, maxNeededKernels %d, maxThreads %d\n", + printf("total tasks count = %lu, active task count %lu, maxKernels %lu, maxNeededKernels %d, maxThreads %d\n", this->getLength(), activeTasks, maxKernels, maxNeededKernels, maxThreads); if (maxKernels > maxThreads) { @@ -161,7 +161,9 @@ public: const double total = totalProcessTasks; auto timer_pause = Utils::getAbsoluteTime(); - + auto timer_killed = Utils::getAbsoluteTime(); + bool killed = false; + while (activeTasks) { long oldActiveTasks = activeTasks; emptyKeys.clear(); @@ -196,23 +198,8 @@ public: // очищаем от пустых ключей for (auto& empty : emptyKeys) sortedByKernelNeeds.erase(empty); - - // проверяем нет ли завершившихся задач - for (auto& task : activeTaskSet) - { - if (task->Check()) { - toDel.push_back(task); - activeTasks--; - done += task->getKernels(); - busyKernels -= task->getKernels(); -#if DEB - printf(" done task with %d kernels and id %ld\n", task->getKernels(), task->getId()); -#endif - buf += to_string(task->getId()) + " " + string(task->printState().getCharArray()) + " " + to_string(task->getTotalTime()) + "\n"; - //copy after end of while - //task->copyResults(pathRes); - } - } + + checkTasksFinish(activeTaskSet, toDel, activeTasks, done, busyKernels, buf); // очищаем завершенные задачи for (auto& del : toDel) @@ -230,20 +217,42 @@ public: // прошло больше 30 секунд, проверяем паузу if (Utils::getAbsoluteTime() - timer_pause > 30) { - printf("stoped\n"); + bool hasSleep = false; while (checkPause()) { + hasSleep = true; + printf("stoped, sleep 10 seconds\n"); Utils::Sleep(10); } timer_pause = Utils::getAbsoluteTime(); - printf("resume\n"); + if (hasSleep) + printf("resume\n"); + } + + // прошло больше 10 секунд, проверяем нужно ли завершиться + if (Utils::getAbsoluteTime() - timer_killed > 10) { + if (checkKilled()) { + printf("killed\n"); + + while (activeTasks) { + printf("waiting for activeTasks %lu\n", activeTasks); + checkTasksFinish(activeTaskSet, toDel, activeTasks, done, busyKernels, buf); + Utils::Sleep(5); + } + + printf("exit for main while\n"); + killed = true; + break; + } + timer_killed = Utils::getAbsoluteTime(); } } - for (auto& task : taskList) - task->copyResults(pathRes); + if (!killed) { + for (auto& task : taskList) + task->copyResults(pathRes); + } changeState(); - String outFile(pathRes + "/" + getStatePrefix() + "Info.txt"); File tmp(outFile, String(buf.c_str())); } @@ -254,11 +263,6 @@ public: } void saveProgress(long long persentDone) { - /*String dump_progress = "echo '"; - dump_progress = dump_progress + to_string(persentDone).c_str() + "' > progress"; - - system(dump_progress.getCharArray());*/ - FILE* f = fopen("progress", "w"); if (f) { fprintf(f, "%lld", persentDone); @@ -267,7 +271,31 @@ public: } } - bool checkPause() { + bool checkPause() const { return Utils::Exists("pause"); } + + bool checkKilled() const { + return Utils::Exists("kill"); + } + + void checkTasksFinish(const set& activeTaskSet, vector& toDel, size_t& activeTasks, + size_t& done, int& busyKernels, string& buf) { + // проверяем нет ли завершившихся задач + for (auto& task : activeTaskSet) + { + if (task->Check()) { + toDel.push_back(task); + activeTasks--; + done += task->getKernels(); + busyKernels -= task->getKernels(); +#if DEB + printf(" done task with %d kernels and id %ld\n", task->getKernels(), task->getId()); +#endif + buf += to_string(task->getId()) + " " + string(task->printState().getCharArray()) + " " + to_string(task->getTotalTime()) + "\n"; + //copy after end of while + //task->copyResults(pathRes); + } + } + } }; \ No newline at end of file diff --git a/src/files/version.h b/src/files/version.h index 19c7bdba..8e2afd34 100644 --- a/src/files/version.h +++ b/src/files/version.h @@ -1 +1 @@ -16 \ No newline at end of file +17 \ No newline at end of file