[Conf] Implement configuration parsing
authorDongju Chae <dongju.chae@samsung.com>
Tue, 16 Jul 2019 12:27:50 +0000 (21:27 +0900)
committer함명주/On-Device Lab(SR)/Principal Engineer/삼성전자 <myungjoo.ham@samsung.com>
Wed, 17 Jul 2019 07:21:24 +0000 (16:21 +0900)
This commit implements configuration parsing in `ne-conf.c`

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
core/npu-engine/src/ne-conf.c
core/npu-engine/src/ne-conf.h

index 1860c28..767af49 100644 (file)
 #define DEFAULT_WORKING_DIR     "/"
 #define DEFAULT_LOG_DIR         "/"
 
+#define ENV_RESV_MEM_SIZE       "NE_RESV_MEM_SIZE"
+#define ENV_WORKING_DIR         "NE_WORKING_DIR"
+#define ENV_LOG_DIR             "NE_LOG_DIR"
+
 static conf_t config;
 const conf_t *conf = &config;
 
@@ -33,35 +37,97 @@ const conf_t *conf = &config;
  * @brief Load configuration with default values
  * @param[out] c The configuration object to be updated.
  */
-static void load_conf_default(conf_t *c) {
+static void
+load_conf_default(conf_t *c)
+{
   c->reserved_mem_size = DEFAULT_RESV_MEM_SIZE;
-  c->working_dir = DEFAULT_WORKING_DIR;
-  c->log_dir = DEFAULT_LOG_DIR;
+
+  strncpy (c->working_dir, DEFAULT_WORKING_DIR, strlen(DEFAULT_WORKING_DIR));
+  c->working_dir[strlen(DEFAULT_WORKING_DIR)] = '\x00';
+
+  strncpy (c->log_dir, DEFAULT_LOG_DIR, strlen(DEFAULT_LOG_DIR));
+  c->log_dir[strlen(DEFAULT_LOG_DIR)] = '\x00';
+
   /** @todo NYI */
 }
 
 /**
- * @brief Load configuration with .ini file.
+ * @brief Load configuration for each config parameter
+ * @param[in] resv_mem_size config string for reserved mem size
+ * @param[in] working_dir config string for working dir
+ * @param[in] log_dir config string for log dir
  * @param[out] c The configuration object to be updated.
+ */
+static void
+load_conf_each (const char *resv_mem_size, const char *working_dir,
+                const char *log_dir, conf_t *c)
+{
+  if (resv_mem_size) {
+    uint64_t val = 0;
+    char unit = '\x00';
+    
+    sscanf (resv_mem_size, "%lu%c", &val, &unit);
+
+    if (val > 0) {
+      if (unit != '\x00') {
+        if (unit == 'K' || unit == 'k') {
+          val *= 1024;
+        } else if (unit == 'M' || unit == 'm') {
+          val *= 1024 * 1024;
+        } else if (unit == 'G' || unit == 'g') {
+          val *= 1024 * 1024 * 1024;
+        } else
+          fprintf (stderr, "Unknown unit for memory size: %c\n", unit);
+      }
+      c->reserved_mem_size = val;
+    } else
+      fprintf (stderr, "Cannot parse 'resv_mem_size': %s\n", resv_mem_size);
+  }
+
+  if (working_dir && strlen(working_dir) < MAX_DIR_LEN) {
+    strncpy (c->working_dir, working_dir, strlen(working_dir));
+    c->working_dir[strlen(working_dir)] = '\x00';
+  }
+
+  if (log_dir && strlen(log_dir) < MAX_DIR_LEN) {
+    strncpy (c->log_dir, log_dir, strlen(log_dir));
+    c->log_dir[strlen(log_dir)] = '\x00';
+  }
+}
+
+/**
+ * @brief Load configuration with .ini file.
  * @param[in] ini The parsed .ini file.
+ * @param[out] c The configuration object to be updated.
  */
-static void load_conf_ini(const dictionary *ini, conf_t *c) {
-  /** @todo NYI */
+static void load_conf_ini(const dictionary *ini, conf_t *c)
+{
+  const char *resv_mem_size = iniparser_getstring(ini, "main:resv_mem_size", NULL);
+  const char *working_dir = iniparser_getstring(ini, "main:working_dir", NULL);
+  const char *log_dir = iniparser_getstring(ini, "main:log_dir", NULL);
+
+  load_conf_each (resv_mem_size, working_dir, log_dir, c);
 }
 
 /**
  * @brief Load configuration with env-vars
  * @param[out] c The configuration object to be updated.
  */
-static void load_conf_envvar(conf_t *c) {
-  /** @todo NYI */
+static void load_conf_envvar(conf_t *c)
+{
+  const char *resv_mem_size = getenv (ENV_RESV_MEM_SIZE);
+  const char *working_dir = getenv (ENV_WORKING_DIR);
+  const char *log_dir = getenv (ENV_LOG_DIR);
+
+  load_conf_each (resv_mem_size, working_dir, log_dir, c);
 }
 
 /**
  * @brief Load the configuration from /etc/npu-engine.ini and env-vars.
  * @detail Refer to the header file for more detail.
  */
-int load_conf(const char *inipath) {
+int load_conf(const char *inipath)
+{
   dictionary *ini;
 
   /* Load up config from default */
index 7e62f5d..bd4cc95 100644 (file)
@@ -19,6 +19,8 @@
 
 #include <stdint.h>
 
+#define MAX_DIR_LEN 256
+
 /**
  * @brief The global environmental and configuration values
  * @detail The list may keep grow.
@@ -32,8 +34,8 @@ typedef struct {
     COMM_SOCIP,
     COMM_PCIE,
   } communication_method; /**< Determined at build-time. Not configurable at runtime. The activated N1x plugin. */
-  char *working_dir; /**< NE_WORKING_DIR, [main] working_dir, the path where the daemon is executed */
-  char *log_dir; /**< NE_LOG_DIR, [main] log_dir, the path where log files are created */
+  char working_dir[MAX_DIR_LEN]; /**< NE_WORKING_DIR, [main] working_dir, the path where the daemon is executed */
+  char log_dir[MAX_DIR_LEN]; /**< NE_LOG_DIR, [main] log_dir, the path where log files are created */
 } conf_t;
 
 extern const conf_t *conf; /** Users cannot modify this. */