int makeflow_clean_file( struct dag *d, struct batch_queue *queue, struct dag_file *f, int silent) { if(!f) return 1; if(batch_fs_unlink(queue, f->filename) == 0) { makeflow_log_file_state_change(d, f, DAG_FILE_STATE_DELETE); debug(D_MAKEFLOW_RUN, "File deleted %s\n", f->filename); } else if(errno != ENOENT) { if(f->state == DAG_FILE_STATE_EXPECT || dag_file_should_exist(f)) makeflow_log_file_state_change(d, f, DAG_FILE_STATE_DELETE); debug(D_MAKEFLOW_RUN, "Makeflow: Couldn't delete %s: %s\n", f->filename, strerror(errno)); return 1; } return 0; }
void makeflow_clean(struct dag *d, struct batch_queue *queue, makeflow_clean_depth clean_depth) { struct dag_file *f; char *name; hash_table_firstkey(d->files); while(hash_table_nextkey(d->files, &name, (void **) &f)) { if(dag_file_is_source(f)) continue; int silent = 1; if(dag_file_should_exist(f)) silent = 0; if(clean_depth == MAKEFLOW_CLEAN_ALL){ makeflow_file_clean(d, queue, f, silent); } else if(set_lookup(d->outputs, f) && (clean_depth == MAKEFLOW_CLEAN_OUTPUTS)) { makeflow_file_clean(d, queue, f, silent); } else if(!set_lookup(d->outputs, f) && (clean_depth == MAKEFLOW_CLEAN_INTERMEDIATES)){ makeflow_file_clean(d, queue, f, silent); } } struct dag_node *n; for(n = d->nodes; n; n = n->next) { /* If the node is a Makeflow job, then we should recursively call the * * clean operation on it. */ if(n->nested_job) { char *command = xxmalloc(sizeof(char) * (strlen(n->command) + 4)); sprintf(command, "%s -c", n->command); /* XXX this should use the batch job interface for consistency */ makeflow_node_export_variables(d, n); system(command); free(command); } } }
/** The clean_mode variable was added so that we could better print out error messages * apply in the situation. Currently only used to silence node rerun checking. */ void makeflow_log_recover(struct dag *d, const char *filename, int verbose_mode, struct batch_queue *queue, makeflow_clean_depth clean_mode) { char *line, *name, file[MAX_BUFFER_SIZE]; int nodeid, state, jobid, file_state; int first_run = 1; struct dag_node *n; struct dag_file *f; struct stat buf; timestamp_t previous_completion_time; d->logfile = fopen(filename, "r"); if(d->logfile) { int linenum = 0; first_run = 0; printf("recovering from log file %s...\n",filename); while((line = get_line(d->logfile))) { linenum++; if(sscanf(line, "# %d %s %" SCNu64 "", &file_state, file, &previous_completion_time) == 3) { f = dag_file_lookup_or_create(d, file); f->state = file_state; if(file_state == DAG_FILE_STATE_EXISTS){ d->completed_files += 1; f->creation_logged = (time_t) (previous_completion_time / 1000000); } else if(file_state == DAG_FILE_STATE_DELETE){ d->deleted_files += 1; } continue; } if(line[0] == '#') continue; if(sscanf(line, "%" SCNu64 " %d %d %d", &previous_completion_time, &nodeid, &state, &jobid) == 4) { n = itable_lookup(d->node_table, nodeid); if(n) { n->state = state; n->jobid = jobid; /* Log timestamp is in microseconds, we need seconds for diff. */ n->previous_completion = (time_t) (previous_completion_time / 1000000); continue; } } fprintf(stderr, "makeflow: %s appears to be corrupted on line %d\n", filename, linenum); exit(1); } fclose(d->logfile); } d->logfile = fopen(filename, "a"); if(!d->logfile) { fprintf(stderr, "makeflow: couldn't open logfile %s: %s\n", filename, strerror(errno)); exit(1); } if(setvbuf(d->logfile, NULL, _IOLBF, BUFSIZ) != 0) { fprintf(stderr, "makeflow: couldn't set line buffer on logfile %s: %s\n", filename, strerror(errno)); exit(1); } if(first_run && verbose_mode) { struct dag_file *f; struct dag_node *p; for(n = d->nodes; n; n = n->next) { /* Record node information to log */ fprintf(d->logfile, "# NODE\t%d\t%s\n", n->nodeid, n->command); /* Record the node category to the log */ fprintf(d->logfile, "# CATEGORY\t%d\t%s\n", n->nodeid, n->category->label); fprintf(d->logfile, "# SYMBOL\t%d\t%s\n", n->nodeid, n->category->label); /* also write the SYMBOL as alias of CATEGORY, deprecated. */ /* Record node parents to log */ fprintf(d->logfile, "# PARENTS\t%d", n->nodeid); list_first_item(n->source_files); while( (f = list_next_item(n->source_files)) ) { p = f->created_by; if(p) fprintf(d->logfile, "\t%d", p->nodeid); } fputc('\n', d->logfile); /* Record node inputs to log */ fprintf(d->logfile, "# SOURCES\t%d", n->nodeid); list_first_item(n->source_files); while( (f = list_next_item(n->source_files)) ) { fprintf(d->logfile, "\t%s", f->filename); } fputc('\n', d->logfile); /* Record node outputs to log */ fprintf(d->logfile, "# TARGETS\t%d", n->nodeid); list_first_item(n->target_files); while( (f = list_next_item(n->target_files)) ) { fprintf(d->logfile, "\t%s", f->filename); } fputc('\n', d->logfile); /* Record translated command to log */ fprintf(d->logfile, "# COMMAND\t%d\t%s\n", n->nodeid, n->command); } } dag_count_states(d); // Check for log consistency if(!first_run) { hash_table_firstkey(d->files); while(hash_table_nextkey(d->files, &name, (void **) &f)) { if(dag_file_should_exist(f) && !dag_file_is_source(f) && !(batch_fs_stat(queue, f->filename, &buf) >= 0)){ fprintf(stderr, "makeflow: %s is reported as existing, but does not exist.\n", f->filename); makeflow_log_file_state_change(d, f, DAG_FILE_STATE_UNKNOWN); continue; } if(S_ISDIR(buf.st_mode)) continue; if(dag_file_should_exist(f) && !dag_file_is_source(f) && difftime(buf.st_mtime, f->creation_logged) > 0) { fprintf(stderr, "makeflow: %s is reported as existing, but has been modified (%" SCNu64 " ,%" SCNu64 ").\n", f->filename, (uint64_t)buf.st_mtime, (uint64_t)f->creation_logged); makeflow_clean_file(d, queue, f, 0); makeflow_log_file_state_change(d, f, DAG_FILE_STATE_UNKNOWN); } } } int silent = 0; if(clean_mode != MAKEFLOW_CLEAN_NONE) silent = 1; // Decide rerun tasks if(!first_run) { struct itable *rerun_table = itable_create(0); for(n = d->nodes; n; n = n->next) { makeflow_node_decide_rerun(rerun_table, d, n, silent); } itable_delete(rerun_table); } //Update file reference counts from nodes in log for(n = d->nodes; n; n = n->next) { if(n->state == DAG_NODE_STATE_COMPLETE) { struct dag_file *f; list_first_item(n->source_files); while((f = list_next_item(n->source_files))) f->ref_count += -1; } } }
int makeflow_clean(struct dag *d, struct batch_queue *queue, makeflow_clean_depth clean_depth)//, struct makeflow_wrapper *w, struct makeflow_monitor *m) { struct dag_file *f; char *name; hash_table_firstkey(d->files); while(hash_table_nextkey(d->files, &name, (void **) &f)) { int silent = 1; if(dag_file_should_exist(f)) silent = 0; /* We have a record of the file, but it is no longer created or used so delete */ if(dag_file_is_source(f) && dag_file_is_sink(f) && !set_lookup(d->inputs, f)) makeflow_clean_file(d, queue, f, silent); if(dag_file_is_source(f)) { if(f->source && (clean_depth == MAKEFLOW_CLEAN_CACHE || clean_depth == MAKEFLOW_CLEAN_ALL)) { /* this file is specified in the mountfile */ if(makeflow_clean_mount_target(f->filename)) { fprintf(stderr, "Failed to remove %s!\n", f->filename); return -1; } } continue; } if(clean_depth == MAKEFLOW_CLEAN_ALL) { makeflow_clean_file(d, queue, f, silent); } else if(set_lookup(d->outputs, f) && (clean_depth == MAKEFLOW_CLEAN_OUTPUTS)) { makeflow_clean_file(d, queue, f, silent); } else if(!set_lookup(d->outputs, f) && (clean_depth == MAKEFLOW_CLEAN_INTERMEDIATES)){ makeflow_clean_file(d, queue, f, silent); } } /* clean up the cache dir created due to the usage of mountfile */ if(clean_depth == MAKEFLOW_CLEAN_CACHE || clean_depth == MAKEFLOW_CLEAN_ALL) { if(d->cache_dir && unlink_recursive(d->cache_dir)) { fprintf(stderr, "Failed to clean up the cache dir (%s) created due to the usage of the mountfile!\n", d->cache_dir); dag_mount_clean(d); return -1; } dag_mount_clean(d); } struct dag_node *n; for(n = d->nodes; n; n = n->next) { /* If the node is a Makeflow job, then we should recursively call the * * clean operation on it. */ if(n->nested_job) { char *command = xxmalloc(sizeof(char) * (strlen(n->command) + 4)); sprintf(command, "%s -c", n->command); /* XXX this should use the batch job interface for consistency */ makeflow_node_export_variables(d, n); system(command); free(command); } } return 0; }