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");
149 * To get the parent process, put the pid in the tid field
150 * because the parent process gets pid = tid
152 struct processtop
*find_process_tid(struct lttngtop
*ctx
, int tid
, char *comm
)
154 struct processtop
*tmp
;
156 tmp
= g_hash_table_lookup(ctx
->process_hash_table
,
157 (gconstpointer
) (unsigned long) tid
);
162 struct processtop
* add_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
163 unsigned long timestamp
)
165 struct processtop
*newproc
;
167 if (opt_pid
&& tid
!= opt_pid
)
170 /* if the PID already exists, we just rename the process */
171 /* FIXME : need to integrate with clone/fork/exit to be accurate */
172 newproc
= find_process_tid(ctx
, tid
, comm
);
175 newproc
= g_new0(struct processtop
, 1);
177 newproc
->birth
= timestamp
;
178 newproc
->process_files_table
= g_ptr_array_new();
179 newproc
->files_history
= NULL
;
180 newproc
->totalfileread
= 0;
181 newproc
->totalfilewrite
= 0;
182 newproc
->fileread
= 0;
183 newproc
->filewrite
= 0;
184 newproc
->syscall_info
= NULL
;
185 newproc
->threadparent
= NULL
;
186 newproc
->threads
= g_ptr_array_new();
187 newproc
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
188 g_ptr_array_add(ctx
->process_table
, newproc
);
189 g_hash_table_insert(ctx
->process_hash_table
,
190 (gpointer
) (unsigned long) tid
, newproc
);
195 newproc
->comm
= strdup(comm
);
200 struct processtop
* update_proc(struct processtop
* proc
, int pid
, int tid
,
201 int ppid
, int vpid
, int vtid
, int vppid
, char *comm
)
210 if (strcmp(proc
->comm
, comm
) != 0) {
212 proc
->comm
= strdup(comm
);
219 * This function just sets the time of death of a process.
220 * When we rotate the cputime we remove it from the process list.
222 void death_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
223 unsigned long timestamp
)
225 struct processtop
*tmp
;
226 tmp
= find_process_tid(ctx
, tid
, comm
);
228 g_hash_table_remove(ctx
->process_hash_table
,
229 (gpointer
) (unsigned long) tid
);
230 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0) {
231 tmp
->death
= timestamp
;
232 ctx
->nbdeadthreads
++;
237 struct processtop
* get_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
238 unsigned long timestamp
)
240 struct processtop
*tmp
;
241 tmp
= find_process_tid(ctx
, tid
, comm
);
242 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0)
244 return add_proc(ctx
, tid
, comm
, timestamp
);
247 struct processtop
*get_proc_pid(struct lttngtop
*ctx
, int tid
, int pid
,
248 unsigned long timestamp
)
250 struct processtop
*tmp
;
251 tmp
= find_process_tid(ctx
, tid
, NULL
);
252 if (tmp
&& tmp
->pid
== pid
)
254 return add_proc(ctx
, tid
, "Unknown", timestamp
);
257 void add_thread(struct processtop
*parent
, struct processtop
*thread
)
260 struct processtop
*tmp
;
265 for (i
= 0; i
< parent
->threads
->len
; i
++) {
266 tmp
= g_ptr_array_index(parent
->threads
, i
);
270 g_ptr_array_add(parent
->threads
, thread
);
273 struct cputime
* add_cpu(int cpu
)
275 struct cputime
*newcpu
;
277 newcpu
= g_new0(struct cputime
, 1);
279 newcpu
->current_task
= NULL
;
280 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
282 g_ptr_array_add(lttngtop
.cpu_table
, newcpu
);
286 struct cputime
* get_cpu(int cpu
)
291 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
292 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
301 * At the end of a sampling period, we need to display the cpu time for each
302 * process and to reset it to zero for the next period
304 void rotate_cputime(unsigned long end
)
308 unsigned long elapsed
;
310 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
311 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
312 elapsed
= end
- tmp
->task_start
;
313 if (tmp
->current_task
) {
314 tmp
->current_task
->totalcpunsec
+= elapsed
;
315 tmp
->current_task
->threadstotalcpunsec
+= elapsed
;
316 if (tmp
->current_task
->pid
!= tmp
->current_task
->tid
&&
317 tmp
->current_task
->threadparent
) {
318 tmp
->current_task
->threadparent
->threadstotalcpunsec
+= elapsed
;
321 tmp
->task_start
= end
;
325 void reset_perf_counter(gpointer key
, gpointer value
, gpointer user_data
)
327 ((struct perfcounter
*) value
)->count
= 0;
330 void copy_perf_counter(gpointer key
, gpointer value
, gpointer new_table
)
332 struct perfcounter
*newperf
;
334 newperf
= g_new0(struct perfcounter
, 1);
335 newperf
->count
= ((struct perfcounter
*) value
)->count
;
336 newperf
->visible
= ((struct perfcounter
*) value
)->visible
;
337 newperf
->sort
= ((struct perfcounter
*) value
)->sort
;
338 g_hash_table_insert((GHashTable
*) new_table
, strdup(key
), newperf
);
341 void copy_process_table(gpointer key
, gpointer value
, gpointer new_table
)
343 g_hash_table_insert((GHashTable
*) new_table
, key
, value
);
346 void rotate_perfcounter() {
348 struct processtop
*tmp
;
349 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
350 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
351 g_hash_table_foreach(tmp
->perf
, reset_perf_counter
, NULL
);
355 void cleanup_processtop()
358 struct processtop
*tmp
;
359 struct files
*tmpf
; /* a temporary file */
361 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
362 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
363 tmp
->totalcpunsec
= 0;
364 tmp
->threadstotalcpunsec
= 0;
368 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
369 tmpf
= g_ptr_array_index(tmp
->process_files_table
, j
);
374 if (tmpf
->flag
== __NR_close
)
376 tmp
->process_files_table
, j
383 void reset_global_counters()
385 lttngtop
.nbnewproc
= 0;
386 lttngtop
.nbdeadproc
= 0;
387 lttngtop
.nbnewthreads
= 0;
388 lttngtop
.nbdeadthreads
= 0;
389 lttngtop
.nbnewfiles
= 0;
390 lttngtop
.nbclosedfiles
= 0;
393 void copy_global_counters(struct lttngtop
*dst
)
395 dst
->nbproc
= lttngtop
.nbproc
;
396 dst
->nbnewproc
= lttngtop
.nbnewproc
;
397 dst
->nbdeadproc
= lttngtop
.nbdeadproc
;
398 dst
->nbthreads
= lttngtop
.nbthreads
;
399 dst
->nbnewthreads
= lttngtop
.nbnewthreads
;
400 dst
->nbdeadthreads
= lttngtop
.nbdeadthreads
;
401 dst
->nbfiles
= lttngtop
.nbfiles
;
402 dst
->nbnewfiles
= lttngtop
.nbnewfiles
;
403 dst
->nbclosedfiles
= lttngtop
.nbclosedfiles
;
404 reset_global_counters();
407 struct lttngtop
* get_copy_lttngtop(unsigned long start
, unsigned long end
)
411 struct lttngtop
*dst
;
412 struct processtop
*tmp
, *tmp2
, *new;
413 struct cputime
*tmpcpu
, *newcpu
;
414 struct files
*tmpfile
, *newfile
;
416 dst
= g_new0(struct lttngtop
, 1);
419 copy_global_counters(dst
);
420 dst
->process_table
= g_ptr_array_new();
421 dst
->files_table
= g_ptr_array_new();
422 dst
->cpu_table
= g_ptr_array_new();
423 dst
->process_hash_table
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
424 g_hash_table_foreach(lttngtop
.process_hash_table
, copy_process_table
,
425 dst
->process_hash_table
);
429 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
430 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
431 new = g_new0(struct processtop
, 1);
433 memcpy(new, tmp
, sizeof(struct processtop
));
434 new->threads
= g_ptr_array_new();
435 new->comm
= strdup(tmp
->comm
);
436 new->process_files_table
= g_ptr_array_new();
437 new->files_history
= tmp
->files_history
;
438 new->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
439 g_hash_table_foreach(tmp
->perf
, copy_perf_counter
, new->perf
);
441 /* compute the stream speed */
442 if (end
- start
!= 0) {
443 time
= (end
- start
) / NSEC_PER_SEC
;
444 new->fileread
= new->fileread
/(time
);
445 new->filewrite
= new->filewrite
/(time
);
448 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
449 tmpfile
= g_ptr_array_index(tmp
->process_files_table
, j
);
451 newfile
= malloc(sizeof(struct files
));
453 if (tmpfile
!= NULL
) {
454 memcpy(newfile
, tmpfile
, sizeof(struct files
));
455 newfile
->name
= strdup(tmpfile
->name
);
457 g_ptr_array_add(new->process_files_table
,
459 g_ptr_array_add(dst
->files_table
, newfile
);
461 g_ptr_array_add(new->process_files_table
, NULL
);
462 g_ptr_array_add(dst
->files_table
, NULL
);
465 * if the process died during the last period, we remove all
466 * files associated with if after the copy
468 if (tmp
->death
> 0 && tmp
->death
< end
) {
469 /* FIXME : close the files before */
470 g_ptr_array_remove(tmp
->process_files_table
, tmpfile
);
474 g_ptr_array_add(dst
->process_table
, new);
477 * if the process died during the last period, we remove it from
478 * the current process list after the copy
480 if (tmp
->death
> 0 && tmp
->death
< end
) {
481 g_ptr_array_remove(lttngtop
.process_table
, tmp
);
482 /* FIXME : TRUE does not mean clears the object in it */
483 g_ptr_array_free(tmp
->threads
, TRUE
);
485 g_ptr_array_free(tmp
->process_files_table
, TRUE
);
486 /* FIXME : clear elements */
487 g_hash_table_destroy(tmp
->perf
);
491 rotate_perfcounter();
493 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
494 tmpcpu
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
495 newcpu
= g_new0(struct cputime
, 1);
496 memcpy(newcpu
, tmpcpu
, sizeof(struct cputime
));
497 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
498 g_hash_table_foreach(tmpcpu
->perf
, copy_perf_counter
, newcpu
->perf
);
500 * note : we don't care about the current process pointer in the copy
501 * so the reference is invalid after the memcpy
503 g_ptr_array_add(dst
->cpu_table
, newcpu
);
505 /* FIXME : better algo */
506 /* create the threads index if required */
507 for (i
= 0; i
< dst
->process_table
->len
; i
++) {
508 tmp
= g_ptr_array_index(dst
->process_table
, i
);
509 if (tmp
->pid
== tmp
->tid
) {
510 for (j
= 0; j
< dst
->process_table
->len
; j
++) {
511 tmp2
= g_ptr_array_index(dst
->process_table
, j
);
512 if (tmp2
->pid
== tmp
->pid
) {
513 tmp2
->threadparent
= tmp
;
514 g_ptr_array_add(tmp
->threads
, tmp2
);
520 // update_global_stats(dst);
521 cleanup_processtop();
527 enum bt_cb_ret
handle_statedump_process_state(struct bt_ctf_event
*call_data
,
530 const struct bt_definition
*scope
;
531 struct processtop
*proc
;
532 unsigned long timestamp
;
533 int64_t pid
, tid
, ppid
, vtid
, vpid
, vppid
;
536 timestamp
= bt_ctf_get_timestamp(call_data
);
537 if (timestamp
== -1ULL)
540 scope
= bt_ctf_get_top_level_scope(call_data
,
542 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
544 if (bt_ctf_field_get_error()) {
545 fprintf(stderr
, "Missing pid context info\n");
548 ppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
550 if (bt_ctf_field_get_error()) {
551 fprintf(stderr
, "Missing ppid context info\n");
554 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
556 if (bt_ctf_field_get_error()) {
557 fprintf(stderr
, "Missing tid context info\n");
560 vtid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
562 if (bt_ctf_field_get_error()) {
563 fprintf(stderr
, "Missing vtid context info\n");
566 vpid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
568 if (bt_ctf_field_get_error()) {
569 fprintf(stderr
, "Missing vpid context info\n");
572 vppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
574 if (bt_ctf_field_get_error()) {
575 fprintf(stderr
, "Missing vppid context info\n");
579 scope
= bt_ctf_get_top_level_scope(call_data
,
581 procname
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
583 if (bt_ctf_field_get_error()) {
584 fprintf(stderr
, "Missing process name context info\n");
588 proc
= find_process_tid(<tngtop
, tid
, procname
);
590 proc
= add_proc(<tngtop
, tid
, procname
, timestamp
);
591 update_proc(proc
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, procname
);
595 proc
->comm
= strdup(procname
);
602 return BT_CB_ERROR_STOP
;
605 struct tm
format_timestamp(uint64_t timestamp
)
608 uint64_t ts_sec
= 0, ts_nsec
;
612 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
613 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
615 time_s
= (time_t) ts_sec
;
617 localtime_r(&time_s
, &tm
);
This page took 0.041039 seconds and 4 git commands to generate.