| 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <fstream> |
| 4 | #include <cstdlib> |
| 5 | #include <sstream> |
| 6 | #include <omp.h> |
| 7 | #include <vector> |
| 8 | #include <map> |
| 9 | #include <algorithm> |
| 10 | #include <signal.h> |
| 11 | #include <sys/wait.h> |
| 12 | |
| 13 | #include "argProcessor.hpp" |
| 14 | |
| 15 | |
| 16 | string usage(); |
| 17 | |
| 18 | |
| 19 | using namespace std; |
| 20 | |
| 21 | |
| 22 | static int VERBOSE_LEVEL = 0; |
| 23 | |
| 24 | int main(int argc, char* argv[]) { |
| 25 | |
| 26 | /* This program executes commands in Parallel using C++, the program is a wrapper designed for the orignal Trinity Program |
| 27 | for seemless parallel OPENMP execution, Authors of this wrapper are MB Couger (mbcouger(AT Symbol)gmail.com, Matt Stowe mstowe(AT Symbol)okstate.edu*/ |
| 28 | // code modified by bhaas for general cmd-line processing |
| 29 | |
| 30 | |
| 31 | ArgProcessor args(argc, argv); |
| 32 | |
| 33 | bool shuffle_flag = false; |
| 34 | |
| 35 | int max_retries = 10; |
| 36 | |
| 37 | if (args.isArgSet("-help") |
| 38 | || ! (args.isArgSet("-c") ) |
| 39 | || ! (args.isArgSet("-CPU") ) |
| 40 | ) { |
| 41 | cerr << usage() << endl << endl; |
| 42 | return(1); |
| 43 | } |
| 44 | |
| 45 | if (args.isArgSet("-v")) { |
| 46 | VERBOSE_LEVEL = 1; |
| 47 | } |
| 48 | else if (args.isArgSet("-vv")) { |
| 49 | VERBOSE_LEVEL = 2; |
| 50 | } |
| 51 | |
| 52 | if (args.isArgSet("-shuffle")) { |
| 53 | shuffle_flag = true; |
| 54 | } |
| 55 | if (args.isArgSet("-max_retry")) { |
| 56 | max_retries = args.getIntVal("-max_retry"); |
| 57 | } |
| 58 | |
| 59 | string commands_file = args.getStringVal("-c"); |
| 60 | |
| 61 | int num_cpus = args.getIntVal("-CPU"); |
| 62 | |
| 63 | |
| 64 | string failed_commands_filename = "FailedCommands"; |
| 65 | if (args.isArgSet("-failed_cmds")) { |
| 66 | failed_commands_filename = args.getStringVal("-failed_cmds"); |
| 67 | } |
| 68 | |
| 69 | //declare variables start input stream |
| 70 | ifstream in; // Create an input file stream. |
| 71 | in.open(commands_file.c_str()); // Use it to read from a file named data.txt. |
| 72 | if ( ! in ) { |
| 73 | stringstream errstr; |
| 74 | errstr << "Error, cannot open file : " << commands_file << endl; |
| 75 | cerr << errstr.str(); |
| 76 | exit(1); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // check to see if any commands had completed |
| 81 | map<string,bool> previously_completed_commands; |
| 82 | bool have_previously_completed_commands = false; |
| 83 | |
| 84 | ofstream successfully_completed_fh; |
| 85 | |
| 86 | { |
| 87 | ifstream prev_completed_fh; |
| 88 | stringstream completed_commands_filename_ss; |
| 89 | completed_commands_filename_ss << commands_file << ".completed"; |
| 90 | string completed_commands_filename = completed_commands_filename_ss.str(); |
| 91 | prev_completed_fh.open(completed_commands_filename.c_str()); |
| 92 | |
| 93 | if (prev_completed_fh) { |
| 94 | have_previously_completed_commands = true; |
| 95 | |
| 96 | string command_line; |
| 97 | getline(prev_completed_fh, command_line); |
| 98 | while(! prev_completed_fh.eof()) { |
| 99 | previously_completed_commands[command_line] = true; |
| 100 | getline(prev_completed_fh, command_line); |
| 101 | } |
| 102 | prev_completed_fh.close(); |
| 103 | } |
| 104 | |
| 105 | successfully_completed_fh.open(completed_commands_filename.c_str(), ios_base::app); // open for append |
| 106 | } |
| 107 | |
| 108 | long int NumberofCommands; |
| 109 | NumberofCommands=0; |
| 110 | |
| 111 | vector<string> commandsArray; |
| 112 | |
| 113 | string line; |
| 114 | getline(in,line); |
| 115 | |
| 116 | int count_skip = 0; |
| 117 | int max_report_skip = 10; |
| 118 | |
| 119 | while (! in.eof()) { |
| 120 | |
| 121 | if (have_previously_completed_commands |
| 122 | && |
| 123 | previously_completed_commands.find(line) != previously_completed_commands.end()) { |
| 124 | |
| 125 | // this command has been run before successfully, not running it again. |
| 126 | if (VERBOSE_LEVEL and count_skip < max_report_skip) { |
| 127 | cerr << "warning, command: " << line << " has successfully completed from a previous run. Skipping it here." << endl; |
| 128 | count_skip++; |
| 129 | if (count_skip >= max_report_skip) { |
| 130 | cerr << "-reached max skip warning, turning off warnings for this." << endl; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | else { |
| 136 | NumberofCommands++; |
| 137 | commandsArray.push_back(line); |
| 138 | } |
| 139 | getline(in,line); |
| 140 | } |
| 141 | |
| 142 | if (VERBOSE_LEVEL) { |
| 143 | cerr << "Number of Commands: " << NumberofCommands << endl; |
| 144 | } |
| 145 | |
| 146 | //Parrell Execution of Individual Commands |
| 147 | vector<string> failedCommands; |
| 148 | int num_failed_commands = 0; |
| 149 | int num_succeeded_commands = 0; |
| 150 | |
| 151 | if (shuffle_flag) { |
| 152 | random_shuffle(commandsArray.begin(), commandsArray.end()); |
| 153 | } |
| 154 | |
| 155 | omp_set_dynamic(0); |
| 156 | omp_set_num_threads(num_cpus); |
| 157 | |
| 158 | #pragma omp parallel for schedule (dynamic) |
| 159 | for (long int i=0;i<NumberofCommands;i++) { |
| 160 | string command = commandsArray[i]; |
| 161 | |
| 162 | if (VERBOSE_LEVEL == 2) { |
| 163 | |
| 164 | int thread_no = omp_get_thread_num(); |
| 165 | |
| 166 | #pragma omp critical (standard_error) |
| 167 | { |
| 168 | cerr << "CMD[" << i << "], thread[" << thread_no << "]: " << command << endl; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | int ret = -1; |
| 173 | int try_count = 0; |
| 174 | while (ret != 0 && try_count < max_retries) { |
| 175 | ret = system(command.c_str()); |
| 176 | if (ret != 0 && try_count < max_retries) { |
| 177 | try_count++; |
| 178 | cerr << "warning, cmd: " << command << " failed with ret: " << ret << ", going to retry." << endl; |
| 179 | system("sleep 10"); // give the system a short break to recover |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // exit if child received SIGINT or SIGQUIT |
| 184 | if (WIFSIGNALED(ret) && |
| 185 | (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) { |
| 186 | #pragma omp critical (exit_critical) |
| 187 | exit(ret); // behavior undefined if exit() called more than once |
| 188 | } |
| 189 | else if (ret != 0) { |
| 190 | |
| 191 | #pragma omp critical (capture_failed_command) |
| 192 | { |
| 193 | num_failed_commands++; |
| 194 | failedCommands.push_back(command); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | if (VERBOSE_LEVEL == 2) { |
| 199 | #pragma omp critical (standard_error) |
| 200 | { |
| 201 | cerr << "FAILURE:[" << i << "] " << command << endl; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | else { |
| 207 | #pragma omp critical (report_success) |
| 208 | { |
| 209 | num_succeeded_commands++; |
| 210 | |
| 211 | successfully_completed_fh << command << endl; |
| 212 | } |
| 213 | |
| 214 | if (VERBOSE_LEVEL == 2) { |
| 215 | #pragma omp critical (standard_error) |
| 216 | cerr << "SUCCESS:[" << i << "] " << command << endl; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (VERBOSE_LEVEL == 1) { |
| 221 | |
| 222 | stringstream ss; |
| 223 | ss << "\rsucceeded(" << num_succeeded_commands << ")"; |
| 224 | if (num_failed_commands > 0) { |
| 225 | ss << ", failed(" << num_failed_commands << ")"; |
| 226 | } |
| 227 | int total_executed = num_succeeded_commands + num_failed_commands; |
| 228 | float percent_done = (float)total_executed/NumberofCommands * 100; |
| 229 | |
| 230 | ss << " " << percent_done << "% completed. "; |
| 231 | |
| 232 | #pragma omp critical (standard_error) |
| 233 | cerr << ss.str(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | successfully_completed_fh.close(); |
| 238 | |
| 239 | |
| 240 | //ErrorCheckingOut |
| 241 | if (num_failed_commands != 0) { |
| 242 | ofstream outdata(failed_commands_filename.c_str()); |
| 243 | for (int t=0;t<num_failed_commands; ++t) { |
| 244 | outdata << failedCommands[t] << endl; |
| 245 | } |
| 246 | outdata.close(); |
| 247 | cout << endl << endl << "We are sorry, commands in file: [" << failed_commands_filename << "] failed. :-( " << endl << endl; |
| 248 | |
| 249 | exit(1); |
| 250 | } |
| 251 | else { |
| 252 | if (VERBOSE_LEVEL) { |
| 253 | cout << endl << endl << "All commands completed successfully. :-)" << endl << endl; |
| 254 | } |
| 255 | exit(0); // used to be return(0), but sometimes in OMP land this would not exit 0....?!?! |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | string usage () { |
| 261 | |
| 262 | stringstream ss; |
| 263 | |
| 264 | ss << endl |
| 265 | << "##########################################################" << endl |
| 266 | << "#" << endl |
| 267 | << "# Usage: ParaFly (opts)" << endl |
| 268 | << "#" << endl |
| 269 | << "# Required: " << endl |
| 270 | << "# -c <str> :filename containing list of bash-style commands to execute." << endl |
| 271 | << "# -CPU <int> :number_of_threads" << endl |
| 272 | << "#" << endl |
| 273 | << "# Optional:" << endl |
| 274 | << "# -max_retry <int> :number of times to retry a command if it fails. (default: 10)" << endl |
| 275 | << "# -shuffle :randomly shuffles the command order. " << endl |
| 276 | << "# -failed_cmds <str> :filename to capture failed commands. default(\"FailedCommands\")" << endl |
| 277 | << "# -v :simple progress monitoring." << endl |
| 278 | << "# -vv :increased verbosity in progress monitoring." << endl |
| 279 | << "#" << endl |
| 280 | << "##########################################################" << endl << endl; |
| 281 | |
| 282 | ss << "Note: This process creates a file named based on your commands filename with a .completed extension." << endl |
| 283 | << "This enables a resume functionality, where if rerun, only those commands not completed successfully will be reprocessed." << endl << endl; |
| 284 | |
| 285 | return(ss.str()); |
| 286 | } |
| 287 |