report-generator.c
report-json-serializer.c
worker.c
+ task_factory.c
+ task.c
)
ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
/**
* @brief Observe cpu usage.
*/
- OBSERVE_CPU = 1,
+ OBSERVE_CPU,
/**
* @brief Observe memory usage.
*/
- OBSERVE_MEMORY = 1 << 1
+ OBSERVE_MEMORY
} config_options_e;
/**
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
--- /dev/null
+/*
+ * 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
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);
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);
+/*
+ * 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
* limitations under the License.
*/
+#ifndef __TASK_H_
+#define __TASK_H_
+
+#include <stddef.h>
+#include "report-generator.h"
+
/**
* @brief The task template.
*/
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
+/*
+ * 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
* @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
#include <glib.h>
#include "worker.h"
+#include "task.h"
#include "log.h"
#include "err-check.h"
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);
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
#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.
*/