2014-07-14 11:49:44 +00:00
|
|
|
/** Main routine.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2014-12-05 12:41:06 +01:00
|
|
|
#include <string.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-08-22 17:42:38 +02:00
|
|
|
#include <sched.h>
|
|
|
|
|
2014-06-05 09:34:50 +00:00
|
|
|
#include "config.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "utils.h"
|
2015-05-06 11:43:05 +02:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "path.h"
|
|
|
|
#include "node.h"
|
2015-09-17 00:55:20 +02:00
|
|
|
#include "checks.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
#ifdef ENABLE_OPAL_ASYNC
|
2015-08-21 10:19:32 +02:00
|
|
|
#include "opal.h"
|
2015-03-17 22:46:46 +01:00
|
|
|
#endif
|
|
|
|
|
2015-10-11 13:22:58 +02:00
|
|
|
struct list nodes; /**< Linked list of nodes */
|
|
|
|
struct list paths; /**< Linked list of paths */
|
|
|
|
struct settings settings; /**< The global configuration */
|
|
|
|
static config_t config; /**< libconfig handle */
|
2014-06-10 16:57:40 +00:00
|
|
|
|
2014-12-05 12:41:06 +01:00
|
|
|
static void quit()
|
2015-03-21 18:03:29 +01:00
|
|
|
{
|
2015-03-31 18:13:43 +02:00
|
|
|
info("Stopping paths");
|
2015-12-11 17:56:14 +01:00
|
|
|
list_foreach(struct path *p, &paths) { INDENT
|
2015-10-09 12:50:35 +02:00
|
|
|
path_stop(p);
|
2015-12-11 17:56:14 +01:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-03-31 18:13:43 +02:00
|
|
|
info("Stopping nodes");
|
2015-12-11 17:56:14 +01:00
|
|
|
list_foreach(struct node *n, &nodes) { INDENT
|
2015-10-09 12:50:35 +02:00
|
|
|
node_stop(n);
|
2015-12-11 17:56:14 +01:00
|
|
|
}
|
2014-06-25 01:53:49 +00:00
|
|
|
|
2015-08-22 17:42:38 +02:00
|
|
|
info("De-initializing node types");
|
2015-12-11 17:56:14 +01:00
|
|
|
{ INDENT
|
|
|
|
node_deinit();
|
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2015-03-21 15:19:41 +01:00
|
|
|
/* Freeing dynamically allocated memory */
|
|
|
|
list_destroy(&paths);
|
|
|
|
list_destroy(&nodes);
|
2014-06-10 16:57:40 +00:00
|
|
|
config_destroy(&config);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-13 16:11:58 +02:00
|
|
|
info(GRN("Goodbye!"));
|
2015-03-21 18:03:55 +01:00
|
|
|
|
2014-06-05 09:34:45 +00:00
|
|
|
_exit(EXIT_SUCCESS);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 22:04:03 +02:00
|
|
|
static void realtime_init()
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-09-30 11:12:16 +02:00
|
|
|
if (check_kernel_cmdline())
|
|
|
|
warn("You should reserve some cores for the server (see 'isolcpus')");
|
2015-10-09 15:48:55 +02:00
|
|
|
if (check_kernel_rt())
|
|
|
|
warn("We recommend to use an PREEMPT_RT patched kernel!");
|
2015-09-30 11:12:16 +02:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
/* Use FIFO scheduler with real time priority */
|
2014-07-04 09:44:12 +00:00
|
|
|
if (settings.priority) {
|
2015-08-22 17:42:38 +02:00
|
|
|
struct sched_param param = {
|
|
|
|
.sched_priority = settings.priority
|
|
|
|
};
|
|
|
|
|
2014-07-04 09:44:12 +00:00
|
|
|
if (sched_setscheduler(0, SCHED_FIFO, ¶m))
|
2015-03-20 18:55:14 +01:00
|
|
|
serror("Failed to set real time priority");
|
2015-09-30 11:12:16 +02:00
|
|
|
|
|
|
|
debug(3, "Set task priority to %u", settings.priority);
|
2014-07-04 09:44:12 +00:00
|
|
|
}
|
2015-09-30 11:12:16 +02:00
|
|
|
warn("Use setting 'priority' to enable real-time scheduling!");
|
2014-06-10 19:44:22 +00:00
|
|
|
|
|
|
|
/* Pin threads to CPUs by setting the affinity */
|
2014-07-04 09:44:12 +00:00
|
|
|
if (settings.affinity) {
|
|
|
|
cpu_set_t cset = to_cpu_set(settings.affinity);
|
|
|
|
if (sched_setaffinity(0, sizeof(cset), &cset))
|
2014-12-09 15:39:17 +00:00
|
|
|
serror("Failed to set CPU affinity to '%#x'", settings.affinity);
|
2015-09-30 11:12:16 +02:00
|
|
|
|
|
|
|
debug(3, "Set affinity to %#x", settings.affinity);
|
2014-07-04 09:44:12 +00:00
|
|
|
}
|
2015-09-30 11:12:16 +02:00
|
|
|
warn("Use setting 'affinity' to pin process to isolated CPU cores!");
|
2014-12-05 12:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup exit handler */
|
2015-06-02 22:04:03 +02:00
|
|
|
static void signals_init()
|
2015-03-31 14:28:00 +02:00
|
|
|
{
|
2014-12-05 12:41:06 +01: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);
|
2014-12-05 12:41:06 +01:00
|
|
|
}
|
|
|
|
|
2015-06-02 22:04:03 +02:00
|
|
|
static void usage(const char *name)
|
2014-12-05 12:41:06 +01:00
|
|
|
{
|
|
|
|
printf("Usage: %s CONFIG\n", name);
|
|
|
|
printf(" CONFIG is a required path to a configuration file\n\n");
|
2015-03-17 22:46:46 +01:00
|
|
|
#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
|
2015-09-21 17:12:40 +02:00
|
|
|
printf("Supported node types:\n");
|
2015-11-23 16:44:01 +01:00
|
|
|
list_foreach(struct node_type *vt, &node_types)
|
|
|
|
printf(" - %s: %s\n", vt->name, vt->description);
|
2015-08-22 17:42:38 +02:00
|
|
|
printf("\n");
|
2015-03-21 18:16:28 +01:00
|
|
|
printf("Simulator2Simulator Server %s (built on %s %s)\n",
|
2014-12-05 12:41:06 +01:00
|
|
|
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");
|
2015-03-21 18:16:28 +01:00
|
|
|
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
2014-12-05 12:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
/* Check arguments */
|
2015-03-17 22:46:46 +01:00
|
|
|
#ifdef ENABLE_OPAL_ASYNC
|
|
|
|
if (argc != 2 && argc != 4)
|
2015-03-12 22:56:58 +01:00
|
|
|
#else
|
2014-12-05 12:41:06 +01:00
|
|
|
if (argc != 2)
|
2015-03-12 22:56:58 +01:00
|
|
|
#endif
|
2014-12-05 12:41:06 +01:00
|
|
|
usage(argv[0]);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-31 13:54:04 +02:00
|
|
|
char *configfile = (argc == 2) ? argv[1] : "opal-shmem.conf";
|
2015-03-21 18:03:29 +01:00
|
|
|
|
2015-10-11 13:25:06 +02:00
|
|
|
log_init();
|
2015-11-29 22:50:39 +01:00
|
|
|
info("This is Simulator2Simulator Server (S2SS) %s (built on %s, %s)", BLD(YEL(VERSION)),
|
2015-09-17 00:58:04 +02:00
|
|
|
BLD(MAG(__DATE__)), BLD(MAG(__TIME__)));
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-09-17 00:55:20 +02:00
|
|
|
/* Checks system requirements*/
|
|
|
|
if (check_kernel_version())
|
|
|
|
error("Your kernel version is to old: required >= %u.%u", KERNEL_VERSION_MAJ, KERNEL_VERSION_MIN);
|
2015-05-07 19:02:12 +02:00
|
|
|
|
2015-03-21 15:23:57 +01:00
|
|
|
/* 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");
|
2015-12-11 17:56:14 +01:00
|
|
|
{ INDENT
|
|
|
|
realtime_init();
|
|
|
|
}
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2015-03-31 18:13:43 +02:00
|
|
|
info("Initialize signals");
|
2015-12-11 17:56:14 +01:00
|
|
|
{ INDENT
|
|
|
|
signals_init();
|
|
|
|
}
|
2015-03-21 15:29:00 +01:00
|
|
|
|
2015-03-31 18:13:43 +02:00
|
|
|
info("Parsing configuration");
|
2015-12-11 17:56:14 +01:00
|
|
|
{ INDENT
|
|
|
|
config_init(&config);
|
|
|
|
config_parse(configfile, &config, &settings, &nodes, &paths);
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:49:13 +02:00
|
|
|
info("Initialize node types");
|
2015-12-11 17:56:14 +01:00
|
|
|
{ INDENT
|
2015-12-11 12:35:32 +01:00
|
|
|
node_init(argc, argv, config_root_setting(&config));
|
2015-12-11 17:56:14 +01:00
|
|
|
}
|
2014-12-05 12:41:06 +01:00
|
|
|
|
2015-03-31 18:13:43 +02:00
|
|
|
info("Starting nodes");
|
2015-12-11 17:56:14 +01:00
|
|
|
list_foreach(struct node *n, &nodes) { INDENT
|
2015-11-23 16:32:24 +01:00
|
|
|
int used_by_path(struct path *p, struct node *n) {
|
|
|
|
return (p->in == n) || list_contains(&p->destinations, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
int refs = list_count(&paths, (cmp_cb_t) used_by_path, n);
|
2015-12-04 01:54:33 +01:00
|
|
|
if (refs > 0)
|
2015-11-23 16:32:24 +01:00
|
|
|
node_start(n);
|
|
|
|
else
|
2015-12-11 17:56:14 +01:00
|
|
|
warn("No path is using the node %s. Skipping...", node_name(n));
|
2015-11-23 16:32:24 +01:00
|
|
|
}
|
2014-06-05 09:35:10 +00:00
|
|
|
|
2015-03-31 18:13:43 +02:00
|
|
|
info("Starting paths");
|
2015-12-11 17:56:14 +01:00
|
|
|
list_foreach(struct path *p, &paths) { INDENT
|
2015-11-23 16:32:24 +01:00
|
|
|
if (p->enabled)
|
|
|
|
path_start(p);
|
|
|
|
else
|
2015-11-29 22:45:46 +01:00
|
|
|
warn("Path %s is disabled. Skipping...", path_name(p));
|
2015-11-23 16:32:24 +01:00
|
|
|
}
|
2014-12-05 12:41:06 +01:00
|
|
|
|
|
|
|
/* Run! */
|
2014-09-07 16:28:54 +00:00
|
|
|
if (settings.stats > 0) {
|
2015-10-12 16:16:25 +02:00
|
|
|
hook_stats_header();
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-10-14 14:52:29 +02:00
|
|
|
for (;;) {
|
|
|
|
list_foreach(struct path *p, &paths)
|
|
|
|
path_run_hook(p, HOOK_PERIODIC);
|
2014-09-07 16:28:54 +00:00
|
|
|
usleep(settings.stats * 1e6);
|
2015-10-14 14:52:29 +02:00
|
|
|
}
|
2014-07-04 09:47:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pause();
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|