Commit 3ff86beb authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: still cleaning

parent d40d0057
......@@ -43,11 +43,11 @@ lib_LTLIBRARIES = liblogger.la
# List required headers
include_HEADERS = logger_header.h logger_io.h logger_particle.h logger_time.h logger_tools.h logger_reader.h \
logger_index.h logger_dump.h
logger_index.h logger_logfile.h
# Common source files
AM_SOURCES = logger_header.c logger_io.c logger_particle.c logger_time.c logger_tools.c logger_reader.c \
logger_dump.c
logger_logfile.c
if HAVEPYTHON
AM_SOURCES += logger_python_wrapper.c
endif
......
......@@ -18,9 +18,10 @@
******************************************************************************/
#include "logger_header.h"
#include "logger_reader.h"
#include "logger_io.h"
#include "logger_tools.h"
#include "logger_dump.h"
#include "logger_logfile.h"
#include <stdio.h>
#include <stdlib.h>
......@@ -43,22 +44,22 @@ void header_print(const struct header *h) {
message("Debug checks enabled");
#endif
message("Version: %s", h->version);
message("First Offset: %lu", h->offset_first);
message("First Offset: %lu", h->offset_first_record);
message("Offset direction: %s", logger_offset_name[h->offset_direction]);
message("Number masks: %lu", h->number_mask);
for (size_t i = 0; i < h->number_mask; i++) {
message("\tMask: %s", h->masks[i].name);
message("\tValue: %u", h->masks[i].mask);
message("\tSize: %i", h->masks[i].size);
message(" Mask: %s", h->masks[i].name);
message(" Value: %u", h->masks[i].mask);
message(" Size: %i", h->masks[i].size);
message("");
}
};
/**
* @brief free allocated memory
* @brief free allocated memory.
*
* @param h The #header
* @param h The #header.
*/
void header_free(struct header *h) { free(h->masks); };
......@@ -80,34 +81,49 @@ int header_get_field_index(const struct header *h, const char *field) {
};
/**
* @brief Inverse the offset direction
* @brief Inverse the offset direction.
*
* @param h #header file structure
* @param new_value The new value to write
* @param h #header file structure.
* @param new_value The new value to write.
*
*/
void header_change_offset_direction(struct header *h, int new_value) {
void header_change_offset_direction(struct header *h, enum logger_offset_direction new_value) {
h->offset_direction = new_value;
size_t offset = LOGGER_VERSION_SIZE;
/* Skip file format and version numbers */
size_t offset = LOGGER_VERSION_SIZE + 2 * sizeof(int);
io_write_data(h->dump->dump.map, LOGGER_NUMBER_SIZE, &new_value, offset);
io_write_data(h->log->log.map, LOGGER_NUMBER_SIZE, &new_value, offset);
}
/**
* @brief read the logger header
* @brief read the logger header.
*
* @param h out: The #header.
* @param dump The #logger_dump.
* @param log The #logger_logfile.
*/
void header_read(struct header *h, struct logger_dump *dump) {
void header_read(struct header *h, struct logger_logfile *log) {
size_t offset = 0;
void *map = dump->dump.map;
void *map = log->log.map;
/* Set pointer to log */
h->log = log;
/* read file format */
char file_format[STRING_SIZE];
offset = io_read_data(map, LOGGER_VERSION_SIZE, &file_format, offset);
if (strcmp(file_format, "SWIFT_LOGGER"))
error("Wrong file format");
/* Read major version */
int major;
offset = io_read_data(map, sizeof(int), &major, offset);
/* Set pointer to dump */
h->dump = dump;
/* Read minor version */
int minor;
offset = io_read_data(map, sizeof(int), &minor, offset);
/* read version */
offset = io_read_data(map, LOGGER_VERSION_SIZE, &h->version, offset);
if (h->log->reader->verbose > 0)
message("File version %i.%i", major, minor);
/* read offset direction */
offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->offset_direction, offset);
......@@ -116,15 +132,15 @@ void header_read(struct header *h, struct logger_dump *dump) {
!header_is_corrupted(h))
error("Wrong offset value in the header (%i)", h->offset_direction);
/* read offset to first data */
offset = io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first, offset);
/* read offset to first record */
offset = io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first_record, offset);
/* read name size */
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) {
error("Name too large in dump file");
error("Name too large in log file");
}
/* read number of masks */
......@@ -145,22 +161,22 @@ void header_read(struct header *h, struct logger_dump *dump) {
offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->masks[i].size, offset);
}
if (offset != h->offset_first) {
if (offset != h->offset_first_record) {
header_print(h);
error("Wrong header size (in header %zi, current %zi)", h->offset_first,
error("Wrong header size (in header %zi, current %zi)", h->offset_first_record,
offset);
}
};
/**
* @brief count number of bits in a given mask
* @brief Count number of bits in a given mask (without the record header).
*
* @param h #header file structure
* @param mask mask to compute
* @param h #header file structure.
* @param mask Mask to compute.
*
* @return number of bits in mask
* @return number of bits in mask.
*/
size_t header_get_mask_size(const struct header *h, const size_t mask) {
size_t header_get_record_size_from_mask(const struct header *h, const size_t mask) {
size_t count = 0;
for (size_t i = 0; i < h->number_mask; i++) {
if (mask & h->masks[i].mask) {
......
......@@ -43,7 +43,7 @@ enum logger_offset_direction {
extern const char *logger_offset_name[];
struct logger_dump;
struct logger_logfile;
/**
* @brief This structure contains everything from the file header.
......@@ -60,8 +60,8 @@ struct header {
/* Logger version. */
char version[STRING_SIZE];
/* Offset of the first header. */
size_t offset_first;
/* Offset of the first record. */
size_t offset_first_record;
/* Number of bytes for names. */
size_t name_length;
......@@ -72,19 +72,19 @@ struct header {
/* List of masks. */
struct mask_data *masks;
/* Direction of the offset in the chunks. */
/* Direction of the offset in the records. */
enum logger_offset_direction offset_direction;
/* The corresponding dump */
struct logger_dump *dump;
/* The corresponding log */
struct logger_logfile *log;
};
void header_print(const struct header *h);
void header_free(struct header *h);
int header_get_field_index(const struct header *h, const char *field);
void header_read(struct header *h, struct logger_dump *dump);
void header_read(struct header *h, struct logger_logfile *log);
size_t header_get_mask_size(const struct header *h, const size_t mask);
void header_change_offset_direction(struct header *h, int new_value);
void header_change_offset_direction(struct header *h, enum logger_offset_direction new_value);
/**
......
......@@ -26,11 +26,11 @@
#include "logger_tools.h"
/**
* @brief get the size of a file
* @brief get the size of a file.
*
* @param fd file id
* @param fd file id.
*
* @return file size
* @return file size.
*/
size_t io_get_file_size(int fd) {
struct stat s;
......@@ -69,9 +69,9 @@ void *io_mmap_file(char *filename, size_t *file_size) {
}
/**
* @brief Unmap a file
* @brief Unmap a file.
*
* @param map file mapping
* @param map file mapping.
* @param file_size The file size.
*
*/
......
......@@ -33,16 +33,16 @@ void *io_mmap_file(char *filename, size_t *file_size);
void io_munmap_file(void *map, size_t file_size);
/**
* @brief read a mask with its offset
* @brief read a mask with its offset.
*
* @param h #header file structure
* @param h #header file structure.
* @param data Pointer to the data to read.
* @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
* @param offset position in the file.
* @param mask mask read from the data.
* @param diff_offset offset difference to previous/next corresponding record read.
* from the data.
*
* @return offset after the record header
* @return offset after the record header.
*/
__attribute__((always_inline)) INLINE static size_t io_read_mask(
const struct header *h, void *data, size_t offset, size_t *mask,
......@@ -68,11 +68,11 @@ __attribute__((always_inline)) INLINE static size_t io_read_mask(
* @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 size size of the data to read.
* @param p pointer where to store the data.
* @param offset position to read.
* @return offset after the record.
* @return offset after the data written.
*/
__attribute__((always_inline)) INLINE static size_t io_read_data(
void *data, const size_t size, void *p, size_t offset) {
......@@ -84,7 +84,7 @@ __attribute__((always_inline)) INLINE static size_t io_read_data(
* @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 size size of the data to write.
* @param p pointer to the data.
* @param offset position to write.
*
......
......@@ -16,62 +16,62 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "logger_dump.h"
#include "logger_logfile.h"
#include "logger_reader.h"
#include "logger_io.h"
/**
* @brief Initialize the #logger_dump.
* @brief Initialize the #logger_logfile.
*
* If required this function will also reverse the offsets.
* @param dump The #logger_dump.
* @param filename the dump's filename.
* @param log The #logger_logfile.
* @param filename the log's filename.
* @param reader The #logger_reader.
*/
void logger_dump_init(
struct logger_dump *dump, char *filename,
void logger_logfile_init(
struct logger_logfile *log, char *filename,
struct logger_reader *reader) {
/* Set the pointer to the reader. */
dump->reader = reader;
log->reader = reader;
/* Set pointers to zero */
time_array_init_to_zero(&dump->times);
time_array_init_to_zero(&log->times);
/* Open file, map it and get its size. */
if (reader->verbose > 1)
message("Mapping the dump file.");
dump->dump.map = io_mmap_file(filename, &dump->dump.file_size);
message("Mapping the log file.");
log->log.map = io_mmap_file(filename, &log->log.file_size);
/* Read header. */
if (reader->verbose > 1)
message("Reading the header.");
header_read(&dump->header, dump);
header_read(&log->header, log);
/* Print the header. */
if (reader->verbose > 0) {
header_print(&dump->header);
header_print(&log->header);
}
/* Check if the offset are corrupted */
if (header_is_corrupted(&dump->header)) {
if (header_is_corrupted(&log->header)) {
error("The offsets have been corrupted");
}
/* Reverse offset direction */
if (reader->verbose > 1)
message("Checking if offsets need to be reversed.");
if (header_is_backward(&dump->header)) {
logger_dump_reverse_offset(dump);
if (header_is_backward(&log->header)) {
logger_logfile_reverse_offset(log);
}
/* Initialize the time array */
if (reader->verbose > 1)
message("Reading the time stamps.");
time_array_init(&dump->times, dump);
time_array_init(&log->times, log);
if (reader->verbose > 0) {
time_array_print(&dump->times);
time_array_print(&log->times);
}
}
......@@ -79,23 +79,23 @@ void logger_dump_init(
/**
* @brief Free the allocated memory and unmap the file.
*
* @param dump The #logger_dump.
* @param log The #logger_logfile.
*/
void logger_dump_free(struct logger_dump *dump) {
io_munmap_file(dump->dump.map, dump->dump.file_size);
void logger_logfile_free(struct logger_logfile *log) {
io_munmap_file(log->log.map, log->log.file_size);
}
/**
* @brief Reverse offset in dump file
* @brief Reverse offset in log file
*
* @param dump The #logger_dump
* @param log The #logger_logfile
*/
void logger_dump_reverse_offset(struct logger_dump *dump) {
void logger_logfile_reverse_offset(struct logger_logfile *log) {
struct header *header = &dump->header;
const struct logger_reader *reader = dump->reader;
struct header *header = &log->header;
const struct logger_reader *reader = log->reader;
if (!header_is_backward(header)) {
error("The offset are already reversed.");
......@@ -108,9 +108,9 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
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)) {}
for(size_t offset_debug = header->offset_first_record;
offset_debug < log->log.file_size;
offset_debug = tools_check_record_consistency(reader, offset_debug)) {}
if (reader->verbose > 0) {
message("Check done");
......@@ -120,14 +120,14 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
/* reverse header offset */
header_change_offset_direction(header, logger_offset_corrupted);
/* reverse chunks */
/* reverse record */
if (reader->verbose > 0) {
message("Reversing offsets...");
}
for(size_t offset = header->offset_first;
offset < dump->dump.file_size;
offset = tools_reverse_offset(header, dump->dump.map, offset)) {}
for(size_t offset = header->offset_first_record;
offset < log->log.file_size;
offset = tools_reverse_offset(header, log->log.map, offset)) {}
if (reader->verbose > 0) {
message("Reversing done");
......@@ -142,9 +142,9 @@ void logger_dump_reverse_offset(struct logger_dump *dump) {
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)) {}
for(size_t offset_debug = header->offset_first_record;
offset_debug < log->log.file_size;
offset_debug = tools_check_record_consistency(reader, offset_debug)) {}
if (reader->verbose > 0) {
message("Check done");
......
......@@ -17,10 +17,10 @@
*
******************************************************************************/
/**
* @brief This file contains the high level function for the dump.
* @brief This file contains the high level function for the log.
*/
#ifndef __LOGGER_LOGGER_DUMP_H__
#define __LOGGER_LOGGER_DUMP_H__
#ifndef __LOGGER_LOGGER_LOGFILE_H__
#define __LOGGER_LOGGER_LOGFILE_H__
#include "logger_header.h"
#include "logger_time.h"
......@@ -28,22 +28,22 @@
struct logger_reader;
/**
* @brief This structure deals with the dump file.
* @brief This structure deals with the log file.
*
* TODO
*/
struct logger_dump {
struct logger_logfile {
/* Information contained in the file header. */
struct header header;
/* The reader that is using this dump. */
/* The reader that is using this log file. */
struct logger_reader *reader;
/* Information about the time chunks */
/* Information about the time records */
struct time_array times;
/* The dump's variables. */
/* The log's variables. */
struct {
/* Mapped data */
void *map;
......@@ -51,13 +51,13 @@ struct logger_dump {
/* File size */
size_t file_size;
} dump;
} log;
};
void logger_dump_init(struct logger_dump *dump, char *filename, struct logger_reader *reader);
void logger_dump_reverse_offset(struct logger_dump *dump);
void logger_dump_free(struct logger_dump *dump);
void logger_logfile_init(struct logger_logfile *log, char *filename, struct logger_reader *reader);
void logger_logfile_reverse_offset(struct logger_logfile *log);
void logger_logfile_free(struct logger_logfile *log);
#endif // __LOGGER_LOGGER_DUMP_H__
#endif // __LOGGER_LOGGER_LOGFILE_H__
......@@ -64,7 +64,7 @@ void logger_particle_init(struct logger_particle *part) {
}
/**
* @brief Read a single field for a particle.
* @brief Read a single named entry for a particle.
*
* @param part The #logger_particle to update.
* @param map The mapped data.
......@@ -79,6 +79,7 @@ size_t logger_particle_read_field(struct logger_particle *part, void *map,
const size_t size) {
void *p = NULL;
/* Get the correct pointer */
if (strcmp("positions", field) == 0) {
p = &part->pos;
} else if (strcmp("velocities", field) == 0) {
......@@ -97,8 +98,10 @@ size_t logger_particle_read_field(struct logger_particle *part, void *map,
error("Type %s not defined", field);
}
/* read the data */
offset = io_read_data(map, size, p, offset);
/* Split the required fields */
if (strcmp("consts", field) == 0) {
part->mass = 0;
part->id = 0;
......@@ -113,13 +116,12 @@ size_t logger_particle_read_field(struct logger_particle *part, void *map,
}
/**
* @brief Read a particle in the dump file.
* @brief Read a particle entry in the log file.
*
* @param reader The #logger_reader.
* @param part The #logger_particle to update.
* @param offset offset of the chunk to read.
* @param time time to interpolate (if #logger_reader_type is an interpolating
* one).
* @param offset offset of the record to read.
* @param time time to interpolate.
* @param reader_type #logger_reader_type.
*
* @return position after the record.
......@@ -128,10 +130,10 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
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 header *h = &reader->log.header;
void *map = reader->log.log.map;
const struct time_array *times = &reader->dump.times;
const struct time_array *times = &reader->log.times;
size_t mask = 0;
size_t h_offset = 0;
......@@ -150,7 +152,7 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
}
if (times) {
/* move offset by 1 in order to be in the required chunk */
/* move offset by 1 in order to be in the required record */
part->time = time_array_get_time(times, offset - 1);
}
else
......@@ -194,9 +196,9 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
* @param time interpolation time
*
*/
void logger_particle_interpolate(struct logger_particle *part_curr,
const struct logger_particle *part_next,
const double time) {
void logger_particle_interpolate(
struct logger_particle *part_curr, const struct logger_particle *part_next,
const double time) {
if (!part_curr) error("part_curr is NULL");
if (!part_next) error("part_next is NULL");
......@@ -206,7 +208,7 @@ void logger_particle_interpolate(struct logger_particle *part_curr,
error("Wrong particle order (next before current)");
if ((time < part_curr->time) || (part_next->time < time))
error(
"Interpolating, not extrapolating (particle time: %f, "
"Cannot extrapolate (particle time: %f, "
"interpolating time: %f, next particle time: %f)",
part_curr->time, time, part_next->time);
#endif
......
......@@ -26,21 +26,26 @@
#include <stdio.h>
#include <stdlib.h>
#if defined(HYDRO_DIMENSION_1D)
#define DIM 1
#elif defined(HYDRO_DIMENSION_2D)
#define DIM 2
#elif defined(HYDRO_DIMENSION_3D)
#define DIM 3
#endif
struct logger_reader;
/**
* @brief Store the data from a chunk.
* @brief Store the data from a record.
*
* This structure contains all the required fields
* present in a file.
*
* As we need only a single particle, no need to keep
* it small.
* As we need only a few particles, no need to keep it small.
*
* The particle is initialized with #logger_particle_init
* and can be updated with a chunk through #logger_particle_read.
* and can be updated with a record through #logger_particle_read.
*
* In #logger_particle_read, we use #logger_particle_read_field on
* each field and #logger_particle_interpolate if a linear
......