diff --git a/logger/logger_header.c b/logger/logger_header.c index 47682d940b151a376819cad3deea9e2b38c3b27c..364e99f10ddf2f3d1518df5aa17b782321e01c15 100644 --- a/logger/logger_header.c +++ b/logger/logger_header.c @@ -9,6 +9,8 @@ /** * @brief print a header struct + * + * @param h The #header */ void header_print(const struct header *h) { #ifdef SWIFT_DEBUG_CHECKS @@ -30,12 +32,12 @@ void header_print(const struct header *h) { printf("\tSize: %lu\n", h->masks_size[i]); printf("\n"); } - - /* mask contains... TODO */ }; /** * @brief free allocated memory + * + * @param h The #header */ void header_free(struct header *h) { for (size_t i = 0; i < h->nber_mask; i++) { @@ -49,11 +51,12 @@ void header_free(struct header *h) { /** * @brief check if field is present in header * + * @param h The #header * @param field name of the requested field * @param ind (return value) indice of the requested field */ -int header_is_present_and_get_index(const struct header *h, const char *field, - size_t *ind) { +int header_field_is_present(const struct header *h, const char *field, + size_t *ind) { for (size_t i = 0; i < h->nber_mask; i++) { if (strcmp(h->masks_name[i], field) == 0) { if (ind != NULL) { @@ -66,28 +69,18 @@ int header_is_present_and_get_index(const struct header *h, const char *field, return 0; }; -/** - * @brief check if field is present in header - * - * @param field name of the requested field - */ -int header_is_present(const struct header *h, const char *field) { - return header_is_present_and_get_index(h, field, NULL); -}; - /** * @brief Inverse the offset direction * * @param h #header file structure * @param map file mapping * - * @return error code */ -int header_change_offset_direction(struct header *h, void *map) { +void header_change_offset_direction(struct header *h, void *map) { h->forward_offset = !h->forward_offset; size_t offset = LOGGER_VERSION_SIZE; - return io_write_data(map, LOGGER_NBER_SIZE, &h->forward_offset, &offset); + io_write_data(map, LOGGER_NBER_SIZE, &h->forward_offset, &offset); } /** @@ -96,7 +89,7 @@ int header_change_offset_direction(struct header *h, void *map) { * @param h out: header * @param map file mapping */ -int header_read(struct header *h, void *map) { +void header_read(struct header *h, void *map) { size_t offset = 0; /* read version */ @@ -107,7 +100,7 @@ int header_read(struct header *h, void *map) { io_read_data(map, LOGGER_NBER_SIZE, &h->forward_offset, &offset); if (h->forward_offset != 0 && h->forward_offset != 1) - error(EIO, "Non boolean value for the offset direction (%i)", + error("Non boolean value for the offset direction (%i)", h->forward_offset); /* read offset to first data */ @@ -120,7 +113,7 @@ int header_read(struct header *h, void *map) { /* check if value defined in this file is large enough */ if (STRING_SIZE < h->name) { - error(EOVERFLOW, "Name too large in dump file"); + error("Name too large in dump file"); } /* read number of masks */ @@ -150,11 +143,10 @@ int header_read(struct header *h, void *map) { #ifdef SWIFT_DEBUG_CHECKS header_print(h); #endif - error(EIO, "Wrong header size (in header %li, current %li)", + error("Wrong header size (in header %li, current %li)", h->offset_first, offset); } - return 0; }; /** diff --git a/logger/logger_header.h b/logger/logger_header.h index 80095af82f81bbd8444cfd73e5489abd7f737871..0d752569ee52e99e8d9065826cf56f10b481ba0f 100644 --- a/logger/logger_header.h +++ b/logger/logger_header.h @@ -39,11 +39,10 @@ struct header { void header_print(const struct header *h); void header_free(struct header *h); -int header_is_present_and_get_index(const struct header *h, const char *field, - size_t *ind); -int header_is_present(const struct header *h, const char *field); -int header_read(struct header *h, void *map); +int header_field_is_present(const struct header *h, const char *field, + size_t *ind); +void header_read(struct header *h, void *map); size_t header_get_mask_size(const struct header *h, const size_t mask); -int header_change_offset_direction(struct header *h, void *map); +void header_change_offset_direction(struct header *h, void *map); #endif // __LOGGER_HEADER_H__ diff --git a/logger/logger_io.c b/logger/logger_io.c index b6e12da855d5d6d3d323c4eabd744ddf71ce28c3..89d8a8a91d796691059428a7376664e47a5837a3 100644 --- a/logger/logger_io.c +++ b/logger/logger_io.c @@ -13,15 +13,12 @@ * @param fd file id * @param size out: file size * - * @return error code */ -int io_get_file_size(int fd, size_t *size) { +void io_get_file_size(int fd, size_t *size) { struct stat s; int status = fstat(fd, &s); - if (status != 0) error(errno, "Unable to get file size"); + if (status != 0) error("Unable to get file size (%s)", strerror(errno)); *size = s.st_size; - - return 0; } /** @@ -31,24 +28,20 @@ int io_get_file_size(int fd, size_t *size) { * @param fd out: file id * @param map out: file mapping * - * @return error code */ -int io_open_file(char *filename, int *fd, void **map) { +void io_open_file(char *filename, int *fd, void **map) { /* open file */ *fd = open(filename, O_RDWR); - if (*fd == -1) error(errno, "Unable to open file %s", filename); + if (*fd == -1) error("Unable to open file %s (%s)", filename, strerror(errno)); /* get file size */ size_t size = 0; - int status = io_get_file_size(*fd, &size); - if (status != 0) return status; + io_get_file_size(*fd, &size); /* map memory */ *map = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, *fd, 0); if (map == MAP_FAILED) - error(errno, "Failed to allocate map of size %zi bytes.", size); - - return 0; + error("Failed to allocate map of size %zi bytes. (%s)", size, strerror(errno)); } /** @@ -57,20 +50,18 @@ int io_open_file(char *filename, int *fd, void **map) { * @param fd file id * @param map file mapping * - * @return error code */ -int io_close_file(int *fd, void **map) { +void io_close_file(int *fd, void **map) { /* get file size */ size_t size = 0; - int status = io_get_file_size(*fd, &size); - if (status != 0) return status; + io_get_file_size(*fd, &size); /* unmap */ - if (munmap(*map, size) != 0) error(errno, "Unable to unmap the file"); + if (munmap(*map, size) != 0) { + error("Unable to unmap the file (%s)", strerror(errno)); + } close(*fd); - - return 0; } /** @@ -83,10 +74,9 @@ int io_close_file(int *fd, void **map) { * @param mask mask read * @param diff_offset offset difference to previous/next corresponding chunk * - * @return error code */ -int io_read_mask(const struct header *h, void *map, size_t *offset, - size_t *mask, size_t *diff_offset) { +void io_read_mask(const struct header *h, void *map, size_t *offset, + size_t *mask, size_t *diff_offset) { /* read mask */ if (mask) { *mask = 0; @@ -100,8 +90,6 @@ int io_read_mask(const struct header *h, void *map, size_t *offset, memcpy(diff_offset, map + *offset, LOGGER_OFFSET_SIZE); } *offset += LOGGER_OFFSET_SIZE; - - return 0; } /** @@ -111,14 +99,10 @@ int io_read_mask(const struct header *h, void *map, size_t *offset, * @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 - * - * @return error code */ -int io_read_data(void *map, const size_t size, void *p, size_t *offset) { +void io_read_data(void *map, const size_t size, void *p, size_t *offset) { memcpy(p, map + *offset, size); *offset += size; - - return 0; }; /** @@ -129,11 +113,8 @@ int io_read_data(void *map, const size_t size, void *p, size_t *offset) { * @param p pointer to the data * @param offset In: position to write, Out: shifted by size * - * @return error code */ -int io_write_data(void *map, const size_t size, const void *p, size_t *offset) { +void io_write_data(void *map, const size_t size, const void *p, size_t *offset) { memcpy(map + *offset, p, size); *offset += size; - - return 0; }; diff --git a/logger/logger_io.h b/logger/logger_io.h index 0b4cba242cb0803d07b7f4d55367a31639ea56cb..a5f87668147db04730674e848c50c5a1c64be4e7 100644 --- a/logger/logger_io.h +++ b/logger/logger_io.h @@ -7,12 +7,12 @@ #include <stdio.h> #include <stdlib.h> -int io_get_file_size(int fd, size_t *size); -int io_open_file(char *filename, int *fd, void **map); -int io_close_file(int *fd, void **map); -int io_read_data(void *map, const size_t size, void *p, size_t *offset); -int io_write_data(void *map, const size_t size, const void *p, size_t *offset); -int io_read_mask(const struct header *h, void *map, size_t *offset, +void io_get_file_size(int fd, size_t *size); +void io_open_file(char *filename, int *fd, void **map); +void io_close_file(int *fd, void **map); +void io_read_data(void *map, const size_t size, void *p, size_t *offset); +void io_write_data(void *map, const size_t size, const void *p, size_t *offset); +void io_read_mask(const struct header *h, void *map, size_t *offset, size_t *mask, size_t *diff_offset); #endif // __SWIFT_LOGGER_IO_H__ diff --git a/logger/logger_particle.c b/logger/logger_particle.c index 4a4191ab61b59e06900480cf3c17fa61415c439e..05cf2b289d0629671256308292d5f0c86232f2a6 100644 --- a/logger/logger_particle.c +++ b/logger/logger_particle.c @@ -25,18 +25,6 @@ void particle_print(const struct particle *p) { printf("Density: %g\n", p->density); } -/** - * @brief Check if dump data type are compatible with the particle type - * - * @param h #header structure of the file - * - * @return error code - */ -int particle_check_data_type(__attribute__((unused)) const struct header *h) { - printf("TODO check_data_type\n"); - return 1; -} - /** * @brief initialize a particle * @@ -66,10 +54,9 @@ void particle_init(struct particle *part) { * @param field field to read * @param size number of bits to read * - * @return error code */ -int particle_read_field(struct particle *part, void *map, size_t *offset, - const char *field, const size_t size) { +void particle_read_field(struct particle *part, void *map, size_t *offset, + const char *field, const size_t size) { void *p = NULL; if (strcmp("positions", field) == 0) { @@ -87,12 +74,10 @@ int particle_read_field(struct particle *part, void *map, size_t *offset, } else if (strcmp("consts", field) == 0) { p = malloc(size); } else { - error(ENOTSUP, "Type %s not defined", field); + error("Type %s not defined", field); } - int error_code = io_read_data(map, size, p, offset); - - if (error_code != 0) return error_code; + io_read_data(map, size, p, offset); if (strcmp("consts", field) == 0) { part->mass = 0; @@ -103,7 +88,6 @@ int particle_read_field(struct particle *part, void *map, size_t *offset, p -= sizeof(float); free(p); } - return error_code; } /** @@ -117,27 +101,23 @@ int particle_read_field(struct particle *part, void *map, size_t *offset, * @param reader #reader_type * @param times #time_array times in the dump * - * @return error code */ -int particle_read(struct particle *part, const struct header *h, void *map, - size_t *offset, const double time, const int reader, - struct time_array *times) { - int error_code = 0; +void particle_read(struct particle *part, const struct header *h, void *map, + size_t *offset, const double time, const int reader, + struct time_array *times) { size_t mask = 0; size_t h_offset = 0; particle_init(part); - error_code = io_read_mask(h, map, offset, &mask, &h_offset); - if (error_code != 0) return error_code; - - if (mask != 127) error(EIO, "Unexpected mask: %lu", mask); + io_read_mask(h, map, offset, &mask, &h_offset); + + if (mask != 127) error("Unexpected mask: %lu", mask); for (size_t i = 0; i < h->nber_mask; i++) { if (mask & h->masks[i]) { - error_code = particle_read_field(part, map, offset, h->masks_name[i], + particle_read_field(part, map, offset, h->masks_name[i], h->masks_size[i]); - if (error_code != 0) return error_code; } } @@ -147,14 +127,14 @@ int particle_read(struct particle *part, const struct header *h, void *map, part->time = -1; /* end of const case */ - if (reader == reader_const) return 0; + if (reader == reader_const) return; /* read next particle */ struct particle part_next; - if (!h->forward_offset) error(ENOSYS, "TODO"); + if (!h->forward_offset) error("TODO"); - if (h_offset == 0) return 0; + if (h_offset == 0) return; /* get absolute offset of next particle */ h_offset += *offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE; @@ -162,14 +142,10 @@ int particle_read(struct particle *part, const struct header *h, void *map, part_next.time = time_array_get_time(times, h_offset); /* previous part exists */ - error_code = particle_read(&part_next, h, map, &h_offset, part_next.time, - reader_const, times); - if (error_code != 0) return error_code; + particle_read(&part_next, h, map, &h_offset, part_next.time, + reader_const, times); - error_code = particle_interpolate(part, &part_next, time); - if (error_code != 0) return error_code; - - return 0; + particle_interpolate(part, &part_next, time); } /** @@ -180,20 +156,18 @@ int particle_read(struct particle *part, const struct header *h, void *map, * @param part_next #particle next particle (after time) * @param time interpolation time * - * @return error code */ -int particle_interpolate(struct particle *part_curr, - const struct particle *part_next, const double time) { +void particle_interpolate(struct particle *part_curr, + const struct particle *part_next, const double time) { - if (!part_curr) error(EFAULT, "part_curr is NULL"); - if (!part_next) error(EFAULT, "part_next is NULL"); + if (!part_curr) error("part_curr is NULL"); + if (!part_next) error("part_next is NULL"); #ifdef SWIFT_DEBUG_CHECKS if (part_next->time <= part_curr->time) - error(EIO, "Wrong particle order (next before current)"); + error("Wrong particle order (next before current)"); if ((time < part_curr->time) || (part_next->time < time)) - error(EIO, - "Interpolating, not extrapolating (particle time: %f, " + error("Interpolating, not extrapolating (particle time: %f, " "interpolating time: %f, next particle time: %f)", part_curr->time, time, part_next->time); #endif @@ -223,6 +197,4 @@ int particle_interpolate(struct particle *part_curr, /* set time */ part_curr->time = time; - - return 0; } diff --git a/logger/logger_particle.h b/logger/logger_particle.h index 5eb6648684948019a406eeb0cc4c397ffb887ba9..ff2ec866976189020cb896ea24ad25f36a7d9617 100644 --- a/logger/logger_particle.h +++ b/logger/logger_particle.h @@ -46,17 +46,16 @@ enum reader_type { void particle_print(const struct particle *p); -int particle_read(struct particle *part, const struct header *h, void *map, +void particle_read(struct particle *part, const struct header *h, void *map, size_t *offset, const double time, const int reader, struct time_array *times); -int particle_check_data_type(const struct header *h); void particle_init(struct particle *part); -int particle_read_field(struct particle *part, void *map, size_t *offset, +void particle_read_field(struct particle *part, void *map, size_t *offset, const char *field, const size_t size); -int particle_interpolate(struct particle *part_curr, +void particle_interpolate(struct particle *part_curr, const struct particle *part_next, const double time); #endif //__PARTICLE_H__ diff --git a/logger/logger_python_wrapper.c b/logger/logger_python_wrapper.c index d4eba9b25969784ad0c6b2d8f529726f86697aae..3a28e774d5271b03b39e8935c4bebd4a811ac67b 100644 --- a/logger/logger_python_wrapper.c +++ b/logger/logger_python_wrapper.c @@ -44,44 +44,41 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self, return NULL; if (!PyArray_Check(offset)) { - error_no_return(ENOTSUP, "Offset is not a numpy array"); - return NULL; + error("Offset is not a numpy array"); } if (PyArray_NDIM(offset) != 1) { - error_no_return(ENOTSUP, "Offset is not a 1 dimensional array"); - return NULL; + error("Offset is not a 1 dimensional array"); } if (PyArray_TYPE(offset) != NPY_UINT64) { - error_no_return(ENOTSUP, "Offset does not contain unsigned int"); - return NULL; + error("Offset does not contain unsigned int"); } /* open file */ int fd; void *map; - if (io_open_file(filename, &fd, &map) != 0) return NULL; + io_open_file(filename, &fd, &map); /* read header */ - if (header_read(&h, map) != 0) return NULL; + header_read(&h, map); /* reverse offset if needed */ if (!h.forward_offset) { - if (io_close_file(&fd, &map) != 0) return NULL; + io_close_file(&fd, &map); - if (reverse_offset(filename) != 0) return NULL; + reverse_offset(filename); - if (io_open_file(filename, &fd, &map) != 0) return NULL; + io_open_file(filename, &fd, &map); /* Reset header */ header_free(&h); - if (header_read(&h, map) != 0) return NULL; + header_read(&h, map); } /* read timestamps */ struct time_array times; - if (time_array_init(×, &h, map, fd) != 0) return NULL; + time_array_init(×, &h, map, fd); time_array_print(×); /* get required time */ @@ -93,56 +90,47 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self, dim[1] = DIM; /* init output */ - if (header_is_present(&h, "positions")) { + if (header_field_is_present(&h, "positions", /* ind */ NULL)) { pos = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_DOUBLE); } - if (header_is_present(&h, "velocities")) { + if (header_field_is_present(&h, "velocities", /* ind */ NULL)) { vel = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_FLOAT); } - if (header_is_present(&h, "accelerations")) { + if (header_field_is_present(&h, "accelerations", /* ind */ NULL)) { acc = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_FLOAT); } - if (header_is_present(&h, "entropy")) { + if (header_field_is_present(&h, "entropy", /* ind */ NULL)) { entropy = (PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT); } - if (header_is_present(&h, "smoothing length")) { + if (header_field_is_present(&h, "smoothing length", /* ind */ NULL)) { h_sph = (PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT); } - if (header_is_present(&h, "density")) { + if (header_field_is_present(&h, "density", /* ind */ NULL)) { rho = (PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT); } - if (header_is_present(&h, "consts")) { + if (header_field_is_present(&h, "consts", /* ind */ NULL)) { mass = (PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT); id = (PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_ULONG); } - int error_code = 0; - - /* check data type in particles */ - if (!particle_check_data_type(&h)) { - error_no_return(ENOTSUP, "Particle data type are not compatible"); - return NULL; - } - /* loop over all particles */ for (npy_intp i = 0; i < PyArray_DIMS(offset)[0]; i++) { struct particle part; size_t *offset_particle = (size_t *)PyArray_GETPTR1(offset, i); - error_code = particle_read(&part, &h, map, offset_particle, time, - reader_lin, ×); - if (error_code != 0) return NULL; + particle_read(&part, &h, map, offset_particle, time, + reader_lin, ×); double *dtmp; float *ftmp; @@ -253,8 +241,8 @@ static PyObject *pyReverseOffset(__attribute__((unused)) PyObject *self, if (!PyArg_ParseTuple(args, "s", &filename)) return NULL; - if (reverse_offset(filename) != 0) return NULL; - + reverse_offset(filename); + return Py_BuildValue(""); } diff --git a/logger/logger_reader.c b/logger/logger_reader.c new file mode 100644 index 0000000000000000000000000000000000000000..609798c4bc08a4b49cd69ff3233c15b41c829fab --- /dev/null +++ b/logger/logger_reader.c @@ -0,0 +1,70 @@ +#include "logger_header.h" +#include "logger_io.h" + +/** + * @brief Reverse offset in dump file + * + * @param filename string filename of the dump file + */ +void reverse_offset(char *filename) { + struct header h; + + /* open file */ + int fd; + void *map; + io_open_file(filename, &fd, &map); + + /* read header */ + header_read(&h, map); + + header_print(&h); + + /* check offset direction */ + if (h.forward_offset) { + error("Offset are already reversed"); + } + + /* compute file size */ + size_t sz; + io_get_file_size(fd, &sz); + + size_t offset; + +#ifdef SWIFT_DEBUG_CHECKS + /* check offset */ + printf("Check offsets...\n"); + offset = h.offset_first; + while (offset < sz) { + tools_check_offset(&h, map, &offset); + } + printf("Check done\n"); +#endif + + /* reverse header offset */ + header_change_offset_direction(&h, map); + + offset = h.offset_first; + + /* reverse chunks */ + printf("Reversing offsets...\n"); + while (offset < sz) { + tools_reverse_offset(&h, map, &offset); + } + printf("Reversing done\n"); + +#ifdef SWIFT_DEBUG_CHECKS + /* check offset */ + printf("Check offsets...\n"); + offset = h.offset_first; + while (offset < sz) { + tools_check_offset(&h, map, &offset); + } + printf("Check done\n"); +#endif + + /* free internal variables */ + header_free(&h); + + io_close_file(&fd, &map); + +} diff --git a/logger/logger_reader.h b/logger/logger_reader.h new file mode 100644 index 0000000000000000000000000000000000000000..72e42d63677cb00d9e797b5cbaa64e7e25b0c830 --- /dev/null +++ b/logger/logger_reader.h @@ -0,0 +1,6 @@ +#ifndef __LOGGER_READER_H__ +#define __LOGGER_READER_H__ + +void reverse_offset(char *filename); + +#endif // __LOGGER_READER_H__ diff --git a/logger/logger_time.c b/logger/logger_time.c index 530a5f49c8fd4ca23203c9f585df4d50c8d05566..99b3572a2c45c30208c42544198672279422d2ee 100644 --- a/logger/logger_time.c +++ b/logger/logger_time.c @@ -1,30 +1,6 @@ #include "logger_time.h" #include "logger_io.h" -/** - * @brief convert an integer time to a real time - * - * @param ti integer time to convert - * @param timeBase time base of the integer time - * - * @return converted time - */ -double time_convert_to_double(const integertime_t ti, const double timeBase) { - return ti * timeBase; // should add timebegin -} - -/** - * @brief convert an integer time to a real time - * - * @param ti integer time to convert - * @param timeBase time base of the integer time - * - * @return converted time - */ -integertime_t time_convert_to_integer(const double ti, const double timeBase) { - return ti / timeBase; // should add timebegin -} - /** * @brief read a time stamp * @@ -34,36 +10,32 @@ integertime_t time_convert_to_integer(const double ti, const double timeBase) { * @param map file mapping * @param offset In: position in the file, Out: shifted by the timestamp */ -int time_read(integertime_t *timestamp, double *time, const struct header *h, +void time_read(integertime_t *timestamp, double *time, const struct header *h, void *map, size_t *offset) { - int error_code = 0; size_t mask = 0; size_t prev_offset = 0; *timestamp = 0; *time = 0; /* read chunck header */ - error_code = io_read_mask(h, map, offset, &mask, &prev_offset); - if (error_code != 0) return error_code; + io_read_mask(h, map, offset, &mask, &prev_offset); #ifdef SWIFT_DEBUG_CHECKS size_t ind = 0; /* check if timestamp is present */ - if (!header_is_present_and_get_index(h, "timestamp", &ind)) - error(EIO, "Header does not contain a timestamp"); + if (!header_field_is_present(h, "timestamp", &ind)) + error("Header does not contain a timestamp"); /* check if timestamp */ - if (h->masks[ind] != mask) error(EIO, "Not a timestamp"); + if (h->masks[ind] != mask) error("Not a timestamp"); #endif /* read data */ // TODO - error_code = - io_read_data(map, sizeof(unsigned long long int), timestamp, offset); - error_code = io_read_data(map, sizeof(double), time, offset); + io_read_data(map, sizeof(unsigned long long int), timestamp, offset); + io_read_data(map, sizeof(double), time, offset); - return error_code; } /** @@ -73,26 +45,22 @@ int time_read(integertime_t *timestamp, double *time, const struct header *h, * @param map file mapping * @param offset out: offset of first timestamp * - * @return error code */ -int time_first_timestamp(const struct header *h, void *map, size_t *offset) { +void time_first_timestamp(const struct header *h, void *map, size_t *offset) { *offset = h->offset_first; - int test; size_t i; - if (!header_is_present_and_get_index(h, "timestamp", &i)) - error(EIO, "Time stamp not present in header"); + if (!header_field_is_present(h, "timestamp", &i)) + error("Time stamp not present in header"); size_t tmp = *offset; size_t mask = 0; - test = io_read_mask(h, map, offset, &mask, NULL); - if (test != 0) return test; + io_read_mask(h, map, offset, &mask, NULL); - if (mask != h->masks[i]) error(EIO, "Dump should begin by timestep"); + if (mask != h->masks[i]) error("Dump should begin by timestep"); *offset = tmp; - return 0; } /** @@ -103,7 +71,7 @@ int time_first_timestamp(const struct header *h, void *map, size_t *offset) { * @param map file mapping * @param fd file id */ -int time_array_init(struct time_array *t, const struct header *h, void *map, +void time_array_init(struct time_array *t, const struct header *h, void *map, int fd) { t->next = NULL; @@ -118,7 +86,7 @@ int time_array_init(struct time_array *t, const struct header *h, void *map, /* get file size */ size_t file_size; - int test = io_get_file_size(fd, &file_size); + io_get_file_size(fd, &file_size); while (offset < file_size) { @@ -130,11 +98,9 @@ int time_array_init(struct time_array *t, const struct header *h, void *map, t->time = time; /* get next chunk */ - test = tools_get_next_chunk(h, map, &offset, fd); + int test = tools_get_next_chunk(h, map, &offset, fd); if (test == -1) break; - else if (test != 0) - return test; /* allocate next time_array */ struct time_array *tmp = malloc(sizeof(struct time_array)); @@ -148,8 +114,6 @@ int time_array_init(struct time_array *t, const struct header *h, void *map, struct time_array *tmp = t->prev; tmp->next = NULL; free(t); - - return 0; } /** diff --git a/logger/logger_time.h b/logger/logger_time.h index 757b932b87639777ca156c4f9596f8371ca6254d..82b99a81b58ef2ffe41efa751ba43953cf3518a0 100644 --- a/logger/logger_time.h +++ b/logger/logger_time.h @@ -26,9 +26,9 @@ struct time_array { double time_convert_to_double(const integertime_t ti, const double timeBase); integertime_t time_convert_to_integer(const double ti, const double timeBase); -int time_read(integertime_t *timestamp, double *time, const struct header *h, +void time_read(integertime_t *timestamp, double *time, const struct header *h, void *map, size_t *offset); -int time_array_init(struct time_array *t, const struct header *h, void *map, +void time_array_init(struct time_array *t, const struct header *h, void *map, int fd); integertime_t time_array_get_integertime(struct time_array *t, const size_t offset); @@ -39,6 +39,6 @@ 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); -int time_first_timestamp(const struct header *h, void *map, size_t *offset); +void time_first_timestamp(const struct header *h, void *map, size_t *offset); #endif // __TIMELINE_H__ diff --git a/logger/logger_tools.c b/logger/logger_tools.c index 056019f497e017f1ee8ed372dba9a632ad4c45a8..1f667b49828436f0a0909fe6b83d1b822ac77789 100644 --- a/logger/logger_tools.c +++ b/logger/logger_tools.c @@ -14,7 +14,7 @@ * @param offset In: initial offset, Out: offset of the next chunk * @param fd file id * - * @return error code, -1 if no next chunk + * @return -1 if no next chunk, otherwise 0 */ int tools_get_next_chunk(const struct header *h, void *map, size_t *offset, int fd) { @@ -38,8 +38,7 @@ int _tools_get_next_chunk_forward(const struct header *h, void *map, size_t diff_offset = 0; /* read offset */ - int test = io_read_mask(h, map, offset, NULL, &diff_offset); - if (test != 0) return test; + io_read_mask(h, map, offset, NULL, &diff_offset); if (diff_offset == 0) return -1; @@ -62,22 +61,19 @@ int _tools_get_next_chunk_forward(const struct header *h, void *map, int _tools_get_next_chunk_backward(const struct header *h, void *map, size_t *offset, int fd) { #ifndef SWIFT_DEBUG_CHECKS - error(ENOTSUP, "Should not be used, method too slow"); + error("Should not be used, method too slow"); #endif size_t current_offset = *offset; size_t chunk_header = LOGGER_MASK_SIZE + LOGGER_OFFSET_SIZE; size_t file_size = 0; - int test = io_get_file_size(fd, &file_size); - - if (test != 0) return test; + io_get_file_size(fd, &file_size); while (current_offset < file_size) { size_t mask = 0; size_t prev_offset; - test = io_read_mask(h, map, ¤t_offset, &mask, &prev_offset); - if (test != 0) return test; - + io_read_mask(h, map, ¤t_offset, &mask, &prev_offset); + prev_offset = current_offset - prev_offset - chunk_header; if (*offset == prev_offset) { *offset = current_offset - chunk_header; @@ -98,51 +94,45 @@ int _tools_get_next_chunk_backward(const struct header *h, void *map, * @param map file mapping * @param offset In: initial offset, Out: offset of next chunk * - * @return error code */ -int tools_reverse_offset(const struct header *h, void *map, size_t *offset) { +void 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; /* read mask + offset */ - int error_code = io_read_mask(h, map, offset, &mask, &prev_offset); - if (error_code != 0) return error_code; + 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; - error_code = io_write_data(map, LOGGER_OFFSET_SIZE, &zero, offset); - if (error_code != 0) error(errno, "Unable to write data while reversing"); + io_write_data(map, LOGGER_OFFSET_SIZE, &zero, offset); /* set offset after current chunk */ *offset += header_get_mask_size(h, mask); /* first chunks do not have a previous partner */ - if (prev_offset == cur_offset) return 0; + if (prev_offset == cur_offset) return; if (prev_offset > cur_offset) - error(EIO, "Unexpected offset, header %lu, current %lu", prev_offset, + error("Unexpected offset, header %lu, current %lu", prev_offset, cur_offset); /* modify previous offset */ size_t abs_prev_offset = cur_offset - prev_offset + LOGGER_MASK_SIZE; - error_code = - io_write_data(map, LOGGER_OFFSET_SIZE, &prev_offset, &abs_prev_offset); - if (error_code != 0) error(errno, "Unable to write 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; - error_code = io_read_mask(h, map, &abs_prev_offset, &prev_mask, NULL); + io_read_mask(h, map, &abs_prev_offset, &prev_mask, NULL); if (prev_mask != mask) - error(EIO, "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 0; } /** @@ -156,9 +146,9 @@ int tools_reverse_offset(const struct header *h, void *map, size_t *offset) { * * @return error code */ -int tools_check_offset(const struct header *h, void *map, size_t *offset) { +void tools_check_offset(const struct header *h, void *map, size_t *offset) { #ifndef SWIFT_DEBUG_CHECKS - error(ENOTSUP, "Should not check in non debug mode"); + error("Should not check in non debug mode"); #endif size_t tmp = *offset; @@ -167,15 +157,14 @@ int tools_check_offset(const struct header *h, void *map, size_t *offset) { size_t pointed_offset; /* read mask + offset */ - int error_code = io_read_mask(h, map, offset, &mask, &pointed_offset); - if (error_code != 0) return error_code; - + io_read_mask(h, map, offset, &mask, &pointed_offset); + /* get absolute offset */ if (h->forward_offset) pointed_offset += tmp; else { if (tmp < pointed_offset) - error(EIO, "Offset too large (%lu) at %lu with mask %lu", pointed_offset, + error("Offset too large (%lu) at %lu with mask %lu", pointed_offset, tmp, mask); pointed_offset = tmp - pointed_offset; } @@ -183,32 +172,31 @@ int tools_check_offset(const struct header *h, void *map, size_t *offset) { /* set offset after current chunk */ *offset += header_get_mask_size(h, mask); - if (pointed_offset == tmp || pointed_offset == 0) return 0; + if (pointed_offset == tmp || pointed_offset == 0) return; /* read mask of the pointed chunk */ size_t pointed_mask = 0; - error_code = io_read_mask(h, map, &pointed_offset, &pointed_mask, NULL); + io_read_mask(h, map, &pointed_offset, &pointed_mask, NULL); /* check masks */ if (pointed_mask != mask) - error(EIO, "Error in the offset (mask %lu != %lu) at %lu and %lu", 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, pointed_offset - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE); - if (pointed_mask == 128) return 0; + if (pointed_mask == 128) return; struct particle part; - error_code = particle_read(&part, h, map, &tmp, 0, reader_const, NULL); + particle_read(&part, h, map, &tmp, 0, reader_const, NULL); size_t id = part.id; tmp = pointed_offset - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE; - error_code = particle_read(&part, h, map, &tmp, 0, reader_const, NULL); + particle_read(&part, h, map, &tmp, 0, reader_const, NULL); if (id != part.id) - error(EIO, "Offset wrong, id incorrect (%lu != %lu) at %lu", id, part.id, + error("Offset wrong, id incorrect (%lu != %lu) at %lu", id, part.id, tmp); - return 0; } diff --git a/logger/logger_tools.h b/logger/logger_tools.h index f067c6d2710537f33a7b3abd5a39cab237c610cd..ed30fa2ccac4e35749ef14b967342c778701bd88 100644 --- a/logger/logger_tools.h +++ b/logger/logger_tools.h @@ -17,43 +17,14 @@ struct header; -#ifdef HAVE_PYTHON -/* Set the error message for python and return the error code - * WARNING for python, you need to return NULL and not the error code - */ -#define error(err, s, ...) \ - ({ \ - error_no_return(err, s, ##__VA_ARGS__); \ - return err; \ - }) - -/* Same as error but does not return the error code. - * Should be used for python functions */ -#define error_no_return(err, s, ...) \ - ({ \ - char error_msg[STRING_SIZE]; \ - sprintf(error_msg, "%s:%s():%i: " s ": %s\n", __FILE__, __FUNCTION__, \ - __LINE__, ##__VA_ARGS__, strerror(err)); \ - PyErr_SetString(PyExc_RuntimeError, error_msg); \ - }) - -#else - -#define error(err, s, ...) \ - ({ \ - error_no_return(err, s, ##__VA_ARGS__); \ - exit(1); \ - }) - -#define error_no_return(err, s, ...) \ - ({ \ - char error_msg[STRING_SIZE]; \ - sprintf(error_msg, "%s:%s():%i: " s ": %s\n", __FILE__, __FUNCTION__, \ - __LINE__, ##__VA_ARGS__, strerror(err)); \ +#define error(s, ...) \ + ({ \ + fflush(stdout); \ + fprintf(stderr, "%s:%s():%i: " s "\n", \ + __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ + abort(); \ }) -#endif - #define message(s, ...) \ ({ \ printf("%s:%s():%i: " s "\n", __FILE__, __FUNCTION__, __LINE__, \ @@ -66,7 +37,7 @@ int _tools_get_next_chunk_backward(const struct header *h, void *map, size_t *offset, int fd); int _tools_get_next_chunk_forward(const struct header *h, void *map, size_t *offset); -int tools_reverse_offset(const struct header *h, void *map, size_t *offset); -int tools_check_offset(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); #endif //__LOGGER_TOOLS_H__