Introduce new generic report_generator_t.
Change-Id: Ib2bdb246413542e08dc22fad22352639ccf253c6
#include "proc-scanner.h"
#include "process.h"
#include "json-schema-defs.h"
+#include "macros.h"
+
+static void report_generator_release_system_generator(report_generator_t *generator);
+static bool report_generator_refresh_system_generator(report_generator_t *generator);
+static void report_generator_generate_system_report(report_generator_t *generator, JsonBuilder *builder);
struct report_generator_system {
+ /** generator base - should be first */
+ struct report_generator base;
/** system cpu usage statistics */
struct sys_stats stats;
+ time_t update_time;
};
struct report_generator_process
struct report_generator_top_closure {
struct sys_stats sys_stats;
- int current_index; int max_index;
+ int current_index;
+ int max_index;
struct process_usage_report *usage_report;
};
if (!ret)
return NULL;
- if (sys_stats_update(&ret->stats) != 0) {
- ERR("stats_update_system_stats failed");
+ ret->base.refresh = report_generator_refresh_system_generator;
+ ret->base.generate = report_generator_generate_system_report;
+ ret->base.release = report_generator_release_system_generator;
+
+ if (report_generator_refresh((report_generator_t*)ret) != true) {
free(ret);
return NULL;
}
return ret;
}
-void report_generator_free_system_generator(report_generator_system_t *generator)
+static void report_generator_release_system_generator(report_generator_t *generator)
{
- if (!generator)
- return;
-
- free(generator);
+ report_generator_system_t *gen = container_of(generator, report_generator_system_t, base);
+ free(gen);
}
report_generator_process_t *report_generator_new_process_report_generator()
generator->user_data = (void*)user_data;
}
-int report_generator_generate_system_report(
- report_generator_system_t *generator,
- struct system_report *report)
+static bool report_generator_refresh_system_generator(
+ report_generator_t *generator)
{
- ON_NULL_RETURN_VAL(generator, -1);
- ON_NULL_RETURN_VAL(report, -1);
+ report_generator_system_t *sys_gen = container_of(generator, report_generator_system_t, base);
- if (sys_stats_update(&generator->stats) != 0) {
+ if (sys_stats_update(&sys_gen->stats) != 0) {
ERR("stats_update_system_stats failed.");
- return -1;
+ return false;
}
+ sys_gen->update_time = clock_realtime_get();
+ return true;
+}
- if (sys_stats_get_cpu_usage_percentage(&generator->stats, &report->cpu))
- {
- ERR("stats_get_system_cpu_usage_average failed");
- return -1;
- }
+static void report_generator_generate_system_report(
+ report_generator_t *generator,
+ JsonBuilder *builder)
+{
+ report_generator_system_t *gen = container_of(generator, report_generator_system_t, base);
- if (sys_stats_get_memory_usage_percentage(&generator->stats, &report->memory) != 0) {
- ERR("stats_get_system_memory_usage failed.");
- return -1;
- }
+ json_builder_begin_object(builder);
- report->time = clock_realtime_get();
+ json_builder_set_member_name(builder, SCHEMA_TYPE);
+ json_builder_add_string_value(builder, SCHEMA_TYPE_SYSTEM);
- return 0;
+ json_builder_set_member_name(builder, SCHEMA_RESULT_DATA_SYSTEM);
+ json_builder_begin_object(builder);
+
+ json_builder_set_member_name(builder, SCHEMA_RESULT_TIME);
+ json_builder_add_int_value(builder, gen->update_time);
+
+ json_builder_set_member_name(builder, SCHEMA_RESULT_CPU);
+ json_builder_add_double_value(builder, sys_stats_get_cpu_usage_percentage(&gen->stats));
+
+ json_builder_set_member_name(builder, SCHEMA_RESULT_MEMORY);
+ json_builder_add_double_value(builder, sys_stats_get_memory_usage_percentage(&gen->stats));
+
+ json_builder_end_object(builder);
+ json_builder_end_object(builder);
}
int report_generator_generate_load_average_report(struct system_load_average_report *report)
return 0;
}
+
+bool report_generator_refresh(report_generator_t *generator)
+{
+ ON_NULL_RETURN_VAL(generator, false);
+
+ if (generator->refresh)
+ return generator->refresh(generator);
+ return true;
+}
+
+void report_generator_generate(report_generator_t *generator, JsonBuilder *builder)
+{
+ ON_NULL_RETURN(generator);
+ ON_NULL_RETURN(builder);
+
+ if (generator->generate) generator->generate(generator, builder);
+}
+
+void report_generator_release(report_generator_t *generator)
+{
+ if (!generator) return;
+ if (generator->release) generator->release(generator);
+}
#include "process.h"
#include "report.h"
+typedef struct report_generator report_generator_t;
+
+/**
+ * @brief Called by @report_generator_refresh
+ */
+typedef bool (*report_generator_refresh_cb)(report_generator_t *generator);
+
+/**
+ * @brief Called by @report_generator_generate
+ */
+typedef void (*report_generator_generate_cb)(report_generator_t *generator, JsonBuilder *builder);
+
+/**
+ * @brief Called by @report_generator_release
+ */
+typedef void (*report_generator_release_cb)(report_generator_t *generator);
+
+struct report_generator
+{
+ report_generator_refresh_cb refresh;
+ report_generator_generate_cb generate;
+ report_generator_release_cb release;
+};
+
+/**
+ * @brief Refresh report data.
+ *
+ * @param[in] generator report generator
+ *
+ * @return true on success, false otherwise.
+ */
+bool report_generator_refresh(report_generator_t *generator);
+
+/**
+ * @brief Refresh report as json.
+ *
+ * @param[in] generator report generator
+ * @param[in] builder the build to which report will be appended to.
+ */
+void report_generator_generate(report_generator_t *generator, JsonBuilder *builder);
+
+/**
+ * @brief Release report generator.
+ *
+ * @param[in] generator report generator
+ */
+void report_generator_release(report_generator_t *generator);
+
/** Generator for system-wide reports */
typedef struct report_generator_system report_generator_system_t;
report_generator_system_t *report_generator_new_system_report_generator();
/**
- * @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);
-
-/**
* @brief Creates new instance of report_generator_process_t
*
* @return new report_generator_system_t object, or NULL on error
void report_generator_free_top_generator(report_generator_top_t *generator);
/**
- * @brief Fills system_usage_report.
- *
- * return report with cpu system usage calculated as average between current
- * time and last call to report_generator_generate_system_cpu_usage_report for
- * generator
- *
- * @param[in] generator system generator
- * @param[out] report
- *
- * @return 0 on success, other value on failure
- */
-int report_generator_generate_system_report(
- report_generator_system_t *generator,
- struct system_report *report);
-
-/**
* @brief Get process_report.
*
* return report with process cpu usage calculated as average between current
#include "err-check.h"
#include "clock.h"
-int sys_stats_get_cpu_usage_percentage(struct sys_stats *stats, float *usage)
+float sys_stats_get_cpu_usage_percentage(struct sys_stats *stats)
{
- ON_NULL_RETURN_VAL(stats, -1);
- ON_NULL_RETURN_VAL(usage, -1);
+ ON_NULL_RETURN_VAL(stats, -1.0f);
if (stats->total_ticks_delta == 0) {
- *usage = 0;
+ return 0.0f;
} else {
- *usage = (float)stats->busy_ticks_delta / stats->total_ticks_delta;
+ return (float)stats->busy_ticks_delta / stats->total_ticks_delta;
}
-
- return 0;
}
int sys_stats_update(struct sys_stats *sys)
return 0;
}
-int sys_stats_get_memory_usage_percentage(struct sys_stats *sys, float *usage)
+float sys_stats_get_memory_usage_percentage(struct sys_stats *sys)
{
- ON_NULL_RETURN_VAL(sys, -1);
- ON_NULL_RETURN_VAL(usage, -1);
-
- *usage = (float)sys->memory_used / sys->total_memory;
+ ON_NULL_RETURN_VAL(sys, -1.0f);
- return 0;
+ if (sys->total_memory == 0.0f) {
+ return 0.0f;
+ } else {
+ return (float)sys->memory_used / sys->total_memory;
+ }
}
int sys_stats_get_load_averages(float *a1, float *a5, float *a15)
* @brief Calculates average cpu usage between two stats snapshots.
*
* @param[in] stats the stats snapshots
- * @param[out] usage the cpu usage as percent.
*
- * @return: 0 on success, other value on error
+ * @return usage the cpu usage as percent.
*/
-int sys_stats_get_cpu_usage_percentage(struct sys_stats *stats, float *usage);
+float sys_stats_get_cpu_usage_percentage(struct sys_stats *stats);
/**
* @brief Takes system statistics snapshot.
/**
* @brief Calculates system memory usage.
*
- * @param[out] usage the memory usage as percent.
+ * @param[in] stats the stats snapshots
*
- * @return: 0 on success, other value on error
+ * @return usage the system memory usage as percent.
*/
-int sys_stats_get_memory_usage_percentage(struct sys_stats *stats, float *usage);
+float sys_stats_get_memory_usage_percentage(struct sys_stats *stats);
/**
* @brief Gets system load averages stats.
JsonGenerator *generator;
} process_task_t;
+/**
+ * @brief System task structure.
+ */
+typedef struct system_task
+{
+ task_t task;
+ report_generator_system_t *report_generator;
+ JsonBuilder *builder;
+ JsonGenerator *generator;
+} system_task_t;
+
task_t *task_factory_create_task(const config_t *config)
{
switch (config->scope)
_system_task->task.release = release_system_task;
_system_task->report_generator = report_generator_new_system_report_generator();
_system_task->task.execute = execute_scan_system;
+ _system_task->builder = json_builder_new();
+ _system_task->generator = json_generator_new();
return &_system_task->task;
}
system_task_t *_system_task = container_of(task, system_task_t, task);
- struct system_report report;
+ json_builder_reset(_system_task->builder);
- int ret = report_generator_generate_system_report(_system_task->report_generator, &report);
- ON_TRUE_RETURN(ret != 0);
+ if (report_generator_refresh((report_generator_t*)_system_task->report_generator) != true) {
+ return;
+ }
+
+ report_generator_generate((report_generator_t*)_system_task->report_generator, _system_task->builder);
+
+ JsonNode *root = json_builder_get_root(_system_task->builder);
+ json_generator_set_root(_system_task->generator, root);
+ char *json_report = json_generator_to_data(_system_task->generator, NULL);
- char *json_report = report_json_serializer_serialize_system_report(&report);
ipc_send_report(json_report);
g_free(json_report);
}
system_task_t *_system_task = container_of(task, system_task_t, task);
- report_generator_free_system_generator(_system_task->report_generator);
+ report_generator_release((report_generator_t*)_system_task->report_generator);
+ g_object_unref(_system_task->builder);
+ g_object_unref(_system_task->generator);
g_free(_system_task);
}
task_release_cb release;
};
-/**
- * @brief System task structure.
- */
-typedef struct system_task
-{
- task_t task;
- report_generator_system_t *report_generator;
-} system_task_t;
/**
* @brief Top task structure.