2014-07-14 11:49:44 +00:00
|
|
|
/** Some helper functions.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-05 09:34:56 +00:00
|
|
|
#include <string.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
#include <time.h>
|
2014-06-05 09:34:56 +00:00
|
|
|
#include <errno.h>
|
2014-06-05 09:35:23 +00:00
|
|
|
#include <unistd.h>
|
2014-06-05 09:34:56 +00:00
|
|
|
#include <netdb.h>
|
2014-06-25 17:50:27 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <math.h>
|
2014-06-05 09:35:23 +00:00
|
|
|
|
2014-06-05 09:34:56 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2014-06-05 09:35:23 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-09-07 16:28:50 +00:00
|
|
|
/* This global variable contains the debug level for debug() and assert() macros */
|
|
|
|
int debug = V;
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
void print(enum log_level lvl, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
|
2014-06-25 01:53:40 +00:00
|
|
|
/* Timestamp */
|
|
|
|
printf("%15.4f", ts.tv_sec + ts.tv_nsec / 1e9);
|
|
|
|
|
|
|
|
switch (lvl) {
|
|
|
|
case DEBUG: printf(" [" BLU("Debug") "] "); break;
|
|
|
|
case INFO: printf(" [" WHT("Info ") "] "); break;
|
|
|
|
case WARN: printf(" [" YEL("Warn ") "] "); break;
|
|
|
|
case ERROR: printf(" [" RED("Error") "] "); break;
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
vprintf(fmt, ap);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
va_end(ap);
|
2014-06-05 09:34:56 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:33 +00:00
|
|
|
int resolve_addr(const char *addr, struct sockaddr_in *sa, int flags)
|
2014-06-05 09:34:56 +00:00
|
|
|
{
|
2014-06-25 01:53:42 +00:00
|
|
|
/* Split string */
|
|
|
|
char *tmp = strdup(addr);
|
|
|
|
char *node = strtok(tmp, ":");
|
|
|
|
char *service = strtok(NULL, ":");
|
2014-06-05 09:34:56 +00:00
|
|
|
|
2014-06-25 01:53:42 +00:00
|
|
|
if (node && !strcmp(node, "*"))
|
2014-06-05 09:34:56 +00:00
|
|
|
node = NULL;
|
|
|
|
|
2014-06-25 01:53:42 +00:00
|
|
|
if (service && !strcmp(service, "*"))
|
2014-06-05 09:34:56 +00:00
|
|
|
service = NULL;
|
|
|
|
|
|
|
|
/* Get IP */
|
|
|
|
struct addrinfo *result;
|
|
|
|
struct addrinfo hint = {
|
|
|
|
.ai_flags = flags,
|
|
|
|
.ai_family = AF_INET,
|
|
|
|
.ai_socktype = SOCK_DGRAM,
|
|
|
|
.ai_protocol = 0
|
|
|
|
};
|
|
|
|
|
2014-09-10 12:22:21 +00:00
|
|
|
int ret = getaddrinfo(node, service, &hint, &result);
|
|
|
|
if (!ret) {
|
|
|
|
memcpy(sa, result->ai_addr, result->ai_addrlen);
|
|
|
|
freeaddrinfo(result);
|
|
|
|
}
|
2014-06-05 09:34:56 +00:00
|
|
|
|
2014-06-25 01:53:42 +00:00
|
|
|
free(tmp);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-09-10 12:22:21 +00:00
|
|
|
return ret;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2014-06-05 09:35:23 +00:00
|
|
|
|
2014-06-25 01:53:44 +00:00
|
|
|
cpu_set_t to_cpu_set(int set)
|
2014-06-05 09:35:34 +00:00
|
|
|
{
|
|
|
|
cpu_set_t cset;
|
|
|
|
|
2014-06-25 01:53:44 +00:00
|
|
|
CPU_ZERO(&cset);
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(set) * 8; i++) {
|
2014-06-05 09:35:34 +00:00
|
|
|
if (set & (1L << i))
|
|
|
|
CPU_SET(i, &cset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cset;
|
|
|
|
}
|
2014-06-25 17:50:27 +00:00
|
|
|
|
|
|
|
double timespec_delta(struct timespec *start, struct timespec *end)
|
|
|
|
{
|
|
|
|
double sec = end->tv_sec - start->tv_sec;
|
|
|
|
double nsec = end->tv_nsec - start->tv_nsec;
|
|
|
|
|
|
|
|
if (nsec < 0) {
|
|
|
|
sec -= 1;
|
|
|
|
nsec += 1e9;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sec + nsec * 1e-9;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec timespec_rate(double rate)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = 1 / rate;
|
|
|
|
ts.tv_nsec = 1.0e9 * (1 / rate - ts.tv_sec);
|
|
|
|
|
|
|
|
return ts;
|
|
|
|
}
|
2014-09-09 09:03:11 +00:00
|
|
|
|
2014-09-09 11:11:29 +00:00
|
|
|
void hist_plot(unsigned *hist, int length)
|
2014-09-09 09:23:23 +00:00
|
|
|
{
|
2014-09-09 11:11:15 +00:00
|
|
|
char buf[HIST_HEIGHT + 32];
|
|
|
|
int bar;
|
2014-09-09 09:23:23 +00:00
|
|
|
int max = 0;
|
|
|
|
|
2014-09-09 11:11:15 +00:00
|
|
|
/* Get max, first & last */
|
2014-09-09 09:23:23 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (hist[i] > hist[max])
|
|
|
|
max = i;
|
|
|
|
}
|
|
|
|
|
2014-09-09 11:11:15 +00:00
|
|
|
|
|
|
|
/* Print header */
|
|
|
|
info("%2s | %5s | %s", "Id", "Value", "Histogram Plot:");
|
|
|
|
|
2014-09-09 09:23:23 +00:00
|
|
|
/* Print plot */
|
2014-09-09 11:11:15 +00:00
|
|
|
memset(buf, '#', sizeof(buf));
|
2014-09-09 09:23:23 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2014-09-09 11:11:15 +00:00
|
|
|
bar = HIST_HEIGHT * (float) hist[i] / hist[max];
|
2014-09-10 12:29:02 +00:00
|
|
|
if (hist[i] == 0)
|
2014-09-09 12:07:50 +00:00
|
|
|
info("%2u | " GRN("%5u") " | " , i, hist[i]);
|
2014-09-10 12:29:02 +00:00
|
|
|
else if (hist[i] == hist[max])
|
|
|
|
info("%2u | " RED("%5u") " | " BLD("%.*s"), i, hist[i], bar, buf);
|
2014-09-09 11:11:15 +00:00
|
|
|
else
|
|
|
|
info("%2u | " "%5u" " | " "%.*s", i, hist[i], bar, buf);
|
2014-09-09 09:23:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 09:03:11 +00:00
|
|
|
void hist_dump(unsigned *hist, int length)
|
|
|
|
{
|
|
|
|
char tok[16];
|
|
|
|
char buf[length * sizeof(tok)];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
/* Print in Matlab vector format */
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
snprintf(tok, sizeof(tok), "%u ", hist[i]);
|
|
|
|
strncat(buf, tok, sizeof(buf)-strlen(buf));
|
|
|
|
}
|
|
|
|
|
2014-09-09 11:11:15 +00:00
|
|
|
info("Matlab: hist = [ %s]", buf);
|
2014-09-09 09:03:11 +00:00
|
|
|
}
|