error handling on setup live tracing
[lttngtop.git] / src / common.c
CommitLineData
1fc22eb4 1/*
aa15ac1c 2 * Copyright (C) 2011-2012 Julien Desfossez
1fc22eb4
JD
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
71bd7ce1
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1fc22eb4
JD
16 */
17
1dec520a 18#include <babeltrace/ctf/events.h>
1fc22eb4 19#include <stdlib.h>
ceb3a221 20#include <linux/unistd.h>
1fc22eb4
JD
21#include <string.h>
22#include "common.h"
23
4adc8274 24uint64_t get_cpu_id(const struct bt_ctf_event *event)
d67167cd 25{
2e0a1190 26 const struct bt_definition *scope;
d67167cd
JD
27 uint64_t cpu_id;
28
29 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT);
30 cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(event, scope, "cpu_id"));
31 if (bt_ctf_field_get_error()) {
32 fprintf(stderr, "[error] get cpu_id\n");
33 return -1ULL;
34 }
35
36 return cpu_id;
37}
38
4adc8274 39uint64_t get_context_tid(const struct bt_ctf_event *event)
1dec520a 40{
2e0a1190 41 const struct bt_definition *scope;
1dec520a
JD
42 uint64_t tid;
43
44 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
45 tid = bt_ctf_get_int64(bt_ctf_get_field(event,
46 scope, "_tid"));
47 if (bt_ctf_field_get_error()) {
48 fprintf(stderr, "Missing tid context info\n");
49 return -1ULL;
50 }
51
52 return tid;
53}
54
4adc8274 55uint64_t get_context_pid(const struct bt_ctf_event *event)
1dec520a 56{
2e0a1190 57 const struct bt_definition *scope;
1dec520a
JD
58 uint64_t pid;
59
60 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
61 pid = bt_ctf_get_int64(bt_ctf_get_field(event,
62 scope, "_pid"));
63 if (bt_ctf_field_get_error()) {
64 fprintf(stderr, "Missing pid context info\n");
65 return -1ULL;
66 }
67
68 return pid;
69}
70
4adc8274 71uint64_t get_context_ppid(const struct bt_ctf_event *event)
1dec520a 72{
2e0a1190 73 const struct bt_definition *scope;
1dec520a
JD
74 uint64_t ppid;
75
76 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
77 ppid = bt_ctf_get_int64(bt_ctf_get_field(event,
78 scope, "_ppid"));
79 if (bt_ctf_field_get_error()) {
80 fprintf(stderr, "Missing ppid context info\n");
81 return -1ULL;
82 }
83
84 return ppid;
85}
86
4adc8274 87char *get_context_comm(const struct bt_ctf_event *event)
1dec520a 88{
2e0a1190 89 const struct bt_definition *scope;
1dec520a
JD
90 char *comm;
91
92 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
93 comm = bt_ctf_get_char_array(bt_ctf_get_field(event,
94 scope, "_procname"));
95 if (bt_ctf_field_get_error()) {
96 fprintf(stderr, "Missing comm context info\n");
97 return NULL;
98 }
99
100 return comm;
101}
102
59288610
MB
103/*
104 * To get the parent process, put the pid in the tid field
105 * because the parent process gets pid = tid
59288610 106 */
1fc22eb4
JD
107struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
108{
1fc22eb4
JD
109 struct processtop *tmp;
110
30b646c4
JD
111 tmp = g_hash_table_lookup(ctx->process_hash_table,
112 (gconstpointer) (unsigned long) tid);
113
114 return tmp;
1fc22eb4
JD
115}
116
117struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
118 unsigned long timestamp)
119{
120 struct processtop *newproc;
121
122 /* if the PID already exists, we just rename the process */
123 /* FIXME : need to integrate with clone/fork/exit to be accurate */
124 newproc = find_process_tid(ctx, tid, comm);
125 if (!newproc) {
559c9f86 126 newproc = g_new0(struct processtop, 1);
1fc22eb4
JD
127 newproc->tid = tid;
128 newproc->birth = timestamp;
129 newproc->process_files_table = g_ptr_array_new();
ceb3a221 130 newproc->files_history = NULL;
b093de8a
MB
131 newproc->totalfileread = 0;
132 newproc->totalfilewrite = 0;
133 newproc->fileread = 0;
134 newproc->filewrite = 0;
135 newproc->syscall_info = NULL;
59288610 136 newproc->threadparent = NULL;
1fc22eb4 137 newproc->threads = g_ptr_array_new();
85db4618 138 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4 139 g_ptr_array_add(ctx->process_table, newproc);
30b646c4
JD
140 g_hash_table_insert(ctx->process_hash_table,
141 (gpointer) (unsigned long) tid, newproc);
e05a35a6
JD
142
143 ctx->nbnewthreads++;
144 ctx->nbthreads++;
1fc22eb4
JD
145 }
146 newproc->comm = strdup(comm);
147
148 return newproc;
149}
150
151struct processtop* update_proc(struct processtop* proc, int pid, int tid,
152 int ppid, char *comm)
153{
154 if (proc) {
155 proc->pid = pid;
156 proc->tid = tid;
157 proc->ppid = ppid;
158 if (strcmp(proc->comm, comm) != 0) {
159 free(proc->comm);
160 proc->comm = strdup(comm);
161 }
162 }
163 return proc;
164}
165
166/*
167 * This function just sets the time of death of a process.
168 * When we rotate the cputime we remove it from the process list.
169 */
170void death_proc(struct lttngtop *ctx, int tid, char *comm,
171 unsigned long timestamp)
172{
173 struct processtop *tmp;
174 tmp = find_process_tid(ctx, tid, comm);
30b646c4
JD
175
176 g_hash_table_remove(ctx->process_hash_table,
177 (gpointer) (unsigned long) tid);
e05a35a6 178 if (tmp && strcmp(tmp->comm, comm) == 0) {
1fc22eb4 179 tmp->death = timestamp;
e05a35a6
JD
180 ctx->nbdeadthreads++;
181 ctx->nbthreads--;
182 }
1fc22eb4
JD
183}
184
185struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
186 unsigned long timestamp)
187{
188 struct processtop *tmp;
189 tmp = find_process_tid(ctx, tid, comm);
190 if (tmp && strcmp(tmp->comm, comm) == 0)
191 return tmp;
192 return add_proc(ctx, tid, comm, timestamp);
193}
194
59288610
MB
195struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid,
196 unsigned long timestamp)
197{
198 struct processtop *tmp;
199 tmp = find_process_tid(ctx, tid, NULL);
200 if (tmp && tmp->pid == pid)
201 return tmp;
202 return add_proc(ctx, tid, "Unknown", timestamp);
203}
204
1fc22eb4
JD
205void add_thread(struct processtop *parent, struct processtop *thread)
206{
207 gint i;
208 struct processtop *tmp;
209
210 for (i = 0; i < parent->threads->len; i++) {
211 tmp = g_ptr_array_index(parent->threads, i);
212 if (tmp == thread)
213 return;
214 }
215 g_ptr_array_add(parent->threads, thread);
216}
217
218struct cputime* add_cpu(int cpu)
219{
220 struct cputime *newcpu;
221
559c9f86 222 newcpu = g_new0(struct cputime, 1);
1fc22eb4
JD
223 newcpu->id = cpu;
224 newcpu->current_task = NULL;
85db4618 225 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
226
227 g_ptr_array_add(lttngtop.cpu_table, newcpu);
228
229 return newcpu;
230}
231struct cputime* get_cpu(int cpu)
232{
233 gint i;
234 struct cputime *tmp;
235
236 for (i = 0; i < lttngtop.cpu_table->len; i++) {
237 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
238 if (tmp->id == cpu)
239 return tmp;
240 }
241
242 return add_cpu(cpu);
243}
244
245/*
246 * At the end of a sampling period, we need to display the cpu time for each
247 * process and to reset it to zero for the next period
248 */
249void rotate_cputime(unsigned long end)
250{
251 gint i;
252 struct cputime *tmp;
253 unsigned long elapsed;
254
255 for (i = 0; i < lttngtop.cpu_table->len; i++) {
256 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
257 elapsed = end - tmp->task_start;
258 if (tmp->current_task) {
259 tmp->current_task->totalcpunsec += elapsed;
260 tmp->current_task->threadstotalcpunsec += elapsed;
261 if (tmp->current_task->pid != tmp->current_task->tid &&
262 tmp->current_task->threadparent) {
263 tmp->current_task->threadparent->threadstotalcpunsec += elapsed;
264 }
265 }
266 tmp->task_start = end;
267 }
268}
269
270void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
271{
272 ((struct perfcounter*) value)->count = 0;
273}
274
275void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
276{
277 struct perfcounter *newperf;
59288610 278
559c9f86 279 newperf = g_new0(struct perfcounter, 1);
1fc22eb4
JD
280 newperf->count = ((struct perfcounter *) value)->count;
281 newperf->visible = ((struct perfcounter *) value)->visible;
282 newperf->sort = ((struct perfcounter *) value)->sort;
85db4618 283 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
1fc22eb4
JD
284}
285
30b646c4
JD
286void copy_process_table(gpointer key, gpointer value, gpointer new_table)
287{
288 g_hash_table_insert((GHashTable *) new_table, key, value);
289}
290
1fc22eb4
JD
291void rotate_perfcounter() {
292 int i;
293 struct processtop *tmp;
294 for (i = 0; i < lttngtop.process_table->len; i++) {
295 tmp = g_ptr_array_index(lttngtop.process_table, i);
296 g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL);
297 }
298}
299
300void cleanup_processtop()
301{
b093de8a 302 gint i, j;
1fc22eb4 303 struct processtop *tmp;
b093de8a 304 struct files *tmpf; /* a temporary file */
1fc22eb4
JD
305
306 for (i = 0; i < lttngtop.process_table->len; i++) {
307 tmp = g_ptr_array_index(lttngtop.process_table, i);
308 tmp->totalcpunsec = 0;
309 tmp->threadstotalcpunsec = 0;
b093de8a
MB
310 tmp->fileread = 0;
311 tmp->filewrite = 0;
312
313 for (j = 0; j < tmp->process_files_table->len; j++) {
314 tmpf = g_ptr_array_index(tmp->process_files_table, j);
315 if (tmpf != NULL) {
316 tmpf->read = 0;
317 tmpf->write = 0;
ceb3a221
MB
318
319 if (tmpf->flag == __NR_close)
320 g_ptr_array_index(
321 tmp->process_files_table, j
322 ) = NULL;
b093de8a
MB
323 }
324 }
1fc22eb4
JD
325 }
326}
327
e05a35a6
JD
328void reset_global_counters()
329{
330 lttngtop.nbnewproc = 0;
331 lttngtop.nbdeadproc = 0;
332 lttngtop.nbnewthreads = 0;
333 lttngtop.nbdeadthreads = 0;
334 lttngtop.nbnewfiles = 0;
335 lttngtop.nbclosedfiles = 0;
336}
337
338void copy_global_counters(struct lttngtop *dst)
339{
340 dst->nbproc = lttngtop.nbproc;
341 dst->nbnewproc = lttngtop.nbnewproc;
342 dst->nbdeadproc = lttngtop.nbdeadproc;
343 dst->nbthreads = lttngtop.nbthreads;
344 dst->nbnewthreads = lttngtop.nbnewthreads;
345 dst->nbdeadthreads = lttngtop.nbdeadthreads;
346 dst->nbfiles = lttngtop.nbfiles;
347 dst->nbnewfiles = lttngtop.nbnewfiles;
348 dst->nbclosedfiles = lttngtop.nbclosedfiles;
349 reset_global_counters();
350}
351
1fc22eb4
JD
352struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
353{
354 gint i, j;
355 unsigned long time;
356 struct lttngtop *dst;
357 struct processtop *tmp, *tmp2, *new;
358 struct cputime *tmpcpu, *newcpu;
359 struct files *tmpfile, *newfile;
360
559c9f86 361 dst = g_new0(struct lttngtop, 1);
1fc22eb4
JD
362 dst->start = start;
363 dst->end = end;
e05a35a6 364 copy_global_counters(dst);
1fc22eb4
JD
365 dst->process_table = g_ptr_array_new();
366 dst->files_table = g_ptr_array_new();
367 dst->cpu_table = g_ptr_array_new();
30b646c4
JD
368 dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
369 g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table,
370 dst->process_hash_table);
1fc22eb4
JD
371
372 rotate_cputime(end);
373
1fc22eb4
JD
374 for (i = 0; i < lttngtop.process_table->len; i++) {
375 tmp = g_ptr_array_index(lttngtop.process_table, i);
559c9f86 376 new = g_new0(struct processtop, 1);
1fc22eb4
JD
377
378 memcpy(new, tmp, sizeof(struct processtop));
379 new->threads = g_ptr_array_new();
380 new->comm = strdup(tmp->comm);
381 new->process_files_table = g_ptr_array_new();
ceb3a221 382 new->files_history = tmp->files_history;
85db4618 383 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
384 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
385
1fc22eb4 386 /* compute the stream speed */
85db4618
JD
387 if (end - start != 0) {
388 time = (end - start) / NSEC_PER_SEC;
b093de8a
MB
389 new->fileread = new->fileread/(time);
390 new->filewrite = new->filewrite/(time);
1fc22eb4
JD
391 }
392
393 for (j = 0; j < tmp->process_files_table->len; j++) {
394 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
1fc22eb4 395
b093de8a
MB
396 newfile = malloc(sizeof(struct files));
397
398 if (tmpfile != NULL) {
399 memcpy(newfile, tmpfile, sizeof(struct files));
400 newfile->name = strdup(tmpfile->name);
401 newfile->ref = new;
402 g_ptr_array_add(new->process_files_table,
403 newfile);
404 g_ptr_array_add(dst->files_table, newfile);
405 } else {
406 g_ptr_array_add(new->process_files_table, NULL);
407 g_ptr_array_add(dst->files_table, NULL);
408 }
1fc22eb4
JD
409 /*
410 * if the process died during the last period, we remove all
411 * files associated with if after the copy
412 */
413 if (tmp->death > 0 && tmp->death < end) {
e05a35a6 414 /* FIXME : close the files before */
1fc22eb4 415 g_ptr_array_remove(tmp->process_files_table, tmpfile);
559c9f86 416 g_free(tmpfile);
1fc22eb4
JD
417 }
418 }
419 g_ptr_array_add(dst->process_table, new);
420
421 /*
422 * if the process died during the last period, we remove it from
423 * the current process list after the copy
424 */
425 if (tmp->death > 0 && tmp->death < end) {
426 g_ptr_array_remove(lttngtop.process_table, tmp);
85db4618 427 /* FIXME : TRUE does not mean clears the object in it */
1fc22eb4
JD
428 g_ptr_array_free(tmp->threads, TRUE);
429 free(tmp->comm);
430 g_ptr_array_free(tmp->process_files_table, TRUE);
85db4618 431 /* FIXME : clear elements */
1fc22eb4 432 g_hash_table_destroy(tmp->perf);
559c9f86 433 g_free(tmp);
1fc22eb4
JD
434 }
435 }
436 rotate_perfcounter();
437
438 for (i = 0; i < lttngtop.cpu_table->len; i++) {
439 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
559c9f86 440 newcpu = g_new0(struct cputime, 1);
1fc22eb4 441 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
85db4618 442 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
443 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
444 /*
445 * note : we don't care about the current process pointer in the copy
446 * so the reference is invalid after the memcpy
447 */
448 g_ptr_array_add(dst->cpu_table, newcpu);
449 }
85db4618 450 /* FIXME : better algo */
1fc22eb4
JD
451 /* create the threads index if required */
452 for (i = 0; i < dst->process_table->len; i++) {
453 tmp = g_ptr_array_index(dst->process_table, i);
454 if (tmp->pid == tmp->tid) {
455 for (j = 0; j < dst->process_table->len; j++) {
456 tmp2 = g_ptr_array_index(dst->process_table, j);
457 if (tmp2->pid == tmp->pid) {
458 tmp2->threadparent = tmp;
459 g_ptr_array_add(tmp->threads, tmp2);
460 }
461 }
462 }
463 }
464
465 // update_global_stats(dst);
466 cleanup_processtop();
467
468 return dst;
469}
470
928f18a6
MB
471
472enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
473 void *private_data)
474{
2e0a1190 475 const struct bt_definition *scope;
928f18a6
MB
476 struct processtop *proc;
477 unsigned long timestamp;
478 int64_t pid, tid;
479 char *procname;
480
c78f2cdc 481 timestamp = bt_ctf_get_timestamp(call_data);
928f18a6
MB
482 if (timestamp == -1ULL)
483 goto error;
484
485 scope = bt_ctf_get_top_level_scope(call_data,
486 BT_EVENT_FIELDS);
487 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
488 scope, "_pid"));
489 if (bt_ctf_field_get_error()) {
490 fprintf(stderr, "Missing pid context info\n");
491 goto error;
492 }
493
494 scope = bt_ctf_get_top_level_scope(call_data,
495 BT_EVENT_FIELDS);
496 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
497 scope, "_tid"));
498 if (bt_ctf_field_get_error()) {
499 fprintf(stderr, "Missing tid context info\n");
500 goto error;
501 }
502
503 /*
504 * FIXME
505 * I first tried with bt_ctf_get_string but doesn`t work at all
506 * It couldn`t find the field _name because it is an integer in
507 * the metadata and not a string like _filename for the
508 * statedump_file_descriptor
509 */
510 scope = bt_ctf_get_top_level_scope(call_data,
511 BT_EVENT_FIELDS);
512 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
513 scope, "_name"));
514 if (bt_ctf_field_get_error()) {
515 fprintf(stderr, "Missing process name context info\n");
516 goto error;
517 }
518
519 proc = find_process_tid(&lttngtop, tid, procname);
520 if (proc == NULL)
521 proc = add_proc(&lttngtop, tid, procname, timestamp);
522
523 free(proc->comm);
524 proc->comm = strdup(procname);
525 proc->pid = pid;
526
527 /*
528 * FIXME
529 * I would like to free procname because it is duplicated
530 * when the process is created but it segfaults...
531 *
532 * free(procname);
533 */
534
535 return BT_CB_OK;
536
537error:
538 return BT_CB_ERROR_STOP;
539}
b520ab45
JD
540
541struct tm format_timestamp(uint64_t timestamp)
542{
543 struct tm tm;
544 uint64_t ts_sec = 0, ts_nsec;
545 time_t time_s;
546
547 ts_nsec = timestamp;
548 ts_sec += ts_nsec / NSEC_PER_SEC;
549 ts_nsec = ts_nsec % NSEC_PER_SEC;
550
551 time_s = (time_t) ts_sec;
552
553 localtime_r(&time_s, &tm);
554
555 return tm;
556}
This page took 0.077525 seconds and 4 git commands to generate.