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

Update comments

parent b474d592
......@@ -27,7 +27,7 @@
#include <stdlib.h>
#include <string.h>
/* Offset type names. */
/* Name of each offset direction. */
const char *logger_offset_name[logger_offset_count] = {
"Forward", "Backward", "Corrupted",
};
......@@ -57,7 +57,7 @@ void header_print(const struct header *h) {
};
/**
* @brief free allocated memory.
* @brief free the allocated memory.
*
* @param h The #header.
*/
......@@ -114,18 +114,18 @@ void header_read(struct header *h, struct logger_logfile *log) {
if (strcmp(file_format, "SWIFT_LOGGER"))
error("Wrong file format");
/* Read major version */
/* Read major version number */
int major;
offset = io_read_data(map, sizeof(int), &major, offset);
/* Read minor version */
/* Read minor version number */
int minor;
offset = io_read_data(map, sizeof(int), &minor, offset);
if (h->log->reader->verbose > 0)
message("File version %i.%i", major, minor);
/* read offset direction */
/* read the offset directions */
offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->offset_direction, offset);
if (!header_is_forward(h) && !header_is_backward(h) &&
......@@ -135,32 +135,33 @@ void header_read(struct header *h, struct logger_logfile *log) {
/* 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);
/* read the size of the strings */
offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->string_length, offset);
/* check if value defined in this file is large enough */
if (STRING_SIZE < h->name_length) {
/* check if value defined in this file is large enough. */
if (STRING_SIZE < h->string_length) {
error("Name too large in log file");
}
/* read number of masks */
/* read the number of masks. */
offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->number_mask, offset);
/* allocate memory */
/* allocate the masks memory. */
h->masks = malloc(sizeof(struct mask_data) * h->number_mask);
/* loop over all masks */
/* loop over all masks. */
for (size_t i = 0; i < h->number_mask; i++) {
/* read mask name */
offset = io_read_data(map, h->name_length, h->masks[i].name, offset);
/* read the mask name. */
offset = io_read_data(map, h->string_length, h->masks[i].name, offset);
/* set mask value */
/* set the mask value. */
h->masks[i].mask = 1 << i;
/* read mask data size */
/* read the mask data size. */
offset = io_read_data(map, LOGGER_NUMBER_SIZE, &h->masks[i].size, offset);
}
/* Check the logfile header's size */
if (offset != h->offset_first_record) {
header_print(h);
error("Wrong header size (in header %zi, current %zi)", h->offset_first_record,
......@@ -178,6 +179,7 @@ void header_read(struct header *h, struct logger_logfile *log) {
*/
size_t header_get_record_size_from_mask(const struct header *h, const size_t mask) {
size_t count = 0;
/* Loop over each masks */
for (size_t i = 0; i < h->number_mask; i++) {
if (mask & h->masks[i].mask) {
count += h->masks[i].size;
......
......@@ -52,7 +52,7 @@ struct logger_logfile;
* with #header_free.
*
* The information contained by the header can be easily access with
* the functions #header_get_mask_size and #header_get_field_index.
* the functions #header_get_record_size_from_mask and #header_get_field_index.
*
* The only function that modify the file is #header_change_offset_direction.
*/
......@@ -63,8 +63,8 @@ struct header {
/* Offset of the first record. */
size_t offset_first_record;
/* Number of bytes for names. */
size_t name_length;
/* Number of bytes for strings. */
size_t string_length;
/* Number of masks. */
size_t number_mask;
......@@ -83,7 +83,7 @@ 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_logfile *log);
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);
void header_change_offset_direction(struct header *h, enum logger_offset_direction new_value);
......
......@@ -38,9 +38,13 @@
void logger_index_init(struct logger_index *index, struct logger_reader *reader,
char *basename) {
/* Set pointers to 0 */
/* Set pointers to 0. */
index->offsets = NULL;
index->ids = NULL;
/* Set variables default value. */
index->current_file = -1;
index->number_of_particles = 0;
}
/**
......@@ -50,39 +54,45 @@ void logger_index_init(struct logger_index *index, struct logger_reader *reader,
* @param i The index of the file.
*/
void logger_index_read_file(struct logger_index *index, int i) {
/* Cleanup the memory of previous file */
/* Cleanup the memory of previous file. */
logger_index_free_current_file(index);
/* Open file */
/* Save current index. */
index->current_file = i;
/* Open file. */
FILE *f = NULL;
f = fopen(index->filenames[i], "wb");
/* Read the double time */
/* Read the double time. */
double time;
fread(&time, sizeof(double), 1, f);
/* Read integer time */
/* Read the integer time. */
double int_time;
fread(&int_time, sizeof(integertime_t), 1, f);
/* Read the number of particles */
/* Read the number of particles. */
long long N_total[swift_type_count];
fread(N_total, sizeof(long long), swift_type_count, f);
/* Count total number of particles */
/* Count total number of particles. */
long long N = 0;
for(int i = 0; i < swift_type_count; i++) {
N += N_total[i];
}
/* Read Ids */
index->number_of_particles = N;
/* Read the particles ids. */
if (posix_memalign((void**)&index->ids, IO_BUFFER_ALIGNMENT,
N * sizeof(long long)) != 0)
error("Unable to allocate the offset buffer");
fread(index->ids, sizeof(size_t), N, f);
/* Read offsets */
/* Read the particles offsets. */
if (posix_memalign((void**)&index->offsets, IO_BUFFER_ALIGNMENT,
N * sizeof(size_t)) != 0)
error("Unable to allocate the offset buffer");
......@@ -90,7 +100,7 @@ void logger_index_read_file(struct logger_index *index, int i) {
fread(index->offset, sizeof(size_t), N, f);
/* Close file */
/* Close the file. */
fclose(f);
}
......@@ -100,15 +110,19 @@ void logger_index_read_file(struct logger_index *index, int i) {
* @param index The #logger_index.
*/
void logger_index_free_current_file(struct logger_index *index) {
/* Free offsets */
/* Free the particles offsets. */
if (index->offsets)
free(index->offsets);
index->offsets = NULL;
/* Free ids */
/* Free the particles ids. */
if (index->ids)
free(index->ids);
index->ids = NULL;
index->ids = NULL;
/* Set variables to default value. */
index->current_file = -1;
index->number_of_particles = 0;
}
/**
......@@ -118,17 +132,21 @@ void logger_index_free_current_file(struct logger_index *index) {
*/
void logger_index_free(struct logger_index *index) {
/* Free the memory allocated for current file */
/* Set variables to default value. */
index->current_file = -1;
index->number_of_particles = 0;
/* Free the memory allocated for current file. */
logger_index_free_file_data(index);
/* Free the filenames */
/* Free the filenames. */
for(int i = 0; i < index->number_files; i++) {
if (index->filenames[i])
free(index->filenames[i]);
index->filenames[i] = NULL;
}
/* Free the array of filenames */
/* Free the array of filenames. */
free(index->filenames);
index->filenames = NULL;
}
......@@ -18,6 +18,7 @@
******************************************************************************/
/**
* @file logger_index.h
* @brief This file deals with the index files.
*/
#ifndef __LOGGER_LOGGER_INDEX_H__
......@@ -28,6 +29,12 @@ struct logger_reader;
/**
* @brief This structure will contain the data related to
* the index file.
*
* It is initialized with #logger_index_init and freed with
* #logger_index_free.
*
* The files are read with #logger_index_read_file and
* can be freed with #logger_index_free_current_file.
*/
struct logger_index {
/* The reader */
......@@ -39,14 +46,20 @@ struct logger_index {
/* Number of files */
int number_files;
/* List of all the index files */
/* List of all the index filenames */
char **filenames;
/* Offsets */
/* Index of current file */
int current_file;
/* Offsets of the particles in current file */
size_t *offsets;
/* ids */
long long ids;
/* ids of the particles in current file */
long long *ids;
/* Number of particles */
size_t number_of_particles;
};
void logger_index_init(struct logger_index *index, struct logger_reader *reader,
......
......@@ -49,20 +49,21 @@ size_t io_get_file_size(int fd) {
*
*/
void *io_mmap_file(char *filename, size_t *file_size) {
/* open file */
/* open the file. */
int fd = open(filename, O_RDWR);
if (fd == -1)
error("Unable to open file %s (%s)", filename, strerror(errno));
/* get file size */
/* get the file size. */
*file_size = io_get_file_size(fd);
/* map memory */
/* map the memory. */
void *map = mmap(NULL, *file_size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
error("Failed to allocate map of size %zi bytes. (%s)", *file_size,
strerror(errno));
/* Close the file. */
close(fd);
return map;
......
......@@ -17,6 +17,7 @@
*
******************************************************************************/
/**
* @file logger_io.h
* @brief This file contains basic IO function.
*/
#ifndef __LOGGER_LOGGER_IO_H__
......
......@@ -43,7 +43,7 @@ void logger_logfile_init(
message("Mapping the log file.");
log->log.map = io_mmap_file(filename, &log->log.file_size);
/* Read header. */
/* Read the header. */
if (reader->verbose > 1)
message("Reading the header.");
header_read(&log->header, log);
......@@ -58,9 +58,7 @@ void logger_logfile_init(
error("The offsets have been corrupted");
}
/* Reverse offset direction */
if (reader->verbose > 1)
message("Checking if offsets need to be reversed.");
/* Reverse the offsets direction */
if (header_is_backward(&log->header)) {
logger_logfile_reverse_offset(log);
}
......@@ -70,6 +68,7 @@ void logger_logfile_init(
message("Reading the time stamps.");
time_array_init(&log->times, log);
/* Print the time array */
if (reader->verbose > 0) {
time_array_print(&log->times);
}
......@@ -94,37 +93,41 @@ void logger_logfile_free(struct logger_logfile *log) {
*/
void logger_logfile_reverse_offset(struct logger_logfile *log) {
/* Get pointers */
struct header *header = &log->header;
const struct logger_reader *reader = log->reader;
/* Check if the offsets need to be reversed */
if (!header_is_backward(header)) {
error("The offset are already reversed.");
error("The offsets are already reversed.");
}
#ifdef SWIFT_DEBUG_CHECKS
/* check offset */
if (reader->verbose > 0) {
message("Check offsets...");
message("Check record's headers...");
}
/* check that the record offset points to another record. */
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");
message("Record's headers are correct.");
}
#endif
/* reverse header offset */
message("WARNING: Modifying the logfile, do not kill the job!");
/* Set the offset direction to a corrupted status. */
header_change_offset_direction(header, logger_offset_corrupted);
/* reverse record */
if (reader->verbose > 0) {
message("Reversing offsets...");
}
/* reverse the record's offset. */
for(size_t offset = header->offset_first_record;
offset < log->log.file_size;
offset = tools_reverse_offset(header, log->log.map, offset)) {}
......@@ -133,21 +136,24 @@ void logger_logfile_reverse_offset(struct logger_logfile *log) {
message("Reversing done");
}
/* reverse header offset */
/* Now that the offset are effectively reversed, can set the direction to
forward. */
header_change_offset_direction(header, logger_offset_forward);
message("WARNING: Modification done, you can now safely kill the job.");
#ifdef SWIFT_DEBUG_CHECKS
/* check offset */
if (reader->verbose > 0) {
message("Check offsets...");
message("Check record's headers...");
}
/* check that the record offset points to another record. */
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");
message("Record's headers are correct.");
}
#endif
......
......@@ -17,6 +17,7 @@
*
******************************************************************************/
/**
* @file logger_logfile.h
* @brief This file contains the high level function for the log.
*/
#ifndef __LOGGER_LOGGER_LOGFILE_H__
......@@ -30,7 +31,12 @@ struct logger_reader;
/**
* @brief This structure deals with the log file.
*
* TODO
* This structure is initialized by the #logger_reader
* and deals with the log file.
* It maps it, reverse the offsets (if required) and unmap it.
*
* The structure is initialized with #logger_logfile_init and
* freed with #logger_logfile_free.
*/
struct logger_logfile {
......
......@@ -130,6 +130,7 @@ 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) {
/* Get a few pointers */
const struct header *h = &reader->log.header;
void *map = reader->log.log.map;
......@@ -140,10 +141,13 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
logger_particle_init(part);
/* Read the record mask */
offset = io_read_mask(h, map, offset, &mask, &h_offset);
/* Check if it is not a time record */
if (mask != 127) error("Unexpected mask: %lu", mask);
/* Read all the fields */
for (size_t i = 0; i < h->number_mask; i++) {
if (mask & h->masks[i].mask) {
offset = logger_particle_read_field(part, map, offset, h->masks[i].name,
......@@ -151,37 +155,42 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
}
}
/* Get the time of current record. */
if (times) {
/* move offset by 1 in order to be in the required record */
/* move offset by 1 in order to be in the required record. */
part->time = time_array_get_time(times, offset - 1);
}
else
part->time = -1;
/* end of const case */
/* Check if an interpolation is required. */
if (reader_type == logger_reader_const)
return offset;
/* read next particle */
/* Start reading next record. */
struct logger_particle part_next;
/* Check that the offset are in the correct direction. */
if (!header_is_forward(h)) {
error("Cannot read a particle with non forward offsets.");
}
/* No next particle. */
if (h_offset == 0)
return offset;
/* get absolute offset of next particle */
h_offset += offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE -
/* get absolute offset of next particle. */
h_offset += offset - header_get_record_size_from_mask(h, mask) - LOGGER_MASK_SIZE -
LOGGER_OFFSET_SIZE;
/* Get time of next record */
part_next.time = time_array_get_time(times, h_offset);
/* previous part exists */
/* Read next record. */
h_offset = logger_particle_read(&part_next, reader, h_offset, part_next.time,
logger_reader_const);
/* Interpolate the two particles. */
logger_particle_interpolate(part, &part_next, time);
return offset;
......@@ -200,10 +209,12 @@ void logger_particle_interpolate(
struct logger_particle *part_curr, const struct logger_particle *part_next,
const double time) {
/* Check that a particle is provided. */
if (!part_curr) error("part_curr is NULL");
if (!part_next) error("part_next is NULL");
#ifdef SWIFT_DEBUG_CHECKS
/* Check the particle order. */
if (part_next->time <= part_curr->time)
error("Wrong particle order (next before current)");
if ((time < part_curr->time) || (part_next->time < time))
......@@ -213,6 +224,7 @@ void logger_particle_interpolate(
part_curr->time, time, part_next->time);
#endif
/* Compute the interpolation scaling. */
double scaling = part_next->time - part_curr->time;
scaling = (time - part_curr->time) / scaling;
......
......@@ -29,10 +29,11 @@
void logger_reader_init(struct logger_reader *reader, char *filename, int verbose) {
if (verbose > 1)
message("Initializing the reader");
/* Initialize the reader variables */
reader->verbose = verbose;
/* Initialize the log */
/* Initialize the log file */
logger_logfile_init(&reader->log, filename, reader);
if (verbose > 1)
......
......@@ -49,6 +49,9 @@
*
* This structure contains all the variables required for the logger.
* It should be the only structure that the user see.
*
* It is initialized with #logger_reader_init and freed with
* #logger_reader_free.
*/
struct logger_reader {
......
......@@ -34,6 +34,7 @@
size_t time_read(integertime_t *int_time, double *time, const struct logger_reader *reader,
size_t offset) {
/* Initialize variables. */
const struct header *h = &reader->log.header;
void *map = h->log->log.map;
......@@ -55,7 +56,7 @@ size_t time_read(integertime_t *int_time, double *time, const struct logger_read
if (h->masks[ind].mask != mask) error("Not a time record");
#endif
/* read data */
/* read the record */
offset = io_read_data(map, sizeof(unsigned long long int), int_time, offset);
offset = io_read_data(map, sizeof(double), time, offset);
......@@ -70,9 +71,12 @@ size_t time_read(integertime_t *int_time, double *time, const struct logger_read
*
*/
size_t time_offset_first_record(const struct header *h) {
/* Initialize a few variables */
size_t offset = h->offset_first_record;
void *map = h->log->log.map;
/* Check that the first record is really a time record. */
int i = header_get_field_index(h, "timestamp");
if (i == -1) error("Time mask not present in the log file header");
......@@ -103,6 +107,7 @@ void time_array_init_to_zero(struct time_array *t) {
*/
void time_array_init(struct time_array *t, struct logger_logfile *log) {
/* Initialize a few variables. */
t->next = NULL;
t->prev = NULL;
......@@ -115,7 +120,7 @@ void time_array_init(struct time_array *t, struct logger_logfile *log) {
/* get first time stamp */
size_t offset = time_offset_first_record(&log->header);
while (offset < file_size) {
/* read time */
/* read current time record and store it. */
t->offset = offset;
size_t tmp_offset = offset;
tmp_offset = time_read(&int_time, &time, log->reader, tmp_offset);
......@@ -127,7 +132,7 @@ void time_array_init(struct time_array *t, struct logger_logfile *log) {
log->log.file_size);
if (test == -1) break;
/* allocate next time_array */
/* allocate next time_array. */
struct time_array *tmp = malloc(sizeof(struct time_array));
tmp->prev = t;
tmp->next = NULL;
......@@ -183,10 +188,12 @@ struct time_array *time_array_get_time_array(const struct time_array *t,
if (!t) error("NULL pointer");
#endif
const struct time_array *tmp;
/* Find the time_array with the correct offset */
for(tmp = t; tmp->next && tmp->offset <= offset; tmp = tmp->next)
{}
if (tmp->prev == NULL) return (struct time_array *) tmp;
/* If providing the offset of a time_array, need to give it back. */
if (tmp->offset == offset) return (struct time_array *) tmp;
return (struct time_array *) tmp->prev;