Commit bc867e00 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: cleanup the code

parent 7f9436fd
...@@ -35,6 +35,9 @@ void logger_dump_init( ...@@ -35,6 +35,9 @@ void logger_dump_init(
/* Set the pointer to the reader. */ /* Set the pointer to the reader. */
dump->reader = reader; dump->reader = reader;
/* Set pointers to zero */
time_array_init_to_zero(&dump->times);
/* Open file, map it and get its size. */ /* Open file, map it and get its size. */
if (reader->verbose > 1) if (reader->verbose > 1)
message("Mapping the dump file."); message("Mapping the dump file.");
...@@ -51,14 +54,14 @@ void logger_dump_init( ...@@ -51,14 +54,14 @@ void logger_dump_init(
} }
/* Check if the offset are corrupted */ /* Check if the offset are corrupted */
if (header_are_offset_corrupted(&dump->header)) { if (header_is_corrupted(&dump->header)) {
error("The offsets have been corrupted"); error("The offsets have been corrupted");
} }
/* Reverse offset direction */ /* Reverse offset direction */
if (reader->verbose > 1) if (reader->verbose > 1)
message("Checking if offsets need to be reversed."); message("Checking if offsets need to be reversed.");
if (header_are_offset_backward(&dump->header)) { if (header_is_backward(&dump->header)) {
logger_dump_reverse_offset(dump); logger_dump_reverse_offset(dump);
} }
...@@ -94,7 +97,7 @@ void logger_dump_reverse_offset(struct logger_dump *dump) { ...@@ -94,7 +97,7 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
struct header *header = &dump->header; struct header *header = &dump->header;
const struct logger_reader *reader = dump->reader; const struct logger_reader *reader = dump->reader;
if (!header_are_offset_backward(header)) { if (!header_is_backward(header)) {
error("The offset are already reversed."); error("The offset are already reversed.");
} }
...@@ -102,31 +105,32 @@ void logger_dump_reverse_offset(struct logger_dump *dump) { ...@@ -102,31 +105,32 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
#ifdef SWIFT_DEBUG_CHECKS #ifdef SWIFT_DEBUG_CHECKS
/* check offset */ /* check offset */
if (reader->verbose > 0) { if (reader->verbose > 0) {
message("Check offsets...\n"); message("Check offsets...");
}
size_t offset_debug = header->offset_first;
while (offset_debug < dump->dump.file_size) {
tools_check_offset(header, dump->dump.map, &offset_debug);
} }
for(size_t offset_debug = header->offset_first;
offset_debug < dump->dump.file_size;
offset_debug = tools_check_offset(reader, offset_debug)) {}
if (reader->verbose > 0) { if (reader->verbose > 0) {
message("Check done\n"); message("Check done");
} }
#endif #endif
/* reverse header offset */ /* reverse header offset */
header_change_offset_direction(header, logger_offset_corrupted); header_change_offset_direction(header, logger_offset_corrupted);
size_t offset = header->offset_first;
/* reverse chunks */ /* reverse chunks */
if (reader->verbose > 0) { if (reader->verbose > 0) {
message("Reversing offsets...\n"); message("Reversing offsets...");
}
while (offset < dump->dump.file_size) {
tools_reverse_offset(header, dump->dump.map, &offset);
} }
for(size_t offset = header->offset_first;
offset < dump->dump.file_size;
offset = tools_reverse_offset(header, dump->dump.map, offset)) {}
if (reader->verbose > 0) { if (reader->verbose > 0) {
message("Reversing done\n"); message("Reversing done");
} }
/* reverse header offset */ /* reverse header offset */
...@@ -135,14 +139,15 @@ void logger_dump_reverse_offset(struct logger_dump *dump) { ...@@ -135,14 +139,15 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
#ifdef SWIFT_DEBUG_CHECKS #ifdef SWIFT_DEBUG_CHECKS
/* check offset */ /* check offset */
if (reader->verbose > 0) { if (reader->verbose > 0) {
message("Check offsets...\n"); message("Check offsets...");
}
offset_debug = header->offset_first;
while (offset_debug < dump->dump.file_size) {
tools_check_offset(header, dump->dump.map, &offset_debug);
} }
for(size_t offset_debug = header->offset_first;
offset_debug < dump->dump.file_size;
offset_debug = tools_check_offset(reader, offset_debug)) {}
if (reader->verbose > 0) { if (reader->verbose > 0) {
message("Check done\n"); message("Check done");
} }
#endif #endif
......
...@@ -90,7 +90,7 @@ void header_change_offset_direction(struct header *h, int new_value) { ...@@ -90,7 +90,7 @@ void header_change_offset_direction(struct header *h, int new_value) {
h->offset_direction = new_value; h->offset_direction = new_value;
size_t offset = LOGGER_VERSION_SIZE; size_t offset = LOGGER_VERSION_SIZE;
io_write_data(h->dump->dump.map, LOGGER_NUMBER_SIZE, &new_value, &offset); io_write_data(h->dump->dump.map, LOGGER_NUMBER_SIZE, &new_value, offset);
} }
/** /**
...@@ -107,23 +107,23 @@ void header_read(struct header *h, struct logger_dump *dump) { ...@@ -107,23 +107,23 @@ void header_read(struct header *h, struct logger_dump *dump) {
h->dump = dump; h->dump = dump;
/* read version */ /* read version */
io_read_data(map, LOGGER_VERSION_SIZE, &h->version, &offset); offset = io_read_data(map, LOGGER_VERSION_SIZE, &h->version, offset);
/* read offset direction */ /* read offset direction */
h->offset_direction = -1; h->offset_direction = -1;
io_read_data(map, LOGGER_NUMBER_SIZE, &h->offset_direction, &offset); offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->offset_direction, offset);
if (!header_are_offset_forward(h) && !header_are_offset_backward(h) && if (!header_is_forward(h) && !header_is_backward(h) &&
!header_are_offset_corrupted(h)) !header_is_corrupted(h))
error("Wrong offset value in the header (%i)", h->offset_direction); error("Wrong offset value in the header (%i)", h->offset_direction);
/* read offset to first data */ /* read offset to first data */
h->offset_first = 0; h->offset_first = 0;
io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first, &offset); offset = io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first, offset);
/* read name size */ /* read name size */
h->name_length = 0; h->name_length = 0;
io_read_data(map, LOGGER_NUMBER_SIZE, &h->name_length, &offset); offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->name_length, offset);
/* check if value defined in this file is large enough */ /* check if value defined in this file is large enough */
if (STRING_SIZE < h->name_length) { if (STRING_SIZE < h->name_length) {
...@@ -132,7 +132,7 @@ void header_read(struct header *h, struct logger_dump *dump) { ...@@ -132,7 +132,7 @@ void header_read(struct header *h, struct logger_dump *dump) {
/* read number of masks */ /* read number of masks */
h->number_mask = 0; h->number_mask = 0;
io_read_data(map, LOGGER_NUMBER_SIZE, &h->number_mask, &offset); offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->number_mask, offset);
/* allocate memory */ /* allocate memory */
h->masks = malloc(sizeof(struct mask_data) * h->number_mask); h->masks = malloc(sizeof(struct mask_data) * h->number_mask);
...@@ -140,14 +140,14 @@ void header_read(struct header *h, struct logger_dump *dump) { ...@@ -140,14 +140,14 @@ void header_read(struct header *h, struct logger_dump *dump) {
/* loop over all masks */ /* loop over all masks */
for (size_t i = 0; i < h->number_mask; i++) { for (size_t i = 0; i < h->number_mask; i++) {
/* read mask name */ /* read mask name */
io_read_data(map, h->name_length, h->masks[i].name, &offset); offset = io_read_data(map, h->name_length, h->masks[i].name, offset);
/* get mask value */ /* get mask value */
h->masks[i].mask = 1 << i; h->masks[i].mask = 1 << i;
/* read mask data size */ /* read mask data size */
h->masks[i].size = 0; h->masks[i].size = 0;
io_read_data(map, LOGGER_NUMBER_SIZE, &h->masks[i].size, &offset); offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->masks[i].size, offset);
} }
if (offset != h->offset_first) { if (offset != h->offset_first) {
......
...@@ -91,7 +91,7 @@ void header_change_offset_direction(struct header *h, int new_value); ...@@ -91,7 +91,7 @@ void header_change_offset_direction(struct header *h, int new_value);
* @brief Check if the offset are forward. * @brief Check if the offset are forward.
* @param h The #header. * @param h The #header.
*/ */
__attribute__((always_inline)) INLINE static int header_are_offset_forward( __attribute__((always_inline)) INLINE static int header_is_forward(
const struct header *h) { const struct header *h) {
return h->offset_direction == logger_offset_forward; return h->offset_direction == logger_offset_forward;
} }
...@@ -100,7 +100,7 @@ __attribute__((always_inline)) INLINE static int header_are_offset_forward( ...@@ -100,7 +100,7 @@ __attribute__((always_inline)) INLINE static int header_are_offset_forward(
* @brief Check if the offset are backward. * @brief Check if the offset are backward.
* @param h The #header. * @param h The #header.
*/ */
__attribute__((always_inline)) INLINE static int header_are_offset_backward( __attribute__((always_inline)) INLINE static int header_is_backward(
const struct header *h) { const struct header *h) {
return h->offset_direction == logger_offset_backward; return h->offset_direction == logger_offset_backward;
} }
...@@ -109,7 +109,7 @@ __attribute__((always_inline)) INLINE static int header_are_offset_backward( ...@@ -109,7 +109,7 @@ __attribute__((always_inline)) INLINE static int header_are_offset_backward(
* @brief Check if the offset are corrupted. * @brief Check if the offset are corrupted.
* @param h The #header. * @param h The #header.
*/ */
__attribute__((always_inline)) INLINE static int header_are_offset_corrupted( __attribute__((always_inline)) INLINE static int header_is_corrupted(
const struct header *h) { const struct header *h) {
return h->offset_direction == logger_offset_corrupted; return h->offset_direction == logger_offset_corrupted;
} }
......
...@@ -37,57 +37,64 @@ void io_munmap_file(void *map, size_t file_size); ...@@ -37,57 +37,64 @@ void io_munmap_file(void *map, size_t file_size);
* *
* @param h #header file structure * @param h #header file structure
* @param data Pointer to the data to read. * @param data Pointer to the data to read.
* @param offset In: position in the file, Out: shifted by the mask + offset * @param offset position in the file, Out: shifted by the mask + offset
* size * size
* @param mask mask read * @param mask mask read
* @param diff_offset offset difference to previous/next corresponding chunk * @param diff_offset offset difference to previous/next corresponding chunk
* *
* @return offset after the record header
*/ */
__attribute__((always_inline)) INLINE static void io_read_mask( __attribute__((always_inline)) INLINE static size_t io_read_mask(
const struct header *h, void *data, size_t *offset, size_t *mask, const struct header *h, void *data, size_t offset, size_t *mask,
size_t *diff_offset) { size_t *diff_offset) {
/* read mask */ /* read mask */
if (mask) { if (mask) {
*mask = 0; *mask = 0;
memcpy(mask, data + *offset, LOGGER_MASK_SIZE); memcpy(mask, data + offset, LOGGER_MASK_SIZE);
} }
*offset += LOGGER_MASK_SIZE; offset += LOGGER_MASK_SIZE;
/* read offset */ /* read offset */
if (diff_offset) { if (diff_offset) {
*diff_offset = 0; *diff_offset = 0;
memcpy(diff_offset, data + *offset, LOGGER_OFFSET_SIZE); memcpy(diff_offset, data + offset, LOGGER_OFFSET_SIZE);
} }
*offset += LOGGER_OFFSET_SIZE; offset += LOGGER_OFFSET_SIZE;
return offset;
} }
/** /**
* @brief read a single value in a file * @brief read a single value in a file.
* *
* @param data Pointer to the data to read. * @param data Pointer to the data to read.
* @param size size of the chunk to read * @param size size of the chunk to read.
* @param p pointer where to store the data * @param p pointer where to store the data.
* @param offset In: position to read, Out: shifted by size * @param offset position to read.
* @return offset after the record.
*/ */
__attribute__((always_inline)) INLINE static void io_read_data( __attribute__((always_inline)) INLINE static size_t io_read_data(
void *data, const size_t size, void *p, size_t *offset) { void *data, const size_t size, void *p, size_t offset) {
memcpy(p, data + *offset, size); memcpy(p, data + offset, size);
*offset += size; return offset + size;
}; };
/** /**
* @brief write a single value in a file * @brief write a single value in a file.
* *
* @param data Pointer to the data to read. * @param data Pointer to the data to read.
* @param size size of the chunk to write * @param size size of the chunk to write.
* @param p pointer to the data * @param p pointer to the data.
* @param offset In: position to write, Out: shifted by size * @param offset position to write.
* *
* @return offset after the data written.
*/ */
__attribute__((always_inline)) INLINE static void io_write_data( __attribute__((always_inline)) INLINE static size_t io_write_data(
void *data, const size_t size, const void *p, size_t *offset) { void *data, const size_t size, const void *p, size_t offset) {
memcpy(data + *offset, p, size); memcpy(data + offset, p, size);
*offset += size;
return offset + size;
}; };
#endif // __LOGGER_LOGGER_IO_H__ #endif // __LOGGER_LOGGER_IO_H__
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "logger_particle.h" #include "logger_particle.h"
#include "logger_header.h" #include "logger_header.h"
#include "logger_io.h" #include "logger_io.h"
#include "logger_reader.h"
#include "logger_time.h" #include "logger_time.h"
#include "logger_tools.h" #include "logger_tools.h"
...@@ -63,19 +64,19 @@ void logger_particle_init(struct logger_particle *part) { ...@@ -63,19 +64,19 @@ void logger_particle_init(struct logger_particle *part) {
} }
/** /**
* @brief Read a single field for a particle * @brief Read a single field for a particle.
* *
* @param part The #logger_particle to update * @param part The #logger_particle to update.
* @param data Pointer to the data to read. * @param data Pointer to the data to read.
* @param offset In: read position, Out: input shifted by the required amount of * @param offset position to read.
* data * @param field field to read.
* @param field field to read * @param size number of bits to read.
* @param size number of bits to read
* *
* @return position after the data read.
*/ */
void logger_particle_read_field(struct logger_particle *part, void *map, size_t logger_particle_read_field(struct logger_particle *part, void *map,
size_t *offset, const char *field, size_t offset, const char *field,
const size_t size) { const size_t size) {
void *p = NULL; void *p = NULL;
if (strcmp("positions", field) == 0) { if (strcmp("positions", field) == 0) {
...@@ -96,7 +97,7 @@ void logger_particle_read_field(struct logger_particle *part, void *map, ...@@ -96,7 +97,7 @@ void logger_particle_read_field(struct logger_particle *part, void *map,
error("Type %s not defined", field); error("Type %s not defined", field);
} }
io_read_data(map, size, p, offset); offset = io_read_data(map, size, p, offset);
if (strcmp("consts", field) == 0) { if (strcmp("consts", field) == 0) {
part->mass = 0; part->mass = 0;
...@@ -107,67 +108,81 @@ void logger_particle_read_field(struct logger_particle *part, void *map, ...@@ -107,67 +108,81 @@ void logger_particle_read_field(struct logger_particle *part, void *map,
p -= sizeof(float); p -= sizeof(float);
free(p); free(p);
} }
return offset;
} }
/** /**
* @brief Read a particle in the dump file. * @brief Read a particle in the dump file.
* *
* @param reader The #logger_reader.
* @param part The #logger_particle to update. * @param part The #logger_particle to update.
* @param h #header structure of the file * @param offset offset of the chunk to read.
* @param map file mapping
* @param offset offset of the chunk to read (update it to the end of the chunk)
* @param time time to interpolate (if #logger_reader_type is an interpolating * @param time time to interpolate (if #logger_reader_type is an interpolating
* one) * one).
* @param reader #logger_reader_type * @param reader_type #logger_reader_type.
* @param times #time_array times in the dump
* *
* @return position after the record.
*/ */
void logger_particle_read(struct logger_particle *part, const struct header *h, size_t logger_particle_read(struct logger_particle *part, const struct logger_reader *reader,
void *map, size_t *offset, const double time, size_t offset, const double time,
const int reader, struct time_array *times) { const enum logger_reader_type reader_type) {
const struct header *h = &reader->dump.header;
void *map = reader->dump.dump.map;
const struct time_array *times = &reader->dump.times;
size_t mask = 0; size_t mask = 0;
size_t h_offset = 0; size_t h_offset = 0;
logger_particle_init(part); logger_particle_init(part);
io_read_mask(h, map, offset, &mask, &h_offset); offset = io_read_mask(h, map, offset, &mask, &h_offset);
if (mask != 127) error("Unexpected mask: %lu", mask); if (mask != 127) error("Unexpected mask: %lu", mask);
for (size_t i = 0; i < h->number_mask; i++) { for (size_t i = 0; i < h->number_mask; i++) {
if (mask & h->masks[i].mask) { if (mask & h->masks[i].mask) {
logger_particle_read_field(part, map, offset, h->masks[i].name, offset = logger_particle_read_field(part, map, offset, h->masks[i].name,
h->masks[i].size); h->masks[i].size);
} }
} }
if (times) /* move offset by 1 in order to be in the required chunk */ if (times) {
part->time = time_array_get_time(times, *offset - 1); /* move offset by 1 in order to be in the required chunk */
part->time = time_array_get_time(times, offset - 1);
}
else else
part->time = -1; part->time = -1;
/* end of const case */ /* end of const case */
if (reader == logger_reader_const) return; if (reader_type == logger_reader_const)
return offset;
/* read next particle */ /* read next particle */
struct logger_particle part_next; struct logger_particle part_next;
if (!header_are_offset_forward(h)) { if (!header_is_forward(h)) {
error("Cannot read a particle with non forward offsets."); error("Cannot read a particle with non forward offsets.");
} }
if (h_offset == 0) return; if (h_offset == 0)
return offset;
/* get absolute offset of next particle */ /* get absolute offset of next particle */
h_offset += *offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE - h_offset += offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE -
LOGGER_OFFSET_SIZE; LOGGER_OFFSET_SIZE;
part_next.time = time_array_get_time(times, h_offset); part_next.time = time_array_get_time(times, h_offset);
/* previous part exists */ /* previous part exists */
logger_particle_read(&part_next, h, map, &h_offset, part_next.time, h_offset = logger_particle_read(&part_next, reader, h_offset, part_next.time,
logger_reader_const, times); logger_reader_const);
logger_particle_interpolate(part, &part_next, time); logger_particle_interpolate(part, &part_next, time);
return offset;
} }
/** /**
......
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#define DIM 3 #define DIM 3
struct logger_reader;
/** /**
* @brief Store the data from a chunk. * @brief Store the data from a chunk.
* *
...@@ -83,14 +85,14 @@ enum logger_reader_type { ...@@ -83,14 +85,14 @@ enum logger_reader_type {
void logger_particle_print(const struct logger_particle *p); void logger_particle_print(const struct logger_particle *p);
void logger_particle_read(struct logger_particle *part, const struct header *h, size_t logger_particle_read(struct logger_particle *part, const struct logger_reader *reader,
void *map, size_t *offset, const double time, size_t offset, const double time,
const int reader, struct time_array *times); const enum logger_reader_type reader_type);
void logger_particle_init(struct logger_particle *part); void logger_particle_init(struct logger_particle *part);
void logger_particle_read_field(struct logger_particle *part, void *map, size_t logger_particle_read_field(struct logger_particle *part, void *map,
size_t *offset, const char *field, size_t offset, const char *