void set_command_status (command_t cmd, outfile_t head) { if (check_dependency (cmd, head) == -1) error (1, 0, "Error check dependency!" ) else if (check_dependency (cmd, head) == 0) { execute_command (cmd, time_travel); wakeup_waitlist(cmd); } else { add_to_waitlist(); //如果dependency>1,add to 多个waitlist,每次wake 则 dependency-1 //当dependency=0时,则execute command } }
void execute_time_travel_stream(command_stream_t command_stream) { //in the time travel mode, the command_stream is the one passed to the arguments command_stream_t track = command_stream_head; //pid_t* children = checked_malloc(runnable * sizeof(pid_t)); //int i = 0; if(track ==NULL) { printf("The command stream is empty\n"); return; } while(track != NULL) { modify_depend_list (track->p_command); if(check_dependency(track->p_command,outfile_head) == 0 ) { pid_t pid = fork(); if(pid == 0) { execute_command(track->p_command, 0); wakeup_waitlist(track->p_command); exit(track->p_command->status); } else if (pid > 0) { //;//children[i] = child; { //int status; // wait for the child process if ( (waitpid(pid, NULL, 0)) == -1) error(1, errno, "Child process error"); // harvest the execution status //printf("parent: %d\n", status); //c->status = WEXITSTATUS(status); //0 indicates that the command is exited } } else error(3, 0, "Cannot create child process."); //i++; track = track ->next; } } // execute ready list }
void build_waitlist (command_stream_t command_stream, outfile_t head) { command_stream_t track = command_stream_head; if(track ==NULL) { error(1,0, "Error: the command stream is empty."); //printf("The command stream is empty\n"); return; } while(track != NULL) { modify_depend_list (track->p_command); int check =check_dependency(track->p_command,outfile_head); printf("check=%d\n",check ); track = track ->next; } }
int* create_dependencygraph(command_stream_t stream) { int i, j; int *dependency_array = (int*) checked_malloc(stream->size * sizeof(int*)); fstruct_t* files_array = (fstruct_t*) checked_malloc(stream->size * sizeof(fstruct_t)); /*initially there are no dependencies */ for (i = 0; i < stream->size; i++) { dependency_array[i] = 0; } /*first get all the files for each command in the command stream and store them in a fstruct array */ for (i = 0; i < stream->size; i++) { files_array[i]= create_fstruct(); get_files(stream->command_array[i], &files_array[i]->ip_array, &files_array[i]->op_array, &files_array[i]->input_size, &files_array[i]->output_size); } /*check the files array and set the dependencies accordingly we start at 1 since the first command never has any dependencies*/ for (i = 1; i < stream->size; i++) { for (j = 0; j < i; j++) { if (check_dependency(files_array[j], files_array[i])) { if ((dependency_array[j] + 1)> dependency_array[i]) { dependency_array[i] = dependency_array[j] +1; } } } } return dependency_array; }
void execute_time_travel_stream(command_stream_t command_stream) { //in the time travel mode, the command_stream is the one passed to the arguments command_stream_t track = command_stream_head; //pid_t* children = checked_malloc(runnable * sizeof(pid_t)); //int i = 0; if(track ==NULL) { printf("The command stream is empty\n"); return; } while(track != NULL) { if(check_dependency(track->p_command,outfile_head) == 0 ) { pid_t pid = fork(); if(pid == 0) { execute_command(track->p_command, 0); wakeup_waitlist(track->p_command); } else if (pid > 0) { ;//children[i] = child; } else error(3, 0, "Cannot create child process."); //i++; track = track ->next; } } // execute ready list }
void execute_time_travel_stream(command_stream_t command_stream) { //in the time travel mode, the command_stream is the one passed to the arguments command_stream_t track = command_stream_head; //pid_t* children = checked_malloc(runnable * sizeof(pid_t)); //int i = 0; if(track ==NULL) { error(1,0, "Error: the command stream is empty."); //printf("The command stream is empty\n"); return; } while(track != NULL) { modify_depend_list (track->p_command); int check = check_dependency(track->p_command,outfile_head); if(check == 0) { (*runnable) ++; add_command_to_run(track->p_command); track->p_command->c_status = RUNNABLE; } track = track->next; } printf("%d\n",*runnable ); command_stream_t run = command_run_head; while(*runnable > 0) { if(run->p_command->c_status==RUNNABLE) { pid_t pid = fork(); if(pid == 0) { printf("%d\n",run->p_command->c_status ); execute_command(run->p_command, 0); //wakeup_waitlist(run->p_command); printf("children\n"); //(*runnable) --; run->p_command->c_status = EXITED; printf("%d\n",run->p_command->c_status ); exit(run->p_command->status); } else if (pid > 0) { //;//children[i] = child; //int status; // wait for the child process //if ( (waitpid(pid, NULL, 0)) == -1) printf ("parent"); pidArray[countArray] = pid; countArray++; //error(1, errno, "Child process error"); // harvest the execution status //printf("parent: %d\n", status); //c->status = WEXITSTATUS(status); //0 indicates that the command is exited } else error(3, 0, "Cannot create child process."); //i++; run = run ->next; } else run = run->next; } int countloop = 0; while (countloop < countArray ) { if (pidArray[countloop] > 0) { waitpid(pidArray[countloop], NULL, 0); printf("count: %d\n",pidArray[countloop]); } countloop++; } // execute ready list }
void wakeup_waitlist(command_t cmd) { printf("wake_up\n"); delete_outfile(cmd->outfile_ptr); command_stream_t track = command_stream_head; //pid_t* children = checked_malloc(runnable * sizeof(pid_t)); //int i = 0; if(track ==NULL) { error(1,0, "Error: the command stream is empty."); //printf("The command stream is empty\n"); return; } while(track != NULL) { if(track->p_command->c_status == RUNNABLE) track = track->next; int check = check_dependency(track->p_command,outfile_head); if(check == 0) { runnable ++; add_command_to_run(track->p_command); track->p_command->c_status = RUNNABLE; } track = track->next; } /*if (cmd -> waitlist_head == NULL) { printf("NULL\n"); return; } else { printf("ADD WAIT\n"); waitlist_t tmp = cmd->waitlist_head; while (tmp != NULL) { tmp->cmd->dependency --; if (tmp->cmd->dependency == 0) { //execute_command (tmp->cmd, 0 ); pid_t pid = fork(); if(pid == 0) { execute_command(tmp->cmd, 0); wakeup_waitlist(tmp->cmd); exit(tmp->cmd->status); } else if (pid > 0) { //;//children[i] = child; { pidArray[countArray] = pid; countArray++; //int status; // wait for the child process //if ( (waitpid(pid, NULL, 0)) == -1) //error(1, errno, "Child process error"); // harvest the execution status //printf("parent: %d\n", status); //c->status = WEXITSTATUS(status); //0 indicates that the command is exited } } else error(3, 0, "Cannot create child process."); //wakeup_waitlist (tmp->cmd); } tmp = tmp->next; } return; }*/ }
/* * For a given filename, see if there exists a filename.ompi_info, which * lists dependencies that must be loaded before this component is * loaded. If we find this file, try to load those components first. * * Detect dependency cycles and error out. */ static int check_ompi_info(component_file_item_t *target_file, opal_list_t *dependencies, opal_list_t *found_components) { size_t len; FILE *fp; char *depname; char buffer[BUFSIZ], *p; /* Form the filename */ len = strlen(target_file->filename) + strlen(ompi_info_suffix) + 16; depname = (char*)malloc(len); if (NULL == depname) return OPAL_ERR_OUT_OF_RESOURCE; snprintf(depname, len, "%s%s", target_file->filename, ompi_info_suffix); /* Try to open the file. If there's no file, return success (i.e., there are no dependencies). */ if (NULL == (fp = fopen(depname, "r"))) { free(depname); return 0; } /* Otherwise, loop reading the lines in the file and trying to load them. Return failure upon the first component that fails to load. */ opal_output_verbose(40, 0, "mca: base: component_find: opening ompi_info file: %s", depname, NULL); while (NULL != fgets(buffer, BUFSIZ, fp)) { /* Perl chomp */ buffer[BUFSIZ - 1] = '\0'; len = strlen(buffer); if ('\n' == buffer[len - 1]) buffer[len - 1] = '\0'; /* Ignore emtpy lines and lines beginning with "#" or "//" */ for (p = buffer; '\0' != p; ++p) if (!isspace(*p)) break; if ('\0' == *p) continue; else if (*p == '#' || ('/' == *p && '/' == *(p + 1))) continue; /* Is it a dependency? */ else if (0 == strncasecmp(p, key_dependency, strlen(key_dependency))) { if (OPAL_SUCCESS != check_dependency(p + strlen(key_dependency), target_file, dependencies, found_components)) { fclose(fp); free(depname); /* We can leave any successfully loaded dependencies; we might need them again later. But free the dependency list for this component, because since [at least] one of them didn't load, we have to pretend like all of them didn't load and disallow loading this component. So free the dependency list. */ free_dependency_list(dependencies); return OPAL_ERR_OUT_OF_RESOURCE; } } } opal_output_verbose(40, 0, "mca: base: component_find: ompi_info file closed (%s)", target_file->basename, NULL); /* All done -- all depenencies satisfied */ fclose(fp); free(depname); return 0; }