Commit a4dc8b8c authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

use C++ compiler

parent a72a0e23
......@@ -121,7 +121,7 @@ int vlist_contains(struct vlist *l, void *p);
void vlist_sort(struct vlist *l, cmp_cb_t cmp);
/** Set single element in list */
int vlist_set(struct vlist *l, int index, void *value);
int vlist_set(struct vlist *l, unsigned index, void *value);
/** Return index in list for value.
*
......
......@@ -27,38 +27,72 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <vector>
#include <string>
struct table_column {
int width; /**< Width of the column. */
char *title; /**< The title as shown in the table header. */
char *format; /**< The format which is used to print the table rows. */
char *unit; /**< An optional unit which will be shown in the table header. */
class Table;
class TableColumn {
friend Table;
enum {
TABLE_ALIGN_LEFT,
TABLE_ALIGN_RIGHT
} align;
public:
enum align {
LEFT,
RIGHT
};
protected:
int _width; /**< The real width of this column. Calculated by table_header() */
int width; /**< Width of the column. */
public:
TableColumn(int w, enum align a, const std::string &t, const std::string &f, const std::string &u = "") :
width(w),
title(t),
format(f),
unit(u),
align(a)
{ }
std::string title; /**< The title as shown in the table header. */
std::string format; /**< The format which is used to print the table rows. */
std::string unit; /**< An optional unit which will be shown in the table header. */
enum align align;
int getWidth() const
{
return _width;
}
};
struct table {
int ncols;
class Table {
protected:
int resize(int w);
int width;
struct table_column *cols;
};
/** Print a table header consisting of \p n columns. */
void table_header(struct table *t);
std::vector<TableColumn> columns;
/** Print table rows. */
void table_row(struct table *t, ...);
public:
Table(const std::vector<TableColumn> &cols) :
width(-1),
columns(cols)
{ }
/** @} */
/** Print a table header consisting of \p n columns. */
void header();
#ifdef __cplusplus
}
#endif
/** Print table rows. */
void row(int count, ...);
int getWidth() const
{
return width;
}
};
/** @} */
\ No newline at end of file
......@@ -21,34 +21,33 @@
##############################################################################
add_library(villas-common SHARED
advio.c
bitset.c
buffer.c
advio.cpp
bitset.cpp
buffer.cpp
json_buffer.cpp
compat.c
crypt.c
hash_table.c
hist.c
kernel/kernel.c
compat.cpp
crypt.cpp
hash_table.cpp
hist.cpp
kernel/kernel.cpp
kernel/kernel.cpp
kernel/rt.cpp
list.c
list.cpp
log.cpp
log_legacy.cpp
memory.cpp
memory_manager.cpp
plugin.cpp
table.c
task.c
timing.c
utils.c
table.cpp
task.cpp
timing.cpp
utils.cpp
cpuset.cpp
terminal.cpp
version.cpp
copyright.cpp
common.c
window.c
common.cpp
window.cpp
)
execute_process(
......@@ -58,13 +57,14 @@ execute_process(
)
if(ARCH STREQUAL "x86_64")
target_sources(villas-common PRIVATE tsc.c)
target_sources(villas-common PRIVATE tsc.cpp)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
target_sources(villas-common PRIVATE
kernel/pci.c
kernel/pci.cpp
kernel/vfio.cpp
# kernel/vfio_legacy.cpp
)
endif()
......
......@@ -39,7 +39,7 @@
#define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */
static int advio_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
static int advio_trace(CURL * /* handle */, curl_infotype type, char *data, size_t size, void * /* userp */)
{
const char *text;
......@@ -83,7 +83,7 @@ static int advio_trace(CURL *handle, curl_infotype type, char *data, size_t size
static char * advio_human_time(double t, char *buf, size_t len)
{
int i = 0;
unsigned i = 0;
const char *units[] = { "secs", "mins", "hrs", "days", "weeks", "months", "years" };
int divs[] = { 60, 60, 24, 7, 4, 12 };
......@@ -99,7 +99,7 @@ static char * advio_human_time(double t, char *buf, size_t len)
static char * advio_human_size(double s, char *buf, size_t len)
{
int i = 0;
unsigned i = 0;
const char *units[] = { "B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
while (s > 1024 && i < ARRAY_LEN(units)) {
......@@ -180,14 +180,14 @@ static int advio_xferinfo(void *p, curl_off_t dl_total_bytes, curl_off_t dl_byte
int aislocal(const char *uri)
{
char *sep;
const char *sep;
const char *supported_schemas[] = { "file", "http", "https", "tftp", "ftp", "scp", "sftp", "smb", "smbs" };
sep = strstr(uri, "://");
if (!sep)
return 1; /* no schema, we assume its a local file */
for (int i = 0; i < ARRAY_LEN(supported_schemas); i++) {
for (unsigned i = 0; i < ARRAY_LEN(supported_schemas); i++) {
if (!strncmp(supported_schemas[i], uri, sep - uri))
return 0;
}
......@@ -198,9 +198,10 @@ int aislocal(const char *uri)
AFILE * afopen(const char *uri, const char *mode)
{
int ret;
char *sep, *cwd;
char *cwd;
const char *sep;
AFILE *af = alloc(sizeof(AFILE));
AFILE *af = (AFILE *) alloc(sizeof(AFILE));
snprintf(af->mode, sizeof(af->mode), "%s", mode);
......@@ -290,29 +291,29 @@ int afclose(AFILE *af)
int afseek(AFILE *af, long offset, int origin)
{
long new, cur = aftell(af);
long new_seek, cur_seek = aftell(af);
switch (origin) {
case SEEK_SET:
new = offset;
new_seek = offset;
break;
case SEEK_END:
fseek(af->file, 0, SEEK_END);
new = aftell(af);
fseek(af->file, cur, SEEK_SET);
new_seek = aftell(af);
fseek(af->file, cur_seek, SEEK_SET);
break;
case SEEK_CUR:
new = cur + offset;
new_seek = cur_seek + offset;
break;
default:
return -1;
}
if (new < af->uploaded)
af->uploaded = new;
if (new_seek < af->uploaded)
af->uploaded = new_seek;
return fseek(af->file, offset, origin);
}
......@@ -403,6 +404,7 @@ int adownload(AFILE *af, int resume)
double total_bytes = 0, total_time = 0;
char buf[2][32];
char *total_bytes_human, *total_time_human;
pos = aftell(af);
......@@ -428,8 +430,8 @@ int adownload(AFILE *af, int resume)
curl_easy_getinfo(af->curl, CURLINFO_SIZE_DOWNLOAD, &total_bytes);
curl_easy_getinfo(af->curl, CURLINFO_TOTAL_TIME, &total_time);
char *total_bytes_human = advio_human_size(total_bytes, buf[0], sizeof(buf[0]));
char *total_time_human = advio_human_time(total_time, buf[1], sizeof(buf[1]));
total_bytes_human = advio_human_size(total_bytes, buf[0], sizeof(buf[0]));
total_time_human = advio_human_time(total_time, buf[1], sizeof(buf[1]));
info("Finished download of %s in %s", total_bytes_human, total_time_human);
......@@ -460,6 +462,7 @@ int adownload(AFILE *af, int resume)
af->file = fopen(af->uri, af->mode);
if (!af->file)
return -1;
break;
default:
error("Failed to download file: %s: %s", af->uri, curl_easy_strerror(res));
......
......@@ -34,7 +34,7 @@ int bitset_init(struct bitset *b, size_t dim)
{
int s = bitset_nslots(dim);
b->set = alloc(s * CHAR_BIT);
b->set = (char *) alloc(s * CHAR_BIT);
b->dimension = dim;
return 0;
......@@ -163,7 +163,7 @@ char * bitset_dump(struct bitset *b)
{
char *str = NULL;
for (int i = 0; i < b->dimension; i++)
for (unsigned i = 0; i < b->dimension; i++)
strcatf(&str, "%d", bitset_test(b, i));
return str;
......
......@@ -30,7 +30,7 @@ int buffer_init(struct buffer *b, size_t size)
{
b->len = 0;
b->size = size;
b->buf = malloc(size);
b->buf = (char *) malloc(size);
if (!b->buf)
return -1;
......@@ -58,7 +58,7 @@ int buffer_append(struct buffer *b, const char *data, size_t len)
{
if (b->len + len > b->size) {
b->size = b->len + len;
b->buf = realloc(b->buf, b->size);
b->buf = (char *) realloc(b->buf, b->size);
if (!b->buf)
return -1;
}
......@@ -85,7 +85,7 @@ int buffer_append_json(struct buffer *b, json_t *j)
retry: len = json_dumpb(j, b->buf + b->len, b->size - b->len, 0);
if (b->size < b->len + len) {
b->buf = realloc(b->buf, b->len + len);
b->buf = (char *) realloc(b->buf, b->len + len);
if (!b->buf)
return -1;
......
......@@ -43,7 +43,7 @@ int hash_table_init(struct hash_table *ht, size_t size)
if (ret)
return ret;
ht->table = alloc(len);
ht->table = (struct hash_table_entry **) alloc(len);
memset(ht->table, 0, len);
......@@ -62,7 +62,7 @@ int hash_table_destroy(struct hash_table *ht, dtor_cb_t dtor, bool release)
pthread_mutex_lock(&ht->lock);
for (int i = 0; i < ht->size; i++) {
for (unsigned i = 0; i < ht->size; i++) {
for (cur = ht->table[i]; cur; cur = next) {
if (dtor)
dtor(cur->data);
......@@ -106,7 +106,7 @@ int hash_table_insert(struct hash_table *ht, const void *key, void *data)
if (cur)
ret = -1;
else {
hte = alloc(sizeof(struct hash_table_entry));
hte = (struct hash_table_entry *) alloc(sizeof(struct hash_table_entry));
if (hte) {
hte->key = key;
hte->data = data;
......@@ -191,7 +191,7 @@ void hash_table_dump(struct hash_table *ht)
pthread_mutex_lock(&ht->lock);
for (int i = 0; i < ht->size; i++) {
for (unsigned i = 0; i < ht->size; i++) {
char *strlst = NULL;
for (hte = ht->table[i]; hte; hte = hte->next)
......
......@@ -28,9 +28,9 @@
#include <time.h>
#include <villas/utils.h>
#include <villas/hist.h>
#include <villas/hist.hpp>
#include <villas/config.h>
#include <villas/table.h>
#include <villas/table.hpp>
#define VAL(h, i) ((h)->low + (i) * (h)->resolution)
#define INDEX(h, v) round((v - (h)->low) / (h)->resolution)
......@@ -40,7 +40,7 @@ int hist_init(struct hist *h, int buckets, hist_cnt_t warmup)
h->length = buckets;
h->warmup = warmup;
h->data = buckets ? alloc(h->length * sizeof(hist_cnt_t)) : NULL;
h->data = (hist_cnt_t *) (buckets ? alloc(h->length * sizeof(hist_cnt_t)) : nullptr);
hist_reset(h);
......@@ -51,7 +51,7 @@ int hist_destroy(struct hist *h)
{
if (h->data) {
free(h->data);
h->data = NULL;
h->data = nullptr;
}
return 0;
......@@ -83,7 +83,7 @@ void hist_put(struct hist *h, double value)
h->higher++;
else if (idx < 0)
h->lower++;
else if (h->data != NULL)
else if (h->data != nullptr)
h->data[idx]++;
}
......@@ -169,30 +169,27 @@ void hist_plot(const struct hist *h)
max = h->data[i];
}
struct table_column cols[] = {
{ -9, "Value", "%+9.3g", NULL, TABLE_ALIGN_RIGHT },
{ -6, "Count", "%6ju", NULL, TABLE_ALIGN_RIGHT },
{ 0, "Plot", "%s", "occurences", TABLE_ALIGN_LEFT }
std::vector<TableColumn> cols = {
{ -9, TableColumn::align::RIGHT, "Value", "%+9.3g" },
{ -6, TableColumn::align::RIGHT, "Count", "%6ju" },
{ 0, TableColumn::align::LEFT, "Plot", "%s", "occurences" }
};
struct table table = {
.ncols = ARRAY_LEN(cols),
.cols = cols
};
Table table = Table(cols);
/* Print plot */
table_header(&table);
table.header();
for (int i = 0; i < h->length; i++) {
double value = VAL(h, i);
hist_cnt_t cnt = h->data[i];
int bar = cols[2]._width * ((double) cnt / max);
int bar = cols[2].getWidth() * ((double) cnt / max);
char *buf = strf("%s", "");
for (int i = 0; i < bar; i++)
buf = strcatf(&buf, "\u2588");
table_row(&table, value, cnt, buf);
table.row(value, cnt, buf);
free(buf);
}
......@@ -200,7 +197,7 @@ void hist_plot(const struct hist *h)
char * hist_dump(const struct hist *h)
{
char *buf = alloc(128);
char *buf = (char *) alloc(128);
strcatf(&buf, "[ ");
......
/** Linux kernel related functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <villas/utils.h>
#include <villas/config.h>
#include <villas/kernel/kernel.h>
int kernel_get_cacheline_size()
{
#if defined(__linux__) && defined(__x86_64__)
return sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
#elif defined(__MACH__)
/* Open the command for reading. */
FILE *fp = popen("sysctl -n machdep.cpu.cache.linesize", "r");
if (fp == NULL)
return -1;
int ret, size;
ret = fscanf(fp, "%d", &size);
pclose(fp);
return ret == 1 ? size : -1;
#else
return CACHELINE_SIZE;
#endif
}
#if defined(__linux__) || defined(__APPLE__)
int kernel_get_page_size()
{
return sysconf(_SC_PAGESIZE);
}
#else
#error "Unsupported platform"
#endif
/* There is no sysconf interface to get the hugepage size */
int kernel_get_hugepage_size()
{
#ifdef __linux__
char *key, *value, *unit, *line = NULL, *lasts;
int sz = -1;
size_t len = 0;
FILE *f;
f = fopen(PROCFS_PATH "/meminfo", "r");
if (!f)
return -1;
while (getline(&line, &len, f) != -1) {
key = strtok_r(line, ": ", &lasts);
value = strtok_r(NULL, " ", &lasts);
unit = strtok_r(NULL, "\n", &lasts);
if (!strcmp(key, "Hugepagesize") && !strcmp(unit, "kB")) {
sz = strtoul(value, NULL, 10) * 1024;
break;
}
}
free(line);
fclose(f);
return sz;
#elif defined(__x86_64__)
return 1 << 21;
#elif defined(__i386__)
return 1 << 22;
#else
#error "Unsupported architecture"
#endif
}
#ifdef __linux__
int kernel_module_set_param(const char *module, const char *param, const char *value)
{
FILE *f;
char fn[256];
snprintf(fn, sizeof(fn), "%s/module/%s/parameters/%s", SYSFS_PATH, module, param);
f = fopen(fn, "w");
if (!f)
serror("Failed set parameter %s for kernel module %s to %s", module, param, value);
debug(LOG_KERNEL | 5, "Set parameter %s of kernel module %s to %s", module, param, value);
fprintf(f, "%s", value);
fclose(f);
return 0;
}
int kernel_module_load(const char *module)
{
int ret;
ret = kernel_module_loaded(module);
if (!ret) {
debug(LOG_KERNEL | 5, "Kernel module %s already loaded...", module);
return 0;
}
pid_t pid = fork();
switch (pid) {
case -1: /* error */
return -1;
case 0: /* child */
execlp("modprobe", "modprobe", module, (char *) 0);
exit(EXIT_FAILURE); /* exec never returns */
default:
wait(&ret);
return kernel_module_loaded(module);
}
}
int kernel_module_loaded(const char *module)
{
FILE *f;
int ret = -1;
char *line = NULL;
size_t len = 0;
f = fopen(PROCFS_PATH "/modules", "r");
if (!f)
return -1;