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(
/* Set the pointer to the reader. */
dump->reader = reader;
/* Set pointers to zero */
time_array_init_to_zero(&dump->times);
/* Open file, map it and get its size. */
if (reader->verbose > 1)
message("Mapping the dump file.");
......@@ -51,14 +54,14 @@ void logger_dump_init(
}
/* 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");
}
/* Reverse offset direction */
if (reader->verbose > 1)
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);
}
......@@ -94,7 +97,7 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
struct header *header = &dump->header;
const struct logger_reader *reader = dump->reader;
if (!header_are_offset_backward(header)) {
if (!header_is_backward(header)) {
error("The offset are already reversed.");
}
......@@ -102,31 +105,32 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
#ifdef SWIFT_DEBUG_CHECKS
/* check offset */
if (reader->verbose > 0) {
message("Check offsets...\n");
}
size_t offset_debug = header->offset_first;
while (offset_debug < dump->dump.file_size) {
tools_check_offset(header, dump->dump.map, &offset_debug);
message("Check offsets...");
}
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) {
message("Check done\n");
message("Check done");
}
#endif
/* reverse header offset */
header_change_offset_direction(header, logger_offset_corrupted);
size_t offset = header->offset_first;
/* reverse chunks */
if (reader->verbose > 0) {
message("Reversing offsets...\n");
}
while (offset < dump->dump.file_size) {
tools_reverse_offset(header, dump->dump.map, &offset);
message("Reversing offsets...");
}
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) {
message("Reversing done\n");
message("Reversing done");
}
/* reverse header offset */
......@@ -135,14 +139,15 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
#ifdef SWIFT_DEBUG_CHECKS
/* check offset */
if (reader->verbose > 0) {
message("Check offsets...\n");
}
offset_debug = header->offset_first;
while (offset_debug < dump->dump.file_size) {
tools_check_offset(header, dump->dump.map, &offset_debug);
message("Check offsets...");
}
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) {
message("Check done\n");
message("Check done");
}
#endif
......
......@@ -90,7 +90,7 @@ void header_change_offset_direction(struct header *h, int new_value) {
h->offset_direction = new_value;
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) {
h->dump = dump;
/* 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 */
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) &&
!header_are_offset_corrupted(h))
if (!header_is_forward(h) && !header_is_backward(h) &&
!header_is_corrupted(h))
error("Wrong offset value in the header (%i)", h->offset_direction);
/* read offset to first data */
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 */
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 */
if (STRING_SIZE < h->name_length) {
......@@ -132,7 +132,7 @@ void header_read(struct header *h, struct logger_dump *dump) {
/* read number of masks */
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 */
h->masks = malloc(sizeof(struct mask_data) * h->number_mask);
......@@ -140,14 +140,14 @@ void header_read(struct header *h, struct logger_dump *dump) {
/* loop over all masks */
for (size_t i = 0; i < h->number_mask; i++) {
/* 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 */
h->masks[i].mask = 1 << i;
/* read mask data size */
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) {
......
......@@ -91,7 +91,7 @@ void header_change_offset_direction(struct header *h, int new_value);
* @brief Check if the offset are forward.
* @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) {
return h->offset_direction == logger_offset_forward;
}
......@@ -100,7 +100,7 @@ __attribute__((always_inline)) INLINE static int header_are_offset_forward(
* @brief Check if the offset are backward.
* @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) {
return h->offset_direction == logger_offset_backward;
}
......@@ -109,7 +109,7 @@ __attribute__((always_inline)) INLINE static int header_are_offset_backward(
* @brief Check if the offset are corrupted.
* @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) {
return h->offset_direction == logger_offset_corrupted;
}
......
......@@ -37,57 +37,64 @@ void io_munmap_file(void *map, size_t file_size);
*
* @param h #header file structure
* @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
* @param mask mask read
* @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(
const struct header *h, void *data, size_t *offset, size_t *mask,
__attribute__((always_inline)) INLINE static size_t io_read_mask(
const struct header *h, void *data, size_t offset, size_t *mask,
size_t *diff_offset) {
/* read mask */
if (mask) {
*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 */
if (diff_offset) {
*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 size size of the chunk to read
* @param p pointer where to store the data
* @param offset In: position to read, Out: shifted by size
* @param size size of the chunk to read.
* @param p pointer where to store the data.
* @param offset position to read.
* @return offset after the record.
*/
__attribute__((always_inline)) INLINE static void io_read_data(
void *data, const size_t size, void *p, size_t *offset) {
memcpy(p, data + *offset, size);
*offset += size;
__attribute__((always_inline)) INLINE static size_t io_read_data(
void *data, const size_t size, void *p, size_t offset) {
memcpy(p, data + 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 size size of the chunk to write
* @param p pointer to the data
* @param offset In: position to write, Out: shifted by size
* @param size size of the chunk to write.
* @param p pointer to the data.
* @param offset position to write.
*
* @return offset after the data written.
*/
__attribute__((always_inline)) INLINE static void io_write_data(
void *data, const size_t size, const void *p, size_t *offset) {
memcpy(data + *offset, p, size);
*offset += size;
__attribute__((always_inline)) INLINE static size_t io_write_data(
void *data, const size_t size, const void *p, size_t offset) {
memcpy(data + offset, p, size);
return offset + size;
};
#endif // __LOGGER_LOGGER_IO_H__
......@@ -19,6 +19,7 @@
#include "logger_particle.h"
#include "logger_header.h"
#include "logger_io.h"
#include "logger_reader.h"
#include "logger_time.h"
#include "logger_tools.h"
......@@ -63,18 +64,18 @@ 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 offset In: read position, Out: input shifted by the required amount of
* data
* @param field field to read
* @param size number of bits to read
* @param offset position to read.
* @param field field 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 *offset, const char *field,
size_t logger_particle_read_field(struct logger_particle *part, void *map,
size_t offset, const char *field,
const size_t size) {
void *p = NULL;
......@@ -96,7 +97,7 @@ void logger_particle_read_field(struct logger_particle *part, void *map,
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) {
part->mass = 0;
......@@ -107,67 +108,81 @@ void logger_particle_read_field(struct logger_particle *part, void *map,
p -= sizeof(float);
free(p);
}
return offset;
}
/**
* @brief Read a particle in the dump file.
*
* @param reader The #logger_reader.
* @param part The #logger_particle to update.
* @param h #header structure of the file
* @param map file mapping
* @param offset offset of the chunk to read (update it to the end of the chunk)
* @param offset offset of the chunk to read.
* @param time time to interpolate (if #logger_reader_type is an interpolating
* one)
* @param reader #logger_reader_type
* @param times #time_array times in the dump
* one).
* @param reader_type #logger_reader_type.
*
* @return position after the record.
*/
void logger_particle_read(struct logger_particle *part, const struct header *h,
void *map, size_t *offset, const double time,
const int reader, struct time_array *times) {
size_t logger_particle_read(struct logger_particle *part, const struct logger_reader *reader,
size_t offset, const double time,
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 h_offset = 0;
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);
for (size_t i = 0; i < h->number_mask; i++) {
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);
}
}
if (times) /* move offset by 1 in order to be in the required chunk */
part->time = time_array_get_time(times, *offset - 1);
if (times) {
/* move offset by 1 in order to be in the required chunk */
part->time = time_array_get_time(times, offset - 1);
}
else
part->time = -1;
/* end of const case */
if (reader == logger_reader_const) return;
if (reader_type == logger_reader_const)
return offset;
/* read next particle */
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.");
}
if (h_offset == 0) return;
if (h_offset == 0)
return offset;
/* 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;
part_next.time = time_array_get_time(times, h_offset);
/* previous part exists */
logger_particle_read(&part_next, h, map, &h_offset, part_next.time,
logger_reader_const, times);
h_offset = logger_particle_read(&part_next, reader, h_offset, part_next.time,
logger_reader_const);
logger_particle_interpolate(part, &part_next, time);
return offset;
}
/**
......
......@@ -28,6 +28,8 @@
#define DIM 3
struct logger_reader;
/**
* @brief Store the data from a chunk.
*
......@@ -83,14 +85,14 @@ enum logger_reader_type {
void logger_particle_print(const struct logger_particle *p);
void logger_particle_read(struct logger_particle *part, const struct header *h,
void *map, size_t *offset, const double time,
const int reader, struct time_array *times);
size_t logger_particle_read(struct logger_particle *part, const struct logger_reader *reader,
size_t offset, const double time,
const enum logger_reader_type reader_type);
void logger_particle_init(struct logger_particle *part);
void logger_particle_read_field(struct logger_particle *part, void *map,
size_t *offset, const char *field,
size_t logger_particle_read_field(struct logger_particle *part, void *map,
size_t offset, const char *field,
const size_t size);
void logger_particle_interpolate(struct logger_particle *part_curr,
......
......@@ -56,7 +56,7 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
PyArrayObject *id = NULL;
size_t time_offset;
int verbose = 0;
int verbose = 2;
/* parse arguments */
......@@ -123,14 +123,14 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
if (verbose > 1)
message("Reading particles.");
/* loop over all particles */
for (npy_intp i = 0; i < PyArray_DIMS(offset)[0]; i++) {
struct logger_particle part;
size_t *offset_particle = (size_t *)PyArray_GETPTR1(offset, i);
size_t offset_particle = *(size_t *)PyArray_GETPTR1(offset, i);
logger_particle_read(&part, &reader.dump.header, reader.dump.dump.map, offset_particle, time,
logger_reader_lin, &reader.dump.times);
logger_particle_read(&part, &reader, offset_particle, time, logger_reader_lin);
double *dtmp;
float *ftmp;
......
......@@ -26,19 +26,24 @@
*
* @param timestamp timestamp read
* @param time time read
* @param h #header file structure
* @param map file mapping
* @param offset In: position in the file, Out: shifted by the timestamp
* @param reader The #logger_reader
* @param offset position in the file
*
* @return position after the timestamp
*/
void time_read(integertime_t *timestamp, double *time, const struct header *h,
void *map, size_t *offset) {
size_t time_read(integertime_t *timestamp, double *time, const struct logger_reader *reader,
size_t offset) {
const struct header *h = &reader->dump.header;
void *map = h->dump->dump.map;
size_t mask = 0;
size_t prev_offset = 0;
*timestamp = 0;
*time = 0;
/* read chunck header */
io_read_mask(h, map, offset, &mask, &prev_offset);
offset = io_read_mask(h, map, offset, &mask, &prev_offset);
#ifdef SWIFT_DEBUG_CHECKS
......@@ -51,33 +56,44 @@ void time_read(integertime_t *timestamp, double *time, const struct header *h,
#endif
/* read data */
// TODO
io_read_data(map, sizeof(unsigned long long int), timestamp, offset);
io_read_data(map, sizeof(double), time, offset);
offset = io_read_data(map, sizeof(unsigned long long int), timestamp, offset);
offset = io_read_data(map, sizeof(double), time, offset);
return offset;
}
/**
* @brief get offset of first timestamp
*
* @param h file #header
* @param map file mapping
* @param offset out: offset of first timestamp
* @return offset of first timestamp
*
*/
void time_first_timestamp(const struct header *h, void *map, size_t *offset) {
*offset = h->offset_first;
size_t time_first_timestamp(const struct header *h) {
size_t offset = h->offset_first;
void *map = h->dump->dump.map;
int i = header_get_field_index(h, "timestamp");
if (i == -1) error("Time stamp not present in header");
size_t tmp = *offset;
size_t mask = 0;
io_read_mask(h, map, offset, &mask, NULL);
if (mask != h->masks[i].mask) error("Dump should begin by timestep");
*offset = tmp;
return h->offset_first;
}
/**
* @brief Initialize an empty time array.
*
* @param t #time_array to initialize.
* @param dump The #logger_dump.
*/
void time_array_init_to_zero(struct time_array *t) {
t->next = NULL;
t->prev = NULL;
}
/**
......@@ -91,21 +107,19 @@ void time_array_init(struct time_array *t, struct logger_dump *dump) {
t->next = NULL;
t->prev = NULL;
/* get first time stamp */
size_t offset = 0;
time_first_timestamp(&dump->header, dump->dump.map, &offset);
integertime_t timestamp = 0;
double time = 0;
/* get file size */
size_t file_size = dump->dump.file_size;
/* get first time stamp */
size_t offset = time_first_timestamp(&dump->header);
while (offset < file_size) {
/* read time */
t->offset = offset;
size_t tmp_offset = offset;
time_read(&timestamp, &time, &dump->header, dump->dump.map, &tmp_offset);
tmp_offset = time_read(&timestamp, &time, dump->reader, tmp_offset);
t->timestamp = timestamp;
t->time = time;
......@@ -150,7 +164,7 @@ integertime_t time_array_get_integertime(struct time_array *t,
*
* @return time of the chunk
*/
double time_array_get_time(struct time_array *t, const size_t offset) {
double time_array_get_time(const struct time_array *t, const size_t offset) {
const struct time_array *tmp = time_array_get_time_array(t, offset);
return tmp->time;
}
......@@ -163,22 +177,19 @@ double time_array_get_time(struct time_array *t, const size_t offset) {
*
* @return pointer to the requested #time_array
*/
struct time_array *time_array_get_time_array(struct time_array *t,
struct time_array *time_array_get_time_array(const struct time_array *t,
const size_t offset) {
#ifdef SWIFT_DEBUG_CHECKS
if (!t) return 0;
if (!t) error("NULL pointer");
#endif
while (t->next) {
if (t->offset > offset) break;
t = t->next;
}
const struct time_array *tmp;