Commit ff758afb authored by GUYOT DOMINIQUE's avatar GUYOT DOMINIQUE
Browse files

Add Cline and process classes

parent fab86521
#include "cxxopts.hpp"
using namespace::std;
struct Cline{
bool server;
string input;
string output;
string log;
string conf;
string report;
string host;
string port;
string shm;
Cline(int argc, char** argv){
cxxopts::Options options("Cline", "Command line parser");
options.add_options()
("s,server", "Launch a server (bool true) or a client (bool false)", cxxopts::value<bool>()->default_value("false"))
("i,input", "Input file [server]", cxxopts::value<string>()->default_value(""))
("o,output", "Output file [server]", cxxopts::value<string>()->default_value(""))
("l,log", "Log file [server]", cxxopts::value<string>()->default_value(""))
("C,conf", "Conf file [server]", cxxopts::value<string>()->default_value("paraload.conf"))
("r,report", "Report file [server]", cxxopts::value<string>()->default_value(""))
("h,host", "Server host [client]", cxxopts::value<string>()->default_value("localhost"))
("p,port", "Server port [client]", cxxopts::value<string>()->default_value(""))
("S,shm", "Temporary directory used for computing [client]", cxxopts::value<string>()->default_value("/dev/shm/"))
;
auto result = options.parse(argc, argv);
server = result["server"].as<bool>();
input = result["input"].as<string>();
output = result["output"].as<string>();
log = result["log"].as<string>();
conf = result["conf"].as<string>();
report = result["report"].as<string>();
host = result["host"].as<string>();
port = result["port"].as<string>();
shm = result["shm"].as<string>();
};
//string input(){return input;};
};/*
{"watch", 0, 0, (int)('w')},
{"verbose", 0, 0, (int)('v')},
{"help", 0, 0, (int)('?')},
{"version", 0, 0, (int)('V')},
{"ping", 0, 0, (int)('P')},
{"info", 0, 0, (int)('I')},
{"bg", 0, 0, (int)('b')},
{"report", 1, 0, (int)('r')},
{"fails", 1, 0, (int)('f')},
{"tool", 1, 0, (int)('t')},
{"outsort", 1, 0, (int)('O')},
*/
int main(int argc, char** argv)
{
Cline* cline = new Cline(argc,argv);
cout << cline->input << endl;
cout << cline->output << endl;
cout << cline->conf << endl;
return 0;
}
#include <stdlib.h>
#include <string>
#include <string_view>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "parser.cpp"
using namespace::std;
struct Process{
string pid;
string uid;
string chunkinfile;
string chunkoutfile;
string cmd;
uint64_t rtime;
uint64_t utime;
uint64_t ktime;
uint64_t rcode;
//size_t chunkoutsize;
//char* chunkoutmap;
string_view chunkout_view;
Process(string command, string tmpdir){
//basic template to construct temporary files from command
string basicfilename("pld");
string chunkinsentinel("#chunkin#");
string chunkoutsentinel("#chunkout#");
cmd = command;
//get pid and uid to construct a unique file for each client process
uid = to_string(getuid());
pid = to_string(getpid());
//name of file: /tmporaryfile/pld_uid_pid
chunkinfile = tmpdir + basicfilename + "in" + "_" + uid + "_" + pid;
chunkoutfile = tmpdir + basicfilename + "out" + "_" + uid + "_" + pid;
//replace sentinels by temporary files
cmd = command.replace(cmd.find(chunkinsentinel),chunkinsentinel.size(),chunkinfile);
cout << cmd << endl;
cmd = command.replace(cmd.find(chunkoutsentinel),chunkoutsentinel.size(),chunkoutfile);
cout << cmd << endl;
}
int write_chunkin(string_view chunkin){
int fd = open(chunkinfile.c_str(), O_RDWR | O_CREAT, static_cast<mode_t>(0644));
// !!!!!! should destroy file if fails
if (write(fd, chunkin.data(), chunkin.size()) != static_cast<ssize_t>(chunkin.size())){return(1);};
if (fsync(fd) != 0){return(1);};
close(fd);
return(0);
}
/*
uint64_t hash_chunkin(){
struct stat sfd;
int fd;
fd = open(chunkoutfile.c_str(), O_RDONLY);
fstat(fd,&sfd);
chunkoutsize = sfd.st_size;
chunkoutmap = (char *)mmap(NULL, chunkoutsize, PROT_READ, MAP_PRIVATE | MAP_DENYWRITE, fd, 0);
close(fd);
}
*/
void run(){
int timerok = 1;
struct rusage timer;
struct timeval rtimer;
if ((gettimeofday(&rtimer, NULL) == 0) && (getrusage(RUSAGE_CHILDREN,&timer) == 0)){
rtime = UINT64_C(1000000) * (uint64_t)rtimer.tv_sec + (uint64_t)rtimer.tv_usec;
utime = UINT64_C(1000000) * (uint64_t)timer.ru_utime.tv_sec + (uint64_t)timer.ru_utime.tv_usec;
ktime = UINT64_C(1000000) * (uint64_t)timer.ru_stime.tv_sec + (uint64_t)timer.ru_stime.tv_usec;
}
else{
timerok = 0;
}
rcode = static_cast<uint64_t>(system(cmd.c_str()));
if ((gettimeofday(&rtimer, NULL) == 0) && (getrusage(RUSAGE_CHILDREN,&timer) == 0)){
rtime = UINT64_C(1000000) * (uint64_t)rtimer.tv_sec + (uint64_t)rtimer.tv_usec - rtime;
utime = UINT64_C(1000000) * (uint64_t)timer.ru_utime.tv_sec + (uint64_t)timer.ru_utime.tv_usec - utime;
ktime = UINT64_C(1000000) * (uint64_t)timer.ru_stime.tv_sec + (uint64_t)timer.ru_stime.tv_usec - ktime;
}
else{
timerok = 0;
}
if (timerok == 0){
rtime = -1;
utime = -1;
ktime = -1;
}
}
string_view read_chunkout(){
struct stat sfd;
int fd;
size_t chunkoutsize;
char* chunkoutmap;
fd = open(chunkoutfile.c_str(), O_RDONLY);
fstat(fd,&sfd);
chunkoutsize = sfd.st_size;
chunkoutmap = static_cast<char *>(mmap(NULL, chunkoutsize, PROT_READ, MAP_PRIVATE | MAP_DENYWRITE, fd, 0));
close(fd);
string_view view(chunkoutmap, chunkoutsize);
chunkout_view = view;
return(view);
}
void free_chunkout(){
char* chunkoutmap = const_cast<char*>(chunkout_view.data());
size_t chunkoutsize = chunkout_view.size();
munmap(chunkoutmap, chunkoutsize);
}
};
int main(int argc, char** argv){
Process* pr = new Process("cat #chunkin# > #chunkout# && sleep 1", "/dev/shm/");
Parser* pa = new Parser(argv);
pr->write_chunkin(pa->get_chunk(4));
pr->run();
cout << pr->rtime << endl;
cout << pr->utime << endl;
cout << pr->ktime << endl;
cout << pr->read_chunkout() << endl;
pr->free_chunkout();
delete pa;
delete pr;
return(0);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment