2017-04-15 21:22:19 +02:00
|
|
|
/** Test "client" for the shared memory interface.
|
|
|
|
*
|
|
|
|
* Waits on the incoming queue, prints received samples and writes them
|
|
|
|
* back to the other queue.
|
|
|
|
*
|
|
|
|
* @author Georg Martin Reinke <georg.reinke@rwth-aachen.de>
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*********************************************************************************/
|
2017-04-05 12:52:21 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "log.h"
|
2017-04-07 12:57:40 +02:00
|
|
|
#include "node.h"
|
2017-04-05 12:52:21 +02:00
|
|
|
#include "nodes/shmem.h"
|
|
|
|
#include "pool.h"
|
2017-04-12 14:38:18 +02:00
|
|
|
#include "queue_signalled.h"
|
2017-04-05 12:52:21 +02:00
|
|
|
#include "sample.h"
|
2017-04-07 12:57:40 +02:00
|
|
|
#include "shmem.h"
|
2017-04-06 12:12:56 +02:00
|
|
|
#include "utils.h"
|
2017-04-05 12:52:21 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-04-12 14:38:18 +02:00
|
|
|
void *base;
|
|
|
|
struct shmem_shared *shared;
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
void usage()
|
|
|
|
{
|
2017-04-15 21:49:40 +02:00
|
|
|
printf("Usage: villas-test-shmem SHM_NAME VECTORIZE\n");
|
2017-04-07 13:31:40 +02:00
|
|
|
printf(" SHMNAME name of the shared memory object\n");
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 14:38:18 +02:00
|
|
|
void quit(int sig)
|
|
|
|
{
|
|
|
|
shmem_shared_close(shared, base);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2017-04-15 18:14:09 +02:00
|
|
|
struct log log;
|
|
|
|
|
|
|
|
log_init(&log, V, LOG_ALL);
|
|
|
|
log_start(&log);
|
|
|
|
|
2017-04-15 18:59:22 +02:00
|
|
|
int readcnt, writecnt, avail;
|
|
|
|
|
2017-04-15 20:18:31 +02:00
|
|
|
if (argc != 3) {
|
2017-04-05 12:52:21 +02:00
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 20:18:31 +02:00
|
|
|
|
|
|
|
char *object = argv[1];
|
|
|
|
int vectorize = atoi(argv[2]);
|
2017-04-05 12:52:21 +02:00
|
|
|
|
2017-04-15 20:18:31 +02:00
|
|
|
shared = shmem_shared_open(object, &base);
|
2017-04-07 13:27:10 +02:00
|
|
|
if (!shared)
|
2017-04-05 12:52:21 +02:00
|
|
|
serror("Failed to open shmem interface");
|
|
|
|
|
2017-04-12 14:38:18 +02:00
|
|
|
signal(SIGINT, quit);
|
|
|
|
signal(SIGTERM, quit);
|
2017-04-15 20:18:31 +02:00
|
|
|
struct sample *insmps[vectorize], *outsmps[vectorize];
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
while (1) {
|
2017-04-15 20:18:31 +02:00
|
|
|
readcnt = shmem_shared_read(shared, insmps, vectorize);
|
2017-04-15 18:59:22 +02:00
|
|
|
if (readcnt == -1) {
|
2017-04-15 20:18:31 +02:00
|
|
|
printf("Node stopped, exiting");
|
2017-04-12 14:38:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-04-15 20:18:31 +02:00
|
|
|
|
2017-04-15 18:59:22 +02:00
|
|
|
avail = sample_alloc(&shared->pool, outsmps, readcnt);
|
|
|
|
if (avail < readcnt)
|
|
|
|
warn("Pool underrun: %d / %d\n", avail, readcnt);
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
for (int i = 0; i < avail; i++) {
|
|
|
|
outsmps[i]->sequence = insmps[i]->sequence;
|
|
|
|
outsmps[i]->ts = insmps[i]->ts;
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
int len = MIN(insmps[i]->length, outsmps[i]->capacity);
|
2017-04-15 18:59:22 +02:00
|
|
|
memcpy(outsmps[i]->data, insmps[i]->data, SAMPLE_DATA_LEN(len));
|
|
|
|
|
2017-04-05 12:52:21 +02:00
|
|
|
outsmps[i]->length = len;
|
|
|
|
}
|
2017-04-15 18:59:22 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < readcnt; i++)
|
2017-04-05 12:52:21 +02:00
|
|
|
sample_put(insmps[i]);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
|
|
|
writecnt = shmem_shared_write(shared, outsmps, avail);
|
|
|
|
if (writecnt < avail)
|
|
|
|
warn("Short write");
|
|
|
|
|
|
|
|
info("Read / Write: %d / %d", readcnt, writecnt);
|
2017-04-05 12:52:21 +02:00
|
|
|
}
|
|
|
|
}
|