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: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 */
|
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);
|
|
|
|
}
|
2014-09-07 16:28:50 +00:00
|
|
|
|
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);
|
|
|
|
|
2014-06-25 01:53:40 +00:00
|
|
|
/* Timestamp */
|
2014-12-05 12:17:27 +01:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
fprintf(stderr, "%8.3f ", timespec_delta(&epoch, &ts));
|
2014-06-25 01:53:40 +00:00
|
|
|
|
|
|
|
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-06-25 01:53:40 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 12:17:27 +01:00
|
|
|
if (_indent) {
|
|
|
|
for (int i = 0; i < _indent-1; i++)
|
|
|
|
fprintf(stderr, GFX("\x78") " ");
|
2014-06-05 09:34:56 +00:00
|
|
|
|
2014-12-05 12:17:27 +01:00
|
|
|
fprintf(stderr, GFX("\x74") " ");
|
2014-11-10 12:15:17 +01:00
|
|
|
}
|
2014-06-05 09:34:56 +00:00
|
|
|
|
2014-12-05 12:17:27 +01:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:17:27 +01:00
|
|
|
va_end(ap);
|
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
|
|
|
}
|