Skip to content
Snippets Groups Projects
Commit bc867e00 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: cleanup the code

parent 7f9436fd
Branches
Tags
1 merge request!685Logger loader
......@@ -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,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 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,
const size_t size) {
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;
if (strcmp("positions", field) == 0) {
......@@ -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,
h->masks[i].size);
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;
const struct time_array *tmp;
for(tmp = t; tmp->next && tmp->offset <= offset; tmp = tmp->next)
{}
t = t->next;
}
if (tmp->prev == NULL) return (struct time_array *) tmp;
if (t->prev == NULL) return t;
struct time_array *tmp = t->prev;
return tmp;
return (struct time_array *) tmp->prev;
}
/**
......@@ -188,10 +199,9 @@ struct time_array *time_array_get_time_array(struct time_array *t,
*/
void time_array_free(struct time_array *t) {
struct time_array *tmp;
while (t->next) {
for(tmp = t; t->next; t = tmp) {
tmp = t->next;
free(t);
t = tmp;
}
}
......@@ -204,13 +214,15 @@ void time_array_print(const struct time_array *t) {
size_t threshold = 4;
size_t n = time_array_count(t);
size_t i = 0;
size_t up_threshold = n - threshold;
printf("Times (size %lu): [%lli (%g)", n, t->timestamp, t->time);
while (t->next) {
i += 1;
for(size_t i = 1; i < n; i++) {
/* The last time_array does not have a next */
if (!t->next)
error("Next pointer not initialized %li", i);
t = t->next;
if (i < threshold || i > up_threshold)
printf(", %lli (%g)", t->timestamp, t->time);
......@@ -230,13 +242,12 @@ void time_array_print_offset(const struct time_array *t) {
size_t threshold = 4;
size_t n = time_array_count(t);
size_t i = 0;
size_t up_threshold = n - threshold;
printf("Times (size %lu): [%lu", n, t->offset);
while (t->next) {
i += 1;
for(size_t i = 1; i < n; i++) {
t = t->next;
if (i < threshold || i > up_threshold) printf(", %lu", t->offset);
......@@ -255,8 +266,7 @@ void time_array_print_offset(const struct time_array *t) {
*/
size_t time_array_count(const struct time_array *t) {
size_t count = 1;
while (t->next) {
t = t->next;
for(; t->next; t = t->next) {
count += 1;
}
......
......@@ -25,6 +25,8 @@
typedef char timebin_t;
typedef long long integertime_t;
struct logger_reader;
/**
* @brief This structure contains all the timestamp.
*
......@@ -59,18 +61,26 @@ struct time_array {
size_t offset;
};
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);
void time_array_init_to_zero(struct time_array *t);
void time_array_init(struct time_array *t, struct logger_dump *dump);
integertime_t time_array_get_integertime(struct time_array *t,
const size_t offset);
double time_array_get_time(struct time_array *t, const size_t offset);
struct time_array *time_array_get_time_array(struct time_array *t,
const size_t offset);
integertime_t time_array_get_integertime(struct time_array *t, const size_t offset);
double time_array_get_time(const struct time_array *t, const size_t offset);
struct time_array *time_array_get_time_array(const struct time_array *t, const size_t offset);
void time_array_free(struct time_array *t);
void time_array_print(const struct time_array *t);
void time_array_print_offset(const struct time_array *t);
size_t time_array_count(const struct time_array *t);
void time_first_timestamp(const struct header *h, void *map, size_t *offset);
size_t time_first_timestamp(const struct header *h);
#endif // __LOGGER_LOGGER_TIMELINE_H__
......@@ -19,6 +19,7 @@
#include "logger_tools.h"
#include "logger_header.h"
#include "logger_io.h"
#include "logger_reader.h"
#include "logger_particle.h"
......@@ -36,9 +37,9 @@
*/
int tools_get_next_chunk(const struct header *h, void *map, size_t *offset,
size_t file_size) {
if (header_are_offset_forward(h))
if (header_is_forward(h))
return _tools_get_next_chunk_forward(h, map, offset);
if (header_are_offset_backward(h))
if (header_is_backward(h))
return _tools_get_next_chunk_backward(h, map, offset, file_size);
else
error("Offsets are corrupted");
......@@ -58,7 +59,7 @@ int _tools_get_next_chunk_forward(const struct header *h, void *map,
size_t diff_offset = 0;
/* read offset */
io_read_mask(h, map, offset, NULL, &diff_offset);
*offset = io_read_mask(h, map, *offset, NULL, &diff_offset);
if (diff_offset == 0) return -1;
......@@ -89,7 +90,7 @@ int _tools_get_next_chunk_backward(const struct header *h, void *map,
while (current_offset < file_size) {
size_t mask = 0;
size_t prev_offset;
io_read_mask(h, map, &current_offset, &mask, &prev_offset);
current_offset = io_read_mask(h, map, current_offset, &mask, &prev_offset);
prev_offset = current_offset - prev_offset - chunk_header;
if (*offset == prev_offset) {
......@@ -104,32 +105,34 @@ int _tools_get_next_chunk_backward(const struct header *h, void *map,
}
/**
* @brief switch side offset
* @brief switch side offset.
*
* From current chunk, switch side of the offset of the previous one.
* @param h #header structure of the file
* @param map file mapping
* @param offset In: initial offset, Out: offset of next chunk
* @param h #header structure of the file.
* @param map file mapping.
* @param position of the record.
*
* @return position after the record.
*/
void tools_reverse_offset(const struct header *h, void *map, size_t *offset) {
size_t tools_reverse_offset(const struct header *h, void *map, size_t offset) {
size_t mask = 0;
size_t prev_offset = 0;
const size_t cur_offset = *offset;
const size_t cur_offset = offset;
/* read mask + offset */
io_read_mask(h, map, offset, &mask, &prev_offset);
offset = io_read_mask(h, map, offset, &mask, &prev_offset);
/* write offset of zero (in case it is the last chunk) */
const size_t zero = 0;
*offset -= LOGGER_OFFSET_SIZE;
io_write_data(map, LOGGER_OFFSET_SIZE, &zero, offset);
offset -= LOGGER_OFFSET_SIZE;
offset = io_write_data(map, LOGGER_OFFSET_SIZE, &zero, offset);
/* set offset after current chunk */
*offset += header_get_mask_size(h, mask);
offset += header_get_mask_size(h, mask);
/* first chunks do not have a previous partner */
if (prev_offset == cur_offset) return;
if (prev_offset == cur_offset)
return offset;
if (prev_offset > cur_offset)
error("Unexpected offset, header %lu, current %lu", prev_offset,
......@@ -137,18 +140,20 @@ void tools_reverse_offset(const struct header *h, void *map, size_t *offset) {
/* modify previous offset */
size_t abs_prev_offset = cur_offset - prev_offset + LOGGER_MASK_SIZE;
io_write_data(map, LOGGER_OFFSET_SIZE, &prev_offset, &abs_prev_offset);
abs_prev_offset = io_write_data(map, LOGGER_OFFSET_SIZE, &prev_offset, abs_prev_offset);
#ifdef SWIFT_DEBUG_CHECKS
size_t prev_mask = 0;
abs_prev_offset -= LOGGER_MASK_SIZE + LOGGER_OFFSET_SIZE;
io_read_mask(h, map, &abs_prev_offset, &prev_mask, NULL);
abs_prev_offset = io_read_mask(h, map, abs_prev_offset, &prev_mask, NULL);
if (prev_mask != mask)
error("Unexpected mask: %lu (at %lu), got %lu (at %lu)", mask, *offset,
error("Unexpected mask: %lu (at %lu), got %lu (at %lu)", mask, offset,
prev_mask, prev_offset);
#endif // SWIFT_DEBUG_CHECKS
return offset;
}
/**
......@@ -156,29 +161,32 @@ void tools_reverse_offset(const struct header *h, void *map, size_t *offset) {
*
* Compare the mask with the one pointed by the header.
* if the chunk is a particle, check the id too.
* @param h #header structure of the file
* @param map file mapping
* @param offset In: chunk offset, Out: offset after the chunk
*
* @return error code
* @param reader The #logger_reader.
* @param offset position of the record.
*
* @return position after the record.
*/
void tools_check_offset(const struct header *h, void *map, size_t *offset) {
size_t tools_check_offset(const struct logger_reader *reader, size_t offset) {
#ifndef SWIFT_DEBUG_CHECKS
error("Should not check in non debug mode");
#endif
size_t tmp = *offset;
const struct header *h = &reader->dump.header;
void *map = reader->dump.dump.map;
size_t tmp = offset;
size_t mask;
size_t pointed_offset;
/* read mask + offset */
io_read_mask(h, map, offset, &mask, &pointed_offset);
offset = io_read_mask(h, map, offset, &mask, &pointed_offset);
/* get absolute offset */
if (header_are_offset_forward(h))
if (header_is_forward(h))
pointed_offset += tmp;
if (header_are_offset_backward(h)) {
else if (header_is_backward(h)) {
if (tmp < pointed_offset)
error("Offset too large (%lu) at %lu with mask %lu", pointed_offset, tmp,
mask);
......@@ -189,31 +197,36 @@ void tools_check_offset(const struct header *h, void *map, size_t *offset) {
}
/* set offset after current chunk */
*offset += header_get_mask_size(h, mask);
offset += header_get_mask_size(h, mask);
if (pointed_offset == tmp || pointed_offset == 0) return;
if (pointed_offset == tmp || pointed_offset == 0)
return offset;
/* read mask of the pointed chunk */
size_t pointed_mask = 0;
io_read_mask(h, map, &pointed_offset, &pointed_mask, NULL);
pointed_offset = io_read_mask(h, map, pointed_offset, &pointed_mask, NULL);
/* check masks */
if (pointed_mask != mask)
error("Error in the offset (mask %lu != %lu) at %lu and %lu", mask,
pointed_mask,
*offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE -
LOGGER_OFFSET_SIZE,
offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE -
LOGGER_OFFSET_SIZE,
pointed_offset - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE);
if (pointed_mask == 128) return;
if (pointed_mask == 128)
return offset;
struct logger_particle part;
logger_particle_read(&part, h, map, &tmp, 0, logger_reader_const, NULL);
tmp = logger_particle_read(&part, reader, tmp, 0, logger_reader_const);
size_t id = part.id;
tmp = pointed_offset - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE;
logger_particle_read(&part, h, map, &tmp, 0, logger_reader_const, NULL);
tmp = logger_particle_read(&part, reader, tmp, 0, logger_reader_const);
if (id != part.id)
error("Offset wrong, id incorrect (%lu != %lu) at %lu", id, part.id, tmp);
return offset;
}
......@@ -41,6 +41,7 @@
#define STRING_SIZE 200
struct header;
struct logger_reader;
#define error(s, ...) \
({ \
......@@ -62,7 +63,7 @@ int _tools_get_next_chunk_backward(const struct header *h, void *map,
size_t *offset, size_t file_size);
int _tools_get_next_chunk_forward(const struct header *h, void *map,
size_t *offset);
void tools_reverse_offset(const struct header *h, void *map, size_t *offset);
void tools_check_offset(const struct header *h, void *map, size_t *offset);
size_t tools_reverse_offset(const struct header *h, void *map, size_t offset);
size_t tools_check_offset(const struct logger_reader *reader, size_t offset);
#endif //__LOGGER_LOGGER_TOOLS_H__
......@@ -14,7 +14,7 @@ import liblogger as logger
# Get filenames
if len(sys.argv) != 3:
print("WARNING missing arguments. Will use the default ones")
index = "../../examples/SedovBlast_3D/index_0005.hdf5"
index = "../../examples/SedovBlast_3D/index_0002.hdf5"
dump = "../../examples/SedovBlast_3D/index.dump"
else:
index = sys.argv[-1]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment