diff options
author | Lukasz Stanislawski <l.stanislaws@samsung.com> | 2018-06-29 13:58:39 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@review.ap-northeast-2.compute.internal> | 2018-06-29 13:58:39 +0000 |
commit | 2b6b761274926317fd1e8269132eb8f17675c737 (patch) | |
tree | d689d088a4caefdb63590991c28da63563d8952b | |
parent | b55e6d893ed4a128497a2b7df27723bd843273b6 (diff) | |
parent | 209905c3f8ae3835910f0f01512c554401b5faa0 (diff) | |
download | ttsd-worker-task-2b6b761274926317fd1e8269132eb8f17675c737.tar.gz ttsd-worker-task-2b6b761274926317fd1e8269132eb8f17675c737.tar.bz2 ttsd-worker-task-2b6b761274926317fd1e8269132eb8f17675c737.zip |
Merge "report-generator: implement app_report_generator"
-rw-r--r-- | src/report-generator.c | 104 | ||||
-rw-r--r-- | src/report-generator.h | 36 | ||||
-rw-r--r-- | src/report.h | 6 | ||||
-rw-r--r-- | src/task-factory.c | 34 | ||||
-rwxr-xr-x | src/task-worker | bin | 69648 -> 0 bytes | |||
-rw-r--r-- | src/task.h | 4 |
6 files changed, 125 insertions, 59 deletions
diff --git a/src/report-generator.c b/src/report-generator.c index 40e1858..3b24dbf 100644 --- a/src/report-generator.c +++ b/src/report-generator.c @@ -24,9 +24,9 @@ #include "procfs.h" #include "log.h" #include "err-check.h" +#include "appinfo-provider.h" -struct report_generator_system -{ +struct report_generator_system { /** previous read from procfs */ struct procfs_system_cpu_usage_info previous; }; @@ -45,7 +45,14 @@ struct report_generator_process struct process_ticks_snapshot previous; }; +struct report_generator_app +{ + char *app_id; + report_generator_process_t *process_gen; +}; + static int _report_generator_read_process_ticks(int pid, struct process_ticks_snapshot *ticks); +int _app_report_generator_setup_process_generator(report_generator_app_t *generator); static struct timespec clock_get_monotonic() { @@ -112,15 +119,27 @@ void report_generator_free_process_generator(report_generator_process_t *generat free(generator); } -report_generator_apps_t *report_generator_new_apps_report_generator(const char *app_id) +report_generator_app_t *report_generator_new_app_report_generator(const char *app_id) { - //TODO implement - return NULL; + ON_NULL_RETURN_VAL(app_id, NULL); + + report_generator_app_t *ret = malloc(sizeof(struct report_generator_app)); + if (!ret) { + ERR("malloc failed"); + return NULL; + } + + ret->app_id = strdup(app_id); + _app_report_generator_setup_process_generator(ret); + return ret; } -void report_generator_free_apps_generator(report_generator_apps_t *generator) +void report_generator_free_app_generator(report_generator_app_t *generator) { - //TODO implement + if (!generator) return; + report_generator_free_process_generator(generator->process_gen); + free(generator->app_id); + free(generator); } static void _calculate_system_cpu_usage( @@ -295,7 +314,7 @@ int report_generator_generate_process_cpu_usage_report( return 0; } -int report_generator_generate_proccess_memory_usage_report( +int report_generator_generate_process_memory_usage_report( report_generator_process_t *generator, struct process_memory_usage_report *report) { @@ -321,22 +340,69 @@ int report_generator_generate_proccess_memory_usage_report( return 0; } -int report_generator_generate_apps_cpu_usage_report( - report_generator_apps_t *generator, - int interval, - struct app_cpu_usage_report **reports) +int _app_report_generator_setup_process_generator(report_generator_app_t *generator) { - //TODO implement - return -1; + int pid = app_info_provider_find_main_pid(generator->app_id); + if (pid < 0) { + return -1; + } + + if (!generator->process_gen || generator->process_gen->pid != pid) { + if (generator->process_gen) + report_generator_free_process_generator(generator->process_gen); + generator->process_gen = report_generator_new_process_report_generator(pid); + } + return 0; } +int report_generator_generate_app_cpu_usage_report( + report_generator_app_t *generator, + int interval, + struct app_cpu_usage_report *report) +{ + ON_NULL_RETURN_VAL(generator, -1); + ON_TRUE_RETURN_VAL(interval < 0, -1); + ON_NULL_RETURN_VAL(report, -1); + + if (_app_report_generator_setup_process_generator(generator) != 0) { + ERR("_app_report_generator_setup_process_generator failed."); + return -1; + } + + if (report_generator_generate_process_cpu_usage_report( + generator->process_gen, interval, &report->process_report) != 0) + { + ERR("report_generator_generate_process_cpu_usage_report failed."); + return 0; + } + + strncpy(report->app_id, generator->app_id, sizeof(report->app_id)); + + return 0; +} -int report_generator_generate_apps_memory_usage_report( - report_generator_apps_t *generator, - struct app_memory_usage_report **report) +int report_generator_generate_app_memory_usage_report( + report_generator_app_t *generator, + struct app_memory_usage_report *report) { - //TODO implement - return -1; + ON_NULL_RETURN_VAL(generator, -1); + ON_NULL_RETURN_VAL(report, -1); + + if (_app_report_generator_setup_process_generator(generator)) { + ERR("_app_report_generator_setup_process_generator failed."); + return -1; + } + + if (report_generator_generate_process_memory_usage_report( + generator->process_gen, &report->process_report) != 0) + { + ERR("report_generator_generate_process_memory_usage_report failed."); + return 0; + } + + strncpy(report->app_id, generator->app_id, sizeof(report->app_id)); + + return 0; } int report_generator_generate_load_average_report(struct system_load_average_report *report) diff --git a/src/report-generator.h b/src/report-generator.h index 3927168..aaec19c 100644 --- a/src/report-generator.h +++ b/src/report-generator.h @@ -25,8 +25,8 @@ typedef struct report_generator_system report_generator_system_t; /** Generator for per process report */ typedef struct report_generator_process report_generator_process_t; -/** Generator for apps report */ -typedef struct report_generator_apps report_generator_apps_t; +/** Generator for app report */ +typedef struct report_generator_app report_generator_app_t; /** * @brief Creates new instance of report_generator_system_t @@ -61,21 +61,21 @@ report_generator_process_t *report_generator_new_process_report_generator(int pi void report_generator_free_process_generator(report_generator_process_t *generator); /** - * @brief Creates new instance of report_generator_apps_t + * @brief Creates new instance of report_generator_app_t * - * @param[in] Apps id regex. - * @return New report_generator_apps_t object, or NULL on error + * @param[in] Apps id. + * @return New report_generator_app_t object, or NULL on error * * @remark return value should be released with - * @report_generator_free_apps_generator + * @report_generator_free_app_generator */ -report_generator_apps_t *report_generator_new_apps_report_generator(const char *app_id); +report_generator_app_t *report_generator_new_app_report_generator(const char *app_id); /** - * @brief Release report_generator_apps_t created with - * @report_generator_new_apps_report_generator + * @brief Release report_generator_app_t created with + * @report_generator_new_app_report_generator */ -void report_generator_free_apps_generator(report_generator_apps_t *generator); +void report_generator_free_app_generator(report_generator_app_t *generator); /** * @brief Fills system_cpu_usage_report. @@ -169,25 +169,25 @@ int report_generator_generate_proccess_memory_usage_report( * * @param[in] generator apps generator * @param[in] interval in seconds, Should be >= 0 - * @param[out] Dynamically allocated array of reports. + * @param[out] report * @return 0 on success, other value on failure * @remarks Returned value should be freed. */ -int report_generator_generate_apps_cpu_usage_report( - report_generator_apps_t *generator, +int report_generator_generate_app_cpu_usage_report( + report_generator_app_t *generator, int interval, - struct app_cpu_usage_report **reports); + struct app_cpu_usage_report *report); /** * @brief Fills process_memory_usage_report. * * @param[in] generator apps generator - * @param[out] Dynamically allocated array of reports. + * @param[out] report * @return 0 on success, other value on failure */ -int report_generator_generate_apps_memory_usage_report( - report_generator_apps_t *generator, - struct app_memory_usage_report **report); +int report_generator_generate_app_memory_usage_report( + report_generator_app_t *generator, + struct app_memory_usage_report *report); /** * @brief Fills system_load_average_report diff --git a/src/report.h b/src/report.h index f338ef1..294314d 100644 --- a/src/report.h +++ b/src/report.h @@ -19,6 +19,8 @@ #include <time.h> +#define APP_ID_MAX_SIZE 256 + /** * @brief Load average report */ @@ -76,7 +78,7 @@ struct process_memory_usage_report { * @brief Application CPU usage report. */ struct app_cpu_usage_report { - const char appid[256]; + char app_id[APP_ID_MAX_SIZE]; struct process_cpu_usage_report process_report; }; @@ -84,7 +86,7 @@ struct app_cpu_usage_report { * @brief Application memory usage report. */ struct app_memory_usage_report { - const char appid[256]; + char app_id[APP_ID_MAX_SIZE]; struct process_memory_usage_report process_report; }; diff --git a/src/task-factory.c b/src/task-factory.c index 2fa11d8..1ab2cc6 100644 --- a/src/task-factory.c +++ b/src/task-factory.c @@ -26,12 +26,12 @@ static task_t *create_system_report_task(config_options_e options); static task_t *create_load_avg_report_task(); -static task_t *create_apps_report_task(config_options_e options, const char *regex); +static task_t *create_app_report_task(config_options_e options, const char *regex); static void execute_scan_system_memory(task_t *task); static void execute_scan_system_cpu(task_t *task); static void execute_scan_load_avg(task_t *task); -static void execute_scan_apps_memory(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); @@ -48,7 +48,7 @@ task_t *task_factory_create_single(const config_t *config) case LOAD_AVG: return create_load_avg_report_task(); case APPS: - return create_apps_report_task(config->data.apps.options, config->data.apps.app_id); + return create_app_report_task(config->data.apps.options, config->data.apps.app_id); case TOP: default: return NULL; @@ -89,12 +89,12 @@ static task_t *create_load_avg_report_task() return _load_avg_task; } -static task_t *create_apps_report_task(config_options_e options, const char *regex) +static task_t *create_app_report_task(config_options_e options, const char *app_id) { app_task_t *_app_task = (app_task_t *)g_malloc(sizeof(app_task_t)); _app_task->task.release = release_app_task; - _app_task->report_generator = report_generator_new_apps_report_generator(regex); + _app_task->report_generator = report_generator_new_app_report_generator(app_id); switch (options) { @@ -102,10 +102,10 @@ static task_t *create_apps_report_task(config_options_e options, const char *reg _app_task->task.execute = execute_scan_apps_cpu; break; case OBSERVE_MEMORY: - _app_task->task.execute = execute_scan_apps_memory; + _app_task->task.execute = execute_scan_app_memory; break; default: - report_generator_free_apps_generator(_app_task->report_generator); + report_generator_free_app_generator(_app_task->report_generator); g_free(_app_task); return NULL; } @@ -159,19 +159,18 @@ static void execute_scan_load_avg(task_t *task) g_free(json_report); } -static void execute_scan_apps_memory(task_t *task) +static void execute_scan_app_memory(task_t *task) { ON_NULL_RETURN(task); app_task_t *_app_task = container_of(task, app_task_t, task); - struct app_memory_usage_report *reports; + struct app_memory_usage_report reports; - report_generator_generate_apps_memory_usage_report(_app_task->report_generator, &reports); + report_generator_generate_app_memory_usage_report(_app_task->report_generator, &reports); - char *json_report = report_json_serializer_serialize_apps_memory_usage_report(reports); + char *json_report = report_json_serializer_serialize_apps_memory_usage_report(&reports); send_report(json_report); - g_free(reports); g_free(json_report); } @@ -181,13 +180,12 @@ static void execute_scan_apps_cpu(task_t *task) app_task_t *_app_task = container_of(task, app_task_t, task); - struct app_cpu_usage_report *reports; + struct app_cpu_usage_report reports; - report_generator_generate_apps_cpu_usage_report(_app_task->report_generator, 0, &reports); + 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); + char *json_report = report_json_serializer_serialize_apps_cpu_usage_report(&reports); send_report(json_report); - g_free(reports); g_free(json_report); } @@ -212,6 +210,6 @@ static void release_app_task(task_t *task) app_task_t *_app_task = container_of(task, app_task_t, task); - report_generator_free_apps_generator(_app_task->report_generator); + report_generator_free_app_generator(_app_task->report_generator); g_free(_app_task); -}
\ No newline at end of file +} diff --git a/src/task-worker b/src/task-worker Binary files differdeleted file mode 100755 index 155368b..0000000 --- a/src/task-worker +++ /dev/null @@ -59,7 +59,7 @@ typedef struct system_task typedef struct app_task { task_t task; - report_generator_apps_t *report_generator; + report_generator_app_t *report_generator; } app_task_t; /** @@ -84,4 +84,4 @@ void task_release(task_t *task); */ void task_execute(task_t *task); -#endif
\ No newline at end of file +#endif |