fixed code style, removed unnecessary pragmas

This commit is contained in:
2023-12-03 13:09:16 +03:00
parent bd74d4684f
commit 6b7bde1471
12 changed files with 765 additions and 744 deletions

View File

@@ -1,26 +1,28 @@
#pragma once
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#pragma once
template <class T> template <class T>
class Array { class Array {
protected: protected:
long length; long length;
T** elements; T** elements;
public: public:
Array(){ Array() {
length=0; length = 0;
elements=NULL; elements = NULL;
} }
virtual ~Array(){ virtual ~Array() {
if (elements !=NULL){ if (elements != NULL) {
for (long i=0; i<length; ++i) for (long i = 0; i < length; ++i)
delete elements[i]; delete elements[i];
delete [] elements; delete[] elements;
} }
} }
void add(T * new_line) { void add(T* new_line) {
T ** buf = new T*[length + 1]; T** buf = new T * [length + 1];
for (long i = 0; i < length; ++i) { for (long i = 0; i < length; ++i) {
buf[i] = elements[i]; buf[i] = elements[i];
} }
@@ -30,13 +32,13 @@ public:
elements = buf; elements = buf;
buf = NULL; buf = NULL;
} }
long getLength(){ long getLength() {
return length; return length;
} }
T * get(long i){ T* get(long i) {
return elements[i]; return elements[i];
} }
T** getElements(){ T** getElements() {
return elements; return elements;
} }
}; };

View File

@@ -1,20 +1,22 @@
#pragma once
#include "Supervisor.h" #include "Supervisor.h"
#include "CompilationTask.h" #include "CompilationTask.h"
#pragma once
class CompilationSupervisor: public Supervisor<CompilationTask> { class CompilationSupervisor : public Supervisor<CompilationTask> {
public: public:
CompilationSupervisor(){ CompilationSupervisor() {
this->init("compilationTasks", 4); this->init("compilationTasks", 4);
} }
CompilationTask * getTaskById(long task_id){ CompilationTask* getTaskById(long task_id) {
for (long i=0; i< length; ++i){ for (long i = 0; i < length; ++i) {
CompilationTask * task = get(i); CompilationTask* task = get(i);
if (task->getId()==task_id) if (task->getId() == task_id)
return task; return task;
} }
return NULL; return NULL;
} }
virtual String getStatePrefix(){ virtual String getStatePrefix() {
return String("Compilation"); return String("Compilation");
} }
}; };

View File

@@ -1,47 +1,50 @@
#include "Task.h"
#pragma once #pragma once
class CompilationTask: public Task {
#include "Task.h"
#include "Text.h"
class CompilationTask : public Task {
String test_id; String test_id;
String makefile_text; String makefile_text;
public: public:
void setTestId(String * test_id_in){ void setTestId(String* test_id_in) {
test_id = String(test_id_in->getCharArray()); test_id = String(test_id_in->getCharArray());
} }
void setMakefileText(String * makefile_text_in){ void setMakefileText(String* makefile_text_in) {
makefile_text = String(makefile_text_in->getCharArray(), '|'); makefile_text = String(makefile_text_in->getCharArray(), '|');
} }
virtual void print(){ virtual void print() {
printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime, printf("id=%ld; maxtime=%d; test_id=%s\n", id, maxtime,
test_id.getCharArray()); test_id.getCharArray());
printf("makefile_text=%s\n", makefile_text.getCharArray()); printf("makefile_text=%s\n", makefile_text.getCharArray());
} }
CompilationTask(Text * lines, int offset):Task(lines,offset) { CompilationTask(Text* lines, int offset) :Task(lines, offset) {
setTestId(lines->get(offset+2)); setTestId(lines->get(offset + 2));
setMakefileText(lines->get(offset+3)); setMakefileText(lines->get(offset + 3));
setState(Waiting); setState(Waiting);
kernels=1; kernels = 1;
} }
virtual void prepareWorkspace(){ virtual void prepareWorkspace() {
String makeFilePath = String(id)+"/Makefile"; String makeFilePath = String(id) + "/Makefile";
File makeFileFile = File(makeFilePath, this->makefile_text); File makeFileFile = File(makeFilePath, this->makefile_text);
String tests = userWorkspace+"/projects"; String tests = userWorkspace + "/projects";
String testPath= tests+"/"+test_id; String testPath = tests + "/" + test_id;
String copyCommand = "cp -r " + String::DQuotes(testPath + "/.") + " "+ String::DQuotes(workspace); String copyCommand = "cp -r " + String::DQuotes(testPath + "/.") + " " + String::DQuotes(workspace);
system(copyCommand.getCharArray()); system(copyCommand.getCharArray());
} }
virtual String getLaunchScriptText(){ virtual String getLaunchScriptText() {
String modules = userWorkspace+"/modules"; String modules = userWorkspace + "/modules";
String starterCall = modules+"/starter"; String starterCall = modules + "/starter";
String launcherCall = modules+"/launcher"; String launcherCall = modules + "/launcher";
return String::DQuotes(starterCall)+" "+ return String::DQuotes(starterCall) + " " +
String::DQuotes(launcherCall)+" "+ String::DQuotes(launcherCall) + " " +
String(maxtime)+" "+ String(maxtime) + " " +
String::DQuotes("")+" "+ String::DQuotes("") + " " +
"make -j -f Makefile"; "make -j -f Makefile";
} }
virtual void analyseResults(){ virtual void analyseResults() {
String binary = workspace+"/0"; String binary = workspace + "/0";
state = Utils::Exists(binary)? Done:DoneWithErrors; state = Utils::Exists(binary) ? Done : DoneWithErrors;
} }
}; };

