f6c20c9c8dee3487dd480bfa37d6bb0cb570d2b4
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
)
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
);
207 newproc
->comm
= strdup(comm
);
212 struct processtop
* update_proc(struct processtop
* proc
, int pid
, int tid
,
213 int ppid
, int vpid
, int vtid
, int vppid
, char *comm
, char *hostname
)
222 if (strcmp(proc
->comm
, comm
) != 0) {
224 proc
->comm
= strdup(comm
);
227 if (proc
->hostname
&& strcmp(proc
->hostname
, hostname
) != 0) {
228 free(proc
->hostname
);
230 proc
->hostname
= strdup(hostname
);
237 * This function just sets the time of death of a process.
238 * When we rotate the cputime we remove it from the process list.
240 void death_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
241 unsigned long timestamp
)
243 struct processtop
*tmp
;
244 tmp
= find_process_tid(ctx
, tid
, comm
);
246 g_hash_table_remove(ctx
->process_hash_table
,
247 (gpointer
) (unsigned long) tid
);
248 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0) {
249 tmp
->death
= timestamp
;
250 ctx
->nbdeadthreads
++;
255 struct processtop
* get_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
256 unsigned long timestamp
)
258 struct processtop
*tmp
;
259 tmp
= find_process_tid(ctx
, tid
, comm
);
260 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0)
262 return add_proc(ctx
, tid
, comm
, timestamp
);
265 struct processtop
*get_proc_pid(struct lttngtop
*ctx
, int tid
, int pid
,
266 unsigned long timestamp
)
268 struct processtop
*tmp
;
269 tmp
= find_process_tid(ctx
, tid
, NULL
);
270 if (tmp
&& tmp
->pid
== pid
)
272 return add_proc(ctx
, tid
, "Unknown", timestamp
);
275 void add_thread(struct processtop
*parent
, struct processtop
*thread
)
278 struct processtop
*tmp
;
283 for (i
= 0; i
< parent
->threads
->len
; i
++) {
284 tmp
= g_ptr_array_index(parent
->threads
, i
);
288 g_ptr_array_add(parent
->threads
, thread
);
291 struct cputime
* add_cpu(int cpu
)
293 struct cputime
*newcpu
;
295 newcpu
= g_new0(struct cputime
, 1);
297 newcpu
->current_task
= NULL
;
298 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
300 g_ptr_array_add(lttngtop
.cpu_table
, newcpu
);
304 struct cputime
* get_cpu(int cpu
)
309 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
310 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
319 * At the end of a sampling period, we need to display the cpu time for each
320 * process and to reset it to zero for the next period
322 void rotate_cputime(unsigned long end
)
326 unsigned long elapsed
;
328 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
329 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
330 elapsed
= end
- tmp
->task_start
;
331 if (tmp
->current_task
) {
332 tmp
->current_task
->totalcpunsec
+= elapsed
;
333 tmp
->current_task
->threadstotalcpunsec
+= elapsed
;
334 if (tmp
->current_task
->pid
!= tmp
->current_task
->tid
&&
335 tmp
->current_task
->threadparent
) {
336 tmp
->current_task
->threadparent
->threadstotalcpunsec
+= elapsed
;
339 tmp
->task_start
= end
;
343 void reset_perf_counter(gpointer key
, gpointer value
, gpointer user_data
)
345 ((struct perfcounter
*) value
)->count
= 0;
348 void copy_perf_counter(gpointer key
, gpointer value
, gpointer new_table
)
350 struct perfcounter
*newperf
;
352 newperf
= g_new0(struct perfcounter
, 1);
353 newperf
->count
= ((struct perfcounter
*) value
)->count
;
354 newperf
->visible
= ((struct perfcounter
*) value
)->visible
;
355 newperf
->sort
= ((struct perfcounter
*) value
)->sort
;
356 g_hash_table_insert((GHashTable
*) new_table
, strdup(key
), newperf
);
359 void copy_process_table(gpointer key
, gpointer value
, gpointer new_table
)
361 g_hash_table_insert((GHashTable
*) new_table
, key
, value
);
364 void rotate_perfcounter() {
366 struct processtop
*tmp
;
367 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
368 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
369 g_hash_table_foreach(tmp
->perf
, reset_perf_counter
, NULL
);
373 void cleanup_processtop()
376 struct processtop
*tmp
;
377 struct files
*tmpf
; /* a temporary file */
379 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
380 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
381 tmp
->totalcpunsec
= 0;
382 tmp
->threadstotalcpunsec
= 0;
386 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
387 tmpf
= g_ptr_array_index(tmp
->process_files_table
, j
);
392 if (tmpf
->flag
== __NR_close
)
394 tmp
->process_files_table
, j
401 void reset_global_counters()
403 lttngtop
.nbnewproc
= 0;
404 lttngtop
.nbdeadproc
= 0;
405 lttngtop
.nbnewthreads
= 0;
406 lttngtop
.nbdeadthreads
= 0;
407 lttngtop
.nbnewfiles
= 0;
408 lttngtop
.nbclosedfiles
= 0;
411 void copy_global_counters(struct lttngtop
*dst
)
413 dst
->nbproc
= lttngtop
.nbproc
;
414 dst
->nbnewproc
= lttngtop
.nbnewproc
;
415 dst
->nbdeadproc
= lttngtop
.nbdeadproc
;
416 dst
->nbthreads
= lttngtop
.nbthreads
;
417 dst
->nbnewthreads
= lttngtop
.nbnewthreads
;
418 dst
->nbdeadthreads
= lttngtop
.nbdeadthreads
;
419 dst
->nbfiles
= lttngtop
.nbfiles
;
420 dst
->nbnewfiles
= lttngtop
.nbnewfiles
;
421 dst
->nbclosedfiles
= lttngtop
.nbclosedfiles
;
422 reset_global_counters();
425 struct lttngtop
* get_copy_lttngtop(unsigned long start
, unsigned long end
)
429 struct lttngtop
*dst
;
430 struct processtop
*tmp
, *tmp2
, *new;
431 struct cputime
*tmpcpu
, *newcpu
;
432 struct files
*tmpfile
, *newfile
;
434 dst
= g_new0(struct lttngtop
, 1);
437 copy_global_counters(dst
);
438 dst
->process_table
= g_ptr_array_new();
439 dst
->files_table
= g_ptr_array_new();
440 dst
->cpu_table
= g_ptr_array_new();
441 dst
->process_hash_table
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
442 g_hash_table_foreach(lttngtop
.process_hash_table
, copy_process_table
,
443 dst
->process_hash_table
);
447 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
448 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
449 new = g_new0(struct processtop
, 1);
451 memcpy(new, tmp
, sizeof(struct processtop
));
452 new->threads
= g_ptr_array_new();
453 new->comm
= strdup(tmp
->comm
);
454 new->process_files_table
= g_ptr_array_new();
455 new->files_history
= tmp
->files_history
;
456 new->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
457 g_hash_table_foreach(tmp
->perf
, copy_perf_counter
, new->perf
);
459 /* compute the stream speed */
460 if (end
- start
!= 0) {
461 time
= (end
- start
) / NSEC_PER_SEC
;
462 new->fileread
= new->fileread
/(time
);
463 new->filewrite
= new->filewrite
/(time
);
466 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
467 tmpfile
= g_ptr_array_index(tmp
->process_files_table
, j
);
469 newfile
= malloc(sizeof(struct files
));
471 if (tmpfile
!= NULL
) {
472 memcpy(newfile
, tmpfile
, sizeof(struct files
));
473 newfile
->name
= strdup(tmpfile
->name
);
475 g_ptr_array_add(new->process_files_table
,
477 g_ptr_array_add(dst
->files_table
, newfile
);
479 g_ptr_array_add(new->process_files_table
, NULL
);
480 g_ptr_array_add(dst
->files_table
, NULL
);
483 * if the process died during the last period, we remove all
484 * files associated with if after the copy
486 if (tmp
->death
> 0 && tmp
->death
< end
) {
487 /* FIXME : close the files before */
488 g_ptr_array_remove(tmp
->process_files_table
, tmpfile
);
492 g_ptr_array_add(dst
->process_table
, new);
495 * if the process died during the last period, we remove it from
496 * the current process list after the copy
498 if (tmp
->death
> 0 && tmp
->death
< end
) {
499 g_ptr_array_remove(lttngtop
.process_table
, tmp
);
500 /* FIXME : TRUE does not mean clears the object in it */
501 g_ptr_array_free(tmp
->threads
, TRUE
);
503 g_ptr_array_free(tmp
->process_files_table
, TRUE
);
504 /* FIXME : clear elements */
505 g_hash_table_destroy(tmp
->perf
);
509 rotate_perfcounter();
511 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
512 tmpcpu
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
513 newcpu
= g_new0(struct cputime
, 1);
514 memcpy(newcpu
, tmpcpu
, sizeof(struct cputime
));
515 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
516 g_hash_table_foreach(tmpcpu
->perf
, copy_perf_counter
, newcpu
->perf
);
518 * note : we don't care about the current process pointer in the copy
519 * so the reference is invalid after the memcpy
521 g_ptr_array_add(dst
->cpu_table
, newcpu
);
523 /* FIXME : better algo */
524 /* create the threads index if required */
525 for (i
= 0; i
< dst
->process_table
->len
; i
++) {
526 tmp
= g_ptr_array_index(dst
->process_table
, i
);
527 if (tmp
->pid
== tmp
->tid
) {
528 for (j
= 0; j
< dst
->process_table
->len
; j
++) {
529 tmp2
= g_ptr_array_index(dst
->process_table
, j
);
530 if (tmp2
->pid
== tmp
->pid
) {
531 tmp2
->threadparent
= tmp
;
532 g_ptr_array_add(tmp
->threads
, tmp2
);
538 // update_global_stats(dst);
539 cleanup_processtop();
545 enum bt_cb_ret
handle_statedump_process_state(struct bt_ctf_event
*call_data
,
548 const struct bt_definition
*scope
;
549 struct processtop
*proc
;
550 unsigned long timestamp
;
551 int64_t pid
, tid
, ppid
, vtid
, vpid
, vppid
;
554 timestamp
= bt_ctf_get_timestamp(call_data
);
555 if (timestamp
== -1ULL)
558 scope
= bt_ctf_get_top_level_scope(call_data
,
560 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
562 if (bt_ctf_field_get_error()) {
563 fprintf(stderr
, "Missing pid context info\n");
566 ppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
568 if (bt_ctf_field_get_error()) {
569 fprintf(stderr
, "Missing ppid context info\n");
572 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
574 if (bt_ctf_field_get_error()) {
575 fprintf(stderr
, "Missing tid context info\n");
578 vtid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
580 if (bt_ctf_field_get_error()) {
581 fprintf(stderr
, "Missing vtid context info\n");
584 vpid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
586 if (bt_ctf_field_get_error()) {
587 fprintf(stderr
, "Missing vpid context info\n");
590 vppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
592 if (bt_ctf_field_get_error()) {
593 fprintf(stderr
, "Missing vppid context info\n");
597 scope
= bt_ctf_get_top_level_scope(call_data
,
599 procname
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
601 if (bt_ctf_field_get_error()) {
602 fprintf(stderr
, "Missing process name context info\n");
606 proc
= find_process_tid(<tngtop
, tid
, procname
);
608 proc
= add_proc(<tngtop
, tid
, procname
, timestamp
);
609 update_proc(proc
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, procname
, NULL
);
613 proc
->comm
= strdup(procname
);
620 return BT_CB_ERROR_STOP
;
623 struct tm
format_timestamp(uint64_t timestamp
)
626 uint64_t ts_sec
= 0, ts_nsec
;
630 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
631 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
633 time_s
= (time_t) ts_sec
;
635 localtime_r(&time_s
, &tm
);
640 int *lookup_tid_list(int tid
)
642 return g_hash_table_lookup(tid_list
, (gpointer
) &tid
);
645 char *lookup_hostname_list(const char *hostname
)
650 return g_hash_table_lookup(hostname_list
, (gpointer
) hostname
);
This page took 0.041446 seconds and 4 git commands to generate.