filtering in GUI working
[lttngtop.git] / src / common.c
1 /*
2 * Copyright (C) 2011-2012 Julien Desfossez
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 *
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.
16 */
17
18 #include <babeltrace/ctf/events.h>
19 #include <stdlib.h>
20 #include <linux/unistd.h>
21 #include <string.h>
22 #include "common.h"
23
24 uint64_t get_cpu_id(const struct bt_ctf_event *event)
25 {
26 const struct bt_definition *scope;
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
39 uint64_t get_context_tid(const struct bt_ctf_event *event)
40 {
41 const struct bt_definition *scope;
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
55 uint64_t get_context_pid(const struct bt_ctf_event *event)
56 {
57 const struct bt_definition *scope;
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
71 uint64_t get_context_ppid(const struct bt_ctf_event *event)
72 {
73 const struct bt_definition *scope;
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
87 uint64_t get_context_vtid(const struct bt_ctf_event *event)
88 {
89 const struct definition *scope;
90 uint64_t vtid;
91
92 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
93 vtid = bt_ctf_get_int64(bt_ctf_get_field(event,
94 scope, "_vtid"));
95 if (bt_ctf_field_get_error()) {
96 return -1ULL;
97 }
98
99 return vtid;
100 }
101
102 uint64_t get_context_vpid(const struct bt_ctf_event *event)
103 {
104 const struct definition *scope;
105 uint64_t vpid;
106
107 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
108 vpid = bt_ctf_get_int64(bt_ctf_get_field(event,
109 scope, "_vpid"));
110 if (bt_ctf_field_get_error()) {
111 return -1ULL;
112 }
113
114 return vpid;
115 }
116
117 uint64_t get_context_vppid(const struct bt_ctf_event *event)
118 {
119 const struct definition *scope;
120 uint64_t vppid;
121
122 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
123 vppid = bt_ctf_get_int64(bt_ctf_get_field(event,
124 scope, "_vppid"));
125 if (bt_ctf_field_get_error()) {
126 return -1ULL;
127 }
128
129 return vppid;
130 }
131
132 char *get_context_comm(const struct bt_ctf_event *event)
133 {
134 const struct bt_definition *scope;
135 char *comm;
136
137 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
138 comm = bt_ctf_get_char_array(bt_ctf_get_field(event,
139 scope, "_procname"));
140 if (bt_ctf_field_get_error()) {
141 fprintf(stderr, "Missing comm context info\n");
142 return NULL;
143 }
144
145 return comm;
146 }
147
148 char *get_context_hostname(const struct bt_ctf_event *event)
149 {
150 const struct definition *scope;
151 char *hostname;
152
153 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
154 hostname = bt_ctf_get_char_array(bt_ctf_get_field(event,
155 scope, "_hostname"));
156 if (bt_ctf_field_get_error()) {
157 return NULL;
158 }
159
160 return hostname;
161 }
162
163 /*
164 * To get the parent process, put the pid in the tid field
165 * because the parent process gets pid = tid
166 */
167 struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
168 {
169 struct processtop *tmp;
170
171 tmp = g_hash_table_lookup(ctx->process_hash_table,
172 (gconstpointer) (unsigned long) tid);
173
174 return tmp;
175 }
176
177 struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
178 unsigned long timestamp)
179 {
180 struct processtop *newproc;
181
182 /* if the PID already exists, we just rename the process */
183 /* FIXME : need to integrate with clone/fork/exit to be accurate */
184 newproc = find_process_tid(ctx, tid, comm);
185
186 if (!newproc) {
187 newproc = g_new0(struct processtop, 1);
188 newproc->tid = tid;
189 newproc->birth = timestamp;
190 newproc->process_files_table = g_ptr_array_new();
191 newproc->files_history = NULL;
192 newproc->totalfileread = 0;
193 newproc->totalfilewrite = 0;
194 newproc->fileread = 0;
195 newproc->filewrite = 0;
196 newproc->syscall_info = NULL;
197 newproc->threadparent = NULL;
198 newproc->threads = g_ptr_array_new();
199 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
200 g_ptr_array_add(ctx->process_table, newproc);
201 g_hash_table_insert(ctx->process_hash_table,
202 (gpointer) (unsigned long) tid, newproc);
203 if (lookup_tid_list(tid)) {
204 add_filter_tid_list(tid, newproc);
205 }
206 ctx->nbnewthreads++;
207 ctx->nbthreads++;
208 }
209 newproc->comm = strdup(comm);
210
211 return newproc;
212 }
213
214 struct processtop* update_proc(struct processtop* proc, int pid, int tid,
215 int ppid, int vpid, int vtid, int vppid, char *comm, char *hostname)
216 {
217 if (proc) {
218 proc->pid = pid;
219 proc->tid = tid;
220 proc->ppid = ppid;
221 proc->vpid = vpid;
222 proc->vtid = vtid;
223 proc->vppid = vppid;
224 if (strcmp(proc->comm, comm) != 0) {
225 free(proc->comm);
226 proc->comm = strdup(comm);
227 }
228 if (hostname) {
229 if (proc->hostname && strcmp(proc->hostname, hostname) != 0) {
230 free(proc->hostname);
231 }
232 proc->hostname = strdup(hostname);
233 if (lookup_hostname_list(hostname)) {
234 add_filter_tid_list(tid, proc);
235 }
236 }
237 }
238 return proc;
239 }
240
241 /*
242 * This function just sets the time of death of a process.
243 * When we rotate the cputime we remove it from the process list.
244 */
245 void death_proc(struct lttngtop *ctx, int tid, char *comm,
246 unsigned long timestamp)
247 {
248 struct processtop *tmp;
249 tmp = find_process_tid(ctx, tid, comm);
250
251 g_hash_table_remove(ctx->process_hash_table,
252 (gpointer) (unsigned long) tid);
253 if (tmp && strcmp(tmp->comm, comm) == 0) {
254 tmp->death = timestamp;
255 ctx->nbdeadthreads++;
256 ctx->nbthreads--;
257 }
258 }
259
260 struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
261 unsigned long timestamp)
262 {
263 struct processtop *tmp;
264 tmp = find_process_tid(ctx, tid, comm);
265 if (tmp && strcmp(tmp->comm, comm) == 0)
266 return tmp;
267 return add_proc(ctx, tid, comm, timestamp);
268 }
269
270 struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid,
271 unsigned long timestamp)
272 {
273 struct processtop *tmp;
274 tmp = find_process_tid(ctx, tid, NULL);
275 if (tmp && tmp->pid == pid)
276 return tmp;
277 return add_proc(ctx, tid, "Unknown", timestamp);
278 }
279
280 void add_thread(struct processtop *parent, struct processtop *thread)
281 {
282 gint i;
283 struct processtop *tmp;
284
285 if (!parent)
286 return;
287
288 for (i = 0; i < parent->threads->len; i++) {
289 tmp = g_ptr_array_index(parent->threads, i);
290 if (tmp == thread)
291 return;
292 }
293 g_ptr_array_add(parent->threads, thread);
294 }
295
296 struct cputime* add_cpu(int cpu)
297 {
298 struct cputime *newcpu;
299
300 newcpu = g_new0(struct cputime, 1);
301 newcpu->id = cpu;
302 newcpu->current_task = NULL;
303 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
304
305 g_ptr_array_add(lttngtop.cpu_table, newcpu);
306
307 return newcpu;
308 }
309 struct cputime* get_cpu(int cpu)
310 {
311 gint i;
312 struct cputime *tmp;
313
314 for (i = 0; i < lttngtop.cpu_table->len; i++) {
315 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
316 if (tmp->id == cpu)
317 return tmp;
318 }
319
320 return add_cpu(cpu);
321 }
322
323 /*
324 * At the end of a sampling period, we need to display the cpu time for each
325 * process and to reset it to zero for the next period
326 */
327 void rotate_cputime(unsigned long end)
328 {
329 gint i;
330 struct cputime *tmp;
331 unsigned long elapsed;
332
333 for (i = 0; i < lttngtop.cpu_table->len; i++) {
334 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
335 elapsed = end - tmp->task_start;
336 if (tmp->current_task) {
337 tmp->current_task->totalcpunsec += elapsed;
338 tmp->current_task->threadstotalcpunsec += elapsed;
339 if (tmp->current_task->pid != tmp->current_task->tid &&
340 tmp->current_task->threadparent) {
341 tmp->current_task->threadparent->threadstotalcpunsec += elapsed;
342 }
343 }
344 tmp->task_start = end;
345 }
346 }
347
348 void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
349 {
350 ((struct perfcounter*) value)->count = 0;
351 }
352
353 void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
354 {
355 struct perfcounter *newperf;
356
357 newperf = g_new0(struct perfcounter, 1);
358 newperf->count = ((struct perfcounter *) value)->count;
359 newperf->visible = ((struct perfcounter *) value)->visible;
360 newperf->sort = ((struct perfcounter *) value)->sort;
361 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
362 }
363
364 void copy_process_table(gpointer key, gpointer value, gpointer new_table)
365 {
366 g_hash_table_insert((GHashTable *) new_table, key, value);
367 }
368
369 void rotate_perfcounter() {
370 int i;
371 struct processtop *tmp;
372 for (i = 0; i < lttngtop.process_table->len; i++) {
373 tmp = g_ptr_array_index(lttngtop.process_table, i);
374 g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL);
375 }
376 }
377
378 void cleanup_processtop()
379 {
380 gint i, j;
381 struct processtop *tmp;
382 struct files *tmpf; /* a temporary file */
383
384 for (i = 0; i < lttngtop.process_table->len; i++) {
385 tmp = g_ptr_array_index(lttngtop.process_table, i);
386 tmp->totalcpunsec = 0;
387 tmp->threadstotalcpunsec = 0;
388 tmp->fileread = 0;
389 tmp->filewrite = 0;
390
391 for (j = 0; j < tmp->process_files_table->len; j++) {
392 tmpf = g_ptr_array_index(tmp->process_files_table, j);
393 if (tmpf != NULL) {
394 tmpf->read = 0;
395 tmpf->write = 0;
396
397 if (tmpf->flag == __NR_close)
398 g_ptr_array_index(
399 tmp->process_files_table, j
400 ) = NULL;
401 }
402 }
403 }
404 }
405
406 void reset_global_counters()
407 {
408 lttngtop.nbnewproc = 0;
409 lttngtop.nbdeadproc = 0;
410 lttngtop.nbnewthreads = 0;
411 lttngtop.nbdeadthreads = 0;
412 lttngtop.nbnewfiles = 0;
413 lttngtop.nbclosedfiles = 0;
414 }
415
416 void copy_global_counters(struct lttngtop *dst)
417 {
418 dst->nbproc = lttngtop.nbproc;
419 dst->nbnewproc = lttngtop.nbnewproc;
420 dst->nbdeadproc = lttngtop.nbdeadproc;
421 dst->nbthreads = lttngtop.nbthreads;
422 dst->nbnewthreads = lttngtop.nbnewthreads;
423 dst->nbdeadthreads = lttngtop.nbdeadthreads;
424 dst->nbfiles = lttngtop.nbfiles;
425 dst->nbnewfiles = lttngtop.nbnewfiles;
426 dst->nbclosedfiles = lttngtop.nbclosedfiles;
427 reset_global_counters();
428 }
429
430 struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
431 {
432 gint i, j;
433 unsigned long time;
434 struct lttngtop *dst;
435 struct processtop *tmp, *tmp2, *new;
436 struct cputime *tmpcpu, *newcpu;
437 struct files *tmpfile, *newfile;
438 struct kprobes *tmpprobe, *newprobe;
439
440 dst = g_new0(struct lttngtop, 1);
441 dst->start = start;
442 dst->end = end;
443 copy_global_counters(dst);
444 dst->process_table = g_ptr_array_new();
445 dst->files_table = g_ptr_array_new();
446 dst->cpu_table = g_ptr_array_new();
447 dst->kprobes_table = g_ptr_array_new();
448 dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
449 g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table,
450 dst->process_hash_table);
451
452 rotate_cputime(end);
453
454 for (i = 0; i < lttngtop.process_table->len; i++) {
455 tmp = g_ptr_array_index(lttngtop.process_table, i);
456 new = g_new0(struct processtop, 1);
457
458 memcpy(new, tmp, sizeof(struct processtop));
459 new->threads = g_ptr_array_new();
460 new->comm = strdup(tmp->comm);
461 new->process_files_table = g_ptr_array_new();
462 new->files_history = tmp->files_history;
463 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
464 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
465
466 /* compute the stream speed */
467 if (end - start != 0) {
468 time = (end - start) / NSEC_PER_SEC;
469 new->fileread = new->fileread/(time);
470 new->filewrite = new->filewrite/(time);
471 }
472
473 for (j = 0; j < tmp->process_files_table->len; j++) {
474 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
475
476 newfile = malloc(sizeof(struct files));
477
478 if (tmpfile != NULL) {
479 memcpy(newfile, tmpfile, sizeof(struct files));
480 newfile->name = strdup(tmpfile->name);
481 newfile->ref = new;
482 g_ptr_array_add(new->process_files_table,
483 newfile);
484 g_ptr_array_add(dst->files_table, newfile);
485 } else {
486 g_ptr_array_add(new->process_files_table, NULL);
487 g_ptr_array_add(dst->files_table, NULL);
488 }
489 /*
490 * if the process died during the last period, we remove all
491 * files associated with if after the copy
492 */
493 if (tmp->death > 0 && tmp->death < end) {
494 /* FIXME : close the files before */
495 g_ptr_array_remove(tmp->process_files_table, tmpfile);
496 g_free(tmpfile);
497 }
498 }
499 g_ptr_array_add(dst->process_table, new);
500
501 /*
502 * if the process died during the last period, we remove it from
503 * the current process list after the copy
504 */
505 if (tmp->death > 0 && tmp->death < end) {
506 g_ptr_array_remove(lttngtop.process_table, tmp);
507 /* FIXME : TRUE does not mean clears the object in it */
508 g_ptr_array_free(tmp->threads, TRUE);
509 free(tmp->comm);
510 g_ptr_array_free(tmp->process_files_table, TRUE);
511 /* FIXME : clear elements */
512 g_hash_table_destroy(tmp->perf);
513 g_free(tmp);
514 }
515 }
516 rotate_perfcounter();
517
518 for (i = 0; i < lttngtop.cpu_table->len; i++) {
519 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
520 newcpu = g_new0(struct cputime, 1);
521 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
522 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
523 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
524 /*
525 * note : we don't care about the current process pointer in the copy
526 * so the reference is invalid after the memcpy
527 */
528 g_ptr_array_add(dst->cpu_table, newcpu);
529 }
530 if (lttngtop.kprobes_table) {
531 for (i = 0; i < lttngtop.kprobes_table->len; i++) {
532 tmpprobe = g_ptr_array_index(lttngtop.kprobes_table, i);
533 newprobe = g_new0(struct kprobes, 1);
534 memcpy(newprobe, tmpprobe, sizeof(struct kprobes));
535 tmpprobe->count = 0;
536 g_ptr_array_add(dst->kprobes_table, newprobe);
537 }
538 }
539 /* FIXME : better algo */
540 /* create the threads index if required */
541 for (i = 0; i < dst->process_table->len; i++) {
542 tmp = g_ptr_array_index(dst->process_table, i);
543 if (tmp->pid == tmp->tid) {
544 for (j = 0; j < dst->process_table->len; j++) {
545 tmp2 = g_ptr_array_index(dst->process_table, j);
546 if (tmp2->pid == tmp->pid) {
547 tmp2->threadparent = tmp;
548 g_ptr_array_add(tmp->threads, tmp2);
549 }
550 }
551 }
552 }
553
554 // update_global_stats(dst);
555 cleanup_processtop();
556
557 return dst;
558 }
559
560
561 enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
562 void *private_data)
563 {
564 const struct bt_definition *scope;
565 struct processtop *proc;
566 unsigned long timestamp;
567 int64_t pid, tid, ppid, vtid, vpid, vppid;
568 char *procname;
569
570 timestamp = bt_ctf_get_timestamp(call_data);
571 if (timestamp == -1ULL)
572 goto error;
573
574 scope = bt_ctf_get_top_level_scope(call_data,
575 BT_EVENT_FIELDS);
576 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
577 scope, "_pid"));
578 if (bt_ctf_field_get_error()) {
579 fprintf(stderr, "Missing pid context info\n");
580 goto error;
581 }
582 ppid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
583 scope, "_ppid"));
584 if (bt_ctf_field_get_error()) {
585 fprintf(stderr, "Missing ppid context info\n");
586 goto error;
587 }
588 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
589 scope, "_tid"));
590 if (bt_ctf_field_get_error()) {
591 fprintf(stderr, "Missing tid context info\n");
592 goto error;
593 }
594 vtid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
595 scope, "_vtid"));
596 if (bt_ctf_field_get_error()) {
597 fprintf(stderr, "Missing vtid context info\n");
598 goto error;
599 }
600 vpid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
601 scope, "_vpid"));
602 if (bt_ctf_field_get_error()) {
603 fprintf(stderr, "Missing vpid context info\n");
604 goto error;
605 }
606 vppid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
607 scope, "_vppid"));
608 if (bt_ctf_field_get_error()) {
609 fprintf(stderr, "Missing vppid context info\n");
610 goto error;
611 }
612
613 scope = bt_ctf_get_top_level_scope(call_data,
614 BT_EVENT_FIELDS);
615 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
616 scope, "_name"));
617 if (bt_ctf_field_get_error()) {
618 fprintf(stderr, "Missing process name context info\n");
619 goto error;
620 }
621
622 proc = find_process_tid(&lttngtop, tid, procname);
623 if (proc == NULL)
624 proc = add_proc(&lttngtop, tid, procname, timestamp);
625 update_proc(proc, pid, tid, ppid, vpid, vtid, vppid, procname, NULL);
626
627 if (proc) {
628 free(proc->comm);
629 proc->comm = strdup(procname);
630 proc->pid = pid;
631 }
632
633 return BT_CB_OK;
634
635 error:
636 return BT_CB_ERROR_STOP;
637 }
638
639 struct tm format_timestamp(uint64_t timestamp)
640 {
641 struct tm tm;
642 uint64_t ts_sec = 0, ts_nsec;
643 time_t time_s;
644
645 ts_nsec = timestamp;
646 ts_sec += ts_nsec / NSEC_PER_SEC;
647 ts_nsec = ts_nsec % NSEC_PER_SEC;
648
649 time_s = (time_t) ts_sec;
650
651 localtime_r(&time_s, &tm);
652
653 return tm;
654 }
655
656 int *lookup_tid_list(int tid)
657 {
658 if (!tid_list)
659 return NULL;
660
661 return g_hash_table_lookup(tid_list, (gpointer) &tid);
662 }
663
664 char *lookup_hostname_list(const char *hostname)
665 {
666 if (!hostname || !hostname_list)
667 return NULL;
668
669 return g_hash_table_lookup(hostname_list, (gpointer) hostname);
670 }
671
672 int *lookup_filter_tid_list(int tid)
673 {
674 return g_hash_table_lookup(global_filter_list, (gpointer) &tid);
675 }
676
677 void add_filter_tid_list(int tid, struct processtop *newproc)
678 {
679 unsigned long *hash_tid;
680
681 hash_tid = malloc(sizeof(unsigned long));
682 *hash_tid = tid;
683 g_hash_table_insert(global_filter_list,
684 (gpointer) (unsigned long) hash_tid, newproc);
685 }
686
687 void remove_filter_tid_list(int tid)
688 {
689 g_hash_table_remove(global_filter_list,
690 (gpointer) (unsigned long) &tid);
691 }
This page took 0.042338 seconds and 4 git commands to generate.