Debian Codegraph @

src/kpi/dlt-kpi.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * SPDX license identifier: MPL-2.0
3 *
4 * Copyright (C) 2011-2015, BMW AG
5 *
6 * This file is part of COVESA Project DLT - Diagnostic Log and Trace.
7 *
8 * This Source Code Form is subject to the terms of the
9 * Mozilla Public License (MPL), v. 2.0.
10 * If a copy of the MPL was not distributed with this file,
11 * You can obtain one at http://mozilla.org/MPL/2.0/.
12 *
13 * For further information see http://www.covesa.org/.
14 */
15
16 /*!
17 * \author Sven Hassler <[email protected]>
18 *
19 * \copyright Copyright © 2011-2015 BMW AG. \n
20 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
21 *
22 * \file dlt-kpi.c
23 */
24
25 #include "dlt-kpi.h"
26
27 #include <signal.h>
28 #include <dirent.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <pthread.h>
32
33 DLT_DECLARE_CONTEXT(kpi_ctx);
34
35 DltKpiConfig config;
36
37 static volatile sig_atomic_t stop_loop = 0;
38 static DltKpiProcessList *list, *new_process_list, *stopped_process_list, *update_process_list;
39 static struct timespec _tmp_time;
40 static pthread_mutex_t process_list_mutex;
41
42 void dlt_kpi_stop_loops(int sig);
43 void dlt_kpi_init_sigterm_handler();
44 DltReturnValue dlt_kpi_init_process_lists();
45 DltReturnValue dlt_kpi_free_process_lists();
46 void *dlt_kpi_start_process_thread();
47 DltReturnValue dlt_kpi_process_loop();
48 DltReturnValue dlt_kpi_update_process_list(DltKpiProcessList *list, unsigned long int time_dif_ms);
49 void *dlt_kpi_start_irq_thread();
50 DltReturnValue dlt_kpi_irq_loop();
51 void *dlt_kpi_start_check_thread();
52 DltReturnValue dlt_kpi_check_loop();
53 DltReturnValue dlt_kpi_log_check_commandlines();
54
55 unsigned long int timespec_to_millis(struct timespec *time)
56 {
57 return (time->tv_sec) * 1000 + (time->tv_nsec / 1000000);
58 }
59
60 unsigned long int get_millis()
61 {
62 clock_gettime(CLOCK_REALTIME, &_tmp_time);
63 return timespec_to_millis(&_tmp_time);
64 }
65
66 int main(int argc, char **argv)
67 {
68 printf("Launching dlt-kpi...\n");
69
70 if (dlt_kpi_init(argc, argv, &config) < DLT_RETURN_OK) {
71 fprintf(stderr, "Initialization error!\n");
72 return -1;
73 }
74
75 dlt_kpi_init_sigterm_handler();
76
77 if (dlt_kpi_init_process_lists() < DLT_RETURN_OK) {
78 fprintf(stderr, "Error occurred initializing process lists\n");
79 return -1;
80 }
81
82 if (pthread_mutex_init(&process_list_mutex, NULL) < 0) {
83 fprintf(stderr, "Error occurred initializing mutex\n");
84 return -1;
85 }
86
87 DLT_REGISTER_APP("PROC", "/proc/-filesystem logger application");
88 DLT_REGISTER_CONTEXT_LL_TS(kpi_ctx, "PROC", "/proc/-filesystem logger context", config.log_level, 1);
89
90 pthread_t process_thread;
91 pthread_t irq_thread;
92 pthread_t check_thread;
93
94 if (pthread_create(&process_thread, NULL, &dlt_kpi_start_process_thread, NULL) != 0) {
95 fprintf(stderr, "Could not create thread\n");
96 return -1;
97 }
98
99 if (pthread_create(&irq_thread, NULL, &dlt_kpi_start_irq_thread, NULL) != 0) {
100 fprintf(stderr, "Could not create thread\n");
101 return -1;
102 }
103
104 if (pthread_create(&check_thread, NULL, &dlt_kpi_start_check_thread, NULL) != 0) {
105 fprintf(stderr, "Could not create thread\n");
106 return -1;
107 }
108
109 pthread_join(process_thread, NULL);
110 pthread_join(irq_thread, NULL);
111 pthread_join(check_thread, NULL);
112
113 DLT_UNREGISTER_CONTEXT(kpi_ctx);
114 DLT_UNREGISTER_APP();
115
116 pthread_mutex_destroy(&process_list_mutex);
117
118 dlt_kpi_free_process_lists();
119
120 printf("Done.\n");
121
122 return 0;
123 }
124
125 void dlt_kpi_init_sigterm_handler()
126 {
127 struct sigaction action;
128 memset(&action, 0, sizeof(struct sigaction));
129 action.sa_handler = dlt_kpi_stop_loops;
130
131 sigaction(SIGTERM, &action, NULL);
132 }
133
134 void dlt_kpi_stop_loops(int sig)
135 {
136 if (sig > -1)
137 fprintf(stderr, "dlt-kpi is now terminating due to signal %d...\n", sig);
138 else
139 fprintf(stderr, "dlt-kpi is now terminating due to an error...\n");
140
141 stop_loop = 1;
142 }
143
144 DltReturnValue dlt_kpi_init_process_lists()
145 {
146 if ((list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR;
147
148 if ((new_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR;
149
150 if ((stopped_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR;
151
152 if ((update_process_list = dlt_kpi_create_process_list()) == NULL) return DLT_RETURN_ERROR;
153
154 return DLT_RETURN_OK;
155 }
156
157 DltReturnValue dlt_kpi_free_process_lists()
158 {
159 DltReturnValue ret = DLT_RETURN_OK;
160
161 if (dlt_kpi_free_process_list(list) < DLT_RETURN_OK)
162 ret = DLT_RETURN_ERROR;
163
164 if (dlt_kpi_free_process_list(new_process_list) < DLT_RETURN_OK)
165 ret = DLT_RETURN_ERROR;
166
167 if (dlt_kpi_free_process_list(stopped_process_list) < DLT_RETURN_OK)
168 ret = DLT_RETURN_ERROR;
169
170 if (dlt_kpi_free_process_list(update_process_list) < DLT_RETURN_OK)
171 ret = DLT_RETURN_ERROR;
172
173 return ret;
174 }
175
176 void *dlt_kpi_start_process_thread()
177 {
178 if (dlt_kpi_process_loop() < DLT_RETURN_OK)
179 dlt_kpi_stop_loops(-1);
180
181 return NULL;
182 }
183
184 DltReturnValue dlt_kpi_process_loop()
185 {
186 static unsigned long int old_millis, sleep_millis, dif_millis;
187 struct timespec ts;
188
189 old_millis = get_millis();
190
191 while (!stop_loop) {
192 /*DltReturnValue ret = */ dlt_kpi_update_process_list(list, config.process_log_interval);
193 /*if(ret < DLT_RETURN_OK) */
194 /* return ret; */
195
196 dif_millis = get_millis() - old_millis;
197
198 if (dif_millis >= (unsigned long)(config.process_log_interval))
199 sleep_millis = 0;
200 else
201 sleep_millis = config.process_log_interval - dif_millis;
202
203 ts.tv_sec = (sleep_millis * NANOSEC_PER_MILLISEC) / NANOSEC_PER_SEC;
204 ts.tv_nsec = (sleep_millis * NANOSEC_PER_MILLISEC) % NANOSEC_PER_SEC;
205 nanosleep(&ts, NULL);
206
207 old_millis = get_millis();
208 }
209
210 return DLT_RETURN_OK;
211 }
212
213 DltReturnValue dlt_kpi_log_list(DltKpiProcessList *list,
214 DltReturnValue (*process_callback)(DltKpiProcess *, char *, int),
215 char *title,
216 int delete_elements)
217 {
218 if ((list == NULL) || (process_callback == NULL) || (title == NULL)) {
219 fprintf(stderr, "%s: Invalid Parameter (NULL)\n", __func__);
220 return DLT_RETURN_WRONG_PARAMETER;
221 }
222
223 dlt_kpi_reset_cursor(list);
224
225 if (list->cursor == NULL)
226 return DLT_RETURN_OK; /* list empty; nothing to do */
227
228 /* Synchronization message */
229 DLT_LOG(kpi_ctx, config.log_level, DLT_STRING(title), DLT_STRING("BEG"));
230
231 DltReturnValue ret;
232 DltContextData data;
233
234 char buffer[BUFFER_SIZE];
235 buffer[0] = '\0';
236
237 if ((ret = dlt_user_log_write_start(&kpi_ctx, &data, config.log_level)) < DLT_RETURN_OK) {
238 fprintf(stderr, "%s: dlt_user_log_write_start() returned error.\n", __func__);
239 return ret;
240 }
241
242 if ((ret = dlt_user_log_write_string(&data, title)) < DLT_RETURN_OK) {
243 fprintf(stderr, "%s: dlt_user_log_write_string() returned error.\n", __func__);
244 return ret;
245 }
246
247 do {
248 if ((ret = (*process_callback)(list->cursor, buffer, sizeof(buffer) - 1)) < DLT_RETURN_OK)
249 return ret;
250
251 if ((ret = dlt_user_log_write_string(&data, buffer)) < DLT_RETURN_OK) {
252 /* Log buffer full => Write log and start new one*/
253 if ((ret = dlt_user_log_write_finish(&data)) < DLT_RETURN_OK) {
254 fprintf(stderr, "%s: dlt_user_log_write_finish() returned error.\n", __func__);
255 return ret;
256 }
257
258 if ((ret = dlt_user_log_write_start(&kpi_ctx, &data, config.log_level)) < DLT_RETURN_OK) {
259 fprintf(stderr, "%s: dlt_user_log_write_start() returned error.\n", __func__);
260 return ret;
261 }
262
263 if ((ret = dlt_user_log_write_string(&data, title)) < DLT_RETURN_OK) {
264 fprintf(stderr, "%s: dlt_user_log_write_string() returned error.\n", __func__);
265 return ret;
266 }
267 }
268 else if (delete_elements)
269 {
270 if ((ret = dlt_kpi_remove_process_at_cursor(list)) < DLT_RETURN_OK)
271 return ret;
272 }
273 else {
274 list->cursor = list->cursor->next;
275 }
276 } while (list->cursor != NULL);
277
278 if ((ret = dlt_user_log_write_finish(&data)) < DLT_RETURN_OK) {
279 fprintf(stderr, "%s: dlt_user_log_write_finish() returned error.\n", __func__);
280 return ret;
281 }
282
283 /* Synchronization message */
284 DLT_LOG(kpi_ctx, config.log_level, DLT_STRING(title), DLT_STRING("END"));
285
286 return DLT_RETURN_OK;
287 }
288
289 DltReturnValue dlt_kpi_update_process_list(DltKpiProcessList *list, unsigned long int time_dif_ms)
290 {
291 static char *strchk;
292 static DltReturnValue tmp_ret;
293 static struct dirent *current_dir;
294 static pid_t current_dir_pid;
295
296 if (list == NULL) {
297 fprintf(stderr, "dlt_kpi_update_process_list(): Nullpointer parameter");
298 return DLT_RETURN_WRONG_PARAMETER;
299 }
300
301 DIR *proc_dir = opendir("/proc");
302
303 if (proc_dir == NULL) {
304 dlt_log(LOG_ERR, "Could not open /proc/ !\n");
305 return DLT_RETURN_ERROR;
306 }
307
308 current_dir = readdir(proc_dir);
309 dlt_kpi_reset_cursor(list);
310
311 int debug_process_count = 0;
312
313 if (pthread_mutex_lock(&process_list_mutex) < 0) {
314 fprintf(stderr, "Can't lock mutex\n");
315 return DLT_RETURN_ERROR;
316 }
317
318 while (1) {
319 if (current_dir == NULL) {
320 /* no more active processes.. delete all remaining processes in the list */
321 if (list->cursor != NULL)
322 while (list->cursor != NULL) {
323 if ((tmp_ret =
324 dlt_kpi_add_process_after_cursor(stopped_process_list,
325 dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK)
326 return tmp_ret;
327
328 dlt_kpi_remove_process_at_cursor(list);
329 }
330
331 break;
332 }
333
334 current_dir_pid = strtol(current_dir->d_name, &strchk, 10);
335
336 if ((*strchk != '\0') || (current_dir_pid <= 0)) {
337 /* no valid PID */
338 current_dir = readdir(proc_dir); /* next process in proc-fs */
339 continue;
340 }
341
342 /* compare the /proc/-filesystem with our process-list */
343 if ((list->cursor == NULL) || (current_dir_pid < list->cursor->pid)) { /* New Process */
344 DltKpiProcess *new_process = dlt_kpi_create_process(current_dir_pid);
345
346 if (new_process == NULL) {
347 fprintf(stderr, "Error: Could not create process (out of memory?)\n");
348 return DLT_RETURN_ERROR;
349 }
350
351 if ((tmp_ret = dlt_kpi_add_process_before_cursor(list, new_process)) < DLT_RETURN_OK)
352 return tmp_ret;
353
354 if ((tmp_ret =
355 dlt_kpi_add_process_before_cursor(new_process_list,
356 dlt_kpi_clone_process(new_process))) < DLT_RETURN_OK)
357 return tmp_ret;
358
359 current_dir = readdir(proc_dir); /* next process in proc-fs */
360 debug_process_count++;
361 }
362 else if (current_dir_pid > list->cursor->pid) /* Process ended */
363 {
364 if ((tmp_ret =
365 dlt_kpi_add_process_after_cursor(stopped_process_list,
366 dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK)
367 return tmp_ret;
368
369 if ((tmp_ret = dlt_kpi_remove_process_at_cursor(list)) < DLT_RETURN_OK)
370 return tmp_ret;
371 }
372 else if (current_dir_pid == list->cursor->pid) /* Staying process */
373 {
374 /* update data */
375 if ((tmp_ret = dlt_kpi_update_process(list->cursor, time_dif_ms)) < DLT_RETURN_OK)
376 return tmp_ret;
377
378 if (list->cursor->cpu_time > 0) /* only log active processes */
379 if ((tmp_ret =
380 dlt_kpi_add_process_after_cursor(update_process_list,
381 dlt_kpi_clone_process(list->cursor))) < DLT_RETURN_OK) {
382 fprintf(stderr, "dlt_kpi_update_process_list: Can't add process to list updateProcessList\n");
383 return tmp_ret;
384 }
385
386 if ((tmp_ret = dlt_kpi_increment_cursor(list)) < DLT_RETURN_OK) /* next process in list */
387 return tmp_ret;
388
389 current_dir = readdir(proc_dir); /* next process in proc-fs */
390 debug_process_count++;
391 }
392 }
393
394 if (pthread_mutex_unlock(&process_list_mutex) < 0) {
395 fprintf(stderr, "Can't unlock mutex\n");
396 return DLT_RETURN_ERROR;
397 }
398
399 /* Log new processes */
400 if ((tmp_ret = dlt_kpi_log_list(new_process_list, &dlt_kpi_get_msg_process_new, "NEW", 1)) < DLT_RETURN_OK)
401 return tmp_ret;
402
403 /* Log stopped processes */
404 if ((tmp_ret = dlt_kpi_log_list(stopped_process_list, &dlt_kpi_get_msg_process_stop, "STP", 1)) < DLT_RETURN_OK)
405 return tmp_ret;
406
407 /* Log active processes */
408 if ((tmp_ret = dlt_kpi_log_list(update_process_list, &dlt_kpi_get_msg_process_update, "ACT", 1)) < DLT_RETURN_OK)
409 return tmp_ret;
410
411 if (closedir(proc_dir) < 0)
412 fprintf(stderr, "Could not close /proc/ directory\n");
413
414 return DLT_RETURN_OK;
415 }
416
417 void *dlt_kpi_start_irq_thread()
418 {
419 if (dlt_kpi_irq_loop() < DLT_RETURN_OK)
420 dlt_kpi_stop_loops(-1);
421
422 return NULL;
423 }
424
425 DltReturnValue dlt_kpi_irq_loop()
426 {
427 static unsigned long int old_millis, sleep_millis, dif_millis;
428 struct timespec ts;
429
430 old_millis = get_millis();
431
432 while (!stop_loop) {
433 /*DltReturnValue ret = */ dlt_kpi_log_interrupts(&kpi_ctx, config.log_level);
434 /*if(ret < DLT_RETURN_OK) */
435 /* return ret; */
436
437 dif_millis = get_millis() - old_millis;
438
439 if (dif_millis >= (unsigned long)(config.irq_log_interval))
440 sleep_millis = 0;
441 else
442 sleep_millis = config.irq_log_interval - dif_millis;
443
444 ts.tv_sec = (sleep_millis * NANOSEC_PER_MILLISEC) / NANOSEC_PER_SEC;
445 ts.tv_nsec = (sleep_millis * NANOSEC_PER_MILLISEC) % NANOSEC_PER_SEC;
446 nanosleep(&ts, NULL);
447
448 old_millis = get_millis();
449 }
450
451 return DLT_RETURN_OK;
452 }
453
454 void *dlt_kpi_start_check_thread()
455 {
456 if (dlt_kpi_check_loop() < DLT_RETURN_OK)
457 dlt_kpi_stop_loops(-1);
458
459 return NULL;
460 }
461
462 DltReturnValue dlt_kpi_check_loop()
463 {
464 static unsigned long int old_millis, sleep_millis, dif_millis;
465 struct timespec ts;
466
467 old_millis = get_millis();
468
469 while (!stop_loop) {
470 /*DltReturnValue ret = */ dlt_kpi_log_check_commandlines();
471 /*if(ret < DLT_RETURN_OK) */
472 /* return ret; */
473
474 dif_millis = get_millis() - old_millis;
475
476 if (dif_millis >= (unsigned long)(config.check_log_interval))
477 sleep_millis = 0;
478 else
479 sleep_millis = config.check_log_interval - dif_millis;
480
481 ts.tv_sec = (sleep_millis * NANOSEC_PER_MILLISEC) / NANOSEC_PER_SEC;
482 ts.tv_nsec = (sleep_millis * NANOSEC_PER_MILLISEC) % NANOSEC_PER_SEC;
483 nanosleep(&ts, NULL);
484
485 old_millis = get_millis();
486 }
487
488 return DLT_RETURN_OK;
489 }
490
491 DltReturnValue dlt_kpi_log_check_commandlines()
492 {
493 if (pthread_mutex_lock(&process_list_mutex) < 0) {
494 fprintf(stderr, "Can't lock mutex\n");
495 return DLT_RETURN_ERROR;
496 }
497
498 DltReturnValue ret = dlt_kpi_log_list(list, dlt_kpi_get_msg_process_commandline, "CHK", 0);
499
500 if (pthread_mutex_unlock(&process_list_mutex) < 0) {
501 fprintf(stderr, "Can't unlock mutex\n");
502 return DLT_RETURN_ERROR;
503 }
504
505 return ret;
506 }