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

Logger: finish testReader

parent d886d653
......@@ -46,11 +46,11 @@ lib_LTLIBRARIES = liblogger.la
SUBDIRS = tests
# List required headers
include_HEADERS = logger_header.h logger_io.h logger_particle.h logger_time.h logger_tools.h logger_reader.h \
include_HEADERS = logger_header.h logger_loader_io.h logger_particle.h logger_time.h logger_tools.h logger_reader.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 \
AM_SOURCES = logger_header.c logger_loader_io.c logger_particle.c logger_time.c logger_tools.c logger_reader.c \
logger_index.c logger_logfile.c
if HAVEPYTHON
AM_SOURCES += logger_python_wrapper.c
......
......@@ -19,7 +19,7 @@
#include "logger_header.h"
#include "logger_reader.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_tools.h"
#include "logger_logfile.h"
......@@ -45,7 +45,7 @@ void header_print(const struct header *h) {
#endif
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);
message("Number masks: %i", h->number_mask);
for (size_t i = 0; i < h->number_mask; i++) {
message(" Mask: %s", h->masks[i].name);
......@@ -92,7 +92,7 @@ void header_change_offset_direction(struct header *h, enum logger_offset_directi
/* Skip file format and version numbers */
size_t offset = LOGGER_VERSION_SIZE + 2 * sizeof(int);
logger_io_write_data(h->log->log.map + offset, LOGGER_NUMBER_SIZE, &new_value);
logger_loader_io_write_data(h->log->log.map + offset, sizeof(unsigned int), &new_value);
}
/**
......@@ -109,39 +109,39 @@ void header_read(struct header *h, struct logger_logfile *log) {
/* read file format */
char file_format[STRING_SIZE];
map = logger_io_read_data(map, LOGGER_VERSION_SIZE, &file_format);
map = logger_loader_io_read_data(map, LOGGER_VERSION_SIZE, &file_format);
if (strcmp(file_format, "SWIFT_LOGGER"))
error("Wrong file format (%s)", file_format);
/* Read major version number */
map = logger_io_read_data(map, sizeof(int), &h->major_version);
map = logger_loader_io_read_data(map, sizeof(int), &h->major_version);
/* Read minor version number */
map = logger_io_read_data(map, sizeof(int), &h->minor_version);
map = logger_loader_io_read_data(map, sizeof(int), &h->minor_version);
if (h->log->reader->verbose > 0)
message("File version %i.%i", h->major_version, h->minor_version);
/* read the offset directions */
map = logger_io_read_data(map, LOGGER_NUMBER_SIZE, &h->offset_direction);
map = logger_loader_io_read_data(map, sizeof(int), &h->offset_direction);
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 record */
map = logger_io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first_record);
map = logger_loader_io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first_record);
/* read the size of the strings */
map = logger_io_read_data(map, LOGGER_NUMBER_SIZE, &h->string_length);
map = logger_loader_io_read_data(map, sizeof(unsigned int), &h->string_length);
/* check if value defined in this file is large enough. */
if (STRING_SIZE < h->string_length) {
error("Name too large in log file %li", h->string_length);
error("Name too large in log file %i", h->string_length);
}
/* read the number of masks. */
map = logger_io_read_data(map, LOGGER_NUMBER_SIZE, &h->number_mask);
map = logger_loader_io_read_data(map, sizeof(unsigned int), &h->number_mask);
/* allocate the masks memory. */
h->masks = malloc(sizeof(struct mask_data) * h->number_mask);
......@@ -149,13 +149,13 @@ void header_read(struct header *h, struct logger_logfile *log) {
/* loop over all masks. */
for (size_t i = 0; i < h->number_mask; i++) {
/* read the mask name. */
map = logger_io_read_data(map, h->string_length, h->masks[i].name);
map = logger_loader_io_read_data(map, h->string_length, h->masks[i].name);
/* set the mask value. */
h->masks[i].mask = 1 << i;
/* read the mask data size. */
map = logger_io_read_data(map, LOGGER_NUMBER_SIZE, &h->masks[i].size);
map = logger_loader_io_read_data(map, sizeof(unsigned int), &h->masks[i].size);
}
/* Check the logfile header's size */
......
......@@ -25,7 +25,6 @@
#include <stdlib.h>
#define LOGGER_VERSION_SIZE 20
#define LOGGER_NUMBER_SIZE 4
#define LOGGER_OFFSET_SIZE 7
#define LOGGER_MASK_SIZE 1
......@@ -67,10 +66,10 @@ struct header {
size_t offset_first_record;
/* Number of bytes for strings. */
size_t string_length;
unsigned int string_length;
/* Number of masks. */
size_t number_mask;
unsigned int number_mask;
/* List of masks. */
struct mask_data *masks;
......
......@@ -26,7 +26,7 @@
/* This object's header. */
#include "logger_index.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_tools.h"
/**
......@@ -40,18 +40,19 @@ void logger_index_init(struct logger_index *index, struct logger_reader *reader,
char *filename) {
/* Open file. */
index->data = logger_io_mmap_file(filename, &index->file_size);
index->data = logger_loader_io_mmap_file(filename, &index->file_size, /* read_only */ 1);
/* /\* Read the double time. *\/ */
/* size_t offset = 0; */
/* offset = logger_io_read_data(index->data, sizeof(double), &index->time, offset); */
/* Read the double time. */
size_t offset = 0;
void *map = index->data + offset;
map = logger_loader_io_read_data(map, sizeof(double), &index->time);
/* /\* Read the integer time. *\/ */
/* offset = logger_io_read_data(index->data, sizeof(integertime_t), &index->int_time, offset); */
/* Read the integer time. */
map = logger_loader_io_read_data(map, sizeof(integertime_t), &index->int_time);
/* /\* Read the number of particles. *\/ */
/* offset = logger_io_read_data(index->data, swift_type_count * sizeof(long long), */
/* &index->number_particles, offset); */
/* Read the number of particles. */
map = logger_loader_io_read_data(map, swift_type_count * sizeof(long long),
&index->number_particles);
/* Count total number of particles. */
long long N = 0;
......@@ -70,7 +71,7 @@ void logger_index_init(struct logger_index *index, struct logger_reader *reader,
void logger_index_free(struct logger_index *index) {
/* unmap file */
logger_io_munmap_file(index->data, index->file_size);
logger_loader_io_munmap_file(index->data, index->file_size);
/* Set variables to default value. */
index->total_number_particles = 0;
......
......@@ -22,7 +22,7 @@
#include <unistd.h>
#include "logger_header.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_tools.h"
/**
......@@ -32,7 +32,7 @@
*
* @return file size.
*/
size_t logger_io_get_file_size(int fd) {
size_t logger_loader_io_get_file_size(int fd) {
struct stat s;
int status = fstat(fd, &s);
if (status != 0) error("Unable to get file size (%s)", strerror(errno));
......@@ -42,23 +42,34 @@ size_t logger_io_get_file_size(int fd) {
/**
* @brief Map a file.
*
* #logger_io_munmap_file should be called to unmap the file.
* #logger_loader_io_munmap_file should be called to unmap the file.
*
* @param filename file to read.
* @param file_size (out) size of the file.
* @param read_only Open the file in read only mode?
*
*/
void *logger_io_mmap_file(char *filename, size_t *file_size) {
void *logger_loader_io_mmap_file(char *filename, size_t *file_size, int read_only) {
/* open the file. */
int fd = open(filename, O_RDWR);
int fd;
if (read_only)
fd = open(filename, O_RDONLY);
else
fd = open(filename, O_RDWR);
if (fd == -1)
error("Unable to open file %s (%s)", filename, strerror(errno));
/* get the file size. */
*file_size = logger_io_get_file_size(fd);
*file_size = logger_loader_io_get_file_size(fd);
/* map the memory. */
void *map = mmap(NULL, *file_size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
int mode = PROT_READ;
if (!read_only)
mode |= PROT_WRITE;
void *map = mmap(NULL, *file_size, mode, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
error("Failed to allocate map of size %zi bytes. (%s)", *file_size,
strerror(errno));
......@@ -76,7 +87,7 @@ void *logger_io_mmap_file(char *filename, size_t *file_size) {
* @param file_size The file size.
*
*/
void logger_io_munmap_file(void *map, size_t file_size) {
void logger_loader_io_munmap_file(void *map, size_t file_size) {
/* unmap */
if (munmap(map, file_size) != 0) {
error("Unable to unmap the file (%s)", strerror(errno));
......
......@@ -17,11 +17,11 @@
*
******************************************************************************/
/**
* @file logger_io.h
* @file logger_loader_io.h
* @brief This file contains basic IO function.
*/
#ifndef __LOGGER_LOGGER_IO_H__
#define __LOGGER_LOGGER_IO_H__
#ifndef __LOGGER_LOGGER_LOADER_IO_H__
#define __LOGGER_LOGGER_LOADER_IO_H__
#include "logger_header.h"
#include "logger_tools.h"
......@@ -29,9 +29,9 @@
#include <stdio.h>
#include <stdlib.h>
size_t logger_io_get_file_size(int fd);
void *logger_io_mmap_file(char *filename, size_t *file_size);
void logger_io_munmap_file(void *map, size_t file_size);
size_t logger_loader_io_get_file_size(int fd);
void *logger_loader_io_mmap_file(char *filename, size_t *file_size, int read_only);
void logger_loader_io_munmap_file(void *map, size_t file_size);
/**
* @brief read a mask with its offset.
......@@ -43,7 +43,7 @@ void logger_io_munmap_file(void *map, size_t file_size);
*
* @return memory after the record header.
*/
__attribute__((always_inline)) INLINE static void* logger_io_read_mask(
__attribute__((always_inline)) INLINE static void* logger_loader_io_read_mask(
const struct header *h, void *data, size_t *mask,
size_t *diff_offset) {
/* read mask */
......@@ -72,7 +72,7 @@ __attribute__((always_inline)) INLINE static void* logger_io_read_mask(
* @return memory after the data written.
*/
__attribute__((always_inline)) INLINE static void* logger_io_read_data(
__attribute__((always_inline)) INLINE static void* logger_loader_io_read_data(
void *data, const size_t size, void *p) {
memcpy(p, data, size);
return data + size;
......@@ -87,11 +87,11 @@ __attribute__((always_inline)) INLINE static void* logger_io_read_data(
*
* @return memory after the data written.
*/
__attribute__((always_inline)) INLINE static void* logger_io_write_data(
__attribute__((always_inline)) INLINE static void* logger_loader_io_write_data(
void *data, const size_t size, const void *p) {
memcpy(data, p, size);
return data + size;
};
#endif // __LOGGER_LOGGER_IO_H__
#endif // __LOGGER_LOGGER_LOADER_IO_H__
......@@ -18,7 +18,7 @@
******************************************************************************/
#include "logger_logfile.h"
#include "logger_reader.h"
#include "logger_io.h"
#include "logger_loader_io.h"
/**
* @brief Initialize the #logger_logfile.
......@@ -42,7 +42,8 @@ void logger_logfile_init(
/* Open file, map it and get its size. */
if (reader->verbose > 1)
message("Mapping the log file.");
log->log.map = logger_io_mmap_file(filename, &log->log.file_size);
log->log.map = logger_loader_io_mmap_file(
filename, &log->log.file_size, /* read_only */ 1);
/* Read the header. */
if (reader->verbose > 1)
......@@ -66,7 +67,7 @@ void logger_logfile_init(
/* Reverse the offsets direction */
if (header_is_backward(&log->header)) {
logger_logfile_reverse_offset(log);
logger_logfile_reverse_offset(log, filename);
}
/* Initialize the time array */
......@@ -87,7 +88,7 @@ void logger_logfile_init(
* @param log The #logger_logfile.
*/
void logger_logfile_free(struct logger_logfile *log) {
logger_io_munmap_file(log->log.map, log->log.file_size);
logger_loader_io_munmap_file(log->log.map, log->log.file_size);
}
......@@ -96,8 +97,14 @@ void logger_logfile_free(struct logger_logfile *log) {
* @brief Reverse offset in log file
*
* @param log The #logger_logfile
* @param filename The log's filename.
*/
void logger_logfile_reverse_offset(struct logger_logfile *log) {
void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename) {
/* Close and reopen the file in write mode */
logger_loader_io_munmap_file(log->log.map, log->log.file_size);
log->log.map = logger_loader_io_mmap_file(
filename, &log->log.file_size, /* read_only */ 0);
/* Get pointers */
struct header *header = &log->header;
......@@ -163,4 +170,8 @@ void logger_logfile_reverse_offset(struct logger_logfile *log) {
}
#endif
/* Close and reopen the file in read mode */
logger_loader_io_munmap_file(log->log.map, log->log.file_size);
log->log.map = logger_loader_io_mmap_file(
filename, &log->log.file_size, /* read_only */ 1);
}
......@@ -64,7 +64,7 @@ struct logger_logfile {
void logger_logfile_init(struct logger_logfile *log, char *filename, struct logger_reader *reader,
int only_header);
void logger_logfile_reverse_offset(struct logger_logfile *log);
void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename);
void logger_logfile_free(struct logger_logfile *log);
#endif // __LOGGER_LOGGER_LOGFILE_H__
......@@ -18,7 +18,7 @@
******************************************************************************/
#include "logger_particle.h"
#include "logger_header.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_reader.h"
#include "logger_time.h"
#include "logger_tools.h"
......@@ -97,7 +97,7 @@ void* logger_particle_read_field(struct logger_particle *part, void *map,
}
/* read the data */
map = logger_io_read_data(map, size, p);
map = logger_loader_io_read_data(map, size, p);
/* Split the required fields */
if (strcmp("consts", field) == 0) {
......@@ -140,7 +140,7 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
logger_particle_init(part);
/* Read the record mask */
map = logger_io_read_mask(h, map + offset, &mask, &h_offset);
map = logger_loader_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);
......
......@@ -91,8 +91,8 @@ enum logger_reader_type {
void logger_particle_print(const struct logger_particle *p);
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);
size_t offset, const double time,
const enum logger_reader_type reader_type);
void logger_particle_init(struct logger_particle *part);
......
......@@ -17,7 +17,7 @@
*
******************************************************************************/
#include "logger_header.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_particle.h"
#include "logger_reader.h"
#include "logger_time.h"
......
......@@ -17,7 +17,7 @@
*
******************************************************************************/
#include "logger_time.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_logfile.h"
#include "logger_reader.h"
......@@ -29,10 +29,9 @@
* @param reader The #logger_reader.
* @param offset position in the file.
*
* @return position after the time record.
*/
size_t time_read(integertime_t *int_time, double *time, const struct logger_reader *reader,
size_t offset) {
size_t offset) {
/* Initialize variables. */
const struct header *h = &reader->log.header;
......@@ -44,7 +43,7 @@ size_t time_read(integertime_t *int_time, double *time, const struct logger_read
*time = 0;
/* read record header */
map = logger_io_read_mask(h, map + offset, &mask, &prev_offset);
map = logger_loader_io_read_mask(h, map + offset, &mask, &prev_offset);
#ifdef SWIFT_DEBUG_CHECKS
......@@ -57,10 +56,11 @@ size_t time_read(integertime_t *int_time, double *time, const struct logger_read
#endif
/* read the record */
map = logger_io_read_data(map, sizeof(unsigned long long int), int_time);
map = logger_io_read_data(map, sizeof(double), time);
map = logger_loader_io_read_data(map, sizeof(unsigned long long int), int_time);
map = logger_loader_io_read_data(map, sizeof(double), time);
return map - h->log->log.map;
return (size_t) (map - h->log->log.map);
}
/**
......@@ -82,7 +82,7 @@ size_t time_offset_first_record(const struct header *h) {
if (i == -1) error("Time mask not present in the log file header");
size_t mask = 0;
logger_io_read_mask(h, map + offset, &mask, NULL);
logger_loader_io_read_mask(h, map + offset, &mask, NULL);
if (mask != h->masks[i].mask) error("Log file should begin by timestep");
......@@ -123,7 +123,7 @@ void time_array_init(struct time_array *t, struct logger_logfile *log) {
/* 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);
time_read(&int_time, &time, log->reader, tmp_offset);
t->int_time = int_time;
t->time = time;
......
......@@ -18,7 +18,7 @@
******************************************************************************/
#include "logger_tools.h"
#include "logger_header.h"
#include "logger_io.h"
#include "logger_loader_io.h"
#include "logger_reader.h"
#include "logger_particle.h"
......@@ -59,7 +59,7 @@ int _tools_get_next_record_forward(const struct header *h, void *map,
size_t diff_offset = 0;
/* read offset */
map = logger_io_read_mask(h, map + *offset, NULL, &diff_offset);
map = logger_loader_io_read_mask(h, map + *offset, NULL, &diff_offset);
if (diff_offset == 0) return -1;
......@@ -90,7 +90,7 @@ int _tools_get_next_record_backward(const struct header *h, void *map,
while (current_offset < file_size) {
size_t mask = 0;
size_t prev_offset;
logger_io_read_mask(h, map + current_offset, &mask, &prev_offset);
logger_loader_io_read_mask(h, map + current_offset, &mask, &prev_offset);
prev_offset = current_offset - prev_offset - record_header;
if (*offset == prev_offset) {
......@@ -121,12 +121,12 @@ size_t tools_reverse_offset(const struct header *h, void *file_map, size_t offse
void *map = file_map;
/* read mask + offset */
map = logger_io_read_mask(h, map + offset, &mask, &prev_offset);
map = logger_loader_io_read_mask(h, map + offset, &mask, &prev_offset);
/* write offset of zero (in case it is the last record) */
const size_t zero = 0;
map -= LOGGER_OFFSET_SIZE;
map = logger_io_write_data(map, LOGGER_OFFSET_SIZE, &zero);
map = logger_loader_io_write_data(map, LOGGER_OFFSET_SIZE, &zero);
/* set offset after current record */
map += header_get_record_size_from_mask(h, mask);
......@@ -142,12 +142,12 @@ size_t tools_reverse_offset(const struct header *h, void *file_map, size_t offse
/* modify previous offset */
map = file_map + cur_offset - prev_offset + LOGGER_MASK_SIZE;
map = logger_io_write_data(map, LOGGER_OFFSET_SIZE, &prev_offset);
map = logger_loader_io_write_data(map, LOGGER_OFFSET_SIZE, &prev_offset);
#ifdef SWIFT_DEBUG_CHECKS
size_t prev_mask = 0;
map -= LOGGER_MASK_SIZE + LOGGER_OFFSET_SIZE;
logger_io_read_mask(h, map, &prev_mask, NULL);
logger_loader_io_read_mask(h, map, &prev_mask, NULL);
if (prev_mask != mask)
error("Unexpected mask: %lu, got %lu", mask, prev_mask);
......@@ -181,7 +181,7 @@ size_t tools_check_record_consistency(const struct logger_reader *reader, size_t
size_t pointed_offset;
/* read mask + offset */
map = logger_io_read_mask(h, map, &mask, &pointed_offset);
map = logger_loader_io_read_mask(h, map, &mask, &pointed_offset);
/* get absolute offset */
if (header_is_forward(h))
......@@ -204,7 +204,7 @@ size_t tools_check_record_consistency(const struct logger_reader *reader, size_t
/* read mask of the pointed record */
size_t pointed_mask = 0;
logger_io_read_mask(h, file_init + pointed_offset, &pointed_mask, NULL);
logger_loader_io_read_mask(h, file_init + pointed_offset, &pointed_mask, NULL);
/* check masks */
if (pointed_mask != mask)
......
......@@ -20,7 +20,7 @@ AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/logger $(HDF5_CPPFLAGS) $(GSL_IN
AM_LDFLAGS = ../../src/.libs/libswiftsim.a ../.libs/liblogger.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(FFTW_LIBS) $(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) $(GSL_LIBS) $(PROFILER_LIBS)
# List of programs and scripts to run in the test suite
TESTS = testHeader testReader
TESTS = testHeader testReader # testIndex
# List of test programs to compile
check_PROGRAMS = testHeader testReader
......@@ -31,6 +31,7 @@ $(check_PROGRAMS): ../../src/.libs/libswiftsim.a
# Sources for the individual programs
testHeader_SOURCES = testHeader.c
testReader_SOURCES = testReader.c
# testIndex_SOURCES = testIndex.c
# Files necessary for distribution
EXTRA_DIST = testHeader.yml testReader.yml
EXTRA_DIST = testHeader.yml testReader.yml testIndex.yml
......@@ -19,6 +19,8 @@
#include "swift.h"
#include "logger_header.h"
#include "logger_loader_io.h"
#include "logger_particle.h"
#include "logger_reader.h"
#define number_parts 100
......@@ -53,6 +55,25 @@ void init_particles(struct part *p, struct xpart *xp) {
}
}
/**
* @brief Provides a time given the step number.
*
* @param step The required step.
*/
integertime_t get_integer_time(int step) {
return step;
}
/**
* @brief Provides a time given the step number.
*
* @param step The required step.
*/
double get_double_time(int step) {
const double time_base = 1e-4;
return step * time_base;
}
/**
* @brief Write a few particles during multiple time steps.
*
......@@ -64,14 +85,6 @@ void write_particles(struct logger *log) {
const int number_steps = 100;
integertime_t ti_int = 0;
double ti_double = 0.;
const double time_base = 1e-4;
/* Create unit system */
struct unit_system us;
units_init_cgs(&us);
/* Create particles and initialize them. */
struct part *parts;
if ((parts = (struct part *)malloc(sizeof(struct part) * number_parts)) == NULL)
......@@ -89,6 +102,8 @@ void write_particles(struct logger *log) {
/* Do step */
for(int i = 0; i < number_steps; i++) {
integertime_t ti_int = get_integer_time(i);
double ti_double = get_double_time(i);
/* Mark the current time step in the particle logger file. */
logger_log_timestamp(log, ti_int, ti_double,
......@@ -113,17 +128,118 @@ void write_particles(struct logger *log) {
&xparts[j].logger_data.last_offset);
}