View File

@@ -1,15 +1,16 @@
#include "Text.h"
#pragma once #pragma once
#include "Text.h"
class File { class File {
FILE* ptr; FILE* ptr;
public: public:
File(String* name) { File(String* name) {
ptr = fopen(name->getCharArray(), "r"); ptr = fopen(name->getCharArray(), "r");
} }
File(const char * name) { File(const char* name) {
ptr = fopen(name, "r"); ptr = fopen(name, "r");
} }
File(const String& name, const String& text){ File(const String& name, const String& text) {
ptr = fopen(name.getCharArray(), "w"); ptr = fopen(name.getCharArray(), "w");
fprintf(ptr, "%s\n", text.getCharArray()); fprintf(ptr, "%s\n", text.getCharArray());
} }
@@ -19,46 +20,47 @@ public:
ptr = NULL; ptr = NULL;
} }
} }
Text* readLines(){ Text* readLines() {
Text* lines = new Text(); Text* lines = new Text();
int c; int c;
String * line = NULL; String* line = NULL;
bool lineStarted = false; bool lineStarted = false;
do { do {
c = fgetc(ptr); c = fgetc(ptr);
if (lineStarted){ if (lineStarted) {
switch (c) { switch (c) {
case '\r': case '\r':
break; break;
case '\n': case '\n':
case EOF: case EOF:
lines->add(line); lines->add(line);
line = NULL; line = NULL;
lineStarted = false; lineStarted = false;
break; break;
default: default:
line->addChar((char)c); line->addChar((char)c);
break; 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); 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; return lines;
} }
}; };

View File

@@ -1,13 +1,10 @@
#pragma once
#include "String.h" #include "String.h"
#pragma once
String userWorkspace; String userWorkspace;
#pragma once
String packageWorkspace; String packageWorkspace;
#pragma once
int maxKernels; int maxKernels;
#pragma once
int busyKernels; int busyKernels;
#pragma once
int freeKernels; int freeKernels;
#pragma once
String dvm_drv; String dvm_drv;

View File

@@ -1,30 +1,32 @@
#pragma once
#include "CompilationSupervisor.h" #include "CompilationSupervisor.h"
#include "RunTask.h" #include "RunTask.h"
#pragma once
class RunSupervisor: public Supervisor<RunTask> { class RunSupervisor : public Supervisor<RunTask> {
public: public:
RunSupervisor(CompilationSupervisor * compilationSupervisor){ RunSupervisor(CompilationSupervisor* compilationSupervisor) {
this->init("runTasks", 8); this->init("runTasks", 8);
//проверить отмененные задачи. //проверить отмененные задачи.
for (long i=0; i< this->length; ++i){ for (long i = 0; i < this->length; ++i) {
RunTask * task = this->get(i); RunTask* task = this->get(i);
CompilationTask * parent = compilationSupervisor->getTaskById( task->getTestCompilationTaskId()); CompilationTask* parent = compilationSupervisor->getTaskById(task->getTestCompilationTaskId());
task->setState((parent->getState()==Done)?Waiting:Canceled); task->setState((parent->getState() == Done) ? Waiting : Canceled);
task->setParent(parent); task->setParent(parent);
printf("id=%ld; parent_id = %ld; state=%s\n", printf("id=%ld; parent_id = %ld; state=%s\n",
task->getId(), task->getId(),
task->getParent()->getId(), task->getParent()->getId(),
task->printState().getCharArray()); task->printState().getCharArray());
} }
} }
virtual String getStatePrefix(){ virtual String getStatePrefix() {
return String("Running"); return String("Running");
} }
virtual void Finalize(){ virtual void Finalize() {
this->state = Archivation; this->state = Archivation;
saveState(); saveState();
printf("Archivation started\n"); printf("Archivation started\n");
Utils::ZipFolder(String("./"),String("archive.zip")); Utils::ZipFolder(String("./"), String("archive.zip"));
printf("Archivation ended\n"); printf("Archivation ended\n");
} }

View File

@@ -1,5 +1,7 @@
#include "CompilationTask.h"
#pragma once #pragma once
#include "CompilationTask.h"
class RunTask : public Task { class RunTask : public Task {
long testcompilationtask_id; long testcompilationtask_id;
String binary_name; String binary_name;
@@ -9,85 +11,85 @@ class RunTask : public Task {
String args; String args;
CompilationTask* parent; CompilationTask* parent;
public: public:
virtual void print(){ virtual void print() {
printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n", printf("id=%ld; maxtime=%d; testcompilationtask_id=%ld; matrix=%s; environments=%s; usr_par=%s; args=%s kernels=%d\n",
id, id,
maxtime, maxtime,
testcompilationtask_id, testcompilationtask_id,
matrix.getCharArray(), matrix.getCharArray(),
environments.getCharArray(), environments.getCharArray(),
usr_par.getCharArray(), usr_par.getCharArray(),
args.getCharArray(), args.getCharArray(),
kernels kernels
); );
} }
int setKernels(String * kernels_s){ int setKernels(String* kernels_s) {
return kernels = atoi(kernels_s->getCharArray()); return kernels = atoi(kernels_s->getCharArray());
} }
long setTestCompilationTaskId(String * id_s){ long setTestCompilationTaskId(String* id_s) {
return testcompilationtask_id=strtol(id_s->getCharArray(), NULL, 10); return testcompilationtask_id = strtol(id_s->getCharArray(), NULL, 10);
} }
long getTestCompilationTaskId(){ long getTestCompilationTaskId() {
return testcompilationtask_id; return testcompilationtask_id;
} }
void setMatrix(String * matrix_in){ void setMatrix(String* matrix_in) {
matrix= String (matrix_in->getCharArray()); matrix = String(matrix_in->getCharArray());
} }
void setEnvironments(String * environments_in){ void setEnvironments(String* environments_in) {
environments= String(environments_in->getCharArray()); environments = String(environments_in->getCharArray());
} }
void setUsrPar(String * usr_par_in){ void setUsrPar(String* usr_par_in) {
usr_par= String(usr_par_in->getCharArray(),'|'); usr_par = String(usr_par_in->getCharArray(), '|');
} }
void setArgs(String * args_in){ void setArgs(String* args_in) {
args= String(args_in->getCharArray()); args = String(args_in->getCharArray());
} }
void setParent(CompilationTask * parent_in){ void setParent(CompilationTask* parent_in) {
parent = parent_in; parent = parent_in;
binary_name = "spf_"+ String(id)+"_"+matrix.Replace(' ','_'); binary_name = "spf_" + String(id) + "_" + matrix.Replace(' ', '_');
} }
CompilationTask * getParent(){ CompilationTask* getParent() {
return parent; return parent;
} }
RunTask(Text * lines, int offset):Task(lines,offset) { RunTask(Text* lines, int offset) :Task(lines, offset) {
setTestCompilationTaskId(lines->get(offset+2)); setTestCompilationTaskId(lines->get(offset + 2));
setMatrix(lines->get(offset+3)); setMatrix(lines->get(offset + 3));
setEnvironments(lines->get(offset+4)); setEnvironments(lines->get(offset + 4));
setUsrPar(lines->get(offset+5)); setUsrPar(lines->get(offset + 5));
setArgs(lines->get(offset+6)); setArgs(lines->get(offset + 6));
setKernels(lines->get(offset+7)); setKernels(lines->get(offset + 7));
} }
virtual String getLaunchScriptText(){ virtual String getLaunchScriptText() {
String modules = userWorkspace+"/modules"; String modules = userWorkspace + "/modules";
String starterCall = modules+"/starter"; String starterCall = modules + "/starter";
String launcherCall = modules+"/launcher"; String launcherCall = modules + "/launcher";
//- //-
String dvm_start = String::DQuotes(dvm_drv) + " run "; String dvm_start = String::DQuotes(dvm_drv) + " run ";
if (!matrix.isEmpty()) if (!matrix.isEmpty())
dvm_start = dvm_start+matrix + " "; dvm_start = dvm_start + matrix + " ";
dvm_start = dvm_start+ String::DQuotes("./" + binary_name); dvm_start = dvm_start + String::DQuotes("./" + binary_name);
if (!args.isEmpty()) if (!args.isEmpty())
dvm_start = dvm_start+ " " + args; dvm_start = dvm_start + " " + args;
return String::DQuotes(starterCall)+" "+ return String::DQuotes(starterCall) + " " +
String::DQuotes(launcherCall)+" "+ String::DQuotes(launcherCall) + " " +
String(maxtime)+" "+ String(maxtime) + " " +
String::DQuotes("killall -SIGKILL " + binary_name)+" "+ String::DQuotes("killall -SIGKILL " + binary_name) + " " +
dvm_start; dvm_start;
} }
virtual void prepareWorkspace(){ virtual void prepareWorkspace() {
String binary_src = parent->getWorkspace()+"/0"; String binary_src = parent->getWorkspace() + "/0";
String binary_dst = workspace+"/"+binary_name; String binary_dst = workspace + "/" + binary_name;
Utils::Copy(binary_src, binary_dst); Utils::Copy(binary_src, binary_dst);
if (!usr_par.isEmpty()){ if (!usr_par.isEmpty()) {
String parPath = String(id)+"/usr.par"; String parPath = String(id) + "/usr.par";
File parFile = File(parPath, usr_par); File parFile = File(parPath, usr_par);
} }
} }
virtual String getStartCommand(){ virtual String getStartCommand() {
String res = workspace+"/run"; String res = workspace + "/run";
if (!environments.isEmpty()) if (!environments.isEmpty())
res = environments+" "+res; res = environments + " " + res;
printf("START %ld: %s\n", id, res.getCharArray()); printf("START %ld: %s\n", id, res.getCharArray());
return res; return res;
} }

View File

@@ -1,8 +1,10 @@
#pragma once
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#pragma once
class String { class String {
friend String operator+(const String& a, const String& b); friend String operator+(const String& a, const String& b);
long length; long length;
@@ -19,22 +21,22 @@ public:
body = new char[length + 1]; body = new char[length + 1];
for (long i = 0; i < length; ++i) for (long i = 0; i < length; ++i)
body[i] = s[i]; body[i] = s[i];
body[length]='\0'; body[length] = '\0';
} }
String(const char* s, char ps) { String(const char* s, char ps) {
length = strlen(s); length = strlen(s);
body = new char[length + 1]; body = new char[length + 1];
for (long i = 0; i < length; ++i){ for (long i = 0; i < length; ++i) {
body[i] = (s[i]==ps)? '\n': s[i]; body[i] = (s[i] == ps) ? '\n' : s[i];
} }
body[length]='\0'; body[length] = '\0';
} }
~String() { ~String() {
if (body != NULL) { if (body != NULL) {
delete[] body; delete[] body;
} }
} }
void println() const{ void println() const {
printf("[%s]\n", body); printf("[%s]\n", body);
} }
void addChar(char c) { void addChar(char c) {
@@ -51,170 +53,171 @@ public:
body = buf; body = buf;
buf = NULL; buf = NULL;
} }
char * getCharArray() const{ char* getCharArray() const {
return body; return body;
} }
String (int s){ String(int s) {
length = 0; length = 0;
body = new char[1]; body = new char[1];
body[0] = '\0'; body[0] = '\0';
if (s>=0){ if (s >= 0) {
int s_ = s; int s_ = s;
int size = 1; int size = 1;
while (s_>=10){ while (s_ >= 10) {
s_ = s_/10; s_ = s_ / 10;
size++; size++;
} }
length = size; length = size;
body = new char [size+1]; body = new char[size + 1];
sprintf(body, "%d", s); sprintf(body, "%d", s);
} }
} }
String (long s){ String(long s) {
length = 0; length = 0;
body = new char[1]; body = new char[1];
body[0] = '\0'; body[0] = '\0';
if (s>=0){ if (s >= 0) {
long s_ = s; long s_ = s;
long size = 1; long size = 1;
while (s_>=10){ while (s_ >= 10) {
s_ = s_/10; s_ = s_ / 10;
size++; size++;
} }
length = size; length = size;
body = new char [size+1]; body = new char[size + 1];
sprintf(body, "%ld", s); sprintf(body, "%ld", s);
} }
} }
const String& operator=(const String& s){ const String& operator=(const String& s) {
if (body != NULL) if (body != NULL)
delete[] body; delete[] body;
length = s.length; length = s.length;
body = new char[length+1]; body = new char[length + 1];
for (long i=0; i<length; ++i) for (long i = 0; i < length; ++i)
body[i]=s.body[i]; body[i] = s.body[i];
body[length]='\0'; body[length] = '\0';
return *this; return *this;
} }
static String DQuotes(const String& s){ static String DQuotes(const String& s) {
String res; String res;
res.length = s.length+2; res.length = s.length + 2;
res.body=new char[res.length+1]; res.body = new char[res.length + 1];
res.body[0] = '"'; res.body[0] = '"';
res.body[res.length-1] = '"'; res.body[res.length - 1] = '"';
res.body[res.length] = '\0'; res.body[res.length] = '\0';
for (long i=0; i< s.length; ++i) for (long i = 0; i < s.length; ++i)
res.body[i+1]=s.body[i]; res.body[i + 1] = s.body[i];
return res; return res;
} }
String Replace(char f, char t){ String Replace(char f, char t) {
String res; String res;
res.length = length; res.length = length;
res.body = new char[length]; res.body = new char[length];
res.body[length]='\0'; res.body[length] = '\0';
for (long i = 0; i < length; ++i) for (long i = 0; i < length; ++i)
res.body[i]= (body[i]==f)? t: body[i]; res.body[i] = (body[i] == f) ? t : body[i];
return res; return res;
} }
String Remove(char f){ String Remove(char f) {
String res; String res;
for (long i=0; i<length; ++i){ for (long i = 0; i < length; ++i) {
if (body[i]!=f) if (body[i] != f)
res.addChar(body[i]); res.addChar(body[i]);
} }
return res; return res;
} }
bool isEmpty() const{ bool isEmpty() const {
return length==0; return length == 0;
} }
bool contains(const String& s) const{ bool contains(const String& s) const {
bool res = false; bool res = false;
bool search_on=false; bool search_on = false;
if (s.isEmpty()) return true; if (s.isEmpty()) return true;
if (s.length>length) return false; if (s.length > length) return false;
long k=0; long k = 0;
long start=-1; long start = -1;
for (long i=0; i<length; ++i){ for (long i = 0; i < length; ++i) {
if (search_on){ if (search_on) {
if (k<s.length){ if (k < s.length) {
if (body[i]==s.body[k]){ if (body[i] == s.body[k]) {
k++; k++;
}else { }
else {
//обрыв поиска. //обрыв поиска.
search_on =false; search_on = false;
k=0; k = 0;
start=-1; start = -1;
} }
} }
else { else {
//printf("starts with %ld", start); //printf("starts with %ld", start);
return true; return true;
} }
}else { }
if (body[i]== s.body[0]){ else {
k=1; if (body[i] == s.body[0]) {
start=i; k = 1;
search_on=true; start = i;
search_on = true;
//printf("search started %d\n", start); //printf("search started %d\n", start);
} }
} }
} }
if (search_on) { if (search_on) {
//printf("starts with %ld\n", start); //printf("starts with %ld\n", start);
res=true; res = true;
} }
return res; return res;
} }
String toUpper(){ String toUpper() {
String res = String(this->getCharArray()); String res = String(this->getCharArray());
for (long i=0; i<length; ++i) for (long i = 0; i < length; ++i)
res.body[i]= toupper(body[i]); res.body[i] = toupper(body[i]);
res.println(); res.println();
return res; return res;
} }
long getLength(){ long getLength() {
return length; return length;
} }
bool operator==(const String&s) const { bool operator==(const String& s) const {
if (length!=s.length) return false; if (length != s.length) return false;
for (long i=0; i<length; ++i){ for (long i = 0; i < length; ++i) {
if (body[i]!=s.body[i]) return false; if (body[i] != s.body[i]) return false;
} }
return true; return true;
} }
/* регистр. /* регистр.
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
int main(void) { int main(void) {
FILE * f; FILE * f;
int c; int c;
if ( ! ( f = fopen("file.txt", "r") ) ) if ( ! ( f = fopen("file.txt", "r") ) )
return -1; return -1;
while ( ( c = fgetc(f) ) != EOF ) while ( ( c = fgetc(f) ) != EOF )
putchar( isupper(c) ? tolower(c) : toupper(c) ); putchar( isupper(c) ? tolower(c) : toupper(c) );
return ( fclose(f) );
}
*/
return ( fclose(f) );
}
*/
}; };
String operator+(const String& a, const String& b){ String operator+(const String& a, const String& b) {
String res = String(); String res = String();
res.length =a.length+b.length; res.length = a.length + b.length;
res.body = new char[res.length+1]; res.body = new char[res.length + 1];
for (long i=0; i<a.length; ++i) for (long i = 0; i < a.length; ++i)
res.body[i]=a.body[i]; res.body[i] = a.body[i];
for (long i=0; i<b.length; ++i) for (long i = 0; i < b.length; ++i)
res.body[i+a.length]=b.body[i]; res.body[i + a.length] = b.body[i];
res.body[res.length]='\0'; res.body[res.length] = '\0';
return res; return res;
} }

View File

@@ -1,8 +1,9 @@
#pragma once
#include <unistd.h>
#include "File.h" #include "File.h"
#include "Task.h" #include "Task.h"
#include <unistd.h>
#pragma once
enum SupervisorState { enum SupervisorState {
WorkspacesCreation, //0 WorkspacesCreation, //0
Preparation, //1 Preparation, //1
@@ -10,119 +11,120 @@ enum SupervisorState {
Archivation, //3 Archivation, //3
End //4 End //4
}; };
#pragma once
template <class T> template <class T>
class Supervisor : public Array <T> { class Supervisor : public Array <T> {
protected: protected:
SupervisorState state; SupervisorState state;
public: public:
virtual String getStatePrefix(){ virtual String getStatePrefix() {
return String(""); return String("");
} }
String printState(){ String printState() {
switch(state){ switch (state) {
case WorkspacesCreation: case WorkspacesCreation:
return String("WorkspacesCreation"); return String("WorkspacesCreation");
case Preparation: case Preparation:
return String("Preparation"); return String("Preparation");
case Execution: case Execution:
return String("Execution"); return String("Execution");
case Archivation: case Archivation:
return String("Archivation"); return String("Archivation");
case End: case End:
return String("End"); return String("End");
default: default:
return "?"; return "?";
} }
} }
//- //-
void print(){ void print() {
for (long i=0; i< this->length; ++i) for (long i = 0; i < this->length; ++i)
this->elements[i]->print(); this->elements[i]->print();
} }
void init(const char * fileName, int recordSize){ void init(const char* fileName, int recordSize) {
state = WorkspacesCreation; state = WorkspacesCreation;
File * packedTasks = new File(fileName); File* packedTasks = new File(fileName);
Text * lines = packedTasks->readLines(); Text* lines = packedTasks->readLines();
this->length = lines->getLength()/recordSize; this->length = lines->getLength() / recordSize;
this->elements = new T* [this->length]; this->elements = new T * [this->length];
int offset=0; int offset = 0;
for (int i=0; i< this->length; ++i){ for (int i = 0; i < this->length; ++i) {
this->elements[i]= new T(lines, offset); this->elements[i] = new T(lines, offset);
offset+=recordSize; offset += recordSize;
} }
delete packedTasks; delete packedTasks;
delete lines; delete lines;
} }
void Do(){ void Do() {
saveState(); saveState();
long activeCount=0; long activeCount = 0;
//todo обязательно убрать отладочную печать. //todo обязательно убрать отладочную печать.
printf("tasks count = %ld\n", this->length); printf("tasks count = %ld\n", this->length);
while (this->state!= End){ while (this->state != End) {
// printf("state=%d\n", this->state); // printf("state=%d\n", this->state);
// printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels); // printf("max=%d; busy=%d; free=%d\n", maxKernels, busyKernels, freeKernels);
activeCount=0; activeCount = 0;
for (long i=0; i<this->length; ++i){ for (long i = 0; i < this->length; ++i) {
T * task = this->elements[i]; T* task = this->elements[i];
switch (this->state){ switch (this->state) {
case WorkspacesCreation: case WorkspacesCreation:
if (task->getState()==Waiting){ if (task->getState() == Waiting) {
activeCount++; activeCount++;
task->createWorkspace(); task->createWorkspace();
task->setState(WorkspaceCreated); task->setState(WorkspaceCreated);
} }
break; break;
case Preparation: case Preparation:
if (task->getState()==WorkspaceCreated){ if (task->getState() == WorkspaceCreated) {
activeCount++; activeCount++;
task->prepareWorkspace(); task->prepareWorkspace();
task->createLaunchScript(); task->createLaunchScript();
task->setState(WorkspaceReady); task->setState(WorkspaceReady);
} }
break; break;
case Execution: case Execution:
if (task->getState()==WorkspaceReady){ if (task->getState() == WorkspaceReady) {
activeCount++; activeCount++;
task->Start(); task->Start();
}else if (task->getState()==Running){ }
activeCount++; else if (task->getState() == Running) {
task->Check(); activeCount++;
} task->Check();
break; }
default: break;
// printf("id = %ld; state = %d\n", task->getId(), task->getState()); default:
break; // printf("id = %ld; state = %d\n", task->getId(), task->getState());
break;
} }
} }
// printf("active count = %d\n", activeCount); // printf("active count = %d\n", activeCount);
if (activeCount==0){ if (activeCount == 0) {
switch (this->state){ switch (this->state) {
case WorkspacesCreation: case WorkspacesCreation:
this->state = Preparation; this->state = Preparation;
saveState(); saveState();
break; break;
case Preparation: case Preparation:
this->state = Execution; this->state = Execution;
saveState(); saveState();
break; break;
case Execution: case Execution:
Finalize(); Finalize();
this->state = End; this->state = End;
saveState(); saveState();
break; break;
default: default:
this->state = End; this->state = End;
break; break;
} }
} }
Utils::Sleep(2); Utils::Sleep(2);
} }
} }
virtual void Finalize(){} virtual void Finalize() {}
void saveState(){ void saveState() {
Utils::Sleep(1); //чтобы не было одинаковых по дате файлов. Utils::Sleep(1); //чтобы не было одинаковых по дате файлов.
String stateFile = packageWorkspace+"/state/"+getStatePrefix()+printState(); String stateFile = packageWorkspace + "/state/" + getStatePrefix() + printState();
//printf("stateFile=<%s>\n", stateFile.getCharArray()); //printf("stateFile=<%s>\n", stateFile.getCharArray());
File(stateFile, Utils::getDate()); File(stateFile, Utils::getDate());
} }

View File

@@ -1,8 +1,9 @@
#pragma once
#include "File.h" #include "File.h"
#include "Utils.h" #include "Utils.h"
#include "Global.h" #include "Global.h"
#pragma once
enum TaskState { enum TaskState {
Inactive, //0 Inactive, //0
Waiting, //1 Waiting, //1
@@ -25,14 +26,13 @@ enum TaskState {
FailedToQueue, //18 FailedToQueue, //18
AbortingByUser //19 AbortingByUser //19
}; };
#pragma once
enum TestType{ enum TestType {
Default, //0 Default, //0
Correctness, //1 Correctness, //1
Performance, //2 Performance, //2
}; };
#pragma once
class Task { class Task {
protected: protected:
long id; long id;
@@ -41,123 +41,125 @@ protected:
String workspace; String workspace;
TaskState state; TaskState state;
public: public:
String printState(){ String printState() {
switch(state){ switch (state) {
case Inactive: case Inactive:
return String("Inactive"); return String("Inactive");
case Waiting: case Waiting:
return String("Waiting"); return String("Waiting");
case WorkspaceCreated: case WorkspaceCreated:
return String("WorkspaceCreated"); return String("WorkspaceCreated");
case WorkspaceReady: case WorkspaceReady:
return String("WorkspaceReady"); return String("WorkspaceReady");
case Running: case Running:
return String("Running"); return String("Running");
case Canceled: case Canceled:
return String("Canceled"); return String("Canceled");
case Finished: case Finished:
return String("Finished"); return String("Finished");
case FinishedAbortedByTimeout: case FinishedAbortedByTimeout:
return String("FinishedAbortedByTimeout"); return String("FinishedAbortedByTimeout");
case FinishedAbortedByUser: case FinishedAbortedByUser:
return String("FinishedAbortedByUser"); return String("FinishedAbortedByUser");
case Done: case Done:
return String("Done"); return String("Done");
case DoneWithErrors: case DoneWithErrors:
return String("DoneWithErrors"); return String("DoneWithErrors");
case AbortedByTimeout: case AbortedByTimeout:
return String("AbortedByTimeout"); return String("AbortedByTimeout");
case AbortedByUser: case AbortedByUser:
return String("AbortedByUser"); return String("AbortedByUser");
case Crushed: case Crushed:
return String("Crushed"); return String("Crushed");
case WrongTestFormat: case WrongTestFormat:
return String("WrongTestFormat"); return String("WrongTestFormat");
case InternalError: case InternalError:
return String("InternalError"); return String("InternalError");
case Queued: case Queued:
return String("Queued"); return String("Queued");
case NoSuchTask: case NoSuchTask:
return String("NoSuchTask"); return String("NoSuchTask");
case FailedToQueue: case FailedToQueue:
return String("FailedToQueue"); return String("FailedToQueue");
case AbortingByUser: case AbortingByUser:
return String("AbortingByUser"); return String("AbortingByUser");
default: default:
return "?"; return "?";
} }
} }
//-------------->> //-------------->>
long getId(){return id;} long getId() { return id; }
long setId(String * id_s){ long setId(String* id_s) {
return id=strtol(id_s->getCharArray(), NULL, 10); return id = strtol(id_s->getCharArray(), NULL, 10);
} }
int getMaxtime(){return maxtime;} int getMaxtime() { return maxtime; }
int setMaxtime(String * maxtime_s){ int setMaxtime(String* maxtime_s) {
return maxtime=atoi(maxtime_s->getCharArray()); return maxtime = atoi(maxtime_s->getCharArray());
} }
const String& getWorkspace(){return workspace;} const String& getWorkspace() { return workspace; }
TaskState getState(){return state;} TaskState getState() { return state; }
TaskState setState(TaskState state_in){return state=state_in;} TaskState setState(TaskState state_in) { return state = state_in; }
Task(Text * lines, int offset){ Task(Text* lines, int offset) {
setId(lines->get(offset)); setId(lines->get(offset));
setMaxtime(lines->get(offset+1)); setMaxtime(lines->get(offset + 1));
workspace = packageWorkspace+"/"+String(id); workspace = packageWorkspace + "/" + String(id);
} }
virtual void print()=0; virtual void print() = 0;
//- //-
virtual void prepareWorkspace(){} virtual void prepareWorkspace() {}
virtual String getLaunchScriptText()=0; virtual String getLaunchScriptText() = 0;
virtual String getStartCommand(){ virtual String getStartCommand() {
return workspace+"/run"; return workspace + "/run";
} }
void createWorkspace(){ void createWorkspace() {
Utils::Mkdir(workspace); Utils::Mkdir(workspace);
} }
void createLaunchScript(){ void createLaunchScript() {
String launchScriptPath = workspace+"/run"; String launchScriptPath = workspace + "/run";
String launchScriptText = String launchScriptText =
String("cd ")+String::DQuotes(workspace)+"\n"+ String("cd ") + String::DQuotes(workspace) + "\n" +
getLaunchScriptText(); getLaunchScriptText();
File launchScriptFile = File(launchScriptPath, launchScriptText); File launchScriptFile = File(launchScriptPath, launchScriptText);
Utils::Chmod(launchScriptPath); Utils::Chmod(launchScriptPath);
} }
virtual void Start(){ virtual void Start() {
if (kernels<=freeKernels){ if (kernels <= freeKernels) {
system(getStartCommand().getCharArray()); system(getStartCommand().getCharArray());
state=Running; state = Running;
//- //-
busyKernels= Utils::min(busyKernels+kernels, maxKernels); busyKernels = Utils::min(busyKernels + kernels, maxKernels);
freeKernels= Utils::max(0, maxKernels-busyKernels); freeKernels = Utils::max(0, maxKernels - busyKernels);
//- //-
} }
} }
virtual void analyseResults(){ virtual void analyseResults() {
state=Finished; state = Finished;
} }
virtual void Check(){ virtual void Check() {
if (Utils::Exists(workspace+"/DONE")){ if (Utils::Exists(workspace + "/DONE")) {
analyseResults(); analyseResults();
}else { }
if (Utils::Exists(workspace+"/TIMEOUT")){ else {
state=AbortedByTimeout; if (Utils::Exists(workspace + "/TIMEOUT")) {
state = AbortedByTimeout;
//todo определить по интервалу времени на всякий случай. //todo определить по интервалу времени на всякий случай.
}else if (Utils::Exists(workspace+"/INTERRUPT")){ }
state=AbortedByUser; else if (Utils::Exists(workspace + "/INTERRUPT")) {
state = AbortedByUser;
} }
} }
if (state!=Running){ if (state != Running) {
//- //-
busyKernels= Utils::min(busyKernels-kernels, maxKernels); busyKernels = Utils::min(busyKernels - kernels, maxKernels);
freeKernels= Utils::max(0, maxKernels-busyKernels); freeKernels = Utils::max(0, maxKernels - busyKernels);
//- //-
saveState(); //не нужно. только для отладки. анализ будет делаться архивом. saveState(); //не нужно. только для отладки. анализ будет делаться архивом.
} }
} }
virtual void saveState(){ virtual void saveState() {
String stateFile = workspace+"/TaskState"; String stateFile = workspace + "/TaskState";
File(stateFile, printState()); File(stateFile, printState());
} }
}; };

View File

@@ -1,23 +1,25 @@
#pragma once
#include "String.h" #include "String.h"
#include "Array.h" #include "Array.h"
#pragma once
class Text: public Array<String> { class Text : public Array<String> {
public: public:
void Print(){ void Print() {
printf("text length=%ld\n", length); printf("text length=%ld\n", length);
for (long i=0; i<length; ++i){ for (long i = 0; i < length; ++i) {
printf("i=%ld; [%s]\n",i, elements[i]->getCharArray()); printf("i=%ld; [%s]\n", i, elements[i]->getCharArray());
// elements[i]->println(); // elements[i]->println();
} }
} }
bool hasMatch(const String& s){ bool hasMatch(const String& s) {
for (long i=0; i<length; ++i){ for (long i = 0; i < length; ++i) {
if (s.contains(*elements[i])){ if (s.contains(*elements[i])) {
//printf("match: [%s]\n", elements[i]->getCharArray()); //printf("match: [%s]\n", elements[i]->getCharArray());
return true; return true;
} }
} }
//printf("no matches for [%s]\n", s.getCharArray()); //printf("no matches for [%s]\n", s.getCharArray());
return false; return false;

View File

@@ -1,49 +1,51 @@
#pragma once
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include "String.h" #include "String.h"
#pragma once
class Utils { class Utils {
public: public:
static int max(int a, int b){ static int max(int a, int b) {
return (a > b)? a:b; return (a > b) ? a : b;
} }
static int min(int a, int b){ static int min(int a, int b) {
return (a > b)? b:a; return (a > b) ? b : a;
} }
static void Mkdir(const String& path){ static void Mkdir(const String& path) {
mkdir(path.getCharArray(), 0777); mkdir(path.getCharArray(), 0777);
} }
//https://stackoverflow.com/questions/4568681/using-chmod-in-a-c-program //https://stackoverflow.com/questions/4568681/using-chmod-in-a-c-program
static void Chmod(const String& path){ static void Chmod(const String& path) {
String command = "chmod 777 "+String::DQuotes(path); String command = "chmod 777 " + String::DQuotes(path);
system(command.getCharArray()); system(command.getCharArray());
} }
//https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c //https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c
static bool Exists(const String& path){ static bool Exists(const String& path) {
struct stat buffer; struct stat buffer;
return (stat (path.getCharArray(), &buffer) == 0); return (stat(path.getCharArray(), &buffer) == 0);
} }
static void Sleep(int s){ static void Sleep(int s) {
usleep(s* 1000000); usleep(s * 1000000);
} }
static void Copy(const String& src, const String& dst){ static void Copy(const String& src, const String& dst) {
String command = "cp "+String::DQuotes(src)+" "+String::DQuotes(dst); String command = "cp " + String::DQuotes(src) + " " + String::DQuotes(dst);
system(command.getCharArray()); system(command.getCharArray());
} }
static long getAbsoluteTime(){ static long getAbsoluteTime() {
return time (NULL); return time(NULL);
} }
static String getDate(){ static String getDate() {
long int ttime; long int ttime;
ttime = time (NULL); ttime = time(NULL);
String res(ctime (&ttime)); String res(ctime(&ttime));
return res; return res;
} }
static void ZipFolder(const String& src, const String& dst){ static void ZipFolder(const String& src, const String& dst) {
String command = "zip -r "+String::DQuotes(dst)+" "+String::DQuotes(src); String command = "zip -r " + String::DQuotes(dst) + " " + String::DQuotes(src);
system(command.getCharArray()); system(command.getCharArray());
} }
}; };