task-factory: app-task implementation 92/182392/9
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Fri, 22 Jun 2018 14:14:25 +0000 (16:14 +0200)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Tue, 26 Jun 2018 11:06:17 +0000 (13:06 +0200)
Change-Id: I52c341a8ed6829714480c4b122267d31e19f14f6
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
src/report-generator.c
src/report-generator.h
src/report-json-serializer.c
src/report-json-serializer.h
src/task-factory.c
src/task-worker [new file with mode: 0755]
src/task.h

index b6069bf..40e1858 100644 (file)
@@ -112,6 +112,17 @@ 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)
+{
+       //TODO implement
+       return NULL;
+}
+
+void report_generator_free_apps_generator(report_generator_apps_t *generator)
+{
+       //TODO implement
+}
+
 static void _calculate_system_cpu_usage(
                struct procfs_system_cpu_usage_info *previous,
                struct procfs_system_cpu_usage_info *current,
@@ -310,6 +321,24 @@ 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)
+{
+       //TODO implement
+       return -1;
+}
+
+
+int report_generator_generate_apps_memory_usage_report(
+               report_generator_apps_t *generator,
+               struct app_memory_usage_report **report)
+{
+       //TODO implement
+       return -1;
+}
+
 int report_generator_generate_load_average_report(struct system_load_average_report *report)
 {
        ON_NULL_RETURN_VAL(report, -1);
index e5eb25c..3927168 100644 (file)
@@ -25,6 +25,9 @@ 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;
+
 /**
  * @brief Creates new instance of report_generator_system_t
  * @return new report_generator_system_t object, or NULL on error
@@ -43,7 +46,7 @@ void report_generator_free_system_generator(report_generator_system_t *generator
 /**
  * @brief Creates new instance of report_generator_process_t
  *
- * @param[in] procsss PID
+ * @param[in] process PID
  * @return new report_generator_system_t object, or NULL on error
  *
  * @remark return value should be released with
@@ -53,11 +56,28 @@ report_generator_process_t *report_generator_new_process_report_generator(int pi
 
 /**
  * @brief Release report_generator_process_t created with
- * @report_generator_new_process_report_generator_
+ * @report_generator_new_process_report_generator
  */
 void report_generator_free_process_generator(report_generator_process_t *generator);
 
 /**
+ * @brief Creates new instance of report_generator_apps_t
+ *
+ * @param[in] Apps id regex.
+ * @return New report_generator_apps_t object, or NULL on error
+ *
+ * @remark return value should be released with
+ * @report_generator_free_apps_generator
+ */
+report_generator_apps_t *report_generator_new_apps_report_generator(const char *app_id);
+
+/**
+ * @brief Release report_generator_apps_t created with
+ * @report_generator_new_apps_report_generator
+ */
+void report_generator_free_apps_generator(report_generator_apps_t *generator);
+
+/**
  * @brief Fills system_cpu_usage_report.
  *
  * When interval > 0 the function will block for interval seconds and return report with
@@ -117,7 +137,7 @@ int report_generator_generate_system_memory_usage_report(
  * cpu usage calculated as average between current time and last call to
  * report_generator_generate_process_cpu_usage_report for generator
  *
- * @param[in] generator system generator
+ * @param[in] generator process generator
  * @param[in] interval in seconds, Should be >= 0
  * @param[out] report
  * @return 0 on success, other value on failure
@@ -130,7 +150,7 @@ int report_generator_generate_process_cpu_usage_report(
 /**
  * @brief Fills process_memory_usage_report.
  *
- * @param[in] generator system generator
+ * @param[in] generator process generator
  * @param[out] report
  * @return 0 on success, other value on failure
  */
@@ -139,6 +159,37 @@ int report_generator_generate_proccess_memory_usage_report(
                struct process_memory_usage_report *report);
 
 /**
+ * @brief Fills apps_cpu_usage_report.
+ *
+ * When interval > 0 the function will block for interval seconds and return report with
+ * apps cpu usage calculated as average during interval period.
+ * When interval == 0 the function will not block and return report with apps
+ * cpu usage calculated as average between current time and last call to
+ * report_generator_generate_process_cpu_usage_report for generator
+ *
+ * @param[in] generator apps generator
+ * @param[in] interval in seconds, Should be >= 0
+ * @param[out] Dynamically allocated array of reports.
+ * @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 interval,
+               struct app_cpu_usage_report **reports);
+
+/**
+ * @brief Fills process_memory_usage_report.
+ *
+ * @param[in] generator apps generator
+ * @param[out] Dynamically allocated array of reports.
+ * @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);
+
+/**
  * @brief Fills system_load_average_report
  *
  * @param[out] report
index 895b314..f6b03fc 100644 (file)
@@ -115,13 +115,13 @@ static void system_cpu_usage_report_to_json_object(JsonBuilder *builder, struct
        json_builder_end_object(builder);
 }
 
-static void app_cpu_usage_report_to_json_object(JsonBuilder *builder, struct app_cpu_usage_report *report)
+static void apps_cpu_usage_report_to_json_object(JsonBuilder *builder, struct app_cpu_usage_report *report)
 {
        //TODO
        //implement after report API changes
 }
 
-static void app_memory_usage_report_to_json_object(JsonBuilder *builder, struct app_memory_usage_report *report)
+static void apps_memory_usage_report_to_json_object(JsonBuilder *builder, struct app_memory_usage_report *report)
 {
        //TODO
        //implement after report API changes
@@ -143,12 +143,12 @@ IMPLEMENT_SERIALIZER_FUNC(
                system_cpu_usage_report_to_json_object)
 
 IMPLEMENT_SERIALIZER_FUNC(
-               report_json_serializer_serialize_app_cpu_usage_report,
+               report_json_serializer_serialize_apps_cpu_usage_report,
                struct app_cpu_usage_report,
-               app_cpu_usage_report_to_json_object)
+               apps_cpu_usage_report_to_json_object)
 
 IMPLEMENT_SERIALIZER_FUNC(
-               report_json_serializer_serialize_app_memory_usage_report,
+               report_json_serializer_serialize_apps_memory_usage_report,
                struct app_memory_usage_report,
-               app_memory_usage_report_to_json_object)
+               apps_memory_usage_report_to_json_object)
 
index c953c00..6b0f926 100644 (file)
@@ -67,13 +67,13 @@ char *report_json_serializer_serialize_process_memory_usage_report(struct proces
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
-char *report_json_serializer_serialize_app_cpu_usage_report(struct app_cpu_usage_report *report);
+char *report_json_serializer_serialize_apps_cpu_usage_report(struct app_cpu_usage_report *report);
 
 /**
  * @brief Serializes app_memory_usage_report to json string
  * @return dynamically allocated string on NULL on error.
  * @remark returned value should be released with @free
  */
-char *report_json_serializer_serialize_app_memory_usage_report(struct app_memory_usage_report *report);
+char *report_json_serializer_serialize_apps_memory_usage_report(struct app_memory_usage_report *report);
 
 #endif
index 8334011..2fa11d8 100644 (file)
 
 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 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_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)
 {
@@ -43,6 +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);
         case TOP:
         default:
             return NULL;
