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.
19 #include <babeltrace/babeltrace.h>
22 #include "lttngtoptypes.h"
26 void update_cputop_data(unsigned long timestamp
, int64_t cpu
,
27 int prev_pid
, int next_pid
, char *prev_comm
, char *next_comm
)
30 Quark current_task_quark
;
31 bool current_task_found
;
34 unsigned long elapsed
;
35 unsigned long task_start
;
36 bool current_task_threadparent_found
;
37 Quark current_task_threadparent
;
39 cpu_quark
= get_cpu(cpu
, timestamp
);
40 current_task_found
= get_current_attribute_value_quark(&cpu_quark
,
41 "current_task", current_task_quark
);
43 if (current_task_found
) {
44 get_current_attribute_value_int(¤t_task_quark
, "pid",
46 if (current_task_pid
== prev_pid
) {
47 get_current_attribute_value_ulong(&cpu_quark
,
48 "task_start", task_start
);
49 elapsed
= timestamp
- task_start
;
50 increase_attribute(timestamp
, ¤t_task_quark
,
51 "totalcpunsec", elapsed
);
52 increase_attribute(timestamp
, ¤t_task_quark
,
53 "threadstotalcpunsec", elapsed
);
54 get_current_attribute_value_int(¤t_task_quark
,
55 "tid", current_task_tid
);
56 if (current_task_tid
!= current_task_pid
) {
57 current_task_threadparent_found
=
58 get_current_attribute_value_quark(
61 current_task_threadparent
);
62 if (current_task_threadparent_found
)
63 increase_attribute(timestamp
,
64 ¤t_task_threadparent
,
65 "threadstotalcpuns", elapsed
);
71 modify_attribute(timestamp
, &cpu_quark
, "current_task",
72 get_proc(next_pid
, next_comm
, timestamp
));
74 nullify_attribute(timestamp
, &cpu_quark
, "current_task");
76 modify_attribute(timestamp
, &cpu_quark
, "task_start", timestamp
);
79 enum bt_cb_ret
handle_sched_switch(struct bt_ctf_event
*call_data
,
82 const struct definition
*scope
;
83 unsigned long timestamp
;
85 char *prev_comm
, *next_comm
;
86 int prev_tid
, next_tid
;
88 timestamp
= bt_ctf_get_timestamp(call_data
);
89 if (timestamp
== -1ULL)
92 scope
= bt_ctf_get_top_level_scope(call_data
,
94 prev_comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
95 scope
, "_prev_comm"));
96 if (bt_ctf_field_get_error()) {
97 fprintf(stderr
, "Missing prev_comm context info\n");
101 next_comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
102 scope
, "_next_comm"));
103 if (bt_ctf_field_get_error()) {
104 fprintf(stderr
, "Missing next_comm context info\n");
108 prev_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
109 scope
, "_prev_tid"));
110 if (bt_ctf_field_get_error()) {
111 fprintf(stderr
, "Missing prev_tid context info\n");
115 next_tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
116 scope
, "_next_tid"));
117 if (bt_ctf_field_get_error()) {
118 fprintf(stderr
, "Missing next_tid context info\n");
122 cpu_id
= get_cpu_id(call_data
);
124 update_cputop_data(timestamp
, cpu_id
, prev_tid
, next_tid
,
125 prev_comm
, next_comm
);
130 return BT_CB_ERROR_STOP
;
133 enum bt_cb_ret
handle_sched_process_free(struct bt_ctf_event
*call_data
,
136 const struct definition
*scope
;
137 unsigned long timestamp
;
141 timestamp
= bt_ctf_get_timestamp(call_data
);
142 if (timestamp
== -1ULL)
145 scope
= bt_ctf_get_top_level_scope(call_data
,
147 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
149 if (bt_ctf_field_get_error()) {
150 fprintf(stderr
, "Missing procname context info\n");
154 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
156 if (bt_ctf_field_get_error()) {
157 fprintf(stderr
, "Missing tid field\n");
161 death_proc(tid
, comm
, timestamp
);
166 return BT_CB_ERROR_STOP
;