| 1 |
/* |
| 2 |
* FFindex |
| 3 |
* written by Andreas Hauser <[email protected]>. |
| 4 |
* Please add your name here if you distribute modified versions. |
| 5 |
* |
| 6 |
* FFindex is provided under the Create Commons license "Attribution-ShareAlike |
| 7 |
* 4.0", which basically captures the spirit of the Gnu Public License (GPL). |
| 8 |
* |
| 9 |
* See: |
| 10 |
* http://creativecommons.org/licenses/by-sa/4.0/ |
| 11 |
* |
| 12 |
* ffindex_apply |
| 13 |
* apply a program to each FFindex entry |
| 14 |
*/ |
| 15 |
|
| 16 |
#define _GNU_SOURCE 1 |
| 17 |
#define _LARGEFILE64_SOURCE 1 |
| 18 |
#define _FILE_OFFSET_BITS 64 |
| 19 |
|
| 20 |
#include <limits.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <sys/types.h> |
| 25 |
#include <sys/wait.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <spawn.h> |
| 29 |
|
| 30 |
|
| 31 |
#include <mpi.h> |
| 32 |
|
| 33 |
|
| 34 |
#include "ffindex.h" |
| 35 |
#include "ffutil.h" |
| 36 |
|
| 37 |
extern char **environ; |
| 38 |
|
| 39 |
|
| 40 |
char* read_buffer; |
| 41 |
|
| 42 |
int ffindex_apply_by_entry(char *data, ffindex_index_t* index, ffindex_entry_t* entry, char* program_name, char** program_argv, FILE* data_file_out, FILE* index_file_out, size_t *offset, FILE* log_file) |
| 43 |
{ |
| 44 |
int ret = 0; |
| 45 |
int capture_stdout = (data_file_out != NULL); |
| 46 |
|
| 47 |
int pipefd_stdin[2]; |
| 48 |
int pipefd_stdout[2]; |
| 49 |
|
| 50 |
ret = pipe(pipefd_stdin); |
| 51 |
if(ret != 0) { fprintf(stderr, "ERROR in pipe stdin!\n"); perror(entry->name); return errno; } |
| 52 |
|
| 53 |
if(capture_stdout) |
| 54 |
{ |
| 55 |
ret = pipe(pipefd_stdout); |
| 56 |
if(ret != 0) { fprintf(stderr, "ERROR in pipe stdout!\n"); perror(entry->name); return errno; } |
| 57 |
} |
| 58 |
|
| 59 |
// Flush so child doesn't copy and also flushes, leading to duplicate output |
| 60 |
fflush(data_file_out); |
| 61 |
fflush(index_file_out); |
| 62 |
|
| 63 |
posix_spawn_file_actions_t factions; |
| 64 |
posix_spawn_file_actions_init(&factions); |
| 65 |
posix_spawn_file_actions_addclose(&factions, pipefd_stdin[1]); |
| 66 |
posix_spawn_file_actions_adddup2(&factions, pipefd_stdin[0], fileno(stdin)); |
| 67 |
if(capture_stdout) |
| 68 |
{ |
| 69 |
posix_spawn_file_actions_addclose(&factions, pipefd_stdout[0]); |
| 70 |
posix_spawn_file_actions_adddup2(&factions, pipefd_stdout[1], fileno(stdout)); |
| 71 |
} |
| 72 |
|
| 73 |
short flags = 0; |
| 74 |
posix_spawnattr_t attr; |
| 75 |
posix_spawnattr_init(&attr); |
| 76 |
flags |= POSIX_SPAWN_USEVFORK; |
| 77 |
posix_spawnattr_setflags(&attr, flags); |
| 78 |
|
| 79 |
pid_t child_pid; |
| 80 |
int err = posix_spawnp(&child_pid, program_name, &factions, &attr, program_argv, environ); |
| 81 |
if(err) |
| 82 |
perror("spawn"); |
| 83 |
|
| 84 |
|
| 85 |
/* |
| 86 |
setenv("FFINDEX_ENTRY_NAME", entry->name, 1); |
| 87 |
|
| 88 |
// exec program with the pipe as stdin |
| 89 |
execvp(program_name, program_argv); |
| 90 |
// never reached |
| 91 |
} |
| 92 |
else if(child_pid > 0) |
| 93 |
*/ |
| 94 |
if(!err) |
| 95 |
{ |
| 96 |
// parent writes to and possible reads from child |
| 97 |
|
| 98 |
int flags = 0; |
| 99 |
|
| 100 |
// Read end is for child only |
| 101 |
close(pipefd_stdin[0]); |
| 102 |
|
| 103 |
if(capture_stdout) |
| 104 |
close(pipefd_stdout[1]); |
| 105 |
|
| 106 |
char *filedata = ffindex_get_data_by_entry(data, entry); |
| 107 |
|
| 108 |
if(capture_stdout) |
| 109 |
{ |
| 110 |
flags = fcntl(pipefd_stdout[0], F_GETFL, 0); |
| 111 |
fcntl(pipefd_stdout[0], F_SETFL, flags | O_NONBLOCK); |
| 112 |
} |
| 113 |
|
| 114 |
// Write file data to child's stdin. |
| 115 |
ssize_t written = 0; |
| 116 |
size_t to_write = entry->length - 1; // Don't write ffindex trailing '\0' |
| 117 |
char* b = read_buffer; |
| 118 |
while(written < to_write) |
| 119 |
{ |
| 120 |
size_t rest = to_write - written; |
| 121 |
int batch_size = PIPE_BUF; |
| 122 |
if(rest < PIPE_BUF) |
| 123 |
batch_size = rest; |
| 124 |
|
| 125 |
ssize_t w = write(pipefd_stdin[1], filedata + written, batch_size); |
| 126 |
if(w < 0 && errno != EPIPE) |
| 127 |
{ fprintf(stderr, "ERROR in child!\n"); perror(entry->name); break; } |
| 128 |
else |
| 129 |
written += w; |
| 130 |
|
| 131 |
if(capture_stdout) |
| 132 |
{ |
| 133 |
// To avoid blocking try to read some data |
| 134 |
ssize_t r = read(pipefd_stdout[0], b, PIPE_BUF); |
| 135 |
if(r > 0) |
| 136 |
b += r; |
| 137 |
} |
| 138 |
} |
| 139 |
close(pipefd_stdin[1]); // child gets EOF |
| 140 |
|
| 141 |
if(capture_stdout) |
| 142 |
{ |
| 143 |
// Read rest |
| 144 |
fcntl(pipefd_stdout[0], F_SETFL, flags); // Remove O_NONBLOCK |
| 145 |
ssize_t r; |
| 146 |
while((r = read(pipefd_stdout[0], b, PIPE_BUF)) > 0) |
| 147 |
b += r; |
| 148 |
close(pipefd_stdout[0]); |
| 149 |
|
| 150 |
ffindex_insert_memory(data_file_out, index_file_out, offset, read_buffer, b - read_buffer, entry->name); |
| 151 |
} |
| 152 |
|
| 153 |
int status; |
| 154 |
waitpid(child_pid, &status, 0); |
| 155 |
if(log_file != NULL) |
| 156 |
fprintf(log_file, "%s\t%ld\t%ld\t%d\n", entry->name, entry->offset, entry->length, WEXITSTATUS(status)); |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
fprintf(stderr, "ERROR in fork()\n"); |
| 161 |
perror(entry->name); |
| 162 |
return errno; |
| 163 |
} |
| 164 |
return EXIT_SUCCESS; |
| 165 |
} |
| 166 |
|
| 167 |
int main(int argn, char **argv) |
| 168 |
{ |
| 169 |
int mpi_error, |
| 170 |
mpi_rank, |
| 171 |
mpi_num_procs; |
| 172 |
|
| 173 |
mpi_error = MPI_Init(&argn, &argv); if(mpi_error) goto EXCEPTION; |
| 174 |
mpi_error = MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); if(mpi_error) goto EXCEPTION; |
| 175 |
mpi_error = MPI_Comm_size(MPI_COMM_WORLD, &mpi_num_procs); if(mpi_error) goto EXCEPTION; |
| 176 |
|
| 177 |
int opt, merge = 1; |
| 178 |
char *data_filename_out = NULL, |
| 179 |
*index_filename_out = NULL, |
| 180 |
*log_filename = NULL; |
| 181 |
|
| 182 |
while ((opt = getopt(argn, argv, "d:i:mql:")) != -1) |
| 183 |
{ |
| 184 |
switch (opt) |
| 185 |
{ |
| 186 |
case 'l': |
| 187 |
log_filename = optarg; |
| 188 |
break; |
| 189 |
case 'm': |
| 190 |
merge = 0; |
| 191 |
break; |
| 192 |
case 'd': |
| 193 |
data_filename_out = optarg; |
| 194 |
break; |
| 195 |
case 'i': |
| 196 |
index_filename_out = optarg; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if(argn - optind < 3) |
| 202 |
{ |
| 203 |
fprintf(stderr, "ERROR Not enough arguments.\n\n"); |
| 204 |
|
| 205 |
fprintf(stderr, "USAGE: %s [-m] [-q] [-l LOG_FILE] -d DATA_FILENAME_OUT -i INDEX_FILENAME_OUT DATA_FILENAME INDEX_FILENAME -- PROGRAM [PROGRAM_ARGS]*\n" |
| 206 |
"\t-l\tLOG_FILE like an FFindex file but with the return value of apply added.\n" |
| 207 |
"\t\tThis file can be processed and used as FFindex file.\n" |
| 208 |
"\t\tE.g. grep \"\t1$\" foo.log | cut -f 1-3 > foo-special.ffindex.\n" |
| 209 |
"\t\twhere maybe the ffindex_apply_mpi ran with -- perl -e 'return 1 if(/^SPECIAL/)'\n" |
| 210 |
"\t-m\tDo not merge the FFindex parts generated by the different MPI processes\n" |
| 211 |
"\t\tThis is useful for large MPI Jobs where merge time might be accounted.\n" |
| 212 |
"\t-d\tFFDATA output file, stdout of program will be captured\n" |
| 213 |
"\t-i\tFFINDEX output file, entries get the same name as input entry\n" |
| 214 |
FFINDEX_COPYRIGHT, |
| 215 |
argv[0]); |
| 216 |
errno = EINVAL; |
| 217 |
goto EXCEPTION; |
| 218 |
} |
| 219 |
read_buffer = malloc(400 * 1024 * 1024); |
| 220 |
char *data_filename = argv[optind++]; |
| 221 |
char *index_filename = argv[optind++]; |
| 222 |
char *program_name = argv[optind]; |
| 223 |
char **program_argv = argv + optind; |
| 224 |
|
| 225 |
FILE *data_file = fopen(data_filename, "r"); |
| 226 |
FILE *index_file = fopen(index_filename, "r"); |
| 227 |
|
| 228 |
if( data_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename); exit(EXIT_FAILURE); } |
| 229 |
if(index_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename); exit(EXIT_FAILURE); } |
| 230 |
|
| 231 |
// Setup one output FFindex for each MPI process |
| 232 |
FILE *data_file_out = NULL, |
| 233 |
*index_file_out = NULL; |
| 234 |
if(data_filename_out != NULL && index_filename_out != NULL) |
| 235 |
{ |
| 236 |
char* data_filename_out_rank = malloc(FILENAME_MAX); |
| 237 |
char* index_filename_out_rank = malloc(FILENAME_MAX); |
| 238 |
snprintf( data_filename_out_rank, FILENAME_MAX, "%s.%d", data_filename_out, mpi_rank); |
| 239 |
snprintf(index_filename_out_rank, FILENAME_MAX, "%s.%d", index_filename_out, mpi_rank); |
| 240 |
data_file_out = fopen(data_filename_out_rank, "w+"); |
| 241 |
index_file_out = fopen(index_filename_out_rank, "w+"); |
| 242 |
if( data_file_out == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename_out); exit(EXIT_FAILURE); } |
| 243 |
if(index_file_out == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename_out); exit(EXIT_FAILURE); } |
| 244 |
} |
| 245 |
|
| 246 |
FILE *log_file = NULL; |
| 247 |
// Setup log file |
| 248 |
if(log_filename) |
| 249 |
{ |
| 250 |
char* log_filename_rank = malloc(FILENAME_MAX); |
| 251 |
snprintf(log_filename_rank, FILENAME_MAX, "%s.%d", index_filename_out, mpi_rank); |
| 252 |
log_file = fopen(log_filename_rank, "w+"); |
| 253 |
if( log_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], log_filename_rank); exit(EXIT_FAILURE); } |
| 254 |
} |
| 255 |
|
| 256 |
int capture_stdout = (data_file_out != NULL); |
| 257 |
|
| 258 |
size_t data_size; |
| 259 |
char *data = ffindex_mmap_data(data_file, &data_size); |
| 260 |
|
| 261 |
ffindex_index_t* index = ffindex_index_parse(index_file, 0); |
| 262 |
if(index == NULL) |
| 263 |
{ |
| 264 |
fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename); |
| 265 |
MPI_Finalize(); |
| 266 |
exit(EXIT_FAILURE); |
| 267 |
} |
| 268 |
|
| 269 |
// Ignore SIGPIPE |
| 270 |
struct sigaction handler; |
| 271 |
handler.sa_handler = SIG_IGN; |
| 272 |
sigemptyset(&handler.sa_mask); |
| 273 |
handler.sa_flags = 0; |
| 274 |
sigaction(SIGPIPE, &handler, NULL); |
| 275 |
|
| 276 |
size_t batch_size, range_start, range_end; |
| 277 |
|
| 278 |
if(index->n_entries >= mpi_num_procs) |
| 279 |
batch_size = index->n_entries / mpi_num_procs; |
| 280 |
else |
| 281 |
batch_size = 0; |
| 282 |
range_start = mpi_rank * batch_size; |
| 283 |
range_end = range_start + batch_size; |
| 284 |
|
| 285 |
|
| 286 |
size_t offset = 0; |
| 287 |
// Foreach entry |
| 288 |
if(batch_size > 0) |
| 289 |
for(size_t entry_index = range_start; entry_index < range_end; entry_index++) |
| 290 |
{ |
| 291 |
ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index); |
| 292 |
if(entry == NULL) { perror(entry->name); goto EXCEPTION; } |
| 293 |
int error = ffindex_apply_by_entry(data, index, entry, program_name, program_argv, data_file_out, index_file_out, &offset, log_file); |
| 294 |
if(error != 0) |
| 295 |
{ perror(entry->name); break; } |
| 296 |
} |
| 297 |
ssize_t left_over = index->n_entries - (batch_size * mpi_num_procs); |
| 298 |
if(mpi_rank < left_over) |
| 299 |
{ |
| 300 |
size_t left_over_entry_index = (batch_size * mpi_num_procs) + mpi_rank; |
| 301 |
ffindex_entry_t* entry = ffindex_get_entry_by_index(index, left_over_entry_index); |
| 302 |
if(entry == NULL) { perror(entry->name); return errno; } |
| 303 |
//fprintf(stderr, "handling left over: %ld\n", left_over_entry_index); |
| 304 |
int error = ffindex_apply_by_entry(data, index, entry, program_name, program_argv, data_file_out, index_file_out, &offset, log_file); |
| 305 |
if(error != 0) |
| 306 |
perror(entry->name); |
| 307 |
} |
| 308 |
|
| 309 |
if(capture_stdout) |
| 310 |
fclose(data_file_out); |
| 311 |
if(index_file_out != NULL) |
| 312 |
fclose(index_file_out); |
| 313 |
|
| 314 |
MPI_Barrier(MPI_COMM_WORLD); |
| 315 |
|
| 316 |
|
| 317 |
// merge FFindexes in master |
| 318 |
if(mpi_rank == 0 && merge && data_filename_out != NULL) |
| 319 |
{ |
| 320 |
char* merge_command = malloc(FILENAME_MAX * 5); |
| 321 |
for(int i = 0; i < mpi_num_procs; i++) |
| 322 |
{ |
| 323 |
snprintf( merge_command, FILENAME_MAX, "ffindex_build -as %s %s -d %s.%d -i %s.%d", |
| 324 |
data_filename_out, index_filename_out, data_filename_out, i, index_filename_out, i); |
| 325 |
//puts(merge_command); |
| 326 |
if(system(merge_command) == 0) |
| 327 |
{ |
| 328 |
snprintf(merge_command, FILENAME_MAX, "%s.%d", data_filename_out, i); |
| 329 |
unlink(merge_command); |
| 330 |
snprintf(merge_command, FILENAME_MAX, "%s.%d", index_filename_out, i); |
| 331 |
unlink(merge_command); |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
fprintf(stderr, "merge command failed: %s\n", merge_command); |
| 336 |
goto EXCEPTION; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
MPI_Finalize(); |
| 342 |
|
| 343 |
return EXIT_SUCCESS; |
| 344 |
|
| 345 |
|
| 346 |
EXCEPTION: |
| 347 |
|
| 348 |
perror(argv[0]); |
| 349 |
fprintf(stderr, "\nEXCEPTION ffindex_apply_mpi in rank: %d\n", mpi_rank); |
| 350 |
|
| 351 |
MPI_Finalize(); |
| 352 |
|
| 353 |
return EXIT_FAILURE; |
| 354 |
} |
| 355 |
|
| 356 |
/* vim: ts=2 sw=2 et |
| 357 |
*/ |