1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/server/src/server.c

209 lines
5.2 KiB
C
Raw Normal View History

/** Main routine.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
2015-08-22 17:42:38 +02:00
#include <sched.h>
#include "config.h"
#include "utils.h"
2015-05-06 11:43:05 +02:00
#include "cfg.h"
#include "path.h"
#include "node.h"
2015-08-22 17:42:38 +02:00
#include "stats.h"
2015-09-17 00:55:20 +02:00
#include "checks.h"
#ifdef ENABLE_OPAL_ASYNC
2015-08-21 10:19:32 +02:00
#include "opal.h"
#endif
/** Linked list of nodes */
2015-06-02 22:04:03 +02:00
struct list nodes;
/** Linked list of paths */
2015-06-02 22:04:03 +02:00
struct list paths;
/** The global configuration */
2015-06-02 22:04:03 +02:00
struct settings settings;
/** libconfig handle */
2015-03-21 18:03:55 +01:00
static config_t config;
static void quit()
{
2015-03-31 18:13:43 +02:00
info("Stopping paths");
FOREACH(&paths, it)
path_stop(it->path);
2015-03-31 18:13:43 +02:00
info("Stopping nodes");
FOREACH(&nodes, it)
node_stop(it->node);
2015-08-22 17:42:38 +02:00
info("De-initializing node types");
node_deinit();
/* Freeing dynamically allocated memory */
list_destroy(&paths);
list_destroy(&nodes);
config_destroy(&config);
2015-08-07 01:11:43 +02:00
2015-03-21 18:03:55 +01:00
info("Goodbye!");
_exit(EXIT_SUCCESS);
}
2015-06-02 22:04:03 +02:00
static void realtime_init()
{ INDENT
if (check_kernel_cmdline())
warn("You should reserve some cores for the server (see 'isolcpus')");
if (check_kernel_rtpreempt())
warn("We recommend to use an RT_PREEMPT patched kernel!");
/* Use FIFO scheduler with real time priority */
if (settings.priority) {
2015-08-22 17:42:38 +02:00
struct sched_param param = {
.sched_priority = settings.priority
};
if (sched_setscheduler(0, SCHED_FIFO, &param))
serror("Failed to set real time priority");
debug(3, "Set task priority to %u", settings.priority);
}
warn("Use setting 'priority' to enable real-time scheduling!");
/* Pin threads to CPUs by setting the affinity */
if (settings.affinity) {
cpu_set_t cset = to_cpu_set(settings.affinity);
if (sched_setaffinity(0, sizeof(cset), &cset))
serror("Failed to set CPU affinity to '%#x'", settings.affinity);
debug(3, "Set affinity to %#x", settings.affinity);
}
warn("Use setting 'affinity' to pin process to isolated CPU cores!");
}
/* Setup exit handler */
2015-06-02 22:04:03 +02:00
static void signals_init()
2015-03-31 14:28:00 +02:00
{
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = quit
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGINT, &sa_quit, NULL);
2015-06-02 22:24:44 +02:00
sigaction(SIGTERM, &sa_quit, NULL);
}
2015-06-02 22:04:03 +02:00
static void usage(const char *name)
{
printf("Usage: %s CONFIG\n", name);
printf(" CONFIG is a required path to a configuration file\n\n");
#ifdef ENABLE_OPAL_ASYNC
printf("Usage: %s OPAL_ASYNC_SHMEM_NAME OPAL_ASYNC_SHMEM_SIZE OPAL_PRINT_SHMEM_NAME\n", name);
printf(" This type of invocation is used by OPAL-RT Asynchronous processes.\n");
printf(" See in the RT-LAB User Guide for more information.\n\n");
#endif
printf("Supported node types:\n");
#ifdef ENABLE_FILE
printf(" - file: support for file log / replay node type\n");
2015-08-22 17:42:38 +02:00
#endif
#ifdef ENABLE_SOCKET
printf(" - socket: Network socket (libnl3)\n");
#endif
2015-09-27 18:05:35 +02:00
#ifdef ENABLE_GTFPGA
printf(" - gtfpga: GTFPGA PCIe card (libpci)\n");
2015-08-22 17:42:38 +02:00
#endif
#ifdef ENABLE_OPAL_ASYNC
printf(" - opal: run as OPAL Asynchronous Process (libOpalAsyncApi)\n");
#endif
#ifdef ENABLE_NGSI
printf(" - ngsi: OMA Next Generation Services Interface 10 (libcurl, libjansson, libuuid)\n");
2015-08-22 17:42:38 +02:00
#endif
printf("\n");
printf("Simulator2Simulator Server %s (built on %s %s)\n",
BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
2015-06-02 21:53:04 +02:00
printf(" copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC\n");
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
_mtid = pthread_self();
/* Check arguments */
#ifdef ENABLE_OPAL_ASYNC
if (argc != 2 && argc != 4)
#else
if (argc != 2)
#endif
usage(argv[0]);
2015-08-07 01:11:43 +02:00
char *configfile = (argc == 2) ? argv[1] : "opal-shmem.conf";
2015-03-31 14:27:38 +02:00
log_reset();
2015-09-17 00:58:04 +02:00
info("This is Simulator2Simulator Server (S2SS)");
info (" Version: %s (built on %s, %s)", BLD(YEL(VERSION)),
BLD(MAG(__DATE__)), BLD(MAG(__TIME__)));
2015-09-17 00:55:20 +02:00
/* Checks system requirements*/
#ifdef LICENSE
if (check_license_trace())
error("This software should not be traced!");
if (check_license_time())
error("Your license expired");
if (check_license_ids())
error("This version is compiled for a different machine!");
#endif
if (check_kernel_version())
error("Your kernel version is to old: required >= %u.%u", KERNEL_VERSION_MAJ, KERNEL_VERSION_MIN);
/* Initialize lists */
list_init(&nodes, (dtor_cb_t) node_destroy);
list_init(&paths, (dtor_cb_t) path_destroy);
2015-08-07 01:11:43 +02:00
2015-03-31 18:13:43 +02:00
info("Initialize real-time system");
realtime_init();
2015-03-31 18:13:43 +02:00
info("Initialize signals");
signals_init();
2015-03-21 15:29:00 +01:00
2015-03-31 18:13:43 +02:00
info("Parsing configuration");
config_init(&config);
config_parse(configfile, &config, &settings, &nodes, &paths);
2015-08-07 01:11:43 +02:00
info("Initialize node types");
node_init(argc, argv, &settings);
2015-03-31 18:13:43 +02:00
info("Starting nodes");
FOREACH(&nodes, it)
node_start(it->node);
2015-03-31 18:13:43 +02:00
info("Starting paths");
FOREACH(&paths, it)
path_start(it->path);
/* Run! */
if (settings.stats > 0) {
stats_header();
2015-08-07 01:11:43 +02:00
2015-06-02 22:24:44 +02:00
for (;;) FOREACH(&paths, it) {
usleep(settings.stats * 1e6);
2015-09-19 12:20:17 +02:00
path_run_hook(it->path, HOOK_PERIODIC);
2015-06-02 22:24:44 +02:00
}
}
else
pause();
return 0;
}