task, task factory: implementation (system) 70/181370/8
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Tue, 12 Jun 2018 08:09:46 +0000 (10:09 +0200)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Mon, 18 Jun 2018 13:48:47 +0000 (15:48 +0200)
Change-Id: I83baf01522e4176bf90887209d4ee659b2e5a143
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
src/CMakeLists.txt
src/config.h
src/macros.h [new file with mode: 0644]
src/report-generator.h
src/task.c
src/task.h
src/task_factory.c
src/task_factory.h
src/worker.c
src/worker.h

index 2acb38c..fa2372c 100644 (file)
@@ -18,6 +18,8 @@ SET(SRCS
        report-generator.c
        report-json-serializer.c
        worker.c
+       task_factory.c
+       task.c
 )
 ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
 
index 9f1a1a5..e90df06 100644 (file)
@@ -46,12 +46,12 @@ typedef enum config_options
     /**
      * @brief Observe cpu usage.
      */
-    OBSERVE_CPU = 1,
+    OBSERVE_CPU,
 
     /**
      * @brief Observe memory usage.
      */
-    OBSERVE_MEMORY = 1 << 1
+    OBSERVE_MEMORY
 } config_options_e;
 
 /**
@@ -74,9 +74,19 @@ typedef struct config
     char **config_data;
 
     /**
+     *  @brief Size of config_data array.
+     */
+    size_t config_data_size;
+
+    /**
      * @brief Config options.
      */
-    int options;
+    config_options_e options;
+
+    /**
+     * @brief Task frequency in seconds.
+     */
+    int frequency;
 } config_t;
 
 #endif
\ No newline at end of file
diff --git a/src/macros.h b/src/macros.h
new file mode 100644 (file)
index 0000000..daf83a1
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MACROS_H_
+#define __MACROS_H_
+
+#define container_of(ptr, type, member) ({                      \
+        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
+        (type *)( (char *)__mptr - offsetof(type,member) );})
+
+#endif
\ No newline at end of file
index b1dc4e9..06ea947 100644 (file)
@@ -38,7 +38,7 @@ typedef struct report_generator_app report_generator_app_t;
 report_generator_system_t *report_generator_new_system_report_generator();
 
 /**
- * @brief Releasd report_generator_system_t created with
+ * @brief Release report_generator_system_t created with
  * @report_generator_new_system_report_generator
  */
 void report_generator_free_system_generator(report_generator_system_t *generator);