@@ -83,6 +89,30 @@ 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)
+{
+    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);
+
+    switch (options)
+    {
+        case OBSERVE_CPU:
+            _app_task->task.execute = execute_scan_apps_cpu;
+            break;
+        case OBSERVE_MEMORY:
+            _app_task->task.execute = execute_scan_apps_memory;
+            break;
+        default:
+            report_generator_free_apps_generator(_app_task->report_generator);
+            g_free(_app_task);
+            return NULL;
+    }
+
+    return &_app_task->task;
+}
+
 static void execute_scan_system_memory(task_t *task)
 {
     ON_NULL_RETURN(task);
@@ -129,6 +159,38 @@ static void execute_scan_load_avg(task_t *task)
     g_free(json_report);
 }
 
+static void execute_scan_apps_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;
+
+    report_generator_generate_apps_memory_usage_report(_app_task->report_generator, &reports);
+
+    char *json_report = report_json_serializer_serialize_apps_memory_usage_report(reports);
+    send_report(json_report);
+    g_free(reports);
+    g_free(json_report);
+}
+
+static void execute_scan_apps_cpu(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    app_task_t *_app_task = container_of(task, app_task_t, task);
+
+    struct app_cpu_usage_report *reports;
+
+    report_generator_generate_apps_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);
+    g_free(reports);
+    g_free(json_report);
+}
+
 static void send_report(char *report)
 {
     //TODO send report here
@@ -141,6 +203,15 @@ static void release_system_task(task_t *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);
+}
+
+static void release_app_task(task_t *task)
+{
+    ON_NULL_RETURN(task);
+
+    app_task_t *_app_task = container_of(task, app_task_t, task);
+
+    report_generator_free_apps_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
new file mode 100755 (executable)
index 0000000..a60d716
Binary files /dev/null and b/src/task-worker differ
index 96b2435..ab53f0d 100644 (file)
@@ -59,7 +59,7 @@ typedef struct system_task
 typedef struct app_task
 {
     task_t task;
-    report_generator_process_t *report_generator;
+    report_generator_apps_t *report_generator;
 } app_task_t;
 
 /**