From 6b7bde1471672c4151ffe3a4f6c2218c744f1986 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 13:09:16 +0300 Subject: [PATCH 1/8] fixed code style, removed unnecessary pragmas --- Planner/Array.h | 50 ++-- Planner/CompilationSupervisor.h | 24 +- Planner/CompilationTask.h | 91 ++++---- Planner/File.h | 126 +++++----- Planner/Global.h | 11 +- Planner/RunSupervisor.h | 40 ++-- Planner/RunTask.h | 180 +++++++------- Planner/String.h | 403 ++++++++++++++++---------------- Planner/Supervisor.h | 216 ++++++++--------- Planner/Task.h | 264 ++++++++++----------- Planner/Text.h | 38 +-- Planner/Utils.h | 66 +++--- 12 files changed, 765 insertions(+), 744 deletions(-) diff --git a/Planner/Array.h b/Planner/Array.h index f1e59f8a..c9fe79e8 100644 --- a/Planner/Array.h +++ b/Planner/Array.h @@ -1,42 +1,44 @@ +#pragma once + #include #include #include -#pragma once + template class Array { protected: - long length; + long length; T** elements; public: - Array(){ - length=0; - elements=NULL; + Array() { + length = 0; + elements = NULL; } - virtual ~Array(){ - if (elements !=NULL){ - for (long i=0; i { -public: - CompilationSupervisor(){ - this->init("compilationTasks", 4); + +class CompilationSupervisor : public Supervisor { +public: + CompilationSupervisor() { + this->init("compilationTasks", 4); } - CompilationTask * getTaskById(long task_id){ - for (long i=0; i< length; ++i){ - CompilationTask * task = get(i); - if (task->getId()==task_id) + CompilationTask* getTaskById(long task_id) { + for (long i = 0; i < length; ++i) { + CompilationTask* task = get(i); + if (task->getId() == task_id) return task; } return NULL; } - virtual String getStatePrefix(){ + virtual String getStatePrefix() { return String("Compilation"); - } + } }; \ No newline at end of file diff --git a/Planner/CompilationTask.h b/Planner/CompilationTask.h index ba1fb457..c10b4547 100644 --- a/Planner/CompilationTask.h +++ b/Planner/CompilationTask.h @@ -1,47 +1,50 @@ -#include "Task.h" #pragma once -class CompilationTask: public Task { - String test_id; - String makefile_text; -public: - void setTestId(String * test_id_in){ - test_id = String(test_id_in->getCharArray()); - } - void setMakefileText(String * makefile_text_in){ - makefile_text = String(makefile_text_in->getCharArray(), '|'); - } - virtual void print(){ - printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime, - test_id.getCharArray()); - printf("makefile_text=%s\n", makefile_text.getCharArray()); - } - CompilationTask(Text * lines, int offset):Task(lines,offset) { - setTestId(lines->get(offset+2)); - setMakefileText(lines->get(offset+3)); - setState(Waiting); - kernels=1; - } - - virtual void prepareWorkspace(){ - String makeFilePath = String(id)+"/Makefile"; - File makeFileFile = File(makeFilePath, this->makefile_text); - String tests = userWorkspace+"/projects"; - String testPath= tests+"/"+test_id; - String copyCommand = "cp -r " + String::DQuotes(testPath + "/.") + " "+ String::DQuotes(workspace); - system(copyCommand.getCharArray()); - } - virtual String getLaunchScriptText(){ - String modules = userWorkspace+"/modules"; - String starterCall = modules+"/starter"; - String launcherCall = modules+"/launcher"; - return String::DQuotes(starterCall)+" "+ - String::DQuotes(launcherCall)+" "+ - String(maxtime)+" "+ - String::DQuotes("")+" "+ - "make -j -f Makefile"; - } - virtual void analyseResults(){ - String binary = workspace+"/0"; - state = Utils::Exists(binary)? Done:DoneWithErrors; + +#include "Task.h" +#include "Text.h" + +class CompilationTask : public Task { + String test_id; + String makefile_text; +public: + void setTestId(String* test_id_in) { + test_id = String(test_id_in->getCharArray()); + } + void setMakefileText(String* makefile_text_in) { + makefile_text = String(makefile_text_in->getCharArray(), '|'); + } + virtual void print() { + printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime, + test_id.getCharArray()); + printf("makefile_text=%s\n", makefile_text.getCharArray()); + } + CompilationTask(Text* lines, int offset) :Task(lines, offset) { + setTestId(lines->get(offset + 2)); + setMakefileText(lines->get(offset + 3)); + setState(Waiting); + kernels = 1; + } + + virtual void prepareWorkspace() { + String makeFilePath = String(id) + "/Makefile"; + File makeFileFile = File(makeFilePath, this->makefile_text); + String tests = userWorkspace + "/projects"; + String testPath = tests + "/" + test_id; + String copyCommand = "cp -r " + String::DQuotes(testPath + "/.") + " " + String::DQuotes(workspace); + system(copyCommand.getCharArray()); + } + virtual String getLaunchScriptText() { + String modules = userWorkspace + "/modules"; + String starterCall = modules + "/starter"; + String launcherCall = modules + "/launcher"; + return String::DQuotes(starterCall) + " " + + String::DQuotes(launcherCall) + " " + + String(maxtime) + " " + + String::DQuotes("") + " " + + "make -j -f Makefile"; + } + virtual void analyseResults() { + String binary = workspace + "/0"; + state = Utils::Exists(binary) ? Done : DoneWithErrors; } }; \ No newline at end of file diff --git a/Planner/File.h b/Planner/File.h index 06c92ffc..87f523b5 100644 --- a/Planner/File.h +++ b/Planner/File.h @@ -1,64 +1,66 @@ -#include "Text.h" #pragma once -class File { - FILE* ptr; -public: - File(String* name) { - ptr = fopen(name->getCharArray(), "r"); - } - File(const char * name) { - ptr = fopen(name, "r"); - } - File(const String& name, const String& text){ - ptr = fopen(name.getCharArray(), "w"); - fprintf(ptr, "%s\n", text.getCharArray()); - } - ~File() { - if (ptr != NULL) { - fclose(ptr); - ptr = NULL; - } - } - Text* readLines(){ - Text* lines = new Text(); - int c; - String * line = NULL; - bool lineStarted = false; - do { - c = fgetc(ptr); - if (lineStarted){ - switch (c) { - case '\r': - break; - case '\n': - case EOF: - lines->add(line); - line = NULL; - lineStarted = false; - break; - default: - line->addChar((char)c); - break; - } - }else { - switch (c){ - case '\r': - break; - case '\n': - line = new String(); - lines->add(line); - line = NULL; - break; - case EOF: - break; - default: - line = new String(); - line->addChar((char)c); - lineStarted = true; - break; - } - } - }while (c!=EOF); - return lines; - } + +#include "Text.h" +class File { + FILE* ptr; +public: + File(String* name) { + ptr = fopen(name->getCharArray(), "r"); + } + File(const char* name) { + ptr = fopen(name, "r"); + } + File(const String& name, const String& text) { + ptr = fopen(name.getCharArray(), "w"); + fprintf(ptr, "%s\n", text.getCharArray()); + } + ~File() { + if (ptr != NULL) { + fclose(ptr); + ptr = NULL; + } + } + Text* readLines() { + Text* lines = new Text(); + int c; + String* line = NULL; + bool lineStarted = false; + do { + c = fgetc(ptr); + if (lineStarted) { + switch (c) { + case '\r': + break; + case '\n': + case EOF: + lines->add(line); + line = NULL; + lineStarted = false; + break; + default: + line->addChar((char)c); + break; + } + } + else { + switch (c) { + case '\r': + break; + case '\n': + line = new String(); + lines->add(line); + line = NULL; + break; + case EOF: + break; + default: + line = new String(); + line->addChar((char)c); + lineStarted = true; + break; + } + } + } while (c != EOF); + return lines; + } }; \ No newline at end of file diff --git a/Planner/Global.h b/Planner/Global.h index 965e4836..a3ce0faf 100644 --- a/Planner/Global.h +++ b/Planner/Global.h @@ -1,13 +1,10 @@ +#pragma once + #include "String.h" -#pragma once + String userWorkspace; -#pragma once String packageWorkspace; -#pragma once int maxKernels; -#pragma once int busyKernels; -#pragma once int freeKernels; -#pragma once -String dvm_drv; \ No newline at end of file +String dvm_drv; diff --git a/Planner/RunSupervisor.h b/Planner/RunSupervisor.h index e1105615..64dcd9ee 100644 --- a/Planner/RunSupervisor.h +++ b/Planner/RunSupervisor.h @@ -1,31 +1,33 @@ +#pragma once + #include "CompilationSupervisor.h" #include "RunTask.h" -#pragma once -class RunSupervisor: public Supervisor { -public: - RunSupervisor(CompilationSupervisor * compilationSupervisor){ - this->init("runTasks", 8); + +class RunSupervisor : public Supervisor { +public: + RunSupervisor(CompilationSupervisor* compilationSupervisor) { + this->init("runTasks", 8); //проверить отмененные задачи. - for (long i=0; i< this->length; ++i){ - RunTask * task = this->get(i); - CompilationTask * parent = compilationSupervisor->getTaskById( task->getTestCompilationTaskId()); - task->setState((parent->getState()==Done)?Waiting:Canceled); - task->setParent(parent); + for (long i = 0; i < this->length; ++i) { + RunTask* task = this->get(i); + CompilationTask* parent = compilationSupervisor->getTaskById(task->getTestCompilationTaskId()); + task->setState((parent->getState() == Done) ? Waiting : Canceled); + task->setParent(parent); printf("id=%ld; parent_id = %ld; state=%s\n", - task->getId(), - task->getParent()->getId(), - task->printState().getCharArray()); - } + task->getId(), + task->getParent()->getId(), + task->printState().getCharArray()); + } } - virtual String getStatePrefix(){ + virtual String getStatePrefix() { return String("Running"); } - virtual void Finalize(){ + virtual void Finalize() { this->state = Archivation; saveState(); printf("Archivation started\n"); - Utils::ZipFolder(String("./"),String("archive.zip")); + Utils::ZipFolder(String("./"), String("archive.zip")); printf("Archivation ended\n"); - + } -}; \ No newline at end of file +}; diff --git a/Planner/RunTask.h b/Planner/RunTask.h index 1120c231..e1def5be 100644 --- a/Planner/RunTask.h +++ b/Planner/RunTask.h @@ -1,94 +1,96 @@ +#pragma once + #include "CompilationTask.h" -#pragma once -class RunTask : public Task { - long testcompilationtask_id; - String binary_name; - String matrix; - String environments; - String usr_par; - String args; - CompilationTask* parent; -public: - virtual void print(){ - printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n", - id, - maxtime, - testcompilationtask_id, - matrix.getCharArray(), - environments.getCharArray(), - usr_par.getCharArray(), - args.getCharArray(), - kernels - ); - } - int setKernels(String * kernels_s){ + +class RunTask : public Task { + long testcompilationtask_id; + String binary_name; + String matrix; + String environments; + String usr_par; + String args; + CompilationTask* parent; +public: + virtual void print() { + printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n", + id, + maxtime, + testcompilationtask_id, + matrix.getCharArray(), + environments.getCharArray(), + usr_par.getCharArray(), + args.getCharArray(), + kernels + ); + } + int setKernels(String* kernels_s) { return kernels = atoi(kernels_s->getCharArray()); - } - long setTestCompilationTaskId(String * id_s){ - return testcompilationtask_id=strtol(id_s->getCharArray(), NULL, 10); - } - long getTestCompilationTaskId(){ - return testcompilationtask_id; - } - void setMatrix(String * matrix_in){ - matrix= String (matrix_in->getCharArray()); - } - void setEnvironments(String * environments_in){ - environments= String(environments_in->getCharArray()); - } - void setUsrPar(String * usr_par_in){ - usr_par= String(usr_par_in->getCharArray(),'|'); - } - void setArgs(String * args_in){ - args= String(args_in->getCharArray()); } - void setParent(CompilationTask * parent_in){ - parent = parent_in; - binary_name = "spf_"+ String(id)+"_"+matrix.Replace(' ','_'); - } - CompilationTask * getParent(){ - return parent; - } - RunTask(Text * lines, int offset):Task(lines,offset) { - setTestCompilationTaskId(lines->get(offset+2)); - setMatrix(lines->get(offset+3)); - setEnvironments(lines->get(offset+4)); - setUsrPar(lines->get(offset+5)); - setArgs(lines->get(offset+6)); - setKernels(lines->get(offset+7)); - } - - virtual String getLaunchScriptText(){ - String modules = userWorkspace+"/modules"; - String starterCall = modules+"/starter"; - String launcherCall = modules+"/launcher"; + long setTestCompilationTaskId(String* id_s) { + return testcompilationtask_id = strtol(id_s->getCharArray(), NULL, 10); + } + long getTestCompilationTaskId() { + return testcompilationtask_id; + } + void setMatrix(String* matrix_in) { + matrix = String(matrix_in->getCharArray()); + } + void setEnvironments(String* environments_in) { + environments = String(environments_in->getCharArray()); + } + void setUsrPar(String* usr_par_in) { + usr_par = String(usr_par_in->getCharArray(), '|'); + } + void setArgs(String* args_in) { + args = String(args_in->getCharArray()); + } + void setParent(CompilationTask* parent_in) { + parent = parent_in; + binary_name = "spf_" + String(id) + "_" + matrix.Replace(' ', '_'); + } + CompilationTask* getParent() { + return parent; + } + RunTask(Text* lines, int offset) :Task(lines, offset) { + setTestCompilationTaskId(lines->get(offset + 2)); + setMatrix(lines->get(offset + 3)); + setEnvironments(lines->get(offset + 4)); + setUsrPar(lines->get(offset + 5)); + setArgs(lines->get(offset + 6)); + setKernels(lines->get(offset + 7)); + } + + virtual String getLaunchScriptText() { + String modules = userWorkspace + "/modules"; + String starterCall = modules + "/starter"; + String launcherCall = modules + "/launcher"; //- - String dvm_start = String::DQuotes(dvm_drv) + " run "; - if (!matrix.isEmpty()) - dvm_start = dvm_start+matrix + " "; - dvm_start = dvm_start+ String::DQuotes("./" + binary_name); - if (!args.isEmpty()) - dvm_start = dvm_start+ " " + args; - return String::DQuotes(starterCall)+" "+ - String::DQuotes(launcherCall)+" "+ - String(maxtime)+" "+ - String::DQuotes("killall -SIGKILL " + binary_name)+" "+ - dvm_start; - } - virtual void prepareWorkspace(){ - String binary_src = parent->getWorkspace()+"/0"; - String binary_dst = workspace+"/"+binary_name; - Utils::Copy(binary_src, binary_dst); - if (!usr_par.isEmpty()){ - String parPath = String(id)+"/usr.par"; - File parFile = File(parPath, usr_par); - } - } - virtual String getStartCommand(){ - String res = workspace+"/run"; - if (!environments.isEmpty()) - res = environments+" "+res; - printf("START %ld: %s\n", id, res.getCharArray()); - return res; + String dvm_start = String::DQuotes(dvm_drv) + " run "; + if (!matrix.isEmpty()) + dvm_start = dvm_start + matrix + " "; + dvm_start = dvm_start + String::DQuotes("./" + binary_name); + if (!args.isEmpty()) + dvm_start = dvm_start + " " + args; + return String::DQuotes(starterCall) + " " + + String::DQuotes(launcherCall) + " " + + String(maxtime) + " " + + String::DQuotes("killall -SIGKILL " + binary_name) + " " + + dvm_start; } -}; \ No newline at end of file + virtual void prepareWorkspace() { + String binary_src = parent->getWorkspace() + "/0"; + String binary_dst = workspace + "/" + binary_name; + Utils::Copy(binary_src, binary_dst); + if (!usr_par.isEmpty()) { + String parPath = String(id) + "/usr.par"; + File parFile = File(parPath, usr_par); + } + } + virtual String getStartCommand() { + String res = workspace + "/run"; + if (!environments.isEmpty()) + res = environments + " " + res; + printf("START %ld: %s\n", id, res.getCharArray()); + return res; + } +}; diff --git a/Planner/String.h b/Planner/String.h index ed85121d..c449331c 100644 --- a/Planner/String.h +++ b/Planner/String.h @@ -1,220 +1,223 @@ +#pragma once + #include #include #include #include -#pragma once -class String { - friend String operator+(const String& a, const String& b); - long length; - char* body; -public: - String() { - length = 0; - body = new char[1]; - body[0] = '\0'; - } - - String(const char* s) { - length = 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); - body = new char[length + 1]; - for (long i = 0; i < length; ++i){ - body[i] = (s[i]==ps)? '\n': s[i]; - } - body[length]='\0'; - } - ~String() { - if (body != NULL) { - delete[] body; - } - } - void println() const{ - printf("[%s]\n", body); - } - void addChar(char c) { - char* buf = new char[length + 2]; - for (long i = 0; i < length; ++i) { - buf[i] = body[i]; - } - buf[length] = c; - - length++; + +class String { + friend String operator+(const String& a, const String& b); + long length; + char* body; +public: + String() { + length = 0; + body = new char[1]; + body[0] = '\0'; + } + + String(const char* s) { + length = 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); + body = new char[length + 1]; + for (long i = 0; i < length; ++i) { + body[i] = (s[i] == ps) ? '\n' : s[i]; + } + body[length] = '\0'; + } + ~String() { + if (body != NULL) { + delete[] body; + } + } + void println() const { + printf("[%s]\n", body); + } + void addChar(char c) { + char* buf = new char[length + 2]; + for (long i = 0; i < length; ++i) { + buf[i] = body[i]; + } + buf[length] = c; + + length++; //-- - buf[length] = '\0'; - delete[] body; - body = buf; - buf = NULL; - } - char * getCharArray() const{ - return body; - } - - - String (int s){ - length = 0; - body = new char[1]; - body[0] = '\0'; - if (s>=0){ - int s_ = s; - int size = 1; - while (s_>=10){ - s_ = s_/10; - size++; - } - length = size; - body = new char [size+1]; - sprintf(body, "%d", s); - } - } - String (long s){ - length = 0; - body = new char[1]; - body[0] = '\0'; - if (s>=0){ - long s_ = s; - long size = 1; - while (s_>=10){ - s_ = s_/10; - size++; - } - length = size; - body = new char [size+1]; - sprintf(body, "%ld", s); - } - } - - const String& operator=(const String& s){ - if (body != NULL) - delete[] body; - length = s.length; - body = new char[length+1]; - for (long i=0; i= 0) { + int s_ = s; + int size = 1; + while (s_ >= 10) { + s_ = s_ / 10; + size++; + } + length = size; + body = new char[size + 1]; + sprintf(body, "%d", s); + } + } + String(long s) { + length = 0; + body = new char[1]; + body[0] = '\0'; + if (s >= 0) { + long s_ = s; + long size = 1; + while (s_ >= 10) { + s_ = s_ / 10; + size++; + } + length = size; + body = new char[size + 1]; + sprintf(body, "%ld", s); + } + } + + const String& operator=(const String& s) { + if (body != NULL) + delete[] body; + length = s.length; + body = new char[length + 1]; + for (long i = 0; i < length; ++i) + body[i] = s.body[i]; + body[length] = '\0'; + return *this; + } + + static String DQuotes(const String& s) { String res; - for (long i=0; ilength) return false; - long k=0; - long start=-1; - for (long i=0; i length) return false; + long k = 0; + long start = -1; + for (long i = 0; i < length; ++i) { + if (search_on) { + if (k < s.length) { + if (body[i] == s.body[k]) { + k++; + } + else { //обрыв поиска. - search_on =false; - k=0; - start=-1; - } - } - else { + search_on = false; + k = 0; + start = -1; + } + } + else { //printf("starts with %ld", start); - return true; - } - }else { - if (body[i]== s.body[0]){ - k=1; - start=i; - search_on=true; + return true; + } + } + else { + if (body[i] == s.body[0]) { + k = 1; + start = i; + search_on = true; //printf("search started %d\n", start); - } - } - } - if (search_on) { + } + } + } + if (search_on) { //printf("starts with %ld\n", start); - res=true; - } - return res; - } - String toUpper(){ + res = true; + } + return res; + } + String toUpper() { String res = String(this->getCharArray()); - for (long i=0; i -#include - -int main(void) { - FILE * f; - int c; - - if ( ! ( f = fopen("file.txt", "r") ) ) - return -1; - - while ( ( c = fgetc(f) ) != EOF ) - putchar( isupper(c) ? tolower(c) : toupper(c) ); - - return ( fclose(f) ); -} - */ - -}; + long getLength() { + return length; + } + bool operator==(const String& s) const { + if (length != s.length) return false; + for (long i = 0; i < length; ++i) { + if (body[i] != s.body[i]) return false; + } + return true; + } + /* регистр. + #include + #include + + int main(void) { + FILE * f; + int c; + + if ( ! ( f = fopen("file.txt", "r") ) ) + return -1; + + while ( ( c = fgetc(f) ) != EOF ) + putchar( isupper(c) ? tolower(c) : toupper(c) ); + + return ( fclose(f) ); + } + */ +}; -String operator+(const String& a, const String& b){ - String res = String(); - res.length =a.length+b.length; - res.body = new char[res.length+1]; - for (long i=0; i #include "File.h" #include "Task.h" -#include -#pragma once enum SupervisorState { WorkspacesCreation, //0 Preparation, //1 @@ -10,120 +11,121 @@ enum SupervisorState { Archivation, //3 End //4 }; -#pragma once + template -class Supervisor : public Array { +class Supervisor : public Array { protected: - SupervisorState state; -public: - virtual String getStatePrefix(){ + SupervisorState state; +public: + virtual String getStatePrefix() { return String(""); } - String printState(){ - switch(state){ - case WorkspacesCreation: - return String("WorkspacesCreation"); - case Preparation: - return String("Preparation"); - case Execution: - return String("Execution"); - case Archivation: - return String("Archivation"); - case End: - return String("End"); - default: - return "?"; - } + String printState() { + switch (state) { + case WorkspacesCreation: + return String("WorkspacesCreation"); + case Preparation: + return String("Preparation"); + case Execution: + return String("Execution"); + case Archivation: + return String("Archivation"); + case End: + return String("End"); + default: + return "?"; + } } //- - void print(){ - for (long i=0; i< this->length; ++i) - this->elements[i]->print(); - } - void init(const char * fileName, int recordSize){ - state = WorkspacesCreation; - File * packedTasks = new File(fileName); - Text * lines = packedTasks->readLines(); - this->length = lines->getLength()/recordSize; - this->elements = new T* [this->length]; - int offset=0; - for (int i=0; i< this->length; ++i){ - this->elements[i]= new T(lines, offset); - offset+=recordSize; - } - delete packedTasks; - delete lines; - } - void Do(){ + void print() { + for (long i = 0; i < this->length; ++i) + this->elements[i]->print(); + } + void init(const char* fileName, int recordSize) { + state = WorkspacesCreation; + File* packedTasks = new File(fileName); + Text* lines = packedTasks->readLines(); + this->length = lines->getLength() / recordSize; + this->elements = new T * [this->length]; + int offset = 0; + for (int i = 0; i < this->length; ++i) { + this->elements[i] = new T(lines, offset); + offset += recordSize; + } + delete packedTasks; + delete lines; + } + void Do() { saveState(); - long activeCount=0; + long activeCount = 0; //todo обязательно убрать отладочную печать. - printf("tasks count = %ld\n", this->length); - while (this->state!= End){ -// printf("state=%d\n", this->state); -// printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); - activeCount=0; - for (long i=0; ilength; ++i){ - T * task = this->elements[i]; - switch (this->state){ - case WorkspacesCreation: - if (task->getState()==Waiting){ - activeCount++; - task->createWorkspace(); - task->setState(WorkspaceCreated); - } - break; - case Preparation: - if (task->getState()==WorkspaceCreated){ - activeCount++; - task->prepareWorkspace(); - task->createLaunchScript(); - task->setState(WorkspaceReady); - } - break; - case Execution: - if (task->getState()==WorkspaceReady){ - activeCount++; - task->Start(); - }else if (task->getState()==Running){ - activeCount++; - task->Check(); - } - break; - default: -// printf("id = %ld; state = %d\n", task->getId(), task->getState()); - break; - } - } -// 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; - } - } - Utils::Sleep(2); - } - } - virtual void Finalize(){} - void saveState(){ + printf("tasks count = %ld\n", this->length); + while (this->state != End) { + // printf("state=%d\n", this->state); + // printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); + activeCount = 0; + for (long i = 0; i < this->length; ++i) { + T* task = this->elements[i]; + switch (this->state) { + case WorkspacesCreation: + if (task->getState() == Waiting) { + activeCount++; + task->createWorkspace(); + task->setState(WorkspaceCreated); + } + break; + case Preparation: + if (task->getState() == WorkspaceCreated) { + activeCount++; + task->prepareWorkspace(); + task->createLaunchScript(); + task->setState(WorkspaceReady); + } + break; + case Execution: + if (task->getState() == WorkspaceReady) { + activeCount++; + task->Start(); + } + else if (task->getState() == Running) { + activeCount++; + task->Check(); + } + break; + default: + // printf("id = %ld; state = %d\n", task->getId(), task->getState()); + break; + } + } + // 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; + } + } + Utils::Sleep(2); + } + } + virtual void Finalize() {} + void saveState() { Utils::Sleep(1); //чтобы не было одинаковых по дате файлов. - String stateFile = packageWorkspace+"/state/"+getStatePrefix()+printState(); + String stateFile = packageWorkspace + "/state/" + getStatePrefix() + printState(); //printf("stateFile=<%s>\n", stateFile.getCharArray()); - File(stateFile, Utils::getDate()); + File(stateFile, Utils::getDate()); } }; \ No newline at end of file diff --git a/Planner/Task.h b/Planner/Task.h index 19891b5d..47ee4d14 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -1,9 +1,10 @@ +#pragma once + #include "File.h" #include "Utils.h" #include "Global.h" - -#pragma once -enum TaskState { + +enum TaskState { Inactive, //0 Waiting, //1 WorkspaceCreated, //2 @@ -24,140 +25,141 @@ enum TaskState { NoSuchTask, //17 FailedToQueue, //18 AbortingByUser //19 -}; -#pragma once -enum TestType{ +}; + +enum TestType { Default, //0 Correctness, //1 Performance, //2 }; - -#pragma once -class Task { -protected: - long id; - int maxtime; + +class Task { +protected: + long id; + int maxtime; int kernels; //получение зависит от типа задачи. - String workspace; - TaskState state; -public: - String printState(){ - switch(state){ - case Inactive: - return String("Inactive"); - case Waiting: - return String("Waiting"); - case WorkspaceCreated: - return String("WorkspaceCreated"); - case WorkspaceReady: - return String("WorkspaceReady"); - case Running: - return String("Running"); - case Canceled: - return String("Canceled"); - case Finished: - return String("Finished"); - case FinishedAbortedByTimeout: - return String("FinishedAbortedByTimeout"); - case FinishedAbortedByUser: - return String("FinishedAbortedByUser"); - case Done: - return String("Done"); - case DoneWithErrors: - return String("DoneWithErrors"); - case AbortedByTimeout: - return String("AbortedByTimeout"); - case AbortedByUser: - return String("AbortedByUser"); - case Crushed: - return String("Crushed"); - case WrongTestFormat: - return String("WrongTestFormat"); - case InternalError: - return String("InternalError"); - case Queued: - return String("Queued"); - case NoSuchTask: - return String("NoSuchTask"); - case FailedToQueue: - return String("FailedToQueue"); - case AbortingByUser: - return String("AbortingByUser"); - default: - return "?"; - } - } - //-------------->> - long getId(){return id;} - long setId(String * id_s){ - return id=strtol(id_s->getCharArray(), NULL, 10); - } - int getMaxtime(){return maxtime;} - int setMaxtime(String * maxtime_s){ - return maxtime=atoi(maxtime_s->getCharArray()); - } - const String& getWorkspace(){return workspace;} - TaskState getState(){return state;} - TaskState setState(TaskState state_in){return state=state_in;} - Task(Text * lines, int offset){ - setId(lines->get(offset)); - setMaxtime(lines->get(offset+1)); - workspace = packageWorkspace+"/"+String(id); - } - virtual void print()=0; - //- - virtual void prepareWorkspace(){} - virtual String getLaunchScriptText()=0; - virtual String getStartCommand(){ - return workspace+"/run"; - } - - void createWorkspace(){ - Utils::Mkdir(workspace); - } - void createLaunchScript(){ - String launchScriptPath = workspace+"/run"; - String launchScriptText = - String("cd ")+String::DQuotes(workspace)+"\n"+ - getLaunchScriptText(); - File launchScriptFile = File(launchScriptPath, launchScriptText); - Utils::Chmod(launchScriptPath); - } - virtual void Start(){ - - if (kernels<=freeKernels){ - system(getStartCommand().getCharArray()); - state=Running; - //- - busyKernels= Utils::min(busyKernels+kernels, maxKernels); - freeKernels= Utils::max(0, maxKernels-busyKernels); - //- - } - } - virtual void analyseResults(){ - state=Finished; + String workspace; + TaskState state; +public: + String printState() { + switch (state) { + case Inactive: + return String("Inactive"); + case Waiting: + return String("Waiting"); + case WorkspaceCreated: + return String("WorkspaceCreated"); + case WorkspaceReady: + return String("WorkspaceReady"); + case Running: + return String("Running"); + case Canceled: + return String("Canceled"); + case Finished: + return String("Finished"); + case FinishedAbortedByTimeout: + return String("FinishedAbortedByTimeout"); + case FinishedAbortedByUser: + return String("FinishedAbortedByUser"); + case Done: + return String("Done"); + case DoneWithErrors: + return String("DoneWithErrors"); + case AbortedByTimeout: + return String("AbortedByTimeout"); + case AbortedByUser: + return String("AbortedByUser"); + case Crushed: + return String("Crushed"); + case WrongTestFormat: + return String("WrongTestFormat"); + case InternalError: + return String("InternalError"); + case Queued: + return String("Queued"); + case NoSuchTask: + return String("NoSuchTask"); + case FailedToQueue: + return String("FailedToQueue"); + case AbortingByUser: + return String("AbortingByUser"); + default: + return "?"; + } } - virtual void Check(){ - if (Utils::Exists(workspace+"/DONE")){ - analyseResults(); - }else { - if (Utils::Exists(workspace+"/TIMEOUT")){ - state=AbortedByTimeout; - //todo определить по интервалу времени на всякий случай. - }else if (Utils::Exists(workspace+"/INTERRUPT")){ - state=AbortedByUser; - } - } - if (state!=Running){ + //-------------->> + long getId() { return id; } + long setId(String* id_s) { + return id = strtol(id_s->getCharArray(), NULL, 10); + } + int getMaxtime() { return maxtime; } + int setMaxtime(String* maxtime_s) { + return maxtime = atoi(maxtime_s->getCharArray()); + } + const String& getWorkspace() { return workspace; } + TaskState getState() { return state; } + TaskState setState(TaskState state_in) { return state = state_in; } + Task(Text* lines, int offset) { + setId(lines->get(offset)); + setMaxtime(lines->get(offset + 1)); + workspace = packageWorkspace + "/" + String(id); + } + virtual void print() = 0; + //- + virtual void prepareWorkspace() {} + virtual String getLaunchScriptText() = 0; + virtual String getStartCommand() { + return workspace + "/run"; + } + + void createWorkspace() { + Utils::Mkdir(workspace); + } + void createLaunchScript() { + String launchScriptPath = workspace + "/run"; + String launchScriptText = + String("cd ") + String::DQuotes(workspace) + "\n" + + getLaunchScriptText(); + File launchScriptFile = File(launchScriptPath, launchScriptText); + Utils::Chmod(launchScriptPath); + } + virtual void Start() { + + if (kernels <= freeKernels) { + system(getStartCommand().getCharArray()); + state = Running; //- - busyKernels= Utils::min(busyKernels-kernels, maxKernels); - freeKernels= Utils::max(0, maxKernels-busyKernels); + busyKernels = Utils::min(busyKernels + kernels, maxKernels); + freeKernels = Utils::max(0, maxKernels - busyKernels); + //- + } + } + virtual void analyseResults() { + state = Finished; + } + virtual void Check() { + if (Utils::Exists(workspace + "/DONE")) { + analyseResults(); + } + else { + if (Utils::Exists(workspace + "/TIMEOUT")) { + state = AbortedByTimeout; + //todo определить по интервалу времени на всякий случай. + } + else if (Utils::Exists(workspace + "/INTERRUPT")) { + state = AbortedByUser; + } + } + if (state != Running) { + //- + busyKernels = Utils::min(busyKernels - kernels, maxKernels); + freeKernels = Utils::max(0, maxKernels - busyKernels); //- saveState(); //не нужно. только для отладки. анализ будет делаться архивом. - } - } - virtual void saveState(){ - String stateFile = workspace+"/TaskState"; - File(stateFile, printState()); - } -}; + } + } + virtual void saveState() { + String stateFile = workspace + "/TaskState"; + File(stateFile, printState()); + } +}; diff --git a/Planner/Text.h b/Planner/Text.h index d1a57075..6f216a67 100644 --- a/Planner/Text.h +++ b/Planner/Text.h @@ -1,25 +1,27 @@ +#pragma once + #include "String.h" #include "Array.h" -#pragma once -class Text: public Array { - public: - void Print(){ + +class Text : public Array { +public: + void Print() { printf("text length=%ld\n", length); - - for (long i=0; igetCharArray()); - // elements[i]->println(); + + for (long i = 0; i < length; ++i) { + printf("i=%ld; [%s]\n", i, elements[i]->getCharArray()); + // elements[i]->println(); + } + } + bool hasMatch(const String& s) { + + for (long i = 0; i < length; ++i) { + if (s.contains(*elements[i])) { + //printf("match: [%s]\n", elements[i]->getCharArray()); + return true; + } } - } - bool hasMatch(const String& s){ - - for (long i=0; igetCharArray()); - return true; - } - } //printf("no matches for [%s]\n", s.getCharArray()); return false; } -}; \ No newline at end of file +}; diff --git a/Planner/Utils.h b/Planner/Utils.h index eb077069..1fc0c7de 100644 --- a/Planner/Utils.h +++ b/Planner/Utils.h @@ -1,49 +1,51 @@ +#pragma once + #include #include #include #include #include #include "String.h" -#pragma once + class Utils { public: - static int max(int a, int b){ - return (a > b)? a:b; - } - static int min(int a, int b){ - return (a > b)? b:a; + static int max(int a, int b) { + return (a > b) ? a : b; } - static void Mkdir(const String& path){ + static int min(int a, int b) { + return (a > b) ? b : a; + } + static void Mkdir(const String& path) { mkdir(path.getCharArray(), 0777); - } + } //https://stackoverflow.com/questions/4568681/using-chmod-in-a-c-program - static void Chmod(const String& path){ - String command = "chmod 777 "+String::DQuotes(path); + 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); + static bool Exists(const String& path) { + struct stat buffer; + return (stat(path.getCharArray(), &buffer) == 0); } - static void Sleep(int s){ - usleep(s* 1000000); - } - static void Copy(const String& src, const String& dst){ - String command = "cp "+String::DQuotes(src)+" "+String::DQuotes(dst); - system(command.getCharArray()); + static void Sleep(int s) { + usleep(s * 1000000); } - static long getAbsoluteTime(){ - return time (NULL); - } - static String getDate(){ - long int ttime; - ttime = time (NULL); - String res(ctime (&ttime)); - return res; - } - static void ZipFolder(const String& src, const String& dst){ - String command = "zip -r "+String::DQuotes(dst)+" "+String::DQuotes(src); + static void Copy(const String& src, const String& dst) { + String command = "cp " + String::DQuotes(src) + " " + String::DQuotes(dst); system(command.getCharArray()); } -}; \ No newline at end of file + static long getAbsoluteTime() { + return time(NULL); + } + static String getDate() { + long int ttime; + ttime = time(NULL); + String res(ctime(&ttime)); + return res; + } + static void ZipFolder(const String& src, const String& dst) { + String command = "zip -r " + String::DQuotes(dst) + " " + String::DQuotes(src); + system(command.getCharArray()); + } +}; From c4b8e2dd7a32df9fd556461dca4b3bedcb938b9d Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 13:41:29 +0300 Subject: [PATCH 2/8] improved Array: use std::vector --- Planner/Array.h | 41 ++++++++++++++------------------- Planner/CompilationSupervisor.h | 2 +- Planner/CompilationTask.h | 3 ++- Planner/RunSupervisor.h | 2 +- Planner/RunTask.h | 2 +- Planner/Supervisor.h | 19 +++++++-------- Planner/Task.h | 2 +- Planner/Text.h | 16 +++++++------ 8 files changed, 42 insertions(+), 45 deletions(-) diff --git a/Planner/Array.h b/Planner/Array.h index c9fe79e8..cc40d539 100644 --- a/Planner/Array.h +++ b/Planner/Array.h @@ -3,42 +3,35 @@ #include #include #include +#include template class Array { -protected: - long length; - T** elements; +private: + std::vector elements; + public: - Array() { - length = 0; - elements = NULL; - } + Array() { } + virtual ~Array() { - if (elements != NULL) { - for (long i = 0; i < length; ++i) - delete elements[i]; - delete[] elements; - } + for (auto& elem : elements) + delete elem; + elements.clear(); } + void add(T* new_line) { - T** buf = new T * [length + 1]; - for (long i = 0; i < length; ++i) { - buf[i] = elements[i]; - } - buf[length] = new_line; - length++; - delete[] elements; - elements = buf; - buf = NULL; + elements.push_back(new_line); } - long getLength() { - return length; + + long getLength() const { + return elements.size(); } + T* get(long i) { return elements[i]; } - T** getElements() { + + const std::vector& getElements() const { return elements; } }; \ No newline at end of file diff --git a/Planner/CompilationSupervisor.h b/Planner/CompilationSupervisor.h index 1ebd3f72..b757451e 100644 --- a/Planner/CompilationSupervisor.h +++ b/Planner/CompilationSupervisor.h @@ -9,7 +9,7 @@ public: this->init("compilationTasks", 4); } CompilationTask* getTaskById(long task_id) { - for (long i = 0; i < length; ++i) { + for (long i = 0; i < getLength(); ++i) { CompilationTask* task = get(i); if (task->getId() == task_id) return task; diff --git a/Planner/CompilationTask.h b/Planner/CompilationTask.h index c10b4547..18059a6c 100644 --- a/Planner/CompilationTask.h +++ b/Planner/CompilationTask.h @@ -13,11 +13,12 @@ public: void setMakefileText(String* makefile_text_in) { makefile_text = String(makefile_text_in->getCharArray(), '|'); } - virtual void print() { + virtual void print() const { printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime, test_id.getCharArray()); printf("makefile_text=%s\n", makefile_text.getCharArray()); } + CompilationTask(Text* lines, int offset) :Task(lines, offset) { setTestId(lines->get(offset + 2)); setMakefileText(lines->get(offset + 3)); diff --git a/Planner/RunSupervisor.h b/Planner/RunSupervisor.h index 64dcd9ee..de5c5e8f 100644 --- a/Planner/RunSupervisor.h +++ b/Planner/RunSupervisor.h @@ -8,7 +8,7 @@ public: RunSupervisor(CompilationSupervisor* compilationSupervisor) { this->init("runTasks", 8); //проверить отмененные задачи. - for (long i = 0; i < this->length; ++i) { + for (long i = 0; i < getLength(); ++i) { RunTask* task = this->get(i); CompilationTask* parent = compilationSupervisor->getTaskById(task->getTestCompilationTaskId()); task->setState((parent->getState() == Done) ? Waiting : Canceled); diff --git a/Planner/RunTask.h b/Planner/RunTask.h index e1def5be..622fc876 100644 --- a/Planner/RunTask.h +++ b/Planner/RunTask.h @@ -11,7 +11,7 @@ class RunTask : public Task { String args; CompilationTask* parent; public: - virtual void print() { + virtual void print() const { printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n", id, maxtime, diff --git a/Planner/Supervisor.h b/Planner/Supervisor.h index 504adaa5..d5b11760 100644 --- a/Planner/Supervisor.h +++ b/Planner/Supervisor.h @@ -38,18 +38,19 @@ public: } //- void print() { - for (long i = 0; i < this->length; ++i) - this->elements[i]->print(); + for (auto& elem : this->getElements()) + elem->print(); } + void init(const char* fileName, int recordSize) { state = WorkspacesCreation; File* packedTasks = new File(fileName); Text* lines = packedTasks->readLines(); - this->length = lines->getLength() / recordSize; - this->elements = new T * [this->length]; + + const long length = lines->getLength() / recordSize; int offset = 0; - for (int i = 0; i < this->length; ++i) { - this->elements[i] = new T(lines, offset); + for (int i = 0; i < length; ++i) { + this->add(new T(lines, offset)); offset += recordSize; } delete packedTasks; @@ -59,13 +60,13 @@ public: saveState(); long activeCount = 0; //todo обязательно убрать отладочную печать. - printf("tasks count = %ld\n", this->length); + printf("tasks count = %ld\n", this->getLength()); while (this->state != End) { // printf("state=%d\n", this->state); // printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); activeCount = 0; - for (long i = 0; i < this->length; ++i) { - T* task = this->elements[i]; + for (long i = 0; i < this->getLength(); ++i) { + T* task = this->get(i); switch (this->state) { case WorkspacesCreation: if (task->getState() == Waiting) { diff --git a/Planner/Task.h b/Planner/Task.h index 47ee4d14..0cb161c3 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -104,7 +104,7 @@ public: setMaxtime(lines->get(offset + 1)); workspace = packageWorkspace + "/" + String(id); } - virtual void print() = 0; + virtual void print() const = 0; //- virtual void prepareWorkspace() {} virtual String getLaunchScriptText() = 0; diff --git a/Planner/Text.h b/Planner/Text.h index 6f216a67..8f154458 100644 --- a/Planner/Text.h +++ b/Planner/Text.h @@ -5,18 +5,20 @@ class Text : public Array { public: - void Print() { - printf("text length=%ld\n", length); + void Print() const { + printf("text length=%ld\n", this->getLength()); - for (long i = 0; i < length; ++i) { - printf("i=%ld; [%s]\n", i, elements[i]->getCharArray()); + auto elems = this->getElements(); + for (size_t i = 0; i < elems.size(); ++i) { + printf("i=%ld; [%s]\n", i, elems[i]->getCharArray()); // elements[i]->println(); } } - bool hasMatch(const String& s) { - for (long i = 0; i < length; ++i) { - if (s.contains(*elements[i])) { + bool hasMatch(const String& s) const { + + for (auto& elem : this->getElements()) { + if (s.contains(*elem)) { //printf("match: [%s]\n", elements[i]->getCharArray()); return true; } From 0afbb32788b5a501d370cf4a47862bd1efbf44d7 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 15:31:50 +0300 Subject: [PATCH 3/8] added cross-platform --- Planner/Array.h | 2 +- Planner/Planner.cpp | 17 +++++++++-- Planner/String.h | 6 ++-- Planner/Supervisor.h | 72 ++++++++++++++++++++++++++++++-------------- Planner/Task.h | 22 +++++++++----- Planner/Text.h | 2 +- Planner/Utils.h | 24 ++++++++++++--- 7 files changed, 103 insertions(+), 42 deletions(-) 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; } From ed68c9b11ec7b37edd112c7f97dd7ce8d2e8b876 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 16:25:42 +0300 Subject: [PATCH 4/8] improved planner --- Planner/Planner.cpp | 2 +- Planner/Supervisor.h | 83 ++++++++++++++++++++++++++++++++++++++++++-- Planner/Task.h | 7 ++-- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/Planner/Planner.cpp b/Planner/Planner.cpp index eca4773a..0af9cbf0 100644 --- a/Planner/Planner.cpp +++ b/Planner/Planner.cpp @@ -36,6 +36,6 @@ int main(int argc, char ** argv) RunSupervisor * runSupervisor = new RunSupervisor(compilationSupervisor); printf("%ld\n", runSupervisor->getLength()); runSupervisor->print(); - runSupervisor->Do(); + runSupervisor->DoWithSchedule(maxKernels); return 0; } diff --git a/Planner/Supervisor.h b/Planner/Supervisor.h index 01043451..5183d1d1 100644 --- a/Planner/Supervisor.h +++ b/Planner/Supervisor.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include "File.h" @@ -131,20 +132,96 @@ public: void DoWithSchedule(int maxKernels) { saveState(); - map> sortedByKernelNeeds; + + // подготовка тестов + while (this->state != Execution) { + for (auto& task : this->getElements()) { + switch (this->state) { + case WorkspacesCreation: + if (task->getState() == Waiting) { + task->createWorkspace(); + task->setState(WorkspaceCreated); + } + break; + case Preparation: + if (task->getState() == WorkspaceCreated) { + task->prepareWorkspace(); + task->createLaunchScript(); + task->setState(WorkspaceReady); + } + break; + default: + //printf("id = %ld; state = %d\n", task->getId(), task->getState()); + break; + } + } + changeState(); + } + + map, std::greater> sortedByKernelNeeds; long activeTasks = 0; - for (auto& task : this->getElements()) { + 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); + printf("total tasks count = %ld, active task count %ld, maxkernels %d\n", this->getLength(), activeTasks, maxKernels); + + int busyKernels = 0; + set activeTaskSet; + bool ignoreCheck = true; while (activeTasks) { + vector emptyKeys; + //ставим задачи от больших к меньшему по ядрам + for (auto& elem : sortedByKernelNeeds) { + int freeKernels = maxKernels - busyKernels; + int kernelsNeeded = elem.first; + + while (kernelsNeeded <= freeKernels && elem.second.size()) { + T* task = elem.second.front(); + elem.second.pop(); + + activeTaskSet.insert(task); + task->Start(ignoreCheck); + printf("start task with %d kernels and id %ld\n", task->getKernels(), task->getId()); + + busyKernels += task->getKernels(); + freeKernels = maxKernels - busyKernels; + } + + if (elem.second.size() == 0) + emptyKeys.push_back(kernelsNeeded); + + //если ядер не осталось, то нет смысла дальше смотреть + if (freeKernels == 0) + break; + } + + // очищаем от пустых ключей + for (auto& empty : emptyKeys) + sortedByKernelNeeds.erase(empty); + + // проверяем нет ли завершившихся задач + for (auto it = activeTaskSet.begin(); it != activeTaskSet.end(); ) + { + T* task = *(it); + + if (task->Check()) { + it++; + activeTaskSet.erase(task); + activeTasks--; + busyKernels -= task->getKernels(); + printf(" done task with %d kernels and id %ld\n", task->getKernels(), task->getId()); + + continue; + } + it++; + } } changeState(); diff --git a/Planner/Task.h b/Planner/Task.h index 2e8b3be7..95c9671f 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -138,7 +138,8 @@ public: } } - virtual void Check() { + //return 'true' if done, 'false' - if running + virtual bool Check() { if (Utils::Exists(workspace + "/DONE")) analyseResults(); else { @@ -156,8 +157,10 @@ public: busyKernels = Utils::min(busyKernels - kernels, maxKernels); freeKernels = Utils::max(0, maxKernels - busyKernels); //- - saveState(); //не нужно. только для отладки. анализ будет делаться архивом. + //saveState(); //не нужно. только для отладки. анализ будет делаться архивом. } + + return (state != Running); } virtual void analyseResults() { From 8d5cf5fa4c4cf992a05955ee94f94b80d36f8627 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 16:26:36 +0300 Subject: [PATCH 5/8] improved planner --- Planner/Supervisor.h | 396 +++++++++++++++++++++---------------------- 1 file changed, 198 insertions(+), 198 deletions(-) diff --git a/Planner/Supervisor.h b/Planner/Supervisor.h index 5183d1d1..43a829ea 100644 --- a/Planner/Supervisor.h +++ b/Planner/Supervisor.h @@ -9,229 +9,229 @@ enum SupervisorState { WorkspacesCreation, //0 - Preparation, //1 - Execution, //2 + Preparation, //1 + Execution, //2 Archivation, //3 - End //4 + End //4 }; template class Supervisor : public Array { protected: - SupervisorState state; + SupervisorState state; public: - virtual String getStatePrefix() { - return String(""); - } - String printState() { - switch (state) { - case WorkspacesCreation: - return String("WorkspacesCreation"); - case Preparation: - return String("Preparation"); - case Execution: - return String("Execution"); - case Archivation: - return String("Archivation"); - case End: - return String("End"); - default: - return "?"; - } - } - //- - void print() { - for (auto& elem : this->getElements()) - elem->print(); - } + virtual String getStatePrefix() { + return String(""); + } + String printState() { + switch (state) { + case WorkspacesCreation: + return String("WorkspacesCreation"); + case Preparation: + return String("Preparation"); + case Execution: + return String("Execution"); + case Archivation: + return String("Archivation"); + case End: + return String("End"); + default: + return "?"; + } + } + //- + void print() { + for (auto& elem : this->getElements()) + elem->print(); + } - void init(const char* fileName, int recordSize) { - state = WorkspacesCreation; - File* packedTasks = new File(fileName); - Text* lines = packedTasks->readLines(); + void init(const char* fileName, int recordSize) { + state = WorkspacesCreation; + File* packedTasks = new File(fileName); + Text* lines = packedTasks->readLines(); - const long length = lines->getLength() / recordSize; - int offset = 0; - for (int i = 0; i < length; ++i) { - this->add(new T(lines, offset)); - offset += recordSize; - } - delete packedTasks; - delete lines; - } + const long length = lines->getLength() / recordSize; + int offset = 0; + for (int i = 0; i < length; ++i) { + this->add(new T(lines, offset)); + offset += recordSize; + } + 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 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; - //todo обязательно убрать отладочную печать. - printf("tasks count = %ld\n", this->getLength()); - while (this->state != End) { - // printf("state=%d\n", this->state); - // printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); - activeCount = 0; - for (long i = 0; i < this->getLength(); ++i) { - T* task = this->get(i); - switch (this->state) { - case WorkspacesCreation: - if (task->getState() == Waiting) { - activeCount++; - task->createWorkspace(); - task->setState(WorkspaceCreated); - } - break; - case Preparation: - if (task->getState() == WorkspaceCreated) { - activeCount++; - task->prepareWorkspace(); - task->createLaunchScript(); - task->setState(WorkspaceReady); - } - break; - case Execution: - if (task->getState() == WorkspaceReady) { - activeCount++; - task->Start(); - } - else if (task->getState() == Running) { - activeCount++; - task->Check(); - } - break; - default: - // printf("id = %ld; state = %d\n", task->getId(), task->getState()); - break; - } - } - // printf("active count = %d\n", activeCount); - if (activeCount == 0) - changeState(); - Utils::Sleep(2); - } - } + void Do() { + saveState(); + long activeCount = 0; + //todo обязательно убрать отладочную печать. + printf("tasks count = %ld\n", this->getLength()); + while (this->state != End) { + // printf("state=%d\n", this->state); + // printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); + activeCount = 0; + for (long i = 0; i < this->getLength(); ++i) { + T* task = this->get(i); + switch (this->state) { + case WorkspacesCreation: + if (task->getState() == Waiting) { + activeCount++; + task->createWorkspace(); + task->setState(WorkspaceCreated); + } + break; + case Preparation: + if (task->getState() == WorkspaceCreated) { + activeCount++; + task->prepareWorkspace(); + task->createLaunchScript(); + task->setState(WorkspaceReady); + } + break; + case Execution: + if (task->getState() == WorkspaceReady) { + activeCount++; + task->Start(); + } + else if (task->getState() == Running) { + activeCount++; + task->Check(); + } + break; + default: + // printf("id = %ld; state = %d\n", task->getId(), task->getState()); + break; + } + } + // printf("active count = %d\n", activeCount); + if (activeCount == 0) + changeState(); + Utils::Sleep(2); + } + } - void DoWithSchedule(int maxKernels) { - saveState(); + void DoWithSchedule(int maxKernels) { + saveState(); - // подготовка тестов - while (this->state != Execution) { - for (auto& task : this->getElements()) { - switch (this->state) { - case WorkspacesCreation: - if (task->getState() == Waiting) { - task->createWorkspace(); - task->setState(WorkspaceCreated); - } - break; - case Preparation: - if (task->getState() == WorkspaceCreated) { - task->prepareWorkspace(); - task->createLaunchScript(); - task->setState(WorkspaceReady); - } - break; - default: - //printf("id = %ld; state = %d\n", task->getId(), task->getState()); - break; - } - } - changeState(); - } + // подготовка тестов + while (this->state != Execution) { + for (auto& task : this->getElements()) { + switch (this->state) { + case WorkspacesCreation: + if (task->getState() == Waiting) { + task->createWorkspace(); + task->setState(WorkspaceCreated); + } + break; + case Preparation: + if (task->getState() == WorkspaceCreated) { + task->prepareWorkspace(); + task->createLaunchScript(); + task->setState(WorkspaceReady); + } + break; + default: + //printf("id = %ld; state = %d\n", task->getId(), task->getState()); + break; + } + } + changeState(); + } - map, std::greater> sortedByKernelNeeds; + map, std::greater> sortedByKernelNeeds; - long activeTasks = 0; - for (auto& task : this->getElements()) { - if (task->getState() == WorkspaceReady) { - activeTasks++; - sortedByKernelNeeds[task->getKernels()].push(task); - } - } + 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, maxkernels %d\n", this->getLength(), activeTasks, maxKernels); + printf("total tasks count = %ld, active task count %ld, maxkernels %d\n", this->getLength(), activeTasks, maxKernels); - int busyKernels = 0; - set activeTaskSet; - bool ignoreCheck = true; + int busyKernels = 0; + set activeTaskSet; + bool ignoreCheck = true; - while (activeTasks) { - vector emptyKeys; + while (activeTasks) { + vector emptyKeys; - //ставим задачи от больших к меньшему по ядрам - for (auto& elem : sortedByKernelNeeds) { - int freeKernels = maxKernels - busyKernels; - int kernelsNeeded = elem.first; - - while (kernelsNeeded <= freeKernels && elem.second.size()) { - T* task = elem.second.front(); - elem.second.pop(); + //ставим задачи от больших к меньшему по ядрам + for (auto& elem : sortedByKernelNeeds) { + int freeKernels = maxKernels - busyKernels; + int kernelsNeeded = elem.first; + + while (kernelsNeeded <= freeKernels && elem.second.size()) { + T* task = elem.second.front(); + elem.second.pop(); - activeTaskSet.insert(task); - task->Start(ignoreCheck); - printf("start task with %d kernels and id %ld\n", task->getKernels(), task->getId()); + activeTaskSet.insert(task); + task->Start(ignoreCheck); + printf("start task with %d kernels and id %ld\n", task->getKernels(), task->getId()); - busyKernels += task->getKernels(); - freeKernels = maxKernels - busyKernels; - } + busyKernels += task->getKernels(); + freeKernels = maxKernels - busyKernels; + } - if (elem.second.size() == 0) - emptyKeys.push_back(kernelsNeeded); + if (elem.second.size() == 0) + emptyKeys.push_back(kernelsNeeded); - //если ядер не осталось, то нет смысла дальше смотреть - if (freeKernels == 0) - break; - } + //если ядер не осталось, то нет смысла дальше смотреть + if (freeKernels == 0) + break; + } - // очищаем от пустых ключей - for (auto& empty : emptyKeys) - sortedByKernelNeeds.erase(empty); - - // проверяем нет ли завершившихся задач - for (auto it = activeTaskSet.begin(); it != activeTaskSet.end(); ) - { - T* task = *(it); - - if (task->Check()) { - it++; - activeTaskSet.erase(task); - activeTasks--; - busyKernels -= task->getKernels(); - printf(" done task with %d kernels and id %ld\n", task->getKernels(), task->getId()); + // очищаем от пустых ключей + for (auto& empty : emptyKeys) + sortedByKernelNeeds.erase(empty); + + // проверяем нет ли завершившихся задач + for (auto it = activeTaskSet.begin(); it != activeTaskSet.end(); ) + { + T* task = *(it); + + if (task->Check()) { + it++; + activeTaskSet.erase(task); + activeTasks--; + busyKernels -= task->getKernels(); + printf(" done task with %d kernels and id %ld\n", task->getKernels(), task->getId()); - continue; - } - it++; - } - } + continue; + } + it++; + } + } - changeState(); - } + changeState(); + } - virtual void Finalize() {} - void saveState() { - Utils::Sleep(1); //чтобы не было одинаковых по дате файлов. - String stateFile = packageWorkspace + "/state/" + getStatePrefix() + printState(); - //printf("stateFile=<%s>\n", stateFile.getCharArray()); - File tmp(stateFile, Utils::getDate()); - } + virtual void Finalize() {} + void saveState() { + Utils::Sleep(1); //чтобы не было одинаковых по дате файлов. + String stateFile = packageWorkspace + "/state/" + getStatePrefix() + printState(); + //printf("stateFile=<%s>\n", stateFile.getCharArray()); + File tmp(stateFile, Utils::getDate()); + } }; \ No newline at end of file From f4c7353741c8d37399b97ce56a4e2e71a867c548 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 18:06:42 +0300 Subject: [PATCH 6/8] fixed --- Planner/String.h | 6 ++---- Planner/Task.h | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Planner/String.h b/Planner/String.h index 2db4de42..f6d6315e 100644 --- a/Planner/String.h +++ b/Planner/String.h @@ -75,16 +75,14 @@ public: } } - String(long s) { - String((long long)s); - } + String(long s) : String((long long)s) { } String(long long s) { length = 0; body = new char[1]; body[0] = '\0'; if (s >= 0) { - long s_ = s; + long long s_ = s; long size = 1; while (s_ >= 10) { s_ = s_ / 10; diff --git a/Planner/Task.h b/Planner/Task.h index c3882a92..196bd439 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -42,7 +42,7 @@ protected: String workspace; TaskState state; public: - long start_time; + long long start_time; String printState(){ switch(state){ case Inactive: @@ -152,8 +152,8 @@ public: }else if (Utils::Exists(workspace+"/INTERRUPT")){ state=AbortedByUser; } else { - long now = Utils::getAbsoluteTime(); - long delta = now-start_time; + long long now = Utils::getAbsoluteTime(); + long long delta = now-start_time; if (maxtime Date: Sun, 3 Dec 2023 18:22:25 +0300 Subject: [PATCH 7/8] fixed --- Planner/Task.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Planner/Task.h b/Planner/Task.h index 196bd439..7d998f75 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -164,7 +164,7 @@ public: busyKernels = Utils::min(busyKernels - kernels, maxKernels); freeKernels = Utils::max(0, maxKernels - busyKernels); //- - //saveState(); //не нужно. только для отладки. анализ будет делаться архивом. + saveState(); } return (state != Running); From 4318d6756f9284d86b8d2a8a200a6dba0e404712 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 3 Dec 2023 19:01:25 +0300 Subject: [PATCH 8/8] fixed --- Planner/Supervisor.h | 1 - Planner/Task.h | 140 +++++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 66 deletions(-) diff --git a/Planner/Supervisor.h b/Planner/Supervisor.h index de2c6dcb..f89a9de3 100644 --- a/Planner/Supervisor.h +++ b/Planner/Supervisor.h @@ -112,7 +112,6 @@ public: case Execution: if (task->getState() == WorkspaceReady) { activeCount++; - task->start_time = Utils::getAbsoluteTime(); task->Start(); } else if (task->getState() == Running) { diff --git a/Planner/Task.h b/Planner/Task.h index 7d998f75..cb246abb 100644 --- a/Planner/Task.h +++ b/Planner/Task.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "File.h" #include "Utils.h" #include "Global.h" @@ -33,64 +35,67 @@ enum TestType { Performance, //2 }; -class Task { -protected: +class Task { +protected: long id; - int maxtime; + int maxtime; int kernels; //получение зависит от типа задачи. - String workspace; - TaskState state; + String workspace; + TaskState state; + std::chrono::system_clock::time_point start_time; + public: - long long start_time; - String printState(){ - switch(state){ - case Inactive: - return String("Inactive"); - case Waiting: - return String("Waiting"); - case WorkspaceCreated: - return String("WorkspaceCreated"); - case WorkspaceReady: - return String("WorkspaceReady"); - case Running: - return String("Running"); - case Canceled: - return String("Canceled"); - case Finished: - return String("Finished"); - case FinishedAbortedByTimeout: - return String("FinishedAbortedByTimeout"); - case FinishedAbortedByUser: - return String("FinishedAbortedByUser"); - case Done: - return String("Done"); - case DoneWithErrors: - return String("DoneWithErrors"); - case AbortedByTimeout: - return String("AbortedByTimeout"); - case AbortedByUser: - return String("AbortedByUser"); - case Crushed: - return String("Crushed"); - case WrongTestFormat: - return String("WrongTestFormat"); - case InternalError: - return String("InternalError"); - case Queued: - return String("Queued"); - case NoSuchTask: - return String("NoSuchTask"); - case FailedToQueue: - return String("FailedToQueue"); - case AbortingByUser: - return String("AbortingByUser"); - default: - return "?"; - } - } + String printState() { + switch (state) { + case Inactive: + return String("Inactive"); + case Waiting: + return String("Waiting"); + case WorkspaceCreated: + return String("WorkspaceCreated"); + case WorkspaceReady: + return String("WorkspaceReady"); + case Running: + return String("Running"); + case Canceled: + return String("Canceled"); + case Finished: + return String("Finished"); + case FinishedAbortedByTimeout: + return String("FinishedAbortedByTimeout"); + case FinishedAbortedByUser: + return String("FinishedAbortedByUser"); + case Done: + return String("Done"); + case DoneWithErrors: + return String("DoneWithErrors"); + case AbortedByTimeout: + return String("AbortedByTimeout"); + case AbortedByUser: + return String("AbortedByUser"); + case Crushed: + return String("Crushed"); + case WrongTestFormat: + return String("WrongTestFormat"); + case InternalError: + return String("InternalError"); + case Queued: + return String("Queued"); + case NoSuchTask: + return String("NoSuchTask"); + case FailedToQueue: + return String("FailedToQueue"); + case AbortingByUser: + return String("AbortingByUser"); + default: + return "?"; + } + } //-------------->> + void setStart() { start_time = std::chrono::system_clock::now(); } + long getId() { return id; } long setId(String* id_s) { return id = strtol(id_s->getCharArray(), NULL, 10); @@ -129,9 +134,9 @@ public: int getKernels() const { return kernels; } - virtual void Start(bool dontCheck = false) { - + void Start(bool dontCheck = false) { if (kernels <= freeKernels || dontCheck) { + setStart(); system(getStartCommand().getCharArray()); state = Running; //- @@ -140,7 +145,7 @@ public: //- } } - + //return 'true' if done, 'false' - if running virtual bool Check() { if (Utils::Exists(workspace + "/DONE")) @@ -149,17 +154,22 @@ public: if (Utils::Exists(workspace + "/TIMEOUT")) { state = AbortedByTimeout; //todo определить по интервалу времени на всякий случай. - }else if (Utils::Exists(workspace+"/INTERRUPT")){ - state=AbortedByUser; - } else { - long long now = Utils::getAbsoluteTime(); - long long delta = now-start_time; - if (maxtime diff_time = end_time - start_time; + + if (maxtime * 1.1 < diff_time.count()) { + printf("SET STATUS ABORTED by timer to %ld with time %f sec / %f sec", id, diff_time.count(), maxtime * 1.1); + state = AbortedByTimeout; + } + } + } + + if (state != Running) { //- busyKernels = Utils::min(busyKernels - kernels, maxKernels); freeKernels = Utils::max(0, maxKernels - busyKernels);