Aufgrund eines Versionsupdates wird GitLab am 01.04. zwischen 9:00 und 9:30 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to a version upgrade, GitLab won't be accessible at 01.04. between 9:00 and 9:30 a.m.

Commit 90ff2f70 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼

more refactoring to C++

parent 051bab81
......@@ -27,6 +27,7 @@
#include <curl/curl.h>
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
struct advio {
CURL *curl;
......
......@@ -27,24 +27,28 @@
#include <jansson.h>
#include <villas/common.h>
#include <villas/common.hpp>
struct buffer {
enum State state;
namespace villas {
class Buffer {
public:
char *buf;
size_t len;
size_t size;
};
int buffer_init(struct buffer *b, size_t size);
Buffer(size_t size);
int buffer_destroy(struct buffer *b);
~Buffer();
void buffer_clear(struct buffer *b);
void clear();
int buffer_append(struct buffer *b, const char *data, size_t len);
int append(const char *data, size_t len);
int buffer_parse_json(struct buffer *b, json_t **j);
int parseJson(json_t **j);
int appendJson(json_t *j);
};
int buffer_append_json(struct buffer *b, json_t *j);
} /* namespace villas */
......@@ -31,7 +31,7 @@ enum class State {
CHECKED = 3,
STARTED = 4,
LOADED = 4, /* alias for STARTED used by struct plugin */
OPENED = 4, /* alias for STARTED used by struct io */
OPENED = 4, /* alias for STARTED used by IO */
STOPPED = 5,
UNLOADED = 5, /* alias for STARTED used by struct plugin */
CLOSED = 5, /* alias for STARTED used by struct io */
......
......@@ -45,14 +45,14 @@ protected:
public:
Window(size_type s = 0, T i = 0) :
init(i)
init(i),
steps(s)
{
size_type len = LOG2_CEIL(s);
/* Allocate memory for circular history buffer */
data = std::vector<T>(len, i);
steps = s;
pos = len;
mask = len - 1;
}
......
......@@ -31,17 +31,7 @@
namespace villas {
class Buffer : public std::vector<char> {
public:
void append(const char *data, size_t len)
{
insert(end(), data, data + len);
}
};
class JsonBuffer : public Buffer
class JsonBuffer : public std::vector<char>
{
protected:
......@@ -53,6 +43,11 @@ public:
/** Decode JSON document from the beginning of the buffer */
json_t * decode();
void append(const char *data, size_t len)
{
insert(end(), data, data + len);
}
};
} /* namespace villas */
/** Linux kernel related functions.
*
* @file
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2014-2020, 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/>.
*********************************************************************************/
/** @addtogroup kernel Kernel
* @{
*/
#pragma once
#include <cstring>
#include <cstdint>
#if WITH_CAP
#include <sys/capability.h>
/** Check if current process has capability \p cap.
*
* @retval 0 If capabilty is present.
* @retval <0 If capability is not present.
*/
int kernel_check_cap(cap_value_t cap);
#endif
/** Get number of reserved hugepages. */
int kernel_get_nr_hugepages();
/** Set number of reserved hugepages. */
int kernel_set_nr_hugepages(int nr);
/** Get kernel cmdline parameter
*
* See https://www.kernel.org/doc/Documentation/kernel-parameters.txt
*
* @param param The cmdline parameter to look for.
* @param buf The string buffer to which the parameter value will be copied to.
* @param len The length of the buffer \p value
* @retval 0 Parameter \p key was found and value was copied to \p value
* @reval <>0 Kernel was not booted with parameter \p key
*/
int kernel_get_cmdline_param(const char *param, char *buf, size_t len);
/** Checks if a kernel module is loaded
*
* @param module the name of the module
* @retval 0 Module is loaded.
* @reval <>0 Module is not loaded.
*/
int kernel_module_loaded(const char *module);
/** Load kernel module via modprobe */
int kernel_module_load(const char *module);
/** Set parameter of loaded kernel module */
int kernel_module_set_param(const char *module, const char *param, const char *value);
/** Get cacheline size in bytes */
int kernel_get_cacheline_size();
/** Get the size of a standard page in bytes. */
int kernel_get_page_size();
/** Get the size of a huge page in bytes. */
int kernel_get_hugepage_size();
/** Get CPU base frequency */
int kernel_get_cpu_frequency(uint64_t *freq);
/** Set SMP affinity of IRQ */
int kernel_irq_setaffinity(unsigned irq, uintmax_t aff , uintmax_t *old);
/** @} */
......@@ -23,6 +23,8 @@
#pragma once
#include <cstddef>
#include <villas/version.hpp>
namespace villas {
......@@ -31,5 +33,63 @@ namespace kernel {
/** Get the version of the kernel. */
utils::Version getVersion();
#if WITH_CAP
#include <sys/capability.h>
/** Check if current process has capability \p cap.
*
* @retval 0 If capabilty is present.
* @retval <0 If capability is not present.
*/
int check_cap(cap_value_t cap);
#endif
/** Get number of reserved hugepages. */
int get_nr_hugepages();
/** Set number of reserved hugepages. */
int set_nr_hugepages(int nr);
/** Get kernel cmdline parameter
*
* See https://www.kernel.org/doc/Documentation/kernel-parameters.txt
*
* @param param The cmdline parameter to look for.
* @param buf The string buffer to which the parameter value will be copied to.
* @param len The length of the buffer \p value
* @retval 0 Parameter \p key was found and value was copied to \p value
* @reval <>0 Kernel was not booted with parameter \p key
*/
int get_cmdline_param(const char *param, char *buf, size_t len);
/** Checks if a kernel module is loaded
*
* @param module the name of the module
* @retval 0 Module is loaded.
* @reval <>0 Module is not loaded.
*/
int module_loaded(const char *module);
/** Load kernel module via modprobe */
int module_load(const char *module);
/** Set parameter of loaded kernel module */
int module_set_param(const char *module, const char *param, const char *value);
/** Get cacheline size in bytes */
int get_cacheline_size();
/** Get the size of a standard page in bytes. */
int get_page_size();
/** Get the size of a huge page in bytes. */
int get_hugepage_size();
/** Get CPU base frequency */
int get_cpu_frequency(uint64_t *freq);
/** Set SMP affinity of IRQ */
int irq_setaffinity(unsigned irq, uintmax_t aff , uintmax_t *old);
} /* namespace villas */
} /* namespace kernel */
......@@ -31,7 +31,7 @@
#include <sys/types.h>
#include <pthread.h>
#include <villas/common.h>
#include <villas/common.hpp>
#define LIST_CHUNKSIZE 16
......@@ -76,7 +76,7 @@ int vlist_init(struct vlist *l);
* @param dtor A function pointer to a desctructor which will be called for every list item when the list is destroyed.
* @param l A pointer to the list data structure.
*/
int vlist_destroy(struct vlist *l, dtor_cb_t dtor, bool free);
int vlist_destroy(struct vlist *l, dtor_cb_t dtor = nullptr, bool free = false);
/** Append an element to the end of the list */
void vlist_push(struct vlist *l, void *p);
......@@ -101,7 +101,7 @@ int vlist_insert(struct vlist *l, size_t idx, void *p);
*/
void * vlist_lookup(struct vlist *l, const char *name);
ssize_t vlist_lookup_index(struct vlist *l, const char *name);
ssize_t vlist_lookup_index(struct vlist *l, const void *ptr);
/** Return the first element of the list for which cmp returns zero */
void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx);
......
......@@ -33,8 +33,6 @@
#include <jansson.h>
#include <villas/terminal.hpp>
namespace villas {
/* Forward declarations */
......
......@@ -29,7 +29,7 @@
#include <jansson.h>
#include <villas/log.hpp>
#include <villas/common.h>
#include <villas/common.hpp>
namespace villas {
namespace plugin {
......@@ -120,8 +120,8 @@ public:
virtual void dump();
std::string getName();
std::string getDescription();
const std::string & getName() const;
const std::string & getDescription() const;
protected:
std::string name;
......
......@@ -47,7 +47,7 @@
#include <villas/tsc.h>
#endif
struct task {
struct Task {
int clock; /**< CLOCK_{MONOTONIC,REALTIME} */
#if PERIODIC_TASK_IMPL == RDTSC /* We use cycle counts in RDTSC mode */
......@@ -63,26 +63,28 @@ struct task {
#elif PERIODIC_TASK_IMPL == RDTSC
struct tsc tsc; /**< Initialized by tsc_init(). */
#endif
};
/** Create a new task with the given rate. */
int task_init(struct task *t, double rate, int clock);
/** Create a new task with the given rate. */
Task(int clock = CLOCK_REALTIME);
int task_destroy(struct task *t);
~Task();
/** Wait until task elapsed
*
* @retval 0 An error occured. Maybe the task was stopped.
* @retval >0 The nummer of runs this task already fired.
*/
uint64_t task_wait(struct task *t);
/** Wait until task elapsed
*
* @retval 0 An error occured. Maybe the task was stopped.
* @retval >0 The nummer of runs this task already fired.
*/
uint64_t wait();
int task_set_next(struct task *t, struct timespec *next);
int task_set_timeout(struct task *t, double to);
int task_set_rate(struct task *t, double rate);
void setNext(const struct timespec *next);
void setTimeout(double to);
void setRate(double rate);
/** Returns a poll'able file descriptor which becomes readable when the timer expires.
*
* Note: currently not supported on all platforms.
*/
int task_fd(struct task *t);
void stop();
/** Returns a poll'able file descriptor which becomes readable when the timer expires.
*
* Note: currently not supported on all platforms.
*/
int getFD() const;
};
......@@ -35,7 +35,7 @@
#include <sys/sysctl.h>
#endif
#include <villas/kernel/kernel.h>
#include <villas/kernel/kernel.hpp>
#ifndef bit_TSC
#define bit_TSC (1 << 4)
......
......@@ -28,7 +28,6 @@ add_library(villas-common SHARED
hist.cpp
dsp/pid.cpp
kernel/kernel.cpp
kernel/kernel.cpp
kernel/rt.cpp
list.cpp
log.cpp
......
......@@ -41,8 +41,9 @@
#include <villas/utils.hpp>
#include <villas/config.h>
#include <villas/advio.h>
#include <villas/advio.hpp>
using namespace villas;
using namespace villas::utils;
#define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */
......@@ -209,6 +210,10 @@ AFILE * afopen(const char *uri, const char *mode)
const char *sep;
AFILE *af = new AFILE;
if (!af)
throw RuntimeError("Failed to allocate memory!");
memset(af, 0, sizeof(AFILE));
snprintf(af->mode, sizeof(af->mode), "%s", mode);
......
......@@ -22,78 +22,74 @@
#include <cstring>
#include <villas/compat.h>
#include <villas/buffer.h>
#include <villas/common.h>
#include <villas/compat.hpp>
#include <villas/buffer.hpp>
#include <villas/common.hpp>
#include <villas/exceptions.hpp>
int buffer_init(struct buffer *b, size_t size)
{
b->len = 0;
b->size = size;
b->buf = new char[size];
if (!b->buf)
return -1;
using namespace villas;
b->state = State::INITIALIZED;
Buffer::Buffer(size_t sz) :
len(0),
size(sz)
{
buf = new char[size];
if (!buf)
throw RuntimeError("Failed to allocate memory");
return 0;
memset(buf, 0, size);
}
int buffer_destroy(struct buffer *b)
Buffer::~Buffer()
{
if (b->buf)
delete[] b->buf;
b->state = State::DESTROYED;
return 0;
delete[] buf;
}
void buffer_clear(struct buffer *b)
void Buffer::clear()
{
b->len = 0;
len = 0;
}
int buffer_append(struct buffer *b, const char *data, size_t len)
int Buffer::append(const char *data, size_t l)
{
if (b->len + len > b->size) {
b->size = b->len + len;
b->buf = (char *) realloc(b->buf, b->size);
if (!b->buf)
if (len + l > size) {
size = len + l;
buf = (char *) realloc(buf, size);
if (!buf)
return -1;
}
memcpy(b->buf + b->len, data, len);
memcpy(buf + len, data, l);
b->len += len;
len += l;
return 0;
}
int buffer_parse_json(struct buffer *b, json_t **j)
int Buffer::parseJson(json_t **j)
{
*j = json_loadb(b->buf, b->len, 0, nullptr);
*j = json_loadb(buf, len, 0, nullptr);
if (!*j)
return -1;
return 0;
}
int buffer_append_json(struct buffer *b, json_t *j)
int Buffer::appendJson(json_t *j)
{
size_t len;
size_t l;
retry: len = json_dumpb(j, b->buf + b->len, b->size - b->len, 0);
if (b->size < b->len + len) {
b->buf = (char *) realloc(b->buf, b->len + len);
if (!b->buf)
retry: l = json_dumpb(j, buf + len, size - len, 0);
if (size < len + l) {
buf = (char *) realloc(buf, len + l);
if (!buf)
return -1;
b->size = b->len + len;
size = len + l;
goto retry;
}
b->len += len;
len += l;
return 0;
}
......@@ -21,7 +21,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <villas/common.h>
#include <villas/common.hpp>
#include <cstdlib>
......
......@@ -24,7 +24,7 @@
#include <jansson.h>
#include <unistd.h>
#include <villas/compat.h>
#include <villas/compat.hpp>
#if JANSSON_VERSION_HEX < 0x020A00
size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags)
......
......@@ -28,6 +28,7 @@
#include <villas/hist.hpp>
#include <villas/config.h>
#include <villas/table.hpp>
#include <villas/exceptions.hpp>
using namespace villas::utils;
......@@ -188,6 +189,10 @@ void Hist::plot() const
char * Hist::dump() const
{
char *buf = new char[128];
if (!buf)
throw RuntimeError("Failed to allocate memory!");
memset(buf, 0, 128);
strcatf(&buf, "[ ");
......
......@@ -20,7 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <villas/compat.h>
#include <villas/compat.hpp>
#include <villas/json_buffer.hpp>
using namespace villas;
......@@ -47,7 +47,7 @@ int JsonBuffer::encode(json_t *j)
int JsonBuffer::callback(const char *data, size_t len, void *ctx)
{
Buffer *b = static_cast<Buffer *>(ctx);
JsonBuffer *b = static_cast<JsonBuffer *>(ctx);
/* Append junk of JSON to buffer */
b->insert(b->end(), &data[0], &data[len]);
......
......@@ -34,7 +34,7 @@
#include <villas/utils.hpp>
#include <villas/config.h>
#include <villas/kernel/kernel.h>
#include <villas/kernel/kernel.hpp>
#include <villas/kernel/kernel.hpp>
#include <villas/exceptions.hpp>
......@@ -57,7 +57,7 @@ Version villas::kernel::getVersion()
return Version(ver);
}
int kernel_get_cacheline_size()
int villas::kernel::get_cacheline_size()
{
#if defined(__linux__) && defined(__x86_64__)
return sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
......@@ -80,7 +80,7 @@ int kernel_get_cacheline_size()
}
#if defined(__linux__) || defined(__APPLE__)
int kernel_get_page_size()
int villas::kernel::get_page_size()
{
return sysconf(_SC_PAGESIZE);
}
......@@ -89,7 +89,7 @@ int kernel_get_page_size()
#endif
/* There is no sysconf interface to get the hugepage size */
int kernel_get_hugepage_size()
int villas::kernel::get_hugepage_size()
{
#ifdef __linux__
char *key, *value, *unit, *line = nullptr, *lasts;
......@@ -127,7 +127,7 @@ int kernel_get_hugepage_size()
#ifdef __linux__
int kernel_module_set_param(const char *module, const char *param, const char *value)
int villas::kernel::module_set_param(const char *module, const char *param, const char *value)
{
FILE *f;
char fn[256];
......@@ -144,11 +144,11 @@ int kernel_module_set_param(const char *module, const char *param, const char *v
return 0;
}
int kernel_module_load(const char *module)
int villas::kernel::module_load(const char *module)
{
int ret;
ret = kernel_module_loaded(module);
ret = module_loaded(module);
if (!ret) {
debug(LOG_KERNEL | 5, "Kernel module %s already loaded...", module);
return 0;
......@@ -166,11 +166,11 @@ int kernel_module_load(const char *module)
default:
wait(&ret);
return kernel_module_loaded(module);
return module_loaded(module);
}
}
int kernel_module_loaded(const char *module)
int villas::kernel::module_loaded(const char *module)
{
FILE *f;
int ret = -1;
......@@ -194,7 +194,7 @@ int kernel_module_loaded(const char *module)
return ret;
}
int kernel_get_cmdline_param(const char *param, char *buf, size_t len)
int villas::kernel::get_cmdline_param(const char *param, char *buf, size_t len)
{
int ret;
char cmdline[512], key[128], value[128], *lasts, *tok;
......@@ -230,7 +230,7 @@ out:
return -1; /* not found or error */
}
int kernel_get_nr_hugepages()
int villas::kernel::get_nr_hugepages()
{
FILE *f;
int nr, ret;
......@@ -248,7 +248,7 @@ int kernel_get_nr_hugepages()
return nr;
}
int kernel_set_nr_hugepages(int nr)
int villas::kernel::set_nr_hugepages(int nr)
{
FILE *f;
int ret;
......@@ -272,7 +272,7 @@ int kernel_set_nr_hugepages(int nr)
return 0;
}
int kernel_irq_setaffinity(unsigned irq, uintmax_t aff, uintmax_t *old)
int villas::kernel::irq_setaffinity(unsigned irq, uintmax_t aff, uintmax_t *old)
{
char fn[64];
FILE *f;
......@@ -293,7 +293,7 @@ int kernel_irq_setaffinity(unsigned irq, uintmax_t aff, uintmax_t *old)
return ret;
}
int kernel_get_cpu_frequency(uint64_t *freq)
int villas::kernel::get_cpu_frequency(uint64_t *freq)
{
char *line = nullptr, *sep, *end;
size_t len = 0;
......
......@@ -26,11 +26,13 @@
#include <unistd.h>
#include <linux/limits.h>
#include <villas/log.h>
#include <villas/log.hpp>
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
#include <villas/config.h>
#include <villas/kernel/pci.h>
using namespace villas;
using namespace villas::utils;
int pci_init(struct pci *p)
......@@ -58,6 +60,10 @@ int pci_init(struct pci *p)
continue;
struct pci_device *d = new struct pci_device;
if (!d)
throw RuntimeError("Failed to allocate memory!");
memset(d, 0, sizeof(struct pci_device));