diff options
author | Michal Kolodziejski <m.kolodziejs@samsung.com> | 2018-07-13 14:59:50 +0200 |
---|---|---|
committer | Michal Kolodziejski <m.kolodziejs@samsung.com> | 2018-07-16 12:40:43 +0200 |
commit | 13ff103f7a36b0f059ac8da01ffa4682c8febcba (patch) | |
tree | 74b09fc77d084e5260c0237966cebc122523a788 | |
parent | b7a0e3821a9647709e0ffbe9268c04281669e7a7 (diff) | |
download | ttsd-worker-task-13ff103f7a36b0f059ac8da01ffa4682c8febcba.tar.gz ttsd-worker-task-13ff103f7a36b0f059ac8da01ffa4682c8febcba.tar.bz2 ttsd-worker-task-13ff103f7a36b0f059ac8da01ffa4682c8febcba.zip |
ipc: integrated with project
Change-Id: I0bf64847369d7969abad3fa80760ae7d9e76da70
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
-rw-r--r-- | src/config-deserializer.c | 25 | ||||
-rw-r--r-- | src/config-deserializer.h | 5 | ||||
-rw-r--r-- | src/ipc.c | 48 | ||||
-rw-r--r-- | src/ipc.h | 19 | ||||
-rw-r--r-- | src/scheduler.c | 13 | ||||
-rw-r--r-- | src/scheduler.h | 3 | ||||
-rw-r--r-- | src/task-factory.c | 21 | ||||
-rw-r--r-- | src/task-factory.h | 11 | ||||
-rw-r--r-- | src/task-worker.c | 26 |
9 files changed, 87 insertions, 84 deletions
diff --git a/src/config-deserializer.c b/src/config-deserializer.c index 298d305..a7afe08 100644 --- a/src/config-deserializer.c +++ b/src/config-deserializer.c @@ -26,16 +26,26 @@ static JsonNode *parse_string(JsonParser *parser, const char *config_json); static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *element, gpointer user_data); static config_options_e config_parse_options(const char *option); +static int calculate_task_counter(int frequency); -config_t *deserialize_configs(const char *config_json, int *size, int *total_duration) +static struct _cfg_data +{ + int total_duration; + int task_counter; +} cfg_data = +{ + .total_duration = 0, + .task_counter = 0 +}; + +config_t *deserialize_configs(const char *config_json, int *size, int *task_counter) { JsonParser *parser = json_parser_new(); JsonNode *root = parse_string(parser, config_json); JsonObject *obj = json_node_get_object(root); - int td = json_object_get_int_member(obj, SCHEMA_CONFIG_TOTAL_DURATION); - *total_duration = td; + cfg_data.total_duration = json_object_get_int_member(obj, SCHEMA_CONFIG_TOTAL_DURATION); JsonArray *array = json_object_get_array_member(obj, SCHEMA_CONFIG_ENTRY); if (!array) @@ -52,7 +62,7 @@ config_t *deserialize_configs(const char *config_json, int *size, int *total_dur json_array_foreach_element(array, config_array_iterate_func, configs); g_object_unref(parser); - + *task_counter = cfg_data.task_counter; return configs; } @@ -79,6 +89,8 @@ static void config_array_iterate_func(JsonArray *array, guint index, JsonNode *e gint64 frequency = json_object_get_int_member(entry, SCHEMA_FREQUENCY); configs[index].frequency = frequency; + int c = calculate_task_counter(frequency); + cfg_data.task_counter += c; if (g_strcmp0(type, SCHEMA_TYPE_TOP) == 0) { @@ -125,4 +137,9 @@ static config_options_e config_parse_options(const char *option) } return -1; +} + +static int calculate_task_counter(int frequency) +{ + return cfg_data.total_duration / frequency; }
\ No newline at end of file diff --git a/src/config-deserializer.h b/src/config-deserializer.h index 62fe4aa..6793e85 100644 --- a/src/config-deserializer.h +++ b/src/config-deserializer.h @@ -21,11 +21,14 @@ /** * @brief Deserializes json string to configs structures. + * * @param[in] config_json Json string with config. * @param[out] size Size of config_t array. + * @param[out] task_counter The number of tasks to be executed. + * * @return Dynamically allocated config_t array. * @note Returned array should be released with free. */ -config_t *deserialize_configs(const char *config_json, int *size, int *total_duration); +config_t *deserialize_configs(const char *config_json, int *size, int *task_counter); #endif
\ No newline at end of file @@ -19,50 +19,54 @@ #include <stdio.h> #include "ipc.h" #include "err-check.h" +#include "task-worker.h" +#include "log.h" -struct ipc +static void send_report(const char *report, int status); + +static struct ipc { char *command_id; volatile gint counter; +} ipc_data = +{ + .command_id = NULL }; -ipc_t *ipc_initialize(const char *command_id, int counter) +void ipc_init(const char *command_id, int counter) { - ON_NULL_RETURN_VAL(command_id, NULL); - ON_TRUE_RETURN_VAL(counter < 0, NULL); + ON_NULL_RETURN(command_id); + ON_TRUE_RETURN(counter < 0); - struct ipc *_ipc = (struct ipc *)g_malloc(sizeof(struct ipc)); int len = strlen(command_id) + 1; - _ipc->command_id = (char *)g_malloc(len * sizeof(char)); - snprintf(_ipc->command_id, len, "%s", command_id); - g_atomic_int_set(&_ipc->counter, counter); - - return _ipc; + ipc_data.command_id = (char *)g_malloc(len * sizeof(char)); + snprintf(ipc_data.command_id, len, "%s", command_id); + g_atomic_int_set(&ipc_data.counter, counter); } -void ipc_free(ipc_t *ipc) +void ipc_shutdown() { - ON_NULL_RETURN(ipc); - - if (ipc->command_id) - g_free(ipc->command_id); - - g_free(ipc); + if (ipc_data.command_id) + g_free(ipc_data.command_id); } -void ipc_send_report(ipc_t *ipc, const char *report) +void ipc_send_report(const char *report) { - ON_NULL_RETURN(ipc); ON_NULL_RETURN(report); - if (!g_atomic_int_dec_and_test(&ipc->counter)) + if (!g_atomic_int_dec_and_test(&ipc_data.counter)) { - //TODO set current report state to WORKING + send_report(report, 1 /*WORKING STATE*/); } else { - //TODO set current report state to COMPLETED + send_report(report, 2 /*COMPLETED STATE*/); + cleanup_and_exit(); } +} +//TODO change int status to enum, when project will be integrated with TTSD +static void send_report(const char *report, int status) +{ //TODO send report here }
\ No newline at end of file @@ -15,29 +15,22 @@ */ /** - * @brief Inter-process communication module. - */ -typedef struct ipc ipc_t; - -/** * @brief Initializes IPC with proper command id. + * * @param[in] command_id Token used by IPC for identification. * @param[in] task_counter Number of task to execute. - * - * @return ipc_t structure of NULL on error. - * @remarks Module must be uninitialized with @ipc_free function. + * + * @remarks Module must be uninitialized with @ipc_shutdown function. */ -ipc_t *ipc_initialize(const char *command_id, int counter); +void ipc_init(const char *command_id, int counter); /** * @brief Frees resources used by IPC. - * @param[in] ipc IPC structure. */ -void ipc_free(ipc_t *ipc); +void ipc_shutdown(); /** * @brief Sends report to TTSD. - * @param[in] ipc IPC structure. * @param[in] report Report to send. */ -void ipc_send_report(ipc_t *ipc, const char *report); +void ipc_send_report(const char *report); diff --git a/src/scheduler.c b/src/scheduler.c index 1992e36..6239cd5 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -39,7 +39,6 @@ struct config_item static gboolean timer_func(gpointer user_data); static void free_items(struct config_item *items, size_t items_size); -static gboolean timeout_func(gpointer user_data); scheduler_t *scheduler_create() { @@ -60,7 +59,7 @@ void scheduler_destroy(scheduler_t *scheduler) g_free(scheduler); } -void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t items_size, int timeout) +void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t items_size) { ON_NULL_RETURN(scheduler); ON_NULL_RETURN(task_configs); @@ -74,18 +73,10 @@ void scheduler_change_config(scheduler_t *scheduler, const config_t *task_config for (int i = 0; i < items_size; i++) { scheduler->items[i].worker = scheduler->worker; - scheduler->items[i].task = task_factory_create_single(&task_configs[i]); + scheduler->items[i].task = task_factory_create_task(&task_configs[i]); scheduler->items[i].frequency = task_configs[i].frequency; scheduler->items[i].timer_id = g_timeout_add(scheduler->items[i].frequency, timer_func, &scheduler->items[i]); } - - g_timeout_add(timeout, timeout_func, scheduler); -} - -static gboolean timeout_func(gpointer user_data) -{ - cleanup_and_exit(); - return TRUE; } static gboolean timer_func(gpointer user_data) diff --git a/src/scheduler.h b/src/scheduler.h index 1ba6e50..53ae15b 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -38,9 +38,8 @@ void scheduler_destroy(scheduler_t *scheduler); * @brief Changes current tasks config. * @param[in] task_configs Array with tasks config. * @param[in] config_size Size of the task_configs array. - * @param[in] timeout After this time scheduler will stop working. */ -void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t config_size, int timeout); +void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t config_size); #endif diff --git a/src/task-factory.c b/src/task-factory.c index 4aa889a..9b5375d 100644 --- a/src/task-factory.c +++ b/src/task-factory.c @@ -23,6 +23,7 @@ #include "log.h" #include "err-check.h" #include "macros.h" +#include "ipc.h" static task_t *create_system_report_task(config_options_e options); static task_t *create_load_avg_report_task(); @@ -34,12 +35,10 @@ static void execute_scan_load_avg(task_t *task); static void execute_scan_app_memory(task_t *task); static void execute_scan_apps_cpu(task_t *task); -static void send_report(char *report); - static void release_system_task(task_t *task); static void release_app_task(task_t *task); -task_t *task_factory_create_single(const config_t *config) +task_t *task_factory_create_task(const config_t *config) { switch(config->scope) { @@ -125,7 +124,7 @@ static void execute_scan_system_memory(task_t *task) ON_TRUE_RETURN(ret != 0); char *json_report = report_json_serializer_serialize_system_memory_usage_report(&report); - send_report(json_report); + ipc_send_report(json_report); g_free(json_report); } @@ -141,7 +140,7 @@ static void execute_scan_system_cpu(task_t *task) ON_TRUE_RETURN(ret != 0); char *json_report = report_json_serializer_serialize_system_cpu_usage_report(&report); - send_report(json_report); + ipc_send_report(json_report); g_free(json_report); } @@ -155,7 +154,7 @@ static void execute_scan_load_avg(task_t *task) ON_TRUE_RETURN(ret != 0); char *json_report = report_json_serializer_serialize_system_load_average_report(&report); - send_report(json_report); + ipc_send_report(json_report); g_free(json_report); } @@ -170,7 +169,7 @@ static void execute_scan_app_memory(task_t *task) report_generator_generate_app_memory_usage_report(_app_task->report_generator, &report); char *json_report = report_json_serializer_serialize_apps_memory_usage_report(&report); - send_report(json_report); + ipc_send_report(json_report); g_free(json_report); } @@ -185,16 +184,10 @@ static void execute_scan_apps_cpu(task_t *task) report_generator_generate_app_cpu_usage_report(_app_task->report_generator, 0, &reports); char *json_report = report_json_serializer_serialize_apps_cpu_usage_report(&reports); - send_report(json_report); + ipc_send_report(json_report); g_free(json_report); } -static void send_report(char *report) -{ - //TODO send report here - DBG("%s", report); -} - static void release_system_task(task_t *task) { ON_NULL_RETURN(task); diff --git a/src/task-factory.h b/src/task-factory.h index 9737e20..5ddd665 100644 --- a/src/task-factory.h +++ b/src/task-factory.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * 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. @@ -25,13 +25,6 @@ * @param[in] config Task configuration depends o it. * @return Returns task structure. */ -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_task(const config_t *config); #endif
\ No newline at end of file diff --git a/src/task-worker.c b/src/task-worker.c index 5a50377..37caf1e 100644 --- a/src/task-worker.c +++ b/src/task-worker.c @@ -25,6 +25,7 @@ #include "scheduler.h" #include "task-worker.h" #include "stats.h" +#include "ipc.h" static gboolean sigint_handler(gpointer user_data); @@ -42,36 +43,45 @@ int main(int argc, char *argv[]) { if (argc != 3) { - DBG("Wrong number of input arguments!"); + ERR("Wrong number of input arguments!"); return -1; } int cfg_size = 0; - int total_duration = 0; - data.current_config = deserialize_configs(argv[2], &cfg_size, &total_duration); + int task_counter = 0; + data.current_config = deserialize_configs(argv[2], &cfg_size, &task_counter); + if (task_counter == 0) + { + ERR("Task counter equals 0"); + g_free(data.current_config); + return -2; + } + app_provider_init(); - if (!stats_init()) + ipc_init(argv[1], task_counter); + if (stats_init() != 0) { ERR("Stats module initialization failed"); g_free(data.current_config); + ipc_shutdown(); app_provider_shutdown(); - return -2; + return -3; } data.scheduler = scheduler_create(); data.main_loop = g_main_loop_new(NULL, true); g_unix_signal_add(SIGINT, sigint_handler, data.main_loop); - scheduler_change_config(data.scheduler, data.current_config, cfg_size, total_duration); + scheduler_change_config(data.scheduler, data.current_config, cfg_size); g_main_loop_run(data.main_loop); g_main_loop_unref(data.main_loop); - app_provider_shutdown(); scheduler_destroy(data.scheduler); + app_provider_shutdown(); + ipc_shutdown(); g_free(data.current_config); - DBG("Task-Worker finished!"); return 0; } |