int SendMessage(int in_reciever, message* in_message, msg_type in_type) { pvm_initsend(PvmDataDefault); pvm_pkbyte((char*)in_message, sizeof(message), 1); return pvm_send(in_reciever, in_type); }
/// user function:add a new query to a pipeline of filters. Called by user manager runs this. /// \param layout System Layout. /// \param work Buffer with a Unit of Work (UoW) /// \param workSize Unit of Work Size (UoW) /// \return Zero on success, -1 on error. int appendWork(Layout *layout, void *work, int workSize) { #ifdef NO_BARRIER // sends work for each filter pvm_initsend(PvmDataRaw); // First tell that is a mensage of WORK int msgType = MSGT_WORK; pvm_pkint(&msgType, 1, 1); //then attach the work to it pvm_pkbyte((char *)work, workSize, 1); // for each filter, send his work for(i = 0; i < layout->numFilters; i++) { FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement); // sends work to all filters of this set pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0); } #else int i; int totalEows = 0, numEowsReceived; #ifdef ATTACH int totalAttachedFilters = 0; #endif int reconf = 0 /** should we reconfigure? */, remainingReconfs = 0; //how many times should we try? #ifdef VOID_INST #ifdef BMI_FT char instDir[MAX_IDIR_LENGTH]; sprintf(instDir, "%s/", INST_DIR); for (i=0; i < layout->numFilters-1; i++) { if (strlen(instDir) >= (MAX_IDIR_LENGTH - 3)) { //dont want to overflow this array fprintf(stderr, "%s %d: warning, instrumentation directory name too big, truncating to %s\n", __FILE__, __LINE__, instDir); break; } sprintf(instDir, "%s%d-", instDir, layout->filters[i]->filterPlacement.numInstances); } sprintf(instDir, "%s%d", instDir, layout->filters[layout->numFilters-1]->filterPlacement.numInstances); char managerLogFile[MAX_IDIR_LENGTH+20]; sprintf(managerLogFile, "%s/manager.time", instDir); FILE *fp = fopen(managerLogFile, "w"); struct timeval tv; struct timezone tz; //not used //get the time gettimeofday(&tv, &tz); if(fp != NULL) fprintf(fp, "1 %ld %ld\n", tv.tv_sec, tv.tv_usec); #endif #endif //before sending, we check if we received any filter error int bufid = pvm_probe(-1, MSGT_FERROR); if (bufid != 0) { int bytes, tag, tid; char *msg; pvm_bufinfo(bufid, &bytes, &tag, &tid); msg = (char*)malloc(bytes+1); pvm_recv(tid, MSGT_FERROR); pvm_upkbyte(msg, bytes, 1); msg[bytes] = '\0'; fprintf(stderr, "Manager.c: Error, received death notification\n"); fprintf(stderr, "Manager.c: %s\n", msg); free(msg); killAllFilters(layout); exit(-1); } printf("Manager.c: starting work...\n"); // number of EOWs we expect to receive for(i = 0; i < layout->numFilters; i++) { totalEows += layout->filters[i]->filterPlacement.numInstances; #ifdef ATTACH if(layout->filters[i]->attached) { totalAttachedFilters += layout->filters[i]->filterPlacement.numInstances; } // TODO:TOSCO::: if(layout->filters[i]->attach) return 0; #endif } //we stay in this loop while we have to reconfigure //usually, this will be only one time, unless a we get some reconf message do { // sends work for each filter pvm_initsend(PvmDataRaw); int msgType; #ifdef VOID_FT if(!reconf) { #endif // First tell that is a mensage of WORK msgType = MSGT_WORK; #ifdef VOID_FT } else { // one fault has occurred msgType = MSGT_FT; } #endif pvm_pkint(&msgType, 1, 1); //then attach the work to it pvm_pkbyte((char *)work, workSize, 1); reconf = 0; //we are optimistic, always expect to not reconfigure // for each filter, send his work for(i = 0; i < layout->numFilters; i++) { FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement); // sends work to all filters of this set pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0); } /* TaskIdList *finalTaskIdList = NULL, *currentTaskIdList; int filtersThatUseTasks = 0; // Manager receives filter's terminated tasks list for(i = 0; i < layout->numFilters; i++) { FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement); #ifdef ATTACH // if this filter is of the attached // type i dont need worry about use Task // if(layout->filters[i]->attached)continue; #endif for(j = 0; j < pFilterP->numInstances; j++) { int instanceUseTasks = -1; // Get is this filter use tasks pvm_recv(pFilterP->tids[j], 0); pvm_upkint(&instanceUseTasks, 1, 1); layout->filters[i]->useTasks = instanceUseTasks; #ifdef VOID_FT if (instanceUseTasks) { currentTaskIdList = (TaskIdList *)unpackTaskIdList(); // Para fazer intersecao, gerente ordenar? as listas de tarefas recebidas e utilizar? a fun??o meet() do CrazyMiner/ID3. qsort(currentTaskIdList->vetor, currentTaskIdList->size, sizeof(int), compareTaskId); if(finalTaskIdList == NULL) { finalTaskIdList = currentTaskIdList; } else { // Manager makes the intersection of all finished tasks lists finalTaskIdList = taskIdListIntersection(finalTaskIdList, currentTaskIdList); taskIdListDestroy(currentTaskIdList); } } #endif } // for if (layout->filters[i]->useTasks) filtersThatUseTasks++; } // Gerente devolve resultado das intersecoes para todas as instancias de todos os filtros. for(i = 0; i < layout->numFilters; i++) { FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement); #ifdef ATTACH // if this filter is of the attached // type i dont need worry about use Task //TODO if(layout->filters[i]->attached)continue; #endif int needForwardTaskMessages = 1; if (filtersThatUseTasks < 2) needForwardTaskMessages = 0; #ifdef VOID_FT if (layout->filters[i]->useTasks) { // Send if they should forward task messages // and pigback :-) the final task id list pvm_initsend(PvmDataDefault); pvm_pkint(&needForwardTaskMessages, 1, 1); packTaskIdList(finalTaskIdList); pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0); } else { #endif // Only send if they should forward task messages pvm_initsend(PvmDataDefault); pvm_pkint(&needForwardTaskMessages, 1, 1); pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0); #ifdef VOID_FT } #endif } taskIdListDestroy(finalTaskIdList); */ //now we receive the EOWs numEowsReceived = 0; //now we expect to receive EOW or errors while(numEowsReceived < totalEows) { //we are open to receive anything from anyone here //all messages to the manager should be tagged, so we now their type int szMsg = -1; int inst_tid = -1; int msgTag = -1; int bufid = pvm_recv(-1, -1); pvm_bufinfo(bufid, &szMsg, &msgTag, &inst_tid); switch (msgTag) { case (MSGT_EOW): { //received EOW, expect this usually int instance = -1; FilterSpec *pFilter = NULL; getFilterByTid(layout, inst_tid, &pFilter, &instance); if ((pFilter != NULL) && (instance != -1)) { printf("Manager.c: EOW received from %s, instance %d\n", pFilter->name, instance); } else { fprintf(stderr, "Manager.c: unknown EOW received! Shouldnt get here!\n"); } numEowsReceived++; break; } case (MSGT_AEXIT): case (MSGT_FERROR): { //someone called dsExit or system error at the filter side //common cause for this are library not found, wrong initscritpt etc char *message = (char*)malloc(sizeof(char)*szMsg+1); pvm_upkbyte(message, szMsg, 1); message[szMsg] = '\0'; //the filter and the instance FilterSpec *fp = NULL; int instance = -1; getFilterByTid(layout, inst_tid, &fp, &instance); if (msgTag == MSGT_AEXIT) { printf("Manager.c: Filter %s, instance %d(tid %x) called dsExit: %s\n", fp->name, instance, inst_tid, message); } else { printf("Manager.c: Filter %s error, instance %d(tid %x) called exit: %s\n", fp->name, instance, inst_tid, message); } free(message); // kill all instances killAllFilters(layout); exit(-1); break; } //task exited or host crashed case (MSGT_TEXIT): case (MSGT_HDEL): { //we only reconfigure a fixed number of times if (remainingReconfs <= 0) { //max number of reconfigurations reached... aborting fprintf(stderr, "Manager.c: max reconfigurations reached, aborting...\n"); fflush(stderr); fprintf(stdout, "Manager.c: max reconfigurations reached, aborting...\n"); fflush(stdout); reconf = 0; // kill all instances which might be alive killAllFilters(layout); exit(-1);; } #ifdef BMI_FT gettimeofday(&tv, &tz); fprintf(fp, "2 %ld %ld\n", tv.tv_sec, tv.tv_usec); #endif remainingReconfs--; reconf = 1; // In case of pvm notification, inst_tid will be t80000000 int notifiesRecv = 1; // We are receiving the first death notification int deadFilterTid = -1; FilterSpec *pFilter = NULL; int instanceDead = -1; // Get the tid and name of the dead filter int info = pvm_upkint(&deadFilterTid, 1, 1); if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint"); //discover which filter died getFilterByTid(layout, deadFilterTid, &pFilter, &instanceDead); if((pFilter != NULL) && (instanceDead != -1)) { if (msgTag == MSGT_TEXIT) { fprintf(stderr, "Manager.c: filter %s: instance %d (tid t%x) of %d is dead!!!\n", pFilter->name, instanceDead, deadFilterTid, pFilter->filterPlacement.numInstances); } else { fprintf(stderr, "Manager.c: filter %s: instance %d (tid t%x) of %d machine's crashed!!!\n", pFilter->name, instanceDead, deadFilterTid, pFilter->filterPlacement.numInstances); } } printf("Manager.c: starting reconfiguration\n"); // kill all filters in the pipeline killAllFilters(layout); if (msgTag == MSGT_HDEL) { //int his case, host died, so we must change layout replaceCrashedHost(layout, pFilter, instanceDead); } #ifdef ATTACH if (pFilter->attached) { // In this case, all filters that were killed have to notify // their dead. notifiesRecv = 0; } #endif //Flush the streams //receive all messages which are about to arrive till we get the death notification //pvm order should garantee this #ifdef ATTACH while (notifiesRecv < (totalEows - totalAttachedFilters)) { #else while (notifiesRecv < totalEows) { #endif int newMsgTag = -1; bufid = pvm_recv(-1, MSGT_TEXIT); info = pvm_bufinfo(bufid, NULL, &newMsgTag, &inst_tid); info = pvm_upkint(&deadFilterTid, 1, 1); if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint"); fprintf(stderr, "Manager.c: WARNING: received notification (tag %d) about pvm tid t%x death\n", newMsgTag, deadFilterTid); notifiesRecv++; } #ifdef ATTACH if(pFilter->attached) { notifiesRecv = 1; } else { notifiesRecv = 0; } // Receive all EOW messages from the attached filters. while(notifiesRecv < totalAttachedFilters) { int newMsgTag = -1; bufid = pvm_recv(-1, MSGT_EOW); info = pvm_bufinfo(bufid, NULL, &newMsgTag, &inst_tid); if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint"); fprintf(stderr, "Manager.c: WARNING: received EOW (tag %d) from pvm tid t%x\n", newMsgTag, inst_tid); notifiesRecv++; } #endif // probes for remaining machine crash notifications while (pvm_probe(-1, MSGT_HDEL) > 0) { int newMsgTag = -1; bufid = pvm_recv(-1, MSGT_HDEL); info = pvm_bufinfo(bufid, NULL, &newMsgTag, &inst_tid); info = pvm_upkint(&deadFilterTid, 1, 1); if (info < 0) pvm_perror("Manager.c: error calling pvm_upkint"); fprintf(stderr, "Manager.c: WARNING: received notification (tag %d) about pvm tid t%x machine's crash\n", newMsgTag, deadFilterTid); // Replace the died host FilterSpec *pCrashedFilter = NULL; int crashedInstance = -1; getFilterByTid(layout, deadFilterTid, &pCrashedFilter, &crashedInstance); replaceCrashedHost(layout, pCrashedFilter, crashedInstance); } #ifdef BMI_FT updateAllFiltersFaultStatus(layout, FAULT_OTHER_FILTER_INST); pFilter->faultStatus = instanceDead; #endif //spawn all filters again spawnAllFilter(layout); #ifdef ATTACH // Verifies if the dead filter is an attached. If yes, spawn it. if(pFilter->attached == 1) { spawnOneAttachedInstance(layout, pFilter, instanceDead); } #endif resetStreams(layout); //resend the data sendFiltersData(layout); //start all over again numEowsReceived = 0; #ifdef BMI_FT gettimeofday(&tv, &tz); fprintf(fp, "3 %ld %ld\n", tv.tv_sec, tv.tv_usec); #endif break; } #ifdef VOID_TERM // One filter instance detected local termination case (MSGT_LOCALTERM): { int localTermTag; // filter instance local termination tag pvm_upkint(&localTermTag, 1, 1); verifyGlobalTermination(inst_tid, localTermTag); break; } #endif default: { fprintf(stderr, "Manager.c: error receiving EOW, unknown tag!!!\n"); } } //end switch message tag if((msgTag == MSGT_TEXIT) || (msgTag == MSGT_HDEL)) { // work should be sent again break; } } //end receiving eows } while(reconf == 1); //leave this loop if we will not reconfigure #ifdef BMI_FT gettimeofday(&tv, &tz); fprintf(fp, "4 %ld %ld\n", tv.tv_sec, tv.tv_usec); #endif printf("Manager.c: Work ended\n\n"); return 0; #endif } /// Finalize a Void pipeline. Only manager runs this. int finalizeDs(Layout *layout) { #ifdef NO_BARRIER #else int i; // Envia eof para todos os filtros // Primeiro envia se eh work (WORK) ou EOF (END_OF_FILTER) pvm_initsend(PvmDataRaw); int tipo_msg = MSGT_EOF; pvm_pkint(&tipo_msg, 1, 1); //sends the EOF message for all instances of the filter for(i = 0; i < layout->numFilters; i++) { FilterPlacement *pFilterP = &(layout->filters[i]->filterPlacement); #ifdef ATTACH if(layout->filters[i]->attach) continue;// this filter cant not receive a EOF because // it needs still runnig #endif pvm_mcast(pFilterP->tids, pFilterP->numInstances, 0); } destroyLayout(layout); pvm_exit(); return 0; #endif }
/** This sends filter data to all filters we have in the layout * \param layout the void layout */ static void sendFiltersData(Layout *layout) { FilterSpec *pFilter; int i, j, k, x; printf("Manager.c: sending filter data now\n"); #ifdef VOID_INST //build instrumentation directory(a string like 1-1-1, which is the number of instances of all filters) //this is the same for all filters, so we build here and send inside the loop char instDir[MAX_IDIR_LENGTH]; sprintf(instDir, "%s/", INST_DIR); for (i=0; i < layout->numFilters-1; i++) { if (strlen(instDir) >= (MAX_IDIR_LENGTH - 3)) { //dont want to overflow this array fprintf(stderr, "%s %d: warning, instrumentation directory name too big, truncating to %s\n", __FILE__, __LINE__, instDir); break; } sprintf(instDir, "%s%d-", instDir, layout->filters[i]->filterPlacement.numInstances); } sprintf(instDir, "%s%d", instDir, layout->filters[layout->numFilters-1]->filterPlacement.numInstances); #endif for(i = 0; i < layout->numFilters; i++) { pFilter = layout->filters[i]; #ifdef ATTACH // if this is a filter of the type attach it wait for another process attach him // so we dont need send information about layout now, because the process tha // attach this filter go to do it. if(pFilter->attach) continue; #endif //for each instance of the filter, we send its data for (j=0; j < pFilter->filterPlacement.numInstances; j++) { int l1, l2, l3, l4, l5, l6, l7, res, pTid; //we send only one message with all information inside pvm_initsend(PvmDataRaw); //Teste //current working directory l1 = strlen(layout->cwd); pvm_pkint(&l1, 1, 1); pvm_pkbyte(layout->cwd, l1, 1); //send filterID pvm_pkint(&i, 1, 1); // rank pvm_pkint(&j, 1, 1); // total number of instances of this filter pvm_pkint(&pFilter->filterPlacement.numInstances, 1, 1); //send all tids of this filter pvm_pkint(pFilter->filterPlacement.tids, pFilter->filterPlacement.numInstances, 1); #ifdef ATTACH pTid = pvm_mytid(); // if you are using attach your parent in this moment // can be other process and not the guy that create you // so the parent must send his tid pvm_pkint(&pTid, 1, 1); // this said if this filter have been attached to this pipeline // its usefull because this filter cant finish with a msg of // the type EOF from this manager pvm_pkint(&pFilter->attached ,1, 1); #endif #ifdef BMI_FT // indicates the fault status: NO_FAULT, FAULT_OTHER_FILTER_INST, // or filter instance rank pvm_pkint(&pFilter->faultStatus, 1, 1); // if this filter is the last filter in the pipeline pvm_pkint(&pFilter->lastFilter, 1, 1); #endif // filtername l2 = strlen(pFilter->name); pvm_pkint(&l2, 1, 1); pvm_pkbyte(pFilter->name, l2, 1); //machine memory HostsStruct *h = layout->hostsStruct; int hostIndex = hostsGetIndexByName(h, pFilter->filterPlacement.hosts[j]); int memory = hostsGetMemory(h, hostIndex); pvm_pkint(&memory, 1, 1); //how many brothers do I have in this machine?(used for memory management) int z, numLocalInstances = 0; // me and my brothers for (z=0; z < pFilter->filterPlacement.numInstances; z++) { if (strncmp(pFilter->filterPlacement.hosts[z], pFilter->filterPlacement.hosts[j], MAX_HNAME_LENGTH) == 0) { numLocalInstances++; } } pvm_pkint(&numLocalInstances, 1, 1); #ifdef VOID_INST int lInst; //send instrumentation directory lInst = strlen(instDir); pvm_pkint(&lInst, 1, 1); pvm_pkbyte(instDir, lInst, 1); #endif // shared lib name l3 = strlen(pFilter->libname); pvm_pkint(&l3, 1, 1); pvm_pkbyte(pFilter->libname, l3, 1); //port data //numOutputs pvm_pkint(&pFilter->numOutputs, 1, 1); //numInputs pvm_pkint(&pFilter->numInputs, 1, 1); // OutputPorts // for each OutputPort // Alterado por FELIPE // Because now every filter output can have // many destinations, we are implementing a // loop to send the information of each destination filter for(k = 0; k < pFilter->numOutputs; k++) { //port data //ITAMAR numToSend = numbers of 'to' one from has pvm_pkint(&(pFilter->outputs[k]->numToSend), 1, 1); l4 = strlen(pFilter->outputs[k]->fromPortName); pvm_pkint(&l4, 1, 1); pvm_pkbyte(pFilter->outputs[k]->fromPortName, l4, 1); //portname //stream tag pvm_pkint(&pFilter->outputs[k]->tag, 1, 1); for( x = 0; x < pFilter->outputs[k]->numToSend; x++ ) { //number of tids & tids pvm_pkint(&pFilter->outputs[k]->toFilter[x]->filterPlacement.numInstances, 1, 1); pvm_pkint(pFilter->outputs[k]->toFilter[x]->filterPlacement.tids, pFilter->outputs[k]->toFilter[x]->filterPlacement.numInstances, 1); //tids //write policy name l5 = strlen(pFilter->outputs[k]->writePolicyName[x]); pvm_pkint(&l5, 1, 1); pvm_pkbyte(pFilter->outputs[k]->writePolicyName[x], l5, 1); writePolicy_t wp = getWritePolicyByName(pFilter->outputs[k]->writePolicyName[x]); // send labeled stream libname if policy is LS if ( (wp == LABELED_STREAM) || (wp == MULTICAST_LABELED_STREAM) ) { l6 = strlen(pFilter->outputs[k]->lsLibName[x]); pvm_pkint(&l6, 1, 1); pvm_pkbyte(pFilter->outputs[k]->lsLibName[x], l6, 1); } else { //if not LS, we need know who will be the first instance to receive msgs //else we can create hotspots //we use the rank % number of receiving instances res = j % pFilter->outputs[k]->toFilter[x]->filterPlacement.numInstances; pvm_pkint(&res, 1, 1); } } } // InputPorts // foreach InputPort for(k = 0; k < pFilter->numInputs; k++) { int ind_toPort; // Search for the index corresponding to current filter in the // destinations of current stream for(x = 0; x < pFilter->inputs[k]->numToSend; x++) { if( strcmp(pFilter->name, pFilter->inputs[k]->toFilter[x]->name) == 0 ) { // Found it! ind_toPort = x; break; } } //portName l7 = strlen(pFilter->inputs[k]->toPortName[ind_toPort]); pvm_pkint(&l7, 1, 1); pvm_pkbyte(pFilter->inputs[k]->toPortName[ind_toPort], l7, 1); //number of tids we listen to pvm_pkint(&pFilter->inputs[k]->fromFilter->filterPlacement.numInstances, 1, 1); // number of instances pvm_pkint(pFilter->inputs[k]->fromFilter->filterPlacement.tids, pFilter->inputs[k]->fromFilter->filterPlacement.numInstances, 1); //the tids of the instances pvm_pkint(&pFilter->inputs[k]->tag, 1, 1); //the stream tag writePolicy_t wp = getWritePolicyByName(pFilter->inputs[k]->writePolicyName[x]); int aux = 0; // send labeled stream libname if policy is LS if ( (wp == LABELED_STREAM) || (wp == MULTICAST_LABELED_STREAM) ) { // yes it has a label aux = 1; pvm_pkint(&aux, 1, 1); l6 = strlen(pFilter->inputs[k]->lsLibName[x]); pvm_pkint(&l6, 1, 1); pvm_pkbyte(pFilter->inputs[k]->lsLibName[x], l6, 1); } else { pvm_pkint(&aux, 1, 1); } } pvm_send(pFilter->filterPlacement.tids[j], 0); } } }
int main(int argc, char** argv) { int pid = pvm_mytid(); if (argc != 2 && argc != 3) stop("Usage: tm_driver <control_file> [TM_tid]\n"); int tm_tid = 0; char * control_file = strdup(argv[1]); if (argc == 3) sscanf(argv[2], "t%x", &tm_tid); int info = 0; // Get the machine configuration int nhost = 0; int narch = 0; struct pvmhostinfo *hostp = 0; info = pvm_config(&nhost, &narch, &hostp); // Parse the control file int to_delete_size = 0; // # of machsto delete char ** to_delete = 0; // names of machs to delete int to_add_size = 0; // # of machsto add char ** to_add = 0; // names of machs to add int delete_proc_num = 0; // # of procs to delete int * tid_delete = 0; // the tids of procs to delete // # of various procs to start int lp_num = 0; int cg_num = 0; int vg_num = 0; int cp_num = 0; int vp_num = 0; // the mach names where the procs shoud be started char ** lp_mach = 0; char ** cg_mach = 0; char ** vg_mach = 0; char ** cp_mach = 0; char ** vp_mach = 0; // Do the parsing. First count ifstream ctl(control_file); if (!ctl) stop("Cannot open parameter file... Aborting.\n"); // Get the lines of the parameter file one-by-one and if a line contains a // (keyword, value) pair then interpret it. const int MAX_PARAM_LINE_LENGTH = 1024; char line[MAX_PARAM_LINE_LENGTH+1], *end_of_line, *keyword, *value, *ctmp; char ch; while (ctl) { ctl.get(line, MAX_PARAM_LINE_LENGTH); if (ctl) { ctl.get(ch); if (ch != '\n') { printf("Too long (>= %i chars) line in the parameter file.\n", MAX_PARAM_LINE_LENGTH); stop("This is absurd. Aborting.\n"); } } end_of_line = line + strlen(line); //-------------------------- First separate the keyword and value ------ keyword = find_if(line, end_of_line, isgraph); if (keyword == end_of_line) // empty line continue; ctmp = find_if(keyword, end_of_line, isspace); if (ctmp == end_of_line) // line is just one word. must be a comment continue; *ctmp = 0; // terminate the keyword with a 0 character ++ctmp; value = find_if(ctmp, end_of_line, isgraph); if (value == end_of_line) // line is just one word. must be a comment continue; ctmp = find_if(value, end_of_line, isspace); *ctmp = 0; // terminate the value with a 0 character. this is good even // if ctmp == end_ofline if (str_eq(keyword, "BCP_delete_machine")) { ++to_delete_size; } else if (str_eq(keyword, "BCP_add_machine")) { ++to_add_size; } else if (str_eq(keyword, "BCP_delete_proc")) { ++delete_proc_num; } else if (str_eq(keyword, "BCP_lp_process")) { ++lp_num; } else if (str_eq(keyword, "BCP_cg_process")) { ++cg_num; } else if (str_eq(keyword, "BCP_vg_process")) { ++vg_num; } else if (str_eq(keyword, "BCP_cp_process")) { ++cp_num; } else if (str_eq(keyword, "BCP_vp_process")) { ++vp_num; } } ctl.close(); if (to_delete_size > 0) { to_delete = new char*[to_delete_size]; to_delete_size = 0; } if (to_add_size > 0) { to_add = new char*[to_add_size]; to_add_size = 0; } if (delete_proc_num > 0) { tid_delete = new int[delete_proc_num]; delete_proc_num = 0; } if (lp_num) { lp_mach = new char*[lp_num]; lp_num = 0; } if (cg_num) { cg_mach = new char*[cg_num]; cg_num = 0; } if (vg_num) { vg_mach = new char*[vg_num]; vg_num = 0; } if (cp_num) { cp_mach = new char*[cp_num]; cp_num = 0; } if (vp_num) { vp_mach = new char*[vp_num]; vp_num = 0; } ctl.open(control_file); while (ctl) { ctl.get(line, MAX_PARAM_LINE_LENGTH); if (ctl) { ctl.get(ch); if (ch != '\n') { printf("Too long (>= %i chars) line in the parameter file.\n", MAX_PARAM_LINE_LENGTH); stop("This is absurd. Aborting.\n"); } } end_of_line = line + strlen(line); //-------------------------- First separate the keyword and value ------ keyword = find_if(line, end_of_line, isgraph); if (keyword == end_of_line) // empty line continue; ctmp = find_if(keyword, end_of_line, isspace); if (ctmp == end_of_line) // line is just one word. must be a comment continue; *ctmp = 0; // terminate the keyword with a 0 character ++ctmp; value = find_if(ctmp, end_of_line, isgraph); if (value == end_of_line) // line is just one word. must be a comment continue; ctmp = find_if(value, end_of_line, isspace); *ctmp = 0; // terminate the value with a 0 character. this is good even // if ctmp == end_ofline if (str_eq(keyword, "BCP_delete_machine")) { to_delete[to_delete_size++] = strdup(value); } else if (str_eq(keyword, "BCP_add_machine")) { to_add[to_add_size++] = strdup(value); } else if (str_eq(keyword, "BCP_delete_proc")) { sscanf(value, "t%x", &tid_delete[delete_proc_num++]); } else if (str_eq(keyword, "BCP_lp_process")) { lp_mach[lp_num++] = strdup(value); } else if (str_eq(keyword, "BCP_cg_process")) { cg_mach[cg_num++] = strdup(value); } else if (str_eq(keyword, "BCP_vg_process")) { vg_mach[vg_num++] = strdup(value); } else if (str_eq(keyword, "BCP_cp_process")) { cp_mach[cp_num++] = strdup(value); } else if (str_eq(keyword, "BCP_vp_process")) { vp_mach[vp_num++] = strdup(value); } } ctl.close(); // Check that machine deletions and additions are correct char ** last = 0; // Are there duplicates on the to be deleted list ? if (to_delete_size > 0) { sort(to_delete, to_delete + to_delete_size, str_lt); last = unique(to_delete, to_delete + to_delete_size, str_eq); if (to_delete_size != last - to_delete) stop("A machine to be deleted is listed twice... Aborting.\n"); } // Are there duplicates on the to be added list? if (to_add_size > 0) { sort(to_add, to_add + to_add_size, str_lt); last = unique(to_add, to_add + to_add_size, str_eq); if (to_add_size != last - to_add) stop("A machine to be added is listed twice... Aborting.\n"); } int i; char ** mach_list = new char*[nhost + to_add_size]; for (i = 0; i < nhost; ++i) mach_list[i] = strdup(hostp[i].hi_name); sort(mach_list, mach_list + nhost, str_lt); char ** current_list = new char*[nhost + to_add_size]; // Is there a nonexisting machine to be deleted? if (to_delete_size > 0) { last = set_difference(to_delete, to_delete + to_delete_size, mach_list, mach_list + nhost, current_list, str_lt); if (last != current_list) stop("A nonexisting machine is to be deleted... Aborting.\n"); last = set_difference(mach_list, mach_list + nhost, to_delete, to_delete + to_delete_size, current_list, str_lt); ::swap(mach_list, current_list); } // Is there an already existing machine to be added? if (to_add_size > 0) { last = set_intersection(to_add, to_add + to_add_size, mach_list, mach_list + nhost, current_list, str_lt); if (last != current_list) stop("A machine to be added is already there... Aborting.\n"); last = merge(to_add, to_add + to_add_size, mach_list, mach_list + nhost, current_list, str_lt); ::swap(mach_list, current_list); } const int mach_num = nhost - to_delete_size + to_add_size; // Check that the machines the new processes are supposed to be started on // really exist. if (lp_num > 0) { sort(lp_mach, lp_mach + lp_num, str_lt); if (set_difference(lp_mach, lp_mach + lp_num, mach_list, mach_list + mach_num, current_list, str_lt) != current_list) stop("An lp machine is not in the final machine list... Aborting.\n"); } if (cg_num > 0) { sort(cg_mach, cg_mach + cg_num, str_lt); if (set_difference(cg_mach, cg_mach + cg_num, mach_list, mach_list + mach_num, current_list, str_lt) != current_list) stop("An cg machine is not in the final machine list... Aborting.\n"); } if (vg_num > 0) { sort(vg_mach, vg_mach + vg_num, str_lt); if (set_difference(vg_mach, vg_mach + vg_num, mach_list, mach_list + mach_num, current_list, str_lt) != current_list) stop("An vg machine is not in the final machine list... Aborting.\n"); } if (cp_num > 0) { sort(cp_mach, cp_mach + cp_num, str_lt); if (set_difference(cp_mach, cp_mach + cp_num, mach_list, mach_list + mach_num, current_list, str_lt) != current_list) stop("An cp machine is not in the final machine list... Aborting.\n"); } if (vp_num > 0) { sort(vp_mach, vp_mach + vp_num, str_lt); if (set_difference(vp_mach, vp_mach + vp_num, mach_list, mach_list + mach_num, current_list, str_lt) != current_list) stop("An vp machine is not in the final machine list... Aborting.\n"); } // Find the tree manager find_tree_manager(pid, tm_tid); // Check that the TM is not on one of the machines to be deleted. if (to_delete_size > 0) { const int dtid = pvm_tidtohost(tm_tid); for (i = 0; i < nhost; ++i) { if (hostp[i].hi_tid == dtid) for (int j = 0; j < to_delete_size; ++j) { if (str_eq(hostp[i].hi_name, to_delete[j])) stop("Can't delete the machine the TM is on. Aborting.\n"); } } } // Check that the TM is not one of the processes to be deleted if (delete_proc_num > 0) { if (find(tid_delete, tid_delete + delete_proc_num, tm_tid) != tid_delete + delete_proc_num) stop("Can't delete the TM... Aborting.\n"); } // Modify the machine configuration if (to_delete_size > 0 || to_add_size > 0) { int * infos = new int[max(to_delete_size, to_add_size)]; if (to_delete_size > 0) if (pvm_delhosts(to_delete, to_delete_size, infos) < 0) { printf("Failed to delete all specified machines...\n"); stop("Please check the situation manually... Aborting.\n"); } if (to_add_size > 0) if (pvm_addhosts(to_add, to_add_size, infos) < 0) { printf("Failed to add all specified machines...\n"); stop("Please check the situation manually... Aborting.\n"); } } // Kill the processes to be killed for (i = 0; i < delete_proc_num; ++i) pvm_kill(tid_delete[i]); // Put together a message to be sent to the TM that contains the machine // names on which the new processes should be spawned int len = (lp_num + cg_num + vg_num + cp_num + vp_num) * sizeof(int); if (len > 0) { len += 5 * sizeof(int); for (i = 0; i < lp_num; ++i) len += strlen(lp_mach[i]); for (i = 0; i < cg_num; ++i) len += strlen(cg_mach[i]); for (i = 0; i < vg_num; ++i) len += strlen(vg_mach[i]); for (i = 0; i < cp_num; ++i) len += strlen(cp_mach[i]); for (i = 0; i < vp_num; ++i) len += strlen(vp_mach[i]); char * buf = new char[len]; memcpy(buf, &lp_num, sizeof(int)); buf += sizeof(int); for (i = 0; i < lp_num; ++i) { const int l = strlen(lp_mach[i]); memcpy(buf, &l, sizeof(int)); buf += sizeof(int); memcpy(buf, lp_mach[i], l); buf += l; } memcpy(buf, &cg_num, sizeof(int)); buf += sizeof(int); for (i = 0; i < cg_num; ++i) { const int l = strlen(cg_mach[i]); memcpy(buf, &l, sizeof(int)); buf += sizeof(int); memcpy(buf, cg_mach[i], l); buf += l; } memcpy(buf, &vg_num, sizeof(int)); buf += sizeof(int); for (i = 0; i < vg_num; ++i) { const int l = strlen(vg_mach[i]); memcpy(buf, &l, sizeof(int)); buf += sizeof(int); memcpy(buf, vg_mach[i], l); buf += l; } memcpy(buf, &cp_num, sizeof(int)); buf += sizeof(int); for (i = 0; i < cp_num; ++i) { const int l = strlen(cp_mach[i]); memcpy(buf, &l, sizeof(int)); buf += sizeof(int); memcpy(buf, cp_mach[i], l); buf += l; } memcpy(buf, &vp_num, sizeof(int)); buf += sizeof(int); for (i = 0; i < vp_num; ++i) { const int l = strlen(vp_mach[i]); memcpy(buf, &l, sizeof(int)); buf += sizeof(int); memcpy(buf, vp_mach[i], l); buf += l; } buf -= len; pvm_initsend(PvmDataRaw); pvm_pkbyte(buf, len, 1); pvm_send(tm_tid, BCP_CONFIG_CHANGE); int bufid = pvm_recv(tm_tid, -1); int bytes = 0, msgtag = 0; pvm_bufinfo(bufid, &bytes, &msgtag, &tm_tid); if (msgtag == BCP_CONFIG_ERROR) stop("TM had difficulties. Please check the situation manually.\n"); } pvm_exit(); return 0; }
void pkjobinfo_done(jobinfo *j) { pvm_pkint(&j->jid,3,1); // jid, tid, pjid pvm_pkint(&j->s.k,4,1); // k,o,r,rd pvm_pkbyte(j->s.m,MAXMVC,1); }
int main (int argc, char* argv[]) { if(argc !=4) { printf("usage : ./craquage p r m\n"); return EXIT_FAILURE; } //Initialisation des variables int nb_esclaves = atoi(argv[1]); int* tids = (int*) calloc(nb_esclaves, sizeof(int)); int longueur_mdp = atoi(argv[2]); char* mdp = (char*) calloc(strlen(argv[3])+1, sizeof(char)); strcpy(mdp, argv[3]); //declaration de type de tres long entiers (avec bibliotheque GMP) mpz_t debut_sequence, pas_reel, pas, fin_exec; mpz_init(debut_sequence); mpz_init(pas_reel); mpz_init(pas); mpz_init(fin_exec); //recuperation du chemin de l executable char* chemin = getcwd(NULL, 1000); strcat(chemin, "/craquage_esclave"); //creation des arguments pour l esclave char *argv_esclave[3]; argv_esclave[2]=NULL; argv_esclave[0] = (char*) calloc(strlen(argv[2])+1, sizeof(char)); strcpy(argv_esclave[0],argv[2]); argv_esclave[1] = (char*) calloc(strlen(argv[3])+1, sizeof(char)); strcpy(argv_esclave[1],argv[3]); //printf("strlen %lu, %lu\n", (long unsigned)strlen(argv[2]),(long unsigned) strlen(argv[3])); //printf("nb_esclaves %d\n", nb_esclaves); int i; int trouve = 0; int fini = 0; int size; int nb_envoi = 0; int nb_pas = nb_esclaves*longueur_mdp; int nb_changement = 0; char* envoi_char; int bufid, info, bytes, type, source; char * solution; pvm_catchout(stderr); struct timeval tv1, tv2; gettimeofday(&tv1, NULL); pvm_spawn(chemin, argv_esclave, PvmTaskDefault,"", nb_esclaves, tids); //calcul du pas, fin_exec (= fin execution) mpz_set_ui(debut_sequence, 0); mpz_ui_pow_ui(fin_exec, 15, longueur_mdp+1); mpz_sub_ui(fin_exec, fin_exec, 15); mpz_cdiv_q_ui(fin_exec, fin_exec, 14); mpz_set(pas, fin_exec); mpz_cdiv_q_ui(pas, pas, nb_pas); if(mpz_cmp_ui(pas, 0)==0) { mpz_set_ui(pas,1); } //gmp_printf("fin_exec: %Zd\npas:%Zd\ndebut_sequence:%Zd\n",fin_exec, pas, debut_sequence); //boucle principale while(!trouve && fini!=nb_esclaves) { //Attente de reception de donnees d un esclave bufid = pvm_recv( -1, -1 ); info = pvm_bufinfo( bufid, &bytes, &type, &source ); if (info < 0) { printf("Erreur de reception : %d\n", info); exit(1); } //selon le tag du message, demande de donnees ou solution trouvee switch(type) { case(0)://mot de passe trouve solution = calloc(bytes, sizeof(char)); pvm_upkstr(solution); printf("\nLa solution est : %s\n\n", solution); trouve = 1; break; case(1)://esclave veut plus de donnees //prendre en compte la fin des donnees dans le calcul du pas if(nb_changement <= 2 && nb_envoi>=(3*nb_pas/4)) { mpz_cdiv_q_ui(pas, pas, 2); nb_envoi = 0; nb_pas/=2; nb_changement++; } //gmp_printf("fin_exec: %Zd pas:%Zd debut_sequence:%Zd\n",fin_exec, pas, debut_sequence); if(mpz_cmp(debut_sequence, fin_exec)< 0){ mpz_sub(pas_reel, fin_exec, debut_sequence); if(mpz_cmp(pas, pas_reel)<0) { mpz_set(pas_reel, pas); } //envoi des donnes a l esclave pvm_initsend(PvmDataDefault); size = gmp_asprintf(&envoi_char, "%Zd", debut_sequence); pvm_pkint(&size, 1, 1); pvm_pkbyte(envoi_char,size+1, 1); free(envoi_char); size = gmp_asprintf(&envoi_char, "%Zd", pas_reel); pvm_pkint(&size, 1, 1); pvm_pkbyte(envoi_char,size+1 , 1); free(envoi_char); pvm_send(source,0); if(mpz_cmp(pas_reel,pas)!=0) { mpz_set(debut_sequence,fin_exec); } else { mpz_add(debut_sequence, debut_sequence,pas); } nb_envoi++; } else{ fini++ ; printf("Pas de solution pour %d esclave(s)\n", fini); } break; default: break; } } // suppression des esclave for(i=0; i<nb_esclaves;i++) { info = pvm_kill(tids[i]); //printf("Suppression de l esclave %d: retour de pvm_kill: %d\n",i ,info); } pvm_exit(); gettimeofday(&tv2, NULL); printf("%d %ld\n",longueur_mdp,(tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000); mpz_clear(debut_sequence); mpz_clear(pas_reel); mpz_clear(pas); mpz_clear(fin_exec); free(tids); free(mdp); free(argv_esclave[0]); free(argv_esclave[1]); return EXIT_SUCCESS; }