diff --git a/Planner/Array.h b/Planner/Array.h index cc40d539..492f57e4 100644 --- a/Planner/Array.h +++ b/Planner/Array.h @@ -24,7 +24,7 @@ public: } long getLength() const { - return elements.size(); + return (long)elements.size(); } T* get(long i) { diff --git a/Planner/Planner.cpp b/Planner/Planner.cpp index 9b0a2d8c..eca4773a 100644 --- a/Planner/Planner.cpp +++ b/Planner/Planner.cpp @@ -1,3 +1,10 @@ +#define _CRT_SECURE_NO_WARNINGS +using namespace std; + +#if __cplusplus >= 201703L +#include +#endif + #include "CompilationSupervisor.h" #include "RunSupervisor.h" #include "Global.h" @@ -10,8 +17,14 @@ int main(int argc, char ** argv) //-- freeKernels = maxKernels; busyKernels= 0; - //-- - chdir(packageWorkspace.getCharArray()); + //-- + +#if __cplusplus >= 201703L + std::filesystem::current_path(packageWorkspace.getCharArray()); +#else + chdir(packageWorkspace.getCharArray()); +#endif + userWorkspace.println(); packageWorkspace.println(); printf("%d\n", maxKernels); diff --git a/Planner/String.h b/Planner/String.h index c449331c..8ec848ce 100644 --- a/Planner/String.h +++ b/Planner/String.h @@ -3,7 +3,7 @@ #include #include #include -#include +#include class String { friend String operator+(const String& a, const String& b); @@ -17,14 +17,14 @@ public: } String(const char* s) { - length = strlen(s); + length = (long)strlen(s); body = new char[length + 1]; for (long i = 0; i < length; ++i) body[i] = s[i]; body[length] = '\0'; } String(const char* s, char ps) { - length = strlen(s); + length = (long)strlen(s); body = new char[length + 1]; for (long i = 0; i < length; ++i) { body[i] = (s[i] == ps) ? '\n' : s[i]; diff --git a/Planner/Supervisor.h b/Planner/Supervisor.h index d5b11760..01043451 100644 --- a/Planner/Supervisor.h +++ b/Planner/Supervisor.h @@ -1,6 +1,8 @@ #pragma once -#include +#include +#include +#include #include "File.h" #include "Task.h" @@ -56,6 +58,28 @@ public: delete packedTasks; delete lines; } + + void changeState() { + switch (this->state) { + case WorkspacesCreation: + this->state = Preparation; + saveState(); + break; + case Preparation: + this->state = Execution; + saveState(); + break; + case Execution: + Finalize(); + this->state = End; + saveState(); + break; + default: + this->state = End; + break; + } + } + void Do() { saveState(); long activeCount = 0; @@ -99,34 +123,38 @@ public: } } // printf("active count = %d\n", activeCount); - if (activeCount == 0) { - switch (this->state) { - case WorkspacesCreation: - this->state = Preparation; - saveState(); - break; - case Preparation: - this->state = Execution; - saveState(); - break; - case Execution: - Finalize(); - this->state = End; - saveState(); - break; - default: - this->state = End; - break; - } - } + if (activeCount == 0) + changeState(); Utils::Sleep(2); } } + + void DoWithSchedule(int maxKernels) { + saveState(); + map> sortedByKernelNeeds; + + long activeTasks = 0; + for (auto& task : this->getElements()) { + if (task->getState() == WorkspaceReady) { + activeTasks++; + sortedByKernelNeeds[task->getKernels()].push(task); + } + } + + printf("total tasks count = %ld, active task count %ld\n", this->getLength(), activeTasks); + + while (activeTasks) { + + } + + changeState(); + } + virtual void Finalize() {} void saveState() { Utils::Sleep(1); //чтобы не было одинаковых по дате файлов. String stateFile = packageWorkspace + "/state/" + getStatePrefix() + printState(); //printf("stateFile=<%s>\n", stateFile.getCharArray()); - File(stateFile, Utils::getDate()); + File tmp(stateFile, Utils::getDate()); } }; \ No newline at end of file diff --git a/Planner/Task.h b/Planner/Task.h index 0cb161c3..2e8b3be7 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -123,9 +123,12 @@ public: File launchScriptFile = File(launchScriptPath, launchScriptText); Utils::Chmod(launchScriptPath); } - virtual void Start() { - if (kernels <= freeKernels) { + int getKernels() const { return kernels; } + + virtual void Start(bool dontCheck = false) { + + if (kernels <= freeKernels || dontCheck) { system(getStartCommand().getCharArray()); state = Running; //- @@ -134,13 +137,10 @@ public: //- } } - virtual void analyseResults() { - state = Finished; - } + virtual void Check() { - if (Utils::Exists(workspace + "/DONE")) { + if (Utils::Exists(workspace + "/DONE")) analyseResults(); - } else { if (Utils::Exists(workspace + "/TIMEOUT")) { state = AbortedByTimeout; @@ -150,6 +150,7 @@ public: state = AbortedByUser; } } + if (state != Running) { //- busyKernels = Utils::min(busyKernels - kernels, maxKernels); @@ -158,8 +159,13 @@ public: saveState(); //не нужно. только для отладки. анализ будет делаться архивом. } } + + virtual void analyseResults() { + state = Finished; + } + virtual void saveState() { String stateFile = workspace + "/TaskState"; - File(stateFile, printState()); + File tmp(stateFile, printState()); } }; diff --git a/Planner/Text.h b/Planner/Text.h index 8f154458..fb759e31 100644 --- a/Planner/Text.h +++ b/Planner/Text.h @@ -9,7 +9,7 @@ public: printf("text length=%ld\n", this->getLength()); auto elems = this->getElements(); - for (size_t i = 0; i < elems.size(); ++i) { + for (long i = 0; i < elems.size(); ++i) { printf("i=%ld; [%s]\n", i, elems[i]->getCharArray()); // elements[i]->println(); } diff --git a/Planner/Utils.h b/Planner/Utils.h index 1fc0c7de..c3658b9d 100644 --- a/Planner/Utils.h +++ b/Planner/Utils.h @@ -3,8 +3,14 @@ #include #include #include -#include #include +#include +#include + +#if __cplusplus >= 201703L +#include +#endif + #include "String.h" class Utils { @@ -16,31 +22,39 @@ public: return (a > b) ? b : a; } static void Mkdir(const String& path) { +#if __cplusplus >= 201703L + std::filesystem::create_directory(path.getCharArray()); +#else mkdir(path.getCharArray(), 0777); +#endif } + //https://stackoverflow.com/questions/4568681/using-chmod-in-a-c-program static void Chmod(const String& path) { String command = "chmod 777 " + String::DQuotes(path); system(command.getCharArray()); } + //https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c static bool Exists(const String& path) { struct stat buffer; return (stat(path.getCharArray(), &buffer) == 0); } + + //in seconds static void Sleep(int s) { - usleep(s * 1000000); + std::chrono::seconds timespan(s); + std::this_thread::sleep_for(timespan); } static void Copy(const String& src, const String& dst) { String command = "cp " + String::DQuotes(src) + " " + String::DQuotes(dst); system(command.getCharArray()); } - static long getAbsoluteTime() { + static time_t getAbsoluteTime() { return time(NULL); } static String getDate() { - long int ttime; - ttime = time(NULL); + auto ttime = time(NULL); String res(ctime(&ttime)); return res; }