| 1 | /* |
| 2 | * Copyright (C) 2011-2012 Julien Desfossez |
| 3 | * |
| 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; |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #include <babeltrace/babeltrace.h> |
| 19 | |
| 20 | #include "lttngtoptypes.h" |
| 21 | #include "common.h" |
| 22 | #include "cputop.h" |
| 23 | #include "lttngtop.h" |
| 24 | |
| 25 | void update_cputop_data(unsigned long timestamp, int64_t cpu, int prev_pid, |
| 26 | int next_pid, char *prev_comm, char *next_comm, char *hostname) |
| 27 | { |
| 28 | struct cputime *tmpcpu; |
| 29 | unsigned long elapsed; |
| 30 | |
| 31 | tmpcpu = get_cpu(cpu); |
| 32 | |
| 33 | if (tmpcpu->current_task && tmpcpu->current_task->pid == prev_pid) { |
| 34 | elapsed = timestamp - tmpcpu->task_start; |
| 35 | tmpcpu->current_task->totalcpunsec += elapsed; |
| 36 | tmpcpu->current_task->threadstotalcpunsec += elapsed; |
| 37 | if (tmpcpu->current_task->threadparent && |
| 38 | tmpcpu->current_task->pid != tmpcpu->current_task->tid) |
| 39 | tmpcpu->current_task->threadparent->threadstotalcpunsec += elapsed; |
| 40 | } |
| 41 | |
| 42 | if (next_pid != 0) |
| 43 | tmpcpu->current_task = get_proc(<tngtop, next_pid, next_comm, |
| 44 | timestamp, hostname); |
| 45 | else |
| 46 | tmpcpu->current_task = NULL; |
| 47 | |
| 48 | tmpcpu->task_start = timestamp; |
| 49 | } |
| 50 | |
| 51 | enum bt_cb_ret handle_sched_switch(struct bt_ctf_event *call_data, |
| 52 | void *private_data) |
| 53 | { |
| 54 | const struct bt_definition *scope; |
| 55 | unsigned long timestamp; |
| 56 | uint64_t cpu_id; |
| 57 | char *prev_comm, *next_comm; |
| 58 | int prev_tid, next_tid; |
| 59 | char *hostname = NULL; |
| 60 | |
| 61 | timestamp = bt_ctf_get_timestamp(call_data); |
| 62 | if (timestamp == -1ULL) |
| 63 | goto error; |
| 64 | |
| 65 | scope = bt_ctf_get_top_level_scope(call_data, |
| 66 | BT_EVENT_FIELDS); |
| 67 | prev_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 68 | scope, "_prev_comm")); |
| 69 | if (bt_ctf_field_get_error()) { |
| 70 | fprintf(stderr, "Missing prev_comm context info\n"); |
| 71 | goto error; |
| 72 | } |
| 73 | |
| 74 | next_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 75 | scope, "_next_comm")); |
| 76 | if (bt_ctf_field_get_error()) { |
| 77 | fprintf(stderr, "Missing next_comm context info\n"); |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | prev_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 82 | scope, "_prev_tid")); |
| 83 | if (bt_ctf_field_get_error()) { |
| 84 | fprintf(stderr, "Missing prev_tid context info\n"); |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 89 | scope, "_next_tid")); |
| 90 | if (bt_ctf_field_get_error()) { |
| 91 | fprintf(stderr, "Missing next_tid context info\n"); |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | cpu_id = get_cpu_id(call_data); |
| 96 | |
| 97 | update_cputop_data(timestamp, cpu_id, prev_tid, next_tid, |
| 98 | prev_comm, next_comm, hostname); |
| 99 | |
| 100 | return BT_CB_OK; |
| 101 | |
| 102 | error: |
| 103 | return BT_CB_ERROR_STOP; |
| 104 | } |
| 105 | |
| 106 | enum bt_cb_ret handle_sched_process_free(struct bt_ctf_event *call_data, |
| 107 | void *private_data) |
| 108 | { |
| 109 | const struct bt_definition *scope; |
| 110 | unsigned long timestamp; |
| 111 | char *comm; |
| 112 | int tid; |
| 113 | |
| 114 | timestamp = bt_ctf_get_timestamp(call_data); |
| 115 | if (timestamp == -1ULL) |
| 116 | goto error; |
| 117 | |
| 118 | scope = bt_ctf_get_top_level_scope(call_data, |
| 119 | BT_EVENT_FIELDS); |
| 120 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 121 | scope, "_comm")); |
| 122 | if (bt_ctf_field_get_error()) { |
| 123 | fprintf(stderr, "Missing procname context info\n"); |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 128 | scope, "_tid")); |
| 129 | if (bt_ctf_field_get_error()) { |
| 130 | fprintf(stderr, "Missing tid field\n"); |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | death_proc(<tngtop, tid, comm, timestamp); |
| 135 | |
| 136 | return BT_CB_OK; |
| 137 | |
| 138 | error: |
| 139 | return BT_CB_ERROR_STOP; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | enum bt_cb_ret handle_sched_process_fork(struct bt_ctf_event *call_data, |
| 144 | void *private_data) |
| 145 | { |
| 146 | const struct bt_definition *scope; |
| 147 | struct processtop *tmp; |
| 148 | int tid, *hash_tid, parent_pid; |
| 149 | unsigned long timestamp; |
| 150 | char *comm; |
| 151 | |
| 152 | timestamp = bt_ctf_get_timestamp(call_data); |
| 153 | if (timestamp == -1ULL) |
| 154 | goto error; |
| 155 | |
| 156 | scope = bt_ctf_get_top_level_scope(call_data, |
| 157 | BT_EVENT_FIELDS); |
| 158 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 159 | scope, "_child_comm")); |
| 160 | if (bt_ctf_field_get_error()) { |
| 161 | fprintf(stderr, "Missing procname context info\n"); |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 166 | scope, "_child_tid")); |
| 167 | if (bt_ctf_field_get_error()) { |
| 168 | fprintf(stderr, "Missing child_tid field\n"); |
| 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | parent_pid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 173 | scope, "_parent_pid")); |
| 174 | if (bt_ctf_field_get_error()) { |
| 175 | fprintf(stderr, "Missing parent_pid field\n"); |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | tmp = get_proc(<tngtop, tid, comm, timestamp, NULL); |
| 180 | |
| 181 | if (opt_child) { |
| 182 | hash_tid = lookup_filter_tid_list(parent_pid); |
| 183 | if (hash_tid) { |
| 184 | add_filter_tid_list(tmp); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return BT_CB_OK; |
| 189 | |
| 190 | error: |
| 191 | return BT_CB_ERROR_STOP; |
| 192 | |
| 193 | } |
| 194 | |