2016-06-08 22:26:40 +02:00
|
|
|
/** The internal datastructure for a sample of simulation data.
|
|
|
|
*
|
|
|
|
* @file
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2019-04-23 13:05:31 +02:00
|
|
|
#include <atomic>
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/log.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/signal.hpp>
|
|
|
|
#include <villas/signal_list.hpp>
|
2016-09-10 22:16:23 -04:00
|
|
|
|
2016-06-08 22:26:40 +02:00
|
|
|
/** The length of a sample datastructure with \p values values in bytes. */
|
2021-08-10 10:12:48 -04:00
|
|
|
#define SAMPLE_LENGTH(len) (sizeof(struct Sample) + SAMPLE_DATA_LENGTH(len))
|
2016-06-08 22:26:40 +02:00
|
|
|
|
|
|
|
/** The length of a sample data portion of a sample datastructure with \p values values in bytes. */
|
2018-08-02 10:38:54 +02:00
|
|
|
#define SAMPLE_DATA_LENGTH(len) ((len) * sizeof(double))
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2018-10-11 14:18:30 +02:00
|
|
|
/** The number of values in a sample datastructure. */
|
|
|
|
#define SAMPLE_NUMBER_OF_VALUES(len) ((len) / sizeof(double))
|
|
|
|
|
2016-06-08 22:26:40 +02:00
|
|
|
/** The offset to the beginning of the data section. */
|
2021-08-10 10:12:48 -04:00
|
|
|
#define SAMPLE_DATA_OFFSET(smp) ((char *) (smp) + offsetof(struct Sample, data))
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
/* Forward declarations */
|
|
|
|
struct Pool;
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2017-09-04 14:28:55 +02:00
|
|
|
/** Parts of a sample that can be serialized / de-serialized by the IO formats */
|
2019-06-23 16:13:23 +02:00
|
|
|
enum class SampleFlags {
|
|
|
|
HAS_TS_ORIGIN = (1 << 0), /**< Include origin timestamp in output. */
|
|
|
|
HAS_TS_RECEIVED = (1 << 1), /**< Include receive timestamp in output. */
|
|
|
|
HAS_OFFSET = (1 << 2), /**< Include offset (received - origin timestamp) in output. */
|
|
|
|
HAS_SEQUENCE = (1 << 3), /**< Include sequence number in output. */
|
|
|
|
HAS_DATA = (1 << 4), /**< Include values in output. */
|
2021-02-22 23:16:53 +01:00
|
|
|
|
|
|
|
HAS_TS = HAS_TS_ORIGIN | HAS_TS_RECEIVED, /**< Include origin timestamp in output. */
|
2019-06-23 16:13:23 +02:00
|
|
|
HAS_ALL = (1 << 5) - 1, /**< Enable all output options. */
|
|
|
|
|
|
|
|
IS_FIRST = (1 << 16), /**< This sample is the first of a new simulation case */
|
|
|
|
IS_LAST = (1 << 17) /**< This sample is the last of a running simulation case */
|
2017-09-04 14:28:55 +02:00
|
|
|
};
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample {
|
|
|
|
uint64_t sequence; /**< The sequence number of this sample. */
|
|
|
|
unsigned length; /**< The number of values in sample::values which are valid. */
|
|
|
|
unsigned capacity; /**< The number of values in sample::values for which memory is reserved. */
|
|
|
|
int flags; /**< Flags are used to store binary properties of a sample. */
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
SignalList::Ptr signals; /**< The list of signal descriptors. */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
std::atomic<int> refcnt; /**< Reference counter. */
|
|
|
|
ptrdiff_t pool_off; /**< This sample belongs to this memory pool (relative pointer). See sample_pool(). */
|
2017-09-16 15:04:59 +02:00
|
|
|
|
2016-06-08 22:26:40 +02:00
|
|
|
/** All timestamps are seconds / nano seconds after 1.1.1970 UTC */
|
|
|
|
struct {
|
|
|
|
struct timespec origin; /**< The point in time when this data was sampled. */
|
|
|
|
struct timespec received; /**< The point in time when this data was received. */
|
|
|
|
} ts;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-08-13 15:31:39 +02:00
|
|
|
/** The sample signal values.
|
|
|
|
*
|
2021-08-10 10:12:48 -04:00
|
|
|
* This variable length array (VLA) extends over the end of struct Sample.
|
|
|
|
* Make sure that pointers to struct Sample point to memory blocks of adequate size.
|
2018-08-13 15:31:39 +02:00
|
|
|
* Use the SAMPLE_LENGTH() macro to calculate the required size.
|
|
|
|
*
|
|
|
|
* Metadata describing the details of signal values (such as name, unit, data type and more)
|
2021-08-10 10:12:48 -04:00
|
|
|
* are stored in the struct Sample::signals list. Each entry in this list corresponedents
|
|
|
|
* to an entry in the struct Sample::data array.
|
2018-08-13 15:31:39 +02:00
|
|
|
*/
|
2021-08-10 10:12:48 -04:00
|
|
|
union SignalData data[];
|
2016-06-08 22:26:40 +02:00
|
|
|
};
|
|
|
|
|
2018-05-08 11:43:53 +02:00
|
|
|
#define SAMPLE_NON_POOL PTRDIFF_MIN
|
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
/** Get the address of the pool to which the sample belongs. */
|
2021-08-10 10:12:48 -04:00
|
|
|
#define sample_pool(s) ((s)->pool_off == SAMPLE_NON_POOL ? nullptr : (struct Pool *) ((char *) (s) + (s)->pool_off))
|
|
|
|
|
|
|
|
struct Sample * sample_alloc(struct Pool *p);
|
2017-08-30 23:50:57 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample * sample_alloc_mem(int capacity);
|
2017-10-16 08:08:17 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int sample_init(struct Sample *s);
|
2018-05-08 11:43:53 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample * sample_clone(struct Sample *smp);
|
2017-10-16 08:08:17 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void sample_free(struct Sample *s);
|
2017-10-16 08:08:17 +02:00
|
|
|
|
2016-10-22 20:47:36 -04:00
|
|
|
/** Request \p cnt samples from memory pool \p p and initialize them.
|
2017-05-12 11:35:21 +02:00
|
|
|
* The reference count will already be set to 1.
|
2018-08-07 09:22:26 +02:00
|
|
|
* Use the sample_incref() function to increase it. */
|
2021-08-10 10:12:48 -04:00
|
|
|
int sample_alloc_many(struct Pool *p, struct Sample *smps[], int cnt);
|
2016-10-22 20:47:36 -04:00
|
|
|
|
|
|
|
/** Release an array of samples back to their pools */
|
2021-08-10 10:12:48 -04:00
|
|
|
void sample_free_many(struct Sample *smps[], int cnt);
|
2016-09-10 22:16:23 -04:00
|
|
|
|
2016-08-28 23:57:11 -04:00
|
|
|
/** Increase reference count of sample */
|
2021-08-10 10:12:48 -04:00
|
|
|
int sample_incref(struct Sample *s);
|
2016-08-28 23:57:11 -04:00
|
|
|
|
|
|
|
/** Decrease reference count and release memory if last reference was held. */
|
2021-08-10 10:12:48 -04:00
|
|
|
int sample_decref(struct Sample *s);
|
2016-08-28 23:57:11 -04:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int sample_copy(struct Sample *dst, const struct Sample *src);
|
2017-04-07 17:46:50 +02:00
|
|
|
|
2019-01-23 00:49:28 +01:00
|
|
|
/** Dump all details about a sample to debug log */
|
2021-08-10 10:12:48 -04:00
|
|
|
void sample_dump(villas::Logger logger, struct Sample *s);
|
2019-01-23 00:49:28 +01:00
|
|
|
|
2017-09-04 16:19:43 +02:00
|
|
|
/** Compare two samples */
|
2021-08-10 10:12:48 -04:00
|
|
|
int sample_cmp(struct Sample *a, struct Sample *b, double epsilon, int flags);
|
|
|
|
|
|
|
|
int sample_clone_many(struct Sample *dsts[], const struct Sample * const srcs[], int cnt);
|
|
|
|
int sample_copy_many(struct Sample * const dsts[], const struct Sample * const srcs[], int cnt);
|
|
|
|
int sample_incref_many(struct Sample * const smps[], int cnt);
|
|
|
|
int sample_decref_many(struct Sample * const smps[], int cnt);
|
2017-09-04 16:19:43 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
enum SignalType sample_format(const struct Sample *s, unsigned idx);
|
2017-04-07 17:46:50 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void sample_data_insert(struct Sample *smp, const union SignalData *src, size_t offset, size_t len);
|
|
|
|
void sample_data_remove(struct Sample *smp, size_t offset, size_t len);
|
2018-08-06 11:26:25 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|