diff --git a/src/dump.c b/src/dump.c index 9c519c2130b2612309e623b8234e3369214b52e2..60125fba76eca6cdb635a06aa222c6a0b0353927 100644 --- a/src/dump.c +++ b/src/dump.c @@ -158,4 +158,27 @@ void dump_init(struct dump *d, const char *filename, size_t size) { d->page_mask = page_mask; } +/** + * @brief Restart a file dump. + * + * @param d The #dump to restart. + * @param filename The fully qualified name of the file in which to dump, + * note that it will be overwritten. + */ +void dump_restart(struct dump *d, const char *filename) { + /* Create the output file. + The option O_RDWR seems to be required by mmap. + */ + if ((d->fd = open(filename, O_RDWR, 0660)) == -1) { + error("Failed to open dump file '%s' (%s).", filename, strerror(errno)); + } + + /* Map memory to the created file. */ + if ((d->data = mmap(NULL, d->size, PROT_WRITE, MAP_SHARED, d->fd, d->file_offset)) == + MAP_FAILED) { + error("Failed to allocate map of size %zi bytes (%s).", d->size, + strerror(errno)); + } +} + #endif diff --git a/src/dump.h b/src/dump.h index 021bc1e1dc22c178a893e42384c91fafdcf63112..e92684c6f7c60e414365b06278c9afedc8e74c50 100644 --- a/src/dump.h +++ b/src/dump.h @@ -51,6 +51,7 @@ struct dump { /* Function prototypes. */ void dump_init(struct dump *d, const char *filename, size_t size); +void dump_restart(struct dump *d, const char *filename); void dump_ensure(struct dump *d, size_t required_size, size_t increase_size); void dump_sync(struct dump *d); void dump_close(struct dump *d); diff --git a/src/engine.c b/src/engine.c index 1b87ab2ab9a35bf4cfec7c7d36e510b156e1bc55..bca19b7da3d0d32c1abaa7d6ed0f78d3ec1dc252 100644 --- a/src/engine.c +++ b/src/engine.c @@ -4196,8 +4196,10 @@ void engine_config(int restart, int fof, struct engine *e, } #ifdef WITH_LOGGER - /* Write the particle logger header */ - logger_write_file_header(e->logger); + if (!restart) { + /* Write the particle logger header */ + logger_write_file_header(e->logger); + } #endif /* Initialise the structure finder */ @@ -4794,6 +4796,10 @@ void engine_struct_dump(struct engine *e, FILE *stream) { if (e->output_list_stats) output_list_struct_dump(e->output_list_stats, stream); if (e->output_list_stf) output_list_struct_dump(e->output_list_stf, stream); + +#ifdef WITH_LOGGER + logger_struct_dump(e->logger, stream); +#endif } /** @@ -4938,6 +4944,13 @@ void engine_struct_restore(struct engine *e, FILE *stream) { e->output_list_stf = output_list_stf; } +#ifdef WITH_LOGGER + struct logger_writer *log = + (struct logger_writer *)malloc(sizeof(struct logger_writer)); + logger_struct_restore(log, stream); + e->logger = log; +#endif + #ifdef EOS_PLANETARY eos_init(&eos, e->physical_constants, e->snapshot_units, e->parameter_file); #endif diff --git a/src/logger.c b/src/logger.c index 20d77783166984304f5f8f5801cd20ff9a442112..d31c5b58b1e0bdcdc7cafc953b0e9dbd4fb649d4 100644 --- a/src/logger.c +++ b/src/logger.c @@ -836,6 +836,37 @@ int logger_read_timestamp(unsigned long long int *t, double *time, return mask; } +/** + * @brief Write a swift_params struct to the given FILE as a stream of bytes. + * + * @param log the struct + * @param stream the file stream + */ +void logger_struct_dump(const struct logger_writer *log, FILE *stream) { + restart_write_blocks((void *)log, sizeof(struct logger_writer), 1, stream, + "logger", "logger"); +} + +/** + * @brief Restore a logger struct from the given FILE as a stream of + * bytes. + * + * @param logger the struct + * @param stream the file stream + */ +void logger_struct_restore(struct logger_writer *log, FILE *stream) { + /* Read the block */ + restart_read_blocks((void *)log, sizeof(struct logger_writer), 1, stream, + NULL, "logger"); + + /* generate dump filename */ + char logger_name_file[PARSER_MAX_LINE_SIZE]; + strcpy(logger_name_file, log->base_name); + strcat(logger_name_file, ".dump"); + + dump_restart(&log->dump, logger_name_file); +} + #endif /* WITH_LOGGER */ #endif /* HAVE_POSIX_FALLOCATE */ diff --git a/src/logger.h b/src/logger.h index 3f17cb92c6ccd5870f79f4a39f3520e9dbbed94e..54c19758163a2595913b907323af9c81c804be96 100644 --- a/src/logger.h +++ b/src/logger.h @@ -176,6 +176,8 @@ int logger_read_part(struct part *p, size_t *offset, const char *buff); int logger_read_gpart(struct gpart *p, size_t *offset, const char *buff); int logger_read_timestamp(unsigned long long int *t, double *time, size_t *offset, const char *buff); +void logger_struct_dump(const struct logger_writer *log, FILE *stream); +void logger_struct_restore(struct logger_writer *log, FILE *stream); /** * @brief Initialize the logger data for a particle.