2015-06-05 12:15:31 +02:00
|
|
|
/** General purpose helper functions.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-06-19 19:23:19 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdarg.h>
|
2014-06-05 09:34:56 +00:00
|
|
|
#include <string.h>
|
2014-06-05 09:35:23 +00:00
|
|
|
#include <unistd.h>
|
2014-06-25 17:50:27 +00:00
|
|
|
#include <math.h>
|
2015-05-06 11:28:08 +02:00
|
|
|
#include <pthread.h>
|
2016-06-19 19:23:19 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
2015-03-17 22:44:09 +01:00
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2016-01-15 15:25:22 +01:00
|
|
|
void print_copyright()
|
|
|
|
{
|
2016-06-08 23:21:42 +02:00
|
|
|
printf("VILLASnode %s (built on %s %s)\n",
|
2017-03-29 04:04:20 +02:00
|
|
|
BLU(BUILDID), MAG(__DATE__), MAG(__TIME__));
|
|
|
|
printf(" Copyright 2014-2017, Institute for Automation of Complex Power Systems, EONERC\n");
|
2016-01-15 15:25:22 +01:00
|
|
|
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:55:20 +02:00
|
|
|
int version_parse(const char *s, struct version *v)
|
|
|
|
{
|
|
|
|
return sscanf(s, "%u.%u", &v->major, &v->minor) != 2;
|
|
|
|
}
|
|
|
|
|
2016-10-30 18:38:47 -04:00
|
|
|
int version_cmp(struct version *a, struct version *b) {
|
2015-09-17 00:55:20 +02:00
|
|
|
int major = a->major - b->major;
|
|
|
|
int minor = a->minor - b->minor;
|
|
|
|
|
|
|
|
return major ? major : minor;
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:53:39 +02:00
|
|
|
double box_muller(float m, float s)
|
|
|
|
{
|
|
|
|
double x1, x2, y1;
|
|
|
|
static double y2;
|
|
|
|
static int use_last = 0;
|
|
|
|
|
|
|
|
if (use_last) { /* use value from previous call */
|
|
|
|
y1 = y2;
|
|
|
|
use_last = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
double w;
|
|
|
|
do {
|
|
|
|
x1 = 2.0 * randf() - 1.0;
|
|
|
|
x2 = 2.0 * randf() - 1.0;
|
|
|
|
w = x1*x1 + x2*x2;
|
|
|
|
} while (w >= 1.0);
|
|
|
|
|
|
|
|
w = sqrt(-2.0 * log(w) / w);
|
|
|
|
y1 = x1 * w;
|
|
|
|
y2 = x2 * w;
|
|
|
|
use_last = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m + y1 * s;
|
|
|
|
}
|
|
|
|
|
|
|
|
double randf()
|
|
|
|
{
|
|
|
|
return (double) random() / RAND_MAX;
|
|
|
|
}
|
|
|
|
|
2015-03-21 18:03:29 +01:00
|
|
|
void die()
|
2014-12-05 12:17:27 +01:00
|
|
|
{
|
2015-11-29 21:20:42 +01:00
|
|
|
int zero = 0;
|
|
|
|
log_outdent(&zero);
|
|
|
|
abort();
|
2014-12-05 12:17:27 +01:00
|
|
|
}
|
2014-09-07 16:28:50 +00:00
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
char * strcatf(char **dest, const char *fmt, ...)
|
2015-03-18 15:38:06 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2015-09-22 12:58:37 +02:00
|
|
|
vstrcatf(dest, fmt, ap);
|
2015-03-18 15:38:06 +01:00
|
|
|
va_end(ap);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
return *dest;
|
2015-03-18 15:38:06 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
char * vstrcatf(char **dest, const char *fmt, va_list ap)
|
2015-03-18 15:38:06 +01:00
|
|
|
{
|
2015-09-22 12:58:37 +02:00
|
|
|
char *tmp;
|
|
|
|
int n = *dest ? strlen(*dest) : 0;
|
|
|
|
int i = vasprintf(&tmp, fmt, ap);
|
|
|
|
|
|
|
|
*dest = (char *)(realloc(*dest, n + i + 1));
|
|
|
|
if (*dest != NULL)
|
|
|
|
strncpy(*dest+n, tmp, i + 1);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
free(tmp);
|
2015-03-18 15:38:06 +01:00
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
return *dest;
|
2015-03-18 15:38:06 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 08:10:00 +02:00
|
|
|
void cpuset_to_integer(cpu_set_t *cset, uintmax_t *set)
|
|
|
|
{
|
|
|
|
*set = 0;
|
|
|
|
for (int i = 0; i < MIN(sizeof(*set) * 8, CPU_SETSIZE); i++) {
|
|
|
|
if (CPU_ISSET(i, cset))
|
|
|
|
*set |= 1ULL << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:33:59 +02:00
|
|
|
void cpuset_from_integer(uintmax_t set, cpu_set_t *cset)
|
2014-06-05 09:35:34 +00:00
|
|
|
{
|
2016-06-26 15:33:59 +02:00
|
|
|
CPU_ZERO(cset);
|
2017-03-29 08:10:00 +02:00
|
|
|
for (int i = 0; i < MIN(sizeof(set) * 8, CPU_SETSIZE); i++) {
|
2016-06-26 15:33:59 +02:00
|
|
|
if (set & (1L << i))
|
|
|
|
CPU_SET(i, cset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* From: https://github.com/mmalecki/util-linux/blob/master/lib/cpuset.c */
|
|
|
|
static const char *nexttoken(const char *q, int sep)
|
|
|
|
{
|
|
|
|
if (q)
|
|
|
|
q = strchr(q, sep);
|
|
|
|
if (q)
|
|
|
|
q++;
|
|
|
|
return q;
|
|
|
|
}
|
2014-06-05 09:35:34 +00:00
|
|
|
|
2016-06-26 15:33:59 +02:00
|
|
|
int cpulist_parse(const char *str, cpu_set_t *set, int fail)
|
|
|
|
{
|
|
|
|
const char *p, *q;
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
q = str;
|
|
|
|
CPU_ZERO(set);
|
|
|
|
|
|
|
|
while (p = q, q = nexttoken(q, ','), p) {
|
|
|
|
unsigned int a; /* beginning of range */
|
|
|
|
unsigned int b; /* end of range */
|
|
|
|
unsigned int s; /* stride */
|
|
|
|
const char *c1, *c2;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if ((r = sscanf(p, "%u%c", &a, &c)) < 1)
|
|
|
|
return 1;
|
|
|
|
b = a;
|
|
|
|
s = 1;
|
|
|
|
|
|
|
|
c1 = nexttoken(p, '-');
|
|
|
|
c2 = nexttoken(p, ',');
|
|
|
|
if (c1 != NULL && (c2 == NULL || c1 < c2)) {
|
|
|
|
if ((r = sscanf(c1, "%u%c", &b, &c)) < 1)
|
|
|
|
return 1;
|
|
|
|
c1 = nexttoken(c1, ':');
|
|
|
|
if (c1 != NULL && (c2 == NULL || c1 < c2)) {
|
|
|
|
if ((r = sscanf(c1, "%u%c", &s, &c)) < 1)
|
|
|
|
return 1;
|
|
|
|
if (s == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 01:53:44 +00:00
|
|
|
|
2016-06-26 15:33:59 +02:00
|
|
|
if (!(a <= b))
|
|
|
|
return 1;
|
|
|
|
while (a <= b) {
|
|
|
|
if (fail && (a >= CPU_SETSIZE))
|
|
|
|
return 2;
|
|
|
|
CPU_SET(a, set);
|
|
|
|
a += s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 2)
|
|
|
|
return 1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-26 15:33:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *cpulist_create(char *str, size_t len, cpu_set_t *set)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char *ptr = str;
|
|
|
|
int entry_made = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < CPU_SETSIZE; i++) {
|
|
|
|
if (CPU_ISSET(i, set)) {
|
|
|
|
int rlen;
|
|
|
|
size_t j, run = 0;
|
|
|
|
entry_made = 1;
|
|
|
|
for (j = i + 1; j < CPU_SETSIZE; j++) {
|
|
|
|
if (CPU_ISSET(j, set))
|
|
|
|
run++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!run)
|
|
|
|
rlen = snprintf(ptr, len, "%zd,", i);
|
|
|
|
else if (run == 1) {
|
|
|
|
rlen = snprintf(ptr, len, "%zd,%zd,", i, i + 1);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
rlen = snprintf(ptr, len, "%zd-%zd,", i, i + run);
|
|
|
|
i += run;
|
|
|
|
}
|
|
|
|
if (rlen < 0 || (size_t) rlen + 1 > len)
|
|
|
|
return NULL;
|
|
|
|
ptr += rlen;
|
|
|
|
if (rlen > 0 && len > (size_t) rlen)
|
|
|
|
len -= rlen;
|
|
|
|
else
|
|
|
|
len = 0;
|
|
|
|
}
|
2014-06-05 09:35:34 +00:00
|
|
|
}
|
2016-06-26 15:33:59 +02:00
|
|
|
ptr -= entry_made;
|
|
|
|
*ptr = '\0';
|
2014-06-05 09:35:34 +00:00
|
|
|
|
2016-06-26 15:33:59 +02:00
|
|
|
return str;
|
2014-06-05 09:35:34 +00:00
|
|
|
}
|
2014-06-25 17:50:27 +00:00
|
|
|
|
2015-03-17 23:20:47 +01:00
|
|
|
void * alloc(size_t bytes)
|
|
|
|
{
|
|
|
|
void *p = malloc(bytes);
|
|
|
|
if (!p)
|
|
|
|
error("Failed to allocate memory");
|
|
|
|
|
|
|
|
memset(p, 0, bytes);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 23:20:47 +01:00
|
|
|
return p;
|
|
|
|
}
|
2015-10-07 09:12:56 +02:00
|
|
|
|
|
|
|
void * memdup(const void *src, size_t bytes)
|
|
|
|
{
|
|
|
|
void *dst = alloc(bytes);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-07 09:12:56 +02:00
|
|
|
memcpy(dst, src, bytes);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2015-10-07 09:12:56 +02:00
|
|
|
return dst;
|
2016-06-14 01:19:17 +02:00
|
|
|
}
|
|
|
|
|
2017-02-12 14:36:44 -03:00
|
|
|
ssize_t read_random(char *buf, size_t len)
|
2016-06-14 01:19:17 +02:00
|
|
|
{
|
2016-07-08 12:15:37 +02:00
|
|
|
int fd;
|
2017-02-12 14:36:44 -03:00
|
|
|
ssize_t bytes, total;
|
|
|
|
|
2016-06-14 01:19:17 +02:00
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
2017-02-12 14:36:44 -03:00
|
|
|
|
|
|
|
bytes = 0;
|
|
|
|
total = 0;
|
2016-07-08 12:15:37 +02:00
|
|
|
while (total < len) {
|
|
|
|
bytes = read(fd, buf + total, len - total);
|
|
|
|
if (bytes < 0)
|
2017-02-12 14:36:44 -03:00
|
|
|
break;
|
2016-06-14 01:19:17 +02:00
|
|
|
|
2016-07-08 12:15:37 +02:00
|
|
|
total += bytes;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-07-08 12:15:37 +02:00
|
|
|
close(fd);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-12 14:36:44 -03:00
|
|
|
return bytes;
|
2016-06-14 01:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rdtsc_sleep(uint64_t nanosecs, uint64_t start)
|
|
|
|
{
|
|
|
|
uint64_t cycles;
|
|
|
|
|
2016-10-19 01:25:05 -04:00
|
|
|
/** @todo Replace the hard coded CPU clock frequency */
|
2016-06-14 01:19:17 +02:00
|
|
|
cycles = (double) nanosecs / (1e9 / 3392389000);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-14 01:19:17 +02:00
|
|
|
if (start == 0)
|
2016-10-19 01:25:05 -04:00
|
|
|
start = rdtsc();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-06-14 01:19:17 +02:00
|
|
|
do {
|
|
|
|
__asm__("nop");
|
2016-10-19 01:25:05 -04:00
|
|
|
} while (rdtsc() - start < cycles);
|
2017-03-06 19:09:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup exit handler */
|
|
|
|
void signals_init(void (*cb)(int signal, siginfo_t *sinfo, void *ctx))
|
|
|
|
{
|
|
|
|
info("Initialize signals");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-06 19:09:44 -04:00
|
|
|
struct sigaction sa_quit = {
|
|
|
|
.sa_flags = SA_SIGINFO,
|
|
|
|
.sa_sigaction = cb
|
|
|
|
};
|
|
|
|
|
|
|
|
sigemptyset(&sa_quit.sa_mask);
|
|
|
|
sigaction(SIGINT, &sa_quit, NULL);
|
|
|
|
sigaction(SIGTERM, &sa_quit, NULL);
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-12 17:07:59 +02:00
|
|
|
struct sigaction sa_chld = {
|
|
|
|
.sa_flags = 0,
|
|
|
|
.sa_handler = SIG_IGN
|
|
|
|
};
|
2017-04-15 18:59:22 +02:00
|
|
|
|
2017-04-12 17:07:59 +02:00
|
|
|
sigaction(SIGCHLD, &sa_chld, NULL);
|
2017-03-14 01:46:44 -03:00
|
|
|
}
|
|
|
|
|
2017-04-15 18:22:47 +02:00
|
|
|
pid_t spawn(const char* name, char *const argv[])
|
|
|
|
{
|
|
|
|
pid_t pid;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 18:22:47 +02:00
|
|
|
pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case -1: return -1;
|
|
|
|
case 0: return execvp(name, (char * const*) argv);
|
2017-04-12 17:07:59 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 18:22:47 +02:00
|
|
|
return pid;
|
2017-04-12 17:07:59 +02:00
|
|
|
}
|