1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* 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"
#include "ipc.h"
static task_t *create_system_report_task(config_options_e options);
static task_t *create_load_avg_report_task();
static task_t *create_app_report_task(config_options_e options, const char *regex);
static task_t *create_top_report_task(struct config_data_top options);
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_app_memory(task_t *task);
static void execute_scan_apps_cpu(task_t *task);
static void execute_scan_top_cpu(task_t *task);
static void execute_scan_top_memory(task_t *task);
static void release_system_task(task_t *task);
static void release_app_task(task_t *task);
static void release_top_task(task_t *task);
task_t *task_factory_create_task(const config_t *config)
{
switch(config->scope)
{
case SYSTEM:
return create_system_report_task(config->data.system.options);
case LOAD_AVG:
return create_load_avg_report_task();
case APPS:
return create_app_report_task(config->data.apps.options, config->data.apps.app_id);
case TOP:
return create_top_report_task(config->data.top);
default:
return NULL;
}
}
static task_t *create_system_report_task(config_options_e options)
{
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:
report_generator_free_system_generator(_system_task->report_generator);
g_free(_system_task);
return NULL;
}
return &_system_task->task;
}
static task_t *create_load_avg_report_task()
{
task_t *_load_avg_task = (task_t *)g_malloc(sizeof(task_t));
_load_avg_task->execute = execute_scan_load_avg;
_load_avg_task->release = NULL;
return _load_avg_task;
}
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_app_report_generator(app_id);
switch (options)
{
case OBSERVE_CPU:
_app_task->task.execute = execute_scan_apps_cpu;
break;
case OBSERVE_MEMORY:
_app_task->task.execute = execute_scan_app_memory;
break;
default:
report_generator_free_app_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);
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);
ipc_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);
ipc_send_report(json_report);
g_free(json_report);
}
static void execute_scan_load_avg(task_t *task)
{
ON_NULL_RETURN(task);
struct system_load_average_report report;
int ret = report_generator_generate_load_average_report(&report);
ON_TRUE_RETURN(ret != 0);
char *json_report = report_json_serializer_serialize_system_load_average_report(&report);
ipc_send_report(json_report);
g_free(json_report);
}
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 report;
report_generator_generate_app_memory_usage_report(_app_task->report_generator, &report);
char *json_report = report_json_serializer_serialize_apps_memory_usage_report(&report);
ipc_send_report(json_report);
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_app_cpu_usage_report(_app_task->report_generator, 0, &reports);
char *json_report = report_json_serializer_serialize_apps_cpu_usage_report(&reports);
ipc_send_report(json_report);
g_free(json_report);
}
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);
}
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_app_generator(_app_task->report_generator);
g_free(_app_task);
}
static task_t *create_top_report_task(struct config_data_top options)
{
top_task_t *_top_task;
report_generator_top_type_e type;
task_execute_cb execute;
switch (options.subject) {
case TOP_SUBJECT_APPS:
type = REPORT_GENERATOR_TOP_TYPE_APPS;
break;
case TOP_SUBJECT_ALL:
type = REPORT_GENERATOR_TOP_TYPE_ALL;
break;
default:
return NULL;
}
switch (options.options) {
case OBSERVE_CPU:
execute = execute_scan_top_cpu;
break;
case OBSERVE_MEMORY:
execute = execute_scan_top_memory;
break;
default:
return NULL;
}
_top_task = (top_task_t *)g_malloc(sizeof(top_task_t));
_top_task->task.execute = execute;
_top_task->task.release = release_top_task;
_top_task->report_generator = report_generator_new_top_report_generator(type, options.top);
if (!_top_task->report_generator) {
g_free(_top_task);
return NULL;
}
return &_top_task->task;
}
static void release_top_task(task_t *task)
{
ON_NULL_RETURN(task);
top_task_t *_top_task = container_of(task, top_task_t, task);
report_generator_free_top_generator(_top_task->report_generator);
g_free(_top_task);
}
static void execute_scan_top_cpu(task_t *task)
{
ON_NULL_RETURN(task);
top_task_t *_top_task = container_of(task, top_task_t, task);
struct app_cpu_usage_report *reports;
int n_reports;
report_generator_generate_top_cpu_report(_top_task->report_generator, &reports, &n_reports);
char *json_report = report_json_serializer_serialize_top_cpu_usage_reports(reports, n_reports);
ipc_send_report(json_report);
g_free(json_report);
free(reports);
}
static void execute_scan_top_memory(task_t *task)
{
ON_NULL_RETURN(task);
top_task_t *_top_task = container_of(task, top_task_t, task);
struct app_memory_usage_report *reports;
int n_reports;
report_generator_generate_top_memory_report(_top_task->report_generator, &reports, &n_reports);
char *json_report = report_json_serializer_serialize_top_memory_usage_reports(reports, n_reports);
ipc_send_report(json_report);
g_free(json_report);
free(reports);
}
|