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/utils.c

147 lines
2.9 KiB
C
Raw Normal View History

/** Some helper functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
#include <math.h>
#include "config.h"
#include "cfg.h"
#include "utils.h"
/* This global variable contains the debug level for debug() and assert() macros */
2014-12-05 12:17:27 +01:00
int _debug = V;
int _indent = 0;
struct timespec epoch;
void outdent(int *old)
{
_indent = *old;
}
void epoch_reset()
{
clock_gettime(CLOCK_REALTIME, &epoch);
}
void print(enum log_level lvl, const char *fmt, ...)
{
struct timespec ts;
va_list ap;
va_start(ap, fmt);
/* Timestamp */
2014-12-05 12:17:27 +01:00
clock_gettime(CLOCK_REALTIME, &ts);
fprintf(stderr, "%8.3f ", timespec_delta(&epoch, &ts));
switch (lvl) {
2014-12-05 12:17:27 +01:00
case DEBUG: fprintf(stderr, BLD("%-5s "), GRY("Debug")); break;
case INFO: fprintf(stderr, BLD("%-5s "), " " ); break;
case WARN: fprintf(stderr, BLD("%-5s "), YEL(" Warn")); break;
case ERROR: fprintf(stderr, BLD("%-5s "), RED("Error")); break;
}
2014-12-05 12:17:27 +01:00
if (_indent) {
for (int i = 0; i < _indent-1; i++)
fprintf(stderr, GFX("\x78") " ");
2014-12-05 12:17:27 +01:00
fprintf(stderr, GFX("\x74") " ");
}
2014-12-05 12:17:27 +01:00
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
2014-12-05 12:17:27 +01:00
va_end(ap);
}
cpu_set_t to_cpu_set(int set)
{
cpu_set_t cset;
CPU_ZERO(&cset);
for (int i = 0; i < sizeof(set) * 8; i++) {
if (set & (1L << i))
CPU_SET(i, &cset);
}
return cset;
}
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;
}
void hist_plot(unsigned *hist, int length)
{
char buf[HIST_HEIGHT + 32];
int bar;
int max = 0;
/* Get max, first & last */
for (int i = 0; i < length; i++) {
if (hist[i] > hist[max])
max = i;
}
/* Print header */
info("%2s | %5s | %s", "Id", "Value", "Histogram Plot:");
/* Print plot */
memset(buf, '#', sizeof(buf));
for (int i = 0; i < length; i++) {
bar = HIST_HEIGHT * (float) hist[i] / hist[max];
if (hist[i] == 0)
info("%2u | " GRN("%5u") " | " , i, hist[i]);
else if (hist[i] == hist[max])
info("%2u | " RED("%5u") " | " BLD("%.*s"), i, hist[i], bar, buf);
else
info("%2u | " "%5u" " | " "%.*s", i, hist[i], bar, buf);
}
}
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));
}
info("Matlab: hist = [ %s]", buf);
}