@@ -55,7 +55,7 @@ void report_generator_free_system_generator(report_generator_system_t *generator
 report_generator_process_t *report_generator_new_process_report_generator(int pid);
 
 /**
- * @brief Releasd report_generator_process_t created with
+ * @brief Release report_generator_process_t created with
  * @report_generator_new_process_report_generator_
  */
 void report_generator_free_process_generator(report_generator_process_t *generator);
index e69de29..2036c54 100644 (file)
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "task.h"
+#include "err-check.h"
+
+void task_release(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    if (task->release)
+    {
+        task->release(task);
+    }
+}
+
+void task_execute(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    if (task->execute)
+    {
+        task->execute(task);
+    }
+}
\ No newline at end of file
index 3132cb8..35662b1 100644 (file)
 * limitations under the License.
 */
 
+#ifndef __TASK_H_
+#define __TASK_H_
+
+#include <stddef.h>
+#include "report-generator.h"
+
 /**
  * @brief The task template.
  */
@@ -25,52 +31,57 @@ typedef struct task task_t;
 typedef void(*task_execute_cb)(task_t *task);
 
 /**
- * @brief Called on the task initialization.
+ * @brief Called on the task release.
  */
-typedef int(*task_init_cb)(task_t *task);
+typedef void(*task_release_cb)(task_t *task);
 
 /**
- * @brief Called on the task shutdown.
+ * @brief Base task structure.
  */
-typedef void(*task_shutdown_cb)(task_t *task);
-
 struct task
 {
-    task_init_cb init;
-    task_shutdown_cb shutdown;
     task_execute_cb execute;
-    int frequency;
-    void *user_data;
+    task_release_cb release;
 };
 
 /**
- * @brief Initializes the task.
- * @param[in] task The task handle.
+ * @brief System task structure.
  */
-int task_initialize(task_t *task);
+typedef struct system_task
+{
+    task_t task;
+    report_generator_system_t *report_generator;
+} system_task_t;
 
 /**
- * @brief Shutdowns the task.
- * @param[in] task The task handle.
+ * @brief Application task structure.
  */
-void task_shutdown(task_t *task);
+typedef struct app_task
+{
+    task_t task;
+    report_generator_app_t *report_generator;
+} app_task_t;
 
 /**
- * @brief Executes the task.
- * @param[in] task The task handle.
+ * @brief Process task structure.
  */
-void task_execute(task_t *task);
+typedef struct process_task
+{
+    task_t task;
+    report_generator_process_t *report_generator;
+} process_task_t;
+
 
 /**
- * @brief Sets the user data.
+ * @brief Releases the task.
  * @param[in] task The task handle.
- * @param[in] data The user data.
  */
-void task_set_user_data(task_t *task, void *data);
+void task_release(task_t *task);
 
 /**
- * @brief Gets the user data.
+ * @brief Executes the task.
  * @param[in] task The task handle.
- * @return User data associated with given task.
  */
-void *task_get_user_data(task_t *task);
\ No newline at end of file
+void task_execute(task_t *task);
+
+#endif
\ No newline at end of file
index e69de29..61ce91c 100644 (file)
@@ -0,0 +1,115 @@
+/*
+     * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <stdlib.h>
+#include "task_factory.h"
+#include "config.h"
+#include "report-generator.h"
+#include "report-json-serializer.h"
+#include "log.h"
+#include "err-check.h"
+#include "macros.h"
+
+static task_t *create_system_report_task(config_options_e options, int frequency);
+static void execute_scan_system_memory(task_t *task);
+static void execute_scan_system_cpu(task_t *task);
+static void send_report(char *report);
+static void release_system_task(task_t *task);
+
+task_t *task_factory_create_single(const config_t *config)
+{
+    switch(config->scope)
+    {
+        case SYSTEM:
+            return create_system_report_task(config->options, config->frequency);
+        case APPS:
+        case CORES:
+        default:
+            return NULL;
+    }
+}
+
+static task_t *create_system_report_task(config_options_e options, int frequency)
+{
+    system_task_t *_system_task = (system_task_t *)g_malloc(sizeof(system_task_t));
+
+    _system_task->task.release = release_system_task;
+    _system_task->report_generator = report_generator_new_system_report_generator();
+
+    switch (options)
+    {
+        case OBSERVE_CPU:
+            _system_task->task.execute = execute_scan_system_cpu;
+            break;
+        case OBSERVE_MEMORY:
+            _system_task->task.execute = execute_scan_system_memory;
+            break;
+        default:
+            g_free(_system_task);
+            return NULL;
+    }
+
+    return &_system_task->task;
+}
+
+static void execute_scan_system_memory(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    system_task_t *_system_task = container_of(task, system_task_t, task);
+
+    struct system_memory_usage_report report;
+
+    int ret = report_generator_generate_system_memory_usage_report(_system_task->report_generator, &report);
+    ON_TRUE_RETURN(ret != 0);
+
+    char *json_report = report_json_serializer_serialize_system_memory_usage_report(&report);
+    send_report(json_report);
+    g_free(json_report);
+}
+
+static void execute_scan_system_cpu(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    system_task_t *_system_task = container_of(task, system_task_t, task);
+
+    struct system_cpu_usage_report report;
+
+    int ret = report_generator_generate_system_cpu_usage_report(_system_task->report_generator, 0, &report);
+    ON_TRUE_RETURN(ret != 0);
+
+    char *json_report = report_json_serializer_serialize_system_cpu_usage_report(&report);
+    send_report(json_report);
+    g_free(json_report);
+}
+
+static void send_report(char *report)
+{
+    //TODO send report here
+}
+
+static void release_system_task(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    system_task_t *_system_task = container_of(task, system_task_t, task);
+
+    report_generator_free_system_generator(_system_task->report_generator);
+
+    g_free(_system_task);
+}
\ No newline at end of file
index 795a994..9737e20 100644 (file)
  * @param[in] config Task configuration depends o it.
  * @return Returns task structure.
  */
-task_t task_factory_create_single(const config_t *config);
+task_t *task_factory_create_single(const config_t *config);
 
 /**
  * @brief Creates one or more tasks.
  * @param[in] configs Tasks configurations depends o those.
  * @return Returns null terminated array of task structures.
  */
-task_t *task_factory_create_many(const config_t **configs);
+task_t **task_factory_create_many(const config_t **configs);
 
 #endif
\ No newline at end of file
index 8a5804d..d8b0164 100644 (file)
@@ -17,6 +17,7 @@
 #include <glib.h>
 
 #include "worker.h"
+#include "task.h"
 #include "log.h"
 #include "err-check.h"
 
@@ -53,6 +54,20 @@ void worker_destroy(worker_t *worker)
     g_free(worker);
 }
 
+void worker_sync(worker_t *worker)
+{
+    GError *err = NULL;
+
+    g_thread_pool_free(worker->thread_pool, FALSE, TRUE);
+    worker->thread_pool = g_thread_pool_new((GFunc)worker_function, NULL, MAX_THREAD_DEFAULT, FALSE, &err);
+    if (err != NULL)
+    {
+        ERR("Failed to create thread pool ", err->message);
+        g_error_free (err);
+        return;
+    }
+}
+
 void worker_set_max_num_of_threads(worker_t *worker, int max_threads)
 {
     ON_NULL_RETURN(worker);
@@ -76,7 +91,5 @@ int worker_enqueue_task(worker_t *worker, task_t task)
 static void worker_function(gpointer data, gpointer user_data)
 {
     task_t *task = (task_t *)data;
-    task->init(task);
-    task->execute(task);
-    task->shutdown(task);
+    task_execute(task);
 }
\ No newline at end of file
index 05021d4..3c4fee7 100644 (file)
 #include "task.h"
 
 /**
- * @brief Worker structure.
+ * @brief The worker structure.
  */
 typedef struct worker worker_t;
 
 /**
- * @brief Creates worker.
+ * @brief Creates the worker.
  */
 worker_t *worker_create();
 
 /**
- * @brief Destroys worker.
+ * @brief Destroys the worker.
  */
 void worker_destroy(worker_t *worker);
 
 /**
- * @brief Sets maximum number of threads that worker can use.
+ * @brief Syncs the worker.
+ * @remarks Function blocks until all current tasks are finished.
+ */
+void worker_sync(worker_t *worker);
+
+/**
+ * @brief Sets maximum number of threads that the worker can use.
  * @param[in] worker Worker reference.
  * @param[in] max_threads Maximum number of threads that worker can use.
  */
 void worker_set_max_num_of_threads(worker_t *worker, int max_threads);
 
 /**
- * @brief Adds new task to task queue.
+ * @brief Adds new task to the task queue.
  * @param[in] worker Worker reference.
  * @param[in] task Task to do.
  */