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

Logger: Add structure for time record

parent c739e843
Branches
Tags
1 merge request!685Logger loader
...@@ -33,14 +33,19 @@ void time_array_ensure_size(struct time_array *t) { ...@@ -33,14 +33,19 @@ void time_array_ensure_size(struct time_array *t) {
/* Increase the size */ /* Increase the size */
t->capacity *= 2; t->capacity *= 2;
t->time = realloc(t->time, sizeof(double) * t->capacity); /* Allocate the new array */
if (t->time == NULL) error("Failed to realloc time memory."); struct time_record *tmp = malloc(sizeof(struct time_record) * t->capacity);
if (tmp == NULL) error("Failed to allocate the time records.");
t->int_time = realloc(t->int_time, sizeof(integertime_t) * t->capacity); /* Copy the memory */
if (t->int_time == NULL) error("Failed to realloc integer time memory."); memcpy(tmp, t->records, sizeof(struct time_record) * t->size);
t->offset = realloc(t->offset, sizeof(size_t) * t->capacity); /* Cleanup the memory */
if (t->offset == NULL) error("Failed to realloc offset memory."); free(t->records);
/* Set the pointer to the new array */
t->records = tmp;
} }
/** /**
...@@ -58,9 +63,9 @@ void time_array_append(struct time_array *t, const integertime_t int_time, ...@@ -58,9 +63,9 @@ void time_array_append(struct time_array *t, const integertime_t int_time,
time_array_ensure_size(t); time_array_ensure_size(t);
/* Copy the values */ /* Copy the values */
t->time[t->size] = time; t->records[t->size].time = time;
t->int_time[t->size] = int_time; t->records[t->size].int_time = int_time;
t->offset[t->size] = offset; t->records[t->size].offset = offset;
/* Increase the size used. */ /* Increase the size used. */
t->size += 1; t->size += 1;
...@@ -141,14 +146,8 @@ size_t time_offset_first_record(const struct header *h) { ...@@ -141,14 +146,8 @@ size_t time_offset_first_record(const struct header *h) {
*/ */
void time_array_init(struct time_array *t) { void time_array_init(struct time_array *t) {
/* Allocate the arrays */ /* Allocate the arrays */
t->int_time = malloc(sizeof(integertime_t) * LOGGER_TIME_INIT_SIZE); t->records = malloc(sizeof(struct time_record) * LOGGER_TIME_INIT_SIZE);
if (t->int_time == NULL) error("Failed to initialize the integer times."); if (t->records == NULL) error("Failed to initialize the time records.");
t->time = malloc(sizeof(double) * LOGGER_TIME_INIT_SIZE);
if (t->time == NULL) error("Failed to initialize the times.");
t->offset = malloc(sizeof(size_t) * LOGGER_TIME_INIT_SIZE);
if (t->offset == NULL) error("Failed to initialize the offsets.");
/* Initialize the sizes */ /* Initialize the sizes */
t->size = 0; t->size = 0;
...@@ -196,7 +195,7 @@ void time_array_populate(struct time_array *t, struct logger_logfile *log) { ...@@ -196,7 +195,7 @@ void time_array_populate(struct time_array *t, struct logger_logfile *log) {
integertime_t time_array_get_integertime(struct time_array *t, integertime_t time_array_get_integertime(struct time_array *t,
const size_t offset) { const size_t offset) {
size_t ind = time_array_get_index(t, offset); size_t ind = time_array_get_index(t, offset);
return t->int_time[ind]; return t->records[ind].int_time;
} }
/** /**
...@@ -209,7 +208,7 @@ integertime_t time_array_get_integertime(struct time_array *t, ...@@ -209,7 +208,7 @@ integertime_t time_array_get_integertime(struct time_array *t,
*/ */
double time_array_get_time(const struct time_array *t, const size_t offset) { double time_array_get_time(const struct time_array *t, const size_t offset) {
size_t ind = time_array_get_index(t, offset); size_t ind = time_array_get_index(t, offset);
return t->time[ind]; return t->records[ind].time;
} }
/** /**
...@@ -224,15 +223,18 @@ size_t time_array_get_index(const struct time_array *t, const size_t offset) { ...@@ -224,15 +223,18 @@ size_t time_array_get_index(const struct time_array *t, const size_t offset) {
#ifdef SWIFT_DEBUG_CHECKS #ifdef SWIFT_DEBUG_CHECKS
if (!t) error("NULL pointer."); if (!t) error("NULL pointer.");
if (offset < t->records[0].offset || offset > t->records[t->size].offset)
error("Offset outside of range.");
#endif #endif
/* Find the time_array with the correct offset. */ /* Find the time_array with the correct offset. */
for (size_t i = 1; i < t->size; i++) { for (size_t i = 1; i < t->size; i++) {
if (offset < t->offset[i]) { if (offset < t->records[i].offset) {
return i - 1; return i - 1;
} }
else if (offset == t->offset[i]) else if (offset == t->records[i].offset)
return i; return i;
} }
...@@ -246,14 +248,8 @@ size_t time_array_get_index(const struct time_array *t, const size_t offset) { ...@@ -246,14 +248,8 @@ size_t time_array_get_index(const struct time_array *t, const size_t offset) {
*/ */
void time_array_free(struct time_array *t) { void time_array_free(struct time_array *t) {
/* Free the arrays */ /* Free the arrays */
free(t->int_time); free(t->records);
t->int_time = NULL; t->records = NULL;
free(t->time);
t->time = NULL;
free(t->offset);
t->offset = NULL;
/* Reset the counters */ /* Reset the counters */
t->size = 0; t->size = 0;
...@@ -271,13 +267,14 @@ void time_array_print(const struct time_array *t) { ...@@ -271,13 +267,14 @@ void time_array_print(const struct time_array *t) {
size_t n = t->size; size_t n = t->size;
size_t up_threshold = n - threshold; size_t up_threshold = n - threshold;
printf("Times (size %lu): [%lli (%g)", n, t->int_time[0], t->time[0]); printf("Times (size %lu): [%lli (%g)", n,
t->records[0].int_time, t->records[0].time);
/* Loop over all elements. */ /* Loop over all elements. */
for (size_t i = 1; i < n; i++) { for (size_t i = 1; i < n; i++) {
/* Skip the times at the center of the array. */ /* Skip the times at the center of the array. */
if (i < threshold || i > up_threshold) if (i < threshold || i > up_threshold)
printf(", %lli (%g)", t->int_time[i], t->time[i]); printf(", %lli (%g)", t->records[i].int_time, t->records[i].time);
if (i == threshold) printf(", ..."); if (i == threshold) printf(", ...");
} }
...@@ -296,12 +293,12 @@ void time_array_print_offset(const struct time_array *t) { ...@@ -296,12 +293,12 @@ void time_array_print_offset(const struct time_array *t) {
size_t n = t->size; size_t n = t->size;
size_t up_threshold = n - threshold; size_t up_threshold = n - threshold;
printf("Times (size %lu): [%lu", n, t->offset[0]); printf("Times (size %lu): [%lu", n, t->records[0].offset);
/* Loop over all elements. */ /* Loop over all elements. */
for (size_t i = 1; i < n; i++) { for (size_t i = 1; i < n; i++) {
/* Skip the offset in the middle of the array. */ /* Skip the offset in the middle of the array. */
if (i < threshold || i > up_threshold) printf(", %lu", t->offset[i]); if (i < threshold || i > up_threshold) printf(", %lu", t->records[i].offset);
if (i == threshold) printf(", ..."); if (i == threshold) printf(", ...");
} }
......
...@@ -29,6 +29,20 @@ struct logger_reader; ...@@ -29,6 +29,20 @@ struct logger_reader;
#define LOGGER_TIME_INIT_SIZE 1024 #define LOGGER_TIME_INIT_SIZE 1024
/**
* @brief This structure contains all the information present in a time record.
*/
struct time_record {
/* Integertime of the records. */
integertime_t int_time;
/* Double time of the records. */
double time;
/* Offset in the file of the time records. */
size_t offset;
};
/** /**
* @brief This structure contains all the time record. * @brief This structure contains all the time record.
* *
...@@ -44,14 +58,9 @@ struct logger_reader; ...@@ -44,14 +58,9 @@ struct logger_reader;
* #time_array_get_index. * #time_array_get_index.
*/ */
struct time_array { struct time_array {
/* Integertime of the records. */
integertime_t *int_time;
/* Double time of the records. */ /* The complete list of time record */
double *time; struct time_record *records;
/* Offset in the file of the time records. */
size_t *offset;
/* Number of element in the arrays. */ /* Number of element in the arrays. */
size_t size; size_t size;
......
...@@ -27,11 +27,14 @@ ...@@ -27,11 +27,14 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
/* Check that we are really testing the reallocation */
if (NUMBER_OF_ELEMENT < LOGGER_TIME_INIT_SIZE) { if (NUMBER_OF_ELEMENT < LOGGER_TIME_INIT_SIZE) {
error("Not testing the reallocation."); error("Not testing the reallocation.");
} }
/* Fix the random seed in order to reproduce the results */
srand(100); srand(100);
/* Initialize the time array */ /* Initialize the time array */
struct time_array times; struct time_array times;
time_array_init(&times); time_array_init(&times);
...@@ -66,9 +69,9 @@ int main(int argc, char *argv[]) { ...@@ -66,9 +69,9 @@ int main(int argc, char *argv[]) {
/* Check the values obtained */ /* Check the values obtained */
assert(i == ind); assert(i == ind);
assert(int_time == times.int_time[ind]); assert(int_time == times.records[ind].int_time);
assert(time == times.time[ind]); assert(time == times.records[ind].time);
assert(offset == times.offset[ind]); assert(offset == times.records[ind].offset);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment