2016-06-08 22:26:40 +02:00
|
|
|
/** The internal datastructure for a sample of simulation data.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-08 22:26:40 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cinttypes>
|
2017-04-07 17:46:50 +02:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/sample.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2020-01-25 17:39:06 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2019-04-23 13:11:08 +02:00
|
|
|
#include <villas/colors.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/timing.h>
|
2018-08-20 18:31:27 +02:00
|
|
|
#include <villas/signal.h>
|
|
|
|
#include <villas/list.h>
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2020-01-25 17:39:06 +01:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
int sample_init(struct sample *s)
|
|
|
|
{
|
|
|
|
struct pool *p = sample_pool(s);
|
|
|
|
|
|
|
|
s->length = 0;
|
|
|
|
s->capacity = (p->blocksz - sizeof(struct sample)) / sizeof(s->data[0]);
|
|
|
|
s->refcnt = ATOMIC_VAR_INIT(1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sample * sample_alloc(struct pool *p)
|
|
|
|
{
|
2018-04-26 09:32:13 +02:00
|
|
|
struct sample *s;
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
s = (struct sample *) pool_get(p);
|
2018-04-26 09:32:13 +02:00
|
|
|
if (!s)
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2017-10-16 08:08:17 +02:00
|
|
|
|
|
|
|
s->pool_off = (char *) p - (char *) s;
|
|
|
|
|
|
|
|
sample_init(s);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-05-08 11:43:53 +02:00
|
|
|
struct sample * sample_alloc_mem(int capacity)
|
|
|
|
{
|
2018-08-02 10:38:54 +02:00
|
|
|
size_t sz = SAMPLE_LENGTH(capacity);
|
2018-05-08 11:43:53 +02:00
|
|
|
|
2020-09-10 18:23:25 +02:00
|
|
|
auto *s = (struct sample *) new char[sz];
|
|
|
|
if (!s)
|
2020-07-04 22:39:15 +02:00
|
|
|
throw MemoryAllocationError();
|
2020-01-25 17:39:06 +01:00
|
|
|
|
2020-09-10 18:23:25 +02:00
|
|
|
memset((void *) s, 0, sz);
|
2018-05-08 11:43:53 +02:00
|
|
|
|
|
|
|
s->pool_off = SAMPLE_NON_POOL;
|
|
|
|
|
|
|
|
s->length = 0;
|
|
|
|
s->capacity = capacity;
|
|
|
|
s->refcnt = ATOMIC_VAR_INIT(1);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
void sample_free(struct sample *s)
|
|
|
|
{
|
|
|
|
struct pool *p = sample_pool(s);
|
|
|
|
|
2018-05-08 11:43:53 +02:00
|
|
|
if (p)
|
|
|
|
pool_put(p, s);
|
|
|
|
else
|
2020-01-21 16:26:51 +01:00
|
|
|
delete[] (char *) s;
|
2017-10-16 08:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sample_alloc_many(struct pool *p, struct sample *smps[], int cnt)
|
2017-03-27 12:50:39 +02:00
|
|
|
{
|
2016-09-10 22:16:23 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pool_get_many(p, (void **) smps, cnt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-10-22 20:47:36 -04:00
|
|
|
for (int i = 0; i < ret; i++) {
|
2017-04-04 12:32:48 +02:00
|
|
|
smps[i]->pool_off = (char *) p - (char *) smps[i];
|
2017-10-16 08:08:17 +02:00
|
|
|
|
|
|
|
sample_init(smps[i]);
|
2016-10-22 20:47:36 -04:00
|
|
|
}
|
2016-09-10 22:16:23 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
void sample_free_many(struct sample *smps[], int cnt)
|
2016-10-22 20:47:36 -04:00
|
|
|
{
|
2018-05-08 11:43:53 +02:00
|
|
|
for (int i = 0; i < cnt; i++)
|
|
|
|
sample_free(smps[i]);
|
2016-10-22 20:47:36 -04:00
|
|
|
}
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
int sample_decref_many(struct sample *smps[], int cnt)
|
2017-04-07 17:46:50 +02:00
|
|
|
{
|
|
|
|
int released = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
for (int i = 0; i < cnt; i++) {
|
2018-08-07 09:22:26 +02:00
|
|
|
if (sample_decref(smps[i]) == 0)
|
2017-04-07 17:46:50 +02:00
|
|
|
released++;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
return released;
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
int sample_incref_many(struct sample *smps[], int cnt)
|
2017-04-07 17:46:50 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < cnt; i++)
|
2018-08-07 09:22:26 +02:00
|
|
|
sample_incref(smps[i]);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
int sample_incref(struct sample *s)
|
2016-08-28 23:57:11 -04:00
|
|
|
{
|
|
|
|
return atomic_fetch_add(&s->refcnt, 1) + 1;
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:22:26 +02:00
|
|
|
int sample_decref(struct sample *s)
|
2016-08-28 23:57:11 -04:00
|
|
|
{
|
|
|
|
int prev = atomic_fetch_sub(&s->refcnt, 1);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-04 12:32:48 +02:00
|
|
|
/* Did we had the last reference? */
|
2017-08-30 23:50:57 +02:00
|
|
|
if (prev == 1)
|
2018-05-08 11:43:53 +02:00
|
|
|
sample_free(s);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-08-28 23:57:11 -04:00
|
|
|
return prev - 1;
|
|
|
|
}
|
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
int sample_copy(struct sample *dst, struct sample *src)
|
|
|
|
{
|
|
|
|
dst->length = MIN(src->length, dst->capacity);
|
|
|
|
|
|
|
|
dst->sequence = src->sequence;
|
2017-09-16 15:04:59 +02:00
|
|
|
dst->flags = src->flags;
|
2017-09-04 14:28:55 +02:00
|
|
|
dst->ts = src->ts;
|
2018-08-20 18:31:27 +02:00
|
|
|
dst->signals = src->signals;
|
2017-04-07 17:46:50 +02:00
|
|
|
|
2018-08-02 10:38:54 +02:00
|
|
|
memcpy(&dst->data, &src->data, SAMPLE_DATA_LENGTH(dst->length));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-16 08:08:17 +02:00
|
|
|
struct sample * sample_clone(struct sample *orig)
|
|
|
|
{
|
|
|
|
struct sample *clone;
|
|
|
|
struct pool *pool;
|
|
|
|
|
|
|
|
pool = sample_pool(orig);
|
|
|
|
if (!pool)
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2017-10-16 08:08:17 +02:00
|
|
|
|
|
|
|
clone = sample_alloc(pool);
|
|
|
|
if (!clone)
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2017-10-16 08:08:17 +02:00
|
|
|
|
|
|
|
sample_copy(clone, orig);
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sample_clone_many(struct sample *clones[], struct sample *origs[], int cnt)
|
|
|
|
{
|
|
|
|
int alloced, copied;
|
|
|
|
struct pool *pool;
|
|
|
|
|
|
|
|
if (cnt <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pool = sample_pool(origs[0]);
|
|
|
|
if (!pool)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
alloced = sample_alloc_many(pool, clones, cnt);
|
|
|
|
|
|
|
|
copied = sample_copy_many(clones, origs, alloced);
|
|
|
|
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
int sample_copy_many(struct sample *dsts[], struct sample *srcs[], int cnt)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < cnt; i++)
|
|
|
|
sample_copy(dsts[i], srcs[i]);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:43 +02:00
|
|
|
int sample_cmp(struct sample *a, struct sample *b, double epsilon, int flags)
|
|
|
|
{
|
2017-09-16 15:04:59 +02:00
|
|
|
if ((a->flags & b->flags & flags) != flags) {
|
|
|
|
printf("flags: a=%#x, b=%#x, wanted=%#x\n", a->flags, b->flags, flags);
|
2017-09-04 16:19:43 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare sequence no */
|
2019-06-23 16:13:23 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_SEQUENCE) {
|
2017-09-04 16:19:43 +02:00
|
|
|
if (a->sequence != b->sequence) {
|
2018-08-09 14:24:45 +02:00
|
|
|
printf("sequence no: %" PRIu64 " != %" PRIu64 "\n", a->sequence, b->sequence);
|
2017-09-04 23:03:00 +02:00
|
|
|
return 2;
|
2017-09-04 16:19:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare timestamp */
|
2019-06-23 16:13:23 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_TS_ORIGIN) {
|
2017-09-04 16:19:43 +02:00
|
|
|
if (time_delta(&a->ts.origin, &b->ts.origin) > epsilon) {
|
|
|
|
printf("ts.origin: %f != %f\n", time_to_double(&a->ts.origin), time_to_double(&b->ts.origin));
|
2017-09-04 23:03:00 +02:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:43 +02:00
|
|
|
/* Compare data */
|
2019-06-23 16:13:23 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_DATA) {
|
2017-09-04 16:19:43 +02:00
|
|
|
if (a->length != b->length) {
|
2020-09-11 14:57:05 +02:00
|
|
|
printf("length: %u != %u\n", a->length, b->length);
|
2017-09-04 23:03:00 +02:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
for (unsigned i = 0; i < a->length; i++) {
|
2018-08-20 18:31:27 +02:00
|
|
|
/* Compare format */
|
|
|
|
if (sample_format(a, i) != sample_format(b, i))
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
switch (sample_format(a, i)) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::FLOAT:
|
2017-09-04 23:03:00 +02:00
|
|
|
if (fabs(a->data[i].f - b->data[i].f) > epsilon) {
|
2020-09-11 14:57:05 +02:00
|
|
|
printf("data[%u].f: %f != %f\n", i, a->data[i].f, b->data[i].f);
|
2017-09-04 23:03:00 +02:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::INTEGER:
|
2017-09-04 23:03:00 +02:00
|
|
|
if (a->data[i].i != b->data[i].i) {
|
2020-09-11 14:57:05 +02:00
|
|
|
printf("data[%u].i: %" PRId64 " != %" PRId64 "\n", i, a->data[i].i, b->data[i].i);
|
2017-09-04 23:03:00 +02:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
break;
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::BOOLEAN:
|
2018-08-20 18:31:27 +02:00
|
|
|
if (a->data[i].b != b->data[i].b) {
|
2020-09-11 14:57:05 +02:00
|
|
|
printf("data[%u].b: %s != %s\n", i, a->data[i].b ? "true" : "false", b->data[i].b ? "true" : "false");
|
2018-08-20 18:31:27 +02:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case SignalType::COMPLEX:
|
2019-06-23 16:57:00 +02:00
|
|
|
if (std::abs(a->data[i].z - b->data[i].z) > epsilon) {
|
2020-09-11 14:57:05 +02:00
|
|
|
printf("data[%u].z: %f+%fi != %f+%fi\n", i, std::real(a->data[i].z), std::imag(a->data[i].z), std::real(b->data[i].z), std::imag(b->data[i].z));
|
2018-08-20 18:31:27 +02:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: { }
|
2017-09-04 16:19:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-06 11:26:25 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum SignalType sample_format(const struct sample *s, unsigned idx)
|
2018-08-06 11:26:25 +02:00
|
|
|
{
|
|
|
|
struct signal *sig;
|
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
sig = (struct signal *) vlist_at_safe(s->signals, idx);
|
2018-08-06 11:26:25 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
return sig ? sig->type : SignalType::INVALID;
|
2018-08-06 11:26:25 +02:00
|
|
|
}
|
2019-01-23 00:49:28 +01:00
|
|
|
|
|
|
|
void sample_dump(struct sample *s)
|
|
|
|
{
|
2020-07-04 17:14:39 +02:00
|
|
|
info("Sample: sequence=%" PRIu64 ", length=%d, capacity=%d, flags=%#x, signals=%p, #signals=%zu, "
|
2019-01-23 00:49:28 +01:00
|
|
|
"refcnt=%d, pool_off=%zd",
|
|
|
|
s->sequence, s->length, s->capacity, s->flags, s->signals,
|
|
|
|
s->signals ? vlist_length(s->signals) : 0, atomic_load(&s->refcnt), s->pool_off);
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (s->flags & (int) SampleFlags::HAS_TS_ORIGIN)
|
2021-01-06 15:11:26 +01:00
|
|
|
info(" ts.origin=%lld.%lld", (long long) s->ts.origin.tv_sec,
|
|
|
|
(long long) s->ts.origin.tv_nsec);
|
2019-01-23 00:49:28 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (s->flags & (int) SampleFlags::HAS_TS_RECEIVED)
|
2021-01-06 15:11:26 +01:00
|
|
|
info(" ts.received=%lld.%lld", (long long) s->ts.received.tv_sec,
|
|
|
|
(long long) s->ts.received.tv_nsec);
|
2019-01-23 00:49:28 +01:00
|
|
|
|
2020-07-04 17:14:39 +02:00
|
|
|
if (s->signals) {
|
|
|
|
info(" Signals:");
|
2019-01-23 00:49:28 +01:00
|
|
|
signal_list_dump(s->signals, s->data, s->length);
|
2020-07-04 17:14:39 +02:00
|
|
|
}
|
2019-01-23 00:49:28 +01:00
|
|
|
}
|
2019-03-08 15:21:46 +01:00
|
|
|
|
|
|
|
void sample_data_insert(struct sample *smp, const union signal_data *src, size_t offset, size_t len)
|
|
|
|
{
|
|
|
|
memmove(&smp->data[offset + len], &smp->data[offset], sizeof(smp->data[0]) * (smp->length - offset));
|
|
|
|
memcpy(&smp->data[offset], src, sizeof(smp->data[0]) * len);
|
|
|
|
|
|
|
|
smp->length += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sample_data_remove(struct sample *smp, size_t offset, size_t len)
|
|
|
|
{
|
|
|
|
size_t sz = sizeof(smp->data[0]) * len;
|
|
|
|
|
|
|
|
memmove(&smp->data[offset], &smp->data[offset + len], sz);
|
|
|
|
|
|
|
|
smp->length -= len;
|
|
|
|
}
|