1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added progress bar to advio up/downloads

This commit is contained in:
Steffen Vogel 2017-03-07 08:03:14 -04:00
parent 4b2d4fe603
commit 7626c67616
3 changed files with 189 additions and 145 deletions

View file

@ -21,8 +21,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <math.h>
#include <curl/curl.h>
@ -30,7 +29,46 @@
#include "config.h"
#include "advio.h"
AFILE *afopen(const char *uri, const char *mode, int flags)
#define BAR_WIDTH 60 /**< How wide you want the progress meter to be. */
static int xferinfo(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
struct advio *af = (struct advio *) p;
double curtime = 0;
curl_easy_getinfo(af->curl, CURLINFO_TOTAL_TIME, &curtime);
// ensure that the file to be downloaded is not empty
// because that would cause a division by zero error later on
if (dltotal <= 0.0)
return 0;
double frac = dlnow / dltotal;
// part of the progressmeter that's already "full"
int dotz = round(frac * BAR_WIDTH);
// create the "meter"
printf("%3.0f%% in %f s (%" CURL_FORMAT_CURL_OFF_T " / %" CURL_FORMAT_CURL_OFF_T ") [", frac * 100, curtime, dlnow, dltotal);
// part that's full already
int i = 0;
for ( ; i < dotz; i++)
printf("=");
// remaining part (spaces)
for ( ; i < BAR_WIDTH; i++)
printf(" ");
// and back to line begin - do not forget the fflush to avoid output buffering problems!
printf("]\r");
fflush(stdout);
return 0;
}
AFILE * afopen(const char *uri, const char *mode, int flags)
{
CURLcode res;
@ -54,6 +92,9 @@ AFILE *afopen(const char *uri, const char *mode, int flags)
curl_easy_setopt(af->curl, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(af->curl, CURLOPT_URL, uri);
curl_easy_setopt(af->curl, CURLOPT_WRITEDATA, af->file);
curl_easy_setopt(af->curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
curl_easy_setopt(af->curl, CURLOPT_XFERINFODATA, af);
curl_easy_setopt(af->curl, CURLOPT_NOPROGRESS, 0L);
res = curl_easy_perform(af->curl);
switch (res) {

281
lib/cfg.c
View file

@ -40,9 +40,7 @@ int cfg_init_pre(struct cfg *cfg)
int cfg_init_post(struct cfg *cfg)
{
info("Initialize memory system");
memory_init();
rt_init(cfg->priority, cfg->affinity);
api_init(&cfg->api, cfg);
web_init(&cfg->web, &cfg->api);
@ -107,163 +105,166 @@ int cfg_parse(struct cfg *cfg, const char *uri)
config_setting_t *cfg_root, *cfg_nodes, *cfg_paths, *cfg_plugins, *cfg_logging, *cfg_web;
info("Parsing configuration: uri=%s", uri);
int ret = CONFIG_FALSE;
if (uri) {
/* Setup libconfig */
config_set_auto_convert(&cfg->cfg, 1);
FILE *f;
AFILE *af;
/* Via stdin */
if (strcmp("-", uri) == 0) {
af = NULL;
f = stdin;
}
/* Local file? */
else if (access(uri, F_OK) != -1) {
/* Setup libconfig include path.
* This is only supported for local files */
char *uri_cpy = strdup(uri);
char *include_dir = dirname(uri_cpy);
{ INDENT
config_set_include_dir(&cfg->cfg, include_dir);
int ret = CONFIG_FALSE;
if (uri) {
/* Setup libconfig */
config_set_auto_convert(&cfg->cfg, 1);
FILE *f;
AFILE *af;
/* Via stdin */
if (strcmp("-", uri) == 0) {
af = NULL;
f = stdin;
}
/* Local file? */
else if (access(uri, F_OK) != -1) {
/* Setup libconfig include path.
* This is only supported for local files */
char *uri_cpy = strdup(uri);
char *include_dir = dirname(uri_cpy);
config_set_include_dir(&cfg->cfg, include_dir);
free(uri_cpy);
free(uri_cpy);
af = NULL;
f = fopen(uri, "r");
af = NULL;
f = fopen(uri, "r");
}
/* Use advio (libcurl) to fetch the config from a remote */
else {
af = afopen(uri, "r", ADVIO_MEM);
f = af ? af->file : NULL;
}
/* Check if file could be loaded / opened */
if (!f)
error("Failed to open configuration from: %s", uri);
/* Parse config */
ret = config_read(&cfg->cfg, f);
if (ret != CONFIG_TRUE)
error("Failed to parse configuration: %s in %s:%d", config_error_text(&cfg->cfg), uri, config_error_line(&cfg->cfg));
/* Close configuration file */
if (af)
afclose(af);
else
fclose(f);
}
/* Use advio (libcurl) to fetch the config from a remote */
else {
af = afopen(uri, "r", ADVIO_MEM);
f = af ? af->file : NULL;
warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
cfg->web.port = 80;
cfg->web.htdocs = "/villas/web/socket/";
}
/* Check if file could be loaded / opened */
if (!f)
error("Failed to open configuration from: %s", uri);
/* Parse config */
ret = config_read(&cfg->cfg, f);
if (ret != CONFIG_TRUE)
error("Failed to parse configuration: %s in %s:%d", config_error_text(&cfg->cfg), uri, config_error_line(&cfg->cfg));
/* Parse global settings */
cfg_root = config_root_setting(&cfg->cfg);
if (cfg_root) {
if (!config_setting_is_group(cfg_root))
warn("Missing global section in config file.");
/* Close configuration file */
if (af)
afclose(af);
else
fclose(f);
}
else {
warn("No configuration file specified. Starting unconfigured. Use the API to configure this instance.");
if (!config_setting_lookup_int(cfg_root, "affinity", &cfg->affinity))
cfg->affinity = 0;
cfg->web.port = 80;
cfg->web.htdocs = "/villas/web/socket/";
}
if (!config_setting_lookup_int(cfg_root, "priority", &cfg->priority))
cfg->priority = 0;
/* Parse global settings */
cfg_root = config_root_setting(&cfg->cfg);
if (cfg_root) {
if (!config_setting_is_group(cfg_root))
warn("Missing global section in config file.");
if (!config_setting_lookup_int(cfg_root, "affinity", &cfg->affinity))
cfg->affinity = 0;
if (!config_setting_lookup_int(cfg_root, "priority", &cfg->priority))
cfg->priority = 0;
if (!config_setting_lookup_float(cfg_root, "stats", &cfg->stats))
cfg->stats = 0;
}
if (!config_setting_lookup_float(cfg_root, "stats", &cfg->stats))
cfg->stats = 0;
}
cfg_web = config_setting_get_member(cfg_root, "http");
if (cfg_web)
web_parse(&cfg->web, cfg_web);
cfg_web = config_setting_get_member(cfg_root, "http");
if (cfg_web)
web_parse(&cfg->web, cfg_web);
/* Parse logging settings */
cfg_logging = config_setting_get_member(cfg_root, "logging");
if (cfg_logging)
log_parse(&cfg->log, cfg_logging);
/* Parse logging settings */
cfg_logging = config_setting_get_member(cfg_root, "logging");
if (cfg_logging)
log_parse(&cfg->log, cfg_logging);
/* Parse plugins */
cfg_plugins = config_setting_get_member(cfg_root, "plugins");
if (cfg_plugins) {
if (!config_setting_is_array(cfg_plugins))
cerror(cfg_plugins, "Setting 'plugins' must be a list of strings");
/* Parse plugins */
cfg_plugins = config_setting_get_member(cfg_root, "plugins");
if (cfg_plugins) {
if (!config_setting_is_array(cfg_plugins))
cerror(cfg_plugins, "Setting 'plugins' must be a list of strings");
for (int i = 0; i < config_setting_length(cfg_plugins); i++) {
struct config_setting_t *cfg_plugin = config_setting_get_elem(cfg_plugins, i);
for (int i = 0; i < config_setting_length(cfg_plugins); i++) {
struct config_setting_t *cfg_plugin = config_setting_get_elem(cfg_plugins, i);
struct plugin plugin;
struct plugin plugin;
ret = plugin_parse(&plugin, cfg_plugin);
if (ret)
cerror(cfg_plugin, "Failed to parse plugin");
list_push(&cfg->plugins, memdup(&plugin, sizeof(plugin)));
}
}
/* Parse nodes */
cfg_nodes = config_setting_get_member(cfg_root, "nodes");
if (cfg_nodes) {
if (!config_setting_is_group(cfg_nodes))
warn("Setting 'nodes' must be a group with node name => group mappings.");
for (int i = 0; i < config_setting_length(cfg_nodes); i++) {
config_setting_t *cfg_node = config_setting_get_elem(cfg_nodes, i);
struct plugin *p;
const char *type;
/* Required settings */
if (!config_setting_lookup_string(cfg_node, "type", &type))
cerror(cfg_node, "Missing node type");
p = plugin_lookup(PLUGIN_TYPE_NODE, type);
if (!p)
cerror(cfg_node, "Invalid node type: %s", type);
struct node *n = node_create(&p->node);
ret = node_parse(n, cfg_node);
if (ret)
cerror(cfg_node, "Failed to parse node");
list_push(&cfg->nodes, n);
}
}
/* Parse paths */
cfg_paths = config_setting_get_member(cfg_root, "paths");
if (cfg_paths) {
if (!config_setting_is_list(cfg_paths))
warn("Setting 'paths' must be a list.");
for (int i = 0; i < config_setting_length(cfg_paths); i++) {
config_setting_t *cfg_path = config_setting_get_elem(cfg_paths, i);
struct path *p = path_create();
ret = path_parse(p, cfg_path, &cfg->nodes);
if (ret)
cerror(cfg_path, "Failed to parse path");
list_push(&cfg->paths, p);
if (p->reverse) {
struct path *r = path_create();
ret = path_reverse(p, r);
ret = plugin_parse(&plugin, cfg_plugin);
if (ret)
cerror(cfg_path, "Failed to reverse path %s", path_name(p));
cerror(cfg_plugin, "Failed to parse plugin");
list_push(&cfg->plugins, memdup(&plugin, sizeof(plugin)));
}
}
list_push(&cfg->paths, r);
/* Parse nodes */
cfg_nodes = config_setting_get_member(cfg_root, "nodes");
if (cfg_nodes) {
if (!config_setting_is_group(cfg_nodes))
warn("Setting 'nodes' must be a group with node name => group mappings.");
for (int i = 0; i < config_setting_length(cfg_nodes); i++) {
config_setting_t *cfg_node = config_setting_get_elem(cfg_nodes, i);
struct plugin *p;
const char *type;
/* Required settings */
if (!config_setting_lookup_string(cfg_node, "type", &type))
cerror(cfg_node, "Missing node type");
p = plugin_lookup(PLUGIN_TYPE_NODE, type);
if (!p)
cerror(cfg_node, "Invalid node type: %s", type);
struct node *n = node_create(&p->node);
ret = node_parse(n, cfg_node);
if (ret)
cerror(cfg_node, "Failed to parse node");
list_push(&cfg->nodes, n);
}
}
/* Parse paths */
cfg_paths = config_setting_get_member(cfg_root, "paths");
if (cfg_paths) {
if (!config_setting_is_list(cfg_paths))
warn("Setting 'paths' must be a list.");
for (int i = 0; i < config_setting_length(cfg_paths); i++) {
config_setting_t *cfg_path = config_setting_get_elem(cfg_paths, i);
struct path *p = path_create();
ret = path_parse(p, cfg_path, &cfg->nodes);
if (ret)
cerror(cfg_path, "Failed to parse path");
list_push(&cfg->paths, p);
if (p->reverse) {
struct path *r = path_create();
ret = path_reverse(p, r);
if (ret)
cerror(cfg_path, "Failed to reverse path %s", path_name(p));
list_push(&cfg->paths, r);
}
}
}
}

View file

@ -18,11 +18,13 @@
#include "memory.h"
int memory_init()
{ INDENT
{
#ifdef __linux__
info("Initialize memory sub-system");
int nr = kernel_get_nr_hugepages();
if (nr < DEFAULT_NR_HUGEPAGES) {
if (nr < DEFAULT_NR_HUGEPAGES) { INDENT
kernel_set_nr_hugepages(DEFAULT_NR_HUGEPAGES);
debug(LOG_MEM | 2, "Reserve %d hugepages (was %d)", DEFAULT_NR_HUGEPAGES, nr);
}