/** Some helper functions. * * @author Steffen Vogel * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC */ #include #include #include #include #include #include #include #include #include #include "config.h" #include "cfg.h" #include "utils.h" /* This global variable contains the debug level for debug() and assert() macros */ 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 */ clock_gettime(CLOCK_REALTIME, &ts); fprintf(stderr, "%8.3f ", timespec_delta(&epoch, &ts)); switch (lvl) { 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; } if (_indent) { for (int i = 0; i < _indent-1; i++) fprintf(stderr, GFX("\x78") " "); fprintf(stderr, GFX("\x74") " "); } vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); 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); }