f4974e77a3bb0c64f1638c4ed3bd22ba0c41b8ed
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 the PID already exists, we just rename the process */
168 /* FIXME : need to integrate with clone/fork/exit to be accurate */
169 newproc
= find_process_tid(ctx
, tid
, comm
);
171 newproc
= g_new0(struct processtop
, 1);
173 newproc
->birth
= timestamp
;
174 newproc
->process_files_table
= g_ptr_array_new();
175 newproc
->files_history
= NULL
;
176 newproc
->totalfileread
= 0;
177 newproc
->totalfilewrite
= 0;
178 newproc
->fileread
= 0;
179 newproc
->filewrite
= 0;
180 newproc
->syscall_info
= NULL
;
181 newproc
->threadparent
= NULL
;
182 newproc
->threads
= g_ptr_array_new();
183 newproc
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
184 g_ptr_array_add(ctx
->process_table
, newproc
);
185 g_hash_table_insert(ctx
->process_hash_table
,
186 (gpointer
) (unsigned long) tid
, newproc
);
191 newproc
->comm
= strdup(comm
);
196 struct processtop
* update_proc(struct processtop
* proc
, int pid
, int tid
,
197 int ppid
, int vpid
, int vtid
, int vppid
, char *comm
)
206 if (strcmp(proc
->comm
, comm
) != 0) {
208 proc
->comm
= strdup(comm
);
215 * This function just sets the time of death of a process.
216 * When we rotate the cputime we remove it from the process list.
218 void death_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
219 unsigned long timestamp
)
221 struct processtop
*tmp
;
222 tmp
= find_process_tid(ctx
, tid
, comm
);
224 g_hash_table_remove(ctx
->process_hash_table
,
225 (gpointer
) (unsigned long) tid
);
226 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0) {
227 tmp
->death
= timestamp
;
228 ctx
->nbdeadthreads
++;
233 struct processtop
* get_proc(struct lttngtop
*ctx
, int tid
, char *comm
,
234 unsigned long timestamp
)
236 struct processtop
*tmp
;
237 tmp
= find_process_tid(ctx
, tid
, comm
);
238 if (tmp
&& strcmp(tmp
->comm
, comm
) == 0)
240 return add_proc(ctx
, tid
, comm
, timestamp
);
243 struct processtop
*get_proc_pid(struct lttngtop
*ctx
, int tid
, int pid
,
244 unsigned long timestamp
)
246 struct processtop
*tmp
;
247 tmp
= find_process_tid(ctx
, tid
, NULL
);
248 if (tmp
&& tmp
->pid
== pid
)
250 return add_proc(ctx
, tid
, "Unknown", timestamp
);
253 void add_thread(struct processtop
*parent
, struct processtop
*thread
)
256 struct processtop
*tmp
;
258 for (i
= 0; i
< parent
->threads
->len
; i
++) {
259 tmp
= g_ptr_array_index(parent
->threads
, i
);
263 g_ptr_array_add(parent
->threads
, thread
);
266 struct cputime
* add_cpu(int cpu
)
268 struct cputime
*newcpu
;
270 newcpu
= g_new0(struct cputime
, 1);
272 newcpu
->current_task
= NULL
;
273 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
275 g_ptr_array_add(lttngtop
.cpu_table
, newcpu
);
279 struct cputime
* get_cpu(int cpu
)
284 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
285 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
294 * At the end of a sampling period, we need to display the cpu time for each
295 * process and to reset it to zero for the next period
297 void rotate_cputime(unsigned long end
)
301 unsigned long elapsed
;
303 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
304 tmp
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
305 elapsed
= end
- tmp
->task_start
;
306 if (tmp
->current_task
) {
307 tmp
->current_task
->totalcpunsec
+= elapsed
;
308 tmp
->current_task
->threadstotalcpunsec
+= elapsed
;
309 if (tmp
->current_task
->pid
!= tmp
->current_task
->tid
&&
310 tmp
->current_task
->threadparent
) {
311 tmp
->current_task
->threadparent
->threadstotalcpunsec
+= elapsed
;
314 tmp
->task_start
= end
;
318 void reset_perf_counter(gpointer key
, gpointer value
, gpointer user_data
)
320 ((struct perfcounter
*) value
)->count
= 0;
323 void copy_perf_counter(gpointer key
, gpointer value
, gpointer new_table
)
325 struct perfcounter
*newperf
;
327 newperf
= g_new0(struct perfcounter
, 1);
328 newperf
->count
= ((struct perfcounter
*) value
)->count
;
329 newperf
->visible
= ((struct perfcounter
*) value
)->visible
;
330 newperf
->sort
= ((struct perfcounter
*) value
)->sort
;
331 g_hash_table_insert((GHashTable
*) new_table
, strdup(key
), newperf
);
334 void copy_process_table(gpointer key
, gpointer value
, gpointer new_table
)
336 g_hash_table_insert((GHashTable
*) new_table
, key
, value
);
339 void rotate_perfcounter() {
341 struct processtop
*tmp
;
342 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
343 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
344 g_hash_table_foreach(tmp
->perf
, reset_perf_counter
, NULL
);
348 void cleanup_processtop()
351 struct processtop
*tmp
;
352 struct files
*tmpf
; /* a temporary file */
354 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
355 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
356 tmp
->totalcpunsec
= 0;
357 tmp
->threadstotalcpunsec
= 0;
361 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
362 tmpf
= g_ptr_array_index(tmp
->process_files_table
, j
);
367 if (tmpf
->flag
== __NR_close
)
369 tmp
->process_files_table
, j
376 void reset_global_counters()
378 lttngtop
.nbnewproc
= 0;
379 lttngtop
.nbdeadproc
= 0;
380 lttngtop
.nbnewthreads
= 0;
381 lttngtop
.nbdeadthreads
= 0;
382 lttngtop
.nbnewfiles
= 0;
383 lttngtop
.nbclosedfiles
= 0;
386 void copy_global_counters(struct lttngtop
*dst
)
388 dst
->nbproc
= lttngtop
.nbproc
;
389 dst
->nbnewproc
= lttngtop
.nbnewproc
;
390 dst
->nbdeadproc
= lttngtop
.nbdeadproc
;
391 dst
->nbthreads
= lttngtop
.nbthreads
;
392 dst
->nbnewthreads
= lttngtop
.nbnewthreads
;
393 dst
->nbdeadthreads
= lttngtop
.nbdeadthreads
;
394 dst
->nbfiles
= lttngtop
.nbfiles
;
395 dst
->nbnewfiles
= lttngtop
.nbnewfiles
;
396 dst
->nbclosedfiles
= lttngtop
.nbclosedfiles
;
397 reset_global_counters();
400 struct lttngtop
* get_copy_lttngtop(unsigned long start
, unsigned long end
)
404 struct lttngtop
*dst
;
405 struct processtop
*tmp
, *tmp2
, *new;
406 struct cputime
*tmpcpu
, *newcpu
;
407 struct files
*tmpfile
, *newfile
;
409 dst
= g_new0(struct lttngtop
, 1);
412 copy_global_counters(dst
);
413 dst
->process_table
= g_ptr_array_new();
414 dst
->files_table
= g_ptr_array_new();
415 dst
->cpu_table
= g_ptr_array_new();
416 dst
->process_hash_table
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
417 g_hash_table_foreach(lttngtop
.process_hash_table
, copy_process_table
,
418 dst
->process_hash_table
);
422 for (i
= 0; i
< lttngtop
.process_table
->len
; i
++) {
423 tmp
= g_ptr_array_index(lttngtop
.process_table
, i
);
424 new = g_new0(struct processtop
, 1);
426 memcpy(new, tmp
, sizeof(struct processtop
));
427 new->threads
= g_ptr_array_new();
428 new->comm
= strdup(tmp
->comm
);
429 new->process_files_table
= g_ptr_array_new();
430 new->files_history
= tmp
->files_history
;
431 new->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
432 g_hash_table_foreach(tmp
->perf
, copy_perf_counter
, new->perf
);
434 /* compute the stream speed */
435 if (end
- start
!= 0) {
436 time
= (end
- start
) / NSEC_PER_SEC
;
437 new->fileread
= new->fileread
/(time
);
438 new->filewrite
= new->filewrite
/(time
);
441 for (j
= 0; j
< tmp
->process_files_table
->len
; j
++) {
442 tmpfile
= g_ptr_array_index(tmp
->process_files_table
, j
);
444 newfile
= malloc(sizeof(struct files
));
446 if (tmpfile
!= NULL
) {
447 memcpy(newfile
, tmpfile
, sizeof(struct files
));
448 newfile
->name
= strdup(tmpfile
->name
);
450 g_ptr_array_add(new->process_files_table
,
452 g_ptr_array_add(dst
->files_table
, newfile
);
454 g_ptr_array_add(new->process_files_table
, NULL
);
455 g_ptr_array_add(dst
->files_table
, NULL
);
458 * if the process died during the last period, we remove all
459 * files associated with if after the copy
461 if (tmp
->death
> 0 && tmp
->death
< end
) {
462 /* FIXME : close the files before */
463 g_ptr_array_remove(tmp
->process_files_table
, tmpfile
);
467 g_ptr_array_add(dst
->process_table
, new);
470 * if the process died during the last period, we remove it from
471 * the current process list after the copy
473 if (tmp
->death
> 0 && tmp
->death
< end
) {
474 g_ptr_array_remove(lttngtop
.process_table
, tmp
);
475 /* FIXME : TRUE does not mean clears the object in it */
476 g_ptr_array_free(tmp
->threads
, TRUE
);
478 g_ptr_array_free(tmp
->process_files_table
, TRUE
);
479 /* FIXME : clear elements */
480 g_hash_table_destroy(tmp
->perf
);
484 rotate_perfcounter();
486 for (i
= 0; i
< lttngtop
.cpu_table
->len
; i
++) {
487 tmpcpu
= g_ptr_array_index(lttngtop
.cpu_table
, i
);
488 newcpu
= g_new0(struct cputime
, 1);
489 memcpy(newcpu
, tmpcpu
, sizeof(struct cputime
));
490 newcpu
->perf
= g_hash_table_new(g_str_hash
, g_str_equal
);
491 g_hash_table_foreach(tmpcpu
->perf
, copy_perf_counter
, newcpu
->perf
);
493 * note : we don't care about the current process pointer in the copy
494 * so the reference is invalid after the memcpy
496 g_ptr_array_add(dst
->cpu_table
, newcpu
);
498 /* FIXME : better algo */
499 /* create the threads index if required */
500 for (i
= 0; i
< dst
->process_table
->len
; i
++) {
501 tmp
= g_ptr_array_index(dst
->process_table
, i
);
502 if (tmp
->pid
== tmp
->tid
) {
503 for (j
= 0; j
< dst
->process_table
->len
; j
++) {
504 tmp2
= g_ptr_array_index(dst
->process_table
, j
);
505 if (tmp2
->pid
== tmp
->pid
) {
506 tmp2
->threadparent
= tmp
;
507 g_ptr_array_add(tmp
->threads
, tmp2
);
513 // update_global_stats(dst);
514 cleanup_processtop();
520 enum bt_cb_ret
handle_statedump_process_state(struct bt_ctf_event
*call_data
,
523 const struct bt_definition
*scope
;
524 struct processtop
*proc
;
525 unsigned long timestamp
;
526 int64_t pid
, tid
, ppid
, vtid
, vpid
, vppid
;
529 timestamp
= bt_ctf_get_timestamp(call_data
);
530 if (timestamp
== -1ULL)
533 scope
= bt_ctf_get_top_level_scope(call_data
,
535 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
537 if (bt_ctf_field_get_error()) {
538 fprintf(stderr
, "Missing pid context info\n");
541 ppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
543 if (bt_ctf_field_get_error()) {
544 fprintf(stderr
, "Missing ppid context info\n");
547 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
549 if (bt_ctf_field_get_error()) {
550 fprintf(stderr
, "Missing tid context info\n");
553 vtid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
555 if (bt_ctf_field_get_error()) {
556 fprintf(stderr
, "Missing vtid context info\n");
559 vpid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
561 if (bt_ctf_field_get_error()) {
562 fprintf(stderr
, "Missing vpid context info\n");
565 vppid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
567 if (bt_ctf_field_get_error()) {
568 fprintf(stderr
, "Missing vppid context info\n");
572 scope
= bt_ctf_get_top_level_scope(call_data
,
574 procname
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
576 if (bt_ctf_field_get_error()) {
577 fprintf(stderr
, "Missing process name context info\n");
581 proc
= find_process_tid(<tngtop
, tid
, procname
);
583 proc
= add_proc(<tngtop
, tid
, procname
, timestamp
);
584 update_proc(proc
, pid
, tid
, ppid
, vpid
, vtid
, vppid
, procname
);
587 proc
->comm
= strdup(procname
);
593 return BT_CB_ERROR_STOP
;
596 struct tm
format_timestamp(uint64_t timestamp
)
599 uint64_t ts_sec
= 0, ts_nsec
;
603 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
604 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
606 time_s
= (time_t) ts_sec
;
608 localtime_r(&time_s
, &tm
);
This page took 0.039388 seconds and 3 git commands to generate.