//functions below is the implements of moving the cursor. //only should be called when its under normal or insert mode. //return code: //0: move failed. //1: move success. //-1: exceptions. int cursor_left() { unsigned int lines; lines = get_total_lines(cur_file); if (cur_column == 1 && cur_left == 1) { return 0; } cur_column--; if (!is_position_in_file()) { if (cur_line + cur_top - 1 <= lines) { return 1; } cur_column++; return 0; } else if (cur_column > 1) { return 1; } else if (cur_left > 1) { cur_column++; roll_rightward(-1); return 1; } return -1; }
int parse_highlighting(v_file_text *file_struct) { int i, j, k; int lines = get_total_lines(cur_file); char **highlight_list; char word_temp[FILE_LINE_LENGTH]; for (i = 0; i < lines; i++) { v_line *line = get_line(cur_file, (unsigned int) (i + 1)); memset(line->info, 0, FILE_LINE_LENGTH); } switch (cur_file_type) { case C_SOURCE: highlight_list = c_highlight_list; break; case CPLUSPLUS_SOURCE: highlight_list = cplusplus_highlight_list; break; default: return -1; // highlight_list = c_highlight_list; } for (i = 0; i < lines; i++) { v_line *line_processing = get_line(file_struct, (unsigned int) (i + 1)); int length = get_length(line_processing); for (j = 0; j < length; j++) { if (j != 0 && !is_word_start(line_processing->text + j)) { continue; } line_processing->info[j] = COMMON_TEXT; int end = judge_word(line_processing, (unsigned int) (j + 1)); if (end != 0) { memset(word_temp, 0, sizeof(word_temp)); strncpy(word_temp, line_processing->text + j, (size_t) (end - j + 1)); // print_log(word_temp); } for (k = 0; *(highlight_list[k]) != 0; k++) { if ((strcmp(highlight_list[k], word_temp) == 0)) { for (int t = j; t <= end; t++) { line_processing->info[t] = KEYWORD; } break; } } } } return 0; }
int delete_line(v_file_text *file_struct, unsigned int line_to_delete) { if (line_to_delete == 0) { return -1; } unsigned int lines = get_total_lines(file_struct); if (lines < line_to_delete) { return -1; } v_line *line_pre = get_line(file_struct, line_to_delete - 1); v_line *tmp = line_pre->next->next; free(line_pre->next); line_pre->next = tmp; return 0; }
//This function judge if the position of cursor is contained in the file. //***!!! position available: including one pos at each end of line !!!*** //return code: //1 for True //0 for False int is_position_in_file() { unsigned int cur_file_line; unsigned int cur_file_column; cur_file_line = cur_top + cur_line - 1; cur_file_column = cur_left + cur_column - 1; if (cur_file_line > get_total_lines(cur_file)) { return 0; } if (cur_file_column > 1 + get_length(get_line(cur_file, cur_file_line))) { return 0; } return 1; }
///Not finished. int connect_line(v_file_text *file_struct, unsigned int line_to_fold) { if (line_to_fold == 0) { return -1; } unsigned int lines = get_total_lines(file_struct); if (lines < line_to_fold) { return -1; } v_line *line_pre = get_line(file_struct, line_to_fold - 1); v_line *line_cur = get_line(file_struct, line_to_fold); unsigned int length_pre = get_length(line_pre); unsigned int length_cur = get_length(line_cur); char *pre_end = line_pre->text; pre_end += length_pre; char *cur_start = line_cur->text; memcpy(pre_end, cur_start, length_cur); pre_end += length_cur; *pre_end = 0; delete_line(file_struct, line_to_fold); return 0; }
int main(int argc, char *argv[]) { struct work_queue *q; int port = 0; //pick an arbitrary port int c; char *sort_arguments = NULL; const char *proj_name = NULL; char *outfile= NULL; int auto_partition = 0; int sample_env = 0; int print_runtime_estimates = 0; int estimate_partition= 0; struct timeval current; long long unsigned int execn_start_time, execn_time, workload_runtime; int keepalive_interval = 300; int keepalive_timeout = 30; unsigned long long records = 0; int partitions = PARTITION_DEFAULT; int sample_size = SAMPLE_SIZE_DEFAULT; gettimeofday(¤t, 0); execn_start_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; debug_flags_set("all"); if(argc < 3) { show_help(argv[0]); return 0; } while((c = getopt(argc, argv, "N:k:o:ASs:p:MR:L:I:T:B:h")) != (char) -1) { switch (c) { case 'N': proj_name = strdup(optarg); break; case 'k': partitions = atoi(optarg); break; case 'o': outfile = strdup(optarg); break; case 'A': auto_partition = 1; break; case 's': sample_size = atoi(optarg); break; case 'S': sample_env = 1; break; case 'p': sort_arguments = strdup(optarg); break; case 'M': print_runtime_estimates = 1; break; case 'R': estimate_partition = atoi(optarg); break; case 'L': records = atoll(optarg); break; case 'I': keepalive_interval = atoi(optarg); break; case 'T': keepalive_timeout = atoi(optarg); break; case 'B': bandwidth_bytes_per_sec = atoi(optarg) * 1000000; break; case 'h': show_help(argv[0]); return 0; default: show_help(argv[0]); return -1; } } char sort_executable[256], infile[256]; off_t last_partition_offset_end = 0; int optimal_partitions, optimal_resources, current_optimal_partitions; double current_optimal_time = DBL_MAX; double optimal_times[5]; int sample_partition_offset_end = 0; int i; sprintf(sort_executable, "%s", argv[optind]); sprintf(infile, "%s", argv[optind+1]); if(!outfile){ char *infile_dup = strdup(infile); outfile = (char *) malloc((strlen(infile)+8)*sizeof(char)); sprintf(outfile, "%s.sorted", basename(infile_dup)); free(infile_dup); } if(records == 0) { records = get_total_lines(infile); fprintf(stdout, "Input file %s has %llu records to sort\n", infile, records); if(records == 0) { fprintf(stderr, "Error in reading records. Quitting...\n"); return 0; } } if(estimate_partition) { double *estimated_runtimes = (double *)malloc(sizeof(double) * 5); for (i = 1; i <= 2*estimate_partition; i++) { estimated_runtimes = sort_estimate_runtime(infile, sort_executable, records, i, estimate_partition); if(estimated_runtimes[0] < current_optimal_time) { current_optimal_time = estimated_runtimes[0]; optimal_times[0] = estimated_runtimes[0]; optimal_times[1] = estimated_runtimes[1]; optimal_times[2] = estimated_runtimes[2]; optimal_times[3] = estimated_runtimes[3]; optimal_times[4] = estimated_runtimes[4]; optimal_resources = i; } } fprintf(stdout, "For partition %d: %d %f %f %f %f %f\n", estimate_partition, optimal_resources, optimal_times[0], optimal_times[1], optimal_times[2], optimal_times[3], optimal_times[4]); free(estimated_runtimes); return 1; } if(print_runtime_estimates) { fprintf(stdout, "Resources \t Partitions \t Runtime \t Part time \t Merge time \t Task time \t Transfer time\n"); for (i = 1; i <= 100; i++) { optimal_partitions = get_optimal_runtimes(infile, sort_executable, i, records, optimal_times); fprintf(stdout, "%d \t \t %d \t %f \t %f \t %f \t %f \t %f\n", i, optimal_partitions, optimal_times[0], optimal_times[1], optimal_times[2], optimal_times[3], optimal_times[4]); } return 1; } q = work_queue_create(port); if(!q) { fprintf(stderr, "couldn't listen on port %d: %s\n", port, strerror(errno)); return 1; } fprintf(stdout, "listening on port %d...\n", work_queue_port(q)); if(proj_name){ work_queue_specify_master_mode(q, WORK_QUEUE_MASTER_MODE_CATALOG); work_queue_specify_name(q, proj_name); } work_queue_specify_keepalive_interval(q, keepalive_interval); work_queue_specify_keepalive_timeout(q, keepalive_timeout); free((void *)proj_name); fprintf(stdout, "%s will be run to sort contents of %s\n", sort_executable, infile); long long unsigned int sample_start_time, sample_end_time, sample_time; if(sample_env) { gettimeofday(¤t, 0); sample_start_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; int sample_record_size = (5*records)/100; //sample size is 5% of the total records char *sample_partition_file_prefix = (char *) malloc((strlen(outfile)+8) * sizeof(char)); sprintf(sample_partition_file_prefix, "%s.sample", outfile); char *sample_outfile = (char *) malloc((strlen(outfile)+3) * sizeof(char)); sprintf(sample_outfile, "%s.0", outfile); sample_partition_offset_end = sample_run(q, sort_executable, sort_arguments, infile, 0, sample_partition_file_prefix, sample_outfile, sample_size, sample_record_size); records -= sample_record_size; free(sample_partition_file_prefix); free(sample_outfile); gettimeofday(¤t, 0); sample_end_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; sample_time = sample_end_time - sample_start_time; fprintf(stdout, "Sampling time is %llu\n", sample_time); } if(auto_partition) { fprintf(stdout, "Determining optimal partition size for %s\n", infile); for (i = 1; i <= 100; i++) { current_optimal_partitions = get_optimal_runtimes(infile, sort_executable, i, records, optimal_times); if (optimal_times[0] < current_optimal_time) { current_optimal_time = optimal_times[0]; optimal_partitions = current_optimal_partitions; optimal_resources = i; } } fprintf(stdout, "Optimal partition size is %d that runs the workload in %f\n", optimal_partitions, current_optimal_time); fprintf(stdout, "--> Please allocate %d resources for running this workload in a cost-efficient manner.\n", optimal_resources); partitions = optimal_partitions; } long long unsigned int part_start_time, part_end_time, part_time; gettimeofday(¤t, 0); part_start_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; last_partition_offset_end = partition_tasks(q, sort_executable, sort_arguments, infile, 0+sample_partition_offset_end, outfile, partitions, records); if(last_partition_offset_end <= 0) { fprintf(stderr, "Partitioning failed. Quitting...\n"); return 0; } gettimeofday(¤t, 0); part_end_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; part_time = part_end_time - part_start_time; fprintf(stdout, "Partition time is %llu\n", part_time); free(sort_arguments); fprintf(stdout, "Waiting for tasks to complete...\n"); long long unsigned int parallel_start_time, parallel_end_time, parallel_time; gettimeofday(¤t, 0); parallel_start_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; char *record_task_times_file = (char *)malloc((strlen(outfile)+11) * sizeof(char)); sprintf(record_task_times_file, "%s.tasktimes", outfile); wait_partition_tasks(q, 5, record_task_times_file); free(record_task_times_file); gettimeofday(¤t, 0); parallel_end_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; parallel_time = parallel_end_time - parallel_start_time; fprintf(stdout, "Parallel execution time is %llu\n", parallel_time); long long unsigned int merge_start_time, merge_end_time, merge_time; gettimeofday(¤t, 0); merge_start_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; merge_sorted_outputs(outfile, outfile, created_partitions); gettimeofday(¤t, 0); merge_end_time = ((long long unsigned int) current.tv_sec) * 1000000 + current.tv_usec; merge_time = merge_end_time - merge_start_time; fprintf(stdout, "Merge time is %llu\n", merge_time); fprintf(stdout, "Sorting complete. Output is at: %s!\n", outfile); execn_time = merge_end_time - execn_start_time; workload_runtime = merge_end_time - part_start_time; fprintf(stdout, "Workload execn time is %llu\n", workload_runtime); fprintf(stdout, "Total execn time is %llu\n", execn_time); FILE *time_file = fopen("wq_sort.times", "w"); if (time_file) { fprintf(time_file, "Partition time: %llu\n", part_time); fprintf(time_file, "Parallel time: %llu\n", parallel_time); fprintf(time_file, "Merge time: %llu\n", merge_time); if(sample_env) fprintf(time_file, "Sampling time: %llu\n", sample_time); fprintf(time_file, "Workload execution time: %llu\n", workload_runtime); fprintf(time_file, "Total execution time: %llu\n", execn_time); } fclose(time_file); work_queue_delete(q); free(outfile); return 0; }
//return code: //0: move failed. //1: move success. //-1: exceptions. int cursor_down() { unsigned int length = 0; unsigned int lines = 0; // unsigned int actual_column = 0; lines = get_total_lines(cur_file); cur_line++; if (cur_top + cur_line - 1 > lines) { cur_line--; return 0; } length = (unsigned int) strlen((const char *) get_line(cur_file, cur_line + cur_top - 1)); // actual_column = cur_left + cur_column - 1; if (is_position_in_file()) { if (cur_line > screen_lines) { cur_line--; roll_downward(1); return 1; } else { return 1; } } else //is_position_in_file = false { if (cur_line + cur_top - 1 <= lines) //not exceed the lines EOF { if (cur_line > screen_lines) { cur_line--; roll_downward(1); } // assert(length != 0); // assert(actual_column > length); //length = cur_left + cur_column - 1 // decline if the end of line outside the screen. if (length == 0) { cur_left = 1; cur_column = 1; return 1; } if (cur_left <= length) { cur_column = length - cur_left + 1; cur_column = (cur_column == 0) ? 1 : cur_column; return 1; } else { cur_column = screen_columns; cur_left = length - cur_column + 1; return 1; } } else //exceed EOF lines { cur_line--; return 0; } } assert(-1); return -1; }