Skip to content

Commit

Permalink
allow user-supplied cfg and topo files via env var
Browse files Browse the repository at this point in the history
and in the process i cleaned up init_configuration a bit
  • Loading branch information
domke committed Feb 6, 2024
1 parent 2054ecf commit 23c2212
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 66 deletions.
109 changes: 50 additions & 59 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,79 +133,74 @@ default_configuration(void)
int
init_configuration(void)
{
if (init_config == 1)
{
return 0;
}

FILE* fp = NULL;
char line[512];
char name[128];
char value[256];
char filename[1024];

filename[0] = '\0';
char preconfigured[1024];
preconfigured[0] = '\0';
if (init_config == 1)
char* customtopo = getenv("LIKWID_TOPO_FILE");
if (customtopo && !access(customtopo, R_OK))
{
return 0;
sprintf(filename, "%s", customtopo);
}
sprintf(preconfigured, "%s%s",TOSTRING(INSTALL_PREFIX),TOSTRING(CFGFILE));

if (access(preconfigured, R_OK) != 0)
else if (!access(TOSTRING(INSTALL_PREFIX, TOPOFILE), R_OK))
{
if (access(TOSTRING(CFGFILE), R_OK) != 0)
{
if (!access("/etc/likwid.cfg",R_OK))
{
sprintf(filename,"%s", "/etc/likwid.cfg");
}
}
else
{
sprintf(filename,"%s",TOSTRING(CFGFILE));
}
sprintf(filename, "%s", TOSTRING(INSTALL_PREFIX, TOPOFILE));
}
else
else if (!access(TOSTRING(TOPOFILE), R_OK))
{
sprintf(filename, "%s",preconfigured);
sprintf(filename, "%s", TOSTRING(TOPOFILE));
}

if ((config.topologyCfgFileName == NULL) && (strlen(filename) == 0))
if (filename[0] != '\0')
{
if (!access(TOSTRING(TOPOFILE), R_OK))
if (1023 == strlen(filename) && access(filename, R_OK))
{
preconfigured[0] = '\0';
sprintf(preconfigured,"%s", TOSTRING(TOPOFILE));
}
else
{
sprintf(preconfigured, "%s%s",TOSTRING(INSTALL_PREFIX),TOSTRING(TOPOFILE));
if (access(preconfigured, R_OK))
{
preconfigured[0] = '\0';
}
ERROR_PLAIN_PRINT(Topology file path too long for internal buffer);
return -1;
}
if (preconfigured[0] != '\0')
config.topologyCfgFileName = (char*)malloc((strlen(filename)+1) * sizeof(char));
stpcpy(config.topologyCfgFileName, filename);
}

filename[0] = '\0';
char* customcfg = getenv("LIKWID_CFG_FILE");
if (customcfg && !access(customcfg, R_OK))
{
snprintf(filename, 1024, "%s", customcfg);
}
else if (!access(TOSTRING(INSTALL_PREFIX, CFGFILE), R_OK))
{
snprintf(filename, 1024, "%s", TOSTRING(INSTALL_PREFIX, CFGFILE));
}
else if (!access(TOSTRING(CFGFILE), R_OK))
{
snprintf(filename, 1024, "%s", TOSTRING(CFGFILE));
}
if (filename[0] != '\0')
{
if (1023 == strlen(filename) && access(filename, R_OK))
{
config.topologyCfgFileName = (char*)malloc((strlen(preconfigured)+1) * sizeof(char));
strcpy(config.topologyCfgFileName, preconfigured);
config.topologyCfgFileName[strlen(preconfigured)] = '\0';
ERROR_PLAIN_PRINT(Config file path too long for internal buffer);
if (config.topologyCfgFileName) free(config.topologyCfgFileName);
return -1;
}
config.configFileName = (char*)malloc((strlen(filename)+1) * sizeof(char));
stpcpy(config.configFileName, filename);
}

if ((strlen(filename) == 0) || (!access(filename, R_OK)))
else
{
return default_configuration();
}
DEBUG_PRINT(DEBUGLEV_INFO, Reading configuration from %s, filename);
// Copy determined config filename to struct
config.configFileName = malloc((strlen(filename)+1)*sizeof(char));
strcpy(config.configFileName, filename);
config.configFileName[strlen(filename)] = '\0';

DEBUG_PRINT(DEBUGLEV_INFO, Reading configuration from %s, config.configFileName)
fp = fopen(config.configFileName, "r");
if (fp == NULL)
{
DEBUG_PLAIN_PRINT(DEBUGLEV_INFO, Using compile-time configuration)
return default_configuration();
}
DEBUG_PRINT(DEBUGLEV_INFO, Reading configuration from %s, filename)
while (fgets(line, 512, fp) != NULL) {
if (sscanf(line,"%s = %s", name, value) != 2)
{
Expand All @@ -215,17 +210,15 @@ init_configuration(void)
{
continue;
}
if (strcmp(name, "topology_file") == 0)
if (strcmp(name, "topology_file") == 0 && /*don't overrule user request*/!(customtopo && !access(customtopo, R_OK)))
{
config.topologyCfgFileName = (char*)malloc((strlen(value)+1) * sizeof(char));
strcpy(config.topologyCfgFileName, value);
config.topologyCfgFileName[strlen(value)] = '\0';
stpcpy(config.topologyCfgFileName, value);
}
else if (strcmp(name, "daemon_path") == 0)
{
config.daemonPath = (char*)malloc((strlen(value)+1) * sizeof(char));
strcpy(config.daemonPath, value);
config.daemonPath[strlen(value)] = '\0';
stpcpy(config.daemonPath, value);
if (access(config.daemonPath, R_OK))
{
if (default_configuration() < 0)
Expand All @@ -246,8 +239,7 @@ init_configuration(void)
if (S_ISDIR(st.st_mode))
{
config.groupPath = (char*)malloc((strlen(value)+1) * sizeof(char));
strcpy(config.groupPath, value);
config.groupPath[strlen(value)] = '\0';
stpcpy(config.groupPath, value);
}
else
{
Expand Down Expand Up @@ -279,10 +271,9 @@ init_configuration(void)
config.maxNumNodes = atoi(value);
}
}
fclose(fp);

init_config = 1;

fclose(fp);
return 0;
}

Expand Down
4 changes: 0 additions & 4 deletions src/includes/frequency.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
#ifndef FREQUENCY_H
#define FREQUENCY_H

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)


extern char* daemon_path;

#if !defined(__ARM_ARCH_7A__) && !defined(__ARM_ARCH_8A) && !defined(_ARCH_PPC)
Expand Down
2 changes: 0 additions & 2 deletions src/includes/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
#include <errno.h>

#define LOCK_INIT -1
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

static inline int
lock_acquire(int* var, int newval)
Expand Down
13 changes: 12 additions & 1 deletion src/includes/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,19 @@ typedef struct {

#define LLU_CAST (unsigned long long)

// borrowed from https://www.scs.stanford.edu/~dm/blog/va-opt.html
#define PARENS ()
#define EXPAND(...) EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__)))
#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__)))
#define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__)))
#define EXPAND1(...) __VA_ARGS__
#define FOR_EACH(macro, ...) \
__VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__)))
#define FOR_EACH_HELPER(macro, a1, ...) \
macro(a1)__VA_OPT__(FOR_EACH_AGAIN PARENS (macro, __VA_ARGS__))
#define FOR_EACH_AGAIN() FOR_EACH_HELPER
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define TOSTRING(...) FOR_EACH(STRINGIFY, __VA_ARGS__)

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
Expand Down

0 comments on commit 23c2212

Please sign in to comment.