2 * Copyright (C) 2011-2012 Julien Desfossez
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;
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.
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.
18 #include <babeltrace/ctf/events.h>
20 #include <linux/unistd.h>
24 uint64_t get_cpu_id(const struct bt_ctf_event
*event
)
26 const struct bt_definition
*scope
;
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");
39 uint64_t get_context_tid(const struct bt_ctf_event
*event
)
41 const struct bt_definition
*scope
;
44 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
45 tid
= bt_ctf_get_int64(bt_ctf_get_field(event
,
47 if (bt_ctf_field_get_error()) {
48 fprintf(stderr
, "Missing tid context info\n");
55 uint64_t get_context_pid(const struct bt_ctf_event
*event
)
57 const struct bt_definition
*scope
;
60 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
61 pid
= bt_ctf_get_int64(bt_ctf_get_field(event
,
63 if (bt_ctf_field_get_error()) {
64 fprintf(stderr
, "Missing pid context info\n");
71 uint64_t get_context_ppid(const struct bt_ctf_event
*event
)
73 const struct bt_definition
*scope
;
76 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
77 ppid
= bt_ctf_get_int64(bt_ctf_get_field(event
,
79 if (bt_ctf_field_get_error()) {
80 fprintf(stderr
, "Missing ppid context info\n");
87 uint64_t get_context_vtid(const struct bt_ctf_event
*event
)
89 const struct definition
*scope
;
92 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
93 vtid
= bt_ctf_get_int64(bt_ctf_get_field(event
,
95 if (bt_ctf_field_get_error()) {
102 uint64_t get_context_vpid(const struct bt_ctf_event
*event
)
104 const struct definition
*scope
;
107 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
108 vpid
= bt_ctf_get_int64(bt_ctf_get_field(event
,
110 if (bt_ctf_field_get_error()) {
117 uint64_t get_context_vppid(const struct bt_ctf_event
*event
)
119 const struct definition
*scope
;
122 scope
= bt_ctf_get_top_level_scope(event
, BT_STREAM_EVENT_CONTEXT
);
123 vppid
= bt_ctf_get_int64(bt_ctf_get_field(event
,
125 if (bt_ctf_field_get_error()) {
132 char *get_context_comm(const struct bt_ctf_event
*event
)
134 const struct bt_definition
*scope
;
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");
148 char *get_context_hostname(const struct bt_ctf_event
*event
)
150 const struct definition
*scope
;
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()) {
164 * To get the parent process, put the pid in the tid field
165 * because the parent process gets pid = tid
167 struct processtop
*find_process_tid(struct lttngtop
*ctx
, int tid
, char *comm
)
169 struct processtop
*tmp
;
171 tmp
= g_hash_table_lookup(ctx
->process_hash_table
,
172 (gconstpointer
) (unsigned long) tid
);
177 struct processtop
* add_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
178 unsigned long timestamp
, char *hostname
)
180 struct processtop
*newproc
;
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
);
187 newproc
= g_new0(struct processtop
, 1);
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
);
209 newproc
->comm
= strdup(comm
);
211 if (newproc
->hostname
&& strcmp(newproc
->hostname
, hostname
) != 0) {
212 free(newproc
->hostname
);
214 newproc
->hostname
= strdup(hostname
);
215 if (is_hostname_filtered(hostname
)) {
216 add_filter_tid_list(tid
, newproc
);
223 struct processtop
* update_proc(struct processtop
* proc
, int pid
, int tid
,
224 int ppid
, int vpid
, int vtid
, int vppid
, char *comm
, char *hostname
)
233 if (strcmp(proc
->comm
, comm
) != 0) {
235 proc
->comm
= strdup(comm
);
237 if (hostname
&& !proc
->hostname
) {
238 proc
->hostname
= strdup(hostname
);
239 if (is_hostname_filtered(hostname
)) {
240 add_filter_tid_list(tid
, proc
);
248 * This function just sets the time of death of a process.
249 * When we rotate the cputime we remove it from the process list.
251 void death_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
252 unsigned long timestamp
)
254 struct processtop
*tmp
;
255 tmp
= find_process_tid(ctx
, tid
, comm
);
257 g_hash_table_remove(ctx
->process_hash_table
,
258 (gpointer
) (unsigned long) tid
);
259 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0) {
260 tmp
->death
= timestamp
;
261 ctx
->nbdeadthreads
++;
266 struct processtop
* get_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
267 unsigned long timestamp
, char *hostname
)
269 struct processtop
*tmp
;
271 tmp
= find_process_tid(ctx
, tid
, comm
);
272 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0) {
275 return add_proc(ctx
, tid
, comm
, timestamp
, hostname
);
278 struct processtop
*get_proc_pid(struct lttngtop
*ctx
, int tid
, int pid
,
279 unsigned long timestamp
, char *hostname
)
281 struct processtop
*tmp
;
282 tmp
= find_process_tid(ctx
, tid
, NULL
);
283 if (tmp
&& tmp
->pid
== pid
)
285 return add_proc(ctx
, tid
, "Unknown", timestamp
, hostname
);
288 void add_thread(struct processtop
*parent
, struct processtop
*thread
)
291 struct processtop
*tmp
;
296 for (i
= 0; i
< parent
->threads
->len
; i
++) {
297 tmp
= g_ptr_array_index(parent
->threads
, i
);
301 g_ptr_array_add(parent
->threads
, thread
);
304 struct cputime
* add_cpu(int cpu
)
306 struct cputime
*newcpu
;
308 newcpu
= g_new0(struct cputime
, 1);
310 newcpu
->current_task
= NULL
;
311 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
313 g_ptr_array_add(lttngtop
.cpu_table
, newcpu
);
317 struct cputime
* get_cpu(int cpu
)
322 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
323 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
332 * At the end of a sampling period, we need to display the cpu time for each
333 * process and to reset it to zero for the next period
335 void rotate_cputime(unsigned long end
)
339 unsigned long elapsed
;
341 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
342 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
343 elapsed
= end
- tmp
->task_start
;
344 if (tmp
->current_task
) {
345 tmp
->current_task
->totalcpunsec
+= elapsed
;
346 tmp
->current_task
->threadstotalcpunsec
+= elapsed
;
347 if (tmp
->current_task
->pid
!= tmp
->current_task
->tid
&&
348 tmp
->current_task
->threadparent
) {
349 tmp
->current_task
->threadparent
->threadstotalcpunsec
+= elapsed
;
352 tmp
->task_start
= end
;
356 void reset_perf_counter(gpointer key
, gpointer value
, gpointer user_data
)
358 ((struct perfcounter
*) value
)->count
= 0;
361 void copy_perf_counter(gpointer key
, gpointer value
, gpointer new_table
)
363 struct perfcounter
*newperf
;
365 newperf
= g_new0(struct perfcounter
, 1);
366 newperf
->count
= ((struct perfcounter
*) value
)->count
;
367 newperf
->visible
= ((struct perfcounter
*) value
)->visible
;
368 newperf
->sort
= ((struct perfcounter
*) value
)->sort
;
369 g_hash_table_insert((GHashTable
*) new_table
, strdup(key
), newperf
);
372 void copy_process_table(gpointer key
, gpointer value
, gpointer new_table
)
374 g_hash_table_insert((GHashTable
*) new_table
, key
, value
);
377 void rotate_perfcounter() {
379 struct processtop
*tmp
;
380 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
381 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
382 g_hash_table_foreach(tmp
->perf
, reset_perf_counter
, NULL
);
386 void cleanup_processtop()
389 struct processtop
*tmp
;
390 struct files
*tmpf
; /* a temporary file */
392 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
393 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
394 tmp
->totalcpunsec
= 0;
395 tmp
->threadstotalcpunsec
= 0;
399 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
400 tmpf
= g_ptr_array_index(tmp
->process_files_table
, j
);
405 if (tmpf
->flag
== __NR_close
)
407 tmp
->process_files_table
, j
414 void reset_global_counters()
416 lttngtop
.nbnewproc
= 0;
417 lttngtop
.nbdeadproc
= 0;
418 lttngtop
.nbnewthreads
= 0;
419 lttngtop
.nbdeadthreads
= 0;
420 lttngtop
.nbnewfiles
= 0;
421 lttngtop
.nbclosedfiles
= 0;
424 void copy_global_counters(struct lttngtop
*dst
)
426 dst
->nbproc
= lttngtop
.nbproc
;
427 dst
->nbnewproc
= lttngtop
.nbnewproc
;
428 dst
->nbdeadproc
= lttngtop
.nbdeadproc
;
429 dst
->nbthreads
= lttngtop
.nbthreads
;
430 dst
->nbnewthreads
= lttngtop
.nbnewthreads
;
431 dst
->nbdeadthreads
= lttngtop
.nbdeadthreads
;
432 dst
->nbfiles
= lttngtop
.nbfiles
;
433 dst
->nbnewfiles
= lttngtop
.nbnewfiles
;
434 dst
->nbclosedfiles
= lttngtop
.nbclosedfiles
;
435 reset_global_counters();
438 struct lttngtop
* get_copy_lttngtop(unsigned long start
, unsigned long end
)
442 struct lttngtop
*dst
;
443 struct processtop
*tmp
, *tmp2
, *new;
444 struct cputime
*tmpcpu
, *newcpu
;
445 struct files
*tmpfile
, *newfile
;
446 struct kprobes
*tmpprobe
, *newprobe
;
448 dst
= g_new0(struct lttngtop
, 1);
451 copy_global_counters(dst
);
452 dst
->process_table
= g_ptr_array_new();
453 dst
->files_table
= g_ptr_array_new();
454 dst
->cpu_table
= g_ptr_array_new();
455 dst
->kprobes_table
= g_ptr_array_new();
456 dst
->process_hash_table
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
457 g_hash_table_foreach(lttngtop
.process_hash_table
, copy_process_table
,
458 dst
->process_hash_table
);
462 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
463 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
464 new = g_new0(struct processtop
, 1);
466 memcpy(new, tmp
, sizeof(struct processtop
));
467 new->threads
= g_ptr_array_new();
468 new->comm
= strdup(tmp
->comm
);
469 new->process_files_table
= g_ptr_array_new();
470 new->files_history
= tmp
->files_history
;
471 new->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
472 g_hash_table_foreach(tmp
->perf
, copy_perf_counter
, new->perf
);
474 /* compute the stream speed */
475 if (end
- start
!= 0) {
476 time
= (end
- start
) / NSEC_PER_SEC
;
477 new->fileread
= new->fileread
/(time
);
478 new->filewrite
= new->filewrite
/(time
);
481 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
482 tmpfile
= g_ptr_array_index(tmp
->process_files_table
, j
);
484 newfile
= malloc(sizeof(struct files
));
486 if (tmpfile
!= NULL
) {
487 memcpy(newfile
, tmpfile
, sizeof(struct files
));
488 newfile
->name
= strdup(tmpfile
->name
);
490 g_ptr_array_add(new->process_files_table
,
492 g_ptr_array_add(dst
->files_table
, newfile
);
494 g_ptr_array_add(new->process_files_table
, NULL
);
495 g_ptr_array_add(dst
->files_table
, NULL
);
498 * if the process died during the last period, we remove all
499 * files associated with if after the copy
501 if (tmp
->death
> 0 && tmp
->death
< end
) {
502 /* FIXME : close the files before */
503 g_ptr_array_remove(tmp
->process_files_table
, tmpfile
);
507 g_ptr_array_add(dst
->process_table
, new);
510 * if the process died during the last period, we remove it from
511 * the current process list after the copy
513 if (tmp
->death
> 0 && tmp
->death
< end
) {
514 g_ptr_array_remove(lttngtop
.process_table
, tmp
);
515 /* FIXME : TRUE does not mean clears the object in it */
516 g_ptr_array_free(tmp
->threads
, TRUE
);
518 g_ptr_array_free(tmp
->process_files_table
, TRUE
);
519 /* FIXME : clear elements */
520 g_hash_table_destroy(tmp
->perf
);
524 rotate_perfcounter();
526 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
527 tmpcpu
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
528 newcpu
= g_new0(struct cputime
, 1);
529 memcpy(newcpu
, tmpcpu
, sizeof(struct cputime
));
530 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
531 g_hash_table_foreach(tmpcpu
->perf
, copy_perf_counter
, newcpu
->perf
);
533 * note : we don't care about the current process pointer in the copy
534 * so the reference is invalid after the memcpy
536 g_ptr_array_add(dst
->cpu_table
, newcpu
);
538 if (lttngtop
.kprobes_table
) {
539 for (i
= 0; i
< lttngtop
.kprobes_table
->len
; i
++) {
540 tmpprobe
= g_ptr_array_index(lttngtop
.kprobes_table
, i
);
541 newprobe
= g_new0(struct kprobes
, 1);
542 memcpy(newprobe
, tmpprobe
, sizeof(struct kprobes
));
544 g_ptr_array_add(dst
->kprobes_table
, newprobe
);
547 /* FIXME : better algo */
548 /* create the threads index if required */
549 for (i
= 0; i
< dst
->process_table
->len
; i
++) {
550 tmp
= g_ptr_array_index(dst
->process_table
, i
);
551 if (tmp
->pid
== tmp
->tid
) {
552 for (j
= 0; j
< dst
->process_table
->len
; j
++) {
553 tmp2
= g_ptr_array_index(dst
->process_table
, j
);
554 if (tmp2
->pid
== tmp
->pid
) {
555 tmp2
->threadparent
= tmp
;
556 g_ptr_array_add(tmp
->threads
, tmp2
);
562 // update_global_stats(dst);
563 cleanup_processtop();
569 enum bt_cb_ret
handle_statedump_process_state(struct bt_ctf_event
*call_data
,
572 const struct bt_definition
*scope
;
573 struct processtop
*proc
;
574 unsigned long timestamp
;
575 int64_t pid
, tid
, ppid
, vtid
, vpid
, vppid
;
576 char *procname
, *hostname
= NULL
;
578 timestamp
= bt_ctf_get_timestamp(call_data
);
579 if (timestamp
== -1ULL)
582 scope
= bt_ctf_get_top_level_scope(call_data
,
584 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
586 if (bt_ctf_field_get_error()) {
587 fprintf(stderr
, "Missing pid context info\n");
590 ppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
592 if (bt_ctf_field_get_error()) {
593 fprintf(stderr
, "Missing ppid context info\n");
596 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
598 if (bt_ctf_field_get_error()) {
599 fprintf(stderr
, "Missing tid context info\n");
602 vtid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
604 if (bt_ctf_field_get_error()) {
605 fprintf(stderr
, "Missing vtid context info\n");
608 vpid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
610 if (bt_ctf_field_get_error()) {
611 fprintf(stderr
, "Missing vpid context info\n");
614 vppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
616 if (bt_ctf_field_get_error()) {
617 fprintf(stderr
, "Missing vppid context info\n");
621 scope
= bt_ctf_get_top_level_scope(call_data
,
623 procname
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
625 if (bt_ctf_field_get_error()) {
626 fprintf(stderr
, "Missing process name context info\n");
630 hostname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
631 scope, "_hostname"));
632 if (bt_ctf_field_get_error()) {
636 proc
= find_process_tid(<tngtop
, tid
, procname
);
638 proc
= add_proc(<tngtop
, tid
, procname
, timestamp
, hostname
);
639 update_proc(proc
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, procname
, hostname
);
643 proc
->comm
= strdup(procname
);
650 return BT_CB_ERROR_STOP
;
653 struct tm
format_timestamp(uint64_t timestamp
)
656 uint64_t ts_sec
= 0, ts_nsec
;
660 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
661 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
663 time_s
= (time_t) ts_sec
;
665 localtime_r(&time_s
, &tm
);
670 int *lookup_tid_list(int tid
)
672 if (!tid_filter_list
)
675 return g_hash_table_lookup(tid_filter_list
, (gpointer
) &tid
);
678 struct host
*lookup_hostname_list(const char *hostname
)
680 if (!hostname
|| !global_host_list
)
683 return g_hash_table_lookup(global_host_list
, (gpointer
) hostname
);
686 int is_hostname_filtered(const char *hostname
)
690 host
= lookup_hostname_list(hostname
);
696 int *lookup_filter_tid_list(int tid
)
698 return g_hash_table_lookup(global_filter_list
, (gpointer
) &tid
);
701 void add_filter_tid_list(int tid
, struct processtop
*newproc
)
703 unsigned long *hash_tid
;
705 hash_tid
= malloc(sizeof(unsigned long));
707 g_hash_table_insert(global_filter_list
,
708 (gpointer
) (unsigned long) hash_tid
, newproc
);
711 void remove_filter_tid_list(int tid
)
713 g_hash_table_remove(global_filter_list
,
714 (gpointer
) (unsigned long) &tid
);
This page took 0.044487 seconds and 5 git commands to generate.